cyberjack.c 13.1 KB
Newer Older
L
Linus Torvalds 已提交
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 35 36 37 38 39
/*
 *  REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
 *
 *  Copyright (C) 2001  REINER SCT
 *  Author: Matthias Bruestle
 *
 *  Contact: support@reiner-sct.com (see MAINTAINERS)
 *
 *  This program is largely derived from work by the linux-usb group
 *  and associated source files.  Please see the usb/serial files for
 *  individual credits and copyrights.
 *
 *  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.
 *
 *  Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
 *  patience.
 *
 *  In case of problems, please write to the contact e-mail address
 *  mentioned above.
 *
 *  Please note that later models of the cyberjack reader family are
 *  supported by a libusb-based userspace device driver.
 *
 *  Homepage: http://www.reiner-sct.de/support/treiber_cyberjack.php#linux
 */


#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/spinlock.h>
A
Alan Cox 已提交
40
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
41
#include <linux/usb.h>
42
#include <linux/usb/serial.h>
L
Linus Torvalds 已提交
43 44 45

#define CYBERJACK_LOCAL_BUF_SIZE 32

46
static bool debug;
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59

/*
 * Version Information
 */
#define DRIVER_VERSION "v1.01"
#define DRIVER_AUTHOR "Matthias Bruestle"
#define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"


#define CYBERJACK_VENDOR_ID	0x0C4B
#define CYBERJACK_PRODUCT_ID	0x0100

/* Function prototypes */
A
Alan Cox 已提交
60
static int cyberjack_startup(struct usb_serial *serial);
61 62
static void cyberjack_disconnect(struct usb_serial *serial);
static void cyberjack_release(struct usb_serial *serial);
A
Alan Cox 已提交
63
static int  cyberjack_open(struct tty_struct *tty,
64
	struct usb_serial_port *port);
65
static void cyberjack_close(struct usb_serial_port *port);
A
Alan Cox 已提交
66 67
static int cyberjack_write(struct tty_struct *tty,
	struct usb_serial_port *port, const unsigned char *buf, int count);
A
Alan Cox 已提交
68
static int cyberjack_write_room(struct tty_struct *tty);
A
Alan Cox 已提交
69 70 71
static void cyberjack_read_int_callback(struct urb *urb);
static void cyberjack_read_bulk_callback(struct urb *urb);
static void cyberjack_write_bulk_callback(struct urb *urb);
L
Linus Torvalds 已提交
72

73
static const struct usb_device_id id_table[] = {
L
Linus Torvalds 已提交
74 75 76 77
	{ USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
	{ }			/* Terminating entry */
};

A
Alan Cox 已提交
78
MODULE_DEVICE_TABLE(usb, id_table);
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86

static struct usb_driver cyberjack_driver = {
	.name =		"cyberjack",
	.probe =	usb_serial_probe,
	.disconnect =	usb_serial_disconnect,
	.id_table =	id_table,
};

87
static struct usb_serial_driver cyberjack_device = {
88 89
	.driver = {
		.owner =	THIS_MODULE,
90
		.name =		"cyberjack",
91
	},
92
	.description =		"Reiner SCT Cyberjack USB card reader",
L
Linus Torvalds 已提交
93 94 95
	.id_table =		id_table,
	.num_ports =		1,
	.attach =		cyberjack_startup,
96 97
	.disconnect =		cyberjack_disconnect,
	.release =		cyberjack_release,
L
Linus Torvalds 已提交
98 99 100
	.open =			cyberjack_open,
	.close =		cyberjack_close,
	.write =		cyberjack_write,
101
	.write_room =		cyberjack_write_room,
L
Linus Torvalds 已提交
102 103 104 105 106
	.read_int_callback =	cyberjack_read_int_callback,
	.read_bulk_callback =	cyberjack_read_bulk_callback,
	.write_bulk_callback =	cyberjack_write_bulk_callback,
};

107 108 109 110
static struct usb_serial_driver * const serial_drivers[] = {
	&cyberjack_device, NULL
};

L
Linus Torvalds 已提交
111 112 113 114 115 116 117 118 119
struct cyberjack_private {
	spinlock_t	lock;		/* Lock for SMP */
	short		rdtodo;		/* Bytes still to read */
	unsigned char	wrbuf[5*64];	/* Buffer for collecting data to write */
	short		wrfilled;	/* Overall data size we already got */
	short		wrsent;		/* Data already sent */
};

/* do some startup allocations not currently performed by usb_serial_probe() */
A
Alan Cox 已提交
120
static int cyberjack_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
121 122 123 124
{
	struct cyberjack_private *priv;
	int i;

125
	dbg("%s", __func__);
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

	/* allocate the private data structure */
	priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	/* set initial values */
	spin_lock_init(&priv->lock);
	priv->rdtodo = 0;
	priv->wrfilled = 0;
	priv->wrsent = 0;
	usb_set_serial_port_data(serial->port[0], priv);

	init_waitqueue_head(&serial->port[0]->write_wait);

	for (i = 0; i < serial->num_ports; ++i) {
		int result;
A
Alan Cox 已提交
143
		result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
L
Linus Torvalds 已提交
144 145
					GFP_KERNEL);
		if (result)
146 147
			dev_err(&serial->dev->dev,
				"usb_submit_urb(read int) failed\n");
148
		dbg("%s - usb_submit_urb(int urb)", __func__);
L
Linus Torvalds 已提交
149 150
	}

A
Alan Cox 已提交
151
	return 0;
L
Linus Torvalds 已提交
152 153
}

