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