Pen tablet driver for Linux kernels
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hanvon.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #include <linux/kernel.h>
  2. #include <linux/slab.h>
  3. #include <linux/module.h>
  4. #include <linux/init.h>
  5. #include <linux/usb/input.h>
  6. #include <asm/unaligned.h>
  7. #define DRIVER_VERSION "0.6"
  8. #define DRIVER_AUTHOR "Ondra Havel <ondra.havel@gmail.com>"
  9. #define DRIVER_DESC "USB Hanvon tablet driver"
  10. #define DRIVER_LICENSE "GPL"
  11. MODULE_AUTHOR(DRIVER_AUTHOR);
  12. MODULE_DESCRIPTION(DRIVER_DESC);
  13. MODULE_LICENSE(DRIVER_LICENSE);
  14. #define USB_VENDOR_ID_HANVON 0x0b57
  15. #define USB_PRODUCT_ID_AM3M 0x8528
  16. #define USB_PRODUCT_ID_AM0806 0x8502
  17. #define USB_PRODUCT_ID_AM0605 0x8503
  18. #define USB_PRODUCT_ID_AM1107 0x8505
  19. #define USB_PRODUCT_ID_AM1209 0x8501
  20. #define USB_PRODUCT_ID_RL0604 0x851f
  21. #define USB_PRODUCT_ID_RL0504 0x851d
  22. #define USB_PRODUCT_ID_GP0806 0x8039
  23. #define USB_PRODUCT_ID_GP0806B 0x8511
  24. #define USB_PRODUCT_ID_GP0605 0x8512
  25. #define USB_PRODUCT_ID_GP0605A 0x803a
  26. #define USB_PRODUCT_ID_GP0504 0x8037
  27. #define USB_PRODUCT_ID_NXS1513 0x8030
  28. #define USB_PRODUCT_ID_GP0906 0x8521
  29. #define USB_PRODUCT_ID_APPIV0906 0x8532
  30. #define USB_AM_PACKET_LEN 10
  31. static int lbuttons[]={BTN_0,BTN_1,BTN_2,BTN_3}; /* reported on all AMs */
  32. static int rbuttons[]={BTN_4,BTN_5,BTN_6,BTN_7}; /* reported on AM1107+ */
  33. #define AM_WHEEL_THRESHOLD 4
  34. #define AM_MAX_TILT_X 0x3f
  35. #define AM_MAX_TILT_Y 0x7f
  36. #define AM_MAX_PRESSURE 0x400
  37. struct hanvon {
  38. unsigned char *data;
  39. dma_addr_t data_dma;
  40. struct input_dev *dev;
  41. struct usb_device *usbdev;
  42. struct urb *irq;
  43. int old_wheel_pos;
  44. char phys[32];
  45. };
  46. static void report_buttons(struct hanvon *hanvon, int buttons[], unsigned char dta)
  47. {
  48. struct input_dev *dev = hanvon->dev;
  49. if((dta & 0xf0) == 0xa0) {
  50. input_report_key(dev, buttons[1], dta & 0x02);
  51. input_report_key(dev, buttons[2], dta & 0x04);
  52. input_report_key(dev, buttons[3], dta & 0x08);
  53. } else {
  54. if(dta <= 0x3f) { /* slider area active */
  55. int diff = dta - hanvon->old_wheel_pos;
  56. if(abs(diff) < AM_WHEEL_THRESHOLD)
  57. input_report_rel(dev, REL_WHEEL, diff);
  58. hanvon->old_wheel_pos = dta;
  59. }
  60. }
  61. }
  62. static inline void handle_default(struct hanvon *hanvon)
  63. {
  64. unsigned char *data = hanvon->data;
  65. struct input_dev *dev = hanvon->dev;
  66. #define AM_MAX_ABS_X 0x27de
  67. #define AM_MAX_ABS_Y 0x1cfe
  68. switch(data[0]) {
  69. case 0x01: /* button press */
  70. if(data[1]==0x55) /* left side */
  71. report_buttons(hanvon, lbuttons,data[2]);
  72. if(data[3]==0xaa) /* right side (am1107, am1209) */
  73. report_buttons(hanvon, rbuttons,data[4]);
  74. break;
  75. case 0x02: /* position change */
  76. if((data[1] & 0xf0) != 0) {
  77. input_report_abs(dev, ABS_X, get_unaligned_be16(&data[2]) * 0xffff / AM_MAX_ABS_X);
  78. input_report_abs(dev, ABS_Y, get_unaligned_be16(&data[4]) * 0xffff / AM_MAX_ABS_Y);
  79. input_report_abs(dev, ABS_TILT_X, data[7] & 0x3f);
  80. input_report_abs(dev, ABS_TILT_Y, data[8]);
  81. input_report_abs(dev, ABS_PRESSURE, get_unaligned_be16(&data[6])>>6);
  82. }
  83. input_report_key(dev, BTN_LEFT, data[1] & 0x01); /* pen touches the surface */
  84. input_report_key(dev, BTN_RIGHT, data[1] & 0x02); /* stylus button pressed (right click) */
  85. input_report_key(dev, lbuttons[0], data[1] & 0x20); /* 'eraser' button */
  86. break;
  87. }
  88. }
  89. static inline void handle_gp0504(struct hanvon *hanvon)
  90. {
  91. unsigned char *data = hanvon->data;
  92. struct input_dev *dev = hanvon->dev;
  93. #define AM_MAX_ABS_X 0x27de
  94. #define AM_MAX_ABS_Y 0x1cfe
  95. switch(data[0]) {
  96. case 0x01: /* button press */
  97. if(data[1]==0x55) /* left side */
  98. report_buttons(hanvon, lbuttons,data[2]);
  99. if(data[3]==0xaa) /* right side (am1107, am1209) */
  100. report_buttons(hanvon, rbuttons,data[4]);
  101. break;
  102. case 0x02: /* position change */
  103. /*printk(KERN_INFO "Hanvon Test : 1x%04x 2x%04x 4x%04x 6x%04x 7x%04x 8x%04x\n\t\tClicked : %s %i/%04x\n", data[1], data[2], data[4], data[6], data[7], data[8], data[6] > 63 ? "true" : "false", data[6], get_unaligned_be16(&data[6]));*/
  104. if((data[1] & 0xf0) != 0) {
  105. input_report_abs(dev, ABS_X, get_unaligned_be16(&data[2]) * 0xffff / AM_MAX_ABS_X);
  106. input_report_abs(dev, ABS_Y, get_unaligned_be16(&data[4]) * 0xffff / AM_MAX_ABS_Y);
  107. input_report_abs(dev, ABS_TILT_X, data[7] & 0x3f);
  108. input_report_abs(dev, ABS_TILT_Y, data[8]);
  109. input_report_abs(dev, ABS_PRESSURE, get_unaligned_be16(&data[6])>>6);
  110. }
  111. /*printk(KERN_INFO "Hanvon Test : %i\n", data[6]);*/
  112. input_report_key(dev, BTN_LEFT, data[6] > 68); /* pen touches the surface */
  113. input_report_key(dev, BTN_RIGHT, data[1] & 0x02); /* stylus button pressed (right click) */
  114. input_report_key(dev, lbuttons[0], data[1] & 0x20); /* 'eraser' button */
  115. break;
  116. }
  117. }
  118. static inline void handle_gp0906(struct hanvon *hanvon)
  119. {
  120. unsigned char *data = hanvon->data;
  121. struct input_dev *dev = hanvon->dev;
  122. /*
  123. hanvon graphic pal 3, gp0906!
  124. - data definition. this is not official...
  125. [header, 1Byte][event type, 1Byte][x, 2Bytes][y, 2Bytes][pressure, 2Bytes][tilt, 2Bytes]
  126. */
  127. switch( data[0] ) {
  128. case 0x02: /* pen event */
  129. if( ( data[1] & 0xe0) == 0xe0 ) {
  130. if( data[1] & 0x01 ) /* pressure change */
  131. input_report_abs(dev, ABS_PRESSURE, get_unaligned_be16(&data[6])>>6);
  132. if( data[1] & 0x04 )
  133. input_report_key(dev, BTN_RIGHT, data[1] & 0x2); /* stylus button pressed (right click) */
  134. input_report_abs(dev, ABS_X, get_unaligned_be16(&data[2]) * 0xffff / AM_MAX_ABS_X);
  135. input_report_abs(dev, ABS_Y, get_unaligned_be16(&data[4]) * 0xffff / AM_MAX_ABS_Y);
  136. input_report_key(dev, lbuttons[0], data[6]);
  137. } else {
  138. if( data[1] == 0xc2) /* pen enters */
  139. {}
  140. if( data[1] == 0x80) /* pen leaves */
  141. {}
  142. }
  143. break;
  144. case 0x0c: /* key press on the tablet */
  145. input_report_key(dev, lbuttons[0], data[3] & 0x01);
  146. input_report_key(dev, lbuttons[1], data[3] & 0x02);
  147. input_report_key(dev, lbuttons[2], data[3] & 0x04);
  148. input_report_key(dev, lbuttons[3], data[3] & 0x08);
  149. break;
  150. }
  151. }
  152. static inline void handle_appiv0906(struct hanvon *hanvon)
  153. {
  154. unsigned char *data = hanvon->data;
  155. struct input_dev *dev = hanvon->dev;
  156. #define APPIV_XMAX 0x5750
  157. #define APPIV_YMAX 0x3692
  158. switch(data[0]) {
  159. case 0x01: /* pen button event */
  160. input_report_abs(dev, ABS_X, get_unaligned_le16(&data[2]) * 0xffff / APPIV_XMAX);
  161. input_report_abs(dev, ABS_Y, get_unaligned_le16(&data[4]) * 0xffff / APPIV_YMAX);
  162. input_report_key(dev, BTN_LEFT, data[1] & 0x01); /* pen touches the surface */
  163. input_report_key(dev, BTN_RIGHT, data[1] & 0x02); /* stylus button pressed (right click) */
  164. input_report_key(dev, BTN_MIDDLE, data[1] & 0x04); /* stylus button pressed (right click) */
  165. input_report_key(dev, BTN_0, data[1] & 0x08); /* stylus button pressed (right click) */
  166. break;
  167. case 0x02: /* pen event */
  168. input_report_abs(dev, ABS_X, get_unaligned_be16(&data[2]) * 0xffff / APPIV_XMAX);
  169. input_report_abs(dev, ABS_Y, get_unaligned_be16(&data[4]) * 0xffff / APPIV_YMAX);
  170. if(data[1] & 1)
  171. input_report_abs(dev, ABS_PRESSURE, get_unaligned_be16(&data[6])>>6);
  172. break;
  173. case 0x0c: /* tablet button event */
  174. input_report_key(dev, BTN_1, data[3] & 0x01);
  175. input_report_key(dev, BTN_2, data[3] & 0x02);
  176. input_report_key(dev, BTN_3, data[3] & 0x04);
  177. input_report_key(dev, BTN_4, data[3] & 0x08);
  178. input_report_key(dev, BTN_5, data[3] & 0x10);
  179. input_report_key(dev, BTN_6, data[3] & 0x20);
  180. input_report_key(dev, BTN_7, data[3] & 0x40);
  181. break;
  182. }
  183. }
  184. static void hanvon_irq(struct urb *urb)
  185. {
  186. struct hanvon *hanvon = urb->context;
  187. int retval;
  188. switch (urb->status) {
  189. case 0:
  190. /* success */
  191. switch( hanvon->usbdev->descriptor.idProduct ) {
  192. case USB_PRODUCT_ID_GP0906:
  193. handle_gp0906(hanvon);
  194. break;
  195. case USB_PRODUCT_ID_APPIV0906:
  196. handle_appiv0906(hanvon);
  197. break;
  198. case USB_PRODUCT_ID_GP0504:
  199. handle_gp0504(hanvon);
  200. break;
  201. default:
  202. handle_default(hanvon);
  203. break;
  204. }
  205. break;
  206. case -ECONNRESET:
  207. case -ENOENT:
  208. case -ESHUTDOWN:
  209. /* this urb is terminated, clean up */
  210. printk("%s - urb shutting down with status: %d", __func__, urb->status);
  211. return;
  212. default:
  213. printk("%s - nonzero urb status received: %d", __func__, urb->status);
  214. break;
  215. }
  216. input_sync(hanvon->dev);
  217. retval = usb_submit_urb (urb, GFP_ATOMIC);
  218. if (retval)
  219. printk("%s - usb_submit_urb failed with result %d", __func__, retval);
  220. }
  221. static struct usb_device_id hanvon_ids[] = {
  222. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_AM3M) },
  223. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_AM1209) },
  224. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_AM1107) },
  225. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_AM0806) },
  226. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_AM0605) },
  227. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_RL0604) },
  228. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_RL0504) },
  229. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_GP0806) },
  230. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_GP0806B) },
  231. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_GP0605) },
  232. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_GP0605A) },
  233. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_GP0504) },
  234. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_NXS1513) },
  235. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_GP0906) },
  236. { USB_DEVICE(USB_VENDOR_ID_HANVON, USB_PRODUCT_ID_APPIV0906)},
  237. {}
  238. };
  239. MODULE_DEVICE_TABLE(usb, hanvon_ids);
  240. static int hanvon_open(struct input_dev *dev)
  241. {
  242. struct hanvon *hanvon = input_get_drvdata(dev);
  243. hanvon->old_wheel_pos = -AM_WHEEL_THRESHOLD-1;
  244. hanvon->irq->dev = hanvon->usbdev;
  245. if (usb_submit_urb(hanvon->irq, GFP_KERNEL))
  246. return -EIO;
  247. return 0;
  248. }
  249. static void hanvon_close(struct input_dev *dev)
  250. {
  251. struct hanvon *hanvon = input_get_drvdata(dev);
  252. usb_kill_urb(hanvon->irq);
  253. }
  254. static int hanvon_probe(struct usb_interface *intf, const struct usb_device_id *id)
  255. {
  256. struct usb_device *dev = interface_to_usbdev(intf);
  257. struct usb_endpoint_descriptor *endpoint;
  258. struct hanvon *hanvon;
  259. struct input_dev *input_dev;
  260. int error = -ENOMEM, i;
  261. hanvon = kzalloc(sizeof(struct hanvon), GFP_KERNEL);
  262. input_dev = input_allocate_device();
  263. if (!hanvon || !input_dev)
  264. goto fail1;
  265. hanvon->data = (unsigned char *)usb_alloc_coherent(dev, USB_AM_PACKET_LEN, GFP_KERNEL, &hanvon->data_dma);
  266. if (!hanvon->data)
  267. goto fail1;
  268. hanvon->irq = usb_alloc_urb(0, GFP_KERNEL);
  269. if (!hanvon->irq)
  270. goto fail2;
  271. hanvon->usbdev = dev;
  272. hanvon->dev = input_dev;
  273. usb_make_path(dev, hanvon->phys, sizeof(hanvon->phys));
  274. strlcat(hanvon->phys, "/input0", sizeof(hanvon->phys));
  275. input_dev->name = "Hanvon tablet";
  276. input_dev->phys = hanvon->phys;
  277. usb_to_input_id(dev, &input_dev->id);
  278. input_dev->dev.parent = &intf->dev;
  279. input_set_drvdata(input_dev, hanvon);
  280. input_dev->open = hanvon_open;
  281. input_dev->close = hanvon_close;
  282. input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) | BIT_MASK(EV_REL);
  283. input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_PEN) | BIT_MASK(BTN_TOUCH);
  284. input_dev->keybit[BIT_WORD(BTN_LEFT)] |= BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
  285. for(i=0;i<sizeof(lbuttons)/sizeof(lbuttons[0]);i++)
  286. __set_bit(lbuttons[i], input_dev->keybit);
  287. for(i=0;i<sizeof(rbuttons)/sizeof(rbuttons[0]);i++)
  288. __set_bit(rbuttons[i], input_dev->keybit);
  289. input_set_abs_params(input_dev, ABS_X, 0, 0xffff, 4, 0);
  290. input_set_abs_params(input_dev, ABS_Y, 0, 0xffff, 4, 0);
  291. input_set_abs_params(input_dev, ABS_TILT_X, 0, AM_MAX_TILT_X, 0, 0);
  292. input_set_abs_params(input_dev, ABS_TILT_Y, 0, AM_MAX_TILT_Y, 0, 0);
  293. input_set_abs_params(input_dev, ABS_PRESSURE, 0, AM_MAX_PRESSURE, 0, 0);
  294. input_set_capability(input_dev, EV_REL, REL_WHEEL);
  295. endpoint = &intf->cur_altsetting->endpoint[0].desc;
  296. usb_fill_int_urb(hanvon->irq, dev,
  297. usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  298. hanvon->data, USB_AM_PACKET_LEN,
  299. hanvon_irq, hanvon, endpoint->bInterval);
  300. hanvon->irq->transfer_dma = hanvon->data_dma;
  301. hanvon->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  302. error = input_register_device(hanvon->dev);
  303. if (error)
  304. goto fail3;
  305. usb_set_intfdata(intf, hanvon);
  306. return 0;
  307. fail3: usb_free_urb(hanvon->irq);
  308. fail2: usb_free_coherent(dev, USB_AM_PACKET_LEN, hanvon->data, hanvon->data_dma);
  309. fail1: input_free_device(input_dev);
  310. kfree(hanvon);
  311. return error;
  312. }
  313. static void hanvon_disconnect(struct usb_interface *intf)
  314. {
  315. struct hanvon *hanvon = usb_get_intfdata(intf);
  316. usb_set_intfdata(intf, NULL);
  317. if (hanvon) {
  318. usb_kill_urb(hanvon->irq);
  319. input_unregister_device(hanvon->dev);
  320. usb_free_urb(hanvon->irq);
  321. usb_free_coherent(interface_to_usbdev(intf), USB_AM_PACKET_LEN, hanvon->data, hanvon->data_dma);
  322. kfree(hanvon);
  323. }
  324. }
  325. static struct usb_driver hanvon_driver = {
  326. .name = "hanvon",
  327. .probe = hanvon_probe,
  328. .disconnect = hanvon_disconnect,
  329. .id_table = hanvon_ids,
  330. };
  331. static int __init hanvon_init(void)
  332. {
  333. int rv;
  334. if((rv = usb_register(&hanvon_driver)) != 0)
  335. return rv;
  336. printk(DRIVER_DESC " " DRIVER_VERSION "\n");
  337. return 0;
  338. }
  339. static void __exit hanvon_exit(void)
  340. {
  341. usb_deregister(&hanvon_driver);
  342. }
  343. module_init(hanvon_init);
  344. module_exit(hanvon_exit);