pegasus_notetaker.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Pegasus Mobile Notetaker Pen input tablet driver
  3. *
  4. * Copyright (c) 2016 Martin Kepplinger <martink@posteo.de>
  5. */
  6. /*
  7. * request packet (control endpoint):
  8. * |-------------------------------------|
  9. * | Report ID | Nr of bytes | command |
  10. * | (1 byte) | (1 byte) | (n bytes) |
  11. * |-------------------------------------|
  12. * | 0x02 | n | |
  13. * |-------------------------------------|
  14. *
  15. * data packet after set xy mode command, 0x80 0xb5 0x02 0x01
  16. * and pen is in range:
  17. *
  18. * byte byte name value (bits)
  19. * --------------------------------------------
  20. * 0 status 0 1 0 0 0 0 X X
  21. * 1 color 0 0 0 0 H 0 S T
  22. * 2 X low
  23. * 3 X high
  24. * 4 Y low
  25. * 5 Y high
  26. *
  27. * X X battery state:
  28. * no state reported 0x00
  29. * battery low 0x01
  30. * battery good 0x02
  31. *
  32. * H Hovering
  33. * S Switch 1 (pen button)
  34. * T Tip
  35. */
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. #include <linux/input.h>
  39. #include <linux/usb/input.h>
  40. #include <linux/slab.h>
  41. #include <linux/workqueue.h>
  42. /* USB HID defines */
  43. #define USB_REQ_GET_REPORT 0x01
  44. #define USB_REQ_SET_REPORT 0x09
  45. #define USB_VENDOR_ID_PEGASUSTECH 0x0e20
  46. #define USB_DEVICE_ID_PEGASUS_NOTETAKER_EN100 0x0101
  47. /* device specific defines */
  48. #define NOTETAKER_REPORT_ID 0x02
  49. #define NOTETAKER_SET_CMD 0x80
  50. #define NOTETAKER_SET_MODE 0xb5
  51. #define NOTETAKER_LED_MOUSE 0x02
  52. #define PEN_MODE_XY 0x01
  53. #define SPECIAL_COMMAND 0x80
  54. #define BUTTON_PRESSED 0xb5
  55. #define COMMAND_VERSION 0xa9
  56. /* in xy data packet */
  57. #define BATTERY_NO_REPORT 0x40
  58. #define BATTERY_LOW 0x41
  59. #define BATTERY_GOOD 0x42
  60. #define PEN_BUTTON_PRESSED BIT(1)
  61. #define PEN_TIP BIT(0)
  62. struct pegasus {
  63. unsigned char *data;
  64. u8 data_len;
  65. dma_addr_t data_dma;
  66. struct input_dev *dev;
  67. struct usb_device *usbdev;
  68. struct usb_interface *intf;
  69. struct urb *irq;
  70. char name[128];
  71. char phys[64];
  72. struct work_struct init;
  73. };
  74. static int pegasus_control_msg(struct pegasus *pegasus, u8 *data, int len)
  75. {
  76. const int sizeof_buf = len + 2;
  77. int result;
  78. int error;
  79. u8 *cmd_buf;
  80. cmd_buf = kmalloc(sizeof_buf, GFP_KERNEL);
  81. if (!cmd_buf)
  82. return -ENOMEM;
  83. cmd_buf[0] = NOTETAKER_REPORT_ID;
  84. cmd_buf[1] = len;
  85. memcpy(cmd_buf + 2, data, len);
  86. result = usb_control_msg(pegasus->usbdev,
  87. usb_sndctrlpipe(pegasus->usbdev, 0),
  88. USB_REQ_SET_REPORT,
  89. USB_TYPE_VENDOR | USB_DIR_OUT,
  90. 0, 0, cmd_buf, sizeof_buf,
  91. USB_CTRL_SET_TIMEOUT);
  92. kfree(cmd_buf);
  93. if (unlikely(result != sizeof_buf)) {
  94. error = result < 0 ? result : -EIO;
  95. dev_err(&pegasus->usbdev->dev, "control msg error: %d\n",
  96. error);
  97. return error;
  98. }
  99. return 0;
  100. }
  101. static int pegasus_set_mode(struct pegasus *pegasus, u8 mode, u8 led)
  102. {
  103. u8 cmd[] = { NOTETAKER_SET_CMD, NOTETAKER_SET_MODE, led, mode };
  104. return pegasus_control_msg(pegasus, cmd, sizeof(cmd));
  105. }
  106. static void pegasus_parse_packet(struct pegasus *pegasus)
  107. {
  108. unsigned char *data = pegasus->data;
  109. struct input_dev *dev = pegasus->dev;
  110. u16 x, y;
  111. switch (data[0]) {
  112. case SPECIAL_COMMAND:
  113. /* device button pressed */
  114. if (data[1] == BUTTON_PRESSED)
  115. schedule_work(&pegasus->init);
  116. break;
  117. /* xy data */
  118. case BATTERY_LOW:
  119. dev_warn_once(&dev->dev, "Pen battery low\n");
  120. /* fall through */
  121. case BATTERY_NO_REPORT:
  122. case BATTERY_GOOD:
  123. x = le16_to_cpup((__le16 *)&data[2]);
  124. y = le16_to_cpup((__le16 *)&data[4]);
  125. /* pen-up event */
  126. if (x == 0 && y == 0)
  127. break;
  128. input_report_key(dev, BTN_TOUCH, data[1] & PEN_TIP);
  129. input_report_key(dev, BTN_RIGHT, data[1] & PEN_BUTTON_PRESSED);
  130. input_report_key(dev, BTN_TOOL_PEN, 1);
  131. input_report_abs(dev, ABS_X, (s16)x);
  132. input_report_abs(dev, ABS_Y, y);
  133. input_sync(dev);
  134. break;
  135. default:
  136. dev_warn_once(&pegasus->usbdev->dev,
  137. "unknown answer from device\n");
  138. }
  139. }
  140. static void pegasus_irq(struct urb *urb)
  141. {
  142. struct pegasus *pegasus = urb->context;
  143. struct usb_device *dev = pegasus->usbdev;
  144. int retval;
  145. switch (urb->status) {
  146. case 0:
  147. pegasus_parse_packet(pegasus);
  148. usb_mark_last_busy(pegasus->usbdev);
  149. break;
  150. case -ECONNRESET:
  151. case -ENOENT:
  152. case -ESHUTDOWN:
  153. dev_err(&dev->dev, "%s - urb shutting down with status: %d",
  154. __func__, urb->status);
  155. return;
  156. default:
  157. dev_err(&dev->dev, "%s - nonzero urb status received: %d",
  158. __func__, urb->status);
  159. break;
  160. }
  161. retval = usb_submit_urb(urb, GFP_ATOMIC);
  162. if (retval)
  163. dev_err(&dev->dev, "%s - usb_submit_urb failed with result %d",
  164. __func__, retval);
  165. }
  166. static void pegasus_init(struct work_struct *work)
  167. {
  168. struct pegasus *pegasus = container_of(work, struct pegasus, init);
  169. int error;
  170. error = pegasus_set_mode(pegasus, PEN_MODE_XY, NOTETAKER_LED_MOUSE);
  171. if (error)
  172. dev_err(&pegasus->usbdev->dev, "pegasus_set_mode error: %d\n",
  173. error);
  174. }
  175. static int pegasus_open(struct input_dev *dev)
  176. {
  177. struct pegasus *pegasus = input_get_drvdata(dev);
  178. int error;
  179. error = usb_autopm_get_interface(pegasus->intf);
  180. if (error)
  181. return error;
  182. pegasus->irq->dev = pegasus->usbdev;
  183. if (usb_submit_urb(pegasus->irq, GFP_KERNEL)) {
  184. error = -EIO;
  185. goto err_autopm_put;
  186. }
  187. error = pegasus_set_mode(pegasus, PEN_MODE_XY, NOTETAKER_LED_MOUSE);
  188. if (error)
  189. goto err_kill_urb;
  190. return 0;
  191. err_kill_urb:
  192. usb_kill_urb(pegasus->irq);
  193. cancel_work_sync(&pegasus->init);
  194. err_autopm_put:
  195. usb_autopm_put_interface(pegasus->intf);
  196. return error;
  197. }
  198. static void pegasus_close(struct input_dev *dev)
  199. {
  200. struct pegasus *pegasus = input_get_drvdata(dev);
  201. usb_kill_urb(pegasus->irq);
  202. cancel_work_sync(&pegasus->init);
  203. usb_autopm_put_interface(pegasus->intf);
  204. }
  205. static int pegasus_probe(struct usb_interface *intf,
  206. const struct usb_device_id *id)
  207. {
  208. struct usb_device *dev = interface_to_usbdev(intf);
  209. struct usb_endpoint_descriptor *endpoint;
  210. struct pegasus *pegasus;
  211. struct input_dev *input_dev;
  212. int error;
  213. int pipe;
  214. /* We control interface 0 */
  215. if (intf->cur_altsetting->desc.bInterfaceNumber >= 1)
  216. return -ENODEV;
  217. /* Sanity check that the device has an endpoint */
  218. if (intf->altsetting[0].desc.bNumEndpoints < 1) {
  219. dev_err(&intf->dev, "Invalid number of endpoints\n");
  220. return -EINVAL;
  221. }
  222. endpoint = &intf->cur_altsetting->endpoint[0].desc;
  223. pegasus = kzalloc(sizeof(*pegasus), GFP_KERNEL);
  224. input_dev = input_allocate_device();
  225. if (!pegasus || !input_dev) {
  226. error = -ENOMEM;
  227. goto err_free_mem;
  228. }
  229. pegasus->usbdev = dev;
  230. pegasus->dev = input_dev;
  231. pegasus->intf = intf;
  232. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  233. pegasus->data_len = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  234. pegasus->data = usb_alloc_coherent(dev, pegasus->data_len, GFP_KERNEL,
  235. &pegasus->data_dma);
  236. if (!pegasus->data) {
  237. error = -ENOMEM;
  238. goto err_free_mem;
  239. }
  240. pegasus->irq = usb_alloc_urb(0, GFP_KERNEL);
  241. if (!pegasus->irq) {
  242. error = -ENOMEM;
  243. goto err_free_dma;
  244. }
  245. usb_fill_int_urb(pegasus->irq, dev, pipe,
  246. pegasus->data, pegasus->data_len,
  247. pegasus_irq, pegasus, endpoint->bInterval);
  248. pegasus->irq->transfer_dma = pegasus->data_dma;
  249. pegasus->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  250. if (dev->manufacturer)
  251. strlcpy(pegasus->name, dev->manufacturer,
  252. sizeof(pegasus->name));
  253. if (dev->product) {
  254. if (dev->manufacturer)
  255. strlcat(pegasus->name, " ", sizeof(pegasus->name));
  256. strlcat(pegasus->name, dev->product, sizeof(pegasus->name));
  257. }
  258. if (!strlen(pegasus->name))
  259. snprintf(pegasus->name, sizeof(pegasus->name),
  260. "USB Pegasus Device %04x:%04x",
  261. le16_to_cpu(dev->descriptor.idVendor),
  262. le16_to_cpu(dev->descriptor.idProduct));
  263. usb_make_path(dev, pegasus->phys, sizeof(pegasus->phys));
  264. strlcat(pegasus->phys, "/input0", sizeof(pegasus->phys));
  265. INIT_WORK(&pegasus->init, pegasus_init);
  266. usb_set_intfdata(intf, pegasus);
  267. input_dev->name = pegasus->name;
  268. input_dev->phys = pegasus->phys;
  269. usb_to_input_id(dev, &input_dev->id);
  270. input_dev->dev.parent = &intf->dev;
  271. input_set_drvdata(input_dev, pegasus);
  272. input_dev->open = pegasus_open;
  273. input_dev->close = pegasus_close;
  274. __set_bit(EV_ABS, input_dev->evbit);
  275. __set_bit(EV_KEY, input_dev->evbit);
  276. __set_bit(ABS_X, input_dev->absbit);
  277. __set_bit(ABS_Y, input_dev->absbit);
  278. __set_bit(BTN_TOUCH, input_dev->keybit);
  279. __set_bit(BTN_RIGHT, input_dev->keybit);
  280. __set_bit(BTN_TOOL_PEN, input_dev->keybit);
  281. __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
  282. __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
  283. input_set_abs_params(input_dev, ABS_X, -1500, 1500, 8, 0);
  284. input_set_abs_params(input_dev, ABS_Y, 1600, 3000, 8, 0);
  285. error = input_register_device(pegasus->dev);
  286. if (error)
  287. goto err_free_urb;
  288. return 0;
  289. err_free_urb:
  290. usb_free_urb(pegasus->irq);
  291. err_free_dma:
  292. usb_free_coherent(dev, pegasus->data_len,
  293. pegasus->data, pegasus->data_dma);
  294. err_free_mem:
  295. input_free_device(input_dev);
  296. kfree(pegasus);
  297. usb_set_intfdata(intf, NULL);
  298. return error;
  299. }
  300. static void pegasus_disconnect(struct usb_interface *intf)
  301. {
  302. struct pegasus *pegasus = usb_get_intfdata(intf);
  303. input_unregister_device(pegasus->dev);
  304. usb_free_urb(pegasus->irq);
  305. usb_free_coherent(interface_to_usbdev(intf),
  306. pegasus->data_len, pegasus->data,
  307. pegasus->data_dma);
  308. kfree(pegasus);
  309. usb_set_intfdata(intf, NULL);
  310. }
  311. static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
  312. {
  313. struct pegasus *pegasus = usb_get_intfdata(intf);
  314. mutex_lock(&pegasus->dev->mutex);
  315. usb_kill_urb(pegasus->irq);
  316. cancel_work_sync(&pegasus->init);
  317. mutex_unlock(&pegasus->dev->mutex);
  318. return 0;
  319. }
  320. static int pegasus_resume(struct usb_interface *intf)
  321. {
  322. struct pegasus *pegasus = usb_get_intfdata(intf);
  323. int retval = 0;
  324. mutex_lock(&pegasus->dev->mutex);
  325. if (pegasus->dev->users && usb_submit_urb(pegasus->irq, GFP_NOIO) < 0)
  326. retval = -EIO;
  327. mutex_unlock(&pegasus->dev->mutex);
  328. return retval;
  329. }
  330. static int pegasus_reset_resume(struct usb_interface *intf)
  331. {
  332. struct pegasus *pegasus = usb_get_intfdata(intf);
  333. int retval = 0;
  334. mutex_lock(&pegasus->dev->mutex);
  335. if (pegasus->dev->users) {
  336. retval = pegasus_set_mode(pegasus, PEN_MODE_XY,
  337. NOTETAKER_LED_MOUSE);
  338. if (!retval && usb_submit_urb(pegasus->irq, GFP_NOIO) < 0)
  339. retval = -EIO;
  340. }
  341. mutex_unlock(&pegasus->dev->mutex);
  342. return retval;
  343. }
  344. static const struct usb_device_id pegasus_ids[] = {
  345. { USB_DEVICE(USB_VENDOR_ID_PEGASUSTECH,
  346. USB_DEVICE_ID_PEGASUS_NOTETAKER_EN100) },
  347. { }
  348. };
  349. MODULE_DEVICE_TABLE(usb, pegasus_ids);
  350. static struct usb_driver pegasus_driver = {
  351. .name = "pegasus_notetaker",
  352. .probe = pegasus_probe,
  353. .disconnect = pegasus_disconnect,
  354. .suspend = pegasus_suspend,
  355. .resume = pegasus_resume,
  356. .reset_resume = pegasus_reset_resume,
  357. .id_table = pegasus_ids,
  358. .supports_autosuspend = 1,
  359. };
  360. module_usb_driver(pegasus_driver);
  361. MODULE_AUTHOR("Martin Kepplinger <martink@posteo.de>");
  362. MODULE_DESCRIPTION("Pegasus Mobile Notetaker Pen tablet driver");
  363. MODULE_LICENSE("GPL");