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