wacom_sys.c 32 KB

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