wacom_sys.c 36 KB

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