static inline void call_back(unsigned event, long arg1, long arg2) { const cancb_t *cb = LNKTBL_HEAD(cancb_t); while (cb++ < LNKTBL_TAIL(cancb_t)) cb[-1].call(event, arg1, arg2); }
时间: 2024-02-26 14:56:16 浏览: 164
这段代码是一个静态内联函数,函数名为call_back。该函数接收三个参数,分别为unsigned类型的event,long类型的arg1和arg2。函数内部定义了一个指向const cancb_t类型的指针cb,用于指向一个链表的头节点。然后通过while循环遍历整个链表(除了尾节点),并调用每个结构体中的call函数,将event、arg1和arg2作为参数传递进去。
该函数的作用是用于触发回调函数。在链表的每个结构体中,都存储了一个回调函数指针以及一些其他的参数,当事件发生时,可以通过遍历链表来触发每个回调函数的执行。这种设计可以让多个模块或插件来注册自己的回调函数,以响应特定的事件,这样可以增加程序的可扩展性和灵活性。使用内联方式实现,可以避免函数调用的开销。
相关问题
#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
(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的输出电平
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/uaccess.h> #include <linux/slab.h> #define DEVICE_NAME "mydevice" #define BUF_SIZE 4096 static char *dev_buf; static int major; static int open(struct inode *inode, struct file *file) { printk(KERN_INFO "mydevice: device opened.\n"); return 0; } static int release(struct inode *inode, struct file *file) { printk(KERN_INFO "mydevice: device closed.\n"); return 0; } static ssize_t read(struct file *file, char __user *buf, size_t count, loff_t *pos) { int bytes_read = 0; if (*pos >= BUF_SIZE) { return 0; } if (count + *pos > BUF_SIZE) { count = BUF_SIZE - *pos; } if (copy_to_user(buf, dev_buf + *pos, count)) { return -EFAULT; } *pos += count; bytes_read = count; printk(KERN_INFO "mydevice: %d bytes read.\n", bytes_read); return bytes_read; } static ssize_t write(struct file *file, const char __user *buf, size_t count, loff_t *pos) { int bytes_written = 0; if (*pos >= BUF_SIZE) { return -ENOSPC; } if (count + *pos > BUF_SIZE) { count = BUF_SIZE - *pos; } if (copy_from_user(dev_buf + *pos, buf, count)) { return -EFAULT; } *pos += count; bytes_written = count; printk(KERN_INFO "mydevice: %d bytes written.\n", bytes_written); return bytes_written; } static long ioctl(struct file *file, unsigned int cmd, unsigned long arg) { switch (cmd) { case 0: // 控制命令0 // 执行相应的控制操作 break; case 1: // 控制命令1 // 执行相应的控制操作 break; default: return -ENOTTY; } return 0; } static loff_t lseek(struct file *file, loff_t offset, int whence) { loff_t newpos = 0; switch (whence) { case 0: // SEEK_SET newpos = offset; break; case 1: // SEEK_CUR newpos = file->f_pos + offset; break; case 2: // SEEK_END newpos = BUF_SIZE + offset; break; default: return -EINVAL; } if (newpos < 0 || newpos > BUF_SIZE) { return -EINVAL; } file->f_pos = newpos; return newpos; } static struct file_operations mydevice_fops = { .owner = THIS_MODULE, .open = open, .release = release, .read = read, .write = write, .unlocked_ioctl = ioctl, .llseek = lseek, }; static int __init mydevice_init(void) { dev_buf = kmalloc(BUF_SIZE, GFP_KERNEL); if (!dev_buf) { printk(KERN_ALERT "mydevice: kmalloc failed.\n"); return -ENOMEM; } memset(dev_buf, 0, BUF_SIZE); major = register_chrdev(0, DEVICE_NAME, &mydevice_fops); if (major < 0) { printk(KERN_ALERT "mydevice: register_chrdev failed.\n"); return major; } printk(KERN_INFO "mydevice: Device registered, major = %d.\n", major); return 0; } static void __exit mydevice_exit(void) { unregister_chrdev(major, DEVICE_NAME); kfree(dev_buf); printk(KERN_INFO "mydevice: Device unregistered.\n"); } module_init(mydevice_init); module_exit(mydevice_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("My Device Driver");解释这串代码
这段代码是一个Linux设备驱动程序的示例,其目的是创建一个名为“mydevice”的字符设备,并实现设备文件的读写操作、文件指针的移动、ioctl操作等。该驱动程序通过调用register_chrdev函数注册字符设备,并实现了open、read、write、ioctl、lseek、release等文件操作函数。其中,open和release函数用于打开和关闭设备文件;read和write函数用于读写设备文件;lseek函数用于移动文件指针;ioctl函数用于实现设备的控制操作。该驱动程序还使用了动态内存分配函数kmalloc和动态内存释放函数kfree来分配和释放设备缓冲区dev_buf。最后,该驱动程序使用module_init和module_exit宏定义来指定驱动程序的初始化和退出函数,以及使用MODULE_LICENSE、MODULE_AUTHOR和MODULE_DESCRIPTION宏定义来指定驱动程序的许可证、作者和描述信息。
阅读全文