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