]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/input/tablet/hanwang.c
Merge remote-tracking branch 'wireless-next/master' into iwlwifi-next
[karo-tx-linux.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_MASTER_PKGLEN_MAX   10
46
47 /* device IDs */
48 #define STYLUS_DEVICE_ID        0x02
49 #define TOUCH_DEVICE_ID         0x03
50 #define CURSOR_DEVICE_ID        0x06
51 #define ERASER_DEVICE_ID        0x0A
52 #define PAD_DEVICE_ID           0x0F
53
54 /* match vendor and interface info  */
55 #define HANWANG_TABLET_DEVICE(vend, cl, sc, pr) \
56         .match_flags = USB_DEVICE_ID_MATCH_VENDOR \
57                 | USB_DEVICE_ID_MATCH_INT_INFO, \
58         .idVendor = (vend), \
59         .bInterfaceClass = (cl), \
60         .bInterfaceSubClass = (sc), \
61         .bInterfaceProtocol = (pr)
62
63 enum hanwang_tablet_type {
64         HANWANG_ART_MASTER_III,
65         HANWANG_ART_MASTER_HD,
66         HANWANG_ART_MASTER_II,
67 };
68
69 struct hanwang {
70         unsigned char *data;
71         dma_addr_t data_dma;
72         struct input_dev *dev;
73         struct usb_device *usbdev;
74         struct urb *irq;
75         const struct hanwang_features *features;
76         unsigned int current_tool;
77         unsigned int current_id;
78         char name[64];
79         char phys[32];
80 };
81
82 struct hanwang_features {
83         unsigned short pid;
84         char *name;
85         enum hanwang_tablet_type type;
86         int pkg_len;
87         int max_x;
88         int max_y;
89         int max_tilt_x;
90         int max_tilt_y;
91         int max_pressure;
92 };
93
94 static const struct hanwang_features features_array[] = {
95         { 0x8528, "Hanwang Art Master III 0906", HANWANG_ART_MASTER_III,
96           ART_MASTER_PKGLEN_MAX, 0x5757, 0x3692, 0x3f, 0x7f, 2048 },
97         { 0x8529, "Hanwang Art Master III 0604", HANWANG_ART_MASTER_III,
98           ART_MASTER_PKGLEN_MAX, 0x3d84, 0x2672, 0x3f, 0x7f, 2048 },
99         { 0x852a, "Hanwang Art Master III 1308", HANWANG_ART_MASTER_III,
100           ART_MASTER_PKGLEN_MAX, 0x7f00, 0x4f60, 0x3f, 0x7f, 2048 },
101         { 0x8401, "Hanwang Art Master HD 5012", HANWANG_ART_MASTER_HD,
102           ART_MASTER_PKGLEN_MAX, 0x678e, 0x4150, 0x3f, 0x7f, 1024 },
103         { 0x8503, "Hanwang Art Master II", HANWANG_ART_MASTER_II,
104           ART_MASTER_PKGLEN_MAX, 0x27de, 0x1cfe, 0x3f, 0x7f, 1024 },
105 };
106
107 static const int hw_eventtypes[] = {
108         EV_KEY, EV_ABS, EV_MSC,
109 };
110
111 static const int hw_absevents[] = {
112         ABS_X, ABS_Y, ABS_TILT_X, ABS_TILT_Y, ABS_WHEEL,
113         ABS_RX, ABS_RY, ABS_PRESSURE, ABS_MISC,
114 };
115
116 static const int hw_btnevents[] = {
117         BTN_STYLUS, BTN_STYLUS2, BTN_TOOL_PEN, BTN_TOOL_RUBBER,
118         BTN_TOOL_MOUSE, BTN_TOOL_FINGER,
119         BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8,
120 };
121
122 static const int hw_mscevents[] = {
123         MSC_SERIAL,
124 };
125
126 static void hanwang_parse_packet(struct hanwang *hanwang)
127 {
128         unsigned char *data = hanwang->data;
129         struct input_dev *input_dev = hanwang->dev;
130         struct usb_device *dev = hanwang->usbdev;
131         enum hanwang_tablet_type type = hanwang->features->type;
132         int i;
133         u16 p;
134
135         if (type == HANWANG_ART_MASTER_II) {
136                 hanwang->current_tool = BTN_TOOL_PEN;
137                 hanwang->current_id = STYLUS_DEVICE_ID;
138         }
139
140         switch (data[0]) {
141         case 0x02:      /* data packet */
142                 switch (data[1]) {
143                 case 0x80:      /* tool prox out */
144                         if (type != HANWANG_ART_MASTER_II) {
145                                 hanwang->current_id = 0;
146                                 input_report_key(input_dev,
147                                                  hanwang->current_tool, 0);
148                         }
149                         break;
150
151                 case 0x00:      /* artmaster ii pen leave */
152                         if (type == HANWANG_ART_MASTER_II) {
153                                 hanwang->current_id = 0;
154                                 input_report_key(input_dev,
155                                                  hanwang->current_tool, 0);
156                         }
157                         break;
158
159                 case 0xc2:      /* first time tool prox in */
160                         switch (data[3] & 0xf0) {
161                         case 0x20:      /* art_master III */
162                         case 0x30:      /* art_master_HD */
163                                 hanwang->current_id = STYLUS_DEVICE_ID;
164                                 hanwang->current_tool = BTN_TOOL_PEN;
165                                 input_report_key(input_dev, BTN_TOOL_PEN, 1);
166                                 break;
167                         case 0xa0:      /* art_master III */
168                         case 0xb0:      /* art_master_HD */
169                                 hanwang->current_id = ERASER_DEVICE_ID;
170                                 hanwang->current_tool = BTN_TOOL_RUBBER;
171                                 input_report_key(input_dev, BTN_TOOL_RUBBER, 1);
172                                 break;
173                         default:
174                                 hanwang->current_id = 0;
175                                 dev_dbg(&dev->dev,
176                                         "unknown tablet tool %02x\n", data[0]);
177                                 break;
178                         }
179                         break;
180
181                 default:        /* tool data packet */
182                         switch (type) {
183                         case HANWANG_ART_MASTER_III:
184                                 p = (data[6] << 3) |
185                                     ((data[7] & 0xc0) >> 5) |
186                                     (data[1] & 0x01);
187                                 break;
188
189                         case HANWANG_ART_MASTER_HD:
190                         case HANWANG_ART_MASTER_II:
191                                 p = (data[7] >> 6) | (data[6] << 2);
192                                 break;
193
194                         default:
195                                 p = 0;
196                                 break;
197                         }
198
199                         input_report_abs(input_dev, ABS_X,
200                                          be16_to_cpup((__be16 *)&data[2]));
201                         input_report_abs(input_dev, ABS_Y,
202                                          be16_to_cpup((__be16 *)&data[4]));
203                         input_report_abs(input_dev, ABS_PRESSURE, p);
204                         input_report_abs(input_dev, ABS_TILT_X, data[7] & 0x3f);
205                         input_report_abs(input_dev, ABS_TILT_Y, data[8] & 0x7f);
206                         input_report_key(input_dev, BTN_STYLUS, data[1] & 0x02);
207
208                         if (type != HANWANG_ART_MASTER_II)
209                                 input_report_key(input_dev, BTN_STYLUS2,
210                                                  data[1] & 0x04);
211                         else
212                                 input_report_key(input_dev, BTN_TOOL_PEN, 1);
213
214                         break;
215                 }
216
217                 input_report_abs(input_dev, ABS_MISC, hanwang->current_id);
218                 input_event(input_dev, EV_MSC, MSC_SERIAL,
219                                 hanwang->features->pid);
220                 break;
221
222         case 0x0c:
223                 /* roll wheel */
224                 hanwang->current_id = PAD_DEVICE_ID;
225
226                 switch (type) {
227                 case HANWANG_ART_MASTER_III:
228                         input_report_key(input_dev, BTN_TOOL_FINGER,
229                                          data[1] || data[2] || data[3]);
230                         input_report_abs(input_dev, ABS_WHEEL, data[1]);
231                         input_report_key(input_dev, BTN_0, data[2]);
232                         for (i = 0; i < 8; i++)
233                                 input_report_key(input_dev,
234                                          BTN_1 + i, data[3] & (1 << i));
235                         break;
236
237                 case HANWANG_ART_MASTER_HD:
238                         input_report_key(input_dev, BTN_TOOL_FINGER, data[1] ||
239                                         data[2] || data[3] || data[4] ||
240                                         data[5] || data[6]);
241                         input_report_abs(input_dev, ABS_RX,
242                                         ((data[1] & 0x1f) << 8) | data[2]);
243                         input_report_abs(input_dev, ABS_RY,
244                                         ((data[3] & 0x1f) << 8) | data[4]);
245                         input_report_key(input_dev, BTN_0, data[5] & 0x01);
246                         for (i = 0; i < 4; i++) {
247                                 input_report_key(input_dev,
248                                          BTN_1 + i, data[5] & (1 << i));
249                                 input_report_key(input_dev,
250                                          BTN_5 + i, data[6] & (1 << i));
251                         }
252                         break;
253
254                 case HANWANG_ART_MASTER_II:
255                         dev_dbg(&dev->dev, "error packet  %02x\n", data[0]);
256                         return;
257                 }
258
259                 input_report_abs(input_dev, ABS_MISC, hanwang->current_id);
260                 input_event(input_dev, EV_MSC, MSC_SERIAL, 0xffffffff);
261                 break;
262
263         default:
264                 dev_dbg(&dev->dev, "error packet  %02x\n", data[0]);
265                 break;
266         }
267
268         input_sync(input_dev);
269 }
270
271 static void hanwang_irq(struct urb *urb)
272 {
273         struct hanwang *hanwang = urb->context;
274         struct usb_device *dev = hanwang->usbdev;
275         int retval;
276
277         switch (urb->status) {
278         case 0:
279                 /* success */;
280                 hanwang_parse_packet(hanwang);
281                 break;
282         case -ECONNRESET:
283         case -ENOENT:
284         case -ESHUTDOWN:
285                 /* this urb is terminated, clean up */
286                 dev_err(&dev->dev, "%s - urb shutting down with status: %d",
287                         __func__, urb->status);
288                 return;
289         default:
290                 dev_err(&dev->dev, "%s - nonzero urb status received: %d",
291                         __func__, urb->status);
292                 break;
293         }
294
295         retval = usb_submit_urb(urb, GFP_ATOMIC);
296         if (retval)
297                 dev_err(&dev->dev, "%s - usb_submit_urb failed with result %d",
298                         __func__, retval);
299 }
300
301 static int hanwang_open(struct input_dev *dev)
302 {
303         struct hanwang *hanwang = input_get_drvdata(dev);
304
305         hanwang->irq->dev = hanwang->usbdev;
306         if (usb_submit_urb(hanwang->irq, GFP_KERNEL))
307                 return -EIO;
308
309         return 0;
310 }
311
312 static void hanwang_close(struct input_dev *dev)
313 {
314         struct hanwang *hanwang = input_get_drvdata(dev);
315
316         usb_kill_urb(hanwang->irq);
317 }
318
319 static bool get_features(struct usb_device *dev, struct hanwang *hanwang)
320 {
321         int i;
322
323         for (i = 0; i < ARRAY_SIZE(features_array); i++) {
324                 if (le16_to_cpu(dev->descriptor.idProduct) ==
325                                 features_array[i].pid) {
326                         hanwang->features = &features_array[i];
327                         return true;
328                 }
329         }
330
331         return false;
332 }
333
334
335 static int hanwang_probe(struct usb_interface *intf, const struct usb_device_id *id)
336 {
337         struct usb_device *dev = interface_to_usbdev(intf);
338         struct usb_endpoint_descriptor *endpoint;
339         struct hanwang *hanwang;
340         struct input_dev *input_dev;
341         int error;
342         int i;
343
344         hanwang = kzalloc(sizeof(struct hanwang), GFP_KERNEL);
345         input_dev = input_allocate_device();
346         if (!hanwang || !input_dev) {
347                 error = -ENOMEM;
348                 goto fail1;
349         }
350
351         if (!get_features(dev, hanwang)) {
352                 error = -ENXIO;
353                 goto fail1;
354         }
355
356         hanwang->data = usb_alloc_coherent(dev, hanwang->features->pkg_len,
357                                         GFP_KERNEL, &hanwang->data_dma);
358         if (!hanwang->data) {
359                 error = -ENOMEM;
360                 goto fail1;
361         }
362
363         hanwang->irq = usb_alloc_urb(0, GFP_KERNEL);
364         if (!hanwang->irq) {
365                 error = -ENOMEM;
366                 goto fail2;
367         }
368
369         hanwang->usbdev = dev;
370         hanwang->dev = input_dev;
371
372         usb_make_path(dev, hanwang->phys, sizeof(hanwang->phys));
373         strlcat(hanwang->phys, "/input0", sizeof(hanwang->phys));
374
375         strlcpy(hanwang->name, hanwang->features->name, sizeof(hanwang->name));
376         input_dev->name = hanwang->name;
377         input_dev->phys = hanwang->phys;
378         usb_to_input_id(dev, &input_dev->id);
379         input_dev->dev.parent = &intf->dev;
380
381         input_set_drvdata(input_dev, hanwang);
382
383         input_dev->open = hanwang_open;
384         input_dev->close = hanwang_close;
385
386         for (i = 0; i < ARRAY_SIZE(hw_eventtypes); ++i)
387                 __set_bit(hw_eventtypes[i], input_dev->evbit);
388
389         for (i = 0; i < ARRAY_SIZE(hw_absevents); ++i)
390                 __set_bit(hw_absevents[i], input_dev->absbit);
391
392         for (i = 0; i < ARRAY_SIZE(hw_btnevents); ++i)
393                 __set_bit(hw_btnevents[i], input_dev->keybit);
394
395         for (i = 0; i < ARRAY_SIZE(hw_mscevents); ++i)
396                 __set_bit(hw_mscevents[i], input_dev->mscbit);
397
398         input_set_abs_params(input_dev, ABS_X,
399                              0, hanwang->features->max_x, 4, 0);
400         input_set_abs_params(input_dev, ABS_Y,
401                              0, hanwang->features->max_y, 4, 0);
402         input_set_abs_params(input_dev, ABS_TILT_X,
403                              0, hanwang->features->max_tilt_x, 0, 0);
404         input_set_abs_params(input_dev, ABS_TILT_Y,
405                              0, hanwang->features->max_tilt_y, 0, 0);
406         input_set_abs_params(input_dev, ABS_PRESSURE,
407                              0, hanwang->features->max_pressure, 0, 0);
408
409         endpoint = &intf->cur_altsetting->endpoint[0].desc;
410         usb_fill_int_urb(hanwang->irq, dev,
411                         usb_rcvintpipe(dev, endpoint->bEndpointAddress),
412                         hanwang->data, hanwang->features->pkg_len,
413                         hanwang_irq, hanwang, endpoint->bInterval);
414         hanwang->irq->transfer_dma = hanwang->data_dma;
415         hanwang->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
416
417         error = input_register_device(hanwang->dev);
418         if (error)
419                 goto fail3;
420
421         usb_set_intfdata(intf, hanwang);
422
423         return 0;
424
425  fail3: usb_free_urb(hanwang->irq);
426  fail2: usb_free_coherent(dev, hanwang->features->pkg_len,
427                         hanwang->data, hanwang->data_dma);
428  fail1: input_free_device(input_dev);
429         kfree(hanwang);
430         return error;
431
432 }
433
434 static void hanwang_disconnect(struct usb_interface *intf)
435 {
436         struct hanwang *hanwang = usb_get_intfdata(intf);
437
438         input_unregister_device(hanwang->dev);
439         usb_free_urb(hanwang->irq);
440         usb_free_coherent(interface_to_usbdev(intf),
441                         hanwang->features->pkg_len, hanwang->data,
442                         hanwang->data_dma);
443         kfree(hanwang);
444         usb_set_intfdata(intf, NULL);
445 }
446
447 static const struct usb_device_id hanwang_ids[] = {
448         { HANWANG_TABLET_DEVICE(USB_VENDOR_ID_HANWANG, HANWANG_TABLET_INT_CLASS,
449                 HANWANG_TABLET_INT_SUB_CLASS, HANWANG_TABLET_INT_PROTOCOL) },
450         {}
451 };
452
453 MODULE_DEVICE_TABLE(usb, hanwang_ids);
454
455 static struct usb_driver hanwang_driver = {
456         .name           = "hanwang",
457         .probe          = hanwang_probe,
458         .disconnect     = hanwang_disconnect,
459         .id_table       = hanwang_ids,
460 };
461
462 module_usb_driver(hanwang_driver);