生成设备节点
来源:互联网 发布:淘宝黑客 编辑:程序博客网 时间:2024/03/29 17:38
一部分驱动要和上层通信,都需要生成设备节点,上层应用通过一套标准的接口函数调用设备节点就可以控制底层以及和底层通信。最简单易用的杂项设备节点如何生成。
为什么引入杂项设备
在exynos4412开发板,在超级终端中输入命令“cat /proc/misc”可以查看对应的杂项设备。主设备号只有256 个,设备又非常多,所以引入了子设备号。其中杂项设备的主设备号是10,在任何Linux 系统中它都是固定的。一般将Linux驱动分为字符设备、块设备、网络设备,但是这个分类不能包含所有的设备,所以将无法归类的设备统称为杂项设备,杂项设备可能用到字符设备、快设备、网络设备中的一项或者多项设备。
在linux虚拟机上在内核源码文件下,ls drivers/char可以看到misc.c和misc.o,可见它被编译进内核,使用vim drivers/char/Makefile打开杂项设备文件的编译文件。
obj-y +=misc.o,该文件被强制编译进内核
杂项设备设备部分完全制作好了,只需要添加子设备,非常方便。
这样杂项设备的引入即解决了设备号数量少的问题,又降低了使用难度,还能防止碎片化,一举多得。
杂项设备注册函数以及结构体
杂项设备的头文件在“include/linux/miscdevice.h”
struct miscdevice { int minor;//设备号,赋值为MISC_DYNAMIC_MINOR,这个宏定义可以查到为10 const char *name;//设备名称 const struct file_operations *fops;//file_operations 结构体 struct list_head list; struct device *parent; struct device *this_device; const char *nodename; mode_t mode;};extern int misc_register(struct miscdevice * misc);extern int misc_deregister(struct miscdevice *misc);
Cextern int misc_register(struct miscdevice * misc);
杂项设备注册函数;一般在probe 中调用,参数是miscdevice
Cextern int misc_deregister(struct miscdevice *misc);
杂项设备卸载函数;一般是在hello_remove 中用于卸载驱动。
file_operations结构体
file_operations 结构体的成员函数属于驱动开发的主体内容,里面的函数和Linux 系统给应用程序提供系统接口一一对应。
file_operations结构体在内核源码“include/linux/fs.h”中,使用命令vim /include/linux/fs.h打开
struct file_operations { struct module *owner; loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t); ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t); int (*readdir) (struct file *, void *, filldir_t); unsigned int (*poll) (struct file *, struct poll_table_struct *);/* remove by cym 20130408 support for MT660.ko */#if 0//#ifdef CONFIG_SMM6260_MODEM#if 1// liang, Pixtree also need to use ioctl interface... int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);#endif#endif/* end remove */ long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); long (*compat_ioctl) (struct file *, unsigned int, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*flush) (struct file *, fl_owner_t id); int (*release) (struct inode *, struct file *); int (*fsync) (struct file *, int datasync); int (*aio_fsync) (struct kiocb *, int datasync); int (*fasync) (int, struct file *, int); int (*lock) (struct file *, int, struct file_lock *); ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int); unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); int (*check_flags)(int); int (*flock) (struct file *, int, struct file_lock *); ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int); ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int); int (*setlease)(struct file *, long, struct file_lock **); long (*fallocate)(struct file *file, int mode, loff_t offset,loff_t len);/* add by cym 20130408 support for MT6260 and Pixtree */#if defined(CONFIG_SMM6260_MODEM) || defined(CONFIG_USE_GPIO_AS_I2C) int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);#endif/* end add */};
该结构体中参数非常之多,介绍常用的的参数
struct module *owner;一般是THIS_MODULE。int (*open) (struct inode *, struct file *);//对应上层的open 函数,打开文件。int (*release) (struct inode *, struct file *);//对应上层的close 函数,打开文件操作之后一般需要关闭。ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);//读函数,上层应用从底层读取函数。ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);//写函数,上层应用向底层传输数据。long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);//这个函数功能和写函数稍微有点重合,但是这个函数占用的内存非常小,主要针对IO 口的控制。
定义一个DEVICE_NAME,将其赋值为”hello_ctl123”,理解这个设备节点名称和前面介绍的注册设备名称是不同的。
示例代码
linux_misc.c#include <linux/init.h>#include <linux/module.h>/*驱动注册的头文件,包含驱动的结构体和注册和卸载的函数*/#include <linux/platform_device.h>/*注册杂项设备头文件*/#include <linux/miscdevice.h>/*注册设备节点的文件结构体*/#include <linux/fs.h>#define DRIVER_NAME "hello_ctl"#define DEVICE_NAME "hello_ctl123"MODULE_LICENSE("Dual BSD/GPL");MODULE_AUTHOR("chenmiaohong");static long hello_ioctl( struct file *files, unsigned int cmd, unsigned long arg){ printk("cmd is %d,arg is %d\n",cmd,arg); return 0;}static int hello_release(struct inode *inode, struct file *file){ printk(KERN_EMERG "hello release\n"); return 0;}static int hello_open(struct inode *inode, struct file *file){ printk(KERN_EMERG "hello open\n"); return 0;}static struct file_operations hello_ops = { .owner = THIS_MODULE, .open = hello_open, .release = hello_release, .unlocked_ioctl = hello_ioctl,};static struct miscdevice hello_dev = { .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &hello_ops,};static int hello_probe(struct platform_device *pdv){ printk(KERN_EMERG "\tinitialized\n"); misc_register(&hello_dev); return 0;}static int hello_remove(struct platform_device *pdv){ printk(KERN_EMERG "\tremove\n"); misc_deregister(&hello_dev); return 0;}static void hello_shutdown(struct platform_device *pdv){ ;}static int hello_suspend(struct platform_device *pdv,pm_message_t pmt){ return 0;}static int hello_resume(struct platform_device *pdv){ return 0;}struct platform_driver hello_driver = { .probe = hello_probe, .remove = hello_remove, .shutdown = hello_shutdown, .suspend = hello_suspend, .resume = hello_resume, .driver = { .name = DRIVER_NAME, .owner = THIS_MODULE, }};static int hello_init(void){ int DriverState; printk(KERN_EMERG "HELLO WORLD enter!\n"); DriverState = platform_driver_register(&hello_driver); printk(KERN_EMERG "\tDriverState is %d\n",DriverState); return 0;}static void hello_exit(void){ printk(KERN_EMERG "HELLO WORLD exit!\n"); platform_driver_unregister(&hello_driver); }module_init(hello_init);module_exit(hello_exit);
编译完成后通过TFTP服务器下载到exynos4412开发板中,使用insmod加载linux_misc.ko文件,然后使用ls /dev查看出现了新的设备hello_ctl123,
- 生成设备节点
- itop4412生成设备节点
- (六) 生成设备节点
- Android如何生成设备节点
- Android如何生成设备节点
- Android如何生成设备节点
- Android如何生成设备节点
- Android如何生成设备节点
- Android如何生成设备节点
- Android如何生成设备节点
- Android如何生成设备节点
- 设备文件节点的生成
- 设备文件节点的生成
- Android如何生成设备节点
- Android如何生成设备节点
- Android如何生成设备节点
- Android如何生成设备节点
- Android如何生成设备节点
- 收藏网址
- 帝国cms添加栏目前台显示正常,后台管理栏目中不显示
- hdu 1003 Max Sum(简单dp)
- BootStrap 标签页
- IOS在钥匙串里保存APP的账号密码
- 生成设备节点
- SDUT1130数据结构上机测试1:顺序表的应用(上一个代码太蠢)
- leetcode 376. Wiggle Subsequence
- Error running app: This version of Android Studio is incompatible with the Gradle Plugin used. Try d
- 评价UML工具
- linux命令提示符显示当前完整路径
- 线程安全与共享资源
- DOM对象模型高级编程(增查)
- 遍历Map的四种方法