wacom_sys.c 23 KB

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