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