2 * Bluetooth Wacom Tablet support
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7 * Copyright (c) 2006-2007 Jiri Kosina
8 * Copyright (c) 2007 Paul Walmsley
9 * Copyright (c) 2008 Jiri Slaby <jirislaby@gmail.com>
10 * Copyright (c) 2006 Andrew Zabolotny <zap@homelink.ru>
11 * Copyright (c) 2009 Bastien Nocera <hadess@hadess.net>
12 * Copyright (c) 2011 Przemysław Firszt <przemo@firszt.eu>
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/device.h>
25 #include <linux/hid.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
29 #include <linux/power_supply.h>
34 #define PAD_DEVICE_ID 0x0F
43 unsigned char high_speed;
44 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
46 struct power_supply battery;
47 struct power_supply ac;
51 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
52 /*percent of battery capacity, 0 means AC online*/
53 static unsigned short batcap[8] = { 1, 15, 25, 35, 50, 70, 100, 0 };
55 static enum power_supply_property wacom_battery_props[] = {
56 POWER_SUPPLY_PROP_PRESENT,
57 POWER_SUPPLY_PROP_CAPACITY,
58 POWER_SUPPLY_PROP_SCOPE,
61 static enum power_supply_property wacom_ac_props[] = {
62 POWER_SUPPLY_PROP_PRESENT,
63 POWER_SUPPLY_PROP_ONLINE,
64 POWER_SUPPLY_PROP_SCOPE,
67 static int wacom_battery_get_property(struct power_supply *psy,
68 enum power_supply_property psp,
69 union power_supply_propval *val)
71 struct wacom_data *wdata = container_of(psy,
72 struct wacom_data, battery);
73 int power_state = batcap[wdata->battery_capacity];
77 case POWER_SUPPLY_PROP_PRESENT:
80 case POWER_SUPPLY_PROP_SCOPE:
81 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
83 case POWER_SUPPLY_PROP_CAPACITY:
84 /* show 100% battery capacity when charging */
88 val->intval = power_state;
97 static int wacom_ac_get_property(struct power_supply *psy,
98 enum power_supply_property psp,
99 union power_supply_propval *val)
101 struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
102 int power_state = batcap[wdata->battery_capacity];
106 case POWER_SUPPLY_PROP_PRESENT:
108 case POWER_SUPPLY_PROP_ONLINE:
109 if (power_state == 0)
114 case POWER_SUPPLY_PROP_SCOPE:
115 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
125 static void wacom_set_features(struct hid_device *hdev)
130 /*set high speed, tablet mode*/
133 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
138 static void wacom_poke(struct hid_device *hdev, u8 speed)
140 struct wacom_data *wdata = hid_get_drvdata(hdev);
144 rep_data[0] = 0x03 ; rep_data[1] = 0x00;
147 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
149 } while (ret < 0 && limit-- > 0);
160 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
162 } while (ret < 0 && limit-- > 0);
165 wdata->high_speed = speed;
171 * Note that if the raw queries fail, it's not a hard failure and it
172 * is safe to continue
174 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
179 static ssize_t wacom_show_speed(struct device *dev,
180 struct device_attribute
183 struct wacom_data *wdata = dev_get_drvdata(dev);
185 return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
188 static ssize_t wacom_store_speed(struct device *dev,
189 struct device_attribute *attr,
190 const char *buf, size_t count)
192 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
195 if (sscanf(buf, "%1d", &new_speed ) != 1)
198 if (new_speed == 0 || new_speed == 1) {
199 wacom_poke(hdev, new_speed);
200 return strnlen(buf, PAGE_SIZE);
205 static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,
206 wacom_show_speed, wacom_store_speed);
208 static int wacom_gr_parse_report(struct hid_device *hdev,
209 struct wacom_data *wdata,
210 struct input_dev *input, unsigned char *data)
215 /* Get X & Y positions */
216 x = le16_to_cpu(*(__le16 *) &data[2]);
217 y = le16_to_cpu(*(__le16 *) &data[4]);
219 /* Get current tool identifier */
220 if (data[1] & 0x90) { /* If pen is in the in/active area */
221 switch ((data[1] >> 5) & 3) {
227 tool = BTN_TOOL_RUBBER;
230 case 2: /* Mouse with wheel */
231 case 3: /* Mouse without wheel */
232 tool = BTN_TOOL_MOUSE;
236 /* Reset tool if out of active tablet area */
237 if (!(data[1] & 0x10))
241 /* If tool changed, notify input subsystem */
242 if (wdata->tool != tool) {
244 /* Completely reset old tool state */
245 if (wdata->tool == BTN_TOOL_MOUSE) {
246 input_report_key(input, BTN_LEFT, 0);
247 input_report_key(input, BTN_RIGHT, 0);
248 input_report_key(input, BTN_MIDDLE, 0);
249 input_report_abs(input, ABS_DISTANCE,
250 input_abs_get_max(input, ABS_DISTANCE));
252 input_report_key(input, BTN_TOUCH, 0);
253 input_report_key(input, BTN_STYLUS, 0);
254 input_report_key(input, BTN_STYLUS2, 0);
255 input_report_abs(input, ABS_PRESSURE, 0);
257 input_report_key(input, wdata->tool, 0);
262 input_report_key(input, tool, 1);
266 input_report_abs(input, ABS_X, x);
267 input_report_abs(input, ABS_Y, y);
269 switch ((data[1] >> 5) & 3) {
270 case 2: /* Mouse with wheel */
271 input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
272 rw = (data[6] & 0x01) ? -1 :
273 (data[6] & 0x02) ? 1 : 0;
274 input_report_rel(input, REL_WHEEL, rw);
277 case 3: /* Mouse without wheel */
278 input_report_key(input, BTN_LEFT, data[1] & 0x01);
279 input_report_key(input, BTN_RIGHT, data[1] & 0x02);
280 /* Compute distance between mouse and tablet */
281 rw = 44 - (data[6] >> 2);
286 input_report_abs(input, ABS_DISTANCE, rw);
290 input_report_abs(input, ABS_PRESSURE,
291 data[6] | (((__u16) (data[1] & 0x08)) << 5));
292 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
293 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
294 input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
301 /* Report the state of the two buttons at the top of the tablet
302 * as two extra fingerpad keys (buttons 4 & 5). */
304 if (rw != wdata->butstate) {
305 wdata->butstate = rw;
306 input_report_key(input, BTN_0, rw & 0x02);
307 input_report_key(input, BTN_1, rw & 0x01);
308 input_report_key(input, BTN_TOOL_FINGER, 0xf0);
309 input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
313 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
314 /* Store current battery capacity */
315 rw = (data[7] >> 2 & 0x07);
316 if (rw != wdata->battery_capacity)
317 wdata->battery_capacity = rw;
322 static void wacom_i4_parse_button_report(struct wacom_data *wdata,
323 struct input_dev *input, unsigned char *data)
329 new_whlstate = data[1];
330 if (new_whlstate != wdata->whlstate) {
331 wdata->whlstate = new_whlstate;
332 if (new_whlstate & 0x80) {
333 input_report_key(input, BTN_TOUCH, 1);
334 input_report_abs(input, ABS_WHEEL, (new_whlstate & 0x7f));
335 input_report_key(input, BTN_TOOL_FINGER, 1);
337 input_report_key(input, BTN_TOUCH, 0);
338 input_report_abs(input, ABS_WHEEL, 0);
339 input_report_key(input, BTN_TOOL_FINGER, 0);
344 new_butstate = (data[3] << 1) | (data[2] & 0x01);
345 if (new_butstate != wdata->butstate) {
346 wdata->butstate = new_butstate;
347 input_report_key(input, BTN_0, new_butstate & 0x001);
348 input_report_key(input, BTN_1, new_butstate & 0x002);
349 input_report_key(input, BTN_2, new_butstate & 0x004);
350 input_report_key(input, BTN_3, new_butstate & 0x008);
351 input_report_key(input, BTN_4, new_butstate & 0x010);
352 input_report_key(input, BTN_5, new_butstate & 0x020);
353 input_report_key(input, BTN_6, new_butstate & 0x040);
354 input_report_key(input, BTN_7, new_butstate & 0x080);
355 input_report_key(input, BTN_8, new_butstate & 0x100);
356 input_report_key(input, BTN_TOOL_FINGER, 1);
361 input_report_abs(input, ABS_MISC, PAD_DEVICE_ID);
362 input_event(input, EV_MSC, MSC_SERIAL, 0xffffffff);
367 static void wacom_i4_parse_pen_report(struct wacom_data *wdata,
368 struct input_dev *input, unsigned char *data)
370 __u16 x, y, pressure;
374 case 0x80: /* Out of proximity report */
375 input_report_key(input, BTN_TOUCH, 0);
376 input_report_abs(input, ABS_PRESSURE, 0);
377 input_report_key(input, BTN_STYLUS, 0);
378 input_report_key(input, BTN_STYLUS2, 0);
379 input_report_key(input, wdata->tool, 0);
380 input_report_abs(input, ABS_MISC, 0);
381 input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
385 case 0xC2: /* Tool report */
386 wdata->id = ((data[2] << 4) | (data[3] >> 4) |
387 ((data[7] & 0x0f) << 20) |
388 ((data[8] & 0xf0) << 12));
389 wdata->serial = ((data[3] & 0x0f) << 28) +
390 (data[4] << 20) + (data[5] << 12) +
391 (data[6] << 4) + (data[7] >> 4);
395 wdata->tool = BTN_TOOL_PEN;
398 wdata->tool = BTN_TOOL_RUBBER;
402 default: /* Position/pressure report */
403 x = data[2] << 9 | data[3] << 1 | ((data[9] & 0x02) >> 1);
404 y = data[4] << 9 | data[5] << 1 | (data[9] & 0x01);
405 pressure = (data[6] << 3) | ((data[7] & 0xC0) >> 5)
407 distance = (data[9] >> 2) & 0x3f;
409 input_report_key(input, BTN_TOUCH, pressure > 1);
411 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
412 input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
413 input_report_key(input, wdata->tool, 1);
414 input_report_abs(input, ABS_X, x);
415 input_report_abs(input, ABS_Y, y);
416 input_report_abs(input, ABS_PRESSURE, pressure);
417 input_report_abs(input, ABS_DISTANCE, distance);
418 input_report_abs(input, ABS_MISC, wdata->id);
419 input_event(input, EV_MSC, MSC_SERIAL, wdata->serial);
420 input_report_key(input, wdata->tool, 1);
428 static void wacom_i4_parse_report(struct hid_device *hdev,
429 struct wacom_data *wdata,
430 struct input_dev *input, unsigned char *data)
433 case 0x00: /* Empty report */
435 case 0x02: /* Pen report */
436 wacom_i4_parse_pen_report(wdata, input, data);
438 case 0x03: /* Features Report */
439 wdata->features = data[2];
441 case 0x0C: /* Button report */
442 wacom_i4_parse_button_report(wdata, input, data);
445 hid_err(hdev, "Unknown report: %d,%d\n", data[0], data[1]);
450 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
451 u8 *raw_data, int size)
453 struct wacom_data *wdata = hid_get_drvdata(hdev);
454 struct hid_input *hidinput;
455 struct input_dev *input;
456 unsigned char *data = (unsigned char *) raw_data;
459 if (!(hdev->claimed & HID_CLAIMED_INPUT))
462 hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
463 input = hidinput->input;
465 /* Check if this is a tablet report */
469 switch (hdev->product) {
470 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
471 return wacom_gr_parse_report(hdev, wdata, input, data);
473 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
478 wacom_i4_parse_report(hdev, wdata, input, data + i);
482 wacom_i4_parse_report(hdev, wdata, input, data + i);
484 wacom_i4_parse_report(hdev, wdata, input, data + i);
487 hid_err(hdev, "Unknown report: %d,%d size:%d\n",
488 data[0], data[1], size);
495 static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
496 struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
499 struct input_dev *input = hi->input;
501 __set_bit(INPUT_PROP_POINTER, input->propbit);
504 input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
506 __set_bit(REL_WHEEL, input->relbit);
508 __set_bit(BTN_TOOL_PEN, input->keybit);
509 __set_bit(BTN_TOUCH, input->keybit);
510 __set_bit(BTN_STYLUS, input->keybit);
511 __set_bit(BTN_STYLUS2, input->keybit);
512 __set_bit(BTN_LEFT, input->keybit);
513 __set_bit(BTN_RIGHT, input->keybit);
514 __set_bit(BTN_MIDDLE, input->keybit);
517 input_set_capability(input, EV_MSC, MSC_SERIAL);
519 __set_bit(BTN_0, input->keybit);
520 __set_bit(BTN_1, input->keybit);
521 __set_bit(BTN_TOOL_FINGER, input->keybit);
523 /* Distance, rubber and mouse */
524 __set_bit(BTN_TOOL_RUBBER, input->keybit);
525 __set_bit(BTN_TOOL_MOUSE, input->keybit);
527 switch (hdev->product) {
528 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
529 input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
530 input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
531 input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
532 input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
534 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
535 __set_bit(ABS_WHEEL, input->absbit);
536 __set_bit(ABS_MISC, input->absbit);
537 __set_bit(BTN_2, input->keybit);
538 __set_bit(BTN_3, input->keybit);
539 __set_bit(BTN_4, input->keybit);
540 __set_bit(BTN_5, input->keybit);
541 __set_bit(BTN_6, input->keybit);
542 __set_bit(BTN_7, input->keybit);
543 __set_bit(BTN_8, input->keybit);
544 input_set_abs_params(input, ABS_WHEEL, 0, 71, 0, 0);
545 input_set_abs_params(input, ABS_X, 0, 40640, 4, 0);
546 input_set_abs_params(input, ABS_Y, 0, 25400, 4, 0);
547 input_set_abs_params(input, ABS_PRESSURE, 0, 2047, 0, 0);
548 input_set_abs_params(input, ABS_DISTANCE, 0, 63, 0, 0);
555 static int wacom_probe(struct hid_device *hdev,
556 const struct hid_device_id *id)
558 struct wacom_data *wdata;
561 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
563 hid_err(hdev, "can't alloc wacom descriptor\n");
567 hid_set_drvdata(hdev, wdata);
569 /* Parse the HID report now */
570 ret = hid_parse(hdev);
572 hid_err(hdev, "parse failed\n");
576 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
578 hid_err(hdev, "hw start failed\n");
582 ret = device_create_file(&hdev->dev, &dev_attr_speed);
585 "can't create sysfs speed attribute err: %d\n", ret);
587 switch (hdev->product) {
588 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
589 /* Set Wacom mode 2 with high reporting speed */
592 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
593 sprintf(hdev->name, "%s", "Wacom Intuos4 WL");
595 wacom_set_features(hdev);
599 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
600 wdata->battery.properties = wacom_battery_props;
601 wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
602 wdata->battery.get_property = wacom_battery_get_property;
603 wdata->battery.name = "wacom_battery";
604 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
605 wdata->battery.use_for_apm = 0;
608 ret = power_supply_register(&hdev->dev, &wdata->battery);
610 hid_warn(hdev, "can't create sysfs battery attribute, err: %d\n",
615 power_supply_powers(&wdata->battery, &hdev->dev);
617 wdata->ac.properties = wacom_ac_props;
618 wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
619 wdata->ac.get_property = wacom_ac_get_property;
620 wdata->ac.name = "wacom_ac";
621 wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
622 wdata->ac.use_for_apm = 0;
624 ret = power_supply_register(&hdev->dev, &wdata->ac);
627 "can't create ac battery attribute, err: %d\n", ret);
631 power_supply_powers(&wdata->ac, &hdev->dev);
635 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
637 power_supply_unregister(&wdata->battery);
639 device_remove_file(&hdev->dev, &dev_attr_speed);
647 static void wacom_remove(struct hid_device *hdev)
649 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
650 struct wacom_data *wdata = hid_get_drvdata(hdev);
652 device_remove_file(&hdev->dev, &dev_attr_speed);
655 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
656 power_supply_unregister(&wdata->battery);
657 power_supply_unregister(&wdata->ac);
659 kfree(hid_get_drvdata(hdev));
662 static const struct hid_device_id wacom_devices[] = {
663 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
664 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
668 MODULE_DEVICE_TABLE(hid, wacom_devices);
670 static struct hid_driver wacom_driver = {
672 .id_table = wacom_devices,
673 .probe = wacom_probe,
674 .remove = wacom_remove,
675 .raw_event = wacom_raw_event,
676 .input_mapped = wacom_input_mapped,
679 static int __init wacom_init(void)
683 ret = hid_register_driver(&wacom_driver);
685 pr_err("can't register wacom driver\n");
689 static void __exit wacom_exit(void)
691 hid_unregister_driver(&wacom_driver);
694 module_init(wacom_init);
695 module_exit(wacom_exit);
696 MODULE_LICENSE("GPL");