]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hid/wacom_sys.c
HID: wacom: use devres to allocate driver data
[mirror_ubuntu-bionic-kernel.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
4451e088 570static int wacom_add_shared_data(struct hid_device *hdev)
4492efff 571{
4451e088
BT
572 struct wacom *wacom = hid_get_drvdata(hdev);
573 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
574 struct wacom_hdev_data *data;
4492efff
PC
575 int retval = 0;
576
577 mutex_lock(&wacom_udev_list_lock);
578
4451e088 579 data = wacom_get_hdev_data(hdev);
4492efff 580 if (!data) {
4451e088 581 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
4492efff
PC
582 if (!data) {
583 retval = -ENOMEM;
584 goto out;
585 }
586
587 kref_init(&data->kref);
4451e088 588 data->dev = hdev;
4492efff
PC
589 list_add_tail(&data->list, &wacom_udev_list);
590 }
591
4451e088 592 wacom_wac->shared = &data->shared;
4492efff 593
aa86b18c 594 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
a97ac104 595 wacom_wac->shared->touch = hdev;
aa86b18c 596 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
a97ac104
BT
597 wacom_wac->shared->pen = hdev;
598
4492efff
PC
599out:
600 mutex_unlock(&wacom_udev_list_lock);
601 return retval;
602}
603
604static void wacom_release_shared_data(struct kref *kref)
605{
4451e088
BT
606 struct wacom_hdev_data *data =
607 container_of(kref, struct wacom_hdev_data, kref);
4492efff
PC
608
609 mutex_lock(&wacom_udev_list_lock);
610 list_del(&data->list);
611 mutex_unlock(&wacom_udev_list_lock);
612
613 kfree(data);
614}
615
a97ac104 616static void wacom_remove_shared_data(struct wacom *wacom)
4492efff 617{
4451e088 618 struct wacom_hdev_data *data;
a97ac104
BT
619 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
620
621 if (wacom_wac->shared) {
622 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
623 shared);
624
625 if (wacom_wac->shared->touch == wacom->hdev)
626 wacom_wac->shared->touch = NULL;
627 else if (wacom_wac->shared->pen == wacom->hdev)
628 wacom_wac->shared->pen = NULL;
4492efff 629
4492efff 630 kref_put(&data->kref, wacom_release_shared_data);
a97ac104 631 wacom_wac->shared = NULL;
4492efff
PC
632 }
633}
634
5d7e7d47
EH
635static int wacom_led_control(struct wacom *wacom)
636{
637 unsigned char *buf;
9b5b95dd 638 int retval;
912ca216
PC
639 unsigned char report_id = WAC_CMD_LED_CONTROL;
640 int buf_size = 9;
5d7e7d47 641
912ca216
PC
642 if (wacom->wacom_wac.pid) { /* wireless connected */
643 report_id = WAC_CMD_WL_LED_CONTROL;
644 buf_size = 13;
645 }
646 buf = kzalloc(buf_size, GFP_KERNEL);
5d7e7d47
EH
647 if (!buf)
648 return -ENOMEM;
649
9b5b95dd 650 if (wacom->wacom_wac.features.type >= INTUOS5S &&
9a35c411 651 wacom->wacom_wac.features.type <= INTUOSPL) {
9b5b95dd
JG
652 /*
653 * Touch Ring and crop mark LED luminance may take on
654 * one of four values:
655 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
656 */
657 int ring_led = wacom->led.select[0] & 0x03;
658 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
659 int crop_lum = 0;
912ca216
PC
660 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
661
662 buf[0] = report_id;
663 if (wacom->wacom_wac.pid) {
664 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
665 buf, buf_size, WAC_CMD_RETRIES);
666 buf[0] = report_id;
667 buf[4] = led_bits;
668 } else
669 buf[1] = led_bits;
9b5b95dd
JG
670 }
671 else {
672 int led = wacom->led.select[0] | 0x4;
673
674 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
675 wacom->wacom_wac.features.type == WACOM_24HD)
676 led |= (wacom->led.select[1] << 4) | 0x40;
677
912ca216 678 buf[0] = report_id;
9b5b95dd
JG
679 buf[1] = led;
680 buf[2] = wacom->led.llv;
681 buf[3] = wacom->led.hlv;
682 buf[4] = wacom->led.img_lum;
683 }
5d7e7d47 684
912ca216 685 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
296b7378 686 WAC_CMD_RETRIES);
5d7e7d47
EH
687 kfree(buf);
688
689 return retval;
690}
691
849e2f06
BT
692static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
693 const unsigned len, const void *img)
5d7e7d47
EH
694{
695 unsigned char *buf;
696 int i, retval;
849e2f06 697 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
5d7e7d47 698
849e2f06 699 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
5d7e7d47
EH
700 if (!buf)
701 return -ENOMEM;
702
703 /* Send 'start' command */
704 buf[0] = WAC_CMD_ICON_START;
705 buf[1] = 1;
296b7378
PF
706 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
707 WAC_CMD_RETRIES);
5d7e7d47
EH
708 if (retval < 0)
709 goto out;
710
849e2f06 711 buf[0] = xfer_id;
5d7e7d47
EH
712 buf[1] = button_id & 0x07;
713 for (i = 0; i < 4; i++) {
714 buf[2] = i;
849e2f06 715 memcpy(buf + 3, img + i * chunk_len, chunk_len);
5d7e7d47 716
27b20a9d 717 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
296b7378 718 buf, chunk_len + 3, WAC_CMD_RETRIES);
5d7e7d47
EH
719 if (retval < 0)
720 break;
721 }
722
723 /* Send 'stop' */
724 buf[0] = WAC_CMD_ICON_START;
725 buf[1] = 0;
296b7378
PF
726 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
727 WAC_CMD_RETRIES);
5d7e7d47
EH
728
729out:
730 kfree(buf);
731 return retval;
732}
733
09e7d941 734static ssize_t wacom_led_select_store(struct device *dev, int set_id,
5d7e7d47
EH
735 const char *buf, size_t count)
736{
ee79a8f8 737 struct hid_device *hdev = to_hid_device(dev);
29b47391 738 struct wacom *wacom = hid_get_drvdata(hdev);
5d7e7d47
EH
739 unsigned int id;
740 int err;
741
742 err = kstrtouint(buf, 10, &id);
743 if (err)
744 return err;
745
746 mutex_lock(&wacom->lock);
747
09e7d941 748 wacom->led.select[set_id] = id & 0x3;
5d7e7d47
EH
749 err = wacom_led_control(wacom);
750
751 mutex_unlock(&wacom->lock);
752
753 return err < 0 ? err : count;
754}
755
09e7d941
PC
756#define DEVICE_LED_SELECT_ATTR(SET_ID) \
757static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
758 struct device_attribute *attr, const char *buf, size_t count) \
759{ \
760 return wacom_led_select_store(dev, SET_ID, buf, count); \
761} \
04c59abd
PC
762static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
763 struct device_attribute *attr, char *buf) \
764{ \
ee79a8f8 765 struct hid_device *hdev = to_hid_device(dev);\
29b47391 766 struct wacom *wacom = hid_get_drvdata(hdev); \
37449adc
PC
767 return scnprintf(buf, PAGE_SIZE, "%d\n", \
768 wacom->led.select[SET_ID]); \
04c59abd 769} \
e0984bc3 770static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
04c59abd 771 wacom_led##SET_ID##_select_show, \
09e7d941
PC
772 wacom_led##SET_ID##_select_store)
773
774DEVICE_LED_SELECT_ATTR(0);
775DEVICE_LED_SELECT_ATTR(1);
5d7e7d47
EH
776
777static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
778 const char *buf, size_t count)
779{
780 unsigned int value;
781 int err;
782
783 err = kstrtouint(buf, 10, &value);
784 if (err)
785 return err;
786
787 mutex_lock(&wacom->lock);
788
789 *dest = value & 0x7f;
790 err = wacom_led_control(wacom);
791
792 mutex_unlock(&wacom->lock);
793
794 return err < 0 ? err : count;
795}
796
797#define DEVICE_LUMINANCE_ATTR(name, field) \
798static ssize_t wacom_##name##_luminance_store(struct device *dev, \
799 struct device_attribute *attr, const char *buf, size_t count) \
800{ \
ee79a8f8 801 struct hid_device *hdev = to_hid_device(dev);\
29b47391 802 struct wacom *wacom = hid_get_drvdata(hdev); \
5d7e7d47
EH
803 \
804 return wacom_luminance_store(wacom, &wacom->led.field, \
805 buf, count); \
806} \
37449adc
PC
807static ssize_t wacom_##name##_luminance_show(struct device *dev, \
808 struct device_attribute *attr, char *buf) \
809{ \
810 struct wacom *wacom = dev_get_drvdata(dev); \
811 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
812} \
e0984bc3 813static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
37449adc
PC
814 wacom_##name##_luminance_show, \
815 wacom_##name##_luminance_store)
5d7e7d47
EH
816
817DEVICE_LUMINANCE_ATTR(status0, llv);
818DEVICE_LUMINANCE_ATTR(status1, hlv);
819DEVICE_LUMINANCE_ATTR(buttons, img_lum);
820
821static ssize_t wacom_button_image_store(struct device *dev, int button_id,
822 const char *buf, size_t count)
823{
ee79a8f8 824 struct hid_device *hdev = to_hid_device(dev);
29b47391 825 struct wacom *wacom = hid_get_drvdata(hdev);
5d7e7d47 826 int err;
849e2f06
BT
827 unsigned len;
828 u8 xfer_id;
829
830 if (hdev->bus == BUS_BLUETOOTH) {
831 len = 256;
832 xfer_id = WAC_CMD_ICON_BT_XFER;
833 } else {
834 len = 1024;
835 xfer_id = WAC_CMD_ICON_XFER;
836 }
5d7e7d47 837
849e2f06 838 if (count != len)
5d7e7d47
EH
839 return -EINVAL;
840
841 mutex_lock(&wacom->lock);
842
849e2f06 843 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
5d7e7d47
EH
844
845 mutex_unlock(&wacom->lock);
846
847 return err < 0 ? err : count;
848}
849
850#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
851static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
852 struct device_attribute *attr, const char *buf, size_t count) \
853{ \
854 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
855} \
e0984bc3 856static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
5d7e7d47
EH
857 NULL, wacom_btnimg##BUTTON_ID##_store)
858
859DEVICE_BTNIMG_ATTR(0);
860DEVICE_BTNIMG_ATTR(1);
861DEVICE_BTNIMG_ATTR(2);
862DEVICE_BTNIMG_ATTR(3);
863DEVICE_BTNIMG_ATTR(4);
864DEVICE_BTNIMG_ATTR(5);
865DEVICE_BTNIMG_ATTR(6);
866DEVICE_BTNIMG_ATTR(7);
867
09e7d941
PC
868static struct attribute *cintiq_led_attrs[] = {
869 &dev_attr_status_led0_select.attr,
870 &dev_attr_status_led1_select.attr,
871 NULL
872};
873
874static struct attribute_group cintiq_led_attr_group = {
875 .name = "wacom_led",
876 .attrs = cintiq_led_attrs,
877};
878
879static struct attribute *intuos4_led_attrs[] = {
5d7e7d47
EH
880 &dev_attr_status0_luminance.attr,
881 &dev_attr_status1_luminance.attr,
09e7d941 882 &dev_attr_status_led0_select.attr,
5d7e7d47
EH
883 &dev_attr_buttons_luminance.attr,
884 &dev_attr_button0_rawimg.attr,
885 &dev_attr_button1_rawimg.attr,
886 &dev_attr_button2_rawimg.attr,
887 &dev_attr_button3_rawimg.attr,
888 &dev_attr_button4_rawimg.attr,
889 &dev_attr_button5_rawimg.attr,
890 &dev_attr_button6_rawimg.attr,
891 &dev_attr_button7_rawimg.attr,
892 NULL
893};
894
09e7d941 895static struct attribute_group intuos4_led_attr_group = {
5d7e7d47 896 .name = "wacom_led",
09e7d941 897 .attrs = intuos4_led_attrs,
5d7e7d47
EH
898};
899
9b5b95dd
JG
900static struct attribute *intuos5_led_attrs[] = {
901 &dev_attr_status0_luminance.attr,
902 &dev_attr_status_led0_select.attr,
903 NULL
904};
905
906static struct attribute_group intuos5_led_attr_group = {
907 .name = "wacom_led",
908 .attrs = intuos5_led_attrs,
909};
910
2df68a88
BT
911struct wacom_sysfs_group_devres {
912 struct attribute_group *group;
913 struct kobject *root;
914};
915
916static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
917{
918 struct wacom_sysfs_group_devres *devres = res;
919 struct kobject *kobj = devres->root;
920
921 dev_dbg(dev, "%s: dropping reference to %s\n",
922 __func__, devres->group->name);
923 sysfs_remove_group(kobj, devres->group);
924}
925
926static int wacom_devm_sysfs_create_group(struct wacom *wacom,
927 struct attribute_group *group)
928{
929 struct wacom_sysfs_group_devres *devres;
930 int error;
931
932 devres = devres_alloc(wacom_devm_sysfs_group_release,
933 sizeof(struct wacom_sysfs_group_devres),
934 GFP_KERNEL);
935 if (!devres)
936 return -ENOMEM;
937
938 devres->group = group;
939 devres->root = &wacom->hdev->dev.kobj;
940
941 error = sysfs_create_group(devres->root, group);
942 if (error)
943 return error;
944
945 devres_add(&wacom->hdev->dev, devres);
946
947 return 0;
948}
949
5d7e7d47
EH
950static int wacom_initialize_leds(struct wacom *wacom)
951{
952 int error;
953
862cf553
JG
954 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
955 return 0;
956
09e7d941
PC
957 /* Initialize default values */
958 switch (wacom->wacom_wac.features.type) {
a19fc986 959 case INTUOS4S:
09e7d941 960 case INTUOS4:
81af7e61 961 case INTUOS4WL:
09e7d941
PC
962 case INTUOS4L:
963 wacom->led.select[0] = 0;
964 wacom->led.select[1] = 0;
f4fa9a6d 965 wacom->led.llv = 10;
5d7e7d47
EH
966 wacom->led.hlv = 20;
967 wacom->led.img_lum = 10;
2df68a88
BT
968 error = wacom_devm_sysfs_create_group(wacom,
969 &intuos4_led_attr_group);
09e7d941
PC
970 break;
971
246835fc 972 case WACOM_24HD:
09e7d941
PC
973 case WACOM_21UX2:
974 wacom->led.select[0] = 0;
975 wacom->led.select[1] = 0;
976 wacom->led.llv = 0;
977 wacom->led.hlv = 0;
978 wacom->led.img_lum = 0;
5d7e7d47 979
2df68a88
BT
980 error = wacom_devm_sysfs_create_group(wacom,
981 &cintiq_led_attr_group);
09e7d941
PC
982 break;
983
9b5b95dd
JG
984 case INTUOS5S:
985 case INTUOS5:
986 case INTUOS5L:
9a35c411
PC
987 case INTUOSPS:
988 case INTUOSPM:
989 case INTUOSPL:
862cf553
JG
990 wacom->led.select[0] = 0;
991 wacom->led.select[1] = 0;
992 wacom->led.llv = 32;
993 wacom->led.hlv = 0;
994 wacom->led.img_lum = 0;
995
2df68a88
BT
996 error = wacom_devm_sysfs_create_group(wacom,
997 &intuos5_led_attr_group);
9b5b95dd
JG
998 break;
999
09e7d941
PC
1000 default:
1001 return 0;
5d7e7d47
EH
1002 }
1003
09e7d941 1004 if (error) {
c31a408f 1005 hid_err(wacom->hdev,
09e7d941
PC
1006 "cannot create sysfs group err: %d\n", error);
1007 return error;
1008 }
1009 wacom_led_control(wacom);
1010
5d7e7d47
EH
1011 return 0;
1012}
1013
a1d552cc 1014static enum power_supply_property wacom_battery_props[] = {
71fa641e 1015 POWER_SUPPLY_PROP_PRESENT,
ac8d1010 1016 POWER_SUPPLY_PROP_STATUS,
6e2a6e80 1017 POWER_SUPPLY_PROP_SCOPE,
a1d552cc
CB
1018 POWER_SUPPLY_PROP_CAPACITY
1019};
1020
7dbd229e
BT
1021static enum power_supply_property wacom_ac_props[] = {
1022 POWER_SUPPLY_PROP_PRESENT,
1023 POWER_SUPPLY_PROP_ONLINE,
1024 POWER_SUPPLY_PROP_SCOPE,
1025};
1026
a1d552cc
CB
1027static int wacom_battery_get_property(struct power_supply *psy,
1028 enum power_supply_property psp,
1029 union power_supply_propval *val)
1030{
297d716f 1031 struct wacom *wacom = power_supply_get_drvdata(psy);
a1d552cc
CB
1032 int ret = 0;
1033
1034 switch (psp) {
71fa641e
JG
1035 case POWER_SUPPLY_PROP_PRESENT:
1036 val->intval = wacom->wacom_wac.bat_connected;
1037 break;
6e2a6e80
BN
1038 case POWER_SUPPLY_PROP_SCOPE:
1039 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1040 break;
a1d552cc
CB
1041 case POWER_SUPPLY_PROP_CAPACITY:
1042 val->intval =
ac8d1010
BT
1043 wacom->wacom_wac.battery_capacity;
1044 break;
1045 case POWER_SUPPLY_PROP_STATUS:
1046 if (wacom->wacom_wac.bat_charging)
1047 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1048 else if (wacom->wacom_wac.battery_capacity == 100 &&
1049 wacom->wacom_wac.ps_connected)
1050 val->intval = POWER_SUPPLY_STATUS_FULL;
b0882cb7
JG
1051 else if (wacom->wacom_wac.ps_connected)
1052 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
ac8d1010
BT
1053 else
1054 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
a1d552cc
CB
1055 break;
1056 default:
1057 ret = -EINVAL;
1058 break;
1059 }
1060
1061 return ret;
1062}
1063
7dbd229e
BT
1064static int wacom_ac_get_property(struct power_supply *psy,
1065 enum power_supply_property psp,
1066 union power_supply_propval *val)
1067{
297d716f 1068 struct wacom *wacom = power_supply_get_drvdata(psy);
7dbd229e
BT
1069 int ret = 0;
1070
1071 switch (psp) {
1072 case POWER_SUPPLY_PROP_PRESENT:
1073 /* fall through */
1074 case POWER_SUPPLY_PROP_ONLINE:
1075 val->intval = wacom->wacom_wac.ps_connected;
1076 break;
1077 case POWER_SUPPLY_PROP_SCOPE:
1078 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1079 break;
1080 default:
1081 ret = -EINVAL;
1082 break;
1083 }
1084 return ret;
1085}
1086
a1d552cc
CB
1087static int wacom_initialize_battery(struct wacom *wacom)
1088{
d70420b9 1089 static atomic_t battery_no = ATOMIC_INIT(0);
b189da90 1090 struct device *dev = &wacom->hdev->dev;
297d716f 1091 struct power_supply_config psy_cfg = { .drv_data = wacom, };
b189da90 1092 struct power_supply_desc *bat_desc = &wacom->battery_desc;
d70420b9 1093 unsigned long n;
b189da90
BT
1094 int error;
1095
1096 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1097 return -ENOMEM;
a1d552cc 1098
ac8d1010 1099 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
297d716f 1100 struct power_supply_desc *ac_desc = &wacom->ac_desc;
d70420b9 1101 n = atomic_inc_return(&battery_no) - 1;
7dbd229e 1102
297d716f
KK
1103 bat_desc->properties = wacom_battery_props;
1104 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1105 bat_desc->get_property = wacom_battery_get_property;
d70420b9 1106 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
297d716f
KK
1107 bat_desc->name = wacom->wacom_wac.bat_name;
1108 bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1109 bat_desc->use_for_apm = 0;
a1d552cc 1110
297d716f
KK
1111 ac_desc->properties = wacom_ac_props;
1112 ac_desc->num_properties = ARRAY_SIZE(wacom_ac_props);
1113 ac_desc->get_property = wacom_ac_get_property;
7dbd229e 1114 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
297d716f
KK
1115 ac_desc->name = wacom->wacom_wac.ac_name;
1116 ac_desc->type = POWER_SUPPLY_TYPE_MAINS;
1117 ac_desc->use_for_apm = 0;
1118
b189da90
BT
1119 wacom->battery = devm_power_supply_register(dev,
1120 &wacom->battery_desc,
1121 &psy_cfg);
1122 if (IS_ERR(wacom->battery)) {
1123 error = PTR_ERR(wacom->battery);
1124 goto err;
1125 }
297d716f
KK
1126
1127 power_supply_powers(wacom->battery, &wacom->hdev->dev);
1128
b189da90
BT
1129 wacom->ac = devm_power_supply_register(dev,
1130 &wacom->ac_desc,
1131 &psy_cfg);
297d716f 1132 if (IS_ERR(wacom->ac)) {
b189da90
BT
1133 error = PTR_ERR(wacom->ac);
1134 goto err;
7dbd229e
BT
1135 }
1136
297d716f 1137 power_supply_powers(wacom->ac, &wacom->hdev->dev);
a1d552cc
CB
1138 }
1139
b189da90 1140 devres_close_group(dev, bat_desc);
7dbd229e 1141 return 0;
b189da90
BT
1142
1143err:
1144 devres_release_group(dev, bat_desc);
1145 return error;
a1d552cc
CB
1146}
1147
1148static void wacom_destroy_battery(struct wacom *wacom)
1149{
8de29a35 1150 if (wacom->battery) {
b189da90 1151 devres_release_group(&wacom->hdev->dev, &wacom->battery_desc);
297d716f 1152 wacom->battery = NULL;
297d716f 1153 wacom->ac = NULL;
b7af2bb8 1154 }
a1d552cc
CB
1155}
1156
f81a1295
BT
1157static ssize_t wacom_show_speed(struct device *dev,
1158 struct device_attribute
1159 *attr, char *buf)
1160{
ee79a8f8 1161 struct hid_device *hdev = to_hid_device(dev);
f81a1295
BT
1162 struct wacom *wacom = hid_get_drvdata(hdev);
1163
1164 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1165}
1166
1167static ssize_t wacom_store_speed(struct device *dev,
1168 struct device_attribute *attr,
1169 const char *buf, size_t count)
1170{
ee79a8f8 1171 struct hid_device *hdev = to_hid_device(dev);
f81a1295
BT
1172 struct wacom *wacom = hid_get_drvdata(hdev);
1173 u8 new_speed;
1174
1175 if (kstrtou8(buf, 0, &new_speed))
1176 return -EINVAL;
1177
1178 if (new_speed != 0 && new_speed != 1)
1179 return -EINVAL;
1180
1181 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1182
1183 return count;
1184}
1185
e0984bc3 1186static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
f81a1295
BT
1187 wacom_show_speed, wacom_store_speed);
1188
72b236d6
AS
1189
1190static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1191 struct kobj_attribute *kattr,
1192 char *buf, int index)
1193{
2cf83833 1194 struct device *dev = kobj_to_dev(kobj->parent);
ee79a8f8 1195 struct hid_device *hdev = to_hid_device(dev);
72b236d6
AS
1196 struct wacom *wacom = hid_get_drvdata(hdev);
1197 u8 mode;
1198
1199 mode = wacom->led.select[index];
1200 if (mode >= 0 && mode < 3)
1201 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
1202 else
1203 return snprintf(buf, PAGE_SIZE, "%d\n", -1);
1204}
1205
1206#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1207static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1208 struct kobj_attribute *kattr, char *buf) \
1209{ \
1210 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1211} \
1212static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1213 .attr = {.name = "remote_mode", \
1214 .mode = DEV_ATTR_RO_PERM}, \
1215 .show = wacom_show_remote##SET_ID##_mode, \
1216}; \
1217static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1218 &remote##SET_ID##_mode_attr.attr, \
1219 NULL \
1220}; \
1221static struct attribute_group remote##SET_ID##_serial_group = { \
1222 .name = NULL, \
1223 .attrs = remote##SET_ID##_serial_attrs, \
1224}
1225
1226DEVICE_EKR_ATTR_GROUP(0);
1227DEVICE_EKR_ATTR_GROUP(1);
1228DEVICE_EKR_ATTR_GROUP(2);
1229DEVICE_EKR_ATTR_GROUP(3);
1230DEVICE_EKR_ATTR_GROUP(4);
1231
1232int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial, int index)
1233{
1234 int error = 0;
72b236d6
AS
1235 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1236
1237 wacom_wac->serial[index] = serial;
1238
c1f5409b
BT
1239 wacom->remote_group[index].name = devm_kasprintf(&wacom->hdev->dev,
1240 GFP_KERNEL,
1241 "%d", serial);
1242 if (!wacom->remote_group[index].name)
72b236d6 1243 return -ENOMEM;
72b236d6
AS
1244
1245 error = sysfs_create_group(wacom->remote_dir,
1246 &wacom->remote_group[index]);
1247 if (error) {
1248 hid_err(wacom->hdev,
1249 "cannot create sysfs group err: %d\n", error);
72b236d6
AS
1250 return error;
1251 }
1252
1253 return 0;
1254}
1255
1256void wacom_remote_destroy_attr_group(struct wacom *wacom, __u32 serial)
1257{
1258 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1259 int i;
1260
1261 if (!serial)
1262 return;
1263
1264 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1265 if (wacom_wac->serial[i] == serial) {
1266 wacom_wac->serial[i] = 0;
1267 wacom->led.select[i] = WACOM_STATUS_UNKNOWN;
1268 if (wacom->remote_group[i].name) {
1269 sysfs_remove_group(wacom->remote_dir,
1270 &wacom->remote_group[i]);
c1f5409b
BT
1271 devm_kfree(&wacom->hdev->dev,
1272 (char *)wacom->remote_group[i].name);
72b236d6
AS
1273 wacom->remote_group[i].name = NULL;
1274 }
1275 }
1276 }
1277}
1278
1279static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1280{
1281 const size_t buf_size = 2;
1282 unsigned char *buf;
1283 int retval;
1284
1285 buf = kzalloc(buf_size, GFP_KERNEL);
1286 if (!buf)
1287 return -ENOMEM;
1288
1289 buf[0] = WAC_CMD_DELETE_PAIRING;
1290 buf[1] = selector;
1291
1292 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1293 buf_size, WAC_CMD_RETRIES);
1294 kfree(buf);
1295
1296 return retval;
1297}
1298
1299static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1300 struct kobj_attribute *attr,
1301 const char *buf, size_t count)
1302{
1303 unsigned char selector = 0;
2cf83833 1304 struct device *dev = kobj_to_dev(kobj->parent);
ee79a8f8 1305 struct hid_device *hdev = to_hid_device(dev);
72b236d6
AS
1306 struct wacom *wacom = hid_get_drvdata(hdev);
1307 int err;
1308
1309 if (!strncmp(buf, "*\n", 2)) {
1310 selector = WAC_CMD_UNPAIR_ALL;
1311 } else {
1312 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1313 buf);
1314 return -1;
1315 }
1316
1317 mutex_lock(&wacom->lock);
1318
1319 err = wacom_cmd_unpair_remote(wacom, selector);
1320 mutex_unlock(&wacom->lock);
1321
1322 return err < 0 ? err : count;
1323}
1324
1325static struct kobj_attribute unpair_remote_attr = {
1326 .attr = {.name = "unpair_remote", .mode = 0200},
1327 .store = wacom_store_unpair_remote,
1328};
1329
1330static const struct attribute *remote_unpair_attrs[] = {
1331 &unpair_remote_attr.attr,
1332 NULL
1333};
1334
1335static int wacom_initialize_remote(struct wacom *wacom)
1336{
1337 int error = 0;
1338 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1339 int i;
1340
1341 if (wacom->wacom_wac.features.type != REMOTE)
1342 return 0;
1343
1344 wacom->remote_group[0] = remote0_serial_group;
1345 wacom->remote_group[1] = remote1_serial_group;
1346 wacom->remote_group[2] = remote2_serial_group;
1347 wacom->remote_group[3] = remote3_serial_group;
1348 wacom->remote_group[4] = remote4_serial_group;
1349
1350 wacom->remote_dir = kobject_create_and_add("wacom_remote",
1351 &wacom->hdev->dev.kobj);
1352 if (!wacom->remote_dir)
1353 return -ENOMEM;
1354
1355 error = sysfs_create_files(wacom->remote_dir, remote_unpair_attrs);
1356
1357 if (error) {
1358 hid_err(wacom->hdev,
1359 "cannot create sysfs group err: %d\n", error);
1360 return error;
1361 }
1362
1363 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1364 wacom->led.select[i] = WACOM_STATUS_UNKNOWN;
1365 wacom_wac->serial[i] = 0;
1366 }
1367
1368 return 0;
1369}
1370
d2d13f18 1371static struct input_dev *wacom_allocate_input(struct wacom *wacom)
3aac0ef1
CB
1372{
1373 struct input_dev *input_dev;
b6c79f2c 1374 struct hid_device *hdev = wacom->hdev;
3aac0ef1 1375 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
3aac0ef1 1376
3dad188e 1377 input_dev = devm_input_allocate_device(&hdev->dev);
d2d13f18
BT
1378 if (!input_dev)
1379 return NULL;
3aac0ef1 1380
2bdd163c 1381 input_dev->name = wacom_wac->features.name;
b6c79f2c
BT
1382 input_dev->phys = hdev->phys;
1383 input_dev->dev.parent = &hdev->dev;
3aac0ef1
CB
1384 input_dev->open = wacom_open;
1385 input_dev->close = wacom_close;
b6c79f2c
BT
1386 input_dev->uniq = hdev->uniq;
1387 input_dev->id.bustype = hdev->bus;
1388 input_dev->id.vendor = hdev->vendor;
12969e3b 1389 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
b6c79f2c 1390 input_dev->id.version = hdev->version;
3aac0ef1
CB
1391 input_set_drvdata(input_dev, wacom);
1392
d2d13f18
BT
1393 return input_dev;
1394}
1395
d9f2d203
JG
1396static int wacom_allocate_inputs(struct wacom *wacom)
1397{
1398 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1399
1400 wacom_wac->pen_input = wacom_allocate_input(wacom);
1401 wacom_wac->touch_input = wacom_allocate_input(wacom);
1402 wacom_wac->pad_input = wacom_allocate_input(wacom);
3dad188e
BT
1403 if (!wacom_wac->pen_input ||
1404 !wacom_wac->touch_input ||
1405 !wacom_wac->pad_input)
d9f2d203 1406 return -ENOMEM;
d9f2d203 1407
2bdd163c 1408 wacom_wac->pen_input->name = wacom_wac->pen_name;
d9f2d203
JG
1409 wacom_wac->touch_input->name = wacom_wac->touch_name;
1410 wacom_wac->pad_input->name = wacom_wac->pad_name;
1411
1412 return 0;
1413}
1414
2546dacd
BT
1415static int wacom_register_inputs(struct wacom *wacom)
1416{
2a6cdbdd 1417 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2546dacd 1418 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2636a3f2 1419 int error = 0;
2546dacd 1420
2a6cdbdd
JG
1421 pen_input_dev = wacom_wac->pen_input;
1422 touch_input_dev = wacom_wac->touch_input;
2546dacd
BT
1423 pad_input_dev = wacom_wac->pad_input;
1424
2a6cdbdd 1425 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
2546dacd
BT
1426 return -EINVAL;
1427
2a6cdbdd
JG
1428 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1429 if (error) {
1430 /* no pen in use on this interface */
1431 input_free_device(pen_input_dev);
1432 wacom_wac->pen_input = NULL;
1433 pen_input_dev = NULL;
1434 } else {
1435 error = input_register_device(pen_input_dev);
1436 if (error)
3dad188e 1437 goto fail;
2a6cdbdd
JG
1438 }
1439
1440 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1441 if (error) {
1442 /* no touch in use on this interface */
1443 input_free_device(touch_input_dev);
1444 wacom_wac->touch_input = NULL;
1445 touch_input_dev = NULL;
1446 } else {
1447 error = input_register_device(touch_input_dev);
30ebc1ae 1448 if (error)
3dad188e 1449 goto fail;
30ebc1ae 1450 }
1963518b 1451
d2d13f18
BT
1452 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1453 if (error) {
1454 /* no pad in use on this interface */
1455 input_free_device(pad_input_dev);
1456 wacom_wac->pad_input = NULL;
1457 pad_input_dev = NULL;
1458 } else {
1459 error = input_register_device(pad_input_dev);
1460 if (error)
3dad188e 1461 goto fail;
d2d13f18
BT
1462 }
1463
1963518b 1464 return 0;
3aac0ef1 1465
3dad188e
BT
1466fail:
1467 wacom_wac->pad_input = NULL;
2a6cdbdd 1468 wacom_wac->touch_input = NULL;
2a6cdbdd 1469 wacom_wac->pen_input = NULL;
3aac0ef1
CB
1470 return error;
1471}
1472
0be01712
JG
1473/*
1474 * Not all devices report physical dimensions from HID.
1475 * Compute the default from hardcoded logical dimension
1476 * and resolution before driver overwrites them.
1477 */
1478static void wacom_set_default_phy(struct wacom_features *features)
1479{
1480 if (features->x_resolution) {
1481 features->x_phy = (features->x_max * 100) /
1482 features->x_resolution;
1483 features->y_phy = (features->y_max * 100) /
1484 features->y_resolution;
1485 }
1486}
1487
1488static void wacom_calculate_res(struct wacom_features *features)
1489{
1490 /* set unit to "100th of a mm" for devices not reported by HID */
1491 if (!features->unit) {
1492 features->unit = 0x11;
1493 features->unitExpo = -3;
1494 }
1495
1496 features->x_resolution = wacom_calc_hid_res(features->x_max,
1497 features->x_phy,
1498 features->unit,
1499 features->unitExpo);
1500 features->y_resolution = wacom_calc_hid_res(features->y_max,
1501 features->y_phy,
1502 features->unit,
1503 features->unitExpo);
1504}
1505
fce9957d
JG
1506void wacom_battery_work(struct work_struct *work)
1507{
d17d1f17 1508 struct wacom *wacom = container_of(work, struct wacom, battery_work);
fce9957d
JG
1509
1510 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
8de29a35 1511 !wacom->battery) {
fce9957d
JG
1512 wacom_initialize_battery(wacom);
1513 }
1514 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
8de29a35 1515 wacom->battery) {
fce9957d
JG
1516 wacom_destroy_battery(wacom);
1517 }
1518}
1519
01c846f9
BT
1520static size_t wacom_compute_pktlen(struct hid_device *hdev)
1521{
1522 struct hid_report_enum *report_enum;
1523 struct hid_report *report;
1524 size_t size = 0;
1525
1526 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1527
1528 list_for_each_entry(report, &report_enum->report_list, list) {
dabb05c6 1529 size_t report_size = hid_report_len(report);
01c846f9
BT
1530 if (report_size > size)
1531 size = report_size;
1532 }
1533
1534 return size;
1535}
1536
fd5f92b6 1537static void wacom_update_name(struct wacom *wacom, const char *suffix)
c24eab4e
PC
1538{
1539 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1540 struct wacom_features *features = &wacom_wac->features;
44b5250b 1541 char name[WACOM_NAME_MAX];
c24eab4e
PC
1542
1543 /* Generic devices name unspecified */
1544 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1545 if (strstr(wacom->hdev->name, "Wacom") ||
1546 strstr(wacom->hdev->name, "wacom") ||
1547 strstr(wacom->hdev->name, "WACOM")) {
1548 /* name is in HID descriptor, use it */
44b5250b 1549 strlcpy(name, wacom->hdev->name, sizeof(name));
c24eab4e
PC
1550
1551 /* strip out excess whitespaces */
1552 while (1) {
44b5250b 1553 char *gap = strstr(name, " ");
c24eab4e
PC
1554 if (gap == NULL)
1555 break;
1556 /* shift everything including the terminator */
1557 memmove(gap, gap+1, strlen(gap));
1558 }
1559 /* get rid of trailing whitespace */
44b5250b
JG
1560 if (name[strlen(name)-1] == ' ')
1561 name[strlen(name)-1] = '\0';
c24eab4e
PC
1562 } else {
1563 /* no meaningful name retrieved. use product ID */
44b5250b 1564 snprintf(name, sizeof(name),
c24eab4e
PC
1565 "%s %X", features->name, wacom->hdev->product);
1566 }
1567 } else {
44b5250b 1568 strlcpy(name, features->name, sizeof(name));
c24eab4e
PC
1569 }
1570
1571 /* Append the device type to the name */
2a6cdbdd 1572 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
fd5f92b6 1573 "%s%s Pen", name, suffix);
2a6cdbdd 1574 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
fd5f92b6 1575 "%s%s Finger", name, suffix);
c24eab4e 1576 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
fd5f92b6 1577 "%s%s Pad", name, suffix);
c24eab4e
PC
1578}
1579
84dfbd7f
BT
1580static void wacom_release_resources(struct wacom *wacom)
1581{
1582 struct hid_device *hdev = wacom->hdev;
1583
1584 if (!wacom->resources)
1585 return;
1586
1587 devres_release_group(&hdev->dev, wacom);
1588
1589 wacom->resources = false;
1590
1591 wacom->wacom_wac.pen_input = NULL;
1592 wacom->wacom_wac.touch_input = NULL;
1593 wacom->wacom_wac.pad_input = NULL;
1594}
1595
fd5f92b6 1596static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
3bea733a 1597{
c58ac3a8
BT
1598 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1599 struct wacom_features *features = &wacom_wac->features;
1600 struct hid_device *hdev = wacom->hdev;
e33da8a5 1601 int error;
7704ac93 1602 unsigned int connect_mask = HID_CONNECT_HIDRAW;
3bea733a 1603
01c846f9 1604 features->pktlen = wacom_compute_pktlen(hdev);
c58ac3a8
BT
1605 if (features->pktlen > WACOM_PKGLEN_MAX)
1606 return -EINVAL;
3bea733a 1607
84dfbd7f
BT
1608 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
1609 return -ENOMEM;
1610
1611 wacom->resources = true;
1612
3f14a63a
JG
1613 error = wacom_allocate_inputs(wacom);
1614 if (error)
84dfbd7f 1615 goto fail_open_group;
494078b0 1616
8c97a765
BT
1617 /*
1618 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
1619 * into debug mode for the touch part.
1620 * We ignore the other interfaces.
1621 */
1622 if (features->type == BAMBOO_PAD) {
1623 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
1624 features->type = HID_GENERIC;
1625 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
1626 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
1627 error = -ENODEV;
c58ac3a8 1628 goto fail_allocate_inputs;
8c97a765
BT
1629 }
1630 }
1631
401d7d10
PC
1632 /* set the default size in case we do not get them from hid */
1633 wacom_set_default_phy(features);
1634
f393ee2b 1635 /* Retrieve the physical and logical size for touch devices */
c669fb2b 1636 wacom_retrieve_hid_descriptor(hdev, features);
42f4f272 1637 wacom_setup_device_quirks(wacom);
042628ab 1638
aa86b18c
JG
1639 if (features->device_type == WACOM_DEVICETYPE_NONE &&
1640 features->type != WIRELESS) {
8e116d31
JG
1641 error = features->type == HID_GENERIC ? -ENODEV : 0;
1642
042628ab 1643 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
8e116d31
JG
1644 hdev->name,
1645 error ? "Ignoring" : "Assuming pen");
1646
1647 if (error)
c58ac3a8 1648 goto fail_parsed;
042628ab 1649
aa86b18c 1650 features->device_type |= WACOM_DEVICETYPE_PEN;
042628ab
JG
1651 }
1652
401d7d10
PC
1653 wacom_calculate_res(features);
1654
fd5f92b6 1655 wacom_update_name(wacom, wireless ? " (WL)" : "");
4492efff 1656
f3586d2f
PC
1657 error = wacom_add_shared_data(hdev);
1658 if (error)
1659 goto fail_shared_data;
49b764ae 1660
ccad85cc 1661 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
f81a1295
BT
1662 (features->quirks & WACOM_QUIRK_BATTERY)) {
1663 error = wacom_initialize_battery(wacom);
1664 if (error)
7fefeec5 1665 goto fail_battery;
f81a1295
BT
1666 }
1667
3f14a63a
JG
1668 error = wacom_register_inputs(wacom);
1669 if (error)
1670 goto fail_register_inputs;
f81a1295 1671
b62f6465 1672 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
85d2c77b
BT
1673 error = wacom_initialize_leds(wacom);
1674 if (error)
1675 goto fail_leds;
1676
b62f6465
BT
1677 error = wacom_initialize_remote(wacom);
1678 if (error)
1679 goto fail_remote;
1680 }
1681
7704ac93
BT
1682 if (features->type == HID_GENERIC)
1683 connect_mask |= HID_CONNECT_DRIVER;
1684
29b47391 1685 /* Regular HID work starts now */
7704ac93 1686 error = hid_hw_start(hdev, connect_mask);
29b47391
BT
1687 if (error) {
1688 hid_err(hdev, "hw start failed\n");
7fefeec5 1689 goto fail_hw_start;
d3825d51 1690 }
961794a0 1691
fd5f92b6
BT
1692 if (!wireless) {
1693 /* Note that if query fails it is not a hard failure */
1694 wacom_query_tablet_data(hdev, features);
1695 }
86e88f0e
JG
1696
1697 /* touch only Bamboo doesn't support pen */
1698 if ((features->type == BAMBOO_TOUCH) &&
1699 (features->device_type & WACOM_DEVICETYPE_PEN)) {
1700 error = -ENODEV;
b62f6465 1701 goto fail_quirks;
86e88f0e
JG
1702 }
1703
1704 /* pen only Bamboo neither support touch nor pad */
1705 if ((features->type == BAMBOO_PEN) &&
1706 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
1707 (features->device_type & WACOM_DEVICETYPE_PAD))) {
1708 error = -ENODEV;
b62f6465 1709 goto fail_quirks;
86e88f0e
JG
1710 }
1711
ccad85cc 1712 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
29b47391
BT
1713 error = hid_hw_open(hdev);
1714
eda01dab 1715 if ((wacom_wac->features.type == INTUOSHT ||
97f9afa4 1716 wacom_wac->features.type == INTUOSHT2) &&
eda01dab 1717 (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
97f9afa4
BT
1718 wacom_wac->shared->type = wacom_wac->features.type;
1719 wacom_wac->shared->touch_input = wacom_wac->touch_input;
961794a0
PC
1720 }
1721
84dfbd7f
BT
1722 devres_close_group(&hdev->dev, wacom);
1723
3bea733a
PC
1724 return 0;
1725
b62f6465 1726fail_quirks:
c58ac3a8 1727 hid_hw_stop(hdev);
b62f6465
BT
1728fail_hw_start:
1729 kobject_put(wacom->remote_dir);
1730fail_remote:
85d2c77b 1731fail_leds:
b62f6465 1732fail_register_inputs:
7fefeec5 1733fail_battery:
a97ac104 1734 wacom_remove_shared_data(wacom);
7fefeec5 1735fail_shared_data:
c58ac3a8 1736fail_parsed:
494078b0 1737fail_allocate_inputs:
84dfbd7f
BT
1738fail_open_group:
1739 wacom_release_resources(wacom);
c58ac3a8
BT
1740 return error;
1741}
1742
a2f091af
BT
1743static void wacom_wireless_work(struct work_struct *work)
1744{
d17d1f17 1745 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
a2f091af
BT
1746 struct usb_device *usbdev = wacom->usbdev;
1747 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1748 struct hid_device *hdev1, *hdev2;
1749 struct wacom *wacom1, *wacom2;
1750 struct wacom_wac *wacom_wac1, *wacom_wac2;
1751 int error;
1752
1753 /*
1754 * Regardless if this is a disconnect or a new tablet,
1755 * remove any existing input and battery devices.
1756 */
1757
1758 wacom_destroy_battery(wacom);
1759
1760 /* Stylus interface */
1761 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1762 wacom1 = hid_get_drvdata(hdev1);
1763 wacom_wac1 = &(wacom1->wacom_wac);
84dfbd7f 1764 wacom_release_resources(wacom1);
a2f091af
BT
1765
1766 /* Touch interface */
1767 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1768 wacom2 = hid_get_drvdata(hdev2);
1769 wacom_wac2 = &(wacom2->wacom_wac);
84dfbd7f 1770 wacom_release_resources(wacom2);
a2f091af
BT
1771
1772 if (wacom_wac->pid == 0) {
1773 hid_info(wacom->hdev, "wireless tablet disconnected\n");
1774 wacom_wac1->shared->type = 0;
1775 } else {
1776 const struct hid_device_id *id = wacom_ids;
1777
1778 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1779 wacom_wac->pid);
1780
1781 while (id->bus) {
1782 if (id->vendor == USB_VENDOR_ID_WACOM &&
1783 id->product == wacom_wac->pid)
1784 break;
1785 id++;
1786 }
1787
1788 if (!id->bus) {
1789 hid_info(wacom->hdev, "ignoring unknown PID.\n");
1790 return;
1791 }
1792
1793 /* Stylus interface */
1794 wacom_wac1->features =
1795 *((struct wacom_features *)id->driver_data);
fd5f92b6 1796
a2f091af 1797 wacom_wac1->pid = wacom_wac->pid;
fd5f92b6
BT
1798 hid_hw_stop(hdev1);
1799 error = wacom_parse_and_register(wacom1, true);
a2f091af
BT
1800 if (error)
1801 goto fail;
1802
1803 /* Touch interface */
1804 if (wacom_wac1->features.touch_max ||
1805 (wacom_wac1->features.type >= INTUOSHT &&
1806 wacom_wac1->features.type <= BAMBOO_PT)) {
1807 wacom_wac2->features =
1808 *((struct wacom_features *)id->driver_data);
a2f091af 1809 wacom_wac2->pid = wacom_wac->pid;
fd5f92b6
BT
1810 hid_hw_stop(hdev2);
1811 error = wacom_parse_and_register(wacom2, true);
a2f091af
BT
1812 if (error)
1813 goto fail;
a2f091af
BT
1814 }
1815
1816 error = wacom_initialize_battery(wacom);
1817 if (error)
1818 goto fail;
1819 }
1820
1821 return;
1822
1823fail:
84dfbd7f 1824 wacom_release_resources(wacom1);
84dfbd7f 1825 wacom_release_resources(wacom2);
a2f091af
BT
1826 return;
1827}
1828
c58ac3a8
BT
1829static int wacom_probe(struct hid_device *hdev,
1830 const struct hid_device_id *id)
1831{
1832 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1833 struct usb_device *dev = interface_to_usbdev(intf);
1834 struct wacom *wacom;
1835 struct wacom_wac *wacom_wac;
1836 struct wacom_features *features;
1837 int error;
1838
1839 if (!id->driver_data)
1840 return -EINVAL;
1841
1842 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1843
1844 /* hid-core sets this quirk for the boot interface */
1845 hdev->quirks &= ~HID_QUIRK_NOGET;
1846
19b64330 1847 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
c58ac3a8
BT
1848 if (!wacom)
1849 return -ENOMEM;
1850
1851 hid_set_drvdata(hdev, wacom);
1852 wacom->hdev = hdev;
1853
1854 wacom_wac = &wacom->wacom_wac;
1855 wacom_wac->features = *((struct wacom_features *)id->driver_data);
1856 features = &wacom_wac->features;
1857
1858 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1859 error = -ENODEV;
1860 goto fail_type;
1861 }
1862
c6fa1aeb 1863 wacom_wac->hid_data.inputmode = -1;
326ea2a9 1864 wacom_wac->mode_report = -1;
c6fa1aeb 1865
c58ac3a8
BT
1866 wacom->usbdev = dev;
1867 wacom->intf = intf;
1868 mutex_init(&wacom->lock);
d17d1f17
BT
1869 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
1870 INIT_WORK(&wacom->battery_work, wacom_battery_work);
c58ac3a8
BT
1871
1872 /* ask for the report descriptor to be loaded by HID */
1873 error = hid_parse(hdev);
1874 if (error) {
1875 hid_err(hdev, "parse failed\n");
1876 goto fail_parse;
1877 }
1878
fd5f92b6 1879 error = wacom_parse_and_register(wacom, false);
c58ac3a8
BT
1880 if (error)
1881 goto fail_parse;
1882
1883 if (hdev->bus == BUS_BLUETOOTH) {
1884 error = device_create_file(&hdev->dev, &dev_attr_speed);
1885 if (error)
1886 hid_warn(hdev,
1887 "can't create sysfs speed attribute err: %d\n",
1888 error);
1889 }
1890
1891 return 0;
1892
7fefeec5 1893fail_type:
7fefeec5 1894fail_parse:
29b47391 1895 hid_set_drvdata(hdev, NULL);
5014186d 1896 return error;
3bea733a
PC
1897}
1898
29b47391 1899static void wacom_remove(struct hid_device *hdev)
3bea733a 1900{
29b47391 1901 struct wacom *wacom = hid_get_drvdata(hdev);
f6205161
BT
1902 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1903 struct wacom_features *features = &wacom_wac->features;
1904
1905 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
1906 hid_hw_close(hdev);
3bea733a 1907
29b47391 1908 hid_hw_stop(hdev);
e7224094 1909
d17d1f17
BT
1910 cancel_work_sync(&wacom->wireless_work);
1911 cancel_work_sync(&wacom->battery_work);
b62f6465 1912 kobject_put(wacom->remote_dir);
f81a1295
BT
1913 if (hdev->bus == BUS_BLUETOOTH)
1914 device_remove_file(&hdev->dev, &dev_attr_speed);
a97ac104 1915 wacom_remove_shared_data(wacom);
e7224094 1916
29b47391 1917 hid_set_drvdata(hdev, NULL);
e7224094
ON
1918}
1919
41a74581 1920#ifdef CONFIG_PM
29b47391 1921static int wacom_resume(struct hid_device *hdev)
e7224094 1922{
29b47391 1923 struct wacom *wacom = hid_get_drvdata(hdev);
51269fe8 1924 struct wacom_features *features = &wacom->wacom_wac.features;
e7224094
ON
1925
1926 mutex_lock(&wacom->lock);
38101475
PC
1927
1928 /* switch to wacom mode first */
27b20a9d 1929 wacom_query_tablet_data(hdev, features);
5d7e7d47 1930 wacom_led_control(wacom);
38101475 1931
e7224094
ON
1932 mutex_unlock(&wacom->lock);
1933
29b47391 1934 return 0;
e7224094
ON
1935}
1936
29b47391 1937static int wacom_reset_resume(struct hid_device *hdev)
e7224094 1938{
29b47391 1939 return wacom_resume(hdev);
3bea733a 1940}
41a74581 1941#endif /* CONFIG_PM */
3bea733a 1942
29b47391 1943static struct hid_driver wacom_driver = {
3bea733a 1944 .name = "wacom",
b036f6fb 1945 .id_table = wacom_ids,
3bea733a 1946 .probe = wacom_probe,
29b47391 1947 .remove = wacom_remove,
7704ac93 1948 .report = wacom_wac_report,
29b47391 1949#ifdef CONFIG_PM
e7224094
ON
1950 .resume = wacom_resume,
1951 .reset_resume = wacom_reset_resume,
29b47391
BT
1952#endif
1953 .raw_event = wacom_raw_event,
3bea733a 1954};
29b47391 1955module_hid_driver(wacom_driver);
f2e0a7d4
BT
1956
1957MODULE_VERSION(DRIVER_VERSION);
1958MODULE_AUTHOR(DRIVER_AUTHOR);
1959MODULE_DESCRIPTION(DRIVER_DESC);
1960MODULE_LICENSE(DRIVER_LICENSE);