154
static void cyberjack_disconnect(struct usb_serial *serial)
L
Linus Torvalds 已提交
155 156
{
	int i;
A
Alan Cox 已提交
157

158
	dbg("%s", __func__);
L
Linus Torvalds 已提交
159

160
	for (i = 0; i < serial->num_ports; ++i)
L
Linus Torvalds 已提交
161
		usb_kill_urb(serial->port[i]->interrupt_in_urb);
162 163 164 165 166 167 168 169 170
}

static void cyberjack_release(struct usb_serial *serial)
{
	int i;

	dbg("%s", __func__);

	for (i = 0; i < serial->num_ports; ++i) {
L
Linus Torvalds 已提交
171 172 173 174
		/* My special items, the standard routines free my urbs */
		kfree(usb_get_serial_port_data(serial->port[i]));
	}
}
A
Alan Cox 已提交
175

A
Alan Cox 已提交
176
static int  cyberjack_open(struct tty_struct *tty,
177
					struct usb_serial_port *port)
L
Linus Torvalds 已提交
178 179 180 181 182
{
	struct cyberjack_private *priv;
	unsigned long flags;
	int result = 0;

183
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
184

A
Alan Cox 已提交
185
	dbg("%s - usb_clear_halt", __func__);
L
Linus Torvalds 已提交
186 187 188 189 190 191 192 193 194 195 196 197
	usb_clear_halt(port->serial->dev, port->write_urb->pipe);

	priv = usb_get_serial_port_data(port);
	spin_lock_irqsave(&priv->lock, flags);
	priv->rdtodo = 0;
	priv->wrfilled = 0;
	priv->wrsent = 0;
	spin_unlock_irqrestore(&priv->lock, flags);

	return result;
}

198
static void cyberjack_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
199
{
200
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
201 202 203 204 205 206 207 208

	if (port->serial->dev) {
		/* shutdown any bulk reads that might be going on */
		usb_kill_urb(port->write_urb);
		usb_kill_urb(port->read_urb);
	}
}

A
Alan Cox 已提交
209 210
static int cyberjack_write(struct tty_struct *tty,
	struct usb_serial_port *port, const unsigned char *buf, int count)
