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