ledmod.c

来自「arm9200开发板LED驱动实现」· C语言 代码 · 共 102 行

C
102
字号
#include <linux/module.h> // Needed by all modules#include <linux/kernel.h> // Needed for KERN_ALERT#include <linux/init.h>   // Needed for the macros#include <asm/io.h>#include <linux/types.h>#include <linux/fcntl.h>#include <sys/syscall.h>#include <linux/errno.h>#include <linux/fs.h>#include <linux/unistd.h>#include <linux/wrapper.h>#include <asm/uaccess.h>#define led_major 42//#include <linux/modversions.h>MODULE_LICENSE("GPL");static int led_open(struct inode *,struct file *);static ssize_t led_read(struct file *,char *,size_t,loff_t *);static ssize_t led_write(struct file *,const char *,size_t,loff_t *);static int led_close(struct inode *,struct file *); 
static int led_open(struct inode *inode,struct file *file){ unsigned int *PIO_PER = 0; unsigned int *PIO_OER = 0; unsigned int *PIO_OWER = 0; PIO_PER = ioremap((unsigned long)0xfffff800,(unsigned long)1);  //I/O enable   PIO_OER = ioremap((unsigned long)0xfffff810,(unsigned long)1);  //output enable PIO_OWER = ioremap((unsigned long)0xfffff8a0,(unsigned long)1); //output write enable  *PIO_PER=0x40;  *PIO_OER=0x40;  *PIO_OWER=0X40;  return 0;}static ssize_t led_read(struct file *file,char *buffer,size_t count,loff_t *offset){ unsigned char *PIO_PDSR = 0; unsigned char a=0;
  PIO_PDSR = ioremap((unsigned long)0xfffff83c,(unsigned long)1); a=readb(PIO_PDSR); put_user(a,buffer); if((0x40&a)==0)printk(KERN_ALERT "current status is low\n"); if((a&0x40)==0x40)printk(KERN_ALERT "current status is high\n"); return (count);}static ssize_t led_write(struct file *file,const char *buffer,size_t count,loff_t *offset){unsigned char *PIO_ODSR = 0;
unsigned char value;PIO_ODSR = ioremap((unsigned long)0xfffff838,(unsigned long)1);	
get_user(value,buffer);writeb(value,PIO_ODSR);  return (count);}static int led_close(struct inode *inode,struct file *file){	return 0;}struct file_operations led_fops ={	open:led_open,	read:led_read,	write:led_write,	release:led_close,};int led_init(void){	int rc;	rc=register_chrdev(led_major,"led_device",&led_fops);	if (rc<0)	{		printk(KERN_WARNING "led:can't get major %d\n",led_major);		return rc;	}	printk(KERN_INFO "led:get major %d\n",led_major);	return 0;}	int init_module(void){	return led_init();}void cleanup_module(void){
	unregister_chrdev(led_major,"led_device");	printk(KERN_ALERT "Goodbye, world\n");}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?