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 <asm/uaccess.h>
41 #include <linux/usb.h>
42 #include <linux/usb/serial.h>
44 #define CYBERJACK_LOCAL_BUF_SIZE 32
51 #define DRIVER_VERSION "v1.01"
52 #define DRIVER_AUTHOR "Matthias Bruestle"
53 #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
56 #define CYBERJACK_VENDOR_ID 0x0C4B
57 #define CYBERJACK_PRODUCT_ID 0x0100
59 /* Function prototypes */
60 static int cyberjack_startup (struct usb_serial
*serial
);
61 static void cyberjack_shutdown (struct usb_serial
*serial
);
62 static int cyberjack_open (struct usb_serial_port
*port
, struct file
*filp
);
63 static void cyberjack_close (struct usb_serial_port
*port
, struct file
*filp
);
64 static int cyberjack_write (struct usb_serial_port
*port
, const unsigned char *buf
, int count
);
65 static int cyberjack_write_room( struct usb_serial_port
*port
);
66 static void cyberjack_read_int_callback (struct urb
*urb
);
67 static void cyberjack_read_bulk_callback (struct urb
*urb
);
68 static void cyberjack_write_bulk_callback (struct urb
*urb
);
70 static struct usb_device_id id_table
[] = {
71 { USB_DEVICE(CYBERJACK_VENDOR_ID
, CYBERJACK_PRODUCT_ID
) },
72 { } /* Terminating entry */
75 MODULE_DEVICE_TABLE (usb
, id_table
);
77 static struct usb_driver cyberjack_driver
= {
79 .probe
= usb_serial_probe
,
80 .disconnect
= usb_serial_disconnect
,
85 static struct usb_serial_driver cyberjack_device
= {
90 .description
= "Reiner SCT Cyberjack USB card reader",
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
;
121 dbg("%s", __FUNCTION__
);
123 /* allocate the private data structure */
124 priv
= kmalloc(sizeof(struct cyberjack_private
), GFP_KERNEL
);
128 /* set initial values */
129 spin_lock_init(&priv
->lock
);
133 usb_set_serial_port_data(serial
->port
[0], priv
);
135 init_waitqueue_head(&serial
->port
[0]->write_wait
);
137 for (i
= 0; i
< serial
->num_ports
; ++i
) {
139 serial
->port
[i
]->interrupt_in_urb
->dev
= serial
->dev
;
140 result
= usb_submit_urb(serial
->port
[i
]->interrupt_in_urb
,
143 err(" usb_submit_urb(read int) failed");
144 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__
);
150 static void cyberjack_shutdown (struct usb_serial
*serial
)
154 dbg("%s", __FUNCTION__
);
156 for (i
=0; i
< serial
->num_ports
; ++i
) {
157 usb_kill_urb(serial
->port
[i
]->interrupt_in_urb
);
158 /* My special items, the standard routines free my urbs */
159 kfree(usb_get_serial_port_data(serial
->port
[i
]));
160 usb_set_serial_port_data(serial
->port
[i
], NULL
);
164 static int cyberjack_open (struct usb_serial_port
*port
, struct file
*filp
)
166 struct cyberjack_private
*priv
;
170 dbg("%s - port %d", __FUNCTION__
, port
->number
);
172 dbg("%s - usb_clear_halt", __FUNCTION__
);
173 usb_clear_halt(port
->serial
->dev
, port
->write_urb
->pipe
);
175 /* force low_latency on so that our tty_push actually forces
176 * the data through, otherwise it is scheduled, and with high
177 * data rates (like with OHCI) data can get lost.
179 port
->tty
->low_latency
= 1;
181 priv
= usb_get_serial_port_data(port
);
182 spin_lock_irqsave(&priv
->lock
, flags
);
186 spin_unlock_irqrestore(&priv
->lock
, flags
);
191 static void cyberjack_close (struct usb_serial_port
*port
, struct file
*filp
)
193 dbg("%s - port %d", __FUNCTION__
, port
->number
);
195 if (port
->serial
->dev
) {
196 /* shutdown any bulk reads that might be going on */
197 usb_kill_urb(port
->write_urb
);
198 usb_kill_urb(port
->read_urb
);
202 static int cyberjack_write (struct usb_serial_port
*port
, const unsigned char *buf
, int count
)
204 struct usb_serial
*serial
= port
->serial
;
205 struct cyberjack_private
*priv
= usb_get_serial_port_data(port
);
210 dbg("%s - port %d", __FUNCTION__
, port
->number
);
213 dbg("%s - write request of 0 bytes", __FUNCTION__
);
217 spin_lock_bh(&port
->lock
);
218 if (port
->write_urb_busy
) {
219 spin_unlock_bh(&port
->lock
);
220 dbg("%s - already writing", __FUNCTION__
);
223 port
->write_urb_busy
= 1;
224 spin_unlock_bh(&port
->lock
);
226 spin_lock_irqsave(&priv
->lock
, flags
);
228 if( (count
+priv
->wrfilled
)>sizeof(priv
->wrbuf
) ) {
229 /* To much data for buffer. Reset buffer. */
231 spin_unlock_irqrestore(&priv
->lock
, flags
);
232 port
->write_urb_busy
= 0;
237 memcpy (priv
->wrbuf
+priv
->wrfilled
, buf
, count
);
239 usb_serial_debug_data(debug
, &port
->dev
, __FUNCTION__
, count
,
240 priv
->wrbuf
+priv
->wrfilled
);
241 priv
->wrfilled
+= count
;
243 if( priv
->wrfilled
>= 3 ) {
244 wrexpected
= ((int)priv
->wrbuf
[2]<<8)+priv
->wrbuf
[1]+3;
245 dbg("%s - expected data: %d", __FUNCTION__
, wrexpected
);
247 wrexpected
= sizeof(priv
->wrbuf
);
250 if( priv
->wrfilled
>= wrexpected
) {
251 /* We have enough data to begin transmission */
254 dbg("%s - transmitting data (frame 1)", __FUNCTION__
);
255 length
= (wrexpected
> port
->bulk_out_size
) ? port
->bulk_out_size
: wrexpected
;
257 memcpy (port
->write_urb
->transfer_buffer
, priv
->wrbuf
, length
);
261 usb_fill_bulk_urb(port
->write_urb
, serial
->dev
,
262 usb_sndbulkpipe(serial
->dev
, port
->bulk_out_endpointAddress
),
263 port
->write_urb
->transfer_buffer
, length
,
264 ((serial
->type
->write_bulk_callback
) ?
265 serial
->type
->write_bulk_callback
:
266 cyberjack_write_bulk_callback
),
269 /* send the data out the bulk port */
270 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
272 err("%s - failed submitting write urb, error %d", __FUNCTION__
, result
);
273 /* Throw away data. No better idea what to do with it. */
276 spin_unlock_irqrestore(&priv
->lock
, flags
);
277 port
->write_urb_busy
= 0;
281 dbg("%s - priv->wrsent=%d", __FUNCTION__
,priv
->wrsent
);
282 dbg("%s - priv->wrfilled=%d", __FUNCTION__
,priv
->wrfilled
);
284 if( priv
->wrsent
>=priv
->wrfilled
) {
285 dbg("%s - buffer cleaned", __FUNCTION__
);
286 memset( priv
->wrbuf
, 0, sizeof(priv
->wrbuf
) );
292 spin_unlock_irqrestore(&priv
->lock
, flags
);
297 static int cyberjack_write_room( struct usb_serial_port
*port
)
299 return CYBERJACK_LOCAL_BUF_SIZE
;
302 static void cyberjack_read_int_callback( struct urb
*urb
)
304 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
305 struct cyberjack_private
*priv
= usb_get_serial_port_data(port
);
306 unsigned char *data
= urb
->transfer_buffer
;
309 dbg("%s - port %d", __FUNCTION__
, port
->number
);
311 /* the urb might have been killed. */
315 usb_serial_debug_data(debug
, &port
->dev
, __FUNCTION__
, urb
->actual_length
, data
);
317 /* React only to interrupts signaling a bulk_in transfer */
318 if( (urb
->actual_length
==4) && (data
[0]==0x01) ) {
322 /* This is a announcement of coming bulk_ins. */
323 unsigned short size
= ((unsigned short)data
[3]<<8)+data
[2]+3;
325 spin_lock(&priv
->lock
);
327 old_rdtodo
= priv
->rdtodo
;
329 if( (old_rdtodo
+size
)<(old_rdtodo
) ) {
330 dbg( "To many bulk_in urbs to do." );
331 spin_unlock(&priv
->lock
);
335 /* "+=" is probably more fault tollerant than "=" */
336 priv
->rdtodo
+= size
;
338 dbg("%s - rdtodo: %d", __FUNCTION__
, priv
->rdtodo
);
340 spin_unlock(&priv
->lock
);
343 port
->read_urb
->dev
= port
->serial
->dev
;
344 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
346 err("%s - failed resubmitting read urb, error %d", __FUNCTION__
, result
);
347 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__
);
352 port
->interrupt_in_urb
->dev
= port
->serial
->dev
;
353 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_ATOMIC
);
355 err(" usb_submit_urb(read int) failed");
356 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__
);
359 static void cyberjack_read_bulk_callback (struct urb
*urb
)
361 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
362 struct cyberjack_private
*priv
= usb_get_serial_port_data(port
);
363 struct tty_struct
*tty
;
364 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 tty_buffer_request_room(tty
, urb
->actual_length
);
383 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
384 tty_flip_buffer_push(tty
);
387 spin_lock(&priv
->lock
);
389 /* Reduce urbs to do by one. */
390 priv
->rdtodo
-=urb
->actual_length
;
391 /* Just to be sure */
392 if ( priv
->rdtodo
<0 ) priv
->rdtodo
= 0;
395 spin_unlock(&priv
->lock
);
397 dbg("%s - rdtodo: %d", __FUNCTION__
, todo
);
399 /* Continue to read if we have still urbs to do. */
400 if( todo
/* || (urb->actual_length==port->bulk_in_endpointAddress)*/ ) {
401 port
->read_urb
->dev
= port
->serial
->dev
;
402 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
404 err("%s - failed resubmitting read urb, error %d", __FUNCTION__
, result
);
405 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__
);
409 static void cyberjack_write_bulk_callback (struct urb
*urb
)
411 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
412 struct cyberjack_private
*priv
= usb_get_serial_port_data(port
);
414 dbg("%s - port %d", __FUNCTION__
, port
->number
);
416 port
->write_urb_busy
= 0;
418 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__
, urb
->status
);
422 spin_lock(&priv
->lock
);
424 /* only do something if we have more data to send */
425 if( priv
->wrfilled
) {
426 int length
, blksize
, result
;
428 dbg("%s - transmitting data (frame n)", __FUNCTION__
);
430 length
= ((priv
->wrfilled
- priv
->wrsent
) > port
->bulk_out_size
) ?
431 port
->bulk_out_size
: (priv
->wrfilled
- priv
->wrsent
);
433 memcpy (port
->write_urb
->transfer_buffer
, priv
->wrbuf
+ priv
->wrsent
,
435 priv
->wrsent
+=length
;
438 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
439 usb_sndbulkpipe(port
->serial
->dev
, port
->bulk_out_endpointAddress
),
440 port
->write_urb
->transfer_buffer
, length
,
441 ((port
->serial
->type
->write_bulk_callback
) ?
442 port
->serial
->type
->write_bulk_callback
:
443 cyberjack_write_bulk_callback
),
446 /* send the data out the bulk port */
447 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
449 err("%s - failed submitting write urb, error %d", __FUNCTION__
, result
);
450 /* Throw away data. No better idea what to do with it. */
456 dbg("%s - priv->wrsent=%d", __FUNCTION__
,priv
->wrsent
);
457 dbg("%s - priv->wrfilled=%d", __FUNCTION__
,priv
->wrfilled
);
459 blksize
= ((int)priv
->wrbuf
[2]<<8)+priv
->wrbuf
[1]+3;
461 if( (priv
->wrsent
>=priv
->wrfilled
) || (priv
->wrsent
>=blksize
) ) {
462 dbg("%s - buffer cleaned", __FUNCTION__
);
463 memset( priv
->wrbuf
, 0, sizeof(priv
->wrbuf
) );
470 spin_unlock(&priv
->lock
);
471 usb_serial_port_softint(port
);
474 static int __init
cyberjack_init (void)
477 retval
= usb_serial_register(&cyberjack_device
);
479 goto failed_usb_serial_register
;
480 retval
= usb_register(&cyberjack_driver
);
482 goto failed_usb_register
;
484 info(DRIVER_VERSION
" " DRIVER_AUTHOR
);
489 usb_serial_deregister(&cyberjack_device
);
490 failed_usb_serial_register
:
494 static void __exit
cyberjack_exit (void)
496 usb_deregister (&cyberjack_driver
);
497 usb_serial_deregister (&cyberjack_device
);
500 module_init(cyberjack_init
);
501 module_exit(cyberjack_exit
);
503 MODULE_AUTHOR( DRIVER_AUTHOR
);
504 MODULE_DESCRIPTION( DRIVER_DESC
);
505 MODULE_VERSION( DRIVER_VERSION
);
506 MODULE_LICENSE("GPL");
508 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
509 MODULE_PARM_DESC(debug
, "Debug enabled or not");