wacom_sys.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. int retval;
  88. switch (urb->status) {
  89. case 0:
  90. /* success */
  91. break;
  92. case -ECONNRESET:
  93. case -ENOENT:
  94. case -ESHUTDOWN:
  95. /* this urb is terminated, clean up */
  96. dbg("%s - urb shutting down with status: %d", __func__, urb->status);
  97. return;
  98. default:
  99. dbg("%s - nonzero urb status received: %d", __func__, urb->status);
  100. goto exit;
  101. }
  102. wacom_wac_irq(&wacom->wacom_wac, urb->actual_length);
  103. exit:
  104. usb_mark_last_busy(wacom->usbdev);
  105. retval = usb_submit_urb(urb, GFP_ATOMIC);
  106. if (retval)
  107. err ("%s - usb_submit_urb failed with result %d",
  108. __func__, retval);
  109. }
  110. static int wacom_open(struct input_dev *dev)
  111. {
  112. struct wacom *wacom = input_get_drvdata(dev);
  113. int retval = 0;
  114. if (usb_autopm_get_interface(wacom->intf) < 0)
  115. return -EIO;
  116. mutex_lock(&wacom->lock);
  117. if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
  118. retval = -EIO;
  119. goto out;
  120. }
  121. wacom->open = true;
  122. wacom->intf->needs_remote_wakeup = 1;
  123. out:
  124. mutex_unlock(&wacom->lock);
  125. usb_autopm_put_interface(wacom->intf);
  126. return retval;
  127. }
  128. static void wacom_close(struct input_dev *dev)
  129. {
  130. struct wacom *wacom = input_get_drvdata(dev);
  131. int autopm_error;
  132. autopm_error = usb_autopm_get_interface(wacom->intf);
  133. mutex_lock(&wacom->lock);
  134. usb_kill_urb(wacom->irq);
  135. wacom->open = false;
  136. wacom->intf->needs_remote_wakeup = 0;
  137. mutex_unlock(&wacom->lock);
  138. if (!autopm_error)
  139. usb_autopm_put_interface(wacom->intf);
  140. }
  141. static int wacom_parse_logical_collection(unsigned char *report,
  142. struct wacom_features *features)
  143. {
  144. int length = 0;
  145. if (features->type == BAMBOO_PT) {
  146. /* Logical collection is only used by 3rd gen Bamboo Touch */
  147. features->pktlen = WACOM_PKGLEN_BBTOUCH3;
  148. features->device_type = BTN_TOOL_DOUBLETAP;
  149. /*
  150. * Stylus and Touch have same active area
  151. * so compute physical size based on stylus
  152. * data before its overwritten.
  153. */
  154. features->x_phy =
  155. (features->x_max * features->x_resolution) / 100;
  156. features->y_phy =
  157. (features->y_max * features->y_resolution) / 100;
  158. features->x_max = features->y_max =
  159. get_unaligned_le16(&report[10]);
  160. length = 11;
  161. }
  162. return length;
  163. }
  164. /*
  165. * Interface Descriptor of wacom devices can be incomplete and
  166. * inconsistent so wacom_features table is used to store stylus
  167. * device's packet lengths, various maximum values, and tablet
  168. * resolution based on product ID's.
  169. *
  170. * For devices that contain 2 interfaces, wacom_features table is
  171. * inaccurate for the touch interface. Since the Interface Descriptor
  172. * for touch interfaces has pretty complete data, this function exists
  173. * to query tablet for this missing information instead of hard coding in
  174. * an additional table.
  175. *
  176. * A typical Interface Descriptor for a stylus will contain a
  177. * boot mouse application collection that is not of interest and this
  178. * function will ignore it.
  179. *
  180. * It also contains a digitizer application collection that also is not
  181. * of interest since any information it contains would be duplicate
  182. * of what is in wacom_features. Usually it defines a report of an array
  183. * of bytes that could be used as max length of the stylus packet returned.
  184. * If it happens to define a Digitizer-Stylus Physical Collection then
  185. * the X and Y logical values contain valid data but it is ignored.
  186. *
  187. * A typical Interface Descriptor for a touch interface will contain a
  188. * Digitizer-Finger Physical Collection which will define both logical
  189. * X/Y maximum as well as the physical size of tablet. Since touch
  190. * interfaces haven't supported pressure or distance, this is enough
  191. * information to override invalid values in the wacom_features table.
  192. *
  193. * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
  194. * Collection. Instead they define a Logical Collection with a single
  195. * Logical Maximum for both X and Y.
  196. */
  197. static int wacom_parse_hid(struct usb_interface *intf,
  198. struct hid_descriptor *hid_desc,
  199. struct wacom_features *features)
  200. {
  201. struct usb_device *dev = interface_to_usbdev(intf);
  202. char limit = 0;
  203. /* result has to be defined as int for some devices */
  204. int result = 0;
  205. int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
  206. unsigned char *report;
  207. report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
  208. if (!report)
  209. return -ENOMEM;
  210. /* retrive report descriptors */
  211. do {
  212. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  213. USB_REQ_GET_DESCRIPTOR,
  214. USB_RECIP_INTERFACE | USB_DIR_IN,
  215. HID_DEVICET_REPORT << 8,
  216. intf->altsetting[0].desc.bInterfaceNumber, /* interface */
  217. report,
  218. hid_desc->wDescriptorLength,
  219. 5000); /* 5 secs */
  220. } while (result < 0 && limit++ < WAC_MSG_RETRIES);
  221. /* No need to parse the Descriptor. It isn't an error though */
  222. if (result < 0)
  223. goto out;
  224. for (i = 0; i < hid_desc->wDescriptorLength; i++) {
  225. switch (report[i]) {
  226. case HID_USAGE_PAGE:
  227. switch (report[i + 1]) {
  228. case HID_USAGE_PAGE_DIGITIZER:
  229. usage = WCM_DIGITIZER;
  230. i++;
  231. break;
  232. case HID_USAGE_PAGE_DESKTOP:
  233. usage = WCM_DESKTOP;
  234. i++;
  235. break;
  236. }
  237. break;
  238. case HID_USAGE:
  239. switch (report[i + 1]) {
  240. case HID_USAGE_X:
  241. if (usage == WCM_DESKTOP) {
  242. if (finger) {
  243. features->device_type = BTN_TOOL_FINGER;
  244. if (features->type == TABLETPC2FG) {
  245. /* need to reset back */
  246. features->pktlen = WACOM_PKGLEN_TPC2FG;
  247. features->device_type = BTN_TOOL_DOUBLETAP;
  248. }
  249. if (features->type == BAMBOO_PT) {
  250. /* need to reset back */
  251. features->pktlen = WACOM_PKGLEN_BBTOUCH;
  252. features->device_type = BTN_TOOL_DOUBLETAP;
  253. features->x_phy =
  254. get_unaligned_le16(&report[i + 5]);
  255. features->x_max =
  256. get_unaligned_le16(&report[i + 8]);
  257. i += 15;
  258. } else {
  259. features->x_max =
  260. get_unaligned_le16(&report[i + 3]);
  261. features->x_phy =
  262. get_unaligned_le16(&report[i + 6]);
  263. features->unit = report[i + 9];
  264. features->unitExpo = report[i + 11];
  265. i += 12;
  266. }
  267. } else if (pen) {
  268. /* penabled only accepts exact bytes of data */
  269. if (features->type == TABLETPC2FG)
  270. features->pktlen = WACOM_PKGLEN_GRAPHIRE;
  271. features->device_type = BTN_TOOL_PEN;
  272. features->x_max =
  273. get_unaligned_le16(&report[i + 3]);
  274. i += 4;
  275. }
  276. }
  277. break;
  278. case HID_USAGE_Y:
  279. if (usage == WCM_DESKTOP) {
  280. if (finger) {
  281. features->device_type = BTN_TOOL_FINGER;
  282. if (features->type == TABLETPC2FG) {
  283. /* need to reset back */
  284. features->pktlen = WACOM_PKGLEN_TPC2FG;
  285. features->device_type = BTN_TOOL_DOUBLETAP;
  286. features->y_max =
  287. get_unaligned_le16(&report[i + 3]);
  288. features->y_phy =
  289. get_unaligned_le16(&report[i + 6]);
  290. i += 7;
  291. } else if (features->type == BAMBOO_PT) {
  292. /* need to reset back */
  293. features->pktlen = WACOM_PKGLEN_BBTOUCH;
  294. features->device_type = BTN_TOOL_DOUBLETAP;
  295. features->y_phy =
  296. get_unaligned_le16(&report[i + 3]);
  297. features->y_max =
  298. get_unaligned_le16(&report[i + 6]);
  299. i += 12;
  300. } else {
  301. features->y_max =
  302. features->x_max;
  303. features->y_phy =
  304. get_unaligned_le16(&report[i + 3]);
  305. i += 4;
  306. }
  307. } else if (pen) {
  308. /* penabled only accepts exact bytes of data */
  309. if (features->type == TABLETPC2FG)
  310. features->pktlen = WACOM_PKGLEN_GRAPHIRE;
  311. features->device_type = BTN_TOOL_PEN;
  312. features->y_max =
  313. get_unaligned_le16(&report[i + 3]);
  314. i += 4;
  315. }
  316. }
  317. break;
  318. case HID_USAGE_FINGER:
  319. finger = 1;
  320. i++;
  321. break;
  322. /*
  323. * Requiring Stylus Usage will ignore boot mouse
  324. * X/Y values and some cases of invalid Digitizer X/Y
  325. * values commonly reported.
  326. */
  327. case HID_USAGE_STYLUS:
  328. pen = 1;
  329. i++;
  330. break;
  331. }
  332. break;
  333. case HID_COLLECTION_END:
  334. /* reset UsagePage and Finger */
  335. finger = usage = 0;
  336. break;
  337. case HID_COLLECTION:
  338. i++;
  339. switch (report[i]) {
  340. case HID_COLLECTION_LOGICAL:
  341. i += wacom_parse_logical_collection(&report[i],
  342. features);
  343. break;
  344. }
  345. break;
  346. }
  347. }
  348. out:
  349. result = 0;
  350. kfree(report);
  351. return result;
  352. }
  353. static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
  354. {
  355. unsigned char *rep_data;
  356. int limit = 0, report_id = 2;
  357. int error = -ENOMEM;
  358. rep_data = kmalloc(4, GFP_KERNEL);
  359. if (!rep_data)
  360. return error;
  361. /* ask to report tablet data if it is MT Tablet PC or
  362. * not a Tablet PC */
  363. if (features->type == TABLETPC2FG) {
  364. do {
  365. rep_data[0] = 3;
  366. rep_data[1] = 4;
  367. rep_data[2] = 0;
  368. rep_data[3] = 0;
  369. report_id = 3;
  370. error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
  371. report_id, rep_data, 4, 1);
  372. if (error >= 0)
  373. error = wacom_get_report(intf,
  374. WAC_HID_FEATURE_REPORT,
  375. report_id, rep_data, 4, 1);
  376. } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
  377. } else if (features->type != TABLETPC &&
  378. features->device_type == BTN_TOOL_PEN) {
  379. do {
  380. rep_data[0] = 2;
  381. rep_data[1] = 2;
  382. error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
  383. report_id, rep_data, 2, 1);
  384. if (error >= 0)
  385. error = wacom_get_report(intf,
  386. WAC_HID_FEATURE_REPORT,
  387. report_id, rep_data, 2, 1);
  388. } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
  389. }
  390. kfree(rep_data);
  391. return error < 0 ? error : 0;
  392. }
  393. static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
  394. struct wacom_features *features)
  395. {
  396. int error = 0;
  397. struct usb_host_interface *interface = intf->cur_altsetting;
  398. struct hid_descriptor *hid_desc;
  399. /* default features */
  400. features->device_type = BTN_TOOL_PEN;
  401. features->x_fuzz = 4;
  402. features->y_fuzz = 4;
  403. features->pressure_fuzz = 0;
  404. features->distance_fuzz = 0;
  405. /* only Tablet PCs and Bamboo P&T need to retrieve the info */
  406. if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
  407. (features->type != BAMBOO_PT))
  408. goto out;
  409. if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
  410. if (usb_get_extra_descriptor(&interface->endpoint[0],
  411. HID_DEVICET_REPORT, &hid_desc)) {
  412. printk("wacom: can not retrieve extra class descriptor\n");
  413. error = 1;
  414. goto out;
  415. }
  416. }
  417. error = wacom_parse_hid(intf, hid_desc, features);
  418. if (error)
  419. goto out;
  420. out:
  421. return error;
  422. }
  423. struct wacom_usbdev_data {
  424. struct list_head list;
  425. struct kref kref;
  426. struct usb_device *dev;
  427. struct wacom_shared shared;
  428. };
  429. static LIST_HEAD(wacom_udev_list);
  430. static DEFINE_MUTEX(wacom_udev_list_lock);
  431. static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
  432. {
  433. struct wacom_usbdev_data *data;
  434. list_for_each_entry(data, &wacom_udev_list, list) {
  435. if (data->dev == dev) {
  436. kref_get(&data->kref);
  437. return data;
  438. }
  439. }
  440. return NULL;
  441. }
  442. static int wacom_add_shared_data(struct wacom_wac *wacom,
  443. struct usb_device *dev)
  444. {
  445. struct wacom_usbdev_data *data;
  446. int retval = 0;
  447. mutex_lock(&wacom_udev_list_lock);
  448. data = wacom_get_usbdev_data(dev);
  449. if (!data) {
  450. data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
  451. if (!data) {
  452. retval = -ENOMEM;
  453. goto out;
  454. }
  455. kref_init(&data->kref);
  456. data->dev = dev;
  457. list_add_tail(&data->list, &wacom_udev_list);
  458. }
  459. wacom->shared = &data->shared;
  460. out:
  461. mutex_unlock(&wacom_udev_list_lock);
  462. return retval;
  463. }
  464. static void wacom_release_shared_data(struct kref *kref)
  465. {
  466. struct wacom_usbdev_data *data =
  467. container_of(kref, struct wacom_usbdev_data, kref);
  468. mutex_lock(&wacom_udev_list_lock);
  469. list_del(&data->list);
  470. mutex_unlock(&wacom_udev_list_lock);
  471. kfree(data);
  472. }
  473. static void wacom_remove_shared_data(struct wacom_wac *wacom)
  474. {
  475. struct wacom_usbdev_data *data;
  476. if (wacom->shared) {
  477. data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
  478. kref_put(&data->kref, wacom_release_shared_data);
  479. wacom->shared = NULL;
  480. }
  481. }
  482. static int wacom_led_control(struct wacom *wacom)
  483. {
  484. unsigned char *buf;
  485. int retval, led = 0;
  486. buf = kzalloc(9, GFP_KERNEL);
  487. if (!buf)
  488. return -ENOMEM;
  489. if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
  490. wacom->wacom_wac.features.type == WACOM_24HD)
  491. led = (wacom->led.select[1] << 4) | 0x40;
  492. led |= wacom->led.select[0] | 0x4;
  493. buf[0] = WAC_CMD_LED_CONTROL;
  494. buf[1] = led;
  495. buf[2] = wacom->led.llv;
  496. buf[3] = wacom->led.hlv;
  497. buf[4] = wacom->led.img_lum;
  498. retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
  499. buf, 9, WAC_CMD_RETRIES);
  500. kfree(buf);
  501. return retval;
  502. }
  503. static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
  504. {
  505. unsigned char *buf;
  506. int i, retval;
  507. buf = kzalloc(259, GFP_KERNEL);
  508. if (!buf)
  509. return -ENOMEM;
  510. /* Send 'start' command */
  511. buf[0] = WAC_CMD_ICON_START;
  512. buf[1] = 1;
  513. retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
  514. buf, 2, WAC_CMD_RETRIES);
  515. if (retval < 0)
  516. goto out;
  517. buf[0] = WAC_CMD_ICON_XFER;
  518. buf[1] = button_id & 0x07;
  519. for (i = 0; i < 4; i++) {
  520. buf[2] = i;
  521. memcpy(buf + 3, img + i * 256, 256);
  522. retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
  523. buf, 259, WAC_CMD_RETRIES);
  524. if (retval < 0)
  525. break;
  526. }
  527. /* Send 'stop' */
  528. buf[0] = WAC_CMD_ICON_START;
  529. buf[1] = 0;
  530. wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
  531. buf, 2, WAC_CMD_RETRIES);
  532. out:
  533. kfree(buf);
  534. return retval;
  535. }
  536. static ssize_t wacom_led_select_store(struct device *dev, int set_id,
  537. const char *buf, size_t count)
  538. {
  539. struct wacom *wacom = dev_get_drvdata(dev);
  540. unsigned int id;
  541. int err;
  542. err = kstrtouint(buf, 10, &id);
  543. if (err)
  544. return err;
  545. mutex_lock(&wacom->lock);
  546. wacom->led.select[set_id] = id & 0x3;
  547. err = wacom_led_control(wacom);
  548. mutex_unlock(&wacom->lock);
  549. return err < 0 ? err : count;
  550. }
  551. #define DEVICE_LED_SELECT_ATTR(SET_ID) \
  552. static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
  553. struct device_attribute *attr, const char *buf, size_t count) \
  554. { \
  555. return wacom_led_select_store(dev, SET_ID, buf, count); \
  556. } \
  557. static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
  558. struct device_attribute *attr, char *buf) \
  559. { \
  560. struct wacom *wacom = dev_get_drvdata(dev); \
  561. return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
  562. } \
  563. static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
  564. wacom_led##SET_ID##_select_show, \
  565. wacom_led##SET_ID##_select_store)
  566. DEVICE_LED_SELECT_ATTR(0);
  567. DEVICE_LED_SELECT_ATTR(1);
  568. static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
  569. const char *buf, size_t count)
  570. {
  571. unsigned int value;
  572. int err;
  573. err = kstrtouint(buf, 10, &value);
  574. if (err)
  575. return err;
  576. mutex_lock(&wacom->lock);
  577. *dest = value & 0x7f;
  578. err = wacom_led_control(wacom);
  579. mutex_unlock(&wacom->lock);
  580. return err < 0 ? err : count;
  581. }
  582. #define DEVICE_LUMINANCE_ATTR(name, field) \
  583. static ssize_t wacom_##name##_luminance_store(struct device *dev, \
  584. struct device_attribute *attr, const char *buf, size_t count) \
  585. { \
  586. struct wacom *wacom = dev_get_drvdata(dev); \
  587. \
  588. return wacom_luminance_store(wacom, &wacom->led.field, \
  589. buf, count); \
  590. } \
  591. static DEVICE_ATTR(name##_luminance, S_IWUSR, \
  592. NULL, wacom_##name##_luminance_store)
  593. DEVICE_LUMINANCE_ATTR(status0, llv);
  594. DEVICE_LUMINANCE_ATTR(status1, hlv);
  595. DEVICE_LUMINANCE_ATTR(buttons, img_lum);
  596. static ssize_t wacom_button_image_store(struct device *dev, int button_id,
  597. const char *buf, size_t count)
  598. {
  599. struct wacom *wacom = dev_get_drvdata(dev);
  600. int err;
  601. if (count != 1024)
  602. return -EINVAL;
  603. mutex_lock(&wacom->lock);
  604. err = wacom_led_putimage(wacom, button_id, buf);
  605. mutex_unlock(&wacom->lock);
  606. return err < 0 ? err : count;
  607. }
  608. #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
  609. static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
  610. struct device_attribute *attr, const char *buf, size_t count) \
  611. { \
  612. return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
  613. } \
  614. static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
  615. NULL, wacom_btnimg##BUTTON_ID##_store)
  616. DEVICE_BTNIMG_ATTR(0);
  617. DEVICE_BTNIMG_ATTR(1);
  618. DEVICE_BTNIMG_ATTR(2);
  619. DEVICE_BTNIMG_ATTR(3);
  620. DEVICE_BTNIMG_ATTR(4);
  621. DEVICE_BTNIMG_ATTR(5);
  622. DEVICE_BTNIMG_ATTR(6);
  623. DEVICE_BTNIMG_ATTR(7);
  624. static struct attribute *cintiq_led_attrs[] = {
  625. &dev_attr_status_led0_select.attr,
  626. &dev_attr_status_led1_select.attr,
  627. NULL
  628. };
  629. static struct attribute_group cintiq_led_attr_group = {
  630. .name = "wacom_led",
  631. .attrs = cintiq_led_attrs,
  632. };
  633. static struct attribute *intuos4_led_attrs[] = {
  634. &dev_attr_status0_luminance.attr,
  635. &dev_attr_status1_luminance.attr,
  636. &dev_attr_status_led0_select.attr,
  637. &dev_attr_buttons_luminance.attr,
  638. &dev_attr_button0_rawimg.attr,
  639. &dev_attr_button1_rawimg.attr,
  640. &dev_attr_button2_rawimg.attr,
  641. &dev_attr_button3_rawimg.attr,
  642. &dev_attr_button4_rawimg.attr,
  643. &dev_attr_button5_rawimg.attr,
  644. &dev_attr_button6_rawimg.attr,
  645. &dev_attr_button7_rawimg.attr,
  646. NULL
  647. };
  648. static struct attribute_group intuos4_led_attr_group = {
  649. .name = "wacom_led",
  650. .attrs = intuos4_led_attrs,
  651. };
  652. static int wacom_initialize_leds(struct wacom *wacom)
  653. {
  654. int error;
  655. /* Initialize default values */
  656. switch (wacom->wacom_wac.features.type) {
  657. case INTUOS4:
  658. case INTUOS4L:
  659. wacom->led.select[0] = 0;
  660. wacom->led.select[1] = 0;
  661. wacom->led.llv = 10;
  662. wacom->led.hlv = 20;
  663. wacom->led.img_lum = 10;
  664. error = sysfs_create_group(&wacom->intf->dev.kobj,
  665. &intuos4_led_attr_group);
  666. break;
  667. case WACOM_24HD:
  668. case WACOM_21UX2:
  669. wacom->led.select[0] = 0;
  670. wacom->led.select[1] = 0;
  671. wacom->led.llv = 0;
  672. wacom->led.hlv = 0;
  673. wacom->led.img_lum = 0;
  674. error = sysfs_create_group(&wacom->intf->dev.kobj,
  675. &cintiq_led_attr_group);
  676. break;
  677. default:
  678. return 0;
  679. }
  680. if (error) {
  681. dev_err(&wacom->intf->dev,
  682. "cannot create sysfs group err: %d\n", error);
  683. return error;
  684. }
  685. wacom_led_control(wacom);
  686. return 0;
  687. }
  688. static void wacom_destroy_leds(struct wacom *wacom)
  689. {
  690. switch (wacom->wacom_wac.features.type) {
  691. case INTUOS4:
  692. case INTUOS4L:
  693. sysfs_remove_group(&wacom->intf->dev.kobj,
  694. &intuos4_led_attr_group);
  695. break;
  696. case WACOM_24HD:
  697. case WACOM_21UX2:
  698. sysfs_remove_group(&wacom->intf->dev.kobj,
  699. &cintiq_led_attr_group);
  700. break;
  701. }
  702. }
  703. static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
  704. {
  705. struct usb_device *dev = interface_to_usbdev(intf);
  706. struct usb_endpoint_descriptor *endpoint;
  707. struct wacom *wacom;
  708. struct wacom_wac *wacom_wac;
  709. struct wacom_features *features;
  710. struct input_dev *input_dev;
  711. int error;
  712. if (!id->driver_info)
  713. return -EINVAL;
  714. wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
  715. input_dev = input_allocate_device();
  716. if (!wacom || !input_dev) {
  717. error = -ENOMEM;
  718. goto fail1;
  719. }
  720. wacom_wac = &wacom->wacom_wac;
  721. wacom_wac->features = *((struct wacom_features *)id->driver_info);
  722. features = &wacom_wac->features;
  723. if (features->pktlen > WACOM_PKGLEN_MAX) {
  724. error = -EINVAL;
  725. goto fail1;
  726. }
  727. wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
  728. GFP_KERNEL, &wacom->data_dma);
  729. if (!wacom_wac->data) {
  730. error = -ENOMEM;
  731. goto fail1;
  732. }
  733. wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
  734. if (!wacom->irq) {
  735. error = -ENOMEM;
  736. goto fail2;
  737. }
  738. wacom->usbdev = dev;
  739. wacom->intf = intf;
  740. mutex_init(&wacom->lock);
  741. usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
  742. strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
  743. wacom_wac->input = input_dev;
  744. endpoint = &intf->cur_altsetting->endpoint[0].desc;
  745. /* Retrieve the physical and logical size for OEM devices */
  746. error = wacom_retrieve_hid_descriptor(intf, features);
  747. if (error)
  748. goto fail3;
  749. wacom_setup_device_quirks(features);
  750. strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
  751. if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
  752. /* Append the device type to the name */
  753. strlcat(wacom_wac->name,
  754. features->device_type == BTN_TOOL_PEN ?
  755. " Pen" : " Finger",
  756. sizeof(wacom_wac->name));
  757. error = wacom_add_shared_data(wacom_wac, dev);
  758. if (error)
  759. goto fail3;
  760. }
  761. input_dev->name = wacom_wac->name;
  762. input_dev->dev.parent = &intf->dev;
  763. input_dev->open = wacom_open;
  764. input_dev->close = wacom_close;
  765. usb_to_input_id(dev, &input_dev->id);
  766. input_set_drvdata(input_dev, wacom);
  767. wacom_setup_input_capabilities(input_dev, wacom_wac);
  768. usb_fill_int_urb(wacom->irq, dev,
  769. usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  770. wacom_wac->data, features->pktlen,
  771. wacom_sys_irq, wacom, endpoint->bInterval);
  772. wacom->irq->transfer_dma = wacom->data_dma;
  773. wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  774. error = wacom_initialize_leds(wacom);
  775. if (error)
  776. goto fail4;
  777. error = input_register_device(input_dev);
  778. if (error)
  779. goto fail5;
  780. /* Note that if query fails it is not a hard failure */
  781. wacom_query_tablet_data(intf, features);
  782. usb_set_intfdata(intf, wacom);
  783. return 0;
  784. fail5: wacom_destroy_leds(wacom);
  785. fail4: wacom_remove_shared_data(wacom_wac);
  786. fail3: usb_free_urb(wacom->irq);
  787. fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
  788. fail1: input_free_device(input_dev);
  789. kfree(wacom);
  790. return error;
  791. }
  792. static void wacom_disconnect(struct usb_interface *intf)
  793. {
  794. struct wacom *wacom = usb_get_intfdata(intf);
  795. usb_set_intfdata(intf, NULL);
  796. usb_kill_urb(wacom->irq);
  797. input_unregister_device(wacom->wacom_wac.input);
  798. wacom_destroy_leds(wacom);
  799. usb_free_urb(wacom->irq);
  800. usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
  801. wacom->wacom_wac.data, wacom->data_dma);
  802. wacom_remove_shared_data(&wacom->wacom_wac);
  803. kfree(wacom);
  804. }
  805. static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
  806. {
  807. struct wacom *wacom = usb_get_intfdata(intf);
  808. mutex_lock(&wacom->lock);
  809. usb_kill_urb(wacom->irq);
  810. mutex_unlock(&wacom->lock);
  811. return 0;
  812. }
  813. static int wacom_resume(struct usb_interface *intf)
  814. {
  815. struct wacom *wacom = usb_get_intfdata(intf);
  816. struct wacom_features *features = &wacom->wacom_wac.features;
  817. int rv = 0;
  818. mutex_lock(&wacom->lock);
  819. /* switch to wacom mode first */
  820. wacom_query_tablet_data(intf, features);
  821. wacom_led_control(wacom);
  822. if (wacom->open && usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
  823. rv = -EIO;
  824. mutex_unlock(&wacom->lock);
  825. return rv;
  826. }
  827. static int wacom_reset_resume(struct usb_interface *intf)
  828. {
  829. return wacom_resume(intf);
  830. }
  831. static struct usb_driver wacom_driver = {
  832. .name = "wacom",
  833. .id_table = wacom_ids,
  834. .probe = wacom_probe,
  835. .disconnect = wacom_disconnect,
  836. .suspend = wacom_suspend,
  837. .resume = wacom_resume,
  838. .reset_resume = wacom_reset_resume,
  839. .supports_autosuspend = 1,
  840. };
  841. module_usb_driver(wacom_driver);