mnt: Don't propagate umounts in __detach_mounts
[deliverable/linux.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 }
500d4160
PC
406 else if (features->type == WACOM_27QHDT) {
407 return wacom_set_device_mode(hdev, 131, 3, 2);
408 }
fe494bc2
JG
409 } else if (features->device_type == BTN_TOOL_PEN) {
410 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
27b20a9d 411 return wacom_set_device_mode(hdev, 2, 2, 2);
1963518b 412 }
ec67bbed 413 }
3b7307c2 414
fe494bc2 415 return 0;
3b7307c2
DT
416}
417
c669fb2b 418static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
1963518b 419 struct wacom_features *features)
ec67bbed 420{
27b20a9d
BT
421 struct wacom *wacom = hid_get_drvdata(hdev);
422 struct usb_interface *intf = wacom->intf;
ec67bbed 423
fed87e65 424 /* default features */
ec67bbed 425 features->device_type = BTN_TOOL_PEN;
fed87e65
HR
426 features->x_fuzz = 4;
427 features->y_fuzz = 4;
428 features->pressure_fuzz = 0;
429 features->distance_fuzz = 0;
ec67bbed 430
d3825d51
CB
431 /*
432 * The wireless device HID is basic and layout conflicts with
433 * other tablets (monitor and touch interface can look like pen).
434 * Skip the query for this type and modify defaults based on
435 * interface number.
436 */
437 if (features->type == WIRELESS) {
438 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
439 features->device_type = 0;
440 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
adad004e 441 features->device_type = BTN_TOOL_FINGER;
d3825d51
CB
442 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
443 }
444 }
445
1963518b 446 /* only devices that support touch need to retrieve the info */
c669fb2b
BT
447 if (features->type < BAMBOO_PT)
448 return;
ec67bbed 449
c669fb2b 450 wacom_parse_hid(hdev, features);
ec67bbed
PC
451}
452
4451e088 453struct wacom_hdev_data {
4492efff
PC
454 struct list_head list;
455 struct kref kref;
4451e088 456 struct hid_device *dev;
4492efff
PC
457 struct wacom_shared shared;
458};
459
460static LIST_HEAD(wacom_udev_list);
461static DEFINE_MUTEX(wacom_udev_list_lock);
462
4451e088
BT
463static bool wacom_are_sibling(struct hid_device *hdev,
464 struct hid_device *sibling)
aea2bf6a 465{
4451e088
BT
466 struct wacom *wacom = hid_get_drvdata(hdev);
467 struct wacom_features *features = &wacom->wacom_wac.features;
468 int vid = features->oVid;
469 int pid = features->oPid;
470 int n1,n2;
471
472 if (vid == 0 && pid == 0) {
473 vid = hdev->vendor;
474 pid = hdev->product;
475 }
aea2bf6a 476
4451e088
BT
477 if (vid != sibling->vendor || pid != sibling->product)
478 return false;
aea2bf6a 479
4451e088
BT
480 /* Compare the physical path. */
481 n1 = strrchr(hdev->phys, '.') - hdev->phys;
482 n2 = strrchr(sibling->phys, '.') - sibling->phys;
483 if (n1 != n2 || n1 <= 0 || n2 <= 0)
484 return false;
aea2bf6a 485
4451e088 486 return !strncmp(hdev->phys, sibling->phys, n1);
aea2bf6a
JG
487}
488
4451e088 489static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
4492efff 490{
4451e088 491 struct wacom_hdev_data *data;
4492efff
PC
492
493 list_for_each_entry(data, &wacom_udev_list, list) {
4451e088 494 if (wacom_are_sibling(hdev, data->dev)) {
4492efff
PC
495 kref_get(&data->kref);
496 return data;
497 }
498 }
499
500 return NULL;
501}
502
4451e088 503static int wacom_add_shared_data(struct hid_device *hdev)
4492efff 504{
4451e088
BT
505 struct wacom *wacom = hid_get_drvdata(hdev);
506 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
507 struct wacom_hdev_data *data;
4492efff
PC
508 int retval = 0;
509
510 mutex_lock(&wacom_udev_list_lock);
511
4451e088 512 data = wacom_get_hdev_data(hdev);
4492efff 513 if (!data) {
4451e088 514 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
4492efff
PC
515 if (!data) {
516 retval = -ENOMEM;
517 goto out;
518 }
519
520 kref_init(&data->kref);
4451e088 521 data->dev = hdev;
4492efff
PC
522 list_add_tail(&data->list, &wacom_udev_list);
523 }
524
4451e088 525 wacom_wac->shared = &data->shared;
4492efff
PC
526
527out:
528 mutex_unlock(&wacom_udev_list_lock);
529 return retval;
530}
531
532static void wacom_release_shared_data(struct kref *kref)
533{
4451e088
BT
534 struct wacom_hdev_data *data =
535 container_of(kref, struct wacom_hdev_data, kref);
4492efff
PC
536
537 mutex_lock(&wacom_udev_list_lock);
538 list_del(&data->list);
539 mutex_unlock(&wacom_udev_list_lock);
540
541 kfree(data);
542}
543
544static void wacom_remove_shared_data(struct wacom_wac *wacom)
545{
4451e088 546 struct wacom_hdev_data *data;
4492efff
PC
547
548 if (wacom->shared) {
4451e088 549 data = container_of(wacom->shared, struct wacom_hdev_data, shared);
4492efff
PC
550 kref_put(&data->kref, wacom_release_shared_data);
551 wacom->shared = NULL;
552 }
553}
554
5d7e7d47
EH
555static int wacom_led_control(struct wacom *wacom)
556{
557 unsigned char *buf;
9b5b95dd 558 int retval;
912ca216
PC
559 unsigned char report_id = WAC_CMD_LED_CONTROL;
560 int buf_size = 9;
5d7e7d47 561
912ca216
PC
562 if (wacom->wacom_wac.pid) { /* wireless connected */
563 report_id = WAC_CMD_WL_LED_CONTROL;
564 buf_size = 13;
565 }
566 buf = kzalloc(buf_size, GFP_KERNEL);
5d7e7d47
EH
567 if (!buf)
568 return -ENOMEM;
569
9b5b95dd 570 if (wacom->wacom_wac.features.type >= INTUOS5S &&
9a35c411 571 wacom->wacom_wac.features.type <= INTUOSPL) {
9b5b95dd
JG
572 /*
573 * Touch Ring and crop mark LED luminance may take on
574 * one of four values:
575 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
576 */
577 int ring_led = wacom->led.select[0] & 0x03;
578 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
579 int crop_lum = 0;
912ca216
PC
580 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
581
582 buf[0] = report_id;
583 if (wacom->wacom_wac.pid) {
584 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
585 buf, buf_size, WAC_CMD_RETRIES);
586 buf[0] = report_id;
587 buf[4] = led_bits;
588 } else
589 buf[1] = led_bits;
9b5b95dd
JG
590 }
591 else {
592 int led = wacom->led.select[0] | 0x4;
593
594 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
595 wacom->wacom_wac.features.type == WACOM_24HD)
596 led |= (wacom->led.select[1] << 4) | 0x40;
597
912ca216 598 buf[0] = report_id;
9b5b95dd
JG
599 buf[1] = led;
600 buf[2] = wacom->led.llv;
601 buf[3] = wacom->led.hlv;
602 buf[4] = wacom->led.img_lum;
603 }
5d7e7d47 604
912ca216 605 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
296b7378 606 WAC_CMD_RETRIES);
5d7e7d47
EH
607 kfree(buf);
608
609 return retval;
610}
611
849e2f06
BT
612static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
613 const unsigned len, const void *img)
5d7e7d47
EH
614{
615 unsigned char *buf;
616 int i, retval;
849e2f06 617 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
5d7e7d47 618
849e2f06 619 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
5d7e7d47
EH
620 if (!buf)
621 return -ENOMEM;
622
623 /* Send 'start' command */
624 buf[0] = WAC_CMD_ICON_START;
625 buf[1] = 1;
296b7378
PF
626 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
627 WAC_CMD_RETRIES);
5d7e7d47
EH
628 if (retval < 0)
629 goto out;
630
849e2f06 631 buf[0] = xfer_id;
5d7e7d47
EH
632 buf[1] = button_id & 0x07;
633 for (i = 0; i < 4; i++) {
634 buf[2] = i;
849e2f06 635 memcpy(buf + 3, img + i * chunk_len, chunk_len);
5d7e7d47 636
27b20a9d 637 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
296b7378 638 buf, chunk_len + 3, WAC_CMD_RETRIES);
5d7e7d47
EH
639 if (retval < 0)
640 break;
641 }
642
643 /* Send 'stop' */
644 buf[0] = WAC_CMD_ICON_START;
645 buf[1] = 0;
296b7378
PF
646 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
647 WAC_CMD_RETRIES);
5d7e7d47
EH
648
649out:
650 kfree(buf);
651 return retval;
652}
653
09e7d941 654static ssize_t wacom_led_select_store(struct device *dev, int set_id,
5d7e7d47
EH
655 const char *buf, size_t count)
656{
c31a408f 657 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
29b47391 658 struct wacom *wacom = hid_get_drvdata(hdev);
5d7e7d47
EH
659 unsigned int id;
660 int err;
661
662 err = kstrtouint(buf, 10, &id);
663 if (err)
664 return err;
665
666 mutex_lock(&wacom->lock);
667
09e7d941 668 wacom->led.select[set_id] = id & 0x3;
5d7e7d47
EH
669 err = wacom_led_control(wacom);
670
671 mutex_unlock(&wacom->lock);
672
673 return err < 0 ? err : count;
674}
675
09e7d941
PC
676#define DEVICE_LED_SELECT_ATTR(SET_ID) \
677static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
678 struct device_attribute *attr, const char *buf, size_t count) \
679{ \
680 return wacom_led_select_store(dev, SET_ID, buf, count); \
681} \
04c59abd
PC
682static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
683 struct device_attribute *attr, char *buf) \
684{ \
c31a408f 685 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
29b47391 686 struct wacom *wacom = hid_get_drvdata(hdev); \
37449adc
PC
687 return scnprintf(buf, PAGE_SIZE, "%d\n", \
688 wacom->led.select[SET_ID]); \
04c59abd 689} \
e0984bc3 690static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
04c59abd 691 wacom_led##SET_ID##_select_show, \
09e7d941
PC
692 wacom_led##SET_ID##_select_store)
693
694DEVICE_LED_SELECT_ATTR(0);
695DEVICE_LED_SELECT_ATTR(1);
5d7e7d47
EH
696
697static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
698 const char *buf, size_t count)
699{
700 unsigned int value;
701 int err;
702
703 err = kstrtouint(buf, 10, &value);
704 if (err)
705 return err;
706
707 mutex_lock(&wacom->lock);
708
709 *dest = value & 0x7f;
710 err = wacom_led_control(wacom);
711
712 mutex_unlock(&wacom->lock);
713
714 return err < 0 ? err : count;
715}
716
717#define DEVICE_LUMINANCE_ATTR(name, field) \
718static ssize_t wacom_##name##_luminance_store(struct device *dev, \
719 struct device_attribute *attr, const char *buf, size_t count) \
720{ \
c31a408f 721 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
29b47391 722 struct wacom *wacom = hid_get_drvdata(hdev); \
5d7e7d47
EH
723 \
724 return wacom_luminance_store(wacom, &wacom->led.field, \
725 buf, count); \
726} \
37449adc
PC
727static ssize_t wacom_##name##_luminance_show(struct device *dev, \
728 struct device_attribute *attr, char *buf) \
729{ \
730 struct wacom *wacom = dev_get_drvdata(dev); \
731 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
732} \
e0984bc3 733static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
37449adc
PC
734 wacom_##name##_luminance_show, \
735 wacom_##name##_luminance_store)
5d7e7d47
EH
736
737DEVICE_LUMINANCE_ATTR(status0, llv);
738DEVICE_LUMINANCE_ATTR(status1, hlv);
739DEVICE_LUMINANCE_ATTR(buttons, img_lum);
740
741static ssize_t wacom_button_image_store(struct device *dev, int button_id,
742 const char *buf, size_t count)
743{
c31a408f 744 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
29b47391 745 struct wacom *wacom = hid_get_drvdata(hdev);
5d7e7d47 746 int err;
849e2f06
BT
747 unsigned len;
748 u8 xfer_id;
749
750 if (hdev->bus == BUS_BLUETOOTH) {
751 len = 256;
752 xfer_id = WAC_CMD_ICON_BT_XFER;
753 } else {
754 len = 1024;
755 xfer_id = WAC_CMD_ICON_XFER;
756 }
5d7e7d47 757
849e2f06 758 if (count != len)
5d7e7d47
EH
759 return -EINVAL;
760
761 mutex_lock(&wacom->lock);
762
849e2f06 763 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
5d7e7d47
EH
764
765 mutex_unlock(&wacom->lock);
766
767 return err < 0 ? err : count;
768}
769
770#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
771static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
772 struct device_attribute *attr, const char *buf, size_t count) \
773{ \
774 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
775} \
e0984bc3 776static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
5d7e7d47
EH
777 NULL, wacom_btnimg##BUTTON_ID##_store)
778
779DEVICE_BTNIMG_ATTR(0);
780DEVICE_BTNIMG_ATTR(1);
781DEVICE_BTNIMG_ATTR(2);
782DEVICE_BTNIMG_ATTR(3);
783DEVICE_BTNIMG_ATTR(4);
784DEVICE_BTNIMG_ATTR(5);
785DEVICE_BTNIMG_ATTR(6);
786DEVICE_BTNIMG_ATTR(7);
787
09e7d941
PC
788static struct attribute *cintiq_led_attrs[] = {
789 &dev_attr_status_led0_select.attr,
790 &dev_attr_status_led1_select.attr,
791 NULL
792};
793
794static struct attribute_group cintiq_led_attr_group = {
795 .name = "wacom_led",
796 .attrs = cintiq_led_attrs,
797};
798
799static struct attribute *intuos4_led_attrs[] = {
5d7e7d47
EH
800 &dev_attr_status0_luminance.attr,
801 &dev_attr_status1_luminance.attr,
09e7d941 802 &dev_attr_status_led0_select.attr,
5d7e7d47
EH
803 &dev_attr_buttons_luminance.attr,
804 &dev_attr_button0_rawimg.attr,
805 &dev_attr_button1_rawimg.attr,
806 &dev_attr_button2_rawimg.attr,
807 &dev_attr_button3_rawimg.attr,
808 &dev_attr_button4_rawimg.attr,
809 &dev_attr_button5_rawimg.attr,
810 &dev_attr_button6_rawimg.attr,
811 &dev_attr_button7_rawimg.attr,
812 NULL
813};
814
09e7d941 815static struct attribute_group intuos4_led_attr_group = {
5d7e7d47 816 .name = "wacom_led",
09e7d941 817 .attrs = intuos4_led_attrs,
5d7e7d47
EH
818};
819
9b5b95dd
JG
820static struct attribute *intuos5_led_attrs[] = {
821 &dev_attr_status0_luminance.attr,
822 &dev_attr_status_led0_select.attr,
823 NULL
824};
825
826static struct attribute_group intuos5_led_attr_group = {
827 .name = "wacom_led",
828 .attrs = intuos5_led_attrs,
829};
830
5d7e7d47
EH
831static int wacom_initialize_leds(struct wacom *wacom)
832{
833 int error;
834
09e7d941
PC
835 /* Initialize default values */
836 switch (wacom->wacom_wac.features.type) {
a19fc986 837 case INTUOS4S:
09e7d941 838 case INTUOS4:
81af7e61 839 case INTUOS4WL:
09e7d941
PC
840 case INTUOS4L:
841 wacom->led.select[0] = 0;
842 wacom->led.select[1] = 0;
f4fa9a6d 843 wacom->led.llv = 10;
5d7e7d47
EH
844 wacom->led.hlv = 20;
845 wacom->led.img_lum = 10;
c31a408f 846 error = sysfs_create_group(&wacom->hdev->dev.kobj,
09e7d941
PC
847 &intuos4_led_attr_group);
848 break;
849
246835fc 850 case WACOM_24HD:
09e7d941
PC
851 case WACOM_21UX2:
852 wacom->led.select[0] = 0;
853 wacom->led.select[1] = 0;
854 wacom->led.llv = 0;
855 wacom->led.hlv = 0;
856 wacom->led.img_lum = 0;
5d7e7d47 857
c31a408f 858 error = sysfs_create_group(&wacom->hdev->dev.kobj,
09e7d941
PC
859 &cintiq_led_attr_group);
860 break;
861
9b5b95dd
JG
862 case INTUOS5S:
863 case INTUOS5:
864 case INTUOS5L:
9a35c411
PC
865 case INTUOSPS:
866 case INTUOSPM:
867 case INTUOSPL:
c2b0c273
PC
868 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
869 wacom->led.select[0] = 0;
870 wacom->led.select[1] = 0;
871 wacom->led.llv = 32;
872 wacom->led.hlv = 0;
873 wacom->led.img_lum = 0;
874
c31a408f 875 error = sysfs_create_group(&wacom->hdev->dev.kobj,
c2b0c273
PC
876 &intuos5_led_attr_group);
877 } else
878 return 0;
9b5b95dd
JG
879 break;
880
09e7d941
PC
881 default:
882 return 0;
5d7e7d47
EH
883 }
884
09e7d941 885 if (error) {
c31a408f 886 hid_err(wacom->hdev,
09e7d941
PC
887 "cannot create sysfs group err: %d\n", error);
888 return error;
889 }
890 wacom_led_control(wacom);
c757cbaf 891 wacom->led_initialized = true;
09e7d941 892
5d7e7d47
EH
893 return 0;
894}
895
896static void wacom_destroy_leds(struct wacom *wacom)
897{
c757cbaf
BT
898 if (!wacom->led_initialized)
899 return;
900
901 wacom->led_initialized = false;
902
09e7d941 903 switch (wacom->wacom_wac.features.type) {
a19fc986 904 case INTUOS4S:
09e7d941 905 case INTUOS4:
81af7e61 906 case INTUOS4WL:
09e7d941 907 case INTUOS4L:
c31a408f 908 sysfs_remove_group(&wacom->hdev->dev.kobj,
09e7d941
PC
909 &intuos4_led_attr_group);
910 break;
911
246835fc 912 case WACOM_24HD:
09e7d941 913 case WACOM_21UX2:
c31a408f 914 sysfs_remove_group(&wacom->hdev->dev.kobj,
09e7d941
PC
915 &cintiq_led_attr_group);
916 break;
9b5b95dd
JG
917
918 case INTUOS5S:
919 case INTUOS5:
920 case INTUOS5L:
9a35c411
PC
921 case INTUOSPS:
922 case INTUOSPM:
923 case INTUOSPL:
c2b0c273 924 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
c31a408f 925 sysfs_remove_group(&wacom->hdev->dev.kobj,
c2b0c273 926 &intuos5_led_attr_group);
9b5b95dd 927 break;
5d7e7d47
EH
928 }
929}
930
a1d552cc 931static enum power_supply_property wacom_battery_props[] = {
ac8d1010 932 POWER_SUPPLY_PROP_STATUS,
6e2a6e80 933 POWER_SUPPLY_PROP_SCOPE,
a1d552cc
CB
934 POWER_SUPPLY_PROP_CAPACITY
935};
936
7dbd229e
BT
937static enum power_supply_property wacom_ac_props[] = {
938 POWER_SUPPLY_PROP_PRESENT,
939 POWER_SUPPLY_PROP_ONLINE,
940 POWER_SUPPLY_PROP_SCOPE,
941};
942
a1d552cc
CB
943static int wacom_battery_get_property(struct power_supply *psy,
944 enum power_supply_property psp,
945 union power_supply_propval *val)
946{
947 struct wacom *wacom = container_of(psy, struct wacom, battery);
948 int ret = 0;
949
950 switch (psp) {
6e2a6e80
BN
951 case POWER_SUPPLY_PROP_SCOPE:
952 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
953 break;
a1d552cc
CB
954 case POWER_SUPPLY_PROP_CAPACITY:
955 val->intval =
ac8d1010
BT
956 wacom->wacom_wac.battery_capacity;
957 break;
958 case POWER_SUPPLY_PROP_STATUS:
959 if (wacom->wacom_wac.bat_charging)
960 val->intval = POWER_SUPPLY_STATUS_CHARGING;
961 else if (wacom->wacom_wac.battery_capacity == 100 &&
962 wacom->wacom_wac.ps_connected)
963 val->intval = POWER_SUPPLY_STATUS_FULL;
964 else
965 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
a1d552cc
CB
966 break;
967 default:
968 ret = -EINVAL;
969 break;
970 }
971
972 return ret;
973}
974
7dbd229e
BT
975static int wacom_ac_get_property(struct power_supply *psy,
976 enum power_supply_property psp,
977 union power_supply_propval *val)
978{
979 struct wacom *wacom = container_of(psy, struct wacom, ac);
980 int ret = 0;
981
982 switch (psp) {
983 case POWER_SUPPLY_PROP_PRESENT:
984 /* fall through */
985 case POWER_SUPPLY_PROP_ONLINE:
986 val->intval = wacom->wacom_wac.ps_connected;
987 break;
988 case POWER_SUPPLY_PROP_SCOPE:
989 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
990 break;
991 default:
992 ret = -EINVAL;
993 break;
994 }
995 return ret;
996}
997
a1d552cc
CB
998static int wacom_initialize_battery(struct wacom *wacom)
999{
d70420b9 1000 static atomic_t battery_no = ATOMIC_INIT(0);
7dbd229e 1001 int error;
d70420b9 1002 unsigned long n;
a1d552cc 1003
ac8d1010 1004 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
d70420b9 1005 n = atomic_inc_return(&battery_no) - 1;
7dbd229e 1006
a1d552cc
CB
1007 wacom->battery.properties = wacom_battery_props;
1008 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
1009 wacom->battery.get_property = wacom_battery_get_property;
d70420b9
BT
1010 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
1011 wacom->battery.name = wacom->wacom_wac.bat_name;
a1d552cc
CB
1012 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1013 wacom->battery.use_for_apm = 0;
1014
7dbd229e
BT
1015 wacom->ac.properties = wacom_ac_props;
1016 wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
1017 wacom->ac.get_property = wacom_ac_get_property;
1018 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
1019 wacom->ac.name = wacom->wacom_wac.ac_name;
1020 wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
1021 wacom->ac.use_for_apm = 0;
1022
dd3181a7 1023 error = power_supply_register(&wacom->hdev->dev,
a1d552cc 1024 &wacom->battery);
7dbd229e
BT
1025 if (error)
1026 return error;
1027
1028 power_supply_powers(&wacom->battery, &wacom->hdev->dev);
b7af2bb8 1029
7dbd229e
BT
1030 error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
1031 if (error) {
1032 power_supply_unregister(&wacom->battery);
1033 return error;
1034 }
1035
1036 power_supply_powers(&wacom->ac, &wacom->hdev->dev);
a1d552cc
CB
1037 }
1038
7dbd229e 1039 return 0;
a1d552cc
CB
1040}
1041
1042static void wacom_destroy_battery(struct wacom *wacom)
1043{
ac8d1010
BT
1044 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1045 wacom->battery.dev) {
a1d552cc 1046 power_supply_unregister(&wacom->battery);
b7af2bb8 1047 wacom->battery.dev = NULL;
7dbd229e
BT
1048 power_supply_unregister(&wacom->ac);
1049 wacom->ac.dev = NULL;
b7af2bb8 1050 }
a1d552cc
CB
1051}
1052
f81a1295
BT
1053static ssize_t wacom_show_speed(struct device *dev,
1054 struct device_attribute
1055 *attr, char *buf)
1056{
1057 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1058 struct wacom *wacom = hid_get_drvdata(hdev);
1059
1060 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1061}
1062
1063static ssize_t wacom_store_speed(struct device *dev,
1064 struct device_attribute *attr,
1065 const char *buf, size_t count)
1066{
1067 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1068 struct wacom *wacom = hid_get_drvdata(hdev);
1069 u8 new_speed;
1070
1071 if (kstrtou8(buf, 0, &new_speed))
1072 return -EINVAL;
1073
1074 if (new_speed != 0 && new_speed != 1)
1075 return -EINVAL;
1076
1077 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1078
1079 return count;
1080}
1081
e0984bc3 1082static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
f81a1295
BT
1083 wacom_show_speed, wacom_store_speed);
1084
d2d13f18 1085static struct input_dev *wacom_allocate_input(struct wacom *wacom)
3aac0ef1
CB
1086{
1087 struct input_dev *input_dev;
b6c79f2c 1088 struct hid_device *hdev = wacom->hdev;
3aac0ef1 1089 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
3aac0ef1
CB
1090
1091 input_dev = input_allocate_device();
d2d13f18
BT
1092 if (!input_dev)
1093 return NULL;
3aac0ef1
CB
1094
1095 input_dev->name = wacom_wac->name;
b6c79f2c
BT
1096 input_dev->phys = hdev->phys;
1097 input_dev->dev.parent = &hdev->dev;
3aac0ef1
CB
1098 input_dev->open = wacom_open;
1099 input_dev->close = wacom_close;
b6c79f2c
BT
1100 input_dev->uniq = hdev->uniq;
1101 input_dev->id.bustype = hdev->bus;
1102 input_dev->id.vendor = hdev->vendor;
12969e3b 1103 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
b6c79f2c 1104 input_dev->id.version = hdev->version;
3aac0ef1
CB
1105 input_set_drvdata(input_dev, wacom);
1106
d2d13f18
BT
1107 return input_dev;
1108}
1109
2546dacd 1110static void wacom_free_inputs(struct wacom *wacom)
008f4d9e 1111{
2546dacd
BT
1112 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1113
1114 if (wacom_wac->input)
1115 input_free_device(wacom_wac->input);
1116 if (wacom_wac->pad_input)
1117 input_free_device(wacom_wac->pad_input);
1118 wacom_wac->input = NULL;
1119 wacom_wac->pad_input = NULL;
008f4d9e
BT
1120}
1121
2546dacd 1122static int wacom_allocate_inputs(struct wacom *wacom)
d2d13f18
BT
1123{
1124 struct input_dev *input_dev, *pad_input_dev;
1125 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
d2d13f18
BT
1126
1127 input_dev = wacom_allocate_input(wacom);
1128 pad_input_dev = wacom_allocate_input(wacom);
1129 if (!input_dev || !pad_input_dev) {
2546dacd
BT
1130 wacom_free_inputs(wacom);
1131 return -ENOMEM;
d2d13f18
BT
1132 }
1133
3aac0ef1 1134 wacom_wac->input = input_dev;
d2d13f18
BT
1135 wacom_wac->pad_input = pad_input_dev;
1136 wacom_wac->pad_input->name = wacom_wac->pad_name;
1137
2546dacd
BT
1138 return 0;
1139}
1140
1141static void wacom_clean_inputs(struct wacom *wacom)
1142{
1143 if (wacom->wacom_wac.input) {
1144 if (wacom->wacom_wac.input_registered)
1145 input_unregister_device(wacom->wacom_wac.input);
1146 else
1147 input_free_device(wacom->wacom_wac.input);
1148 }
1149 if (wacom->wacom_wac.pad_input) {
954df6ad 1150 if (wacom->wacom_wac.pad_registered)
2546dacd
BT
1151 input_unregister_device(wacom->wacom_wac.pad_input);
1152 else
1153 input_free_device(wacom->wacom_wac.pad_input);
1154 }
1155 wacom->wacom_wac.input = NULL;
1156 wacom->wacom_wac.pad_input = NULL;
1157 wacom_destroy_leds(wacom);
1158}
1159
1160static int wacom_register_inputs(struct wacom *wacom)
1161{
1162 struct input_dev *input_dev, *pad_input_dev;
1163 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1164 int error;
1165
1166 input_dev = wacom_wac->input;
1167 pad_input_dev = wacom_wac->pad_input;
1168
1169 if (!input_dev || !pad_input_dev)
1170 return -EINVAL;
1171
30ebc1ae
PC
1172 error = wacom_setup_pentouch_input_capabilities(input_dev, wacom_wac);
1173 if (!error) {
1174 error = input_register_device(input_dev);
1175 if (error)
1176 return error;
954df6ad 1177 wacom_wac->input_registered = true;
30ebc1ae 1178 }
1963518b 1179
d2d13f18
BT
1180 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1181 if (error) {
1182 /* no pad in use on this interface */
1183 input_free_device(pad_input_dev);
1184 wacom_wac->pad_input = NULL;
1185 pad_input_dev = NULL;
1186 } else {
1187 error = input_register_device(pad_input_dev);
1188 if (error)
7fefeec5 1189 goto fail_register_pad_input;
954df6ad 1190 wacom_wac->pad_registered = true;
912ca216
PC
1191
1192 error = wacom_initialize_leds(wacom);
1193 if (error)
7fefeec5 1194 goto fail_leds;
d2d13f18
BT
1195 }
1196
1963518b 1197 return 0;
3aac0ef1 1198
7fefeec5 1199fail_leds:
912ca216
PC
1200 input_unregister_device(pad_input_dev);
1201 pad_input_dev = NULL;
954df6ad 1202 wacom_wac->pad_registered = false;
7fefeec5 1203fail_register_pad_input:
d2d13f18 1204 input_unregister_device(input_dev);
1963518b 1205 wacom_wac->input = NULL;
954df6ad 1206 wacom_wac->input_registered = false;
3aac0ef1
CB
1207 return error;
1208}
1209
16bf288c
CB
1210static void wacom_wireless_work(struct work_struct *work)
1211{
1212 struct wacom *wacom = container_of(work, struct wacom, work);
1213 struct usb_device *usbdev = wacom->usbdev;
1214 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
29b47391 1215 struct hid_device *hdev1, *hdev2;
b7af2bb8
CB
1216 struct wacom *wacom1, *wacom2;
1217 struct wacom_wac *wacom_wac1, *wacom_wac2;
1218 int error;
16bf288c
CB
1219
1220 /*
1221 * Regardless if this is a disconnect or a new tablet,
b7af2bb8 1222 * remove any existing input and battery devices.
16bf288c
CB
1223 */
1224
b7af2bb8
CB
1225 wacom_destroy_battery(wacom);
1226
16bf288c 1227 /* Stylus interface */
29b47391
BT
1228 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1229 wacom1 = hid_get_drvdata(hdev1);
b7af2bb8 1230 wacom_wac1 = &(wacom1->wacom_wac);
2546dacd 1231 wacom_clean_inputs(wacom1);
16bf288c
CB
1232
1233 /* Touch interface */
29b47391
BT
1234 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1235 wacom2 = hid_get_drvdata(hdev2);
b7af2bb8 1236 wacom_wac2 = &(wacom2->wacom_wac);
2546dacd 1237 wacom_clean_inputs(wacom2);
16bf288c
CB
1238
1239 if (wacom_wac->pid == 0) {
e2114ce1 1240 hid_info(wacom->hdev, "wireless tablet disconnected\n");
ac8d1010 1241 wacom_wac1->shared->type = 0;
16bf288c 1242 } else {
29b47391 1243 const struct hid_device_id *id = wacom_ids;
16bf288c 1244
e2114ce1 1245 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
eb71d1bb 1246 wacom_wac->pid);
16bf288c 1247
29b47391
BT
1248 while (id->bus) {
1249 if (id->vendor == USB_VENDOR_ID_WACOM &&
1250 id->product == wacom_wac->pid)
16bf288c
CB
1251 break;
1252 id++;
1253 }
1254
29b47391 1255 if (!id->bus) {
e2114ce1 1256 hid_info(wacom->hdev, "ignoring unknown PID.\n");
16bf288c
CB
1257 return;
1258 }
1259
1260 /* Stylus interface */
b7af2bb8 1261 wacom_wac1->features =
29b47391 1262 *((struct wacom_features *)id->driver_data);
b7af2bb8 1263 wacom_wac1->features.device_type = BTN_TOOL_PEN;
57bcfce3
PC
1264 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1265 wacom_wac1->features.name);
008f4d9e
BT
1266 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1267 wacom_wac1->features.name);
961794a0
PC
1268 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1269 wacom_wac1->shared->type = wacom_wac1->features.type;
912ca216 1270 wacom_wac1->pid = wacom_wac->pid;
2546dacd
BT
1271 error = wacom_allocate_inputs(wacom1) ||
1272 wacom_register_inputs(wacom1);
b7af2bb8 1273 if (error)
57bcfce3 1274 goto fail;
16bf288c
CB
1275
1276 /* Touch interface */
b5fd2a3e
PC
1277 if (wacom_wac1->features.touch_max ||
1278 wacom_wac1->features.type == INTUOSHT) {
57bcfce3 1279 wacom_wac2->features =
29b47391 1280 *((struct wacom_features *)id->driver_data);
57bcfce3
PC
1281 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1282 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1283 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1284 if (wacom_wac2->features.touch_max)
1285 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1286 "%s (WL) Finger",wacom_wac2->features.name);
1287 else
1288 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1289 "%s (WL) Pad",wacom_wac2->features.name);
008f4d9e
BT
1290 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1291 "%s (WL) Pad", wacom_wac2->features.name);
912ca216 1292 wacom_wac2->pid = wacom_wac->pid;
2546dacd
BT
1293 error = wacom_allocate_inputs(wacom2) ||
1294 wacom_register_inputs(wacom2);
57bcfce3
PC
1295 if (error)
1296 goto fail;
961794a0
PC
1297
1298 if (wacom_wac1->features.type == INTUOSHT &&
1299 wacom_wac1->features.touch_max)
1300 wacom_wac->shared->touch_input = wacom_wac2->input;
57bcfce3 1301 }
b7af2bb8
CB
1302
1303 error = wacom_initialize_battery(wacom);
1304 if (error)
57bcfce3 1305 goto fail;
16bf288c 1306 }
b7af2bb8
CB
1307
1308 return;
1309
57bcfce3 1310fail:
2546dacd
BT
1311 wacom_clean_inputs(wacom1);
1312 wacom_clean_inputs(wacom2);
b7af2bb8 1313 return;
16bf288c
CB
1314}
1315
401d7d10
PC
1316/*
1317 * Not all devices report physical dimensions from HID.
1318 * Compute the default from hardcoded logical dimension
1319 * and resolution before driver overwrites them.
1320 */
1321static void wacom_set_default_phy(struct wacom_features *features)
1322{
1323 if (features->x_resolution) {
1324 features->x_phy = (features->x_max * 100) /
1325 features->x_resolution;
1326 features->y_phy = (features->y_max * 100) /
1327 features->y_resolution;
1328 }
1329}
1330
1331static void wacom_calculate_res(struct wacom_features *features)
1332{
1333 features->x_resolution = wacom_calc_hid_res(features->x_max,
1334 features->x_phy,
1335 features->unit,
1336 features->unitExpo);
1337 features->y_resolution = wacom_calc_hid_res(features->y_max,
1338 features->y_phy,
1339 features->unit,
1340 features->unitExpo);
1341}
1342
01c846f9
BT
1343static size_t wacom_compute_pktlen(struct hid_device *hdev)
1344{
1345 struct hid_report_enum *report_enum;
1346 struct hid_report *report;
1347 size_t size = 0;
1348
1349 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1350
1351 list_for_each_entry(report, &report_enum->report_list, list) {
dabb05c6 1352 size_t report_size = hid_report_len(report);
01c846f9
BT
1353 if (report_size > size)
1354 size = report_size;
1355 }
1356
1357 return size;
1358}
1359
29b47391
BT
1360static int wacom_probe(struct hid_device *hdev,
1361 const struct hid_device_id *id)
3bea733a 1362{
29b47391 1363 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
3bea733a 1364 struct usb_device *dev = interface_to_usbdev(intf);
3bea733a
PC
1365 struct wacom *wacom;
1366 struct wacom_wac *wacom_wac;
e33da8a5 1367 struct wacom_features *features;
e33da8a5 1368 int error;
7704ac93 1369 unsigned int connect_mask = HID_CONNECT_HIDRAW;
3bea733a 1370
29b47391 1371 if (!id->driver_data)
b036f6fb
BB
1372 return -EINVAL;
1373
8ffffd52
BT
1374 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1375
3bea733a 1376 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
f1823940
DC
1377 if (!wacom)
1378 return -ENOMEM;
e33da8a5 1379
29b47391
BT
1380 hid_set_drvdata(hdev, wacom);
1381 wacom->hdev = hdev;
1382
ba9a3541
BT
1383 /* ask for the report descriptor to be loaded by HID */
1384 error = hid_parse(hdev);
1385 if (error) {
1386 hid_err(hdev, "parse failed\n");
7fefeec5 1387 goto fail_parse;
ba9a3541
BT
1388 }
1389
51269fe8 1390 wacom_wac = &wacom->wacom_wac;
29b47391 1391 wacom_wac->features = *((struct wacom_features *)id->driver_data);
e33da8a5 1392 features = &wacom_wac->features;
01c846f9 1393 features->pktlen = wacom_compute_pktlen(hdev);
e33da8a5
JC
1394 if (features->pktlen > WACOM_PKGLEN_MAX) {
1395 error = -EINVAL;
7fefeec5 1396 goto fail_pktlen;
e33da8a5 1397 }
3bea733a 1398
29b47391
BT
1399 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1400 error = -ENODEV;
7fefeec5 1401 goto fail_type;
e33da8a5 1402 }
3bea733a 1403
3bea733a 1404 wacom->usbdev = dev;
e7224094
ON
1405 wacom->intf = intf;
1406 mutex_init(&wacom->lock);
16bf288c 1407 INIT_WORK(&wacom->work, wacom_wireless_work);
3bea733a 1408
494078b0
BT
1409 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1410 error = wacom_allocate_inputs(wacom);
1411 if (error)
1412 goto fail_allocate_inputs;
1413 }
1414
401d7d10
PC
1415 /* set the default size in case we do not get them from hid */
1416 wacom_set_default_phy(features);
1417
f393ee2b 1418 /* Retrieve the physical and logical size for touch devices */
c669fb2b 1419 wacom_retrieve_hid_descriptor(hdev, features);
545f4e99 1420
ae584ca4
JG
1421 /*
1422 * Intuos5 has no useful data about its touch interface in its
01c846f9 1423 * HID descriptor. If this is the touch interface (PacketSize
ae584ca4
JG
1424 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1425 */
b5fd2a3e 1426 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
01c846f9 1427 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
ae584ca4 1428 features->device_type = BTN_TOOL_FINGER;
ae584ca4 1429
ae584ca4
JG
1430 features->x_max = 4096;
1431 features->y_max = 4096;
1432 } else {
1433 features->device_type = BTN_TOOL_PEN;
1434 }
1435 }
1436
c669fb2b
BT
1437 /*
1438 * Same thing for Bamboo 3rd gen.
1439 */
1440 if ((features->type == BAMBOO_PT) &&
1441 (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1442 (features->device_type == BTN_TOOL_PEN)) {
1443 features->device_type = BTN_TOOL_FINGER;
1444
1445 features->x_max = 4096;
1446 features->y_max = 4096;
1447 }
1448
f81a1295
BT
1449 if (hdev->bus == BUS_BLUETOOTH)
1450 features->quirks |= WACOM_QUIRK_BATTERY;
1451
bc73dd39
HR
1452 wacom_setup_device_quirks(features);
1453
401d7d10
PC
1454 /* set unit to "100th of a mm" for devices not reported by HID */
1455 if (!features->unit) {
1456 features->unit = 0x11;
c669fb2b 1457 features->unitExpo = -3;
401d7d10
PC
1458 }
1459 wacom_calculate_res(features);
1460
49b764ae 1461 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
d2d13f18
BT
1462 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1463 "%s Pad", features->name);
49b764ae 1464
bc73dd39 1465 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
49b764ae 1466 /* Append the device type to the name */
57bcfce3
PC
1467 if (features->device_type != BTN_TOOL_FINGER)
1468 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1469 else if (features->touch_max)
1470 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1471 else
1472 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
4492efff 1473
4451e088 1474 error = wacom_add_shared_data(hdev);
4492efff 1475 if (error)
7fefeec5 1476 goto fail_shared_data;
49b764ae
PC
1477 }
1478
f81a1295
BT
1479 if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1480 (features->quirks & WACOM_QUIRK_BATTERY)) {
1481 error = wacom_initialize_battery(wacom);
1482 if (error)
7fefeec5 1483 goto fail_battery;
f81a1295
BT
1484 }
1485
d3825d51 1486 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
494078b0 1487 error = wacom_register_inputs(wacom);
d3825d51 1488 if (error)
7fefeec5 1489 goto fail_register_inputs;
f81a1295
BT
1490 }
1491
1492 if (hdev->bus == BUS_BLUETOOTH) {
1493 error = device_create_file(&hdev->dev, &dev_attr_speed);
1494 if (error)
1495 hid_warn(hdev,
1496 "can't create sysfs speed attribute err: %d\n",
1497 error);
d3825d51 1498 }
5d7e7d47 1499
7704ac93
BT
1500 if (features->type == HID_GENERIC)
1501 connect_mask |= HID_CONNECT_DRIVER;
1502
29b47391 1503 /* Regular HID work starts now */
7704ac93 1504 error = hid_hw_start(hdev, connect_mask);
29b47391
BT
1505 if (error) {
1506 hid_err(hdev, "hw start failed\n");
7fefeec5 1507 goto fail_hw_start;
d3825d51 1508 }
961794a0 1509
5ae6e89f
BT
1510 /* Note that if query fails it is not a hard failure */
1511 wacom_query_tablet_data(hdev, features);
1512
29b47391
BT
1513 if (features->quirks & WACOM_QUIRK_MONITOR)
1514 error = hid_hw_open(hdev);
1515
961794a0
PC
1516 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1517 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1518 wacom_wac->shared->touch_input = wacom_wac->input;
1519 }
1520
3bea733a
PC
1521 return 0;
1522
7fefeec5 1523fail_hw_start:
7fefeec5 1524 if (hdev->bus == BUS_BLUETOOTH)
f81a1295 1525 device_remove_file(&hdev->dev, &dev_attr_speed);
7fefeec5 1526fail_register_inputs:
2546dacd 1527 wacom_clean_inputs(wacom);
7fefeec5
BT
1528 wacom_destroy_battery(wacom);
1529fail_battery:
1530 wacom_remove_shared_data(wacom_wac);
1531fail_shared_data:
494078b0
BT
1532 wacom_clean_inputs(wacom);
1533fail_allocate_inputs:
7fefeec5
BT
1534fail_type:
1535fail_pktlen:
1536fail_parse:
1537 kfree(wacom);
29b47391 1538 hid_set_drvdata(hdev, NULL);
5014186d 1539 return error;
3bea733a
PC
1540}
1541
29b47391 1542static void wacom_remove(struct hid_device *hdev)
3bea733a 1543{
29b47391 1544 struct wacom *wacom = hid_get_drvdata(hdev);
3bea733a 1545
29b47391 1546 hid_hw_stop(hdev);
e7224094 1547
16bf288c 1548 cancel_work_sync(&wacom->work);
2546dacd 1549 wacom_clean_inputs(wacom);
f81a1295
BT
1550 if (hdev->bus == BUS_BLUETOOTH)
1551 device_remove_file(&hdev->dev, &dev_attr_speed);
a1d552cc 1552 wacom_destroy_battery(wacom);
51269fe8 1553 wacom_remove_shared_data(&wacom->wacom_wac);
e7224094 1554
29b47391
BT
1555 hid_set_drvdata(hdev, NULL);
1556 kfree(wacom);
e7224094
ON
1557}
1558
41a74581 1559#ifdef CONFIG_PM
29b47391 1560static int wacom_resume(struct hid_device *hdev)
e7224094 1561{
29b47391 1562 struct wacom *wacom = hid_get_drvdata(hdev);
51269fe8 1563 struct wacom_features *features = &wacom->wacom_wac.features;
e7224094
ON
1564
1565 mutex_lock(&wacom->lock);
38101475
PC
1566
1567 /* switch to wacom mode first */
27b20a9d 1568 wacom_query_tablet_data(hdev, features);
5d7e7d47 1569 wacom_led_control(wacom);
38101475 1570
e7224094
ON
1571 mutex_unlock(&wacom->lock);
1572
29b47391 1573 return 0;
e7224094
ON
1574}
1575
29b47391 1576static int wacom_reset_resume(struct hid_device *hdev)
e7224094 1577{
29b47391 1578 return wacom_resume(hdev);
3bea733a 1579}
41a74581 1580#endif /* CONFIG_PM */
3bea733a 1581
29b47391 1582static struct hid_driver wacom_driver = {
3bea733a 1583 .name = "wacom",
b036f6fb 1584 .id_table = wacom_ids,
3bea733a 1585 .probe = wacom_probe,
29b47391 1586 .remove = wacom_remove,
7704ac93
BT
1587 .event = wacom_wac_event,
1588 .report = wacom_wac_report,
29b47391 1589#ifdef CONFIG_PM
e7224094
ON
1590 .resume = wacom_resume,
1591 .reset_resume = wacom_reset_resume,
29b47391
BT
1592#endif
1593 .raw_event = wacom_raw_event,
3bea733a 1594};
29b47391 1595module_hid_driver(wacom_driver);
f2e0a7d4
BT
1596
1597MODULE_VERSION(DRIVER_VERSION);
1598MODULE_AUTHOR(DRIVER_AUTHOR);
1599MODULE_DESCRIPTION(DRIVER_DESC);
1600MODULE_LICENSE(DRIVER_LICENSE);
This page took 0.575091 seconds and 5 git commands to generate.