wacom_sys.c 36 KB

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