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