Input: HID - add a quirk for Aashima Trust (06d6:0025) gamepad
[safe/jmp/linux-2.6] / drivers / usb / input / mtouchusb.c
1 /******************************************************************************
2  * mtouchusb.c  --  Driver for Microtouch (Now 3M) USB Touchscreens
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * Based upon original work by Radoslaw Garbacz (usb-support@ite.pl)
19  *  (http://freshmeat.net/projects/3mtouchscreendriver)
20  *
21  * History
22  *
23  *  0.3 & 0.4  2002 (TEJ) tejohnson@yahoo.com
24  *    Updated to 2.4.18, then 2.4.19
25  *    Old version still relied on stealing a minor
26  *
27  *  0.5  02/26/2004 (TEJ) tejohnson@yahoo.com
28  *    Complete rewrite using Linux Input in 2.6.3
29  *    Unfortunately no calibration support at this time
30  *
31  *  1.4 04/25/2004 (TEJ) tejohnson@yahoo.com
32  *    Changed reset from standard USB dev reset to vendor reset
33  *    Changed data sent to host from compensated to raw coordinates
34  *    Eliminated vendor/product module params
35  *    Performed multiple successful tests with an EXII-5010UC
36  *
37  *  1.5 02/27/2005 ddstreet@ieee.org
38  *    Added module parameter to select raw or hw-calibrated coordinate reporting
39  *
40  *****************************************************************************/
41
42 #include <linux/config.h>
43
44 #ifdef CONFIG_USB_DEBUG
45         #define DEBUG
46 #else
47         #undef DEBUG
48 #endif
49
50 #include <linux/kernel.h>
51 #include <linux/slab.h>
52 #include <linux/input.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/usb.h>
56 #include <linux/usb_input.h>
57
58 #define MTOUCHUSB_MIN_XC                0x0
59 #define MTOUCHUSB_MAX_RAW_XC            0x4000
60 #define MTOUCHUSB_MAX_CALIB_XC          0xffff
61 #define MTOUCHUSB_XC_FUZZ               0x0
62 #define MTOUCHUSB_XC_FLAT               0x0
63 #define MTOUCHUSB_MIN_YC                0x0
64 #define MTOUCHUSB_MAX_RAW_YC            0x4000
65 #define MTOUCHUSB_MAX_CALIB_YC          0xffff
66 #define MTOUCHUSB_YC_FUZZ               0x0
67 #define MTOUCHUSB_YC_FLAT               0x0
68
69 #define MTOUCHUSB_ASYNC_REPORT          1
70 #define MTOUCHUSB_RESET                 7
71 #define MTOUCHUSB_REPORT_DATA_SIZE      11
72 #define MTOUCHUSB_REQ_CTRLLR_ID         10
73
74 #define MTOUCHUSB_GET_RAW_XC(data)      (data[8]<<8 | data[7])
75 #define MTOUCHUSB_GET_CALIB_XC(data)    (data[4]<<8 | data[3])
76 #define MTOUCHUSB_GET_RAW_YC(data)      (data[10]<<8 | data[9])
77 #define MTOUCHUSB_GET_CALIB_YC(data)    (data[6]<<8 | data[5])
78 #define MTOUCHUSB_GET_XC(data)          (raw_coordinates ? \
79                                          MTOUCHUSB_GET_RAW_XC(data) : \
80                                          MTOUCHUSB_GET_CALIB_XC(data))
81 #define MTOUCHUSB_GET_YC(data)          (raw_coordinates ? \
82                                          MTOUCHUSB_GET_RAW_YC(data) : \
83                                          MTOUCHUSB_GET_CALIB_YC(data))
84 #define MTOUCHUSB_GET_TOUCHED(data)     ((data[2] & 0x40) ? 1:0)
85
86 #define DRIVER_VERSION "v1.5"
87 #define DRIVER_AUTHOR "Todd E. Johnson, tejohnson@yahoo.com"
88 #define DRIVER_DESC "3M USB Touchscreen Driver"
89 #define DRIVER_LICENSE "GPL"
90
91 static int raw_coordinates = 1;
92
93 module_param(raw_coordinates, bool, S_IRUGO | S_IWUSR);
94 MODULE_PARM_DESC(raw_coordinates, "report raw coordinate values (y, default) or hardware-calibrated coordinate values (n)");
95
96 struct mtouch_usb {
97         unsigned char *data;
98         dma_addr_t data_dma;
99         struct urb *irq;
100         struct usb_device *udev;
101         struct input_dev input;
102         char name[128];
103         char phys[64];
104 };
105
106 static struct usb_device_id mtouchusb_devices[] = {
107         { USB_DEVICE(0x0596, 0x0001) },
108         { }
109 };
110
111 static void mtouchusb_irq(struct urb *urb, struct pt_regs *regs)
112 {
113         struct mtouch_usb *mtouch = urb->context;
114         int retval;
115
116         switch (urb->status) {
117         case 0:
118                 /* success */
119                 break;
120         case -ETIMEDOUT:
121                 /* this urb is timing out */
122                 dbg("%s - urb timed out - was the device unplugged?",
123                     __FUNCTION__);
124                 return;
125         case -ECONNRESET:
126         case -ENOENT:
127         case -ESHUTDOWN:
128                 /* this urb is terminated, clean up */
129                 dbg("%s - urb shutting down with status: %d",
130                     __FUNCTION__, urb->status);
131                 return;
132         default:
133                 dbg("%s - nonzero urb status received: %d",
134                     __FUNCTION__, urb->status);
135                 goto exit;
136         }
137
138         input_regs(&mtouch->input, regs);
139         input_report_key(&mtouch->input, BTN_TOUCH,
140                          MTOUCHUSB_GET_TOUCHED(mtouch->data));
141         input_report_abs(&mtouch->input, ABS_X, MTOUCHUSB_GET_XC(mtouch->data));
142         input_report_abs(&mtouch->input, ABS_Y,
143                          (raw_coordinates ? MTOUCHUSB_MAX_RAW_YC : MTOUCHUSB_MAX_CALIB_YC)
144                          - MTOUCHUSB_GET_YC(mtouch->data));
145         input_sync(&mtouch->input);
146
147 exit:
148         retval = usb_submit_urb(urb, GFP_ATOMIC);
149         if (retval)
150                 err("%s - usb_submit_urb failed with result: %d",
151                     __FUNCTION__, retval);
152 }
153
154 static int mtouchusb_open(struct input_dev *input)
155 {
156         struct mtouch_usb *mtouch = input->private;
157
158         mtouch->irq->dev = mtouch->udev;
159
160         if (usb_submit_urb(mtouch->irq, GFP_ATOMIC))
161                 return -EIO;
162
163         return 0;
164 }
165
166 static void mtouchusb_close(struct input_dev *input)
167 {
168         struct mtouch_usb *mtouch = input->private;
169
170         usb_kill_urb(mtouch->irq);
171 }
172
173 static int mtouchusb_alloc_buffers(struct usb_device *udev, struct mtouch_usb *mtouch)
174 {
175         dbg("%s - called", __FUNCTION__);
176
177         mtouch->data = usb_buffer_alloc(udev, MTOUCHUSB_REPORT_DATA_SIZE,
178                                         SLAB_ATOMIC, &mtouch->data_dma);
179
180         if (!mtouch->data)
181                 return -1;
182
183         return 0;
184 }
185
186 static void mtouchusb_free_buffers(struct usb_device *udev, struct mtouch_usb *mtouch)
187 {
188         dbg("%s - called", __FUNCTION__);
189
190         if (mtouch->data)
191                 usb_buffer_free(udev, MTOUCHUSB_REPORT_DATA_SIZE,
192                                 mtouch->data, mtouch->data_dma);
193 }
194
195 static int mtouchusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
196 {
197         struct mtouch_usb *mtouch;
198         struct usb_host_interface *interface;
199         struct usb_endpoint_descriptor *endpoint;
200         struct usb_device *udev = interface_to_usbdev(intf);
201         char path[64];
202         int nRet;
203
204         dbg("%s - called", __FUNCTION__);
205
206         dbg("%s - setting interface", __FUNCTION__);
207         interface = intf->cur_altsetting;
208
209         dbg("%s - setting endpoint", __FUNCTION__);
210         endpoint = &interface->endpoint[0].desc;
211
212         if (!(mtouch = kmalloc(sizeof(struct mtouch_usb), GFP_KERNEL))) {
213                 err("%s - Out of memory.", __FUNCTION__);
214                 return -ENOMEM;
215         }
216
217         memset(mtouch, 0, sizeof(struct mtouch_usb));
218         mtouch->udev = udev;
219
220         dbg("%s - allocating buffers", __FUNCTION__);
221         if (mtouchusb_alloc_buffers(udev, mtouch)) {
222                 mtouchusb_free_buffers(udev, mtouch);
223                 kfree(mtouch);
224                 return -ENOMEM;
225         }
226
227         mtouch->input.private = mtouch;
228         mtouch->input.open = mtouchusb_open;
229         mtouch->input.close = mtouchusb_close;
230
231         usb_make_path(udev, path, 64);
232         sprintf(mtouch->phys, "%s/input0", path);
233
234         mtouch->input.name = mtouch->name;
235         mtouch->input.phys = mtouch->phys;
236         usb_to_input_id(udev, &mtouch->input.id);
237         mtouch->input.dev = &intf->dev;
238
239         mtouch->input.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
240         mtouch->input.absbit[0] = BIT(ABS_X) | BIT(ABS_Y);
241         mtouch->input.keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
242
243         /* Used to Scale Compensated Data and Flip Y */
244         mtouch->input.absmin[ABS_X] = MTOUCHUSB_MIN_XC;
245         mtouch->input.absmax[ABS_X] = raw_coordinates ?
246                                         MTOUCHUSB_MAX_RAW_XC : MTOUCHUSB_MAX_CALIB_XC;
247         mtouch->input.absfuzz[ABS_X] = MTOUCHUSB_XC_FUZZ;
248         mtouch->input.absflat[ABS_X] = MTOUCHUSB_XC_FLAT;
249         mtouch->input.absmin[ABS_Y] = MTOUCHUSB_MIN_YC;
250         mtouch->input.absmax[ABS_Y] = raw_coordinates ?
251                                         MTOUCHUSB_MAX_RAW_YC : MTOUCHUSB_MAX_CALIB_YC;
252         mtouch->input.absfuzz[ABS_Y] = MTOUCHUSB_YC_FUZZ;
253         mtouch->input.absflat[ABS_Y] = MTOUCHUSB_YC_FLAT;
254
255         if (udev->manufacturer)
256                 strcat(mtouch->name, udev->manufacturer);
257         if (udev->product)
258                 sprintf(mtouch->name, "%s %s", mtouch->name, udev->product);
259
260         if (!strlen(mtouch->name))
261                 sprintf(mtouch->name, "USB Touchscreen %04x:%04x",
262                         mtouch->input.id.vendor, mtouch->input.id.product);
263
264         nRet = usb_control_msg(mtouch->udev, usb_rcvctrlpipe(udev, 0),
265                                MTOUCHUSB_RESET,
266                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
267                                1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
268         dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
269             __FUNCTION__, nRet);
270
271         dbg("%s - usb_alloc_urb: mtouch->irq", __FUNCTION__);
272         mtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
273         if (!mtouch->irq) {
274                 dbg("%s - usb_alloc_urb failed: mtouch->irq", __FUNCTION__);
275                 mtouchusb_free_buffers(udev, mtouch);
276                 kfree(mtouch);
277                 return -ENOMEM;
278         }
279
280         dbg("%s - usb_fill_int_urb", __FUNCTION__);
281         usb_fill_int_urb(mtouch->irq, mtouch->udev,
282                          usb_rcvintpipe(mtouch->udev, 0x81),
283                          mtouch->data, MTOUCHUSB_REPORT_DATA_SIZE,
284                          mtouchusb_irq, mtouch, endpoint->bInterval);
285
286         dbg("%s - input_register_device", __FUNCTION__);
287         input_register_device(&mtouch->input);
288
289         nRet = usb_control_msg(mtouch->udev, usb_rcvctrlpipe(udev, 0),
290                                MTOUCHUSB_ASYNC_REPORT,
291                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
292                                1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
293         dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
294             __FUNCTION__, nRet);
295
296         printk(KERN_INFO "input: %s on %s\n", mtouch->name, path);
297         usb_set_intfdata(intf, mtouch);
298
299         return 0;
300 }
301
302 static void mtouchusb_disconnect(struct usb_interface *intf)
303 {
304         struct mtouch_usb *mtouch = usb_get_intfdata(intf);
305
306         dbg("%s - called", __FUNCTION__);
307         usb_set_intfdata(intf, NULL);
308         if (mtouch) {
309                 dbg("%s - mtouch is initialized, cleaning up", __FUNCTION__);
310                 usb_kill_urb(mtouch->irq);
311                 input_unregister_device(&mtouch->input);
312                 usb_free_urb(mtouch->irq);
313                 mtouchusb_free_buffers(interface_to_usbdev(intf), mtouch);
314                 kfree(mtouch);
315         }
316 }
317
318 MODULE_DEVICE_TABLE(usb, mtouchusb_devices);
319
320 static struct usb_driver mtouchusb_driver = {
321         .owner          = THIS_MODULE,
322         .name           = "mtouchusb",
323         .probe          = mtouchusb_probe,
324         .disconnect     = mtouchusb_disconnect,
325         .id_table       = mtouchusb_devices,
326 };
327
328 static int __init mtouchusb_init(void)
329 {
330         dbg("%s - called", __FUNCTION__);
331         return usb_register(&mtouchusb_driver);
332 }
333
334 static void __exit mtouchusb_cleanup(void)
335 {
336         dbg("%s - called", __FUNCTION__);
337         usb_deregister(&mtouchusb_driver);
338 }
339
340 module_init(mtouchusb_init);
341 module_exit(mtouchusb_cleanup);
342
343 MODULE_AUTHOR(DRIVER_AUTHOR);
344 MODULE_DESCRIPTION(DRIVER_DESC);
345 MODULE_LICENSE("GPL");