]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - drivers/hid/wacom_sys.c
033cc032f45cba3ac889844cde743038ea58a380
[mirror_ubuntu-eoan-kernel.git] / drivers / hid / 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/input/mt.h>
17
18 #define WAC_MSG_RETRIES 5
19
20 #define WAC_CMD_WL_LED_CONTROL 0x03
21 #define WAC_CMD_LED_CONTROL 0x20
22 #define WAC_CMD_ICON_START 0x21
23 #define WAC_CMD_ICON_XFER 0x23
24 #define WAC_CMD_ICON_BT_XFER 0x26
25 #define WAC_CMD_RETRIES 10
26 #define WAC_CMD_DELETE_PAIRING 0x20
27 #define WAC_CMD_UNPAIR_ALL 0xFF
28
29 #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
30 #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
31 #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
32
33 static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
34 size_t size, unsigned int retries)
35 {
36 int retval;
37
38 do {
39 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
40 HID_REQ_GET_REPORT);
41 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
42
43 if (retval < 0)
44 hid_err(hdev, "wacom_get_report: ran out of retries "
45 "(last error = %d)\n", retval);
46
47 return retval;
48 }
49
50 static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
51 size_t size, unsigned int retries)
52 {
53 int retval;
54
55 do {
56 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
57 HID_REQ_SET_REPORT);
58 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
59
60 if (retval < 0)
61 hid_err(hdev, "wacom_set_report: ran out of retries "
62 "(last error = %d)\n", retval);
63
64 return retval;
65 }
66
67 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
68 u8 *raw_data, int size)
69 {
70 struct wacom *wacom = hid_get_drvdata(hdev);
71
72 if (size > WACOM_PKGLEN_MAX)
73 return 1;
74
75 memcpy(wacom->wacom_wac.data, raw_data, size);
76
77 wacom_wac_irq(&wacom->wacom_wac, size);
78
79 return 0;
80 }
81
82 static int wacom_open(struct input_dev *dev)
83 {
84 struct wacom *wacom = input_get_drvdata(dev);
85
86 return hid_hw_open(wacom->hdev);
87 }
88
89 static void wacom_close(struct input_dev *dev)
90 {
91 struct wacom *wacom = input_get_drvdata(dev);
92
93 /*
94 * wacom->hdev should never be null, but surprisingly, I had the case
95 * once while unplugging the Wacom Wireless Receiver.
96 */
97 if (wacom->hdev)
98 hid_hw_close(wacom->hdev);
99 }
100
101 /*
102 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
103 */
104 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
105 unsigned unit, int exponent)
106 {
107 struct hid_field field = {
108 .logical_maximum = logical_extents,
109 .physical_maximum = physical_extents,
110 .unit = unit,
111 .unit_exponent = exponent,
112 };
113
114 return hidinput_calc_abs_res(&field, ABS_X);
115 }
116
117 static void wacom_feature_mapping(struct hid_device *hdev,
118 struct hid_field *field, struct hid_usage *usage)
119 {
120 struct wacom *wacom = hid_get_drvdata(hdev);
121 struct wacom_features *features = &wacom->wacom_wac.features;
122 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
123 u8 *data;
124 int ret;
125
126 switch (usage->hid) {
127 case HID_DG_CONTACTMAX:
128 /* leave touch_max as is if predefined */
129 if (!features->touch_max) {
130 /* read manually */
131 data = kzalloc(2, GFP_KERNEL);
132 if (!data)
133 break;
134 data[0] = field->report->id;
135 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
136 data, 2, WAC_CMD_RETRIES);
137 if (ret == 2) {
138 features->touch_max = data[1];
139 } else {
140 features->touch_max = 16;
141 hid_warn(hdev, "wacom_feature_mapping: "
142 "could not get HID_DG_CONTACTMAX, "
143 "defaulting to %d\n",
144 features->touch_max);
145 }
146 kfree(data);
147 }
148 break;
149 case HID_DG_INPUTMODE:
150 /* Ignore if value index is out of bounds. */
151 if (usage->usage_index >= field->report_count) {
152 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
153 break;
154 }
155
156 hid_data->inputmode = field->report->id;
157 hid_data->inputmode_index = usage->usage_index;
158 break;
159
160 case HID_UP_DIGITIZER:
161 if (field->report->id == 0x0B &&
162 (field->application == WACOM_HID_G9_PEN ||
163 field->application == WACOM_HID_G11_PEN)) {
164 wacom->wacom_wac.mode_report = field->report->id;
165 wacom->wacom_wac.mode_value = 0;
166 }
167 break;
168
169 case WACOM_HID_UP_G9:
170 case WACOM_HID_UP_G11:
171 if (field->report->id == 0x03 &&
172 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
173 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
174 wacom->wacom_wac.mode_report = field->report->id;
175 wacom->wacom_wac.mode_value = 0;
176 }
177 break;
178 }
179 }
180
181 /*
182 * Interface Descriptor of wacom devices can be incomplete and
183 * inconsistent so wacom_features table is used to store stylus
184 * device's packet lengths, various maximum values, and tablet
185 * resolution based on product ID's.
186 *
187 * For devices that contain 2 interfaces, wacom_features table is
188 * inaccurate for the touch interface. Since the Interface Descriptor
189 * for touch interfaces has pretty complete data, this function exists
190 * to query tablet for this missing information instead of hard coding in
191 * an additional table.
192 *
193 * A typical Interface Descriptor for a stylus will contain a
194 * boot mouse application collection that is not of interest and this
195 * function will ignore it.
196 *
197 * It also contains a digitizer application collection that also is not
198 * of interest since any information it contains would be duplicate
199 * of what is in wacom_features. Usually it defines a report of an array
200 * of bytes that could be used as max length of the stylus packet returned.
201 * If it happens to define a Digitizer-Stylus Physical Collection then
202 * the X and Y logical values contain valid data but it is ignored.
203 *
204 * A typical Interface Descriptor for a touch interface will contain a
205 * Digitizer-Finger Physical Collection which will define both logical
206 * X/Y maximum as well as the physical size of tablet. Since touch
207 * interfaces haven't supported pressure or distance, this is enough
208 * information to override invalid values in the wacom_features table.
209 *
210 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
211 * data. We deal with them after returning from this function.
212 */
213 static void wacom_usage_mapping(struct hid_device *hdev,
214 struct hid_field *field, struct hid_usage *usage)
215 {
216 struct wacom *wacom = hid_get_drvdata(hdev);
217 struct wacom_features *features = &wacom->wacom_wac.features;
218 bool finger = WACOM_FINGER_FIELD(field);
219 bool pen = WACOM_PEN_FIELD(field);
220
221 /*
222 * Requiring Stylus Usage will ignore boot mouse
223 * X/Y values and some cases of invalid Digitizer X/Y
224 * values commonly reported.
225 */
226 if (pen)
227 features->device_type |= WACOM_DEVICETYPE_PEN;
228 else if (finger)
229 features->device_type |= WACOM_DEVICETYPE_TOUCH;
230 else
231 return;
232
233 /*
234 * Bamboo models do not support HID_DG_CONTACTMAX.
235 * And, Bamboo Pen only descriptor contains touch.
236 */
237 if (features->type > BAMBOO_PT) {
238 /* ISDv4 touch devices at least supports one touch point */
239 if (finger && !features->touch_max)
240 features->touch_max = 1;
241 }
242
243 /*
244 * ISDv4 devices which predate HID's adoption of the
245 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
246 * position instead. We can accurately detect if a
247 * usage with that value should be HID_DG_BARRELSWITCH2
248 * based on the surrounding usages, which have remained
249 * constant across generations.
250 */
251 if (features->type == HID_GENERIC &&
252 usage->hid == 0x000D0000 &&
253 field->application == HID_DG_PEN &&
254 field->physical == HID_DG_STYLUS) {
255 int i = usage->usage_index;
256
257 if (i-4 >= 0 && i+1 < field->maxusage &&
258 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
259 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
260 field->usage[i-2].hid == HID_DG_ERASER &&
261 field->usage[i-1].hid == HID_DG_INVERT &&
262 field->usage[i+1].hid == HID_DG_INRANGE) {
263 usage->hid = HID_DG_BARRELSWITCH2;
264 }
265 }
266
267 switch (usage->hid) {
268 case HID_GD_X:
269 features->x_max = field->logical_maximum;
270 if (finger) {
271 features->x_phy = field->physical_maximum;
272 if ((features->type != BAMBOO_PT) &&
273 (features->type != BAMBOO_TOUCH)) {
274 features->unit = field->unit;
275 features->unitExpo = field->unit_exponent;
276 }
277 }
278 break;
279 case HID_GD_Y:
280 features->y_max = field->logical_maximum;
281 if (finger) {
282 features->y_phy = field->physical_maximum;
283 if ((features->type != BAMBOO_PT) &&
284 (features->type != BAMBOO_TOUCH)) {
285 features->unit = field->unit;
286 features->unitExpo = field->unit_exponent;
287 }
288 }
289 break;
290 case HID_DG_TIPPRESSURE:
291 if (pen)
292 features->pressure_max = field->logical_maximum;
293 break;
294 }
295
296 if (features->type == HID_GENERIC)
297 wacom_wac_usage_mapping(hdev, field, usage);
298 }
299
300 static void wacom_post_parse_hid(struct hid_device *hdev,
301 struct wacom_features *features)
302 {
303 struct wacom *wacom = hid_get_drvdata(hdev);
304 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
305
306 if (features->type == HID_GENERIC) {
307 /* Any last-minute generic device setup */
308 if (features->touch_max > 1) {
309 input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
310 INPUT_MT_DIRECT);
311 }
312 }
313 }
314
315 static void wacom_parse_hid(struct hid_device *hdev,
316 struct wacom_features *features)
317 {
318 struct hid_report_enum *rep_enum;
319 struct hid_report *hreport;
320 int i, j;
321
322 /* check features first */
323 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
324 list_for_each_entry(hreport, &rep_enum->report_list, list) {
325 for (i = 0; i < hreport->maxfield; i++) {
326 /* Ignore if report count is out of bounds. */
327 if (hreport->field[i]->report_count < 1)
328 continue;
329
330 for (j = 0; j < hreport->field[i]->maxusage; j++) {
331 wacom_feature_mapping(hdev, hreport->field[i],
332 hreport->field[i]->usage + j);
333 }
334 }
335 }
336
337 /* now check the input usages */
338 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
339 list_for_each_entry(hreport, &rep_enum->report_list, list) {
340
341 if (!hreport->maxfield)
342 continue;
343
344 for (i = 0; i < hreport->maxfield; i++)
345 for (j = 0; j < hreport->field[i]->maxusage; j++)
346 wacom_usage_mapping(hdev, hreport->field[i],
347 hreport->field[i]->usage + j);
348 }
349
350 wacom_post_parse_hid(hdev, features);
351 }
352
353 static int wacom_hid_set_device_mode(struct hid_device *hdev)
354 {
355 struct wacom *wacom = hid_get_drvdata(hdev);
356 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
357 struct hid_report *r;
358 struct hid_report_enum *re;
359
360 if (hid_data->inputmode < 0)
361 return 0;
362
363 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
364 r = re->report_id_hash[hid_data->inputmode];
365 if (r) {
366 r->field[0]->value[hid_data->inputmode_index] = 2;
367 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
368 }
369 return 0;
370 }
371
372 static int wacom_set_device_mode(struct hid_device *hdev,
373 struct wacom_wac *wacom_wac)
374 {
375 u8 *rep_data;
376 struct hid_report *r;
377 struct hid_report_enum *re;
378 int length;
379 int error = -ENOMEM, limit = 0;
380
381 if (wacom_wac->mode_report < 0)
382 return 0;
383
384 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
385 r = re->report_id_hash[wacom_wac->mode_report];
386 if (!r)
387 return -EINVAL;
388
389 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
390 if (!rep_data)
391 return -ENOMEM;
392
393 length = hid_report_len(r);
394
395 do {
396 rep_data[0] = wacom_wac->mode_report;
397 rep_data[1] = wacom_wac->mode_value;
398
399 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
400 length, 1);
401 if (error >= 0)
402 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
403 rep_data, length, 1);
404 } while (error >= 0 &&
405 rep_data[1] != wacom_wac->mode_report &&
406 limit++ < WAC_MSG_RETRIES);
407
408 kfree(rep_data);
409
410 return error < 0 ? error : 0;
411 }
412
413 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
414 struct wacom_features *features)
415 {
416 struct wacom *wacom = hid_get_drvdata(hdev);
417 int ret;
418 u8 rep_data[2];
419
420 switch (features->type) {
421 case GRAPHIRE_BT:
422 rep_data[0] = 0x03;
423 rep_data[1] = 0x00;
424 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
425 3);
426
427 if (ret >= 0) {
428 rep_data[0] = speed == 0 ? 0x05 : 0x06;
429 rep_data[1] = 0x00;
430
431 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
432 rep_data, 2, 3);
433
434 if (ret >= 0) {
435 wacom->wacom_wac.bt_high_speed = speed;
436 return 0;
437 }
438 }
439
440 /*
441 * Note that if the raw queries fail, it's not a hard failure
442 * and it is safe to continue
443 */
444 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
445 rep_data[0], ret);
446 break;
447 case INTUOS4WL:
448 if (speed == 1)
449 wacom->wacom_wac.bt_features &= ~0x20;
450 else
451 wacom->wacom_wac.bt_features |= 0x20;
452
453 rep_data[0] = 0x03;
454 rep_data[1] = wacom->wacom_wac.bt_features;
455
456 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
457 1);
458 if (ret >= 0)
459 wacom->wacom_wac.bt_high_speed = speed;
460 break;
461 }
462
463 return 0;
464 }
465
466 /*
467 * Switch the tablet into its most-capable mode. Wacom tablets are
468 * typically configured to power-up in a mode which sends mouse-like
469 * reports to the OS. To get absolute position, pressure data, etc.
470 * from the tablet, it is necessary to switch the tablet out of this
471 * mode and into one which sends the full range of tablet data.
472 */
473 static int wacom_query_tablet_data(struct hid_device *hdev,
474 struct wacom_features *features)
475 {
476 struct wacom *wacom = hid_get_drvdata(hdev);
477 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
478
479 if (hdev->bus == BUS_BLUETOOTH)
480 return wacom_bt_query_tablet_data(hdev, 1, features);
481
482 if (features->type != HID_GENERIC) {
483 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
484 if (features->type > TABLETPC) {
485 /* MT Tablet PC touch */
486 wacom_wac->mode_report = 3;
487 wacom_wac->mode_value = 4;
488 } else if (features->type == WACOM_24HDT) {
489 wacom_wac->mode_report = 18;
490 wacom_wac->mode_value = 2;
491 } else if (features->type == WACOM_27QHDT) {
492 wacom_wac->mode_report = 131;
493 wacom_wac->mode_value = 2;
494 } else if (features->type == BAMBOO_PAD) {
495 wacom_wac->mode_report = 2;
496 wacom_wac->mode_value = 2;
497 }
498 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
499 if (features->type <= BAMBOO_PT) {
500 wacom_wac->mode_report = 2;
501 wacom_wac->mode_value = 2;
502 }
503 }
504 }
505
506 wacom_set_device_mode(hdev, wacom_wac);
507
508 if (features->type == HID_GENERIC)
509 return wacom_hid_set_device_mode(hdev);
510
511 return 0;
512 }
513
514 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
515 struct wacom_features *features)
516 {
517 struct wacom *wacom = hid_get_drvdata(hdev);
518 struct usb_interface *intf = wacom->intf;
519
520 /* default features */
521 features->x_fuzz = 4;
522 features->y_fuzz = 4;
523 features->pressure_fuzz = 0;
524 features->distance_fuzz = 1;
525 features->tilt_fuzz = 1;
526
527 /*
528 * The wireless device HID is basic and layout conflicts with
529 * other tablets (monitor and touch interface can look like pen).
530 * Skip the query for this type and modify defaults based on
531 * interface number.
532 */
533 if (features->type == WIRELESS) {
534 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
535 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
536 else
537 features->device_type = WACOM_DEVICETYPE_NONE;
538 return;
539 }
540
541 wacom_parse_hid(hdev, features);
542 }
543
544 struct wacom_hdev_data {
545 struct list_head list;
546 struct kref kref;
547 struct hid_device *dev;
548 struct wacom_shared shared;
549 };
550
551 static LIST_HEAD(wacom_udev_list);
552 static DEFINE_MUTEX(wacom_udev_list_lock);
553
554 static bool compare_device_paths(struct hid_device *hdev_a,
555 struct hid_device *hdev_b, char separator)
556 {
557 int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
558 int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
559
560 if (n1 != n2 || n1 <= 0 || n2 <= 0)
561 return false;
562
563 return !strncmp(hdev_a->phys, hdev_b->phys, n1);
564 }
565
566 static bool wacom_are_sibling(struct hid_device *hdev,
567 struct hid_device *sibling)
568 {
569 struct wacom *wacom = hid_get_drvdata(hdev);
570 struct wacom_features *features = &wacom->wacom_wac.features;
571 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
572 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
573 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
574 __u32 oPid = features->oPid ? features->oPid : hdev->product;
575
576 /* The defined oVid/oPid must match that of the sibling */
577 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
578 return false;
579 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
580 return false;
581
582 /*
583 * Devices with the same VID/PID must share the same physical
584 * device path, while those with different VID/PID must share
585 * the same physical parent device path.
586 */
587 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
588 if (!compare_device_paths(hdev, sibling, '/'))
589 return false;
590 } else {
591 if (!compare_device_paths(hdev, sibling, '.'))
592 return false;
593 }
594
595 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
596 if (features->type != HID_GENERIC)
597 return true;
598
599 /*
600 * Direct-input devices may not be siblings of indirect-input
601 * devices.
602 */
603 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
604 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
605 return false;
606
607 /*
608 * Indirect-input devices may not be siblings of direct-input
609 * devices.
610 */
611 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
612 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
613 return false;
614
615 /* Pen devices may only be siblings of touch devices */
616 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
617 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
618 return false;
619
620 /* Touch devices may only be siblings of pen devices */
621 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
622 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
623 return false;
624
625 /*
626 * No reason could be found for these two devices to NOT be
627 * siblings, so there's a good chance they ARE siblings
628 */
629 return true;
630 }
631
632 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
633 {
634 struct wacom_hdev_data *data;
635
636 /* Try to find an already-probed interface from the same device */
637 list_for_each_entry(data, &wacom_udev_list, list) {
638 if (compare_device_paths(hdev, data->dev, '/'))
639 return data;
640 }
641
642 /* Fallback to finding devices that appear to be "siblings" */
643 list_for_each_entry(data, &wacom_udev_list, list) {
644 if (wacom_are_sibling(hdev, data->dev)) {
645 kref_get(&data->kref);
646 return data;
647 }
648 }
649
650 return NULL;
651 }
652
653 static void wacom_release_shared_data(struct kref *kref)
654 {
655 struct wacom_hdev_data *data =
656 container_of(kref, struct wacom_hdev_data, kref);
657
658 mutex_lock(&wacom_udev_list_lock);
659 list_del(&data->list);
660 mutex_unlock(&wacom_udev_list_lock);
661
662 kfree(data);
663 }
664
665 static void wacom_remove_shared_data(void *res)
666 {
667 struct wacom *wacom = res;
668 struct wacom_hdev_data *data;
669 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
670
671 if (wacom_wac->shared) {
672 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
673 shared);
674
675 if (wacom_wac->shared->touch == wacom->hdev)
676 wacom_wac->shared->touch = NULL;
677 else if (wacom_wac->shared->pen == wacom->hdev)
678 wacom_wac->shared->pen = NULL;
679
680 kref_put(&data->kref, wacom_release_shared_data);
681 wacom_wac->shared = NULL;
682 }
683 }
684
685 static int wacom_add_shared_data(struct hid_device *hdev)
686 {
687 struct wacom *wacom = hid_get_drvdata(hdev);
688 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
689 struct wacom_hdev_data *data;
690 int retval = 0;
691
692 mutex_lock(&wacom_udev_list_lock);
693
694 data = wacom_get_hdev_data(hdev);
695 if (!data) {
696 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
697 if (!data) {
698 retval = -ENOMEM;
699 goto out;
700 }
701
702 kref_init(&data->kref);
703 data->dev = hdev;
704 list_add_tail(&data->list, &wacom_udev_list);
705 }
706
707 wacom_wac->shared = &data->shared;
708
709 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
710 if (retval) {
711 mutex_unlock(&wacom_udev_list_lock);
712 wacom_remove_shared_data(wacom);
713 return retval;
714 }
715
716 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
717 wacom_wac->shared->touch = hdev;
718 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
719 wacom_wac->shared->pen = hdev;
720
721 out:
722 mutex_unlock(&wacom_udev_list_lock);
723 return retval;
724 }
725
726 static int wacom_led_control(struct wacom *wacom)
727 {
728 unsigned char *buf;
729 int retval;
730 unsigned char report_id = WAC_CMD_LED_CONTROL;
731 int buf_size = 9;
732
733 if (!hid_get_drvdata(wacom->hdev))
734 return -ENODEV;
735
736 if (!wacom->led.groups)
737 return -ENOTSUPP;
738
739 if (wacom->wacom_wac.pid) { /* wireless connected */
740 report_id = WAC_CMD_WL_LED_CONTROL;
741 buf_size = 13;
742 }
743 buf = kzalloc(buf_size, GFP_KERNEL);
744 if (!buf)
745 return -ENOMEM;
746
747 if (wacom->wacom_wac.features.type >= INTUOS5S &&
748 wacom->wacom_wac.features.type <= INTUOSPL) {
749 /*
750 * Touch Ring and crop mark LED luminance may take on
751 * one of four values:
752 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
753 */
754 int ring_led = wacom->led.groups[0].select & 0x03;
755 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
756 int crop_lum = 0;
757 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
758
759 buf[0] = report_id;
760 if (wacom->wacom_wac.pid) {
761 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
762 buf, buf_size, WAC_CMD_RETRIES);
763 buf[0] = report_id;
764 buf[4] = led_bits;
765 } else
766 buf[1] = led_bits;
767 }
768 else {
769 int led = wacom->led.groups[0].select | 0x4;
770
771 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
772 wacom->wacom_wac.features.type == WACOM_24HD)
773 led |= (wacom->led.groups[1].select << 4) | 0x40;
774
775 buf[0] = report_id;
776 buf[1] = led;
777 buf[2] = wacom->led.llv;
778 buf[3] = wacom->led.hlv;
779 buf[4] = wacom->led.img_lum;
780 }
781
782 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
783 WAC_CMD_RETRIES);
784 kfree(buf);
785
786 return retval;
787 }
788
789 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
790 const unsigned len, const void *img)
791 {
792 unsigned char *buf;
793 int i, retval;
794 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
795
796 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
797 if (!buf)
798 return -ENOMEM;
799
800 /* Send 'start' command */
801 buf[0] = WAC_CMD_ICON_START;
802 buf[1] = 1;
803 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
804 WAC_CMD_RETRIES);
805 if (retval < 0)
806 goto out;
807
808 buf[0] = xfer_id;
809 buf[1] = button_id & 0x07;
810 for (i = 0; i < 4; i++) {
811 buf[2] = i;
812 memcpy(buf + 3, img + i * chunk_len, chunk_len);
813
814 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
815 buf, chunk_len + 3, WAC_CMD_RETRIES);
816 if (retval < 0)
817 break;
818 }
819
820 /* Send 'stop' */
821 buf[0] = WAC_CMD_ICON_START;
822 buf[1] = 0;
823 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
824 WAC_CMD_RETRIES);
825
826 out:
827 kfree(buf);
828 return retval;
829 }
830
831 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
832 const char *buf, size_t count)
833 {
834 struct hid_device *hdev = to_hid_device(dev);
835 struct wacom *wacom = hid_get_drvdata(hdev);
836 unsigned int id;
837 int err;
838
839 err = kstrtouint(buf, 10, &id);
840 if (err)
841 return err;
842
843 mutex_lock(&wacom->lock);
844
845 wacom->led.groups[set_id].select = id & 0x3;
846 err = wacom_led_control(wacom);
847
848 mutex_unlock(&wacom->lock);
849
850 return err < 0 ? err : count;
851 }
852
853 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
854 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
855 struct device_attribute *attr, const char *buf, size_t count) \
856 { \
857 return wacom_led_select_store(dev, SET_ID, buf, count); \
858 } \
859 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
860 struct device_attribute *attr, char *buf) \
861 { \
862 struct hid_device *hdev = to_hid_device(dev);\
863 struct wacom *wacom = hid_get_drvdata(hdev); \
864 return scnprintf(buf, PAGE_SIZE, "%d\n", \
865 wacom->led.groups[SET_ID].select); \
866 } \
867 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
868 wacom_led##SET_ID##_select_show, \
869 wacom_led##SET_ID##_select_store)
870
871 DEVICE_LED_SELECT_ATTR(0);
872 DEVICE_LED_SELECT_ATTR(1);
873
874 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
875 const char *buf, size_t count)
876 {
877 unsigned int value;
878 int err;
879
880 err = kstrtouint(buf, 10, &value);
881 if (err)
882 return err;
883
884 mutex_lock(&wacom->lock);
885
886 *dest = value & 0x7f;
887 err = wacom_led_control(wacom);
888
889 mutex_unlock(&wacom->lock);
890
891 return err < 0 ? err : count;
892 }
893
894 #define DEVICE_LUMINANCE_ATTR(name, field) \
895 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
896 struct device_attribute *attr, const char *buf, size_t count) \
897 { \
898 struct hid_device *hdev = to_hid_device(dev);\
899 struct wacom *wacom = hid_get_drvdata(hdev); \
900 \
901 return wacom_luminance_store(wacom, &wacom->led.field, \
902 buf, count); \
903 } \
904 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
905 struct device_attribute *attr, char *buf) \
906 { \
907 struct wacom *wacom = dev_get_drvdata(dev); \
908 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
909 } \
910 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
911 wacom_##name##_luminance_show, \
912 wacom_##name##_luminance_store)
913
914 DEVICE_LUMINANCE_ATTR(status0, llv);
915 DEVICE_LUMINANCE_ATTR(status1, hlv);
916 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
917
918 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
919 const char *buf, size_t count)
920 {
921 struct hid_device *hdev = to_hid_device(dev);
922 struct wacom *wacom = hid_get_drvdata(hdev);
923 int err;
924 unsigned len;
925 u8 xfer_id;
926
927 if (hdev->bus == BUS_BLUETOOTH) {
928 len = 256;
929 xfer_id = WAC_CMD_ICON_BT_XFER;
930 } else {
931 len = 1024;
932 xfer_id = WAC_CMD_ICON_XFER;
933 }
934
935 if (count != len)
936 return -EINVAL;
937
938 mutex_lock(&wacom->lock);
939
940 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
941
942 mutex_unlock(&wacom->lock);
943
944 return err < 0 ? err : count;
945 }
946
947 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
948 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
949 struct device_attribute *attr, const char *buf, size_t count) \
950 { \
951 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
952 } \
953 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
954 NULL, wacom_btnimg##BUTTON_ID##_store)
955
956 DEVICE_BTNIMG_ATTR(0);
957 DEVICE_BTNIMG_ATTR(1);
958 DEVICE_BTNIMG_ATTR(2);
959 DEVICE_BTNIMG_ATTR(3);
960 DEVICE_BTNIMG_ATTR(4);
961 DEVICE_BTNIMG_ATTR(5);
962 DEVICE_BTNIMG_ATTR(6);
963 DEVICE_BTNIMG_ATTR(7);
964
965 static struct attribute *cintiq_led_attrs[] = {
966 &dev_attr_status_led0_select.attr,
967 &dev_attr_status_led1_select.attr,
968 NULL
969 };
970
971 static struct attribute_group cintiq_led_attr_group = {
972 .name = "wacom_led",
973 .attrs = cintiq_led_attrs,
974 };
975
976 static struct attribute *intuos4_led_attrs[] = {
977 &dev_attr_status0_luminance.attr,
978 &dev_attr_status1_luminance.attr,
979 &dev_attr_status_led0_select.attr,
980 &dev_attr_buttons_luminance.attr,
981 &dev_attr_button0_rawimg.attr,
982 &dev_attr_button1_rawimg.attr,
983 &dev_attr_button2_rawimg.attr,
984 &dev_attr_button3_rawimg.attr,
985 &dev_attr_button4_rawimg.attr,
986 &dev_attr_button5_rawimg.attr,
987 &dev_attr_button6_rawimg.attr,
988 &dev_attr_button7_rawimg.attr,
989 NULL
990 };
991
992 static struct attribute_group intuos4_led_attr_group = {
993 .name = "wacom_led",
994 .attrs = intuos4_led_attrs,
995 };
996
997 static struct attribute *intuos5_led_attrs[] = {
998 &dev_attr_status0_luminance.attr,
999 &dev_attr_status_led0_select.attr,
1000 NULL
1001 };
1002
1003 static struct attribute_group intuos5_led_attr_group = {
1004 .name = "wacom_led",
1005 .attrs = intuos5_led_attrs,
1006 };
1007
1008 struct wacom_sysfs_group_devres {
1009 struct attribute_group *group;
1010 struct kobject *root;
1011 };
1012
1013 static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1014 {
1015 struct wacom_sysfs_group_devres *devres = res;
1016 struct kobject *kobj = devres->root;
1017
1018 dev_dbg(dev, "%s: dropping reference to %s\n",
1019 __func__, devres->group->name);
1020 sysfs_remove_group(kobj, devres->group);
1021 }
1022
1023 static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1024 struct kobject *root,
1025 struct attribute_group *group)
1026 {
1027 struct wacom_sysfs_group_devres *devres;
1028 int error;
1029
1030 devres = devres_alloc(wacom_devm_sysfs_group_release,
1031 sizeof(struct wacom_sysfs_group_devres),
1032 GFP_KERNEL);
1033 if (!devres)
1034 return -ENOMEM;
1035
1036 devres->group = group;
1037 devres->root = root;
1038
1039 error = sysfs_create_group(devres->root, group);
1040 if (error)
1041 return error;
1042
1043 devres_add(&wacom->hdev->dev, devres);
1044
1045 return 0;
1046 }
1047
1048 static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1049 struct attribute_group *group)
1050 {
1051 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1052 group);
1053 }
1054
1055 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1056 {
1057 struct wacom *wacom = led->wacom;
1058
1059 if (wacom->led.max_hlv)
1060 return led->hlv * LED_FULL / wacom->led.max_hlv;
1061
1062 if (wacom->led.max_llv)
1063 return led->llv * LED_FULL / wacom->led.max_llv;
1064
1065 /* device doesn't support brightness tuning */
1066 return LED_FULL;
1067 }
1068
1069 static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1070 {
1071 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1072 struct wacom *wacom = led->wacom;
1073
1074 if (wacom->led.groups[led->group].select != led->id)
1075 return LED_OFF;
1076
1077 return wacom_leds_brightness_get(led);
1078 }
1079
1080 static int wacom_led_brightness_set(struct led_classdev *cdev,
1081 enum led_brightness brightness)
1082 {
1083 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1084 struct wacom *wacom = led->wacom;
1085 int error;
1086
1087 mutex_lock(&wacom->lock);
1088
1089 if (!wacom->led.groups || (brightness == LED_OFF &&
1090 wacom->led.groups[led->group].select != led->id)) {
1091 error = 0;
1092 goto out;
1093 }
1094
1095 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1096 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1097
1098 wacom->led.groups[led->group].select = led->id;
1099
1100 error = wacom_led_control(wacom);
1101
1102 out:
1103 mutex_unlock(&wacom->lock);
1104
1105 return error;
1106 }
1107
1108 static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1109 enum led_brightness brightness)
1110 {
1111 }
1112
1113 static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1114 struct wacom_led *led, unsigned int group,
1115 unsigned int id, bool read_only)
1116 {
1117 int error;
1118 char *name;
1119
1120 name = devm_kasprintf(dev, GFP_KERNEL,
1121 "%s::wacom-%d.%d",
1122 dev_name(dev),
1123 group,
1124 id);
1125 if (!name)
1126 return -ENOMEM;
1127
1128 if (!read_only) {
1129 led->trigger.name = name;
1130 error = devm_led_trigger_register(dev, &led->trigger);
1131 if (error) {
1132 hid_err(wacom->hdev,
1133 "failed to register LED trigger %s: %d\n",
1134 led->cdev.name, error);
1135 return error;
1136 }
1137 }
1138
1139 led->group = group;
1140 led->id = id;
1141 led->wacom = wacom;
1142 led->llv = wacom->led.llv;
1143 led->hlv = wacom->led.hlv;
1144 led->cdev.name = name;
1145 led->cdev.max_brightness = LED_FULL;
1146 led->cdev.flags = LED_HW_PLUGGABLE;
1147 led->cdev.brightness_get = __wacom_led_brightness_get;
1148 if (!read_only) {
1149 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
1150 led->cdev.default_trigger = led->cdev.name;
1151 } else {
1152 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
1153 }
1154
1155 error = devm_led_classdev_register(dev, &led->cdev);
1156 if (error) {
1157 hid_err(wacom->hdev,
1158 "failed to register LED %s: %d\n",
1159 led->cdev.name, error);
1160 led->cdev.name = NULL;
1161 return error;
1162 }
1163
1164 return 0;
1165 }
1166
1167 static void wacom_led_groups_release_one(void *data)
1168 {
1169 struct wacom_group_leds *group = data;
1170
1171 devres_release_group(group->dev, group);
1172 }
1173
1174 static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1175 struct wacom *wacom,
1176 int group_id, int count,
1177 bool read_only)
1178 {
1179 struct wacom_led *leds;
1180 int i, error;
1181
1182 if (group_id >= wacom->led.count || count <= 0)
1183 return -EINVAL;
1184
1185 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1186 return -ENOMEM;
1187
1188 leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1189 if (!leds) {
1190 error = -ENOMEM;
1191 goto err;
1192 }
1193
1194 wacom->led.groups[group_id].leds = leds;
1195 wacom->led.groups[group_id].count = count;
1196
1197 for (i = 0; i < count; i++) {
1198 error = wacom_led_register_one(dev, wacom, &leds[i],
1199 group_id, i, read_only);
1200 if (error)
1201 goto err;
1202 }
1203
1204 wacom->led.groups[group_id].dev = dev;
1205
1206 devres_close_group(dev, &wacom->led.groups[group_id]);
1207
1208 /*
1209 * There is a bug (?) in devm_led_classdev_register() in which its
1210 * increments the refcount of the parent. If the parent is an input
1211 * device, that means the ref count never reaches 0 when
1212 * devm_input_device_release() gets called.
1213 * This means that the LEDs are still there after disconnect.
1214 * Manually force the release of the group so that the leds are released
1215 * once we are done using them.
1216 */
1217 error = devm_add_action_or_reset(&wacom->hdev->dev,
1218 wacom_led_groups_release_one,
1219 &wacom->led.groups[group_id]);
1220 if (error)
1221 return error;
1222
1223 return 0;
1224
1225 err:
1226 devres_release_group(dev, &wacom->led.groups[group_id]);
1227 return error;
1228 }
1229
1230 struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1231 unsigned int id)
1232 {
1233 struct wacom_group_leds *group;
1234
1235 if (group_id >= wacom->led.count)
1236 return NULL;
1237
1238 group = &wacom->led.groups[group_id];
1239
1240 if (!group->leds)
1241 return NULL;
1242
1243 id %= group->count;
1244
1245 return &group->leds[id];
1246 }
1247
1248 /**
1249 * wacom_led_next: gives the next available led with a wacom trigger.
1250 *
1251 * returns the next available struct wacom_led which has its default trigger
1252 * or the current one if none is available.
1253 */
1254 struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1255 {
1256 struct wacom_led *next_led;
1257 int group, next;
1258
1259 if (!wacom || !cur)
1260 return NULL;
1261
1262 group = cur->group;
1263 next = cur->id;
1264
1265 do {
1266 next_led = wacom_led_find(wacom, group, ++next);
1267 if (!next_led || next_led == cur)
1268 return next_led;
1269 } while (next_led->cdev.trigger != &next_led->trigger);
1270
1271 return next_led;
1272 }
1273
1274 static void wacom_led_groups_release(void *data)
1275 {
1276 struct wacom *wacom = data;
1277
1278 wacom->led.groups = NULL;
1279 wacom->led.count = 0;
1280 }
1281
1282 static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1283 {
1284 struct device *dev = &wacom->hdev->dev;
1285 struct wacom_group_leds *groups;
1286 int error;
1287
1288 groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
1289 GFP_KERNEL);
1290 if (!groups)
1291 return -ENOMEM;
1292
1293 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1294 if (error)
1295 return error;
1296
1297 wacom->led.groups = groups;
1298 wacom->led.count = count;
1299
1300 return 0;
1301 }
1302
1303 static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1304 int led_per_group, bool read_only)
1305 {
1306 struct device *dev;
1307 int i, error;
1308
1309 if (!wacom->wacom_wac.pad_input)
1310 return -EINVAL;
1311
1312 dev = &wacom->wacom_wac.pad_input->dev;
1313
1314 error = wacom_led_groups_allocate(wacom, group_count);
1315 if (error)
1316 return error;
1317
1318 for (i = 0; i < group_count; i++) {
1319 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1320 led_per_group,
1321 read_only);
1322 if (error)
1323 return error;
1324 }
1325
1326 return 0;
1327 }
1328
1329 static int wacom_initialize_leds(struct wacom *wacom)
1330 {
1331 int error;
1332
1333 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1334 return 0;
1335
1336 /* Initialize default values */
1337 switch (wacom->wacom_wac.features.type) {
1338 case INTUOS4S:
1339 case INTUOS4:
1340 case INTUOS4WL:
1341 case INTUOS4L:
1342 wacom->led.llv = 10;
1343 wacom->led.hlv = 20;
1344 wacom->led.max_llv = 127;
1345 wacom->led.max_hlv = 127;
1346 wacom->led.img_lum = 10;
1347
1348 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1349 if (error) {
1350 hid_err(wacom->hdev,
1351 "cannot create leds err: %d\n", error);
1352 return error;
1353 }
1354
1355 error = wacom_devm_sysfs_create_group(wacom,
1356 &intuos4_led_attr_group);
1357 break;
1358
1359 case WACOM_24HD:
1360 case WACOM_21UX2:
1361 wacom->led.llv = 0;
1362 wacom->led.hlv = 0;
1363 wacom->led.img_lum = 0;
1364
1365 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1366 if (error) {
1367 hid_err(wacom->hdev,
1368 "cannot create leds err: %d\n", error);
1369 return error;
1370 }
1371
1372 error = wacom_devm_sysfs_create_group(wacom,
1373 &cintiq_led_attr_group);
1374 break;
1375
1376 case INTUOS5S:
1377 case INTUOS5:
1378 case INTUOS5L:
1379 case INTUOSPS:
1380 case INTUOSPM:
1381 case INTUOSPL:
1382 wacom->led.llv = 32;
1383 wacom->led.max_llv = 96;
1384
1385 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1386 if (error) {
1387 hid_err(wacom->hdev,
1388 "cannot create leds err: %d\n", error);
1389 return error;
1390 }
1391
1392 error = wacom_devm_sysfs_create_group(wacom,
1393 &intuos5_led_attr_group);
1394 break;
1395
1396 case REMOTE:
1397 wacom->led.llv = 255;
1398 wacom->led.max_llv = 255;
1399 error = wacom_led_groups_allocate(wacom, 5);
1400 if (error) {
1401 hid_err(wacom->hdev,
1402 "cannot create leds err: %d\n", error);
1403 return error;
1404 }
1405 return 0;
1406
1407 default:
1408 return 0;
1409 }
1410
1411 if (error) {
1412 hid_err(wacom->hdev,
1413 "cannot create sysfs group err: %d\n", error);
1414 return error;
1415 }
1416 wacom_led_control(wacom);
1417
1418 return 0;
1419 }
1420
1421 static enum power_supply_property wacom_battery_props[] = {
1422 POWER_SUPPLY_PROP_MODEL_NAME,
1423 POWER_SUPPLY_PROP_PRESENT,
1424 POWER_SUPPLY_PROP_STATUS,
1425 POWER_SUPPLY_PROP_SCOPE,
1426 POWER_SUPPLY_PROP_CAPACITY
1427 };
1428
1429 static int wacom_battery_get_property(struct power_supply *psy,
1430 enum power_supply_property psp,
1431 union power_supply_propval *val)
1432 {
1433 struct wacom_battery *battery = power_supply_get_drvdata(psy);
1434 int ret = 0;
1435
1436 switch (psp) {
1437 case POWER_SUPPLY_PROP_MODEL_NAME:
1438 val->strval = battery->wacom->wacom_wac.name;
1439 break;
1440 case POWER_SUPPLY_PROP_PRESENT:
1441 val->intval = battery->bat_connected;
1442 break;
1443 case POWER_SUPPLY_PROP_SCOPE:
1444 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1445 break;
1446 case POWER_SUPPLY_PROP_CAPACITY:
1447 val->intval = battery->battery_capacity;
1448 break;
1449 case POWER_SUPPLY_PROP_STATUS:
1450 if (battery->bat_charging)
1451 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1452 else if (battery->battery_capacity == 100 &&
1453 battery->ps_connected)
1454 val->intval = POWER_SUPPLY_STATUS_FULL;
1455 else if (battery->ps_connected)
1456 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1457 else
1458 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1459 break;
1460 default:
1461 ret = -EINVAL;
1462 break;
1463 }
1464
1465 return ret;
1466 }
1467
1468 static int __wacom_initialize_battery(struct wacom *wacom,
1469 struct wacom_battery *battery)
1470 {
1471 static atomic_t battery_no = ATOMIC_INIT(0);
1472 struct device *dev = &wacom->hdev->dev;
1473 struct power_supply_config psy_cfg = { .drv_data = battery, };
1474 struct power_supply *ps_bat;
1475 struct power_supply_desc *bat_desc = &battery->bat_desc;
1476 unsigned long n;
1477 int error;
1478
1479 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1480 return -ENOMEM;
1481
1482 battery->wacom = wacom;
1483
1484 n = atomic_inc_return(&battery_no) - 1;
1485
1486 bat_desc->properties = wacom_battery_props;
1487 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1488 bat_desc->get_property = wacom_battery_get_property;
1489 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1490 bat_desc->name = battery->bat_name;
1491 bat_desc->type = POWER_SUPPLY_TYPE_USB;
1492 bat_desc->use_for_apm = 0;
1493
1494 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1495 if (IS_ERR(ps_bat)) {
1496 error = PTR_ERR(ps_bat);
1497 goto err;
1498 }
1499
1500 power_supply_powers(ps_bat, &wacom->hdev->dev);
1501
1502 battery->battery = ps_bat;
1503
1504 devres_close_group(dev, bat_desc);
1505 return 0;
1506
1507 err:
1508 devres_release_group(dev, bat_desc);
1509 return error;
1510 }
1511
1512 static int wacom_initialize_battery(struct wacom *wacom)
1513 {
1514 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1515 return __wacom_initialize_battery(wacom, &wacom->battery);
1516
1517 return 0;
1518 }
1519
1520 static void wacom_destroy_battery(struct wacom *wacom)
1521 {
1522 if (wacom->battery.battery) {
1523 devres_release_group(&wacom->hdev->dev,
1524 &wacom->battery.bat_desc);
1525 wacom->battery.battery = NULL;
1526 }
1527 }
1528
1529 static ssize_t wacom_show_speed(struct device *dev,
1530 struct device_attribute
1531 *attr, char *buf)
1532 {
1533 struct hid_device *hdev = to_hid_device(dev);
1534 struct wacom *wacom = hid_get_drvdata(hdev);
1535
1536 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1537 }
1538
1539 static ssize_t wacom_store_speed(struct device *dev,
1540 struct device_attribute *attr,
1541 const char *buf, size_t count)
1542 {
1543 struct hid_device *hdev = to_hid_device(dev);
1544 struct wacom *wacom = hid_get_drvdata(hdev);
1545 u8 new_speed;
1546
1547 if (kstrtou8(buf, 0, &new_speed))
1548 return -EINVAL;
1549
1550 if (new_speed != 0 && new_speed != 1)
1551 return -EINVAL;
1552
1553 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1554
1555 return count;
1556 }
1557
1558 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1559 wacom_show_speed, wacom_store_speed);
1560
1561
1562 static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1563 struct kobj_attribute *kattr,
1564 char *buf, int index)
1565 {
1566 struct device *dev = kobj_to_dev(kobj->parent);
1567 struct hid_device *hdev = to_hid_device(dev);
1568 struct wacom *wacom = hid_get_drvdata(hdev);
1569 u8 mode;
1570
1571 mode = wacom->led.groups[index].select;
1572 if (mode >= 0 && mode < 3)
1573 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
1574 else
1575 return snprintf(buf, PAGE_SIZE, "%d\n", -1);
1576 }
1577
1578 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1579 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1580 struct kobj_attribute *kattr, char *buf) \
1581 { \
1582 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1583 } \
1584 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1585 .attr = {.name = "remote_mode", \
1586 .mode = DEV_ATTR_RO_PERM}, \
1587 .show = wacom_show_remote##SET_ID##_mode, \
1588 }; \
1589 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1590 &remote##SET_ID##_mode_attr.attr, \
1591 NULL \
1592 }; \
1593 static struct attribute_group remote##SET_ID##_serial_group = { \
1594 .name = NULL, \
1595 .attrs = remote##SET_ID##_serial_attrs, \
1596 }
1597
1598 DEVICE_EKR_ATTR_GROUP(0);
1599 DEVICE_EKR_ATTR_GROUP(1);
1600 DEVICE_EKR_ATTR_GROUP(2);
1601 DEVICE_EKR_ATTR_GROUP(3);
1602 DEVICE_EKR_ATTR_GROUP(4);
1603
1604 static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1605 int index)
1606 {
1607 int error = 0;
1608 struct wacom_remote *remote = wacom->remote;
1609
1610 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1611 GFP_KERNEL,
1612 "%d", serial);
1613 if (!remote->remotes[index].group.name)
1614 return -ENOMEM;
1615
1616 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1617 &remote->remotes[index].group);
1618 if (error) {
1619 remote->remotes[index].group.name = NULL;
1620 hid_err(wacom->hdev,
1621 "cannot create sysfs group err: %d\n", error);
1622 return error;
1623 }
1624
1625 return 0;
1626 }
1627
1628 static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1629 {
1630 const size_t buf_size = 2;
1631 unsigned char *buf;
1632 int retval;
1633
1634 buf = kzalloc(buf_size, GFP_KERNEL);
1635 if (!buf)
1636 return -ENOMEM;
1637
1638 buf[0] = WAC_CMD_DELETE_PAIRING;
1639 buf[1] = selector;
1640
1641 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1642 buf_size, WAC_CMD_RETRIES);
1643 kfree(buf);
1644
1645 return retval;
1646 }
1647
1648 static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1649 struct kobj_attribute *attr,
1650 const char *buf, size_t count)
1651 {
1652 unsigned char selector = 0;
1653 struct device *dev = kobj_to_dev(kobj->parent);
1654 struct hid_device *hdev = to_hid_device(dev);
1655 struct wacom *wacom = hid_get_drvdata(hdev);
1656 int err;
1657
1658 if (!strncmp(buf, "*\n", 2)) {
1659 selector = WAC_CMD_UNPAIR_ALL;
1660 } else {
1661 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1662 buf);
1663 return -1;
1664 }
1665
1666 mutex_lock(&wacom->lock);
1667
1668 err = wacom_cmd_unpair_remote(wacom, selector);
1669 mutex_unlock(&wacom->lock);
1670
1671 return err < 0 ? err : count;
1672 }
1673
1674 static struct kobj_attribute unpair_remote_attr = {
1675 .attr = {.name = "unpair_remote", .mode = 0200},
1676 .store = wacom_store_unpair_remote,
1677 };
1678
1679 static const struct attribute *remote_unpair_attrs[] = {
1680 &unpair_remote_attr.attr,
1681 NULL
1682 };
1683
1684 static void wacom_remotes_destroy(void *data)
1685 {
1686 struct wacom *wacom = data;
1687 struct wacom_remote *remote = wacom->remote;
1688
1689 if (!remote)
1690 return;
1691
1692 kobject_put(remote->remote_dir);
1693 kfifo_free(&remote->remote_fifo);
1694 wacom->remote = NULL;
1695 }
1696
1697 static int wacom_initialize_remotes(struct wacom *wacom)
1698 {
1699 int error = 0;
1700 struct wacom_remote *remote;
1701 int i;
1702
1703 if (wacom->wacom_wac.features.type != REMOTE)
1704 return 0;
1705
1706 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1707 GFP_KERNEL);
1708 if (!remote)
1709 return -ENOMEM;
1710
1711 wacom->remote = remote;
1712
1713 spin_lock_init(&remote->remote_lock);
1714
1715 error = kfifo_alloc(&remote->remote_fifo,
1716 5 * sizeof(struct wacom_remote_data),
1717 GFP_KERNEL);
1718 if (error) {
1719 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1720 return -ENOMEM;
1721 }
1722
1723 remote->remotes[0].group = remote0_serial_group;
1724 remote->remotes[1].group = remote1_serial_group;
1725 remote->remotes[2].group = remote2_serial_group;
1726 remote->remotes[3].group = remote3_serial_group;
1727 remote->remotes[4].group = remote4_serial_group;
1728
1729 remote->remote_dir = kobject_create_and_add("wacom_remote",
1730 &wacom->hdev->dev.kobj);
1731 if (!remote->remote_dir)
1732 return -ENOMEM;
1733
1734 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
1735
1736 if (error) {
1737 hid_err(wacom->hdev,
1738 "cannot create sysfs group err: %d\n", error);
1739 return error;
1740 }
1741
1742 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1743 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
1744 remote->remotes[i].serial = 0;
1745 }
1746
1747 error = devm_add_action_or_reset(&wacom->hdev->dev,
1748 wacom_remotes_destroy, wacom);
1749 if (error)
1750 return error;
1751
1752 return 0;
1753 }
1754
1755 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1756 {
1757 struct input_dev *input_dev;
1758 struct hid_device *hdev = wacom->hdev;
1759 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1760
1761 input_dev = devm_input_allocate_device(&hdev->dev);
1762 if (!input_dev)
1763 return NULL;
1764
1765 input_dev->name = wacom_wac->features.name;
1766 input_dev->phys = hdev->phys;
1767 input_dev->dev.parent = &hdev->dev;
1768 input_dev->open = wacom_open;
1769 input_dev->close = wacom_close;
1770 input_dev->uniq = hdev->uniq;
1771 input_dev->id.bustype = hdev->bus;
1772 input_dev->id.vendor = hdev->vendor;
1773 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1774 input_dev->id.version = hdev->version;
1775 input_set_drvdata(input_dev, wacom);
1776
1777 return input_dev;
1778 }
1779
1780 static int wacom_allocate_inputs(struct wacom *wacom)
1781 {
1782 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1783
1784 wacom_wac->pen_input = wacom_allocate_input(wacom);
1785 wacom_wac->touch_input = wacom_allocate_input(wacom);
1786 wacom_wac->pad_input = wacom_allocate_input(wacom);
1787 if (!wacom_wac->pen_input ||
1788 !wacom_wac->touch_input ||
1789 !wacom_wac->pad_input)
1790 return -ENOMEM;
1791
1792 wacom_wac->pen_input->name = wacom_wac->pen_name;
1793 wacom_wac->touch_input->name = wacom_wac->touch_name;
1794 wacom_wac->pad_input->name = wacom_wac->pad_name;
1795
1796 return 0;
1797 }
1798
1799 static int wacom_register_inputs(struct wacom *wacom)
1800 {
1801 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
1802 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1803 int error = 0;
1804
1805 pen_input_dev = wacom_wac->pen_input;
1806 touch_input_dev = wacom_wac->touch_input;
1807 pad_input_dev = wacom_wac->pad_input;
1808
1809 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
1810 return -EINVAL;
1811
1812 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1813 if (error) {
1814 /* no pen in use on this interface */
1815 input_free_device(pen_input_dev);
1816 wacom_wac->pen_input = NULL;
1817 pen_input_dev = NULL;
1818 } else {
1819 error = input_register_device(pen_input_dev);
1820 if (error)
1821 goto fail;
1822 }
1823
1824 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1825 if (error) {
1826 /* no touch in use on this interface */
1827 input_free_device(touch_input_dev);
1828 wacom_wac->touch_input = NULL;
1829 touch_input_dev = NULL;
1830 } else {
1831 error = input_register_device(touch_input_dev);
1832 if (error)
1833 goto fail;
1834 }
1835
1836 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1837 if (error) {
1838 /* no pad in use on this interface */
1839 input_free_device(pad_input_dev);
1840 wacom_wac->pad_input = NULL;
1841 pad_input_dev = NULL;
1842 } else {
1843 error = input_register_device(pad_input_dev);
1844 if (error)
1845 goto fail;
1846 }
1847
1848 return 0;
1849
1850 fail:
1851 wacom_wac->pad_input = NULL;
1852 wacom_wac->touch_input = NULL;
1853 wacom_wac->pen_input = NULL;
1854 return error;
1855 }
1856
1857 /*
1858 * Not all devices report physical dimensions from HID.
1859 * Compute the default from hardcoded logical dimension
1860 * and resolution before driver overwrites them.
1861 */
1862 static void wacom_set_default_phy(struct wacom_features *features)
1863 {
1864 if (features->x_resolution) {
1865 features->x_phy = (features->x_max * 100) /
1866 features->x_resolution;
1867 features->y_phy = (features->y_max * 100) /
1868 features->y_resolution;
1869 }
1870 }
1871
1872 static void wacom_calculate_res(struct wacom_features *features)
1873 {
1874 /* set unit to "100th of a mm" for devices not reported by HID */
1875 if (!features->unit) {
1876 features->unit = 0x11;
1877 features->unitExpo = -3;
1878 }
1879
1880 features->x_resolution = wacom_calc_hid_res(features->x_max,
1881 features->x_phy,
1882 features->unit,
1883 features->unitExpo);
1884 features->y_resolution = wacom_calc_hid_res(features->y_max,
1885 features->y_phy,
1886 features->unit,
1887 features->unitExpo);
1888 }
1889
1890 void wacom_battery_work(struct work_struct *work)
1891 {
1892 struct wacom *wacom = container_of(work, struct wacom, battery_work);
1893
1894 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1895 !wacom->battery.battery) {
1896 wacom_initialize_battery(wacom);
1897 }
1898 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1899 wacom->battery.battery) {
1900 wacom_destroy_battery(wacom);
1901 }
1902 }
1903
1904 static size_t wacom_compute_pktlen(struct hid_device *hdev)
1905 {
1906 struct hid_report_enum *report_enum;
1907 struct hid_report *report;
1908 size_t size = 0;
1909
1910 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1911
1912 list_for_each_entry(report, &report_enum->report_list, list) {
1913 size_t report_size = hid_report_len(report);
1914 if (report_size > size)
1915 size = report_size;
1916 }
1917
1918 return size;
1919 }
1920
1921 static void wacom_update_name(struct wacom *wacom, const char *suffix)
1922 {
1923 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1924 struct wacom_features *features = &wacom_wac->features;
1925 char name[WACOM_NAME_MAX];
1926
1927 /* Generic devices name unspecified */
1928 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1929 if (strstr(wacom->hdev->name, "Wacom") ||
1930 strstr(wacom->hdev->name, "wacom") ||
1931 strstr(wacom->hdev->name, "WACOM")) {
1932 /* name is in HID descriptor, use it */
1933 strlcpy(name, wacom->hdev->name, sizeof(name));
1934
1935 /* strip out excess whitespaces */
1936 while (1) {
1937 char *gap = strstr(name, " ");
1938 if (gap == NULL)
1939 break;
1940 /* shift everything including the terminator */
1941 memmove(gap, gap+1, strlen(gap));
1942 }
1943 /* get rid of trailing whitespace */
1944 if (name[strlen(name)-1] == ' ')
1945 name[strlen(name)-1] = '\0';
1946 } else {
1947 /* no meaningful name retrieved. use product ID */
1948 snprintf(name, sizeof(name),
1949 "%s %X", features->name, wacom->hdev->product);
1950 }
1951 } else {
1952 strlcpy(name, features->name, sizeof(name));
1953 }
1954
1955 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
1956 name, suffix);
1957
1958 /* Append the device type to the name */
1959 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
1960 "%s%s Pen", name, suffix);
1961 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
1962 "%s%s Finger", name, suffix);
1963 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1964 "%s%s Pad", name, suffix);
1965 }
1966
1967 static void wacom_release_resources(struct wacom *wacom)
1968 {
1969 struct hid_device *hdev = wacom->hdev;
1970
1971 if (!wacom->resources)
1972 return;
1973
1974 devres_release_group(&hdev->dev, wacom);
1975
1976 wacom->resources = false;
1977
1978 wacom->wacom_wac.pen_input = NULL;
1979 wacom->wacom_wac.touch_input = NULL;
1980 wacom->wacom_wac.pad_input = NULL;
1981 }
1982
1983 static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
1984 {
1985 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1986 struct wacom_features *features = &wacom_wac->features;
1987 struct hid_device *hdev = wacom->hdev;
1988 int error;
1989 unsigned int connect_mask = HID_CONNECT_HIDRAW;
1990
1991 features->pktlen = wacom_compute_pktlen(hdev);
1992 if (features->pktlen > WACOM_PKGLEN_MAX)
1993 return -EINVAL;
1994
1995 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
1996 return -ENOMEM;
1997
1998 wacom->resources = true;
1999
2000 error = wacom_allocate_inputs(wacom);
2001 if (error)
2002 goto fail;
2003
2004 /*
2005 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2006 * into debug mode for the touch part.
2007 * We ignore the other interfaces.
2008 */
2009 if (features->type == BAMBOO_PAD) {
2010 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2011 features->type = HID_GENERIC;
2012 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2013 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2014 error = -ENODEV;
2015 goto fail;
2016 }
2017 }
2018
2019 /* set the default size in case we do not get them from hid */
2020 wacom_set_default_phy(features);
2021
2022 /* Retrieve the physical and logical size for touch devices */
2023 wacom_retrieve_hid_descriptor(hdev, features);
2024 wacom_setup_device_quirks(wacom);
2025
2026 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2027 features->type != WIRELESS) {
2028 error = features->type == HID_GENERIC ? -ENODEV : 0;
2029
2030 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2031 hdev->name,
2032 error ? "Ignoring" : "Assuming pen");
2033
2034 if (error)
2035 goto fail;
2036
2037 features->device_type |= WACOM_DEVICETYPE_PEN;
2038 }
2039
2040 wacom_calculate_res(features);
2041
2042 wacom_update_name(wacom, wireless ? " (WL)" : "");
2043
2044 error = wacom_add_shared_data(hdev);
2045 if (error)
2046 goto fail;
2047
2048 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2049 (features->quirks & WACOM_QUIRK_BATTERY)) {
2050 error = wacom_initialize_battery(wacom);
2051 if (error)
2052 goto fail;
2053 }
2054
2055 error = wacom_register_inputs(wacom);
2056 if (error)
2057 goto fail;
2058
2059 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2060 error = wacom_initialize_leds(wacom);
2061 if (error)
2062 goto fail;
2063
2064 error = wacom_initialize_remotes(wacom);
2065 if (error)
2066 goto fail;
2067 }
2068
2069 if (features->type == HID_GENERIC)
2070 connect_mask |= HID_CONNECT_DRIVER;
2071
2072 /* Regular HID work starts now */
2073 error = hid_hw_start(hdev, connect_mask);
2074 if (error) {
2075 hid_err(hdev, "hw start failed\n");
2076 goto fail;
2077 }
2078
2079 if (!wireless) {
2080 /* Note that if query fails it is not a hard failure */
2081 wacom_query_tablet_data(hdev, features);
2082 }
2083
2084 /* touch only Bamboo doesn't support pen */
2085 if ((features->type == BAMBOO_TOUCH) &&
2086 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2087 error = -ENODEV;
2088 goto fail_quirks;
2089 }
2090
2091 /* pen only Bamboo neither support touch nor pad */
2092 if ((features->type == BAMBOO_PEN) &&
2093 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2094 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2095 error = -ENODEV;
2096 goto fail_quirks;
2097 }
2098
2099 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2100 error = hid_hw_open(hdev);
2101
2102 if ((wacom_wac->features.type == INTUOSHT ||
2103 wacom_wac->features.type == INTUOSHT2) &&
2104 (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
2105 wacom_wac->shared->type = wacom_wac->features.type;
2106 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2107 }
2108
2109 devres_close_group(&hdev->dev, wacom);
2110
2111 return 0;
2112
2113 fail_quirks:
2114 hid_hw_stop(hdev);
2115 fail:
2116 wacom_release_resources(wacom);
2117 return error;
2118 }
2119
2120 static void wacom_wireless_work(struct work_struct *work)
2121 {
2122 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2123 struct usb_device *usbdev = wacom->usbdev;
2124 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2125 struct hid_device *hdev1, *hdev2;
2126 struct wacom *wacom1, *wacom2;
2127 struct wacom_wac *wacom_wac1, *wacom_wac2;
2128 int error;
2129
2130 /*
2131 * Regardless if this is a disconnect or a new tablet,
2132 * remove any existing input and battery devices.
2133 */
2134
2135 wacom_destroy_battery(wacom);
2136
2137 /* Stylus interface */
2138 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2139 wacom1 = hid_get_drvdata(hdev1);
2140 wacom_wac1 = &(wacom1->wacom_wac);
2141 wacom_release_resources(wacom1);
2142
2143 /* Touch interface */
2144 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2145 wacom2 = hid_get_drvdata(hdev2);
2146 wacom_wac2 = &(wacom2->wacom_wac);
2147 wacom_release_resources(wacom2);
2148
2149 if (wacom_wac->pid == 0) {
2150 hid_info(wacom->hdev, "wireless tablet disconnected\n");
2151 } else {
2152 const struct hid_device_id *id = wacom_ids;
2153
2154 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2155 wacom_wac->pid);
2156
2157 while (id->bus) {
2158 if (id->vendor == USB_VENDOR_ID_WACOM &&
2159 id->product == wacom_wac->pid)
2160 break;
2161 id++;
2162 }
2163
2164 if (!id->bus) {
2165 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2166 return;
2167 }
2168
2169 /* Stylus interface */
2170 wacom_wac1->features =
2171 *((struct wacom_features *)id->driver_data);
2172
2173 wacom_wac1->pid = wacom_wac->pid;
2174 hid_hw_stop(hdev1);
2175 error = wacom_parse_and_register(wacom1, true);
2176 if (error)
2177 goto fail;
2178
2179 /* Touch interface */
2180 if (wacom_wac1->features.touch_max ||
2181 (wacom_wac1->features.type >= INTUOSHT &&
2182 wacom_wac1->features.type <= BAMBOO_PT)) {
2183 wacom_wac2->features =
2184 *((struct wacom_features *)id->driver_data);
2185 wacom_wac2->pid = wacom_wac->pid;
2186 hid_hw_stop(hdev2);
2187 error = wacom_parse_and_register(wacom2, true);
2188 if (error)
2189 goto fail;
2190 }
2191
2192 strlcpy(wacom_wac->name, wacom_wac1->name,
2193 sizeof(wacom_wac->name));
2194 error = wacom_initialize_battery(wacom);
2195 if (error)
2196 goto fail;
2197 }
2198
2199 return;
2200
2201 fail:
2202 wacom_release_resources(wacom1);
2203 wacom_release_resources(wacom2);
2204 return;
2205 }
2206
2207 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2208 {
2209 struct wacom_remote *remote = wacom->remote;
2210 u32 serial = remote->remotes[index].serial;
2211 int i;
2212 unsigned long flags;
2213
2214 spin_lock_irqsave(&remote->remote_lock, flags);
2215 remote->remotes[index].registered = false;
2216 spin_unlock_irqrestore(&remote->remote_lock, flags);
2217
2218 if (remote->remotes[index].battery.battery)
2219 devres_release_group(&wacom->hdev->dev,
2220 &remote->remotes[index].battery.bat_desc);
2221
2222 if (remote->remotes[index].group.name)
2223 devres_release_group(&wacom->hdev->dev,
2224 &remote->remotes[index]);
2225
2226 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2227 if (remote->remotes[i].serial == serial) {
2228 remote->remotes[i].serial = 0;
2229 remote->remotes[i].group.name = NULL;
2230 remote->remotes[i].registered = false;
2231 remote->remotes[i].battery.battery = NULL;
2232 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2233 }
2234 }
2235 }
2236
2237 static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2238 unsigned int index)
2239 {
2240 struct wacom_remote *remote = wacom->remote;
2241 struct device *dev = &wacom->hdev->dev;
2242 int error, k;
2243
2244 /* A remote can pair more than once with an EKR,
2245 * check to make sure this serial isn't already paired.
2246 */
2247 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2248 if (remote->remotes[k].serial == serial)
2249 break;
2250 }
2251
2252 if (k < WACOM_MAX_REMOTES) {
2253 remote->remotes[index].serial = serial;
2254 return 0;
2255 }
2256
2257 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2258 return -ENOMEM;
2259
2260 error = wacom_remote_create_attr_group(wacom, serial, index);
2261 if (error)
2262 goto fail;
2263
2264 remote->remotes[index].input = wacom_allocate_input(wacom);
2265 if (!remote->remotes[index].input) {
2266 error = -ENOMEM;
2267 goto fail;
2268 }
2269 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2270 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2271
2272 if (!remote->remotes[index].input->name) {
2273 error = -EINVAL;
2274 goto fail;
2275 }
2276
2277 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2278 &wacom->wacom_wac);
2279 if (error)
2280 goto fail;
2281
2282 remote->remotes[index].serial = serial;
2283
2284 error = input_register_device(remote->remotes[index].input);
2285 if (error)
2286 goto fail;
2287
2288 error = wacom_led_groups_alloc_and_register_one(
2289 &remote->remotes[index].input->dev,
2290 wacom, index, 3, true);
2291 if (error)
2292 goto fail;
2293
2294 remote->remotes[index].registered = true;
2295
2296 devres_close_group(dev, &remote->remotes[index]);
2297 return 0;
2298
2299 fail:
2300 devres_release_group(dev, &remote->remotes[index]);
2301 remote->remotes[index].serial = 0;
2302 return error;
2303 }
2304
2305 static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2306 {
2307 struct wacom_remote *remote = wacom->remote;
2308 int error;
2309
2310 if (!remote->remotes[index].registered)
2311 return 0;
2312
2313 if (remote->remotes[index].battery.battery)
2314 return 0;
2315
2316 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2317 return 0;
2318
2319 error = __wacom_initialize_battery(wacom,
2320 &wacom->remote->remotes[index].battery);
2321 if (error)
2322 return error;
2323
2324 return 0;
2325 }
2326
2327 static void wacom_remote_work(struct work_struct *work)
2328 {
2329 struct wacom *wacom = container_of(work, struct wacom, remote_work);
2330 struct wacom_remote *remote = wacom->remote;
2331 struct wacom_remote_data data;
2332 unsigned long flags;
2333 unsigned int count;
2334 u32 serial;
2335 int i;
2336
2337 spin_lock_irqsave(&remote->remote_lock, flags);
2338
2339 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2340
2341 if (count != sizeof(data)) {
2342 hid_err(wacom->hdev,
2343 "workitem triggered without status available\n");
2344 spin_unlock_irqrestore(&remote->remote_lock, flags);
2345 return;
2346 }
2347
2348 if (!kfifo_is_empty(&remote->remote_fifo))
2349 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2350
2351 spin_unlock_irqrestore(&remote->remote_lock, flags);
2352
2353 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2354 serial = data.remote[i].serial;
2355 if (data.remote[i].connected) {
2356
2357 if (remote->remotes[i].serial == serial) {
2358 wacom_remote_attach_battery(wacom, i);
2359 continue;
2360 }
2361
2362 if (remote->remotes[i].serial)
2363 wacom_remote_destroy_one(wacom, i);
2364
2365 wacom_remote_create_one(wacom, serial, i);
2366
2367 } else if (remote->remotes[i].serial) {
2368 wacom_remote_destroy_one(wacom, i);
2369 }
2370 }
2371 }
2372
2373 static int wacom_probe(struct hid_device *hdev,
2374 const struct hid_device_id *id)
2375 {
2376 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2377 struct usb_device *dev = interface_to_usbdev(intf);
2378 struct wacom *wacom;
2379 struct wacom_wac *wacom_wac;
2380 struct wacom_features *features;
2381 int error;
2382
2383 if (!id->driver_data)
2384 return -EINVAL;
2385
2386 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2387
2388 /* hid-core sets this quirk for the boot interface */
2389 hdev->quirks &= ~HID_QUIRK_NOGET;
2390
2391 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2392 if (!wacom)
2393 return -ENOMEM;
2394
2395 hid_set_drvdata(hdev, wacom);
2396 wacom->hdev = hdev;
2397
2398 wacom_wac = &wacom->wacom_wac;
2399 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2400 features = &wacom_wac->features;
2401
2402 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2403 error = -ENODEV;
2404 goto fail;
2405 }
2406
2407 wacom_wac->hid_data.inputmode = -1;
2408 wacom_wac->mode_report = -1;
2409
2410 wacom->usbdev = dev;
2411 wacom->intf = intf;
2412 mutex_init(&wacom->lock);
2413 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2414 INIT_WORK(&wacom->battery_work, wacom_battery_work);
2415 INIT_WORK(&wacom->remote_work, wacom_remote_work);
2416
2417 /* ask for the report descriptor to be loaded by HID */
2418 error = hid_parse(hdev);
2419 if (error) {
2420 hid_err(hdev, "parse failed\n");
2421 goto fail;
2422 }
2423
2424 error = wacom_parse_and_register(wacom, false);
2425 if (error)
2426 goto fail;
2427
2428 if (hdev->bus == BUS_BLUETOOTH) {
2429 error = device_create_file(&hdev->dev, &dev_attr_speed);
2430 if (error)
2431 hid_warn(hdev,
2432 "can't create sysfs speed attribute err: %d\n",
2433 error);
2434 }
2435
2436 return 0;
2437
2438 fail:
2439 hid_set_drvdata(hdev, NULL);
2440 return error;
2441 }
2442
2443 static void wacom_remove(struct hid_device *hdev)
2444 {
2445 struct wacom *wacom = hid_get_drvdata(hdev);
2446 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2447 struct wacom_features *features = &wacom_wac->features;
2448
2449 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2450 hid_hw_close(hdev);
2451
2452 hid_hw_stop(hdev);
2453
2454 cancel_work_sync(&wacom->wireless_work);
2455 cancel_work_sync(&wacom->battery_work);
2456 cancel_work_sync(&wacom->remote_work);
2457 if (hdev->bus == BUS_BLUETOOTH)
2458 device_remove_file(&hdev->dev, &dev_attr_speed);
2459
2460 hid_set_drvdata(hdev, NULL);
2461 }
2462
2463 #ifdef CONFIG_PM
2464 static int wacom_resume(struct hid_device *hdev)
2465 {
2466 struct wacom *wacom = hid_get_drvdata(hdev);
2467 struct wacom_features *features = &wacom->wacom_wac.features;
2468
2469 mutex_lock(&wacom->lock);
2470
2471 /* switch to wacom mode first */
2472 wacom_query_tablet_data(hdev, features);
2473 wacom_led_control(wacom);
2474
2475 mutex_unlock(&wacom->lock);
2476
2477 return 0;
2478 }
2479
2480 static int wacom_reset_resume(struct hid_device *hdev)
2481 {
2482 return wacom_resume(hdev);
2483 }
2484 #endif /* CONFIG_PM */
2485
2486 static struct hid_driver wacom_driver = {
2487 .name = "wacom",
2488 .id_table = wacom_ids,
2489 .probe = wacom_probe,
2490 .remove = wacom_remove,
2491 .report = wacom_wac_report,
2492 #ifdef CONFIG_PM
2493 .resume = wacom_resume,
2494 .reset_resume = wacom_reset_resume,
2495 #endif
2496 .raw_event = wacom_raw_event,
2497 };
2498 module_hid_driver(wacom_driver);
2499
2500 MODULE_VERSION(DRIVER_VERSION);
2501 MODULE_AUTHOR(DRIVER_AUTHOR);
2502 MODULE_DESCRIPTION(DRIVER_DESC);
2503 MODULE_LICENSE(DRIVER_LICENSE);