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>
36 unsigned char butstate;
38 unsigned char high_speed;
39 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
41 struct power_supply battery;
42 struct power_supply ac;
46 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
47 /*percent of battery capacity, 0 means AC online*/
48 static unsigned short batcap[8] = { 1, 15, 25, 35, 50, 70, 100, 0 };
50 static enum power_supply_property wacom_battery_props[] = {
51 POWER_SUPPLY_PROP_PRESENT,
52 POWER_SUPPLY_PROP_CAPACITY
55 static enum power_supply_property wacom_ac_props[] = {
56 POWER_SUPPLY_PROP_PRESENT,
57 POWER_SUPPLY_PROP_ONLINE
60 static int wacom_battery_get_property(struct power_supply *psy,
61 enum power_supply_property psp,
62 union power_supply_propval *val)
64 struct wacom_data *wdata = container_of(psy,
65 struct wacom_data, battery);
66 int power_state = batcap[wdata->battery_capacity];
70 case POWER_SUPPLY_PROP_PRESENT:
73 case POWER_SUPPLY_PROP_CAPACITY:
74 /* show 100% battery capacity when charging */
78 val->intval = power_state;
87 static int wacom_ac_get_property(struct power_supply *psy,
88 enum power_supply_property psp,
89 union power_supply_propval *val)
91 struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
92 int power_state = batcap[wdata->battery_capacity];
96 case POWER_SUPPLY_PROP_PRESENT:
98 case POWER_SUPPLY_PROP_ONLINE:
112 static void wacom_set_features(struct hid_device *hdev)
117 /*set high speed, tablet mode*/
120 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
125 static void wacom_poke(struct hid_device *hdev, u8 speed)
127 struct wacom_data *wdata = hid_get_drvdata(hdev);
131 rep_data[0] = 0x03 ; rep_data[1] = 0x00;
134 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
136 } while (ret < 0 && limit-- > 0);
147 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
149 } while (ret < 0 && limit-- > 0);
152 wdata->high_speed = speed;
158 * Note that if the raw queries fail, it's not a hard failure and it
159 * is safe to continue
161 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
166 static ssize_t wacom_show_speed(struct device *dev,
167 struct device_attribute
170 struct wacom_data *wdata = dev_get_drvdata(dev);
172 return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
175 static ssize_t wacom_store_speed(struct device *dev,
176 struct device_attribute *attr,
177 const char *buf, size_t count)
179 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
182 if (sscanf(buf, "%1d", &new_speed ) != 1)
185 if (new_speed == 0 || new_speed == 1) {
186 wacom_poke(hdev, new_speed);
187 return strnlen(buf, PAGE_SIZE);
192 static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,
193 wacom_show_speed, wacom_store_speed);
195 static int wacom_gr_parse_report(struct hid_device *hdev,
196 struct wacom_data *wdata,
197 struct input_dev *input, unsigned char *data)
202 /* Get X & Y positions */
203 x = le16_to_cpu(*(__le16 *) &data[2]);
204 y = le16_to_cpu(*(__le16 *) &data[4]);
206 /* Get current tool identifier */
207 if (data[1] & 0x90) { /* If pen is in the in/active area */
208 switch ((data[1] >> 5) & 3) {
214 tool = BTN_TOOL_RUBBER;
217 case 2: /* Mouse with wheel */
218 case 3: /* Mouse without wheel */
219 tool = BTN_TOOL_MOUSE;
223 /* Reset tool if out of active tablet area */
224 if (!(data[1] & 0x10))
228 /* If tool changed, notify input subsystem */
229 if (wdata->tool != tool) {
231 /* Completely reset old tool state */
232 if (wdata->tool == BTN_TOOL_MOUSE) {
233 input_report_key(input, BTN_LEFT, 0);
234 input_report_key(input, BTN_RIGHT, 0);
235 input_report_key(input, BTN_MIDDLE, 0);
236 input_report_abs(input, ABS_DISTANCE,
237 input_abs_get_max(input, ABS_DISTANCE));
239 input_report_key(input, BTN_TOUCH, 0);
240 input_report_key(input, BTN_STYLUS, 0);
241 input_report_key(input, BTN_STYLUS2, 0);
242 input_report_abs(input, ABS_PRESSURE, 0);
244 input_report_key(input, wdata->tool, 0);
249 input_report_key(input, tool, 1);
253 input_report_abs(input, ABS_X, x);
254 input_report_abs(input, ABS_Y, y);
256 switch ((data[1] >> 5) & 3) {
257 case 2: /* Mouse with wheel */
258 input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
259 rw = (data[6] & 0x01) ? -1 :
260 (data[6] & 0x02) ? 1 : 0;
261 input_report_rel(input, REL_WHEEL, rw);
264 case 3: /* Mouse without wheel */
265 input_report_key(input, BTN_LEFT, data[1] & 0x01);
266 input_report_key(input, BTN_RIGHT, data[1] & 0x02);
267 /* Compute distance between mouse and tablet */
268 rw = 44 - (data[6] >> 2);
273 input_report_abs(input, ABS_DISTANCE, rw);
277 input_report_abs(input, ABS_PRESSURE,
278 data[6] | (((__u16) (data[1] & 0x08)) << 5));
279 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
280 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
281 input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
288 /* Report the state of the two buttons at the top of the tablet
289 * as two extra fingerpad keys (buttons 4 & 5). */
291 if (rw != wdata->butstate) {
292 wdata->butstate = rw;
293 input_report_key(input, BTN_0, rw & 0x02);
294 input_report_key(input, BTN_1, rw & 0x01);
295 input_report_key(input, BTN_TOOL_FINGER, 0xf0);
296 input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
300 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
301 /* Store current battery capacity */
302 rw = (data[7] >> 2 & 0x07);
303 if (rw != wdata->battery_capacity)
304 wdata->battery_capacity = rw;
309 static void wacom_i4_parse_pen_report(struct wacom_data *wdata,
310 struct input_dev *input, unsigned char *data)
312 __u16 x, y, pressure;
316 case 0x80: /* Out of proximity report */
318 input_report_key(input, BTN_TOUCH, 0);
319 input_report_abs(input, ABS_PRESSURE, 0);
320 input_report_key(input, wdata->tool, 0);
323 case 0xC2: /* Tool report */
324 id = ((data[2] << 4) | (data[3] >> 4) |
325 ((data[7] & 0x0f) << 20) |
326 ((data[8] & 0xf0) << 12)) & 0xfffff;
330 wdata->tool = BTN_TOOL_PEN;
333 wdata->tool = BTN_TOOL_RUBBER;
337 default: /* Position/pressure report */
338 x = data[2] << 9 | data[3] << 1 | ((data[9] & 0x02) >> 1);
339 y = data[4] << 9 | data[5] << 1 | (data[9] & 0x01);
340 pressure = (data[6] << 3) | ((data[7] & 0xC0) >> 5)
343 input_report_key(input, BTN_TOUCH, pressure > 1);
345 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
346 input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
347 input_report_key(input, wdata->tool, 1);
348 input_report_abs(input, ABS_X, x);
349 input_report_abs(input, ABS_Y, y);
350 input_report_abs(input, ABS_PRESSURE, pressure);
358 static void wacom_i4_parse_report(struct hid_device *hdev,
359 struct wacom_data *wdata,
360 struct input_dev *input, unsigned char *data)
363 case 0x00: /* Empty report */
365 case 0x02: /* Pen report */
366 wacom_i4_parse_pen_report(wdata, input, data);
368 case 0x03: /* Features Report */
369 wdata->features = data[2];
371 case 0x0C: /* Button report */
374 hid_err(hdev, "Unknown report: %d,%d\n", data[0], data[1]);
379 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
380 u8 *raw_data, int size)
382 struct wacom_data *wdata = hid_get_drvdata(hdev);
383 struct hid_input *hidinput;
384 struct input_dev *input;
385 unsigned char *data = (unsigned char *) raw_data;
388 if (!(hdev->claimed & HID_CLAIMED_INPUT))
391 hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
392 input = hidinput->input;
394 /* Check if this is a tablet report */
398 switch (hdev->product) {
399 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
400 return wacom_gr_parse_report(hdev, wdata, input, data);
402 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
407 wacom_i4_parse_report(hdev, wdata, input, data + i);
411 wacom_i4_parse_report(hdev, wdata, input, data + i);
413 wacom_i4_parse_report(hdev, wdata, input, data + i);
416 hid_err(hdev, "Unknown report: %d,%d size:%d\n",
417 data[0], data[1], size);
424 static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
425 struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
428 struct input_dev *input = hi->input;
430 __set_bit(INPUT_PROP_POINTER, input->propbit);
433 input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
435 __set_bit(REL_WHEEL, input->relbit);
437 __set_bit(BTN_TOOL_PEN, input->keybit);
438 __set_bit(BTN_TOUCH, input->keybit);
439 __set_bit(BTN_STYLUS, input->keybit);
440 __set_bit(BTN_STYLUS2, input->keybit);
441 __set_bit(BTN_LEFT, input->keybit);
442 __set_bit(BTN_RIGHT, input->keybit);
443 __set_bit(BTN_MIDDLE, input->keybit);
446 input->evbit[0] |= BIT(EV_MSC);
448 __set_bit(MSC_SERIAL, input->mscbit);
450 __set_bit(BTN_0, input->keybit);
451 __set_bit(BTN_1, input->keybit);
452 __set_bit(BTN_TOOL_FINGER, input->keybit);
454 /* Distance, rubber and mouse */
455 __set_bit(BTN_TOOL_RUBBER, input->keybit);
456 __set_bit(BTN_TOOL_MOUSE, input->keybit);
458 switch (hdev->product) {
459 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
460 input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
461 input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
462 input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
463 input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
465 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
466 input_set_abs_params(input, ABS_X, 0, 40640, 4, 0);
467 input_set_abs_params(input, ABS_Y, 0, 25400, 4, 0);
468 input_set_abs_params(input, ABS_PRESSURE, 0, 2047, 0, 0);
475 static int wacom_probe(struct hid_device *hdev,
476 const struct hid_device_id *id)
478 struct wacom_data *wdata;
481 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
483 hid_err(hdev, "can't alloc wacom descriptor\n");
487 hid_set_drvdata(hdev, wdata);
489 /* Parse the HID report now */
490 ret = hid_parse(hdev);
492 hid_err(hdev, "parse failed\n");
496 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
498 hid_err(hdev, "hw start failed\n");
502 ret = device_create_file(&hdev->dev, &dev_attr_speed);
505 "can't create sysfs speed attribute err: %d\n", ret);
507 switch (hdev->product) {
508 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
509 /* Set Wacom mode 2 with high reporting speed */
512 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
514 wacom_set_features(hdev);
518 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
519 wdata->battery.properties = wacom_battery_props;
520 wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
521 wdata->battery.get_property = wacom_battery_get_property;
522 wdata->battery.name = "wacom_battery";
523 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
524 wdata->battery.use_for_apm = 0;
526 ret = power_supply_register(&hdev->dev, &wdata->battery);
528 hid_warn(hdev, "can't create sysfs battery attribute, err: %d\n",
533 wdata->ac.properties = wacom_ac_props;
534 wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
535 wdata->ac.get_property = wacom_ac_get_property;
536 wdata->ac.name = "wacom_ac";
537 wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
538 wdata->ac.use_for_apm = 0;
540 ret = power_supply_register(&hdev->dev, &wdata->ac);
543 "can't create ac battery attribute, err: %d\n", ret);
549 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
551 power_supply_unregister(&wdata->battery);
553 device_remove_file(&hdev->dev, &dev_attr_speed);
561 static void wacom_remove(struct hid_device *hdev)
563 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
564 struct wacom_data *wdata = hid_get_drvdata(hdev);
566 device_remove_file(&hdev->dev, &dev_attr_speed);
569 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
570 power_supply_unregister(&wdata->battery);
571 power_supply_unregister(&wdata->ac);
573 kfree(hid_get_drvdata(hdev));
576 static const struct hid_device_id wacom_devices[] = {
577 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
578 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
582 MODULE_DEVICE_TABLE(hid, wacom_devices);
584 static struct hid_driver wacom_driver = {
586 .id_table = wacom_devices,
587 .probe = wacom_probe,
588 .remove = wacom_remove,
589 .raw_event = wacom_raw_event,
590 .input_mapped = wacom_input_mapped,
593 static int __init wacom_init(void)
597 ret = hid_register_driver(&wacom_driver);
599 pr_err("can't register wacom driver\n");
603 static void __exit wacom_exit(void)
605 hid_unregister_driver(&wacom_driver);
608 module_init(wacom_init);
609 module_exit(wacom_exit);
610 MODULE_LICENSE("GPL");