高级I/O操作-ioctl设备操作

2019-07-12 23:20发布

1 ioctl设备操作
参考书籍 《嵌入式Linux驱动开发教程》 华清远见嵌入式学院 2 虚拟串口中添加unlocked_ioctl后的代码 vser.h /*vser.h*/ #ifndef _VSER_H #define _VSER_H struct option{ unsigned int datab; unsigned int parity; unsigned int stopb; }; #define VS_MAGIC 's' /*幻数*/ #define VS_SET_BAUD _IOW(VS_MAGIC,0,unsigned int) #define VS_GET_BAUD _IOW(VS_MAGIC,1,unsigned int) #define VS_SET_FFMT _IOW(VS_MAGIC,2,struct option) #define VS_GET_FFMT _IOW(VS_MAGIC,3,struct option) #endif vser.c /*************************************** 高级I/O操作 ioctl设备操作 *****************************************/ /*vser.c*/ #include #include #include #include #include #include #include #include #include "vser.h" #define VSER_MAJOR 256 #define VSER_MINOR 0 #define VSER_DEV_CNT 1 #define VSER_DEV_NAME "vser" struct vser_dev { unsigned int baud; /*波特率*/ struct option opt; /*帧格式信息*/ struct cdev cdev; }; DEFINE_KFIFO(vsfifo, char, 32); static struct vser_dev vsdev; static int vser_open(struct inode *inode, struct file *filp) { return 0; } static int vser_release(struct inode *inode, struct file *filp) { return 0; } static ssize_t vser_read(struct file *filp, char __user *buf, size_t count, loff_t *pos) { int ret; unsigned int copied = 0; ret = kfifo_to_user(&vsfifo, buf, count, &copied); return ret == 0 ? copied : ret; } static ssize_t vser_write(struct file *filp, const char __user *buf, size_t count, loff_t *pos) { int ret; unsigned int copied = 0; ret = kfifo_from_user(&vsfifo, buf, count, &copied); return ret == 0 ? copied : ret; } static long vser_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { if(_IOC_TYPE(cmd) != VS_MAGIC) { return -ENOTTY; } switch(cmd) { case VS_SET_BAUD: vsdev.baud=arg; break; case VS_GET_BAUD: arg=vsdev.baud; break; case VS_SET_FFMT: if( copy_from_user(&vsdev.opt,(struct option __user*)arg,sizeof(struct option)) ) return -EFAULT; break; /*这里的break千万不要忘记加*/ case VS_GET_FFMT: if( copy_to_user((struct option __user*)arg,&vsdev.opt,sizeof(struct option)) ) return -EFAULT; break; default: return -ENOTTY; } return 0; } static struct file_operations vser_ops = { .owner = THIS_MODULE, .open = vser_open, .release = vser_release, .read = vser_read, .write = vser_write, .unlocked_ioctl = vser_ioctl, }; static int __init vser_init(void) { int ret; dev_t dev; /*设备号*/ dev = MKDEV(VSER_MAJOR, VSER_MINOR); /*注册字符设备*/ ret = register_chrdev_region(dev, VSER_DEV_CNT, VSER_DEV_NAME); if (ret) goto reg_err; cdev_init(&vsdev.cdev, &vser_ops); vsdev.cdev.owner = THIS_MODULE; vsdev.baud = 115200; vsdev.opt.datab = 8; vsdev.opt.parity = 0; /*奇偶位*/ vsdev.opt.stopb = 1; /*cdev对象初始化以后,就要添加到内核中的cdev_map散列表中 */ ret = cdev_add(&vsdev.cdev, dev, VSER_DEV_CNT); if (ret) goto add_err; return 0; add_err: unregister_chrdev_region(dev, VSER_DEV_CNT); reg_err: return ret; } /*卸载函数*/ static void __exit vser_exit(void) { dev_t dev; dev = MKDEV(VSER_MAJOR, VSER_MINOR); cdev_del(&vsdev.cdev); unregister_chrdev_region(dev, VSER_DEV_CNT); } module_init(vser_init); module_exit(vser_exit); 测试文件 #include #include #include #include #include #include #include #include "vser.h" int main(int argc, char *argv[]) { int fd; int ret; unsigned int baud; struct option opt = {8,2,1}; fd = open("/dev/vser0", O_RDWR); if (fd == -1) { printf("open file error "); goto fail; } baud = 9600; ret = ioctl(fd, VS_SET_BAUD, baud); if (ret == -1) { printf("ioctl(fd, VS_SET_BAUD, baud) error "); goto fail; } ret = ioctl(fd, VS_GET_BAUD, baud); if (ret == -1) goto fail; ret = ioctl(fd, VS_SET_FFMT, &opt); if (ret == -1) goto fail; ret = ioctl(fd, VS_GET_FFMT, &opt); if (ret == -1) goto fail; printf("baud rate: %d ", baud); printf("frame format: %d%c%d ", opt.datab, opt.parity == 0 ? 'N' : opt.parity == 1 ? 'O' : 'E', opt.stopb); close(fd); exit(EXIT_SUCCESS); fail: perror("ioctl test"); exit(EXIT_FAILURE); } 3 运行结果
这里写图片描述