L
Linus Torvalds 已提交
211 212 213 214 215 216
{
	struct cyberjack_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;
	int result;
	int wrexpected;

217
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
218 219

	if (count == 0) {
220
		dbg("%s - write request of 0 bytes", __func__);
221
		return 0;
L
Linus Torvalds 已提交
222 223
	}

224
	if (!test_and_clear_bit(0, &port->write_urbs_free)) {
225
		dbg("%s - already writing", __func__);
226
		return 0;
L
Linus Torvalds 已提交
227 228 229 230
	}

	spin_lock_irqsave(&priv->lock, flags);

A
Alan Cox 已提交
231
	if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
L
Linus Torvalds 已提交
232
		/* To much data for buffer. Reset buffer. */
233 234
		priv->wrfilled = 0;
		spin_unlock_irqrestore(&priv->lock, flags);
235
		set_bit(0, &port->write_urbs_free);
236
		return 0;
L
Linus Torvalds 已提交
237 238 239
	}

	/* Copy data */
A
Alan Cox 已提交
240
	memcpy(priv->wrbuf + priv->wrfilled, buf, count);
L
Linus Torvalds 已提交
241

242
	usb_serial_debug_data(debug, &port->dev, __func__, count,
A
Alan Cox 已提交
243
		priv->wrbuf + priv->wrfilled);
L
Linus Torvalds 已提交
244 245
	priv->wrfilled += count;

A
Alan Cox 已提交
246
	if (priv->wrfilled >= 3) {
L
Linus Torvalds 已提交
247
		wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
248
		dbg("%s - expected data: %d", __func__, wrexpected);
A
Alan Cox 已提交
249
	} else
L
Linus Torvalds 已提交
250 251
		wrexpected = sizeof(priv->wrbuf);

A
Alan Cox 已提交
252
	if (priv->wrfilled >= wrexpected) {
L
Linus Torvalds 已提交
253 254 255
		/* We have enough data to begin transmission */
		int length;

256
		dbg("%s - transmitting data (frame 1)", __func__);
A
Alan Cox 已提交
257 258
		length = (wrexpected > port->bulk_out_size) ?
					port->bulk_out_size : wrexpected;
L
Linus Torvalds 已提交
259

A
Alan Cox 已提交
260 261
		memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
		priv->wrsent = length;
L
Linus Torvalds 已提交
262 263

		/* set up our urb */
264
		port->write_urb->transfer_buffer_length = length;
L
Linus Torvalds 已提交
265 266 267 268

		/* send the data out the bulk port */
		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (result) {
269 270 271
			dev_err(&port->dev,
				"%s - failed submitting write urb, error %d",
				__func__, result);
L
Linus Torvalds 已提交
272
			/* Throw away data. No better idea what to do with it. */
273 274
			priv->wrfilled = 0;
			priv->wrsent = 0;
L
Linus Torvalds 已提交
275
			spin_unlock_irqrestore(&priv->lock, flags);
276
			set_bit(0, &port->write_urbs_free);
L
Linus Torvalds 已提交
277 278 279
			return 0;
		}

A
Alan Cox 已提交
280 281
		dbg("%s - priv->wrsent=%d", __func__, priv->wrsent);
		dbg("%s - priv->wrfilled=%d", __func__, priv->wrfilled);
L
Linus Torvalds 已提交
282

A
Alan Cox 已提交
283
		if (priv->wrsent >= priv->wrfilled) {
284
			dbg("%s - buffer cleaned", __func__);
A
Alan Cox 已提交
285
			memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
286 287
			priv->wrfilled = 0;
			priv->wrsent = 0;
L
Linus Torvalds 已提交
288 289 290 291 292
		}
	}

	spin_unlock_irqrestore(&priv->lock, flags);

A
Alan Cox 已提交
293 294
	return count;
}
L
Linus Torvalds 已提交
295

A
Alan Cox 已提交
296
static int cyberjack_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
297
{
298
	/* FIXME: .... */
L
Linus Torvalds 已提交
299 300 301
	return CYBERJACK_LOCAL_BUF_SIZE;
}

