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