patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / 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 successfull tests with an EXII-5010UC
36  *
37  *****************************************************************************/
38
39 #include <linux/config.h>
40
41 #ifdef CONFIG_USB_DEBUG
42         #define DEBUG
43 #else
44         #undef DEBUG
45 #endif
46
47 #include <linux/kernel.h>
48 #include <linux/slab.h>
49 #include <linux/input.h>
50 #include <linux/module.h>
51 #include <linux/init.h>
52 #include <linux/usb.h>
53
54 #define MTOUCHUSB_MIN_XC                0x0
55 #define MTOUCHUSB_MAX_XC                0x4000
56 #define MTOUCHUSB_XC_FUZZ               0x0
57 #define MTOUCHUSB_XC_FLAT               0x0
58 #define MTOUCHUSB_MIN_YC                0x0
59 #define MTOUCHUSB_MAX_YC                0x4000
60 #define MTOUCHUSB_YC_FUZZ               0x0
61 #define MTOUCHUSB_YC_FLAT               0x0
62
63 #define MTOUCHUSB_ASYNC_REPORT          1
64 #define MTOUCHUSB_RESET                 7
65 #define MTOUCHUSB_REPORT_DATA_SIZE      11
66 #define MTOUCHUSB_REQ_CTRLLR_ID         10
67
68 #define MTOUCHUSB_GET_XC(data)          (data[8]<<8 | data[7])
69 #define MTOUCHUSB_GET_YC(data)          (data[10]<<8 | data[9])
70 #define MTOUCHUSB_GET_TOUCHED(data)     ((data[2] & 0x40) ? 1:0)
71
72 #define DRIVER_VERSION "v1.4"
73 #define DRIVER_AUTHOR "Todd E. Johnson, tejohnson@yahoo.com"
74 #define DRIVER_DESC "3M USB Touchscreen Driver"
75 #define DRIVER_LICENSE "GPL"
76
77 struct mtouch_usb {
78         unsigned char *data;
79         dma_addr_t data_dma;
80         struct urb *irq;
81         struct usb_device *udev;
82         struct input_dev input;
83         int open;
84         char name[128];
85         char phys[64];
86 };
87
88 static struct usb_device_id mtouchusb_devices [] = {
89         { USB_DEVICE(0x0596, 0x0001) },
90         { }
91 };
92
93 static void mtouchusb_irq(struct urb *urb, struct pt_regs *regs)
94 {
95         struct mtouch_usb *mtouch = urb->context;
96         int retval;
97
98         switch (urb->status) {
99                 case 0:
100                         /* success */
101                         break;
102                 case -ETIMEDOUT:
103                         /* this urb is timing out */
104                         dbg("%s - urb timed out - was the device unplugged?",
105                             __FUNCTION__);
106                         return;
107                 case -ECONNRESET:
108                 case -ENOENT:
109                 case -ESHUTDOWN:
110                         /* this urb is terminated, clean up */
111                         dbg("%s - urb shutting down with status: %d",
112                             __FUNCTION__, urb->status);
113                         return;
114                 default:
115                         dbg("%s - nonzero urb status received: %d",
116                             __FUNCTION__, urb->status);
117                         goto exit;
118         }
119
120         input_regs(&mtouch->input, regs);
121         input_report_key(&mtouch->input, BTN_TOUCH,
122                          MTOUCHUSB_GET_TOUCHED(mtouch->data));
123         input_report_abs(&mtouch->input, ABS_X,
124                          MTOUCHUSB_GET_XC(mtouch->data));
125         input_report_abs(&mtouch->input, ABS_Y,
126                          MTOUCHUSB_GET_YC(mtouch->data));
127         input_sync(&mtouch->input);
128
129 exit:
130         retval = usb_submit_urb (urb, GFP_ATOMIC);
131         if (retval)
132                 err ("%s - usb_submit_urb failed with result: %d",
133                      __FUNCTION__, retval);
134 }
135
136 static int mtouchusb_open (struct input_dev *input)
137 {
138         struct mtouch_usb *mtouch = input->private;
139
140         if (mtouch->open++)
141                 return 0;
142
143         mtouch->irq->dev = mtouch->udev;
144
145         if (usb_submit_urb (mtouch->irq, GFP_ATOMIC)) {
146                 mtouch->open--;
147                 return -EIO;
148         }
149
150         return 0;
151 }
152
153 static void mtouchusb_close (struct input_dev *input)
154 {
155         struct mtouch_usb *mtouch = input->private;
156
157         if (!--mtouch->open)
158                 usb_unlink_urb (mtouch->irq);
159 }
160
161 static int mtouchusb_alloc_buffers(struct usb_device *udev, struct mtouch_usb *mtouch)
162 {
163         dbg("%s - called", __FUNCTION__);
164
165         mtouch->data = usb_buffer_alloc(udev, MTOUCHUSB_REPORT_DATA_SIZE,
166                                         SLAB_ATOMIC, &mtouch->data_dma);
167
168         if (!mtouch->data)
169                 return -1;
170
171         return 0;
172 }
173
174 static void mtouchusb_free_buffers(struct usb_device *udev, struct mtouch_usb *mtouch)
175 {
176         dbg("%s - called", __FUNCTION__);
177
178         if (mtouch->data)
179                 usb_buffer_free(udev, MTOUCHUSB_REPORT_DATA_SIZE,
180                                 mtouch->data, mtouch->data_dma);
181 }
182
183 static int mtouchusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
184 {
185         struct mtouch_usb *mtouch;
186         struct usb_host_interface *interface;
187         struct usb_endpoint_descriptor *endpoint;
188         struct usb_device *udev = interface_to_usbdev (intf);
189         char path[64];
190         char *buf;
191         int nRet;
192
193         dbg("%s - called", __FUNCTION__);
194
195         dbg("%s - setting interface", __FUNCTION__);
196         interface = intf->cur_altsetting;
197
198         dbg("%s - setting endpoint", __FUNCTION__);
199         endpoint = &interface->endpoint[0].desc;
200
201         if (!(mtouch = kmalloc (sizeof (struct mtouch_usb), GFP_KERNEL))) {
202                 err("%s - Out of memory.", __FUNCTION__);
203                 return -ENOMEM;
204         }
205
206         memset(mtouch, 0, sizeof(struct mtouch_usb));
207         mtouch->udev = udev;
208
209         dbg("%s - allocating buffers", __FUNCTION__);
210         if (mtouchusb_alloc_buffers(udev, mtouch)) {
211                 mtouchusb_free_buffers(udev, mtouch);
212                 kfree(mtouch);
213                 return -ENOMEM;
214         }
215
216         mtouch->input.private = mtouch;
217         mtouch->input.open = mtouchusb_open;
218         mtouch->input.close = mtouchusb_close;
219
220         usb_make_path(udev, path, 64);
221         sprintf(mtouch->phys, "%s/input0", path);
222
223         mtouch->input.name = mtouch->name;
224         mtouch->input.phys = mtouch->phys;
225         mtouch->input.id.bustype = BUS_USB;
226         mtouch->input.id.vendor = udev->descriptor.idVendor;
227         mtouch->input.id.product = udev->descriptor.idProduct;
228         mtouch->input.id.version = udev->descriptor.bcdDevice;
229         mtouch->input.dev = &intf->dev;
230
231         mtouch->input.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
232         mtouch->input.absbit[0] = BIT(ABS_X) | BIT(ABS_Y);
233         mtouch->input.keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
234
235         /* Used to Scale Compensated Data and Flip Y */
236         mtouch->input.absmin[ABS_X] =  MTOUCHUSB_MIN_XC;
237         mtouch->input.absmax[ABS_X] =  MTOUCHUSB_MAX_XC;
238         mtouch->input.absfuzz[ABS_X] = MTOUCHUSB_XC_FUZZ;
239         mtouch->input.absflat[ABS_X] = MTOUCHUSB_XC_FLAT;
240         mtouch->input.absmin[ABS_Y] =  MTOUCHUSB_MIN_YC;
241         mtouch->input.absmax[ABS_Y] =  MTOUCHUSB_MAX_YC;
242         mtouch->input.absfuzz[ABS_Y] = MTOUCHUSB_YC_FUZZ;
243         mtouch->input.absflat[ABS_Y] = MTOUCHUSB_YC_FLAT;
244
245         if (!(buf = kmalloc(63, GFP_KERNEL))) {
246                 kfree(mtouch);
247                 return -ENOMEM;
248         }
249
250         if (udev->descriptor.iManufacturer &&
251             usb_string(udev, udev->descriptor.iManufacturer, buf, 63) > 0)
252                         strcat(mtouch->name, buf);
253         if (udev->descriptor.iProduct &&
254             usb_string(udev, udev->descriptor.iProduct, buf, 63) > 0)
255                         sprintf(mtouch->name, "%s %s", mtouch->name, buf);
256
257         if (!strlen(mtouch->name))
258                 sprintf(mtouch->name, "USB Touchscreen %04x:%04x",
259                         mtouch->input.id.vendor, mtouch->input.id.product);
260
261         kfree(buf);
262
263         nRet = usb_control_msg(mtouch->udev,
264                                usb_rcvctrlpipe(udev, 0),
265                                MTOUCHUSB_RESET,
266                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
267                                1,
268                                0,
269                                NULL,
270                                0,
271                                HZ * USB_CTRL_SET_TIMEOUT);
272         dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
273             __FUNCTION__, nRet);
274
275         dbg("%s - usb_alloc_urb: mtouch->irq", __FUNCTION__);
276         mtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
277         if (!mtouch->irq) {
278                 dbg("%s - usb_alloc_urb failed: mtouch->irq", __FUNCTION__);
279                 mtouchusb_free_buffers(udev, mtouch);
280                 kfree(mtouch);
281                 return -ENOMEM;
282         }
283
284         dbg("%s - usb_fill_int_urb", __FUNCTION__);
285         usb_fill_int_urb(mtouch->irq,
286                          mtouch->udev,
287                          usb_rcvintpipe(mtouch->udev, 0x81),
288                          mtouch->data,
289                          MTOUCHUSB_REPORT_DATA_SIZE,
290                          mtouchusb_irq,
291                          mtouch,
292                          endpoint->bInterval);
293
294         dbg("%s - input_register_device", __FUNCTION__);
295         input_register_device(&mtouch->input);
296
297         nRet = usb_control_msg(mtouch->udev,
298                                usb_rcvctrlpipe(udev, 0),
299                                MTOUCHUSB_ASYNC_REPORT,
300                                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
301                                1,
302                                1,
303                                NULL,
304                                0,
305                                HZ * USB_CTRL_SET_TIMEOUT);
306         dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
307             __FUNCTION__, nRet);
308
309         printk(KERN_INFO "input: %s on %s\n", mtouch->name, path);
310         usb_set_intfdata(intf, mtouch);
311
312         return 0;
313 }
314
315 static void mtouchusb_disconnect(struct usb_interface *intf)
316 {
317         struct mtouch_usb *mtouch = usb_get_intfdata (intf);
318
319         dbg("%s - called", __FUNCTION__);
320         usb_set_intfdata(intf, NULL);
321         if (mtouch) {
322                 dbg("%s - mtouch is initialized, cleaning up", __FUNCTION__);
323                 usb_unlink_urb(mtouch->irq);
324                 input_unregister_device(&mtouch->input);
325                 usb_free_urb(mtouch->irq);
326                 mtouchusb_free_buffers(interface_to_usbdev(intf), mtouch);
327                 kfree(mtouch);
328         }
329 }
330
331 MODULE_DEVICE_TABLE (usb, mtouchusb_devices);
332
333 static struct usb_driver mtouchusb_driver = {
334         .owner =      THIS_MODULE,
335         .name =       "mtouchusb",
336         .probe =      mtouchusb_probe,
337         .disconnect = mtouchusb_disconnect,
338         .id_table =   mtouchusb_devices,
339 };
340
341 static int __init mtouchusb_init(void) {
342         dbg("%s - called", __FUNCTION__);
343         return usb_register(&mtouchusb_driver);
344 }
345
346 static void __exit mtouchusb_cleanup(void) {
347         dbg("%s - called", __FUNCTION__);
348         usb_deregister(&mtouchusb_driver);
349 }
350
351 module_init(mtouchusb_init);
352 module_exit(mtouchusb_cleanup);
353
354 MODULE_AUTHOR( DRIVER_AUTHOR );
355 MODULE_DESCRIPTION( DRIVER_DESC );
356 MODULE_LICENSE("GPL");