wacom_sys.c 16 KB

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