wacom_sys.c 23 KB

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