wacom_sys.c 29 KB

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