wacom_sys.c 32 KB

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