Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/driver-2.6
[safe/jmp/linux-2.6] / drivers / usb / input / itmtouch.c
1 /******************************************************************************
2  * itmtouch.c  --  Driver for ITM touchscreen panel
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 Chris Collins <xfire-itmtouch@xware.cx>.
19  *
20  * Kudos to ITM for providing me with the datasheet for the panel,
21  * even though it was a day later than I had finished writing this
22  * driver.
23  *
24  * It has meant that I've been able to correct my interpretation of the
25  * protocol packets however.
26  *
27  * CC -- 2003/9/29
28  *
29  * History
30  * 1.0 & 1.1  2003 (CC) vojtech@suse.cz
31  *   Original version for 2.4.x kernels
32  *
33  * 1.2  02/03/2005 (HCE) hc@mivu.no
34  *   Complete rewrite to support Linux 2.6.10, thanks to mtouchusb.c for hints.
35  *   Unfortunately no calibration support at this time.
36  *
37  * 1.2.1  09/03/2005 (HCE) hc@mivu.no
38  *   Code cleanup and adjusting syntax to start matching kernel standards
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 /* only an 8 byte buffer necessary for a single packet */
59 #define ITM_BUFSIZE                     8
60 #define PATH_SIZE                       64
61
62 #define USB_VENDOR_ID_ITMINC            0x0403
63 #define USB_PRODUCT_ID_TOUCHPANEL       0xf9e9
64
65 #define DRIVER_AUTHOR "Hans-Christian Egtvedt <hc@mivu.no>"
66 #define DRIVER_VERSION "v1.2.1"
67 #define DRIVER_DESC "USB ITM Inc Touch Panel Driver"
68 #define DRIVER_LICENSE "GPL"
69
70 MODULE_AUTHOR( DRIVER_AUTHOR );
71 MODULE_DESCRIPTION( DRIVER_DESC );
72 MODULE_LICENSE( DRIVER_LICENSE );
73
74 struct itmtouch_dev {
75         struct usb_device       *usbdev; /* usb device */
76         struct input_dev        *inputdev; /* input device */
77         struct urb              *readurb; /* urb */
78         char                    rbuf[ITM_BUFSIZE]; /* data */
79         int                     users;
80         char name[128];
81         char phys[64];
82 };
83
84 static struct usb_device_id itmtouch_ids [] = {
85         { USB_DEVICE(USB_VENDOR_ID_ITMINC, USB_PRODUCT_ID_TOUCHPANEL) },
86         { }
87 };
88
89 static void itmtouch_irq(struct urb *urb, struct pt_regs *regs)
90 {
91         struct itmtouch_dev *itmtouch = urb->context;
92         unsigned char *data = urb->transfer_buffer;
93         struct input_dev *dev = itmtouch->inputdev;
94         int retval;
95
96         switch (urb->status) {
97         case 0:
98                 /* success */
99                 break;
100         case -ETIMEDOUT:
101                 /* this urb is timing out */
102                 dbg("%s - urb timed out - was the device unplugged?",
103                     __FUNCTION__);
104                 return;
105         case -ECONNRESET:
106         case -ENOENT:
107         case -ESHUTDOWN:
108                 /* this urb is terminated, clean up */
109                 dbg("%s - urb shutting down with status: %d",
110                     __FUNCTION__, urb->status);
111                 return;
112         default:
113                 dbg("%s - nonzero urb status received: %d",
114                     __FUNCTION__, urb->status);
115                 goto exit;
116         }
117
118         input_regs(dev, regs);
119
120         /* if pressure has been released, then don't report X/Y */
121         if (data[7] & 0x20) {
122                 input_report_abs(dev, ABS_X, (data[0] & 0x1F) << 7 | (data[3] & 0x7F));
123                 input_report_abs(dev, ABS_Y, (data[1] & 0x1F) << 7 | (data[4] & 0x7F));
124         }
125
126         input_report_abs(dev, ABS_PRESSURE, (data[2] & 1) << 7 | (data[5] & 0x7F));
127         input_report_key(dev, BTN_TOUCH, ~data[7] & 0x20);
128         input_sync(dev);
129
130 exit:
131         retval = usb_submit_urb (urb, GFP_ATOMIC);
132         if (retval)
133                 printk(KERN_ERR "%s - usb_submit_urb failed with result: %d",
134                                 __FUNCTION__, retval);
135 }
136
137 static int itmtouch_open(struct input_dev *input)
138 {
139         struct itmtouch_dev *itmtouch = input->private;
140
141         itmtouch->readurb->dev = itmtouch->usbdev;
142
143         if (usb_submit_urb(itmtouch->readurb, GFP_KERNEL))
144                 return -EIO;
145
146         return 0;
147 }
148
149 static void itmtouch_close(struct input_dev *input)
150 {
151         struct itmtouch_dev *itmtouch = input->private;
152
153         usb_kill_urb(itmtouch->readurb);
154 }
155
156 static int itmtouch_probe(struct usb_interface *intf, const struct usb_device_id *id)
157 {
158         struct itmtouch_dev *itmtouch;
159         struct input_dev *input_dev;
160         struct usb_host_interface *interface;
161         struct usb_endpoint_descriptor *endpoint;
162         struct usb_device *udev = interface_to_usbdev(intf);
163         unsigned int pipe;
164         unsigned int maxp;
165
166         interface = intf->cur_altsetting;
167         endpoint = &interface->endpoint[0].desc;
168
169         itmtouch = kzalloc(sizeof(struct itmtouch_dev), GFP_KERNEL);
170         input_dev = input_allocate_device();
171         if (!itmtouch || !input_dev) {
172                 err("%s - Out of memory.", __FUNCTION__);
173                 goto fail;
174         }
175
176         itmtouch->usbdev = udev;
177         itmtouch->inputdev = input_dev;
178
179         if (udev->manufacturer)
180                 strlcpy(itmtouch->name, udev->manufacturer, sizeof(itmtouch->name));
181
182         if (udev->product) {
183                 if (udev->manufacturer)
184                         strlcat(itmtouch->name, " ", sizeof(itmtouch->name));
185                 strlcat(itmtouch->name, udev->product, sizeof(itmtouch->name));
186         }
187
188         if (!strlen(itmtouch->name))
189                 sprintf(itmtouch->name, "USB ITM touchscreen");
190
191         usb_make_path(udev, itmtouch->phys, sizeof(itmtouch->phys));
192         strlcpy(itmtouch->phys, "/input0", sizeof(itmtouch->phys));
193
194         input_dev->name = itmtouch->name;
195         input_dev->phys = itmtouch->phys;
196         usb_to_input_id(udev, &input_dev->id);
197         input_dev->cdev.dev = &intf->dev;
198         input_dev->private = itmtouch;
199
200         input_dev->open = itmtouch_open;
201         input_dev->close = itmtouch_close;
202
203         input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
204         input_dev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
205         input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
206
207         /* device limits */
208         /* as specified by the ITM datasheet, X and Y are 12bit,
209          * Z (pressure) is 8 bit. However, the fields are defined up
210          * to 14 bits for future possible expansion.
211          */
212         input_set_abs_params(input_dev, ABS_X, 0, 0x0FFF, 2, 0);
213         input_set_abs_params(input_dev, ABS_Y, 0, 0x0FFF, 2, 0);
214         input_set_abs_params(input_dev, ABS_PRESSURE, 0, 0xFF, 2, 0);
215
216         /* initialise the URB so we can read from the transport stream */
217         pipe = usb_rcvintpipe(itmtouch->usbdev, endpoint->bEndpointAddress);
218         maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
219
220         if (maxp > ITM_BUFSIZE)
221                 maxp = ITM_BUFSIZE;
222
223         itmtouch->readurb = usb_alloc_urb(0, GFP_KERNEL);
224         if (!itmtouch->readurb) {
225                 dbg("%s - usb_alloc_urb failed: itmtouch->readurb", __FUNCTION__);
226                 goto fail;
227         }
228
229         usb_fill_int_urb(itmtouch->readurb, itmtouch->usbdev, pipe, itmtouch->rbuf,
230                          maxp, itmtouch_irq, itmtouch, endpoint->bInterval);
231
232         input_register_device(itmtouch->inputdev);
233
234         usb_set_intfdata(intf, itmtouch);
235
236         return 0;
237
238  fail:  input_free_device(input_dev);
239         kfree(itmtouch);
240         return -ENOMEM;
241 }
242
243 static void itmtouch_disconnect(struct usb_interface *intf)
244 {
245         struct itmtouch_dev *itmtouch = usb_get_intfdata(intf);
246
247         usb_set_intfdata(intf, NULL);
248
249         if (itmtouch) {
250                 input_unregister_device(itmtouch->inputdev);
251                 usb_kill_urb(itmtouch->readurb);
252                 usb_free_urb(itmtouch->readurb);
253                 kfree(itmtouch);
254         }
255 }
256
257 MODULE_DEVICE_TABLE(usb, itmtouch_ids);
258
259 static struct usb_driver itmtouch_driver = {
260         .owner =        THIS_MODULE,
261         .name =         "itmtouch",
262         .probe =        itmtouch_probe,
263         .disconnect =   itmtouch_disconnect,
264         .id_table =     itmtouch_ids,
265 };
266
267 static int __init itmtouch_init(void)
268 {
269         info(DRIVER_DESC " " DRIVER_VERSION);
270         info(DRIVER_AUTHOR);
271         return usb_register(&itmtouch_driver);
272 }
273
274 static void __exit itmtouch_exit(void)
275 {
276         usb_deregister(&itmtouch_driver);
277 }
278
279 module_init(itmtouch_init);
280 module_exit(itmtouch_exit);