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