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
49 #define DRIVER_VERSION "v1.01"
50 #define DRIVER_AUTHOR "Matthias Bruestle"
51 #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
54 #define CYBERJACK_VENDOR_ID 0x0C4B
55 #define CYBERJACK_PRODUCT_ID 0x0100
57 /* Function prototypes */
58 static int cyberjack_startup(struct usb_serial *serial);
59 static void cyberjack_disconnect(struct usb_serial *serial);
60 static void cyberjack_release(struct usb_serial *serial);
61 static int cyberjack_open(struct tty_struct *tty,
62 struct usb_serial_port *port);
63 static void cyberjack_close(struct usb_serial_port *port);
64 static int cyberjack_write(struct tty_struct *tty,
65 struct usb_serial_port *port, const unsigned char *buf, int count);
66 static int cyberjack_write_room(struct tty_struct *tty);
67 static void cyberjack_read_int_callback(struct urb *urb);
68 static void cyberjack_read_bulk_callback(struct urb *urb);
69 static void cyberjack_write_bulk_callback(struct urb *urb);
71 static const struct usb_device_id id_table[] = {
72 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
73 { } /* Terminating entry */
76 MODULE_DEVICE_TABLE(usb, id_table);
78 static struct usb_serial_driver cyberjack_device = {
83 .description = "Reiner SCT Cyberjack USB card reader",
86 .attach = cyberjack_startup,
87 .disconnect = cyberjack_disconnect,
88 .release = cyberjack_release,
89 .open = cyberjack_open,
90 .close = cyberjack_close,
91 .write = cyberjack_write,
92 .write_room = cyberjack_write_room,
93 .read_int_callback = cyberjack_read_int_callback,
94 .read_bulk_callback = cyberjack_read_bulk_callback,
95 .write_bulk_callback = cyberjack_write_bulk_callback,
98 static struct usb_serial_driver * const serial_drivers[] = {
99 &cyberjack_device, NULL
102 struct cyberjack_private {
103 spinlock_t lock; /* Lock for SMP */
104 short rdtodo; /* Bytes still to read */
105 unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
106 short wrfilled; /* Overall data size we already got */
107 short wrsent; /* Data already sent */
110 /* do some startup allocations not currently performed by usb_serial_probe() */
111 static int cyberjack_startup(struct usb_serial *serial)
113 struct cyberjack_private *priv;
116 /* allocate the private data structure */
117 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
121 /* set initial values */
122 spin_lock_init(&priv->lock);
126 usb_set_serial_port_data(serial->port[0], priv);
128 init_waitqueue_head(&serial->port[0]->write_wait);
130 for (i = 0; i < serial->num_ports; ++i) {
132 result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
135 dev_err(&serial->dev->dev,
136 "usb_submit_urb(read int) failed\n");
137 dev_dbg(&serial->dev->dev, "%s - usb_submit_urb(int urb)\n",
144 static void cyberjack_disconnect(struct usb_serial *serial)
148 for (i = 0; i < serial->num_ports; ++i)
149 usb_kill_urb(serial->port[i]->interrupt_in_urb);
152 static void cyberjack_release(struct usb_serial *serial)
156 for (i = 0; i < serial->num_ports; ++i) {
157 /* My special items, the standard routines free my urbs */
158 kfree(usb_get_serial_port_data(serial->port[i]));
162 static int cyberjack_open(struct tty_struct *tty,
163 struct usb_serial_port *port)
165 struct cyberjack_private *priv;
169 dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
170 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
172 priv = usb_get_serial_port_data(port);
173 spin_lock_irqsave(&priv->lock, flags);
177 spin_unlock_irqrestore(&priv->lock, flags);
182 static void cyberjack_close(struct usb_serial_port *port)
184 if (port->serial->dev) {
185 /* shutdown any bulk reads that might be going on */
186 usb_kill_urb(port->write_urb);
187 usb_kill_urb(port->read_urb);
191 static int cyberjack_write(struct tty_struct *tty,
192 struct usb_serial_port *port, const unsigned char *buf, int count)
194 struct device *dev = &port->dev;
195 struct cyberjack_private *priv = usb_get_serial_port_data(port);
201 dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
205 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
206 dev_dbg(dev, "%s - already writing\n", __func__);
210 spin_lock_irqsave(&priv->lock, flags);
212 if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
213 /* To much data for buffer. Reset buffer. */
215 spin_unlock_irqrestore(&priv->lock, flags);
216 set_bit(0, &port->write_urbs_free);
221 memcpy(priv->wrbuf + priv->wrfilled, buf, count);
223 usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
224 priv->wrfilled += count;
226 if (priv->wrfilled >= 3) {
227 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
228 dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
230 wrexpected = sizeof(priv->wrbuf);
232 if (priv->wrfilled >= wrexpected) {
233 /* We have enough data to begin transmission */
236 dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
237 length = (wrexpected > port->bulk_out_size) ?
238 port->bulk_out_size : wrexpected;
240 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
241 priv->wrsent = length;
244 port->write_urb->transfer_buffer_length = length;
246 /* send the data out the bulk port */
247 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
250 "%s - failed submitting write urb, error %d",
252 /* Throw away data. No better idea what to do with it. */
255 spin_unlock_irqrestore(&priv->lock, flags);
256 set_bit(0, &port->write_urbs_free);
260 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
261 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
263 if (priv->wrsent >= priv->wrfilled) {
264 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
265 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
271 spin_unlock_irqrestore(&priv->lock, flags);
276 static int cyberjack_write_room(struct tty_struct *tty)
279 return CYBERJACK_LOCAL_BUF_SIZE;
282 static void cyberjack_read_int_callback(struct urb *urb)
284 struct usb_serial_port *port = urb->context;
285 struct cyberjack_private *priv = usb_get_serial_port_data(port);
286 struct device *dev = &port->dev;
287 unsigned char *data = urb->transfer_buffer;
288 int status = urb->status;
291 /* the urb might have been killed. */
295 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
297 /* React only to interrupts signaling a bulk_in transfer */
298 if (urb->actual_length == 4 && data[0] == 0x01) {
301 /* This is a announcement of coming bulk_ins. */
302 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
304 spin_lock(&priv->lock);
306 old_rdtodo = priv->rdtodo;
308 if (old_rdtodo + size < old_rdtodo) {
309 dev_dbg(dev, "To many bulk_in urbs to do.\n");
310 spin_unlock(&priv->lock);
314 /* "+=" is probably more fault tollerant than "=" */
315 priv->rdtodo += size;
317 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
319 spin_unlock(&priv->lock);
322 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
324 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
326 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
331 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
333 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
334 dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
337 static void cyberjack_read_bulk_callback(struct urb *urb)
339 struct usb_serial_port *port = urb->context;
340 struct cyberjack_private *priv = usb_get_serial_port_data(port);
341 struct device *dev = &port->dev;
342 struct tty_struct *tty;
343 unsigned char *data = urb->transfer_buffer;
346 int status = urb->status;
348 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
350 dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
355 tty = tty_port_tty_get(&port->port);
357 dev_dbg(dev, "%s - ignoring since device not open\n", __func__);
360 if (urb->actual_length) {
361 tty_insert_flip_string(tty, data, urb->actual_length);
362 tty_flip_buffer_push(tty);
366 spin_lock(&priv->lock);
368 /* Reduce urbs to do by one. */
369 priv->rdtodo -= urb->actual_length;
370 /* Just to be sure */
371 if (priv->rdtodo < 0)
375 spin_unlock(&priv->lock);
377 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
379 /* Continue to read if we have still urbs to do. */
380 if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
381 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
383 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
385 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
389 static void cyberjack_write_bulk_callback(struct urb *urb)
391 struct usb_serial_port *port = urb->context;
392 struct cyberjack_private *priv = usb_get_serial_port_data(port);
393 struct device *dev = &port->dev;
394 int status = urb->status;
396 set_bit(0, &port->write_urbs_free);
398 dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
403 spin_lock(&priv->lock);
405 /* only do something if we have more data to send */
406 if (priv->wrfilled) {
407 int length, blksize, result;
409 dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
411 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
412 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
414 memcpy(port->write_urb->transfer_buffer,
415 priv->wrbuf + priv->wrsent, length);
416 priv->wrsent += length;
419 port->write_urb->transfer_buffer_length = length;
421 /* send the data out the bulk port */
422 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
424 dev_err(dev, "%s - failed submitting write urb, error %d\n",
426 /* Throw away data. No better idea what to do with it. */
432 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
433 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
435 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
437 if (priv->wrsent >= priv->wrfilled ||
438 priv->wrsent >= blksize) {
439 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
440 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
447 spin_unlock(&priv->lock);
448 usb_serial_port_softint(port);
451 module_usb_serial_driver(serial_drivers, id_table);
453 MODULE_AUTHOR(DRIVER_AUTHOR);
454 MODULE_DESCRIPTION(DRIVER_DESC);
455 MODULE_VERSION(DRIVER_VERSION);
456 MODULE_LICENSE("GPL");