Merge branch 'upstream' into for-linus
[linux-2.6.git] / drivers / hid / hid-wacom.c
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>
24 #include <linux/slab.h>
25 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
26 #include <linux/power_supply.h>
27 #endif
28
29 #include "hid-ids.h"
30
31 struct wacom_data {
32         __u16 tool;
33         unsigned char butstate;
34         unsigned char high_speed;
35 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
36         int battery_capacity;
37         struct power_supply battery;
38         struct power_supply ac;
39 #endif
40 };
41
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 };
45
46 static enum power_supply_property wacom_battery_props[] = {
47         POWER_SUPPLY_PROP_PRESENT,
48         POWER_SUPPLY_PROP_CAPACITY
49 };
50
51 static enum power_supply_property wacom_ac_props[] = {
52         POWER_SUPPLY_PROP_PRESENT,
53         POWER_SUPPLY_PROP_ONLINE
54 };
55
56 static int wacom_battery_get_property(struct power_supply *psy,
57                                 enum power_supply_property psp,
58                                 union power_supply_propval *val)
59 {
60         struct wacom_data *wdata = container_of(psy,
61                                         struct wacom_data, battery);
62         int power_state = batcap[wdata->battery_capacity];
63         int ret = 0;
64
65         switch (psp) {
66         case POWER_SUPPLY_PROP_PRESENT:
67                 val->intval = 1;
68                 break;
69         case POWER_SUPPLY_PROP_CAPACITY:
70                 /* show 100% battery capacity when charging */
71                 if (power_state == 0)
72                         val->intval = 100;
73                 else
74                         val->intval = power_state;
75                 break;
76         default:
77                 ret = -EINVAL;
78                 break;
79         }
80         return ret;
81 }
82
83 static int wacom_ac_get_property(struct power_supply *psy,
84                                 enum power_supply_property psp,
85                                 union power_supply_propval *val)
86 {
87         struct wacom_data *wdata = container_of(psy, struct wacom_data, ac);
88         int power_state = batcap[wdata->battery_capacity];
89         int ret = 0;
90
91         switch (psp) {
92         case POWER_SUPPLY_PROP_PRESENT:
93                 /* fall through */
94         case POWER_SUPPLY_PROP_ONLINE:
95                 if (power_state == 0)
96                         val->intval = 1;
97                 else
98                         val->intval = 0;
99                 break;
100         default:
101                 ret = -EINVAL;
102                 break;
103         }
104         return ret;
105 }
106 #endif
107
108 static void wacom_poke(struct hid_device *hdev, u8 speed)
109 {
110         struct wacom_data *wdata = hid_get_drvdata(hdev);
111         int limit, ret;
112         char rep_data[2];
113
114         rep_data[0] = 0x03 ; rep_data[1] = 0x00;
115         limit = 3;
116         do {
117                 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
118                                 HID_FEATURE_REPORT);
119         } while (ret < 0 && limit-- > 0);
120
121         if (ret >= 0) {
122                 if (speed == 0)
123                         rep_data[0] = 0x05;
124                 else
125                         rep_data[0] = 0x06;
126
127                 rep_data[1] = 0x00;
128                 limit = 3;
129                 do {
130                         ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
131                                         HID_FEATURE_REPORT);
132                 } while (ret < 0 && limit-- > 0);
133
134                 if (ret >= 0) {
135                         wdata->high_speed = speed;
136                         return;
137                 }
138         }
139
140         /*
141          * Note that if the raw queries fail, it's not a hard failure and it
142          * is safe to continue
143          */
144         dev_warn(&hdev->dev, "failed to poke device, command %d, err %d\n",
145                                 rep_data[0], ret);
146         return;
147 }
148
149 static ssize_t wacom_show_speed(struct device *dev,
150                                 struct device_attribute
151                                 *attr, char *buf)
152 {
153         struct wacom_data *wdata = dev_get_drvdata(dev);
154
155         return snprintf(buf, PAGE_SIZE, "%i\n", wdata->high_speed);
156 }
157
158 static ssize_t wacom_store_speed(struct device *dev,
159                                 struct device_attribute *attr,
160                                 const char *buf, size_t count)
161 {
162         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
163         int new_speed;
164
165         if (sscanf(buf, "%1d", &new_speed ) != 1)
166                 return -EINVAL;
167
168         if (new_speed == 0 || new_speed == 1) {
169                 wacom_poke(hdev, new_speed);
170                 return strnlen(buf, PAGE_SIZE);
171         } else
172                 return -EINVAL;
173 }
174
175 static DEVICE_ATTR(speed, S_IRUGO | S_IWUGO,
176                 wacom_show_speed, wacom_store_speed);
177
178 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
179                 u8 *raw_data, int size)
180 {
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;
185         int tool, x, y, rw;
186
187         if (!(hdev->claimed & HID_CLAIMED_INPUT))
188                 return 0;
189
190         tool = 0;
191         hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
192         input = hidinput->input;
193
194         /* Check if this is a tablet report */
195         if (data[0] != 0x03)
196                 return 0;
197
198         /* Get X & Y positions */
199         x = le16_to_cpu(*(__le16 *) &data[2]);
200         y = le16_to_cpu(*(__le16 *) &data[4]);
201
202         /* Get current tool identifier */
203         if (data[1] & 0x90) { /* If pen is in the in/active area */
204                 switch ((data[1] >> 5) & 3) {
205                 case 0: /* Pen */
206                         tool = BTN_TOOL_PEN;
207                         break;
208
209                 case 1: /* Rubber */
210                         tool = BTN_TOOL_RUBBER;
211                         break;
212
213                 case 2: /* Mouse with wheel */
214                 case 3: /* Mouse without wheel */
215                         tool = BTN_TOOL_MOUSE;
216                         break;
217                 }
218
219                 /* Reset tool if out of active tablet area */
220                 if (!(data[1] & 0x10))
221                         tool = 0;
222         }
223
224         /* If tool changed, notify input subsystem */
225         if (wdata->tool != tool) {
226                 if (wdata->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->absmax[ABS_DISTANCE]);
234                         } else {
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);
239                         }
240                         input_report_key(input, wdata->tool, 0);
241                         input_sync(input);
242                 }
243                 wdata->tool = tool;
244                 if (tool)
245                         input_report_key(input, tool, 1);
246         }
247
248         if (tool) {
249                 input_report_abs(input, ABS_X, x);
250                 input_report_abs(input, ABS_Y, y);
251
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);
258                         /* fall through */
259
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);
265                         if (rw < 0)
266                                 rw = 0;
267                         else if (rw > 31)
268                                 rw = 31;
269                         input_report_abs(input, ABS_DISTANCE, rw);
270                         break;
271
272                 default:
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);
278                         break;
279                 }
280
281                 input_sync(input);
282         }
283
284         /* Report the state of the two buttons at the top of the tablet
285          * as two extra fingerpad keys (buttons 4 & 5). */
286         rw = data[7] & 0x03;
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);
293                 input_sync(input);
294         }
295
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;
301 #endif
302         return 1;
303 }
304
305 static int wacom_probe(struct hid_device *hdev,
306                 const struct hid_device_id *id)
307 {
308         struct hid_input *hidinput;
309         struct input_dev *input;
310         struct wacom_data *wdata;
311         int ret;
312
313         wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
314         if (wdata == NULL) {
315                 dev_err(&hdev->dev, "can't alloc wacom descriptor\n");
316                 return -ENOMEM;
317         }
318
319         hid_set_drvdata(hdev, wdata);
320
321         /* Parse the HID report now */
322         ret = hid_parse(hdev);
323         if (ret) {
324                 dev_err(&hdev->dev, "parse failed\n");
325                 goto err_free;
326         }
327
328         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
329         if (ret) {
330                 dev_err(&hdev->dev, "hw start failed\n");
331                 goto err_free;
332         }
333
334         ret = device_create_file(&hdev->dev, &dev_attr_speed);
335         if (ret)
336                 dev_warn(&hdev->dev,
337                         "can't create sysfs speed attribute err: %d\n", ret);
338
339         /* Set Wacom mode 2 with high reporting speed */
340         wacom_poke(hdev, 1);
341
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;
349
350         ret = power_supply_register(&hdev->dev, &wdata->battery);
351         if (ret) {
352                 dev_warn(&hdev->dev,
353                         "can't create sysfs battery attribute, err: %d\n", ret);
354                 /*
355                  * battery attribute is not critical for the tablet, but if it
356                  * failed then there is no need to create ac attribute
357                  */
358                 goto move_on;
359         }
360
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;
367
368         ret = power_supply_register(&hdev->dev, &wdata->ac);
369         if (ret) {
370                 dev_warn(&hdev->dev,
371                         "can't create ac battery attribute, err: %d\n", ret);
372                 /*
373                  * ac attribute is not critical for the tablet, but if it
374                  * failed then we don't want to battery attribute to exist
375                  */
376                 power_supply_unregister(&wdata->battery);
377         }
378
379 move_on:
380 #endif
381         hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
382         input = hidinput->input;
383
384         /* Basics */
385         input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
386         input->absbit[0] |= BIT(ABS_X) | BIT(ABS_Y) |
387                 BIT(ABS_PRESSURE) | BIT(ABS_DISTANCE);
388         input->relbit[0] |= BIT(REL_WHEEL);
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);
396
397         /* Pad */
398         input->evbit[0] |= BIT(EV_MSC);
399         input->mscbit[0] |= BIT(MSC_SERIAL);
400         set_bit(BTN_0, input->keybit);
401         set_bit(BTN_1, input->keybit);
402         set_bit(BTN_TOOL_FINGER, input->keybit);
403
404         /* Distance, rubber and mouse */
405         input->absbit[0] |= BIT(ABS_DISTANCE);
406         set_bit(BTN_TOOL_RUBBER, input->keybit);
407         set_bit(BTN_TOOL_MOUSE, input->keybit);
408
409         input->absmax[ABS_PRESSURE] = 511;
410         input->absmax[ABS_DISTANCE] = 32;
411
412         input->absmax[ABS_X] = 16704;
413         input->absmax[ABS_Y] = 12064;
414         input->absfuzz[ABS_X] = 4;
415         input->absfuzz[ABS_Y] = 4;
416
417         return 0;
418 err_free:
419         kfree(wdata);
420         return ret;
421 }
422
423 static void wacom_remove(struct hid_device *hdev)
424 {
425 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
426         struct wacom_data *wdata = hid_get_drvdata(hdev);
427 #endif
428         hid_hw_stop(hdev);
429
430 #ifdef CONFIG_HID_WACOM_POWER_SUPPLY
431         power_supply_unregister(&wdata->battery);
432         power_supply_unregister(&wdata->ac);
433 #endif
434         kfree(hid_get_drvdata(hdev));
435 }
436
437 static const struct hid_device_id wacom_devices[] = {
438         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
439         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_INTUOS4_BLUETOOTH) },
440         { }
441 };
442 MODULE_DEVICE_TABLE(hid, wacom_devices);
443
444 static struct hid_driver wacom_driver = {
445         .name = "wacom",
446         .id_table = wacom_devices,
447         .probe = wacom_probe,
448         .remove = wacom_remove,
449         .raw_event = wacom_raw_event,
450 };
451
452 static int __init wacom_init(void)
453 {
454         int ret;
455
456         ret = hid_register_driver(&wacom_driver);
457         if (ret)
458                 printk(KERN_ERR "can't register wacom driver\n");
459         return ret;
460 }
461
462 static void __exit wacom_exit(void)
463 {
464         hid_unregister_driver(&wacom_driver);
465 }
466
467 module_init(wacom_init);
468 module_exit(wacom_exit);
469 MODULE_LICENSE("GPL");
470