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