pegasus_notetaker.c 11 KB

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