在USB设备的逻辑组织中,包含设备、配置、接口和端点4个层次。
- /* USB_DT_DEVICE: Device descriptor */
- struct usb_device_descriptor {
- __u8 bLength;
- __u8 bDescriptorType;
- __le16 bcdUSB;
- __u8 bDeviceClass;
- __u8 bDeviceSubClass;
- __u8 bDeviceProtocol;
- __u8 bMaxPacketSize0;
- __le16 idVendor;
- __le16 idProduct;
- __le16 bcdDevice;
- __u8 iManufacturer;
- __u8 iProduct;
- __u8 iSerialNumber;
- __u8 bNumConfigurations;
- } __attribute__ ((packed));
- /* USB_DT_CONFIG: Configuration descriptor information.
- *
- * USB_DT_OTHER_SPEED_CONFIG is the same descriptor, except that the
- * descriptor type is different. Highspeed-capable devices can look
- * different depending on what speed they're currently running. Only
- * devices with a USB_DT_DEVICE_QUALIFIER have any OTHER_SPEED_CONFIG
- * descriptors.
- */
- struct usb_config_descriptor {
- __u8 bLength;
- __u8 bDescriptorType;
- __le16 wTotalLength;
- __u8 bNumInterfaces;
- __u8 bConfigurationValue;
- __u8 iConfiguration;
- __u8 bmAttributes;
- __u8 bMaxPower;
- } __attribute__ ((packed));
- /* USB_DT_INTERFACE: Interface descriptor */
- struct usb_interface_descriptor {
- __u8 bLength;
- __u8 bDescriptorType;
- __u8 bInterfaceNumber;
- __u8 bAlternateSetting;
- __u8 bNumEndpoints;
- __u8 bInterfaceClass;
- __u8 bInterfaceSubClass;
- __u8 bInterfaceProtocol;
- __u8 iInterface;
- } __attribute__ ((packed));
- /* USB_DT_ENDPOINT: Endpoint descriptor */
- struct usb_endpoint_descriptor {
- __u8 bLength;
- __u8 bDescriptorType;
- __u8 bEndpointAddress;
- __u8 bmAttributes;
- __le16 wMaxPacketSize;
- __u8 bInterval;
- /* NOTE: these two are _only_ in audio endpoints. */
- /* use USB_DT_ENDPOINT*_SIZE in bLength, not sizeof. */
- __u8 bRefresh;
- __u8 bSynchAddress;
- } __attribute__ ((packed));
关于usb抓包可以看Document/usb/usbmon.txt
- #include <linux/kernel.h>
- #include <linux/slab.h>
- #include <linux/input.h>
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/usb.h>
- #include <linux/usb/input.h>
- #include <linux/hid.h>
- #define DRIVER_VERSION "v1.0"
- #define DRIVER_AUTHOR "hacker.do@163.com"
- #define DRIVER_DESC "USB Touchscreen Driver"
- #define CONFIG_TOUCHSCREEN_USB_IR_TOUCH
- #define LCD_X_SIZE 320
- #define LCD_Y_SIZE 480
- static int swap_xy;
- module_param(swap_xy, bool, 0644);
- MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
- static int hwcalib_xy;
- module_param(hwcalib_xy, bool, 0644);
- MODULE_PARM_DESC(hwcalib_xy, "If set hw-calibrated X/Y are used if available");
- /* device specifc data/functions */
- struct usbtouch_usb;
- struct usbtouch_device_info {
- int min_xc, max_xc;
- int min_yc, max_yc;
- int min_press, max_press;
- int rept_size;
- /*
- * Always service the USB devices irq not just when the input device is
- * open. This is useful when devices have a watchdog which prevents us
- * from periodically polling the device. Leave this unset unless your
- * touchscreen device requires it, as it does consume more of the USB
- * bandwidth.
- */
- bool irq_always;
- void (*process_pkt) (struct usbtouch_usb *usbtouch, unsigned char *pkt, int len);
- /*
- * used to get the packet len. possible return values:
- * > 0: packet len
- * = 0: skip one byte
- * < 0: -return value more bytes needed
- */
- int (*get_pkt_len) (unsigned char *pkt, int len);
- int (*read_data) (struct usbtouch_usb *usbtouch, unsigned char *pkt);
- int (*alloc) (struct usbtouch_usb *usbtouch);
- int (*init) (struct usbtouch_usb *usbtouch);
- void (*exit) (struct usbtouch_usb *usbtouch);
- };
- /* a usbtouch device */
- struct usbtouch_usb {
- unsigned char *data;
- dma_addr_t data_dma;
- unsigned char *buffer;
- int buf_len;
- struct urb *irq;
- struct usb_interface *interface;
- struct input_dev *input;
- struct usbtouch_device_info *type;
- char name[128];
- char phys[64];
- void *priv;
- int x, y;
- int touch, press;
- };
- /* device types */
- enum {
- DEVTYPE_IGNORE = -1,
- DEVTYPE_EGALAX,
- DEVTYPE_PANJIT,
- DEVTYPE_3M,
- DEVTYPE_ITM,
- DEVTYPE_ETURBO,
- DEVTYPE_GUNZE,
- DEVTYPE_DMC_TSC10,
- DEVTYPE_IRTOUCH,
- DEVTYPE_IDEALTEK,
- DEVTYPE_GENERAL_TOUCH,
- DEVTYPE_GOTOP,
- DEVTYPE_JASTEC,
- DEVTYPE_E2I,
- DEVTYPE_ZYTRONIC,
- DEVTYPE_TC45USB,
- DEVTYPE_NEXIO,
- };
- static const struct usb_device_id usbtouch_devices[] = {
- #ifdef CONFIG_TOUCHSCREEN_USB_IR_TOUCH
- {USB_DEVICE(0x10c4, 0x8002), .driver_info = DEVTYPE_IRTOUCH},
- #endif
- {}
- };
- /*****************************************************************************
- * IRTOUCH Part
- */
- #ifdef CONFIG_TOUCHSCREEN_USB_IR_TOUCH
- static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
- {
- //printk("finger num is %d, P1_x : %d, P1_y : %d\n", pkt[1], pkt[3], pkt[5]);
- //printk("%4d %4d %4d %4d %4d %4d %4d %4d\n", pkt[15], pkt[14], pkt[13], pkt[12], pkt[11], pkt[10], pkt[9], pkt[8]);
- return 1;
- }
- #endif
- static struct usbtouch_device_info usbtouch_dev_info[] = {
- #ifdef CONFIG_TOUCHSCREEN_USB_IR_TOUCH
- [DEVTYPE_IRTOUCH] = {
- .min_xc = 0x0,
- .max_xc = 0x0fff,
- .min_yc = 0x0,
- .max_yc = 0x0fff,
- .rept_size = 32,
- .read_data = irtouch_read_data,
- },
- #endif
- };
- /*****************************************************************************
- * Generic Part
- */
- static void usbtouch_process_pkt(struct usbtouch_usb *usbtouch,
- unsigned char *pkt, int len)
- {
- struct usbtouch_device_info *type = usbtouch->type;
- volatile int x, y;
- if (!type->read_data(usbtouch, pkt))
- return;
- if (!swap_xy) {
- x = pkt[3] * LCD_X_SIZE / 26;
- y = pkt[5] * LCD_Y_SIZE / 15;
- } else {
- y = pkt[3] * LCD_X_SIZE / 26;
- x = pkt[5] * LCD_Y_SIZE / 15;
- }
- if (pkt[1] == 1) {
- input_report_abs(usbtouch->input, ABS_MT_TOUCH_MAJOR, 1);
- input_report_abs(usbtouch->input, ABS_MT_POSITION_X, x);
- input_report_abs(usbtouch->input, ABS_MT_POSITION_Y, y);
- input_mt_sync(usbtouch->input);
- } else {
- input_report_abs(usbtouch->input, ABS_MT_TOUCH_MAJOR, 0);
- input_mt_sync(usbtouch->input);
- }
- input_sync(usbtouch->input);
- }
- #ifdef MULTI_PACKET
- static void usbtouch_process_multi(struct usbtouch_usb *usbtouch,
- unsigned char *pkt, int len)
- {
- unsigned char *buffer;
- int pkt_len, pos, buf_len, tmp;
- /* process buffer */
- if (unlikely(usbtouch->buf_len)) {
- /* try to get size */
- pkt_len = usbtouch->type->get_pkt_len(
- usbtouch->buffer, usbtouch->buf_len);
- /* drop? */
- if (unlikely(!pkt_len))
- goto out_flush_buf;
- /* need to append -pkt_len bytes before able to get size */
- if (unlikely(pkt_len < 0)) {
- int append = -pkt_len;
- if (unlikely(append > len))
- append = len;
- if (usbtouch->buf_len + append >= usbtouch->type->rept_size)
- goto out_flush_buf;
- memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, append);
- usbtouch->buf_len += append;
- pkt_len = usbtouch->type->get_pkt_len(
- usbtouch->buffer, usbtouch->buf_len);
- if (pkt_len < 0)
- return;
- }
- /* append */
- tmp = pkt_len - usbtouch->buf_len;
- if (usbtouch->buf_len + tmp >= usbtouch->type->rept_size)
- goto out_flush_buf;
- memcpy(usbtouch->buffer + usbtouch->buf_len, pkt, tmp);
- usbtouch_process_pkt(usbtouch, usbtouch->buffer, pkt_len);
- buffer = pkt + tmp;
- buf_len = len - tmp;
- } else {
- buffer = pkt;
- buf_len = len;
- }
- /* loop over the received packet, process */
- pos = 0;
- while (pos < buf_len) {
- /* get packet len */
- pkt_len = usbtouch->type->get_pkt_len(buffer + pos,
- buf_len - pos);
- /* unknown packet: skip one byte */
- if (unlikely(!pkt_len)) {
- pos++;
- continue;
- }
- /* full packet: process */
- if (likely((pkt_len > 0) && (pkt_len <= buf_len - pos))) {
- usbtouch_process_pkt(usbtouch, buffer + pos, pkt_len);
- } else {
- /* incomplete packet: save in buffer */
- memcpy(usbtouch->buffer, buffer + pos, buf_len - pos);
- usbtouch->buf_len = buf_len - pos;
- return;
- }
- pos += pkt_len;
- }
- out_flush_buf:
- usbtouch->buf_len = 0;
- return;
- }
- #endif
- static void usbtouch_irq(struct urb *urb)
- {
- struct usbtouch_usb *usbtouch = urb->context;
- int retval;
- switch (urb->status) {
- case 0:
- /* success */
- break;
- case -ETIME:
- /* this urb is timing out */
- dbg("%s - urb timed out - was the device unplugged?",
- __func__);
- return;
- case -ECONNRESET:
- case -ENOENT:
- case -ESHUTDOWN:
- case -EPIPE:
- /* this urb is terminated, clean up */
- dbg("%s - urb shutting down with status: %d",
- __func__, urb->status);
- return;
- default:
- dbg("%s - nonzero urb status received: %d",
- __func__, urb->status);
- goto exit;
- }
- usbtouch->type->process_pkt(usbtouch, usbtouch->data, urb->actual_length);
- exit:
- usb_mark_last_busy(interface_to_usbdev(usbtouch->interface));
- retval = usb_submit_urb(urb, GFP_ATOMIC);
- if (retval)
- err("%s - usb_submit_urb failed with result: %d",
- __func__, retval);
- }
- static int usbtouch_open(struct input_dev *input)
- {
- struct usbtouch_usb *usbtouch = input_get_drvdata(input);
- int r;
- usbtouch->irq->dev = interface_to_usbdev(usbtouch->interface);
- r = usb_autopm_get_interface(usbtouch->interface) ? -EIO : 0;
- if (r < 0)
- goto out;
- if (!usbtouch->type->irq_always) {
- if (usb_submit_urb(usbtouch->irq, GFP_KERNEL)) {
- r = -EIO;
- goto out_put;
- }
- }
- usbtouch->interface->needs_remote_wakeup = 1;
- out_put:
- usb_autopm_put_interface(usbtouch->interface);
- out:
- return r;
- }
- static void usbtouch_close(struct input_dev *input)
- {
- struct usbtouch_usb *usbtouch = input_get_drvdata(input);
- int r;
- if (!usbtouch->type->irq_always)
- usb_kill_urb(usbtouch->irq);
- r = usb_autopm_get_interface(usbtouch->interface);
- usbtouch->interface->needs_remote_wakeup = 0;
- if (!r)
- usb_autopm_put_interface(usbtouch->interface);
- }
- static int usbtouch_suspend
- (struct usb_interface *intf, pm_message_t message)
- {
- struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
- usb_kill_urb(usbtouch->irq);
- return 0;
- }
- static int usbtouch_resume(struct usb_interface *intf)
- {
- struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
- struct input_dev *input = usbtouch->input;
- int result = 0;
- mutex_lock(&input->mutex);
- if (input->users || usbtouch->type->irq_always)
- result = usb_submit_urb(usbtouch->irq, GFP_NOIO);
- mutex_unlock(&input->mutex);
- return result;
- }
- static int usbtouch_reset_resume(struct usb_interface *intf)
- {
- struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
- struct input_dev *input = usbtouch->input;
- int err = 0;
- /* reinit the device */
- if (usbtouch->type->init) {
- err = usbtouch->type->init(usbtouch);
- if (err) {
- dbg("%s - type->init() failed, err: %d",
- __func__, err);
- return err;
- }
- }
- /* restart IO if needed */
- mutex_lock(&input->mutex);
- if (input->users)
- err = usb_submit_urb(usbtouch->irq, GFP_NOIO);
- mutex_unlock(&input->mutex);
- return err;
- }
- static void usbtouch_free_buffers(struct usb_device *udev,
- struct usbtouch_usb *usbtouch)
- {
- usb_free_coherent(udev, usbtouch->type->rept_size,
- usbtouch->data, usbtouch->data_dma);
- kfree(usbtouch->buffer);
- }
- static struct usb_endpoint_descriptor *
- usbtouch_get_input_endpoint(struct usb_host_interface *interface)
- {
- int i;
- for (i = 0; i < interface->desc.bNumEndpoints; i++)
- if (usb_endpoint_dir_in(&interface->endpoint[i].desc))
- return &interface->endpoint[i].desc;
- return NULL;
- }
- static int usbtouch_probe(struct usb_interface *intf,
- const struct usb_device_id *id)
- {
- struct usbtouch_usb *usbtouch;
- struct input_dev *input_dev;
- struct usb_endpoint_descriptor *endpoint;
- struct usb_device *udev = interface_to_usbdev(intf);
- struct usbtouch_device_info *type;
- int err = -ENOMEM;
- /* some devices are ignored */
- if (id->driver_info == DEVTYPE_IGNORE)
- return -ENODEV;
- endpoint = usbtouch_get_input_endpoint(intf->cur_altsetting);
- if (!endpoint)
- return -ENXIO;
- usbtouch = kzalloc(sizeof(struct usbtouch_usb), GFP_KERNEL);
- input_dev = input_allocate_device();
- if (!usbtouch || !input_dev)
- goto out_free;
- type = &usbtouch_dev_info[id->driver_info];
- usbtouch->type = type;
- if (!type->process_pkt)
- type->process_pkt = usbtouch_process_pkt;
- usbtouch->data = usb_alloc_coherent(udev, type->rept_size,
- GFP_KERNEL, &usbtouch->data_dma);
- if (!usbtouch->data)
- goto out_free;
- if (type->get_pkt_len) {
- usbtouch->buffer = kmalloc(type->rept_size, GFP_KERNEL);
- if (!usbtouch->buffer)
- goto out_free_buffers;
- }
- usbtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
- if (!usbtouch->irq) {
- dbg("%s - usb_alloc_urb failed: usbtouch->irq", __func__);
- goto out_free_buffers;
- }
- usbtouch->interface = intf;
- usbtouch->input = input_dev;
- if (udev->manufacturer)
- strlcpy(usbtouch->name, udev->manufacturer, sizeof(usbtouch->name));
- if (udev->product) {
- if (udev->manufacturer)
- strlcat(usbtouch->name, " ", sizeof(usbtouch->name));
- strlcat(usbtouch->name, udev->product, sizeof(usbtouch->name));
- }
- if (!strlen(usbtouch->name))
- snprintf(usbtouch->name, sizeof(usbtouch->name),
- "USB Touchscreen %04x:%04x",
- le16_to_cpu(udev->descriptor.idVendor),
- le16_to_cpu(udev->descriptor.idProduct));
- usb_make_path(udev, usbtouch->phys, sizeof(usbtouch->phys));
- strlcat(usbtouch->phys, "/input0", sizeof(usbtouch->phys));
- input_dev->name = usbtouch->name;
- input_dev->phys = usbtouch->phys;
- usb_to_input_id(udev, &input_dev->id);
- input_dev->dev.parent = &intf->dev;
- input_set_drvdata(input_dev, usbtouch);
- input_dev->open = usbtouch_open;
- input_dev->close = usbtouch_close;
- input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
- input_set_abs_params(input_dev, ABS_MT_POSITION_X, type->min_xc, type->max_xc, 0, 0);
- input_set_abs_params(input_dev, ABS_MT_POSITION_Y, type->min_yc, type->max_yc, 0, 0);
- input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 1, 0, 0);
- if (usb_endpoint_type(endpoint) == USB_ENDPOINT_XFER_INT)
- usb_fill_int_urb(usbtouch->irq, udev,
- usb_rcvintpipe(udev, endpoint->bEndpointAddress),
- usbtouch->data, type->rept_size,
- usbtouch_irq, usbtouch, endpoint->bInterval);
- else
- usb_fill_bulk_urb(usbtouch->irq, udev,
- usb_rcvbulkpipe(udev, endpoint->bEndpointAddress),
- usbtouch->data, type->rept_size,
- usbtouch_irq, usbtouch);
- usbtouch->irq->dev = udev;
- usbtouch->irq->transfer_dma = usbtouch->data_dma;
- usbtouch->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
- /* device specific allocations */
- if (type->alloc) {
- err = type->alloc(usbtouch);
- if (err) {
- dbg("%s - type->alloc() failed, err: %d", __func__, err);
- goto out_free_urb;
- }
- }
- /* device specific initialisation*/
- if (type->init) {
- err = type->init(usbtouch);
- if (err) {
- dbg("%s - type->init() failed, err: %d", __func__, err);
- goto out_do_exit;
- }
- }
- err = input_register_device(usbtouch->input);
- if (err) {
- dbg("%s - input_register_device failed, err: %d", __func__, err);
- goto out_do_exit;
- }
- usb_set_intfdata(intf, usbtouch);
- if (usbtouch->type->irq_always) {
- /* this can't fail */
- usb_autopm_get_interface(intf);
- err = usb_submit_urb(usbtouch->irq, GFP_KERNEL);
- if (err) {
- usb_autopm_put_interface(intf);
- err("%s - usb_submit_urb failed with result: %d",
- __func__, err);
- goto out_unregister_input;
- }
- }
- return 0;
- out_unregister_input:
- input_unregister_device(input_dev);
- input_dev = NULL;
- out_do_exit:
- if (type->exit)
- type->exit(usbtouch);
- out_free_urb:
- usb_free_urb(usbtouch->irq);
- out_free_buffers:
- usbtouch_free_buffers(udev, usbtouch);
- out_free:
- input_free_device(input_dev);
- kfree(usbtouch);
- return err;
- }
- static void usbtouch_disconnect(struct usb_interface *intf)
- {
- struct usbtouch_usb *usbtouch = usb_get_intfdata(intf);
- dbg("%s - called", __func__);
- if (!usbtouch)
- return;
- dbg("%s - usbtouch is initialized, cleaning up", __func__);
- usb_set_intfdata(intf, NULL);
- /* this will stop IO via close */
- input_unregister_device(usbtouch->input);
- usb_free_urb(usbtouch->irq);
- if (usbtouch->type->exit)
- usbtouch->type->exit(usbtouch);
- usbtouch_free_buffers(interface_to_usbdev(intf), usbtouch);
- kfree(usbtouch);
- }
- MODULE_DEVICE_TABLE(usb, usbtouch_devices);
- static struct usb_driver usbtouch_driver = {
- .name = "usbtouchscreen",
- .probe = usbtouch_probe,
- .disconnect = usbtouch_disconnect,
- .suspend = usbtouch_suspend,
- .resume = usbtouch_resume,
- .reset_resume = usbtouch_reset_resume,
- .id_table = usbtouch_devices,
- .supports_autosuspend = 1,
- };
- static int __init usbtouch_init(void)
- {
- return usb_register(&usbtouch_driver);
- }
- static void __exit usbtouch_cleanup(void)
- {
- usb_deregister(&usbtouch_driver);
- }
- module_init(usbtouch_init);
- module_exit(usbtouch_cleanup);
- MODULE_AUTHOR(DRIVER_AUTHOR);
- MODULE_DESCRIPTION(DRIVER_DESC);
- MODULE_LICENSE("GPL");
- MODULE_ALIAS("touchkitusb");
- MODULE_ALIAS("itmtouch");
- MODULE_ALIAS("mtouchusb");
阅读(1164) | 评论(0) | 转发(0) |