本文共 3157 字,大约阅读时间需要 10 分钟。
/*包含初始化宏定义的头文件,代码中的module_init和module_exit在此文件中*/#include/*包含初始化加载模块的头文件,代码中的MODULE_LICENSE在此头文件中*/#include /*定义module_param module_param_array的头文件*/#include /*定义module_param module_param_array中perm的头文件*/#include /*三个字符设备函数*/#include /*MKDEV转换设备号数据类型的宏定义*/#include /*定义字符设备的结构体*/#include /*分配内存空间函数头文件*/#include /*包含函数device_create 结构体class等头文件*/#include /*自定义头文件*/#include "char_driver_leds.h"/*Linux中申请GPIO的头文件*/#include /*三星平台的GPIO配置函数头文件*//*三星平台EXYNOS系列平台,GPIO配置参数宏定义头文件*/#include /*三星平台4412平台,GPIO宏定义头文件*/#include MODULE_LICENSE("Dual BSD/GPL");/*声明是开源的,没有内核版本限制*/MODULE_AUTHOR("iTOPEET_dz");/*声明作者*/static int led_gpios[] = { EXYNOS4_GPL2(0),EXYNOS4_GPK1(1),};#define LED_NUM ARRAY_SIZE(led_gpios)int numdev_major = DEV_MAJOR;int numdev_minor = DEV_MINOR;/*输入主设备号*/module_param(numdev_major,int,S_IRUSR);/*输入次设备号*/module_param(numdev_minor,int,S_IRUSR);static struct class *myclass;struct reg_dev *my_devices;/*打开操作*/static int chardevnode_open(struct inode *inode, struct file *file){ printk(KERN_EMERG "chardevnode_open is success!\n"); return 0;}/*关闭操作*/static int chardevnode_release(struct inode *inode, struct file *file){ printk(KERN_EMERG "chardevnode_release is success!\n"); return 0;}/*IO操作*/static long chardevnode_ioctl(struct file *file, unsigned int cmd, unsigned long arg){ switch(cmd) { case 0: case 1: if (arg > LED_NUM) { return -EINVAL; } gpio_set_value(led_gpios[arg], cmd); break; default: return -EINVAL; } printk(KERN_EMERG "chardevnode_ioctl is success! cmd is %d,arg is %d \n",cmd,arg); return 0;}ssize_t chardevnode_read(struct file *file, char __user *buf, size_t count, loff_t *f_ops){ return 0;}ssize_t chardevnode_write(struct file *file, const char __user *buf, size_t count, loff_t *f_ops){ return 0;}loff_t chardevnode_llseek(struct file *file, loff_t offset, int ence){ return 0;}struct file_operations my_fops = { .owner = THIS_MODULE, .open = chardevnode_open, .release = chardevnode_release, .unlocked_ioctl = chardevnode_ioctl, .read = chardevnode_read, .write = chardevnode_write, .llseek = chardevnode_llseek,};/*设备注册到系统*/static void reg_init_cdev(struct reg_dev *dev,int index){ int err; int devno = MKDEV(numdev_major,numdev_minor+index); /*数据初始化*/ cdev_init(&dev->cdev,&my_fops); dev->cdev.owner = THIS_MODULE; dev->cdev.ops = &my_fops; /*注册到系统*/ err = cdev_add(&dev->cdev,devno,1); if(err){ printk(KERN_EMERG "cdev_add %d is fail! %d\n",index,err); } else{ printk(KERN_EMERG "cdev_add %d is success!\n",numdev_minor+index); }}static int gpio_init(void){ int i=0,ret; for(i=0;i
#include#include #include #include #include #include /*argv[1] is cmd , argv[2] is io_arg*/int main(int argc , char **argv){ int fd; char *lednode = "/dev/chardevnode0"; /*O_RDWR只读打开,O_NDELAY非阻塞方式*/ if((fd = open(lednode,O_RDWR|O_NDELAY))<0){ printf("APP open %s failed!\n",lednode); } else{ printf("APP open %s success!\n",lednode); ioctl(fd,atoi(argv[1]),atoi(argv[2])); printf("APP ioctl %s ,cmd is %s! io_arg is %s!\n",lednode,argv[1],argv[2]); } close(fd);}
转载地址:http://xhoin.baihongyu.com/