Input: wacom - install LED/OLED sysfs files in the HID device instead of USB
[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 */
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;
989 struct usb_interface *intf = wacom->intf;
990 struct usb_device *dev = interface_to_usbdev(intf);
991 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
3aac0ef1
CB
992
993 input_dev = input_allocate_device();
d2d13f18
BT
994 if (!input_dev)
995 return NULL;
3aac0ef1
CB
996
997 input_dev->name = wacom_wac->name;
7097d4cb 998 input_dev->phys = wacom->phys;
3aac0ef1
CB
999 input_dev->dev.parent = &intf->dev;
1000 input_dev->open = wacom_open;
1001 input_dev->close = wacom_close;
1002 usb_to_input_id(dev, &input_dev->id);
1003 input_set_drvdata(input_dev, wacom);
1004
d2d13f18
BT
1005 return input_dev;
1006}
1007
008f4d9e
BT
1008static void wacom_unregister_inputs(struct wacom *wacom)
1009{
1010 if (wacom->wacom_wac.input)
1011 input_unregister_device(wacom->wacom_wac.input);
1012 if (wacom->wacom_wac.pad_input)
1013 input_unregister_device(wacom->wacom_wac.pad_input);
1014 wacom->wacom_wac.input = NULL;
1015 wacom->wacom_wac.pad_input = NULL;
1016}
1017
1018static int wacom_register_inputs(struct wacom *wacom)
d2d13f18
BT
1019{
1020 struct input_dev *input_dev, *pad_input_dev;
1021 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1022 int error;
1023
1024 input_dev = wacom_allocate_input(wacom);
1025 pad_input_dev = wacom_allocate_input(wacom);
1026 if (!input_dev || !pad_input_dev) {
1027 error = -ENOMEM;
1028 goto fail1;
1029 }
1030
3aac0ef1 1031 wacom_wac->input = input_dev;
d2d13f18
BT
1032 wacom_wac->pad_input = pad_input_dev;
1033 wacom_wac->pad_input->name = wacom_wac->pad_name;
1034
1963518b
PC
1035 error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1036 if (error)
d2d13f18 1037 goto fail2;
3aac0ef1
CB
1038
1039 error = input_register_device(input_dev);
1963518b
PC
1040 if (error)
1041 goto fail2;
1042
d2d13f18
BT
1043 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1044 if (error) {
1045 /* no pad in use on this interface */
1046 input_free_device(pad_input_dev);
1047 wacom_wac->pad_input = NULL;
1048 pad_input_dev = NULL;
1049 } else {
1050 error = input_register_device(pad_input_dev);
1051 if (error)
1052 goto fail3;
1053 }
1054
1963518b 1055 return 0;
3aac0ef1 1056
d2d13f18
BT
1057fail3:
1058 input_unregister_device(input_dev);
1059 input_dev = NULL;
1963518b 1060fail2:
1963518b 1061 wacom_wac->input = NULL;
d2d13f18 1062 wacom_wac->pad_input = NULL;
1963518b 1063fail1:
d2d13f18
BT
1064 if (input_dev)
1065 input_free_device(input_dev);
1066 if (pad_input_dev)
1067 input_free_device(pad_input_dev);
3aac0ef1
CB
1068 return error;
1069}
1070
16bf288c
CB
1071static void wacom_wireless_work(struct work_struct *work)
1072{
1073 struct wacom *wacom = container_of(work, struct wacom, work);
1074 struct usb_device *usbdev = wacom->usbdev;
1075 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
29b47391 1076 struct hid_device *hdev1, *hdev2;
b7af2bb8
CB
1077 struct wacom *wacom1, *wacom2;
1078 struct wacom_wac *wacom_wac1, *wacom_wac2;
1079 int error;
16bf288c
CB
1080
1081 /*
1082 * Regardless if this is a disconnect or a new tablet,
b7af2bb8 1083 * remove any existing input and battery devices.
16bf288c
CB
1084 */
1085
b7af2bb8
CB
1086 wacom_destroy_battery(wacom);
1087
16bf288c 1088 /* Stylus interface */
29b47391
BT
1089 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1090 wacom1 = hid_get_drvdata(hdev1);
b7af2bb8 1091 wacom_wac1 = &(wacom1->wacom_wac);
008f4d9e 1092 wacom_unregister_inputs(wacom1);
16bf288c
CB
1093
1094 /* Touch interface */
29b47391
BT
1095 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1096 wacom2 = hid_get_drvdata(hdev2);
b7af2bb8 1097 wacom_wac2 = &(wacom2->wacom_wac);
008f4d9e 1098 wacom_unregister_inputs(wacom2);
16bf288c
CB
1099
1100 if (wacom_wac->pid == 0) {
eb71d1bb 1101 dev_info(&wacom->intf->dev, "wireless tablet disconnected\n");
16bf288c 1102 } else {
29b47391 1103 const struct hid_device_id *id = wacom_ids;
16bf288c 1104
eb71d1bb
DT
1105 dev_info(&wacom->intf->dev,
1106 "wireless tablet connected with PID %x\n",
1107 wacom_wac->pid);
16bf288c 1108
29b47391
BT
1109 while (id->bus) {
1110 if (id->vendor == USB_VENDOR_ID_WACOM &&
1111 id->product == wacom_wac->pid)
16bf288c
CB
1112 break;
1113 id++;
1114 }
1115
29b47391 1116 if (!id->bus) {
eb71d1bb
DT
1117 dev_info(&wacom->intf->dev,
1118 "ignoring unknown PID.\n");
16bf288c
CB
1119 return;
1120 }
1121
1122 /* Stylus interface */
b7af2bb8 1123 wacom_wac1->features =
29b47391 1124 *((struct wacom_features *)id->driver_data);
b7af2bb8 1125 wacom_wac1->features.device_type = BTN_TOOL_PEN;
57bcfce3
PC
1126 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1127 wacom_wac1->features.name);
008f4d9e
BT
1128 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1129 wacom_wac1->features.name);
961794a0
PC
1130 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1131 wacom_wac1->shared->type = wacom_wac1->features.type;
008f4d9e 1132 error = wacom_register_inputs(wacom1);
b7af2bb8 1133 if (error)
57bcfce3 1134 goto fail;
16bf288c
CB
1135
1136 /* Touch interface */
b5fd2a3e
PC
1137 if (wacom_wac1->features.touch_max ||
1138 wacom_wac1->features.type == INTUOSHT) {
57bcfce3 1139 wacom_wac2->features =
29b47391 1140 *((struct wacom_features *)id->driver_data);
57bcfce3
PC
1141 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1142 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1143 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1144 if (wacom_wac2->features.touch_max)
1145 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1146 "%s (WL) Finger",wacom_wac2->features.name);
1147 else
1148 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1149 "%s (WL) Pad",wacom_wac2->features.name);
008f4d9e
BT
1150 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1151 "%s (WL) Pad", wacom_wac2->features.name);
1152 error = wacom_register_inputs(wacom2);
57bcfce3
PC
1153 if (error)
1154 goto fail;
961794a0
PC
1155
1156 if (wacom_wac1->features.type == INTUOSHT &&
1157 wacom_wac1->features.touch_max)
1158 wacom_wac->shared->touch_input = wacom_wac2->input;
57bcfce3 1159 }
b7af2bb8
CB
1160
1161 error = wacom_initialize_battery(wacom);
1162 if (error)
57bcfce3 1163 goto fail;
16bf288c 1164 }
b7af2bb8
CB
1165
1166 return;
1167
57bcfce3 1168fail:
008f4d9e
BT
1169 wacom_unregister_inputs(wacom1);
1170 wacom_unregister_inputs(wacom2);
b7af2bb8 1171 return;
16bf288c
CB
1172}
1173
401d7d10
PC
1174/*
1175 * Not all devices report physical dimensions from HID.
1176 * Compute the default from hardcoded logical dimension
1177 * and resolution before driver overwrites them.
1178 */
1179static void wacom_set_default_phy(struct wacom_features *features)
1180{
1181 if (features->x_resolution) {
1182 features->x_phy = (features->x_max * 100) /
1183 features->x_resolution;
1184 features->y_phy = (features->y_max * 100) /
1185 features->y_resolution;
1186 }
1187}
1188
1189static void wacom_calculate_res(struct wacom_features *features)
1190{
1191 features->x_resolution = wacom_calc_hid_res(features->x_max,
1192 features->x_phy,
1193 features->unit,
1194 features->unitExpo);
1195 features->y_resolution = wacom_calc_hid_res(features->y_max,
1196 features->y_phy,
1197 features->unit,
1198 features->unitExpo);
1199}
1200
01c846f9
BT
1201static int wacom_hid_report_len(struct hid_report *report)
1202{
1203 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1204 return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1205}
1206
1207static size_t wacom_compute_pktlen(struct hid_device *hdev)
1208{
1209 struct hid_report_enum *report_enum;
1210 struct hid_report *report;
1211 size_t size = 0;
1212
1213 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1214
1215 list_for_each_entry(report, &report_enum->report_list, list) {
1216 size_t report_size = wacom_hid_report_len(report);
1217 if (report_size > size)
1218 size = report_size;
1219 }
1220
1221 return size;
1222}
1223
29b47391
BT
1224static int wacom_probe(struct hid_device *hdev,
1225 const struct hid_device_id *id)
3bea733a 1226{
29b47391 1227 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
3bea733a 1228 struct usb_device *dev = interface_to_usbdev(intf);
3bea733a
PC
1229 struct wacom *wacom;
1230 struct wacom_wac *wacom_wac;
e33da8a5 1231 struct wacom_features *features;
e33da8a5 1232 int error;
3bea733a 1233
29b47391 1234 if (!id->driver_data)
b036f6fb
BB
1235 return -EINVAL;
1236
3bea733a 1237 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
f1823940
DC
1238 if (!wacom)
1239 return -ENOMEM;
e33da8a5 1240
29b47391
BT
1241 hid_set_drvdata(hdev, wacom);
1242 wacom->hdev = hdev;
1243
ba9a3541
BT
1244 /* ask for the report descriptor to be loaded by HID */
1245 error = hid_parse(hdev);
1246 if (error) {
1247 hid_err(hdev, "parse failed\n");
1248 goto fail1;
1249 }
1250
51269fe8 1251 wacom_wac = &wacom->wacom_wac;
29b47391 1252 wacom_wac->features = *((struct wacom_features *)id->driver_data);
e33da8a5 1253 features = &wacom_wac->features;
01c846f9 1254 features->pktlen = wacom_compute_pktlen(hdev);
e33da8a5
JC
1255 if (features->pktlen > WACOM_PKGLEN_MAX) {
1256 error = -EINVAL;
3bea733a 1257 goto fail1;
e33da8a5 1258 }
3bea733a 1259
29b47391
BT
1260 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1261 error = -ENODEV;
3bea733a 1262 goto fail1;
e33da8a5 1263 }
3bea733a 1264
3bea733a 1265 wacom->usbdev = dev;
e7224094
ON
1266 wacom->intf = intf;
1267 mutex_init(&wacom->lock);
16bf288c 1268 INIT_WORK(&wacom->work, wacom_wireless_work);
3bea733a
PC
1269 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
1270 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
1271
401d7d10
PC
1272 /* set the default size in case we do not get them from hid */
1273 wacom_set_default_phy(features);
1274
f393ee2b 1275 /* Retrieve the physical and logical size for touch devices */
27b20a9d 1276 error = wacom_retrieve_hid_descriptor(hdev, features);
ec67bbed 1277 if (error)
29b47391 1278 goto fail1;
545f4e99 1279
ae584ca4
JG
1280 /*
1281 * Intuos5 has no useful data about its touch interface in its
01c846f9 1282 * HID descriptor. If this is the touch interface (PacketSize
ae584ca4
JG
1283 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1284 */
b5fd2a3e 1285 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
01c846f9 1286 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
ae584ca4 1287 features->device_type = BTN_TOOL_FINGER;
ae584ca4 1288
ae584ca4
JG
1289 features->x_max = 4096;
1290 features->y_max = 4096;
1291 } else {
1292 features->device_type = BTN_TOOL_PEN;
1293 }
1294 }
1295
bc73dd39
HR
1296 wacom_setup_device_quirks(features);
1297
401d7d10
PC
1298 /* set unit to "100th of a mm" for devices not reported by HID */
1299 if (!features->unit) {
1300 features->unit = 0x11;
1301 features->unitExpo = 16 - 3;
1302 }
1303 wacom_calculate_res(features);
1304
49b764ae 1305 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
d2d13f18
BT
1306 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1307 "%s Pad", features->name);
49b764ae 1308
bc73dd39 1309 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
aea2bf6a
JG
1310 struct usb_device *other_dev;
1311
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
aea2bf6a
JG
1320 other_dev = wacom_get_sibling(dev, features->oVid, features->oPid);
1321 if (other_dev == NULL || wacom_get_usbdev_data(other_dev) == NULL)
1322 other_dev = dev;
1323 error = wacom_add_shared_data(wacom_wac, other_dev);
4492efff 1324 if (error)
29b47391 1325 goto fail1;
49b764ae
PC
1326 }
1327
5d7e7d47 1328 error = wacom_initialize_leds(wacom);
5014186d 1329 if (error)
29b47391 1330 goto fail2;
3bea733a 1331
d3825d51 1332 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
008f4d9e 1333 error = wacom_register_inputs(wacom);
d3825d51 1334 if (error)
29b47391 1335 goto fail3;
d3825d51 1336 }
5d7e7d47 1337
ec67bbed 1338 /* Note that if query fails it is not a hard failure */
27b20a9d 1339 wacom_query_tablet_data(hdev, features);
3bea733a 1340
29b47391 1341 /* Regular HID work starts now */
29b47391
BT
1342 error = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1343 if (error) {
1344 hid_err(hdev, "hw start failed\n");
1345 goto fail4;
d3825d51 1346 }
961794a0 1347
29b47391
BT
1348 if (features->quirks & WACOM_QUIRK_MONITOR)
1349 error = hid_hw_open(hdev);
1350
961794a0
PC
1351 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1352 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1353 wacom_wac->shared->touch_input = wacom_wac->input;
1354 }
1355
3bea733a
PC
1356 return 0;
1357
29b47391
BT
1358 fail4: wacom_unregister_inputs(wacom);
1359 fail3: wacom_destroy_leds(wacom);
1360 fail2: wacom_remove_shared_data(wacom_wac);
3aac0ef1 1361 fail1: kfree(wacom);
29b47391 1362 hid_set_drvdata(hdev, NULL);
5014186d 1363 return error;
3bea733a
PC
1364}
1365
29b47391 1366static void wacom_remove(struct hid_device *hdev)
3bea733a 1367{
29b47391 1368 struct wacom *wacom = hid_get_drvdata(hdev);
3bea733a 1369
29b47391 1370 hid_hw_stop(hdev);
e7224094 1371
16bf288c 1372 cancel_work_sync(&wacom->work);
008f4d9e 1373 wacom_unregister_inputs(wacom);
a1d552cc 1374 wacom_destroy_battery(wacom);
5d7e7d47 1375 wacom_destroy_leds(wacom);
51269fe8 1376 wacom_remove_shared_data(&wacom->wacom_wac);
e7224094 1377
29b47391
BT
1378 hid_set_drvdata(hdev, NULL);
1379 kfree(wacom);
e7224094
ON
1380}
1381
29b47391 1382static int wacom_resume(struct hid_device *hdev)
e7224094 1383{
29b47391 1384 struct wacom *wacom = hid_get_drvdata(hdev);
51269fe8 1385 struct wacom_features *features = &wacom->wacom_wac.features;
e7224094
ON
1386
1387 mutex_lock(&wacom->lock);
38101475
PC
1388
1389 /* switch to wacom mode first */
27b20a9d 1390 wacom_query_tablet_data(hdev, features);
5d7e7d47 1391 wacom_led_control(wacom);
38101475 1392
e7224094
ON
1393 mutex_unlock(&wacom->lock);
1394
29b47391 1395 return 0;
e7224094
ON
1396}
1397
29b47391 1398static int wacom_reset_resume(struct hid_device *hdev)
e7224094 1399{
29b47391 1400 return wacom_resume(hdev);
3bea733a
PC
1401}
1402
29b47391 1403static struct hid_driver wacom_driver = {
3bea733a 1404 .name = "wacom",
b036f6fb 1405 .id_table = wacom_ids,
3bea733a 1406 .probe = wacom_probe,
29b47391
BT
1407 .remove = wacom_remove,
1408#ifdef CONFIG_PM
e7224094
ON
1409 .resume = wacom_resume,
1410 .reset_resume = wacom_reset_resume,
29b47391
BT
1411#endif
1412 .raw_event = wacom_raw_event,
3bea733a 1413};
29b47391 1414module_hid_driver(wacom_driver);
This page took 0.488298 seconds and 5 git commands to generate.