mtouchusb.c 10.1 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/******************************************************************************
 * mtouchusb.c  --  Driver for Microtouch (Now 3M) USB Touchscreens
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Based upon original work by Radoslaw Garbacz (usb-support@ite.pl)
 *  (http://freshmeat.net/projects/3mtouchscreendriver)
 *
 * History
 *
 *  0.3 & 0.4  2002 (TEJ) tejohnson@yahoo.com
 *    Updated to 2.4.18, then 2.4.19
 *    Old version still relied on stealing a minor
 *
 *  0.5  02/26/2004 (TEJ) tejohnson@yahoo.com
 *    Complete rewrite using Linux Input in 2.6.3
 *    Unfortunately no calibration support at this time
 *
 *  1.4 04/25/2004 (TEJ) tejohnson@yahoo.com
 *    Changed reset from standard USB dev reset to vendor reset
 *    Changed data sent to host from compensated to raw coordinates
 *    Eliminated vendor/product module params
35
 *    Performed multiple successful tests with an EXII-5010UC
Linus Torvalds's avatar
Linus Torvalds committed
36 37 38 39 40 41 42 43 44 45
 *
 *  1.5 02/27/2005 ddstreet@ieee.org
 *    Added module parameter to select raw or hw-calibrated coordinate reporting
 *
 *****************************************************************************/

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/init.h>
46
#include <linux/usb/input.h>
Linus Torvalds's avatar
Linus Torvalds committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

#define MTOUCHUSB_MIN_XC                0x0
#define MTOUCHUSB_MAX_RAW_XC            0x4000
#define MTOUCHUSB_MAX_CALIB_XC          0xffff
#define MTOUCHUSB_XC_FUZZ               0x0
#define MTOUCHUSB_XC_FLAT               0x0
#define MTOUCHUSB_MIN_YC                0x0
#define MTOUCHUSB_MAX_RAW_YC            0x4000
#define MTOUCHUSB_MAX_CALIB_YC          0xffff
#define MTOUCHUSB_YC_FUZZ               0x0
#define MTOUCHUSB_YC_FLAT               0x0

#define MTOUCHUSB_ASYNC_REPORT          1
#define MTOUCHUSB_RESET                 7
#define MTOUCHUSB_REPORT_DATA_SIZE      11
#define MTOUCHUSB_REQ_CTRLLR_ID         10

#define MTOUCHUSB_GET_RAW_XC(data)      (data[8]<<8 | data[7])
#define MTOUCHUSB_GET_CALIB_XC(data)    (data[4]<<8 | data[3])
#define MTOUCHUSB_GET_RAW_YC(data)      (data[10]<<8 | data[9])
#define MTOUCHUSB_GET_CALIB_YC(data)    (data[6]<<8 | data[5])
#define MTOUCHUSB_GET_XC(data)          (raw_coordinates ? \
                                         MTOUCHUSB_GET_RAW_XC(data) : \
                                         MTOUCHUSB_GET_CALIB_XC(data))
#define MTOUCHUSB_GET_YC(data)          (raw_coordinates ? \
                                         MTOUCHUSB_GET_RAW_YC(data) : \
                                         MTOUCHUSB_GET_CALIB_YC(data))
#define MTOUCHUSB_GET_TOUCHED(data)     ((data[2] & 0x40) ? 1:0)

#define DRIVER_VERSION "v1.5"
#define DRIVER_AUTHOR "Todd E. Johnson, tejohnson@yahoo.com"
#define DRIVER_DESC "3M USB Touchscreen Driver"
#define DRIVER_LICENSE "GPL"

static int raw_coordinates = 1;

