kbtab.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <linux/kernel.h>
  2. #include <linux/slab.h>
  3. #include <linux/module.h>
  4. #include <linux/init.h>
  5. #include <linux/usb/input.h>
  6. #include <asm/unaligned.h>
  7. /*
  8. * Version Information
  9. * v0.0.1 - Original, extremely basic version, 2.4.xx only
  10. * v0.0.2 - Updated, works with 2.5.62 and 2.4.20;
  11. * - added pressure-threshold modules param code from
  12. * Alex Perry <alex.perry@ieee.org>
  13. */
  14. #define DRIVER_VERSION "v0.0.2"
  15. #define DRIVER_AUTHOR "Josh Myer <josh@joshisanerd.com>"
  16. #define DRIVER_DESC "USB KB Gear JamStudio Tablet driver"
  17. #define DRIVER_LICENSE "GPL"
  18. MODULE_AUTHOR(DRIVER_AUTHOR);
  19. MODULE_DESCRIPTION(DRIVER_DESC);
  20. MODULE_LICENSE(DRIVER_LICENSE);
  21. #define USB_VENDOR_ID_KBGEAR 0x084e
  22. static int kb_pressure_click = 0x10;
  23. module_param(kb_pressure_click, int, 0);
  24. MODULE_PARM_DESC(kb_pressure_click, "pressure threshold for clicks");
  25. struct kbtab {
  26. unsigned char *data;
  27. dma_addr_t data_dma;
  28. struct input_dev *dev;
  29. struct usb_device *usbdev;
  30. struct urb *irq;
  31. char phys[32];
  32. };
  33. static void kbtab_irq(struct urb *urb)
  34. {
  35. struct kbtab *kbtab = urb->context;
  36. unsigned char *data = kbtab->data;
  37. struct input_dev *dev = kbtab->dev;
  38. int pressure;
  39. int retval;
  40. switch (urb->status) {
  41. case 0:
  42. /* success */
  43. break;
  44. case -ECONNRESET:
  45. case -ENOENT:
  46. case -ESHUTDOWN:
  47. /* this urb is terminated, clean up */
  48. dbg("%s - urb shutting down with status: %d", __func__, urb->status);
  49. return;
  50. default:
  51. dbg("%s - nonzero urb status received: %d", __func__, urb->status);
  52. goto exit;
  53. }
  54. input_report_key(dev, BTN_TOOL_PEN, 1);
  55. input_report_abs(dev, ABS_X, get_unaligned_le16(&data[1]));
  56. input_report_abs(dev, ABS_Y, get_unaligned_le16(&data[3]));
  57. /*input_report_key(dev, BTN_TOUCH , data[0] & 0x01);*/
  58. input_report_key(dev, BTN_RIGHT, data[0] & 0x02);
  59. pressure = data[5];
  60. if (kb_pressure_click == -1)
  61. input_report_abs(dev, ABS_PRESSURE, pressure);
  62. else
  63. input_report_key(dev, BTN_LEFT, pressure > kb_pressure_click ? 1 : 0);
  64. input_sync(dev);
  65. exit:
  66. retval = usb_submit_urb(urb, GFP_ATOMIC);
  67. if (retval)
  68. err("%s - usb_submit_urb failed with result %d",
  69. __func__, retval);
  70. }
  71. static struct usb_device_id kbtab_ids[] = {
  72. { USB_DEVICE(USB_VENDOR_ID_KBGEAR, 0x1001), .driver_info = 0 },
  73. { }
  74. };
  75. MODULE_DEVICE_TABLE(usb, kbtab_ids);
  76. static int kbtab_open(struct input_dev *dev)
  77. {
  78. struct kbtab *kbtab = input_get_drvdata(dev);
  79. kbtab->irq->dev = kbtab->usbdev;
  80. if (usb_submit_urb(kbtab->irq, GFP_KERNEL))
  81. return -EIO;
  82. return 0;
  83. }
  84. static void kbtab_close(struct input_dev *dev)
  85. {
  86. struct kbtab *kbtab = input_get_drvdata(dev);
  87. usb_kill_urb(kbtab->irq);
  88. }
  89. static int kbtab_probe(struct usb_interface *intf, const struct usb_device_id *id)
  90. {
  91. struct usb_device *dev = interface_to_usbdev(intf);
  92. struct usb_endpoint_descriptor *endpoint;
  93. struct kbtab *kbtab;
  94. struct input_dev *input_dev;
  95. int error = -ENOMEM;
  96. kbtab = kzalloc(sizeof(struct kbtab), GFP_KERNEL);
  97. input_dev = input_allocate_device();
  98. if (!kbtab || !input_dev)
  99. goto fail1;
  100. kbtab->data = usb_alloc_coherent(dev, 8, GFP_KERNEL, &kbtab->data_dma);
  101. if (!kbtab->data)
  102. goto fail1;
  103. kbtab->irq = usb_alloc_urb(0, GFP_KERNEL);
  104. if (!kbtab->irq)
  105. goto fail2;
  106. kbtab->usbdev = dev;
  107. kbtab->dev = input_dev;
  108. usb_make_path(dev, kbtab->phys, sizeof(kbtab->phys));
  109. strlcat(kbtab->phys, "/input0", sizeof(kbtab->phys));
  110. input_dev->name = "KB Gear Tablet";
  111. input_dev->phys = kbtab->phys;
  112. usb_to_input_id(dev, &input_dev->id);
  113. input_dev->dev.parent = &intf->dev;
  114. input_set_drvdata(input_dev, kbtab);
  115. input_dev->open = kbtab_open;
  116. input_dev->close = kbtab_close;
  117. input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  118. input_dev->keybit[BIT_WORD(BTN_LEFT)] |=
  119. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
  120. input_dev->keybit[BIT_WORD(BTN_DIGI)] |=
  121. BIT_MASK(BTN_TOOL_PEN) | BIT_MASK(BTN_TOUCH);
  122. input_set_abs_params(input_dev, ABS_X, 0, 0x2000, 4, 0);
  123. input_set_abs_params(input_dev, ABS_Y, 0, 0x1750, 4, 0);
  124. input_set_abs_params(input_dev, ABS_PRESSURE, 0, 0xff, 0, 0);
  125. endpoint = &intf->cur_altsetting->endpoint[0].desc;
  126. usb_fill_int_urb(kbtab->irq, dev,
  127. usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  128. kbtab->data, 8,
  129. kbtab_irq, kbtab, endpoint->bInterval);
  130. kbtab->irq->transfer_dma = kbtab->data_dma;
  131. kbtab->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  132. error = input_register_device(kbtab->dev);
  133. if (error)
  134. goto fail3;
  135. usb_set_intfdata(intf, kbtab);
  136. return 0;
  137. fail3: usb_free_urb(kbtab->irq);
  138. fail2: usb_free_coherent(dev, 8, kbtab->data, kbtab->data_dma);
  139. fail1: input_free_device(input_dev);
  140. kfree(kbtab);
  141. return error;
  142. }
  143. static void kbtab_disconnect(struct usb_interface *intf)
  144. {
  145. struct kbtab *kbtab = usb_get_intfdata(intf);
  146. usb_set_intfdata(intf, NULL);
  147. input_unregister_device(kbtab->dev);
  148. usb_free_urb(kbtab->irq);
  149. usb_free_coherent(kbtab->usbdev, 8, kbtab->data, kbtab->data_dma);
  150. kfree(kbtab);
  151. }
  152. static struct usb_driver kbtab_driver = {
  153. .name = "kbtab",
  154. .probe = kbtab_probe,
  155. .disconnect = kbtab_disconnect,
  156. .id_table = kbtab_ids,
  157. };
  158. module_usb_driver(kbtab_driver);