wacom_sys.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. __u16 wacom_be16_to_cpu(unsigned char *data)
  91. {
  92. __u16 value;
  93. value = be16_to_cpu(*(__be16 *) data);
  94. return value;
  95. }
  96. __u16 wacom_le16_to_cpu(unsigned char *data)
  97. {
  98. __u16 value;
  99. value = le16_to_cpu(*(__le16 *) data);
  100. return value;
  101. }
  102. static int wacom_open(struct input_dev *dev)
  103. {
  104. struct wacom *wacom = input_get_drvdata(dev);
  105. mutex_lock(&wacom->lock);
  106. wacom->irq->dev = wacom->usbdev;
  107. if (usb_autopm_get_interface(wacom->intf) < 0) {
  108. mutex_unlock(&wacom->lock);
  109. return -EIO;
  110. }
  111. if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
  112. usb_autopm_put_interface(wacom->intf);
  113. mutex_unlock(&wacom->lock);
  114. return -EIO;
  115. }
  116. wacom->open = true;
  117. wacom->intf->needs_remote_wakeup = 1;
  118. mutex_unlock(&wacom->lock);
  119. return 0;
  120. }
  121. static void wacom_close(struct input_dev *dev)
  122. {
  123. struct wacom *wacom = input_get_drvdata(dev);
  124. mutex_lock(&wacom->lock);
  125. usb_kill_urb(wacom->irq);
  126. wacom->open = false;
  127. wacom->intf->needs_remote_wakeup = 0;
  128. mutex_unlock(&wacom->lock);
  129. }
  130. static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
  131. struct wacom_features *features)
  132. {
  133. struct usb_device *dev = interface_to_usbdev(intf);
  134. char limit = 0;
  135. /* result has to be defined as int for some devices */
  136. int result = 0;
  137. int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
  138. unsigned char *report;
  139. report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
  140. if (!report)
  141. return -ENOMEM;
  142. /* retrive report descriptors */
  143. do {
  144. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  145. USB_REQ_GET_DESCRIPTOR,
  146. USB_RECIP_INTERFACE | USB_DIR_IN,
  147. HID_DEVICET_REPORT << 8,
  148. intf->altsetting[0].desc.bInterfaceNumber, /* interface */
  149. report,
  150. hid_desc->wDescriptorLength,
  151. 5000); /* 5 secs */
  152. } while (result < 0 && limit++ < 5);
  153. /* No need to parse the Descriptor. It isn't an error though */
  154. if (result < 0)
  155. goto out;
  156. for (i = 0; i < hid_desc->wDescriptorLength; i++) {
  157. switch (report[i]) {
  158. case HID_USAGE_PAGE:
  159. switch (report[i + 1]) {
  160. case HID_USAGE_PAGE_DIGITIZER:
  161. usage = WCM_DIGITIZER;
  162. i++;
  163. break;
  164. case HID_USAGE_PAGE_DESKTOP:
  165. usage = WCM_DESKTOP;
  166. i++;
  167. break;
  168. }
  169. break;
  170. case HID_USAGE:
  171. switch (report[i + 1]) {
  172. case HID_USAGE_X:
  173. if (usage == WCM_DESKTOP) {
  174. if (finger) {
  175. features->device_type = BTN_TOOL_DOUBLETAP;
  176. if (features->type == TABLETPC2FG) {
  177. /* need to reset back */
  178. features->pktlen = WACOM_PKGLEN_TPC2FG;
  179. features->device_type = BTN_TOOL_TRIPLETAP;
  180. }
  181. features->x_max =
  182. wacom_le16_to_cpu(&report[i + 3]);
  183. features->x_phy =
  184. wacom_le16_to_cpu(&report[i + 6]);
  185. features->unit = report[i + 9];
  186. features->unitExpo = report[i + 11];
  187. i += 12;
  188. } else if (pen) {
  189. /* penabled only accepts exact bytes of data */
  190. if (features->type == TABLETPC2FG)
  191. features->pktlen = WACOM_PKGLEN_GRAPHIRE;
  192. features->device_type = BTN_TOOL_PEN;
  193. features->x_max =
  194. wacom_le16_to_cpu(&report[i + 3]);
  195. i += 4;
  196. }
  197. } else if (usage == WCM_DIGITIZER) {
  198. /* max pressure isn't reported
  199. features->pressure_max = (unsigned short)
  200. (report[i+4] << 8 | report[i + 3]);
  201. */
  202. features->pressure_max = 255;
  203. i += 4;
  204. }
  205. break;
  206. case HID_USAGE_Y:
  207. if (usage == WCM_DESKTOP) {
  208. if (finger) {
  209. features->device_type = BTN_TOOL_DOUBLETAP;
  210. if (features->type == TABLETPC2FG) {
  211. /* need to reset back */
  212. features->pktlen = WACOM_PKGLEN_TPC2FG;
  213. features->device_type = BTN_TOOL_TRIPLETAP;
  214. features->y_max =
  215. wacom_le16_to_cpu(&report[i + 3]);
  216. features->y_phy =
  217. wacom_le16_to_cpu(&report[i + 6]);
  218. i += 7;
  219. } else {
  220. features->y_max =
  221. features->x_max;
  222. features->y_phy =
  223. wacom_le16_to_cpu(&report[i + 3]);
  224. i += 4;
  225. }
  226. } else if (pen) {
  227. /* penabled only accepts exact bytes of data */
  228. if (features->type == TABLETPC2FG)
  229. features->pktlen = WACOM_PKGLEN_GRAPHIRE;
  230. features->device_type = BTN_TOOL_PEN;
  231. features->y_max =
  232. wacom_le16_to_cpu(&report[i + 3]);
  233. i += 4;
  234. }
  235. }
  236. break;
  237. case HID_USAGE_FINGER:
  238. finger = 1;
  239. i++;
  240. break;
  241. case HID_USAGE_STYLUS:
  242. pen = 1;
  243. i++;
  244. break;
  245. case HID_USAGE_UNDEFINED:
  246. if (usage == WCM_DESKTOP && finger) /* capacity */
  247. features->pressure_max =
  248. wacom_le16_to_cpu(&report[i + 3]);
  249. i += 4;
  250. break;
  251. }
  252. break;
  253. case HID_COLLECTION:
  254. /* reset UsagePage and Finger */
  255. finger = usage = 0;
  256. break;
  257. }
  258. }
  259. out:
  260. result = 0;
  261. kfree(report);
  262. return result;
  263. }
  264. static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
  265. {
  266. unsigned char *rep_data;
  267. int limit = 0, report_id = 2;
  268. int error = -ENOMEM;
  269. rep_data = kmalloc(2, GFP_KERNEL);
  270. if (!rep_data)
  271. return error;
  272. /* ask to report tablet data if it is 2FGT or not a Tablet PC */
  273. if (features->device_type == BTN_TOOL_TRIPLETAP) {
  274. do {
  275. rep_data[0] = 3;
  276. rep_data[1] = 4;
  277. report_id = 3;
  278. error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
  279. report_id, rep_data, 2);
  280. if (error >= 0)
  281. error = usb_get_report(intf,
  282. WAC_HID_FEATURE_REPORT, report_id,
  283. rep_data, 3);
  284. } while ((error < 0 || rep_data[1] != 4) && limit++ < 5);
  285. } else if (features->type != TABLETPC && features->type != TABLETPC2FG) {
  286. do {
  287. rep_data[0] = 2;
  288. rep_data[1] = 2;
  289. error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
  290. report_id, rep_data, 2);
  291. if (error >= 0)
  292. error = usb_get_report(intf,
  293. WAC_HID_FEATURE_REPORT, report_id,
  294. rep_data, 2);
  295. } while ((error < 0 || rep_data[1] != 2) && limit++ < 5);
  296. }
  297. kfree(rep_data);
  298. return error < 0 ? error : 0;
  299. }
  300. static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
  301. struct wacom_features *features)
  302. {
  303. int error = 0;
  304. struct usb_host_interface *interface = intf->cur_altsetting;
  305. struct hid_descriptor *hid_desc;
  306. /* default device to penabled */
  307. features->device_type = BTN_TOOL_PEN;
  308. /* only Tablet PCs need to retrieve the info */
  309. if ((features->type != TABLETPC) && (features->type != TABLETPC2FG))
  310. goto out;
  311. if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
  312. if (usb_get_extra_descriptor(&interface->endpoint[0],
  313. HID_DEVICET_REPORT, &hid_desc)) {
  314. printk("wacom: can not retrieve extra class descriptor\n");
  315. error = 1;
  316. goto out;
  317. }
  318. }
  319. error = wacom_parse_hid(intf, hid_desc, features);
  320. if (error)
  321. goto out;
  322. /* touch device found but size is not defined. use default */
  323. if (features->device_type == BTN_TOOL_DOUBLETAP && !features->x_max) {
  324. features->x_max = 1023;
  325. features->y_max = 1023;
  326. }
  327. out:
  328. return error;
  329. }
  330. struct wacom_usbdev_data {
  331. struct list_head list;
  332. struct kref kref;
  333. struct usb_device *dev;
  334. struct wacom_shared shared;
  335. };
  336. static LIST_HEAD(wacom_udev_list);
  337. static DEFINE_MUTEX(wacom_udev_list_lock);
  338. static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
  339. {
  340. struct wacom_usbdev_data *data;
  341. list_for_each_entry(data, &wacom_udev_list, list) {
  342. if (data->dev == dev) {
  343. kref_get(&data->kref);
  344. return data;
  345. }
  346. }
  347. return NULL;
  348. }
  349. static int wacom_add_shared_data(struct wacom_wac *wacom,
  350. struct usb_device *dev)
  351. {
  352. struct wacom_usbdev_data *data;
  353. int retval = 0;
  354. mutex_lock(&wacom_udev_list_lock);
  355. data = wacom_get_usbdev_data(dev);
  356. if (!data) {
  357. data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
  358. if (!data) {
  359. retval = -ENOMEM;
  360. goto out;
  361. }
  362. kref_init(&data->kref);
  363. data->dev = dev;
  364. list_add_tail(&data->list, &wacom_udev_list);
  365. }
  366. wacom->shared = &data->shared;
  367. out:
  368. mutex_unlock(&wacom_udev_list_lock);
  369. return retval;
  370. }
  371. static void wacom_release_shared_data(struct kref *kref)
  372. {
  373. struct wacom_usbdev_data *data =
  374. container_of(kref, struct wacom_usbdev_data, kref);
  375. mutex_lock(&wacom_udev_list_lock);
  376. list_del(&data->list);
  377. mutex_unlock(&wacom_udev_list_lock);
  378. kfree(data);
  379. }
  380. static void wacom_remove_shared_data(struct wacom_wac *wacom)
  381. {
  382. struct wacom_usbdev_data *data;
  383. if (wacom->shared) {
  384. data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
  385. kref_put(&data->kref, wacom_release_shared_data);
  386. wacom->shared = NULL;
  387. }
  388. }
  389. static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
  390. {
  391. struct usb_device *dev = interface_to_usbdev(intf);
  392. struct usb_endpoint_descriptor *endpoint;
  393. struct wacom *wacom;
  394. struct wacom_wac *wacom_wac;
  395. struct wacom_features *features;
  396. struct input_dev *input_dev;
  397. int error;
  398. if (!id->driver_info)
  399. return -EINVAL;
  400. wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
  401. input_dev = input_allocate_device();
  402. if (!wacom || !input_dev) {
  403. error = -ENOMEM;
  404. goto fail1;
  405. }
  406. wacom_wac = &wacom->wacom_wac;
  407. wacom_wac->features = *((struct wacom_features *)id->driver_info);
  408. features = &wacom_wac->features;
  409. if (features->pktlen > WACOM_PKGLEN_MAX) {
  410. error = -EINVAL;
  411. goto fail1;
  412. }
  413. wacom_wac->data = usb_buffer_alloc(dev, WACOM_PKGLEN_MAX,
  414. GFP_KERNEL, &wacom->data_dma);
  415. if (!wacom_wac->data) {
  416. error = -ENOMEM;
  417. goto fail1;
  418. }
  419. wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
  420. if (!wacom->irq) {
  421. error = -ENOMEM;
  422. goto fail2;
  423. }
  424. wacom->usbdev = dev;
  425. wacom->intf = intf;
  426. mutex_init(&wacom->lock);
  427. usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
  428. strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
  429. wacom_wac->input = input_dev;
  430. endpoint = &intf->cur_altsetting->endpoint[0].desc;
  431. /* Retrieve the physical and logical size for OEM devices */
  432. error = wacom_retrieve_hid_descriptor(intf, features);
  433. if (error)
  434. goto fail2;
  435. strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
  436. if (features->type == TABLETPC || features->type == TABLETPC2FG) {
  437. /* Append the device type to the name */
  438. strlcat(wacom_wac->name,
  439. features->device_type == BTN_TOOL_PEN ?
  440. " Pen" : " Finger",
  441. sizeof(wacom_wac->name));
  442. error = wacom_add_shared_data(wacom_wac, dev);
  443. if (error)
  444. goto fail3;
  445. }
  446. input_dev->name = wacom_wac->name;
  447. input_dev->name = wacom_wac->name;
  448. input_dev->dev.parent = &intf->dev;
  449. input_dev->open = wacom_open;
  450. input_dev->close = wacom_close;
  451. usb_to_input_id(dev, &input_dev->id);
  452. input_set_drvdata(input_dev, wacom);
  453. wacom_setup_input_capabilities(input_dev, wacom_wac);
  454. usb_fill_int_urb(wacom->irq, dev,
  455. usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  456. wacom_wac->data, features->pktlen,
  457. wacom_sys_irq, wacom, endpoint->bInterval);
  458. wacom->irq->transfer_dma = wacom->data_dma;
  459. wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  460. error = input_register_device(input_dev);
  461. if (error)
  462. goto fail4;
  463. /* Note that if query fails it is not a hard failure */
  464. wacom_query_tablet_data(intf, features);
  465. usb_set_intfdata(intf, wacom);
  466. return 0;
  467. fail4: wacom_remove_shared_data(wacom_wac);
  468. fail3: usb_free_urb(wacom->irq);
  469. fail2: usb_buffer_free(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
  470. fail1: input_free_device(input_dev);
  471. kfree(wacom);
  472. return error;
  473. }
  474. static void wacom_disconnect(struct usb_interface *intf)
  475. {
  476. struct wacom *wacom = usb_get_intfdata(intf);
  477. usb_set_intfdata(intf, NULL);
  478. usb_kill_urb(wacom->irq);
  479. input_unregister_device(wacom->wacom_wac.input);
  480. usb_free_urb(wacom->irq);
  481. usb_buffer_free(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
  482. wacom->wacom_wac.data, wacom->data_dma);
  483. wacom_remove_shared_data(&wacom->wacom_wac);
  484. kfree(wacom);
  485. }
  486. static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
  487. {
  488. struct wacom *wacom = usb_get_intfdata(intf);
  489. mutex_lock(&wacom->lock);
  490. usb_kill_urb(wacom->irq);
  491. mutex_unlock(&wacom->lock);
  492. return 0;
  493. }
  494. static int wacom_resume(struct usb_interface *intf)
  495. {
  496. struct wacom *wacom = usb_get_intfdata(intf);
  497. struct wacom_features *features = &wacom->wacom_wac.features;
  498. int rv;
  499. mutex_lock(&wacom->lock);
  500. /* switch to wacom mode first */
  501. wacom_query_tablet_data(intf, features);
  502. if (wacom->open)
  503. rv = usb_submit_urb(wacom->irq, GFP_NOIO);
  504. else
  505. rv = 0;
  506. mutex_unlock(&wacom->lock);
  507. return rv;
  508. }
  509. static int wacom_reset_resume(struct usb_interface *intf)
  510. {
  511. return wacom_resume(intf);
  512. }
  513. static struct usb_driver wacom_driver = {
  514. .name = "wacom",
  515. .id_table = wacom_ids,
  516. .probe = wacom_probe,
  517. .disconnect = wacom_disconnect,
  518. .suspend = wacom_suspend,
  519. .resume = wacom_resume,
  520. .reset_resume = wacom_reset_resume,
  521. .supports_autosuspend = 1,
  522. };
  523. static int __init wacom_init(void)
  524. {
  525. int result;
  526. result = usb_register(&wacom_driver);
  527. if (result == 0)
  528. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  529. DRIVER_DESC "\n");
  530. return result;
  531. }
  532. static void __exit wacom_exit(void)
  533. {
  534. usb_deregister(&wacom_driver);
  535. }
  536. module_init(wacom_init);
  537. module_exit(wacom_exit);