cyberjack.c 11.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
L
Linus Torvalds 已提交
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
/*
 *  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.
 *
 *  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/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 已提交
35
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
36
#include <linux/usb.h>
37
#include <linux/usb/serial.h>
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46 47 48

#define CYBERJACK_LOCAL_BUF_SIZE 32

#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 */
49
static int cyberjack_port_probe(struct usb_serial_port *port);
50
static void cyberjack_port_remove(struct usb_serial_port *port);
A
Alan Cox 已提交
51
static int  cyberjack_open(struct tty_struct *tty,
52
	struct usb_serial_port *port);
53
static void cyberjack_close(struct usb_serial_port *port);
A
Alan Cox 已提交
54 55
static int cyberjack_write(struct tty_struct *tty,
	struct usb_serial_port *port, const unsigned char *buf, int count);
A
Alan Cox 已提交
56
static int cyberjack_write_room(struct tty_struct *tty);
A
Alan Cox 已提交
57 58 59
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 已提交
60

61
static const struct usb_device_id id_table[] = {
L
Linus Torvalds 已提交
62 63 64 65
	{ USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
	{ }			/* Terminating entry */
};

A
Alan Cox 已提交
66
MODULE_DEVICE_TABLE(usb, id_table);
L
Linus Torvalds 已提交
67

68
static struct usb_serial_driver cyberjack_device = {
69 70
	.driver = {
		.owner =	THIS_MODULE,
71
		.name =		"cyberjack",
72
	},
73
	.description =		"Reiner SCT Cyberjack USB card reader",
L
Linus Torvalds 已提交
74 75
	.id_table =		id_table,
	.num_ports =		1,
76
	.num_bulk_out =		1,
77 78
	.port_probe =		cyberjack_port_probe,
	.port_remove =		cyberjack_port_remove,
L
Linus Torvalds 已提交
79 80 81
	.open =			cyberjack_open,
	.close =		cyberjack_close,
	.write =		cyberjack_write,
82
	.write_room =		cyberjack_write_room,
L
Linus Torvalds 已提交
83 84 85 86 87
	.read_int_callback =	cyberjack_read_int_callback,
	.read_bulk_callback =	cyberjack_read_bulk_callback,
	.write_bulk_callback =	cyberjack_write_bulk_callback,
};

88 89 90 91
static struct usb_serial_driver * const serial_drivers[] = {
	&cyberjack_device, NULL
};

L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99
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 */
};

100
static int cyberjack_port_probe(struct usb_serial_port *port)
L
Linus Torvalds 已提交
101 102
{
	struct cyberjack_private *priv;
103
	int result;
L
Linus Torvalds 已提交
104 105 106 107 108 109 110 111 112

	priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	spin_lock_init(&priv->lock);
	priv->rdtodo = 0;
	priv->wrfilled = 0;
	priv->wrsent = 0;
113 114 115 116 117 118

	usb_set_serial_port_data(port, priv);

	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
	if (result)
		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
L
Linus Torvalds 已提交
119

A
Alan Cox 已提交
120
	return 0;
L
Linus Torvalds 已提交
121 122
}

123
static void cyberjack_port_remove(struct usb_serial_port *port)
L
Linus Torvalds 已提交
124
{
125
	struct cyberjack_private *priv;
A
Alan Cox 已提交
126

127 128
	usb_kill_urb(port->interrupt_in_urb);

129 130
	priv = usb_get_serial_port_data(port);
	kfree(priv);
131 132
}

A
Alan Cox 已提交
133
static int  cyberjack_open(struct tty_struct *tty,
134
					struct usb_serial_port *port)
L
Linus Torvalds 已提交
135 136 137 138
{
	struct cyberjack_private *priv;
	unsigned long flags;

139
	dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
L
Linus Torvalds 已提交
140 141 142 143 144 145 146 147 148
	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);

149
	return 0;
L
Linus Torvalds 已提交
150 151
}

152
static void cyberjack_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
153
{
154 155
	usb_kill_urb(port->write_urb);
	usb_kill_urb(port->read_urb);
L
Linus Torvalds 已提交
156 157
}

A
Alan Cox 已提交
158 159
static int cyberjack_write(struct tty_struct *tty,
	struct usb_serial_port *port, const unsigned char *buf, int count)
L
Linus Torvalds 已提交
160
{
161
	struct device *dev = &port->dev;
L
Linus Torvalds 已提交
162 163 164 165 166 167
	struct cyberjack_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;
	int result;
	int wrexpected;

	if (count == 0) {
168
		dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
169
		return 0;
L
Linus Torvalds 已提交
170 171
	}

172
	if (!test_and_clear_bit(0, &port->write_urbs_free)) {
173
		dev_dbg(dev, "%s - already writing\n", __func__);
174
		return 0;
L
Linus Torvalds 已提交
175 176 177 178
	}

	spin_lock_irqsave(&priv->lock, flags);

A
Alan Cox 已提交
179
	if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
L
Linus Torvalds 已提交
180
		/* To much data for buffer. Reset buffer. */
181 182
		priv->wrfilled = 0;
		spin_unlock_irqrestore(&priv->lock, flags);
183
		set_bit(0, &port->write_urbs_free);
184
		return 0;
L
Linus Torvalds 已提交
185 186 187
	}

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

