Merge branch 'master' into gfs2
[pandora-kernel.git] / drivers / usb / input / wacom_sys.c
1 /*
2  * drivers/usb/input/wacom_sys.c
3  *
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
20 static 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
30 static 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
40 static struct input_dev * get_input_dev(struct wacom_combo *wcombo)
41 {
42         return wcombo->wacom->dev;
43 }
44
45 void wacom_sys_irq(struct urb *urb, struct pt_regs *regs)
46 {
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;
68         wcombo.regs = regs;
69
70         if (wacom_wac_irq(wacom->wacom_wac, (void *)&wcombo))
71                 input_sync(get_input_dev(&wcombo));
72
73  exit:
74         retval = usb_submit_urb (urb, GFP_ATOMIC);
75         if (retval)
76                 err ("%s - usb_submit_urb failed with result %d",
77                      __FUNCTION__, retval);
78 }
79
80 void wacom_report_key(void *wcombo, unsigned int key_type, int key_data)
81 {
82         input_report_key(get_input_dev((struct wacom_combo *)wcombo), key_type, key_data);
83         return;
84 }
85
86 void wacom_report_abs(void *wcombo, unsigned int abs_type, int abs_data)
87 {
88         input_report_abs(get_input_dev((struct wacom_combo *)wcombo), abs_type, abs_data);
89         return;
90 }
91
92 void wacom_report_rel(void *wcombo, unsigned int rel_type, int rel_data)
93 {
94         input_report_rel(get_input_dev((struct wacom_combo *)wcombo), rel_type, rel_data);
95         return;
96 }
97
98 void wacom_input_event(void *wcombo, unsigned int type, unsigned int code, int value)
99 {
100         input_event(get_input_dev((struct wacom_combo *)wcombo), type, code, value);
101         return;
102 }
103
104 __u16 wacom_be16_to_cpu(unsigned char *data)
105 {
106         __u16 value;
107         value = be16_to_cpu(*(__be16 *) data);
108         return value;
109 }
110
111 __u16 wacom_le16_to_cpu(unsigned char *data)
112 {
113         __u16 value;
114         value = be16_to_cpu(*(__be16 *) data);
115         return value;
116 }
117
118 void wacom_input_regs(void *wcombo)
119 {
120         input_regs(get_input_dev((struct wacom_combo *)wcombo), ((struct wacom_combo *)wcombo)->regs);
121         return;
122 }
123
124 void wacom_input_sync(void *wcombo)
125 {
126         input_sync(get_input_dev((struct wacom_combo *)wcombo));
127         return;
128 }
129
130 static int wacom_open(struct input_dev *dev)
131 {
132         struct wacom *wacom = dev->private;
133
134         wacom->irq->dev = wacom->usbdev;
135         if (usb_submit_urb(wacom->irq, GFP_KERNEL))
136                 return -EIO;
137
138         return 0;
139 }
140
141 static void wacom_close(struct input_dev *dev)
142 {
143         struct wacom *wacom = dev->private;
144
145         usb_kill_urb(wacom->irq);
146 }
147
148 void input_dev_g4(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
149 {
150         input_dev->evbit[0] |= BIT(EV_MSC);
151         input_dev->mscbit[0] |= BIT(MSC_SERIAL);
152         input_dev->keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_FINGER);
153         input_dev->keybit[LONG(BTN_LEFT)] |= BIT(BTN_0) | BIT(BTN_1) | BIT(BTN_2) | BIT(BTN_3) | BIT(BTN_4) | BIT(BTN_5) | BIT(BTN_6) | BIT(BTN_7);
154 }
155
156 void input_dev_g(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
157 {
158         input_dev->evbit[0] |= BIT(EV_REL);
159         input_dev->relbit[0] |= BIT(REL_WHEEL);
160         input_dev->keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
161         input_dev->keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_RUBBER) | BIT(BTN_TOOL_MOUSE) | BIT(BTN_STYLUS2);
162         input_set_abs_params(input_dev, ABS_DISTANCE, 0, wacom_wac->features->distance_max, 0, 0);
163 }
164
165 void input_dev_i3(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
166 {
167         input_dev->keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_FINGER);
168         input_dev->keybit[LONG(BTN_LEFT)] |= BIT(BTN_0) | BIT(BTN_1) | BIT(BTN_2) | BIT(BTN_3) | BIT(BTN_4) | BIT(BTN_5) | BIT(BTN_6) | BIT(BTN_7);
169         input_set_abs_params(input_dev, ABS_RX, 0, 4097, 0, 0);
170         input_set_abs_params(input_dev, ABS_RY, 0, 4097, 0, 0);
171 }
172
173 void input_dev_i(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
174 {
175         input_dev->evbit[0] |= BIT(EV_MSC) | BIT(EV_REL);
176         input_dev->mscbit[0] |= BIT(MSC_SERIAL);
177         input_dev->relbit[0] |= BIT(REL_WHEEL);
178         input_dev->keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE) | BIT(BTN_SIDE) | BIT(BTN_EXTRA);
179         input_dev->keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_RUBBER) | BIT(BTN_TOOL_MOUSE) | BIT(BTN_TOOL_BRUSH)
180                 | BIT(BTN_TOOL_PENCIL) | BIT(BTN_TOOL_AIRBRUSH) | BIT(BTN_TOOL_LENS) | BIT(BTN_STYLUS2);
181         input_set_abs_params(input_dev, ABS_DISTANCE, 0, wacom_wac->features->distance_max, 0, 0);
182         input_set_abs_params(input_dev, ABS_WHEEL, 0, 1023, 0, 0);
183         input_set_abs_params(input_dev, ABS_TILT_X, 0, 127, 0, 0);
184         input_set_abs_params(input_dev, ABS_TILT_Y, 0, 127, 0, 0);
185         input_set_abs_params(input_dev, ABS_RZ, -900, 899, 0, 0);
186         input_set_abs_params(input_dev, ABS_THROTTLE, -1023, 1023, 0, 0);
187 }
188
189 void input_dev_pl(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
190 {
191         input_dev->keybit[LONG(BTN_DIGI)] |= BIT(BTN_STYLUS2) | BIT(BTN_TOOL_RUBBER);
192 }
193
194 void input_dev_pt(struct input_dev *input_dev, struct wacom_wac *wacom_wac)
195 {
196         input_dev->keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_RUBBER);
197 }
198
199 static int wacom_probe(struct usb_interface *intf, const struct usb_device_id *id)
200 {
201         struct usb_device *dev = interface_to_usbdev(intf);
202         struct usb_endpoint_descriptor *endpoint;
203         struct wacom *wacom;
204         struct wacom_wac *wacom_wac;
205         struct input_dev *input_dev;
206         char rep_data[2], limit = 0;
207
208         wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
209         wacom_wac = kzalloc(sizeof(struct wacom_wac), GFP_KERNEL);
210         input_dev = input_allocate_device();
211         if (!wacom || !input_dev || !wacom_wac)
212                 goto fail1;
213
214         wacom_wac->data = usb_buffer_alloc(dev, 10, GFP_KERNEL, &wacom->data_dma);
215         if (!wacom_wac->data)
216                 goto fail1;
217
218         wacom->irq = usb_alloc_urb(0, GFP_KERNEL);
219         if (!wacom->irq)
220                 goto fail2;
221
222         wacom->usbdev = dev;
223         wacom->dev = input_dev;
224         usb_make_path(dev, wacom->phys, sizeof(wacom->phys));
225         strlcat(wacom->phys, "/input0", sizeof(wacom->phys));
226
227         wacom_wac->features = get_wacom_feature(id);
228         if (wacom_wac->features->pktlen > 10)
229                 BUG();
230
231         input_dev->name = wacom_wac->features->name;
232         wacom->wacom_wac = wacom_wac;
233         usb_to_input_id(dev, &input_dev->id);
234
235         input_dev->cdev.dev = &intf->dev;
236         input_dev->private = wacom;
237         input_dev->open = wacom_open;
238         input_dev->close = wacom_close;
239
240         input_dev->evbit[0] |= BIT(EV_KEY) | BIT(EV_ABS);
241         input_dev->keybit[LONG(BTN_DIGI)] |= BIT(BTN_TOOL_PEN) | BIT(BTN_TOUCH) | BIT(BTN_STYLUS);
242         input_set_abs_params(input_dev, ABS_X, 0, wacom_wac->features->x_max, 4, 0);
243         input_set_abs_params(input_dev, ABS_Y, 0, wacom_wac->features->y_max, 4, 0);
244         input_set_abs_params(input_dev, ABS_PRESSURE, 0, wacom_wac->features->pressure_max, 0, 0);
245         input_dev->absbit[LONG(ABS_MISC)] |= BIT(ABS_MISC);
246
247         wacom_init_input_dev(input_dev, wacom_wac);
248
249         endpoint = &intf->cur_altsetting->endpoint[0].desc;
250
251         usb_fill_int_urb(wacom->irq, dev,
252                          usb_rcvintpipe(dev, endpoint->bEndpointAddress),
253                          wacom_wac->data, wacom_wac->features->pktlen,
254                          wacom_wac->features->irq, wacom, endpoint->bInterval);
255         wacom->irq->transfer_dma = wacom->data_dma;
256         wacom->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
257
258         input_register_device(wacom->dev);
259
260         /* Ask the tablet to report tablet data. Repeat until it succeeds */
261         do {
262                 rep_data[0] = 2;
263                 rep_data[1] = 2;
264                 usb_set_report(intf, 3, 2, rep_data, 2);
265                 usb_get_report(intf, 3, 2, rep_data, 2);
266         } while (rep_data[1] != 2 && limit++ < 5);
267
268         usb_set_intfdata(intf, wacom);
269         return 0;
270
271 fail2:  usb_buffer_free(dev, 10, wacom_wac->data, wacom->data_dma);
272 fail1:  input_free_device(input_dev);
273         kfree(wacom);
274         kfree(wacom_wac);
275         return -ENOMEM;
276 }
277
278 static void wacom_disconnect(struct usb_interface *intf)
279 {
280         struct wacom *wacom = usb_get_intfdata (intf);
281
282         usb_set_intfdata(intf, NULL);
283         if (wacom) {
284                 usb_kill_urb(wacom->irq);
285                 input_unregister_device(wacom->dev);
286                 usb_free_urb(wacom->irq);
287                 usb_buffer_free(interface_to_usbdev(intf), 10, wacom->wacom_wac->data, wacom->data_dma);
288                 kfree(wacom);
289                 kfree(wacom->wacom_wac);
290         }
291 }
292
293 static struct usb_driver wacom_driver = {
294         .name =         "wacom",
295         .probe =        wacom_probe,
296         .disconnect =   wacom_disconnect,
297 };
298
299 static int __init wacom_init(void)
300 {
301         int result;
302         wacom_driver.id_table = get_device_table();
303         result = usb_register(&wacom_driver);
304         if (result == 0)
305                 info(DRIVER_VERSION ":" DRIVER_DESC);
306         return result;
307 }
308
309 static void __exit wacom_exit(void)
310 {
311         usb_deregister(&wacom_driver);
312 }
313
314 module_init(wacom_init);
315 module_exit(wacom_exit);