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 #include <linux/device.h>
22 #include <linux/hid.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
26 #include <linux/power_supply.h>
33 unsigned char butstate
;
34 unsigned char high_speed
;
35 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
37 struct power_supply battery
;
38 struct power_supply ac
;
42 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
43 /*percent of battery capacity, 0 means AC online*/
44 static unsigned short batcap
[8] = { 1, 15, 25, 35, 50, 70, 100, 0 };
46 static enum power_supply_property wacom_battery_props
[] = {
47 POWER_SUPPLY_PROP_PRESENT
,
48 POWER_SUPPLY_PROP_CAPACITY
51 static enum power_supply_property wacom_ac_props
[] = {
52 POWER_SUPPLY_PROP_PRESENT
,
53 POWER_SUPPLY_PROP_ONLINE
56 static int wacom_battery_get_property(struct power_supply
*psy
,
57 enum power_supply_property psp
,
58 union power_supply_propval
*val
)
60 struct wacom_data
*wdata
= container_of(psy
,
61 struct wacom_data
, battery
);
62 int power_state
= batcap
[wdata
->battery_capacity
];
66 case POWER_SUPPLY_PROP_PRESENT
:
69 case POWER_SUPPLY_PROP_CAPACITY
:
70 /* show 100% battery capacity when charging */
74 val
->intval
= power_state
;
83 static int wacom_ac_get_property(struct power_supply
*psy
,
84 enum power_supply_property psp
,
85 union power_supply_propval
*val
)
87 struct wacom_data
*wdata
= container_of(psy
, struct wacom_data
, ac
);
88 int power_state
= batcap
[wdata
->battery_capacity
];
92 case POWER_SUPPLY_PROP_PRESENT
:
94 case POWER_SUPPLY_PROP_ONLINE
:
108 static void wacom_poke(struct hid_device
*hdev
, u8 speed
)
110 struct wacom_data
*wdata
= hid_get_drvdata(hdev
);
114 rep_data
[0] = 0x03 ; rep_data
[1] = 0x00;
117 ret
= hdev
->hid_output_raw_report(hdev
, rep_data
, 2,
119 } while (ret
< 0 && limit
-- > 0);
130 ret
= hdev
->hid_output_raw_report(hdev
, rep_data
, 2,
132 } while (ret
< 0 && limit
-- > 0);
135 wdata
->high_speed
= speed
;
141 * Note that if the raw queries fail, it's not a hard failure and it
142 * is safe to continue
144 dev_warn(&hdev
->dev
, "failed to poke device, command %d, err %d\n",
149 static ssize_t
wacom_show_speed(struct device
*dev
,
150 struct device_attribute
153 struct wacom_data
*wdata
= dev_get_drvdata(dev
);
155 return snprintf(buf
, PAGE_SIZE
, "%i\n", wdata
->high_speed
);
158 static ssize_t
wacom_store_speed(struct device
*dev
,
159 struct device_attribute
*attr
,
160 const char *buf
, size_t count
)
162 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
165 if (sscanf(buf
, "%1d", &new_speed
) != 1)
168 if (new_speed
== 0 || new_speed
== 1) {
169 wacom_poke(hdev
, new_speed
);
170 return strnlen(buf
, PAGE_SIZE
);
175 static DEVICE_ATTR(speed
, S_IRUGO
| S_IWUGO
,
176 wacom_show_speed
, wacom_store_speed
);
178 static int wacom_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
179 u8
*raw_data
, int size
)
181 struct wacom_data
*wdata
= hid_get_drvdata(hdev
);
182 struct hid_input
*hidinput
;
183 struct input_dev
*input
;
184 unsigned char *data
= (unsigned char *) raw_data
;
187 if (!(hdev
->claimed
& HID_CLAIMED_INPUT
))
191 hidinput
= list_entry(hdev
->inputs
.next
, struct hid_input
, list
);
192 input
= hidinput
->input
;
194 /* Check if this is a tablet report */
198 /* Get X & Y positions */
199 x
= le16_to_cpu(*(__le16
*) &data
[2]);
200 y
= le16_to_cpu(*(__le16
*) &data
[4]);
202 /* Get current tool identifier */
203 if (data
[1] & 0x90) { /* If pen is in the in/active area */
204 switch ((data
[1] >> 5) & 3) {
210 tool
= BTN_TOOL_RUBBER
;
213 case 2: /* Mouse with wheel */
214 case 3: /* Mouse without wheel */
215 tool
= BTN_TOOL_MOUSE
;
219 /* Reset tool if out of active tablet area */
220 if (!(data
[1] & 0x10))
224 /* If tool changed, notify input subsystem */
225 if (wdata
->tool
!= tool
) {
227 /* Completely reset old tool state */
228 if (wdata
->tool
== BTN_TOOL_MOUSE
) {
229 input_report_key(input
, BTN_LEFT
, 0);
230 input_report_key(input
, BTN_RIGHT
, 0);
231 input_report_key(input
, BTN_MIDDLE
, 0);
232 input_report_abs(input
, ABS_DISTANCE
,
233 input_abs_get_max(input
, ABS_DISTANCE
));
235 input_report_key(input
, BTN_TOUCH
, 0);
236 input_report_key(input
, BTN_STYLUS
, 0);
237 input_report_key(input
, BTN_STYLUS2
, 0);
238 input_report_abs(input
, ABS_PRESSURE
, 0);
240 input_report_key(input
, wdata
->tool
, 0);
245 input_report_key(input
, tool
, 1);
249 input_report_abs(input
, ABS_X
, x
);
250 input_report_abs(input
, ABS_Y
, y
);
252 switch ((data
[1] >> 5) & 3) {
253 case 2: /* Mouse with wheel */
254 input_report_key(input
, BTN_MIDDLE
, data
[1] & 0x04);
255 rw
= (data
[6] & 0x01) ? -1 :
256 (data
[6] & 0x02) ? 1 : 0;
257 input_report_rel(input
, REL_WHEEL
, rw
);
260 case 3: /* Mouse without wheel */
261 input_report_key(input
, BTN_LEFT
, data
[1] & 0x01);
262 input_report_key(input
, BTN_RIGHT
, data
[1] & 0x02);
263 /* Compute distance between mouse and tablet */
264 rw
= 44 - (data
[6] >> 2);
269 input_report_abs(input
, ABS_DISTANCE
, rw
);
273 input_report_abs(input
, ABS_PRESSURE
,
274 data
[6] | (((__u16
) (data
[1] & 0x08)) << 5));
275 input_report_key(input
, BTN_TOUCH
, data
[1] & 0x01);
276 input_report_key(input
, BTN_STYLUS
, data
[1] & 0x02);
277 input_report_key(input
, BTN_STYLUS2
, (tool
== BTN_TOOL_PEN
) && data
[1] & 0x04);
284 /* Report the state of the two buttons at the top of the tablet
285 * as two extra fingerpad keys (buttons 4 & 5). */
287 if (rw
!= wdata
->butstate
) {
288 wdata
->butstate
= rw
;
289 input_report_key(input
, BTN_0
, rw
& 0x02);
290 input_report_key(input
, BTN_1
, rw
& 0x01);
291 input_report_key(input
, BTN_TOOL_FINGER
, 0xf0);
292 input_event(input
, EV_MSC
, MSC_SERIAL
, 0xf0);
296 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
297 /* Store current battery capacity */
298 rw
= (data
[7] >> 2 & 0x07);
299 if (rw
!= wdata
->battery_capacity
)
300 wdata
->battery_capacity
= rw
;
305 static int wacom_probe(struct hid_device
*hdev
,
306 const struct hid_device_id
*id
)
308 struct hid_input
*hidinput
;
309 struct input_dev
*input
;
310 struct wacom_data
*wdata
;
313 wdata
= kzalloc(sizeof(*wdata
), GFP_KERNEL
);
315 dev_err(&hdev
->dev
, "can't alloc wacom descriptor\n");
319 hid_set_drvdata(hdev
, wdata
);
321 /* Parse the HID report now */
322 ret
= hid_parse(hdev
);
324 dev_err(&hdev
->dev
, "parse failed\n");
328 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
330 dev_err(&hdev
->dev
, "hw start failed\n");
334 ret
= device_create_file(&hdev
->dev
, &dev_attr_speed
);
337 "can't create sysfs speed attribute err: %d\n", ret
);
339 /* Set Wacom mode 2 with high reporting speed */
342 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
343 wdata
->battery
.properties
= wacom_battery_props
;
344 wdata
->battery
.num_properties
= ARRAY_SIZE(wacom_battery_props
);
345 wdata
->battery
.get_property
= wacom_battery_get_property
;
346 wdata
->battery
.name
= "wacom_battery";
347 wdata
->battery
.type
= POWER_SUPPLY_TYPE_BATTERY
;
348 wdata
->battery
.use_for_apm
= 0;
350 ret
= power_supply_register(&hdev
->dev
, &wdata
->battery
);
353 "can't create sysfs battery attribute, err: %d\n", ret
);
355 * battery attribute is not critical for the tablet, but if it
356 * failed then there is no need to create ac attribute
361 wdata
->ac
.properties
= wacom_ac_props
;
362 wdata
->ac
.num_properties
= ARRAY_SIZE(wacom_ac_props
);
363 wdata
->ac
.get_property
= wacom_ac_get_property
;
364 wdata
->ac
.name
= "wacom_ac";
365 wdata
->ac
.type
= POWER_SUPPLY_TYPE_MAINS
;
366 wdata
->ac
.use_for_apm
= 0;
368 ret
= power_supply_register(&hdev
->dev
, &wdata
->ac
);
371 "can't create ac battery attribute, err: %d\n", ret
);
373 * ac attribute is not critical for the tablet, but if it
374 * failed then we don't want to battery attribute to exist
376 power_supply_unregister(&wdata
->battery
);
381 hidinput
= list_entry(hdev
->inputs
.next
, struct hid_input
, list
);
382 input
= hidinput
->input
;
385 input
->evbit
[0] |= BIT(EV_KEY
) | BIT(EV_ABS
) | BIT(EV_REL
);
387 __set_bit(REL_WHEEL
, input
->relbit
);
389 __set_bit(BTN_TOOL_PEN
, input
->keybit
);
390 __set_bit(BTN_TOUCH
, input
->keybit
);
391 __set_bit(BTN_STYLUS
, input
->keybit
);
392 __set_bit(BTN_STYLUS2
, input
->keybit
);
393 __set_bit(BTN_LEFT
, input
->keybit
);
394 __set_bit(BTN_RIGHT
, input
->keybit
);
395 __set_bit(BTN_MIDDLE
, input
->keybit
);
398 input
->evbit
[0] |= BIT(EV_MSC
);
400 __set_bit(MSC_SERIAL
, input
->mscbit
);
402 __set_bit(BTN_0
, input
->keybit
);
403 __set_bit(BTN_1
, input
->keybit
);
404 __set_bit(BTN_TOOL_FINGER
, input
->keybit
);
406 /* Distance, rubber and mouse */
407 __set_bit(BTN_TOOL_RUBBER
, input
->keybit
);
408 __set_bit(BTN_TOOL_MOUSE
, input
->keybit
);
410 input_set_abs_params(input
, ABS_X
, 0, 16704, 4, 0);
411 input_set_abs_params(input
, ABS_Y
, 0, 12064, 4, 0);
412 input_set_abs_params(input
, ABS_PRESSURE
, 0, 511, 0, 0);
413 input_set_abs_params(input
, ABS_DISTANCE
, 0, 32, 0, 0);
422 static void wacom_remove(struct hid_device
*hdev
)
424 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
425 struct wacom_data
*wdata
= hid_get_drvdata(hdev
);
429 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
430 power_supply_unregister(&wdata
->battery
);
431 power_supply_unregister(&wdata
->ac
);
433 kfree(hid_get_drvdata(hdev
));
436 static const struct hid_device_id wacom_devices
[] = {
437 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM
, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH
) },
441 MODULE_DEVICE_TABLE(hid
, wacom_devices
);
443 static struct hid_driver wacom_driver
= {
445 .id_table
= wacom_devices
,
446 .probe
= wacom_probe
,
447 .remove
= wacom_remove
,
448 .raw_event
= wacom_raw_event
,
451 static int __init
wacom_init(void)
455 ret
= hid_register_driver(&wacom_driver
);
457 printk(KERN_ERR
"can't register wacom driver\n");
461 static void __exit
wacom_exit(void)
463 hid_unregister_driver(&wacom_driver
);
466 module_init(wacom_init
);
467 module_exit(wacom_exit
);
468 MODULE_LICENSE("GPL");