hanwang.c 12 KB

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