A
Alan Cox 已提交
302
static void cyberjack_read_int_callback(struct urb *urb)
L
Linus Torvalds 已提交
303
{
304
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
305 306
	struct cyberjack_private *priv = usb_get_serial_port_data(port);
	unsigned char *data = urb->transfer_buffer;
307
	int status = urb->status;
L
Linus Torvalds 已提交
308 309
	int result;

310
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
311 312

	/* the urb might have been killed. */
313
	if (status)
L
Linus Torvalds 已提交
314 315
		return;

A
Alan Cox 已提交
316 317
	usb_serial_debug_data(debug, &port->dev, __func__,
						urb->actual_length, data);
L
Linus Torvalds 已提交
318 319

	/* React only to interrupts signaling a bulk_in transfer */
A
Alan Cox 已提交
320
	if (urb->actual_length == 4 && data[0] == 0x01) {
L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329
		short old_rdtodo;

		/* This is a announcement of coming bulk_ins. */
		unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;

		spin_lock(&priv->lock);

		old_rdtodo = priv->rdtodo;

A
Alan Cox 已提交
330 331
		if (old_rdtodo + size < old_rdtodo) {
			dbg("To many bulk_in urbs to do.");
L
Linus Torvalds 已提交
332 333 334 335 336 337 338
			spin_unlock(&priv->lock);
			goto resubmit;
		}

		/* "+=" is probably more fault tollerant than "=" */
		priv->rdtodo += size;

339
		dbg("%s - rdtodo: %d", __func__, priv->rdtodo);
L
Linus Torvalds 已提交
340 341 342

		spin_unlock(&priv->lock);

A
Alan Cox 已提交
343
		if (!old_rdtodo) {
L
Linus Torvalds 已提交
344
			result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
A
Alan Cox 已提交
345
			if (result)
346 347 348
				dev_err(&port->dev, "%s - failed resubmitting "
					"read urb, error %d\n",
					__func__, result);
349
			dbg("%s - usb_submit_urb(read urb)", __func__);
L
Linus Torvalds 已提交
350 351 352 353 354 355
		}
	}

resubmit:
	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
	if (result)
356
		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
357
	dbg("%s - usb_submit_urb(int urb)", __func__);
L
Linus Torvalds 已提交
358 359
}

A
Alan Cox 已提交
360
static void cyberjack_read_bulk_callback(struct urb *urb)
L
Linus Torvalds 已提交
361
{
362
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
363 364 365 366 367
	struct cyberjack_private *priv = usb_get_serial_port_data(port);
	struct tty_struct *tty;
	unsigned char *data = urb->transfer_buffer;
	short todo;
	int result;
368
	int status = urb->status;
L
Linus Torvalds 已提交
369

370
	dbg("%s - port %d", __func__, port->number);
371

A
Alan Cox 已提交
372 373
	usb_serial_debug_data(debug, &port->dev, __func__,
						urb->actual_length, data);
374 375
	if (status) {
		dbg("%s - nonzero read bulk status received: %d",
376
		    __func__, status);
L
Linus Torvalds 已提交
377 378 379
		return;
	}

A
Alan Cox 已提交
380
	tty = tty_port_tty_get(&port->port);
L
Linus Torvalds 已提交
381
	if (!tty) {
382
		dbg("%s - ignoring since device not open", __func__);
L
Linus Torvalds 已提交
383 384 385
		return;
	}
	if (urb->actual_length) {
A
Alan Cox 已提交
386
		tty_insert_flip_string(tty, data, urb->actual_length);
A
Alan Cox 已提交
387
		tty_flip_buffer_push(tty);
L
Linus Torvalds 已提交
388
	}
A
Alan Cox 已提交
389
	tty_kref_put(tty);
L
Linus Torvalds 已提交
390 391 392 393

	spin_lock(&priv->lock);

	/* Reduce urbs to do by one. */
A
Alan Cox 已提交
394
	priv->rdtodo -= urb->actual_length;
L
Linus Torvalds 已提交
395
	/* Just to be sure */
A
Alan Cox 已提交
396 397
	if (priv->rdtodo < 0)
		priv->rdtodo = 0;
L
Linus Torvalds 已提交
398 399 400 401
	todo = priv->rdtodo;

	spin_unlock(&priv->lock);

402
	dbg("%s - rdtodo: %d", __func__, todo);
L
Linus Torvalds 已提交
403 404

	/* Continue to read if we have still urbs to do. */
A
Alan Cox 已提交
405
	if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
L
Linus Torvalds 已提交
406 407
		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
		if (result)
408 409
			dev_err(&port->dev, "%s - failed resubmitting read "
				"urb, error %d\n", __func__, result);
410
		dbg("%s - usb_submit_urb(read urb)", __func__);
L
Linus Torvalds 已提交
411 412 413
	}
}

