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