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 #ifdef CONFIG_USB_SERIAL_DEBUG
45 #include "usb-serial.h"
50 #define DRIVER_VERSION "v1.0"
51 #define DRIVER_AUTHOR "Matthias Bruestle"
52 #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
55 #define CYBERJACK_VENDOR_ID 0x0C4B
56 #define CYBERJACK_PRODUCT_ID 0x0100
58 /* Function prototypes */
59 static int cyberjack_startup (struct usb_serial *serial);
60 static void cyberjack_shutdown (struct usb_serial *serial);
61 static int cyberjack_open (struct usb_serial_port *port, struct file *filp);
62 static void cyberjack_close (struct usb_serial_port *port, struct file *filp);
63 static int cyberjack_write (struct usb_serial_port *port, int from_user,
64 const unsigned char *buf, int count);
65 static void cyberjack_read_int_callback( struct urb *urb );
66 static void cyberjack_read_bulk_callback (struct urb *urb);
67 static void cyberjack_write_bulk_callback (struct urb *urb);
69 static struct usb_device_id id_table [] = {
70 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
71 { } /* Terminating entry */
74 MODULE_DEVICE_TABLE (usb, id_table);
76 static struct usb_serial_device_type cyberjack_device = {
78 .name = "Reiner SCT Cyberjack USB card reader",
80 .num_interrupt_in = 1,
84 .startup = cyberjack_startup,
85 .shutdown = cyberjack_shutdown,
86 .open = cyberjack_open,
87 .close = cyberjack_close,
88 .write = cyberjack_write,
89 .read_int_callback = cyberjack_read_int_callback,
90 .read_bulk_callback = cyberjack_read_bulk_callback,
91 .write_bulk_callback = cyberjack_write_bulk_callback,
94 struct cyberjack_private {
95 short rdtodo; /* Bytes still to read */
96 unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
97 short wrfilled; /* Overall data size we already got */
98 short wrsent; /* Data akready sent */
101 /* do some startup allocations not currently performed by usb_serial_probe() */
102 static int cyberjack_startup (struct usb_serial *serial)
104 struct cyberjack_private *priv;
106 dbg("%s", __FUNCTION__);
108 /* allocate the private data structure */
109 serial->port->private = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
110 if (!serial->port->private)
111 return (-1); /* error */
113 /* set initial values */
114 priv = (struct cyberjack_private *)serial->port->private;
119 init_waitqueue_head(&serial->port->write_wait);
124 static void cyberjack_shutdown (struct usb_serial *serial)
128 dbg("%s", __FUNCTION__);
130 for (i=0; i < serial->num_ports; ++i) {
131 /* My special items, the standard routines free my urbs */
132 if (serial->port[i].private)
133 kfree(serial->port[i].private);
137 static int cyberjack_open (struct usb_serial_port *port, struct file *filp)
139 struct cyberjack_private *priv;
142 if (port_paranoia_check (port, __FUNCTION__))
145 dbg("%s - port %d", __FUNCTION__, port->number);
147 /* force low_latency on so that our tty_push actually forces
148 * the data through, otherwise it is scheduled, and with high
149 * data rates (like with OHCI) data can get lost.
151 port->tty->low_latency = 1;
153 priv = (struct cyberjack_private *)port->private;
158 /* shutdown any bulk reads that might be going on */
159 usb_unlink_urb (port->write_urb);
160 usb_unlink_urb (port->read_urb);
161 usb_unlink_urb (port->interrupt_in_urb);
163 port->interrupt_in_urb->dev = port->serial->dev;
164 result = usb_submit_urb(port->interrupt_in_urb);
166 err(" usb_submit_urb(read int) failed");
167 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
172 static void cyberjack_close (struct usb_serial_port *port, struct file *filp)
174 dbg("%s - port %d", __FUNCTION__, port->number);
176 if (port->serial->dev) {
177 /* shutdown any bulk reads that might be going on */
178 usb_unlink_urb (port->write_urb);
179 usb_unlink_urb (port->read_urb);
180 usb_unlink_urb (port->interrupt_in_urb);
184 static int cyberjack_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
186 struct usb_serial *serial = port->serial;
187 struct cyberjack_private *priv = (struct cyberjack_private *)port->private;
191 dbg("%s - port %d", __FUNCTION__, port->number);
192 dbg("%s - from_user %d", __FUNCTION__, from_user);
195 dbg("%s - write request of 0 bytes", __FUNCTION__);
199 if (port->write_urb->status == -EINPROGRESS) {
200 dbg("%s - already writing", __FUNCTION__);
204 if( (count+priv->wrfilled)>sizeof(priv->wrbuf) ) {
205 /* To much data for buffer. Reset buffer. */
212 if (copy_from_user(priv->wrbuf+priv->wrfilled, buf, count)) {
216 memcpy (priv->wrbuf+priv->wrfilled, buf, count);
218 usb_serial_debug_data (__FILE__, __FUNCTION__, count,
219 priv->wrbuf+priv->wrfilled);
220 priv->wrfilled += count;
222 if( priv->wrfilled >= 3 ) {
223 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
224 dbg("%s - expected data: %d", __FUNCTION__, wrexpected);
226 wrexpected = sizeof(priv->wrbuf);
229 if( priv->wrfilled >= wrexpected ) {
230 /* We have enough data to begin transmission */
233 dbg("%s - transmitting data (frame 1)", __FUNCTION__);
234 length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected;
236 memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length );
240 FILL_BULK_URB(port->write_urb, serial->dev,
241 usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
242 port->write_urb->transfer_buffer, length,
243 ((serial->type->write_bulk_callback) ?
244 serial->type->write_bulk_callback :
245 cyberjack_write_bulk_callback),
248 /* send the data out the bulk port */
249 result = usb_submit_urb(port->write_urb);
251 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
252 /* Throw away data. No better idea what to do with it. */
258 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
259 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
261 if( priv->wrsent>=priv->wrfilled ) {
262 dbg("%s - buffer cleaned", __FUNCTION__);
263 memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
272 static void cyberjack_read_int_callback( struct urb *urb )
274 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
275 struct cyberjack_private *priv = (struct cyberjack_private *)port->private;
276 struct usb_serial *serial;
277 unsigned char *data = urb->transfer_buffer;
279 if (port_paranoia_check (port, __FUNCTION__)) return;
281 dbg("%s - port %d", __FUNCTION__, port->number);
283 /* the urb might have been killed. */
287 serial = port->serial;
288 if (serial_paranoia_check (serial, __FUNCTION__)) return;
290 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
292 /* React only to interrupts signaling a bulk_in transfer */
293 if( (urb->actual_length==4) && (data[0]==0x01) ) {
294 short old_rdtodo = priv->rdtodo;
297 /* This is a announcement of comming bulk_ins. */
298 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
300 if( (size>259) || (size==0) ) {
301 dbg( "Bad announced bulk_in data length: %d", size );
302 /* Dunno what is most reliable to do here. */
306 if( (old_rdtodo+size)<(old_rdtodo) ) {
307 dbg( "To many bulk_in urbs to do." );
311 /* "+=" is probably more fault tollerant than "=" */
312 priv->rdtodo += size;
314 dbg("%s - rdtodo: %d", __FUNCTION__, priv->rdtodo);
317 port->read_urb->dev = port->serial->dev;
318 result = usb_submit_urb(port->read_urb);
320 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
321 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
326 static void cyberjack_read_bulk_callback (struct urb *urb)
328 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
329 struct cyberjack_private *priv = (struct cyberjack_private *)port->private;
330 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
331 struct tty_struct *tty;
332 unsigned char *data = urb->transfer_buffer;
336 dbg("%s - port %d", __FUNCTION__, port->number);
339 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
344 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, urb->transfer_buffer);
345 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
349 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
352 if (urb->actual_length) {
353 for (i = 0; i < urb->actual_length ; ++i) {
354 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
355 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
356 tty_flip_buffer_push(tty);
358 /* this doesn't actually push the data through unless tty->low_latency is set */
359 tty_insert_flip_char(tty, data[i], 0);
361 tty_flip_buffer_push(tty);
364 /* Reduce urbs to do by one. */
365 priv->rdtodo-=urb->actual_length;
366 /* Just to be sure */
367 if( priv->rdtodo<0 ) priv->rdtodo=0;
369 dbg("%s - rdtodo: %d", __FUNCTION__, priv->rdtodo);
371 /* Continue to read if we have still urbs to do. */
372 if( priv->rdtodo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/ ) {
373 port->read_urb->dev = port->serial->dev;
374 result = usb_submit_urb(port->read_urb);
376 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
377 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
381 static void cyberjack_write_bulk_callback (struct urb *urb)
383 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
384 struct cyberjack_private *priv = (struct cyberjack_private *)port->private;
385 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
387 dbg("%s - port %d", __FUNCTION__, port->number);
390 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
395 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
399 /* only do something if we have more data to send */
400 if( priv->wrfilled ) {
401 int length, blksize, result;
403 if (port->write_urb->status == -EINPROGRESS) {
404 dbg("%s - already writing", __FUNCTION__);
408 dbg("%s - transmitting data (frame n)", __FUNCTION__);
410 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
411 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
413 memcpy (port->write_urb->transfer_buffer, priv->wrbuf + priv->wrsent,
415 priv->wrsent+=length;
418 FILL_BULK_URB(port->write_urb, serial->dev,
419 usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
420 port->write_urb->transfer_buffer, length,
421 ((serial->type->write_bulk_callback) ?
422 serial->type->write_bulk_callback :
423 cyberjack_write_bulk_callback),
426 /* send the data out the bulk port */
427 result = usb_submit_urb(port->write_urb);
429 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
430 /* Throw away data. No better idea what to do with it. */
433 queue_task(&port->tqueue, &tq_immediate);
434 mark_bh(IMMEDIATE_BH);
438 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
439 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
441 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
443 if( (priv->wrsent>=priv->wrfilled) || (priv->wrsent>=blksize) ) {
444 dbg("%s - buffer cleaned", __FUNCTION__);
445 memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
450 queue_task(&port->tqueue, &tq_immediate);
451 mark_bh(IMMEDIATE_BH);
455 queue_task(&port->tqueue, &tq_immediate);
456 mark_bh(IMMEDIATE_BH);
461 static int __init cyberjack_init (void)
463 usb_serial_register (&cyberjack_device);
465 info(DRIVER_VERSION " " DRIVER_AUTHOR);
471 static void __exit cyberjack_exit (void)
473 usb_serial_deregister (&cyberjack_device);
476 module_init(cyberjack_init);
477 module_exit(cyberjack_exit);
479 MODULE_AUTHOR( DRIVER_AUTHOR );
480 MODULE_DESCRIPTION( DRIVER_DESC );
481 MODULE_LICENSE("GPL");
483 MODULE_PARM(debug, "i");
484 MODULE_PARM_DESC(debug, "Debug enabled or not");