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