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 void cyberjack_disconnect(struct usb_serial *serial);
59 static int cyberjack_port_probe(struct usb_serial_port *port);
60 static int cyberjack_port_remove(struct usb_serial_port *port);
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 .disconnect = cyberjack_disconnect,
87 .port_probe = cyberjack_port_probe,
88 .port_remove = cyberjack_port_remove,
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 static int cyberjack_port_probe(struct usb_serial_port *port)
112 struct cyberjack_private *priv;
115 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
119 spin_lock_init(&priv->lock);
124 usb_set_serial_port_data(port, priv);
126 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
128 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
133 static int cyberjack_port_remove(struct usb_serial_port *port)
135 struct cyberjack_private *priv;
137 priv = usb_get_serial_port_data(port);
143 static void cyberjack_disconnect(struct usb_serial *serial)
147 for (i = 0; i < serial->num_ports; ++i)
148 usb_kill_urb(serial->port[i]->interrupt_in_urb);
151 static int cyberjack_open(struct tty_struct *tty,
152 struct usb_serial_port *port)
154 struct cyberjack_private *priv;
158 dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
159 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
161 priv = usb_get_serial_port_data(port);
162 spin_lock_irqsave(&priv->lock, flags);
166 spin_unlock_irqrestore(&priv->lock, flags);
171 static void cyberjack_close(struct usb_serial_port *port)
173 if (port->serial->dev) {
174 /* shutdown any bulk reads that might be going on */
175 usb_kill_urb(port->write_urb);
176 usb_kill_urb(port->read_urb);
180 static int cyberjack_write(struct tty_struct *tty,
181 struct usb_serial_port *port, const unsigned char *buf, int count)
183 struct device *dev = &port->dev;
184 struct cyberjack_private *priv = usb_get_serial_port_data(port);
190 dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
194 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
195 dev_dbg(dev, "%s - already writing\n", __func__);
199 spin_lock_irqsave(&priv->lock, flags);
201 if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
202 /* To much data for buffer. Reset buffer. */
204 spin_unlock_irqrestore(&priv->lock, flags);
205 set_bit(0, &port->write_urbs_free);
210 memcpy(priv->wrbuf + priv->wrfilled, buf, count);
212 usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
213 priv->wrfilled += count;
215 if (priv->wrfilled >= 3) {
216 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
217 dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
219 wrexpected = sizeof(priv->wrbuf);
221 if (priv->wrfilled >= wrexpected) {
222 /* We have enough data to begin transmission */
225 dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
226 length = (wrexpected > port->bulk_out_size) ?
227 port->bulk_out_size : wrexpected;
229 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
230 priv->wrsent = length;
233 port->write_urb->transfer_buffer_length = length;
235 /* send the data out the bulk port */
236 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
239 "%s - failed submitting write urb, error %d",
241 /* Throw away data. No better idea what to do with it. */
244 spin_unlock_irqrestore(&priv->lock, flags);
245 set_bit(0, &port->write_urbs_free);
249 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
250 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
252 if (priv->wrsent >= priv->wrfilled) {
253 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
254 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
260 spin_unlock_irqrestore(&priv->lock, flags);
265 static int cyberjack_write_room(struct tty_struct *tty)
268 return CYBERJACK_LOCAL_BUF_SIZE;
271 static void cyberjack_read_int_callback(struct urb *urb)
273 struct usb_serial_port *port = urb->context;
274 struct cyberjack_private *priv = usb_get_serial_port_data(port);
275 struct device *dev = &port->dev;
276 unsigned char *data = urb->transfer_buffer;
277 int status = urb->status;
280 /* the urb might have been killed. */
284 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
286 /* React only to interrupts signaling a bulk_in transfer */
287 if (urb->actual_length == 4 && data[0] == 0x01) {
290 /* This is a announcement of coming bulk_ins. */
291 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
293 spin_lock(&priv->lock);
295 old_rdtodo = priv->rdtodo;
297 if (old_rdtodo + size < old_rdtodo) {
298 dev_dbg(dev, "To many bulk_in urbs to do.\n");
299 spin_unlock(&priv->lock);
303 /* "+=" is probably more fault tollerant than "=" */
304 priv->rdtodo += size;
306 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
308 spin_unlock(&priv->lock);
311 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
313 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
315 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
320 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
322 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
323 dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
326 static void cyberjack_read_bulk_callback(struct urb *urb)
328 struct usb_serial_port *port = urb->context;
329 struct cyberjack_private *priv = usb_get_serial_port_data(port);
330 struct device *dev = &port->dev;
331 struct tty_struct *tty;
332 unsigned char *data = urb->transfer_buffer;
335 int status = urb->status;
337 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
339 dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
344 tty = tty_port_tty_get(&port->port);
346 dev_dbg(dev, "%s - ignoring since device not open\n", __func__);
349 if (urb->actual_length) {
350 tty_insert_flip_string(tty, data, urb->actual_length);
351 tty_flip_buffer_push(tty);
355 spin_lock(&priv->lock);
357 /* Reduce urbs to do by one. */
358 priv->rdtodo -= urb->actual_length;
359 /* Just to be sure */
360 if (priv->rdtodo < 0)
364 spin_unlock(&priv->lock);
366 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
368 /* Continue to read if we have still urbs to do. */
369 if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
370 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
372 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
374 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
378 static void cyberjack_write_bulk_callback(struct urb *urb)
380 struct usb_serial_port *port = urb->context;
381 struct cyberjack_private *priv = usb_get_serial_port_data(port);
382 struct device *dev = &port->dev;
383 int status = urb->status;
385 set_bit(0, &port->write_urbs_free);
387 dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
392 spin_lock(&priv->lock);
394 /* only do something if we have more data to send */
395 if (priv->wrfilled) {
396 int length, blksize, result;
398 dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
400 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
401 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
403 memcpy(port->write_urb->transfer_buffer,
404 priv->wrbuf + priv->wrsent, length);
405 priv->wrsent += length;
408 port->write_urb->transfer_buffer_length = length;
410 /* send the data out the bulk port */
411 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
413 dev_err(dev, "%s - failed submitting write urb, error %d\n",
415 /* Throw away data. No better idea what to do with it. */
421 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
422 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
424 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
426 if (priv->wrsent >= priv->wrfilled ||
427 priv->wrsent >= blksize) {
428 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
429 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
436 spin_unlock(&priv->lock);
437 usb_serial_port_softint(port);
440 module_usb_serial_driver(serial_drivers, id_table);
442 MODULE_AUTHOR(DRIVER_AUTHOR);
443 MODULE_DESCRIPTION(DRIVER_DESC);
444 MODULE_VERSION(DRIVER_VERSION);
445 MODULE_LICENSE("GPL");