HID: separate mode switching function for wacom bluetooth driver
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / hid / hid-wacom.c
CommitLineData
ca2dcd40
BN
1/*
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
21#include <linux/device.h>
22#include <linux/hid.h>
23#include <linux/module.h>
59d2334a
PF
24#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
25#include <linux/power_supply.h>
26#endif
ca2dcd40
BN
27
28#include "hid-ids.h"
29
30struct wacom_data {
31 __u16 tool;
32 unsigned char butstate;
59d2334a
PF
33#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
34 int battery_capacity;
35 struct power_supply battery;
36 struct power_supply ac;
37#endif
ca2dcd40
BN
38};
39
59d2334a
PF
40#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
41/*percent of battery capacity, 0 means AC online*/
42static unsigned short batcap[8] = { 1, 15, 25, 35, 50, 70, 100, 0 };
43
44static enum power_supply_property wacom_battery_props[] = {
45 POWER_SUPPLY_PROP_PRESENT,
46 POWER_SUPPLY_PROP_CAPACITY
47};
48
49static enum power_supply_property wacom_ac_props[] = {
50 POWER_SUPPLY_PROP_PRESENT,
51 POWER_SUPPLY_PROP_ONLINE
52};
53
54static int wacom_battery_get_property(struct power_supply *psy,
55 enum power_supply_property psp,
56 union power_supply_propval *val)
57{
58 struct wacom_data *wdata = container_of(psy,
59 struct wacom_data, battery);
60 int power_state = batcap[wdata->battery_capacity];
61 int ret = 0;
62
63 switch (psp) {
64 case POWER_SUPPLY_PROP_PRESENT:
65 val->intval = 1;
66 break;
67 case POWER_SUPPLY_PROP_CAPACITY:
68 /* show 100% battery capacity when charging */
69 if (power_state == 0)
70 val->intval = 100;
71 else
72 val->intval = power_state;
73 break;
74 default:
75 ret = -EINVAL;
76 break;
77 }
78 return ret;
79}
80
81static int wacom_ac_get_property(struct power_supply *psy,
82 enum power_supply_property psp,
83 union power_supply_propval *val)
84{
85 struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
86 int power_state = batcap[wdata->battery_capacity];
87 int ret = 0;
88
89 switch (psp) {
90 case POWER_SUPPLY_PROP_PRESENT:
91 /* fall through */
92 case POWER_SUPPLY_PROP_ONLINE:
93 if (power_state == 0)
94 val->intval = 1;
95 else
96 val->intval = 0;
97 break;
98 default:
99 ret = -EINVAL;
100 break;
101 }
102 return ret;
103}
104#endif
105
06c7c313
PF
106static void wacom_poke(struct hid_device *hdev, u8 speed)
107{
108 int limit, ret;
109 char rep_data[2];
110
111 rep_data[0] = 0x03 ; rep_data[1] = 0x00;
112 limit = 3;
113 do {
114 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
115 HID_FEATURE_REPORT);
116 } while (ret < 0 && limit-- > 0);
117
118 if (ret >= 0) {
119 if (speed == 0)
120 rep_data[0] = 0x05;
121 else
122 rep_data[0] = 0x06;
123
124 rep_data[1] = 0x00;
125 limit = 3;
126 do {
127 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
128 HID_FEATURE_REPORT);
129 } while (ret < 0 && limit-- > 0);
130
131 if (ret >= 0)
132 return;
133 }
134
135 /*
136 * Note that if the raw queries fail, it's not a hard failure and it
137 * is safe to continue
138 */
139 dev_warn(&hdev->dev, "failed to poke device, command %d, err %d\n",
140 rep_data[0], ret);
141 return;
142}
143
ca2dcd40
BN
144static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
145 u8 *raw_data, int size)
146{
147 struct wacom_data *wdata = hid_get_drvdata(hdev);
148 struct hid_input *hidinput;
149 struct input_dev *input;
150 unsigned char *data = (unsigned char *) raw_data;
151 int tool, x, y, rw;
152
153 if (!(hdev->claimed & HID_CLAIMED_INPUT))
154 return 0;
155
156 tool = 0;
157 hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
158 input = hidinput->input;
159
160 /* Check if this is a tablet report */
161 if (data[0] != 0x03)
162 return 0;
163
164 /* Get X & Y positions */
165 x = le16_to_cpu(*(__le16 *) &data[2]);
166 y = le16_to_cpu(*(__le16 *) &data[4]);
167
168 /* Get current tool identifier */
169 if (data[1] & 0x90) { /* If pen is in the in/active area */
170 switch ((data[1] >> 5) & 3) {
171 case 0: /* Pen */
172 tool = BTN_TOOL_PEN;
173 break;
174
175 case 1: /* Rubber */
176 tool = BTN_TOOL_RUBBER;
177 break;
178
179 case 2: /* Mouse with wheel */
180 case 3: /* Mouse without wheel */
181 tool = BTN_TOOL_MOUSE;
182 break;
183 }
184
185 /* Reset tool if out of active tablet area */
186 if (!(data[1] & 0x10))
187 tool = 0;
188 }
189
190 /* If tool changed, notify input subsystem */
191 if (wdata->tool != tool) {
192 if (wdata->tool) {
193 /* Completely reset old tool state */
194 if (wdata->tool == BTN_TOOL_MOUSE) {
195 input_report_key(input, BTN_LEFT, 0);
196 input_report_key(input, BTN_RIGHT, 0);
197 input_report_key(input, BTN_MIDDLE, 0);
198 input_report_abs(input, ABS_DISTANCE,
199 input->absmax[ABS_DISTANCE]);
200 } else {
201 input_report_key(input, BTN_TOUCH, 0);
202 input_report_key(input, BTN_STYLUS, 0);
203 input_report_key(input, BTN_STYLUS2, 0);
204 input_report_abs(input, ABS_PRESSURE, 0);
205 }
206 input_report_key(input, wdata->tool, 0);
207 input_sync(input);
208 }
209 wdata->tool = tool;
210 if (tool)
211 input_report_key(input, tool, 1);
212 }
213
214 if (tool) {
215 input_report_abs(input, ABS_X, x);
216 input_report_abs(input, ABS_Y, y);
217
218 switch ((data[1] >> 5) & 3) {
219 case 2: /* Mouse with wheel */
220 input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
221 rw = (data[6] & 0x01) ? -1 :
222 (data[6] & 0x02) ? 1 : 0;
223 input_report_rel(input, REL_WHEEL, rw);
224 /* fall through */
225
226 case 3: /* Mouse without wheel */
227 input_report_key(input, BTN_LEFT, data[1] & 0x01);
228 input_report_key(input, BTN_RIGHT, data[1] & 0x02);
229 /* Compute distance between mouse and tablet */
230 rw = 44 - (data[6] >> 2);
231 if (rw < 0)
232 rw = 0;
233 else if (rw > 31)
234 rw = 31;
235 input_report_abs(input, ABS_DISTANCE, rw);
236 break;
237
238 default:
239 input_report_abs(input, ABS_PRESSURE,
240 data[6] | (((__u16) (data[1] & 0x08)) << 5));
241 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
242 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
243 input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
244 break;
245 }
246
247 input_sync(input);
248 }
249
250 /* Report the state of the two buttons at the top of the tablet
251 * as two extra fingerpad keys (buttons 4 & 5). */
252 rw = data[7] & 0x03;
253 if (rw != wdata->butstate) {
254 wdata->butstate = rw;
255 input_report_key(input, BTN_0, rw & 0x02);
256 input_report_key(input, BTN_1, rw & 0x01);
0e253fdb 257 input_report_key(input, BTN_TOOL_FINGER, 0xf0);
ca2dcd40
BN
258 input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
259 input_sync(input);
260 }
261
59d2334a
PF
262#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
263 /* Store current battery capacity */
264 rw = (data[7] >> 2 & 0x07);
265 if (rw != wdata->battery_capacity)
266 wdata->battery_capacity = rw;
267#endif
ca2dcd40
BN
268 return 1;
269}
270
271static int wacom_probe(struct hid_device *hdev,
272 const struct hid_device_id *id)
273{
274 struct hid_input *hidinput;
275 struct input_dev *input;
276 struct wacom_data *wdata;
277 int ret;
278
279 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
280 if (wdata == NULL) {
281 dev_err(&hdev->dev, "can't alloc wacom descriptor\n");
282 return -ENOMEM;
283 }
284
285 hid_set_drvdata(hdev, wdata);
286
46a709b9 287 /* Parse the HID report now */
ca2dcd40
BN
288 ret = hid_parse(hdev);
289 if (ret) {
290 dev_err(&hdev->dev, "parse failed\n");
291 goto err_free;
292 }
293
294 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
295 if (ret) {
296 dev_err(&hdev->dev, "hw start failed\n");
297 goto err_free;
298 }
299
06c7c313
PF
300 /* Set Wacom mode 2 with high reporting speed */
301 wacom_poke(hdev, 1);
46a709b9 302
59d2334a
PF
303#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
304 wdata->battery.properties = wacom_battery_props;
305 wdata->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
306 wdata->battery.get_property = wacom_battery_get_property;
307 wdata->battery.name = "wacom_battery";
308 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
309 wdata->battery.use_for_apm = 0;
310
311 ret = power_supply_register(&hdev->dev, &wdata->battery);
312 if (ret) {
313 dev_warn(&hdev->dev,
314 "can't create sysfs battery attribute, err: %d\n", ret);
315 /*
316 * battery attribute is not critical for the tablet, but if it
317 * failed then there is no need to create ac attribute
318 */
319 goto move_on;
320 }
321
322 wdata->ac.properties = wacom_ac_props;
323 wdata->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
324 wdata->ac.get_property = wacom_ac_get_property;
325 wdata->ac.name = "wacom_ac";
326 wdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
327 wdata->ac.use_for_apm = 0;
328
329 ret = power_supply_register(&hdev->dev, &wdata->ac);
330 if (ret) {
331 dev_warn(&hdev->dev,
332 "can't create ac battery attribute, err: %d\n", ret);
333 /*
334 * ac attribute is not critical for the tablet, but if it
335 * failed then we don't want to battery attribute to exist
336 */
337 power_supply_unregister(&wdata->battery);
338 }
339
340move_on:
341#endif
ca2dcd40
BN
342 hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
343 input = hidinput->input;
344
345 /* Basics */
346 input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
347 input->absbit[0] |= BIT(ABS_X) | BIT(ABS_Y) |
348 BIT(ABS_PRESSURE) | BIT(ABS_DISTANCE);
349 input->relbit[0] |= BIT(REL_WHEEL);
350 set_bit(BTN_TOOL_PEN, input->keybit);
351 set_bit(BTN_TOUCH, input->keybit);
352 set_bit(BTN_STYLUS, input->keybit);
353 set_bit(BTN_STYLUS2, input->keybit);
354 set_bit(BTN_LEFT, input->keybit);
355 set_bit(BTN_RIGHT, input->keybit);
356 set_bit(BTN_MIDDLE, input->keybit);
357
358 /* Pad */
359 input->evbit[0] |= BIT(EV_MSC);
360 input->mscbit[0] |= BIT(MSC_SERIAL);
d01799b2
PF
361 set_bit(BTN_0, input->keybit);
362 set_bit(BTN_1, input->keybit);
363 set_bit(BTN_TOOL_FINGER, input->keybit);
ca2dcd40
BN
364
365 /* Distance, rubber and mouse */
366 input->absbit[0] |= BIT(ABS_DISTANCE);
367 set_bit(BTN_TOOL_RUBBER, input->keybit);
368 set_bit(BTN_TOOL_MOUSE, input->keybit);
369
370 input->absmax[ABS_PRESSURE] = 511;
371 input->absmax[ABS_DISTANCE] = 32;
372
373 input->absmax[ABS_X] = 16704;
374 input->absmax[ABS_Y] = 12064;
375 input->absfuzz[ABS_X] = 4;
376 input->absfuzz[ABS_Y] = 4;
377
378 return 0;
379err_free:
380 kfree(wdata);
381 return ret;
382}
383
384static void wacom_remove(struct hid_device *hdev)
385{
59d2334a
PF
386#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
387 struct wacom_data *wdata = hid_get_drvdata(hdev);
388#endif
ca2dcd40 389 hid_hw_stop(hdev);
59d2334a
PF
390
391#ifdef CONFIG_HID_WACOM_POWER_SUPPLY
392 power_supply_unregister(&wdata->battery);
393 power_supply_unregister(&wdata->ac);
394#endif
ca2dcd40
BN
395 kfree(hid_get_drvdata(hdev));
396}
397
398static const struct hid_device_id wacom_devices[] = {
399 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
400
401 { }
402};
403MODULE_DEVICE_TABLE(hid, wacom_devices);
404
405static struct hid_driver wacom_driver = {
406 .name = "wacom",
407 .id_table = wacom_devices,
408 .probe = wacom_probe,
409 .remove = wacom_remove,
410 .raw_event = wacom_raw_event,
411};
412
a24f423b 413static int __init wacom_init(void)
ca2dcd40
BN
414{
415 int ret;
416
417 ret = hid_register_driver(&wacom_driver);
418 if (ret)
419 printk(KERN_ERR "can't register wacom driver\n");
420 printk(KERN_ERR "wacom driver registered\n");
421 return ret;
422}
423
a24f423b 424static void __exit wacom_exit(void)
ca2dcd40
BN
425{
426 hid_unregister_driver(&wacom_driver);
427}
428
429module_init(wacom_init);
430module_exit(wacom_exit);
431MODULE_LICENSE("GPL");
432