Merge branch 'halasa-hdlc' of git://git.tuxdriver.com/git/netdev-jwl
[powerpc.git] / drivers / usb / input / touchkitusb.c
1 /******************************************************************************
2  * touchkitusb.c  --  Driver for eGalax TouchKit USB Touchscreens
3  *
4  * Copyright (C) 2004 by Daniel Ritz
5  * Copyright (C) by Todd E. Johnson (mtouchusb.c)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * Based upon mtouchusb.c
22  *
23  *****************************************************************************/
24
25 //#define DEBUG
26
27 #include <linux/config.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/input.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/usb.h>
34 #include <linux/usb_input.h>
35
36 #define TOUCHKIT_MIN_XC                 0x0
37 #define TOUCHKIT_MAX_XC                 0x07ff
38 #define TOUCHKIT_XC_FUZZ                0x0
39 #define TOUCHKIT_XC_FLAT                0x0
40 #define TOUCHKIT_MIN_YC                 0x0
41 #define TOUCHKIT_MAX_YC                 0x07ff
42 #define TOUCHKIT_YC_FUZZ                0x0
43 #define TOUCHKIT_YC_FLAT                0x0
44 #define TOUCHKIT_REPORT_DATA_SIZE       8
45
46 #define TOUCHKIT_DOWN                   0x01
47 #define TOUCHKIT_POINT_TOUCH            0x81
48 #define TOUCHKIT_POINT_NOTOUCH          0x80
49
50 #define TOUCHKIT_GET_TOUCHED(dat)       ((((dat)[0]) & TOUCHKIT_DOWN) ? 1 : 0)
51 #define TOUCHKIT_GET_X(dat)             (((dat)[3] << 7) | (dat)[4])
52 #define TOUCHKIT_GET_Y(dat)             (((dat)[1] << 7) | (dat)[2])
53
54 #define DRIVER_VERSION                  "v0.1"
55 #define DRIVER_AUTHOR                   "Daniel Ritz <daniel.ritz@gmx.ch>"
56 #define DRIVER_DESC                     "eGalax TouchKit USB HID Touchscreen Driver"
57
58 static int swap_xy;
59 module_param(swap_xy, bool, 0644);
60 MODULE_PARM_DESC(swap_xy, "If set X and Y axes are swapped.");
61
62 struct touchkit_usb {
63         unsigned char *data;
64         dma_addr_t data_dma;
65         struct urb *irq;
66         struct usb_device *udev;
67         struct input_dev *input;
68         char name[128];
69         char phys[64];
70 };
71
72 static struct usb_device_id touchkit_devices[] = {
73         {USB_DEVICE(0x3823, 0x0001)},
74         {USB_DEVICE(0x0123, 0x0001)},
75         {USB_DEVICE(0x0eef, 0x0001)},
76         {USB_DEVICE(0x0eef, 0x0002)},
77         {}
78 };
79
80 static void touchkit_irq(struct urb *urb, struct pt_regs *regs)
81 {
82         struct touchkit_usb *touchkit = urb->context;
83         int retval;
84         int x, y;
85
86         switch (urb->status) {
87         case 0:
88                 /* success */
89                 break;
90         case -ETIMEDOUT:
91                 /* this urb is timing out */
92                 dbg("%s - urb timed out - was the device unplugged?",
93                     __FUNCTION__);
94                 return;
95         case -ECONNRESET:
96         case -ENOENT:
97         case -ESHUTDOWN:
98                 /* this urb is terminated, clean up */
99                 dbg("%s - urb shutting down with status: %d",
100                     __FUNCTION__, urb->status);
101                 return;
102         default:
103                 dbg("%s - nonzero urb status received: %d",
104                     __FUNCTION__, urb->status);
105                 goto exit;
106         }
107
108         if (swap_xy) {
109                 y = TOUCHKIT_GET_X(touchkit->data);
110                 x = TOUCHKIT_GET_Y(touchkit->data);
111         } else {
112                 x = TOUCHKIT_GET_X(touchkit->data);
113                 y = TOUCHKIT_GET_Y(touchkit->data);
114         }
115
116         input_regs(touchkit->input, regs);
117         input_report_key(touchkit->input, BTN_TOUCH,
118                          TOUCHKIT_GET_TOUCHED(touchkit->data));
119         input_report_abs(touchkit->input, ABS_X, x);
120         input_report_abs(touchkit->input, ABS_Y, y);
121         input_sync(touchkit->input);
122
123 exit:
124         retval = usb_submit_urb(urb, GFP_ATOMIC);
125         if (retval)
126                 err("%s - usb_submit_urb failed with result: %d",
127                     __FUNCTION__, retval);
128 }
129
130 static int touchkit_open(struct input_dev *input)
131 {
132         struct touchkit_usb *touchkit = input->private;
133
134         touchkit->irq->dev = touchkit->udev;
135
136         if (usb_submit_urb(touchkit->irq, GFP_ATOMIC))
137                 return -EIO;
138
139         return 0;
140 }
141
142 static void touchkit_close(struct input_dev *input)
143 {
144         struct touchkit_usb *touchkit = input->private;
145
146         usb_kill_urb(touchkit->irq);
147 }
148
149 static int touchkit_alloc_buffers(struct usb_device *udev,
150                                   struct touchkit_usb *touchkit)
151 {
152         touchkit->data = usb_buffer_alloc(udev, TOUCHKIT_REPORT_DATA_SIZE,
153                                           SLAB_ATOMIC, &touchkit->data_dma);
154
155         if (!touchkit->data)
156                 return -1;
157
158         return 0;
159 }
160
161 static void touchkit_free_buffers(struct usb_device *udev,
162                                   struct touchkit_usb *touchkit)
163 {
164         if (touchkit->data)
165                 usb_buffer_free(udev, TOUCHKIT_REPORT_DATA_SIZE,
166                                 touchkit->data, touchkit->data_dma);
167 }
168
169 static int touchkit_probe(struct usb_interface *intf,
170                           const struct usb_device_id *id)
171 {
172         struct touchkit_usb *touchkit;
173         struct input_dev *input_dev;
174         struct usb_host_interface *interface;
175         struct usb_endpoint_descriptor *endpoint;
176         struct usb_device *udev = interface_to_usbdev(intf);
177
178         interface = intf->cur_altsetting;
179         endpoint = &interface->endpoint[0].desc;
180
181         touchkit = kzalloc(sizeof(struct touchkit_usb), GFP_KERNEL);
182         input_dev = input_allocate_device();
183         if (!touchkit || !input_dev)
184                 goto out_free;
185
186         if (touchkit_alloc_buffers(udev, touchkit))
187                 goto out_free;
188
189         touchkit->irq = usb_alloc_urb(0, GFP_KERNEL);
190         if (!touchkit->irq) {
191                 dbg("%s - usb_alloc_urb failed: touchkit->irq", __FUNCTION__);
192                 goto out_free_buffers;
193         }
194
195         touchkit->udev = udev;
196         touchkit->input = input_dev;
197
198         if (udev->manufacturer)
199                 strlcpy(touchkit->name, udev->manufacturer, sizeof(touchkit->name));
200
201         if (udev->product) {
202                 if (udev->manufacturer)
203                         strlcat(touchkit->name, " ", sizeof(touchkit->name));
204                 strlcat(touchkit->name, udev->product, sizeof(touchkit->name));
205         }
206
207         if (!strlen(touchkit->name))
208                 snprintf(touchkit->name, sizeof(touchkit->name),
209                         "USB Touchscreen %04x:%04x",
210                          le16_to_cpu(udev->descriptor.idVendor),
211                          le16_to_cpu(udev->descriptor.idProduct));
212
213         usb_make_path(udev, touchkit->phys, sizeof(touchkit->phys));
214         strlcpy(touchkit->phys, "/input0", sizeof(touchkit->phys));
215
216         input_dev->name = touchkit->name;
217         input_dev->phys = touchkit->phys;
218         usb_to_input_id(udev, &input_dev->id);
219         input_dev->cdev.dev = &intf->dev;
220         input_dev->private = touchkit;
221         input_dev->open = touchkit_open;
222         input_dev->close = touchkit_close;
223
224         input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
225         input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
226         input_set_abs_params(input_dev, ABS_X, TOUCHKIT_MIN_XC, TOUCHKIT_MAX_XC,
227                                 TOUCHKIT_XC_FUZZ, TOUCHKIT_XC_FLAT);
228         input_set_abs_params(input_dev, ABS_Y, TOUCHKIT_MIN_YC, TOUCHKIT_MAX_YC,
229                                 TOUCHKIT_YC_FUZZ, TOUCHKIT_YC_FLAT);
230
231         usb_fill_int_urb(touchkit->irq, touchkit->udev,
232                          usb_rcvintpipe(touchkit->udev, 0x81),
233                          touchkit->data, TOUCHKIT_REPORT_DATA_SIZE,
234                          touchkit_irq, touchkit, endpoint->bInterval);
235
236         input_register_device(touchkit->input);
237
238         usb_set_intfdata(intf, touchkit);
239         return 0;
240
241 out_free_buffers:
242         touchkit_free_buffers(udev, touchkit);
243 out_free:
244         input_free_device(input_dev);
245         kfree(touchkit);
246         return -ENOMEM;
247 }
248
249 static void touchkit_disconnect(struct usb_interface *intf)
250 {
251         struct touchkit_usb *touchkit = usb_get_intfdata(intf);
252
253         dbg("%s - called", __FUNCTION__);
254
255         if (!touchkit)
256                 return;
257
258         dbg("%s - touchkit is initialized, cleaning up", __FUNCTION__);
259         usb_set_intfdata(intf, NULL);
260         usb_kill_urb(touchkit->irq);
261         input_unregister_device(touchkit->input);
262         usb_free_urb(touchkit->irq);
263         touchkit_free_buffers(interface_to_usbdev(intf), touchkit);
264         kfree(touchkit);
265 }
266
267 MODULE_DEVICE_TABLE(usb, touchkit_devices);
268
269 static struct usb_driver touchkit_driver = {
270         .owner          = THIS_MODULE,
271         .name           = "touchkitusb",
272         .probe          = touchkit_probe,
273         .disconnect     = touchkit_disconnect,
274         .id_table       = touchkit_devices,
275 };
276
277 static int __init touchkit_init(void)
278 {
279         return usb_register(&touchkit_driver);
280 }
281
282 static void __exit touchkit_cleanup(void)
283 {
284         usb_deregister(&touchkit_driver);
285 }
286
287 module_init(touchkit_init);
288 module_exit(touchkit_cleanup);
289
290 MODULE_AUTHOR(DRIVER_AUTHOR);
291 MODULE_DESCRIPTION(DRIVER_DESC);
292 MODULE_LICENSE("GPL");