wacom_sys.c 29 KB

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