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