HID: wacom: EKR: add a worker to add/remove resources on addition/removal
[linux-2.6-block.git] / drivers / hid / wacom_sys.c
CommitLineData
3bea733a 1/*
4104d13f 2 * drivers/input/tablet/wacom_sys.c
3bea733a 3 *
232f5693 4 * USB Wacom tablet support - system specific code
3bea733a
PC
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
3bea733a 14#include "wacom_wac.h"
51269fe8 15#include "wacom.h"
b58ba1ba 16#include <linux/input/mt.h>
3bea733a 17
a417ea44 18#define WAC_MSG_RETRIES 5
3bea733a 19
912ca216 20#define WAC_CMD_WL_LED_CONTROL 0x03
5d7e7d47
EH
21#define WAC_CMD_LED_CONTROL 0x20
22#define WAC_CMD_ICON_START 0x21
23#define WAC_CMD_ICON_XFER 0x23
849e2f06 24#define WAC_CMD_ICON_BT_XFER 0x26
5d7e7d47 25#define WAC_CMD_RETRIES 10
72b236d6
AS
26#define WAC_CMD_DELETE_PAIRING 0x20
27#define WAC_CMD_UNPAIR_ALL 0xFF
5d7e7d47 28
e0984bc3
PC
29#define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
30#define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
72b236d6 31#define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
e0984bc3 32
c64d8834
PC
33static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
34 size_t size, unsigned int retries)
3bea733a 35{
5d7e7d47
EH
36 int retval;
37
38 do {
c64d8834 39 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
27b20a9d 40 HID_REQ_GET_REPORT);
aef3156d
JG
41 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
42
43 if (retval < 0)
44 hid_err(hdev, "wacom_get_report: ran out of retries "
45 "(last error = %d)\n", retval);
5d7e7d47
EH
46
47 return retval;
3bea733a
PC
48}
49
296b7378
PF
50static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
51 size_t size, unsigned int retries)
3bea733a 52{
5d7e7d47
EH
53 int retval;
54
55 do {
296b7378 56 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
27b20a9d 57 HID_REQ_SET_REPORT);
aef3156d
JG
58 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
59
60 if (retval < 0)
61 hid_err(hdev, "wacom_set_report: ran out of retries "
62 "(last error = %d)\n", retval);
5d7e7d47
EH
63
64 return retval;
3bea733a
PC
65}
66
29b47391
BT
67static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
68 u8 *raw_data, int size)
3bea733a 69{
29b47391 70 struct wacom *wacom = hid_get_drvdata(hdev);
3bea733a 71
29b47391
BT
72 if (size > WACOM_PKGLEN_MAX)
73 return 1;
3bea733a 74
29b47391 75 memcpy(wacom->wacom_wac.data, raw_data, size);
3bea733a 76
29b47391
BT
77 wacom_wac_irq(&wacom->wacom_wac, size);
78
79 return 0;
3bea733a
PC
80}
81
3bea733a
PC
82static int wacom_open(struct input_dev *dev)
83{
7791bdae 84 struct wacom *wacom = input_get_drvdata(dev);
29b47391 85
dff67416 86 return hid_hw_open(wacom->hdev);
3bea733a
PC
87}
88
89static void wacom_close(struct input_dev *dev)
90{
7791bdae 91 struct wacom *wacom = input_get_drvdata(dev);
3bea733a 92
3dad188e
BT
93 /*
94 * wacom->hdev should never be null, but surprisingly, I had the case
95 * once while unplugging the Wacom Wireless Receiver.
96 */
97 if (wacom->hdev)
98 hid_hw_close(wacom->hdev);
3bea733a
PC
99}
100
115d5e12 101/*
198fdee2 102 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
115d5e12
JG
103 */
104static int wacom_calc_hid_res(int logical_extents, int physical_extents,
c669fb2b 105 unsigned unit, int exponent)
115d5e12 106{
198fdee2
BT
107 struct hid_field field = {
108 .logical_maximum = logical_extents,
109 .physical_maximum = physical_extents,
110 .unit = unit,
111 .unit_exponent = exponent,
112 };
113
114 return hidinput_calc_abs_res(&field, ABS_X);
115d5e12
JG
115}
116
c669fb2b
BT
117static void wacom_feature_mapping(struct hid_device *hdev,
118 struct hid_field *field, struct hid_usage *usage)
f393ee2b 119{
c669fb2b
BT
120 struct wacom *wacom = hid_get_drvdata(hdev);
121 struct wacom_features *features = &wacom->wacom_wac.features;
5ae6e89f 122 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
8ffffd52
BT
123 u8 *data;
124 int ret;
f393ee2b 125
c669fb2b
BT
126 switch (usage->hid) {
127 case HID_DG_CONTACTMAX:
128 /* leave touch_max as is if predefined */
8ffffd52
BT
129 if (!features->touch_max) {
130 /* read manually */
131 data = kzalloc(2, GFP_KERNEL);
132 if (!data)
133 break;
134 data[0] = field->report->id;
135 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
05e8fd92
JG
136 data, 2, WAC_CMD_RETRIES);
137 if (ret == 2) {
8ffffd52 138 features->touch_max = data[1];
05e8fd92
JG
139 } else {
140 features->touch_max = 16;
141 hid_warn(hdev, "wacom_feature_mapping: "
142 "could not get HID_DG_CONTACTMAX, "
143 "defaulting to %d\n",
144 features->touch_max);
145 }
8ffffd52
BT
146 kfree(data);
147 }
c669fb2b 148 break;
5ae6e89f
BT
149 case HID_DG_INPUTMODE:
150 /* Ignore if value index is out of bounds. */
151 if (usage->usage_index >= field->report_count) {
152 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
153 break;
154 }
155
156 hid_data->inputmode = field->report->id;
157 hid_data->inputmode_index = usage->usage_index;
158 break;
326ea2a9
JG
159
160 case HID_UP_DIGITIZER:
161 if (field->report->id == 0x0B &&
162 (field->application == WACOM_G9_DIGITIZER ||
163 field->application == WACOM_G11_DIGITIZER)) {
164 wacom->wacom_wac.mode_report = field->report->id;
165 wacom->wacom_wac.mode_value = 0;
166 }
167 break;
168
169 case WACOM_G9_PAGE:
170 case WACOM_G11_PAGE:
171 if (field->report->id == 0x03 &&
172 (field->application == WACOM_G9_TOUCHSCREEN ||
173 field->application == WACOM_G11_TOUCHSCREEN)) {
174 wacom->wacom_wac.mode_report = field->report->id;
175 wacom->wacom_wac.mode_value = 0;
176 }
177 break;
f393ee2b
PC
178 }
179}
180
428f8588
CB
181/*
182 * Interface Descriptor of wacom devices can be incomplete and
183 * inconsistent so wacom_features table is used to store stylus
184 * device's packet lengths, various maximum values, and tablet
185 * resolution based on product ID's.
186 *
187 * For devices that contain 2 interfaces, wacom_features table is
188 * inaccurate for the touch interface. Since the Interface Descriptor
189 * for touch interfaces has pretty complete data, this function exists
190 * to query tablet for this missing information instead of hard coding in
191 * an additional table.
192 *
193 * A typical Interface Descriptor for a stylus will contain a
194 * boot mouse application collection that is not of interest and this
195 * function will ignore it.
196 *
197 * It also contains a digitizer application collection that also is not
198 * of interest since any information it contains would be duplicate
199 * of what is in wacom_features. Usually it defines a report of an array
200 * of bytes that could be used as max length of the stylus packet returned.
201 * If it happens to define a Digitizer-Stylus Physical Collection then
202 * the X and Y logical values contain valid data but it is ignored.
203 *
204 * A typical Interface Descriptor for a touch interface will contain a
205 * Digitizer-Finger Physical Collection which will define both logical
206 * X/Y maximum as well as the physical size of tablet. Since touch
207 * interfaces haven't supported pressure or distance, this is enough
208 * information to override invalid values in the wacom_features table.
4134361a 209 *
c669fb2b
BT
210 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
211 * data. We deal with them after returning from this function.
428f8588 212 */
c669fb2b
BT
213static void wacom_usage_mapping(struct hid_device *hdev,
214 struct hid_field *field, struct hid_usage *usage)
545f4e99 215{
c669fb2b
BT
216 struct wacom *wacom = hid_get_drvdata(hdev);
217 struct wacom_features *features = &wacom->wacom_wac.features;
d97a5522
BT
218 bool finger = WACOM_FINGER_FIELD(field);
219 bool pen = WACOM_PEN_FIELD(field);
e9fc413f 220
c669fb2b
BT
221 /*
222 * Requiring Stylus Usage will ignore boot mouse
223 * X/Y values and some cases of invalid Digitizer X/Y
224 * values commonly reported.
225 */
042628ab 226 if (pen)
aa86b18c 227 features->device_type |= WACOM_DEVICETYPE_PEN;
042628ab 228 else if (finger)
aa86b18c 229 features->device_type |= WACOM_DEVICETYPE_TOUCH;
042628ab 230 else
c669fb2b
BT
231 return;
232
30ebc1ae
PC
233 /*
234 * Bamboo models do not support HID_DG_CONTACTMAX.
235 * And, Bamboo Pen only descriptor contains touch.
236 */
3b164a00 237 if (features->type > BAMBOO_PT) {
30ebc1ae
PC
238 /* ISDv4 touch devices at least supports one touch point */
239 if (finger && !features->touch_max)
240 features->touch_max = 1;
241 }
c669fb2b
BT
242
243 switch (usage->hid) {
244 case HID_GD_X:
245 features->x_max = field->logical_maximum;
246 if (finger) {
c669fb2b 247 features->x_phy = field->physical_maximum;
3b164a00
PC
248 if ((features->type != BAMBOO_PT) &&
249 (features->type != BAMBOO_TOUCH)) {
c669fb2b
BT
250 features->unit = field->unit;
251 features->unitExpo = field->unit_exponent;
545f4e99 252 }
c669fb2b
BT
253 }
254 break;
255 case HID_GD_Y:
256 features->y_max = field->logical_maximum;
257 if (finger) {
258 features->y_phy = field->physical_maximum;
3b164a00
PC
259 if ((features->type != BAMBOO_PT) &&
260 (features->type != BAMBOO_TOUCH)) {
c669fb2b
BT
261 features->unit = field->unit;
262 features->unitExpo = field->unit_exponent;
263 }
264 }
265 break;
266 case HID_DG_TIPPRESSURE:
267 if (pen)
268 features->pressure_max = field->logical_maximum;
269 break;
270 }
7704ac93
BT
271
272 if (features->type == HID_GENERIC)
273 wacom_wac_usage_mapping(hdev, field, usage);
c669fb2b 274}
4134361a 275
b58ba1ba
JG
276static void wacom_post_parse_hid(struct hid_device *hdev,
277 struct wacom_features *features)
278{
279 struct wacom *wacom = hid_get_drvdata(hdev);
280 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
281
282 if (features->type == HID_GENERIC) {
283 /* Any last-minute generic device setup */
284 if (features->touch_max > 1) {
2a6cdbdd 285 input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
b58ba1ba
JG
286 INPUT_MT_DIRECT);
287 }
288 }
289}
290
c669fb2b
BT
291static void wacom_parse_hid(struct hid_device *hdev,
292 struct wacom_features *features)
293{
294 struct hid_report_enum *rep_enum;
295 struct hid_report *hreport;
296 int i, j;
297
298 /* check features first */
299 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
300 list_for_each_entry(hreport, &rep_enum->report_list, list) {
301 for (i = 0; i < hreport->maxfield; i++) {
302 /* Ignore if report count is out of bounds. */
303 if (hreport->field[i]->report_count < 1)
304 continue;
305
306 for (j = 0; j < hreport->field[i]->maxusage; j++) {
307 wacom_feature_mapping(hdev, hreport->field[i],
308 hreport->field[i]->usage + j);
4134361a 309 }
545f4e99
PC
310 }
311 }
312
c669fb2b
BT
313 /* now check the input usages */
314 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
315 list_for_each_entry(hreport, &rep_enum->report_list, list) {
316
317 if (!hreport->maxfield)
318 continue;
319
320 for (i = 0; i < hreport->maxfield; i++)
321 for (j = 0; j < hreport->field[i]->maxusage; j++)
322 wacom_usage_mapping(hdev, hreport->field[i],
323 hreport->field[i]->usage + j);
324 }
b58ba1ba
JG
325
326 wacom_post_parse_hid(hdev, features);
545f4e99
PC
327}
328
5ae6e89f
BT
329static int wacom_hid_set_device_mode(struct hid_device *hdev)
330{
331 struct wacom *wacom = hid_get_drvdata(hdev);
332 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
333 struct hid_report *r;
334 struct hid_report_enum *re;
335
336 if (hid_data->inputmode < 0)
337 return 0;
338
339 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
340 r = re->report_id_hash[hid_data->inputmode];
341 if (r) {
342 r->field[0]->value[hid_data->inputmode_index] = 2;
343 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
344 }
345 return 0;
346}
347
326ea2a9
JG
348static int wacom_set_device_mode(struct hid_device *hdev,
349 struct wacom_wac *wacom_wac)
3b7307c2 350{
326ea2a9
JG
351 u8 *rep_data;
352 struct hid_report *r;
353 struct hid_report_enum *re;
354 int length;
fe494bc2 355 int error = -ENOMEM, limit = 0;
3b7307c2 356
326ea2a9
JG
357 if (wacom_wac->mode_report < 0)
358 return 0;
359
360 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
361 r = re->report_id_hash[wacom_wac->mode_report];
362 if (!r)
363 return -EINVAL;
364
365 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
3b7307c2 366 if (!rep_data)
326ea2a9
JG
367 return -ENOMEM;
368
369 length = hid_report_len(r);
ec67bbed 370
fe494bc2 371 do {
326ea2a9
JG
372 rep_data[0] = wacom_wac->mode_report;
373 rep_data[1] = wacom_wac->mode_value;
9937c026 374
296b7378
PF
375 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
376 length, 1);
3cb83157 377 if (error >= 0)
27b20a9d 378 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
c64d8834 379 rep_data, length, 1);
326ea2a9
JG
380 } while (error >= 0 &&
381 rep_data[1] != wacom_wac->mode_report &&
382 limit++ < WAC_MSG_RETRIES);
fe494bc2
JG
383
384 kfree(rep_data);
385
386 return error < 0 ? error : 0;
387}
388
f81a1295
BT
389static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
390 struct wacom_features *features)
391{
387142bb
BT
392 struct wacom *wacom = hid_get_drvdata(hdev);
393 int ret;
394 u8 rep_data[2];
395
396 switch (features->type) {
397 case GRAPHIRE_BT:
398 rep_data[0] = 0x03;
399 rep_data[1] = 0x00;
296b7378
PF
400 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
401 3);
387142bb
BT
402
403 if (ret >= 0) {
404 rep_data[0] = speed == 0 ? 0x05 : 0x06;
405 rep_data[1] = 0x00;
406
407 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
296b7378 408 rep_data, 2, 3);
387142bb
BT
409
410 if (ret >= 0) {
411 wacom->wacom_wac.bt_high_speed = speed;
412 return 0;
413 }
414 }
415
416 /*
417 * Note that if the raw queries fail, it's not a hard failure
418 * and it is safe to continue
419 */
420 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
421 rep_data[0], ret);
422 break;
81af7e61
BT
423 case INTUOS4WL:
424 if (speed == 1)
425 wacom->wacom_wac.bt_features &= ~0x20;
426 else
427 wacom->wacom_wac.bt_features |= 0x20;
428
429 rep_data[0] = 0x03;
430 rep_data[1] = wacom->wacom_wac.bt_features;
431
296b7378
PF
432 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
433 1);
81af7e61
BT
434 if (ret >= 0)
435 wacom->wacom_wac.bt_high_speed = speed;
436 break;
387142bb
BT
437 }
438
f81a1295
BT
439 return 0;
440}
441
fe494bc2
JG
442/*
443 * Switch the tablet into its most-capable mode. Wacom tablets are
444 * typically configured to power-up in a mode which sends mouse-like
445 * reports to the OS. To get absolute position, pressure data, etc.
446 * from the tablet, it is necessary to switch the tablet out of this
447 * mode and into one which sends the full range of tablet data.
448 */
27b20a9d
BT
449static int wacom_query_tablet_data(struct hid_device *hdev,
450 struct wacom_features *features)
fe494bc2 451{
326ea2a9
JG
452 struct wacom *wacom = hid_get_drvdata(hdev);
453 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
454
f81a1295
BT
455 if (hdev->bus == BUS_BLUETOOTH)
456 return wacom_bt_query_tablet_data(hdev, 1, features);
457
326ea2a9
JG
458 if (features->type != HID_GENERIC) {
459 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
460 if (features->type > TABLETPC) {
461 /* MT Tablet PC touch */
462 wacom_wac->mode_report = 3;
463 wacom_wac->mode_value = 4;
464 } else if (features->type == WACOM_24HDT) {
465 wacom_wac->mode_report = 18;
466 wacom_wac->mode_value = 2;
467 } else if (features->type == WACOM_27QHDT) {
468 wacom_wac->mode_report = 131;
469 wacom_wac->mode_value = 2;
470 } else if (features->type == BAMBOO_PAD) {
471 wacom_wac->mode_report = 2;
472 wacom_wac->mode_value = 2;
473 }
474 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
475 if (features->type <= BAMBOO_PT) {
476 wacom_wac->mode_report = 2;
477 wacom_wac->mode_value = 2;
478 }
1963518b 479 }
ec67bbed 480 }
3b7307c2 481
326ea2a9
JG
482 wacom_set_device_mode(hdev, wacom_wac);
483
484 if (features->type == HID_GENERIC)
485 return wacom_hid_set_device_mode(hdev);
486
fe494bc2 487 return 0;
3b7307c2
DT
488}
489
c669fb2b 490static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
1963518b 491 struct wacom_features *features)
ec67bbed 492{
27b20a9d
BT
493 struct wacom *wacom = hid_get_drvdata(hdev);
494 struct usb_interface *intf = wacom->intf;
ec67bbed 495
fed87e65 496 /* default features */
fed87e65
HR
497 features->x_fuzz = 4;
498 features->y_fuzz = 4;
499 features->pressure_fuzz = 0;
bef7e200
JG
500 features->distance_fuzz = 1;
501 features->tilt_fuzz = 1;
ec67bbed 502
d3825d51
CB
503 /*
504 * The wireless device HID is basic and layout conflicts with
505 * other tablets (monitor and touch interface can look like pen).
506 * Skip the query for this type and modify defaults based on
507 * interface number.
508 */
509 if (features->type == WIRELESS) {
3f14a63a 510 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
ccad85cc 511 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
3f14a63a 512 else
aa86b18c 513 features->device_type = WACOM_DEVICETYPE_NONE;
3f14a63a 514 return;
d3825d51
CB
515 }
516
c669fb2b 517 wacom_parse_hid(hdev, features);
ec67bbed
PC
518}
519
4451e088 520struct wacom_hdev_data {
4492efff
PC
521 struct list_head list;
522 struct kref kref;
4451e088 523 struct hid_device *dev;
4492efff
PC
524 struct wacom_shared shared;
525};
526
527static LIST_HEAD(wacom_udev_list);
528static DEFINE_MUTEX(wacom_udev_list_lock);
529
4451e088
BT
530static bool wacom_are_sibling(struct hid_device *hdev,
531 struct hid_device *sibling)
aea2bf6a 532{
4451e088
BT
533 struct wacom *wacom = hid_get_drvdata(hdev);
534 struct wacom_features *features = &wacom->wacom_wac.features;
535 int vid = features->oVid;
536 int pid = features->oPid;
537 int n1,n2;
538
539 if (vid == 0 && pid == 0) {
540 vid = hdev->vendor;
541 pid = hdev->product;
542 }
aea2bf6a 543
4451e088
BT
544 if (vid != sibling->vendor || pid != sibling->product)
545 return false;
aea2bf6a 546
4451e088
BT
547 /* Compare the physical path. */
548 n1 = strrchr(hdev->phys, '.') - hdev->phys;
549 n2 = strrchr(sibling->phys, '.') - sibling->phys;
550 if (n1 != n2 || n1 <= 0 || n2 <= 0)
551 return false;
aea2bf6a 552
4451e088 553 return !strncmp(hdev->phys, sibling->phys, n1);
aea2bf6a
JG
554}
555
4451e088 556static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
4492efff 557{
4451e088 558 struct wacom_hdev_data *data;
4492efff
PC
559
560 list_for_each_entry(data, &wacom_udev_list, list) {
4451e088 561 if (wacom_are_sibling(hdev, data->dev)) {
4492efff
PC
562 kref_get(&data->kref);
563 return data;
564 }
565 }
566
567 return NULL;
568}
569
1c817c83
BT
570static void wacom_release_shared_data(struct kref *kref)
571{
572 struct wacom_hdev_data *data =
573 container_of(kref, struct wacom_hdev_data, kref);
574
575 mutex_lock(&wacom_udev_list_lock);
576 list_del(&data->list);
577 mutex_unlock(&wacom_udev_list_lock);
578
579 kfree(data);
580}
581
582static void wacom_remove_shared_data(void *res)
583{
584 struct wacom *wacom = res;
585 struct wacom_hdev_data *data;
586 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
587
588 if (wacom_wac->shared) {
589 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
590 shared);
591
592 if (wacom_wac->shared->touch == wacom->hdev)
593 wacom_wac->shared->touch = NULL;
594 else if (wacom_wac->shared->pen == wacom->hdev)
595 wacom_wac->shared->pen = NULL;
596
597 kref_put(&data->kref, wacom_release_shared_data);
598 wacom_wac->shared = NULL;
599 }
600}
601
4451e088 602static int wacom_add_shared_data(struct hid_device *hdev)
4492efff 603{
4451e088
BT
604 struct wacom *wacom = hid_get_drvdata(hdev);
605 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
606 struct wacom_hdev_data *data;
4492efff
PC
607 int retval = 0;
608
609 mutex_lock(&wacom_udev_list_lock);
610
4451e088 611 data = wacom_get_hdev_data(hdev);
4492efff 612 if (!data) {
4451e088 613 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
4492efff
PC
614 if (!data) {
615 retval = -ENOMEM;
616 goto out;
617 }
618
619 kref_init(&data->kref);
4451e088 620 data->dev = hdev;
4492efff
PC
621 list_add_tail(&data->list, &wacom_udev_list);
622 }
623
4451e088 624 wacom_wac->shared = &data->shared;
4492efff 625
1c817c83
BT
626 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
627 if (retval) {
628 mutex_unlock(&wacom_udev_list_lock);
629 wacom_remove_shared_data(wacom);
630 return retval;
631 }
632
aa86b18c 633 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
a97ac104 634 wacom_wac->shared->touch = hdev;
aa86b18c 635 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
a97ac104
BT
636 wacom_wac->shared->pen = hdev;
637
4492efff
PC
638out:
639 mutex_unlock(&wacom_udev_list_lock);
640 return retval;
641}
642
5d7e7d47
EH
643static int wacom_led_control(struct wacom *wacom)
644{
645 unsigned char *buf;
9b5b95dd 646 int retval;
912ca216
PC
647 unsigned char report_id = WAC_CMD_LED_CONTROL;
648 int buf_size = 9;
5d7e7d47 649
a50aac71
BT
650 if (!wacom->led.groups)
651 return -ENOTSUPP;
652
912ca216
PC
653 if (wacom->wacom_wac.pid) { /* wireless connected */
654 report_id = WAC_CMD_WL_LED_CONTROL;
655 buf_size = 13;
656 }
657 buf = kzalloc(buf_size, GFP_KERNEL);
5d7e7d47
EH
658 if (!buf)
659 return -ENOMEM;
660
9b5b95dd 661 if (wacom->wacom_wac.features.type >= INTUOS5S &&
9a35c411 662 wacom->wacom_wac.features.type <= INTUOSPL) {
9b5b95dd
JG
663 /*
664 * Touch Ring and crop mark LED luminance may take on
665 * one of four values:
666 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
667 */
a50aac71 668 int ring_led = wacom->led.groups[0].select & 0x03;
9b5b95dd
JG
669 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
670 int crop_lum = 0;
912ca216
PC
671 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
672
673 buf[0] = report_id;
674 if (wacom->wacom_wac.pid) {
675 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
676 buf, buf_size, WAC_CMD_RETRIES);
677 buf[0] = report_id;
678 buf[4] = led_bits;
679 } else
680 buf[1] = led_bits;
9b5b95dd
JG
681 }
682 else {
a50aac71 683 int led = wacom->led.groups[0].select | 0x4;
9b5b95dd
JG
684
685 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
686 wacom->wacom_wac.features.type == WACOM_24HD)
a50aac71 687 led |= (wacom->led.groups[1].select << 4) | 0x40;
9b5b95dd 688
912ca216 689 buf[0] = report_id;
9b5b95dd
JG
690 buf[1] = led;
691 buf[2] = wacom->led.llv;
692 buf[3] = wacom->led.hlv;
693 buf[4] = wacom->led.img_lum;
694 }
5d7e7d47 695
912ca216 696 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
296b7378 697 WAC_CMD_RETRIES);
5d7e7d47
EH
698 kfree(buf);
699
700 return retval;
701}
702
849e2f06
BT
703static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
704 const unsigned len, const void *img)
5d7e7d47
EH
705{
706 unsigned char *buf;
707 int i, retval;
849e2f06 708 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
5d7e7d47 709
849e2f06 710 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
5d7e7d47
EH
711 if (!buf)
712 return -ENOMEM;
713
714 /* Send 'start' command */
715 buf[0] = WAC_CMD_ICON_START;
716 buf[1] = 1;
296b7378
PF
717 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
718 WAC_CMD_RETRIES);
5d7e7d47
EH
719 if (retval < 0)
720 goto out;
721
849e2f06 722 buf[0] = xfer_id;
5d7e7d47
EH
723 buf[1] = button_id & 0x07;
724 for (i = 0; i < 4; i++) {
725 buf[2] = i;
849e2f06 726 memcpy(buf + 3, img + i * chunk_len, chunk_len);
5d7e7d47 727
27b20a9d 728 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
296b7378 729 buf, chunk_len + 3, WAC_CMD_RETRIES);
5d7e7d47
EH
730 if (retval < 0)
731 break;
732 }
733
734 /* Send 'stop' */
735 buf[0] = WAC_CMD_ICON_START;
736 buf[1] = 0;
296b7378
PF
737 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
738 WAC_CMD_RETRIES);
5d7e7d47
EH
739
740out:
741 kfree(buf);
742 return retval;
743}
744
09e7d941 745static ssize_t wacom_led_select_store(struct device *dev, int set_id,
5d7e7d47
EH
746 const char *buf, size_t count)
747{
ee79a8f8 748 struct hid_device *hdev = to_hid_device(dev);
29b47391 749 struct wacom *wacom = hid_get_drvdata(hdev);
5d7e7d47
EH
750 unsigned int id;
751 int err;
752
753 err = kstrtouint(buf, 10, &id);
754 if (err)
755 return err;
756
757 mutex_lock(&wacom->lock);
758
a50aac71 759 wacom->led.groups[set_id].select = id & 0x3;
5d7e7d47
EH
760 err = wacom_led_control(wacom);
761
762 mutex_unlock(&wacom->lock);
763
764 return err < 0 ? err : count;
765}
766
09e7d941
PC
767#define DEVICE_LED_SELECT_ATTR(SET_ID) \
768static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
769 struct device_attribute *attr, const char *buf, size_t count) \
770{ \
771 return wacom_led_select_store(dev, SET_ID, buf, count); \
772} \
04c59abd
PC
773static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
774 struct device_attribute *attr, char *buf) \
775{ \
ee79a8f8 776 struct hid_device *hdev = to_hid_device(dev);\
29b47391 777 struct wacom *wacom = hid_get_drvdata(hdev); \
37449adc 778 return scnprintf(buf, PAGE_SIZE, "%d\n", \
a50aac71 779 wacom->led.groups[SET_ID].select); \
04c59abd 780} \
e0984bc3 781static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
04c59abd 782 wacom_led##SET_ID##_select_show, \
09e7d941
PC
783 wacom_led##SET_ID##_select_store)
784
785DEVICE_LED_SELECT_ATTR(0);
786DEVICE_LED_SELECT_ATTR(1);
5d7e7d47
EH
787
788static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
789 const char *buf, size_t count)
790{
791 unsigned int value;
792 int err;
793
794 err = kstrtouint(buf, 10, &value);
795 if (err)
796 return err;
797
798 mutex_lock(&wacom->lock);
799
800 *dest = value & 0x7f;
801 err = wacom_led_control(wacom);
802
803 mutex_unlock(&wacom->lock);
804
805 return err < 0 ? err : count;
806}
807
808#define DEVICE_LUMINANCE_ATTR(name, field) \
809static ssize_t wacom_##name##_luminance_store(struct device *dev, \
810 struct device_attribute *attr, const char *buf, size_t count) \
811{ \
ee79a8f8 812 struct hid_device *hdev = to_hid_device(dev);\
29b47391 813 struct wacom *wacom = hid_get_drvdata(hdev); \
5d7e7d47
EH
814 \
815 return wacom_luminance_store(wacom, &wacom->led.field, \
816 buf, count); \
817} \
37449adc
PC
818static ssize_t wacom_##name##_luminance_show(struct device *dev, \
819 struct device_attribute *attr, char *buf) \
820{ \
821 struct wacom *wacom = dev_get_drvdata(dev); \
822 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
823} \
e0984bc3 824static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
37449adc
PC
825 wacom_##name##_luminance_show, \
826 wacom_##name##_luminance_store)
5d7e7d47
EH
827
828DEVICE_LUMINANCE_ATTR(status0, llv);
829DEVICE_LUMINANCE_ATTR(status1, hlv);
830DEVICE_LUMINANCE_ATTR(buttons, img_lum);
831
832static ssize_t wacom_button_image_store(struct device *dev, int button_id,
833 const char *buf, size_t count)
834{
ee79a8f8 835 struct hid_device *hdev = to_hid_device(dev);
29b47391 836 struct wacom *wacom = hid_get_drvdata(hdev);
5d7e7d47 837 int err;
849e2f06
BT
838 unsigned len;
839 u8 xfer_id;
840
841 if (hdev->bus == BUS_BLUETOOTH) {
842 len = 256;
843 xfer_id = WAC_CMD_ICON_BT_XFER;
844 } else {
845 len = 1024;
846 xfer_id = WAC_CMD_ICON_XFER;
847 }
5d7e7d47 848
849e2f06 849 if (count != len)
5d7e7d47
EH
850 return -EINVAL;
851
852 mutex_lock(&wacom->lock);
853
849e2f06 854 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
5d7e7d47
EH
855
856 mutex_unlock(&wacom->lock);
857
858 return err < 0 ? err : count;
859}
860
861#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
862static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
863 struct device_attribute *attr, const char *buf, size_t count) \
864{ \
865 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
866} \
e0984bc3 867static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
5d7e7d47
EH
868 NULL, wacom_btnimg##BUTTON_ID##_store)
869
870DEVICE_BTNIMG_ATTR(0);
871DEVICE_BTNIMG_ATTR(1);
872DEVICE_BTNIMG_ATTR(2);
873DEVICE_BTNIMG_ATTR(3);
874DEVICE_BTNIMG_ATTR(4);
875DEVICE_BTNIMG_ATTR(5);
876DEVICE_BTNIMG_ATTR(6);
877DEVICE_BTNIMG_ATTR(7);
878
09e7d941
PC
879static struct attribute *cintiq_led_attrs[] = {
880 &dev_attr_status_led0_select.attr,
881 &dev_attr_status_led1_select.attr,
882 NULL
883};
884
885static struct attribute_group cintiq_led_attr_group = {
886 .name = "wacom_led",
887 .attrs = cintiq_led_attrs,
888};
889
890static struct attribute *intuos4_led_attrs[] = {
5d7e7d47
EH
891 &dev_attr_status0_luminance.attr,
892 &dev_attr_status1_luminance.attr,
09e7d941 893 &dev_attr_status_led0_select.attr,
5d7e7d47
EH
894 &dev_attr_buttons_luminance.attr,
895 &dev_attr_button0_rawimg.attr,
896 &dev_attr_button1_rawimg.attr,
897 &dev_attr_button2_rawimg.attr,
898 &dev_attr_button3_rawimg.attr,
899 &dev_attr_button4_rawimg.attr,
900 &dev_attr_button5_rawimg.attr,
901 &dev_attr_button6_rawimg.attr,
902 &dev_attr_button7_rawimg.attr,
903 NULL
904};
905
09e7d941 906static struct attribute_group intuos4_led_attr_group = {
5d7e7d47 907 .name = "wacom_led",
09e7d941 908 .attrs = intuos4_led_attrs,
5d7e7d47
EH
909};
910
9b5b95dd
JG
911static struct attribute *intuos5_led_attrs[] = {
912 &dev_attr_status0_luminance.attr,
913 &dev_attr_status_led0_select.attr,
914 NULL
915};
916
917static struct attribute_group intuos5_led_attr_group = {
918 .name = "wacom_led",
919 .attrs = intuos5_led_attrs,
920};
921
2df68a88
BT
922struct wacom_sysfs_group_devres {
923 struct attribute_group *group;
924 struct kobject *root;
925};
926
927static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
928{
929 struct wacom_sysfs_group_devres *devres = res;
930 struct kobject *kobj = devres->root;
931
932 dev_dbg(dev, "%s: dropping reference to %s\n",
933 __func__, devres->group->name);
934 sysfs_remove_group(kobj, devres->group);
935}
936
937static int wacom_devm_sysfs_create_group(struct wacom *wacom,
938 struct attribute_group *group)
939{
940 struct wacom_sysfs_group_devres *devres;
941 int error;
942
943 devres = devres_alloc(wacom_devm_sysfs_group_release,
944 sizeof(struct wacom_sysfs_group_devres),
945 GFP_KERNEL);
946 if (!devres)
947 return -ENOMEM;
948
949 devres->group = group;
950 devres->root = &wacom->hdev->dev.kobj;
951
952 error = sysfs_create_group(devres->root, group);
953 if (error)
954 return error;
955
956 devres_add(&wacom->hdev->dev, devres);
957
958 return 0;
959}
960
a50aac71
BT
961static void wacom_led_groups_release(void *data)
962{
963 struct wacom *wacom = data;
964
965 wacom->led.groups = NULL;
966}
967
968static int wacom_led_groups_allocate(struct wacom *wacom, int count)
969{
970 struct wacom_group_leds *groups;
971 int error;
972
973 groups = devm_kzalloc(&wacom->hdev->dev,
974 sizeof(struct wacom_group_leds) * count,
975 GFP_KERNEL);
976 if (!groups)
977 return -ENOMEM;
978
979 error = devm_add_action_or_reset(&wacom->hdev->dev,
980 wacom_led_groups_release,
981 wacom);
982 if (error)
983 return error;
984
985 wacom->led.groups = groups;
986
987 return 0;
988}
989
5d7e7d47
EH
990static int wacom_initialize_leds(struct wacom *wacom)
991{
992 int error;
993
862cf553
JG
994 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
995 return 0;
996
09e7d941
PC
997 /* Initialize default values */
998 switch (wacom->wacom_wac.features.type) {
a19fc986 999 case INTUOS4S:
09e7d941 1000 case INTUOS4:
81af7e61 1001 case INTUOS4WL:
09e7d941 1002 case INTUOS4L:
f4fa9a6d 1003 wacom->led.llv = 10;
5d7e7d47
EH
1004 wacom->led.hlv = 20;
1005 wacom->led.img_lum = 10;
a50aac71
BT
1006
1007 error = wacom_led_groups_allocate(wacom, 1);
1008 if (error) {
1009 hid_err(wacom->hdev,
1010 "cannot create leds err: %d\n", error);
1011 return error;
1012 }
1013
2df68a88
BT
1014 error = wacom_devm_sysfs_create_group(wacom,
1015 &intuos4_led_attr_group);
09e7d941
PC
1016 break;
1017
246835fc 1018 case WACOM_24HD:
09e7d941 1019 case WACOM_21UX2:
09e7d941
PC
1020 wacom->led.llv = 0;
1021 wacom->led.hlv = 0;
1022 wacom->led.img_lum = 0;
5d7e7d47 1023
a50aac71
BT
1024 error = wacom_led_groups_allocate(wacom, 2);
1025 if (error) {
1026 hid_err(wacom->hdev,
1027 "cannot create leds err: %d\n", error);
1028 return error;
1029 }
1030
2df68a88
BT
1031 error = wacom_devm_sysfs_create_group(wacom,
1032 &cintiq_led_attr_group);
09e7d941
PC
1033 break;
1034
9b5b95dd
JG
1035 case INTUOS5S:
1036 case INTUOS5:
1037 case INTUOS5L:
9a35c411
PC
1038 case INTUOSPS:
1039 case INTUOSPM:
1040 case INTUOSPL:
862cf553
JG
1041 wacom->led.llv = 32;
1042 wacom->led.hlv = 0;
1043 wacom->led.img_lum = 0;
1044
a50aac71
BT
1045 error = wacom_led_groups_allocate(wacom, 1);
1046 if (error) {
1047 hid_err(wacom->hdev,
1048 "cannot create leds err: %d\n", error);
1049 return error;
1050 }
1051
2df68a88
BT
1052 error = wacom_devm_sysfs_create_group(wacom,
1053 &intuos5_led_attr_group);
9b5b95dd
JG
1054 break;
1055
a50aac71
BT
1056 case REMOTE:
1057 error = wacom_led_groups_allocate(wacom, 5);
1058 if (error) {
1059 hid_err(wacom->hdev,
1060 "cannot create leds err: %d\n", error);
1061 return error;
1062 }
1063 return 0;
1064
09e7d941
PC
1065 default:
1066 return 0;
5d7e7d47
EH
1067 }
1068
09e7d941 1069 if (error) {
c31a408f 1070 hid_err(wacom->hdev,
09e7d941
PC
1071 "cannot create sysfs group err: %d\n", error);
1072 return error;
1073 }
1074 wacom_led_control(wacom);
1075
5d7e7d47
EH
1076 return 0;
1077}
1078
a1d552cc 1079static enum power_supply_property wacom_battery_props[] = {
71fa641e 1080 POWER_SUPPLY_PROP_PRESENT,
ac8d1010 1081 POWER_SUPPLY_PROP_STATUS,
6e2a6e80 1082 POWER_SUPPLY_PROP_SCOPE,
a1d552cc
CB
1083 POWER_SUPPLY_PROP_CAPACITY
1084};
1085
7dbd229e
BT
1086static enum power_supply_property wacom_ac_props[] = {
1087 POWER_SUPPLY_PROP_PRESENT,
1088 POWER_SUPPLY_PROP_ONLINE,
1089 POWER_SUPPLY_PROP_SCOPE,
1090};
1091
a1d552cc
CB
1092static int wacom_battery_get_property(struct power_supply *psy,
1093 enum power_supply_property psp,
1094 union power_supply_propval *val)
1095{
297d716f 1096 struct wacom *wacom = power_supply_get_drvdata(psy);
a1d552cc
CB
1097 int ret = 0;
1098
1099 switch (psp) {
71fa641e
JG
1100 case POWER_SUPPLY_PROP_PRESENT:
1101 val->intval = wacom->wacom_wac.bat_connected;
1102 break;
6e2a6e80
BN
1103 case POWER_SUPPLY_PROP_SCOPE:
1104 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1105 break;
a1d552cc
CB
1106 case POWER_SUPPLY_PROP_CAPACITY:
1107 val->intval =
ac8d1010
BT
1108 wacom->wacom_wac.battery_capacity;
1109 break;
1110 case POWER_SUPPLY_PROP_STATUS:
1111 if (wacom->wacom_wac.bat_charging)
1112 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1113 else if (wacom->wacom_wac.battery_capacity == 100 &&
1114 wacom->wacom_wac.ps_connected)
1115 val->intval = POWER_SUPPLY_STATUS_FULL;
b0882cb7
JG
1116 else if (wacom->wacom_wac.ps_connected)
1117 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
ac8d1010
BT
1118 else
1119 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
a1d552cc
CB
1120 break;
1121 default:
1122 ret = -EINVAL;
1123 break;
1124 }
1125
1126 return ret;
1127}
1128
7dbd229e
BT
1129static int wacom_ac_get_property(struct power_supply *psy,
1130 enum power_supply_property psp,
1131 union power_supply_propval *val)
1132{
297d716f 1133 struct wacom *wacom = power_supply_get_drvdata(psy);
7dbd229e
BT
1134 int ret = 0;
1135
1136 switch (psp) {
1137 case POWER_SUPPLY_PROP_PRESENT:
1138 /* fall through */
1139 case POWER_SUPPLY_PROP_ONLINE:
1140 val->intval = wacom->wacom_wac.ps_connected;
1141 break;
1142 case POWER_SUPPLY_PROP_SCOPE:
1143 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1144 break;
1145 default:
1146 ret = -EINVAL;
1147 break;
1148 }
1149 return ret;
1150}
1151
a1d552cc
CB
1152static int wacom_initialize_battery(struct wacom *wacom)
1153{
d70420b9 1154 static atomic_t battery_no = ATOMIC_INIT(0);
b189da90 1155 struct device *dev = &wacom->hdev->dev;
297d716f 1156 struct power_supply_config psy_cfg = { .drv_data = wacom, };
b189da90 1157 struct power_supply_desc *bat_desc = &wacom->battery_desc;
d70420b9 1158 unsigned long n;
b189da90
BT
1159 int error;
1160
1161 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1162 return -ENOMEM;
a1d552cc 1163
ac8d1010 1164 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
297d716f 1165 struct power_supply_desc *ac_desc = &wacom->ac_desc;
d70420b9 1166 n = atomic_inc_return(&battery_no) - 1;
7dbd229e 1167
297d716f
KK
1168 bat_desc->properties = wacom_battery_props;
1169 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1170 bat_desc->get_property = wacom_battery_get_property;
d70420b9 1171 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
297d716f
KK
1172 bat_desc->name = wacom->wacom_wac.bat_name;
1173 bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1174 bat_desc->use_for_apm = 0;
a1d552cc 1175
297d716f
KK
1176 ac_desc->properties = wacom_ac_props;
1177 ac_desc->num_properties = ARRAY_SIZE(wacom_ac_props);
1178 ac_desc->get_property = wacom_ac_get_property;
7dbd229e 1179 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
297d716f
KK
1180 ac_desc->name = wacom->wacom_wac.ac_name;
1181 ac_desc->type = POWER_SUPPLY_TYPE_MAINS;
1182 ac_desc->use_for_apm = 0;
1183
b189da90
BT
1184 wacom->battery = devm_power_supply_register(dev,
1185 &wacom->battery_desc,
1186 &psy_cfg);
1187 if (IS_ERR(wacom->battery)) {
1188 error = PTR_ERR(wacom->battery);
1189 goto err;
1190 }
297d716f
KK
1191
1192 power_supply_powers(wacom->battery, &wacom->hdev->dev);
1193
b189da90
BT
1194 wacom->ac = devm_power_supply_register(dev,
1195 &wacom->ac_desc,
1196 &psy_cfg);
297d716f 1197 if (IS_ERR(wacom->ac)) {
b189da90
BT
1198 error = PTR_ERR(wacom->ac);
1199 goto err;
7dbd229e
BT
1200 }
1201
297d716f 1202 power_supply_powers(wacom->ac, &wacom->hdev->dev);
a1d552cc
CB
1203 }
1204
b189da90 1205 devres_close_group(dev, bat_desc);
7dbd229e 1206 return 0;
b189da90
BT
1207
1208err:
1209 devres_release_group(dev, bat_desc);
1210 return error;
a1d552cc
CB
1211}
1212
1213static void wacom_destroy_battery(struct wacom *wacom)
1214{
8de29a35 1215 if (wacom->battery) {
b189da90 1216 devres_release_group(&wacom->hdev->dev, &wacom->battery_desc);
297d716f 1217 wacom->battery = NULL;
297d716f 1218 wacom->ac = NULL;
b7af2bb8 1219 }
a1d552cc
CB
1220}
1221
f81a1295
BT
1222static ssize_t wacom_show_speed(struct device *dev,
1223 struct device_attribute
1224 *attr, char *buf)
1225{
ee79a8f8 1226 struct hid_device *hdev = to_hid_device(dev);
f81a1295
BT
1227 struct wacom *wacom = hid_get_drvdata(hdev);
1228
1229 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1230}
1231
1232static ssize_t wacom_store_speed(struct device *dev,
1233 struct device_attribute *attr,
1234 const char *buf, size_t count)
1235{
ee79a8f8 1236 struct hid_device *hdev = to_hid_device(dev);
f81a1295
BT
1237 struct wacom *wacom = hid_get_drvdata(hdev);
1238 u8 new_speed;
1239
1240 if (kstrtou8(buf, 0, &new_speed))
1241 return -EINVAL;
1242
1243 if (new_speed != 0 && new_speed != 1)
1244 return -EINVAL;
1245
1246 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1247
1248 return count;
1249}
1250
e0984bc3 1251static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
f81a1295
BT
1252 wacom_show_speed, wacom_store_speed);
1253
72b236d6
AS
1254
1255static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1256 struct kobj_attribute *kattr,
1257 char *buf, int index)
1258{
2cf83833 1259 struct device *dev = kobj_to_dev(kobj->parent);
ee79a8f8 1260 struct hid_device *hdev = to_hid_device(dev);
72b236d6
AS
1261 struct wacom *wacom = hid_get_drvdata(hdev);
1262 u8 mode;
1263
a50aac71 1264 mode = wacom->led.groups[index].select;
72b236d6
AS
1265 if (mode >= 0 && mode < 3)
1266 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
1267 else
1268 return snprintf(buf, PAGE_SIZE, "%d\n", -1);
1269}
1270
1271#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1272static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1273 struct kobj_attribute *kattr, char *buf) \
1274{ \
1275 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1276} \
1277static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1278 .attr = {.name = "remote_mode", \
1279 .mode = DEV_ATTR_RO_PERM}, \
1280 .show = wacom_show_remote##SET_ID##_mode, \
1281}; \
1282static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1283 &remote##SET_ID##_mode_attr.attr, \
1284 NULL \
1285}; \
1286static struct attribute_group remote##SET_ID##_serial_group = { \
1287 .name = NULL, \
1288 .attrs = remote##SET_ID##_serial_attrs, \
1289}
1290
1291DEVICE_EKR_ATTR_GROUP(0);
1292DEVICE_EKR_ATTR_GROUP(1);
1293DEVICE_EKR_ATTR_GROUP(2);
1294DEVICE_EKR_ATTR_GROUP(3);
1295DEVICE_EKR_ATTR_GROUP(4);
1296
e6f2813a
BT
1297static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1298 int index)
72b236d6
AS
1299{
1300 int error = 0;
72b236d6
AS
1301 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1302
1303 wacom_wac->serial[index] = serial;
1304
c1f5409b
BT
1305 wacom->remote_group[index].name = devm_kasprintf(&wacom->hdev->dev,
1306 GFP_KERNEL,
1307 "%d", serial);
1308 if (!wacom->remote_group[index].name)
72b236d6 1309 return -ENOMEM;
72b236d6
AS
1310
1311 error = sysfs_create_group(wacom->remote_dir,
1312 &wacom->remote_group[index]);
1313 if (error) {
1314 hid_err(wacom->hdev,
1315 "cannot create sysfs group err: %d\n", error);
72b236d6
AS
1316 return error;
1317 }
1318
1319 return 0;
1320}
1321
e6f2813a 1322static void wacom_remote_destroy_attr_group(struct wacom *wacom, __u32 serial)
72b236d6
AS
1323{
1324 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1325 int i;
1326
1327 if (!serial)
1328 return;
1329
1330 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1331 if (wacom_wac->serial[i] == serial) {
1332 wacom_wac->serial[i] = 0;
a50aac71 1333 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
72b236d6
AS
1334 if (wacom->remote_group[i].name) {
1335 sysfs_remove_group(wacom->remote_dir,
1336 &wacom->remote_group[i]);
c1f5409b
BT
1337 devm_kfree(&wacom->hdev->dev,
1338 (char *)wacom->remote_group[i].name);
72b236d6
AS
1339 wacom->remote_group[i].name = NULL;
1340 }
1341 }
1342 }
1343}
1344
1345static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1346{
1347 const size_t buf_size = 2;
1348 unsigned char *buf;
1349 int retval;
1350
1351 buf = kzalloc(buf_size, GFP_KERNEL);
1352 if (!buf)
1353 return -ENOMEM;
1354
1355 buf[0] = WAC_CMD_DELETE_PAIRING;
1356 buf[1] = selector;
1357
1358 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1359 buf_size, WAC_CMD_RETRIES);
1360 kfree(buf);
1361
1362 return retval;
1363}
1364
1365static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1366 struct kobj_attribute *attr,
1367 const char *buf, size_t count)
1368{
1369 unsigned char selector = 0;
2cf83833 1370 struct device *dev = kobj_to_dev(kobj->parent);
ee79a8f8 1371 struct hid_device *hdev = to_hid_device(dev);
72b236d6
AS
1372 struct wacom *wacom = hid_get_drvdata(hdev);
1373 int err;
1374
1375 if (!strncmp(buf, "*\n", 2)) {
1376 selector = WAC_CMD_UNPAIR_ALL;
1377 } else {
1378 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1379 buf);
1380 return -1;
1381 }
1382
1383 mutex_lock(&wacom->lock);
1384
1385 err = wacom_cmd_unpair_remote(wacom, selector);
1386 mutex_unlock(&wacom->lock);
1387
1388 return err < 0 ? err : count;
1389}
1390
1391static struct kobj_attribute unpair_remote_attr = {
1392 .attr = {.name = "unpair_remote", .mode = 0200},
1393 .store = wacom_store_unpair_remote,
1394};
1395
1396static const struct attribute *remote_unpair_attrs[] = {
1397 &unpair_remote_attr.attr,
1398 NULL
1399};
1400
1401static int wacom_initialize_remote(struct wacom *wacom)
1402{
1403 int error = 0;
1404 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1405 int i;
1406
1407 if (wacom->wacom_wac.features.type != REMOTE)
1408 return 0;
1409
1410 wacom->remote_group[0] = remote0_serial_group;
1411 wacom->remote_group[1] = remote1_serial_group;
1412 wacom->remote_group[2] = remote2_serial_group;
1413 wacom->remote_group[3] = remote3_serial_group;
1414 wacom->remote_group[4] = remote4_serial_group;
1415
1416 wacom->remote_dir = kobject_create_and_add("wacom_remote",
1417 &wacom->hdev->dev.kobj);
1418 if (!wacom->remote_dir)
1419 return -ENOMEM;
1420
1421 error = sysfs_create_files(wacom->remote_dir, remote_unpair_attrs);
1422
1423 if (error) {
1424 hid_err(wacom->hdev,
1425 "cannot create sysfs group err: %d\n", error);
1426 return error;
1427 }
1428
1429 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
a50aac71 1430 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
72b236d6
AS
1431 wacom_wac->serial[i] = 0;
1432 }
1433
1434 return 0;
1435}
1436
d2d13f18 1437static struct input_dev *wacom_allocate_input(struct wacom *wacom)
3aac0ef1
CB
1438{
1439 struct input_dev *input_dev;
b6c79f2c 1440 struct hid_device *hdev = wacom->hdev;
3aac0ef1 1441 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
3aac0ef1 1442
3dad188e 1443 input_dev = devm_input_allocate_device(&hdev->dev);
d2d13f18
BT
1444 if (!input_dev)
1445 return NULL;
3aac0ef1 1446
2bdd163c 1447 input_dev->name = wacom_wac->features.name;
b6c79f2c
BT
1448 input_dev->phys = hdev->phys;
1449 input_dev->dev.parent = &hdev->dev;
3aac0ef1
CB
1450 input_dev->open = wacom_open;
1451 input_dev->close = wacom_close;
b6c79f2c
BT
1452 input_dev->uniq = hdev->uniq;
1453 input_dev->id.bustype = hdev->bus;
1454 input_dev->id.vendor = hdev->vendor;
12969e3b 1455 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
b6c79f2c 1456 input_dev->id.version = hdev->version;
3aac0ef1
CB
1457 input_set_drvdata(input_dev, wacom);
1458
d2d13f18
BT
1459 return input_dev;
1460}
1461
d9f2d203
JG
1462static int wacom_allocate_inputs(struct wacom *wacom)
1463{
1464 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1465
1466 wacom_wac->pen_input = wacom_allocate_input(wacom);
1467 wacom_wac->touch_input = wacom_allocate_input(wacom);
1468 wacom_wac->pad_input = wacom_allocate_input(wacom);
3dad188e
BT
1469 if (!wacom_wac->pen_input ||
1470 !wacom_wac->touch_input ||
1471 !wacom_wac->pad_input)
d9f2d203 1472 return -ENOMEM;
d9f2d203 1473
2bdd163c 1474 wacom_wac->pen_input->name = wacom_wac->pen_name;
d9f2d203
JG
1475 wacom_wac->touch_input->name = wacom_wac->touch_name;
1476 wacom_wac->pad_input->name = wacom_wac->pad_name;
1477
1478 return 0;
1479}
1480
2546dacd
BT
1481static int wacom_register_inputs(struct wacom *wacom)
1482{
2a6cdbdd 1483 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2546dacd 1484 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2636a3f2 1485 int error = 0;
2546dacd 1486
2a6cdbdd
JG
1487 pen_input_dev = wacom_wac->pen_input;
1488 touch_input_dev = wacom_wac->touch_input;
2546dacd
BT
1489 pad_input_dev = wacom_wac->pad_input;
1490
2a6cdbdd 1491 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
2546dacd
BT
1492 return -EINVAL;
1493
2a6cdbdd
JG
1494 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1495 if (error) {
1496 /* no pen in use on this interface */
1497 input_free_device(pen_input_dev);
1498 wacom_wac->pen_input = NULL;
1499 pen_input_dev = NULL;
1500 } else {
1501 error = input_register_device(pen_input_dev);
1502 if (error)
3dad188e 1503 goto fail;
2a6cdbdd
JG
1504 }
1505
1506 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1507 if (error) {
1508 /* no touch in use on this interface */
1509 input_free_device(touch_input_dev);
1510 wacom_wac->touch_input = NULL;
1511 touch_input_dev = NULL;
1512 } else {
1513 error = input_register_device(touch_input_dev);
30ebc1ae 1514 if (error)
3dad188e 1515 goto fail;
30ebc1ae 1516 }
1963518b 1517
d2d13f18
BT
1518 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1519 if (error) {
1520 /* no pad in use on this interface */
1521 input_free_device(pad_input_dev);
1522 wacom_wac->pad_input = NULL;
1523 pad_input_dev = NULL;
1524 } else {
1525 error = input_register_device(pad_input_dev);
1526 if (error)
3dad188e 1527 goto fail;
d2d13f18
BT
1528 }
1529
1963518b 1530 return 0;
3aac0ef1 1531
3dad188e
BT
1532fail:
1533 wacom_wac->pad_input = NULL;
2a6cdbdd 1534 wacom_wac->touch_input = NULL;
2a6cdbdd 1535 wacom_wac->pen_input = NULL;
3aac0ef1
CB
1536 return error;
1537}
1538
0be01712
JG
1539/*
1540 * Not all devices report physical dimensions from HID.
1541 * Compute the default from hardcoded logical dimension
1542 * and resolution before driver overwrites them.
1543 */
1544static void wacom_set_default_phy(struct wacom_features *features)
1545{
1546 if (features->x_resolution) {
1547 features->x_phy = (features->x_max * 100) /
1548 features->x_resolution;
1549 features->y_phy = (features->y_max * 100) /
1550 features->y_resolution;
1551 }
1552}
1553
1554static void wacom_calculate_res(struct wacom_features *features)
1555{
1556 /* set unit to "100th of a mm" for devices not reported by HID */
1557 if (!features->unit) {
1558 features->unit = 0x11;
1559 features->unitExpo = -3;
1560 }
1561
1562 features->x_resolution = wacom_calc_hid_res(features->x_max,
1563 features->x_phy,
1564 features->unit,
1565 features->unitExpo);
1566 features->y_resolution = wacom_calc_hid_res(features->y_max,
1567 features->y_phy,
1568 features->unit,
1569 features->unitExpo);
1570}
1571
fce9957d
JG
1572void wacom_battery_work(struct work_struct *work)
1573{
d17d1f17 1574 struct wacom *wacom = container_of(work, struct wacom, battery_work);
fce9957d
JG
1575
1576 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
8de29a35 1577 !wacom->battery) {
fce9957d
JG
1578 wacom_initialize_battery(wacom);
1579 }
1580 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
8de29a35 1581 wacom->battery) {
fce9957d
JG
1582 wacom_destroy_battery(wacom);
1583 }
1584}
1585
01c846f9
BT
1586static size_t wacom_compute_pktlen(struct hid_device *hdev)
1587{
1588 struct hid_report_enum *report_enum;
1589 struct hid_report *report;
1590 size_t size = 0;
1591
1592 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1593
1594 list_for_each_entry(report, &report_enum->report_list, list) {
dabb05c6 1595 size_t report_size = hid_report_len(report);
01c846f9
BT
1596 if (report_size > size)
1597 size = report_size;
1598 }
1599
1600 return size;
1601}
1602
fd5f92b6 1603static void wacom_update_name(struct wacom *wacom, const char *suffix)
c24eab4e
PC
1604{
1605 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1606 struct wacom_features *features = &wacom_wac->features;
44b5250b 1607 char name[WACOM_NAME_MAX];
c24eab4e
PC
1608
1609 /* Generic devices name unspecified */
1610 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1611 if (strstr(wacom->hdev->name, "Wacom") ||
1612 strstr(wacom->hdev->name, "wacom") ||
1613 strstr(wacom->hdev->name, "WACOM")) {
1614 /* name is in HID descriptor, use it */
44b5250b 1615 strlcpy(name, wacom->hdev->name, sizeof(name));
c24eab4e
PC
1616
1617 /* strip out excess whitespaces */
1618 while (1) {
44b5250b 1619 char *gap = strstr(name, " ");
c24eab4e
PC
1620 if (gap == NULL)
1621 break;
1622 /* shift everything including the terminator */
1623 memmove(gap, gap+1, strlen(gap));
1624 }
1625 /* get rid of trailing whitespace */
44b5250b
JG
1626 if (name[strlen(name)-1] == ' ')
1627 name[strlen(name)-1] = '\0';
c24eab4e
PC
1628 } else {
1629 /* no meaningful name retrieved. use product ID */
44b5250b 1630 snprintf(name, sizeof(name),
c24eab4e
PC
1631 "%s %X", features->name, wacom->hdev->product);
1632 }
1633 } else {
44b5250b 1634 strlcpy(name, features->name, sizeof(name));
c24eab4e
PC
1635 }
1636
1637 /* Append the device type to the name */
2a6cdbdd 1638 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
fd5f92b6 1639 "%s%s Pen", name, suffix);
2a6cdbdd 1640 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
fd5f92b6 1641 "%s%s Finger", name, suffix);
c24eab4e 1642 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
fd5f92b6 1643 "%s%s Pad", name, suffix);
c24eab4e
PC
1644}
1645
84dfbd7f
BT
1646static void wacom_release_resources(struct wacom *wacom)
1647{
1648 struct hid_device *hdev = wacom->hdev;
1649
1650 if (!wacom->resources)
1651 return;
1652
1653 devres_release_group(&hdev->dev, wacom);
1654
1655 wacom->resources = false;
1656
1657 wacom->wacom_wac.pen_input = NULL;
1658 wacom->wacom_wac.touch_input = NULL;
1659 wacom->wacom_wac.pad_input = NULL;
1660}
1661
fd5f92b6 1662static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
3bea733a 1663{
c58ac3a8
BT
1664 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1665 struct wacom_features *features = &wacom_wac->features;
1666 struct hid_device *hdev = wacom->hdev;
e33da8a5 1667 int error;
7704ac93 1668 unsigned int connect_mask = HID_CONNECT_HIDRAW;
3bea733a 1669
01c846f9 1670 features->pktlen = wacom_compute_pktlen(hdev);
c58ac3a8
BT
1671 if (features->pktlen > WACOM_PKGLEN_MAX)
1672 return -EINVAL;
3bea733a 1673
84dfbd7f
BT
1674 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
1675 return -ENOMEM;
1676
1677 wacom->resources = true;
1678
3f14a63a
JG
1679 error = wacom_allocate_inputs(wacom);
1680 if (error)
84dfbd7f 1681 goto fail_open_group;
494078b0 1682
8c97a765
BT
1683 /*
1684 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
1685 * into debug mode for the touch part.
1686 * We ignore the other interfaces.
1687 */
1688 if (features->type == BAMBOO_PAD) {
1689 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
1690 features->type = HID_GENERIC;
1691 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
1692 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
1693 error = -ENODEV;
c58ac3a8 1694 goto fail_allocate_inputs;
8c97a765
BT
1695 }
1696 }
1697
401d7d10
PC
1698 /* set the default size in case we do not get them from hid */
1699 wacom_set_default_phy(features);
1700
f393ee2b 1701 /* Retrieve the physical and logical size for touch devices */
c669fb2b 1702 wacom_retrieve_hid_descriptor(hdev, features);
42f4f272 1703 wacom_setup_device_quirks(wacom);
042628ab 1704
aa86b18c
JG
1705 if (features->device_type == WACOM_DEVICETYPE_NONE &&
1706 features->type != WIRELESS) {
8e116d31
JG
1707 error = features->type == HID_GENERIC ? -ENODEV : 0;
1708
042628ab 1709 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
8e116d31
JG
1710 hdev->name,
1711 error ? "Ignoring" : "Assuming pen");
1712
1713 if (error)
c58ac3a8 1714 goto fail_parsed;
042628ab 1715
aa86b18c 1716 features->device_type |= WACOM_DEVICETYPE_PEN;
042628ab
JG
1717 }
1718
401d7d10
PC
1719 wacom_calculate_res(features);
1720
fd5f92b6 1721 wacom_update_name(wacom, wireless ? " (WL)" : "");
4492efff 1722
f3586d2f
PC
1723 error = wacom_add_shared_data(hdev);
1724 if (error)
1725 goto fail_shared_data;
49b764ae 1726
ccad85cc 1727 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
f81a1295
BT
1728 (features->quirks & WACOM_QUIRK_BATTERY)) {
1729 error = wacom_initialize_battery(wacom);
1730 if (error)
7fefeec5 1731 goto fail_battery;
f81a1295
BT
1732 }
1733
3f14a63a
JG
1734 error = wacom_register_inputs(wacom);
1735 if (error)
1736 goto fail_register_inputs;
f81a1295 1737
b62f6465 1738 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
85d2c77b
BT
1739 error = wacom_initialize_leds(wacom);
1740 if (error)
1741 goto fail_leds;
1742
b62f6465
BT
1743 error = wacom_initialize_remote(wacom);
1744 if (error)
1745 goto fail_remote;
1746 }
1747
7704ac93
BT
1748 if (features->type == HID_GENERIC)
1749 connect_mask |= HID_CONNECT_DRIVER;
1750
29b47391 1751 /* Regular HID work starts now */
7704ac93 1752 error = hid_hw_start(hdev, connect_mask);
29b47391
BT
1753 if (error) {
1754 hid_err(hdev, "hw start failed\n");
7fefeec5 1755 goto fail_hw_start;
d3825d51 1756 }
961794a0 1757
fd5f92b6
BT
1758 if (!wireless) {
1759 /* Note that if query fails it is not a hard failure */
1760 wacom_query_tablet_data(hdev, features);
1761 }
86e88f0e
JG
1762
1763 /* touch only Bamboo doesn't support pen */
1764 if ((features->type == BAMBOO_TOUCH) &&
1765 (features->device_type & WACOM_DEVICETYPE_PEN)) {
1766 error = -ENODEV;
b62f6465 1767 goto fail_quirks;
86e88f0e
JG
1768 }
1769
1770 /* pen only Bamboo neither support touch nor pad */
1771 if ((features->type == BAMBOO_PEN) &&
1772 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
1773 (features->device_type & WACOM_DEVICETYPE_PAD))) {
1774 error = -ENODEV;
b62f6465 1775 goto fail_quirks;
86e88f0e
JG
1776 }
1777
ccad85cc 1778 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
29b47391
BT
1779 error = hid_hw_open(hdev);
1780
eda01dab 1781 if ((wacom_wac->features.type == INTUOSHT ||
97f9afa4 1782 wacom_wac->features.type == INTUOSHT2) &&
eda01dab 1783 (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
97f9afa4
BT
1784 wacom_wac->shared->type = wacom_wac->features.type;
1785 wacom_wac->shared->touch_input = wacom_wac->touch_input;
961794a0
PC
1786 }
1787
84dfbd7f
BT
1788 devres_close_group(&hdev->dev, wacom);
1789
3bea733a
PC
1790 return 0;
1791
b62f6465 1792fail_quirks:
c58ac3a8 1793 hid_hw_stop(hdev);
b62f6465
BT
1794fail_hw_start:
1795 kobject_put(wacom->remote_dir);
1796fail_remote:
85d2c77b 1797fail_leds:
b62f6465 1798fail_register_inputs:
7fefeec5 1799fail_battery:
7fefeec5 1800fail_shared_data:
c58ac3a8 1801fail_parsed:
494078b0 1802fail_allocate_inputs:
84dfbd7f
BT
1803fail_open_group:
1804 wacom_release_resources(wacom);
c58ac3a8
BT
1805 return error;
1806}
1807
a2f091af
BT
1808static void wacom_wireless_work(struct work_struct *work)
1809{
d17d1f17 1810 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
a2f091af
BT
1811 struct usb_device *usbdev = wacom->usbdev;
1812 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1813 struct hid_device *hdev1, *hdev2;
1814 struct wacom *wacom1, *wacom2;
1815 struct wacom_wac *wacom_wac1, *wacom_wac2;
1816 int error;
1817
1818 /*
1819 * Regardless if this is a disconnect or a new tablet,
1820 * remove any existing input and battery devices.
1821 */
1822
1823 wacom_destroy_battery(wacom);
1824
1825 /* Stylus interface */
1826 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1827 wacom1 = hid_get_drvdata(hdev1);
1828 wacom_wac1 = &(wacom1->wacom_wac);
84dfbd7f 1829 wacom_release_resources(wacom1);
a2f091af
BT
1830
1831 /* Touch interface */
1832 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1833 wacom2 = hid_get_drvdata(hdev2);
1834 wacom_wac2 = &(wacom2->wacom_wac);
84dfbd7f 1835 wacom_release_resources(wacom2);
a2f091af
BT
1836
1837 if (wacom_wac->pid == 0) {
1838 hid_info(wacom->hdev, "wireless tablet disconnected\n");
a2f091af
BT
1839 } else {
1840 const struct hid_device_id *id = wacom_ids;
1841
1842 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1843 wacom_wac->pid);
1844
1845 while (id->bus) {
1846 if (id->vendor == USB_VENDOR_ID_WACOM &&
1847 id->product == wacom_wac->pid)
1848 break;
1849 id++;
1850 }
1851
1852 if (!id->bus) {
1853 hid_info(wacom->hdev, "ignoring unknown PID.\n");
1854 return;
1855 }
1856
1857 /* Stylus interface */
1858 wacom_wac1->features =
1859 *((struct wacom_features *)id->driver_data);
fd5f92b6 1860
a2f091af 1861 wacom_wac1->pid = wacom_wac->pid;
fd5f92b6
BT
1862 hid_hw_stop(hdev1);
1863 error = wacom_parse_and_register(wacom1, true);
a2f091af
BT
1864 if (error)
1865 goto fail;
1866
1867 /* Touch interface */
1868 if (wacom_wac1->features.touch_max ||
1869 (wacom_wac1->features.type >= INTUOSHT &&
1870 wacom_wac1->features.type <= BAMBOO_PT)) {
1871 wacom_wac2->features =
1872 *((struct wacom_features *)id->driver_data);
a2f091af 1873 wacom_wac2->pid = wacom_wac->pid;
fd5f92b6
BT
1874 hid_hw_stop(hdev2);
1875 error = wacom_parse_and_register(wacom2, true);
a2f091af
BT
1876 if (error)
1877 goto fail;
a2f091af
BT
1878 }
1879
1880 error = wacom_initialize_battery(wacom);
1881 if (error)
1882 goto fail;
1883 }
1884
1885 return;
1886
1887fail:
84dfbd7f 1888 wacom_release_resources(wacom1);
84dfbd7f 1889 wacom_release_resources(wacom2);
a2f091af
BT
1890 return;
1891}
1892
e6f2813a
BT
1893static void wacom_remote_work(struct work_struct *work)
1894{
1895 struct wacom *wacom = container_of(work, struct wacom, remote_work);
1896 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1897 struct wacom_remote_data data;
1898 unsigned long flags;
1899 unsigned int count;
1900 u32 serial;
1901 int i, k;
1902
1903 spin_lock_irqsave(&wacom->remote_lock, flags);
1904
1905 count = kfifo_out(&wacom->remote_fifo, &data, sizeof(data));
1906
1907 if (count != sizeof(data)) {
1908 hid_err(wacom->hdev,
1909 "workitem triggered without status available\n");
1910 spin_unlock_irqrestore(&wacom->remote_lock, flags);
1911 return;
1912 }
1913
1914 if (!kfifo_is_empty(&wacom->remote_fifo))
1915 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
1916
1917 spin_unlock_irqrestore(&wacom->remote_lock, flags);
1918
1919 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1920 serial = data.remote[i].serial;
1921 if (data.remote[i].connected) {
1922
1923 if (wacom_wac->serial[i] == serial)
1924 continue;
1925
1926 if (wacom_wac->serial[i]) {
1927 wacom_remote_destroy_attr_group(wacom,
1928 wacom_wac->serial[i]);
1929 }
1930
1931 /* A remote can pair more than once with an EKR,
1932 * check to make sure this serial isn't already paired.
1933 */
1934 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
1935 if (wacom_wac->serial[k] == serial)
1936 break;
1937 }
1938
1939 if (k < WACOM_MAX_REMOTES) {
1940 wacom_wac->serial[i] = serial;
1941 continue;
1942 }
1943 wacom_remote_create_attr_group(wacom, serial, i);
1944
1945 } else if (wacom_wac->serial[i]) {
1946 wacom_remote_destroy_attr_group(wacom,
1947 wacom_wac->serial[i]);
1948 }
1949 }
1950}
1951
c58ac3a8
BT
1952static int wacom_probe(struct hid_device *hdev,
1953 const struct hid_device_id *id)
1954{
1955 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1956 struct usb_device *dev = interface_to_usbdev(intf);
1957 struct wacom *wacom;
1958 struct wacom_wac *wacom_wac;
1959 struct wacom_features *features;
1960 int error;
1961
1962 if (!id->driver_data)
1963 return -EINVAL;
1964
1965 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1966
1967 /* hid-core sets this quirk for the boot interface */
1968 hdev->quirks &= ~HID_QUIRK_NOGET;
1969
19b64330 1970 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
c58ac3a8
BT
1971 if (!wacom)
1972 return -ENOMEM;
1973
1974 hid_set_drvdata(hdev, wacom);
1975 wacom->hdev = hdev;
1976
1977 wacom_wac = &wacom->wacom_wac;
1978 wacom_wac->features = *((struct wacom_features *)id->driver_data);
1979 features = &wacom_wac->features;
1980
1981 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1982 error = -ENODEV;
1983 goto fail_type;
1984 }
1985
c6fa1aeb 1986 wacom_wac->hid_data.inputmode = -1;
326ea2a9 1987 wacom_wac->mode_report = -1;
c6fa1aeb 1988
c58ac3a8
BT
1989 wacom->usbdev = dev;
1990 wacom->intf = intf;
1991 mutex_init(&wacom->lock);
d17d1f17
BT
1992 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
1993 INIT_WORK(&wacom->battery_work, wacom_battery_work);
e6f2813a
BT
1994 INIT_WORK(&wacom->remote_work, wacom_remote_work);
1995 spin_lock_init(&wacom->remote_lock);
1996
1997 if (kfifo_alloc(&wacom->remote_fifo,
1998 5 * sizeof(struct wacom_remote_data),
1999 GFP_KERNEL)) {
2000 dev_err(&hdev->dev,
2001 "%s:failed allocating remote_fifo\n", __func__);
2002 error = -ENOMEM;
2003 goto fail_type;
2004 }
c58ac3a8
BT
2005
2006 /* ask for the report descriptor to be loaded by HID */
2007 error = hid_parse(hdev);
2008 if (error) {
2009 hid_err(hdev, "parse failed\n");
2010 goto fail_parse;
2011 }
2012
fd5f92b6 2013 error = wacom_parse_and_register(wacom, false);
c58ac3a8
BT
2014 if (error)
2015 goto fail_parse;
2016
2017 if (hdev->bus == BUS_BLUETOOTH) {
2018 error = device_create_file(&hdev->dev, &dev_attr_speed);
2019 if (error)
2020 hid_warn(hdev,
2021 "can't create sysfs speed attribute err: %d\n",
2022 error);
2023 }
2024
2025 return 0;
2026
7fefeec5 2027fail_parse:
e6f2813a
BT
2028 kfifo_free(&wacom->remote_fifo);
2029fail_type:
29b47391 2030 hid_set_drvdata(hdev, NULL);
5014186d 2031 return error;
3bea733a
PC
2032}
2033
29b47391 2034static void wacom_remove(struct hid_device *hdev)
3bea733a 2035{
29b47391 2036 struct wacom *wacom = hid_get_drvdata(hdev);
f6205161
BT
2037 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2038 struct wacom_features *features = &wacom_wac->features;
2039
2040 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2041 hid_hw_close(hdev);
3bea733a 2042
29b47391 2043 hid_hw_stop(hdev);
e7224094 2044
d17d1f17
BT
2045 cancel_work_sync(&wacom->wireless_work);
2046 cancel_work_sync(&wacom->battery_work);
e6f2813a
BT
2047 cancel_work_sync(&wacom->remote_work);
2048 kfifo_free(&wacom->remote_fifo);
b62f6465 2049 kobject_put(wacom->remote_dir);
f81a1295
BT
2050 if (hdev->bus == BUS_BLUETOOTH)
2051 device_remove_file(&hdev->dev, &dev_attr_speed);
e7224094 2052
29b47391 2053 hid_set_drvdata(hdev, NULL);
e7224094
ON
2054}
2055
41a74581 2056#ifdef CONFIG_PM
29b47391 2057static int wacom_resume(struct hid_device *hdev)
e7224094 2058{
29b47391 2059 struct wacom *wacom = hid_get_drvdata(hdev);
51269fe8 2060 struct wacom_features *features = &wacom->wacom_wac.features;
e7224094
ON
2061
2062 mutex_lock(&wacom->lock);
38101475
PC
2063
2064 /* switch to wacom mode first */
27b20a9d 2065 wacom_query_tablet_data(hdev, features);
5d7e7d47 2066 wacom_led_control(wacom);
38101475 2067
e7224094
ON
2068 mutex_unlock(&wacom->lock);
2069
29b47391 2070 return 0;
e7224094
ON
2071}
2072
29b47391 2073static int wacom_reset_resume(struct hid_device *hdev)
e7224094 2074{
29b47391 2075 return wacom_resume(hdev);
3bea733a 2076}
41a74581 2077#endif /* CONFIG_PM */
3bea733a 2078
29b47391 2079static struct hid_driver wacom_driver = {
3bea733a 2080 .name = "wacom",
b036f6fb 2081 .id_table = wacom_ids,
3bea733a 2082 .probe = wacom_probe,
29b47391 2083 .remove = wacom_remove,
7704ac93 2084 .report = wacom_wac_report,
29b47391 2085#ifdef CONFIG_PM
e7224094
ON
2086 .resume = wacom_resume,
2087 .reset_resume = wacom_reset_resume,
29b47391
BT
2088#endif
2089 .raw_event = wacom_raw_event,
3bea733a 2090};
29b47391 2091module_hid_driver(wacom_driver);
f2e0a7d4
BT
2092
2093MODULE_VERSION(DRIVER_VERSION);
2094MODULE_AUTHOR(DRIVER_AUTHOR);
2095MODULE_DESCRIPTION(DRIVER_DESC);
2096MODULE_LICENSE(DRIVER_LICENSE);