static long fs4412_sa_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { int nr; int data; if(cmd == LED_ON || cmd == LED_OFF || cmd == SET_CNT){ data=copy_from_user(&nr, (void *)arg, sizeof(nr)); } switch (cmd) { case LED_ON: fs4412_led_on(nr); break; case LED_OFF: fs4412_led_off(nr); break; case PWM_ON: writel((readl(pwm->gpdcon) & ~0xf) | 0x2,pwm->gpdcon); writel((readl(pwm->timer_base + TCFG0)) | 0xff,pwm->timer_base + TCFG0); writel((readl(pwm->timer_base + TCFG1) & ~0xf) | 0x2,pwm->timer_base + TCFG1); writel((readl(pwm->timer_base + TCON) & ~0xf) | 0x9, pwm->timer_base + TCON); break; case PWM_OFF: writel((readl(pwm->timer_base + TCON) & ~0xf), pwm->timer_base + TCON); break; case SET_PRE: writel((readl(pwm->timer_base + TCFG0) & ~0xff) | (nr & 0xff), pwm->timer_base + TCFG0); writel((readl(pwm->timer_base + TCON) & ~0xf) | 0x9, pwm->timer_base + TCON); break; case SET_CNT: writel(nr, pwm->timer_base + TCNTB0); writel(nr >> 1, pwm->timer_base + TCMPB0); writel(readl(pwm->timer_base + TCON) | (0x1 << 1), pwm->timer_base + TCON); writel(readl(pwm->timer_base + TCON) & ~(0x1 << 1), pwm->timer_base + TCON); break; default: return -ENOTTY; } return 0; }
时间: 2024-02-14 11:19:20 浏览: 68
这段代码是 FS4412 开发板的设备驱动程序中的 unlocked_ioctl 函数,用于处理用户空间发来的 ioctl 命令。函数会根据传入的 cmd 参数来执行相应的操作。其中 LED_ON、LED_OFF 和 SET_CNT 命令需要传入一个整型参数,函数通过 copy_from_user 函数将参数从用户空间拷贝到内核空间。LED_ON 和 LED_OFF 命令用于控制 LED 灯的开关,PWM_ON 和 PWM_OFF 命令用于开启和关闭 PWM 波形输出,SET_PRE 和 SET_CNT 命令用于设置 PWM 的预分频和计数器值。函数执行成功后返回 0。若 cmd 参数不被识别,则返回 -ENOTTY。
相关问题
#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宏定义来指定驱动程序的许可证、作者和描述信息。
阅读全文