Commit | Line | Data |
---|---|---|
bba5394a XW |
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_MASTERIII_PKGLEN_MAX 10 | |
46 | ||
24dd3b58 XW |
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 | ||
bba5394a XW |
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 | struct hanwang { | |
64 | unsigned char *data; | |
65 | dma_addr_t data_dma; | |
66 | struct input_dev *dev; | |
67 | struct usb_device *usbdev; | |
68 | struct urb *irq; | |
69 | const struct hanwang_features *features; | |
70 | unsigned int current_tool; | |
24dd3b58 | 71 | unsigned int current_id; |
bba5394a XW |
72 | char name[64]; |
73 | char phys[32]; | |
74 | }; | |
75 | ||
76 | struct hanwang_features { | |
77 | unsigned short pid; | |
78 | char *name; | |
79 | int pkg_len; | |
80 | int max_x; | |
81 | int max_y; | |
82 | int max_tilt_x; | |
83 | int max_tilt_y; | |
84 | int max_pressure; | |
85 | }; | |
86 | ||
87 | static const struct hanwang_features features_array[] = { | |
88 | { 0x8528, "Hanwang Art Master III 0906", | |
89 | ART_MASTERIII_PKGLEN_MAX, 0x5757, 0x3692, 0x3f, 0x7f, 2048 }, | |
90 | { 0x8529, "Hanwang Art Master III 0604", | |
91 | ART_MASTERIII_PKGLEN_MAX, 0x3d84, 0x2672, 0x3f, 0x7f, 2048 }, | |
92 | { 0x852a, "Hanwang Art Master III 1308", | |
93 | ART_MASTERIII_PKGLEN_MAX, 0x7f00, 0x4f60, 0x3f, 0x7f, 2048 }, | |
94 | }; | |
95 | ||
96 | static const int hw_eventtypes[] = { | |
24dd3b58 | 97 | EV_KEY, EV_ABS, EV_MSC, |
bba5394a XW |
98 | }; |
99 | ||
100 | static const int hw_absevents[] = { | |
24dd3b58 XW |
101 | ABS_X, ABS_Y, ABS_TILT_X, ABS_TILT_Y, ABS_WHEEL, |
102 | ABS_PRESSURE, ABS_MISC, | |
bba5394a XW |
103 | }; |
104 | ||
105 | static const int hw_btnevents[] = { | |
24dd3b58 XW |
106 | BTN_STYLUS, BTN_STYLUS2, BTN_TOOL_PEN, BTN_TOOL_RUBBER, |
107 | BTN_TOOL_MOUSE, BTN_TOOL_FINGER, | |
108 | BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8, | |
109 | }; | |
110 | ||
111 | static const int hw_mscevents[] = { | |
112 | MSC_SERIAL, | |
bba5394a XW |
113 | }; |
114 | ||
115 | static void hanwang_parse_packet(struct hanwang *hanwang) | |
116 | { | |
117 | unsigned char *data = hanwang->data; | |
118 | struct input_dev *input_dev = hanwang->dev; | |
119 | struct usb_device *dev = hanwang->usbdev; | |
120 | int i; | |
121 | ||
122 | switch (data[0]) { | |
123 | case 0x02: /* data packet */ | |
124 | switch (data[1]) { | |
125 | case 0x80: /* tool prox out */ | |
24dd3b58 | 126 | hanwang->current_id = 0; |
bba5394a XW |
127 | input_report_key(input_dev, hanwang->current_tool, 0); |
128 | break; | |
129 | ||
130 | case 0xc2: /* first time tool prox in */ | |
131 | switch (data[3] & 0xf0) { | |
132 | case 0x20: | |
24dd3b58 | 133 | hanwang->current_id = STYLUS_DEVICE_ID; |
bba5394a XW |
134 | hanwang->current_tool = BTN_TOOL_PEN; |
135 | input_report_key(input_dev, BTN_TOOL_PEN, 1); | |
136 | break; | |
137 | case 0xa0: | |
24dd3b58 | 138 | hanwang->current_id = ERASER_DEVICE_ID; |
bba5394a XW |
139 | hanwang->current_tool = BTN_TOOL_RUBBER; |
140 | input_report_key(input_dev, BTN_TOOL_RUBBER, 1); | |
141 | break; | |
142 | default: | |
24dd3b58 | 143 | hanwang->current_id = 0; |
bba5394a XW |
144 | dev_dbg(&dev->dev, |
145 | "unknown tablet tool %02x ", data[0]); | |
146 | break; | |
147 | } | |
148 | break; | |
149 | ||
150 | default: /* tool data packet */ | |
151 | input_report_abs(input_dev, ABS_X, | |
152 | (data[2] << 8) | data[3]); | |
153 | input_report_abs(input_dev, ABS_Y, | |
154 | (data[4] << 8) | data[5]); | |
155 | input_report_abs(input_dev, ABS_PRESSURE, | |
156 | (data[6] << 3) | | |
157 | ((data[7] & 0xc0) >> 5) | | |
158 | (data[1] & 0x01)); | |
159 | input_report_abs(input_dev, ABS_TILT_X, data[7] & 0x3f); | |
160 | input_report_abs(input_dev, ABS_TILT_Y, data[8] & 0x7f); | |
161 | input_report_key(input_dev, BTN_STYLUS, data[1] & 0x02); | |
162 | input_report_key(input_dev, BTN_STYLUS2, data[1] & 0x04); | |
163 | break; | |
164 | } | |
24dd3b58 XW |
165 | input_report_abs(input_dev, ABS_MISC, hanwang->current_id); |
166 | input_event(input_dev, EV_MSC, MSC_SERIAL, | |
167 | hanwang->features->pid); | |
bba5394a XW |
168 | break; |
169 | ||
170 | case 0x0c: | |
171 | /* roll wheel */ | |
24dd3b58 XW |
172 | hanwang->current_id = PAD_DEVICE_ID; |
173 | input_report_key(input_dev, BTN_TOOL_FINGER, data[1] || | |
174 | data[2] || data[3]); | |
bba5394a XW |
175 | input_report_abs(input_dev, ABS_WHEEL, data[1]); |
176 | input_report_key(input_dev, BTN_0, data[2]); | |
177 | for (i = 0; i < 8; i++) | |
178 | input_report_key(input_dev, | |
179 | BTN_1 + i, data[3] & (1 << i)); | |
24dd3b58 XW |
180 | input_report_abs(input_dev, ABS_MISC, hanwang->current_id); |
181 | input_event(input_dev, EV_MSC, MSC_SERIAL, 0xffffffff); | |
bba5394a XW |
182 | break; |
183 | ||
184 | default: | |
185 | dev_dbg(&dev->dev, "error packet %02x ", data[0]); | |
186 | break; | |
187 | } | |
188 | ||
189 | input_sync(input_dev); | |
190 | } | |
191 | ||
192 | static void hanwang_irq(struct urb *urb) | |
193 | { | |
194 | struct hanwang *hanwang = urb->context; | |
195 | struct usb_device *dev = hanwang->usbdev; | |
196 | int retval; | |
197 | ||
198 | switch (urb->status) { | |
199 | case 0: | |
200 | /* success */; | |
201 | hanwang_parse_packet(hanwang); | |
202 | break; | |
203 | case -ECONNRESET: | |
204 | case -ENOENT: | |
205 | case -ESHUTDOWN: | |
206 | /* this urb is terminated, clean up */ | |
207 | dev_err(&dev->dev, "%s - urb shutting down with status: %d", | |
208 | __func__, urb->status); | |
209 | return; | |
210 | default: | |
211 | dev_err(&dev->dev, "%s - nonzero urb status received: %d", | |
212 | __func__, urb->status); | |
213 | break; | |
214 | } | |
215 | ||
216 | retval = usb_submit_urb(urb, GFP_ATOMIC); | |
217 | if (retval) | |
218 | dev_err(&dev->dev, "%s - usb_submit_urb failed with result %d", | |
219 | __func__, retval); | |
220 | } | |
221 | ||
222 | static int hanwang_open(struct input_dev *dev) | |
223 | { | |
224 | struct hanwang *hanwang = input_get_drvdata(dev); | |
225 | ||
226 | hanwang->irq->dev = hanwang->usbdev; | |
227 | if (usb_submit_urb(hanwang->irq, GFP_KERNEL)) | |
228 | return -EIO; | |
229 | ||
230 | return 0; | |
231 | } | |
232 | ||
233 | static void hanwang_close(struct input_dev *dev) | |
234 | { | |
235 | struct hanwang *hanwang = input_get_drvdata(dev); | |
236 | ||
237 | usb_kill_urb(hanwang->irq); | |
238 | } | |
239 | ||
240 | static bool get_features(struct usb_device *dev, struct hanwang *hanwang) | |
241 | { | |
242 | int i; | |
243 | ||
244 | for (i = 0; i < ARRAY_SIZE(features_array); i++) { | |
245 | if (le16_to_cpu(dev->descriptor.idProduct) == | |
246 | features_array[i].pid) { | |
247 | hanwang->features = &features_array[i]; | |
248 | return true; | |
249 | } | |
250 | } | |
251 | ||
252 | return false; | |
253 | } | |
254 | ||
255 | ||
256 | static int hanwang_probe(struct usb_interface *intf, const struct usb_device_id *id) | |
257 | { | |
258 | struct usb_device *dev = interface_to_usbdev(intf); | |
259 | struct usb_endpoint_descriptor *endpoint; | |
260 | struct hanwang *hanwang; | |
261 | struct input_dev *input_dev; | |
262 | int error; | |
263 | int i; | |
264 | ||
265 | hanwang = kzalloc(sizeof(struct hanwang), GFP_KERNEL); | |
266 | input_dev = input_allocate_device(); | |
267 | if (!hanwang || !input_dev) { | |
268 | error = -ENOMEM; | |
269 | goto fail1; | |
270 | } | |
271 | ||
272 | if (!get_features(dev, hanwang)) { | |
273 | error = -ENXIO; | |
274 | goto fail1; | |
275 | } | |
276 | ||
277 | hanwang->data = usb_alloc_coherent(dev, hanwang->features->pkg_len, | |
278 | GFP_KERNEL, &hanwang->data_dma); | |
279 | if (!hanwang->data) { | |
280 | error = -ENOMEM; | |
281 | goto fail1; | |
282 | } | |
283 | ||
284 | hanwang->irq = usb_alloc_urb(0, GFP_KERNEL); | |
285 | if (!hanwang->irq) { | |
286 | error = -ENOMEM; | |
287 | goto fail2; | |
288 | } | |
289 | ||
290 | hanwang->usbdev = dev; | |
291 | hanwang->dev = input_dev; | |
292 | ||
293 | usb_make_path(dev, hanwang->phys, sizeof(hanwang->phys)); | |
294 | strlcat(hanwang->phys, "/input0", sizeof(hanwang->phys)); | |
295 | ||
296 | strlcpy(hanwang->name, hanwang->features->name, sizeof(hanwang->name)); | |
297 | input_dev->name = hanwang->name; | |
298 | input_dev->phys = hanwang->phys; | |
299 | usb_to_input_id(dev, &input_dev->id); | |
300 | input_dev->dev.parent = &intf->dev; | |
301 | ||
302 | input_set_drvdata(input_dev, hanwang); | |
303 | ||
304 | input_dev->open = hanwang_open; | |
305 | input_dev->close = hanwang_close; | |
306 | ||
307 | for (i = 0; i < ARRAY_SIZE(hw_eventtypes); ++i) | |
308 | __set_bit(hw_eventtypes[i], input_dev->evbit); | |
309 | ||
310 | for (i = 0; i < ARRAY_SIZE(hw_absevents); ++i) | |
311 | __set_bit(hw_absevents[i], input_dev->absbit); | |
312 | ||
313 | for (i = 0; i < ARRAY_SIZE(hw_btnevents); ++i) | |
314 | __set_bit(hw_btnevents[i], input_dev->keybit); | |
315 | ||
24dd3b58 XW |
316 | for (i = 0; i < ARRAY_SIZE(hw_mscevents); ++i) |
317 | __set_bit(hw_mscevents[i], input_dev->mscbit); | |
318 | ||
bba5394a XW |
319 | input_set_abs_params(input_dev, ABS_X, |
320 | 0, hanwang->features->max_x, 4, 0); | |
321 | input_set_abs_params(input_dev, ABS_Y, | |
322 | 0, hanwang->features->max_y, 4, 0); | |
323 | input_set_abs_params(input_dev, ABS_TILT_X, | |
324 | 0, hanwang->features->max_tilt_x, 0, 0); | |
325 | input_set_abs_params(input_dev, ABS_TILT_Y, | |
326 | 0, hanwang->features->max_tilt_y, 0, 0); | |
327 | input_set_abs_params(input_dev, ABS_PRESSURE, | |
328 | 0, hanwang->features->max_pressure, 0, 0); | |
329 | ||
330 | endpoint = &intf->cur_altsetting->endpoint[0].desc; | |
331 | usb_fill_int_urb(hanwang->irq, dev, | |
332 | usb_rcvintpipe(dev, endpoint->bEndpointAddress), | |
333 | hanwang->data, hanwang->features->pkg_len, | |
334 | hanwang_irq, hanwang, endpoint->bInterval); | |
335 | hanwang->irq->transfer_dma = hanwang->data_dma; | |
336 | hanwang->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
337 | ||
338 | error = input_register_device(hanwang->dev); | |
339 | if (error) | |
340 | goto fail3; | |
341 | ||
342 | usb_set_intfdata(intf, hanwang); | |
343 | ||
344 | return 0; | |
345 | ||
346 | fail3: usb_free_urb(hanwang->irq); | |
347 | fail2: usb_free_coherent(dev, hanwang->features->pkg_len, | |
348 | hanwang->data, hanwang->data_dma); | |
349 | fail1: input_free_device(input_dev); | |
350 | kfree(hanwang); | |
351 | return error; | |
352 | ||
353 | } | |
354 | ||
355 | static void hanwang_disconnect(struct usb_interface *intf) | |
356 | { | |
357 | struct hanwang *hanwang = usb_get_intfdata(intf); | |
358 | ||
359 | input_unregister_device(hanwang->dev); | |
360 | usb_free_urb(hanwang->irq); | |
361 | usb_free_coherent(interface_to_usbdev(intf), | |
362 | hanwang->features->pkg_len, hanwang->data, | |
363 | hanwang->data_dma); | |
364 | kfree(hanwang); | |
365 | usb_set_intfdata(intf, NULL); | |
366 | } | |
367 | ||
368 | static const struct usb_device_id hanwang_ids[] = { | |
369 | { HANWANG_TABLET_DEVICE(USB_VENDOR_ID_HANWANG, HANWANG_TABLET_INT_CLASS, | |
370 | HANWANG_TABLET_INT_SUB_CLASS, HANWANG_TABLET_INT_PROTOCOL) }, | |
371 | {} | |
372 | }; | |
373 | ||
374 | MODULE_DEVICE_TABLE(usb, hanwang_ids); | |
375 | ||
376 | static struct usb_driver hanwang_driver = { | |
377 | .name = "hanwang", | |
378 | .probe = hanwang_probe, | |
379 | .disconnect = hanwang_disconnect, | |
380 | .id_table = hanwang_ids, | |
381 | }; | |
382 | ||
383 | static int __init hanwang_init(void) | |
384 | { | |
385 | return usb_register(&hanwang_driver); | |
386 | } | |
387 | ||
388 | static void __exit hanwang_exit(void) | |
389 | { | |
390 | usb_deregister(&hanwang_driver); | |
391 | } | |
392 | ||
393 | module_init(hanwang_init); | |
394 | module_exit(hanwang_exit); |