wacom_sys.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. } else if (usage == WCM_DIGITIZER) {
  203. /* max pressure isn't reported
  204. features->pressure_max = (unsigned short)
  205. (report[i+4] << 8 | report[i + 3]);
  206. */
  207. features->pressure_max = 255;
  208. i += 4;
  209. }
  210. break;
  211. case HID_USAGE_Y:
  212. if (usage == WCM_DESKTOP) {
  213. if (finger) {
  214. features->device_type = BTN_TOOL_FINGER;
  215. if (features->type == TABLETPC2FG) {
  216. /* need to reset back */
  217. features->pktlen = WACOM_PKGLEN_TPC2FG;
  218. features->device_type = BTN_TOOL_DOUBLETAP;
  219. features->y_max =
  220. get_unaligned_le16(&report[i + 3]);
  221. features->y_phy =
  222. get_unaligned_le16(&report[i + 6]);
  223. i += 7;
  224. } else if (features->type == BAMBOO_PT) {
  225. /* need to reset back */
  226. features->pktlen = WACOM_PKGLEN_BBTOUCH;
  227. features->device_type = BTN_TOOL_DOUBLETAP;
  228. features->y_phy =
  229. get_unaligned_le16(&report[i + 3]);
  230. features->y_max =
  231. get_unaligned_le16(&report[i + 6]);
  232. i += 12;
  233. } else {
  234. features->y_max =
  235. features->x_max;
  236. features->y_phy =
  237. get_unaligned_le16(&report[i + 3]);
  238. i += 4;
  239. }
  240. } else if (pen) {
  241. /* penabled only accepts exact bytes of data */
  242. if (features->type == TABLETPC2FG)
  243. features->pktlen = WACOM_PKGLEN_GRAPHIRE;
  244. if (features->type == BAMBOO_PT)
  245. features->pktlen = WACOM_PKGLEN_BBFUN;
  246. features->device_type = BTN_TOOL_PEN;
  247. features->y_max =
  248. get_unaligned_le16(&report[i + 3]);
  249. i += 4;
  250. }
  251. }
  252. break;
  253. case HID_USAGE_FINGER:
  254. finger = 1;
  255. i++;
  256. break;
  257. case HID_USAGE_STYLUS:
  258. pen = 1;
  259. i++;
  260. break;
  261. case HID_USAGE_UNDEFINED:
  262. if (usage == WCM_DESKTOP && finger) /* capacity */
  263. features->pressure_max =
  264. get_unaligned_le16(&report[i + 3]);
  265. i += 4;
  266. break;
  267. }
  268. break;
  269. case HID_COLLECTION:
  270. /* reset UsagePage and Finger */
  271. finger = usage = 0;
  272. break;
  273. }
  274. }
  275. out:
  276. result = 0;
  277. kfree(report);
  278. return result;
  279. }
  280. static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
  281. {
  282. unsigned char *rep_data;
  283. int limit = 0, report_id = 2;
  284. int error = -ENOMEM;
  285. rep_data = kmalloc(4, GFP_KERNEL);
  286. if (!rep_data)
  287. return error;
  288. /* ask to report tablet data if it is MT Tablet PC or
  289. * not a Tablet PC */
  290. if (features->type == TABLETPC2FG) {
  291. do {
  292. rep_data[0] = 3;
  293. rep_data[1] = 4;
  294. rep_data[2] = 0;
  295. rep_data[3] = 0;
  296. report_id = 3;
  297. error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
  298. report_id, rep_data, 4);
  299. if (error >= 0)
  300. error = usb_get_report(intf,
  301. WAC_HID_FEATURE_REPORT, report_id,
  302. rep_data, 4);
  303. } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
  304. } else if (features->type != TABLETPC) {
  305. do {
  306. rep_data[0] = 2;
  307. rep_data[1] = 2;
  308. error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
  309. report_id, rep_data, 2);
  310. if (error >= 0)
  311. error = usb_get_report(intf,
  312. WAC_HID_FEATURE_REPORT, report_id,
  313. rep_data, 2);
  314. } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
  315. }
  316. kfree(rep_data);
  317. return error < 0 ? error : 0;
  318. }
  319. static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
  320. struct wacom_features *features)
  321. {
  322. int error = 0;
  323. struct usb_host_interface *interface = intf->cur_altsetting;
  324. struct hid_descriptor *hid_desc;
  325. /* default features */
  326. features->device_type = BTN_TOOL_PEN;
  327. features->x_fuzz = 4;
  328. features->y_fuzz = 4;
  329. features->pressure_fuzz = 0;
  330. features->distance_fuzz = 0;
  331. /* only Tablet PCs and Bamboo P&T need to retrieve the info */
  332. if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
  333. (features->type != BAMBOO_PT))
  334. goto out;
  335. if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
  336. if (usb_get_extra_descriptor(&interface->endpoint[0],
  337. HID_DEVICET_REPORT, &hid_desc)) {
  338. printk("wacom: can not retrieve extra class descriptor\n");
  339. error = 1;
  340. goto out;
  341. }
  342. }
  343. error = wacom_parse_hid(intf, hid_desc, features);
  344. if (error)
  345. goto out;
  346. out:
  347. return error;
  348. }
  349. struct wacom_usbdev_data {
  350. struct list_head list;
  351. struct kref kref;
  352. struct usb_device *dev;
  353. struct wacom_shared shared;
  354. };
  355. static LIST_HEAD(wacom_udev_list);
  356. static DEFINE_MUTEX(wacom_udev_list_lock);
  357. static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
  358. {
  359. struct wacom_usbdev_data *data;
  360. list_for_each_entry(data, &wacom_udev_list, list) {
  361. if (data->dev == dev) {
  362. kref_get(&data->kref);
  363. return data;
  364. }
  365. }
  366. return NULL;
  367. }
  368. static int wacom_add_shared_data(struct wacom_wac *wacom,
  369. struct usb_device *dev)
  370. {
  371. struct wacom_usbdev_data *data;
  372. int retval = 0;
  373. mutex_lock(&wacom_udev_list_lock);
  374. data = wacom_get_usbdev_data(dev);
  375. if (!data) {
  376. data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
  377. if (!data) {
  378. retval = -ENOMEM;
  379. goto out;
  380. }
  381. kref_init(&data->kref);
  382. data->dev = dev;
  383. list_add_tail(&data->list, &wacom_udev_list);
  384. }
  385. wacom->shared = &data->shared;
  386. out:
  387. mutex_unlock(&wacom_udev_list_lock);
  388. return retval;
  389. }
  390. static void wacom_release_shared_data(struct kref *kref)
  391. {
  392. struct wacom_usbdev_data *data =
  393. container_of(kref, struct wacom_usbdev_data, kref);
  394. mutex_lock(&wacom_udev_list_lock);
  395. list_del(&data->list);
  396. mutex_unlock(&wacom_udev_list_lock);
  397. kfree(data);
  398. }
  399. static void wacom_remove_shared_data(struct wacom_wac *wacom)
  400. {
  401. struct wacom_usbdev_data *data;
  402. if (wacom->shared) {
  403. data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
  404. kref_put(&data->kref, wacom_release_shared_data);
  405. wacom->shared = NULL;
  406. }
  407. }
  408. static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
  409. {
  410. struct usb_device *dev = interface_to_usbdev(intf);
  411. struct usb_endpoint_descriptor *endpoint;
  412. struct wacom *wacom;
  413. struct wacom_wac *wacom_wac;
  414. struct wacom_features *features;
  415. struct input_dev *input_dev;
  416. int error;
  417. if (!id->driver_info)
  418. return -EINVAL;
  419. wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
  420. input_dev = input_allocate_device();
  421. if (!wacom || !input_dev) {
  422. error = -ENOMEM;
  423. goto fail1;
  424. }
  425. wacom_wac = &wacom->wacom_wac;
  426. wacom_wac->features = *((struct wacom_features *)id->driver_info);
  427. features = &wacom_wac->features;
  428. if (features->pktlen > WACOM_PKGLEN_MAX) {
  429. error = -EINVAL;
  430. goto fail1;
  431. }
  432. wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
  433. GFP_KERNEL, &wacom->data_dma);
  434. if (!wacom_wac->data) {
  435. error = -ENOMEM;
  436. goto fail1;
  437. }
  438. wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
  439. if (!wacom->irq) {
  440. error = -ENOMEM;
  441. goto fail2;
  442. }
  443. wacom->usbdev = dev;
  444. wacom->intf = intf;
  445. mutex_init(&wacom->lock);
  446. usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
  447. strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
  448. wacom_wac->input = input_dev;
  449. endpoint = &intf->cur_altsetting->endpoint[0].desc;
  450. /* Retrieve the physical and logical size for OEM devices */
  451. error = wacom_retrieve_hid_descriptor(intf, features);
  452. if (error)
  453. goto fail3;
  454. wacom_setup_device_quirks(features);
  455. strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
  456. if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
  457. /* Append the device type to the name */
  458. strlcat(wacom_wac->name,
  459. features->device_type == BTN_TOOL_PEN ?
  460. " Pen" : " Finger",
  461. sizeof(wacom_wac->name));
  462. error = wacom_add_shared_data(wacom_wac, dev);
  463. if (error)
  464. goto fail3;
  465. }
  466. input_dev->name = wacom_wac->name;
  467. input_dev->dev.parent = &intf->dev;
  468. input_dev->open = wacom_open;
  469. input_dev->close = wacom_close;
  470. usb_to_input_id(dev, &input_dev->id);
  471. input_set_drvdata(input_dev, wacom);
  472. wacom_setup_input_capabilities(input_dev, wacom_wac);
  473. usb_fill_int_urb(wacom->irq, dev,
  474. usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  475. wacom_wac->data, features->pktlen,
  476. wacom_sys_irq, wacom, endpoint->bInterval);
  477. wacom->irq->transfer_dma = wacom->data_dma;
  478. wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  479. error = input_register_device(input_dev);
  480. if (error)
  481. goto fail4;
  482. /* Note that if query fails it is not a hard failure */
  483. wacom_query_tablet_data(intf, features);
  484. usb_set_intfdata(intf, wacom);
  485. return 0;
  486. fail4: wacom_remove_shared_data(wacom_wac);
  487. fail3: usb_free_urb(wacom->irq);
  488. fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
  489. fail1: input_free_device(input_dev);
  490. kfree(wacom);
  491. return error;
  492. }
  493. static void wacom_disconnect(struct usb_interface *intf)
  494. {
  495. struct wacom *wacom = usb_get_intfdata(intf);
  496. usb_set_intfdata(intf, NULL);
  497. usb_kill_urb(wacom->irq);
  498. input_unregister_device(wacom->wacom_wac.input);
  499. usb_free_urb(wacom->irq);
  500. usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
  501. wacom->wacom_wac.data, wacom->data_dma);
  502. wacom_remove_shared_data(&wacom->wacom_wac);
  503. kfree(wacom);
  504. }
  505. static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
  506. {
  507. struct wacom *wacom = usb_get_intfdata(intf);
  508. mutex_lock(&wacom->lock);
  509. usb_kill_urb(wacom->irq);
  510. mutex_unlock(&wacom->lock);
  511. return 0;
  512. }
  513. static int wacom_resume(struct usb_interface *intf)
  514. {
  515. struct wacom *wacom = usb_get_intfdata(intf);
  516. struct wacom_features *features = &wacom->wacom_wac.features;
  517. int rv;
  518. mutex_lock(&wacom->lock);
  519. /* switch to wacom mode first */
  520. wacom_query_tablet_data(intf, features);
  521. if (wacom->open)
  522. rv = usb_submit_urb(wacom->irq, GFP_NOIO);
  523. else
  524. rv = 0;
  525. mutex_unlock(&wacom->lock);
  526. return rv;
  527. }
  528. static int wacom_reset_resume(struct usb_interface *intf)
  529. {
  530. return wacom_resume(intf);
  531. }
  532. static struct usb_driver wacom_driver = {
  533. .name = "wacom",
  534. .id_table = wacom_ids,
  535. .probe = wacom_probe,
  536. .disconnect = wacom_disconnect,
  537. .suspend = wacom_suspend,
  538. .resume = wacom_resume,
  539. .reset_resume = wacom_reset_resume,
  540. .supports_autosuspend = 1,
  541. };
  542. static int __init wacom_init(void)
  543. {
  544. int result;
  545. result = usb_register(&wacom_driver);
  546. if (result == 0)
  547. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  548. DRIVER_DESC "\n");
  549. return result;
  550. }
  551. static void __exit wacom_exit(void)
  552. {
  553. usb_deregister(&wacom_driver);
  554. }
  555. module_init(wacom_init);
  556. module_exit(wacom_exit);