datasheet

Tiny210驱动之USB设备驱动程序

2018-10-12来源: eefocus 关键字:Tiny210驱动  USB设备  驱动程序

usbmouse_as_key.c驱动源码:

//参考源码: drivers\hid\usbhid\usbmouse.c

 

#include "linux/kernel.h"

#include "linux/slab.h"

#include "linux/module.h"

#include "linux/init.h"

#include "linux/usb/input.h"

#include "linux/hid.h"

static struct input_dev *uk_dev;

static char *usb_buf;

static dma_addr_t usb_buf_phys;

static int len;

static struct urb *uk_urb;

static struct usb_device_id usbmouse_as_key_id_table [] = {

    {   USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,

        USB_INTERFACE_PROTOCOL_MOUSE) 

    },

    //{USB_DEVICE(0x1234,0x5678)},

    { }    // Terminating entry 

};

static void usbmouse_as_key_irq(struct urb *urb)

{

    static unsigned char pre_val;

#if 0    

    int i;

    static int cnt = 0;

    printk("data cnt %d: ", ++cnt);

    for (i = 0; i < len; i++)

    {

        printk("x ", usb_buf[i]);

    }

    printk("\n");

#endif

    // USB鼠标数据含义

    // data[0]: bit0-左键, 1-按下, 0-松开

    //          bit1-右键, 1-按下, 0-松开

    //          bit2-中键, 1-按下, 0-松开 

    //

    if ((pre_val & (1<<0)) != (usb_buf[0] & (1<<0)))

    {

        // 左键发生了变化 

        input_event(uk_dev, EV_KEY, KEY_L, (usb_buf[0] & (1<<0)) ? 1 : 0);

        input_sync(uk_dev);

    }

    if ((pre_val & (1<<1)) != (usb_buf[0] & (1<<1)))

    {

        // 右键发生了变化 

        input_event(uk_dev, EV_KEY, KEY_S, (usb_buf[0] & (1<<1)) ? 1 : 0);

        input_sync(uk_dev);

    }

    if ((pre_val & (1<<2)) != (usb_buf[0] & (1<<2)))

    {

        // 中键发生了变化 

        input_event(uk_dev, EV_KEY, KEY_ENTER, (usb_buf[0] & (1<<2)) ? 1 : 0);

        input_sync(uk_dev);

    }

    

    pre_val = usb_buf[0];

    // 重新提交urb 

    usb_submit_urb(uk_urb, GFP_KERNEL);

}

static int usbmouse_as_key_probe(struct usb_interface *intf, const struct usb_device_id *id)

{

    struct usb_device *dev = interface_to_usbdev(intf);

    struct usb_host_interface *interface;

    struct usb_endpoint_descriptor *endpoint;

    int pipe;

    

    interface = intf->cur_altsetting;

    endpoint = &interface->endpoint[0].desc;

    // a. 分配一个input_dev 

    uk_dev = input_allocate_device();

    

    // b. 设置 

    // b.1 能产生哪类事件 

    set_bit(EV_KEY, uk_dev->evbit);

    set_bit(EV_REP, uk_dev->evbit);

    

    // b.2 能产生哪些事件 

    set_bit(KEY_L, uk_dev->keybit);

    set_bit(KEY_S, uk_dev->keybit);

    set_bit(KEY_ENTER, uk_dev->keybit);

    

    // c. 注册 

    input_register_device(uk_dev);

    

    // d. 硬件相关操作 

    // 数据传输3要素: 源,目的,长度 

    // 源: USB设备的某个端点 

    pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);

    // 长度: 

    len = endpoint->wMaxPacketSize;

    // 目的: 

    usb_buf = usb_alloc_coherent(dev, len, GFP_ATOMIC, &usb_buf_phys);

    // 使用"3要素" 

    // 分配usb request block 

    uk_urb = usb_alloc_urb(0, GFP_KERNEL);

    // 使用"3要素设置urb" 

    usb_fill_int_urb(uk_urb, dev, pipe, usb_buf, len, usbmouse_as_key_irq, NULL, endpoint->bInterval);

    uk_urb->transfer_dma = usb_buf_phys;

    uk_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;

    // 使用URB 

    usb_submit_urb(uk_urb, GFP_KERNEL);

    

    return 0;

}

static void usbmouse_as_key_disconnect(struct usb_interface *intf)

{

    struct usb_device *dev = interface_to_usbdev(intf);

    //printk("disconnect usbmouse!\n");

    usb_kill_urb(uk_urb);

    usb_free_urb(uk_urb);

    usb_free_coherent(dev, len, usb_buf, usb_buf_phys);

    input_unregister_device(uk_dev);

    input_free_device(uk_dev);

}

