]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/input/tablet/wacom_sys.c
Input: wacom - use hid_info instead of plain dev_info
[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
4451e088 491struct wacom_hdev_data {
4492efff
PC
492 struct list_head list;
493 struct kref kref;
4451e088 494 struct hid_device *dev;
4492efff
PC
495 struct wacom_shared shared;
496};
497
498static LIST_HEAD(wacom_udev_list);
499static DEFINE_MUTEX(wacom_udev_list_lock);
500
4451e088
BT
501static bool wacom_are_sibling(struct hid_device *hdev,
502 struct hid_device *sibling)
aea2bf6a 503{
4451e088
BT
504 struct wacom *wacom = hid_get_drvdata(hdev);
505 struct wacom_features *features = &wacom->wacom_wac.features;
506 int vid = features->oVid;
507 int pid = features->oPid;
508 int n1,n2;
509
510 if (vid == 0 && pid == 0) {
511 vid = hdev->vendor;
512 pid = hdev->product;
513 }
aea2bf6a 514
4451e088
BT
515 if (vid != sibling->vendor || pid != sibling->product)
516 return false;
aea2bf6a 517
4451e088
BT
518 /* Compare the physical path. */
519 n1 = strrchr(hdev->phys, '.') - hdev->phys;
520 n2 = strrchr(sibling->phys, '.') - sibling->phys;
521 if (n1 != n2 || n1 <= 0 || n2 <= 0)
522 return false;
aea2bf6a 523
4451e088 524 return !strncmp(hdev->phys, sibling->phys, n1);
aea2bf6a
JG
525}
526
4451e088 527static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
4492efff 528{
4451e088 529 struct wacom_hdev_data *data;
4492efff
PC
530
531 list_for_each_entry(data, &wacom_udev_list, list) {
4451e088 532 if (wacom_are_sibling(hdev, data->dev)) {
4492efff
PC
533 kref_get(&data->kref);
534 return data;
535 }
536 }
537
538 return NULL;
539}
540
4451e088 541static int wacom_add_shared_data(struct hid_device *hdev)
4492efff 542{
4451e088
BT
543 struct wacom *wacom = hid_get_drvdata(hdev);
544 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
545 struct wacom_hdev_data *data;
4492efff
PC
546 int retval = 0;
547
548 mutex_lock(&wacom_udev_list_lock);
549
4451e088 550 data = wacom_get_hdev_data(hdev);
4492efff 551 if (!data) {
4451e088 552 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
4492efff
PC
553 if (!data) {
554 retval = -ENOMEM;
555 goto out;
556 }
557
558 kref_init(&data->kref);
4451e088 559 data->dev = hdev;
4492efff
PC
560 list_add_tail(&data->list, &wacom_udev_list);
561 }
562
4451e088 563 wacom_wac->shared = &data->shared;
4492efff
PC
564
565out:
566 mutex_unlock(&wacom_udev_list_lock);
567 return retval;
568}
569
570static void wacom_release_shared_data(struct kref *kref)
571{
4451e088
BT
572 struct wacom_hdev_data *data =
573 container_of(kref, struct wacom_hdev_data, kref);
4492efff
PC
574
575 mutex_lock(&wacom_udev_list_lock);
576 list_del(&data->list);
577 mutex_unlock(&wacom_udev_list_lock);
578
579 kfree(data);
580}
581
582static void wacom_remove_shared_data(struct wacom_wac *wacom)
583{
4451e088 584 struct wacom_hdev_data *data;
4492efff
PC
585
586 if (wacom->shared) {
4451e088 587 data = container_of(wacom->shared, struct wacom_hdev_data, shared);
4492efff
PC
588 kref_put(&data->kref, wacom_release_shared_data);
589 wacom->shared = NULL;
590 }
591}
592
5d7e7d47
EH
593static int wacom_led_control(struct wacom *wacom)
594{
595 unsigned char *buf;
9b5b95dd 596 int retval;
5d7e7d47
EH
597
598 buf = kzalloc(9, GFP_KERNEL);
599 if (!buf)
600 return -ENOMEM;
601
9b5b95dd 602 if (wacom->wacom_wac.features.type >= INTUOS5S &&
9a35c411 603 wacom->wacom_wac.features.type <= INTUOSPL) {
9b5b95dd
JG
604 /*
605 * Touch Ring and crop mark LED luminance may take on
606 * one of four values:
607 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
608 */
609 int ring_led = wacom->led.select[0] & 0x03;
610 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
611 int crop_lum = 0;
612
613 buf[0] = WAC_CMD_LED_CONTROL;
614 buf[1] = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
615 }
616 else {
617 int led = wacom->led.select[0] | 0x4;
618
619 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
620 wacom->wacom_wac.features.type == WACOM_24HD)
621 led |= (wacom->led.select[1] << 4) | 0x40;
622
623 buf[0] = WAC_CMD_LED_CONTROL;
624 buf[1] = led;
625 buf[2] = wacom->led.llv;
626 buf[3] = wacom->led.hlv;
627 buf[4] = wacom->led.img_lum;
628 }
5d7e7d47 629
27b20a9d
BT
630 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
631 WAC_CMD_LED_CONTROL, buf, 9, WAC_CMD_RETRIES);
5d7e7d47
EH
632 kfree(buf);
633
634 return retval;
635}
636
637static int wacom_led_putimage(struct wacom *wacom, int button_id, const void *img)
638{
639 unsigned char *buf;
640 int i, retval;
641
642 buf = kzalloc(259, GFP_KERNEL);
643 if (!buf)
644 return -ENOMEM;
645
646 /* Send 'start' command */
647 buf[0] = WAC_CMD_ICON_START;
648 buf[1] = 1;
27b20a9d
BT
649 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
650 WAC_CMD_ICON_START, buf, 2, WAC_CMD_RETRIES);
5d7e7d47
EH
651 if (retval < 0)
652 goto out;
653
654 buf[0] = WAC_CMD_ICON_XFER;
655 buf[1] = button_id & 0x07;
656 for (i = 0; i < 4; i++) {
657 buf[2] = i;
658 memcpy(buf + 3, img + i * 256, 256);
659
27b20a9d
BT
660 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
661 WAC_CMD_ICON_XFER,
5d7e7d47
EH
662 buf, 259, WAC_CMD_RETRIES);
663 if (retval < 0)
664 break;
665 }
666
667 /* Send 'stop' */
668 buf[0] = WAC_CMD_ICON_START;
669 buf[1] = 0;
27b20a9d 670 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, WAC_CMD_ICON_START,
5d7e7d47
EH
671 buf, 2, WAC_CMD_RETRIES);
672
673out:
674 kfree(buf);
675 return retval;
676}
677
09e7d941 678static ssize_t wacom_led_select_store(struct device *dev, int set_id,
5d7e7d47
EH
679 const char *buf, size_t count)
680{
c31a408f 681 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
29b47391 682 struct wacom *wacom = hid_get_drvdata(hdev);
5d7e7d47
EH
683 unsigned int id;
684 int err;
685
686 err = kstrtouint(buf, 10, &id);
687 if (err)
688 return err;
689
690 mutex_lock(&wacom->lock);
691
09e7d941 692 wacom->led.select[set_id] = id & 0x3;
5d7e7d47
EH
693 err = wacom_led_control(wacom);
694
695 mutex_unlock(&wacom->lock);
696
697 return err < 0 ? err : count;
698}
699
09e7d941
PC
700#define DEVICE_LED_SELECT_ATTR(SET_ID) \
701static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
702 struct device_attribute *attr, const char *buf, size_t count) \
703{ \
704 return wacom_led_select_store(dev, SET_ID, buf, count); \
705} \
04c59abd
PC
706static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
707 struct device_attribute *attr, char *buf) \
708{ \
c31a408f 709 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
29b47391 710 struct wacom *wacom = hid_get_drvdata(hdev); \
04c59abd
PC
711 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
712} \
713static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
714 wacom_led##SET_ID##_select_show, \
09e7d941
PC
715 wacom_led##SET_ID##_select_store)
716
717DEVICE_LED_SELECT_ATTR(0);
718DEVICE_LED_SELECT_ATTR(1);
5d7e7d47
EH
719
720static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
721 const char *buf, size_t count)
722{
723 unsigned int value;
724 int err;
725
726 err = kstrtouint(buf, 10, &value);
727 if (err)
728 return err;
729
730 mutex_lock(&wacom->lock);
731
732 *dest = value & 0x7f;
733 err = wacom_led_control(wacom);
734
735 mutex_unlock(&wacom->lock);
736
737 return err < 0 ? err : count;
738}
739
740#define DEVICE_LUMINANCE_ATTR(name, field) \
741static ssize_t wacom_##name##_luminance_store(struct device *dev, \
742 struct device_attribute *attr, const char *buf, size_t count) \
743{ \
c31a408f 744 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
29b47391 745 struct wacom *wacom = hid_get_drvdata(hdev); \
5d7e7d47
EH
746 \
747 return wacom_luminance_store(wacom, &wacom->led.field, \
748 buf, count); \
749} \
750static DEVICE_ATTR(name##_luminance, S_IWUSR, \
751 NULL, wacom_##name##_luminance_store)
752
753DEVICE_LUMINANCE_ATTR(status0, llv);
754DEVICE_LUMINANCE_ATTR(status1, hlv);
755DEVICE_LUMINANCE_ATTR(buttons, img_lum);
756
757static ssize_t wacom_button_image_store(struct device *dev, int button_id,
758 const char *buf, size_t count)
759{
c31a408f 760 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
29b47391 761 struct wacom *wacom = hid_get_drvdata(hdev);
5d7e7d47
EH
762 int err;
763
764 if (count != 1024)
765 return -EINVAL;
766
767 mutex_lock(&wacom->lock);
768
769 err = wacom_led_putimage(wacom, button_id, buf);
770
771 mutex_unlock(&wacom->lock);
772
773 return err < 0 ? err : count;
774}
775
776#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
777static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
778 struct device_attribute *attr, const char *buf, size_t count) \
779{ \
780 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
781} \
782static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
783 NULL, wacom_btnimg##BUTTON_ID##_store)
784
785DEVICE_BTNIMG_ATTR(0);
786DEVICE_BTNIMG_ATTR(1);
787DEVICE_BTNIMG_ATTR(2);
788DEVICE_BTNIMG_ATTR(3);
789DEVICE_BTNIMG_ATTR(4);
790DEVICE_BTNIMG_ATTR(5);
791DEVICE_BTNIMG_ATTR(6);
792DEVICE_BTNIMG_ATTR(7);
793
09e7d941
PC
794static struct attribute *cintiq_led_attrs[] = {
795 &dev_attr_status_led0_select.attr,
796 &dev_attr_status_led1_select.attr,
797 NULL
798};
799
800static struct attribute_group cintiq_led_attr_group = {
801 .name = "wacom_led",
802 .attrs = cintiq_led_attrs,
803};
804
805static struct attribute *intuos4_led_attrs[] = {
5d7e7d47
EH
806 &dev_attr_status0_luminance.attr,
807 &dev_attr_status1_luminance.attr,
09e7d941 808 &dev_attr_status_led0_select.attr,
5d7e7d47
EH
809 &dev_attr_buttons_luminance.attr,
810 &dev_attr_button0_rawimg.attr,
811 &dev_attr_button1_rawimg.attr,
812 &dev_attr_button2_rawimg.attr,
813 &dev_attr_button3_rawimg.attr,
814 &dev_attr_button4_rawimg.attr,
815 &dev_attr_button5_rawimg.attr,
816 &dev_attr_button6_rawimg.attr,
817 &dev_attr_button7_rawimg.attr,
818 NULL
819};
820
09e7d941 821static struct attribute_group intuos4_led_attr_group = {
5d7e7d47 822 .name = "wacom_led",
09e7d941 823 .attrs = intuos4_led_attrs,
5d7e7d47
EH
824};
825
9b5b95dd
JG
826static struct attribute *intuos5_led_attrs[] = {
827 &dev_attr_status0_luminance.attr,
828 &dev_attr_status_led0_select.attr,
829 NULL
830};
831
832static struct attribute_group intuos5_led_attr_group = {
833 .name = "wacom_led",
834 .attrs = intuos5_led_attrs,
835};
836
5d7e7d47
EH
837static int wacom_initialize_leds(struct wacom *wacom)
838{
839 int error;
840
09e7d941
PC
841 /* Initialize default values */
842 switch (wacom->wacom_wac.features.type) {
a19fc986 843 case INTUOS4S:
09e7d941
PC
844 case INTUOS4:
845 case INTUOS4L:
846 wacom->led.select[0] = 0;
847 wacom->led.select[1] = 0;
f4fa9a6d 848 wacom->led.llv = 10;
5d7e7d47
EH
849 wacom->led.hlv = 20;
850 wacom->led.img_lum = 10;
c31a408f 851 error = sysfs_create_group(&wacom->hdev->dev.kobj,
09e7d941
PC
852 &intuos4_led_attr_group);
853 break;
854
246835fc 855 case WACOM_24HD:
09e7d941
PC
856 case WACOM_21UX2:
857 wacom->led.select[0] = 0;
858 wacom->led.select[1] = 0;
859 wacom->led.llv = 0;
860 wacom->led.hlv = 0;
861 wacom->led.img_lum = 0;
5d7e7d47 862
c31a408f 863 error = sysfs_create_group(&wacom->hdev->dev.kobj,
09e7d941
PC
864 &cintiq_led_attr_group);
865 break;
866
9b5b95dd
JG
867 case INTUOS5S:
868 case INTUOS5:
869 case INTUOS5L:
9a35c411
PC
870 case INTUOSPS:
871 case INTUOSPM:
872 case INTUOSPL:
c2b0c273
PC
873 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
874 wacom->led.select[0] = 0;
875 wacom->led.select[1] = 0;
876 wacom->led.llv = 32;
877 wacom->led.hlv = 0;
878 wacom->led.img_lum = 0;
879
c31a408f 880 error = sysfs_create_group(&wacom->hdev->dev.kobj,
c2b0c273
PC
881 &intuos5_led_attr_group);
882 } else
883 return 0;
9b5b95dd
JG
884 break;
885
09e7d941
PC
886 default:
887 return 0;
5d7e7d47
EH
888 }
889
09e7d941 890 if (error) {
c31a408f 891 hid_err(wacom->hdev,
09e7d941
PC
892 "cannot create sysfs group err: %d\n", error);
893 return error;
894 }
895 wacom_led_control(wacom);
896
5d7e7d47
EH
897 return 0;
898}
899
900static void wacom_destroy_leds(struct wacom *wacom)
901{
09e7d941 902 switch (wacom->wacom_wac.features.type) {
a19fc986 903 case INTUOS4S:
09e7d941
PC
904 case INTUOS4:
905 case INTUOS4L:
c31a408f 906 sysfs_remove_group(&wacom->hdev->dev.kobj,
09e7d941
PC
907 &intuos4_led_attr_group);
908 break;
909
246835fc 910 case WACOM_24HD:
09e7d941 911 case WACOM_21UX2:
c31a408f 912 sysfs_remove_group(&wacom->hdev->dev.kobj,
09e7d941
PC
913 &cintiq_led_attr_group);
914 break;
9b5b95dd
JG
915
916 case INTUOS5S:
917 case INTUOS5:
918 case INTUOS5L:
9a35c411
PC
919 case INTUOSPS:
920 case INTUOSPM:
921 case INTUOSPL:
c2b0c273 922 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
c31a408f 923 sysfs_remove_group(&wacom->hdev->dev.kobj,
c2b0c273 924 &intuos5_led_attr_group);
9b5b95dd 925 break;
5d7e7d47
EH
926 }
927}
928
a1d552cc 929static enum power_supply_property wacom_battery_props[] = {
6e2a6e80 930 POWER_SUPPLY_PROP_SCOPE,
a1d552cc
CB
931 POWER_SUPPLY_PROP_CAPACITY
932};
933
934static int wacom_battery_get_property(struct power_supply *psy,
935 enum power_supply_property psp,
936 union power_supply_propval *val)
937{
938 struct wacom *wacom = container_of(psy, struct wacom, battery);
939 int ret = 0;
940
941 switch (psp) {
6e2a6e80
BN
942 case POWER_SUPPLY_PROP_SCOPE:
943 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
944 break;
a1d552cc
CB
945 case POWER_SUPPLY_PROP_CAPACITY:
946 val->intval =
947 wacom->wacom_wac.battery_capacity * 100 / 31;
948 break;
949 default:
950 ret = -EINVAL;
951 break;
952 }
953
954 return ret;
955}
956
957static int wacom_initialize_battery(struct wacom *wacom)
958{
959 int error = 0;
960
961 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR) {
962 wacom->battery.properties = wacom_battery_props;
963 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
964 wacom->battery.get_property = wacom_battery_get_property;
965 wacom->battery.name = "wacom_battery";
966 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
967 wacom->battery.use_for_apm = 0;
968
dd3181a7 969 error = power_supply_register(&wacom->hdev->dev,
a1d552cc 970 &wacom->battery);
b7af2bb8
CB
971
972 if (!error)
973 power_supply_powers(&wacom->battery,
dd3181a7 974 &wacom->hdev->dev);
a1d552cc
CB
975 }
976
977 return error;
978}
979
980static void wacom_destroy_battery(struct wacom *wacom)
981{
b7af2bb8
CB
982 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR &&
983 wacom->battery.dev) {
a1d552cc 984 power_supply_unregister(&wacom->battery);
b7af2bb8
CB
985 wacom->battery.dev = NULL;
986 }
a1d552cc
CB
987}
988
d2d13f18 989static struct input_dev *wacom_allocate_input(struct wacom *wacom)
3aac0ef1
CB
990{
991 struct input_dev *input_dev;
b6c79f2c 992 struct hid_device *hdev = wacom->hdev;
3aac0ef1 993 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
3aac0ef1
CB
994
995 input_dev = input_allocate_device();
d2d13f18
BT
996 if (!input_dev)
997 return NULL;
3aac0ef1
CB
998
999 input_dev->name = wacom_wac->name;
b6c79f2c
BT
1000 input_dev->phys = hdev->phys;
1001 input_dev->dev.parent = &hdev->dev;
3aac0ef1
CB
1002 input_dev->open = wacom_open;
1003 input_dev->close = wacom_close;
b6c79f2c
BT
1004 input_dev->uniq = hdev->uniq;
1005 input_dev->id.bustype = hdev->bus;
1006 input_dev->id.vendor = hdev->vendor;
1007 input_dev->id.product = hdev->product;
1008 input_dev->id.version = hdev->version;
3aac0ef1
CB
1009 input_set_drvdata(input_dev, wacom);
1010
d2d13f18
BT
1011 return input_dev;
1012}
1013
008f4d9e
BT
1014static void wacom_unregister_inputs(struct wacom *wacom)
1015{
1016 if (wacom->wacom_wac.input)
1017 input_unregister_device(wacom->wacom_wac.input);
1018 if (wacom->wacom_wac.pad_input)
1019 input_unregister_device(wacom->wacom_wac.pad_input);
1020 wacom->wacom_wac.input = NULL;
1021 wacom->wacom_wac.pad_input = NULL;
1022}
1023
1024static int wacom_register_inputs(struct wacom *wacom)
d2d13f18
BT
1025{
1026 struct input_dev *input_dev, *pad_input_dev;
1027 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1028 int error;
1029
1030 input_dev = wacom_allocate_input(wacom);
1031 pad_input_dev = wacom_allocate_input(wacom);
1032 if (!input_dev || !pad_input_dev) {
1033 error = -ENOMEM;
1034 goto fail1;
1035 }
1036
3aac0ef1 1037 wacom_wac->input = input_dev;
d2d13f18
BT
1038 wacom_wac->pad_input = pad_input_dev;
1039 wacom_wac->pad_input->name = wacom_wac->pad_name;
1040
1963518b
PC
1041 error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1042 if (error)
d2d13f18 1043 goto fail2;
3aac0ef1
CB
1044
1045 error = input_register_device(input_dev);
1963518b
PC
1046 if (error)
1047 goto fail2;
1048
d2d13f18
BT
1049 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1050 if (error) {
1051 /* no pad in use on this interface */
1052 input_free_device(pad_input_dev);
1053 wacom_wac->pad_input = NULL;
1054 pad_input_dev = NULL;
1055 } else {
1056 error = input_register_device(pad_input_dev);
1057 if (error)
1058 goto fail3;
1059 }
1060
1963518b 1061 return 0;
3aac0ef1 1062
d2d13f18
BT
1063fail3:
1064 input_unregister_device(input_dev);
1065 input_dev = NULL;
1963518b 1066fail2:
1963518b 1067 wacom_wac->input = NULL;
d2d13f18 1068 wacom_wac->pad_input = NULL;
1963518b 1069fail1:
d2d13f18
BT
1070 if (input_dev)
1071 input_free_device(input_dev);
1072 if (pad_input_dev)
1073 input_free_device(pad_input_dev);
3aac0ef1
CB
1074 return error;
1075}
1076
16bf288c
CB
1077static void wacom_wireless_work(struct work_struct *work)
1078{
1079 struct wacom *wacom = container_of(work, struct wacom, work);
1080 struct usb_device *usbdev = wacom->usbdev;
1081 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
29b47391 1082 struct hid_device *hdev1, *hdev2;
b7af2bb8
CB
1083 struct wacom *wacom1, *wacom2;
1084 struct wacom_wac *wacom_wac1, *wacom_wac2;
1085 int error;
16bf288c
CB
1086
1087 /*
1088 * Regardless if this is a disconnect or a new tablet,
b7af2bb8 1089 * remove any existing input and battery devices.
16bf288c
CB
1090 */
1091
b7af2bb8
CB
1092 wacom_destroy_battery(wacom);
1093
16bf288c 1094 /* Stylus interface */
29b47391
BT
1095 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1096 wacom1 = hid_get_drvdata(hdev1);
b7af2bb8 1097 wacom_wac1 = &(wacom1->wacom_wac);
008f4d9e 1098 wacom_unregister_inputs(wacom1);
16bf288c
CB
1099
1100 /* Touch interface */
29b47391
BT
1101 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1102 wacom2 = hid_get_drvdata(hdev2);
b7af2bb8 1103 wacom_wac2 = &(wacom2->wacom_wac);
008f4d9e 1104 wacom_unregister_inputs(wacom2);
16bf288c
CB
1105
1106 if (wacom_wac->pid == 0) {
e2114ce1 1107 hid_info(wacom->hdev, "wireless tablet disconnected\n");
16bf288c 1108 } else {
29b47391 1109 const struct hid_device_id *id = wacom_ids;
16bf288c 1110
e2114ce1 1111 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
eb71d1bb 1112 wacom_wac->pid);
16bf288c 1113
29b47391
BT
1114 while (id->bus) {
1115 if (id->vendor == USB_VENDOR_ID_WACOM &&
1116 id->product == wacom_wac->pid)
16bf288c
CB
1117 break;
1118 id++;
1119 }
1120
29b47391 1121 if (!id->bus) {
e2114ce1 1122 hid_info(wacom->hdev, "ignoring unknown PID.\n");
16bf288c
CB
1123 return;
1124 }
1125
1126 /* Stylus interface */
b7af2bb8 1127 wacom_wac1->features =
29b47391 1128 *((struct wacom_features *)id->driver_data);
b7af2bb8 1129 wacom_wac1->features.device_type = BTN_TOOL_PEN;
57bcfce3
PC
1130 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1131 wacom_wac1->features.name);
008f4d9e
BT
1132 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1133 wacom_wac1->features.name);
961794a0
PC
1134 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1135 wacom_wac1->shared->type = wacom_wac1->features.type;
008f4d9e 1136 error = wacom_register_inputs(wacom1);
b7af2bb8 1137 if (error)
57bcfce3 1138 goto fail;
16bf288c
CB
1139
1140 /* Touch interface */
b5fd2a3e
PC
1141 if (wacom_wac1->features.touch_max ||
1142 wacom_wac1->features.type == INTUOSHT) {
57bcfce3 1143 wacom_wac2->features =
29b47391 1144 *((struct wacom_features *)id->driver_data);
57bcfce3
PC
1145 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1146 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1147 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1148 if (wacom_wac2->features.touch_max)
1149 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1150 "%s (WL) Finger",wacom_wac2->features.name);
1151 else
1152 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1153 "%s (WL) Pad",wacom_wac2->features.name);
008f4d9e
BT
1154 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1155 "%s (WL) Pad", wacom_wac2->features.name);
1156 error = wacom_register_inputs(wacom2);
57bcfce3
PC
1157 if (error)
1158 goto fail;
961794a0
PC
1159
1160 if (wacom_wac1->features.type == INTUOSHT &&
1161 wacom_wac1->features.touch_max)
1162 wacom_wac->shared->touch_input = wacom_wac2->input;
57bcfce3 1163 }
b7af2bb8
CB
1164
1165 error = wacom_initialize_battery(wacom);
1166 if (error)
57bcfce3 1167 goto fail;
16bf288c 1168 }
b7af2bb8
CB
1169
1170 return;
1171
57bcfce3 1172fail:
008f4d9e
BT
1173 wacom_unregister_inputs(wacom1);
1174 wacom_unregister_inputs(wacom2);
b7af2bb8 1175 return;
16bf288c
CB
1176}
1177
401d7d10
PC
1178/*
1179 * Not all devices report physical dimensions from HID.
1180 * Compute the default from hardcoded logical dimension
1181 * and resolution before driver overwrites them.
1182 */
1183static void wacom_set_default_phy(struct wacom_features *features)
1184{
1185 if (features->x_resolution) {
1186 features->x_phy = (features->x_max * 100) /
1187 features->x_resolution;
1188 features->y_phy = (features->y_max * 100) /
1189 features->y_resolution;
1190 }
1191}
1192
1193static void wacom_calculate_res(struct wacom_features *features)
1194{
1195 features->x_resolution = wacom_calc_hid_res(features->x_max,
1196 features->x_phy,
1197 features->unit,
1198 features->unitExpo);
1199 features->y_resolution = wacom_calc_hid_res(features->y_max,
1200 features->y_phy,
1201 features->unit,
1202 features->unitExpo);
1203}
1204
01c846f9
BT
1205static int wacom_hid_report_len(struct hid_report *report)
1206{
1207 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1208 return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1209}
1210
1211static size_t wacom_compute_pktlen(struct hid_device *hdev)
1212{
1213 struct hid_report_enum *report_enum;
1214 struct hid_report *report;
1215 size_t size = 0;
1216
1217 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1218
1219 list_for_each_entry(report, &report_enum->report_list, list) {
1220 size_t report_size = wacom_hid_report_len(report);
1221 if (report_size > size)
1222 size = report_size;
1223 }
1224
1225 return size;
1226}
1227
29b47391
BT
1228static int wacom_probe(struct hid_device *hdev,
1229 const struct hid_device_id *id)
3bea733a 1230{
29b47391 1231 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
3bea733a 1232 struct usb_device *dev = interface_to_usbdev(intf);
3bea733a
PC
1233 struct wacom *wacom;
1234 struct wacom_wac *wacom_wac;
e33da8a5 1235 struct wacom_features *features;
e33da8a5 1236 int error;
3bea733a 1237
29b47391 1238 if (!id->driver_data)
b036f6fb
BB
1239 return -EINVAL;
1240
3bea733a 1241 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
f1823940
DC
1242 if (!wacom)
1243 return -ENOMEM;
e33da8a5 1244
29b47391
BT
1245 hid_set_drvdata(hdev, wacom);
1246 wacom->hdev = hdev;
1247
ba9a3541
BT
1248 /* ask for the report descriptor to be loaded by HID */
1249 error = hid_parse(hdev);
1250 if (error) {
1251 hid_err(hdev, "parse failed\n");
1252 goto fail1;
1253 }
1254
51269fe8 1255 wacom_wac = &wacom->wacom_wac;
29b47391 1256 wacom_wac->features = *((struct wacom_features *)id->driver_data);
e33da8a5 1257 features = &wacom_wac->features;
01c846f9 1258 features->pktlen = wacom_compute_pktlen(hdev);
e33da8a5
JC
1259 if (features->pktlen > WACOM_PKGLEN_MAX) {
1260 error = -EINVAL;
3bea733a 1261 goto fail1;
e33da8a5 1262 }
3bea733a 1263
29b47391
BT
1264 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1265 error = -ENODEV;
3bea733a 1266 goto fail1;
e33da8a5 1267 }
3bea733a 1268
3bea733a 1269 wacom->usbdev = dev;
e7224094
ON
1270 wacom->intf = intf;
1271 mutex_init(&wacom->lock);
16bf288c 1272 INIT_WORK(&wacom->work, wacom_wireless_work);
3bea733a 1273
401d7d10
PC
1274 /* set the default size in case we do not get them from hid */
1275 wacom_set_default_phy(features);
1276
f393ee2b 1277 /* Retrieve the physical and logical size for touch devices */
27b20a9d 1278 error = wacom_retrieve_hid_descriptor(hdev, features);
ec67bbed 1279 if (error)
29b47391 1280 goto fail1;
545f4e99 1281
ae584ca4
JG
1282 /*
1283 * Intuos5 has no useful data about its touch interface in its
01c846f9 1284 * HID descriptor. If this is the touch interface (PacketSize
ae584ca4
JG
1285 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1286 */
b5fd2a3e 1287 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
01c846f9 1288 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
ae584ca4 1289 features->device_type = BTN_TOOL_FINGER;
ae584ca4 1290
ae584ca4
JG
1291 features->x_max = 4096;
1292 features->y_max = 4096;
1293 } else {
1294 features->device_type = BTN_TOOL_PEN;
1295 }
1296 }
1297
bc73dd39
HR
1298 wacom_setup_device_quirks(features);
1299
401d7d10
PC
1300 /* set unit to "100th of a mm" for devices not reported by HID */
1301 if (!features->unit) {
1302 features->unit = 0x11;
1303 features->unitExpo = 16 - 3;
1304 }
1305 wacom_calculate_res(features);
1306
49b764ae 1307 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
d2d13f18
BT
1308 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1309 "%s Pad", features->name);
49b764ae 1310
bc73dd39 1311 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
49b764ae 1312 /* Append the device type to the name */
57bcfce3
PC
1313 if (features->device_type != BTN_TOOL_FINGER)
1314 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1315 else if (features->touch_max)
1316 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1317 else
1318 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
4492efff 1319
4451e088 1320 error = wacom_add_shared_data(hdev);
4492efff 1321 if (error)
29b47391 1322 goto fail1;
49b764ae
PC
1323 }
1324
5d7e7d47 1325 error = wacom_initialize_leds(wacom);
5014186d 1326 if (error)
29b47391 1327 goto fail2;
3bea733a 1328
d3825d51 1329 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
008f4d9e 1330 error = wacom_register_inputs(wacom);
d3825d51 1331 if (error)
29b47391 1332 goto fail3;
d3825d51 1333 }
5d7e7d47 1334
ec67bbed 1335 /* Note that if query fails it is not a hard failure */
27b20a9d 1336 wacom_query_tablet_data(hdev, features);
3bea733a 1337
29b47391 1338 /* Regular HID work starts now */
29b47391
BT
1339 error = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1340 if (error) {
1341 hid_err(hdev, "hw start failed\n");
1342 goto fail4;
d3825d51 1343 }
961794a0 1344
29b47391
BT
1345 if (features->quirks & WACOM_QUIRK_MONITOR)
1346 error = hid_hw_open(hdev);
1347
961794a0
PC
1348 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1349 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1350 wacom_wac->shared->touch_input = wacom_wac->input;
1351 }
1352
3bea733a
PC
1353 return 0;
1354
29b47391
BT
1355 fail4: wacom_unregister_inputs(wacom);
1356 fail3: wacom_destroy_leds(wacom);
1357 fail2: wacom_remove_shared_data(wacom_wac);
3aac0ef1 1358 fail1: kfree(wacom);
29b47391 1359 hid_set_drvdata(hdev, NULL);
5014186d 1360 return error;
3bea733a
PC
1361}
1362
29b47391 1363static void wacom_remove(struct hid_device *hdev)
3bea733a 1364{
29b47391 1365 struct wacom *wacom = hid_get_drvdata(hdev);
3bea733a 1366
29b47391 1367 hid_hw_stop(hdev);
e7224094 1368
16bf288c 1369 cancel_work_sync(&wacom->work);
008f4d9e 1370 wacom_unregister_inputs(wacom);
a1d552cc 1371 wacom_destroy_battery(wacom);
5d7e7d47 1372 wacom_destroy_leds(wacom);
51269fe8 1373 wacom_remove_shared_data(&wacom->wacom_wac);
e7224094 1374
29b47391
BT
1375 hid_set_drvdata(hdev, NULL);
1376 kfree(wacom);
e7224094
ON
1377}
1378
29b47391 1379static int wacom_resume(struct hid_device *hdev)
e7224094 1380{
29b47391 1381 struct wacom *wacom = hid_get_drvdata(hdev);
51269fe8 1382 struct wacom_features *features = &wacom->wacom_wac.features;
e7224094
ON
1383
1384 mutex_lock(&wacom->lock);
38101475
PC
1385
1386 /* switch to wacom mode first */
27b20a9d 1387 wacom_query_tablet_data(hdev, features);
5d7e7d47 1388 wacom_led_control(wacom);
38101475 1389
e7224094
ON
1390 mutex_unlock(&wacom->lock);
1391
29b47391 1392 return 0;
e7224094
ON
1393}
1394
29b47391 1395static int wacom_reset_resume(struct hid_device *hdev)
e7224094 1396{
29b47391 1397 return wacom_resume(hdev);
3bea733a
PC
1398}
1399
29b47391 1400static struct hid_driver wacom_driver = {
3bea733a 1401 .name = "wacom",
b036f6fb 1402 .id_table = wacom_ids,
3bea733a 1403 .probe = wacom_probe,
29b47391
BT
1404 .remove = wacom_remove,
1405#ifdef CONFIG_PM
e7224094
ON
1406 .resume = wacom_resume,
1407 .reset_resume = wacom_reset_resume,
29b47391
BT
1408#endif
1409 .raw_event = wacom_raw_event,
3bea733a 1410};
29b47391 1411module_hid_driver(wacom_driver);