1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * USB Hanwang tablet support
5 * Copyright (c) 2010 Xing Wei <weixing@hanwang.com.cn>
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/usb/input.h>
14 MODULE_AUTHOR("Xing Wei <weixing@hanwang.com.cn>");
15 MODULE_DESCRIPTION("USB Hanwang tablet driver");
16 MODULE_LICENSE("GPL");
18 #define USB_VENDOR_ID_HANWANG 0x0b57
19 #define HANWANG_TABLET_INT_CLASS 0x0003
20 #define HANWANG_TABLET_INT_SUB_CLASS 0x0001
21 #define HANWANG_TABLET_INT_PROTOCOL 0x0002
23 #define ART_MASTER_PKGLEN_MAX 10
26 #define STYLUS_DEVICE_ID 0x02
27 #define TOUCH_DEVICE_ID 0x03
28 #define CURSOR_DEVICE_ID 0x06
29 #define ERASER_DEVICE_ID 0x0A
30 #define PAD_DEVICE_ID 0x0F
32 /* match vendor and interface info */
33 #define HANWANG_TABLET_DEVICE(vend, cl, sc, pr) \
34 .match_flags = USB_DEVICE_ID_MATCH_VENDOR \
35 | USB_DEVICE_ID_MATCH_INT_INFO, \
37 .bInterfaceClass = (cl), \
38 .bInterfaceSubClass = (sc), \
39 .bInterfaceProtocol = (pr)
41 enum hanwang_tablet_type
{
42 HANWANG_ART_MASTER_III
,
43 HANWANG_ART_MASTER_HD
,
44 HANWANG_ART_MASTER_II
,
50 struct input_dev
*dev
;
51 struct usb_device
*usbdev
;
53 const struct hanwang_features
*features
;
54 unsigned int current_tool
;
55 unsigned int current_id
;
60 struct hanwang_features
{
63 enum hanwang_tablet_type type
;
72 static const struct hanwang_features features_array
[] = {
73 { 0x8528, "Hanwang Art Master III 0906", HANWANG_ART_MASTER_III
,
74 ART_MASTER_PKGLEN_MAX
, 0x5757, 0x3692, 0x3f, 0x7f, 2048 },
75 { 0x8529, "Hanwang Art Master III 0604", HANWANG_ART_MASTER_III
,
76 ART_MASTER_PKGLEN_MAX
, 0x3d84, 0x2672, 0x3f, 0x7f, 2048 },
77 { 0x852a, "Hanwang Art Master III 1308", HANWANG_ART_MASTER_III
,
78 ART_MASTER_PKGLEN_MAX
, 0x7f00, 0x4f60, 0x3f, 0x7f, 2048 },
79 { 0x8401, "Hanwang Art Master HD 5012", HANWANG_ART_MASTER_HD
,
80 ART_MASTER_PKGLEN_MAX
, 0x678e, 0x4150, 0x3f, 0x7f, 1024 },
81 { 0x8503, "Hanwang Art Master II", HANWANG_ART_MASTER_II
,
82 ART_MASTER_PKGLEN_MAX
, 0x27de, 0x1cfe, 0x3f, 0x7f, 1024 },
85 static const int hw_eventtypes
[] = {
86 EV_KEY
, EV_ABS
, EV_MSC
,
89 static const int hw_absevents
[] = {
90 ABS_X
, ABS_Y
, ABS_TILT_X
, ABS_TILT_Y
, ABS_WHEEL
,
91 ABS_RX
, ABS_RY
, ABS_PRESSURE
, ABS_MISC
,
94 static const int hw_btnevents
[] = {
95 BTN_STYLUS
, BTN_STYLUS2
, BTN_TOOL_PEN
, BTN_TOOL_RUBBER
,
96 BTN_TOOL_MOUSE
, BTN_TOOL_FINGER
,
97 BTN_0
, BTN_1
, BTN_2
, BTN_3
, BTN_4
, BTN_5
, BTN_6
, BTN_7
, BTN_8
,
100 static const int hw_mscevents
[] = {
104 static void hanwang_parse_packet(struct hanwang
*hanwang
)
106 unsigned char *data
= hanwang
->data
;
107 struct input_dev
*input_dev
= hanwang
->dev
;
108 struct usb_device
*dev
= hanwang
->usbdev
;
109 enum hanwang_tablet_type type
= hanwang
->features
->type
;
113 if (type
== HANWANG_ART_MASTER_II
) {
114 hanwang
->current_tool
= BTN_TOOL_PEN
;
115 hanwang
->current_id
= STYLUS_DEVICE_ID
;
119 case 0x02: /* data packet */
121 case 0x80: /* tool prox out */
122 if (type
!= HANWANG_ART_MASTER_II
) {
123 hanwang
->current_id
= 0;
124 input_report_key(input_dev
,
125 hanwang
->current_tool
, 0);
129 case 0x00: /* artmaster ii pen leave */
130 if (type
== HANWANG_ART_MASTER_II
) {
131 hanwang
->current_id
= 0;
132 input_report_key(input_dev
,
133 hanwang
->current_tool
, 0);
137 case 0xc2: /* first time tool prox in */
138 switch (data
[3] & 0xf0) {
139 case 0x20: /* art_master III */
140 case 0x30: /* art_master_HD */
141 hanwang
->current_id
= STYLUS_DEVICE_ID
;
142 hanwang
->current_tool
= BTN_TOOL_PEN
;
143 input_report_key(input_dev
, BTN_TOOL_PEN
, 1);
145 case 0xa0: /* art_master III */
146 case 0xb0: /* art_master_HD */
147 hanwang
->current_id
= ERASER_DEVICE_ID
;
148 hanwang
->current_tool
= BTN_TOOL_RUBBER
;
149 input_report_key(input_dev
, BTN_TOOL_RUBBER
, 1);
152 hanwang
->current_id
= 0;
154 "unknown tablet tool %02x\n", data
[0]);
159 default: /* tool data packet */
161 case HANWANG_ART_MASTER_III
:
163 ((data
[7] & 0xc0) >> 5) |
167 case HANWANG_ART_MASTER_HD
:
168 case HANWANG_ART_MASTER_II
:
169 p
= (data
[7] >> 6) | (data
[6] << 2);
177 input_report_abs(input_dev
, ABS_X
,
178 be16_to_cpup((__be16
*)&data
[2]));
179 input_report_abs(input_dev
, ABS_Y
,
180 be16_to_cpup((__be16
*)&data
[4]));
181 input_report_abs(input_dev
, ABS_PRESSURE
, p
);
182 input_report_abs(input_dev
, ABS_TILT_X
, data
[7] & 0x3f);
183 input_report_abs(input_dev
, ABS_TILT_Y
, data
[8] & 0x7f);
184 input_report_key(input_dev
, BTN_STYLUS
, data
[1] & 0x02);
186 if (type
!= HANWANG_ART_MASTER_II
)
187 input_report_key(input_dev
, BTN_STYLUS2
,
190 input_report_key(input_dev
, BTN_TOOL_PEN
, 1);
195 input_report_abs(input_dev
, ABS_MISC
, hanwang
->current_id
);
196 input_event(input_dev
, EV_MSC
, MSC_SERIAL
,
197 hanwang
->features
->pid
);
202 hanwang
->current_id
= PAD_DEVICE_ID
;
205 case HANWANG_ART_MASTER_III
:
206 input_report_key(input_dev
, BTN_TOOL_FINGER
,
207 data
[1] || data
[2] || data
[3]);
208 input_report_abs(input_dev
, ABS_WHEEL
, data
[1]);
209 input_report_key(input_dev
, BTN_0
, data
[2]);
210 for (i
= 0; i
< 8; i
++)
211 input_report_key(input_dev
,
212 BTN_1
+ i
, data
[3] & (1 << i
));
215 case HANWANG_ART_MASTER_HD
:
216 input_report_key(input_dev
, BTN_TOOL_FINGER
, data
[1] ||
217 data
[2] || data
[3] || data
[4] ||
219 input_report_abs(input_dev
, ABS_RX
,
220 ((data
[1] & 0x1f) << 8) | data
[2]);
221 input_report_abs(input_dev
, ABS_RY
,
222 ((data
[3] & 0x1f) << 8) | data
[4]);
223 input_report_key(input_dev
, BTN_0
, data
[5] & 0x01);
224 for (i
= 0; i
< 4; i
++) {
225 input_report_key(input_dev
,
226 BTN_1
+ i
, data
[5] & (1 << i
));
227 input_report_key(input_dev
,
228 BTN_5
+ i
, data
[6] & (1 << i
));
232 case HANWANG_ART_MASTER_II
:
233 dev_dbg(&dev
->dev
, "error packet %02x\n", data
[0]);
237 input_report_abs(input_dev
, ABS_MISC
, hanwang
->current_id
);
238 input_event(input_dev
, EV_MSC
, MSC_SERIAL
, 0xffffffff);
242 dev_dbg(&dev
->dev
, "error packet %02x\n", data
[0]);
246 input_sync(input_dev
);
249 static void hanwang_irq(struct urb
*urb
)
251 struct hanwang
*hanwang
= urb
->context
;
252 struct usb_device
*dev
= hanwang
->usbdev
;
255 switch (urb
->status
) {
258 hanwang_parse_packet(hanwang
);
263 /* this urb is terminated, clean up */
264 dev_err(&dev
->dev
, "%s - urb shutting down with status: %d",
265 __func__
, urb
->status
);
268 dev_err(&dev
->dev
, "%s - nonzero urb status received: %d",
269 __func__
, urb
->status
);
273 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
275 dev_err(&dev
->dev
, "%s - usb_submit_urb failed with result %d",
279 static int hanwang_open(struct input_dev
*dev
)
281 struct hanwang
*hanwang
= input_get_drvdata(dev
);
283 hanwang
->irq
->dev
= hanwang
->usbdev
;
284 if (usb_submit_urb(hanwang
->irq
, GFP_KERNEL
))
290 static void hanwang_close(struct input_dev
*dev
)
292 struct hanwang
*hanwang
= input_get_drvdata(dev
);
294 usb_kill_urb(hanwang
->irq
);
297 static bool get_features(struct usb_device
*dev
, struct hanwang
*hanwang
)
301 for (i
= 0; i
< ARRAY_SIZE(features_array
); i
++) {
302 if (le16_to_cpu(dev
->descriptor
.idProduct
) ==
303 features_array
[i
].pid
) {
304 hanwang
->features
= &features_array
[i
];
313 static int hanwang_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
315 struct usb_device
*dev
= interface_to_usbdev(intf
);
316 struct usb_endpoint_descriptor
*endpoint
;
317 struct hanwang
*hanwang
;
318 struct input_dev
*input_dev
;
322 if (intf
->cur_altsetting
->desc
.bNumEndpoints
< 1)
325 hanwang
= kzalloc(sizeof(*hanwang
), GFP_KERNEL
);
326 input_dev
= input_allocate_device();
327 if (!hanwang
|| !input_dev
) {
332 if (!get_features(dev
, hanwang
)) {
337 hanwang
->data
= usb_alloc_coherent(dev
, hanwang
->features
->pkg_len
,
338 GFP_KERNEL
, &hanwang
->data_dma
);
339 if (!hanwang
->data
) {
344 hanwang
->irq
= usb_alloc_urb(0, GFP_KERNEL
);
350 hanwang
->usbdev
= dev
;
351 hanwang
->dev
= input_dev
;
353 usb_make_path(dev
, hanwang
->phys
, sizeof(hanwang
->phys
));
354 strlcat(hanwang
->phys
, "/input0", sizeof(hanwang
->phys
));
356 strscpy(hanwang
->name
, hanwang
->features
->name
, sizeof(hanwang
->name
));
357 input_dev
->name
= hanwang
->name
;
358 input_dev
->phys
= hanwang
->phys
;
359 usb_to_input_id(dev
, &input_dev
->id
);
360 input_dev
->dev
.parent
= &intf
->dev
;
362 input_set_drvdata(input_dev
, hanwang
);
364 input_dev
->open
= hanwang_open
;
365 input_dev
->close
= hanwang_close
;
367 for (i
= 0; i
< ARRAY_SIZE(hw_eventtypes
); ++i
)
368 __set_bit(hw_eventtypes
[i
], input_dev
->evbit
);
370 for (i
= 0; i
< ARRAY_SIZE(hw_absevents
); ++i
)
371 __set_bit(hw_absevents
[i
], input_dev
->absbit
);
373 for (i
= 0; i
< ARRAY_SIZE(hw_btnevents
); ++i
)
374 __set_bit(hw_btnevents
[i
], input_dev
->keybit
);
376 for (i
= 0; i
< ARRAY_SIZE(hw_mscevents
); ++i
)
377 __set_bit(hw_mscevents
[i
], input_dev
->mscbit
);
379 input_set_abs_params(input_dev
, ABS_X
,
380 0, hanwang
->features
->max_x
, 4, 0);
381 input_set_abs_params(input_dev
, ABS_Y
,
382 0, hanwang
->features
->max_y
, 4, 0);
383 input_set_abs_params(input_dev
, ABS_TILT_X
,
384 0, hanwang
->features
->max_tilt_x
, 0, 0);
385 input_set_abs_params(input_dev
, ABS_TILT_Y
,
386 0, hanwang
->features
->max_tilt_y
, 0, 0);
387 input_set_abs_params(input_dev
, ABS_PRESSURE
,
388 0, hanwang
->features
->max_pressure
, 0, 0);
390 endpoint
= &intf
->cur_altsetting
->endpoint
[0].desc
;
391 usb_fill_int_urb(hanwang
->irq
, dev
,
392 usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
),
393 hanwang
->data
, hanwang
->features
->pkg_len
,
394 hanwang_irq
, hanwang
, endpoint
->bInterval
);
395 hanwang
->irq
->transfer_dma
= hanwang
->data_dma
;
396 hanwang
->irq
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
398 error
= input_register_device(hanwang
->dev
);
402 usb_set_intfdata(intf
, hanwang
);
406 fail3
: usb_free_urb(hanwang
->irq
);
407 fail2
: usb_free_coherent(dev
, hanwang
->features
->pkg_len
,
408 hanwang
->data
, hanwang
->data_dma
);
409 fail1
: input_free_device(input_dev
);
415 static void hanwang_disconnect(struct usb_interface
*intf
)
417 struct hanwang
*hanwang
= usb_get_intfdata(intf
);
419 input_unregister_device(hanwang
->dev
);
420 usb_free_urb(hanwang
->irq
);
421 usb_free_coherent(interface_to_usbdev(intf
),
422 hanwang
->features
->pkg_len
, hanwang
->data
,
425 usb_set_intfdata(intf
, NULL
);
428 static const struct usb_device_id hanwang_ids
[] = {
429 { HANWANG_TABLET_DEVICE(USB_VENDOR_ID_HANWANG
, HANWANG_TABLET_INT_CLASS
,
430 HANWANG_TABLET_INT_SUB_CLASS
, HANWANG_TABLET_INT_PROTOCOL
) },
434 MODULE_DEVICE_TABLE(usb
, hanwang_ids
);
436 static struct usb_driver hanwang_driver
= {
438 .probe
= hanwang_probe
,
439 .disconnect
= hanwang_disconnect
,
440 .id_table
= hanwang_ids
,
443 module_usb_driver(hanwang_driver
);