wacom_sys.c 22 KB

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