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