2 * REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
4 * Copyright (C) 2001 REINER SCT
5 * Author: Matthias Bruestle
7 * Contact: support@reiner-sct.com (see MAINTAINERS)
9 * This program is largely derived from work by the linux-usb group
10 * and associated source files. Please see the usb/serial files for
11 * individual credits and copyrights.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
21 * In case of problems, please write to the contact e-mail address
24 * Please note that later models of the cyberjack reader family are
25 * supported by a libusb-based userspace device driver.
27 * Homepage: http://www.reiner-sct.de/support/treiber_cyberjack.php#linux
31 #include <linux/kernel.h>
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/slab.h>
35 #include <linux/tty.h>
36 #include <linux/tty_driver.h>
37 #include <linux/tty_flip.h>
38 #include <linux/module.h>
39 #include <linux/spinlock.h>
40 #include <linux/uaccess.h>
41 #include <linux/usb.h>
42 #include <linux/usb/serial.h>
44 #define CYBERJACK_LOCAL_BUF_SIZE 32
51 #define DRIVER_VERSION "v1.01"
52 #define DRIVER_AUTHOR "Matthias Bruestle"
53 #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
56 #define CYBERJACK_VENDOR_ID 0x0C4B
57 #define CYBERJACK_PRODUCT_ID 0x0100
59 /* Function prototypes */
60 static int cyberjack_startup(struct usb_serial *serial);
61 static void cyberjack_disconnect(struct usb_serial *serial);
62 static void cyberjack_release(struct usb_serial *serial);
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);
66 static int cyberjack_write(struct tty_struct *tty,
67 struct usb_serial_port *port, const unsigned char *buf, int count);
68 static int cyberjack_write_room(struct tty_struct *tty);
69 static void cyberjack_read_int_callback(struct urb *urb);
70 static void cyberjack_read_bulk_callback(struct urb *urb);
71 static void cyberjack_write_bulk_callback(struct urb *urb);
73 static const struct usb_device_id id_table[] = {
74 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
75 { } /* Terminating entry */
78 MODULE_DEVICE_TABLE(usb, id_table);
80 static struct usb_serial_driver cyberjack_device = {
85 .description = "Reiner SCT Cyberjack USB card reader",
88 .attach = cyberjack_startup,
89 .disconnect = cyberjack_disconnect,
90 .release = cyberjack_release,
91 .open = cyberjack_open,
92 .close = cyberjack_close,
93 .write = cyberjack_write,
94 .write_room = cyberjack_write_room,
95 .read_int_callback = cyberjack_read_int_callback,
96 .read_bulk_callback = cyberjack_read_bulk_callback,
97 .write_bulk_callback = cyberjack_write_bulk_callback,
100 static struct usb_serial_driver * const serial_drivers[] = {
101 &cyberjack_device, NULL
104 struct cyberjack_private {
105 spinlock_t lock; /* Lock for SMP */
106 short rdtodo; /* Bytes still to read */
107 unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
108 short wrfilled; /* Overall data size we already got */
109 short wrsent; /* Data already sent */
112 /* do some startup allocations not currently performed by usb_serial_probe() */
113 static int cyberjack_startup(struct usb_serial *serial)
115 struct cyberjack_private *priv;
118 /* allocate the private data structure */
119 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
123 /* set initial values */
124 spin_lock_init(&priv->lock);
128 usb_set_serial_port_data(serial->port[0], priv);
130 init_waitqueue_head(&serial->port[0]->write_wait);
132 for (i = 0; i < serial->num_ports; ++i) {
134 result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
137 dev_err(&serial->dev->dev,
138 "usb_submit_urb(read int) failed\n");
139 dev_dbg(&serial->dev->dev, "%s - usb_submit_urb(int urb)\n",
146 static void cyberjack_disconnect(struct usb_serial *serial)
150 for (i = 0; i < serial->num_ports; ++i)
151 usb_kill_urb(serial->port[i]->interrupt_in_urb);
154 static void cyberjack_release(struct usb_serial *serial)
158 for (i = 0; i < serial->num_ports; ++i) {
159 /* My special items, the standard routines free my urbs */
160 kfree(usb_get_serial_port_data(serial->port[i]));
164 static int cyberjack_open(struct tty_struct *tty,
165 struct usb_serial_port *port)
167 struct cyberjack_private *priv;
171 dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
172 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
174 priv = usb_get_serial_port_data(port);
175 spin_lock_irqsave(&priv->lock, flags);
179 spin_unlock_irqrestore(&priv->lock, flags);
184 static void cyberjack_close(struct usb_serial_port *port)
186 if (port->serial->dev) {
187 /* shutdown any bulk reads that might be going on */
188 usb_kill_urb(port->write_urb);
189 usb_kill_urb(port->read_urb);
193 static int cyberjack_write(struct tty_struct *tty,
194 struct usb_serial_port *port, const unsigned char *buf, int count)
196 struct device *dev = &port->dev;
197 struct cyberjack_private *priv = usb_get_serial_port_data(port);
203 dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
207 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
208 dev_dbg(dev, "%s - already writing\n", __func__);
212 spin_lock_irqsave(&priv->lock, flags);
214 if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
215 /* To much data for buffer. Reset buffer. */
217 spin_unlock_irqrestore(&priv->lock, flags);
218 set_bit(0, &port->write_urbs_free);
223 memcpy(priv->wrbuf + priv->wrfilled, buf, count);
225 usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
226 priv->wrfilled += count;
228 if (priv->wrfilled >= 3) {
229 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
230 dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
232 wrexpected = sizeof(priv->wrbuf);
234 if (priv->wrfilled >= wrexpected) {
235 /* We have enough data to begin transmission */
238 dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
239 length = (wrexpected > port->bulk_out_size) ?
240 port->bulk_out_size : wrexpected;
242 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
243 priv->wrsent = length;
246 port->write_urb->transfer_buffer_length = length;
248 /* send the data out the bulk port */
249 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
252 "%s - failed submitting write urb, error %d",
254 /* Throw away data. No better idea what to do with it. */
257 spin_unlock_irqrestore(&priv->lock, flags);
258 set_bit(0, &port->write_urbs_free);
262 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
263 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
265 if (priv->wrsent >= priv->wrfilled) {
266 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
267 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
273 spin_unlock_irqrestore(&priv->lock, flags);
278 static int cyberjack_write_room(struct tty_struct *tty)
281 return CYBERJACK_LOCAL_BUF_SIZE;
284 static void cyberjack_read_int_callback(struct urb *urb)
286 struct usb_serial_port *port = urb->context;
287 struct cyberjack_private *priv = usb_get_serial_port_data(port);
288 struct device *dev = &port->dev;
289 unsigned char *data = urb->transfer_buffer;
290 int status = urb->status;
293 /* the urb might have been killed. */
297 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
299 /* React only to interrupts signaling a bulk_in transfer */
300 if (urb->actual_length == 4 && data[0] == 0x01) {
303 /* This is a announcement of coming bulk_ins. */
304 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
306 spin_lock(&priv->lock);
308 old_rdtodo = priv->rdtodo;
310 if (old_rdtodo + size < old_rdtodo) {
311 dev_dbg(dev, "To many bulk_in urbs to do.\n");
312 spin_unlock(&priv->lock);
316 /* "+=" is probably more fault tollerant than "=" */
317 priv->rdtodo += size;
319 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
321 spin_unlock(&priv->lock);
324 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
326 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
328 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
333 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
335 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
336 dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
339 static void cyberjack_read_bulk_callback(struct urb *urb)
341 struct usb_serial_port *port = urb->context;
342 struct cyberjack_private *priv = usb_get_serial_port_data(port);
343 struct device *dev = &port->dev;
344 struct tty_struct *tty;
345 unsigned char *data = urb->transfer_buffer;
348 int status = urb->status;
350 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
352 dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
357 tty = tty_port_tty_get(&port->port);
359 dev_dbg(dev, "%s - ignoring since device not open\n", __func__);
362 if (urb->actual_length) {
363 tty_insert_flip_string(tty, data, urb->actual_length);
364 tty_flip_buffer_push(tty);
368 spin_lock(&priv->lock);
370 /* Reduce urbs to do by one. */
371 priv->rdtodo -= urb->actual_length;
372 /* Just to be sure */
373 if (priv->rdtodo < 0)
377 spin_unlock(&priv->lock);
379 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
381 /* Continue to read if we have still urbs to do. */
382 if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
383 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
385 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
387 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
391 static void cyberjack_write_bulk_callback(struct urb *urb)
393 struct usb_serial_port *port = urb->context;
394 struct cyberjack_private *priv = usb_get_serial_port_data(port);
395 struct device *dev = &port->dev;
396 int status = urb->status;
398 set_bit(0, &port->write_urbs_free);
400 dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
405 spin_lock(&priv->lock);
407 /* only do something if we have more data to send */
408 if (priv->wrfilled) {
409 int length, blksize, result;
411 dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
413 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
414 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
416 memcpy(port->write_urb->transfer_buffer,
417 priv->wrbuf + priv->wrsent, length);
418 priv->wrsent += length;
421 port->write_urb->transfer_buffer_length = length;
423 /* send the data out the bulk port */
424 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
426 dev_err(dev, "%s - failed submitting write urb, error %d\n",
428 /* Throw away data. No better idea what to do with it. */
434 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
435 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
437 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
439 if (priv->wrsent >= priv->wrfilled ||
440 priv->wrsent >= blksize) {
441 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
442 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
449 spin_unlock(&priv->lock);
450 usb_serial_port_softint(port);
453 module_usb_serial_driver(serial_drivers, id_table);
455 MODULE_AUTHOR(DRIVER_AUTHOR);
456 MODULE_DESCRIPTION(DRIVER_DESC);
457 MODULE_VERSION(DRIVER_VERSION);
458 MODULE_LICENSE("GPL");
460 module_param(debug, bool, S_IRUGO | S_IWUSR);
461 MODULE_PARM_DESC(debug, "Debug enabled or not");