kbtab.c 5.2 KB

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