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