wacom_sys.c 32 KB

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