x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / drivers / hid / wacom_sys.c
blob18d5b99d13f1b94711396c7c88980c745d50bf1f
1 /*
2 * drivers/input/tablet/wacom_sys.c
4 * USB Wacom tablet support - system specific code
5 */
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.
14 #include "wacom_wac.h"
15 #include "wacom.h"
16 #include <linux/input/mt.h>
18 #define WAC_MSG_RETRIES 5
19 #define WAC_CMD_RETRIES 10
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)
25 static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
26 size_t size, unsigned int retries)
28 int retval;
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);
35 if (retval < 0)
36 hid_err(hdev, "wacom_get_report: ran out of retries "
37 "(last error = %d)\n", retval);
39 return retval;
42 static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
43 size_t size, unsigned int retries)
45 int retval;
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);
52 if (retval < 0)
53 hid_err(hdev, "wacom_set_report: ran out of retries "
54 "(last error = %d)\n", retval);
56 return retval;
59 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
60 u8 *raw_data, int size)
62 struct wacom *wacom = hid_get_drvdata(hdev);
64 if (size > WACOM_PKGLEN_MAX)
65 return 1;
67 memcpy(wacom->wacom_wac.data, raw_data, size);
69 wacom_wac_irq(&wacom->wacom_wac, size);
71 return 0;
74 static int wacom_open(struct input_dev *dev)
76 struct wacom *wacom = input_get_drvdata(dev);
78 return hid_hw_open(wacom->hdev);
81 static void wacom_close(struct input_dev *dev)
83 struct wacom *wacom = input_get_drvdata(dev);
86 * wacom->hdev should never be null, but surprisingly, I had the case
87 * once while unplugging the Wacom Wireless Receiver.
89 if (wacom->hdev)
90 hid_hw_close(wacom->hdev);
94 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
96 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
97 unsigned unit, int exponent)
99 struct hid_field field = {
100 .logical_maximum = logical_extents,
101 .physical_maximum = physical_extents,
102 .unit = unit,
103 .unit_exponent = exponent,
106 return hidinput_calc_abs_res(&field, ABS_X);
109 static void wacom_feature_mapping(struct hid_device *hdev,
110 struct hid_field *field, struct hid_usage *usage)
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;
120 switch (equivalent_usage) {
121 case HID_DG_CONTACTMAX:
122 /* leave touch_max as is if predefined */
123 if (!features->touch_max) {
124 /* read manually */
125 data = kzalloc(2, GFP_KERNEL);
126 if (!data)
127 break;
128 data[0] = field->report->id;
129 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
130 data, 2, WAC_CMD_RETRIES);
131 if (ret == 2) {
132 features->touch_max = data[1];
133 } else {
134 features->touch_max = 16;
135 hid_warn(hdev, "wacom_feature_mapping: "
136 "could not get HID_DG_CONTACTMAX, "
137 "defaulting to %d\n",
138 features->touch_max);
140 kfree(data);
142 break;
143 case HID_DG_INPUTMODE:
144 /* Ignore if value index is out of bounds. */
145 if (usage->usage_index >= field->report_count) {
146 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
147 break;
150 hid_data->inputmode = field->report->id;
151 hid_data->inputmode_index = usage->usage_index;
152 break;
154 case HID_UP_DIGITIZER:
155 if (field->report->id == 0x0B &&
156 (field->application == WACOM_HID_G9_PEN ||
157 field->application == WACOM_HID_G11_PEN)) {
158 wacom->wacom_wac.mode_report = field->report->id;
159 wacom->wacom_wac.mode_value = 0;
161 break;
163 case WACOM_HID_WD_DATAMODE:
164 wacom->wacom_wac.mode_report = field->report->id;
165 wacom->wacom_wac.mode_value = 2;
166 break;
168 case WACOM_HID_UP_G9:
169 case WACOM_HID_UP_G11:
170 if (field->report->id == 0x03 &&
171 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
172 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
173 wacom->wacom_wac.mode_report = field->report->id;
174 wacom->wacom_wac.mode_value = 0;
176 break;
177 case WACOM_HID_WD_OFFSETLEFT:
178 case WACOM_HID_WD_OFFSETTOP:
179 case WACOM_HID_WD_OFFSETRIGHT:
180 case WACOM_HID_WD_OFFSETBOTTOM:
181 /* read manually */
182 n = hid_report_len(field->report);
183 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
184 if (!data)
185 break;
186 data[0] = field->report->id;
187 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
188 data, n, WAC_CMD_RETRIES);
189 if (ret == n) {
190 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
191 data, n, 0);
192 } else {
193 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
194 __func__);
196 kfree(data);
197 break;
202 * Interface Descriptor of wacom devices can be incomplete and
203 * inconsistent so wacom_features table is used to store stylus
204 * device's packet lengths, various maximum values, and tablet
205 * resolution based on product ID's.
207 * For devices that contain 2 interfaces, wacom_features table is
208 * inaccurate for the touch interface. Since the Interface Descriptor
209 * for touch interfaces has pretty complete data, this function exists
210 * to query tablet for this missing information instead of hard coding in
211 * an additional table.
213 * A typical Interface Descriptor for a stylus will contain a
214 * boot mouse application collection that is not of interest and this
215 * function will ignore it.
217 * It also contains a digitizer application collection that also is not
218 * of interest since any information it contains would be duplicate
219 * of what is in wacom_features. Usually it defines a report of an array
220 * of bytes that could be used as max length of the stylus packet returned.
221 * If it happens to define a Digitizer-Stylus Physical Collection then
222 * the X and Y logical values contain valid data but it is ignored.
224 * A typical Interface Descriptor for a touch interface will contain a
225 * Digitizer-Finger Physical Collection which will define both logical
226 * X/Y maximum as well as the physical size of tablet. Since touch
227 * interfaces haven't supported pressure or distance, this is enough
228 * information to override invalid values in the wacom_features table.
230 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
231 * data. We deal with them after returning from this function.
233 static void wacom_usage_mapping(struct hid_device *hdev,
234 struct hid_field *field, struct hid_usage *usage)
236 struct wacom *wacom = hid_get_drvdata(hdev);
237 struct wacom_features *features = &wacom->wacom_wac.features;
238 bool finger = WACOM_FINGER_FIELD(field);
239 bool pen = WACOM_PEN_FIELD(field);
242 * Requiring Stylus Usage will ignore boot mouse
243 * X/Y values and some cases of invalid Digitizer X/Y
244 * values commonly reported.
246 if (pen)
247 features->device_type |= WACOM_DEVICETYPE_PEN;
248 else if (finger)
249 features->device_type |= WACOM_DEVICETYPE_TOUCH;
250 else
251 return;
254 * Bamboo models do not support HID_DG_CONTACTMAX.
255 * And, Bamboo Pen only descriptor contains touch.
257 if (features->type > BAMBOO_PT) {
258 /* ISDv4 touch devices at least supports one touch point */
259 if (finger && !features->touch_max)
260 features->touch_max = 1;
264 * ISDv4 devices which predate HID's adoption of the
265 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
266 * position instead. We can accurately detect if a
267 * usage with that value should be HID_DG_BARRELSWITCH2
268 * based on the surrounding usages, which have remained
269 * constant across generations.
271 if (features->type == HID_GENERIC &&
272 usage->hid == 0x000D0000 &&
273 field->application == HID_DG_PEN &&
274 field->physical == HID_DG_STYLUS) {
275 int i = usage->usage_index;
277 if (i-4 >= 0 && i+1 < field->maxusage &&
278 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
279 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
280 field->usage[i-2].hid == HID_DG_ERASER &&
281 field->usage[i-1].hid == HID_DG_INVERT &&
282 field->usage[i+1].hid == HID_DG_INRANGE) {
283 usage->hid = HID_DG_BARRELSWITCH2;
287 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
288 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
289 hdev->product == 0x0358 &&
290 WACOM_PEN_FIELD(field) &&
291 wacom_equivalent_usage(usage->hid) == HID_GD_Y) {
292 field->logical_maximum = 43200;
295 switch (usage->hid) {
296 case HID_GD_X:
297 features->x_max = field->logical_maximum;
298 if (finger) {
299 features->x_phy = field->physical_maximum;
300 if ((features->type != BAMBOO_PT) &&
301 (features->type != BAMBOO_TOUCH)) {
302 features->unit = field->unit;
303 features->unitExpo = field->unit_exponent;
306 break;
307 case HID_GD_Y:
308 features->y_max = field->logical_maximum;
309 if (finger) {
310 features->y_phy = field->physical_maximum;
311 if ((features->type != BAMBOO_PT) &&
312 (features->type != BAMBOO_TOUCH)) {
313 features->unit = field->unit;
314 features->unitExpo = field->unit_exponent;
317 break;
318 case HID_DG_TIPPRESSURE:
319 if (pen)
320 features->pressure_max = field->logical_maximum;
321 break;
324 if (features->type == HID_GENERIC)
325 wacom_wac_usage_mapping(hdev, field, usage);
328 static void wacom_post_parse_hid(struct hid_device *hdev,
329 struct wacom_features *features)
331 struct wacom *wacom = hid_get_drvdata(hdev);
332 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
334 if (features->type == HID_GENERIC) {
335 /* Any last-minute generic device setup */
336 if (wacom_wac->has_mode_change) {
337 if (wacom_wac->is_direct_mode)
338 features->device_type |= WACOM_DEVICETYPE_DIRECT;
339 else
340 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
343 if (features->touch_max > 1) {
344 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
345 input_mt_init_slots(wacom_wac->touch_input,
346 wacom_wac->features.touch_max,
347 INPUT_MT_DIRECT);
348 else
349 input_mt_init_slots(wacom_wac->touch_input,
350 wacom_wac->features.touch_max,
351 INPUT_MT_POINTER);
356 static void wacom_parse_hid(struct hid_device *hdev,
357 struct wacom_features *features)
359 struct hid_report_enum *rep_enum;
360 struct hid_report *hreport;
361 int i, j;
363 /* check features first */
364 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
365 list_for_each_entry(hreport, &rep_enum->report_list, list) {
366 for (i = 0; i < hreport->maxfield; i++) {
367 /* Ignore if report count is out of bounds. */
368 if (hreport->field[i]->report_count < 1)
369 continue;
371 for (j = 0; j < hreport->field[i]->maxusage; j++) {
372 wacom_feature_mapping(hdev, hreport->field[i],
373 hreport->field[i]->usage + j);
378 /* now check the input usages */
379 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
380 list_for_each_entry(hreport, &rep_enum->report_list, list) {
382 if (!hreport->maxfield)
383 continue;
385 for (i = 0; i < hreport->maxfield; i++)
386 for (j = 0; j < hreport->field[i]->maxusage; j++)
387 wacom_usage_mapping(hdev, hreport->field[i],
388 hreport->field[i]->usage + j);
391 wacom_post_parse_hid(hdev, features);
394 static int wacom_hid_set_device_mode(struct hid_device *hdev)
396 struct wacom *wacom = hid_get_drvdata(hdev);
397 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
398 struct hid_report *r;
399 struct hid_report_enum *re;
401 if (hid_data->inputmode < 0)
402 return 0;
404 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
405 r = re->report_id_hash[hid_data->inputmode];
406 if (r) {
407 r->field[0]->value[hid_data->inputmode_index] = 2;
408 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
410 return 0;
413 static int wacom_set_device_mode(struct hid_device *hdev,
414 struct wacom_wac *wacom_wac)
416 u8 *rep_data;
417 struct hid_report *r;
418 struct hid_report_enum *re;
419 u32 length;
420 int error = -ENOMEM, limit = 0;
422 if (wacom_wac->mode_report < 0)
423 return 0;
425 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
426 r = re->report_id_hash[wacom_wac->mode_report];
427 if (!r)
428 return -EINVAL;
430 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
431 if (!rep_data)
432 return -ENOMEM;
434 length = hid_report_len(r);
436 do {
437 rep_data[0] = wacom_wac->mode_report;
438 rep_data[1] = wacom_wac->mode_value;
440 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
441 length, 1);
442 if (error >= 0)
443 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
444 rep_data, length, 1);
445 } while (error >= 0 &&
446 rep_data[1] != wacom_wac->mode_report &&
447 limit++ < WAC_MSG_RETRIES);
449 kfree(rep_data);
451 return error < 0 ? error : 0;
454 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
455 struct wacom_features *features)
457 struct wacom *wacom = hid_get_drvdata(hdev);
458 int ret;
459 u8 rep_data[2];
461 switch (features->type) {
462 case GRAPHIRE_BT:
463 rep_data[0] = 0x03;
464 rep_data[1] = 0x00;
465 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
468 if (ret >= 0) {
469 rep_data[0] = speed == 0 ? 0x05 : 0x06;
470 rep_data[1] = 0x00;
472 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
473 rep_data, 2, 3);
475 if (ret >= 0) {
476 wacom->wacom_wac.bt_high_speed = speed;
477 return 0;
482 * Note that if the raw queries fail, it's not a hard failure
483 * and it is safe to continue
485 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
486 rep_data[0], ret);
487 break;
488 case INTUOS4WL:
489 if (speed == 1)
490 wacom->wacom_wac.bt_features &= ~0x20;
491 else
492 wacom->wacom_wac.bt_features |= 0x20;
494 rep_data[0] = 0x03;
495 rep_data[1] = wacom->wacom_wac.bt_features;
497 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
499 if (ret >= 0)
500 wacom->wacom_wac.bt_high_speed = speed;
501 break;
504 return 0;
508 * Switch the tablet into its most-capable mode. Wacom tablets are
509 * typically configured to power-up in a mode which sends mouse-like
510 * reports to the OS. To get absolute position, pressure data, etc.
511 * from the tablet, it is necessary to switch the tablet out of this
512 * mode and into one which sends the full range of tablet data.
514 static int _wacom_query_tablet_data(struct wacom *wacom)
516 struct hid_device *hdev = wacom->hdev;
517 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
518 struct wacom_features *features = &wacom_wac->features;
520 if (hdev->bus == BUS_BLUETOOTH)
521 return wacom_bt_query_tablet_data(hdev, 1, features);
523 if (features->type != HID_GENERIC) {
524 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
525 if (features->type > TABLETPC) {
526 /* MT Tablet PC touch */
527 wacom_wac->mode_report = 3;
528 wacom_wac->mode_value = 4;
529 } else if (features->type == WACOM_24HDT) {
530 wacom_wac->mode_report = 18;
531 wacom_wac->mode_value = 2;
532 } else if (features->type == WACOM_27QHDT) {
533 wacom_wac->mode_report = 131;
534 wacom_wac->mode_value = 2;
535 } else if (features->type == BAMBOO_PAD) {
536 wacom_wac->mode_report = 2;
537 wacom_wac->mode_value = 2;
539 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
540 if (features->type <= BAMBOO_PT) {
541 wacom_wac->mode_report = 2;
542 wacom_wac->mode_value = 2;
547 wacom_set_device_mode(hdev, wacom_wac);
549 if (features->type == HID_GENERIC)
550 return wacom_hid_set_device_mode(hdev);
552 return 0;
555 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
556 struct wacom_features *features)
558 struct wacom *wacom = hid_get_drvdata(hdev);
559 struct usb_interface *intf = wacom->intf;
561 /* default features */
562 features->x_fuzz = 4;
563 features->y_fuzz = 4;
564 features->pressure_fuzz = 0;
565 features->distance_fuzz = 1;
566 features->tilt_fuzz = 1;
569 * The wireless device HID is basic and layout conflicts with
570 * other tablets (monitor and touch interface can look like pen).
571 * Skip the query for this type and modify defaults based on
572 * interface number.
574 if (features->type == WIRELESS) {
575 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
576 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
577 else
578 features->device_type = WACOM_DEVICETYPE_NONE;
579 return;
582 wacom_parse_hid(hdev, features);
585 struct wacom_hdev_data {
586 struct list_head list;
587 struct kref kref;
588 struct hid_device *dev;
589 struct wacom_shared shared;
592 static LIST_HEAD(wacom_udev_list);
593 static DEFINE_MUTEX(wacom_udev_list_lock);
595 static bool compare_device_paths(struct hid_device *hdev_a,
596 struct hid_device *hdev_b, char separator)
598 int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
599 int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
601 if (n1 != n2 || n1 <= 0 || n2 <= 0)
602 return false;
604 return !strncmp(hdev_a->phys, hdev_b->phys, n1);
607 static bool wacom_are_sibling(struct hid_device *hdev,
608 struct hid_device *sibling)
610 struct wacom *wacom = hid_get_drvdata(hdev);
611 struct wacom_features *features = &wacom->wacom_wac.features;
612 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
613 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
614 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
615 __u32 oPid = features->oPid ? features->oPid : hdev->product;
617 /* The defined oVid/oPid must match that of the sibling */
618 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
619 return false;
620 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
621 return false;
624 * Devices with the same VID/PID must share the same physical
625 * device path, while those with different VID/PID must share
626 * the same physical parent device path.
628 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
629 if (!compare_device_paths(hdev, sibling, '/'))
630 return false;
631 } else {
632 if (!compare_device_paths(hdev, sibling, '.'))
633 return false;
636 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
637 if (features->type != HID_GENERIC)
638 return true;
641 * Direct-input devices may not be siblings of indirect-input
642 * devices.
644 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
645 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
646 return false;
649 * Indirect-input devices may not be siblings of direct-input
650 * devices.
652 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
653 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
654 return false;
656 /* Pen devices may only be siblings of touch devices */
657 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
658 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
659 return false;
661 /* Touch devices may only be siblings of pen devices */
662 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
663 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
664 return false;
667 * No reason could be found for these two devices to NOT be
668 * siblings, so there's a good chance they ARE siblings
670 return true;
673 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
675 struct wacom_hdev_data *data;
677 /* Try to find an already-probed interface from the same device */
678 list_for_each_entry(data, &wacom_udev_list, list) {
679 if (compare_device_paths(hdev, data->dev, '/')) {
680 kref_get(&data->kref);
681 return data;
685 /* Fallback to finding devices that appear to be "siblings" */
686 list_for_each_entry(data, &wacom_udev_list, list) {
687 if (wacom_are_sibling(hdev, data->dev)) {
688 kref_get(&data->kref);
689 return data;
693 return NULL;
696 static void wacom_release_shared_data(struct kref *kref)
698 struct wacom_hdev_data *data =
699 container_of(kref, struct wacom_hdev_data, kref);
701 mutex_lock(&wacom_udev_list_lock);
702 list_del(&data->list);
703 mutex_unlock(&wacom_udev_list_lock);
705 kfree(data);
708 static void wacom_remove_shared_data(void *res)
710 struct wacom *wacom = res;
711 struct wacom_hdev_data *data;
712 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
714 if (wacom_wac->shared) {
715 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
716 shared);
718 if (wacom_wac->shared->touch == wacom->hdev)
719 wacom_wac->shared->touch = NULL;
720 else if (wacom_wac->shared->pen == wacom->hdev)
721 wacom_wac->shared->pen = NULL;
723 kref_put(&data->kref, wacom_release_shared_data);
724 wacom_wac->shared = NULL;
728 static int wacom_add_shared_data(struct hid_device *hdev)
730 struct wacom *wacom = hid_get_drvdata(hdev);
731 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
732 struct wacom_hdev_data *data;
733 int retval = 0;
735 mutex_lock(&wacom_udev_list_lock);
737 data = wacom_get_hdev_data(hdev);
738 if (!data) {
739 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
740 if (!data) {
741 retval = -ENOMEM;
742 goto out;
745 kref_init(&data->kref);
746 data->dev = hdev;
747 list_add_tail(&data->list, &wacom_udev_list);
750 wacom_wac->shared = &data->shared;
752 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
753 if (retval) {
754 mutex_unlock(&wacom_udev_list_lock);
755 wacom_remove_shared_data(wacom);
756 return retval;
759 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
760 wacom_wac->shared->touch = hdev;
761 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
762 wacom_wac->shared->pen = hdev;
764 out:
765 mutex_unlock(&wacom_udev_list_lock);
766 return retval;
769 static int wacom_led_control(struct wacom *wacom)
771 unsigned char *buf;
772 int retval;
773 unsigned char report_id = WAC_CMD_LED_CONTROL;
774 int buf_size = 9;
776 if (!wacom->led.groups)
777 return -ENOTSUPP;
779 if (wacom->wacom_wac.features.type == REMOTE)
780 return -ENOTSUPP;
782 if (wacom->wacom_wac.pid) { /* wireless connected */
783 report_id = WAC_CMD_WL_LED_CONTROL;
784 buf_size = 13;
786 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
787 report_id = WAC_CMD_WL_INTUOSP2;
788 buf_size = 51;
790 buf = kzalloc(buf_size, GFP_KERNEL);
791 if (!buf)
792 return -ENOMEM;
794 if (wacom->wacom_wac.features.type == HID_GENERIC) {
795 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
796 buf[1] = wacom->led.llv;
797 buf[2] = wacom->led.groups[0].select & 0x03;
799 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
800 wacom->wacom_wac.features.type <= INTUOSPL)) {
802 * Touch Ring and crop mark LED luminance may take on
803 * one of four values:
804 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
806 int ring_led = wacom->led.groups[0].select & 0x03;
807 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
808 int crop_lum = 0;
809 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
811 buf[0] = report_id;
812 if (wacom->wacom_wac.pid) {
813 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
814 buf, buf_size, WAC_CMD_RETRIES);
815 buf[0] = report_id;
816 buf[4] = led_bits;
817 } else
818 buf[1] = led_bits;
820 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
821 buf[0] = report_id;
822 buf[4] = 100; // Power Connection LED (ORANGE)
823 buf[5] = 100; // BT Connection LED (BLUE)
824 buf[6] = 100; // Paper Mode (RED?)
825 buf[7] = 100; // Paper Mode (GREEN?)
826 buf[8] = 100; // Paper Mode (BLUE?)
827 buf[9] = wacom->led.llv;
828 buf[10] = wacom->led.groups[0].select & 0x03;
830 else {
831 int led = wacom->led.groups[0].select | 0x4;
833 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
834 wacom->wacom_wac.features.type == WACOM_24HD)
835 led |= (wacom->led.groups[1].select << 4) | 0x40;
837 buf[0] = report_id;
838 buf[1] = led;
839 buf[2] = wacom->led.llv;
840 buf[3] = wacom->led.hlv;
841 buf[4] = wacom->led.img_lum;
844 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
845 WAC_CMD_RETRIES);
846 kfree(buf);
848 return retval;
851 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
852 const unsigned len, const void *img)
854 unsigned char *buf;
855 int i, retval;
856 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
858 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
859 if (!buf)
860 return -ENOMEM;
862 /* Send 'start' command */
863 buf[0] = WAC_CMD_ICON_START;
864 buf[1] = 1;
865 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
866 WAC_CMD_RETRIES);
867 if (retval < 0)
868 goto out;
870 buf[0] = xfer_id;
871 buf[1] = button_id & 0x07;
872 for (i = 0; i < 4; i++) {
873 buf[2] = i;
874 memcpy(buf + 3, img + i * chunk_len, chunk_len);
876 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
877 buf, chunk_len + 3, WAC_CMD_RETRIES);
878 if (retval < 0)
879 break;
882 /* Send 'stop' */
883 buf[0] = WAC_CMD_ICON_START;
884 buf[1] = 0;
885 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
886 WAC_CMD_RETRIES);
888 out:
889 kfree(buf);
890 return retval;
893 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
894 const char *buf, size_t count)
896 struct hid_device *hdev = to_hid_device(dev);
897 struct wacom *wacom = hid_get_drvdata(hdev);
898 unsigned int id;
899 int err;
901 err = kstrtouint(buf, 10, &id);
902 if (err)
903 return err;
905 mutex_lock(&wacom->lock);
907 wacom->led.groups[set_id].select = id & 0x3;
908 err = wacom_led_control(wacom);
910 mutex_unlock(&wacom->lock);
912 return err < 0 ? err : count;
915 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
916 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
917 struct device_attribute *attr, const char *buf, size_t count) \
919 return wacom_led_select_store(dev, SET_ID, buf, count); \
921 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
922 struct device_attribute *attr, char *buf) \
924 struct hid_device *hdev = to_hid_device(dev);\
925 struct wacom *wacom = hid_get_drvdata(hdev); \
926 return scnprintf(buf, PAGE_SIZE, "%d\n", \
927 wacom->led.groups[SET_ID].select); \
929 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
930 wacom_led##SET_ID##_select_show, \
931 wacom_led##SET_ID##_select_store)
933 DEVICE_LED_SELECT_ATTR(0);
934 DEVICE_LED_SELECT_ATTR(1);
936 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
937 const char *buf, size_t count)
939 unsigned int value;
940 int err;
942 err = kstrtouint(buf, 10, &value);
943 if (err)
944 return err;
946 mutex_lock(&wacom->lock);
948 *dest = value & 0x7f;
949 err = wacom_led_control(wacom);
951 mutex_unlock(&wacom->lock);
953 return err < 0 ? err : count;
956 #define DEVICE_LUMINANCE_ATTR(name, field) \
957 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
958 struct device_attribute *attr, const char *buf, size_t count) \
960 struct hid_device *hdev = to_hid_device(dev);\
961 struct wacom *wacom = hid_get_drvdata(hdev); \
963 return wacom_luminance_store(wacom, &wacom->led.field, \
964 buf, count); \
966 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
967 struct device_attribute *attr, char *buf) \
969 struct wacom *wacom = dev_get_drvdata(dev); \
970 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
972 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
973 wacom_##name##_luminance_show, \
974 wacom_##name##_luminance_store)
976 DEVICE_LUMINANCE_ATTR(status0, llv);
977 DEVICE_LUMINANCE_ATTR(status1, hlv);
978 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
980 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
981 const char *buf, size_t count)
983 struct hid_device *hdev = to_hid_device(dev);
984 struct wacom *wacom = hid_get_drvdata(hdev);
985 int err;
986 unsigned len;
987 u8 xfer_id;
989 if (hdev->bus == BUS_BLUETOOTH) {
990 len = 256;
991 xfer_id = WAC_CMD_ICON_BT_XFER;
992 } else {
993 len = 1024;
994 xfer_id = WAC_CMD_ICON_XFER;
997 if (count != len)
998 return -EINVAL;
1000 mutex_lock(&wacom->lock);
1002 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
1004 mutex_unlock(&wacom->lock);
1006 return err < 0 ? err : count;
1009 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1010 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1011 struct device_attribute *attr, const char *buf, size_t count) \
1013 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1015 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
1016 NULL, wacom_btnimg##BUTTON_ID##_store)
1018 DEVICE_BTNIMG_ATTR(0);
1019 DEVICE_BTNIMG_ATTR(1);
1020 DEVICE_BTNIMG_ATTR(2);
1021 DEVICE_BTNIMG_ATTR(3);
1022 DEVICE_BTNIMG_ATTR(4);
1023 DEVICE_BTNIMG_ATTR(5);
1024 DEVICE_BTNIMG_ATTR(6);
1025 DEVICE_BTNIMG_ATTR(7);
1027 static struct attribute *cintiq_led_attrs[] = {
1028 &dev_attr_status_led0_select.attr,
1029 &dev_attr_status_led1_select.attr,
1030 NULL
1033 static struct attribute_group cintiq_led_attr_group = {
1034 .name = "wacom_led",
1035 .attrs = cintiq_led_attrs,
1038 static struct attribute *intuos4_led_attrs[] = {
1039 &dev_attr_status0_luminance.attr,
1040 &dev_attr_status1_luminance.attr,
1041 &dev_attr_status_led0_select.attr,
1042 &dev_attr_buttons_luminance.attr,
1043 &dev_attr_button0_rawimg.attr,
1044 &dev_attr_button1_rawimg.attr,
1045 &dev_attr_button2_rawimg.attr,
1046 &dev_attr_button3_rawimg.attr,
1047 &dev_attr_button4_rawimg.attr,
1048 &dev_attr_button5_rawimg.attr,
1049 &dev_attr_button6_rawimg.attr,
1050 &dev_attr_button7_rawimg.attr,
1051 NULL
1054 static struct attribute_group intuos4_led_attr_group = {
1055 .name = "wacom_led",
1056 .attrs = intuos4_led_attrs,
1059 static struct attribute *intuos5_led_attrs[] = {
1060 &dev_attr_status0_luminance.attr,
1061 &dev_attr_status_led0_select.attr,
1062 NULL
1065 static struct attribute_group intuos5_led_attr_group = {
1066 .name = "wacom_led",
1067 .attrs = intuos5_led_attrs,
1070 static struct attribute *generic_led_attrs[] = {
1071 &dev_attr_status0_luminance.attr,
1072 &dev_attr_status_led0_select.attr,
1073 NULL
1076 static struct attribute_group generic_led_attr_group = {
1077 .name = "wacom_led",
1078 .attrs = generic_led_attrs,
1081 struct wacom_sysfs_group_devres {
1082 struct attribute_group *group;
1083 struct kobject *root;
1086 static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1088 struct wacom_sysfs_group_devres *devres = res;
1089 struct kobject *kobj = devres->root;
1091 dev_dbg(dev, "%s: dropping reference to %s\n",
1092 __func__, devres->group->name);
1093 sysfs_remove_group(kobj, devres->group);
1096 static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1097 struct kobject *root,
1098 struct attribute_group *group)
1100 struct wacom_sysfs_group_devres *devres;
1101 int error;
1103 devres = devres_alloc(wacom_devm_sysfs_group_release,
1104 sizeof(struct wacom_sysfs_group_devres),
1105 GFP_KERNEL);
1106 if (!devres)
1107 return -ENOMEM;
1109 devres->group = group;
1110 devres->root = root;
1112 error = sysfs_create_group(devres->root, group);
1113 if (error) {
1114 devres_free(devres);
1115 return error;
1118 devres_add(&wacom->hdev->dev, devres);
1120 return 0;
1123 static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1124 struct attribute_group *group)
1126 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1127 group);
1130 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1132 struct wacom *wacom = led->wacom;
1134 if (wacom->led.max_hlv)
1135 return led->hlv * LED_FULL / wacom->led.max_hlv;
1137 if (wacom->led.max_llv)
1138 return led->llv * LED_FULL / wacom->led.max_llv;
1140 /* device doesn't support brightness tuning */
1141 return LED_FULL;
1144 static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1146 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1147 struct wacom *wacom = led->wacom;
1149 if (wacom->led.groups[led->group].select != led->id)
1150 return LED_OFF;
1152 return wacom_leds_brightness_get(led);
1155 static int wacom_led_brightness_set(struct led_classdev *cdev,
1156 enum led_brightness brightness)
1158 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1159 struct wacom *wacom = led->wacom;
1160 int error;
1162 mutex_lock(&wacom->lock);
1164 if (!wacom->led.groups || (brightness == LED_OFF &&
1165 wacom->led.groups[led->group].select != led->id)) {
1166 error = 0;
1167 goto out;
1170 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1171 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1173 wacom->led.groups[led->group].select = led->id;
1175 error = wacom_led_control(wacom);
1177 out:
1178 mutex_unlock(&wacom->lock);
1180 return error;
1183 static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1184 enum led_brightness brightness)
1188 static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1189 struct wacom_led *led, unsigned int group,
1190 unsigned int id, bool read_only)
1192 int error;
1193 char *name;
1195 name = devm_kasprintf(dev, GFP_KERNEL,
1196 "%s::wacom-%d.%d",
1197 dev_name(dev),
1198 group,
1199 id);
1200 if (!name)
1201 return -ENOMEM;
1203 if (!read_only) {
1204 led->trigger.name = name;
1205 error = devm_led_trigger_register(dev, &led->trigger);
1206 if (error) {
1207 hid_err(wacom->hdev,
1208 "failed to register LED trigger %s: %d\n",
1209 led->cdev.name, error);
1210 return error;
1214 led->group = group;
1215 led->id = id;
1216 led->wacom = wacom;
1217 led->llv = wacom->led.llv;
1218 led->hlv = wacom->led.hlv;
1219 led->cdev.name = name;
1220 led->cdev.max_brightness = LED_FULL;
1221 led->cdev.flags = LED_HW_PLUGGABLE;
1222 led->cdev.brightness_get = __wacom_led_brightness_get;
1223 if (!read_only) {
1224 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
1225 led->cdev.default_trigger = led->cdev.name;
1226 } else {
1227 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
1230 error = devm_led_classdev_register(dev, &led->cdev);
1231 if (error) {
1232 hid_err(wacom->hdev,
1233 "failed to register LED %s: %d\n",
1234 led->cdev.name, error);
1235 led->cdev.name = NULL;
1236 return error;
1239 return 0;
1242 static void wacom_led_groups_release_one(void *data)
1244 struct wacom_group_leds *group = data;
1246 devres_release_group(group->dev, group);
1249 static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1250 struct wacom *wacom,
1251 int group_id, int count,
1252 bool read_only)
1254 struct wacom_led *leds;
1255 int i, error;
1257 if (group_id >= wacom->led.count || count <= 0)
1258 return -EINVAL;
1260 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1261 return -ENOMEM;
1263 leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1264 if (!leds) {
1265 error = -ENOMEM;
1266 goto err;
1269 wacom->led.groups[group_id].leds = leds;
1270 wacom->led.groups[group_id].count = count;
1272 for (i = 0; i < count; i++) {
1273 error = wacom_led_register_one(dev, wacom, &leds[i],
1274 group_id, i, read_only);
1275 if (error)
1276 goto err;
1279 wacom->led.groups[group_id].dev = dev;
1281 devres_close_group(dev, &wacom->led.groups[group_id]);
1284 * There is a bug (?) in devm_led_classdev_register() in which its
1285 * increments the refcount of the parent. If the parent is an input
1286 * device, that means the ref count never reaches 0 when
1287 * devm_input_device_release() gets called.
1288 * This means that the LEDs are still there after disconnect.
1289 * Manually force the release of the group so that the leds are released
1290 * once we are done using them.
1292 error = devm_add_action_or_reset(&wacom->hdev->dev,
1293 wacom_led_groups_release_one,
1294 &wacom->led.groups[group_id]);
1295 if (error)
1296 return error;
1298 return 0;
1300 err:
1301 devres_release_group(dev, &wacom->led.groups[group_id]);
1302 return error;
1305 struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1306 unsigned int id)
1308 struct wacom_group_leds *group;
1310 if (group_id >= wacom->led.count)
1311 return NULL;
1313 group = &wacom->led.groups[group_id];
1315 if (!group->leds)
1316 return NULL;
1318 id %= group->count;
1320 return &group->leds[id];
1324 * wacom_led_next: gives the next available led with a wacom trigger.
1326 * returns the next available struct wacom_led which has its default trigger
1327 * or the current one if none is available.
1329 struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1331 struct wacom_led *next_led;
1332 int group, next;
1334 if (!wacom || !cur)
1335 return NULL;
1337 group = cur->group;
1338 next = cur->id;
1340 do {
1341 next_led = wacom_led_find(wacom, group, ++next);
1342 if (!next_led || next_led == cur)
1343 return next_led;
1344 } while (next_led->cdev.trigger != &next_led->trigger);
1346 return next_led;
1349 static void wacom_led_groups_release(void *data)
1351 struct wacom *wacom = data;
1353 wacom->led.groups = NULL;
1354 wacom->led.count = 0;
1357 static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1359 struct device *dev = &wacom->hdev->dev;
1360 struct wacom_group_leds *groups;
1361 int error;
1363 groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
1364 GFP_KERNEL);
1365 if (!groups)
1366 return -ENOMEM;
1368 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1369 if (error)
1370 return error;
1372 wacom->led.groups = groups;
1373 wacom->led.count = count;
1375 return 0;
1378 static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1379 int led_per_group, bool read_only)
1381 struct device *dev;
1382 int i, error;
1384 if (!wacom->wacom_wac.pad_input)
1385 return -EINVAL;
1387 dev = &wacom->wacom_wac.pad_input->dev;
1389 error = wacom_led_groups_allocate(wacom, group_count);
1390 if (error)
1391 return error;
1393 for (i = 0; i < group_count; i++) {
1394 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1395 led_per_group,
1396 read_only);
1397 if (error)
1398 return error;
1401 return 0;
1404 int wacom_initialize_leds(struct wacom *wacom)
1406 int error;
1408 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1409 return 0;
1411 /* Initialize default values */
1412 switch (wacom->wacom_wac.features.type) {
1413 case HID_GENERIC:
1414 if (!wacom->generic_has_leds)
1415 return 0;
1416 wacom->led.llv = 100;
1417 wacom->led.max_llv = 100;
1419 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1420 if (error) {
1421 hid_err(wacom->hdev,
1422 "cannot create leds err: %d\n", error);
1423 return error;
1426 error = wacom_devm_sysfs_create_group(wacom,
1427 &generic_led_attr_group);
1428 break;
1430 case INTUOS4S:
1431 case INTUOS4:
1432 case INTUOS4WL:
1433 case INTUOS4L:
1434 wacom->led.llv = 10;
1435 wacom->led.hlv = 20;
1436 wacom->led.max_llv = 127;
1437 wacom->led.max_hlv = 127;
1438 wacom->led.img_lum = 10;
1440 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1441 if (error) {
1442 hid_err(wacom->hdev,
1443 "cannot create leds err: %d\n", error);
1444 return error;
1447 error = wacom_devm_sysfs_create_group(wacom,
1448 &intuos4_led_attr_group);
1449 break;
1451 case WACOM_24HD:
1452 case WACOM_21UX2:
1453 wacom->led.llv = 0;
1454 wacom->led.hlv = 0;
1455 wacom->led.img_lum = 0;
1457 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1458 if (error) {
1459 hid_err(wacom->hdev,
1460 "cannot create leds err: %d\n", error);
1461 return error;
1464 error = wacom_devm_sysfs_create_group(wacom,
1465 &cintiq_led_attr_group);
1466 break;
1468 case INTUOS5S:
1469 case INTUOS5:
1470 case INTUOS5L:
1471 case INTUOSPS:
1472 case INTUOSPM:
1473 case INTUOSPL:
1474 wacom->led.llv = 32;
1475 wacom->led.max_llv = 96;
1477 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1478 if (error) {
1479 hid_err(wacom->hdev,
1480 "cannot create leds err: %d\n", error);
1481 return error;
1484 error = wacom_devm_sysfs_create_group(wacom,
1485 &intuos5_led_attr_group);
1486 break;
1488 case INTUOSP2_BT:
1489 wacom->led.llv = 50;
1490 wacom->led.max_llv = 100;
1491 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1492 if (error) {
1493 hid_err(wacom->hdev,
1494 "cannot create leds err: %d\n", error);
1495 return error;
1497 return 0;
1499 case REMOTE:
1500 wacom->led.llv = 255;
1501 wacom->led.max_llv = 255;
1502 error = wacom_led_groups_allocate(wacom, 5);
1503 if (error) {
1504 hid_err(wacom->hdev,
1505 "cannot create leds err: %d\n", error);
1506 return error;
1508 return 0;
1510 default:
1511 return 0;
1514 if (error) {
1515 hid_err(wacom->hdev,
1516 "cannot create sysfs group err: %d\n", error);
1517 return error;
1520 return 0;
1523 static void wacom_init_work(struct work_struct *work)
1525 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1527 _wacom_query_tablet_data(wacom);
1528 wacom_led_control(wacom);
1531 static void wacom_query_tablet_data(struct wacom *wacom)
1533 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1536 static enum power_supply_property wacom_battery_props[] = {
1537 POWER_SUPPLY_PROP_MODEL_NAME,
1538 POWER_SUPPLY_PROP_PRESENT,
1539 POWER_SUPPLY_PROP_STATUS,
1540 POWER_SUPPLY_PROP_SCOPE,
1541 POWER_SUPPLY_PROP_CAPACITY
1544 static int wacom_battery_get_property(struct power_supply *psy,
1545 enum power_supply_property psp,
1546 union power_supply_propval *val)
1548 struct wacom_battery *battery = power_supply_get_drvdata(psy);
1549 int ret = 0;
1551 switch (psp) {
1552 case POWER_SUPPLY_PROP_MODEL_NAME:
1553 val->strval = battery->wacom->wacom_wac.name;
1554 break;
1555 case POWER_SUPPLY_PROP_PRESENT:
1556 val->intval = battery->bat_connected;
1557 break;
1558 case POWER_SUPPLY_PROP_SCOPE:
1559 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1560 break;
1561 case POWER_SUPPLY_PROP_CAPACITY:
1562 val->intval = battery->battery_capacity;
1563 break;
1564 case POWER_SUPPLY_PROP_STATUS:
1565 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1566 val->intval = battery->bat_status;
1567 else if (battery->bat_charging)
1568 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1569 else if (battery->battery_capacity == 100 &&
1570 battery->ps_connected)
1571 val->intval = POWER_SUPPLY_STATUS_FULL;
1572 else if (battery->ps_connected)
1573 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1574 else
1575 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1576 break;
1577 default:
1578 ret = -EINVAL;
1579 break;
1582 return ret;
1585 static int __wacom_initialize_battery(struct wacom *wacom,
1586 struct wacom_battery *battery)
1588 static atomic_t battery_no = ATOMIC_INIT(0);
1589 struct device *dev = &wacom->hdev->dev;
1590 struct power_supply_config psy_cfg = { .drv_data = battery, };
1591 struct power_supply *ps_bat;
1592 struct power_supply_desc *bat_desc = &battery->bat_desc;
1593 unsigned long n;
1594 int error;
1596 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1597 return -ENOMEM;
1599 battery->wacom = wacom;
1601 n = atomic_inc_return(&battery_no) - 1;
1603 bat_desc->properties = wacom_battery_props;
1604 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1605 bat_desc->get_property = wacom_battery_get_property;
1606 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1607 bat_desc->name = battery->bat_name;
1608 bat_desc->type = POWER_SUPPLY_TYPE_USB;
1609 bat_desc->use_for_apm = 0;
1611 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1612 if (IS_ERR(ps_bat)) {
1613 error = PTR_ERR(ps_bat);
1614 goto err;
1617 power_supply_powers(ps_bat, &wacom->hdev->dev);
1619 battery->battery = ps_bat;
1621 devres_close_group(dev, bat_desc);
1622 return 0;
1624 err:
1625 devres_release_group(dev, bat_desc);
1626 return error;
1629 static int wacom_initialize_battery(struct wacom *wacom)
1631 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1632 return __wacom_initialize_battery(wacom, &wacom->battery);
1634 return 0;
1637 static void wacom_destroy_battery(struct wacom *wacom)
1639 if (wacom->battery.battery) {
1640 devres_release_group(&wacom->hdev->dev,
1641 &wacom->battery.bat_desc);
1642 wacom->battery.battery = NULL;
1646 static ssize_t wacom_show_speed(struct device *dev,
1647 struct device_attribute
1648 *attr, char *buf)
1650 struct hid_device *hdev = to_hid_device(dev);
1651 struct wacom *wacom = hid_get_drvdata(hdev);
1653 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1656 static ssize_t wacom_store_speed(struct device *dev,
1657 struct device_attribute *attr,
1658 const char *buf, size_t count)
1660 struct hid_device *hdev = to_hid_device(dev);
1661 struct wacom *wacom = hid_get_drvdata(hdev);
1662 u8 new_speed;
1664 if (kstrtou8(buf, 0, &new_speed))
1665 return -EINVAL;
1667 if (new_speed != 0 && new_speed != 1)
1668 return -EINVAL;
1670 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1672 return count;
1675 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1676 wacom_show_speed, wacom_store_speed);
1679 static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1680 struct kobj_attribute *kattr,
1681 char *buf, int index)
1683 struct device *dev = kobj_to_dev(kobj->parent);
1684 struct hid_device *hdev = to_hid_device(dev);
1685 struct wacom *wacom = hid_get_drvdata(hdev);
1686 u8 mode;
1688 mode = wacom->led.groups[index].select;
1689 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
1692 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1693 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1694 struct kobj_attribute *kattr, char *buf) \
1696 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1698 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1699 .attr = {.name = "remote_mode", \
1700 .mode = DEV_ATTR_RO_PERM}, \
1701 .show = wacom_show_remote##SET_ID##_mode, \
1702 }; \
1703 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1704 &remote##SET_ID##_mode_attr.attr, \
1705 NULL \
1706 }; \
1707 static struct attribute_group remote##SET_ID##_serial_group = { \
1708 .name = NULL, \
1709 .attrs = remote##SET_ID##_serial_attrs, \
1712 DEVICE_EKR_ATTR_GROUP(0);
1713 DEVICE_EKR_ATTR_GROUP(1);
1714 DEVICE_EKR_ATTR_GROUP(2);
1715 DEVICE_EKR_ATTR_GROUP(3);
1716 DEVICE_EKR_ATTR_GROUP(4);
1718 static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1719 int index)
1721 int error = 0;
1722 struct wacom_remote *remote = wacom->remote;
1724 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1725 GFP_KERNEL,
1726 "%d", serial);
1727 if (!remote->remotes[index].group.name)
1728 return -ENOMEM;
1730 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1731 &remote->remotes[index].group);
1732 if (error) {
1733 remote->remotes[index].group.name = NULL;
1734 hid_err(wacom->hdev,
1735 "cannot create sysfs group err: %d\n", error);
1736 return error;
1739 return 0;
1742 static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1744 const size_t buf_size = 2;
1745 unsigned char *buf;
1746 int retval;
1748 buf = kzalloc(buf_size, GFP_KERNEL);
1749 if (!buf)
1750 return -ENOMEM;
1752 buf[0] = WAC_CMD_DELETE_PAIRING;
1753 buf[1] = selector;
1755 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1756 buf_size, WAC_CMD_RETRIES);
1757 kfree(buf);
1759 return retval;
1762 static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1763 struct kobj_attribute *attr,
1764 const char *buf, size_t count)
1766 unsigned char selector = 0;
1767 struct device *dev = kobj_to_dev(kobj->parent);
1768 struct hid_device *hdev = to_hid_device(dev);
1769 struct wacom *wacom = hid_get_drvdata(hdev);
1770 int err;
1772 if (!strncmp(buf, "*\n", 2)) {
1773 selector = WAC_CMD_UNPAIR_ALL;
1774 } else {
1775 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1776 buf);
1777 return -1;
1780 mutex_lock(&wacom->lock);
1782 err = wacom_cmd_unpair_remote(wacom, selector);
1783 mutex_unlock(&wacom->lock);
1785 return err < 0 ? err : count;
1788 static struct kobj_attribute unpair_remote_attr = {
1789 .attr = {.name = "unpair_remote", .mode = 0200},
1790 .store = wacom_store_unpair_remote,
1793 static const struct attribute *remote_unpair_attrs[] = {
1794 &unpair_remote_attr.attr,
1795 NULL
1798 static void wacom_remotes_destroy(void *data)
1800 struct wacom *wacom = data;
1801 struct wacom_remote *remote = wacom->remote;
1803 if (!remote)
1804 return;
1806 kobject_put(remote->remote_dir);
1807 kfifo_free(&remote->remote_fifo);
1808 wacom->remote = NULL;
1811 static int wacom_initialize_remotes(struct wacom *wacom)
1813 int error = 0;
1814 struct wacom_remote *remote;
1815 int i;
1817 if (wacom->wacom_wac.features.type != REMOTE)
1818 return 0;
1820 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1821 GFP_KERNEL);
1822 if (!remote)
1823 return -ENOMEM;
1825 wacom->remote = remote;
1827 spin_lock_init(&remote->remote_lock);
1829 error = kfifo_alloc(&remote->remote_fifo,
1830 5 * sizeof(struct wacom_remote_data),
1831 GFP_KERNEL);
1832 if (error) {
1833 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1834 return -ENOMEM;
1837 remote->remotes[0].group = remote0_serial_group;
1838 remote->remotes[1].group = remote1_serial_group;
1839 remote->remotes[2].group = remote2_serial_group;
1840 remote->remotes[3].group = remote3_serial_group;
1841 remote->remotes[4].group = remote4_serial_group;
1843 remote->remote_dir = kobject_create_and_add("wacom_remote",
1844 &wacom->hdev->dev.kobj);
1845 if (!remote->remote_dir)
1846 return -ENOMEM;
1848 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
1850 if (error) {
1851 hid_err(wacom->hdev,
1852 "cannot create sysfs group err: %d\n", error);
1853 return error;
1856 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1857 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
1858 remote->remotes[i].serial = 0;
1861 error = devm_add_action_or_reset(&wacom->hdev->dev,
1862 wacom_remotes_destroy, wacom);
1863 if (error)
1864 return error;
1866 return 0;
1869 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1871 struct input_dev *input_dev;
1872 struct hid_device *hdev = wacom->hdev;
1873 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1875 input_dev = devm_input_allocate_device(&hdev->dev);
1876 if (!input_dev)
1877 return NULL;
1879 input_dev->name = wacom_wac->features.name;
1880 input_dev->phys = hdev->phys;
1881 input_dev->dev.parent = &hdev->dev;
1882 input_dev->open = wacom_open;
1883 input_dev->close = wacom_close;
1884 input_dev->uniq = hdev->uniq;
1885 input_dev->id.bustype = hdev->bus;
1886 input_dev->id.vendor = hdev->vendor;
1887 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1888 input_dev->id.version = hdev->version;
1889 input_set_drvdata(input_dev, wacom);
1891 return input_dev;
1894 static int wacom_allocate_inputs(struct wacom *wacom)
1896 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1898 wacom_wac->pen_input = wacom_allocate_input(wacom);
1899 wacom_wac->touch_input = wacom_allocate_input(wacom);
1900 wacom_wac->pad_input = wacom_allocate_input(wacom);
1901 if (!wacom_wac->pen_input ||
1902 !wacom_wac->touch_input ||
1903 !wacom_wac->pad_input)
1904 return -ENOMEM;
1906 wacom_wac->pen_input->name = wacom_wac->pen_name;
1907 wacom_wac->touch_input->name = wacom_wac->touch_name;
1908 wacom_wac->pad_input->name = wacom_wac->pad_name;
1910 return 0;
1913 static int wacom_register_inputs(struct wacom *wacom)
1915 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
1916 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1917 int error = 0;
1919 pen_input_dev = wacom_wac->pen_input;
1920 touch_input_dev = wacom_wac->touch_input;
1921 pad_input_dev = wacom_wac->pad_input;
1923 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
1924 return -EINVAL;
1926 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1927 if (error) {
1928 /* no pen in use on this interface */
1929 input_free_device(pen_input_dev);
1930 wacom_wac->pen_input = NULL;
1931 pen_input_dev = NULL;
1932 } else {
1933 error = input_register_device(pen_input_dev);
1934 if (error)
1935 goto fail;
1938 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1939 if (error) {
1940 /* no touch in use on this interface */
1941 input_free_device(touch_input_dev);
1942 wacom_wac->touch_input = NULL;
1943 touch_input_dev = NULL;
1944 } else {
1945 error = input_register_device(touch_input_dev);
1946 if (error)
1947 goto fail;
1950 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1951 if (error) {
1952 /* no pad in use on this interface */
1953 input_free_device(pad_input_dev);
1954 wacom_wac->pad_input = NULL;
1955 pad_input_dev = NULL;
1956 } else {
1957 error = input_register_device(pad_input_dev);
1958 if (error)
1959 goto fail;
1962 return 0;
1964 fail:
1965 wacom_wac->pad_input = NULL;
1966 wacom_wac->touch_input = NULL;
1967 wacom_wac->pen_input = NULL;
1968 return error;
1972 * Not all devices report physical dimensions from HID.
1973 * Compute the default from hardcoded logical dimension
1974 * and resolution before driver overwrites them.
1976 static void wacom_set_default_phy(struct wacom_features *features)
1978 if (features->x_resolution) {
1979 features->x_phy = (features->x_max * 100) /
1980 features->x_resolution;
1981 features->y_phy = (features->y_max * 100) /
1982 features->y_resolution;
1986 static void wacom_calculate_res(struct wacom_features *features)
1988 /* set unit to "100th of a mm" for devices not reported by HID */
1989 if (!features->unit) {
1990 features->unit = 0x11;
1991 features->unitExpo = -3;
1994 features->x_resolution = wacom_calc_hid_res(features->x_max,
1995 features->x_phy,
1996 features->unit,
1997 features->unitExpo);
1998 features->y_resolution = wacom_calc_hid_res(features->y_max,
1999 features->y_phy,
2000 features->unit,
2001 features->unitExpo);
2004 void wacom_battery_work(struct work_struct *work)
2006 struct wacom *wacom = container_of(work, struct wacom, battery_work);
2008 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2009 !wacom->battery.battery) {
2010 wacom_initialize_battery(wacom);
2012 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2013 wacom->battery.battery) {
2014 wacom_destroy_battery(wacom);
2018 static size_t wacom_compute_pktlen(struct hid_device *hdev)
2020 struct hid_report_enum *report_enum;
2021 struct hid_report *report;
2022 size_t size = 0;
2024 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2026 list_for_each_entry(report, &report_enum->report_list, list) {
2027 size_t report_size = hid_report_len(report);
2028 if (report_size > size)
2029 size = report_size;
2032 return size;
2035 static void wacom_update_name(struct wacom *wacom, const char *suffix)
2037 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2038 struct wacom_features *features = &wacom_wac->features;
2039 char name[WACOM_NAME_MAX];
2041 /* Generic devices name unspecified */
2042 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
2043 char *product_name = wacom->hdev->name;
2045 if (hid_is_using_ll_driver(wacom->hdev, &usb_hid_driver)) {
2046 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2047 struct usb_device *dev = interface_to_usbdev(intf);
2048 product_name = dev->product;
2051 if (wacom->hdev->bus == BUS_I2C) {
2052 snprintf(name, sizeof(name), "%s %X",
2053 features->name, wacom->hdev->product);
2054 } else if (strstr(product_name, "Wacom") ||
2055 strstr(product_name, "wacom") ||
2056 strstr(product_name, "WACOM")) {
2057 strlcpy(name, product_name, sizeof(name));
2058 } else {
2059 snprintf(name, sizeof(name), "Wacom %s", product_name);
2062 /* strip out excess whitespaces */
2063 while (1) {
2064 char *gap = strstr(name, " ");
2065 if (gap == NULL)
2066 break;
2067 /* shift everything including the terminator */
2068 memmove(gap, gap+1, strlen(gap));
2071 /* get rid of trailing whitespace */
2072 if (name[strlen(name)-1] == ' ')
2073 name[strlen(name)-1] = '\0';
2074 } else {
2075 strlcpy(name, features->name, sizeof(name));
2078 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2079 name, suffix);
2081 /* Append the device type to the name */
2082 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
2083 "%s%s Pen", name, suffix);
2084 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
2085 "%s%s Finger", name, suffix);
2086 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
2087 "%s%s Pad", name, suffix);
2090 static void wacom_release_resources(struct wacom *wacom)
2092 struct hid_device *hdev = wacom->hdev;
2094 if (!wacom->resources)
2095 return;
2097 devres_release_group(&hdev->dev, wacom);
2099 wacom->resources = false;
2101 wacom->wacom_wac.pen_input = NULL;
2102 wacom->wacom_wac.touch_input = NULL;
2103 wacom->wacom_wac.pad_input = NULL;
2106 static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2108 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2109 wacom_wac->shared->type = wacom_wac->features.type;
2110 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2113 if (wacom_wac->has_mute_touch_switch) {
2114 wacom_wac->shared->has_mute_touch_switch = true;
2115 wacom_wac->shared->is_touch_on = true;
2118 if (wacom_wac->shared->has_mute_touch_switch &&
2119 wacom_wac->shared->touch_input) {
2120 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2121 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2122 SW_MUTE_DEVICE);
2126 static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2128 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2129 struct wacom_features *features = &wacom_wac->features;
2130 struct hid_device *hdev = wacom->hdev;
2131 int error;
2132 unsigned int connect_mask = HID_CONNECT_HIDRAW;
2134 features->pktlen = wacom_compute_pktlen(hdev);
2135 if (features->pktlen > WACOM_PKGLEN_MAX)
2136 return -EINVAL;
2138 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2139 return -ENOMEM;
2141 wacom->resources = true;
2143 error = wacom_allocate_inputs(wacom);
2144 if (error)
2145 goto fail;
2148 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2149 * into debug mode for the touch part.
2150 * We ignore the other interfaces.
2152 if (features->type == BAMBOO_PAD) {
2153 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2154 features->type = HID_GENERIC;
2155 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2156 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2157 error = -ENODEV;
2158 goto fail;
2162 /* set the default size in case we do not get them from hid */
2163 wacom_set_default_phy(features);
2165 /* Retrieve the physical and logical size for touch devices */
2166 wacom_retrieve_hid_descriptor(hdev, features);
2167 wacom_setup_device_quirks(wacom);
2169 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2170 features->type != WIRELESS) {
2171 error = features->type == HID_GENERIC ? -ENODEV : 0;
2173 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2174 hdev->name,
2175 error ? "Ignoring" : "Assuming pen");
2177 if (error)
2178 goto fail;
2180 features->device_type |= WACOM_DEVICETYPE_PEN;
2183 wacom_calculate_res(features);
2185 wacom_update_name(wacom, wireless ? " (WL)" : "");
2187 /* pen only Bamboo neither support touch nor pad */
2188 if ((features->type == BAMBOO_PEN) &&
2189 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2190 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2191 error = -ENODEV;
2192 goto fail;
2195 error = wacom_add_shared_data(hdev);
2196 if (error)
2197 goto fail;
2199 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2200 (features->quirks & WACOM_QUIRK_BATTERY)) {
2201 error = wacom_initialize_battery(wacom);
2202 if (error)
2203 goto fail;
2206 error = wacom_register_inputs(wacom);
2207 if (error)
2208 goto fail;
2210 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2211 error = wacom_initialize_leds(wacom);
2212 if (error)
2213 goto fail;
2215 error = wacom_initialize_remotes(wacom);
2216 if (error)
2217 goto fail;
2220 if (features->type == HID_GENERIC)
2221 connect_mask |= HID_CONNECT_DRIVER;
2223 /* Regular HID work starts now */
2224 error = hid_hw_start(hdev, connect_mask);
2225 if (error) {
2226 hid_err(hdev, "hw start failed\n");
2227 goto fail;
2230 if (!wireless) {
2231 /* Note that if query fails it is not a hard failure */
2232 wacom_query_tablet_data(wacom);
2235 /* touch only Bamboo doesn't support pen */
2236 if ((features->type == BAMBOO_TOUCH) &&
2237 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2238 cancel_delayed_work_sync(&wacom->init_work);
2239 _wacom_query_tablet_data(wacom);
2240 error = -ENODEV;
2241 goto fail_quirks;
2244 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2245 error = hid_hw_open(hdev);
2247 wacom_set_shared_values(wacom_wac);
2248 devres_close_group(&hdev->dev, wacom);
2250 return 0;
2252 fail_quirks:
2253 hid_hw_stop(hdev);
2254 fail:
2255 wacom_release_resources(wacom);
2256 return error;
2259 static void wacom_wireless_work(struct work_struct *work)
2261 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2262 struct usb_device *usbdev = wacom->usbdev;
2263 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2264 struct hid_device *hdev1, *hdev2;
2265 struct wacom *wacom1, *wacom2;
2266 struct wacom_wac *wacom_wac1, *wacom_wac2;
2267 int error;
2270 * Regardless if this is a disconnect or a new tablet,
2271 * remove any existing input and battery devices.
2274 wacom_destroy_battery(wacom);
2276 /* Stylus interface */
2277 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2278 wacom1 = hid_get_drvdata(hdev1);
2279 wacom_wac1 = &(wacom1->wacom_wac);
2280 wacom_release_resources(wacom1);
2282 /* Touch interface */
2283 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2284 wacom2 = hid_get_drvdata(hdev2);
2285 wacom_wac2 = &(wacom2->wacom_wac);
2286 wacom_release_resources(wacom2);
2288 if (wacom_wac->pid == 0) {
2289 hid_info(wacom->hdev, "wireless tablet disconnected\n");
2290 } else {
2291 const struct hid_device_id *id = wacom_ids;
2293 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2294 wacom_wac->pid);
2296 while (id->bus) {
2297 if (id->vendor == USB_VENDOR_ID_WACOM &&
2298 id->product == wacom_wac->pid)
2299 break;
2300 id++;
2303 if (!id->bus) {
2304 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2305 return;
2308 /* Stylus interface */
2309 wacom_wac1->features =
2310 *((struct wacom_features *)id->driver_data);
2312 wacom_wac1->pid = wacom_wac->pid;
2313 hid_hw_stop(hdev1);
2314 error = wacom_parse_and_register(wacom1, true);
2315 if (error)
2316 goto fail;
2318 /* Touch interface */
2319 if (wacom_wac1->features.touch_max ||
2320 (wacom_wac1->features.type >= INTUOSHT &&
2321 wacom_wac1->features.type <= BAMBOO_PT)) {
2322 wacom_wac2->features =
2323 *((struct wacom_features *)id->driver_data);
2324 wacom_wac2->pid = wacom_wac->pid;
2325 hid_hw_stop(hdev2);
2326 error = wacom_parse_and_register(wacom2, true);
2327 if (error)
2328 goto fail;
2331 strlcpy(wacom_wac->name, wacom_wac1->name,
2332 sizeof(wacom_wac->name));
2333 error = wacom_initialize_battery(wacom);
2334 if (error)
2335 goto fail;
2338 return;
2340 fail:
2341 wacom_release_resources(wacom1);
2342 wacom_release_resources(wacom2);
2343 return;
2346 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2348 struct wacom_remote *remote = wacom->remote;
2349 u32 serial = remote->remotes[index].serial;
2350 int i;
2351 unsigned long flags;
2353 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2354 if (remote->remotes[i].serial == serial) {
2356 spin_lock_irqsave(&remote->remote_lock, flags);
2357 remote->remotes[i].registered = false;
2358 spin_unlock_irqrestore(&remote->remote_lock, flags);
2360 if (remote->remotes[i].battery.battery)
2361 devres_release_group(&wacom->hdev->dev,
2362 &remote->remotes[i].battery.bat_desc);
2364 if (remote->remotes[i].group.name)
2365 devres_release_group(&wacom->hdev->dev,
2366 &remote->remotes[i]);
2368 remote->remotes[i].serial = 0;
2369 remote->remotes[i].group.name = NULL;
2370 remote->remotes[i].battery.battery = NULL;
2371 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2376 static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2377 unsigned int index)
2379 struct wacom_remote *remote = wacom->remote;
2380 struct device *dev = &wacom->hdev->dev;
2381 int error, k;
2383 /* A remote can pair more than once with an EKR,
2384 * check to make sure this serial isn't already paired.
2386 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2387 if (remote->remotes[k].serial == serial)
2388 break;
2391 if (k < WACOM_MAX_REMOTES) {
2392 remote->remotes[index].serial = serial;
2393 return 0;
2396 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2397 return -ENOMEM;
2399 error = wacom_remote_create_attr_group(wacom, serial, index);
2400 if (error)
2401 goto fail;
2403 remote->remotes[index].input = wacom_allocate_input(wacom);
2404 if (!remote->remotes[index].input) {
2405 error = -ENOMEM;
2406 goto fail;
2408 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2409 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2411 if (!remote->remotes[index].input->name) {
2412 error = -EINVAL;
2413 goto fail;
2416 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2417 &wacom->wacom_wac);
2418 if (error)
2419 goto fail;
2421 remote->remotes[index].serial = serial;
2423 error = input_register_device(remote->remotes[index].input);
2424 if (error)
2425 goto fail;
2427 error = wacom_led_groups_alloc_and_register_one(
2428 &remote->remotes[index].input->dev,
2429 wacom, index, 3, true);
2430 if (error)
2431 goto fail;
2433 remote->remotes[index].registered = true;
2435 devres_close_group(dev, &remote->remotes[index]);
2436 return 0;
2438 fail:
2439 devres_release_group(dev, &remote->remotes[index]);
2440 remote->remotes[index].serial = 0;
2441 return error;
2444 static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2446 struct wacom_remote *remote = wacom->remote;
2447 int error;
2449 if (!remote->remotes[index].registered)
2450 return 0;
2452 if (remote->remotes[index].battery.battery)
2453 return 0;
2455 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2456 return 0;
2458 error = __wacom_initialize_battery(wacom,
2459 &wacom->remote->remotes[index].battery);
2460 if (error)
2461 return error;
2463 return 0;
2466 static void wacom_remote_work(struct work_struct *work)
2468 struct wacom *wacom = container_of(work, struct wacom, remote_work);
2469 struct wacom_remote *remote = wacom->remote;
2470 struct wacom_remote_data data;
2471 unsigned long flags;
2472 unsigned int count;
2473 u32 serial;
2474 int i;
2476 spin_lock_irqsave(&remote->remote_lock, flags);
2478 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2480 if (count != sizeof(data)) {
2481 hid_err(wacom->hdev,
2482 "workitem triggered without status available\n");
2483 spin_unlock_irqrestore(&remote->remote_lock, flags);
2484 return;
2487 if (!kfifo_is_empty(&remote->remote_fifo))
2488 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2490 spin_unlock_irqrestore(&remote->remote_lock, flags);
2492 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2493 serial = data.remote[i].serial;
2494 if (data.remote[i].connected) {
2496 if (remote->remotes[i].serial == serial) {
2497 wacom_remote_attach_battery(wacom, i);
2498 continue;
2501 if (remote->remotes[i].serial)
2502 wacom_remote_destroy_one(wacom, i);
2504 wacom_remote_create_one(wacom, serial, i);
2506 } else if (remote->remotes[i].serial) {
2507 wacom_remote_destroy_one(wacom, i);
2512 static void wacom_mode_change_work(struct work_struct *work)
2514 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2515 struct wacom_shared *shared = wacom->wacom_wac.shared;
2516 struct wacom *wacom1 = NULL;
2517 struct wacom *wacom2 = NULL;
2518 bool is_direct = wacom->wacom_wac.is_direct_mode;
2519 int error = 0;
2521 if (shared->pen) {
2522 wacom1 = hid_get_drvdata(shared->pen);
2523 wacom_release_resources(wacom1);
2524 hid_hw_stop(wacom1->hdev);
2525 wacom1->wacom_wac.has_mode_change = true;
2526 wacom1->wacom_wac.is_direct_mode = is_direct;
2529 if (shared->touch) {
2530 wacom2 = hid_get_drvdata(shared->touch);
2531 wacom_release_resources(wacom2);
2532 hid_hw_stop(wacom2->hdev);
2533 wacom2->wacom_wac.has_mode_change = true;
2534 wacom2->wacom_wac.is_direct_mode = is_direct;
2537 if (wacom1) {
2538 error = wacom_parse_and_register(wacom1, false);
2539 if (error)
2540 return;
2543 if (wacom2) {
2544 error = wacom_parse_and_register(wacom2, false);
2545 if (error)
2546 return;
2549 return;
2552 static int wacom_probe(struct hid_device *hdev,
2553 const struct hid_device_id *id)
2555 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2556 struct usb_device *dev = interface_to_usbdev(intf);
2557 struct wacom *wacom;
2558 struct wacom_wac *wacom_wac;
2559 struct wacom_features *features;
2560 int error;
2562 if (!id->driver_data)
2563 return -EINVAL;
2565 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2567 /* hid-core sets this quirk for the boot interface */
2568 hdev->quirks &= ~HID_QUIRK_NOGET;
2570 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2571 if (!wacom)
2572 return -ENOMEM;
2574 hid_set_drvdata(hdev, wacom);
2575 wacom->hdev = hdev;
2577 wacom_wac = &wacom->wacom_wac;
2578 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2579 features = &wacom_wac->features;
2581 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2582 error = -ENODEV;
2583 goto fail;
2586 wacom_wac->hid_data.inputmode = -1;
2587 wacom_wac->mode_report = -1;
2589 wacom->usbdev = dev;
2590 wacom->intf = intf;
2591 mutex_init(&wacom->lock);
2592 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2593 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2594 INIT_WORK(&wacom->battery_work, wacom_battery_work);
2595 INIT_WORK(&wacom->remote_work, wacom_remote_work);
2596 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
2598 /* ask for the report descriptor to be loaded by HID */
2599 error = hid_parse(hdev);
2600 if (error) {
2601 hid_err(hdev, "parse failed\n");
2602 goto fail;
2605 error = wacom_parse_and_register(wacom, false);
2606 if (error)
2607 goto fail;
2609 if (hdev->bus == BUS_BLUETOOTH) {
2610 error = device_create_file(&hdev->dev, &dev_attr_speed);
2611 if (error)
2612 hid_warn(hdev,
2613 "can't create sysfs speed attribute err: %d\n",
2614 error);
2617 return 0;
2619 fail:
2620 hid_set_drvdata(hdev, NULL);
2621 return error;
2624 static void wacom_remove(struct hid_device *hdev)
2626 struct wacom *wacom = hid_get_drvdata(hdev);
2627 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2628 struct wacom_features *features = &wacom_wac->features;
2630 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2631 hid_hw_close(hdev);
2633 hid_hw_stop(hdev);
2635 cancel_delayed_work_sync(&wacom->init_work);
2636 cancel_work_sync(&wacom->wireless_work);
2637 cancel_work_sync(&wacom->battery_work);
2638 cancel_work_sync(&wacom->remote_work);
2639 cancel_work_sync(&wacom->mode_change_work);
2640 if (hdev->bus == BUS_BLUETOOTH)
2641 device_remove_file(&hdev->dev, &dev_attr_speed);
2643 /* make sure we don't trigger the LEDs */
2644 wacom_led_groups_release(wacom);
2646 if (wacom->wacom_wac.features.type != REMOTE)
2647 wacom_release_resources(wacom);
2649 hid_set_drvdata(hdev, NULL);
2652 #ifdef CONFIG_PM
2653 static int wacom_resume(struct hid_device *hdev)
2655 struct wacom *wacom = hid_get_drvdata(hdev);
2657 mutex_lock(&wacom->lock);
2659 /* switch to wacom mode first */
2660 _wacom_query_tablet_data(wacom);
2661 wacom_led_control(wacom);
2663 mutex_unlock(&wacom->lock);
2665 return 0;
2668 static int wacom_reset_resume(struct hid_device *hdev)
2670 return wacom_resume(hdev);
2672 #endif /* CONFIG_PM */
2674 static struct hid_driver wacom_driver = {
2675 .name = "wacom",
2676 .id_table = wacom_ids,
2677 .probe = wacom_probe,
2678 .remove = wacom_remove,
2679 .report = wacom_wac_report,
2680 #ifdef CONFIG_PM
2681 .resume = wacom_resume,
2682 .reset_resume = wacom_reset_resume,
2683 #endif
2684 .raw_event = wacom_raw_event,
2686 module_hid_driver(wacom_driver);
2688 MODULE_VERSION(DRIVER_VERSION);
2689 MODULE_AUTHOR(DRIVER_AUTHOR);
2690 MODULE_DESCRIPTION(DRIVER_DESC);
2691 MODULE_LICENSE("GPL");