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