blob: db2322310486eeea925ff158eada0b8d50371e74 [file] [log] [blame]
Bastien Noceraca2dcd42009-05-11 17:18:12 +02001/*
2 * Bluetooth Wacom Tablet support
3 *
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 */
13
14/*
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)
18 * any later version.
19 */
20
Joe Perches4291ee32010-12-09 19:29:03 -080021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Bastien Noceraca2dcd42009-05-11 17:18:12 +020023#include <linux/device.h>
24#include <linux/hid.h>
25#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Przemo Firszt59d23342010-03-15 19:16:23 +000027#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
28#include <linux/power_supply.h>
29#endif
Bastien Noceraca2dcd42009-05-11 17:18:12 +020030
31#include "hid-ids.h"
32
33struct wacom_data {
34 __u16 tool;
35 unsigned char butstate;
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +000036 unsigned char high_speed;
Przemo Firszt59d23342010-03-15 19:16:23 +000037#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
38 int battery_capacity;
39 struct power_supply battery;
40 struct power_supply ac;
41#endif
Bastien Noceraca2dcd42009-05-11 17:18:12 +020042};
43
Przemo Firszt59d23342010-03-15 19:16:23 +000044#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
45/*percent of battery capacity, 0 means AC online*/
46static unsigned short batcap[8] = { 1, 15, 25, 35, 50, 70, 100, 0 };
47
48static enum power_supply_property wacom_battery_props[] = {
49 POWER_SUPPLY_PROP_PRESENT,
50 POWER_SUPPLY_PROP_CAPACITY
51};
52
53static enum power_supply_property wacom_ac_props[] = {
54 POWER_SUPPLY_PROP_PRESENT,
55 POWER_SUPPLY_PROP_ONLINE
56};
57
58static int wacom_battery_get_property(struct power_supply *psy,
59 enum power_supply_property psp,
60 union power_supply_propval *val)
61{
62 struct wacom_data *wdata = container_of(psy,
63 struct wacom_data, battery);
64 int power_state = batcap[wdata->battery_capacity];
65 int ret = 0;
66
67 switch (psp) {
68 case POWER_SUPPLY_PROP_PRESENT:
69 val->intval = 1;
70 break;
71 case POWER_SUPPLY_PROP_CAPACITY:
72 /* show 100% battery capacity when charging */
73 if (power_state == 0)
74 val->intval = 100;
75 else
76 val->intval = power_state;
77 break;
78 default:
79 ret = -EINVAL;
80 break;
81 }
82 return ret;
83}
84
85static int wacom_ac_get_property(struct power_supply *psy,
86 enum power_supply_property psp,
87 union power_supply_propval *val)
88{
89 struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
90 int power_state = batcap[wdata->battery_capacity];
91 int ret = 0;
92
93 switch (psp) {
94 case POWER_SUPPLY_PROP_PRESENT:
95 /* fall through */
96 case POWER_SUPPLY_PROP_ONLINE:
97 if (power_state == 0)
98 val->intval = 1;
99 else
100 val->intval = 0;
101 break;
102 default:
103 ret = -EINVAL;
104 break;
105 }
106 return ret;
107}
108#endif
109
Przemo Firszt06c7c312010-03-22 09:55:04 +0100110static void wacom_poke(struct hid_device *hdev, u8 speed)
111{
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000112 struct wacom_data *wdata = hid_get_drvdata(hdev);
Przemo Firszt06c7c312010-03-22 09:55:04 +0100113 int limit, ret;
114 char rep_data[2];
115
116 rep_data[0] = 0x03 ; rep_data[1] = 0x00;
117 limit = 3;
118 do {
119 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
120 HID_FEATURE_REPORT);
121 } while (ret < 0 && limit-- > 0);
122
123 if (ret >= 0) {
124 if (speed == 0)
125 rep_data[0] = 0x05;
126 else
127 rep_data[0] = 0x06;
128
129 rep_data[1] = 0x00;
130 limit = 3;
131 do {
132 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
133 HID_FEATURE_REPORT);
134 } while (ret < 0 && limit-- > 0);
135
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000136 if (ret >= 0) {
137 wdata->high_speed = speed;
Przemo Firszt06c7c312010-03-22 09:55:04 +0100138 return;
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000139 }
Przemo Firszt06c7c312010-03-22 09:55:04 +0100140 }
141
142 /*
143 * Note that if the raw queries fail, it's not a hard failure and it
144 * is safe to continue
145 */
Joe Perches4291ee32010-12-09 19:29:03 -0800146 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
147 rep_data[0], ret);
Przemo Firszt06c7c312010-03-22 09:55:04 +0100148 return;
149}
150
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000151static ssize_t wacom_show_speed(struct device *dev,
152 struct device_attribute
153 *attr, char *buf)
154{
155 struct wacom_data *wdata = dev_get_drvdata(dev);
156
157 return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
158}
159
160static ssize_t wacom_store_speed(struct device *dev,
161 struct device_attribute *attr,
162 const char *buf, size_t count)
163{
164 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
165 int new_speed;
166
167 if (sscanf(buf, "%1d", &new_speed ) != 1)
168 return -EINVAL;
169
170 if (new_speed == 0 || new_speed == 1) {
171 wacom_poke(hdev, new_speed);
172 return strnlen(buf, PAGE_SIZE);
173 } else
174 return -EINVAL;
175}
176
Jiri Kosinaedd21262010-11-18 16:28:43 +0100177static DEVICE_ATTR(speed, S_IRUGO | S_IWUSR | S_IWGRP,
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000178 wacom_show_speed, wacom_store_speed);
179
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000180static int wacom_gr_parse_report(struct hid_device *hdev,
181 struct wacom_data *wdata,
182 struct input_dev *input, unsigned char *data)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200183{
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200184 int tool, x, y, rw;
185
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200186 tool = 0;
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200187 /* Get X & Y positions */
188 x = le16_to_cpu(*(__le16 *) &data[2]);
189 y = le16_to_cpu(*(__le16 *) &data[4]);
190
191 /* Get current tool identifier */
192 if (data[1] & 0x90) { /* If pen is in the in/active area */
193 switch ((data[1] >> 5) & 3) {
194 case 0: /* Pen */
195 tool = BTN_TOOL_PEN;
196 break;
197
198 case 1: /* Rubber */
199 tool = BTN_TOOL_RUBBER;
200 break;
201
202 case 2: /* Mouse with wheel */
203 case 3: /* Mouse without wheel */
204 tool = BTN_TOOL_MOUSE;
205 break;
206 }
207
208 /* Reset tool if out of active tablet area */
209 if (!(data[1] & 0x10))
210 tool = 0;
211 }
212
213 /* If tool changed, notify input subsystem */
214 if (wdata->tool != tool) {
215 if (wdata->tool) {
216 /* Completely reset old tool state */
217 if (wdata->tool == BTN_TOOL_MOUSE) {
218 input_report_key(input, BTN_LEFT, 0);
219 input_report_key(input, BTN_RIGHT, 0);
220 input_report_key(input, BTN_MIDDLE, 0);
221 input_report_abs(input, ABS_DISTANCE,
Daniel Mack987a6c02010-08-02 20:15:17 -0700222 input_abs_get_max(input, ABS_DISTANCE));
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200223 } else {
224 input_report_key(input, BTN_TOUCH, 0);
225 input_report_key(input, BTN_STYLUS, 0);
226 input_report_key(input, BTN_STYLUS2, 0);
227 input_report_abs(input, ABS_PRESSURE, 0);
228 }
229 input_report_key(input, wdata->tool, 0);
230 input_sync(input);
231 }
232 wdata->tool = tool;
233 if (tool)
234 input_report_key(input, tool, 1);
235 }
236
237 if (tool) {
238 input_report_abs(input, ABS_X, x);
239 input_report_abs(input, ABS_Y, y);
240
241 switch ((data[1] >> 5) & 3) {
242 case 2: /* Mouse with wheel */
243 input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
244 rw = (data[6] & 0x01) ? -1 :
245 (data[6] & 0x02) ? 1 : 0;
246 input_report_rel(input, REL_WHEEL, rw);
247 /* fall through */
248
249 case 3: /* Mouse without wheel */
250 input_report_key(input, BTN_LEFT, data[1] & 0x01);
251 input_report_key(input, BTN_RIGHT, data[1] & 0x02);
252 /* Compute distance between mouse and tablet */
253 rw = 44 - (data[6] >> 2);
254 if (rw < 0)
255 rw = 0;
256 else if (rw > 31)
257 rw = 31;
258 input_report_abs(input, ABS_DISTANCE, rw);
259 break;
260
261 default:
262 input_report_abs(input, ABS_PRESSURE,
263 data[6] | (((__u16) (data[1] & 0x08)) << 5));
264 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
265 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
266 input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
267 break;
268 }
269
270 input_sync(input);
271 }
272
273 /* Report the state of the two buttons at the top of the tablet
274 * as two extra fingerpad keys (buttons 4 & 5). */
275 rw = data[7] & 0x03;
276 if (rw != wdata->butstate) {
277 wdata->butstate = rw;
278 input_report_key(input, BTN_0, rw & 0x02);
279 input_report_key(input, BTN_1, rw & 0x01);
Przemo Firszt0e253fd2010-01-09 15:20:03 +0100280 input_report_key(input, BTN_TOOL_FINGER, 0xf0);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200281 input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
282 input_sync(input);
283 }
284
Przemo Firszt59d23342010-03-15 19:16:23 +0000285#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
286 /* Store current battery capacity */
287 rw = (data[7] >> 2 & 0x07);
288 if (rw != wdata->battery_capacity)
289 wdata->battery_capacity = rw;
290#endif
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200291 return 1;
292}
Przemo Firsztf6b7efc2011-11-05 11:28:21 +0000293static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
294 u8 *raw_data, int size)
295{
296 struct wacom_data *wdata = hid_get_drvdata(hdev);
297 struct hid_input *hidinput;
298 struct input_dev *input;
299 unsigned char *data = (unsigned char *) raw_data;
300
301 if (!(hdev->claimed & HID_CLAIMED_INPUT))
302 return 0;
303
304 hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
305 input = hidinput->input;
306
307 /* Check if this is a tablet report */
308 if (data[0] != 0x03)
309 return 0;
310
311 return wacom_gr_parse_report(hdev, wdata, input, data);
312}
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200313
David Herrmann3797ef62011-10-08 23:20:17 +0200314static int wacom_input_mapped(struct hid_device *hdev, struct hid_input *hi,
315 struct hid_field *field, struct hid_usage *usage, unsigned long **bit,
316 int *max)
317{
318 struct input_dev *input = hi->input;
319
Jiri Kosinaf6f12422011-10-25 09:58:12 +0200320 __set_bit(INPUT_PROP_POINTER, input->propbit);
321
David Herrmann3797ef62011-10-08 23:20:17 +0200322 /* Basics */
323 input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
324
325 __set_bit(REL_WHEEL, input->relbit);
326
327 __set_bit(BTN_TOOL_PEN, input->keybit);
328 __set_bit(BTN_TOUCH, input->keybit);
329 __set_bit(BTN_STYLUS, input->keybit);
330 __set_bit(BTN_STYLUS2, input->keybit);
331 __set_bit(BTN_LEFT, input->keybit);
332 __set_bit(BTN_RIGHT, input->keybit);
333 __set_bit(BTN_MIDDLE, input->keybit);
334
335 /* Pad */
336 input->evbit[0] |= BIT(EV_MSC);
337
338 __set_bit(MSC_SERIAL, input->mscbit);
339
340 __set_bit(BTN_0, input->keybit);
341 __set_bit(BTN_1, input->keybit);
342 __set_bit(BTN_TOOL_FINGER, input->keybit);
343
344 /* Distance, rubber and mouse */
345 __set_bit(BTN_TOOL_RUBBER, input->keybit);
346 __set_bit(BTN_TOOL_MOUSE, input->keybit);
347
348 input_set_abs_params(input, ABS_X, 0, 16704, 4, 0);
349 input_set_abs_params(input, ABS_Y, 0, 12064, 4, 0);
350 input_set_abs_params(input, ABS_PRESSURE, 0, 511, 0, 0);
351 input_set_abs_params(input, ABS_DISTANCE, 0, 32, 0, 0);
352
353 return 0;
354}
355
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200356static int wacom_probe(struct hid_device *hdev,
357 const struct hid_device_id *id)
358{
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200359 struct wacom_data *wdata;
360 int ret;
361
362 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
363 if (wdata == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -0800364 hid_err(hdev, "can't alloc wacom descriptor\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200365 return -ENOMEM;
366 }
367
368 hid_set_drvdata(hdev, wdata);
369
Bastien Nocera46a709b2010-01-20 12:00:53 +0000370 /* Parse the HID report now */
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200371 ret = hid_parse(hdev);
372 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800373 hid_err(hdev, "parse failed\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200374 goto err_free;
375 }
376
377 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
378 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800379 hid_err(hdev, "hw start failed\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200380 goto err_free;
381 }
382
Przemo Firszt20a3ce7e2010-03-18 14:34:34 +0000383 ret = device_create_file(&hdev->dev, &dev_attr_speed);
384 if (ret)
Joe Perches4291ee32010-12-09 19:29:03 -0800385 hid_warn(hdev,
386 "can't create sysfs speed attribute err: %d\n", ret);
Jiri Kosina342f31e2010-02-03 15:52:31 +0100387
Przemo Firszt06c7c312010-03-22 09:55:04 +0100388 /* Set Wacom mode 2 with high reporting speed */
389 wacom_poke(hdev, 1);
Bastien Nocera46a709b2010-01-20 12:00:53 +0000390
Przemo Firszt59d23342010-03-15 19:16:23 +0000391#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
392 wdata->battery.properties = wacom_battery_props;
393 wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
394 wdata->battery.get_property = wacom_battery_get_property;
395 wdata->battery.name = "wacom_battery";
396 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
397 wdata->battery.use_for_apm = 0;
Bastien Nocera46a709b2010-01-20 12:00:53 +0000398
Przemo Firszt59d23342010-03-15 19:16:23 +0000399 ret = power_supply_register(&hdev->dev, &wdata->battery);
400 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800401 hid_warn(hdev, "can't create sysfs battery attribute, err: %d\n",
402 ret);
David Herrmanndde58cf2011-09-05 18:45:28 +0200403 goto err_battery;
Przemo Firszt59d23342010-03-15 19:16:23 +0000404 }
405
406 wdata->ac.properties = wacom_ac_props;
407 wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
408 wdata->ac.get_property = wacom_ac_get_property;
409 wdata->ac.name = "wacom_ac";
410 wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
411 wdata->ac.use_for_apm = 0;
412
413 ret = power_supply_register(&hdev->dev, &wdata->ac);
414 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800415 hid_warn(hdev,
416 "can't create ac battery attribute, err: %d\n", ret);
David Herrmanndde58cf2011-09-05 18:45:28 +0200417 goto err_ac;
Przemo Firszt59d23342010-03-15 19:16:23 +0000418 }
Przemo Firszt59d23342010-03-15 19:16:23 +0000419#endif
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200420 return 0;
Daniel Mack987a6c02010-08-02 20:15:17 -0700421
David Herrmanndde58cf2011-09-05 18:45:28 +0200422#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
423err_ac:
424 power_supply_unregister(&wdata->battery);
425err_battery:
426 device_remove_file(&hdev->dev, &dev_attr_speed);
427 hid_hw_stop(hdev);
428#endif
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200429err_free:
430 kfree(wdata);
431 return ret;
432}
433
434static void wacom_remove(struct hid_device *hdev)
435{
Przemo Firszt59d23342010-03-15 19:16:23 +0000436#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
437 struct wacom_data *wdata = hid_get_drvdata(hdev);
438#endif
David Herrmann90866172011-09-05 18:45:29 +0200439 device_remove_file(&hdev->dev, &dev_attr_speed);
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200440 hid_hw_stop(hdev);
Przemo Firszt59d23342010-03-15 19:16:23 +0000441
442#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
443 power_supply_unregister(&wdata->battery);
444 power_supply_unregister(&wdata->ac);
445#endif
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200446 kfree(hid_get_drvdata(hdev));
447}
448
449static const struct hid_device_id wacom_devices[] = {
450 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
Jiri Kosinad5e0a062010-07-20 17:48:48 +0200451
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200452 { }
453};
454MODULE_DEVICE_TABLE(hid, wacom_devices);
455
456static struct hid_driver wacom_driver = {
457 .name = "wacom",
458 .id_table = wacom_devices,
459 .probe = wacom_probe,
460 .remove = wacom_remove,
461 .raw_event = wacom_raw_event,
David Herrmann3797ef62011-10-08 23:20:17 +0200462 .input_mapped = wacom_input_mapped,
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200463};
464
Peter Huewea24f423b2009-07-02 19:08:38 +0200465static int __init wacom_init(void)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200466{
467 int ret;
468
469 ret = hid_register_driver(&wacom_driver);
470 if (ret)
Joe Perches4291ee32010-12-09 19:29:03 -0800471 pr_err("can't register wacom driver\n");
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200472 return ret;
473}
474
Peter Huewea24f423b2009-07-02 19:08:38 +0200475static void __exit wacom_exit(void)
Bastien Noceraca2dcd42009-05-11 17:18:12 +0200476{
477 hid_unregister_driver(&wacom_driver);
478}
479
480module_init(wacom_init);
481module_exit(wacom_exit);
482MODULE_LICENSE("GPL");
483