pegasus_notetaker.c 11 KB

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