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