A
Alan Cox 已提交
414
static void cyberjack_write_bulk_callback(struct urb *urb)
L
Linus Torvalds 已提交
415
{
416
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
417
	struct cyberjack_private *priv = usb_get_serial_port_data(port);
418
	int status = urb->status;
L
Linus Torvalds 已提交
419

420
	dbg("%s - port %d", __func__, port->number);
421

422
	set_bit(0, &port->write_urbs_free);
423 424
	if (status) {
		dbg("%s - nonzero write bulk status received: %d",
425
		    __func__, status);
L
Linus Torvalds 已提交
426 427 428 429 430 431
		return;
	}

	spin_lock(&priv->lock);

	/* only do something if we have more data to send */
A
Alan Cox 已提交
432
	if (priv->wrfilled) {
L
Linus Torvalds 已提交
433 434
		int length, blksize, result;

435
		dbg("%s - transmitting data (frame n)", __func__);
L
Linus Torvalds 已提交
436 437 438 439

		length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
			port->bulk_out_size : (priv->wrfilled - priv->wrsent);

A
Alan Cox 已提交
440 441 442
		memcpy(port->write_urb->transfer_buffer,
					priv->wrbuf + priv->wrsent, length);
		priv->wrsent += length;
L
Linus Torvalds 已提交
443 444

		/* set up our urb */
445
		port->write_urb->transfer_buffer_length = length;
L
Linus Torvalds 已提交
446 447 448 449

		/* send the data out the bulk port */
		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (result) {
450 451 452
			dev_err(&port->dev,
				"%s - failed submitting write urb, error %d\n",
				__func__, result);
L
Linus Torvalds 已提交
453
			/* Throw away data. No better idea what to do with it. */
454 455
			priv->wrfilled = 0;
			priv->wrsent = 0;
L
Linus Torvalds 已提交
456 457 458
			goto exit;
		}

A
Alan Cox 已提交
459 460
		dbg("%s - priv->wrsent=%d", __func__, priv->wrsent);
		dbg("%s - priv->wrfilled=%d", __func__, priv->wrfilled);
L
Linus Torvalds 已提交
461 462 463

		blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;

A
Alan Cox 已提交
464 465
		if (priv->wrsent >= priv->wrfilled ||
					priv->wrsent >= blksize) {
466
			dbg("%s - buffer cleaned", __func__);
A
Alan Cox 已提交
467
			memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
468 469
			priv->wrfilled = 0;
			priv->wrsent = 0;
L
Linus Torvalds 已提交
470 471 472 473 474
		}
	}

exit:
	spin_unlock(&priv->lock);
475
	usb_serial_port_softint(port);
L
Linus Torvalds 已提交
476 477
}

A
Alan Cox 已提交
478
static int __init cyberjack_init(void)
L
Linus Torvalds 已提交
479 480 481
{
	int retval;

482 483 484 485 486 487
	retval = usb_serial_register_drivers(&cyberjack_driver, serial_drivers);
	if (retval == 0) {
		printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION " "
			       DRIVER_AUTHOR "\n");
		printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
	}
L
Linus Torvalds 已提交
488 489 490
	return retval;
}

A
Alan Cox 已提交
491
static void __exit cyberjack_exit(void)
L
Linus Torvalds 已提交
492
{
493
	usb_serial_deregister_drivers(&cyberjack_driver, serial_drivers);
L
Linus Torvalds 已提交
494 495 496 497 498
}

module_init(cyberjack_init);
module_exit(cyberjack_exit);

A
Alan Cox 已提交
499 500 501
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_VERSION(DRIVER_VERSION);
L
Linus Torvalds 已提交
502 503 504 505
MODULE_LICENSE("GPL");

module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Debug enabled or not");