Linux驱动设计——字符杂项设备
生活随笔
收集整理的這篇文章主要介紹了
Linux驱动设计——字符杂项设备
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
雜項設備
linux里面的misc雜項設備是主設備號為10的驅動設備,misc設備其實也就是特殊的字符設備,可自動生成設備節點。 定義頭文件<linux/miscdevice.h> 雜項設備的結構體: struct miscdevice{int minor; //雜項設備的此設備號(如果設置為MISC_DYNAMIC_MINOR,表示系統自動分配未使用的minor)const char *name;const stuct file_operations *fops;//驅動主題函數入口指針struct list_head list;struct device *parent;struct device *this device;const char *nodename;(在/dev下面創建的設備驅動節點)mode_t mode; };? 注冊和釋放 注冊:int misc_register(struct miscdevice *misc) 釋放:int misc_deregister(struct miscdevice *misc) misc_device是特殊字符設備。注冊驅動程序時采用misc_register函數注冊,此函數中會自動創建設備節點,即設備文件。無需mknod指令創建設備文件。因為misc_register()會調用class_device_creat或者device_creat(). 雜項字符設備和一般字符設備的區別: 1.一般字符設備首先申請設備號。 ?但是雜項字符設備的主設備號為10次設備號通過結構體struct miscdevice中的minor來設置。 2.一般字符設備要創建設備文件。 但是雜項字符設備在注冊時會自動創建。 3.一般字符設備要分配一個cdev(字符設備)。 ?但是雜項字符設備只要創建struct miscdevice結構即可。 4.一般字符設備需要初始化cdev(即給字符設備設置對應的操作函數集struct file_operation). 但是雜項字符設備在結構體truct miscdevice中定義。 5.一般字符設備使用注冊函數 int cdev_add struct (cdev *p,devt_t dev, unsigned)(第一個參數為之前初始化的字符設備,第二個參數為設備號,第三個參數為要添加設備的個數) 而雜項字符設備使用int misc_register(struct miscdevice *misc)來注冊 驅動調用的實質: 就是通過?設備文件找到與之對應設備號的設備,再通過設備初始化時綁定的操作函數對硬件進行控制的 1 #include <linux/miscdevice.h> 2 #include <linux/delay.h> 3 #include <asm/irq.h> 4 #include <mach/regs-gpio.h> 5 #include <mach/hardware.h> 6 #include <linux/kernel.h> 7 #include <linux/module.h> 8 #include <linux/init.h> 9 #include <linux/mm.h> 10 #include <linux/fs.h> 11 #include <linux/types.h> 12 #include <linux/delay.h> 13 #include <linux/moduleparam.h> 14 #include <linux/slab.h> 15 #include <linux/errno.h> 16 #include <linux/ioctl.h> 17 #include <linux/cdev.h> 18 #include <linux/string.h> 19 #include <linux/list.h> 20 #include <linux/pci.h> 21 #include <linux/gpio.h> 22 #include <asm/uaccess.h> 23 #include <asm/atomic.h> 24 #include <asm/unistd.h> 25 26 27 #define DEVICE_NAME "leds" 28 29 #define IOCTL_LED_ON 1 30 #define IOCTL_LED_OFF 0 31 32 static unsigned long led_table [] = 33 { 34 S3C2410_GPB(5), 35 S3C2410_GPB(6), 36 S3C2410_GPB(7), 37 S3C2410_GPB(8), 38 }; 39 40 static unsigned int led_cfg_table [] = 41 { 42 S3C2410_GPIO_OUTPUT, 43 S3C2410_GPIO_OUTPUT, 44 S3C2410_GPIO_OUTPUT, 45 S3C2410_GPIO_OUTPUT, 46 }; 47 48 static int gt2440_leds_ioctl( 49 // struct inode *inode, 50 struct file *file, 51 unsigned int cmd, 52 unsigned long arg) 53 { 54 if (arg > 4) 55 { 56 return -EINVAL; 57 } 58 59 switch(cmd) 60 { 61 case IOCTL_LED_ON: 62 // 設置指定引腳的輸出電平為0 63 s3c2410_gpio_setpin(led_table[arg], 0); 64 return 0; 65 66 case IOCTL_LED_OFF: 67 // 設置指定引腳的輸出電平為1 68 s3c2410_gpio_setpin(led_table[arg], 1); 69 return 0; 70 71 default: 72 return -EINVAL; 73 } 74 } 75 76 static struct file_operations dev_fops = { 77 .owner = THIS_MODULE, 78 .unlocked_ioctl = gt2440_leds_ioctl, 79 }; 80 81 static struct miscdevice misc = { 82 .minor = MISC_DYNAMIC_MINOR, 83 .name = DEVICE_NAME, 84 .fops = &dev_fops, 85 }; 86 87 static int __init dev_init(void) 88 { 89 int ret; 90 91 int i; 92 for (i = 0; i < 4; i++) 93 { 94 s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]); 95 s3c2410_gpio_setpin(led_table[i], 0); 96 } 97 98 ret = misc_register(&misc); 99 100 printk (DEVICE_NAME" initialized\n"); 101 102 return ret; 103 } 104 105 static void __exit dev_exit(void) 106 { 107 misc_deregister(&misc); 108 } 109 110 module_init(dev_init); 111 module_exit(dev_exit); 112 113 MODULE_LICENSE("GPL"); 114 MODULE_AUTHOR("www.e-online.cc"); 115 MODULE_DESCRIPTION("LEDS control for GT2440 Board"); View Code?
轉載于:https://www.cnblogs.com/kwseeker-bolgs/p/4472438.html
總結
以上是生活随笔為你收集整理的Linux驱动设计——字符杂项设备的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces Round #30
- 下一篇: jQuery找兄弟系列next(),ne