190
	usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
L
Linus Torvalds 已提交
191 192
	priv->wrfilled += count;

A
Alan Cox 已提交
193
	if (priv->wrfilled >= 3) {
L
Linus Torvalds 已提交
194
		wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
195
		dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
A
Alan Cox 已提交
196
	} else
L
Linus Torvalds 已提交
197 198
		wrexpected = sizeof(priv->wrbuf);

A
Alan Cox 已提交
199
	if (priv->wrfilled >= wrexpected) {
L
Linus Torvalds 已提交
200 201 202
		/* We have enough data to begin transmission */
		int length;

203
		dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
A
Alan Cox 已提交
204 205
		length = (wrexpected > port->bulk_out_size) ?
					port->bulk_out_size : wrexpected;
L
Linus Torvalds 已提交
206

A
Alan Cox 已提交
207 208
		memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
		priv->wrsent = length;
L
Linus Torvalds 已提交
209 210

		/* set up our urb */
211
		port->write_urb->transfer_buffer_length = length;
L
Linus Torvalds 已提交
212 213 214 215

		/* send the data out the bulk port */
		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (result) {
216
			dev_err(&port->dev,
217
				"%s - failed submitting write urb, error %d\n",
218
				__func__, result);
L
Linus Torvalds 已提交
219
			/* Throw away data. No better idea what to do with it. */
220 221
			priv->wrfilled = 0;
			priv->wrsent = 0;
L
Linus Torvalds 已提交
222
			spin_unlock_irqrestore(&priv->lock, flags);
223
			set_bit(0, &port->write_urbs_free);
L
Linus Torvalds 已提交
224 225 226
			return 0;
		}

227 228
		dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
		dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
L
Linus Torvalds 已提交
229

A
Alan Cox 已提交
230
		if (priv->wrsent >= priv->wrfilled) {
231
			dev_dbg(dev, "%s - buffer cleaned\n", __func__);
A
Alan Cox 已提交
232
			memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
233 234
			priv->wrfilled = 0;
			priv->wrsent = 0;
L
Linus Torvalds 已提交
235 236 237 238 239
		}
	}

	spin_unlock_irqrestore(&priv->lock, flags);

A
Alan Cox 已提交
240 241
	return count;
}
L
Linus Torvalds 已提交
242

A
Alan Cox 已提交
243
static int cyberjack_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
244
{
245
	/* FIXME: .... */
L
Linus Torvalds 已提交
246 247 248
	return CYBERJACK_LOCAL_BUF_SIZE;
}

A
Alan Cox 已提交
249
static void cyberjack_read_int_callback(struct urb *urb)
L
Linus Torvalds 已提交
250
{
251
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
252
	struct cyberjack_private *priv = usb_get_serial_port_data(port);
253
	struct device *dev = &port->dev;
L
Linus Torvalds 已提交
254
	unsigned char *data = urb->transfer_buffer;
255
	int status = urb->status;
256
	unsigned long flags;
L
Linus Torvalds 已提交
257 258 259
	int result;

	/* the urb might have been killed. */
260
	if (status)
L
Linus Torvalds 已提交
261 262
		return;

263
	usb_serial_debug_data(dev, __func__, urb->actual_length, data);
L
Linus Torvalds 已提交
264 265

	/* React only to interrupts signaling a bulk_in transfer */
A
Alan Cox 已提交
266
	if (urb->actual_length == 4 && data[0] == 0x01) {
L
Linus Torvalds 已提交
267 268 269 270 271
		short old_rdtodo;

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

272
		spin_lock_irqsave(&priv->lock, flags);
L
Linus Torvalds 已提交
273 274 275

		old_rdtodo = priv->rdtodo;

276
		if (old_rdtodo > SHRT_MAX - size) {
277
			dev_dbg(dev, "Too many bulk_in urbs to do.\n");
278
			spin_unlock_irqrestore(&priv->lock, flags);
L
Linus Torvalds 已提交
279 280 281
			goto resubmit;
		}

282
		/* "+=" is probably more fault tolerant than "=" */
L
Linus Torvalds 已提交
283 284
		priv->rdtodo += size;

285
		dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
L
Linus Torvalds 已提交
286

287
		spin_unlock_irqrestore(&priv->lock, flags);
L
Linus Torvalds 已提交
288

A
Alan Cox 已提交
289
		if (!old_rdtodo) {
L
Linus Torvalds 已提交
290
			result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
A
Alan Cox 已提交
291
			if (result)
292
				dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
293
					__func__, result);
294
			dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
L
Linus Torvalds 已提交
295 296 297 298 299 300
		}
	}

