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