wacom_sys.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  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_USAGE_CONTACTMAX 0x55
  29. #define HID_COLLECTION 0xa1
  30. #define HID_COLLECTION_LOGICAL 0x02
  31. #define HID_COLLECTION_END 0xc0
  32. enum {
  33. WCM_UNDEFINED = 0,
  34. WCM_DESKTOP,
  35. WCM_DIGITIZER,
  36. };
  37. struct hid_descriptor {
  38. struct usb_descriptor_header header;
  39. __le16 bcdHID;
  40. u8 bCountryCode;
  41. u8 bNumDescriptors;
  42. u8 bDescriptorType;
  43. __le16 wDescriptorLength;
  44. } __attribute__ ((packed));
  45. /* defines to get/set USB message */
  46. #define USB_REQ_GET_REPORT 0x01
  47. #define USB_REQ_SET_REPORT 0x09
  48. #define WAC_HID_FEATURE_REPORT 0x03
  49. #define WAC_MSG_RETRIES 5
  50. #define WAC_CMD_LED_CONTROL 0x20
  51. #define WAC_CMD_ICON_START 0x21
  52. #define WAC_CMD_ICON_XFER 0x23
  53. #define WAC_CMD_RETRIES 10
  54. static int wacom_get_report(struct usb_interface *intf, u8 type, u8 id,
  55. void *buf, size_t size, unsigned int retries)
  56. {
  57. struct usb_device *dev = interface_to_usbdev(intf);
  58. int retval;
  59. do {
  60. retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  61. USB_REQ_GET_REPORT,
  62. USB_DIR_IN | USB_TYPE_CLASS |
  63. USB_RECIP_INTERFACE,
  64. (type << 8) + id,
  65. intf->altsetting[0].desc.bInterfaceNumber,
  66. buf, size, 100);
  67. } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
  68. return retval;
  69. }
  70. static int wacom_set_report(struct usb_interface *intf, u8 type, u8 id,
  71. void *buf, size_t size, unsigned int retries)
  72. {
  73. struct usb_device *dev = interface_to_usbdev(intf);
  74. int retval;
  75. do {
  76. retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  77. USB_REQ_SET_REPORT,
  78. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  79. (type << 8) + id,
  80. intf->altsetting[0].desc.bInterfaceNumber,
  81. buf, size, 1000);
  82. } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
  83. return retval;
  84. }
  85. static void wacom_sys_irq(struct urb *urb)
  86. {
  87. struct wacom *wacom = urb->context;
  88. struct device *dev = &wacom->intf->dev;
  89. int retval;
  90. switch (urb->status) {
  91. case 0:
  92. /* success */
  93. break;
  94. case -ECONNRESET:
  95. case -ENOENT:
  96. case -ESHUTDOWN:
  97. /* this urb is terminated, clean up */
  98. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  99. __func__, urb->status);
  100. return;
  101. default:
  102. dev_dbg(dev, "%s - nonzero urb status received: %d\n",
  103. __func__, urb->status);
  104. goto exit;
  105. }
  106. wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
  107. exit:
  108. usb_mark_last_busy(wacom->usbdev);
  109. retval = usb_submit_urb(urb, GFP_ATOMIC);
  110. if (retval)
  111. dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
  112. __func__, retval);
  113. }
  114. static int wacom_open(struct input_dev *dev)
  115. {
  116. struct wacom *wacom = input_get_drvdata(dev);
  117. int retval = 0;
  118. if (usb_autopm_get_interface(wacom->intf) < 0)
  119. return -EIO;
  120. mutex_lock(&wacom->lock);
  121. if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
  122. retval = -EIO;
  123. goto out;
  124. }
  125. wacom->open = true;
  126. wacom->intf->needs_remote_wakeup = 1;
  127. out:
  128. mutex_unlock(&wacom->lock);
  129. usb_autopm_put_interface(wacom->intf);
  130. return retval;
  131. }
  132. static void wacom_close(struct input_dev *dev)
  133. {
  134. struct wacom *wacom = input_get_drvdata(dev);
  135. int autopm_error;
  136. autopm_error = usb_autopm_get_interface(wacom->intf);
  137. mutex_lock(&wacom->lock);
  138. usb_kill_urb(wacom->irq);
  139. wacom->open = false;
  140. wacom->intf->needs_remote_wakeup = 0;
  141. mutex_unlock(&wacom->lock);
  142. if (!autopm_error)
  143. usb_autopm_put_interface(wacom->intf);
  144. }
  145. /*
  146. * Calculate the resolution of the X or Y axis, given appropriate HID data.
  147. * This function is little more than hidinput_calc_abs_res stripped down.
  148. */
  149. static int wacom_calc_hid_res(int logical_extents, int physical_extents,
  150. unsigned char unit, unsigned char exponent)
  151. {
  152. int prev, unit_exponent;
  153. /* Check if the extents are sane */
  154. if (logical_extents <= 0 || physical_extents <= 0)
  155. return 0;
  156. /* Get signed value of nybble-sized twos-compliment exponent */
  157. unit_exponent = exponent;
  158. if (unit_exponent > 7)
  159. unit_exponent -= 16;
  160. /* Convert physical_extents to millimeters */
  161. if (unit == 0x11) { /* If centimeters */
  162. unit_exponent += 1;
  163. } else if (unit == 0x13) { /* If inches */
  164. prev = physical_extents;
  165. physical_extents *= 254;
  166. if (physical_extents < prev)
  167. return 0;
  168. unit_exponent -= 1;
  169. } else {
  170. return 0;
  171. }
  172. /* Apply negative unit exponent */
  173. for (; unit_exponent < 0; unit_exponent++) {
  174. prev = logical_extents;
  175. logical_extents *= 10;
  176. if (logical_extents < prev)
  177. return 0;
  178. }
  179. /* Apply positive unit exponent */
  180. for (; unit_exponent > 0; unit_exponent--) {
  181. prev = physical_extents;
  182. physical_extents *= 10;
  183. if (physical_extents < prev)
  184. return 0;
  185. }
  186. /* Calculate resolution */
  187. return logical_extents / physical_extents;
  188. }
  189. static int wacom_parse_logical_collection(unsigned char *report,
  190. struct wacom_features *features)
  191. {
  192. int length = 0;
  193. if (features->type == BAMBOO_PT) {
  194. /* Logical collection is only used by 3rd gen Bamboo Touch */
  195. features->pktlen = WACOM_PKGLEN_BBTOUCH3;
  196. features->device_type = BTN_TOOL_FINGER;
  197. features->x_max = features->y_max =
  198. get_unaligned_le16(&report[10]);
  199. length = 11;
  200. }
  201. return length;
  202. }
  203. static void wacom_retrieve_report_data(struct usb_interface *intf,
  204. struct wacom_features *features)
  205. {
  206. int result = 0;
  207. unsigned char *rep_data;
  208. rep_data = kmalloc(2, GFP_KERNEL);
  209. if (rep_data) {
  210. rep_data[0] = 12;
  211. result = wacom_get_report(intf, WAC_HID_FEATURE_REPORT,
  212. rep_data[0], rep_data, 2,
  213. WAC_MSG_RETRIES);
  214. if (result >= 0 && rep_data[1] > 2)
  215. features->touch_max = rep_data[1];
  216. kfree(rep_data);
  217. }
  218. }
  219. /*
  220. * Interface Descriptor of wacom devices can be incomplete and
  221. * inconsistent so wacom_features table is used to store stylus
  222. * device's packet lengths, various maximum values, and tablet
  223. * resolution based on product ID's.
  224. *
  225. * For devices that contain 2 interfaces, wacom_features table is
  226. * inaccurate for the touch interface. Since the Interface Descriptor
  227. * for touch interfaces has pretty complete data, this function exists
  228. * to query tablet for this missing information instead of hard coding in
  229. * an additional table.
  230. *
  231. * A typical Interface Descriptor for a stylus will contain a
  232. * boot mouse application collection that is not of interest and this
  233. * function will ignore it.
  234. *
  235. * It also contains a digitizer application collection that also is not
  236. * of interest since any information it contains would be duplicate
  237. * of what is in wacom_features. Usually it defines a report of an array
  238. * of bytes that could be used as max length of the stylus packet returned.
  239. * If it happens to define a Digitizer-Stylus Physical Collection then
  240. * the X and Y logical values contain valid data but it is ignored.
  241. *
  242. * A typical Interface Descriptor for a touch interface will contain a
  243. * Digitizer-Finger Physical Collection which will define both logical
  244. * X/Y maximum as well as the physical size of tablet. Since touch
  245. * interfaces haven't supported pressure or distance, this is enough
  246. * information to override invalid values in the wacom_features table.
  247. *
  248. * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
  249. * Collection. Instead they define a Logical Collection with a single
  250. * Logical Maximum for both X and Y.
  251. *
  252. * Intuos5 touch interface does not contain useful data. We deal with
  253. * this after returning from this function.
  254. */
  255. static int wacom_parse_hid(struct usb_interface *intf,
  256. struct hid_descriptor *hid_desc,
  257. struct wacom_features *features)
  258. {
  259. struct usb_device *dev = interface_to_usbdev(intf);
  260. char limit = 0;
  261. /* result has to be defined as int for some devices */
  262. int result = 0;
  263. int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
  264. unsigned char *report;
  265. report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
  266. if (!report)
  267. return -ENOMEM;
  268. /* retrive report descriptors */
  269. do {
  270. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  271. USB_REQ_GET_DESCRIPTOR,
  272. USB_RECIP_INTERFACE | USB_DIR_IN,
  273. HID_DEVICET_REPORT << 8,
  274. intf->altsetting[0].desc.bInterfaceNumber, /* interface */
  275. report,
  276. hid_desc->wDescriptorLength,
  277. 5000); /* 5 secs */
  278. } while (result < 0 && limit++ < WAC_MSG_RETRIES);
  279. /* No need to parse the Descriptor. It isn't an error though */
  280. if (result < 0)
  281. goto out;
  282. for (i = 0; i < hid_desc->wDescriptorLength; i++) {
  283. switch (report[i]) {
  284. case HID_USAGE_PAGE:
  285. switch (report[i + 1]) {
  286. case HID_USAGE_PAGE_DIGITIZER:
  287. usage = WCM_DIGITIZER;
  288. i++;
  289. break;
  290. case HID_USAGE_PAGE_DESKTOP:
  291. usage = WCM_DESKTOP;
  292. i++;
  293. break;
  294. }
  295. break;
  296. case HID_USAGE:
  297. switch (report[i + 1]) {
  298. case HID_USAGE_X:
  299. if (usage == WCM_DESKTOP) {
  300. if (finger) {
  301. features->device_type = BTN_TOOL_FINGER;
  302. switch (features->type) {
  303. case TABLETPC2FG:
  304. features->pktlen = WACOM_PKGLEN_TPC2FG;
  305. break;
  306. case MTSCREEN:
  307. case WACOM_24HDT:
  308. features->pktlen = WACOM_PKGLEN_MTOUCH;
  309. break;
  310. case MTTPC:
  311. features->pktlen = WACOM_PKGLEN_MTTPC;
  312. break;
  313. case BAMBOO_PT:
  314. features->pktlen = WACOM_PKGLEN_BBTOUCH;
  315. break;
  316. default:
  317. features->pktlen = WACOM_PKGLEN_GRAPHIRE;
  318. break;
  319. }
  320. switch (features->type) {
  321. case BAMBOO_PT:
  322. features->x_phy =
  323. get_unaligned_le16(&report[i + 5]);
  324. features->x_max =
  325. get_unaligned_le16(&report[i + 8]);
  326. i += 15;
  327. break;
  328. case WACOM_24HDT:
  329. features->x_max =
  330. get_unaligned_le16(&report[i + 3]);
  331. features->x_phy =
  332. get_unaligned_le16(&report[i + 8]);
  333. features->unit = report[i - 1];
  334. features->unitExpo = report[i - 3];
  335. i += 12;
  336. break;
  337. default:
  338. features->x_max =
  339. get_unaligned_le16(&report[i + 3]);
  340. features->x_phy =
  341. get_unaligned_le16(&report[i + 6]);
  342. features->unit = report[i + 9];
  343. features->unitExpo = report[i + 11];
  344. i += 12;
  345. break;
  346. }
  347. } else if (pen) {
  348. /* penabled only accepts exact bytes of data */
  349. if (features->type >= TABLETPC)
  350. features->pktlen = WACOM_PKGLEN_GRAPHIRE;
  351. features->device_type = BTN_TOOL_PEN;
  352. features->x_max =
  353. get_unaligned_le16(&report[i + 3]);
  354. i += 4;
  355. }
  356. }
  357. break;
  358. case HID_USAGE_Y:
  359. if (usage == WCM_DESKTOP) {
  360. if (finger) {
  361. switch (features->type) {
  362. case TABLETPC2FG:
  363. case MTSCREEN:
  364. case MTTPC:
  365. features->y_max =
  366. get_unaligned_le16(&report[i + 3]);
  367. features->y_phy =
  368. get_unaligned_le16(&report[i + 6]);
  369. i += 7;
  370. break;
  371. case WACOM_24HDT:
  372. features->y_max =
  373. get_unaligned_le16(&report[i + 3]);
  374. features->y_phy =
  375. get_unaligned_le16(&report[i - 2]);
  376. i += 7;
  377. break;
  378. case BAMBOO_PT:
  379. features->y_phy =
  380. get_unaligned_le16(&report[i + 3]);
  381. features->y_max =
  382. get_unaligned_le16(&report[i + 6]);
  383. i += 12;
  384. break;
  385. default:
  386. features->y_max =
  387. features->x_max;
  388. features->y_phy =
  389. get_unaligned_le16(&report[i + 3]);
  390. i += 4;
  391. break;
  392. }
  393. } else if (pen) {
  394. features->y_max =
  395. get_unaligned_le16(&report[i + 3]);
  396. i += 4;
  397. }
  398. }
  399. break;
  400. case HID_USAGE_FINGER:
  401. finger = 1;
  402. i++;
  403. break;
  404. /*
  405. * Requiring Stylus Usage will ignore boot mouse
  406. * X/Y values and some cases of invalid Digitizer X/Y
  407. * values commonly reported.
  408. */
  409. case HID_USAGE_STYLUS:
  410. pen = 1;
  411. i++;
  412. break;
  413. case HID_USAGE_CONTACTMAX:
  414. /* leave touch_max as is if predefined */
  415. if (!features->touch_max)
  416. wacom_retrieve_report_data(intf, features);
  417. i++;
  418. break;
  419. }
  420. break;
  421. case HID_COLLECTION_END:
  422. /* reset UsagePage and Finger */
  423. finger = usage = 0;
  424. break;
  425. case HID_COLLECTION:
  426. i++;
  427. switch (report[i]) {
  428. case HID_COLLECTION_LOGICAL:
  429. i += wacom_parse_logical_collection(&report[i],
  430. features);
  431. break;
  432. }
  433. break;
  434. }
  435. }
  436. out:
  437. result = 0;
  438. kfree(report);
  439. return result;
  440. }
  441. static int wacom_set_device_mode(struct usb_interface *intf, int report_id, int length, int mode)
  442. {
  443. unsigned char *rep_data;
  444. int error = -ENOMEM, limit = 0;
  445. rep_data = kzalloc(length, GFP_KERNEL);
  446. if (!rep_data)
  447. return error;
  448. do {
  449. rep_data[0] = report_id;
  450. rep_data[1] = mode;
  451. error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
  452. report_id, rep_data, length, 1);
  453. if (error >= 0)
  454. error = wacom_get_report(intf, WAC_HID_FEATURE_REPORT,
  455. report_id, rep_data, length, 1);
  456. } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
  457. kfree(rep_data);
  458. return error < 0 ? error : 0;
  459. }
  460. /*
  461. * Switch the tablet into its most-capable mode. Wacom tablets are
  462. * typically configured to power-up in a mode which sends mouse-like
  463. * reports to the OS. To get absolute position, pressure data, etc.
  464. * from the tablet, it is necessary to switch the tablet out of this
  465. * mode and into one which sends the full range of tablet data.
  466. */
  467. static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
  468. {
  469. if (features->device_type == BTN_TOOL_FINGER) {
  470. if (features->type > TABLETPC) {
  471. /* MT Tablet PC touch */
  472. return wacom_set_device_mode(intf, 3, 4, 4);
  473. }
  474. else if (features->type == WACOM_24HDT) {
  475. return wacom_set_device_mode(intf, 18, 3, 2);
  476. }
  477. } else if (features->device_type == BTN_TOOL_PEN) {
  478. if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
  479. return wacom_set_device_mode(intf, 2, 2, 2);
  480. }
  481. }
  482. return 0;
  483. }
  484. static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
  485. struct wacom_features *features)
  486. {
  487. int error = 0;
  488. struct usb_host_interface *interface = intf->cur_altsetting;
  489. struct hid_descriptor *hid_desc;
  490. /* default features */
  491. features->device_type = BTN_TOOL_PEN;
  492. features->x_fuzz = 4;
  493. features->y_fuzz = 4;
  494. features->pressure_fuzz = 0;
  495. features->distance_fuzz = 0;
  496. /*
  497. * The wireless device HID is basic and layout conflicts with
  498. * other tablets (monitor and touch interface can look like pen).
  499. * Skip the query for this type and modify defaults based on
  500. * interface number.
  501. */
  502. if (features->type == WIRELESS) {
  503. if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
  504. features->device_type = 0;
  505. } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
  506. features->device_type = BTN_TOOL_FINGER;
  507. features->pktlen = WACOM_PKGLEN_BBTOUCH3;
  508. }
  509. }
  510. /* only devices that support touch need to retrieve the info */
  511. if (features->type < BAMBOO_PT) {
  512. goto out;
  513. }
  514. error = usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc);
  515. if (error) {
  516. error = usb_get_extra_descriptor(&interface->endpoint[0],
  517. HID_DEVICET_REPORT, &hid_desc);
  518. if (error) {
  519. dev_err(&intf->dev,
  520. "can not retrieve extra class descriptor\n");
  521. goto out;
  522. }
  523. }
  524. error = wacom_parse_hid(intf, hid_desc, features);
  525. out:
  526. return error;
  527. }
  528. struct wacom_usbdev_data {
  529. struct list_head list;
  530. struct kref kref;
  531. struct usb_device *dev;
  532. struct wacom_shared shared;
  533. };
  534. static LIST_HEAD(wacom_udev_list);
  535. static DEFINE_MUTEX(wacom_udev_list_lock);
  536. static struct usb_device *wacom_get_sibling(struct usb_device *dev, int vendor, int product)
  537. {
  538. int port1;
  539. struct usb_device *sibling;
  540. if (vendor == 0 && product == 0)
  541. return dev;
  542. if (dev->parent == NULL)
  543. return NULL;
  544. usb_hub_for_each_child(dev->parent, port1, sibling) {
  545. struct usb_device_descriptor *d;
  546. if (sibling == NULL)
  547. continue;
  548. d = &sibling->descriptor;
  549. if (d->idVendor == vendor && d->idProduct == product)
  550. return sibling;
  551. }
  552. return NULL;
  553. }
  554. static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
  555. {
  556. struct wacom_usbdev_data *data;
  557. list_for_each_entry(data, &wacom_udev_list, list) {
  558. if (data->dev == dev) {
  559. kref_get(&data->kref);
  560. return data;
  561. }
  562. }
  563. return NULL;
  564. }
  565. static int wacom_add_shared_data(struct wacom_wac *wacom,
  566. struct usb_device *dev)
  567. {
  568. struct wacom_usbdev_data *data;
  569. int retval = 0;
  570. mutex_lock(&wacom_udev_list_lock);
  571. data = wacom_get_usbdev_data(dev);
  572. if (!data) {
  573. data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
  574. if (!data) {
  575. retval = -ENOMEM;
  576. goto out;
  577. }
  578. kref_init(&data->kref);
  579. data->dev = dev;
  580. list_add_tail(&data->list, &wacom_udev_list);
  581. }
  582. wacom->shared = &data->shared;
  583. out:
  584. mutex_unlock(&wacom_udev_list_lock);
  585. return retval;
  586. }
  587. static void wacom_release_shared_data(struct kref *kref)
  588. {
  589. struct wacom_usbdev_data *data =
  590. container_of(kref, struct wacom_usbdev_data, kref);
  591. mutex_lock(&wacom_udev_list_lock);
  592. list_del(&data->list);
  593. mutex_unlock(&wacom_udev_list_lock);
  594. kfree(data);
  595. }
  596. static void wacom_remove_shared_data(struct wacom_wac *wacom)
  597. {
  598. struct wacom_usbdev_data *data;
  599. if (wacom->shared) {
  600. data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
  601. kref_put(&data->kref, wacom_release_shared_data);
  602. wacom->shared = NULL;
  603. }
  604. }
  605. static int wacom_led_control(struct wacom *wacom)
  606. {
  607. unsigned char *buf;
  608. int retval;
  609. buf = kzalloc(9, GFP_KERNEL);
  610. if (!buf)
  611. return -ENOMEM;
  612. if (wacom->wacom_wac.features.type >= INTUOS5S &&
  613. wacom->wacom_wac.features.type <= INTUOS5L) {
  614. /*
  615. * Touch Ring and crop mark LED luminance may take on
  616. * one of four values:
  617. * 0 = Low; 1 = Medium; 2 = High; 3 = Off
  618. */
  619. int ring_led = wacom->led.select[0] & 0x03;
  620. int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
  621. int crop_lum = 0;
  622. buf[0] = WAC_CMD_LED_CONTROL;
  623. buf[1] = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
  624. }
  625. else {
  626. int led = wacom->led.select[0] | 0x4;
  627. if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
  628. wacom->wacom_wac.features.type == WACOM_24HD)
  629. led |= (wacom->led.select[1] << 4) | 0x40;
  630. buf[0] = WAC_CMD_LED_CONTROL;
  631. buf[1] = led;
  632. buf[2] = wacom->led.llv;
  633. buf[3] = wacom->led.hlv;
  634. buf[4] = wacom->led.img_lum;
  635. }
  636. retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
  637. buf, 9, WAC_CMD_RETRIES);
  638. kfree(buf);
  639. return retval;
  640. }
  641. static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
  642. {
  643. unsigned char *buf;
  644. int i, retval;
  645. buf = kzalloc(259, GFP_KERNEL);
  646. if (!buf)
  647. return -ENOMEM;
  648. /* Send 'start' command */
  649. buf[0] = WAC_CMD_ICON_START;
  650. buf[1] = 1;
  651. retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
  652. buf, 2, WAC_CMD_RETRIES);
  653. if (retval < 0)
  654. goto out;
  655. buf[0] = WAC_CMD_ICON_XFER;
  656. buf[1] = button_id & 0x07;
  657. for (i = 0; i < 4; i++) {
  658. buf[2] = i;
  659. memcpy(buf + 3, img + i * 256, 256);
  660. retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
  661. buf, 259, WAC_CMD_RETRIES);
  662. if (retval < 0)
  663. break;
  664. }
  665. /* Send 'stop' */
  666. buf[0] = WAC_CMD_ICON_START;
  667. buf[1] = 0;
  668. wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
  669. buf, 2, WAC_CMD_RETRIES);
  670. out:
  671. kfree(buf);
  672. return retval;
  673. }
  674. static ssize_t wacom_led_select_store(struct device *dev, int set_id,
  675. const char *buf, size_t count)
  676. {
  677. struct wacom *wacom = dev_get_drvdata(dev);
  678. unsigned int id;
  679. int err;
  680. err = kstrtouint(buf, 10, &id);
  681. if (err)
  682. return err;
  683. mutex_lock(&wacom->lock);
  684. wacom->led.select[set_id] = id & 0x3;
  685. err = wacom_led_control(wacom);
  686. mutex_unlock(&wacom->lock);
  687. return err < 0 ? err : count;
  688. }
  689. #define DEVICE_LED_SELECT_ATTR(SET_ID) \
  690. static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
  691. struct device_attribute *attr, const char *buf, size_t count) \
  692. { \
  693. return wacom_led_select_store(dev, SET_ID, buf, count); \
  694. } \
  695. static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
  696. struct device_attribute *attr, char *buf) \
  697. { \
  698. struct wacom *wacom = dev_get_drvdata(dev); \
  699. return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
  700. } \
  701. static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
  702. wacom_led##SET_ID##_select_show, \
  703. wacom_led##SET_ID##_select_store)
  704. DEVICE_LED_SELECT_ATTR(0);
  705. DEVICE_LED_SELECT_ATTR(1);
  706. static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
  707. const char *buf, size_t count)
  708. {
  709. unsigned int value;
  710. int err;
  711. err = kstrtouint(buf, 10, &value);
  712. if (err)
  713. return err;
  714. mutex_lock(&wacom->lock);
  715. *dest = value & 0x7f;
  716. err = wacom_led_control(wacom);
  717. mutex_unlock(&wacom->lock);
  718. return err < 0 ? err : count;
  719. }
  720. #define DEVICE_LUMINANCE_ATTR(name, field) \
  721. static ssize_t wacom_##name##_luminance_store(struct device *dev, \
  722. struct device_attribute *attr, const char *buf, size_t count) \
  723. { \
  724. struct wacom *wacom = dev_get_drvdata(dev); \
  725. \
  726. return wacom_luminance_store(wacom, &wacom->led.field, \
  727. buf, count); \
  728. } \
  729. static DEVICE_ATTR(name##_luminance, S_IWUSR, \
  730. NULL, wacom_##name##_luminance_store)
  731. DEVICE_LUMINANCE_ATTR(status0, llv);
  732. DEVICE_LUMINANCE_ATTR(status1, hlv);
  733. DEVICE_LUMINANCE_ATTR(buttons, img_lum);
  734. static ssize_t wacom_button_image_store(struct device *dev, int button_id,
  735. const char *buf, size_t count)
  736. {
  737. struct wacom *wacom = dev_get_drvdata(dev);
  738. int err;
  739. if (count != 1024)
  740. return -EINVAL;
  741. mutex_lock(&wacom->lock);
  742. err = wacom_led_putimage(wacom, button_id, buf);
  743. mutex_unlock(&wacom->lock);
  744. return err < 0 ? err : count;
  745. }
  746. #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
  747. static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
  748. struct device_attribute *attr, const char *buf, size_t count) \
  749. { \
  750. return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
  751. } \
  752. static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
  753. NULL, wacom_btnimg##BUTTON_ID##_store)
  754. DEVICE_BTNIMG_ATTR(0);
  755. DEVICE_BTNIMG_ATTR(1);
  756. DEVICE_BTNIMG_ATTR(2);
  757. DEVICE_BTNIMG_ATTR(3);
  758. DEVICE_BTNIMG_ATTR(4);
  759. DEVICE_BTNIMG_ATTR(5);
  760. DEVICE_BTNIMG_ATTR(6);
  761. DEVICE_BTNIMG_ATTR(7);
  762. static struct attribute *cintiq_led_attrs[] = {
  763. &dev_attr_status_led0_select.attr,
  764. &dev_attr_status_led1_select.attr,
  765. NULL
  766. };
  767. static struct attribute_group cintiq_led_attr_group = {
  768. .name = "wacom_led",
  769. .attrs = cintiq_led_attrs,
  770. };
  771. static struct attribute *intuos4_led_attrs[] = {
  772. &dev_attr_status0_luminance.attr,
  773. &dev_attr_status1_luminance.attr,
  774. &dev_attr_status_led0_select.attr,
  775. &dev_attr_buttons_luminance.attr,
  776. &dev_attr_button0_rawimg.attr,
  777. &dev_attr_button1_rawimg.attr,
  778. &dev_attr_button2_rawimg.attr,
  779. &dev_attr_button3_rawimg.attr,
  780. &dev_attr_button4_rawimg.attr,
  781. &dev_attr_button5_rawimg.attr,
  782. &dev_attr_button6_rawimg.attr,
  783. &dev_attr_button7_rawimg.attr,
  784. NULL
  785. };
  786. static struct attribute_group intuos4_led_attr_group = {
  787. .name = "wacom_led",
  788. .attrs = intuos4_led_attrs,
  789. };
  790. static struct attribute *intuos5_led_attrs[] = {
  791. &dev_attr_status0_luminance.attr,
  792. &dev_attr_status_led0_select.attr,
  793. NULL
  794. };
  795. static struct attribute_group intuos5_led_attr_group = {
  796. .name = "wacom_led",
  797. .attrs = intuos5_led_attrs,
  798. };
  799. static int wacom_initialize_leds(struct wacom *wacom)
  800. {
  801. int error;
  802. /* Initialize default values */
  803. switch (wacom->wacom_wac.features.type) {
  804. case INTUOS4S:
  805. case INTUOS4:
  806. case INTUOS4L:
  807. wacom->led.select[0] = 0;
  808. wacom->led.select[1] = 0;
  809. wacom->led.llv = 10;
  810. wacom->led.hlv = 20;
  811. wacom->led.img_lum = 10;
  812. error = sysfs_create_group(&wacom->intf->dev.kobj,
  813. &intuos4_led_attr_group);
  814. break;
  815. case WACOM_24HD:
  816. case WACOM_21UX2:
  817. wacom->led.select[0] = 0;
  818. wacom->led.select[1] = 0;
  819. wacom->led.llv = 0;
  820. wacom->led.hlv = 0;
  821. wacom->led.img_lum = 0;
  822. error = sysfs_create_group(&wacom->intf->dev.kobj,
  823. &cintiq_led_attr_group);
  824. break;
  825. case INTUOS5S:
  826. case INTUOS5:
  827. case INTUOS5L:
  828. wacom->led.select[0] = 0;
  829. wacom->led.select[1] = 0;
  830. wacom->led.llv = 32;
  831. wacom->led.hlv = 0;
  832. wacom->led.img_lum = 0;
  833. error = sysfs_create_group(&wacom->intf->dev.kobj,
  834. &intuos5_led_attr_group);
  835. break;
  836. default:
  837. return 0;
  838. }
  839. if (error) {
  840. dev_err(&wacom->intf->dev,
  841. "cannot create sysfs group err: %d\n", error);
  842. return error;
  843. }
  844. wacom_led_control(wacom);
  845. return 0;
  846. }
  847. static void wacom_destroy_leds(struct wacom *wacom)
  848. {
  849. switch (wacom->wacom_wac.features.type) {
  850. case INTUOS4S:
  851. case INTUOS4:
  852. case INTUOS4L:
  853. sysfs_remove_group(&wacom->intf->dev.kobj,
  854. &intuos4_led_attr_group);
  855. break;
  856. case WACOM_24HD:
  857. case WACOM_21UX2:
  858. sysfs_remove_group(&wacom->intf->dev.kobj,
  859. &cintiq_led_attr_group);
  860. break;
  861. case INTUOS5S:
  862. case INTUOS5:
  863. case INTUOS5L:
  864. sysfs_remove_group(&wacom->intf->dev.kobj,
  865. &intuos5_led_attr_group);
  866. break;
  867. }
  868. }
  869. static enum power_supply_property wacom_battery_props[] = {
  870. POWER_SUPPLY_PROP_CAPACITY
  871. };
  872. static int wacom_battery_get_property(struct power_supply *psy,
  873. enum power_supply_property psp,
  874. union power_supply_propval *val)
  875. {
  876. struct wacom *wacom = container_of(psy, struct wacom, battery);
  877. int ret = 0;
  878. switch (psp) {
  879. case POWER_SUPPLY_PROP_CAPACITY:
  880. val->intval =
  881. wacom->wacom_wac.battery_capacity * 100 / 31;
  882. break;
  883. default:
  884. ret = -EINVAL;
  885. break;
  886. }
  887. return ret;
  888. }
  889. static int wacom_initialize_battery(struct wacom *wacom)
  890. {
  891. int error = 0;
  892. if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR) {
  893. wacom->battery.properties = wacom_battery_props;
  894. wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
  895. wacom->battery.get_property = wacom_battery_get_property;
  896. wacom->battery.name = "wacom_battery";
  897. wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  898. wacom->battery.use_for_apm = 0;
  899. error = power_supply_register(&wacom->usbdev->dev,
  900. &wacom->battery);
  901. if (!error)
  902. power_supply_powers(&wacom->battery,
  903. &wacom->usbdev->dev);
  904. }
  905. return error;
  906. }
  907. static void wacom_destroy_battery(struct wacom *wacom)
  908. {
  909. if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR &&
  910. wacom->battery.dev) {
  911. power_supply_unregister(&wacom->battery);
  912. wacom->battery.dev = NULL;
  913. }
  914. }
  915. static int wacom_register_input(struct wacom *wacom)
  916. {
  917. struct input_dev *input_dev;
  918. struct usb_interface *intf = wacom->intf;
  919. struct usb_device *dev = interface_to_usbdev(intf);
  920. struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
  921. int error;
  922. input_dev = input_allocate_device();
  923. if (!input_dev) {
  924. error = -ENOMEM;
  925. goto fail1;
  926. }
  927. input_dev->name = wacom_wac->name;
  928. input_dev->dev.parent = &intf->dev;
  929. input_dev->open = wacom_open;
  930. input_dev->close = wacom_close;
  931. usb_to_input_id(dev, &input_dev->id);
  932. input_set_drvdata(input_dev, wacom);
  933. wacom_wac->input = input_dev;
  934. error = wacom_setup_input_capabilities(input_dev, wacom_wac);
  935. if (error)
  936. goto fail1;
  937. error = input_register_device(input_dev);
  938. if (error)
  939. goto fail2;
  940. return 0;
  941. fail2:
  942. input_free_device(input_dev);
  943. wacom_wac->input = NULL;
  944. fail1:
  945. return error;
  946. }
  947. static void wacom_wireless_work(struct work_struct *work)
  948. {
  949. struct wacom *wacom = container_of(work, struct wacom, work);
  950. struct usb_device *usbdev = wacom->usbdev;
  951. struct wacom_wac *wacom_wac = &wacom->wacom_wac;
  952. struct wacom *wacom1, *wacom2;
  953. struct wacom_wac *wacom_wac1, *wacom_wac2;
  954. int error;
  955. /*
  956. * Regardless if this is a disconnect or a new tablet,
  957. * remove any existing input and battery devices.
  958. */
  959. wacom_destroy_battery(wacom);
  960. /* Stylus interface */
  961. wacom1 = usb_get_intfdata(usbdev->config->interface[1]);
  962. wacom_wac1 = &(wacom1->wacom_wac);
  963. if (wacom_wac1->input)
  964. input_unregister_device(wacom_wac1->input);
  965. wacom_wac1->input = NULL;
  966. /* Touch interface */
  967. wacom2 = usb_get_intfdata(usbdev->config->interface[2]);
  968. wacom_wac2 = &(wacom2->wacom_wac);
  969. if (wacom_wac2->input)
  970. input_unregister_device(wacom_wac2->input);
  971. wacom_wac2->input = NULL;
  972. if (wacom_wac->pid == 0) {
  973. dev_info(&wacom->intf->dev, "wireless tablet disconnected\n");
  974. } else {
  975. const struct usb_device_id *id = wacom_ids;
  976. dev_info(&wacom->intf->dev,
  977. "wireless tablet connected with PID %x\n",
  978. wacom_wac->pid);
  979. while (id->match_flags) {
  980. if (id->idVendor == USB_VENDOR_ID_WACOM &&
  981. id->idProduct == wacom_wac->pid)
  982. break;
  983. id++;
  984. }
  985. if (!id->match_flags) {
  986. dev_info(&wacom->intf->dev,
  987. "ignoring unknown PID.\n");
  988. return;
  989. }
  990. /* Stylus interface */
  991. wacom_wac1->features =
  992. *((struct wacom_features *)id->driver_info);
  993. wacom_wac1->features.device_type = BTN_TOOL_PEN;
  994. error = wacom_register_input(wacom1);
  995. if (error)
  996. goto fail1;
  997. /* Touch interface */
  998. wacom_wac2->features =
  999. *((struct wacom_features *)id->driver_info);
  1000. wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
  1001. wacom_wac2->features.device_type = BTN_TOOL_FINGER;
  1002. wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
  1003. error = wacom_register_input(wacom2);
  1004. if (error)
  1005. goto fail2;
  1006. error = wacom_initialize_battery(wacom);
  1007. if (error)
  1008. goto fail3;
  1009. }
  1010. return;
  1011. fail3:
  1012. input_unregister_device(wacom_wac2->input);
  1013. wacom_wac2->input = NULL;
  1014. fail2:
  1015. input_unregister_device(wacom_wac1->input);
  1016. wacom_wac1->input = NULL;
  1017. fail1:
  1018. return;
  1019. }
  1020. /*
  1021. * Not all devices report physical dimensions from HID.
  1022. * Compute the default from hardcoded logical dimension
  1023. * and resolution before driver overwrites them.
  1024. */
  1025. static void wacom_set_default_phy(struct wacom_features *features)
  1026. {
  1027. if (features->x_resolution) {
  1028. features->x_phy = (features->x_max * 100) /
  1029. features->x_resolution;
  1030. features->y_phy = (features->y_max * 100) /
  1031. features->y_resolution;
  1032. }
  1033. }
  1034. static void wacom_calculate_res(struct wacom_features *features)
  1035. {
  1036. features->x_resolution = wacom_calc_hid_res(features->x_max,
  1037. features->x_phy,
  1038. features->unit,
  1039. features->unitExpo);
  1040. features->y_resolution = wacom_calc_hid_res(features->y_max,
  1041. features->y_phy,
  1042. features->unit,
  1043. features->unitExpo);
  1044. }
  1045. static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1046. {
  1047. struct usb_device *dev = interface_to_usbdev(intf);
  1048. struct usb_endpoint_descriptor *endpoint;
  1049. struct wacom *wacom;
  1050. struct wacom_wac *wacom_wac;
  1051. struct wacom_features *features;
  1052. int error;
  1053. if (!id->driver_info)
  1054. return -EINVAL;
  1055. wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
  1056. if (!wacom)
  1057. return -ENOMEM;
  1058. wacom_wac = &wacom->wacom_wac;
  1059. wacom_wac->features = *((struct wacom_features *)id->driver_info);
  1060. features = &wacom_wac->features;
  1061. if (features->pktlen > WACOM_PKGLEN_MAX) {
  1062. error = -EINVAL;
  1063. goto fail1;
  1064. }
  1065. wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
  1066. GFP_KERNEL, &wacom->data_dma);
  1067. if (!wacom_wac->data) {
  1068. error = -ENOMEM;
  1069. goto fail1;
  1070. }
  1071. wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
  1072. if (!wacom->irq) {
  1073. error = -ENOMEM;
  1074. goto fail2;
  1075. }
  1076. wacom->usbdev = dev;
  1077. wacom->intf = intf;
  1078. mutex_init(&wacom->lock);
  1079. INIT_WORK(&wacom->work, wacom_wireless_work);
  1080. usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
  1081. strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
  1082. endpoint = &intf->cur_altsetting->endpoint[0].desc;
  1083. /* set the default size in case we do not get them from hid */
  1084. wacom_set_default_phy(features);
  1085. /* Retrieve the physical and logical size for touch devices */
  1086. error = wacom_retrieve_hid_descriptor(intf, features);
  1087. if (error)
  1088. goto fail3;
  1089. /*
  1090. * Intuos5 has no useful data about its touch interface in its
  1091. * HID descriptor. If this is the touch interface (wMaxPacketSize
  1092. * of WACOM_PKGLEN_BBTOUCH3), override the table values.
  1093. */
  1094. if (features->type >= INTUOS5S && features->type <= INTUOS5L) {
  1095. if (endpoint->wMaxPacketSize == WACOM_PKGLEN_BBTOUCH3) {
  1096. features->device_type = BTN_TOOL_FINGER;
  1097. features->pktlen = WACOM_PKGLEN_BBTOUCH3;
  1098. features->x_max = 4096;
  1099. features->y_max = 4096;
  1100. } else {
  1101. features->device_type = BTN_TOOL_PEN;
  1102. }
  1103. }
  1104. wacom_setup_device_quirks(features);
  1105. /* set unit to "100th of a mm" for devices not reported by HID */
  1106. if (!features->unit) {
  1107. features->unit = 0x11;
  1108. features->unitExpo = 16 - 3;
  1109. }
  1110. wacom_calculate_res(features);
  1111. strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
  1112. if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
  1113. struct usb_device *other_dev;
  1114. /* Append the device type to the name */
  1115. strlcat(wacom_wac->name,
  1116. features->device_type == BTN_TOOL_PEN ?
  1117. " Pen" : " Finger",
  1118. sizeof(wacom_wac->name));
  1119. other_dev = wacom_get_sibling(dev, features->oVid, features->oPid);
  1120. if (other_dev == NULL || wacom_get_usbdev_data(other_dev) == NULL)
  1121. other_dev = dev;
  1122. error = wacom_add_shared_data(wacom_wac, other_dev);
  1123. if (error)
  1124. goto fail3;
  1125. }
  1126. usb_fill_int_urb(wacom->irq, dev,
  1127. usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  1128. wacom_wac->data, features->pktlen,
  1129. wacom_sys_irq, wacom, endpoint->bInterval);
  1130. wacom->irq->transfer_dma = wacom->data_dma;
  1131. wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1132. error = wacom_initialize_leds(wacom);
  1133. if (error)
  1134. goto fail4;
  1135. if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
  1136. error = wacom_register_input(wacom);
  1137. if (error)
  1138. goto fail5;
  1139. }
  1140. /* Note that if query fails it is not a hard failure */
  1141. wacom_query_tablet_data(intf, features);
  1142. usb_set_intfdata(intf, wacom);
  1143. if (features->quirks & WACOM_QUIRK_MONITOR) {
  1144. if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
  1145. error = -EIO;
  1146. goto fail5;
  1147. }
  1148. }
  1149. return 0;
  1150. fail5: wacom_destroy_leds(wacom);
  1151. fail4: wacom_remove_shared_data(wacom_wac);
  1152. fail3: usb_free_urb(wacom->irq);
  1153. fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
  1154. fail1: kfree(wacom);
  1155. return error;
  1156. }
  1157. static void wacom_disconnect(struct usb_interface *intf)
  1158. {
  1159. struct wacom *wacom = usb_get_intfdata(intf);
  1160. usb_set_intfdata(intf, NULL);
  1161. usb_kill_urb(wacom->irq);
  1162. cancel_work_sync(&wacom->work);
  1163. if (wacom->wacom_wac.input)
  1164. input_unregister_device(wacom->wacom_wac.input);
  1165. wacom_destroy_battery(wacom);
  1166. wacom_destroy_leds(wacom);
  1167. usb_free_urb(wacom->irq);
  1168. usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
  1169. wacom->wacom_wac.data, wacom->data_dma);
  1170. wacom_remove_shared_data(&wacom->wacom_wac);
  1171. kfree(wacom);
  1172. }
  1173. static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
  1174. {
  1175. struct wacom *wacom = usb_get_intfdata(intf);
  1176. mutex_lock(&wacom->lock);
  1177. usb_kill_urb(wacom->irq);
  1178. mutex_unlock(&wacom->lock);
  1179. return 0;
  1180. }
  1181. static int wacom_resume(struct usb_interface *intf)
  1182. {
  1183. struct wacom *wacom = usb_get_intfdata(intf);
  1184. struct wacom_features *features = &wacom->wacom_wac.features;
  1185. int rv = 0;
  1186. mutex_lock(&wacom->lock);
  1187. /* switch to wacom mode first */
  1188. wacom_query_tablet_data(intf, features);
  1189. wacom_led_control(wacom);
  1190. if ((wacom->open || (features->quirks & WACOM_QUIRK_MONITOR)) &&
  1191. usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
  1192. rv = -EIO;
  1193. mutex_unlock(&wacom->lock);
  1194. return rv;
  1195. }
  1196. static int wacom_reset_resume(struct usb_interface *intf)
  1197. {
  1198. return wacom_resume(intf);
  1199. }
  1200. static struct usb_driver wacom_driver = {
  1201. .name = "wacom",
  1202. .id_table = wacom_ids,
  1203. .probe = wacom_probe,
  1204. .disconnect = wacom_disconnect,
  1205. .suspend = wacom_suspend,
  1206. .resume = wacom_resume,
  1207. .reset_resume = wacom_reset_resume,
  1208. .supports_autosuspend = 1,
  1209. };
  1210. module_usb_driver(wacom_driver);