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