wacom_sys.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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.h"
  13. #include "wacom_wac.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. static int usb_get_report(struct usb_interface *intf, unsigned char type,
  47. unsigned char id, void *buf, int size)
  48. {
  49. return usb_control_msg(interface_to_usbdev(intf),
  50. usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
  51. USB_REQ_GET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  52. (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
  53. buf, size, 100);
  54. }
  55. static int usb_set_report(struct usb_interface *intf, unsigned char type,
  56. unsigned char id, void *buf, int size)
  57. {
  58. return usb_control_msg(interface_to_usbdev(intf),
  59. usb_sndctrlpipe(interface_to_usbdev(intf), 0),
  60. USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  61. (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
  62. buf, size, 1000);
  63. }
  64. static struct input_dev * get_input_dev(struct wacom_combo *wcombo)
  65. {
  66. return wcombo->wacom->dev;
  67. }
  68. static void wacom_sys_irq(struct urb *urb)
  69. {
  70. struct wacom *wacom = urb->context;
  71. struct wacom_combo wcombo;
  72. int retval;
  73. switch (urb->status) {
  74. case 0:
  75. /* success */
  76. break;
  77. case -ECONNRESET:
  78. case -ENOENT:
  79. case -ESHUTDOWN:
  80. /* this urb is terminated, clean up */
  81. dbg("%s - urb shutting down with status: %d", __func__, urb->status);
  82. return;
  83. default:
  84. dbg("%s - nonzero urb status received: %d", __func__, urb->status);
  85. goto exit;
  86. }
  87. wcombo.wacom = wacom;
  88. wcombo.urb = urb;
  89. if (wacom_wac_irq(wacom->wacom_wac, (void *)&wcombo))
  90. input_sync(get_input_dev(&wcombo));
  91. exit:
  92. usb_mark_last_busy(wacom->usbdev);
  93. retval = usb_submit_urb (urb, GFP_ATOMIC);
  94. if (retval)
  95. err ("%s - usb_submit_urb failed with result %d",
  96. __func__, retval);
  97. }
  98. void wacom_report_key(void *wcombo, unsigned int key_type, int key_data)
  99. {
  100. input_report_key(get_input_dev((struct wacom_combo *)wcombo), key_type, key_data);
  101. }
  102. void wacom_report_abs(void *wcombo, unsigned int abs_type, int abs_data)
  103. {
  104. input_report_abs(get_input_dev((struct wacom_combo *)wcombo), abs_type, abs_data);
  105. }
  106. void wacom_report_rel(void *wcombo, unsigned int rel_type, int rel_data)
  107. {
  108. input_report_rel(get_input_dev((struct wacom_combo *)wcombo), rel_type, rel_data);
  109. }
  110. void wacom_input_event(void *wcombo, unsigned int type, unsigned int code, int value)
  111. {
  112. input_event(get_input_dev((struct wacom_combo *)wcombo), type, code, value);
  113. }
  114. __u16 wacom_be16_to_cpu(unsigned char *data)
  115. {
  116. __u16 value;
  117. value = be16_to_cpu(*(__be16 *) data);
  118. return value;
  119. }
  120. __u16 wacom_le16_to_cpu(unsigned char *data)
  121. {
  122. __u16 value;
  123. value = le16_to_cpu(*(__le16 *) data);
  124. return value;
  125. }
  126. void wacom_input_sync(void *wcombo)
  127. {
  128. input_sync(get_input_dev((struct wacom_combo *)wcombo));
  129. }
  130. static int wacom_open(struct input_dev *dev)
  131. {
  132. struct wacom *wacom = input_get_drvdata(dev);
  133. mutex_lock(&wacom->lock);
  134. wacom->irq->dev = wacom->usbdev;
  135. if (usb_autopm_get_interface(wacom->intf) < 0) {
  136. mutex_unlock(&wacom->lock);
  137. return -EIO;
  138. }
  139. if (usb_submit_urb(wacom->irq, GFP_KERNEL)) {
  140. usb_autopm_put_interface(wacom->intf);
  141. mutex_unlock(&wacom->lock);
  142. return -EIO;
  143. }
  144. wacom->open = 1;
  145. wacom->intf->needs_remote_wakeup = 1;
  146. mutex_unlock(&wacom->lock);
  147. return 0;
  148. }
  149. static void wacom_close(struct input_dev *dev)
  150. {
  151. struct wacom *wacom = input_get_drvdata(dev);
  152. mutex_lock(&wacom->lock);
  153. usb_kill_urb(wacom->irq);
  154. wacom->open = 0;
  155. wacom->intf->needs_remote_wakeup = 0;
  156. mutex_unlock(&wacom->lock);
  157. }
  158. void input_dev_mo(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
  159. {
  160. input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_1) |
  161. BIT_MASK(BTN_5);
  162. input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
  163. }
  164. void input_dev_g4(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
  165. {
  166. input_dev->evbit[0] |= BIT_MASK(EV_MSC);
  167. input_dev->mscbit[0] |= BIT_MASK(MSC_SERIAL);
  168. input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_FINGER);
  169. input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_0) |
  170. BIT_MASK(BTN_4);
  171. }
  172. void input_dev_g(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
  173. {
  174. input_dev->evbit[0] |= BIT_MASK(EV_REL);
  175. input_dev->relbit[0] |= BIT_MASK(REL_WHEEL);
  176. input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_LEFT) |
  177. BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
  178. input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_RUBBER) |
  179. BIT_MASK(BTN_TOOL_PEN) | BIT_MASK(BTN_STYLUS) |
  180. BIT_MASK(BTN_TOOL_MOUSE) | BIT_MASK(BTN_STYLUS2);
  181. input_set_abs_params(input_dev, ABS_DISTANCE,
  182. 0, wacom_wac->features.distance_max, 0, 0);
  183. }
  184. void input_dev_i3s(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
  185. {
  186. input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_FINGER);
  187. input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_0) |
  188. BIT_MASK(BTN_1) | BIT_MASK(BTN_2) | BIT_MASK(BTN_3);
  189. input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
  190. input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
  191. }
  192. void input_dev_i3(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
  193. {
  194. input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_4) |
  195. BIT_MASK(BTN_5) | BIT_MASK(BTN_6) | BIT_MASK(BTN_7);
  196. input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
  197. }
  198. void input_dev_i4s(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
  199. {
  200. input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_FINGER);
  201. input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_0) | BIT_MASK(BTN_1) | BIT_MASK(BTN_2) | BIT_MASK(BTN_3);
  202. input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_4) | BIT_MASK(BTN_5) | BIT_MASK(BTN_6);
  203. input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
  204. }
  205. void input_dev_i4(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
  206. {
  207. input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_7) | BIT_MASK(BTN_8);
  208. }
  209. void input_dev_bee(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
  210. {
  211. input_dev->keybit[BIT_WORD(BTN_MISC)] |= BIT_MASK(BTN_8) | BIT_MASK(BTN_9);
  212. }
  213. void input_dev_i(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
  214. {
  215. input_dev->evbit[0] |= BIT_MASK(EV_MSC) | BIT_MASK(EV_REL);
  216. input_dev->mscbit[0] |= BIT_MASK(MSC_SERIAL);
  217. input_dev->relbit[0] |= BIT_MASK(REL_WHEEL);
  218. input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_LEFT) |
  219. BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE) |
  220. BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
  221. input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_RUBBER) |
  222. BIT_MASK(BTN_TOOL_PEN) | BIT_MASK(BTN_STYLUS) |
  223. BIT_MASK(BTN_TOOL_MOUSE) | BIT_MASK(BTN_TOOL_BRUSH) |
  224. BIT_MASK(BTN_TOOL_PENCIL) | BIT_MASK(BTN_TOOL_AIRBRUSH) |
  225. BIT_MASK(BTN_TOOL_LENS) | BIT_MASK(BTN_STYLUS2);
  226. input_set_abs_params(input_dev, ABS_DISTANCE,
  227. 0, wacom_wac->features.distance_max, 0, 0);
  228. input_set_abs_params(input_dev, ABS_WHEEL, 0, 1023, 0, 0);
  229. input_set_abs_params(input_dev, ABS_TILT_X, 0, 127, 0, 0);
  230. input_set_abs_params(input_dev, ABS_TILT_Y, 0, 127, 0, 0);
  231. input_set_abs_params(input_dev, ABS_RZ, -900, 899, 0, 0);
  232. input_set_abs_params(input_dev, ABS_THROTTLE, -1023, 1023, 0, 0);
  233. }
  234. void input_dev_pl(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
  235. {
  236. input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_PEN) |
  237. BIT_MASK(BTN_STYLUS) | BIT_MASK(BTN_STYLUS2);
  238. }
  239. void input_dev_pt(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
  240. {
  241. input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_RUBBER);
  242. }
  243. void input_dev_tpc(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
  244. {
  245. struct wacom_features *features = &wacom_wac->features;
  246. if (features->device_type == BTN_TOOL_DOUBLETAP ||
  247. features->device_type == BTN_TOOL_TRIPLETAP) {
  248. input_set_abs_params(input_dev, ABS_RX, 0, features->x_phy, 0, 0);
  249. input_set_abs_params(input_dev, ABS_RY, 0, features->y_phy, 0, 0);
  250. __set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
  251. }
  252. }
  253. void input_dev_tpc2fg(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
  254. {
  255. if (wacom_wac->features.device_type == BTN_TOOL_TRIPLETAP) {
  256. input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_TRIPLETAP);
  257. input_dev->evbit[0] |= BIT_MASK(EV_MSC);
  258. input_dev->mscbit[0] |= BIT_MASK(MSC_SERIAL);
  259. }
  260. }
  261. static int wacom_parse_hid(struct usb_interface *intf, struct hid_descriptor *hid_desc,
  262. struct wacom_features *features)
  263. {
  264. struct usb_device *dev = interface_to_usbdev(intf);
  265. char limit = 0;
  266. /* result has to be defined as int for some devices */
  267. int result = 0;
  268. int i = 0, usage = WCM_UNDEFINED, finger = 0, pen = 0;
  269. unsigned char *report;
  270. report = kzalloc(hid_desc->wDescriptorLength, GFP_KERNEL);
  271. if (!report)
  272. return -ENOMEM;
  273. /* retrive report descriptors */
  274. do {
  275. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  276. USB_REQ_GET_DESCRIPTOR,
  277. USB_RECIP_INTERFACE | USB_DIR_IN,
  278. HID_DEVICET_REPORT << 8,
  279. intf->altsetting[0].desc.bInterfaceNumber, /* interface */
  280. report,
  281. hid_desc->wDescriptorLength,
  282. 5000); /* 5 secs */
  283. } while (result < 0 && limit++ < 5);
  284. /* No need to parse the Descriptor. It isn't an error though */
  285. if (result < 0)
  286. goto out;
  287. for (i = 0; i < hid_desc->wDescriptorLength; i++) {
  288. switch (report[i]) {
  289. case HID_USAGE_PAGE:
  290. switch (report[i + 1]) {
  291. case HID_USAGE_PAGE_DIGITIZER:
  292. usage = WCM_DIGITIZER;
  293. i++;
  294. break;
  295. case HID_USAGE_PAGE_DESKTOP:
  296. usage = WCM_DESKTOP;
  297. i++;
  298. break;
  299. }
  300. break;
  301. case HID_USAGE:
  302. switch (report[i + 1]) {
  303. case HID_USAGE_X:
  304. if (usage == WCM_DESKTOP) {
  305. if (finger) {
  306. features->device_type = BTN_TOOL_DOUBLETAP;
  307. if (features->type == TABLETPC2FG) {
  308. /* need to reset back */
  309. features->pktlen = WACOM_PKGLEN_TPC2FG;
  310. features->device_type = BTN_TOOL_TRIPLETAP;
  311. }
  312. features->x_max =
  313. wacom_le16_to_cpu(&report[i + 3]);
  314. features->x_phy =
  315. wacom_le16_to_cpu(&report[i + 6]);
  316. features->unit = report[i + 9];
  317. features->unitExpo = report[i + 11];
  318. i += 12;
  319. } else if (pen) {
  320. /* penabled only accepts exact bytes of data */
  321. if (features->type == TABLETPC2FG)
  322. features->pktlen = WACOM_PKGLEN_GRAPHIRE;
  323. features->device_type = BTN_TOOL_PEN;
  324. features->x_max =
  325. wacom_le16_to_cpu(&report[i + 3]);
  326. i += 4;
  327. }
  328. } else if (usage == WCM_DIGITIZER) {
  329. /* max pressure isn't reported
  330. features->pressure_max = (unsigned short)
  331. (report[i+4] << 8 | report[i + 3]);
  332. */
  333. features->pressure_max = 255;
  334. i += 4;
  335. }
  336. break;
  337. case HID_USAGE_Y:
  338. if (usage == WCM_DESKTOP) {
  339. if (finger) {
  340. features->device_type = BTN_TOOL_DOUBLETAP;
  341. if (features->type == TABLETPC2FG) {
  342. /* need to reset back */
  343. features->pktlen = WACOM_PKGLEN_TPC2FG;
  344. features->device_type = BTN_TOOL_TRIPLETAP;
  345. features->y_max =
  346. wacom_le16_to_cpu(&report[i + 3]);
  347. features->y_phy =
  348. wacom_le16_to_cpu(&report[i + 6]);
  349. i += 7;
  350. } else {
  351. features->y_max =
  352. features->x_max;
  353. features->y_phy =
  354. wacom_le16_to_cpu(&report[i + 3]);
  355. i += 4;
  356. }
  357. } else if (pen) {
  358. /* penabled only accepts exact bytes of data */
  359. if (features->type == TABLETPC2FG)
  360. features->pktlen = WACOM_PKGLEN_GRAPHIRE;
  361. features->device_type = BTN_TOOL_PEN;
  362. features->y_max =
  363. wacom_le16_to_cpu(&report[i + 3]);
  364. i += 4;
  365. }
  366. }
  367. break;
  368. case HID_USAGE_FINGER:
  369. finger = 1;
  370. i++;
  371. break;
  372. case HID_USAGE_STYLUS:
  373. pen = 1;
  374. i++;
  375. break;
  376. case HID_USAGE_UNDEFINED:
  377. if (usage == WCM_DESKTOP && finger) /* capacity */
  378. features->pressure_max =
  379. wacom_le16_to_cpu(&report[i + 3]);
  380. i += 4;
  381. break;
  382. }
  383. break;
  384. case HID_COLLECTION:
  385. /* reset UsagePage and Finger */
  386. finger = usage = 0;
  387. break;
  388. }
  389. }
  390. out:
  391. result = 0;
  392. kfree(report);
  393. return result;
  394. }
  395. static int wacom_query_tablet_data(struct usb_interface *intf, struct wacom_features *features)
  396. {
  397. unsigned char *rep_data;
  398. int limit = 0, report_id = 2;
  399. int error = -ENOMEM;
  400. rep_data = kmalloc(2, GFP_KERNEL);
  401. if (!rep_data)
  402. return error;
  403. /* ask to report tablet data if it is 2FGT or not a Tablet PC */
  404. if (features->device_type == BTN_TOOL_TRIPLETAP) {
  405. do {
  406. rep_data[0] = 3;
  407. rep_data[1] = 4;
  408. report_id = 3;
  409. error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
  410. report_id, rep_data, 2);
  411. if (error >= 0)
  412. error = usb_get_report(intf,
  413. WAC_HID_FEATURE_REPORT, report_id,
  414. rep_data, 3);
  415. } while ((error < 0 || rep_data[1] != 4) && limit++ < 5);
  416. } else if (features->type != TABLETPC && features->type != TABLETPC2FG) {
  417. do {
  418. rep_data[0] = 2;
  419. rep_data[1] = 2;
  420. error = usb_set_report(intf, WAC_HID_FEATURE_REPORT,
  421. report_id, rep_data, 2);
  422. if (error >= 0)
  423. error = usb_get_report(intf,
  424. WAC_HID_FEATURE_REPORT, report_id,
  425. rep_data, 2);
  426. } while ((error < 0 || rep_data[1] != 2) && limit++ < 5);
  427. }
  428. kfree(rep_data);
  429. return error < 0 ? error : 0;
  430. }
  431. static int wacom_retrieve_hid_descriptor(struct usb_interface *intf,
  432. struct wacom_features *features)
  433. {
  434. int error = 0;
  435. struct usb_host_interface *interface = intf->cur_altsetting;
  436. struct hid_descriptor *hid_desc;
  437. /* default device to penabled */
  438. features->device_type = BTN_TOOL_PEN;
  439. /* only Tablet PCs need to retrieve the info */
  440. if ((features->type != TABLETPC) && (features->type != TABLETPC2FG))
  441. goto out;
  442. if (usb_get_extra_descriptor(interface, HID_DEVICET_HID, &hid_desc)) {
  443. if (usb_get_extra_descriptor(&interface->endpoint[0],
  444. HID_DEVICET_REPORT, &hid_desc)) {
  445. printk("wacom: can not retrieve extra class descriptor\n");
  446. error = 1;
  447. goto out;
  448. }
  449. }
  450. error = wacom_parse_hid(intf, hid_desc, features);
  451. if (error)
  452. goto out;
  453. /* touch device found but size is not defined. use default */
  454. if (features->device_type == BTN_TOOL_DOUBLETAP && !features->x_max) {
  455. features->x_max = 1023;
  456. features->y_max = 1023;
  457. }
  458. out:
  459. return error;
  460. }
  461. static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
  462. {
  463. struct usb_device *dev = interface_to_usbdev(intf);
  464. struct usb_endpoint_descriptor *endpoint;
  465. struct wacom *wacom;
  466. struct wacom_wac *wacom_wac;
  467. struct wacom_features *features;
  468. struct input_dev *input_dev;
  469. int error;
  470. if (!id->driver_info)
  471. return -EINVAL;
  472. wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
  473. wacom_wac = kzalloc(sizeof(struct wacom_wac), GFP_KERNEL);
  474. input_dev = input_allocate_device();
  475. if (!wacom || !input_dev || !wacom_wac) {
  476. error = -ENOMEM;
  477. goto fail1;
  478. }
  479. wacom_wac->features = *((struct wacom_features *)id->driver_info);
  480. features = &wacom_wac->features;
  481. if (features->pktlen > WACOM_PKGLEN_MAX) {
  482. error = -EINVAL;
  483. goto fail1;
  484. }
  485. wacom_wac->data = usb_buffer_alloc(dev, WACOM_PKGLEN_MAX,
  486. GFP_KERNEL, &wacom->data_dma);
  487. if (!wacom_wac->data) {
  488. error = -ENOMEM;
  489. goto fail1;
  490. }
  491. wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
  492. if (!wacom->irq) {
  493. error = -ENOMEM;
  494. goto fail2;
  495. }
  496. wacom->usbdev = dev;
  497. wacom->dev = input_dev;
  498. wacom->intf = intf;
  499. mutex_init(&wacom->lock);
  500. usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
  501. strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
  502. usb_to_input_id(dev, &input_dev->id);
  503. input_dev->dev.parent = &intf->dev;
  504. input_set_drvdata(input_dev, wacom);
  505. input_dev->open = wacom_open;
  506. input_dev->close = wacom_close;
  507. endpoint = &intf->cur_altsetting->endpoint[0].desc;
  508. /* Retrieve the physical and logical size for OEM devices */
  509. error = wacom_retrieve_hid_descriptor(intf, features);
  510. if (error)
  511. goto fail2;
  512. strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
  513. if (features->type == TABLETPC || features->type == TABLETPC2FG) {
  514. /* Append the device type to the name */
  515. strlcat(wacom_wac->name,
  516. features->device_type == BTN_TOOL_PEN ?
  517. " Pen" : " Finger",
  518. sizeof(wacom_wac->name));
  519. }
  520. input_dev->name = wacom_wac->name;
  521. wacom->wacom_wac = wacom_wac;
  522. input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  523. input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOUCH);
  524. input_set_abs_params(input_dev, ABS_X, 0, features->x_max, 4, 0);
  525. input_set_abs_params(input_dev, ABS_Y, 0, features->y_max, 4, 0);
  526. input_set_abs_params(input_dev, ABS_PRESSURE, 0, features->pressure_max, 0, 0);
  527. input_dev->absbit[BIT_WORD(ABS_MISC)] |= BIT_MASK(ABS_MISC);
  528. wacom_init_input_dev(input_dev, wacom_wac);
  529. usb_fill_int_urb(wacom->irq, dev,
  530. usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  531. wacom_wac->data, features->pktlen,
  532. wacom_sys_irq, wacom, endpoint->bInterval);
  533. wacom->irq->transfer_dma = wacom->data_dma;
  534. wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  535. error = input_register_device(wacom->dev);
  536. if (error)
  537. goto fail3;
  538. /* Note that if query fails it is not a hard failure */
  539. wacom_query_tablet_data(intf, features);
  540. usb_set_intfdata(intf, wacom);
  541. return 0;
  542. fail3: usb_free_urb(wacom->irq);
  543. fail2: usb_buffer_free(dev, WACOM_PKGLEN_MAX, wacom_wac->data, wacom->data_dma);
  544. fail1: input_free_device(input_dev);
  545. kfree(wacom);
  546. kfree(wacom_wac);
  547. return error;
  548. }
  549. static void wacom_disconnect(struct usb_interface *intf)
  550. {
  551. struct wacom *wacom = usb_get_intfdata(intf);
  552. usb_set_intfdata(intf, NULL);
  553. usb_kill_urb(wacom->irq);
  554. input_unregister_device(wacom->dev);
  555. usb_free_urb(wacom->irq);
  556. usb_buffer_free(interface_to_usbdev(intf), WACOM_PKGLEN_MAX,
  557. wacom->wacom_wac->data, wacom->data_dma);
  558. kfree(wacom->wacom_wac);
  559. kfree(wacom);
  560. }
  561. static int wacom_suspend(struct usb_interface *intf, pm_message_t message)
  562. {
  563. struct wacom *wacom = usb_get_intfdata(intf);
  564. mutex_lock(&wacom->lock);
  565. usb_kill_urb(wacom->irq);
  566. mutex_unlock(&wacom->lock);
  567. return 0;
  568. }
  569. static int wacom_resume(struct usb_interface *intf)
  570. {
  571. struct wacom *wacom = usb_get_intfdata(intf);
  572. struct wacom_features *features = &wacom->wacom_wac->features;
  573. int rv;
  574. mutex_lock(&wacom->lock);
  575. /* switch to wacom mode first */
  576. wacom_query_tablet_data(intf, features);
  577. if (wacom->open)
  578. rv = usb_submit_urb(wacom->irq, GFP_NOIO);
  579. else
  580. rv = 0;
  581. mutex_unlock(&wacom->lock);
  582. return rv;
  583. }
  584. static int wacom_reset_resume(struct usb_interface *intf)
  585. {
  586. return wacom_resume(intf);
  587. }
  588. static struct usb_driver wacom_driver = {
  589. .name = "wacom",
  590. .id_table = wacom_ids,
  591. .probe = wacom_probe,
  592. .disconnect = wacom_disconnect,
  593. .suspend = wacom_suspend,
  594. .resume = wacom_resume,
  595. .reset_resume = wacom_reset_resume,
  596. .supports_autosuspend = 1,
  597. };
  598. static int __init wacom_init(void)
  599. {
  600. int result;
  601. result = usb_register(&wacom_driver);
  602. if (result == 0)
  603. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  604. DRIVER_DESC "\n");
  605. return result;
  606. }
  607. static void __exit wacom_exit(void)
  608. {
  609. usb_deregister(&wacom_driver);
  610. }
  611. module_init(wacom_init);
  612. module_exit(wacom_exit);