]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/hid/hid-wacom.c
Merge branch 'core-locking-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[karo-tx-linux.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
26 #include "hid-ids.h"
27
28 struct wacom_data {
29         __u16 tool;
30         unsigned char butstate;
31 };
32
33 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
34                 u8 *raw_data, int size)
35 {
36         struct wacom_data *wdata = hid_get_drvdata(hdev);
37         struct hid_input *hidinput;
38         struct input_dev *input;
39         unsigned char *data = (unsigned char *) raw_data;
40         int tool, x, y, rw;
41
42         if (!(hdev->claimed & HID_CLAIMED_INPUT))
43                 return 0;
44
45         tool = 0;
46         hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
47         input = hidinput->input;
48
49         /* Check if this is a tablet report */
50         if (data[0] != 0x03)
51                 return 0;
52
53         /* Get X & Y positions */
54         x = le16_to_cpu(*(__le16 *) &data[2]);
55         y = le16_to_cpu(*(__le16 *) &data[4]);
56
57         /* Get current tool identifier */
58         if (data[1] & 0x90) { /* If pen is in the in/active area */
59                 switch ((data[1] >> 5) & 3) {
60                 case 0: /* Pen */
61                         tool = BTN_TOOL_PEN;
62                         break;
63
64                 case 1: /* Rubber */
65                         tool = BTN_TOOL_RUBBER;
66                         break;
67
68                 case 2: /* Mouse with wheel */
69                 case 3: /* Mouse without wheel */
70                         tool = BTN_TOOL_MOUSE;
71                         break;
72                 }
73
74                 /* Reset tool if out of active tablet area */
75                 if (!(data[1] & 0x10))
76                         tool = 0;
77         }
78
79         /* If tool changed, notify input subsystem */
80         if (wdata->tool != tool) {
81                 if (wdata->tool) {
82                         /* Completely reset old tool state */
83                         if (wdata->tool == BTN_TOOL_MOUSE) {
84                                 input_report_key(input, BTN_LEFT, 0);
85                                 input_report_key(input, BTN_RIGHT, 0);
86                                 input_report_key(input, BTN_MIDDLE, 0);
87                                 input_report_abs(input, ABS_DISTANCE,
88                                                 input->absmax[ABS_DISTANCE]);
89                         } else {
90                                 input_report_key(input, BTN_TOUCH, 0);
91                                 input_report_key(input, BTN_STYLUS, 0);
92                                 input_report_key(input, BTN_STYLUS2, 0);
93                                 input_report_abs(input, ABS_PRESSURE, 0);
94                         }
95                         input_report_key(input, wdata->tool, 0);
96                         input_sync(input);
97                 }
98                 wdata->tool = tool;
99                 if (tool)
100                         input_report_key(input, tool, 1);
101         }
102
103         if (tool) {
104                 input_report_abs(input, ABS_X, x);
105                 input_report_abs(input, ABS_Y, y);
106
107                 switch ((data[1] >> 5) & 3) {
108                 case 2: /* Mouse with wheel */
109                         input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
110                         rw = (data[6] & 0x01) ? -1 :
111                                 (data[6] & 0x02) ? 1 : 0;
112                         input_report_rel(input, REL_WHEEL, rw);
113                         /* fall through */
114
115                 case 3: /* Mouse without wheel */
116                         input_report_key(input, BTN_LEFT, data[1] & 0x01);
117                         input_report_key(input, BTN_RIGHT, data[1] & 0x02);
118                         /* Compute distance between mouse and tablet */
119                         rw = 44 - (data[6] >> 2);
120                         if (rw < 0)
121                                 rw = 0;
122                         else if (rw > 31)
123                                 rw = 31;
124                         input_report_abs(input, ABS_DISTANCE, rw);
125                         break;
126
127                 default:
128                         input_report_abs(input, ABS_PRESSURE,
129                                         data[6] | (((__u16) (data[1] & 0x08)) << 5));
130                         input_report_key(input, BTN_TOUCH, data[1] & 0x01);
131                         input_report_key(input, BTN_STYLUS, data[1] & 0x02);
132                         input_report_key(input, BTN_STYLUS2, (tool == BTN_TOOL_PEN) && data[1] & 0x04);
133                         break;
134                 }
135
136                 input_sync(input);
137         }
138
139         /* Report the state of the two buttons at the top of the tablet
140          * as two extra fingerpad keys (buttons 4 & 5). */
141         rw = data[7] & 0x03;
142         if (rw != wdata->butstate) {
143                 wdata->butstate = rw;
144                 input_report_key(input, BTN_0, rw & 0x02);
145                 input_report_key(input, BTN_1, rw & 0x01);
146                 input_report_key(input, BTN_TOOL_FINGER, 0xf0);
147                 input_event(input, EV_MSC, MSC_SERIAL, 0xf0);
148                 input_sync(input);
149         }
150
151         return 1;
152 }
153
154 static int wacom_probe(struct hid_device *hdev,
155                 const struct hid_device_id *id)
156 {
157         struct hid_input *hidinput;
158         struct input_dev *input;
159         struct wacom_data *wdata;
160         char rep_data[2];
161         int ret;
162         int limit;
163
164         wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
165         if (wdata == NULL) {
166                 dev_err(&hdev->dev, "can't alloc wacom descriptor\n");
167                 return -ENOMEM;
168         }
169
170         hid_set_drvdata(hdev, wdata);
171
172         /* Parse the HID report now */
173         ret = hid_parse(hdev);
174         if (ret) {
175                 dev_err(&hdev->dev, "parse failed\n");
176                 goto err_free;
177         }
178
179         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
180         if (ret) {
181                 dev_err(&hdev->dev, "hw start failed\n");
182                 goto err_free;
183         }
184
185         /*
186          * Note that if the raw queries fail, it's not a hard failure and it
187          * is safe to continue
188          */
189
190         /* Set Wacom mode2 */
191         rep_data[0] = 0x03; rep_data[1] = 0x00;
192         limit = 3;
193         do {
194                 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
195                                 HID_FEATURE_REPORT);
196         } while (ret < 0 && limit-- > 0);
197         if (ret < 0)
198                 dev_warn(&hdev->dev, "failed to poke device #1, %d\n", ret);
199
200         /* 0x06 - high reporting speed, 0x05 - low speed */
201         rep_data[0] = 0x06; rep_data[1] = 0x00;
202         limit = 3;
203         do {
204                 ret = hdev->hid_output_raw_report(hdev, rep_data, 2,
205                                 HID_FEATURE_REPORT);
206         } while (ret < 0 && limit-- > 0);
207         if (ret < 0)
208                 dev_warn(&hdev->dev, "failed to poke device #2, %d\n", ret);
209
210         hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
211         input = hidinput->input;
212
213         /* Basics */
214         input->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_REL);
215         input->absbit[0] |= BIT(ABS_X) | BIT(ABS_Y) |
216                 BIT(ABS_PRESSURE) | BIT(ABS_DISTANCE);
217         input->relbit[0] |= BIT(REL_WHEEL);
218         set_bit(BTN_TOOL_PEN, input->keybit);
219         set_bit(BTN_TOUCH, input->keybit);
220         set_bit(BTN_STYLUS, input->keybit);
221         set_bit(BTN_STYLUS2, input->keybit);
222         set_bit(BTN_LEFT, input->keybit);
223         set_bit(BTN_RIGHT, input->keybit);
224         set_bit(BTN_MIDDLE, input->keybit);
225
226         /* Pad */
227         input->evbit[0] |= BIT(EV_MSC);
228         input->mscbit[0] |= BIT(MSC_SERIAL);
229         set_bit(BTN_0, input->keybit);
230         set_bit(BTN_1, input->keybit);
231         set_bit(BTN_TOOL_FINGER, input->keybit);
232
233         /* Distance, rubber and mouse */
234         input->absbit[0] |= BIT(ABS_DISTANCE);
235         set_bit(BTN_TOOL_RUBBER, input->keybit);
236         set_bit(BTN_TOOL_MOUSE, input->keybit);
237
238         input->absmax[ABS_PRESSURE] = 511;
239         input->absmax[ABS_DISTANCE] = 32;
240
241         input->absmax[ABS_X] = 16704;
242         input->absmax[ABS_Y] = 12064;
243         input->absfuzz[ABS_X] = 4;
244         input->absfuzz[ABS_Y] = 4;
245
246         return 0;
247 err_free:
248         kfree(wdata);
249         return ret;
250 }
251
252 static void wacom_remove(struct hid_device *hdev)
253 {
254         hid_hw_stop(hdev);
255         kfree(hid_get_drvdata(hdev));
256 }
257
258 static const struct hid_device_id wacom_devices[] = {
259         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_WACOM, USB_DEVICE_ID_WACOM_GRAPHIRE_BLUETOOTH) },
260
261         { }
262 };
263 MODULE_DEVICE_TABLE(hid, wacom_devices);
264
265 static struct hid_driver wacom_driver = {
266         .name = "wacom",
267         .id_table = wacom_devices,
268         .probe = wacom_probe,
269         .remove = wacom_remove,
270         .raw_event = wacom_raw_event,
271 };
272
273 static int __init wacom_init(void)
274 {
275         int ret;
276
277         ret = hid_register_driver(&wacom_driver);
278         if (ret)
279                 printk(KERN_ERR "can't register wacom driver\n");
280         return ret;
281 }
282
283 static void __exit wacom_exit(void)
284 {
285         hid_unregister_driver(&wacom_driver);
286 }
287
288 module_init(wacom_init);
289 module_exit(wacom_exit);
290 MODULE_LICENSE("GPL");
291