module_param(raw_coordinates, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(raw_coordinates, "report raw coordinate values (y, default) or hardware-calibrated coordinate values (n)");

struct mtouch_usb {
87 88 89 90
	unsigned char *data;
	dma_addr_t data_dma;
	struct urb *irq;
	struct usb_device *udev;
91
	struct input_dev *input;
92 93
	char name[128];
	char phys[64];
Linus Torvalds's avatar
Linus Torvalds committed
94 95
};

96 97 98
static struct usb_device_id mtouchusb_devices[] = {
	{ USB_DEVICE(0x0596, 0x0001) },
	{ }
Linus Torvalds's avatar
Linus Torvalds committed
99 100 101 102
};

static void mtouchusb_irq(struct urb *urb, struct pt_regs *regs)
{
103 104 105 106 107 108 109
	struct mtouch_usb *mtouch = urb->context;
	int retval;

	switch (urb->status) {
	case 0:
		/* success */
		break;
110
	case -ETIME:
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
		/* this urb is timing out */
		dbg("%s - urb timed out - was the device unplugged?",
		    __FUNCTION__);
		return;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
		dbg("%s - urb shutting down with status: %d",
		    __FUNCTION__, urb->status);
		return;
	default:
		dbg("%s - nonzero urb status received: %d",
		    __FUNCTION__, urb->status);
		goto exit;
	}

128 129
	input_regs(mtouch->input, regs);
	input_report_key(mtouch->input, BTN_TOUCH,
130
			 MTOUCHUSB_GET_TOUCHED(mtouch->data));
131 132
	input_report_abs(mtouch->input, ABS_X, MTOUCHUSB_GET_XC(mtouch->data));
	input_report_abs(mtouch->input, ABS_Y,
Linus Torvalds's avatar
Linus Torvalds committed
133
			 (raw_coordinates ? MTOUCHUSB_MAX_RAW_YC : MTOUCHUSB_MAX_CALIB_YC)
134
			 - MTOUCHUSB_GET_YC(mtouch->data));
135
	input_sync(mtouch->input);
Linus Torvalds's avatar
Linus Torvalds committed
136 137

exit:
138 139 140 141
	retval = usb_submit_urb(urb, GFP_ATOMIC);
	if (retval)
		err("%s - usb_submit_urb failed with result: %d",
		    __FUNCTION__, retval);
Linus Torvalds's avatar
Linus Torvalds committed
142 143
}

144
static int mtouchusb_open(struct input_dev *input)
Linus Torvalds's avatar
Linus Torvalds committed
145
{
146
	struct mtouch_usb *mtouch = input->private;
Linus Torvalds's avatar
Linus Torvalds committed
147

148
	mtouch->irq->dev = mtouch->udev;
Linus Torvalds's avatar
Linus Torvalds committed
149

150
	if (usb_submit_urb(mtouch->irq, GFP_ATOMIC))
151
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
152

153
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
154 155
}

156
static void mtouchusb_close(struct input_dev *input)
Linus Torvalds's avatar
Linus Torvalds committed
157
{
158
	struct mtouch_usb *mtouch = input->private;
Linus Torvalds's avatar
Linus Torvalds committed
159

160
	usb_kill_urb(mtouch->irq);
Linus Torvalds's avatar
Linus Torvalds committed
161 162 163 164
}

static int mtouchusb_alloc_buffers(struct usb_device *udev, struct mtouch_usb *mtouch)
{
165
	dbg("%s - called", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
166

167 168
	mtouch->data = usb_buffer_alloc(udev, MTOUCHUSB_REPORT_DATA_SIZE,
					SLAB_ATOMIC, &mtouch->data_dma);
Linus Torvalds's avatar
Linus Torvalds committed
169

170 171
	if (!mtouch->data)
		return -1;
Linus Torvalds's avatar
Linus Torvalds committed
172

173
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
174 175 176 177
}

static void mtouchusb_free_buffers(struct usb_device *udev, struct mtouch_usb *mtouch)
{
178
	dbg("%s - called", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
179

180 181 182
	if (mtouch->data)
		usb_buffer_free(udev, MTOUCHUSB_REPORT_DATA_SIZE,
				mtouch->data, mtouch->data_dma);
Linus Torvalds's avatar
Linus Torvalds committed
183 184 185 186
}

static int mtouchusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
187
	struct mtouch_usb *mtouch;
188
	struct input_dev *input_dev;
189 190 191 192 193 194 195 196 197 198 199 200 201
	struct usb_host_interface *interface;
	struct usb_endpoint_descriptor *endpoint;
	struct usb_device *udev = interface_to_usbdev(intf);
	int nRet;

	dbg("%s - called", __FUNCTION__);

	dbg("%s - setting interface", __FUNCTION__);
	interface = intf->cur_altsetting;

	dbg("%s - setting endpoint", __FUNCTION__);
	endpoint = &interface->endpoint[0].desc;

202 203 204
	mtouch = kzalloc(sizeof(struct mtouch_usb), GFP_KERNEL);
	input_dev = input_allocate_device();
	if (!mtouch || !input_dev) {
205
		err("%s - Out of memory.", __FUNCTION__);
206
		goto fail1;
207 208 209
	}

	dbg("%s - allocating buffers", __FUNCTION__);
210 211
	if (mtouchusb_alloc_buffers(udev, mtouch))
		goto fail2;
212

213 214
	mtouch->udev = udev;
	mtouch->input = input_dev;
Linus Torvalds's avatar
Linus Torvalds committed
215 216

	if (udev->manufacturer)
217 218 219 220 221 222 223
		strlcpy(mtouch->name, udev->manufacturer, sizeof(mtouch->name));

	if (udev->product) {
		if (udev->manufacturer)
			strlcat(mtouch->name, " ", sizeof(mtouch->name));
		strlcat(mtouch->name, udev->product, sizeof(mtouch->name));
	}
Linus Torvalds's avatar
Linus Torvalds committed
224

225
	if (!strlen(mtouch->name))
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
		snprintf(mtouch->name, sizeof(mtouch->name),
			"USB Touchscreen %04x:%04x",
			le16_to_cpu(udev->descriptor.idVendor),
			le16_to_cpu(udev->descriptor.idProduct));

	usb_make_path(udev, mtouch->phys, sizeof(mtouch->phys));
	strlcpy(mtouch->phys, "/input0", sizeof(mtouch->phys));

	input_dev->name = mtouch->name;
	input_dev->phys = mtouch->phys;
	usb_to_input_id(udev, &input_dev->id);
	input_dev->cdev.dev = &intf->dev;
	input_dev->private = mtouch;

	input_dev->open = mtouchusb_open;
	input_dev->close = mtouchusb_close;

	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
	input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
	input_set_abs_params(input_dev, ABS_X, MTOUCHUSB_MIN_XC,
		raw_coordinates ? MTOUCHUSB_MAX_RAW_XC : MTOUCHUSB_MAX_CALIB_XC,
				MTOUCHUSB_XC_FUZZ, MTOUCHUSB_XC_FLAT);
	input_set_abs_params(input_dev, ABS_Y, MTOUCHUSB_MIN_YC,
		raw_coordinates ? MTOUCHUSB_MAX_RAW_YC : MTOUCHUSB_MAX_CALIB_YC,
		MTOUCHUSB_YC_FUZZ, MTOUCHUSB_YC_FLAT);
251 252 253 254 255 256 257 258 259 260 261 262

	nRet = usb_control_msg(mtouch->udev, usb_rcvctrlpipe(udev, 0),
			       MTOUCHUSB_RESET,
			       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			       1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
	dbg("%s - usb_control_msg - MTOUCHUSB_RESET - bytes|err: %d",
	    __FUNCTION__, nRet);

	dbg("%s - usb_alloc_urb: mtouch->irq", __FUNCTION__);
	mtouch->irq = usb_alloc_urb(0, GFP_KERNEL);
	if (!mtouch->irq) {
		dbg("%s - usb_alloc_urb failed: mtouch->irq", __FUNCTION__);
263
		goto fail2;
264 265 266 267 268 269 270 271 272
	}

	dbg("%s - usb_fill_int_urb", __FUNCTION__);
	usb_fill_int_urb(mtouch->irq, mtouch->udev,
			 usb_rcvintpipe(mtouch->udev, 0x81),
			 mtouch->data, MTOUCHUSB_REPORT_DATA_SIZE,
			 mtouchusb_irq, mtouch, endpoint->bInterval);

	dbg("%s - input_register_device", __FUNCTION__);
273
	input_register_device(mtouch->input);
274 275 276 277 278 279 280 281 282 283

	nRet = usb_control_msg(mtouch->udev, usb_rcvctrlpipe(udev, 0),
			       MTOUCHUSB_ASYNC_REPORT,
			       USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			       1, 1, NULL, 0, USB_CTRL_SET_TIMEOUT);
	dbg("%s - usb_control_msg - MTOUCHUSB_ASYNC_REPORT - bytes|err: %d",
	    __FUNCTION__, nRet);

	usb_set_intfdata(intf, mtouch);
	return 0;
284 285 286 287 288

fail2:	mtouchusb_free_buffers(udev, mtouch);
fail1:	input_free_device(input_dev);
	kfree(mtouch);
	return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
289 290 291 292
}

static void mtouchusb_disconnect(struct usb_interface *intf)
{
293 294 295 296 297 298 299
	struct mtouch_usb *mtouch = usb_get_intfdata(intf);

	dbg("%s - called", __FUNCTION__);
	usb_set_intfdata(intf, NULL);
	if (mtouch) {
		dbg("%s - mtouch is initialized, cleaning up", __FUNCTION__);
		usb_kill_urb(mtouch->irq);
300
		input_unregister_device(mtouch->input);
301 302 303 304
		usb_free_urb(mtouch->irq);
		mtouchusb_free_buffers(interface_to_usbdev(intf), mtouch);
		kfree(mtouch);
	}
Linus Torvalds's avatar
Linus Torvalds committed
305 306
}

307
MODULE_DEVICE_TABLE(usb, mtouchusb_devices);
Linus Torvalds's avatar
Linus Torvalds committed
308 309

static struct usb_driver mtouchusb_driver = {
310 311 312 313
	.name		= "mtouchusb",
	.probe		= mtouchusb_probe,
	.disconnect	= mtouchusb_disconnect,
	.id_table	= mtouchusb_devices,
Linus Torvalds's avatar
Linus Torvalds committed
314 315
};

316 317 318 319
static int __init mtouchusb_init(void)
{
	dbg("%s - called", __FUNCTION__);
	return usb_register(&mtouchusb_driver);
Linus Torvalds's avatar
Linus Torvalds committed
320 321
}

322 323 324 325
static void __exit mtouchusb_cleanup(void)
{
	dbg("%s - called", __FUNCTION__);
	usb_deregister(&mtouchusb_driver);
Linus Torvalds's avatar
Linus Torvalds committed
326 327 328 329 330
}

module_init(mtouchusb_init);
module_exit(mtouchusb_cleanup);

331 332
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds's avatar
Linus Torvalds committed
333
MODULE_LICENSE("GPL");