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