|
Led Linux device drivers #ifndef __KERNEL__ # define __KERNEL__ #endif #ifndef MODULE # define MODULE #endif #include #include #include #include #include / * printk () * / #include / * kmalloc () * / #include / * everything ... * / #include / * error codes * / #include / * size_t * / #include #include / * O_ACCMODE * / #include / * cOPY_TO_USER * / #include / * cli (), * _flags * / #include #define DEVICE_NAME "demo" #define led_MAJOR 212 #define led_MINOR 0 static int MAX_BUF_LEN = 1024; static char drv_buf [1024]; static int WRI_LENGTH = 0; static char combuf [2]; char base = 0x70; char off = 0x07; / ****** ************************************************** ***************************** / static ssize_t led_write (struct file * filp, const char * buffer, size_t count, loff_t * ppos ) { copy_from_user (drv_buf, buffer, count); combuf [0] = drv_buf [0]; combuf [1] = drv_buf [1]; WRI_LENGTH = count; printk ( "user write data to driver n"); IIC_WriteSerial (base, off, combuf, 2); return count; } / ****** ************************************************** ***************************** / static ssize_t led_read (struct file * filp, char * buffer, size_t count, loff_t * ppos) { return count; } / ***************************** ************************************************** ****** / static int led_ioctl (struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg) { printk ( "ioctl runing n" ); switch (cmd) { case 1: printk ( "runing command 1 n"); break; case 2: printk ( "runing command 2 n"); break; default: printk ( "error cmd number n"); break; } return 0; } / **************** ************************************************** ******************* / static int led_open (struct inode * inode, struct file * file) { sprintf (drv_buf, "device ! open sucess n "); printk (" device open sucess n ");! return 0; } / ************* ************************************************** ********************** / static int led_release (struct inode * inode, struct file * filp) { MOD_DEC_USE_COUNT; < BR> printk ( "device release n"); return 0; } / ************************ ************************************************** *********** / static struct file_operations demo_fops = { owner: THIS_MODULE, write: led_write, read: led_read, ioctl: led_ioctl, < BR> open: led_open, release: led_release, }; / **************************** ************************************************** ******* / #ifdef CONFIG_DEVFS_FS static devfs_handle_t devfs_demo_dir, devfs_demoraw; #endif / ****************** ************************************************** ***************** / static int __init led_init (void) { int result; SET_MODULE_OWNER (& demo_fops); result = register_chrdev (led_MAJOR, "demo", & demo_fops); if (result < 0) return result; printk (DEVICE_NAME "initialized n"); return 0; } / ******************************************** ***************************************** / static void __exit led_exit ( void) { unregister_chrdev (led_MAJOR, "demo"); // kfree (demo_devices); printk (DEVICE_NAME "unloaded n"); } / * ************************************************** ********************************** / module_init (led_init); module_exit (led_exit); ////////////////////////////////////////////// ////////////////////////////////
The following is a description of the procedures, are not part of the code: important data structure struct file data structure definitions at include / fs.h struct file structure associated with the drive member read and write permissions mode_t f_mode ID file loff_t f_pos current write position unsigned int_f_flag file mark, mainly for blocking / non-blocking operation inspection struct file_operation * f_op file operations structure pointer void * private_data driver will generally point it has allocated data struct dentry * f_dentry directory entry corresponding to the file structure device driver Interface (struct file_operations), marking methods:
static struct file_operations demo_fops = {
owner: THIS_MODULE,
write: demo_write,
read: demo_read,
ioctl: demo_ioctl,
open: demo_open,
release: demo_release,
};
Device Driver Interface (struct file_operations)
commonly referred to as a device driver interface means struct file_operations {}, its definition is located in include / linux / fs.h in.
in the development of embedded systems, usually just a few interface functions to achieve the following functions of the system will be able to complete the required
When init load the driver (insmod), the kernel automatically call
read from the device to read data P>
write write data to a character device
ioctl control equipment, and other control commands other than read and write operations
open to open the device and initialize
release off the device and release resources
exit uninstall the driver (rmmod), the kernel automatically call
Driver registration process (dynamically assigned major number)
insmod module_name; the driver is loaded, run init function (register_chrdev (dev_Major, "module_name", * fs))
Check / proc / devices
mknod / dev / module_name c / b major device number minor device number
rmmod module_name; uninstall the driver, run the exit function (unregister_chrdev (dev_Major, "module_name", * fs)) |
|
|
|