resubmit:
	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
	if (result)
301
		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
302
	dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
L
Linus Torvalds 已提交
303 304
}

A
Alan Cox 已提交
305
static void cyberjack_read_bulk_callback(struct urb *urb)
L
Linus Torvalds 已提交
306
{
307
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
308
	struct cyberjack_private *priv = usb_get_serial_port_data(port);
309
	struct device *dev = &port->dev;
L
Linus Torvalds 已提交
310
	unsigned char *data = urb->transfer_buffer;
311
	unsigned long flags;
L
Linus Torvalds 已提交
312 313
	short todo;
	int result;
314
	int status = urb->status;
L
Linus Torvalds 已提交
315

316
	usb_serial_debug_data(dev, __func__, urb->actual_length, data);
317
	if (status) {
318 319
		dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
			__func__, status);
L
Linus Torvalds 已提交
320 321 322 323
		return;
	}

	if (urb->actual_length) {
J
Jiri Slaby 已提交
324
		tty_insert_flip_string(&port->port, data, urb->actual_length);
J
Jiri Slaby 已提交
325
		tty_flip_buffer_push(&port->port);
L
Linus Torvalds 已提交
326 327
	}

328
	spin_lock_irqsave(&priv->lock, flags);
L
Linus Torvalds 已提交
329 330

	/* Reduce urbs to do by one. */
A
Alan Cox 已提交
331
	priv->rdtodo -= urb->actual_length;
L
Linus Torvalds 已提交
332
	/* Just to be sure */
A
Alan Cox 已提交
333 334
	if (priv->rdtodo < 0)
		priv->rdtodo = 0;
L
Linus Torvalds 已提交
335 336
	todo = priv->rdtodo;

337
	spin_unlock_irqrestore(&priv->lock, flags);
L
Linus Torvalds 已提交
338

339
	dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
L
Linus Torvalds 已提交
340 341

	/* Continue to read if we have still urbs to do. */
A
Alan Cox 已提交
342
	if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
L
Linus Torvalds 已提交
343 344
		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
		if (result)
345 346 347
			dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
				__func__, result);
		dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
L
Linus Torvalds 已提交
348 349 350
	}
}

A
Alan Cox 已提交
351
static void cyberjack_write_bulk_callback(struct urb *urb)
L
Linus Torvalds 已提交
352
{
353
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
354
	struct cyberjack_private *priv = usb_get_serial_port_data(port);
355
	struct device *dev = &port->dev;
356
	int status = urb->status;
357
	unsigned long flags;
358
	bool resubmitted = false;
L
Linus Torvalds 已提交
359

360
	if (status) {
361 362
		dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
			__func__, status);
363
		set_bit(0, &port->write_urbs_free);
L
Linus Torvalds 已提交
364 365 366
		return;
	}

367
	spin_lock_irqsave(&priv->lock, flags);
L
Linus Torvalds 已提交
368 369

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

373
		dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
L
Linus Torvalds 已提交
374 375 376 377

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

A
Alan Cox 已提交
378 379 380
		memcpy(port->write_urb->transfer_buffer,
					priv->wrbuf + priv->wrsent, length);
		priv->wrsent += length;
L
Linus Torvalds 已提交
381 382

		/* set up our urb */
383
		port->write_urb->transfer_buffer_length = length;
L
Linus Torvalds 已提交
384 385 386 387

		/* send the data out the bulk port */
		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (result) {
388
			dev_err(dev, "%s - failed submitting write urb, error %d\n",
389
				__func__, result);
L
Linus Torvalds 已提交
390
			/* Throw away data. No better idea what to do with it. */
391 392
			priv->wrfilled = 0;
			priv->wrsent = 0;
L
Linus Torvalds 已提交
393 394 395
			goto exit;
		}

396 397
		resubmitted = true;

398 399
		dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
		dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
L
Linus Torvalds 已提交
400 401 402

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

A
Alan Cox 已提交
403 404
		if (priv->wrsent >= priv->wrfilled ||
					priv->wrsent >= blksize) {
405
			dev_dbg(dev, "%s - buffer cleaned\n", __func__);
A
Alan Cox 已提交
406
			memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
407 408
			priv->wrfilled = 0;
			priv->wrsent = 0;
L
Linus Torvalds 已提交
409 410 411 412
		}
	}

exit:
413
	spin_unlock_irqrestore(&priv->lock, flags);
414 415
	if (!resubmitted)
		set_bit(0, &port->write_urbs_free);
416
	usb_serial_port_softint(port);
L
Linus Torvalds 已提交
417 418
}

419
module_usb_serial_driver(serial_drivers, id_table);
L
Linus Torvalds 已提交
420

A
Alan Cox 已提交
421 422
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
423
MODULE_LICENSE("GPL");