wacom_sys.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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(2, GFP_KERNEL);
  286. if (!rep_data)
  287. return error;
  288. /* ask to report tablet data if it is 2FGT 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. report_id = 3;
  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, 3);
  301. } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
  302. } else if (features->type != TABLETPC) {
  303. do {
  304. rep_data[0] = 2;
  305. rep_data[1] = 2;
  306. error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
  307. report_id, rep_data, 2);
  308. if (error >= 0)
  309. error = usb_get_report(intf,
  310. WAC_HID_FEATURE_REPORT, report_id,
  311. rep_data, 2);
  312. } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
  313. }
  314. kfree(rep_data);
  315. return error < 0 ? error : 0;
  316. }
  317. static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
  318. struct wacom_features *features)
  319. {
  320. int error = 0;
  321. struct usb_host_interface *interface = intf->cur_altsetting;
  322. struct hid_descriptor *hid_desc;
  323. /* default features */
  324. features->device_type = BTN_TOOL_PEN;
  325. features->x_fuzz = 4;
  326. features->y_fuzz = 4;
  327. features->pressure_fuzz = 0;
  328. features->distance_fuzz = 0;
  329. /* only Tablet PCs and Bamboo P&T need to retrieve the info */
  330. if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
  331. (features->type != BAMBOO_PT))
  332. goto out;
  333. if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
  334. if (usb_get_extra_descriptor(&interface->endpoint[0],
  335. HID_DEVICET_REPORT, &hid_desc)) {
  336. printk("wacom: can not retrieve extra class descriptor\n");
  337. error = 1;
  338. goto out;
  339. }
  340. }
  341. error = wacom_parse_hid(intf, hid_desc, features);
  342. if (error)
  343. goto out;
  344. out:
  345. return error;
  346. }
  347. struct wacom_usbdev_data {
  348. struct list_head list;
  349. struct kref kref;
  350. struct usb_device *dev;
  351. struct wacom_shared shared;
  352. };
  353. static LIST_HEAD(wacom_udev_list);
  354. static DEFINE_MUTEX(wacom_udev_list_lock);
  355. static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
  356. {
  357. struct wacom_usbdev_data *data;
  358. list_for_each_entry(data, &wacom_udev_list, list) {
  359. if (data->dev == dev) {
  360. kref_get(&data->kref);
  361. return data;
  362. }
  363. }
  364. return NULL;
  365. }
  366. static int wacom_add_shared_data(struct wacom_wac *wacom,
  367. struct usb_device *dev)
  368. {
  369. struct wacom_usbdev_data *data;
  370. int retval = 0;
  371. mutex_lock(&wacom_udev_list_lock);
  372. data = wacom_get_usbdev_data(dev);
  373. if (!data) {
  374. data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
  375. if (!data) {
  376. retval = -ENOMEM;
  377. goto out;
  378. }
  379. kref_init(&data->kref);
  380. data->dev = dev;
  381. list_add_tail(&data->list, &wacom_udev_list);
  382. }
  383. wacom->shared = &data->shared;
  384. out:
  385. mutex_unlock(&wacom_udev_list_lock);
  386. return retval;
  387. }
  388. static void wacom_release_shared_data(struct kref *kref)
  389. {
  390. struct wacom_usbdev_data *data =
  391. container_of(kref, struct wacom_usbdev_data, kref);
  392. mutex_lock(&wacom_udev_list_lock);
  393. list_del(&data->list);
  394. mutex_unlock(&wacom_udev_list_lock);
  395. kfree(data);
  396. }
  397. static void wacom_remove_shared_data(struct wacom_wac *wacom)
  398. {
  399. struct wacom_usbdev_data *data;
  400. if (wacom->shared) {
  401. data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
  402. kref_put(&data->kref, wacom_release_shared_data);
  403. wacom->shared = NULL;
  404. }
  405. }
  406. static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
  407. {
  408. struct usb_device *dev = interface_to_usbdev(intf);
  409. struct usb_endpoint_descriptor *endpoint;
  410. struct wacom *wacom;
  411. struct wacom_wac *wacom_wac;
  412. struct wacom_features *features;
  413. struct input_dev *input_dev;
  414. int error;
  415. if (!id->driver_info)
  416. return -EINVAL;
  417. wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
  418. input_dev = input_allocate_device();
  419. if (!wacom || !input_dev) {
  420. error = -ENOMEM;
  421. goto fail1;
  422. }
  423. wacom_wac = &wacom->wacom_wac;
  424. wacom_wac->features = *((struct wacom_features *)id->driver_info);
  425. features = &wacom_wac->features;
  426. if (features->pktlen > WACOM_PKGLEN_MAX) {
  427. error = -EINVAL;
  428. goto fail1;
  429. }
  430. wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
  431. GFP_KERNEL, &wacom->data_dma);
  432. if (!wacom_wac->data) {
  433. error = -ENOMEM;
  434. goto fail1;
  435. }
  436. wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
  437. if (!wacom->irq) {
  438. error = -ENOMEM;
  439. goto fail2;
  440. }
  441. wacom->usbdev = dev;
  442. wacom->intf = intf;
  443. mutex_init(&wacom->lock);
  444. usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
  445. strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
  446. wacom_wac->input = input_dev;
  447. endpoint = &intf->cur_altsetting->endpoint[0].desc;
  448. /* Retrieve the physical and logical size for OEM devices */
  449. error = wacom_retrieve_hid_descriptor(intf, features);
  450. if (error)
  451. goto fail3;
  452. wacom_setup_device_quirks(features);
  453. strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
  454. if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
  455. /* Append the device type to the name */
  456. strlcat(wacom_wac->name,
  457. features->device_type == BTN_TOOL_PEN ?
  458. " Pen" : " Finger",
  459. sizeof(wacom_wac->name));
  460. error = wacom_add_shared_data(wacom_wac, dev);
  461. if (error)
  462. goto fail3;
  463. }
  464. input_dev->name = wacom_wac->name;
  465. input_dev->dev.parent = &intf->dev;
  466. input_dev->open = wacom_open;
  467. input_dev->close = wacom_close;
  468. usb_to_input_id(dev, &input_dev->id);
  469. input_set_drvdata(input_dev, wacom);
  470. wacom_setup_input_capabilities(input_dev, wacom_wac);
  471. usb_fill_int_urb(wacom->irq, dev,
  472. usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  473. wacom_wac->data, features->pktlen,
  474. wacom_sys_irq, wacom, endpoint->bInterval);
  475. wacom->irq->transfer_dma = wacom->data_dma;
  476. wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  477. error = input_register_device(input_dev);
  478. if (error)
  479. goto fail4;
  480. /* Note that if query fails it is not a hard failure */
  481. wacom_query_tablet_data(intf, features);
  482. usb_set_intfdata(intf, wacom);
  483. return 0;
  484. fail4: wacom_remove_shared_data(wacom_wac);
  485. fail3: usb_free_urb(wacom->irq);
  486. fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
  487. fail1: input_free_device(input_dev);
  488. kfree(wacom);
  489. return error;
  490. }
  491. static void wacom_disconnect(struct usb_interface *intf)
  492. {
  493. struct wacom *wacom = usb_get_intfdata(intf);
  494. usb_set_intfdata(intf, NULL);
  495. usb_kill_urb(wacom->irq);
  496. input_unregister_device(wacom->wacom_wac.input);
  497. usb_free_urb(wacom->irq);
  498. usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
  499. wacom->wacom_wac.data, wacom->data_dma);
  500. wacom_remove_shared_data(&wacom->wacom_wac);
  501. kfree(wacom);
  502. }
  503. static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
  504. {
  505. struct wacom *wacom = usb_get_intfdata(intf);
  506. mutex_lock(&wacom->lock);
  507. usb_kill_urb(wacom->irq);
  508. mutex_unlock(&wacom->lock);
  509. return 0;
  510. }
  511. static int wacom_resume(struct usb_interface *intf)
  512. {
  513. struct wacom *wacom = usb_get_intfdata(intf);
  514. struct wacom_features *features = &wacom->wacom_wac.features;
  515. int rv;
  516. mutex_lock(&wacom->lock);
  517. /* switch to wacom mode first */
  518. wacom_query_tablet_data(intf, features);
  519. if (wacom->open)
  520. rv = usb_submit_urb(wacom->irq, GFP_NOIO);
  521. else
  522. rv = 0;
  523. mutex_unlock(&wacom->lock);
  524. return rv;
  525. }
  526. static int wacom_reset_resume(struct usb_interface *intf)
  527. {
  528. return wacom_resume(intf);
  529. }
  530. static struct usb_driver wacom_driver = {
  531. .name = "wacom",
  532. .id_table = wacom_ids,
  533. .probe = wacom_probe,
  534. .disconnect = wacom_disconnect,
  535. .suspend = wacom_suspend,
  536. .resume = wacom_resume,
  537. .reset_resume = wacom_reset_resume,
  538. .supports_autosuspend = 1,
  539. };
  540. static int __init wacom_init(void)
  541. {
  542. int result;
  543. result = usb_register(&wacom_driver);
  544. if (result == 0)
  545. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  546. DRIVER_DESC "\n");
  547. return result;
  548. }
  549. static void __exit wacom_exit(void)
  550. {
  551. usb_deregister(&wacom_driver);
  552. }
  553. module_init(wacom_init);
  554. module_exit(wacom_exit);