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