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