// 1. 分配/设置usb_driver 

static struct usb_driver usbmouse_as_key_driver = {

    .name         = "usbmouse_as_key_",

    .probe         = usbmouse_as_key_probe,

    .disconnect = usbmouse_as_key_disconnect,

    .id_table      = usbmouse_as_key_id_table,

};

static int usbmouse_as_key_init(void)

{

    // 2. 注册 

    usb_register(&usbmouse_as_key_driver);

    return 0;

}

static void usbmouse_as_key_exit(void)

{

    usb_deregister(&usbmouse_as_key_driver);    

}

module_init(usbmouse_as_key_init);

module_exit(usbmouse_as_key_exit);

MODULE_LICENSE("GPL");


关键字:Tiny210驱动  USB设备  驱动程序

编辑:什么鱼 引用地址:http://www.eeworld.com.cn/mcu/2018/ic-news101241696.html
本网站转载的所有的文章、图片、音频视频文件等资料的版权归版权所有人所有,本站采用的非本站原创文章及图片等内容无法一一联系确认版权者。如果本网所选内容的文章作者及编辑认为其作品不宜公开自由传播,或不应无偿使用,请及时通过电子邮件或电话通知我们,以迅速采取适当措施,避免给双方造成不必要的经济损失。

上一篇:Tiny210块设备驱动之内存模拟磁盘
下一篇:Tiny210触摸屏之一线触摸屏驱动

关注eeworld公众号 快捷获取更多信息
关注eeworld公众号
快捷获取更多信息
关注eeworld服务号 享受更多官方福利
关注eeworld服务号
享受更多官方福利

推荐阅读

Tiny210驱动之LED测试

first_drv.c驱动源码:#include "linux/device.h"#include "linux/module.h"#include "linux/kernel.h"#include "linux/fs.h"#include "linux/init.h"#include "linux/delay.h"#include "asm/uaccess.h"#include "asm/irq.h"#include "asm/io.h"stati
发表于 2018-10-12

Tiny210驱动之KEY测试

second_drv.c驱动源码:#include "linux/device.h"#include "linux/module.h"#include "linux/kernel.h"#include "linux/fs.h"#include "linux/init.h"#include "linux/delay.h"#include "asm/uaccess.h"#include "asm/irq.h"#include "asm/io.h"stati
发表于 2018-10-12

Tiny210驱动之按键中断实验

[5] = {    {S5PV210_GPH2(3), 0x01},    {S5PV210_GPH3(0), 0x02},    {S5PV210_GPH3(1), 0x03},    {S5PV210_GPH3(2), 0x04},    {S5PV210_GPH3(3), 0x05},};// 确定按键值 static irqreturn_t buttons_irq(int irq, void *dev_id){ 
发表于 2018-10-12

Tiny210驱动之按键poll机制

;struct pin_desc pins_desc[5] = {    {S5PV210_GPH2(3), 0x01},    {S5PV210_GPH3(0), 0x02},    {S5PV210_GPH3(1), 0x03},    {S5PV210_GPH3(2), 0x04},    {S5PV210_GPH3(3), 0x05},};// 确定按键值static irqreturn_t buttons_irq(int
发表于 2018-10-12

Tiny210驱动之按键异步通知

, 0x84, 0x85 static unsigned char key_val;struct pin_desc pins_desc[5] = {    {S5PV210_GPH2(3), 0x01},    {S5PV210_GPH3(0), 0x02},    {S5PV210_GPH3(1), 0x03},    {S5PV210_GPH3(2), 0x04},    {S5PV210_GPH3(3), 0x05
发表于 2018-10-12

Tiny210按键互斥锁、原子变量驱动

, 0x84 static unsigned char key_val;// 键值: 按下时, 0x01, 0x02, 0x03, 0x04, 0x05 // 键值: 松开时, 0x81, 0x82, 0x83, 0x84, 0x85 static unsigned char key_val;struct pin_desc pins_desc[5] = {    {S5PV210_GPH2(3), 0x01},    {S5PV210_GPH3(0), 0x02},    {S5PV210
发表于 2018-10-12

小广播

何立民专栏

单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

电子工程世界版权所有 京ICP证060456号 京ICP备10001474号 电信业务审批[2006]字第258号函 京公海网安备110108001534 Copyright © 2005-2018 EEWORLD.com.cn, Inc. All rights reserved
pt type="text/javascript" src="//v3.jiathis.com/code/jia.js?uid=2113614" charset="utf-8">