hanwang.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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/usb/input.h>
  28. #define DRIVER_AUTHOR "Xing Wei <weixing@hanwang.com.cn>"
  29. #define DRIVER_DESC "USB Hanwang tablet driver"
  30. #define DRIVER_LICENSE "GPL"
  31. MODULE_AUTHOR(DRIVER_AUTHOR);
  32. MODULE_DESCRIPTION(DRIVER_DESC);
  33. MODULE_LICENSE(DRIVER_LICENSE);
  34. #define USB_VENDOR_ID_HANWANG 0x0b57
  35. #define HANWANG_TABLET_INT_CLASS 0x0003
  36. #define HANWANG_TABLET_INT_SUB_CLASS 0x0001
  37. #define HANWANG_TABLET_INT_PROTOCOL 0x0002
  38. #define ART_MASTER_PKGLEN_MAX 10
  39. /* device IDs */
  40. #define STYLUS_DEVICE_ID 0x02
  41. #define TOUCH_DEVICE_ID 0x03
  42. #define CURSOR_DEVICE_ID 0x06
  43. #define ERASER_DEVICE_ID 0x0A
  44. #define PAD_DEVICE_ID 0x0F
  45. /* match vendor and interface info */
  46. #define HANWANG_TABLET_DEVICE(vend, cl, sc, pr) \
  47. .match_flags = USB_DEVICE_ID_MATCH_VENDOR \
  48. | USB_DEVICE_ID_MATCH_INT_INFO, \
  49. .idVendor = (vend), \
  50. .bInterfaceClass = (cl), \
  51. .bInterfaceSubClass = (sc), \
  52. .bInterfaceProtocol = (pr)
  53. enum hanwang_tablet_type {
  54. HANWANG_ART_MASTER_III,
  55. HANWANG_ART_MASTER_HD,
  56. HANWANG_ART_MASTER_II,
  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. { 0x8503, "Hanwang Art Master II", HANWANG_ART_MASTER_II,
  91. ART_MASTER_PKGLEN_MAX, 0x27de, 0x1cfe, 0x3f, 0x7f, 1024 },
  92. };
  93. static const int hw_eventtypes[] = {
  94. EV_KEY, EV_ABS, EV_MSC,
  95. };
  96. static const int hw_absevents[] = {
  97. ABS_X, ABS_Y, ABS_TILT_X, ABS_TILT_Y, ABS_WHEEL,
  98. ABS_RX, ABS_RY, ABS_PRESSURE, ABS_MISC,
  99. };
  100. static const int hw_btnevents[] = {
  101. BTN_STYLUS, BTN_STYLUS2, BTN_TOOL_PEN, BTN_TOOL_RUBBER,
  102. BTN_TOOL_MOUSE, BTN_TOOL_FINGER,
  103. BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8,
  104. };
  105. static const int hw_mscevents[] = {
  106. MSC_SERIAL,
  107. };
  108. static void hanwang_parse_packet(struct hanwang *hanwang)
  109. {
  110. unsigned char *data = hanwang->data;
  111. struct input_dev *input_dev = hanwang->dev;
  112. struct usb_device *dev = hanwang->usbdev;
  113. enum hanwang_tablet_type type = hanwang->features->type;
  114. int i;
  115. u16 p;
  116. if (type == HANWANG_ART_MASTER_II) {
  117. hanwang->current_tool = BTN_TOOL_PEN;
  118. hanwang->current_id = STYLUS_DEVICE_ID;
  119. }
  120. switch (data[0]) {
  121. case 0x02: /* data packet */
  122. switch (data[1]) {
  123. case 0x80: /* tool prox out */
  124. if (type != HANWANG_ART_MASTER_II) {
  125. hanwang->current_id = 0;
  126. input_report_key(input_dev,
  127. hanwang->current_tool, 0);
  128. }
  129. break;
  130. case 0x00: /* artmaster ii pen leave */
  131. if (type == HANWANG_ART_MASTER_II) {
  132. hanwang->current_id = 0;
  133. input_report_key(input_dev,
  134. hanwang->current_tool, 0);
  135. }
  136. break;
  137. case 0xc2: /* first time tool prox in */
  138. switch (data[3] & 0xf0) {
  139. case 0x20: /* art_master III */
  140. case 0x30: /* art_master_HD */
  141. hanwang->current_id = STYLUS_DEVICE_ID;
  142. hanwang->current_tool = BTN_TOOL_PEN;
  143. input_report_key(input_dev, BTN_TOOL_PEN, 1);
  144. break;
  145. case 0xa0: /* art_master III */
  146. case 0xb0: /* art_master_HD */
  147. hanwang->current_id = ERASER_DEVICE_ID;
  148. hanwang->current_tool = BTN_TOOL_RUBBER;
  149. input_report_key(input_dev, BTN_TOOL_RUBBER, 1);
  150. break;
  151. default:
  152. hanwang->current_id = 0;
  153. dev_dbg(&dev->dev,
  154. "unknown tablet tool %02x\n", data[0]);
  155. break;
  156. }
  157. break;
  158. default: /* tool data packet */
  159. switch (type) {
  160. case HANWANG_ART_MASTER_III:
  161. p = (data[6] << 3) |
  162. ((data[7] & 0xc0) >> 5) |
  163. (data[1] & 0x01);
  164. break;
  165. case HANWANG_ART_MASTER_HD:
  166. case HANWANG_ART_MASTER_II:
  167. p = (data[7] >> 6) | (data[6] << 2);
  168. break;
  169. default:
  170. p = 0;
  171. break;
  172. }
  173. input_report_abs(input_dev, ABS_X,
  174. be16_to_cpup((__be16 *)&data[2]));
  175. input_report_abs(input_dev, ABS_Y,
  176. be16_to_cpup((__be16 *)&data[4]));
  177. input_report_abs(input_dev, ABS_PRESSURE, p);
  178. input_report_abs(input_dev, ABS_TILT_X, data[7] & 0x3f);
  179. input_report_abs(input_dev, ABS_TILT_Y, data[8] & 0x7f);
  180. input_report_key(input_dev, BTN_STYLUS, data[1] & 0x02);
  181. if (type != HANWANG_ART_MASTER_II)
  182. input_report_key(input_dev, BTN_STYLUS2,
  183. data[1] & 0x04);
  184. else
  185. input_report_key(input_dev, BTN_TOOL_PEN, 1);
  186. break;
  187. }
  188. input_report_abs(input_dev, ABS_MISC, hanwang->current_id);
  189. input_event(input_dev, EV_MSC, MSC_SERIAL,
  190. hanwang->features->pid);
  191. break;
  192. case 0x0c:
  193. /* roll wheel */
  194. hanwang->current_id = PAD_DEVICE_ID;
  195. switch (type) {
  196. case HANWANG_ART_MASTER_III:
  197. input_report_key(input_dev, BTN_TOOL_FINGER,
  198. data[1] || data[2] || data[3]);
  199. input_report_abs(input_dev, ABS_WHEEL, data[1]);
  200. input_report_key(input_dev, BTN_0, data[2]);
  201. for (i = 0; i < 8; i++)
  202. input_report_key(input_dev,
  203. BTN_1 + i, data[3] & (1 << i));
  204. break;
  205. case HANWANG_ART_MASTER_HD:
  206. input_report_key(input_dev, BTN_TOOL_FINGER, data[1] ||
  207. data[2] || data[3] || data[4] ||
  208. data[5] || data[6]);
  209. input_report_abs(input_dev, ABS_RX,
  210. ((data[1] & 0x1f) << 8) | data[2]);
  211. input_report_abs(input_dev, ABS_RY,
  212. ((data[3] & 0x1f) << 8) | data[4]);
  213. input_report_key(input_dev, BTN_0, data[5] & 0x01);
  214. for (i = 0; i < 4; i++) {
  215. input_report_key(input_dev,
  216. BTN_1 + i, data[5] & (1 << i));
  217. input_report_key(input_dev,
  218. BTN_5 + i, data[6] & (1 << i));
  219. }
  220. break;
  221. case HANWANG_ART_MASTER_II:
  222. dev_dbg(&dev->dev, "error packet %02x\n", data[0]);
  223. return;
  224. }
  225. input_report_abs(input_dev, ABS_MISC, hanwang->current_id);
  226. input_event(input_dev, EV_MSC, MSC_SERIAL, 0xffffffff);
  227. break;
  228. default:
  229. dev_dbg(&dev->dev, "error packet %02x\n", data[0]);
  230. break;
  231. }
  232. input_sync(input_dev);
  233. }
  234. static void hanwang_irq(struct urb *urb)
  235. {
  236. struct hanwang *hanwang = urb->context;
  237. struct usb_device *dev = hanwang->usbdev;
  238. int retval;
  239. switch (urb->status) {
  240. case 0:
  241. /* success */;
  242. hanwang_parse_packet(hanwang);
  243. break;
  244. case -ECONNRESET:
  245. case -ENOENT:
  246. case -ESHUTDOWN:
  247. /* this urb is terminated, clean up */
  248. dev_err(&dev->dev, "%s - urb shutting down with status: %d",
  249. __func__, urb->status);
  250. return;
  251. default:
  252. dev_err(&dev->dev, "%s - nonzero urb status received: %d",
  253. __func__, urb->status);
  254. break;
  255. }
  256. retval = usb_submit_urb(urb, GFP_ATOMIC);
  257. if (retval)
  258. dev_err(&dev->dev, "%s - usb_submit_urb failed with result %d",
  259. __func__, retval);
  260. }
  261. static int hanwang_open(struct input_dev *dev)
  262. {
  263. struct hanwang *hanwang = input_get_drvdata(dev);
  264. hanwang->irq->dev = hanwang->usbdev;
  265. if (usb_submit_urb(hanwang->irq, GFP_KERNEL))
  266. return -EIO;
  267. return 0;
  268. }
  269. static void hanwang_close(struct input_dev *dev)
  270. {
  271. struct hanwang *hanwang = input_get_drvdata(dev);
  272. usb_kill_urb(hanwang->irq);
  273. }
  274. static bool get_features(struct usb_device *dev, struct hanwang *hanwang)
  275. {
  276. int i;
  277. for (i = 0; i < ARRAY_SIZE(features_array); i++) {
  278. if (le16_to_cpu(dev->descriptor.idProduct) ==
  279. features_array[i].pid) {
  280. hanwang->features = &features_array[i];
  281. return true;
  282. }
  283. }
  284. return false;
  285. }
  286. static int hanwang_probe(struct usb_interface *intf, const struct usb_device_id *id)
  287. {
  288. struct usb_device *dev = interface_to_usbdev(intf);
  289. struct usb_endpoint_descriptor *endpoint;
  290. struct hanwang *hanwang;
  291. struct input_dev *input_dev;
  292. int error;
  293. int i;
  294. if (intf->cur_altsetting->desc.bNumEndpoints < 1)
  295. return -ENODEV;
  296. hanwang = kzalloc(sizeof(struct hanwang), GFP_KERNEL);
  297. input_dev = input_allocate_device();
  298. if (!hanwang || !input_dev) {
  299. error = -ENOMEM;
  300. goto fail1;
  301. }
  302. if (!get_features(dev, hanwang)) {
  303. error = -ENXIO;
  304. goto fail1;
  305. }
  306. hanwang->data = usb_alloc_coherent(dev, hanwang->features->pkg_len,
  307. GFP_KERNEL, &hanwang->data_dma);
  308. if (!hanwang->data) {
  309. error = -ENOMEM;
  310. goto fail1;
  311. }
  312. hanwang->irq = usb_alloc_urb(0, GFP_KERNEL);
  313. if (!hanwang->irq) {
  314. error = -ENOMEM;
  315. goto fail2;
  316. }
  317. hanwang->usbdev = dev;
  318. hanwang->dev = input_dev;
  319. usb_make_path(dev, hanwang->phys, sizeof(hanwang->phys));
  320. strlcat(hanwang->phys, "/input0", sizeof(hanwang->phys));
  321. strlcpy(hanwang->name, hanwang->features->name, sizeof(hanwang->name));
  322. input_dev->name = hanwang->name;
  323. input_dev->phys = hanwang->phys;
  324. usb_to_input_id(dev, &input_dev->id);
  325. input_dev->dev.parent = &intf->dev;
  326. input_set_drvdata(input_dev, hanwang);
  327. input_dev->open = hanwang_open;
  328. input_dev->close = hanwang_close;
  329. for (i = 0; i < ARRAY_SIZE(hw_eventtypes); ++i)
  330. __set_bit(hw_eventtypes[i], input_dev->evbit);
  331. for (i = 0; i < ARRAY_SIZE(hw_absevents); ++i)
  332. __set_bit(hw_absevents[i], input_dev->absbit);
  333. for (i = 0; i < ARRAY_SIZE(hw_btnevents); ++i)
  334. __set_bit(hw_btnevents[i], input_dev->keybit);
  335. for (i = 0; i < ARRAY_SIZE(hw_mscevents); ++i)
  336. __set_bit(hw_mscevents[i], input_dev->mscbit);
  337. input_set_abs_params(input_dev, ABS_X,
  338. 0, hanwang->features->max_x, 4, 0);
  339. input_set_abs_params(input_dev, ABS_Y,
  340. 0, hanwang->features->max_y, 4, 0);
  341. input_set_abs_params(input_dev, ABS_TILT_X,
  342. 0, hanwang->features->max_tilt_x, 0, 0);
  343. input_set_abs_params(input_dev, ABS_TILT_Y,
  344. 0, hanwang->features->max_tilt_y, 0, 0);
  345. input_set_abs_params(input_dev, ABS_PRESSURE,
  346. 0, hanwang->features->max_pressure, 0, 0);
  347. endpoint = &intf->cur_altsetting->endpoint[0].desc;
  348. usb_fill_int_urb(hanwang->irq, dev,
  349. usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  350. hanwang->data, hanwang->features->pkg_len,
  351. hanwang_irq, hanwang, endpoint->bInterval);
  352. hanwang->irq->transfer_dma = hanwang->data_dma;
  353. hanwang->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  354. error = input_register_device(hanwang->dev);
  355. if (error)
  356. goto fail3;
  357. usb_set_intfdata(intf, hanwang);
  358. return 0;
  359. fail3: usb_free_urb(hanwang->irq);
  360. fail2: usb_free_coherent(dev, hanwang->features->pkg_len,
  361. hanwang->data, hanwang->data_dma);
  362. fail1: input_free_device(input_dev);
  363. kfree(hanwang);
  364. return error;
  365. }
  366. static void hanwang_disconnect(struct usb_interface *intf)
  367. {
  368. struct hanwang *hanwang = usb_get_intfdata(intf);
  369. input_unregister_device(hanwang->dev);
  370. usb_free_urb(hanwang->irq);
  371. usb_free_coherent(interface_to_usbdev(intf),
  372. hanwang->features->pkg_len, hanwang->data,
  373. hanwang->data_dma);
  374. kfree(hanwang);
  375. usb_set_intfdata(intf, NULL);
  376. }
  377. static const struct usb_device_id hanwang_ids[] = {
  378. { HANWANG_TABLET_DEVICE(USB_VENDOR_ID_HANWANG, HANWANG_TABLET_INT_CLASS,
  379. HANWANG_TABLET_INT_SUB_CLASS, HANWANG_TABLET_INT_PROTOCOL) },
  380. {}
  381. };
  382. MODULE_DEVICE_TABLE(usb, hanwang_ids);
  383. static struct usb_driver hanwang_driver = {
  384. .name = "hanwang",
  385. .probe = hanwang_probe,
  386. .disconnect = hanwang_disconnect,
  387. .id_table = hanwang_ids,
  388. };
  389. module_usb_driver(hanwang_driver);