]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/usb/input/mtouchusb.c
[PATCH] USB: move <linux/usb_input.h> to <linux/usb/input.h>
[net-next-2.6.git] / drivers / usb / input / mtouchusb.c
CommitLineData
1da177e4
LT
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
093cf723 35 * Performed multiple successful tests with an EXII-5010UC
1da177e4
LT
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>
1da177e4
LT
43#include <linux/kernel.h>
44#include <linux/slab.h>
1da177e4
LT
45#include <linux/module.h>
46#include <linux/init.h>
ae0dadcf 47#include <linux/usb/input.h>
1da177e4
LT
48
49#define MTOUCHUSB_MIN_XC 0x0
50#define MTOUCHUSB_MAX_RAW_XC 0x4000
51#define MTOUCHUSB_MAX_CALIB_XC 0xffff
52#define MTOUCHUSB_XC_FUZZ 0x0
53#define MTOUCHUSB_XC_FLAT 0x0
54#define MTOUCHUSB_MIN_YC 0x0
55#define MTOUCHUSB_MAX_RAW_YC 0x4000
56#define MTOUCHUSB_MAX_CALIB_YC 0xffff
57#define MTOUCHUSB_YC_FUZZ 0x0
58#define MTOUCHUSB_YC_FLAT 0x0
59
60#define MTOUCHUSB_ASYNC_REPORT 1
61#define MTOUCHUSB_RESET 7
62#define MTOUCHUSB_REPORT_DATA_SIZE 11
63#define MTOUCHUSB_REQ_CTRLLR_ID 10
64
65#define MTOUCHUSB_GET_RAW_XC(data) (data[8]<<8 | data[7])
66#define MTOUCHUSB_GET_CALIB_XC(data) (data[4]<<8 | data[3])
67#define MTOUCHUSB_GET_RAW_YC(data) (data[10]<<8 | data[9])
68#define MTOUCHUSB_GET_CALIB_YC(data) (data[6]<<8 | data[5])
69#define MTOUCHUSB_GET_XC(data) (raw_coordinates ? \
70 MTOUCHUSB_GET_RAW_XC(data) : \
71 MTOUCHUSB_GET_CALIB_XC(data))
72#define MTOUCHUSB_GET_YC(data) (raw_coordinates ? \
73 MTOUCHUSB_GET_RAW_YC(data) : \
74 MTOUCHUSB_GET_CALIB_YC(data))
75#define MTOUCHUSB_GET_TOUCHED(data) ((data[2] & 0x40) ? 1:0)
76
77#define DRIVER_VERSION "v1.5"
78#define DRIVER_AUTHOR "Todd E. Johnson, tejohnson@yahoo.com"
79#define DRIVER_DESC "3M USB Touchscreen Driver"
80#define DRIVER_LICENSE "GPL"
81
82static int raw_coordinates = 1;
83
84module_param(raw_coordinates, bool, S_IRUGO | S_IWUSR);
85MODULE_PARM_DESC(raw_coordinates, "report raw coordinate values (y, default) or hardware-calibrated coordinate values (n)");
86
87struct mtouch_usb {
8baf9ed4
DT
88 unsigned char *data;
89 dma_addr_t data_dma;
90 struct urb *irq;
91 struct usb_device *udev;
c5b7c7c3 92 struct input_dev *input;
8baf9ed4
DT
93 char name[128];
94 char phys[64];
1da177e4
LT
95};
96
8baf9ed4
DT
97static struct usb_device_id mtouchusb_devices[] = {
98 { USB_DEVICE(0x0596, 0x0001) },
99 { }
1da177e4
LT
100};
101
102static void mtouchusb_irq(struct urb *urb, struct pt_regs *regs)
103{
8baf9ed4
DT
104 struct mtouch_usb *mtouch = urb->context;
105 int retval;
106
107 switch (urb->status) {
108 case 0:
109 /* success */
110 break;
111 case -ETIMEDOUT:
112 /* this urb is timing out */
113 dbg("%s - urb timed out - was the device unplugged?",
114 __FUNCTION__);
115 return;
116 case -ECONNRESET:
117 case -ENOENT:
118 case -ESHUTDOWN:
119 /* this urb is terminated, clean up */
120 dbg("%s - urb shutting down with status: %d",
121 __FUNCTION__, urb->status);
122 return;
123 default:
124 dbg("%s - nonzero urb status received: %d",
125 __FUNCTION__, urb->status);
126 goto exit;
127 }
128
c5b7c7c3
DT
129 input_regs(mtouch->input, regs);
130 input_report_key(mtouch->input, BTN_TOUCH,
8baf9ed4 131 MTOUCHUSB_GET_TOUCHED(mtouch->data));
c5b7c7c3
DT
132 input_report_abs(mtouch->input, ABS_X, MTOUCHUSB_GET_XC(mtouch->data));
133 input_report_abs(mtouch->input, ABS_Y,
1da177e4 134 (raw_coordinates ? MTOUCHUSB_MAX_RAW_YC : MTOUCHUSB_MAX_CALIB_YC)
8baf9ed4 135 - MTOUCHUSB_GET_YC(mtouch->data));
c5b7c7c3 136 input_sync(mtouch->input);
1da177e4
LT
137
138exit:
8baf9ed4
DT
139 retval = usb_submit_urb(urb, GFP_ATOMIC);
140 if (retval)
141 err("%s - usb_submit_urb failed with result: %d",
142 __FUNCTION__, retval);
1da177e4
LT
143}
144
8baf9ed4 145static int mtouchusb_open(struct input_dev *input)
1da177e4 146{
8baf9ed4 147 struct mtouch_usb *mtouch = input->private;
1da177e4 148
8baf9ed4 149 mtouch->irq->dev = mtouch->udev;
1da177e4 150
65cde54b 151 if (usb_submit_urb(mtouch->irq, GFP_ATOMIC))
8baf9ed4 152 return -EIO;
1da177e4 153
8baf9ed4 154 return 0;
1da177e4
LT
155}
156
8baf9ed4 157static void mtouchusb_close(struct input_dev *input)
1da177e4 158{
8baf9ed4 159 struct mtouch_usb *mtouch = input->private;
1da177e4 160
65cde54b 161 usb_kill_urb(mtouch->irq);
1da177e4
LT
162}
163
164static int mtouchusb_alloc_buffers(struct usb_device *udev, struct mtouch_usb *mtouch)
165{
8baf9ed4 166 dbg("%s - called", __FUNCTION__);
1da177e4 167
8baf9ed4
DT
168 mtouch->data = usb_buffer_alloc(udev, MTOUCHUSB_REPORT_DATA_SIZE,
169 SLAB_ATOMIC, &mtouch->data_dma);
1da177e4 170
8baf9ed4
DT
171 if (!mtouch->data)
172 return -1;
1da177e4 173
8baf9ed4 174 return 0;
1da177e4
LT
175}
176
177static void mtouchusb_free_buffers(struct usb_device *udev, struct mtouch_usb *mtouch)
178{
8baf9ed4 179 dbg("%s - called", __FUNCTION__);
1da177e4 180
8baf9ed4
DT
181 if (mtouch->data)
182 usb_buffer_free(udev, MTOUCHUSB_REPORT_DATA_SIZE,
183 mtouch->data, mtouch->data_dma);
1da177e4
LT
184}
185
186static int mtouchusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
187{
8baf9ed4 188 struct mtouch_usb *mtouch;
c5b7c7c3 189 struct input_dev *input_dev;
8baf9ed4
DT
190 struct usb_host_interface *interface;
191 struct usb_endpoint_descriptor *endpoint;
192 struct usb_device *udev = interface_to_usbdev(intf);
8baf9ed4
DT
193 int nRet;
194
195 dbg("%s - called", __FUNCTION__);
196
197 dbg("%s - setting interface", __FUNCTION__);
198 interface = intf->cur_altsetting;
199
200 dbg("%s - setting endpoint", __FUNCTION__);
201 endpoint = &interface->endpoint[0].desc;
202
c5b7c7c3
DT
203 mtouch = kzalloc(sizeof(struct mtouch_usb), GFP_KERNEL);
204 input_dev = input_allocate_device();
205 if (!mtouch || !input_dev) {
8baf9ed4 206 err("%s - Out of memory.", __FUNCTION__);
c5b7c7c3 207 goto fail1;
8baf9ed4
DT
208 }
209
8baf9ed4 210 dbg("%s - allocating buffers", __FUNCTION__);
c5b7c7c3
DT
211 if (mtouchusb_alloc_buffers(udev, mtouch))
212 goto fail2;
8baf9ed4 213
c5b7c7c3
DT
214 mtouch->udev = udev;
215 mtouch->input = input_dev;
1da177e4
LT
216
217 if (udev->manufacturer)
c5b7c7c3
DT
218 strlcpy(mtouch->name, udev->manufacturer, sizeof(mtouch->name));
219
220 if (udev->product) {
221 if (udev->manufacturer)
222 strlcat(mtouch->name, " ", sizeof(mtouch->name));
223 strlcat(mtouch->name, udev->product, sizeof(mtouch->name));
224 }
1da177e4 225
8baf9ed4 226 if (!strlen(mtouch->name))
c5b7c7c3
DT
227 snprintf(mtouch->name, sizeof(mtouch->name),
228 "USB Touchscreen %04x:%04x",
229 le16_to_cpu(udev->descriptor.idVendor),
230 le16_to_cpu(udev->descriptor.idProduct));
231
232 usb_make_path(udev, mtouch->phys, sizeof(mtouch->phys));
233 strlcpy(mtouch->phys, "/input0", sizeof(mtouch->phys));
234
235 input_dev->name = mtouch->name;
236 input_dev->phys = mtouch->phys;
237 usb_to_input_id(udev, &input_dev->id);
238 input_dev->cdev.dev = &intf->dev;
239 input_dev->private = mtouch;
240
241 input_dev->open = mtouchusb_open;
242 input_dev->close = mtouchusb_close;
243
244 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
245 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
246 input_set_abs_params(input_dev, ABS_X, MTOUCHUSB_MIN_XC,
247 raw_coordinates ? MTOUCHUSB_MAX_RAW_XC : MTOUCHUSB_MAX_CALIB_XC,
248 MTOUCHUSB_XC_FUZZ, MTOUCHUSB_XC_FLAT);
249 input_set_abs_params(input_dev, ABS_Y, MTOUCHUSB_MIN_YC,
250 raw_coordinates ? MTOUCHUSB_MAX_RAW_YC : MTOUCHUSB_MAX_CALIB_YC,
251 MTOUCHUSB_YC_FUZZ, MTOUCHUSB_YC_FLAT);
8baf9ed4
DT
252
253 nRet = usb_control_msg(mtouch->udev, usb_rcvctrlpipe(udev, 0),
254 MTOUCHUSB_RESET,
255 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
256 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
257 dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
258 __FUNCTION__, nRet);
259
260 dbg("%s - usb_alloc_urb: mtouch->irq", __FUNCTION__);
261 mtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
262 if (!mtouch->irq) {
263 dbg("%s - usb_alloc_urb failed: mtouch->irq", __FUNCTION__);
c5b7c7c3 264 goto fail2;
8baf9ed4
DT
265 }
266
267 dbg("%s - usb_fill_int_urb", __FUNCTION__);
268 usb_fill_int_urb(mtouch->irq, mtouch->udev,
269 usb_rcvintpipe(mtouch->udev, 0x81),
270 mtouch->data, MTOUCHUSB_REPORT_DATA_SIZE,
271 mtouchusb_irq, mtouch, endpoint->bInterval);
272
273 dbg("%s - input_register_device", __FUNCTION__);
c5b7c7c3 274 input_register_device(mtouch->input);
8baf9ed4
DT
275
276 nRet = usb_control_msg(mtouch->udev, usb_rcvctrlpipe(udev, 0),
277 MTOUCHUSB_ASYNC_REPORT,
278 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
279 1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
280 dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
281 __FUNCTION__, nRet);
282
8baf9ed4 283 usb_set_intfdata(intf, mtouch);
8baf9ed4 284 return 0;
c5b7c7c3
DT
285
286fail2: mtouchusb_free_buffers(udev, mtouch);
287fail1: input_free_device(input_dev);
288 kfree(mtouch);
289 return -ENOMEM;
1da177e4
LT
290}
291
292static void mtouchusb_disconnect(struct usb_interface *intf)
293{
8baf9ed4
DT
294 struct mtouch_usb *mtouch = usb_get_intfdata(intf);
295
296 dbg("%s - called", __FUNCTION__);
297 usb_set_intfdata(intf, NULL);
298 if (mtouch) {
299 dbg("%s - mtouch is initialized, cleaning up", __FUNCTION__);
300 usb_kill_urb(mtouch->irq);
c5b7c7c3 301 input_unregister_device(mtouch->input);
8baf9ed4
DT
302 usb_free_urb(mtouch->irq);
303 mtouchusb_free_buffers(interface_to_usbdev(intf), mtouch);
304 kfree(mtouch);
305 }
1da177e4
LT
306}
307
8baf9ed4 308MODULE_DEVICE_TABLE(usb, mtouchusb_devices);
1da177e4
LT
309
310static struct usb_driver mtouchusb_driver = {
8baf9ed4
DT
311 .name = "mtouchusb",
312 .probe = mtouchusb_probe,
313 .disconnect = mtouchusb_disconnect,
314 .id_table = mtouchusb_devices,
1da177e4
LT
315};
316
8baf9ed4
DT
317static int __init mtouchusb_init(void)
318{
319 dbg("%s - called", __FUNCTION__);
320 return usb_register(&mtouchusb_driver);
1da177e4
LT
321}
322
8baf9ed4
DT
323static void __exit mtouchusb_cleanup(void)
324{
325 dbg("%s - called", __FUNCTION__);
326 usb_deregister(&mtouchusb_driver);
1da177e4
LT
327}
328
329module_init(mtouchusb_init);
330module_exit(mtouchusb_cleanup);
331
8baf9ed4
DT
332MODULE_AUTHOR(DRIVER_AUTHOR);
333MODULE_DESCRIPTION(DRIVER_DESC);
1da177e4 334MODULE_LICENSE("GPL");