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