字符设备驱动0:一个简单但完整的字符设备驱动程序
生活随笔
收集整理的這篇文章主要介紹了
字符设备驱动0:一个简单但完整的字符设备驱动程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考:
linux設備驅動程序之簡單字符設備驅動 【很詳細,必看】http://www.cnblogs.com/geneil/archive/2011/12/03/2272869.html?
//在驅動入口函數中,使用register_chrdev添加字符設備驅動:#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h> #include <asm/arch/regs-gpio.h> #include <asm/hardware.h>#define DEVICE_NAME "leds" /* 加載模式后,執行”cat /proc/devices”命令看到的設備名稱 */ #define LED_MAJOR 231 /* 主設備號 */static struct class *leds_class; static struct class_device *leds_class_devs[4];/* bit0<=>D10, 0:亮, 1:滅* bit1<=>D11, 0:亮, 1:滅* bit2<=>D12, 0:亮, 1:滅*/ static char leds_status = 0x0; static DECLARE_MUTEX(leds_lock); // 定義賦值//static int minor; static unsigned long gpio_va;#define GPIO_OFT(x) ((x) - 0x56000000) #define GPFCON (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000050)))//oo00:GPFCON 0x56000050 R/W 配置端口F的引腳 #define GPFDAT (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000054)))//oo00:gpio_va + 0x54 ,把物理地址0x56000054映射為虛擬地址GPFDAT。 //led驅動中,要操作io口,必須要把物理地址映射為虛擬地址。/* 應用程序對設備文件/dev/leds執行open(...)時,* 就會調用s3c24xx_leds_open函數*/ static int s3c24xx_leds_open(struct inode *inode, struct file *file) {int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);switch(minor){case 0: /* /dev/leds */{// 配置3引腳為輸出//s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);GPFCON &= ~(0x3<<(4*2)); //oo00:這里GPFCON是虛擬地址,在驅動模塊開始的時候,這個虛擬地址已經通過宏定義 #define GPFCON (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000050)))被映射到物理地址GPFCON |= (1<<(4*2));//s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);GPFCON &= ~(0x3<<(5*2));GPFCON |= (1<<(5*2));//s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);GPFCON &= ~(0x3<<(6*2));GPFCON |= (1<<(6*2));// 都輸出0//s3c2410_gpio_setpin(S3C2410_GPF4, 0);GPFDAT &= ~(1<<4);//s3c2410_gpio_setpin(S3C2410_GPF5, 0);GPFDAT &= ~(1<<5);//s3c2410_gpio_setpin(S3C2410_GPF6, 0);GPFDAT &= ~(1<<6);down(&leds_lock);leds_status = 0x0;up(&leds_lock);break;}case 1: /* /dev/led1 */{s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);s3c2410_gpio_setpin(S3C2410_GPF4, 0);down(&leds_lock);leds_status &= ~(1<<0);up(&leds_lock);break;}case 2: /* /dev/led2 */{s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);s3c2410_gpio_setpin(S3C2410_GPF5, 0);leds_status &= ~(1<<1);break;}case 3: /* /dev/led3 */{s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);s3c2410_gpio_setpin(S3C2410_GPF6, 0);down(&leds_lock);leds_status &= ~(1<<2);up(&leds_lock);break;}}return 0; }static int s3c24xx_leds_read(struct file *filp, char __user *buff,size_t count, loff_t *offp) {int minor = MINOR(filp->f_dentry->d_inode->i_rdev);char val;switch (minor){case 0: /* /dev/leds */{copy_to_user(buff, (const void *)&leds_status, 1);break;}case 1: /* /dev/led1 */{down(&leds_lock);val = leds_status & 0x1;up(&leds_lock);copy_to_user(buff, (const void *)&val, 1);break;}case 2: /* /dev/led2 */{down(&leds_lock);val = (leds_status>>1) & 0x1;up(&leds_lock);copy_to_user(buff, (const void *)&val, 1);break;}case 3: /* /dev/led3 */{down(&leds_lock);val = (leds_status>>2) & 0x1;up(&leds_lock);copy_to_user(buff, (const void *)&val, 1);break;}}return 1; }static ssize_t s3c24xx_leds_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos) {//int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);int minor = MINOR(file->f_dentry->d_inode->i_rdev);char val;copy_from_user(&val, buf, 1);switch (minor){case 0: /* /dev/leds */{s3c2410_gpio_setpin(S3C2410_GPF4, (val & 0x1));s3c2410_gpio_setpin(S3C2410_GPF5, (val & 0x1));s3c2410_gpio_setpin(S3C2410_GPF6, (val & 0x1));down(&leds_lock);leds_status = val;up(&leds_lock);break;}case 1: /* /dev/led1 */{s3c2410_gpio_setpin(S3C2410_GPF4, val);if (val == 0){down(&leds_lock);leds_status &= ~(1<<0);up(&leds_lock);}else{down(&leds_lock);leds_status |= (1<<0);up(&leds_lock);}break;}case 2: /* /dev/led2 */{s3c2410_gpio_setpin(S3C2410_GPF5, val);if (val == 0){down(&leds_lock);leds_status &= ~(1<<1);up(&leds_lock);}else{down(&leds_lock);leds_status |= (1<<1);up(&leds_lock);}break;}case 3: /* /dev/led3 */{s3c2410_gpio_setpin(S3C2410_GPF6, val);if (val == 0){down(&leds_lock);leds_status &= ~(1<<2);up(&leds_lock);}else{down(&leds_lock);leds_status |= (1<<2);up(&leds_lock);}break;}}return 1; }/* 這個結構是字符設備驅動程序的核心* 當應用程序操作設備文件時所調用的open、read、write等函數,* 最終會調用這個結構中指定的對應函數*/ static struct file_operations s3c24xx_leds_fops = {.owner = THIS_MODULE, /* 這是一個宏,推向編譯模塊時自動創建的__this_module變量 */.open = s3c24xx_leds_open,.read = s3c24xx_leds_read,.write = s3c24xx_leds_write, };/** 執行insmod命令時就會調用這個函數*/ static int __init s3c24xx_leds_init(void) //static int __init init_module(void) {int ret;int minor = 0;gpio_va = ioremap(0x56000000, 0x100000);if (!gpio_va) {return -EIO;}printk(DEVICE_NAME "gpio_va = %x\n",gpio_va);/* 注冊字符設備* 參數為主設備號、設備名字、file_operations結構;* 這樣,主設備號就和具體的file_operations結構聯系起來了,* 操作主設備為LED_MAJOR的設備文件時,就會調用s3c24xx_leds_fops中的相關成員函數* LED_MAJOR可以設為0,表示由內核自動分配主設備號*/ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c24xx_leds_fops);if (ret < 0) {printk(DEVICE_NAME " can't register major number\n");return ret;} //oo00 :begin : 分配了四個子設備號 minor == 0 1 2 3leds_class = class_create(THIS_MODULE, "leds");if (IS_ERR(leds_class))return PTR_ERR(leds_class);leds_class_devs[0] = class_device_create(leds_class, NULL, MKDEV(LED_MAJOR, 0), NULL, "leds");for (minor = 1; minor < 4; minor++){leds_class_devs[minor] = class_device_create(leds_class, NULL, MKDEV(LED_MAJOR, minor), NULL, "led%d", minor);if (unlikely(IS_ERR(leds_class_devs[minor])))return PTR_ERR(leds_class_devs[minor]);} //oo00 :end printk(DEVICE_NAME " initialized\n");return 0; }/** 執行rmmod命令時就會調用這個函數*/ static void __exit s3c24xx_leds_exit(void) {int minor;/* 卸載驅動程序 */unregister_chrdev(LED_MAJOR, DEVICE_NAME);for (minor = 0; minor < 4; minor++){class_device_unregister(leds_class_devs[minor]);}class_destroy(leds_class);iounmap(gpio_va); }/* 這兩行指定驅動程序的初始化函數和卸載函數 */ module_init(s3c24xx_leds_init); module_exit(s3c24xx_leds_exit);/* 描述驅動程序的一些信息,不是必須的 */ MODULE_AUTHOR("http://www.100ask.net"); MODULE_VERSION("0.1.0"); MODULE_DESCRIPTION("S3C2410/S3C2440 LED Driver"); MODULE_LICENSE("GPL");
?
對應的led應用程序:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h>/** ledtest <dev> <on|off>*/void print_usage(char *file) {printf("Usage:\n");printf("%s <dev> <on|off>\n",file);printf("eg. \n");printf("%s /dev/leds on\n", file);printf("%s /dev/leds off\n", file);printf("%s /dev/led1 on\n", file);printf("%s /dev/led1 off\n", file); }int main(int argc, char **argv) {int fd;char* filename;char val;if (argc != 3){print_usage(argv[0]);return 0;}filename = argv[1];fd = open(filename, O_RDWR);if (fd < 0){printf("error, can't open %s\n", filename);return 0;}if (!strcmp("on", argv[2])){// 亮燈val = 0;write(fd, &val, 1);}else if (!strcmp("off", argv[2])){// 滅燈val = 1;write(fd, &val, 1);}else{print_usage(argv[0]);return 0;}return 0; }?
加載模塊后,執行”cat /proc/devices”命令看設備名稱;// register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c24xx_leds_fops); 執行ls /dev/ 命令查看class名稱。//class_device_create(leds_class, NULL, MKDEV(LED_MAJOR, minor), NULL, "led%d", minor);附例: int major; static int first_drv_init(void) {major = register_chrdev(0, "first_drv", &first_drv_fops); // 注冊, 告訴內核 firstdrv_class = class_create(THIS_MODULE, "firstdrv");firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);gpfdat = gpfcon + 1;return 0; } --------------------------------- # cat /proc/devices Character devices: 1 mem 2 pty 204 s3c2410_serial 252 first_drv 254 rtc# ls -l /dev crw-rw---- 1 0 0 204, 64 Jan 1 00:56 s3c2410_serial0 crw-rw---- 1 0 0 204, 65 Jan 1 00:00 s3c2410_serial1 crw-rw---- 1 0 0 204, 66 Jan 1 00:00 s3c2410_serial2 crw-rw---- 1 0 0 252, 0 Jan 1 00:44 xyz ------------------------------------- fd = open("/dev/xyz", O_RDWR);if (fd < 0){printf("can't open!\n");}
?
轉載于:https://www.cnblogs.com/mylinux/p/4010908.html
總結
以上是生活随笔為你收集整理的字符设备驱动0:一个简单但完整的字符设备驱动程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一氧化碳(CO)荧光探针cas85575
- 下一篇: 大数据项目大致流程