Merge git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[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 #include <linux/kernel.h>
44 #include <linux/slab.h>
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/usb/input.h>
48
49 /* only an 8 byte buffer necessary for a single packet */
50 #define ITM_BUFSIZE                     8
51 #define PATH_SIZE                       64
52
53 #define USB_VENDOR_ID_ITMINC            0x0403
54 #define USB_PRODUCT_ID_TOUCHPANEL       0xf9e9
55
56 #define DRIVER_AUTHOR "Hans-Christian Egtvedt <hc@mivu.no>"
57 #define DRIVER_VERSION "v1.2.1"
58 #define DRIVER_DESC "USB ITM Inc Touch Panel Driver"
59 #define DRIVER_LICENSE "GPL"
60
61 MODULE_AUTHOR( DRIVER_AUTHOR );
62 MODULE_DESCRIPTION( DRIVER_DESC );
63 MODULE_LICENSE( DRIVER_LICENSE );
64
65 struct itmtouch_dev {
66         struct usb_device       *usbdev; /* usb device */
67         struct input_dev        *inputdev; /* input device */
68         struct urb              *readurb; /* urb */
69         char                    rbuf[ITM_BUFSIZE]; /* data */
70         int                     users;
71         char name[128];
72         char phys[64];
73 };
74
75 static struct usb_device_id itmtouch_ids [] = {
76         { USB_DEVICE(USB_VENDOR_ID_ITMINC, USB_PRODUCT_ID_TOUCHPANEL) },
77         { }
78 };
79
80 static void itmtouch_irq(struct urb *urb, struct pt_regs *regs)
81 {
82         struct itmtouch_dev *itmtouch = urb->context;
83         unsigned char *data = urb->transfer_buffer;
84         struct input_dev *dev = itmtouch->inputdev;
85         int retval;
86
87         switch (urb->status) {
88         case 0:
89                 /* success */
90                 break;
91         case -ETIMEDOUT:
92                 /* this urb is timing out */
93                 dbg("%s - urb timed out - was the device unplugged?",
94                     __FUNCTION__);
95                 return;
96         case -ECONNRESET:
97         case -ENOENT:
98         case -ESHUTDOWN:
99                 /* this urb is terminated, clean up */
100                 dbg("%s - urb shutting down with status: %d",
101                     __FUNCTION__, urb->status);
102                 return;
103         default:
104                 dbg("%s - nonzero urb status received: %d",
105                     __FUNCTION__, urb->status);
106                 goto exit;
107         }
108
109         input_regs(dev, regs);
110
111         /* if pressure has been released, then don't report X/Y */
112         if (data[7] & 0x20) {
113                 input_report_abs(dev, ABS_X, (data[0] & 0x1F) << 7 | (data[3] & 0x7F));
114                 input_report_abs(dev, ABS_Y, (data[1] & 0x1F) << 7 | (data[4] & 0x7F));
115         }
116
117         input_report_abs(dev, ABS_PRESSURE, (data[2] & 1) << 7 | (data[5] & 0x7F));
118         input_report_key(dev, BTN_TOUCH, ~data[7] & 0x20);
119         input_sync(dev);
120
121 exit:
122         retval = usb_submit_urb (urb, GFP_ATOMIC);
123         if (retval)
124                 printk(KERN_ERR "%s - usb_submit_urb failed with result: %d",
125                                 __FUNCTION__, retval);
126 }
127
128 static int itmtouch_open(struct input_dev *input)
129 {
130         struct itmtouch_dev *itmtouch = input->private;
131
132         itmtouch->readurb->dev = itmtouch->usbdev;
133
134         if (usb_submit_urb(itmtouch->readurb, GFP_KERNEL))
135                 return -EIO;
136
137         return 0;
138 }
139
140 static void itmtouch_close(struct input_dev *input)
141 {
142         struct itmtouch_dev *itmtouch = input->private;
143
144         usb_kill_urb(itmtouch->readurb);
145 }
146
147 static int itmtouch_probe(struct usb_interface *intf, const struct usb_device_id *id)
148 {
149         struct itmtouch_dev *itmtouch;
150         struct input_dev *input_dev;
151         struct usb_host_interface *interface;
152         struct usb_endpoint_descriptor *endpoint;
153         struct usb_device *udev = interface_to_usbdev(intf);
154         unsigned int pipe;
155         unsigned int maxp;
156
157         interface = intf->cur_altsetting;
158         endpoint = &interface->endpoint[0].desc;
159
160         itmtouch = kzalloc(sizeof(struct itmtouch_dev), GFP_KERNEL);
161         input_dev = input_allocate_device();
162         if (!itmtouch || !input_dev) {
163                 err("%s - Out of memory.", __FUNCTION__);
164                 goto fail;
165         }
166
167         itmtouch->usbdev = udev;
168         itmtouch->inputdev = input_dev;
169
170         if (udev->manufacturer)
171                 strlcpy(itmtouch->name, udev->manufacturer, sizeof(itmtouch->name));
172
173         if (udev->product) {
174                 if (udev->manufacturer)
175                         strlcat(itmtouch->name, " ", sizeof(itmtouch->name));
176                 strlcat(itmtouch->name, udev->product, sizeof(itmtouch->name));
177         }
178
179         if (!strlen(itmtouch->name))
180                 sprintf(itmtouch->name, "USB ITM touchscreen");
181
182         usb_make_path(udev, itmtouch->phys, sizeof(itmtouch->phys));
183         strlcpy(itmtouch->phys, "/input0", sizeof(itmtouch->phys));
184
185         input_dev->name = itmtouch->name;
186         input_dev->phys = itmtouch->phys;
187         usb_to_input_id(udev, &input_dev->id);
188         input_dev->cdev.dev = &intf->dev;
189         input_dev->private = itmtouch;
190
191         input_dev->open = itmtouch_open;
192         input_dev->close = itmtouch_close;
193
194         input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
195         input_dev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
196         input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
197
198         /* device limits */
199         /* as specified by the ITM datasheet, X and Y are 12bit,
200          * Z (pressure) is 8 bit. However, the fields are defined up
201          * to 14 bits for future possible expansion.
202          */
203         input_set_abs_params(input_dev, ABS_X, 0, 0x0FFF, 2, 0);
204         input_set_abs_params(input_dev, ABS_Y, 0, 0x0FFF, 2, 0);
205         input_set_abs_params(input_dev, ABS_PRESSURE, 0, 0xFF, 2, 0);
206
207         /* initialise the URB so we can read from the transport stream */
208         pipe = usb_rcvintpipe(itmtouch->usbdev, endpoint->bEndpointAddress);
209         maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
210
211         if (maxp > ITM_BUFSIZE)
212                 maxp = ITM_BUFSIZE;
213
214         itmtouch->readurb = usb_alloc_urb(0, GFP_KERNEL);
215         if (!itmtouch->readurb) {
216                 dbg("%s - usb_alloc_urb failed: itmtouch->readurb", __FUNCTION__);
217                 goto fail;
218         }
219
220         usb_fill_int_urb(itmtouch->readurb, itmtouch->usbdev, pipe, itmtouch->rbuf,
221                          maxp, itmtouch_irq, itmtouch, endpoint->bInterval);
222
223         input_register_device(itmtouch->inputdev);
224
225         usb_set_intfdata(intf, itmtouch);
226
227         return 0;
228
229  fail:  input_free_device(input_dev);
230         kfree(itmtouch);
231         return -ENOMEM;
232 }
233
234 static void itmtouch_disconnect(struct usb_interface *intf)
235 {
236         struct itmtouch_dev *itmtouch = usb_get_intfdata(intf);
237
238         usb_set_intfdata(intf, NULL);
239
240         if (itmtouch) {
241                 input_unregister_device(itmtouch->inputdev);
242                 usb_kill_urb(itmtouch->readurb);
243                 usb_free_urb(itmtouch->readurb);
244                 kfree(itmtouch);
245         }
246 }
247
248 MODULE_DEVICE_TABLE(usb, itmtouch_ids);
249
250 static struct usb_driver itmtouch_driver = {
251         .name =         "itmtouch",
252         .probe =        itmtouch_probe,
253         .disconnect =   itmtouch_disconnect,
254         .id_table =     itmtouch_ids,
255 };
256
257 static int __init itmtouch_init(void)
258 {
259         info(DRIVER_DESC " " DRIVER_VERSION);
260         info(DRIVER_AUTHOR);
261         return usb_register(&itmtouch_driver);
262 }
263
264 static void __exit itmtouch_exit(void)
265 {
266         usb_deregister(&itmtouch_driver);
267 }
268
269 module_init(itmtouch_init);
270 module_exit(itmtouch_exit);