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
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_disconnect(struct usb_serial
*serial
);
62 static void cyberjack_release(struct usb_serial
*serial
);
63 static int cyberjack_open(struct tty_struct
*tty
,
64 struct usb_serial_port
*port
);
65 static void cyberjack_close(struct usb_serial_port
*port
);
66 static int cyberjack_write(struct tty_struct
*tty
,
67 struct usb_serial_port
*port
, const unsigned char *buf
, int count
);
68 static int cyberjack_write_room(struct tty_struct
*tty
);
69 static void cyberjack_read_int_callback(struct urb
*urb
);
70 static void cyberjack_read_bulk_callback(struct urb
*urb
);
71 static void cyberjack_write_bulk_callback(struct urb
*urb
);
73 static const struct usb_device_id id_table
[] = {
74 { USB_DEVICE(CYBERJACK_VENDOR_ID
, CYBERJACK_PRODUCT_ID
) },
75 { } /* Terminating entry */
78 MODULE_DEVICE_TABLE(usb
, id_table
);
80 static struct usb_driver cyberjack_driver
= {
82 .probe
= usb_serial_probe
,
83 .disconnect
= usb_serial_disconnect
,
88 static struct usb_serial_driver cyberjack_device
= {
93 .description
= "Reiner SCT Cyberjack USB card reader",
94 .usb_driver
= &cyberjack_driver
,
97 .attach
= cyberjack_startup
,
98 .disconnect
= cyberjack_disconnect
,
99 .release
= cyberjack_release
,
100 .open
= cyberjack_open
,
101 .close
= cyberjack_close
,
102 .write
= cyberjack_write
,
103 .write_room
= cyberjack_write_room
,
104 .read_int_callback
= cyberjack_read_int_callback
,
105 .read_bulk_callback
= cyberjack_read_bulk_callback
,
106 .write_bulk_callback
= cyberjack_write_bulk_callback
,
109 struct cyberjack_private
{
110 spinlock_t lock
; /* Lock for SMP */
111 short rdtodo
; /* Bytes still to read */
112 unsigned char wrbuf
[5*64]; /* Buffer for collecting data to write */
113 short wrfilled
; /* Overall data size we already got */
114 short wrsent
; /* Data already sent */
117 /* do some startup allocations not currently performed by usb_serial_probe() */
118 static int cyberjack_startup(struct usb_serial
*serial
)
120 struct cyberjack_private
*priv
;
125 /* allocate the private data structure */
126 priv
= kmalloc(sizeof(struct cyberjack_private
), GFP_KERNEL
);
130 /* set initial values */
131 spin_lock_init(&priv
->lock
);
135 usb_set_serial_port_data(serial
->port
[0], priv
);
137 init_waitqueue_head(&serial
->port
[0]->write_wait
);
139 for (i
= 0; i
< serial
->num_ports
; ++i
) {
141 result
= usb_submit_urb(serial
->port
[i
]->interrupt_in_urb
,
144 dev_err(&serial
->dev
->dev
,
145 "usb_submit_urb(read int) failed\n");
146 dbg("%s - usb_submit_urb(int urb)", __func__
);
152 static void cyberjack_disconnect(struct usb_serial
*serial
)
158 for (i
= 0; i
< serial
->num_ports
; ++i
)
159 usb_kill_urb(serial
->port
[i
]->interrupt_in_urb
);
162 static void cyberjack_release(struct usb_serial
*serial
)
168 for (i
= 0; i
< serial
->num_ports
; ++i
) {
169 /* My special items, the standard routines free my urbs */
170 kfree(usb_get_serial_port_data(serial
->port
[i
]));
174 static int cyberjack_open(struct tty_struct
*tty
,
175 struct usb_serial_port
*port
)
177 struct cyberjack_private
*priv
;
181 dbg("%s - port %d", __func__
, port
->number
);
183 dbg("%s - usb_clear_halt", __func__
);
184 usb_clear_halt(port
->serial
->dev
, port
->write_urb
->pipe
);
186 priv
= usb_get_serial_port_data(port
);
187 spin_lock_irqsave(&priv
->lock
, flags
);
191 spin_unlock_irqrestore(&priv
->lock
, flags
);
196 static void cyberjack_close(struct usb_serial_port
*port
)
198 dbg("%s - port %d", __func__
, port
->number
);
200 if (port
->serial
->dev
) {
201 /* shutdown any bulk reads that might be going on */
202 usb_kill_urb(port
->write_urb
);
203 usb_kill_urb(port
->read_urb
);
207 static int cyberjack_write(struct tty_struct
*tty
,
208 struct usb_serial_port
*port
, const unsigned char *buf
, int count
)
210 struct cyberjack_private
*priv
= usb_get_serial_port_data(port
);
215 dbg("%s - port %d", __func__
, port
->number
);
218 dbg("%s - write request of 0 bytes", __func__
);
222 if (!test_and_clear_bit(0, &port
->write_urbs_free
)) {
223 dbg("%s - already writing", __func__
);
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
);
233 set_bit(0, &port
->write_urbs_free
);
238 memcpy(priv
->wrbuf
+ priv
->wrfilled
, buf
, count
);
240 usb_serial_debug_data(debug
, &port
->dev
, __func__
, count
,
241 priv
->wrbuf
+ priv
->wrfilled
);
242 priv
->wrfilled
+= count
;
244 if (priv
->wrfilled
>= 3) {
245 wrexpected
= ((int)priv
->wrbuf
[2]<<8)+priv
->wrbuf
[1]+3;
246 dbg("%s - expected data: %d", __func__
, wrexpected
);
248 wrexpected
= sizeof(priv
->wrbuf
);
250 if (priv
->wrfilled
>= wrexpected
) {
251 /* We have enough data to begin transmission */
254 dbg("%s - transmitting data (frame 1)", __func__
);
255 length
= (wrexpected
> port
->bulk_out_size
) ?
256 port
->bulk_out_size
: wrexpected
;
258 memcpy(port
->write_urb
->transfer_buffer
, priv
->wrbuf
, length
);
259 priv
->wrsent
= length
;
262 port
->write_urb
->transfer_buffer_length
= length
;
264 /* send the data out the bulk port */
265 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
268 "%s - failed submitting write urb, error %d",
270 /* Throw away data. No better idea what to do with it. */
273 spin_unlock_irqrestore(&priv
->lock
, flags
);
274 set_bit(0, &port
->write_urbs_free
);
278 dbg("%s - priv->wrsent=%d", __func__
, priv
->wrsent
);
279 dbg("%s - priv->wrfilled=%d", __func__
, priv
->wrfilled
);
281 if (priv
->wrsent
>= priv
->wrfilled
) {
282 dbg("%s - buffer cleaned", __func__
);
283 memset(priv
->wrbuf
, 0, sizeof(priv
->wrbuf
));
289 spin_unlock_irqrestore(&priv
->lock
, flags
);
294 static int cyberjack_write_room(struct tty_struct
*tty
)
297 return CYBERJACK_LOCAL_BUF_SIZE
;
300 static void cyberjack_read_int_callback(struct urb
*urb
)
302 struct usb_serial_port
*port
= urb
->context
;
303 struct cyberjack_private
*priv
= usb_get_serial_port_data(port
);
304 unsigned char *data
= urb
->transfer_buffer
;
305 int status
= urb
->status
;
308 dbg("%s - port %d", __func__
, port
->number
);
310 /* the urb might have been killed. */
314 usb_serial_debug_data(debug
, &port
->dev
, __func__
,
315 urb
->actual_length
, data
);
317 /* React only to interrupts signaling a bulk_in transfer */
318 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", __func__
, priv
->rdtodo
);
339 spin_unlock(&priv
->lock
);
342 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
344 dev_err(&port
->dev
, "%s - failed resubmitting "
345 "read urb, error %d\n",
347 dbg("%s - usb_submit_urb(read urb)", __func__
);
352 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_ATOMIC
);
354 dev_err(&port
->dev
, "usb_submit_urb(read int) failed\n");
355 dbg("%s - usb_submit_urb(int urb)", __func__
);
358 static void cyberjack_read_bulk_callback(struct urb
*urb
)
360 struct usb_serial_port
*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
;
366 int status
= urb
->status
;
368 dbg("%s - port %d", __func__
, port
->number
);
370 usb_serial_debug_data(debug
, &port
->dev
, __func__
,
371 urb
->actual_length
, data
);
373 dbg("%s - nonzero read bulk status received: %d",
378 tty
= tty_port_tty_get(&port
->port
);
380 dbg("%s - ignoring since device not open", __func__
);
383 if (urb
->actual_length
) {
384 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
385 tty_flip_buffer_push(tty
);
389 spin_lock(&priv
->lock
);
391 /* Reduce urbs to do by one. */
392 priv
->rdtodo
-= urb
->actual_length
;
393 /* Just to be sure */
394 if (priv
->rdtodo
< 0)
398 spin_unlock(&priv
->lock
);
400 dbg("%s - rdtodo: %d", __func__
, todo
);
402 /* Continue to read if we have still urbs to do. */
403 if (todo
/* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
404 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
406 dev_err(&port
->dev
, "%s - failed resubmitting read "
407 "urb, error %d\n", __func__
, result
);
408 dbg("%s - usb_submit_urb(read urb)", __func__
);
412 static void cyberjack_write_bulk_callback(struct urb
*urb
)
414 struct usb_serial_port
*port
= urb
->context
;
415 struct cyberjack_private
*priv
= usb_get_serial_port_data(port
);
416 int status
= urb
->status
;
418 dbg("%s - port %d", __func__
, port
->number
);
420 set_bit(0, &port
->write_urbs_free
);
422 dbg("%s - nonzero write bulk status received: %d",
427 spin_lock(&priv
->lock
);
429 /* only do something if we have more data to send */
430 if (priv
->wrfilled
) {
431 int length
, blksize
, result
;
433 dbg("%s - transmitting data (frame n)", __func__
);
435 length
= ((priv
->wrfilled
- priv
->wrsent
) > port
->bulk_out_size
) ?
436 port
->bulk_out_size
: (priv
->wrfilled
- priv
->wrsent
);
438 memcpy(port
->write_urb
->transfer_buffer
,
439 priv
->wrbuf
+ priv
->wrsent
, length
);
440 priv
->wrsent
+= length
;
443 port
->write_urb
->transfer_buffer_length
= length
;
445 /* send the data out the bulk port */
446 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
449 "%s - failed submitting write urb, error %d\n",
451 /* Throw away data. No better idea what to do with it. */
457 dbg("%s - priv->wrsent=%d", __func__
, priv
->wrsent
);
458 dbg("%s - priv->wrfilled=%d", __func__
, priv
->wrfilled
);
460 blksize
= ((int)priv
->wrbuf
[2]<<8)+priv
->wrbuf
[1]+3;
462 if (priv
->wrsent
>= priv
->wrfilled
||
463 priv
->wrsent
>= blksize
) {
464 dbg("%s - buffer cleaned", __func__
);
465 memset(priv
->wrbuf
, 0, sizeof(priv
->wrbuf
));
472 spin_unlock(&priv
->lock
);
473 usb_serial_port_softint(port
);
476 static int __init
cyberjack_init(void)
479 retval
= usb_serial_register(&cyberjack_device
);
481 goto failed_usb_serial_register
;
482 retval
= usb_register(&cyberjack_driver
);
484 goto failed_usb_register
;
486 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
" "
488 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_DESC
"\n");
492 usb_serial_deregister(&cyberjack_device
);
493 failed_usb_serial_register
:
497 static void __exit
cyberjack_exit(void)
499 usb_deregister(&cyberjack_driver
);
500 usb_serial_deregister(&cyberjack_device
);
503 module_init(cyberjack_init
);
504 module_exit(cyberjack_exit
);
506 MODULE_AUTHOR(DRIVER_AUTHOR
);
507 MODULE_DESCRIPTION(DRIVER_DESC
);
508 MODULE_VERSION(DRIVER_VERSION
);
509 MODULE_LICENSE("GPL");
511 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
512 MODULE_PARM_DESC(debug
, "Debug enabled or not");