blob: 6504b627b234e009477623cfc41bb4b0ad4ce79f [file] [log] [blame]
Xing Weibba53942010-09-01 23:25:35 -07001/*
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
36MODULE_AUTHOR(DRIVER_AUTHOR);
37MODULE_DESCRIPTION(DRIVER_DESC);
38MODULE_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
Xing Wei727eeb72010-09-29 18:25:20 -070045#define ART_MASTER_PKGLEN_MAX 10
Xing Weibba53942010-09-01 23:25:35 -070046
Xing Wei24dd3b52010-09-12 00:24:17 -070047/* 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
Xing Weibba53942010-09-01 23:25:35 -070054/* 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
Xing Wei727eeb72010-09-29 18:25:20 -070063enum hanwang_tablet_type {
64 HANWANG_ART_MASTER_III,
65 HANWANG_ART_MASTER_HD,
66};
67
Xing Weibba53942010-09-01 23:25:35 -070068struct hanwang {
69 unsigned char *data;
70 dma_addr_t data_dma;
71 struct input_dev *dev;
72 struct usb_device *usbdev;
73 struct urb *irq;
74 const struct hanwang_features *features;
75 unsigned int current_tool;
Xing Wei24dd3b52010-09-12 00:24:17 -070076 unsigned int current_id;
Xing Weibba53942010-09-01 23:25:35 -070077 char name[64];
78 char phys[32];
79};
80
81struct hanwang_features {
82 unsigned short pid;
83 char *name;
Xing Wei727eeb72010-09-29 18:25:20 -070084 enum hanwang_tablet_type type;
Xing Weibba53942010-09-01 23:25:35 -070085 int pkg_len;
86 int max_x;
87 int max_y;
88 int max_tilt_x;
89 int max_tilt_y;
90 int max_pressure;
91};
92
93static const struct hanwang_features features_array[] = {
Xing Wei727eeb72010-09-29 18:25:20 -070094 { 0x8528, "Hanwang Art Master III 0906", HANWANG_ART_MASTER_III,
95 ART_MASTER_PKGLEN_MAX, 0x5757, 0x3692, 0x3f, 0x7f, 2048 },
96 { 0x8529, "Hanwang Art Master III 0604", HANWANG_ART_MASTER_III,
97 ART_MASTER_PKGLEN_MAX, 0x3d84, 0x2672, 0x3f, 0x7f, 2048 },
98 { 0x852a, "Hanwang Art Master III 1308", HANWANG_ART_MASTER_III,
99 ART_MASTER_PKGLEN_MAX, 0x7f00, 0x4f60, 0x3f, 0x7f, 2048 },
100 { 0x8401, "Hanwang Art Master HD 5012", HANWANG_ART_MASTER_HD,
101 ART_MASTER_PKGLEN_MAX, 0x678e, 0x4150, 0x3f, 0x7f, 1024 },
Xing Weibba53942010-09-01 23:25:35 -0700102};
103
104static const int hw_eventtypes[] = {
Xing Wei24dd3b52010-09-12 00:24:17 -0700105 EV_KEY, EV_ABS, EV_MSC,
Xing Weibba53942010-09-01 23:25:35 -0700106};
107
108static const int hw_absevents[] = {
Xing Wei24dd3b52010-09-12 00:24:17 -0700109 ABS_X, ABS_Y, ABS_TILT_X, ABS_TILT_Y, ABS_WHEEL,
Xing Wei727eeb72010-09-29 18:25:20 -0700110 ABS_RX, ABS_RY, ABS_PRESSURE, ABS_MISC,
Xing Weibba53942010-09-01 23:25:35 -0700111};
112
113static const int hw_btnevents[] = {
Xing Wei24dd3b52010-09-12 00:24:17 -0700114 BTN_STYLUS, BTN_STYLUS2, BTN_TOOL_PEN, BTN_TOOL_RUBBER,
115 BTN_TOOL_MOUSE, BTN_TOOL_FINGER,
116 BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8,
117};
118
119static const int hw_mscevents[] = {
120 MSC_SERIAL,
Xing Weibba53942010-09-01 23:25:35 -0700121};
122
123static void hanwang_parse_packet(struct hanwang *hanwang)
124{
125 unsigned char *data = hanwang->data;
126 struct input_dev *input_dev = hanwang->dev;
127 struct usb_device *dev = hanwang->usbdev;
Xing Wei727eeb72010-09-29 18:25:20 -0700128 enum hanwang_tablet_type type = hanwang->features->type;
Xing Weibba53942010-09-01 23:25:35 -0700129 int i;
Xing Wei727eeb72010-09-29 18:25:20 -0700130 u16 x, y, p;
Xing Weibba53942010-09-01 23:25:35 -0700131
132 switch (data[0]) {
133 case 0x02: /* data packet */
134 switch (data[1]) {
135 case 0x80: /* tool prox out */
Xing Wei24dd3b52010-09-12 00:24:17 -0700136 hanwang->current_id = 0;
Xing Weibba53942010-09-01 23:25:35 -0700137 input_report_key(input_dev, hanwang->current_tool, 0);
138 break;
139
140 case 0xc2: /* first time tool prox in */
141 switch (data[3] & 0xf0) {
Xing Wei727eeb72010-09-29 18:25:20 -0700142 case 0x20: /* art_master III */
143 case 0x30: /* art_master_HD */
Xing Wei24dd3b52010-09-12 00:24:17 -0700144 hanwang->current_id = STYLUS_DEVICE_ID;
Xing Weibba53942010-09-01 23:25:35 -0700145 hanwang->current_tool = BTN_TOOL_PEN;
146 input_report_key(input_dev, BTN_TOOL_PEN, 1);
147 break;
Xing Wei727eeb72010-09-29 18:25:20 -0700148 case 0xa0: /* art_master III */
149 case 0xb0: /* art_master_HD */
Xing Wei24dd3b52010-09-12 00:24:17 -0700150 hanwang->current_id = ERASER_DEVICE_ID;
Xing Weibba53942010-09-01 23:25:35 -0700151 hanwang->current_tool = BTN_TOOL_RUBBER;
152 input_report_key(input_dev, BTN_TOOL_RUBBER, 1);
153 break;
154 default:
Xing Wei24dd3b52010-09-12 00:24:17 -0700155 hanwang->current_id = 0;
Xing Weibba53942010-09-01 23:25:35 -0700156 dev_dbg(&dev->dev,
157 "unknown tablet tool %02x ", data[0]);
158 break;
159 }
160 break;
161
162 default: /* tool data packet */
Xing Wei727eeb72010-09-29 18:25:20 -0700163 x = (data[2] << 8) | data[3];
164 y = (data[4] << 8) | data[5];
165
166 switch (type) {
167 case HANWANG_ART_MASTER_III:
168 p = (data[6] << 3) |
169 ((data[7] & 0xc0) >> 5) |
170 (data[1] & 0x01);
171 break;
172
173 case HANWANG_ART_MASTER_HD:
174 p = (data[7] >> 6) | (data[6] << 2);
175 break;
176
177 default:
178 p = 0;
179 break;
180 }
181
Xing Weibba53942010-09-01 23:25:35 -0700182 input_report_abs(input_dev, ABS_X,
Xing Wei727eeb72010-09-29 18:25:20 -0700183 le16_to_cpup((__le16 *)&x));
Xing Weibba53942010-09-01 23:25:35 -0700184 input_report_abs(input_dev, ABS_Y,
Xing Wei727eeb72010-09-29 18:25:20 -0700185 le16_to_cpup((__le16 *)&y));
Xing Weibba53942010-09-01 23:25:35 -0700186 input_report_abs(input_dev, ABS_PRESSURE,
Xing Wei727eeb72010-09-29 18:25:20 -0700187 le16_to_cpup((__le16 *)&p));
Xing Weibba53942010-09-01 23:25:35 -0700188 input_report_abs(input_dev, ABS_TILT_X, data[7] & 0x3f);
189 input_report_abs(input_dev, ABS_TILT_Y, data[8] & 0x7f);
190 input_report_key(input_dev, BTN_STYLUS, data[1] & 0x02);
191 input_report_key(input_dev, BTN_STYLUS2, data[1] & 0x04);
192 break;
193 }
Xing Wei24dd3b52010-09-12 00:24:17 -0700194 input_report_abs(input_dev, ABS_MISC, hanwang->current_id);
195 input_event(input_dev, EV_MSC, MSC_SERIAL,
196 hanwang->features->pid);
Xing Weibba53942010-09-01 23:25:35 -0700197 break;
198
199 case 0x0c:
200 /* roll wheel */
Xing Wei24dd3b52010-09-12 00:24:17 -0700201 hanwang->current_id = PAD_DEVICE_ID;
Xing Wei727eeb72010-09-29 18:25:20 -0700202
203 switch (type) {
204 case HANWANG_ART_MASTER_III:
205 input_report_key(input_dev, BTN_TOOL_FINGER, data[1] ||
206 data[2] || data[3]);
207 input_report_abs(input_dev, ABS_WHEEL, data[1]);
208 input_report_key(input_dev, BTN_0, data[2]);
209 for (i = 0; i < 8; i++)
210 input_report_key(input_dev,
Xing Weibba53942010-09-01 23:25:35 -0700211 BTN_1 + i, data[3] & (1 << i));
Xing Wei727eeb72010-09-29 18:25:20 -0700212 break;
213
214 case HANWANG_ART_MASTER_HD:
215 input_report_key(input_dev, BTN_TOOL_FINGER, data[1] ||
216 data[2] || data[3] || data[4] ||
217 data[5] || data[6]);
218 input_report_abs(input_dev, ABS_RX,
219 ((data[1] & 0x1f) << 8) | data[2]);
220 input_report_abs(input_dev, ABS_RY,
221 ((data[3] & 0x1f) << 8) | data[4]);
222 input_report_key(input_dev, BTN_0, data[5] & 0x01);
223 for (i = 0; i < 4; i++) {
224 input_report_key(input_dev,
225 BTN_1 + i, data[5] & (1 << i));
226 input_report_key(input_dev,
227 BTN_5 + i, data[6] & (1 << i));
228 }
229 break;
230 }
231
Xing Wei24dd3b52010-09-12 00:24:17 -0700232 input_report_abs(input_dev, ABS_MISC, hanwang->current_id);
233 input_event(input_dev, EV_MSC, MSC_SERIAL, 0xffffffff);
Xing Weibba53942010-09-01 23:25:35 -0700234 break;
235
236 default:
237 dev_dbg(&dev->dev, "error packet %02x ", data[0]);
238 break;
239 }
240
241 input_sync(input_dev);
242}
243
244static void hanwang_irq(struct urb *urb)
245{
246 struct hanwang *hanwang = urb->context;
247 struct usb_device *dev = hanwang->usbdev;
248 int retval;
249
250 switch (urb->status) {
251 case 0:
252 /* success */;
253 hanwang_parse_packet(hanwang);
254 break;
255 case -ECONNRESET:
256 case -ENOENT:
257 case -ESHUTDOWN:
258 /* this urb is terminated, clean up */
259 dev_err(&dev->dev, "%s - urb shutting down with status: %d",
260 __func__, urb->status);
261 return;
262 default:
263 dev_err(&dev->dev, "%s - nonzero urb status received: %d",
264 __func__, urb->status);
265 break;
266 }
267
268 retval = usb_submit_urb(urb, GFP_ATOMIC);
269 if (retval)
270 dev_err(&dev->dev, "%s - usb_submit_urb failed with result %d",
271 __func__, retval);
272}
273
274static int hanwang_open(struct input_dev *dev)
275{
276 struct hanwang *hanwang = input_get_drvdata(dev);
277
278 hanwang->irq->dev = hanwang->usbdev;
279 if (usb_submit_urb(hanwang->irq, GFP_KERNEL))
280 return -EIO;
281
282 return 0;
283}
284
285static void hanwang_close(struct input_dev *dev)
286{
287 struct hanwang *hanwang = input_get_drvdata(dev);
288
289 usb_kill_urb(hanwang->irq);
290}
291
292static bool get_features(struct usb_device *dev, struct hanwang *hanwang)
293{
294 int i;
295
296 for (i = 0; i < ARRAY_SIZE(features_array); i++) {
297 if (le16_to_cpu(dev->descriptor.idProduct) ==
298 features_array[i].pid) {
299 hanwang->features = &features_array[i];
300 return true;
301 }
302 }
303
304 return false;
305}
306
307
308static int hanwang_probe(struct usb_interface *intf, const struct usb_device_id *id)
309{
310 struct usb_device *dev = interface_to_usbdev(intf);
311 struct usb_endpoint_descriptor *endpoint;
312 struct hanwang *hanwang;
313 struct input_dev *input_dev;
314 int error;
315 int i;
316
317 hanwang = kzalloc(sizeof(struct hanwang), GFP_KERNEL);
318 input_dev = input_allocate_device();
319 if (!hanwang || !input_dev) {
320 error = -ENOMEM;
321 goto fail1;
322 }
323
324 if (!get_features(dev, hanwang)) {
325 error = -ENXIO;
326 goto fail1;
327 }
328
329 hanwang->data = usb_alloc_coherent(dev, hanwang->features->pkg_len,
330 GFP_KERNEL, &hanwang->data_dma);
331 if (!hanwang->data) {
332 error = -ENOMEM;
333 goto fail1;
334 }
335
336 hanwang->irq = usb_alloc_urb(0, GFP_KERNEL);
337 if (!hanwang->irq) {
338 error = -ENOMEM;
339 goto fail2;
340 }
341
342 hanwang->usbdev = dev;
343 hanwang->dev = input_dev;
344
345 usb_make_path(dev, hanwang->phys, sizeof(hanwang->phys));
346 strlcat(hanwang->phys, "/input0", sizeof(hanwang->phys));
347
348 strlcpy(hanwang->name, hanwang->features->name, sizeof(hanwang->name));
349 input_dev->name = hanwang->name;
350 input_dev->phys = hanwang->phys;
351 usb_to_input_id(dev, &input_dev->id);
352 input_dev->dev.parent = &intf->dev;
353
354 input_set_drvdata(input_dev, hanwang);
355
356 input_dev->open = hanwang_open;
357 input_dev->close = hanwang_close;
358
359 for (i = 0; i < ARRAY_SIZE(hw_eventtypes); ++i)
360 __set_bit(hw_eventtypes[i], input_dev->evbit);
361
362 for (i = 0; i < ARRAY_SIZE(hw_absevents); ++i)
363 __set_bit(hw_absevents[i], input_dev->absbit);
364
365 for (i = 0; i < ARRAY_SIZE(hw_btnevents); ++i)
366 __set_bit(hw_btnevents[i], input_dev->keybit);
367
Xing Wei24dd3b52010-09-12 00:24:17 -0700368 for (i = 0; i < ARRAY_SIZE(hw_mscevents); ++i)
369 __set_bit(hw_mscevents[i], input_dev->mscbit);
370
Xing Weibba53942010-09-01 23:25:35 -0700371 input_set_abs_params(input_dev, ABS_X,
372 0, hanwang->features->max_x, 4, 0);
373 input_set_abs_params(input_dev, ABS_Y,
374 0, hanwang->features->max_y, 4, 0);
375 input_set_abs_params(input_dev, ABS_TILT_X,
376 0, hanwang->features->max_tilt_x, 0, 0);
377 input_set_abs_params(input_dev, ABS_TILT_Y,
378 0, hanwang->features->max_tilt_y, 0, 0);
379 input_set_abs_params(input_dev, ABS_PRESSURE,
380 0, hanwang->features->max_pressure, 0, 0);
381
382 endpoint = &intf->cur_altsetting->endpoint[0].desc;
383 usb_fill_int_urb(hanwang->irq, dev,
384 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
385 hanwang->data, hanwang->features->pkg_len,
386 hanwang_irq, hanwang, endpoint->bInterval);
387 hanwang->irq->transfer_dma = hanwang->data_dma;
388 hanwang->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
389
390 error = input_register_device(hanwang->dev);
391 if (error)
392 goto fail3;
393
394 usb_set_intfdata(intf, hanwang);
395
396 return 0;
397
398 fail3: usb_free_urb(hanwang->irq);
399 fail2: usb_free_coherent(dev, hanwang->features->pkg_len,
400 hanwang->data, hanwang->data_dma);
401 fail1: input_free_device(input_dev);
402 kfree(hanwang);
403 return error;
404
405}
406
407static void hanwang_disconnect(struct usb_interface *intf)
408{
409 struct hanwang *hanwang = usb_get_intfdata(intf);
410
411 input_unregister_device(hanwang->dev);
412 usb_free_urb(hanwang->irq);
413 usb_free_coherent(interface_to_usbdev(intf),
414 hanwang->features->pkg_len, hanwang->data,
415 hanwang->data_dma);
416 kfree(hanwang);
417 usb_set_intfdata(intf, NULL);
418}
419
420static const struct usb_device_id hanwang_ids[] = {
421 { HANWANG_TABLET_DEVICE(USB_VENDOR_ID_HANWANG, HANWANG_TABLET_INT_CLASS,
422 HANWANG_TABLET_INT_SUB_CLASS, HANWANG_TABLET_INT_PROTOCOL) },
423 {}
424};
425
426MODULE_DEVICE_TABLE(usb, hanwang_ids);
427
428static struct usb_driver hanwang_driver = {
429 .name = "hanwang",
430 .probe = hanwang_probe,
431 .disconnect = hanwang_disconnect,
432 .id_table = hanwang_ids,
433};
434
435static int __init hanwang_init(void)
436{
437 return usb_register(&hanwang_driver);
438}
439
440static void __exit hanwang_exit(void)
441{
442 usb_deregister(&hanwang_driver);
443}
444
445module_init(hanwang_init);
446module_exit(hanwang_exit);