wacom_sys.c 32 KB

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