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,
53 POWER_SUPPLY_PROP_SCOPE,
56 static enum power_supply_property wacom_ac_props[] = {
57 POWER_SUPPLY_PROP_PRESENT,
58 POWER_SUPPLY_PROP_ONLINE,
59 POWER_SUPPLY_PROP_SCOPE,
62 static int wacom_battery_get_property(struct power_supply *psy,
63 enum power_supply_property psp,
64 union power_supply_propval *val)
66 struct wacom_data *wdata = container_of(psy,
67 struct wacom_data, battery);
68 int power_state = batcap[wdata->battery_capacity];
72 case POWER_SUPPLY_PROP_PRESENT:
75 case POWER_SUPPLY_PROP_SCOPE:
76 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
78 case POWER_SUPPLY_PROP_CAPACITY:
79 /* show 100% battery capacity when charging */
83 val->intval = power_state;
92 static int wacom_ac_get_property(struct power_supply *psy,
93 enum power_supply_property psp,
94 union power_supply_propval *val)
96 struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
97 int power_state = batcap[wdata->battery_capacity];
101 case POWER_SUPPLY_PROP_PRESENT:
103 case POWER_SUPPLY_PROP_ONLINE:
104 if (power_state == 0)
109 case POWER_SUPPLY_PROP_SCOPE:
110 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
120 static void wacom_set_features(struct hid_device *hdev)
125 /*set high speed, tablet mode*/
128 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
133 static void wacom_poke(struct hid_device *hdev, u8 speed)
135 struct wacom_data *wdata = hid_get_drvdata(hdev);
139 rep_data[0] = 0x03 ; rep_data[1] = 0x00;
142 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
144 } while (ret < 0 && limit-- > 0);
155 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
157 } while (ret < 0 && limit-- > 0);
160 wdata->high_speed = speed;
166 * Note that if the raw queries fail, it's not a hard failure and it
167 * is safe to continue
169 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
174 static ssize_t wacom_show_speed(struct device *dev,
175 struct device_attribute
178 struct wacom_data *wdata = dev_get_drvdata(dev);
180 return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
183 static ssize_t wacom_store_speed(struct device *dev,
184 struct device_attribute *attr,
185 const char *buf, size_t count)
187 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
190 if (sscanf(buf, "%1d", &new_speed ) != 1)
193 if (new_speed == 0 || new_speed == 1) {
194 wacom_poke(hdev, new_speed);
195 return strnlen(buf, PAGE_SIZE);
200 static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,
201 wacom_show_speed, wacom_store_speed);
203 static int wacom_gr_parse_report(struct hid_device *hdev,
204 struct wacom_data *wdata,
205 struct input_dev *input, unsigned char *data)
210 /* Get X & Y positions */
211 x = le16_to_cpu(*(__le16 *) &data[2]);
212 y = le16_to_cpu(*(__le16 *) &data[4]);
214 /* Get current tool identifier */
215 if (data[1] & 0x90) { /* If pen is in the in/active area */
216 switch ((data[1] >> 5) & 3) {
222 tool = BTN_TOOL_RUBBER;
225 case 2: /* Mouse with wheel */
226 case 3: /* Mouse without wheel */
227 tool = BTN_TOOL_MOUSE;
231 /* Reset tool if out of active tablet area */
232 if (!(data[1] & 0x10))
236 /* If tool changed, notify input subsystem */
237 if (wdata->tool != tool) {
239 /* Completely reset old tool state */
240 if (wdata->tool == BTN_TOOL_MOUSE) {
241 input_report_key(input, BTN_LEFT, 0);
242 input_report_key(input, BTN_RIGHT, 0);
243 input_report_key(input, BTN_MIDDLE, 0);
244 input_report_abs(input, ABS_DISTANCE,
245 input_abs_get_max(input, ABS_DISTANCE));
247 input_report_key(input, BTN_TOUCH, 0);
248 input_report_key(input, BTN_STYLUS, 0);
249 input_report_key(input, BTN_STYLUS2, 0);
250 input_report_abs(input, ABS_PRESSURE, 0);
252 input_report_key(input, wdata->tool, 0);
257 input_report_key(input, tool, 1);
261 input_report_abs(input, ABS_X, x);
262 input_report_abs(input, ABS_Y, y);
264 switch ((data[1] >> 5) & 3) {
265 case 2: /* Mouse with wheel */
266 input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
267 rw = (data[6] & 0x01) ? -1 :
268 (data[6] & 0x02) ? 1 : 0;
269 input_report_rel(input, REL_WHEEL, rw);
272 case 3: /* Mouse without wheel */
273 input_report_key(input, BTN_LEFT, data[1] & 0x01);
274 input_report_key(input, BTN_RIGHT, data[1] & 0x02);
275 /* Compute distance between mouse and tablet */
276 rw = 44 - (data[6] >> 2);
281 input_report_abs(input, ABS_DISTANCE, rw);
285 input_report_abs(input, ABS_PRESSURE,
286 data[6] | (((__u16) (data[1] & 0x08)) << 5));
287 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
288 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
289 input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
296 /* Report the state of the two buttons at the top of the tablet
297 * as two extra fingerpad keys (buttons 4 & 5). */
299 if (rw != wdata->butstate) {
300 wdata->butstate = rw;
301 input_report_key(input, BTN_0, rw & 0x02);
302 input_report_key(input, BTN_1, rw & 0x01);
303 input_report_key(input, BTN_TOOL_FINGER, 0xf0);
304 input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
308 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
309 /* Store current battery capacity */
310 rw = (data[7] >> 2 & 0x07);
311 if (rw != wdata->battery_capacity)
312 wdata->battery_capacity = rw;
317 static void wacom_i4_parse_pen_report(struct wacom_data *wdata,
318 struct input_dev *input, unsigned char *data)
320 __u16 x, y, pressure;
324 case 0x80: /* Out of proximity report */
326 input_report_key(input, BTN_TOUCH, 0);
327 input_report_abs(input, ABS_PRESSURE, 0);
328 input_report_key(input, wdata->tool, 0);
331 case 0xC2: /* Tool report */
332 id = ((data[2] << 4) | (data[3] >> 4) |
333 ((data[7] & 0x0f) << 20) |
334 ((data[8] & 0xf0) << 12)) & 0xfffff;
338 wdata->tool = BTN_TOOL_PEN;
341 wdata->tool = BTN_TOOL_RUBBER;
345 default: /* Position/pressure report */
346 x = data[2] << 9 | data[3] << 1 | ((data[9] & 0x02) >> 1);
347 y = data[4] << 9 | data[5] << 1 | (data[9] & 0x01);
348 pressure = (data[6] << 3) | ((data[7] & 0xC0) >> 5)
351 input_report_key(input, BTN_TOUCH, pressure > 1);
353 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
354 input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
355 input_report_key(input, wdata->tool, 1);
356 input_report_abs(input, ABS_X, x);
357 input_report_abs(input, ABS_Y, y);
358 input_report_abs(input, ABS_PRESSURE, pressure);
366 static void wacom_i4_parse_report(struct hid_device *hdev,
367 struct wacom_data *wdata,
368 struct input_dev *input, unsigned char *data)
371 case 0x00: /* Empty report */
373 case 0x02: /* Pen report */
374 wacom_i4_parse_pen_report(wdata, input, data);
376 case 0x03: /* Features Report */
377 wdata->features = data[2];
379 case 0x0C: /* Button report */
382 hid_err(hdev, "Unknown report: %d,%d\n", data[0], data[1]);
387 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
388 u8 *raw_data, int size)
390 struct wacom_data *wdata = hid_get_drvdata(hdev);
391 struct hid_input *hidinput;
392 struct input_dev *input;
393 unsigned char *data = (unsigned char *) raw_data;
396 if (!(hdev->claimed & HID_CLAIMED_INPUT))
399 hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
400 input = hidinput->input;
402 /* Check if this is a tablet report */
406 switch (hdev->product) {
407 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
408 return wacom_gr_parse_report(hdev, wdata, input, data);
410 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
415 wacom_i4_parse_report(hdev, wdata, input, data + i);
419 wacom_i4_parse_report(hdev, wdata, input, data + i);
421 wacom_i4_parse_report(hdev, wdata, input, data + i);
424 hid_err(hdev, "Unknown report: %d,%d size:%d\n",
425 data[0], data[1], size);
432 static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
433 struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
436 struct input_dev *input = hi->input;
438 __set_bit(INPUT_PROP_POINTER, input->propbit);
441 input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
443 __set_bit(REL_WHEEL, input->relbit);
445 __set_bit(BTN_TOOL_PEN, input->keybit);
446 __set_bit(BTN_TOUCH, input->keybit);
447 __set_bit(BTN_STYLUS, input->keybit);
448 __set_bit(BTN_STYLUS2, input->keybit);
449 __set_bit(BTN_LEFT, input->keybit);
450 __set_bit(BTN_RIGHT, input->keybit);
451 __set_bit(BTN_MIDDLE, input->keybit);
454 input->evbit[0] |= BIT(EV_MSC);
456 __set_bit(MSC_SERIAL, input->mscbit);
458 __set_bit(BTN_0, input->keybit);
459 __set_bit(BTN_1, input->keybit);
460 __set_bit(BTN_TOOL_FINGER, input->keybit);
462 /* Distance, rubber and mouse */
463 __set_bit(BTN_TOOL_RUBBER, input->keybit);
464 __set_bit(BTN_TOOL_MOUSE, input->keybit);
466 switch (hdev->product) {
467 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
468 input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
469 input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
470 input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
471 input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
473 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
474 input_set_abs_params(input, ABS_X, 0, 40640, 4, 0);
475 input_set_abs_params(input, ABS_Y, 0, 25400, 4, 0);
476 input_set_abs_params(input, ABS_PRESSURE, 0, 2047, 0, 0);
483 static int wacom_probe(struct hid_device *hdev,
484 const struct hid_device_id *id)
486 struct wacom_data *wdata;
489 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
491 hid_err(hdev, "can't alloc wacom descriptor\n");
495 hid_set_drvdata(hdev, wdata);
497 /* Parse the HID report now */
498 ret = hid_parse(hdev);
500 hid_err(hdev, "parse failed\n");
504 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
506 hid_err(hdev, "hw start failed\n");
510 ret = device_create_file(&hdev->dev, &dev_attr_speed);
513 "can't create sysfs speed attribute err: %d\n", ret);
515 switch (hdev->product) {
516 case USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH:
517 /* Set Wacom mode 2 with high reporting speed */
520 case USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH:
522 wacom_set_features(hdev);
526 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
527 wdata->battery.properties = wacom_battery_props;
528 wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
529 wdata->battery.get_property = wacom_battery_get_property;
530 wdata->battery.name = "wacom_battery";
531 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
532 wdata->battery.use_for_apm = 0;
535 ret = power_supply_register(&hdev->dev, &wdata->battery);
537 hid_warn(hdev, "can't create sysfs battery attribute, err: %d\n",
542 power_supply_powers(&wdata->battery, &hdev->dev);
544 wdata->ac.properties = wacom_ac_props;
545 wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
546 wdata->ac.get_property = wacom_ac_get_property;
547 wdata->ac.name = "wacom_ac";
548 wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
549 wdata->ac.use_for_apm = 0;
551 ret = power_supply_register(&hdev->dev, &wdata->ac);
554 "can't create ac battery attribute, err: %d\n", ret);
558 power_supply_powers(&wdata->ac, &hdev->dev);
562 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
564 power_supply_unregister(&wdata->battery);
566 device_remove_file(&hdev->dev, &dev_attr_speed);
574 static void wacom_remove(struct hid_device *hdev)
576 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
577 struct wacom_data *wdata = hid_get_drvdata(hdev);
579 device_remove_file(&hdev->dev, &dev_attr_speed);
582 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
583 power_supply_unregister(&wdata->battery);
584 power_supply_unregister(&wdata->ac);
586 kfree(hid_get_drvdata(hdev));
589 static const struct hid_device_id wacom_devices[] = {
590 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
591 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
595 MODULE_DEVICE_TABLE(hid, wacom_devices);
597 static struct hid_driver wacom_driver = {
599 .id_table = wacom_devices,
600 .probe = wacom_probe,
601 .remove = wacom_remove,
602 .raw_event = wacom_raw_event,
603 .input_mapped = wacom_input_mapped,
606 static int __init wacom_init(void)
610 ret = hid_register_driver(&wacom_driver);
612 pr_err("can't register wacom driver\n");
616 static void __exit wacom_exit(void)
618 hid_unregister_driver(&wacom_driver);
621 module_init(wacom_init);
622 module_exit(wacom_exit);
623 MODULE_LICENSE("GPL");