]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/input/tablet/wacom_sys.c
Input: wacom - register the input devices on top of the HID one
[mirror_ubuntu-zesty-kernel.git] / drivers / input / tablet / wacom_sys.c
1 /*
2 * drivers/input/tablet/wacom_sys.c
3 *
4 * USB Wacom tablet support - system specific code
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
14 #include "wacom_wac.h"
15 #include "wacom.h"
16 #include <linux/hid.h>
17
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
25 #define WAC_MSG_RETRIES 5
26
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
32 static int wacom_get_report(struct hid_device *hdev, u8 type, u8 id,
33 void *buf, size_t size, unsigned int retries)
34 {
35 int retval;
36
37 do {
38 retval = hid_hw_raw_request(hdev, id, buf, size, type,
39 HID_REQ_GET_REPORT);
40 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
41
42 return retval;
43 }
44
45 static int wacom_set_report(struct hid_device *hdev, u8 type, u8 id,
46 void *buf, size_t size, unsigned int retries)
47 {
48 int retval;
49
50 do {
51 retval = hid_hw_raw_request(hdev, id, buf, size, type,
52 HID_REQ_SET_REPORT);
53 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
54
55 return retval;
56 }
57
58 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
59 u8 *raw_data, int size)
60 {
61 struct wacom *wacom = hid_get_drvdata(hdev);
62
63 if (size > WACOM_PKGLEN_MAX)
64 return 1;
65
66 memcpy(wacom->wacom_wac.data, raw_data, size);
67
68 wacom_wac_irq(&wacom->wacom_wac, size);
69
70 return 0;
71 }
72
73 static int wacom_open(struct input_dev *dev)
74 {
75 struct wacom *wacom = input_get_drvdata(dev);
76 int retval;
77
78 mutex_lock(&wacom->lock);
79 retval = hid_hw_open(wacom->hdev);
80 mutex_unlock(&wacom->lock);
81
82 return retval;
83 }
84
85 static void wacom_close(struct input_dev *dev)
86 {
87 struct wacom *wacom = input_get_drvdata(dev);
88
89 mutex_lock(&wacom->lock);
90 hid_hw_close(wacom->hdev);
91 mutex_unlock(&wacom->lock);
92 }
93
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 */
98 static 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
144 static 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->device_type = BTN_TOOL_FINGER;
153
154 features->x_max = features->y_max =
155 get_unaligned_le16(&report[10]);
156
157 length = 11;
158 }
159 return length;
160 }
161
162 static void wacom_retrieve_report_data(struct hid_device *hdev,
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;
172 result = wacom_get_report(hdev, HID_FEATURE_REPORT,
173 rep_data[0], rep_data, 2,
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
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.
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.
215 *
216 * Intuos5 touch interface does not contain useful data. We deal with
217 * this after returning from this function.
218 */
219 static int wacom_parse_hid(struct hid_device *hdev,
220 struct wacom_features *features)
221 {
222 /* result has to be defined as int for some devices */
223 int result = 0, touch_max = 0;
224 int i = 0, page = 0, finger = 0, pen = 0;
225 unsigned char *report = hdev->rdesc;
226
227 for (i = 0; i < hdev->rsize; i++) {
228
229 switch (report[i]) {
230 case HID_HDESC_USAGE_PAGE:
231 page = report[i + 1];
232 i++;
233 break;
234
235 case HID_HDESC_USAGE:
236 switch (page << 16 | report[i + 1]) {
237 case HID_GD_X:
238 if (finger) {
239 features->device_type = BTN_TOOL_FINGER;
240 /* touch device at least supports one touch point */
241 touch_max = 1;
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:
253 features->x_max =
254 get_unaligned_le16(&report[i + 3]);
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
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
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;
281 }
282 } else if (pen) {
283 /* penabled only accepts exact bytes of data */
284 features->device_type = BTN_TOOL_PEN;
285 features->x_max =
286 get_unaligned_le16(&report[i + 3]);
287 i += 4;
288 }
289 break;
290
291 case HID_GD_Y:
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
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
328 default:
329 features->y_max =
330 features->x_max;
331 features->y_phy =
332 get_unaligned_le16(&report[i + 3]);
333 i += 4;
334 break;
335 }
336 } else if (pen) {
337 features->y_max =
338 get_unaligned_le16(&report[i + 3]);
339 i += 4;
340 }
341 break;
342
343 case HID_DG_FINGER:
344 finger = 1;
345 i++;
346 break;
347
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 */
353 case HID_DG_STYLUS:
354 pen = 1;
355 i++;
356 break;
357
358 case HID_DG_CONTACTMAX:
359 /* leave touch_max as is if predefined */
360 if (!features->touch_max)
361 wacom_retrieve_report_data(hdev, features);
362 i++;
363 break;
364
365 case HID_DG_TIPPRESSURE:
366 if (pen) {
367 features->pressure_max =
368 get_unaligned_le16(&report[i + 3]);
369 i += 4;
370 }
371 break;
372 }
373 break;
374
375 case HID_HDESC_COLLECTION_END:
376 /* reset UsagePage and Finger */
377 finger = page = 0;
378 break;
379
380 case HID_HDESC_COLLECTION:
381 i++;
382 switch (report[i]) {
383 case HID_HDESC_COLLECTION_LOGICAL:
384 i += wacom_parse_logical_collection(&report[i],
385 features);
386 break;
387 }
388 break;
389 }
390 }
391
392 if (!features->touch_max && touch_max)
393 features->touch_max = touch_max;
394 result = 0;
395 return result;
396 }
397
398 static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
399 int length, int mode)
400 {
401 unsigned char *rep_data;
402 int error = -ENOMEM, limit = 0;
403
404 rep_data = kzalloc(length, GFP_KERNEL);
405 if (!rep_data)
406 return error;
407
408 do {
409 rep_data[0] = report_id;
410 rep_data[1] = mode;
411
412 error = wacom_set_report(hdev, HID_FEATURE_REPORT,
413 report_id, rep_data, length, 1);
414 if (error >= 0)
415 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
416 report_id, rep_data, length, 1);
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 */
431 static int wacom_query_tablet_data(struct hid_device *hdev,
432 struct wacom_features *features)
433 {
434 if (features->device_type == BTN_TOOL_FINGER) {
435 if (features->type > TABLETPC) {
436 /* MT Tablet PC touch */
437 return wacom_set_device_mode(hdev, 3, 4, 4);
438 }
439 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
440 return wacom_set_device_mode(hdev, 18, 3, 2);
441 }
442 } else if (features->device_type == BTN_TOOL_PEN) {
443 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
444 return wacom_set_device_mode(hdev, 2, 2, 2);
445 }
446 }
447
448 return 0;
449 }
450
451 static int wacom_retrieve_hid_descriptor(struct hid_device *hdev,
452 struct wacom_features *features)
453 {
454 int error = 0;
455 struct wacom *wacom = hid_get_drvdata(hdev);
456 struct usb_interface *intf = wacom->intf;
457
458 /* default features */
459 features->device_type = BTN_TOOL_PEN;
460 features->x_fuzz = 4;
461 features->y_fuzz = 4;
462 features->pressure_fuzz = 0;
463 features->distance_fuzz = 0;
464
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) {
475 features->device_type = BTN_TOOL_FINGER;
476 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
477 }
478 }
479
480 /* only devices that support touch need to retrieve the info */
481 if (features->type < BAMBOO_PT) {
482 goto out;
483 }
484
485 error = wacom_parse_hid(hdev, features);
486
487 out:
488 return error;
489 }
490
491 struct wacom_usbdev_data {
492 struct list_head list;
493 struct kref kref;
494 struct usb_device *dev;
495 struct wacom_shared shared;
496 };
497
498 static LIST_HEAD(wacom_udev_list);
499 static DEFINE_MUTEX(wacom_udev_list_lock);
500
501 static 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
525 static 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
539 static 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
562 out:
563 mutex_unlock(&wacom_udev_list_lock);
564 return retval;
565 }
566
567 static 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
579 static 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
590 static int wacom_led_control(struct wacom *wacom)
591 {
592 unsigned char *buf;
593 int retval;
594
595 buf = kzalloc(9, GFP_KERNEL);
596 if (!buf)
597 return -ENOMEM;
598
599 if (wacom->wacom_wac.features.type >= INTUOS5S &&
600 wacom->wacom_wac.features.type <= INTUOSPL) {
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 }
626
627 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
628 WAC_CMD_LED_CONTROL, buf, 9, WAC_CMD_RETRIES);
629 kfree(buf);
630
631 return retval;
632 }
633
634 static 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;
646 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
647 WAC_CMD_ICON_START, buf, 2, WAC_CMD_RETRIES);
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
657 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
658 WAC_CMD_ICON_XFER,
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;
667 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, WAC_CMD_ICON_START,
668 buf, 2, WAC_CMD_RETRIES);
669
670 out:
671 kfree(buf);
672 return retval;
673 }
674
675 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
676 const char *buf, size_t count)
677 {
678 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
679 struct wacom *wacom = hid_get_drvdata(hdev);
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
689 wacom->led.select[set_id] = id & 0x3;
690 err = wacom_led_control(wacom);
691
692 mutex_unlock(&wacom->lock);
693
694 return err < 0 ? err : count;
695 }
696
697 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
698 static 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 } \
703 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
704 struct device_attribute *attr, char *buf) \
705 { \
706 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
707 struct wacom *wacom = hid_get_drvdata(hdev); \
708 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
709 } \
710 static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
711 wacom_led##SET_ID##_select_show, \
712 wacom_led##SET_ID##_select_store)
713
714 DEVICE_LED_SELECT_ATTR(0);
715 DEVICE_LED_SELECT_ATTR(1);
716
717 static 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) \
738 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
739 struct device_attribute *attr, const char *buf, size_t count) \
740 { \
741 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
742 struct wacom *wacom = hid_get_drvdata(hdev); \
743 \
744 return wacom_luminance_store(wacom, &wacom->led.field, \
745 buf, count); \
746 } \
747 static DEVICE_ATTR(name##_luminance, S_IWUSR, \
748 NULL, wacom_##name##_luminance_store)
749
750 DEVICE_LUMINANCE_ATTR(status0, llv);
751 DEVICE_LUMINANCE_ATTR(status1, hlv);
752 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
753
754 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
755 const char *buf, size_t count)
756 {
757 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
758 struct wacom *wacom = hid_get_drvdata(hdev);
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) \
774 static 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 } \
779 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
780 NULL, wacom_btnimg##BUTTON_ID##_store)
781
782 DEVICE_BTNIMG_ATTR(0);
783 DEVICE_BTNIMG_ATTR(1);
784 DEVICE_BTNIMG_ATTR(2);
785 DEVICE_BTNIMG_ATTR(3);
786 DEVICE_BTNIMG_ATTR(4);
787 DEVICE_BTNIMG_ATTR(5);
788 DEVICE_BTNIMG_ATTR(6);
789 DEVICE_BTNIMG_ATTR(7);
790
791 static struct attribute *cintiq_led_attrs[] = {
792 &dev_attr_status_led0_select.attr,
793 &dev_attr_status_led1_select.attr,
794 NULL
795 };
796
797 static struct attribute_group cintiq_led_attr_group = {
798 .name = "wacom_led",
799 .attrs = cintiq_led_attrs,
800 };
801
802 static struct attribute *intuos4_led_attrs[] = {
803 &dev_attr_status0_luminance.attr,
804 &dev_attr_status1_luminance.attr,
805 &dev_attr_status_led0_select.attr,
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
818 static struct attribute_group intuos4_led_attr_group = {
819 .name = "wacom_led",
820 .attrs = intuos4_led_attrs,
821 };
822
823 static struct attribute *intuos5_led_attrs[] = {
824 &dev_attr_status0_luminance.attr,
825 &dev_attr_status_led0_select.attr,
826 NULL
827 };
828
829 static struct attribute_group intuos5_led_attr_group = {
830 .name = "wacom_led",
831 .attrs = intuos5_led_attrs,
832 };
833
834 static int wacom_initialize_leds(struct wacom *wacom)
835 {
836 int error;
837
838 /* Initialize default values */
839 switch (wacom->wacom_wac.features.type) {
840 case INTUOS4S:
841 case INTUOS4:
842 case INTUOS4L:
843 wacom->led.select[0] = 0;
844 wacom->led.select[1] = 0;
845 wacom->led.llv = 10;
846 wacom->led.hlv = 20;
847 wacom->led.img_lum = 10;
848 error = sysfs_create_group(&wacom->hdev->dev.kobj,
849 &intuos4_led_attr_group);
850 break;
851
852 case WACOM_24HD:
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;
859
860 error = sysfs_create_group(&wacom->hdev->dev.kobj,
861 &cintiq_led_attr_group);
862 break;
863
864 case INTUOS5S:
865 case INTUOS5:
866 case INTUOS5L:
867 case INTUOSPS:
868 case INTUOSPM:
869 case INTUOSPL:
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
877 error = sysfs_create_group(&wacom->hdev->dev.kobj,
878 &intuos5_led_attr_group);
879 } else
880 return 0;
881 break;
882
883 default:
884 return 0;
885 }
886
887 if (error) {
888 hid_err(wacom->hdev,
889 "cannot create sysfs group err: %d\n", error);
890 return error;
891 }
892 wacom_led_control(wacom);
893
894 return 0;
895 }
896
897 static void wacom_destroy_leds(struct wacom *wacom)
898 {
899 switch (wacom->wacom_wac.features.type) {
900 case INTUOS4S:
901 case INTUOS4:
902 case INTUOS4L:
903 sysfs_remove_group(&wacom->hdev->dev.kobj,
904 &intuos4_led_attr_group);
905 break;
906
907 case WACOM_24HD:
908 case WACOM_21UX2:
909 sysfs_remove_group(&wacom->hdev->dev.kobj,
910 &cintiq_led_attr_group);
911 break;
912
913 case INTUOS5S:
914 case INTUOS5:
915 case INTUOS5L:
916 case INTUOSPS:
917 case INTUOSPM:
918 case INTUOSPL:
919 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
920 sysfs_remove_group(&wacom->hdev->dev.kobj,
921 &intuos5_led_attr_group);
922 break;
923 }
924 }
925
926 static enum power_supply_property wacom_battery_props[] = {
927 POWER_SUPPLY_PROP_SCOPE,
928 POWER_SUPPLY_PROP_CAPACITY
929 };
930
931 static 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) {
939 case POWER_SUPPLY_PROP_SCOPE:
940 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
941 break;
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
954 static 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);
968
969 if (!error)
970 power_supply_powers(&wacom->battery,
971 &wacom->usbdev->dev);
972 }
973
974 return error;
975 }
976
977 static void wacom_destroy_battery(struct wacom *wacom)
978 {
979 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_MONITOR &&
980 wacom->battery.dev) {
981 power_supply_unregister(&wacom->battery);
982 wacom->battery.dev = NULL;
983 }
984 }
985
986 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
987 {
988 struct input_dev *input_dev;
989 struct hid_device *hdev = wacom->hdev;
990 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
991
992 input_dev = input_allocate_device();
993 if (!input_dev)
994 return NULL;
995
996 input_dev->name = wacom_wac->name;
997 input_dev->phys = hdev->phys;
998 input_dev->dev.parent = &hdev->dev;
999 input_dev->open = wacom_open;
1000 input_dev->close = wacom_close;
1001 input_dev->uniq = hdev->uniq;
1002 input_dev->id.bustype = hdev->bus;
1003 input_dev->id.vendor = hdev->vendor;
1004 input_dev->id.product = hdev->product;
1005 input_dev->id.version = hdev->version;
1006 input_set_drvdata(input_dev, wacom);
1007
1008 return input_dev;
1009 }
1010
1011 static void wacom_unregister_inputs(struct wacom *wacom)
1012 {
1013 if (wacom->wacom_wac.input)
1014 input_unregister_device(wacom->wacom_wac.input);
1015 if (wacom->wacom_wac.pad_input)
1016 input_unregister_device(wacom->wacom_wac.pad_input);
1017 wacom->wacom_wac.input = NULL;
1018 wacom->wacom_wac.pad_input = NULL;
1019 }
1020
1021 static int wacom_register_inputs(struct wacom *wacom)
1022 {
1023 struct input_dev *input_dev, *pad_input_dev;
1024 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1025 int error;
1026
1027 input_dev = wacom_allocate_input(wacom);
1028 pad_input_dev = wacom_allocate_input(wacom);
1029 if (!input_dev || !pad_input_dev) {
1030 error = -ENOMEM;
1031 goto fail1;
1032 }
1033
1034 wacom_wac->input = input_dev;
1035 wacom_wac->pad_input = pad_input_dev;
1036 wacom_wac->pad_input->name = wacom_wac->pad_name;
1037
1038 error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1039 if (error)
1040 goto fail2;
1041
1042 error = input_register_device(input_dev);
1043 if (error)
1044 goto fail2;
1045
1046 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1047 if (error) {
1048 /* no pad in use on this interface */
1049 input_free_device(pad_input_dev);
1050 wacom_wac->pad_input = NULL;
1051 pad_input_dev = NULL;
1052 } else {
1053 error = input_register_device(pad_input_dev);
1054 if (error)
1055 goto fail3;
1056 }
1057
1058 return 0;
1059
1060 fail3:
1061 input_unregister_device(input_dev);
1062 input_dev = NULL;
1063 fail2:
1064 wacom_wac->input = NULL;
1065 wacom_wac->pad_input = NULL;
1066 fail1:
1067 if (input_dev)
1068 input_free_device(input_dev);
1069 if (pad_input_dev)
1070 input_free_device(pad_input_dev);
1071 return error;
1072 }
1073
1074 static void wacom_wireless_work(struct work_struct *work)
1075 {
1076 struct wacom *wacom = container_of(work, struct wacom, work);
1077 struct usb_device *usbdev = wacom->usbdev;
1078 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1079 struct hid_device *hdev1, *hdev2;
1080 struct wacom *wacom1, *wacom2;
1081 struct wacom_wac *wacom_wac1, *wacom_wac2;
1082 int error;
1083
1084 /*
1085 * Regardless if this is a disconnect or a new tablet,
1086 * remove any existing input and battery devices.
1087 */
1088
1089 wacom_destroy_battery(wacom);
1090
1091 /* Stylus interface */
1092 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1093 wacom1 = hid_get_drvdata(hdev1);
1094 wacom_wac1 = &(wacom1->wacom_wac);
1095 wacom_unregister_inputs(wacom1);
1096
1097 /* Touch interface */
1098 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1099 wacom2 = hid_get_drvdata(hdev2);
1100 wacom_wac2 = &(wacom2->wacom_wac);
1101 wacom_unregister_inputs(wacom2);
1102
1103 if (wacom_wac->pid == 0) {
1104 dev_info(&wacom->intf->dev, "wireless tablet disconnected\n");
1105 } else {
1106 const struct hid_device_id *id = wacom_ids;
1107
1108 dev_info(&wacom->intf->dev,
1109 "wireless tablet connected with PID %x\n",
1110 wacom_wac->pid);
1111
1112 while (id->bus) {
1113 if (id->vendor == USB_VENDOR_ID_WACOM &&
1114 id->product == wacom_wac->pid)
1115 break;
1116 id++;
1117 }
1118
1119 if (!id->bus) {
1120 dev_info(&wacom->intf->dev,
1121 "ignoring unknown PID.\n");
1122 return;
1123 }
1124
1125 /* Stylus interface */
1126 wacom_wac1->features =
1127 *((struct wacom_features *)id->driver_data);
1128 wacom_wac1->features.device_type = BTN_TOOL_PEN;
1129 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1130 wacom_wac1->features.name);
1131 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1132 wacom_wac1->features.name);
1133 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1134 wacom_wac1->shared->type = wacom_wac1->features.type;
1135 error = wacom_register_inputs(wacom1);
1136 if (error)
1137 goto fail;
1138
1139 /* Touch interface */
1140 if (wacom_wac1->features.touch_max ||
1141 wacom_wac1->features.type == INTUOSHT) {
1142 wacom_wac2->features =
1143 *((struct wacom_features *)id->driver_data);
1144 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1145 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1146 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1147 if (wacom_wac2->features.touch_max)
1148 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1149 "%s (WL) Finger",wacom_wac2->features.name);
1150 else
1151 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1152 "%s (WL) Pad",wacom_wac2->features.name);
1153 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1154 "%s (WL) Pad", wacom_wac2->features.name);
1155 error = wacom_register_inputs(wacom2);
1156 if (error)
1157 goto fail;
1158
1159 if (wacom_wac1->features.type == INTUOSHT &&
1160 wacom_wac1->features.touch_max)
1161 wacom_wac->shared->touch_input = wacom_wac2->input;
1162 }
1163
1164 error = wacom_initialize_battery(wacom);
1165 if (error)
1166 goto fail;
1167 }
1168
1169 return;
1170
1171 fail:
1172 wacom_unregister_inputs(wacom1);
1173 wacom_unregister_inputs(wacom2);
1174 return;
1175 }
1176
1177 /*
1178 * Not all devices report physical dimensions from HID.
1179 * Compute the default from hardcoded logical dimension
1180 * and resolution before driver overwrites them.
1181 */
1182 static void wacom_set_default_phy(struct wacom_features *features)
1183 {
1184 if (features->x_resolution) {
1185 features->x_phy = (features->x_max * 100) /
1186 features->x_resolution;
1187 features->y_phy = (features->y_max * 100) /
1188 features->y_resolution;
1189 }
1190 }
1191
1192 static void wacom_calculate_res(struct wacom_features *features)
1193 {
1194 features->x_resolution = wacom_calc_hid_res(features->x_max,
1195 features->x_phy,
1196 features->unit,
1197 features->unitExpo);
1198 features->y_resolution = wacom_calc_hid_res(features->y_max,
1199 features->y_phy,
1200 features->unit,
1201 features->unitExpo);
1202 }
1203
1204 static int wacom_hid_report_len(struct hid_report *report)
1205 {
1206 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1207 return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1208 }
1209
1210 static size_t wacom_compute_pktlen(struct hid_device *hdev)
1211 {
1212 struct hid_report_enum *report_enum;
1213 struct hid_report *report;
1214 size_t size = 0;
1215
1216 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1217
1218 list_for_each_entry(report, &report_enum->report_list, list) {
1219 size_t report_size = wacom_hid_report_len(report);
1220 if (report_size > size)
1221 size = report_size;
1222 }
1223
1224 return size;
1225 }
1226
1227 static int wacom_probe(struct hid_device *hdev,
1228 const struct hid_device_id *id)
1229 {
1230 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1231 struct usb_device *dev = interface_to_usbdev(intf);
1232 struct wacom *wacom;
1233 struct wacom_wac *wacom_wac;
1234 struct wacom_features *features;
1235 int error;
1236
1237 if (!id->driver_data)
1238 return -EINVAL;
1239
1240 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1241 if (!wacom)
1242 return -ENOMEM;
1243
1244 hid_set_drvdata(hdev, wacom);
1245 wacom->hdev = hdev;
1246
1247 /* ask for the report descriptor to be loaded by HID */
1248 error = hid_parse(hdev);
1249 if (error) {
1250 hid_err(hdev, "parse failed\n");
1251 goto fail1;
1252 }
1253
1254 wacom_wac = &wacom->wacom_wac;
1255 wacom_wac->features = *((struct wacom_features *)id->driver_data);
1256 features = &wacom_wac->features;
1257 features->pktlen = wacom_compute_pktlen(hdev);
1258 if (features->pktlen > WACOM_PKGLEN_MAX) {
1259 error = -EINVAL;
1260 goto fail1;
1261 }
1262
1263 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1264 error = -ENODEV;
1265 goto fail1;
1266 }
1267
1268 wacom->usbdev = dev;
1269 wacom->intf = intf;
1270 mutex_init(&wacom->lock);
1271 INIT_WORK(&wacom->work, wacom_wireless_work);
1272
1273 /* set the default size in case we do not get them from hid */
1274 wacom_set_default_phy(features);
1275
1276 /* Retrieve the physical and logical size for touch devices */
1277 error = wacom_retrieve_hid_descriptor(hdev, features);
1278 if (error)
1279 goto fail1;
1280
1281 /*
1282 * Intuos5 has no useful data about its touch interface in its
1283 * HID descriptor. If this is the touch interface (PacketSize
1284 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1285 */
1286 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
1287 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
1288 features->device_type = BTN_TOOL_FINGER;
1289
1290 features->x_max = 4096;
1291 features->y_max = 4096;
1292 } else {
1293 features->device_type = BTN_TOOL_PEN;
1294 }
1295 }
1296
1297 wacom_setup_device_quirks(features);
1298
1299 /* set unit to "100th of a mm" for devices not reported by HID */
1300 if (!features->unit) {
1301 features->unit = 0x11;
1302 features->unitExpo = 16 - 3;
1303 }
1304 wacom_calculate_res(features);
1305
1306 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1307 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1308 "%s Pad", features->name);
1309
1310 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
1311 struct usb_device *other_dev;
1312
1313 /* Append the device type to the name */
1314 if (features->device_type != BTN_TOOL_FINGER)
1315 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1316 else if (features->touch_max)
1317 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1318 else
1319 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
1320
1321 other_dev = wacom_get_sibling(dev, features->oVid, features->oPid);
1322 if (other_dev == NULL || wacom_get_usbdev_data(other_dev) == NULL)
1323 other_dev = dev;
1324 error = wacom_add_shared_data(wacom_wac, other_dev);
1325 if (error)
1326 goto fail1;
1327 }
1328
1329 error = wacom_initialize_leds(wacom);
1330 if (error)
1331 goto fail2;
1332
1333 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1334 error = wacom_register_inputs(wacom);
1335 if (error)
1336 goto fail3;
1337 }
1338
1339 /* Note that if query fails it is not a hard failure */
1340 wacom_query_tablet_data(hdev, features);
1341
1342 /* Regular HID work starts now */
1343 error = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1344 if (error) {
1345 hid_err(hdev, "hw start failed\n");
1346 goto fail4;
1347 }
1348
1349 if (features->quirks & WACOM_QUIRK_MONITOR)
1350 error = hid_hw_open(hdev);
1351
1352 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1353 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1354 wacom_wac->shared->touch_input = wacom_wac->input;
1355 }
1356
1357 return 0;
1358
1359 fail4: wacom_unregister_inputs(wacom);
1360 fail3: wacom_destroy_leds(wacom);
1361 fail2: wacom_remove_shared_data(wacom_wac);
1362 fail1: kfree(wacom);
1363 hid_set_drvdata(hdev, NULL);
1364 return error;
1365 }
1366
1367 static void wacom_remove(struct hid_device *hdev)
1368 {
1369 struct wacom *wacom = hid_get_drvdata(hdev);
1370
1371 hid_hw_stop(hdev);
1372
1373 cancel_work_sync(&wacom->work);
1374 wacom_unregister_inputs(wacom);
1375 wacom_destroy_battery(wacom);
1376 wacom_destroy_leds(wacom);
1377 wacom_remove_shared_data(&wacom->wacom_wac);
1378
1379 hid_set_drvdata(hdev, NULL);
1380 kfree(wacom);
1381 }
1382
1383 static int wacom_resume(struct hid_device *hdev)
1384 {
1385 struct wacom *wacom = hid_get_drvdata(hdev);
1386 struct wacom_features *features = &wacom->wacom_wac.features;
1387
1388 mutex_lock(&wacom->lock);
1389
1390 /* switch to wacom mode first */
1391 wacom_query_tablet_data(hdev, features);
1392 wacom_led_control(wacom);
1393
1394 mutex_unlock(&wacom->lock);
1395
1396 return 0;
1397 }
1398
1399 static int wacom_reset_resume(struct hid_device *hdev)
1400 {
1401 return wacom_resume(hdev);
1402 }
1403
1404 static struct hid_driver wacom_driver = {
1405 .name = "wacom",
1406 .id_table = wacom_ids,
1407 .probe = wacom_probe,
1408 .remove = wacom_remove,
1409 #ifdef CONFIG_PM
1410 .resume = wacom_resume,
1411 .reset_resume = wacom_reset_resume,
1412 #endif
1413 .raw_event = wacom_raw_event,
1414 };
1415 module_hid_driver(wacom_driver);