wacom_sys.c 24 KB

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