本帖最后由 290005999 于 2017-2-27 12:33 编辑
1. 本次实验的项目会用到platform 平台总线以及input输入子系统
2.什么是paltform总线
一个现实的linux设备和驱动通常都需要挂接在一种总线上,比较常见的总线有USB、PCI总线等。但是,在嵌入式系统里面,SoC系统中集成的独立的外设控制器、挂接在SoC内存空间的外设却不依附与此类总线。基于这样的背景下,2.6内核加入了platform虚拟总线。platform机制将设备本身的资源注册进内核,有内核统一管理,在驱动程序使用这些资源时使用统一的接口,这样提高了程序的可移植性。
3.什么是input子系统
Input驱动程序是linux输入设备的驱动程序,分成游戏杆(joys tick)、鼠标(mouse和mice)、事件设备(event)。其中事件驱动程序是目前通用的驱动程序,可支持键盘、鼠标、触摸屏等多种输入设备。
----------------------------------------------------------------------
本次实战的过程,platform _driver 与 platform_device 的probe 过程,并通过App 模拟鼠标的位置(一个random函数,模拟鼠标x , y,并应该直接从鼠标传递,不过在项目实战前篇的开门篇 不想增加过多的难度,所以仅作为模拟)传输到驱动中,并上报了input 核心层给应用层采集信息。
platform_device
- /*
- * platform_device.c
- * Author: chenpeng
- */
- #include
- #include
- #include
- #include
- #include
- #define GPIO_TO_PIN(bank, gpio) (((bank)*32) + (gpio))
- static struct resource mouse_analog_source[] = {
- [0] = {
- .flags = IORESOURCE_IRQ,
- .start = GPIO_TO_PIN(1, 27),
- .end = GPIO_TO_PIN(1, 27),
- },
- };
- static struct platform_device mouse_analog_dev = {
- .name = "irq_control",
- .id = -1,
- .num_resources = ARRAY_SIZE(mouse_analog_source),
- .resource = &mouse_analog_source,
- };
- static int __init platform_device_init(void)
- {
- return platform_device_register(&mouse_analog_dev);
- }
- static void __exit platform_device_exit(void)
- {
- platform_device_unregister(&mouse_analog_dev);
- }
- module_init(platform_device_init);
- module_exit(platform_device_exit);
- MODULE_LICENSE("GPL");
复制代码
再看platform_driver :
- /*
- * platform_driver.c
- * Author: chenpeng
- */
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- //#include
- #define debug 1
- #ifdef debug
- #define DEBUG(...) printk(KERN_INFO #__VA_ARGS__) //printk()
- #else
- #define DEBUG(...) do{} while(0)
- #endif
- static unsigned int gpio_num;
- static unsigned int devno;
- static struct cdev cdev;
- static struct class *platform_class;
- int kbuf[20];
- static int irq;
- static struct platform_device *myinput_platform;
- static struct input_dev *myinput;
- /*
- *
- */
- ----------------以下是我写input的函数,2017-02-22------------------------------------
- ssize_t mouse_analog_write(struct file *filp, const char __user *ubuf, size_t count, unsigned long *ppos)
- {
- int x = 0;
- int y = 0;
- sscanf(ubuf, "%d%d", &x,&y);
- printk("x = %d, y = %d n", x, y);
- input_report_key(myinput, REL_X, x);
- input_report_key(myinput, REL_Y, y);
- input_sync(myinput);
- return count;
- }
- static struct file_operations fops = {
- .owner = THIS_MODULE,
- .write = mouse_analog_write,
- };
-
- static int mouse_analog_probe(struct platform_device *pdev)
- {
- int ret = -1;
- DEBUG("mouse_analog_probe is runningn");
- struct resource *mouse_analog = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
- gpio_num = mouse_analog->start;
- printk("--------------------------------------gpio_num = %d -------------------------------n", gpio_num);
- ret = alloc_chrdev_region(&devno, 0, 1, "mouse_analog_platform");
- if (ret < 0){
- printk("errorn");
- ret = -1;
- goto fail1;
- }
- DEBUG("platform1 is running n");
- cdev_init(&cdev, &fops);
- cdev.owner = THIS_MODULE;
- ret = cdev_add(&cdev, devno, 1);
- if (ret)
- {
- printk("errorn");
- ret = -1;
- goto fail2;
- }
- platform_class = class_create(THIS_MODULE, "platform_mouse_analog_1");
- if (IS_ERR(platform_class))
- {
- printk("is error in platformclass n");
- ret = -1;
- goto fail3;
- }
- DEBUG("platform is running n");
- device_create(platform_class,NULL, devno, NULL, "platform_mouse_analog_2");
- //gpio_request(gpio_num, "mouse_analog_light");
- //gpio_direction_output(gpio_num, 0);
- irq = gpio_to_irq(gpio_num);
- if (irq < 0)
- {
- printk("-----------------------irq is fail--------------------- n");
- }
- ret = request_irq(irq, irq_control_func, IRQF_TRIGGER_FALLING, "myirq", NULL);
- if (ret < 0)
- {
- printk("request_irq is fail n");
- ret = -1;
- goto fail4;
- }
- myinput = input_allocate_device();
- if (!myinput) {
- ret = -1;
- goto fail4;
- printk("request input is fail n");
- }
- DEBUG("platform3 is running n");
- //myinput->name = "my_input";
- set_bit(EV_REL, myinput->evbit);
- set_bit(REL_X, myinput->relbit);
- set_bit(REL_Y, myinput->relbit);
- DEBUG("platform2 is running n");
- ret = input_register_device(myinput);
- if (ret){
- printk("input_register_device is fail n");
- goto fail5;
- }
- DEBUG("platform5 is running n");
- if (IS_ERR(myinput_platform)){
- printk("platform_device_register_simple is fail n");
- ret = -1;
- goto fail5;
- }
- DEBUG("platform6 is running n");
- printk("probe func is running ending n");
- return 0;
- fail5:
- input_free_device(myinput);
- fail4:
- free_irq(irq, NULL);
- fail3:
- class_destroy(platform_class);
- fail2:
- cdev_del(&cdev);
- fail1:
- unregister_chrdev_region(devno, 1);
- return ret;
- }
- static int mouse_analog_remove(struct platform_device *pdev)
- {
- device_destroy(platform_class, devno);
- class_destroy(platform_class);
- input_unregister_device(myinput);
- // platform_device_unregister(myinput_platform);
- cdev_del(&cdev);
- printk("num = %d n", irq);
- free_irq(irq, NULL);
- unregister_chrdev_region(devno, 1);
- return 0;
- }
- static struct platform_driver mouse_analog_drv = {
- .driver ={
- .name = "irq_control",
- .owner = THIS_MODULE,
- },
- .probe = mouse_analog_probe,
- .remove = mouse_analog_remove,
- };
- static int __init platform_driver_init(void)
- {
- return platform_driver_register(&mouse_analog_drv);
- }
- static void __exit platform_driver_exit(void)
- {
- platform_driver_unregister(&mouse_analog_drv);
- }
- module_init(platform_driver_init);
- module_exit(platform_driver_exit);
- MODULE_LICENSE("GPL");
复制代码
最后看app函数:
- /*
- * app.c
- *
- * Author: chenpeng
- */
- #include
- #include
- #include
- #include
- #include
- #define FILE_NAME "/dev/mouse_analog2"
- int main(void)
- {
- int fd, x ,y;
- char buf[20];
- fd = open(FILE_NAME, O_RDWR);
- if (fd < 0)
- {
- printf("open is error n");
- return -1;
- }
- /*
- while(1){
- x = random()%20;
- y = random()%20;
- sprintf(buf, "%d %d", x, y);
- printf("buf = %s n", buf);
- write(fd, buf, 20);
- //fsync(fd);
- sleep(1);
- }
- */
- printf("~~~~~~~~~~~~~success~~~~~~~~~~~~n");
- return 0;
- }
复制代码
这就是全部的代码,下面一节将写关于USB的驱动,与小米 手机连接
0
|
|
|
|