wacom_sys.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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_FINGER;
  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 * 100) / features->x_resolution;
  156. features->y_phy =
  157. (features->y_max * 100) / features->y_resolution;
  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. }
  248. if (features->type == BAMBOO_PT) {
  249. /* need to reset back */
  250. features->pktlen = WACOM_PKGLEN_BBTOUCH;
  251. features->x_phy =
  252. get_unaligned_le16(&report[i + 5]);
  253. features->x_max =
  254. get_unaligned_le16(&report[i + 8]);
  255. i += 15;
  256. } else {
  257. features->x_max =
  258. get_unaligned_le16(&report[i + 3]);
  259. features->x_phy =
  260. get_unaligned_le16(&report[i + 6]);
  261. features->unit = report[i + 9];
  262. features->unitExpo = report[i + 11];
  263. i += 12;
  264. }
  265. } else if (pen) {
  266. /* penabled only accepts exact bytes of data */
  267. if (features->type == TABLETPC2FG)
  268. features->pktlen = WACOM_PKGLEN_GRAPHIRE;
  269. features->device_type = BTN_TOOL_PEN;
  270. features->x_max =
  271. get_unaligned_le16(&report[i + 3]);
  272. i += 4;
  273. }
  274. }
  275. break;
  276. case HID_USAGE_Y:
  277. if (usage == WCM_DESKTOP) {
  278. if (finger) {
  279. features->device_type = BTN_TOOL_FINGER;
  280. if (features->type == TABLETPC2FG) {
  281. /* need to reset back */
  282. features->pktlen = WACOM_PKGLEN_TPC2FG;
  283. features->y_max =
  284. get_unaligned_le16(&report[i + 3]);
  285. features->y_phy =
  286. get_unaligned_le16(&report[i + 6]);
  287. i += 7;
  288. } else if (features->type == BAMBOO_PT) {
  289. /* need to reset back */
  290. features->pktlen = WACOM_PKGLEN_BBTOUCH;
  291. features->y_phy =
  292. get_unaligned_le16(&report[i + 3]);
  293. features->y_max =
  294. get_unaligned_le16(&report[i + 6]);
  295. i += 12;
  296. } else {
  297. features->y_max =
  298. features->x_max;
  299. features->y_phy =
  300. get_unaligned_le16(&report[i + 3]);
  301. i += 4;
  302. }
  303. } else if (pen) {
  304. /* penabled only accepts exact bytes of data */
  305. if (features->type == TABLETPC2FG)
  306. features->pktlen = WACOM_PKGLEN_GRAPHIRE;
  307. features->device_type = BTN_TOOL_PEN;
  308. features->y_max =
  309. get_unaligned_le16(&report[i + 3]);
  310. i += 4;
  311. }
  312. }
  313. break;
  314. case HID_USAGE_FINGER:
  315. finger = 1;
  316. i++;
  317. break;
  318. /*
  319. * Requiring Stylus Usage will ignore boot mouse
  320. * X/Y values and some cases of invalid Digitizer X/Y
  321. * values commonly reported.
  322. */
  323. case HID_USAGE_STYLUS:
  324. pen = 1;
  325. i++;
  326. break;
  327. }
  328. break;
  329. case HID_COLLECTION_END:
  330. /* reset UsagePage and Finger */
  331. finger = usage = 0;
  332. break;
  333. case HID_COLLECTION:
  334. i++;
  335. switch (report[i]) {
  336. case HID_COLLECTION_LOGICAL:
  337. i += wacom_parse_logical_collection(&report[i],
  338. features);
  339. break;
  340. }
  341. break;
  342. }
  343. }
  344. out:
  345. result = 0;
  346. kfree(report);
  347. return result;
  348. }
  349. static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
  350. {
  351. unsigned char *rep_data;
  352. int limit = 0, report_id = 2;
  353. int error = -ENOMEM;
  354. rep_data = kmalloc(4, GFP_KERNEL);
  355. if (!rep_data)
  356. return error;
  357. /* ask to report tablet data if it is MT Tablet PC or
  358. * not a Tablet PC */
  359. if (features->type == TABLETPC2FG) {
  360. do {
  361. rep_data[0] = 3;
  362. rep_data[1] = 4;
  363. rep_data[2] = 0;
  364. rep_data[3] = 0;
  365. report_id = 3;
  366. error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
  367. report_id, rep_data, 4, 1);
  368. if (error >= 0)
  369. error = wacom_get_report(intf,
  370. WAC_HID_FEATURE_REPORT,
  371. report_id, rep_data, 4, 1);
  372. } while ((error < 0 || rep_data[1] != 4) && limit++ < WAC_MSG_RETRIES);
  373. } else if (features->type != TABLETPC &&
  374. features->device_type == BTN_TOOL_PEN) {
  375. do {
  376. rep_data[0] = 2;
  377. rep_data[1] = 2;
  378. error = wacom_set_report(intf, WAC_HID_FEATURE_REPORT,
  379. report_id, rep_data, 2, 1);
  380. if (error >= 0)
  381. error = wacom_get_report(intf,
  382. WAC_HID_FEATURE_REPORT,
  383. report_id, rep_data, 2, 1);
  384. } while ((error < 0 || rep_data[1] != 2) && limit++ < WAC_MSG_RETRIES);
  385. }
  386. kfree(rep_data);
  387. return error < 0 ? error : 0;
  388. }
  389. static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
  390. struct wacom_features *features)
  391. {
  392. int error = 0;
  393. struct usb_host_interface *interface = intf->cur_altsetting;
  394. struct hid_descriptor *hid_desc;
  395. /* default features */
  396. features->device_type = BTN_TOOL_PEN;
  397. features->x_fuzz = 4;
  398. features->y_fuzz = 4;
  399. features->pressure_fuzz = 0;
  400. features->distance_fuzz = 0;
  401. /* only Tablet PCs and Bamboo P&T need to retrieve the info */
  402. if ((features->type != TABLETPC) && (features->type != TABLETPC2FG) &&
  403. (features->type != BAMBOO_PT))
  404. goto out;
  405. if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
  406. if (usb_get_extra_descriptor(&interface->endpoint[0],
  407. HID_DEVICET_REPORT, &hid_desc)) {
  408. printk("wacom: can not retrieve extra class descriptor\n");
  409. error = 1;
  410. goto out;
  411. }
  412. }
  413. error = wacom_parse_hid(intf, hid_desc, features);
  414. if (error)
  415. goto out;
  416. out:
  417. return error;
  418. }
  419. struct wacom_usbdev_data {
  420. struct list_head list;
  421. struct kref kref;
  422. struct usb_device *dev;
  423. struct wacom_shared shared;
  424. };
  425. static LIST_HEAD(wacom_udev_list);
  426. static DEFINE_MUTEX(wacom_udev_list_lock);
  427. static struct wacom_usbdev_data *wacom_get_usbdev_data(struct usb_device *dev)
  428. {
  429. struct wacom_usbdev_data *data;
  430. list_for_each_entry(data, &wacom_udev_list, list) {
  431. if (data->dev == dev) {
  432. kref_get(&data->kref);
  433. return data;
  434. }
  435. }
  436. return NULL;
  437. }
  438. static int wacom_add_shared_data(struct wacom_wac *wacom,
  439. struct usb_device *dev)
  440. {
  441. struct wacom_usbdev_data *data;
  442. int retval = 0;
  443. mutex_lock(&wacom_udev_list_lock);
  444. data = wacom_get_usbdev_data(dev);
  445. if (!data) {
  446. data = kzalloc(sizeof(struct wacom_usbdev_data), GFP_KERNEL);
  447. if (!data) {
  448. retval = -ENOMEM;
  449. goto out;
  450. }
  451. kref_init(&data->kref);
  452. data->dev = dev;
  453. list_add_tail(&data->list, &wacom_udev_list);
  454. }
  455. wacom->shared = &data->shared;
  456. out:
  457. mutex_unlock(&wacom_udev_list_lock);
  458. return retval;
  459. }
  460. static void wacom_release_shared_data(struct kref *kref)
  461. {
  462. struct wacom_usbdev_data *data =
  463. container_of(kref, struct wacom_usbdev_data, kref);
  464. mutex_lock(&wacom_udev_list_lock);
  465. list_del(&data->list);
  466. mutex_unlock(&wacom_udev_list_lock);
  467. kfree(data);
  468. }
  469. static void wacom_remove_shared_data(struct wacom_wac *wacom)
  470. {
  471. struct wacom_usbdev_data *data;
  472. if (wacom->shared) {
  473. data = container_of(wacom->shared, struct wacom_usbdev_data, shared);
  474. kref_put(&data->kref, wacom_release_shared_data);
  475. wacom->shared = NULL;
  476. }
  477. }
  478. static int wacom_led_control(struct wacom *wacom)
  479. {
  480. unsigned char *buf;
  481. int retval, led = 0;
  482. buf = kzalloc(9, GFP_KERNEL);
  483. if (!buf)
  484. return -ENOMEM;
  485. if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
  486. wacom->wacom_wac.features.type == WACOM_24HD)
  487. led = (wacom->led.select[1] << 4) | 0x40;
  488. led |= wacom->led.select[0] | 0x4;
  489. buf[0] = WAC_CMD_LED_CONTROL;
  490. buf[1] = led;
  491. buf[2] = wacom->led.llv;
  492. buf[3] = wacom->led.hlv;
  493. buf[4] = wacom->led.img_lum;
  494. retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_LED_CONTROL,
  495. buf, 9, WAC_CMD_RETRIES);
  496. kfree(buf);
  497. return retval;
  498. }
  499. static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
  500. {
  501. unsigned char *buf;
  502. int i, retval;
  503. buf = kzalloc(259, GFP_KERNEL);
  504. if (!buf)
  505. return -ENOMEM;
  506. /* Send 'start' command */
  507. buf[0] = WAC_CMD_ICON_START;
  508. buf[1] = 1;
  509. retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
  510. buf, 2, WAC_CMD_RETRIES);
  511. if (retval < 0)
  512. goto out;
  513. buf[0] = WAC_CMD_ICON_XFER;
  514. buf[1] = button_id & 0x07;
  515. for (i = 0; i < 4; i++) {
  516. buf[2] = i;
  517. memcpy(buf + 3, img + i * 256, 256);
  518. retval = wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_XFER,
  519. buf, 259, WAC_CMD_RETRIES);
  520. if (retval < 0)
  521. break;
  522. }
  523. /* Send 'stop' */
  524. buf[0] = WAC_CMD_ICON_START;
  525. buf[1] = 0;
  526. wacom_set_report(wacom->intf, 0x03, WAC_CMD_ICON_START,
  527. buf, 2, WAC_CMD_RETRIES);
  528. out:
  529. kfree(buf);
  530. return retval;
  531. }
  532. static ssize_t wacom_led_select_store(struct device *dev, int set_id,
  533. const char *buf, size_t count)
  534. {
  535. struct wacom *wacom = dev_get_drvdata(dev);
  536. unsigned int id;
  537. int err;
  538. err = kstrtouint(buf, 10, &id);
  539. if (err)
  540. return err;
  541. mutex_lock(&wacom->lock);
  542. wacom->led.select[set_id] = id & 0x3;
  543. err = wacom_led_control(wacom);
  544. mutex_unlock(&wacom->lock);
  545. return err < 0 ? err : count;
  546. }
  547. #define DEVICE_LED_SELECT_ATTR(SET_ID) \
  548. static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
  549. struct device_attribute *attr, const char *buf, size_t count) \
  550. { \
  551. return wacom_led_select_store(dev, SET_ID, buf, count); \
  552. } \
  553. static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
  554. struct device_attribute *attr, char *buf) \
  555. { \
  556. struct wacom *wacom = dev_get_drvdata(dev); \
  557. return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
  558. } \
  559. static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
  560. wacom_led##SET_ID##_select_show, \
  561. wacom_led##SET_ID##_select_store)
  562. DEVICE_LED_SELECT_ATTR(0);
  563. DEVICE_LED_SELECT_ATTR(1);
  564. static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
  565. const char *buf, size_t count)
  566. {
  567. unsigned int value;
  568. int err;
  569. err = kstrtouint(buf, 10, &value);
  570. if (err)
  571. return err;
  572. mutex_lock(&wacom->lock);
  573. *dest = value & 0x7f;
  574. err = wacom_led_control(wacom);
  575. mutex_unlock(&wacom->lock);
  576. return err < 0 ? err : count;
  577. }
  578. #define DEVICE_LUMINANCE_ATTR(name, field) \
  579. static ssize_t wacom_##name##_luminance_store(struct device *dev, \
  580. struct device_attribute *attr, const char *buf, size_t count) \
  581. { \
  582. struct wacom *wacom = dev_get_drvdata(dev); \
  583. \
  584. return wacom_luminance_store(wacom, &wacom->led.field, \
  585. buf, count); \
  586. } \
  587. static DEVICE_ATTR(name##_luminance, S_IWUSR, \
  588. NULL, wacom_##name##_luminance_store)
  589. DEVICE_LUMINANCE_ATTR(status0, llv);
  590. DEVICE_LUMINANCE_ATTR(status1, hlv);
  591. DEVICE_LUMINANCE_ATTR(buttons, img_lum);
  592. static ssize_t wacom_button_image_store(struct device *dev, int button_id,
  593. const char *buf, size_t count)
  594. {
  595. struct wacom *wacom = dev_get_drvdata(dev);
  596. int err;
  597. if (count != 1024)
  598. return -EINVAL;
  599. mutex_lock(&wacom->lock);
  600. err = wacom_led_putimage(wacom, button_id, buf);
  601. mutex_unlock(&wacom->lock);
  602. return err < 0 ? err : count;
  603. }
  604. #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
  605. static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
  606. struct device_attribute *attr, const char *buf, size_t count) \
  607. { \
  608. return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
  609. } \
  610. static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
  611. NULL, wacom_btnimg##BUTTON_ID##_store)
  612. DEVICE_BTNIMG_ATTR(0);
  613. DEVICE_BTNIMG_ATTR(1);
  614. DEVICE_BTNIMG_ATTR(2);
  615. DEVICE_BTNIMG_ATTR(3);
  616. DEVICE_BTNIMG_ATTR(4);
  617. DEVICE_BTNIMG_ATTR(5);
  618. DEVICE_BTNIMG_ATTR(6);
  619. DEVICE_BTNIMG_ATTR(7);
  620. static struct attribute *cintiq_led_attrs[] = {
  621. &dev_attr_status_led0_select.attr,
  622. &dev_attr_status_led1_select.attr,
  623. NULL
  624. };
  625. static struct attribute_group cintiq_led_attr_group = {
  626. .name = "wacom_led",
  627. .attrs = cintiq_led_attrs,
  628. };
  629. static struct attribute *intuos4_led_attrs[] = {
  630. &dev_attr_status0_luminance.attr,
  631. &dev_attr_status1_luminance.attr,
  632. &dev_attr_status_led0_select.attr,
  633. &dev_attr_buttons_luminance.attr,
  634. &dev_attr_button0_rawimg.attr,
  635. &dev_attr_button1_rawimg.attr,
  636. &dev_attr_button2_rawimg.attr,
  637. &dev_attr_button3_rawimg.attr,
  638. &dev_attr_button4_rawimg.attr,
  639. &dev_attr_button5_rawimg.attr,
  640. &dev_attr_button6_rawimg.attr,
  641. &dev_attr_button7_rawimg.attr,
  642. NULL
  643. };
  644. static struct attribute_group intuos4_led_attr_group = {
  645. .name = "wacom_led",
  646. .attrs = intuos4_led_attrs,
  647. };
  648. static int wacom_initialize_leds(struct wacom *wacom)
  649. {
  650. int error;
  651. /* Initialize default values */
  652. switch (wacom->wacom_wac.features.type) {
  653. case INTUOS4:
  654. case INTUOS4L:
  655. wacom->led.select[0] = 0;
  656. wacom->led.select[1] = 0;
  657. wacom->led.llv = 10;
  658. wacom->led.hlv = 20;
  659. wacom->led.img_lum = 10;
  660. error = sysfs_create_group(&wacom->intf->dev.kobj,
  661. &intuos4_led_attr_group);
  662. break;
  663. case WACOM_24HD:
  664. case WACOM_21UX2:
  665. wacom->led.select[0] = 0;
  666. wacom->led.select[1] = 0;
  667. wacom->led.llv = 0;
  668. wacom->led.hlv = 0;
  669. wacom->led.img_lum = 0;
  670. error = sysfs_create_group(&wacom->intf->dev.kobj,
  671. &cintiq_led_attr_group);
  672. break;
  673. default:
  674. return 0;
  675. }
  676. if (error) {
  677. dev_err(&wacom->intf->dev,
  678. "cannot create sysfs group err: %d\n", error);
  679. return error;
  680. }
  681. wacom_led_control(wacom);
  682. return 0;
  683. }
  684. static void wacom_destroy_leds(struct wacom *wacom)
  685. {
  686. switch (wacom->wacom_wac.features.type) {
  687. case INTUOS4:
  688. case INTUOS4L:
  689. sysfs_remove_group(&wacom->intf->dev.kobj,
  690. &intuos4_led_attr_group);
  691. break;
  692. case WACOM_24HD:
  693. case WACOM_21UX2:
  694. sysfs_remove_group(&wacom->intf->dev.kobj,
  695. &cintiq_led_attr_group);
  696. break;
  697. }
  698. }
  699. static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
  700. {
  701. struct usb_device *dev = interface_to_usbdev(intf);
  702. struct usb_endpoint_descriptor *endpoint;
  703. struct wacom *wacom;
  704. struct wacom_wac *wacom_wac;
  705. struct wacom_features *features;
  706. struct input_dev *input_dev;
  707. int error;
  708. if (!id->driver_info)
  709. return -EINVAL;
  710. wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
  711. input_dev = input_allocate_device();
  712. if (!wacom || !input_dev) {
  713. error = -ENOMEM;
  714. goto fail1;
  715. }
  716. wacom_wac = &wacom->wacom_wac;
  717. wacom_wac->features = *((struct wacom_features *)id->driver_info);
  718. features = &wacom_wac->features;
  719. if (features->pktlen > WACOM_PKGLEN_MAX) {
  720. error = -EINVAL;
  721. goto fail1;
  722. }
  723. wacom_wac->data = usb_alloc_coherent(dev, WACOM_PKGLEN_MAX,
  724. GFP_KERNEL, &wacom->data_dma);
  725. if (!wacom_wac->data) {
  726. error = -ENOMEM;
  727. goto fail1;
  728. }
  729. wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
  730. if (!wacom->irq) {
  731. error = -ENOMEM;
  732. goto fail2;
  733. }
  734. wacom->usbdev = dev;
  735. wacom->intf = intf;
  736. mutex_init(&wacom->lock);
  737. usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
  738. strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
  739. wacom_wac->input = input_dev;
  740. endpoint = &intf->cur_altsetting->endpoint[0].desc;
  741. /* Retrieve the physical and logical size for OEM devices */
  742. error = wacom_retrieve_hid_descriptor(intf, features);
  743. if (error)
  744. goto fail3;
  745. wacom_setup_device_quirks(features);
  746. strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
  747. if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
  748. /* Append the device type to the name */
  749. strlcat(wacom_wac->name,
  750. features->device_type == BTN_TOOL_PEN ?
  751. " Pen" : " Finger",
  752. sizeof(wacom_wac->name));
  753. error = wacom_add_shared_data(wacom_wac, dev);
  754. if (error)
  755. goto fail3;
  756. }
  757. input_dev->name = wacom_wac->name;
  758. input_dev->dev.parent = &intf->dev;
  759. input_dev->open = wacom_open;
  760. input_dev->close = wacom_close;
  761. usb_to_input_id(dev, &input_dev->id);
  762. input_set_drvdata(input_dev, wacom);
  763. wacom_setup_input_capabilities(input_dev, wacom_wac);
  764. usb_fill_int_urb(wacom->irq, dev,
  765. usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  766. wacom_wac->data, features->pktlen,
  767. wacom_sys_irq, wacom, endpoint->bInterval);
  768. wacom->irq->transfer_dma = wacom->data_dma;
  769. wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  770. error = wacom_initialize_leds(wacom);
  771. if (error)
  772. goto fail4;
  773. error = input_register_device(input_dev);
  774. if (error)
  775. goto fail5;
  776. /* Note that if query fails it is not a hard failure */
  777. wacom_query_tablet_data(intf, features);
  778. usb_set_intfdata(intf, wacom);
  779. return 0;
  780. fail5: wacom_destroy_leds(wacom);
  781. fail4: wacom_remove_shared_data(wacom_wac);
  782. fail3: usb_free_urb(wacom->irq);
  783. fail2: usb_free_coherent(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
  784. fail1: input_free_device(input_dev);
  785. kfree(wacom);
  786. return error;
  787. }
  788. static void wacom_disconnect(struct usb_interface *intf)
  789. {
  790. struct wacom *wacom = usb_get_intfdata(intf);
  791. usb_set_intfdata(intf, NULL);
  792. usb_kill_urb(wacom->irq);
  793. input_unregister_device(wacom->wacom_wac.input);
  794. wacom_destroy_leds(wacom);
  795. usb_free_urb(wacom->irq);
  796. usb_free_coherent(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
  797. wacom->wacom_wac.data, wacom->data_dma);
  798. wacom_remove_shared_data(&wacom->wacom_wac);
  799. kfree(wacom);
  800. }
  801. static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
  802. {
  803. struct wacom *wacom = usb_get_intfdata(intf);
  804. mutex_lock(&wacom->lock);
  805. usb_kill_urb(wacom->irq);
  806. mutex_unlock(&wacom->lock);
  807. return 0;
  808. }
  809. static int wacom_resume(struct usb_interface *intf)
  810. {
  811. struct wacom *wacom = usb_get_intfdata(intf);
  812. struct wacom_features *features = &wacom->wacom_wac.features;
  813. int rv = 0;
  814. mutex_lock(&wacom->lock);
  815. /* switch to wacom mode first */
  816. wacom_query_tablet_data(intf, features);
  817. wacom_led_control(wacom);
  818. if (wacom->open && usb_submit_urb(wacom->irq, GFP_NOIO) < 0)
  819. rv = -EIO;
  820. mutex_unlock(&wacom->lock);
  821. return rv;
  822. }
  823. static int wacom_reset_resume(struct usb_interface *intf)
  824. {
  825. return wacom_resume(intf);
  826. }
  827. static struct usb_driver wacom_driver = {
  828. .name = "wacom",
  829. .id_table = wacom_ids,
  830. .probe = wacom_probe,
  831. .disconnect = wacom_disconnect,
  832. .suspend = wacom_suspend,
  833. .resume = wacom_resume,
  834. .reset_resume = wacom_reset_resume,
  835. .supports_autosuspend = 1,
  836. };
  837. module_usb_driver(wacom_driver);