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>
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 2 of the License, or (at your option)
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/device.h>
24 #include <linux/hid.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
28 #include <linux/power_supply.h>
35 unsigned char butstate;
36 unsigned char high_speed;
37 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
39 struct power_supply battery;
40 struct power_supply ac;
44 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
45 /*percent of battery capacity, 0 means AC online*/
46 static unsigned short batcap[8] = { 1, 15, 25, 35, 50, 70, 100, 0 };
48 static enum power_supply_property wacom_battery_props[] = {
49 POWER_SUPPLY_PROP_PRESENT,
50 POWER_SUPPLY_PROP_CAPACITY,
51 POWER_SUPPLY_PROP_SCOPE,
54 static enum power_supply_property wacom_ac_props[] = {
55 POWER_SUPPLY_PROP_PRESENT,
56 POWER_SUPPLY_PROP_ONLINE,
57 POWER_SUPPLY_PROP_SCOPE,
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_SCOPE:
74 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
76 case POWER_SUPPLY_PROP_CAPACITY:
77 /* show 100% battery capacity when charging */
81 val->intval = power_state;
90 static int wacom_ac_get_property(struct power_supply *psy,
91 enum power_supply_property psp,
92 union power_supply_propval *val)
94 struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
95 int power_state = batcap[wdata->battery_capacity];
99 case POWER_SUPPLY_PROP_PRESENT:
101 case POWER_SUPPLY_PROP_ONLINE:
102 if (power_state == 0)
107 case POWER_SUPPLY_PROP_SCOPE:
108 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
118 static void wacom_poke(struct hid_device *hdev, u8 speed)
120 struct wacom_data *wdata = hid_get_drvdata(hdev);
124 rep_data[0] = 0x03 ; rep_data[1] = 0x00;
127 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
129 } while (ret < 0 && limit-- > 0);
140 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
142 } while (ret < 0 && limit-- > 0);
145 wdata->high_speed = speed;
151 * Note that if the raw queries fail, it's not a hard failure and it
152 * is safe to continue
154 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
159 static ssize_t wacom_show_speed(struct device *dev,
160 struct device_attribute
163 struct wacom_data *wdata = dev_get_drvdata(dev);
165 return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
168 static ssize_t wacom_store_speed(struct device *dev,
169 struct device_attribute *attr,
170 const char *buf, size_t count)
172 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
175 if (sscanf(buf, "%1d", &new_speed ) != 1)
178 if (new_speed == 0 || new_speed == 1) {
179 wacom_poke(hdev, new_speed);
180 return strnlen(buf, PAGE_SIZE);
185 static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,
186 wacom_show_speed, wacom_store_speed);
188 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
189 u8 *raw_data, int size)
191 struct wacom_data *wdata = hid_get_drvdata(hdev);
192 struct hid_input *hidinput;
193 struct input_dev *input;
194 unsigned char *data = (unsigned char *) raw_data;
197 if (!(hdev->claimed & HID_CLAIMED_INPUT))
201 hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
202 input = hidinput->input;
204 /* Check if this is a tablet report */
208 /* Get X & Y positions */
209 x = le16_to_cpu(*(__le16 *) &data[2]);
210 y = le16_to_cpu(*(__le16 *) &data[4]);
212 /* Get current tool identifier */
213 if (data[1] & 0x90) { /* If pen is in the in/active area */
214 switch ((data[1] >> 5) & 3) {
220 tool = BTN_TOOL_RUBBER;
223 case 2: /* Mouse with wheel */
224 case 3: /* Mouse without wheel */
225 tool = BTN_TOOL_MOUSE;
229 /* Reset tool if out of active tablet area */
230 if (!(data[1] & 0x10))
234 /* If tool changed, notify input subsystem */
235 if (wdata->tool != tool) {
237 /* Completely reset old tool state */
238 if (wdata->tool == BTN_TOOL_MOUSE) {
239 input_report_key(input, BTN_LEFT, 0);
240 input_report_key(input, BTN_RIGHT, 0);
241 input_report_key(input, BTN_MIDDLE, 0);
242 input_report_abs(input, ABS_DISTANCE,
243 input_abs_get_max(input, ABS_DISTANCE));
245 input_report_key(input, BTN_TOUCH, 0);
246 input_report_key(input, BTN_STYLUS, 0);
247 input_report_key(input, BTN_STYLUS2, 0);
248 input_report_abs(input, ABS_PRESSURE, 0);
250 input_report_key(input, wdata->tool, 0);
255 input_report_key(input, tool, 1);
259 input_report_abs(input, ABS_X, x);
260 input_report_abs(input, ABS_Y, y);
262 switch ((data[1] >> 5) & 3) {
263 case 2: /* Mouse with wheel */
264 input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
265 rw = (data[6] & 0x01) ? -1 :
266 (data[6] & 0x02) ? 1 : 0;
267 input_report_rel(input, REL_WHEEL, rw);
270 case 3: /* Mouse without wheel */
271 input_report_key(input, BTN_LEFT, data[1] & 0x01);
272 input_report_key(input, BTN_RIGHT, data[1] & 0x02);
273 /* Compute distance between mouse and tablet */
274 rw = 44 - (data[6] >> 2);
279 input_report_abs(input, ABS_DISTANCE, rw);
283 input_report_abs(input, ABS_PRESSURE,
284 data[6] | (((__u16) (data[1] & 0x08)) << 5));
285 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
286 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
287 input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
294 /* Report the state of the two buttons at the top of the tablet
295 * as two extra fingerpad keys (buttons 4 & 5). */
297 if (rw != wdata->butstate) {
298 wdata->butstate = rw;
299 input_report_key(input, BTN_0, rw & 0x02);
300 input_report_key(input, BTN_1, rw & 0x01);
301 input_report_key(input, BTN_TOOL_FINGER, 0xf0);
302 input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
306 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
307 /* Store current battery capacity */
308 rw = (data[7] >> 2 & 0x07);
309 if (rw != wdata->battery_capacity)
310 wdata->battery_capacity = rw;
315 static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
316 struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
319 struct input_dev *input = hi->input;
321 __set_bit(INPUT_PROP_POINTER, input->propbit);
324 input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
326 __set_bit(REL_WHEEL, input->relbit);
328 __set_bit(BTN_TOOL_PEN, input->keybit);
329 __set_bit(BTN_TOUCH, input->keybit);
330 __set_bit(BTN_STYLUS, input->keybit);
331 __set_bit(BTN_STYLUS2, input->keybit);
332 __set_bit(BTN_LEFT, input->keybit);
333 __set_bit(BTN_RIGHT, input->keybit);
334 __set_bit(BTN_MIDDLE, input->keybit);
337 input->evbit[0] |= BIT(EV_MSC);
339 __set_bit(MSC_SERIAL, input->mscbit);
341 __set_bit(BTN_0, input->keybit);
342 __set_bit(BTN_1, input->keybit);
343 __set_bit(BTN_TOOL_FINGER, input->keybit);
345 /* Distance, rubber and mouse */
346 __set_bit(BTN_TOOL_RUBBER, input->keybit);
347 __set_bit(BTN_TOOL_MOUSE, input->keybit);
349 input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
350 input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
351 input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
352 input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
357 static int wacom_probe(struct hid_device *hdev,
358 const struct hid_device_id *id)
360 struct wacom_data *wdata;
363 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
365 hid_err(hdev, "can't alloc wacom descriptor\n");
369 hid_set_drvdata(hdev, wdata);
371 /* Parse the HID report now */
372 ret = hid_parse(hdev);
374 hid_err(hdev, "parse failed\n");
378 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
380 hid_err(hdev, "hw start failed\n");
384 ret = device_create_file(&hdev->dev, &dev_attr_speed);
387 "can't create sysfs speed attribute err: %d\n", ret);
389 /* Set Wacom mode 2 with high reporting speed */
392 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
393 wdata->battery.properties = wacom_battery_props;
394 wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
395 wdata->battery.get_property = wacom_battery_get_property;
396 wdata->battery.name = "wacom_battery";
397 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
398 wdata->battery.use_for_apm = 0;
400 power_supply_powers(&wdata->battery, &hdev->dev);
402 ret = power_supply_register(&hdev->dev, &wdata->battery);
404 hid_warn(hdev, "can't create sysfs battery attribute, err: %d\n",
409 wdata->ac.properties = wacom_ac_props;
410 wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
411 wdata->ac.get_property = wacom_ac_get_property;
412 wdata->ac.name = "wacom_ac";
413 wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
414 wdata->ac.use_for_apm = 0;
416 power_supply_powers(&wdata->battery, &hdev->dev);
418 ret = power_supply_register(&hdev->dev, &wdata->ac);
421 "can't create ac battery attribute, err: %d\n", ret);
427 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
429 power_supply_unregister(&wdata->battery);
431 device_remove_file(&hdev->dev, &dev_attr_speed);
439 static void wacom_remove(struct hid_device *hdev)
441 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
442 struct wacom_data *wdata = hid_get_drvdata(hdev);
444 device_remove_file(&hdev->dev, &dev_attr_speed);
447 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
448 power_supply_unregister(&wdata->battery);
449 power_supply_unregister(&wdata->ac);
451 kfree(hid_get_drvdata(hdev));
454 static const struct hid_device_id wacom_devices[] = {
455 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
459 MODULE_DEVICE_TABLE(hid, wacom_devices);
461 static struct hid_driver wacom_driver = {
463 .id_table = wacom_devices,
464 .probe = wacom_probe,
465 .remove = wacom_remove,
466 .raw_event = wacom_raw_event,
467 .input_mapped = wacom_input_mapped,
470 static int __init wacom_init(void)
474 ret = hid_register_driver(&wacom_driver);
476 pr_err("can't register wacom driver\n");
480 static void __exit wacom_exit(void)
482 hid_unregister_driver(&wacom_driver);
485 module_init(wacom_init);
486 module_exit(wacom_exit);
487 MODULE_LICENSE("GPL");