?? drv.c
字號:
/* leddrv.c - Create an input/output character device */#include <linux/kernel.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/errno.h> /* for -EBUSY */#include <linux/ioport.h> /* for verify_area */#include <linux/init.h> /* for module_init */#include <asm/uaccess.h> /* for get_user and put_user */#include <asm-arm/arch-s3c2410/S3C2410.h>#include <asm-arm/io.h> //這個頭文件包含了io地址映射的函數#include "drv.h"#define DEVICE_NAME "char_dev"unsigned long *remapedadd; //用來保存重新映射后的虛擬地址的起始地址/************************************************************************************///下面這段代碼是從ADT的LEDDEMO中拷貝過來的static int Device_Open = 0;#define U8 unsigned charunsigned char seg7table[16] = { /* 0 1 2 3 4 5 6 7*/ 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, /* 8 9 A B C D E F*/ 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e,};static void Test_Seg7(unsigned char i) { if((i>=0)&&(i<=15)) { *(remapedadd+2) = 0x00; //outb(0x00,(remapedadd+2)); *remapedadd = seg7table[i]; // outb(i,remapedadd); }}/*************************************************************************************/int device_ioctl(struct inode *,struct file *,unsigned int ,unsigned long ); /* This function is called whenever a process attempts * to open the device file */static int device_open(struct inode *inode, struct file *file){ printk("Device Open\n"); /* We don't want to talk to two processes at the * same time */ if (Device_Open) return -EBUSY; Device_Open++; MOD_INC_USE_COUNT; return 0;}/* This function is called when a process closes the * device file. It doesn't have a return value because * it cannot fail. Regardless of what else happens, you * should always be able to close a device (in 2.0, a 2.2 * device file could be impossible to close). */static int device_release(struct inode *inode, struct file *file){ printk(("device_release\n")); /* We're now ready for our next caller */ Device_Open --; MOD_DEC_USE_COUNT; return 0;}//static int flags = 0xff;/* This function is called whenever a process which * has already opened the device file attempts to * read from it. */static ssize_t device_read( struct file *file, char *buffer, /* The buffer to fill with the data */ size_t length, /* The length of the buffer */ loff_t *offset) /* offset to the file */{ return 0;}/* This function is called when somebody tries to * write into our device file. */ static ssize_t device_write(struct file *file, const char *buffer, size_t length, loff_t *offset){ char value; int bytes_writes = 1; char* data = (char*)buffer; get_user(value, data); Test_Seg7(value); /* Again, return the number of input characters used */ return bytes_writes;};/* This function is called whenever a process tries to * do an ioctl on our device file. We get two extra * parameters (additional to the inode and file * structures, which all device functions get): the number * of the ioctl called and the parameter given to the * ioctl function. * * If the ioctl is write or read/write (meaning output * is returned to the calling process), the ioctl call * returns the output of this function. */int device_ioctl( struct inode *inode, struct file *file, unsigned int ioctl_num,/* The number of the ioctl */ unsigned long ioctl_param) /* The parameter to it */{ return 0;};/* Module Declarations *************************** *//* This structure will hold the functions to be called * when a process does something to the device we * created. Since a pointer to this structure is kept in * the devices table, it can't be local to * init_module. NULL is for unimplemented functions. */struct file_operations Fops = { .read = device_read, .write = device_write, .open = device_open, .release = device_release, .ioctl = device_ioctl };/* Initialize the module - Register the character device */static int __init led_module_init( void ){ int ret_val; /* Register the character device (atleast try) */ ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &Fops); /* Negative values signify an error */ if (ret_val < 0) { printk ("%s failed with %d\n", "Sorry, registering the character device ", ret_val); return ret_val; } printk ("%s The major device number is %d.\n", "Registeration is a success", MAJOR_NUM); remapedadd=ioremap(0x10000004,4); printk ("If you want to talk to the device driver,\n"); printk ("you'll have to create a device file. \n"); printk ("We suggest you use:\n"); printk ("mknod /dev/%s c %d 0\n", DEVICE_FILE_NAME,MAJOR_NUM); printk ("The device file name is important, because\n"); Test_Seg7(0) ; return 0;};/* Cleanup - unregister the appropriate file from /proc */static void __exit led_module_cleanup(void){ int ret;// release_region(LED_BASE, 12); /* Unregister the device */ ret = unregister_chrdev(MAJOR_NUM, DEVICE_NAME); /* If there's an error, report it */ if (ret < 0) printk("Error in module_unregister_chrdev: %d\n", ret); iounmap(remapedadd);};module_init(led_module_init);module_exit(led_module_cleanup);MODULE_LICENSE("GPL");MODULE_AUTHOR("gh");MODULE_DESCRIPTION("LED char driver");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -