wacom_sys.c 16 KB

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