http://blog.csdn.net/morixinguan/article/details/77808088
上节,我们明白了proc文件系统的作用,接下来我们在友善之臂已经写好的led驱动的基础上,在proc目录下创建一个文件夹,然后加入led驱动的版本信息读取。
我们在init函数的最后加入:
//定义proc文件系统节点 struct proc_dir_entry *dev_dir , *dev_version; //创建一个目录 dev_dir = proc_mkdir("Tiny4412_leds", NULL); //创建一个文件 dev_version = create_proc_entry("version", S_IRUGO, dev_dir); //调用读版本的函数 dev_version->read_proc = proc_read_version ;然后编写获取版本信息的读函数:
#define VERSION_LEN 20 char Tiny4412_LED_version[VERSION_LEN] = "20170908\n"; int proc_read_version(char *page, char **start, off_t off, int count, int *eof, void *data) { int len ; len = sprintf(page,Tiny4412_LED_version); //将字符串写入page,相当于copy到用户层 return len ; }大功告成!!
接下来编译完成,将kenel刷入开发板后卡机,用adb shell进入可以看到如下:
修改的源码:
#include <linux/kernel.h> #include <linux/module.h> #include <linux/miscdevice.h> #include <linux/fs.h> #include <linux/types.h> #include <linux/moduleparam.h> #include <linux/slab.h> #include <linux/ioctl.h> #include <linux/cdev.h> #include <linux/delay.h> #include <linux/gpio.h> #include <mach/gpio.h> #include <plat/gpio-cfg.h> //添加必要的头文件 #include <linux/proc_fs.h> #include <linux/uaccess.h> #define DEVICE_NAME "leds" static int led_gpios[] = { EXYNOS4212_GPM4(0), EXYNOS4212_GPM4(1), EXYNOS4212_GPM4(2), EXYNOS4212_GPM4(3), }; #define LED_NUM ARRAY_SIZE(led_gpios) static long tiny4412_leds_ioctl(struct file *filp, 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); //printk(DEVICE_NAME": %d %d\n", arg, cmd); break; default: return -EINVAL; } return 0; } static struct file_operations tiny4412_led_dev_fops = { .owner = THIS_MODULE, .unlocked_ioctl = tiny4412_leds_ioctl, }; static struct miscdevice tiny4412_led_dev = { .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &tiny4412_led_dev_fops, }; #define VERSION_LEN 20 char Tiny4412_LED_version[VERSION_LEN] = "20170908\n"; int proc_read_version(char *page, char **start, off_t off, int count, int *eof, void *data) { int len ; len = sprintf(page,Tiny4412_LED_version); return len ; } static int __init tiny4412_led_dev_init(void) { int ret; int i; for (i = 0; i < LED_NUM; i++) { ret = gpio_request(led_gpios[i], "LED"); if (ret) { printk("%s: request GPIO %d for LED failed, ret = %d\n", DEVICE_NAME, led_gpios[i], ret); return ret; } s3c_gpio_cfgpin(led_gpios[i], S3C_GPIO_OUTPUT); gpio_set_value(led_gpios[i], 1); } //杂类设备 ret = misc_register(&tiny4412_led_dev); //定义proc文件系统节点 struct proc_dir_entry *dev_dir , *dev_version; //创建一个目录 dev_dir = proc_mkdir("Tiny4412_leds", NULL); //创建一个文件 dev_version = create_proc_entry("version", S_IRUGO, dev_dir); //调用读版本的函数 dev_version->read_proc = proc_read_version ; printk(DEVICE_NAME"\tinitialized\n"); return ret; } static void __exit tiny4412_led_dev_exit(void) { int i; for (i = 0; i < LED_NUM; i++) { gpio_free(led_gpios[i]); } misc_deregister(&tiny4412_led_dev); } module_init(tiny4412_led_dev_init); module_exit(tiny4412_led_dev_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("FriendlyARM Inc & Y.X.YANG modify");