#include <linux/module.h> #include <linux/fs.h> #include <linux/gpio.h> // 各种gpio的数据结构及函数 #include <linux/cdev.h> #include <linux/init.h> //__init __exit 宏定义声明 #include <linux/device.h> //class devise声明 #include <linux/uaccess.h> //copy_from_user 的头文件 #include <linux/types.h> //设备号 dev_t 类型声明 #include <linux/ioctl.h> MODULE_LICENSE("Dual BSD/GPL"); #define IOCTL_GPIO_OFF 0 /*灭*/ #define IOCTL_GPIO_ON 1 /*亮*/ #define DEVICE_NAME "beepctrl_caiyuxin" static struct class *ioctrl_class; #define BEEP_MAJOR 0 /*预设的主设备号*/ static int BEEP_major = BEEP_MAJOR; /*BEEP设备结构体*/ struct BEEP_dev { struct cdev cdev; /*cdev结构体*/ }; struct BEEP_dev *BEEP_devp; /*设备结构体指针*/ // 定义三色BEEP的GPIO引脚 static const struct gpio beeps[] = { // { 2, GPIOF_OUT_INIT_HIGH, "BEEP_RED" }, // { 3, GPIOF_OUT_INIT_HIGH, "BEEP_GREEN" }, { 25, GPIOF_OUT_INIT_HIGH, "BEEP" }, }; int BEEP_open(struct inode *inode, struct file *filp)//打开设备节点 { // int i; // printk(KERN_INFO " beeps opened\n"); // for(i=0;i<3;i++) // { // gpio_set_value(beeps[i].gpio, 0); // } return 0; } static long int BEEP_ioctl(struct file *filp,unsigned int cmd, unsigned long arg) { //ioctl函数接口 if (arg > sizeof(beeps)/sizeof(unsigned long)) { return -EINVAL; } printk("arg,cmd: %ld %d\n", arg, cmd); switch(cmd) { case IOCTL_GPIO_OFF:// 设置指定引脚的输出电平为0,由电路图可知,输出0时为灭 gpio_set_value(beeps[arg].gpio, 0); break; case IOCTL_GPIO_ON: gpio_set_value(beeps[arg].gpio, 1); break; default: return -EINVAL; } return 0; } int BEEP_release(struct inode *inode, struct file *filp)//释放设备节点 { int i; printk(KERN_INFO "BEEPs driver successfully close\n"); for(i=0;i<3;i++) { gpio_set_value(beeps[i].gpio, 0); } return 0; } static const struct file_operations BEEP_fops = { .owner = THIS_MODULE, .open = BEEP_open, .release = BEEP_release, .unlocked_ioctl = BEEP_ioctl, /* 实现主要控制功能*/ }; /*初始化并注册cdev*/ static void BEEP_setup
时间: 2024-04-02 13:31:59 浏览: 138
(void) { int ret; dev_t devno = MKDEV(BEEP_major, 0); // 申请设备号 if (BEEP_major) { ret = register_chrdev_region(devno, 1, DEVICE_NAME); } else { ret = alloc_chrdev_region(&devno, 0, 1, DEVICE_NAME); BEEP_major = MAJOR(devno); } if (ret < 0) { printk(KERN_WARNING "BEEP: unable to get major %d\n", BEEP_major); return; } BEEP_devp = kmalloc(sizeof(struct BEEP_dev), GFP_KERNEL); if (!BEEP_devp) { ret = -ENOMEM; goto fail_malloc; } memset(BEEP_devp, 0, sizeof(struct BEEP_dev)); /*初始化cdev*/ cdev_init(&BEEP_devp->cdev, &BEEP_fops); BEEP_devp->cdev.owner = THIS_MODULE; ret = cdev_add(&BEEP_devp->cdev, devno, 1); if (ret) { printk(KERN_NOTICE "BEEP: Error %d adding BEEP\n", ret); goto fail_add; } /* 创建class,并创建device */ ioctrl_class = class_create(THIS_MODULE, DEVICE_NAME); if (IS_ERR(ioctrl_class)) { printk(KERN_ERR "failed to create class"); goto fail_class_create; } device_create(ioctrl_class, NULL, devno, NULL, DEVICE_NAME); gpio_request_array(beeps, ARRAY_SIZE(beeps)); printk(KERN_INFO "BEEP driver initialized\n"); return; fail_class_create: cdev_del(&BEEP_devp->cdev); fail_add: kfree(BEEP_devp); fail_malloc: unregister_chrdev_region(devno, 1); } static void __exit BEEP_exit(void)/*模块卸载函数*/ { dev_t devno = MKDEV(BEEP_major, 0); gpio_free_array(beeps, ARRAY_SIZE(beeps)); device_destroy(ioctrl_class, devno); class_destroy(ioctrl_class); cdev_del(&BEEP_devp->cdev); kfree(BEEP_devp); unregister_chrdev_region(devno, 1); printk(KERN_INFO "BEEP driver unregistered\n"); } module_init(BEEP_setup); /*模块入口*/ module_exit(BEEP_exit); /*模块出口*/ MODULE_AUTHOR("caiyuxin"); MODULE_DESCRIPTION("BEEP driver"); MODULE_ALIAS("BEEP driver"); 分析: 1.该驱动程序是一个字符设备驱动程序,对应的设备节点为/beepctrl_caiyuxin 2.定义了IOCTL_GPIO_OFF和IOCTL_GPIO_ON两个命令,用于控制输出灯的亮灭 3.定义了BEEP_open、BEEP_release、BEEP_ioctl三个函数,分别对应设备节点的打开、关闭、控制操作 4.定义了BEEP_setup和BEEP_exit两个函数,分别对应驱动程序的初始化和卸载 5.使用了GPIO控制LED的输出电平
阅读全文