pegasus_notetaker.c 11 KB

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