Input: add support for Hanwang tablets
[linux-2.6.git] / drivers / input / tablet / hanwang.c
1 /*
2  *  USB Hanwang tablet support
3  *
4  *  Copyright (c) 2010 Xing Wei <weixing@hanwang.com.cn>
5  *
6  */
7
8 /*
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  */
24
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/usb/input.h>
31
32 #define DRIVER_AUTHOR   "Xing Wei <weixing@hanwang.com.cn>"
33 #define DRIVER_DESC     "USB Hanwang tablet driver"
34 #define DRIVER_LICENSE  "GPL"
35
36 MODULE_AUTHOR(DRIVER_AUTHOR);
37 MODULE_DESCRIPTION(DRIVER_DESC);
38 MODULE_LICENSE(DRIVER_LICENSE);
39
40 #define USB_VENDOR_ID_HANWANG           0x0b57
41 #define HANWANG_TABLET_INT_CLASS        0x0003
42 #define HANWANG_TABLET_INT_SUB_CLASS    0x0001
43 #define HANWANG_TABLET_INT_PROTOCOL     0x0002
44
45 #define ART_MASTERIII_PKGLEN_MAX        10
46
47 /* match vendor and interface info  */
48 #define HANWANG_TABLET_DEVICE(vend, cl, sc, pr) \
49         .match_flags = USB_DEVICE_ID_MATCH_VENDOR \
50                 | USB_DEVICE_ID_MATCH_INT_INFO, \
51         .idVendor = (vend), \
52         .bInterfaceClass = (cl), \
53         .bInterfaceSubClass = (sc), \
54         .bInterfaceProtocol = (pr)
55
56 struct hanwang {
57         unsigned char *data;
58         dma_addr_t data_dma;
59         struct input_dev *dev;
60         struct usb_device *usbdev;
61         struct urb *irq;
62         const struct hanwang_features *features;
63         unsigned int current_tool;
64         char name[64];
65         char phys[32];
66 };
67
68 struct hanwang_features {
69         unsigned short pid;
70         char *name;
71         int pkg_len;
72         int max_x;
73         int max_y;
74         int max_tilt_x;
75         int max_tilt_y;
76         int max_pressure;
77 };
78
79 static const struct hanwang_features features_array[] = {
80         { 0x8528, "Hanwang Art Master III 0906",
81           ART_MASTERIII_PKGLEN_MAX, 0x5757, 0x3692, 0x3f, 0x7f, 2048 },
82         { 0x8529, "Hanwang Art Master III 0604",
83           ART_MASTERIII_PKGLEN_MAX, 0x3d84, 0x2672, 0x3f, 0x7f, 2048 },
84         { 0x852a, "Hanwang Art Master III 1308",
85           ART_MASTERIII_PKGLEN_MAX, 0x7f00, 0x4f60, 0x3f, 0x7f, 2048 },
86 };
87
88 static const int hw_eventtypes[] = {
89         EV_KEY, EV_ABS,
90 };
91
92 static const int hw_absevents[] = {
93         ABS_X, ABS_Y, ABS_TILT_X, ABS_TILT_Y, ABS_WHEEL, ABS_PRESSURE,
94 };
95
96 static const int hw_btnevents[] = {
97         BTN_STYLUS, BTN_STYLUS2, BTN_TOOL_PEN, BTN_TOOL_RUBBER, BTN_TOOL_MOUSE,
98         BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8
99 };
100
101 static void hanwang_parse_packet(struct hanwang *hanwang)
102 {
103         unsigned char *data = hanwang->data;
104         struct input_dev *input_dev = hanwang->dev;
105         struct usb_device *dev = hanwang->usbdev;
106         int i;
107
108         switch (data[0]) {
109         case 0x02:      /* data packet */
110                 switch (data[1]) {
111                 case 0x80:      /* tool prox out */
112                         input_report_key(input_dev, hanwang->current_tool, 0);
113                         break;
114
115                 case 0xc2:      /* first time tool prox in */
116                         switch (data[3] & 0xf0) {
117                         case 0x20:
118                                 hanwang->current_tool = BTN_TOOL_PEN;
119                                 input_report_key(input_dev, BTN_TOOL_PEN, 1);
120                                 break;
121                         case 0xa0:
122                                 hanwang->current_tool = BTN_TOOL_RUBBER;
123                                 input_report_key(input_dev, BTN_TOOL_RUBBER, 1);
124                                 break;
125                         default:
126                                 dev_dbg(&dev->dev,
127                                         "unknown tablet tool %02x ", data[0]);
128                                 break;
129                         }
130                         break;
131
132                 default:        /* tool data packet */
133                         input_report_abs(input_dev, ABS_X,
134                                          (data[2] << 8) | data[3]);
135                         input_report_abs(input_dev, ABS_Y,
136                                          (data[4] << 8) | data[5]);
137                         input_report_abs(input_dev, ABS_PRESSURE,
138                                          (data[6] << 3) |
139                                          ((data[7] & 0xc0) >> 5) |
140                                          (data[1] & 0x01));
141                         input_report_abs(input_dev, ABS_TILT_X, data[7] & 0x3f);
142                         input_report_abs(input_dev, ABS_TILT_Y, data[8] & 0x7f);
143                         input_report_key(input_dev, BTN_STYLUS, data[1] & 0x02);
144                         input_report_key(input_dev, BTN_STYLUS2, data[1] & 0x04);
145                         break;
146                 }
147                 break;
148
149         case 0x0c:
150                 /* roll wheel */
151                 input_report_abs(input_dev, ABS_WHEEL, data[1]);
152                 input_report_key(input_dev, BTN_0, data[2]);
153                 for (i = 0; i < 8; i++)
154                         input_report_key(input_dev,
155                                          BTN_1 + i, data[3] & (1 << i));
156                 break;
157
158         default:
159                 dev_dbg(&dev->dev, "error packet  %02x ", data[0]);
160                 break;
161         }
162
163         input_sync(input_dev);
164 }
165
166 static void hanwang_irq(struct urb *urb)
167 {
168         struct hanwang *hanwang = urb->context;
169         struct usb_device *dev = hanwang->usbdev;
170         int retval;
171
172         switch (urb->status) {
173         case 0:
174                 /* success */;
175                 hanwang_parse_packet(hanwang);
176                 break;
177         case -ECONNRESET:
178         case -ENOENT:
179         case -ESHUTDOWN:
180                 /* this urb is terminated, clean up */
181                 dev_err(&dev->dev, "%s - urb shutting down with status: %d",
182                         __func__, urb->status);
183                 return;
184         default:
185                 dev_err(&dev->dev, "%s - nonzero urb status received: %d",
186                         __func__, urb->status);
187                 break;
188         }
189
190         retval = usb_submit_urb(urb, GFP_ATOMIC);
191         if (retval)
192                 dev_err(&dev->dev, "%s - usb_submit_urb failed with result %d",
193                         __func__, retval);
194 }
195
196 static int hanwang_open(struct input_dev *dev)
197 {
198         struct hanwang *hanwang = input_get_drvdata(dev);
199
200         hanwang->irq->dev = hanwang->usbdev;
201         if (usb_submit_urb(hanwang->irq, GFP_KERNEL))
202                 return -EIO;
203
204         return 0;
205 }
206
207 static void hanwang_close(struct input_dev *dev)
208 {
209         struct hanwang *hanwang = input_get_drvdata(dev);
210
211         usb_kill_urb(hanwang->irq);
212 }
213
214 static bool get_features(struct usb_device *dev, struct hanwang *hanwang)
215 {
216         int i;
217
218         for (i = 0; i < ARRAY_SIZE(features_array); i++) {
219                 if (le16_to_cpu(dev->descriptor.idProduct) ==
220                                 features_array[i].pid) {
221                         hanwang->features = &features_array[i];
222                         return true;
223                 }
224         }
225
226         return false;
227 }
228
229
230 static int hanwang_probe(struct usb_interface *intf, const struct usb_device_id *id)
231 {
232         struct usb_device *dev = interface_to_usbdev(intf);
233         struct usb_endpoint_descriptor *endpoint;
234         struct hanwang *hanwang;
235         struct input_dev *input_dev;
236         int error;
237         int i;
238
239         hanwang = kzalloc(sizeof(struct hanwang), GFP_KERNEL);
240         input_dev = input_allocate_device();
241         if (!hanwang || !input_dev) {
242                 error = -ENOMEM;
243                 goto fail1;
244         }
245
246         if (!get_features(dev, hanwang)) {
247                 error = -ENXIO;
248                 goto fail1;
249         }
250
251         hanwang->data = usb_alloc_coherent(dev, hanwang->features->pkg_len,
252                                         GFP_KERNEL, &hanwang->data_dma);
253         if (!hanwang->data) {
254                 error = -ENOMEM;
255                 goto fail1;
256         }
257
258         hanwang->irq = usb_alloc_urb(0, GFP_KERNEL);
259         if (!hanwang->irq) {
260                 error = -ENOMEM;
261                 goto fail2;
262         }
263
264         hanwang->usbdev = dev;
265         hanwang->dev = input_dev;
266
267         usb_make_path(dev, hanwang->phys, sizeof(hanwang->phys));
268         strlcat(hanwang->phys, "/input0", sizeof(hanwang->phys));
269
270         strlcpy(hanwang->name, hanwang->features->name, sizeof(hanwang->name));
271         input_dev->name = hanwang->name;
272         input_dev->phys = hanwang->phys;
273         usb_to_input_id(dev, &input_dev->id);
274         input_dev->dev.parent = &intf->dev;
275
276         input_set_drvdata(input_dev, hanwang);
277
278         input_dev->open = hanwang_open;
279         input_dev->close = hanwang_close;
280
281         for (i = 0; i < ARRAY_SIZE(hw_eventtypes); ++i)
282                 __set_bit(hw_eventtypes[i], input_dev->evbit);
283
284         for (i = 0; i < ARRAY_SIZE(hw_absevents); ++i)
285                 __set_bit(hw_absevents[i], input_dev->absbit);
286
287         for (i = 0; i < ARRAY_SIZE(hw_btnevents); ++i)
288                 __set_bit(hw_btnevents[i], input_dev->keybit);
289
290         input_set_abs_params(input_dev, ABS_X,
291                              0, hanwang->features->max_x, 4, 0);
292         input_set_abs_params(input_dev, ABS_Y,
293                              0, hanwang->features->max_y, 4, 0);
294         input_set_abs_params(input_dev, ABS_TILT_X,
295                              0, hanwang->features->max_tilt_x, 0, 0);
296         input_set_abs_params(input_dev, ABS_TILT_Y,
297                              0, hanwang->features->max_tilt_y, 0, 0);
298         input_set_abs_params(input_dev, ABS_PRESSURE,
299                              0, hanwang->features->max_pressure, 0, 0);
300
301         endpoint = &intf->cur_altsetting->endpoint[0].desc;
302         usb_fill_int_urb(hanwang->irq, dev,
303                         usb_rcvintpipe(dev, endpoint->bEndpointAddress),
304                         hanwang->data, hanwang->features->pkg_len,
305                         hanwang_irq, hanwang, endpoint->bInterval);
306         hanwang->irq->transfer_dma = hanwang->data_dma;
307         hanwang->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
308
309         error = input_register_device(hanwang->dev);
310         if (error)
311                 goto fail3;
312
313         usb_set_intfdata(intf, hanwang);
314
315         return 0;
316
317  fail3: usb_free_urb(hanwang->irq);
318  fail2: usb_free_coherent(dev, hanwang->features->pkg_len,
319                         hanwang->data, hanwang->data_dma);
320  fail1: input_free_device(input_dev);
321         kfree(hanwang);
322         return error;
323
324 }
325
326 static void hanwang_disconnect(struct usb_interface *intf)
327 {
328         struct hanwang *hanwang = usb_get_intfdata(intf);
329
330         input_unregister_device(hanwang->dev);
331         usb_free_urb(hanwang->irq);
332         usb_free_coherent(interface_to_usbdev(intf),
333                         hanwang->features->pkg_len, hanwang->data,
334                         hanwang->data_dma);
335         kfree(hanwang);
336         usb_set_intfdata(intf, NULL);
337 }
338
339 static const struct usb_device_id hanwang_ids[] = {
340         { HANWANG_TABLET_DEVICE(USB_VENDOR_ID_HANWANG, HANWANG_TABLET_INT_CLASS,
341                 HANWANG_TABLET_INT_SUB_CLASS, HANWANG_TABLET_INT_PROTOCOL) },
342         {}
343 };
344
345 MODULE_DEVICE_TABLE(usb, hanwang_ids);
346
347 static struct usb_driver hanwang_driver = {
348         .name           = "hanwang",
349         .probe          = hanwang_probe,
350         .disconnect     = hanwang_disconnect,
351         .id_table       = hanwang_ids,
352 };
353
354 static int __init hanwang_init(void)
355 {
356         return usb_register(&hanwang_driver);
357 }
358
359 static void __exit hanwang_exit(void)
360 {
361         usb_deregister(&hanwang_driver);
362 }
363
364 module_init(hanwang_init);
365 module_exit(hanwang_exit);