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