2 * USB Hanwang tablet support
4 * Copyright (c) 2010 Xing Wei <weixing@hanwang.com.cn>
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.
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.
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
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/usb/input.h>
31 #define DRIVER_AUTHOR "Xing Wei <weixing@hanwang.com.cn>"
32 #define DRIVER_DESC "USB Hanwang tablet driver"
33 #define DRIVER_LICENSE "GPL"
35 MODULE_AUTHOR(DRIVER_AUTHOR);
36 MODULE_DESCRIPTION(DRIVER_DESC);
37 MODULE_LICENSE(DRIVER_LICENSE);
39 #define USB_VENDOR_ID_HANWANG 0x0b57
40 #define HANWANG_TABLET_INT_CLASS 0x0003
41 #define HANWANG_TABLET_INT_SUB_CLASS 0x0001
42 #define HANWANG_TABLET_INT_PROTOCOL 0x0002
44 #define ART_MASTER_PKGLEN_MAX 10
47 #define STYLUS_DEVICE_ID 0x02
48 #define TOUCH_DEVICE_ID 0x03
49 #define CURSOR_DEVICE_ID 0x06
50 #define ERASER_DEVICE_ID 0x0A
51 #define PAD_DEVICE_ID 0x0F
53 /* match vendor and interface info */
54 #define HANWANG_TABLET_DEVICE(vend, cl, sc, pr) \
55 .match_flags = USB_DEVICE_ID_MATCH_VENDOR \
56 | USB_DEVICE_ID_MATCH_INT_INFO, \
58 .bInterfaceClass = (cl), \
59 .bInterfaceSubClass = (sc), \
60 .bInterfaceProtocol = (pr)
62 enum hanwang_tablet_type {
63 HANWANG_ART_MASTER_III,
64 HANWANG_ART_MASTER_HD,
65 HANWANG_ART_MASTER_II,
71 struct input_dev *dev;
72 struct usb_device *usbdev;
74 const struct hanwang_features *features;
75 unsigned int current_tool;
76 unsigned int current_id;
81 struct hanwang_features {
84 enum hanwang_tablet_type type;
93 static const struct hanwang_features features_array[] = {
94 { 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 },
102 { 0x8503, "Hanwang Art Master II", HANWANG_ART_MASTER_II,
103 ART_MASTER_PKGLEN_MAX, 0x27de, 0x1cfe, 0x3f, 0x7f, 1024 },
106 static const int hw_eventtypes[] = {
107 EV_KEY, EV_ABS, EV_MSC,
110 static const int hw_absevents[] = {
111 ABS_X, ABS_Y, ABS_TILT_X, ABS_TILT_Y, ABS_WHEEL,
112 ABS_RX, ABS_RY, ABS_PRESSURE, ABS_MISC,
115 static const int hw_btnevents[] = {
116 BTN_STYLUS, BTN_STYLUS2, BTN_TOOL_PEN, BTN_TOOL_RUBBER,
117 BTN_TOOL_MOUSE, BTN_TOOL_FINGER,
118 BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8,
121 static const int hw_mscevents[] = {
125 static void hanwang_parse_packet(struct hanwang *hanwang)
127 unsigned char *data = hanwang->data;
128 struct input_dev *input_dev = hanwang->dev;
129 struct usb_device *dev = hanwang->usbdev;
130 enum hanwang_tablet_type type = hanwang->features->type;
134 if (type == HANWANG_ART_MASTER_II) {
135 hanwang->current_tool = BTN_TOOL_PEN;
136 hanwang->current_id = STYLUS_DEVICE_ID;
140 case 0x02: /* data packet */
142 case 0x80: /* tool prox out */
143 if (type != HANWANG_ART_MASTER_II) {
144 hanwang->current_id = 0;
145 input_report_key(input_dev,
146 hanwang->current_tool, 0);
150 case 0x00: /* artmaster ii pen leave */
151 if (type == HANWANG_ART_MASTER_II) {
152 hanwang->current_id = 0;
153 input_report_key(input_dev,
154 hanwang->current_tool, 0);
158 case 0xc2: /* first time tool prox in */
159 switch (data[3] & 0xf0) {
160 case 0x20: /* art_master III */
161 case 0x30: /* art_master_HD */
162 hanwang->current_id = STYLUS_DEVICE_ID;
163 hanwang->current_tool = BTN_TOOL_PEN;
164 input_report_key(input_dev, BTN_TOOL_PEN, 1);
166 case 0xa0: /* art_master III */
167 case 0xb0: /* art_master_HD */
168 hanwang->current_id = ERASER_DEVICE_ID;
169 hanwang->current_tool = BTN_TOOL_RUBBER;
170 input_report_key(input_dev, BTN_TOOL_RUBBER, 1);
173 hanwang->current_id = 0;
175 "unknown tablet tool %02x\n", data[0]);
180 default: /* tool data packet */
182 case HANWANG_ART_MASTER_III:
184 ((data[7] & 0xc0) >> 5) |
188 case HANWANG_ART_MASTER_HD:
189 case HANWANG_ART_MASTER_II:
190 p = (data[7] >> 6) | (data[6] << 2);
198 input_report_abs(input_dev, ABS_X,
199 be16_to_cpup((__be16 *)&data[2]));
200 input_report_abs(input_dev, ABS_Y,
201 be16_to_cpup((__be16 *)&data[4]));
202 input_report_abs(input_dev, ABS_PRESSURE, p);
203 input_report_abs(input_dev, ABS_TILT_X, data[7] & 0x3f);
204 input_report_abs(input_dev, ABS_TILT_Y, data[8] & 0x7f);
205 input_report_key(input_dev, BTN_STYLUS, data[1] & 0x02);
207 if (type != HANWANG_ART_MASTER_II)
208 input_report_key(input_dev, BTN_STYLUS2,
211 input_report_key(input_dev, BTN_TOOL_PEN, 1);
216 input_report_abs(input_dev, ABS_MISC, hanwang->current_id);
217 input_event(input_dev, EV_MSC, MSC_SERIAL,
218 hanwang->features->pid);
223 hanwang->current_id = PAD_DEVICE_ID;
226 case HANWANG_ART_MASTER_III:
227 input_report_key(input_dev, BTN_TOOL_FINGER,
228 data[1] || data[2] || data[3]);
229 input_report_abs(input_dev, ABS_WHEEL, data[1]);
230 input_report_key(input_dev, BTN_0, data[2]);
231 for (i = 0; i < 8; i++)
232 input_report_key(input_dev,
233 BTN_1 + i, data[3] & (1 << i));
236 case HANWANG_ART_MASTER_HD:
237 input_report_key(input_dev, BTN_TOOL_FINGER, data[1] ||
238 data[2] || data[3] || data[4] ||
240 input_report_abs(input_dev, ABS_RX,
241 ((data[1] & 0x1f) << 8) | data[2]);
242 input_report_abs(input_dev, ABS_RY,
243 ((data[3] & 0x1f) << 8) | data[4]);
244 input_report_key(input_dev, BTN_0, data[5] & 0x01);
245 for (i = 0; i < 4; i++) {
246 input_report_key(input_dev,
247 BTN_1 + i, data[5] & (1 << i));
248 input_report_key(input_dev,
249 BTN_5 + i, data[6] & (1 << i));
253 case HANWANG_ART_MASTER_II:
254 dev_dbg(&dev->dev, "error packet %02x\n", data[0]);
258 input_report_abs(input_dev, ABS_MISC, hanwang->current_id);
259 input_event(input_dev, EV_MSC, MSC_SERIAL, 0xffffffff);
263 dev_dbg(&dev->dev, "error packet %02x\n", data[0]);
267 input_sync(input_dev);
270 static void hanwang_irq(struct urb *urb)
272 struct hanwang *hanwang = urb->context;
273 struct usb_device *dev = hanwang->usbdev;
276 switch (urb->status) {
279 hanwang_parse_packet(hanwang);
284 /* this urb is terminated, clean up */
285 dev_err(&dev->dev, "%s - urb shutting down with status: %d",
286 __func__, urb->status);
289 dev_err(&dev->dev, "%s - nonzero urb status received: %d",
290 __func__, urb->status);
294 retval = usb_submit_urb(urb, GFP_ATOMIC);
296 dev_err(&dev->dev, "%s - usb_submit_urb failed with result %d",
300 static int hanwang_open(struct input_dev *dev)
302 struct hanwang *hanwang = input_get_drvdata(dev);
304 hanwang->irq->dev = hanwang->usbdev;
305 if (usb_submit_urb(hanwang->irq, GFP_KERNEL))
311 static void hanwang_close(struct input_dev *dev)
313 struct hanwang *hanwang = input_get_drvdata(dev);
315 usb_kill_urb(hanwang->irq);
318 static bool get_features(struct usb_device *dev, struct hanwang *hanwang)
322 for (i = 0; i < ARRAY_SIZE(features_array); i++) {
323 if (le16_to_cpu(dev->descriptor.idProduct) ==
324 features_array[i].pid) {
325 hanwang->features = &features_array[i];
334 static int hanwang_probe(struct usb_interface *intf, const struct usb_device_id *id)
336 struct usb_device *dev = interface_to_usbdev(intf);
337 struct usb_endpoint_descriptor *endpoint;
338 struct hanwang *hanwang;
339 struct input_dev *input_dev;
343 hanwang = kzalloc(sizeof(struct hanwang), GFP_KERNEL);
344 input_dev = input_allocate_device();
345 if (!hanwang || !input_dev) {
350 if (!get_features(dev, hanwang)) {
355 hanwang->data = usb_alloc_coherent(dev, hanwang->features->pkg_len,
356 GFP_KERNEL, &hanwang->data_dma);
357 if (!hanwang->data) {
362 hanwang->irq = usb_alloc_urb(0, GFP_KERNEL);
368 hanwang->usbdev = dev;
369 hanwang->dev = input_dev;
371 usb_make_path(dev, hanwang->phys, sizeof(hanwang->phys));
372 strlcat(hanwang->phys, "/input0", sizeof(hanwang->phys));
374 strlcpy(hanwang->name, hanwang->features->name, sizeof(hanwang->name));
375 input_dev->name = hanwang->name;
376 input_dev->phys = hanwang->phys;
377 usb_to_input_id(dev, &input_dev->id);
378 input_dev->dev.parent = &intf->dev;
380 input_set_drvdata(input_dev, hanwang);
382 input_dev->open = hanwang_open;
383 input_dev->close = hanwang_close;
385 for (i = 0; i < ARRAY_SIZE(hw_eventtypes); ++i)
386 __set_bit(hw_eventtypes[i], input_dev->evbit);
388 for (i = 0; i < ARRAY_SIZE(hw_absevents); ++i)
389 __set_bit(hw_absevents[i], input_dev->absbit);
391 for (i = 0; i < ARRAY_SIZE(hw_btnevents); ++i)
392 __set_bit(hw_btnevents[i], input_dev->keybit);
394 for (i = 0; i < ARRAY_SIZE(hw_mscevents); ++i)
395 __set_bit(hw_mscevents[i], input_dev->mscbit);
397 input_set_abs_params(input_dev, ABS_X,
398 0, hanwang->features->max_x, 4, 0);
399 input_set_abs_params(input_dev, ABS_Y,
400 0, hanwang->features->max_y, 4, 0);
401 input_set_abs_params(input_dev, ABS_TILT_X,
402 0, hanwang->features->max_tilt_x, 0, 0);
403 input_set_abs_params(input_dev, ABS_TILT_Y,
404 0, hanwang->features->max_tilt_y, 0, 0);
405 input_set_abs_params(input_dev, ABS_PRESSURE,
406 0, hanwang->features->max_pressure, 0, 0);
408 endpoint = &intf->cur_altsetting->endpoint[0].desc;
409 usb_fill_int_urb(hanwang->irq, dev,
410 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
411 hanwang->data, hanwang->features->pkg_len,
412 hanwang_irq, hanwang, endpoint->bInterval);
413 hanwang->irq->transfer_dma = hanwang->data_dma;
414 hanwang->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
416 error = input_register_device(hanwang->dev);
420 usb_set_intfdata(intf, hanwang);
424 fail3: usb_free_urb(hanwang->irq);
425 fail2: usb_free_coherent(dev, hanwang->features->pkg_len,
426 hanwang->data, hanwang->data_dma);
427 fail1: input_free_device(input_dev);
433 static void hanwang_disconnect(struct usb_interface *intf)
435 struct hanwang *hanwang = usb_get_intfdata(intf);
437 input_unregister_device(hanwang->dev);
438 usb_free_urb(hanwang->irq);
439 usb_free_coherent(interface_to_usbdev(intf),
440 hanwang->features->pkg_len, hanwang->data,
443 usb_set_intfdata(intf, NULL);
446 static const struct usb_device_id hanwang_ids[] = {
447 { HANWANG_TABLET_DEVICE(USB_VENDOR_ID_HANWANG, HANWANG_TABLET_INT_CLASS,
448 HANWANG_TABLET_INT_SUB_CLASS, HANWANG_TABLET_INT_PROTOCOL) },
452 MODULE_DEVICE_TABLE(usb, hanwang_ids);
454 static struct usb_driver hanwang_driver = {
456 .probe = hanwang_probe,
457 .disconnect = hanwang_disconnect,
458 .id_table = hanwang_ids,
461 module_usb_driver(hanwang_driver);