wacom_sys.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * drivers/input/tablet/wacom_sys.c
  3. *
  4. * USB Wacom tablet support - system specific code
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include "wacom_wac.h"
  13. #include "wacom.h"
  14. /* defines to get HID report descriptor */
  15. #define HID_DEVICET_HID (USB_TYPE_CLASS | 0x01)
  16. #define HID_DEVICET_REPORT (USB_TYPE_CLASS | 0x02)
  17. #define HID_USAGE_UNDEFINED 0x00
  18. #define HID_USAGE_PAGE 0x05
  19. #define HID_USAGE_PAGE_DIGITIZER 0x0d
  20. #define HID_USAGE_PAGE_DESKTOP 0x01
  21. #define HID_USAGE 0x09
  22. #define HID_USAGE_X 0x30
  23. #define HID_USAGE_Y 0x31
  24. #define HID_USAGE_X_TILT 0x3d
  25. #define HID_USAGE_Y_TILT 0x3e
  26. #define HID_USAGE_FINGER 0x22
  27. #define HID_USAGE_STYLUS 0x20
  28. #define HID_COLLECTION 0xc0
  29. enum {
  30. WCM_UNDEFINED = 0,
  31. WCM_DESKTOP,
  32. WCM_DIGITIZER,
  33. };
  34. struct hid_descriptor {
  35. struct usb_descriptor_header header;
  36. __le16 bcdHID;
  37. u8 bCountryCode;
  38. u8 bNumDescriptors;
  39. u8 bDescriptorType;
  40. __le16 wDescriptorLength;
  41. } __attribute__ ((packed));
  42. /* defines to get/set USB message */
  43. #define USB_REQ_GET_REPORT 0x01
  44. #define USB_REQ_SET_REPORT 0x09
  45. #define WAC_HID_FEATURE_REPORT 0x03
  46. static int usb_get_report(struct usb_interface *intf, unsigned char type,
  47. unsigned char id, void *buf, int size)
  48. {
  49. return usb_control_msg(interface_to_usbdev(intf),
  50. usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
  51. USB_REQ_GET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  52. (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
  53. buf, size, 100);
  54. }
  55. static int usb_set_report(struct usb_interface *intf, unsigned char type,
  56. unsigned char id, void *buf, int size)
  57. {
  58. return usb_control_msg(interface_to_usbdev(intf),
  59. usb_sndctrlpipe(interface_to_usbdev(intf), 0),
  60. USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  61. (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
  62. buf, size, 1000);
  63. }
  64. static void wacom_sys_irq(struct urb *urb)
  65. {
  66. struct wacom *wacom = urb->context;
  67. int retval;
  68. switch (urb->status) {
  69. case 0:
  70. /* success */
  71. break;
  72. case -ECONNRESET:
  73. case -ENOENT:
  74. case -ESHUTDOWN:
  75. /* this urb is terminated, clean up */
  76. dbg("%s - urb shutting down with status: %d", __func__, urb->status);
  77. return;
  78. default:
  79. dbg("%s - nonzero urb status received: %d", __func__, urb->status);
  80. goto exit;
  81. }
  82. wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
  83. exit:
  84. usb_mark_last_busy(wacom->usbdev);
  85. retval = usb_submit_urb(urb, GFP_ATOMIC);
  86. if (retval)
  87. err ("%s - usb_submit_urb failed with result %d",
  88. __func__, retval);
  89. }
  90. static int wacom_open(struct input_dev *dev)
  91. {
  92. struct wacom *wacom = input_get_drvdata(dev);
  93. mutex_lock(&wacom->lock);
  94. wacom->irq->dev = wacom->usbdev;
  95. if (usb_autopm_get_interface(wacom->intf) < 0) {
  96. mutex_unlock(&wacom->lock);
  97. return -EIO;
  98. }
  99. if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
  100. usb_autopm_put_interface(wacom->intf);
  101. mutex_unlock(&wacom->lock);
  102. return -EIO;
  103. }
  104. wacom->open = true;
  105. wacom->intf->needs_remote_wakeup = 1;
  106. mutex_unlock(&wacom->lock);
  107. return 0;
  108. }
  109. static void wacom_close(struct input_dev *dev)
  110. {
  111. struct wacom *wacom = input_get_drvdata(dev);
  112. mutex_lock(&wacom->lock);
  113. usb_kill_urb(wacom->irq);
  114. wacom->open = false;
  115. wacom->intf->needs_remote_wakeup = 0;
  116. mutex_unlock(&wacom->lock);
  117. }
  118. static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
  119. struct wacom_features *features)
  120. {
  121. struct usb_device *dev = interface_to_usbdev(intf);
  122. char limit = 0;
  123. /* result has to be defined as int for some devices */
  124. int result = 0;
  125. int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
  126. unsigned char *report;
  127. report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
  128. if (!report)
  129. return -ENOMEM;
  130. /* retrive report descriptors */
  131. do {
  132. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  133. USB_REQ_GET_DESCRIPTOR,
  134. USB_RECIP_INTERFACE | USB_DIR_IN,
  135. HID_DEVICET_REPORT << 8,
  136. intf->altsetting[0].desc.bInterfaceNumber, /* interface */
  137. report,
  138. hid_desc->wDescriptorLength,
  139. 5000); /* 5 secs */
  140. } while (result < 0 && limit++ < 5);
  141. /* No need to parse the Descriptor. It isn't an error though */
  142. if (result < 0)
  143. goto out;
  144. for (i = 0; i < hid_desc->wDescriptorLength; i++) {
  145. switch (report[i]) {
  146. case HID_USAGE_PAGE:
  147. switch (report[i + 1]) {
  148. case HID_USAGE_PAGE_DIGITIZER:
  149. usage = WCM_DIGITIZER;
  150. i++;
  151. break;
  152. case HID_USAGE_PAGE_DESKTOP:
  153. usage = WCM_DESKTOP;
  154. i++;
  155. break;
  156. }
  157. break;
  158. case HID_USAGE:
  159. switch (report[i + 1]) {
  160. case HID_USAGE_X:
  161. if (usage == WCM_DESKTOP) {
  162. if (finger) {
  163. features->device_type = BTN_TOOL_DOUBLETAP;
  164. if (features->type == TABLETPC2FG) {
  165. /* need to reset back */
  166. features->pktlen = WACOM_PKGLEN_TPC2FG;
  167. features->device_type = BTN_TOOL_TRIPLETAP;
  168. }
  169. features->x_max =
  170. get_unaligned_le16(&report[i + 3]);
  171. features->x_phy =
  172. get_unaligned_le16(&report[i + 6]);
  173. features->unit = report[i + 9];
  174. features->unitExpo = report[i + 11];
  175. i += 12;
  176. } else if (pen) {
  177. /* penabled only accepts exact bytes of data */
  178. if (features->type == TABLETPC2FG)
  179. features->pktlen = WACOM_PKGLEN_GRAPHIRE;
  180. features->device_type = BTN_TOOL_PEN;
  181. features->x_max =
  182. get_unaligned_le16(&report[i + 3]);
  183. i += 4;
  184. }
  185. } else if (usage == WCM_DIGITIZER) {
  186. /* max pressure isn't reported
  187. features->pressure_max = (unsigned short)
  188. (report[i+4] << 8 | report[i + 3]);
  189. */
  190. features->pressure_max = 255;
  191. i += 4;
  192. }
  193. break;
  194. case HID_USAGE_Y:
  195. if (usage == WCM_DESKTOP) {
  196. if (finger) {
  197. features->device_type = BTN_TOOL_DOUBLETAP;
  198. if (features->type == TABLETPC2FG) {
  199. /* need to reset back */
  200. features->pktlen = WACOM_PKGLEN_TPC2FG;
  201. features->device_type = BTN_TOOL_TRIPLETAP;
  202. features->y_max =
  203. get_unaligned_le16(&report[i + 3]);
  204. features->y_phy =
  205. get_unaligned_le16(&report[i + 6]);
  206. i += 7;
  207. } else {
  208. features->y_max =
  209. features->x_max;
  210. features->y_phy =
  211. get_unaligned_le16(&report[i + 3]);
  212. i += 4;
  213. }
  214. } else if (pen) {
  215. /* penabled only accepts exact bytes of data */
  216. if (features->type == TABLETPC2FG)
  217. features->pktlen = WACOM_PKGLEN_GRAPHIRE;
  218. features->device_type = BTN_TOOL_PEN;
  219. features->y_max =
  220. get_unaligned_le16(&report[i + 3]);
  221. i += 4;
  222. }
  223. }
  224. break;
  225. case HID_USAGE_FINGER:
  226. finger = 1;
  227. i++;
  228. break;
  229. case HID_USAGE_STYLUS:
  230. pen = 1;
  231. i++;
  232. break;
  233. case HID_USAGE_UNDEFINED:
  234. if (usage == WCM_DESKTOP && finger) /* capacity */
  235. features->pressure_max =
  236. get_unaligned_le16(&report[i + 3]);
  237. i += 4;
  238. break;
  239. }
  240. break;
  241. case HID_COLLECTION:
  242. /* reset UsagePage and Finger */
  243. finger = usage = 0;
  244. break;
  245. }
  246. }
  247. out:
  248. result = 0;
  249. kfree(report);
  250. return result;
  251. }
  252. static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
  253. {
  254. unsigned char *rep_data;
  255. int limit = 0, report_id = 2;
  256. int error = -ENOMEM;
  257. rep_data = kmalloc(2, GFP_KERNEL);
  258. if (!rep_data)
  259. return error;
  260. /* ask to report tablet data if it is 2FGT or not a Tablet PC */
  261. if (features->device_type == BTN_TOOL_TRIPLETAP) {
  262. do {
  263. rep_data[0] = 3;
  264. rep_data[1] = 4;
  265. report_id = 3;
  266. error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
  267. report_id, rep_data, 2);
  268. if (error >= 0)
  269. error = usb_get_report(intf,
  270. WAC_HID_FEATURE_REPORT, report_id,
  271. rep_data, 3);
  272. } while ((error < 0 || rep_data[1] != 4) && limit++ < 5);
  273. } else if (features->type != TABLETPC && features->type != TABLETPC2FG) {
  274. do {
  275. rep_data[0] = 2;
  276. rep_data[1] = 2;
  277. error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
  278. report_id, rep_data, 2);
  279. if (error >= 0)
  280. error = usb_get_report(intf,
  281. WAC_HID_FEATURE_REPORT, report_id,
  282. rep_data, 2);
  283. } while ((error < 0 || rep_data[1] != 2) && limit++ < 5);
  284. }
  285. kfree(rep_data);
  286. return error < 0 ? error : 0;
  287. }
  288. static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
  289. struct wacom_features *features)
  290. {
  291. int error = 0;
  292. struct usb_host_interface *interface = intf->cur_altsetting;
  293. struct hid_descriptor *hid_desc;
  294. /* default device to penabled */
  295. features->device_type = BTN_TOOL_PEN;
  296. /* only Tablet PCs need to retrieve the info */
  297. if ((features->type != TABLETPC) && (features->type != TABLETPC2FG))
  298. goto out;
  299. if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
  300. if (usb_get_extra_descriptor(&interface->endpoint[0],
  301. HID_DEVICET_REPORT, &hid_desc)) {
  302. printk("wacom: can not retrieve extra class descriptor\n");
  303. error = 1;
  304. goto out;
  305. }
  306. }
  307. error = wacom_parse_hid(intf, hid_desc, features);
  308. if (error)
  309. goto out;
  310. /* touch device found but size is not defined. use default */
  311. if (features->device_type == BTN_TOOL_DOUBLETAP && !features->x_max) {
  312. features->x_max = 1023;
  313. features->y_max = 1023;
  314. }
  315. out:
  316. return error;
  317. }
  318. struct wacom_usbdev_data {
  319. struct list_head list;
  320. struct kref kref;
  321. struct usb_device *dev;
  322. struct wacom_shared shared;
  323. };
  324. static LIST_HEAD(wacom_udev_list);
  325. static DEFINE_MUTEX(wacom_udev_list_lock);
  326. static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
  327. {
  328. struct wacom_usbdev_data *data;
  329. list_for_each_entry(data, &wacom_udev_list, list) {
  330. if (data->dev == dev) {
  331. kref_get(&data->kref);
  332. return data;
  333. }
  334. }
  335. return NULL;
  336. }
  337. static int wacom_add_shared_data(struct wacom_wac *wacom,
  338. struct usb_device *dev)
  339. {
  340. struct wacom_usbdev_data *data;
  341. int retval = 0;
  342. mutex_lock(&wacom_udev_list_lock);
  343. data = wacom_get_usbdev_data(dev);
  344. if (!data) {
  345. data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
  346. if (!data) {
  347. retval = -ENOMEM;
  348. goto out;
  349. }
  350. kref_init(&data->kref);
  351. data->dev = dev;
  352. list_add_tail(&data->list, &wacom_udev_list);
  353. }
  354. wacom->shared = &data->shared;
  355. out:
  356. mutex_unlock(&wacom_udev_list_lock);
  357. return retval;
  358. }
  359. static void wacom_release_shared_data(struct kref *kref)
  360. {
  361. struct wacom_usbdev_data *data =
  362. container_of(kref, struct wacom_usbdev_data, kref);
  363. mutex_lock(&wacom_udev_list_lock);
  364. list_del(&data->list);
  365. mutex_unlock(&wacom_udev_list_lock);
  366. kfree(data);
  367. }
  368. static void wacom_remove_shared_data(struct wacom_wac *wacom)
  369. {
  370. struct wacom_usbdev_data *data;
  371. if (wacom->shared) {
  372. data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
  373. kref_put(&data->kref, wacom_release_shared_data);
  374. wacom->shared = NULL;
  375. }
  376. }
  377. static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
  378. {
  379. struct usb_device *dev = interface_to_usbdev(intf);
  380. struct usb_endpoint_descriptor *endpoint;
  381. struct wacom *wacom;
  382. struct wacom_wac *wacom_wac;
  383. struct wacom_features *features;
  384. struct input_dev *input_dev;
  385. int error;
  386. if (!id->driver_info)
  387. return -EINVAL;
  388. wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
  389. input_dev = input_allocate_device();
  390. if (!wacom || !input_dev) {
  391. error = -ENOMEM;
  392. goto fail1;
  393. }
  394. wacom_wac = &wacom->wacom_wac;
  395. wacom_wac->features = *((struct wacom_features *)id->driver_info);
  396. features = &wacom_wac->features;
  397. if (features->pktlen > WACOM_PKGLEN_MAX) {
  398. error = -EINVAL;
  399. goto fail1;
  400. }
  401. wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
  402. GFP_KERNEL, &wacom->data_dma);
  403. if (!wacom_wac->data) {
  404. error = -ENOMEM;
  405. goto fail1;
  406. }
  407. wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
  408. if (!wacom->irq) {
  409. error = -ENOMEM;
  410. goto fail2;
  411. }
  412. wacom->usbdev = dev;
  413. wacom->intf = intf;
  414. mutex_init(&wacom->lock);
  415. usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
  416. strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
  417. wacom_wac->input = input_dev;
  418. endpoint = &intf->cur_altsetting->endpoint[0].desc;
  419. /* Retrieve the physical and logical size for OEM devices */
  420. error = wacom_retrieve_hid_descriptor(intf, features);
  421. if (error)
  422. goto fail2;
  423. strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
  424. if (features->type == TABLETPC || features->type == TABLETPC2FG) {
  425. /* Append the device type to the name */
  426. strlcat(wacom_wac->name,
  427. features->device_type == BTN_TOOL_PEN ?
  428. " Pen" : " Finger",
  429. sizeof(wacom_wac->name));
  430. error = wacom_add_shared_data(wacom_wac, dev);
  431. if (error)
  432. goto fail3;
  433. }
  434. input_dev->name = wacom_wac->name;
  435. input_dev->dev.parent = &intf->dev;
  436. input_dev->open = wacom_open;
  437. input_dev->close = wacom_close;
  438. usb_to_input_id(dev, &input_dev->id);
  439. input_set_drvdata(input_dev, wacom);
  440. wacom_setup_input_capabilities(input_dev, wacom_wac);
  441. usb_fill_int_urb(wacom->irq, dev,
  442. usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  443. wacom_wac->data, features->pktlen,
  444. wacom_sys_irq, wacom, endpoint->bInterval);
  445. wacom->irq->transfer_dma = wacom->data_dma;
  446. wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  447. error = input_register_device(input_dev);
  448. if (error)
  449. goto fail4;
  450. /* Note that if query fails it is not a hard failure */
  451. wacom_query_tablet_data(intf, features);
  452. usb_set_intfdata(intf, wacom);
  453. return 0;
  454. fail4: wacom_remove_shared_data(wacom_wac);
  455. fail3: usb_free_urb(wacom->irq);
  456. fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
  457. fail1: input_free_device(input_dev);
  458. kfree(wacom);
  459. return error;
  460. }
  461. static void wacom_disconnect(struct usb_interface *intf)
  462. {
  463. struct wacom *wacom = usb_get_intfdata(intf);
  464. usb_set_intfdata(intf, NULL);
  465. usb_kill_urb(wacom->irq);
  466. input_unregister_device(wacom->wacom_wac.input);
  467. usb_free_urb(wacom->irq);
  468. usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
  469. wacom->wacom_wac.data, wacom->data_dma);
  470. wacom_remove_shared_data(&wacom->wacom_wac);
  471. kfree(wacom);
  472. }
  473. static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
  474. {
  475. struct wacom *wacom = usb_get_intfdata(intf);
  476. mutex_lock(&wacom->lock);
  477. usb_kill_urb(wacom->irq);
  478. mutex_unlock(&wacom->lock);
  479. return 0;
  480. }
  481. static int wacom_resume(struct usb_interface *intf)
  482. {
  483. struct wacom *wacom = usb_get_intfdata(intf);
  484. struct wacom_features *features = &wacom->wacom_wac.features;
  485. int rv;
  486. mutex_lock(&wacom->lock);
  487. /* switch to wacom mode first */
  488. wacom_query_tablet_data(intf, features);
  489. if (wacom->open)
  490. rv = usb_submit_urb(wacom->irq, GFP_NOIO);
  491. else
  492. rv = 0;
  493. mutex_unlock(&wacom->lock);
  494. return rv;
  495. }
  496. static int wacom_reset_resume(struct usb_interface *intf)
  497. {
  498. return wacom_resume(intf);
  499. }
  500. static struct usb_driver wacom_driver = {
  501. .name = "wacom",
  502. .id_table = wacom_ids,
  503. .probe = wacom_probe,
  504. .disconnect = wacom_disconnect,
  505. .suspend = wacom_suspend,
  506. .resume = wacom_resume,
  507. .reset_resume = wacom_reset_resume,
  508. .supports_autosuspend = 1,
  509. };
  510. static int __init wacom_init(void)
  511. {
  512. int result;
  513. result = usb_register(&wacom_driver);
  514. if (result == 0)
  515. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  516. DRIVER_DESC "\n");
  517. return result;
  518. }
  519. static void __exit wacom_exit(void)
  520. {
  521. usb_deregister(&wacom_driver);
  522. }
  523. module_init(wacom_init);
  524. module_exit(wacom_exit);