2 * REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
4 * Copyright (C) 2001 REINER SCT
5 * Author: Matthias Bruestle
7 * Contact: linux-usb@sii.li (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
26 #include <linux/config.h>
27 #include <linux/kernel.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/tty.h>
32 #include <linux/tty_driver.h>
33 #include <linux/tty_flip.h>
34 #include <linux/module.h>
35 #include <linux/spinlock.h>
36 #include <asm/uaccess.h>
37 #include <linux/usb.h>
39 #define CYBERJACK_LOCAL_BUF_SIZE 32
41 #ifdef CONFIG_USB_SERIAL_DEBUG
47 #include "usb-serial.h"
52 #define DRIVER_VERSION "v1.0"
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, int from_user,
66 const unsigned char *buf, int count);
67 static int cyberjack_write_room( struct usb_serial_port *port );
68 static void cyberjack_read_int_callback (struct urb *urb, struct pt_regs *regs);
69 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
70 static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
72 static struct usb_device_id id_table [] = {
73 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
74 { } /* Terminating entry */
77 MODULE_DEVICE_TABLE (usb, id_table);
79 static struct usb_driver cyberjack_driver = {
82 .probe = usb_serial_probe,
83 .disconnect = usb_serial_disconnect,
87 static struct usb_serial_device_type cyberjack_device = {
89 .name = "Reiner SCT Cyberjack USB card reader",
90 .short_name = "cyberjack",
92 .num_interrupt_in = 1,
96 .attach = cyberjack_startup,
97 .shutdown = cyberjack_shutdown,
98 .open = cyberjack_open,
99 .close = cyberjack_close,
100 .write = cyberjack_write,
101 .write_room = cyberjack_write_room,
102 .read_int_callback = cyberjack_read_int_callback,
103 .read_bulk_callback = cyberjack_read_bulk_callback,
104 .write_bulk_callback = cyberjack_write_bulk_callback,
107 struct cyberjack_private {
108 spinlock_t lock; /* Lock for SMP */
109 short rdtodo; /* Bytes still to read */
110 unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
111 short wrfilled; /* Overall data size we already got */
112 short wrsent; /* Data already sent */
115 /* do some startup allocations not currently performed by usb_serial_probe() */
116 static int cyberjack_startup (struct usb_serial *serial)
118 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);
139 static void cyberjack_shutdown (struct usb_serial *serial)
143 dbg("%s", __FUNCTION__);
145 for (i=0; i < serial->num_ports; ++i) {
146 /* My special items, the standard routines free my urbs */
147 kfree(usb_get_serial_port_data(serial->port[i]));
148 usb_set_serial_port_data(serial->port[i], NULL);
152 static int cyberjack_open (struct usb_serial_port *port, struct file *filp)
154 struct cyberjack_private *priv;
158 dbg("%s - port %d", __FUNCTION__, port->number);
160 dbg("%s - usb_clear_halt", __FUNCTION__ );
161 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
163 /* force low_latency on so that our tty_push actually forces
164 * the data through, otherwise it is scheduled, and with high
165 * data rates (like with OHCI) data can get lost.
167 port->tty->low_latency = 1;
169 priv = usb_get_serial_port_data(port);
170 spin_lock_irqsave(&priv->lock, flags);
174 spin_unlock_irqrestore(&priv->lock, flags);
176 /* shutdown any bulk reads that might be going on */
177 usb_unlink_urb (port->write_urb);
178 usb_unlink_urb (port->read_urb);
179 usb_unlink_urb (port->interrupt_in_urb);
181 port->interrupt_in_urb->dev = port->serial->dev;
182 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
184 err(" usb_submit_urb(read int) failed");
185 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
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_unlink_urb (port->write_urb);
197 usb_unlink_urb (port->read_urb);
198 usb_unlink_urb (port->interrupt_in_urb);
199 dbg("%s - usb_clear_halt", __FUNCTION__ );
200 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
201 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
202 usb_clear_halt(port->serial->dev, port->interrupt_in_urb->pipe);
206 static int cyberjack_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
208 struct usb_serial *serial = port->serial;
209 struct cyberjack_private *priv = usb_get_serial_port_data(port);
214 dbg("%s - port %d", __FUNCTION__, port->number);
215 dbg("%s - from_user %d", __FUNCTION__, from_user);
218 dbg("%s - write request of 0 bytes", __FUNCTION__);
222 if (port->write_urb->status == -EINPROGRESS) {
223 dbg("%s - already writing", __FUNCTION__);
227 spin_lock_irqsave(&priv->lock, flags);
229 if( (count+priv->wrfilled)>sizeof(priv->wrbuf) ) {
230 /* To much data for buffer. Reset buffer. */
232 spin_unlock_irqrestore(&priv->lock, flags);
238 if (copy_from_user(priv->wrbuf+priv->wrfilled, buf, count)) {
239 spin_unlock_irqrestore(&priv->lock, flags);
243 memcpy (priv->wrbuf+priv->wrfilled, buf, count);
246 usb_serial_debug_data (__FILE__, __FUNCTION__, count,
247 priv->wrbuf+priv->wrfilled);
248 priv->wrfilled += count;
250 if( priv->wrfilled >= 3 ) {
251 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
252 dbg("%s - expected data: %d", __FUNCTION__, wrexpected);
254 wrexpected = sizeof(priv->wrbuf);
257 if( priv->wrfilled >= wrexpected ) {
258 /* We have enough data to begin transmission */
261 dbg("%s - transmitting data (frame 1)", __FUNCTION__);
262 length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected;
264 memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length );
268 usb_fill_bulk_urb(port->write_urb, serial->dev,
269 usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
270 port->write_urb->transfer_buffer, length,
271 ((serial->type->write_bulk_callback) ?
272 serial->type->write_bulk_callback :
273 cyberjack_write_bulk_callback),
276 /* send the data out the bulk port */
277 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
279 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
280 /* Throw away data. No better idea what to do with it. */
283 spin_unlock_irqrestore(&priv->lock, flags);
287 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
288 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
290 if( priv->wrsent>=priv->wrfilled ) {
291 dbg("%s - buffer cleaned", __FUNCTION__);
292 memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
298 spin_unlock_irqrestore(&priv->lock, flags);
303 static int cyberjack_write_room( struct usb_serial_port *port )
305 return CYBERJACK_LOCAL_BUF_SIZE;
308 static void cyberjack_read_int_callback( struct urb *urb, struct pt_regs *regs )
310 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
311 struct cyberjack_private *priv = usb_get_serial_port_data(port);
312 unsigned char *data = urb->transfer_buffer;
315 dbg("%s - port %d", __FUNCTION__, port->number);
317 /* the urb might have been killed. */
321 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
323 /* React only to interrupts signaling a bulk_in transfer */
324 if( (urb->actual_length==4) && (data[0]==0x01) ) {
328 /* This is a announcement of coming bulk_ins. */
329 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
331 spin_lock(&priv->lock);
333 old_rdtodo = priv->rdtodo;
335 if( (old_rdtodo+size)<(old_rdtodo) ) {
336 dbg( "To many bulk_in urbs to do." );
337 spin_unlock(&priv->lock);
341 /* "+=" is probably more fault tollerant than "=" */
342 priv->rdtodo += size;
344 dbg("%s - rdtodo: %d", __FUNCTION__, priv->rdtodo);
346 spin_unlock(&priv->lock);
349 port->read_urb->dev = port->serial->dev;
350 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
352 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
353 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
358 port->interrupt_in_urb->dev = port->serial->dev;
359 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
361 err(" usb_submit_urb(read int) failed");
362 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
365 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
367 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
368 struct cyberjack_private *priv = usb_get_serial_port_data(port);
369 struct tty_struct *tty;
370 unsigned char *data = urb->transfer_buffer;
375 dbg("%s - port %d", __FUNCTION__, port->number);
378 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, urb->transfer_buffer);
379 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
383 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
386 if (urb->actual_length) {
387 for (i = 0; i < urb->actual_length ; ++i) {
388 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
389 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
390 tty_flip_buffer_push(tty);
392 /* this doesn't actually push the data through unless tty->low_latency is set */
393 tty_insert_flip_char(tty, data[i], 0);
395 tty_flip_buffer_push(tty);
398 spin_lock(&priv->lock);
400 /* Reduce urbs to do by one. */
401 priv->rdtodo-=urb->actual_length;
402 /* Just to be sure */
403 if ( priv->rdtodo<0 ) priv->rdtodo = 0;
406 spin_unlock(&priv->lock);
408 dbg("%s - rdtodo: %d", __FUNCTION__, todo);
410 /* Continue to read if we have still urbs to do. */
411 if( todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/ ) {
412 port->read_urb->dev = port->serial->dev;
413 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
415 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
416 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
420 static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
422 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
423 struct cyberjack_private *priv = usb_get_serial_port_data(port);
425 dbg("%s - port %d", __FUNCTION__, port->number);
428 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
432 spin_lock(&priv->lock);
434 /* only do something if we have more data to send */
435 if( priv->wrfilled ) {
436 int length, blksize, result;
438 if (port->write_urb->status == -EINPROGRESS) {
439 dbg("%s - already writing", __FUNCTION__);
440 spin_unlock(&priv->lock);
444 dbg("%s - transmitting data (frame n)", __FUNCTION__);
446 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
447 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
449 memcpy (port->write_urb->transfer_buffer, priv->wrbuf + priv->wrsent,
451 priv->wrsent+=length;
454 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
455 usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
456 port->write_urb->transfer_buffer, length,
457 ((port->serial->type->write_bulk_callback) ?
458 port->serial->type->write_bulk_callback :
459 cyberjack_write_bulk_callback),
462 /* send the data out the bulk port */
463 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
465 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
466 /* Throw away data. No better idea what to do with it. */
472 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
473 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
475 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
477 if( (priv->wrsent>=priv->wrfilled) || (priv->wrsent>=blksize) ) {
478 dbg("%s - buffer cleaned", __FUNCTION__);
479 memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
486 spin_unlock(&priv->lock);
487 schedule_work(&port->work);
490 static int __init cyberjack_init (void)
493 retval = usb_serial_register(&cyberjack_device);
495 goto failed_usb_serial_register;
496 retval = usb_register(&cyberjack_driver);
498 goto failed_usb_register;
500 info(DRIVER_VERSION " " DRIVER_AUTHOR);
505 usb_serial_deregister(&cyberjack_device);
506 failed_usb_serial_register:
510 static void __exit cyberjack_exit (void)
512 usb_deregister (&cyberjack_driver);
513 usb_serial_deregister (&cyberjack_device);
516 module_init(cyberjack_init);
517 module_exit(cyberjack_exit);
519 MODULE_AUTHOR( DRIVER_AUTHOR );
520 MODULE_DESCRIPTION( DRIVER_DESC );
521 MODULE_LICENSE("GPL");
523 MODULE_PARM(debug, "i");
524 MODULE_PARM_DESC(debug, "Debug enabled or not");