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