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/config.h>
32 #include <linux/kernel.h>
33 #include <linux/errno.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
36 #include <linux/tty.h>
37 #include <linux/tty_driver.h>
38 #include <linux/tty_flip.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <asm/uaccess.h>
42 #include <linux/usb.h>
43 #include "usb-serial.h"
45 #define CYBERJACK_LOCAL_BUF_SIZE 32
52 #define DRIVER_VERSION "v1.01"
53 #define DRIVER_AUTHOR "Matthias Bruestle"
54 #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
57 #define CYBERJACK_VENDOR_ID 0x0C4B
58 #define CYBERJACK_PRODUCT_ID 0x0100
60 /* Function prototypes */
61 static int cyberjack_startup (struct usb_serial *serial);
62 static void cyberjack_shutdown (struct usb_serial *serial);
63 static int cyberjack_open (struct usb_serial_port *port, struct file *filp);
64 static void cyberjack_close (struct usb_serial_port *port, struct file *filp);
65 static int cyberjack_write (struct usb_serial_port *port, const unsigned char *buf, int count);
66 static int cyberjack_write_room( struct usb_serial_port *port );
67 static void cyberjack_read_int_callback (struct urb *urb, struct pt_regs *regs);
68 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
69 static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
71 static 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_driver cyberjack_driver = {
81 .probe = usb_serial_probe,
82 .disconnect = usb_serial_disconnect,
86 static struct usb_serial_device_type cyberjack_device = {
88 .name = "Reiner SCT Cyberjack USB card reader",
89 .short_name = "cyberjack",
91 .num_interrupt_in = 1,
95 .attach = cyberjack_startup,
96 .shutdown = cyberjack_shutdown,
97 .open = cyberjack_open,
98 .close = cyberjack_close,
99 .write = cyberjack_write,
100 .write_room = cyberjack_write_room,
101 .read_int_callback = cyberjack_read_int_callback,
102 .read_bulk_callback = cyberjack_read_bulk_callback,
103 .write_bulk_callback = cyberjack_write_bulk_callback,
106 struct cyberjack_private {
107 spinlock_t lock; /* Lock for SMP */
108 short rdtodo; /* Bytes still to read */
109 unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
110 short wrfilled; /* Overall data size we already got */
111 short wrsent; /* Data already sent */
114 /* do some startup allocations not currently performed by usb_serial_probe() */
115 static int cyberjack_startup (struct usb_serial *serial)
117 struct cyberjack_private *priv;
120 dbg("%s", __FUNCTION__);
122 /* allocate the private data structure */
123 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
127 /* set initial values */
128 spin_lock_init(&priv->lock);
132 usb_set_serial_port_data(serial->port[0], priv);
134 init_waitqueue_head(&serial->port[0]->write_wait);
136 for (i = 0; i < serial->num_ports; ++i) {
138 serial->port[i]->interrupt_in_urb->dev = serial->dev;
139 result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
142 err(" usb_submit_urb(read int) failed");
143 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
149 static void cyberjack_shutdown (struct usb_serial *serial)
153 dbg("%s", __FUNCTION__);
155 for (i=0; i < serial->num_ports; ++i) {
156 usb_kill_urb(serial->port[i]->interrupt_in_urb);
157 /* My special items, the standard routines free my urbs */
158 kfree(usb_get_serial_port_data(serial->port[i]));
159 usb_set_serial_port_data(serial->port[i], NULL);
163 static int cyberjack_open (struct usb_serial_port *port, struct file *filp)
165 struct cyberjack_private *priv;
169 dbg("%s - port %d", __FUNCTION__, port->number);
171 dbg("%s - usb_clear_halt", __FUNCTION__ );
172 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
174 /* force low_latency on so that our tty_push actually forces
175 * the data through, otherwise it is scheduled, and with high
176 * data rates (like with OHCI) data can get lost.
178 port->tty->low_latency = 1;
180 priv = usb_get_serial_port_data(port);
181 spin_lock_irqsave(&priv->lock, flags);
185 spin_unlock_irqrestore(&priv->lock, flags);
190 static void cyberjack_close (struct usb_serial_port *port, struct file *filp)
192 dbg("%s - port %d", __FUNCTION__, port->number);
194 if (port->serial->dev) {
195 /* shutdown any bulk reads that might be going on */
196 usb_kill_urb(port->write_urb);
197 usb_kill_urb(port->read_urb);
201 static int cyberjack_write (struct usb_serial_port *port, const unsigned char *buf, int count)
203 struct usb_serial *serial = port->serial;
204 struct cyberjack_private *priv = usb_get_serial_port_data(port);
209 dbg("%s - port %d", __FUNCTION__, port->number);
212 dbg("%s - write request of 0 bytes", __FUNCTION__);
216 spin_lock(&port->lock);
217 if (port->write_urb_busy) {
218 spin_unlock(&port->lock);
219 dbg("%s - already writing", __FUNCTION__);
222 port->write_urb_busy = 1;
223 spin_unlock(&port->lock);
225 spin_lock_irqsave(&priv->lock, flags);
227 if( (count+priv->wrfilled)>sizeof(priv->wrbuf) ) {
228 /* To much data for buffer. Reset buffer. */
230 spin_unlock_irqrestore(&priv->lock, flags);
231 port->write_urb_busy = 0;
236 memcpy (priv->wrbuf+priv->wrfilled, buf, count);
238 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
239 priv->wrbuf+priv->wrfilled);
240 priv->wrfilled += count;
242 if( priv->wrfilled >= 3 ) {
243 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
244 dbg("%s - expected data: %d", __FUNCTION__, wrexpected);
246 wrexpected = sizeof(priv->wrbuf);
249 if( priv->wrfilled >= wrexpected ) {
250 /* We have enough data to begin transmission */
253 dbg("%s - transmitting data (frame 1)", __FUNCTION__);
254 length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected;
256 memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length );
260 usb_fill_bulk_urb(port->write_urb, serial->dev,
261 usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
262 port->write_urb->transfer_buffer, length,
263 ((serial->type->write_bulk_callback) ?
264 serial->type->write_bulk_callback :
265 cyberjack_write_bulk_callback),
268 /* send the data out the bulk port */
269 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
271 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
272 /* Throw away data. No better idea what to do with it. */
275 spin_unlock_irqrestore(&priv->lock, flags);
276 port->write_urb_busy = 0;
280 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
281 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
283 if( priv->wrsent>=priv->wrfilled ) {
284 dbg("%s - buffer cleaned", __FUNCTION__);
285 memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
291 spin_unlock_irqrestore(&priv->lock, flags);
296 static int cyberjack_write_room( struct usb_serial_port *port )
298 return CYBERJACK_LOCAL_BUF_SIZE;
301 static void cyberjack_read_int_callback( struct urb *urb, struct pt_regs *regs )
303 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
304 struct cyberjack_private *priv = usb_get_serial_port_data(port);
305 unsigned char *data = urb->transfer_buffer;
308 dbg("%s - port %d", __FUNCTION__, port->number);
310 /* the urb might have been killed. */
314 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
316 /* React only to interrupts signaling a bulk_in transfer */
317 if( (urb->actual_length==4) && (data[0]==0x01) ) {
321 /* This is a announcement of coming bulk_ins. */
322 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
324 spin_lock(&priv->lock);
326 old_rdtodo = priv->rdtodo;
328 if( (old_rdtodo+size)<(old_rdtodo) ) {
329 dbg( "To many bulk_in urbs to do." );
330 spin_unlock(&priv->lock);
334 /* "+=" is probably more fault tollerant than "=" */
335 priv->rdtodo += size;
337 dbg("%s - rdtodo: %d", __FUNCTION__, priv->rdtodo);
339 spin_unlock(&priv->lock);
342 port->read_urb->dev = port->serial->dev;
343 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
345 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
346 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
351 port->interrupt_in_urb->dev = port->serial->dev;
352 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
354 err(" usb_submit_urb(read int) failed");
355 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
358 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
360 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
361 struct cyberjack_private *priv = usb_get_serial_port_data(port);
362 struct tty_struct *tty;
363 unsigned char *data = urb->transfer_buffer;
368 dbg("%s - port %d", __FUNCTION__, port->number);
370 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
372 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
378 dbg("%s - ignoring since device not open\n", __FUNCTION__);
381 if (urb->actual_length) {
382 for (i = 0; i < urb->actual_length ; ++i) {
383 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
384 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
385 tty_flip_buffer_push(tty);
387 /* this doesn't actually push the data through unless tty->low_latency is set */
388 tty_insert_flip_char(tty, data[i], 0);
390 tty_flip_buffer_push(tty);
393 spin_lock(&priv->lock);
395 /* Reduce urbs to do by one. */
396 priv->rdtodo-=urb->actual_length;
397 /* Just to be sure */
398 if ( priv->rdtodo<0 ) priv->rdtodo = 0;
401 spin_unlock(&priv->lock);
403 dbg("%s - rdtodo: %d", __FUNCTION__, todo);
405 /* Continue to read if we have still urbs to do. */
406 if( todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/ ) {
407 port->read_urb->dev = port->serial->dev;
408 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
410 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
411 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
415 static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
417 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
418 struct cyberjack_private *priv = usb_get_serial_port_data(port);
420 dbg("%s - port %d", __FUNCTION__, port->number);
422 port->write_urb_busy = 0;
424 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
428 spin_lock(&priv->lock);
430 /* only do something if we have more data to send */
431 if( priv->wrfilled ) {
432 int length, blksize, result;
434 dbg("%s - transmitting data (frame n)", __FUNCTION__);
436 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
437 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
439 memcpy (port->write_urb->transfer_buffer, priv->wrbuf + priv->wrsent,
441 priv->wrsent+=length;
444 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
445 usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
446 port->write_urb->transfer_buffer, length,
447 ((port->serial->type->write_bulk_callback) ?
448 port->serial->type->write_bulk_callback :
449 cyberjack_write_bulk_callback),
452 /* send the data out the bulk port */
453 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
455 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
456 /* Throw away data. No better idea what to do with it. */
462 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
463 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
465 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
467 if( (priv->wrsent>=priv->wrfilled) || (priv->wrsent>=blksize) ) {
468 dbg("%s - buffer cleaned", __FUNCTION__);
469 memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
476 spin_unlock(&priv->lock);
477 schedule_work(&port->work);
480 static int __init cyberjack_init (void)
483 retval = usb_serial_register(&cyberjack_device);
485 goto failed_usb_serial_register;
486 retval = usb_register(&cyberjack_driver);
488 goto failed_usb_register;
490 info(DRIVER_VERSION " " DRIVER_AUTHOR);
495 usb_serial_deregister(&cyberjack_device);
496 failed_usb_serial_register:
500 static void __exit cyberjack_exit (void)
502 usb_deregister (&cyberjack_driver);
503 usb_serial_deregister (&cyberjack_device);
506 module_init(cyberjack_init);
507 module_exit(cyberjack_exit);
509 MODULE_AUTHOR( DRIVER_AUTHOR );
510 MODULE_DESCRIPTION( DRIVER_DESC );
511 MODULE_VERSION( DRIVER_VERSION );
512 MODULE_LICENSE("GPL");
514 module_param(debug, bool, S_IRUGO | S_IWUSR);
515 MODULE_PARM_DESC(debug, "Debug enabled or not");