blob: 41caaef8e2d72c3a8e098086f9f0edbe44be48c8 [file] [log] [blame]
Ping Cheng3bea7332006-07-13 18:01:36 -07001/*
Dmitry Torokhov4104d132007-05-07 16:16:29 -04002 * drivers/input/tablet/wacom_sys.c
Ping Cheng3bea7332006-07-13 18:01:36 -07003 *
4 * USB Wacom Graphire and Wacom Intuos tablet support - system specific code
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include "wacom.h"
15#include "wacom_wac.h"
16
17#define USB_REQ_GET_REPORT 0x01
18#define USB_REQ_SET_REPORT 0x09
19
20static int usb_get_report(struct usb_interface *intf, unsigned char type,
21 unsigned char id, void *buf, int size)
22{
23 return usb_control_msg(interface_to_usbdev(intf),
24 usb_rcvctrlpipe(interface_to_usbdev(intf), 0),
25 USB_REQ_GET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
26 (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
27 buf, size, 100);
28}
29
30static int usb_set_report(struct usb_interface *intf, unsigned char type,
31 unsigned char id, void *buf, int size)
32{
33 return usb_control_msg(interface_to_usbdev(intf),
34 usb_sndctrlpipe(interface_to_usbdev(intf), 0),
35 USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
36 (type << 8) + id, intf->altsetting[0].desc.bInterfaceNumber,
37 buf, size, 1000);
38}
39
40static struct input_dev * get_input_dev(struct wacom_combo *wcombo)
41{
42 return wcombo->wacom->dev;
43}
44
Adrian Bunk54c9b2262006-11-20 03:23:58 +010045static void wacom_sys_irq(struct urb *urb)
Ping Cheng3bea7332006-07-13 18:01:36 -070046{
47 struct wacom *wacom = urb->context;
48 struct wacom_combo wcombo;
49 int retval;
50
51 switch (urb->status) {
52 case 0:
53 /* success */
54 break;
55 case -ECONNRESET:
56 case -ENOENT:
57 case -ESHUTDOWN:
58 /* this urb is terminated, clean up */
59 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
60 return;
61 default:
62 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
63 goto exit;
64 }
65
66 wcombo.wacom = wacom;
67 wcombo.urb = urb;
Ping Cheng3bea7332006-07-13 18:01:36 -070068
69 if (wacom_wac_irq(wacom->wacom_wac, (void *)&wcombo))
70 input_sync(get_input_dev(&wcombo));
71
72 exit:
73 retval = usb_submit_urb (urb, GFP_ATOMIC);
74 if (retval)
75 err ("%s - usb_submit_urb failed with result %d",
76 __FUNCTION__, retval);
77}
78
79void wacom_report_key(void *wcombo, unsigned int key_type, int key_data)
80{
81 input_report_key(get_input_dev((struct wacom_combo *)wcombo), key_type, key_data);
82 return;
83}
84
85void wacom_report_abs(void *wcombo, unsigned int abs_type, int abs_data)
86{
87 input_report_abs(get_input_dev((struct wacom_combo *)wcombo), abs_type, abs_data);
88 return;
89}
90
91void wacom_report_rel(void *wcombo, unsigned int rel_type, int rel_data)
92{
93 input_report_rel(get_input_dev((struct wacom_combo *)wcombo), rel_type, rel_data);
94 return;
95}
96
97void wacom_input_event(void *wcombo, unsigned int type, unsigned int code, int value)
98{
99 input_event(get_input_dev((struct wacom_combo *)wcombo), type, code, value);
100 return;
101}
102
103__u16 wacom_be16_to_cpu(unsigned char *data)
104{
105 __u16 value;
106 value = be16_to_cpu(*(__be16 *) data);
107 return value;
108}
109
110__u16 wacom_le16_to_cpu(unsigned char *data)
111{
112 __u16 value;
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700113 value = le16_to_cpu(*(__le16 *) data);
Ping Cheng3bea7332006-07-13 18:01:36 -0700114 return value;
115}
116
Ping Cheng3bea7332006-07-13 18:01:36 -0700117void wacom_input_sync(void *wcombo)
118{
119 input_sync(get_input_dev((struct wacom_combo *)wcombo));
120 return;
121}
122
123static int wacom_open(struct input_dev *dev)
124{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400125 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700126
127 wacom->irq->dev = wacom->usbdev;
128 if (usb_submit_urb(wacom->irq, GFP_KERNEL))
129 return -EIO;
130
131 return 0;
132}
133
134static void wacom_close(struct input_dev *dev)
135{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400136 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700137
138 usb_kill_urb(wacom->irq);
139}
140
Ping Cheng7ecfbfd2007-06-14 23:32:48 -0400141void input_dev_mo(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
142{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700143 input_dev->keybit[BIT_WORD(BTN_LEFT)] |= BIT_MASK(BTN_1) |
144 BIT_MASK(BTN_5);
Ping Cheng7ecfbfd2007-06-14 23:32:48 -0400145 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
146}
147
Ping Cheng3bea7332006-07-13 18:01:36 -0700148void input_dev_g4(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
149{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700150 input_dev->evbit[0] |= BIT_MASK(EV_MSC);
151 input_dev->mscbit[0] |= BIT_MASK(MSC_SERIAL);
152 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_FINGER);
153 input_dev->keybit[BIT_WORD(BTN_LEFT)] |= BIT_MASK(BTN_0) |
154 BIT_MASK(BTN_4);
Ping Cheng3bea7332006-07-13 18:01:36 -0700155}
156
157void input_dev_g(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
158{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700159 input_dev->evbit[0] |= BIT_MASK(EV_REL);
160 input_dev->relbit[0] |= BIT_MASK(REL_WHEEL);
161 input_dev->keybit[BIT_WORD(BTN_LEFT)] |= BIT_MASK(BTN_LEFT) |
162 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
163 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_RUBBER) |
164 BIT_MASK(BTN_TOOL_MOUSE) | BIT_MASK(BTN_STYLUS2);
Ping Cheng3bea7332006-07-13 18:01:36 -0700165 input_set_abs_params(input_dev, ABS_DISTANCE, 0, wacom_wac->features->distance_max, 0, 0);
166}
167
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700168void input_dev_i3s(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
Ping Cheng3bea7332006-07-13 18:01:36 -0700169{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700170 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_FINGER);
171 input_dev->keybit[BIT_WORD(BTN_LEFT)] |= BIT_MASK(BTN_0) |
172 BIT_MASK(BTN_1) | BIT_MASK(BTN_2) | BIT_MASK(BTN_3);
Ping Cheng071e0a22006-12-05 17:09:51 -0800173 input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
Ping Cheng0e1763f2008-03-13 16:46:46 -0400174 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700175}
176
177void input_dev_i3(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
178{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700179 input_dev->keybit[BIT_WORD(BTN_LEFT)] |= BIT_MASK(BTN_4) |
180 BIT_MASK(BTN_5) | BIT_MASK(BTN_6) | BIT_MASK(BTN_7);
Ping Cheng071e0a22006-12-05 17:09:51 -0800181 input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
Ping Cheng3bea7332006-07-13 18:01:36 -0700182}
183
Ping Cheng0e1763f2008-03-13 16:46:46 -0400184void input_dev_bee(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
185{
186 input_dev->keybit[BIT_WORD(BTN_LEFT)] |= BIT_MASK(BTN_8) | BIT_MASK(BTN_9);
187}
188
Ping Cheng3bea7332006-07-13 18:01:36 -0700189void input_dev_i(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
190{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700191 input_dev->evbit[0] |= BIT_MASK(EV_MSC) | BIT_MASK(EV_REL);
192 input_dev->mscbit[0] |= BIT_MASK(MSC_SERIAL);
193 input_dev->relbit[0] |= BIT_MASK(REL_WHEEL);
194 input_dev->keybit[BIT_WORD(BTN_LEFT)] |= BIT_MASK(BTN_LEFT) |
195 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE) |
196 BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
197 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_RUBBER) |
198 BIT_MASK(BTN_TOOL_MOUSE) | BIT_MASK(BTN_TOOL_BRUSH) |
199 BIT_MASK(BTN_TOOL_PENCIL) | BIT_MASK(BTN_TOOL_AIRBRUSH) |
200 BIT_MASK(BTN_TOOL_LENS) | BIT_MASK(BTN_STYLUS2);
Ping Cheng3bea7332006-07-13 18:01:36 -0700201 input_set_abs_params(input_dev, ABS_DISTANCE, 0, wacom_wac->features->distance_max, 0, 0);
202 input_set_abs_params(input_dev, ABS_WHEEL, 0, 1023, 0, 0);
203 input_set_abs_params(input_dev, ABS_TILT_X, 0, 127, 0, 0);
204 input_set_abs_params(input_dev, ABS_TILT_Y, 0, 127, 0, 0);
205 input_set_abs_params(input_dev, ABS_RZ, -900, 899, 0, 0);
206 input_set_abs_params(input_dev, ABS_THROTTLE, -1023, 1023, 0, 0);
207}
208
209void input_dev_pl(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
210{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700211 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_STYLUS2) |
212 BIT_MASK(BTN_TOOL_RUBBER);
Ping Cheng3bea7332006-07-13 18:01:36 -0700213}
214
215void input_dev_pt(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
216{
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700217 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_RUBBER);
Ping Cheng3bea7332006-07-13 18:01:36 -0700218}
219
220static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
221{
222 struct usb_device *dev = interface_to_usbdev(intf);
223 struct usb_endpoint_descriptor *endpoint;
224 struct wacom *wacom;
225 struct wacom_wac *wacom_wac;
226 struct input_dev *input_dev;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400227 int error = -ENOMEM;
Ping Cheng3bea7332006-07-13 18:01:36 -0700228 char rep_data[2], limit = 0;
229
230 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
231 wacom_wac = kzalloc(sizeof(struct wacom_wac), GFP_KERNEL);
232 input_dev = input_allocate_device();
233 if (!wacom || !input_dev || !wacom_wac)
234 goto fail1;
235
236 wacom_wac->data = usb_buffer_alloc(dev, 10, GFP_KERNEL, &wacom->data_dma);
237 if (!wacom_wac->data)
238 goto fail1;
239
240 wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
241 if (!wacom->irq)
242 goto fail2;
243
244 wacom->usbdev = dev;
245 wacom->dev = input_dev;
246 usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
247 strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
248
249 wacom_wac->features = get_wacom_feature(id);
Eric Sesterhenn29203492006-10-17 14:46:30 -0700250 BUG_ON(wacom_wac->features->pktlen > 10);
Ping Cheng3bea7332006-07-13 18:01:36 -0700251
252 input_dev->name = wacom_wac->features->name;
253 wacom->wacom_wac = wacom_wac;
254 usb_to_input_id(dev, &input_dev->id);
255
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400256 input_dev->dev.parent = &intf->dev;
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400257
258 input_set_drvdata(input_dev, wacom);
259
Ping Cheng3bea7332006-07-13 18:01:36 -0700260 input_dev->open = wacom_open;
261 input_dev->close = wacom_close;
262
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700263 input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
264 input_dev->keybit[BIT_WORD(BTN_DIGI)] |= BIT_MASK(BTN_TOOL_PEN) |
265 BIT_MASK(BTN_TOUCH) | BIT_MASK(BTN_STYLUS);
Ping Cheng3bea7332006-07-13 18:01:36 -0700266 input_set_abs_params(input_dev, ABS_X, 0, wacom_wac->features->x_max, 4, 0);
267 input_set_abs_params(input_dev, ABS_Y, 0, wacom_wac->features->y_max, 4, 0);
268 input_set_abs_params(input_dev, ABS_PRESSURE, 0, wacom_wac->features->pressure_max, 0, 0);
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700269 input_dev->absbit[BIT_WORD(ABS_MISC)] |= BIT_MASK(ABS_MISC);
Ping Cheng3bea7332006-07-13 18:01:36 -0700270
271 wacom_init_input_dev(input_dev, wacom_wac);
272
273 endpoint = &intf->cur_altsetting->endpoint[0].desc;
274
275 usb_fill_int_urb(wacom->irq, dev,
276 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
277 wacom_wac->data, wacom_wac->features->pktlen,
Ping Cheng8d32e3a2006-09-26 13:34:47 -0700278 wacom_sys_irq, wacom, endpoint->bInterval);
Ping Cheng3bea7332006-07-13 18:01:36 -0700279 wacom->irq->transfer_dma = wacom->data_dma;
280 wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
281
Dmitry Torokhov50141862007-04-12 01:33:39 -0400282 error = input_register_device(wacom->dev);
283 if (error)
284 goto fail3;
Ping Cheng3bea7332006-07-13 18:01:36 -0700285
286 /* Ask the tablet to report tablet data. Repeat until it succeeds */
287 do {
288 rep_data[0] = 2;
289 rep_data[1] = 2;
290 usb_set_report(intf, 3, 2, rep_data, 2);
291 usb_get_report(intf, 3, 2, rep_data, 2);
292 } while (rep_data[1] != 2 && limit++ < 5);
293
294 usb_set_intfdata(intf, wacom);
295 return 0;
296
Dmitry Torokhov50141862007-04-12 01:33:39 -0400297 fail3: usb_free_urb(wacom->irq);
298 fail2: usb_buffer_free(dev, 10, wacom_wac->data, wacom->data_dma);
299 fail1: input_free_device(input_dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700300 kfree(wacom);
301 kfree(wacom_wac);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400302 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -0700303}
304
305static void wacom_disconnect(struct usb_interface *intf)
306{
307 struct wacom *wacom = usb_get_intfdata (intf);
308
309 usb_set_intfdata(intf, NULL);
310 if (wacom) {
311 usb_kill_urb(wacom->irq);
312 input_unregister_device(wacom->dev);
313 usb_free_urb(wacom->irq);
314 usb_buffer_free(interface_to_usbdev(intf), 10, wacom->wacom_wac->data, wacom->data_dma);
Ping Cheng3bea7332006-07-13 18:01:36 -0700315 kfree(wacom->wacom_wac);
Eric Sesterhenn2daa4872006-10-04 09:56:44 -0700316 kfree(wacom);
Ping Cheng3bea7332006-07-13 18:01:36 -0700317 }
318}
319
320static struct usb_driver wacom_driver = {
321 .name = "wacom",
322 .probe = wacom_probe,
323 .disconnect = wacom_disconnect,
324};
325
326static int __init wacom_init(void)
327{
328 int result;
329 wacom_driver.id_table = get_device_table();
330 result = usb_register(&wacom_driver);
331 if (result == 0)
332 info(DRIVER_VERSION ":" DRIVER_DESC);
333 return result;
334}
335
336static void __exit wacom_exit(void)
337{
338 usb_deregister(&wacom_driver);
339}
340
341module_init(wacom_init);
342module_exit(wacom_exit);