wacom_sys.c 32 KB

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