hanwang.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * USB Hanwang tablet support
  3. *
  4. * Copyright (c) 2010 Xing Wei <weixing@hanwang.com.cn>
  5. *
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/types.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/usb/input.h>
  29. #define DRIVER_AUTHOR "Xing Wei <weixing@hanwang.com.cn>"
  30. #define DRIVER_DESC "USB Hanwang tablet driver"
  31. #define DRIVER_LICENSE "GPL"
  32. MODULE_AUTHOR(DRIVER_AUTHOR);
  33. MODULE_DESCRIPTION(DRIVER_DESC);
  34. MODULE_LICENSE(DRIVER_LICENSE);
  35. #define USB_VENDOR_ID_HANWANG 0x0b57
  36. #define HANWANG_TABLET_INT_CLASS 0x0003
  37. #define HANWANG_TABLET_INT_SUB_CLASS 0x0001
  38. #define HANWANG_TABLET_INT_PROTOCOL 0x0002
  39. #define ART_MASTER_PKGLEN_MAX 10
  40. /* device IDs */
  41. #define STYLUS_DEVICE_ID 0x02
  42. #define TOUCH_DEVICE_ID 0x03
  43. #define CURSOR_DEVICE_ID 0x06
  44. #define ERASER_DEVICE_ID 0x0A
  45. #define PAD_DEVICE_ID 0x0F
  46. /* match vendor and interface info */
  47. #define HANWANG_TABLET_DEVICE(vend, cl, sc, pr) \
  48. .match_flags = USB_DEVICE_ID_MATCH_VENDOR \
  49. | USB_DEVICE_ID_MATCH_INT_INFO, \
  50. .idVendor = (vend), \
  51. .bInterfaceClass = (cl), \
  52. .bInterfaceSubClass = (sc), \
  53. .bInterfaceProtocol = (pr)
  54. enum hanwang_tablet_type {
  55. HANWANG_ART_MASTER_III,
  56. HANWANG_ART_MASTER_HD,
  57. };
  58. struct hanwang {
  59. unsigned char *data;
  60. dma_addr_t data_dma;
  61. struct input_dev *dev;
  62. struct usb_device *usbdev;
  63. struct urb *irq;
  64. const struct hanwang_features *features;
  65. unsigned int current_tool;
  66. unsigned int current_id;
  67. char name[64];
  68. char phys[32];
  69. };
  70. struct hanwang_features {
  71. unsigned short pid;
  72. char *name;
  73. enum hanwang_tablet_type type;
  74. int pkg_len;
  75. int max_x;
  76. int max_y;
  77. int max_tilt_x;
  78. int max_tilt_y;
  79. int max_pressure;
  80. };
  81. static const struct hanwang_features features_array[] = {
  82. { 0x8528, "Hanwang Art Master III 0906", HANWANG_ART_MASTER_III,
  83. ART_MASTER_PKGLEN_MAX, 0x5757, 0x3692, 0x3f, 0x7f, 2048 },
  84. { 0x8529, "Hanwang Art Master III 0604", HANWANG_ART_MASTER_III,
  85. ART_MASTER_PKGLEN_MAX, 0x3d84, 0x2672, 0x3f, 0x7f, 2048 },
  86. { 0x852a, "Hanwang Art Master III 1308", HANWANG_ART_MASTER_III,
  87. ART_MASTER_PKGLEN_MAX, 0x7f00, 0x4f60, 0x3f, 0x7f, 2048 },
  88. { 0x8401, "Hanwang Art Master HD 5012", HANWANG_ART_MASTER_HD,
  89. ART_MASTER_PKGLEN_MAX, 0x678e, 0x4150, 0x3f, 0x7f, 1024 },
  90. };
  91. static const int hw_eventtypes[] = {
  92. EV_KEY, EV_ABS, EV_MSC,
  93. };
  94. static const int hw_absevents[] = {
  95. ABS_X, ABS_Y, ABS_TILT_X, ABS_TILT_Y, ABS_WHEEL,
  96. ABS_RX, ABS_RY, ABS_PRESSURE, ABS_MISC,
  97. };
  98. static const int hw_btnevents[] = {
  99. BTN_STYLUS, BTN_STYLUS2, BTN_TOOL_PEN, BTN_TOOL_RUBBER,
  100. BTN_TOOL_MOUSE, BTN_TOOL_FINGER,
  101. BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8,
  102. };
  103. static const int hw_mscevents[] = {
  104. MSC_SERIAL,
  105. };
  106. static void hanwang_parse_packet(struct hanwang *hanwang)
  107. {
  108. unsigned char *data = hanwang->data;
  109. struct input_dev *input_dev = hanwang->dev;
  110. struct usb_device *dev = hanwang->usbdev;
  111. enum hanwang_tablet_type type = hanwang->features->type;
  112. int i;
  113. u16 x, y, p;
  114. switch (data[0]) {
  115. case 0x02: /* data packet */
  116. switch (data[1]) {
  117. case 0x80: /* tool prox out */
  118. hanwang->current_id = 0;
  119. input_report_key(input_dev, hanwang->current_tool, 0);
  120. break;
  121. case 0xc2: /* first time tool prox in */
  122. switch (data[3] & 0xf0) {
  123. case 0x20: /* art_master III */
  124. case 0x30: /* art_master_HD */
  125. hanwang->current_id = STYLUS_DEVICE_ID;
  126. hanwang->current_tool = BTN_TOOL_PEN;
  127. input_report_key(input_dev, BTN_TOOL_PEN, 1);
  128. break;
  129. case 0xa0: /* art_master III */
  130. case 0xb0: /* art_master_HD */
  131. hanwang->current_id = ERASER_DEVICE_ID;
  132. hanwang->current_tool = BTN_TOOL_RUBBER;
  133. input_report_key(input_dev, BTN_TOOL_RUBBER, 1);
  134. break;
  135. default:
  136. hanwang->current_id = 0;
  137. dev_dbg(&dev->dev,
  138. "unknown tablet tool %02x ", data[0]);
  139. break;
  140. }
  141. break;
  142. default: /* tool data packet */
  143. x = (data[2] << 8) | data[3];
  144. y = (data[4] << 8) | data[5];
  145. switch (type) {
  146. case HANWANG_ART_MASTER_III:
  147. p = (data[6] << 3) |
  148. ((data[7] & 0xc0) >> 5) |
  149. (data[1] & 0x01);
  150. break;
  151. case HANWANG_ART_MASTER_HD:
  152. p = (data[7] >> 6) | (data[6] << 2);
  153. break;
  154. default:
  155. p = 0;
  156. break;
  157. }
  158. input_report_abs(input_dev, ABS_X,
  159. le16_to_cpup((__le16 *)&x));
  160. input_report_abs(input_dev, ABS_Y,
  161. le16_to_cpup((__le16 *)&y));
  162. input_report_abs(input_dev, ABS_PRESSURE,
  163. le16_to_cpup((__le16 *)&p));
  164. input_report_abs(input_dev, ABS_TILT_X, data[7] & 0x3f);
  165. input_report_abs(input_dev, ABS_TILT_Y, data[8] & 0x7f);
  166. input_report_key(input_dev, BTN_STYLUS, data[1] & 0x02);
  167. input_report_key(input_dev, BTN_STYLUS2, data[1] & 0x04);
  168. break;
  169. }
  170. input_report_abs(input_dev, ABS_MISC, hanwang->current_id);
  171. input_event(input_dev, EV_MSC, MSC_SERIAL,
  172. hanwang->features->pid);
  173. break;
  174. case 0x0c:
  175. /* roll wheel */
  176. hanwang->current_id = PAD_DEVICE_ID;
  177. switch (type) {
  178. case HANWANG_ART_MASTER_III:
  179. input_report_key(input_dev, BTN_TOOL_FINGER, data[1] ||
  180. data[2] || data[3]);
  181. input_report_abs(input_dev, ABS_WHEEL, data[1]);
  182. input_report_key(input_dev, BTN_0, data[2]);
  183. for (i = 0; i < 8; i++)
  184. input_report_key(input_dev,
  185. BTN_1 + i, data[3] & (1 << i));
  186. break;
  187. case HANWANG_ART_MASTER_HD:
  188. input_report_key(input_dev, BTN_TOOL_FINGER, data[1] ||
  189. data[2] || data[3] || data[4] ||
  190. data[5] || data[6]);
  191. input_report_abs(input_dev, ABS_RX,
  192. ((data[1] & 0x1f) << 8) | data[2]);
  193. input_report_abs(input_dev, ABS_RY,
  194. ((data[3] & 0x1f) << 8) | data[4]);
  195. input_report_key(input_dev, BTN_0, data[5] & 0x01);
  196. for (i = 0; i < 4; i++) {
  197. input_report_key(input_dev,
  198. BTN_1 + i, data[5] & (1 << i));
  199. input_report_key(input_dev,
  200. BTN_5 + i, data[6] & (1 << i));
  201. }
  202. break;
  203. }
  204. input_report_abs(input_dev, ABS_MISC, hanwang->current_id);
  205. input_event(input_dev, EV_MSC, MSC_SERIAL, 0xffffffff);
  206. break;
  207. default:
  208. dev_dbg(&dev->dev, "error packet %02x ", data[0]);
  209. break;
  210. }
  211. input_sync(input_dev);
  212. }
  213. static void hanwang_irq(struct urb *urb)
  214. {
  215. struct hanwang *hanwang = urb->context;
  216. struct usb_device *dev = hanwang->usbdev;
  217. int retval;
  218. switch (urb->status) {
  219. case 0:
  220. /* success */;
  221. hanwang_parse_packet(hanwang);
  222. break;
  223. case -ECONNRESET:
  224. case -ENOENT:
  225. case -ESHUTDOWN:
  226. /* this urb is terminated, clean up */
  227. dev_err(&dev->dev, "%s - urb shutting down with status: %d",
  228. __func__, urb->status);
  229. return;
  230. default:
  231. dev_err(&dev->dev, "%s - nonzero urb status received: %d",
  232. __func__, urb->status);
  233. break;
  234. }
  235. retval = usb_submit_urb(urb, GFP_ATOMIC);
  236. if (retval)
  237. dev_err(&dev->dev, "%s - usb_submit_urb failed with result %d",
  238. __func__, retval);
  239. }
  240. static int hanwang_open(struct input_dev *dev)
  241. {
  242. struct hanwang *hanwang = input_get_drvdata(dev);
  243. hanwang->irq->dev = hanwang->usbdev;
  244. if (usb_submit_urb(hanwang->irq, GFP_KERNEL))
  245. return -EIO;
  246. return 0;
  247. }
  248. static void hanwang_close(struct input_dev *dev)
  249. {
  250. struct hanwang *hanwang = input_get_drvdata(dev);
  251. usb_kill_urb(hanwang->irq);
  252. }
  253. static bool get_features(struct usb_device *dev, struct hanwang *hanwang)
  254. {
  255. int i;
  256. for (i = 0; i < ARRAY_SIZE(features_array); i++) {
  257. if (le16_to_cpu(dev->descriptor.idProduct) ==
  258. features_array[i].pid) {
  259. hanwang->features = &features_array[i];
  260. return true;
  261. }
  262. }
  263. return false;
  264. }
  265. static int hanwang_probe(struct usb_interface *intf, const struct usb_device_id *id)
  266. {
  267. struct usb_device *dev = interface_to_usbdev(intf);
  268. struct usb_endpoint_descriptor *endpoint;
  269. struct hanwang *hanwang;
  270. struct input_dev *input_dev;
  271. int error;
  272. int i;
  273. hanwang = kzalloc(sizeof(struct hanwang), GFP_KERNEL);
  274. input_dev = input_allocate_device();
  275. if (!hanwang || !input_dev) {
  276. error = -ENOMEM;
  277. goto fail1;
  278. }
  279. if (!get_features(dev, hanwang)) {
  280. error = -ENXIO;
  281. goto fail1;
  282. }
  283. hanwang->data = usb_alloc_coherent(dev, hanwang->features->pkg_len,
  284. GFP_KERNEL, &hanwang->data_dma);
  285. if (!hanwang->data) {
  286. error = -ENOMEM;
  287. goto fail1;
  288. }
  289. hanwang->irq = usb_alloc_urb(0, GFP_KERNEL);
  290. if (!hanwang->irq) {
  291. error = -ENOMEM;
  292. goto fail2;
  293. }
  294. hanwang->usbdev = dev;
  295. hanwang->dev = input_dev;
  296. usb_make_path(dev, hanwang->phys, sizeof(hanwang->phys));
  297. strlcat(hanwang->phys, "/input0", sizeof(hanwang->phys));
  298. strlcpy(hanwang->name, hanwang->features->name, sizeof(hanwang->name));
  299. input_dev->name = hanwang->name;
  300. input_dev->phys = hanwang->phys;
  301. usb_to_input_id(dev, &input_dev->id);
  302. input_dev->dev.parent = &intf->dev;
  303. input_set_drvdata(input_dev, hanwang);
  304. input_dev->open = hanwang_open;
  305. input_dev->close = hanwang_close;
  306. for (i = 0; i < ARRAY_SIZE(hw_eventtypes); ++i)
  307. __set_bit(hw_eventtypes[i], input_dev->evbit);
  308. for (i = 0; i < ARRAY_SIZE(hw_absevents); ++i)
  309. __set_bit(hw_absevents[i], input_dev->absbit);
  310. for (i = 0; i < ARRAY_SIZE(hw_btnevents); ++i)
  311. __set_bit(hw_btnevents[i], input_dev->keybit);
  312. for (i = 0; i < ARRAY_SIZE(hw_mscevents); ++i)
  313. __set_bit(hw_mscevents[i], input_dev->mscbit);
  314. input_set_abs_params(input_dev, ABS_X,
  315. 0, hanwang->features->max_x, 4, 0);
  316. input_set_abs_params(input_dev, ABS_Y,
  317. 0, hanwang->features->max_y, 4, 0);
  318. input_set_abs_params(input_dev, ABS_TILT_X,
  319. 0, hanwang->features->max_tilt_x, 0, 0);
  320. input_set_abs_params(input_dev, ABS_TILT_Y,
  321. 0, hanwang->features->max_tilt_y, 0, 0);
  322. input_set_abs_params(input_dev, ABS_PRESSURE,
  323. 0, hanwang->features->max_pressure, 0, 0);
  324. endpoint = &intf->cur_altsetting->endpoint[0].desc;
  325. usb_fill_int_urb(hanwang->irq, dev,
  326. usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  327. hanwang->data, hanwang->features->pkg_len,
  328. hanwang_irq, hanwang, endpoint->bInterval);
  329. hanwang->irq->transfer_dma = hanwang->data_dma;
  330. hanwang->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  331. error = input_register_device(hanwang->dev);
  332. if (error)
  333. goto fail3;
  334. usb_set_intfdata(intf, hanwang);
  335. return 0;
  336. fail3: usb_free_urb(hanwang->irq);
  337. fail2: usb_free_coherent(dev, hanwang->features->pkg_len,
  338. hanwang->data, hanwang->data_dma);
  339. fail1: input_free_device(input_dev);
  340. kfree(hanwang);
  341. return error;
  342. }
  343. static void hanwang_disconnect(struct usb_interface *intf)
  344. {
  345. struct hanwang *hanwang = usb_get_intfdata(intf);
  346. input_unregister_device(hanwang->dev);
  347. usb_free_urb(hanwang->irq);
  348. usb_free_coherent(interface_to_usbdev(intf),
  349. hanwang->features->pkg_len, hanwang->data,
  350. hanwang->data_dma);
  351. kfree(hanwang);
  352. usb_set_intfdata(intf, NULL);
  353. }
  354. static const struct usb_device_id hanwang_ids[] = {
  355. { HANWANG_TABLET_DEVICE(USB_VENDOR_ID_HANWANG, HANWANG_TABLET_INT_CLASS,
  356. HANWANG_TABLET_INT_SUB_CLASS, HANWANG_TABLET_INT_PROTOCOL) },
  357. {}
  358. };
  359. MODULE_DEVICE_TABLE(usb, hanwang_ids);
  360. static struct usb_driver hanwang_driver = {
  361. .name = "hanwang",
  362. .probe = hanwang_probe,
  363. .disconnect = hanwang_disconnect,
  364. .id_table = hanwang_ids,
  365. };
  366. module_usb_driver(hanwang_driver);