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