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 void cyberjack_disconnect(struct usb_serial
*serial
);
61 static int cyberjack_port_probe(struct usb_serial_port
*port
);
62 static int cyberjack_port_remove(struct usb_serial_port
*port
);
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_serial_driver cyberjack_device
= {
85 .description
= "Reiner SCT Cyberjack USB card reader",
88 .disconnect
= cyberjack_disconnect
,
89 .port_probe
= cyberjack_port_probe
,
90 .port_remove
= cyberjack_port_remove
,
91 .open
= cyberjack_open
,
92 .close
= cyberjack_close
,
93 .write
= cyberjack_write
,
94 .write_room
= cyberjack_write_room
,
95 .read_int_callback
= cyberjack_read_int_callback
,
96 .read_bulk_callback
= cyberjack_read_bulk_callback
,
97 .write_bulk_callback
= cyberjack_write_bulk_callback
,
100 static struct usb_serial_driver
* const serial_drivers
[] = {
101 &cyberjack_device
, NULL
104 struct cyberjack_private
{
105 spinlock_t lock
; /* Lock for SMP */
106 short rdtodo
; /* Bytes still to read */
107 unsigned char wrbuf
[5*64]; /* Buffer for collecting data to write */
108 short wrfilled
; /* Overall data size we already got */
109 short wrsent
; /* Data already sent */
112 static int cyberjack_port_probe(struct usb_serial_port
*port
)
114 struct cyberjack_private
*priv
;
117 priv
= kmalloc(sizeof(struct cyberjack_private
), GFP_KERNEL
);
121 spin_lock_init(&priv
->lock
);
126 usb_set_serial_port_data(port
, priv
);
128 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
130 dev_err(&port
->dev
, "usb_submit_urb(read int) failed\n");
135 static int cyberjack_port_remove(struct usb_serial_port
*port
)
137 struct cyberjack_private
*priv
;
139 priv
= usb_get_serial_port_data(port
);
145 static void cyberjack_disconnect(struct usb_serial
*serial
)
149 for (i
= 0; i
< serial
->num_ports
; ++i
)
150 usb_kill_urb(serial
->port
[i
]->interrupt_in_urb
);
153 static int cyberjack_open(struct tty_struct
*tty
,
154 struct usb_serial_port
*port
)
156 struct cyberjack_private
*priv
;
160 dbg("%s - usb_clear_halt", __func__
);
161 usb_clear_halt(port
->serial
->dev
, port
->write_urb
->pipe
);
163 priv
= usb_get_serial_port_data(port
);
164 spin_lock_irqsave(&priv
->lock
, flags
);
168 spin_unlock_irqrestore(&priv
->lock
, flags
);
173 static void cyberjack_close(struct usb_serial_port
*port
)
175 if (port
->serial
->dev
) {
176 /* shutdown any bulk reads that might be going on */
177 usb_kill_urb(port
->write_urb
);
178 usb_kill_urb(port
->read_urb
);
182 static int cyberjack_write(struct tty_struct
*tty
,
183 struct usb_serial_port
*port
, const unsigned char *buf
, int count
)
185 struct cyberjack_private
*priv
= usb_get_serial_port_data(port
);
191 dbg("%s - write request of 0 bytes", __func__
);
195 if (!test_and_clear_bit(0, &port
->write_urbs_free
)) {
196 dbg("%s - already writing", __func__
);
200 spin_lock_irqsave(&priv
->lock
, flags
);
202 if (count
+priv
->wrfilled
> sizeof(priv
->wrbuf
)) {
203 /* To much data for buffer. Reset buffer. */
205 spin_unlock_irqrestore(&priv
->lock
, flags
);
206 set_bit(0, &port
->write_urbs_free
);
211 memcpy(priv
->wrbuf
+ priv
->wrfilled
, buf
, count
);
213 usb_serial_debug_data(debug
, &port
->dev
, __func__
, count
,
214 priv
->wrbuf
+ priv
->wrfilled
);
215 priv
->wrfilled
+= count
;
217 if (priv
->wrfilled
>= 3) {
218 wrexpected
= ((int)priv
->wrbuf
[2]<<8)+priv
->wrbuf
[1]+3;
219 dbg("%s - expected data: %d", __func__
, wrexpected
);
221 wrexpected
= sizeof(priv
->wrbuf
);
223 if (priv
->wrfilled
>= wrexpected
) {
224 /* We have enough data to begin transmission */
227 dbg("%s - transmitting data (frame 1)", __func__
);
228 length
= (wrexpected
> port
->bulk_out_size
) ?
229 port
->bulk_out_size
: wrexpected
;
231 memcpy(port
->write_urb
->transfer_buffer
, priv
->wrbuf
, length
);
232 priv
->wrsent
= length
;
235 port
->write_urb
->transfer_buffer_length
= length
;
237 /* send the data out the bulk port */
238 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
241 "%s - failed submitting write urb, error %d",
243 /* Throw away data. No better idea what to do with it. */
246 spin_unlock_irqrestore(&priv
->lock
, flags
);
247 set_bit(0, &port
->write_urbs_free
);
251 dbg("%s - priv->wrsent=%d", __func__
, priv
->wrsent
);
252 dbg("%s - priv->wrfilled=%d", __func__
, priv
->wrfilled
);
254 if (priv
->wrsent
>= priv
->wrfilled
) {
255 dbg("%s - buffer cleaned", __func__
);
256 memset(priv
->wrbuf
, 0, sizeof(priv
->wrbuf
));
262 spin_unlock_irqrestore(&priv
->lock
, flags
);
267 static int cyberjack_write_room(struct tty_struct
*tty
)
270 return CYBERJACK_LOCAL_BUF_SIZE
;
273 static void cyberjack_read_int_callback(struct urb
*urb
)
275 struct usb_serial_port
*port
= urb
->context
;
276 struct cyberjack_private
*priv
= usb_get_serial_port_data(port
);
277 unsigned char *data
= urb
->transfer_buffer
;
278 int status
= urb
->status
;
281 /* the urb might have been killed. */
285 usb_serial_debug_data(debug
, &port
->dev
, __func__
,
286 urb
->actual_length
, data
);
288 /* React only to interrupts signaling a bulk_in transfer */
289 if (urb
->actual_length
== 4 && data
[0] == 0x01) {
292 /* This is a announcement of coming bulk_ins. */
293 unsigned short size
= ((unsigned short)data
[3]<<8)+data
[2]+3;
295 spin_lock(&priv
->lock
);
297 old_rdtodo
= priv
->rdtodo
;
299 if (old_rdtodo
+ size
< old_rdtodo
) {
300 dbg("To many bulk_in urbs to do.");
301 spin_unlock(&priv
->lock
);
305 /* "+=" is probably more fault tollerant than "=" */
306 priv
->rdtodo
+= size
;
308 dbg("%s - rdtodo: %d", __func__
, priv
->rdtodo
);
310 spin_unlock(&priv
->lock
);
313 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
315 dev_err(&port
->dev
, "%s - failed resubmitting "
316 "read urb, error %d\n",
318 dbg("%s - usb_submit_urb(read urb)", __func__
);
323 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_ATOMIC
);
325 dev_err(&port
->dev
, "usb_submit_urb(read int) failed\n");
326 dbg("%s - usb_submit_urb(int urb)", __func__
);
329 static void cyberjack_read_bulk_callback(struct urb
*urb
)
331 struct usb_serial_port
*port
= urb
->context
;
332 struct cyberjack_private
*priv
= usb_get_serial_port_data(port
);
333 struct tty_struct
*tty
;
334 unsigned char *data
= urb
->transfer_buffer
;
337 int status
= urb
->status
;
339 usb_serial_debug_data(debug
, &port
->dev
, __func__
,
340 urb
->actual_length
, data
);
342 dbg("%s - nonzero read bulk status received: %d",
347 tty
= tty_port_tty_get(&port
->port
);
349 dbg("%s - ignoring since device not open", __func__
);
352 if (urb
->actual_length
) {
353 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
354 tty_flip_buffer_push(tty
);
358 spin_lock(&priv
->lock
);
360 /* Reduce urbs to do by one. */
361 priv
->rdtodo
-= urb
->actual_length
;
362 /* Just to be sure */
363 if (priv
->rdtodo
< 0)
367 spin_unlock(&priv
->lock
);
369 dbg("%s - rdtodo: %d", __func__
, todo
);
371 /* Continue to read if we have still urbs to do. */
372 if (todo
/* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
373 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
375 dev_err(&port
->dev
, "%s - failed resubmitting read "
376 "urb, error %d\n", __func__
, result
);
377 dbg("%s - usb_submit_urb(read urb)", __func__
);
381 static void cyberjack_write_bulk_callback(struct urb
*urb
)
383 struct usb_serial_port
*port
= urb
->context
;
384 struct cyberjack_private
*priv
= usb_get_serial_port_data(port
);
385 int status
= urb
->status
;
387 set_bit(0, &port
->write_urbs_free
);
389 dbg("%s - nonzero write bulk status received: %d",
394 spin_lock(&priv
->lock
);
396 /* only do something if we have more data to send */
397 if (priv
->wrfilled
) {
398 int length
, blksize
, result
;
400 dbg("%s - transmitting data (frame n)", __func__
);
402 length
= ((priv
->wrfilled
- priv
->wrsent
) > port
->bulk_out_size
) ?
403 port
->bulk_out_size
: (priv
->wrfilled
- priv
->wrsent
);
405 memcpy(port
->write_urb
->transfer_buffer
,
406 priv
->wrbuf
+ priv
->wrsent
, length
);
407 priv
->wrsent
+= length
;
410 port
->write_urb
->transfer_buffer_length
= length
;
412 /* send the data out the bulk port */
413 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
416 "%s - failed submitting write urb, error %d\n",
418 /* Throw away data. No better idea what to do with it. */
424 dbg("%s - priv->wrsent=%d", __func__
, priv
->wrsent
);
425 dbg("%s - priv->wrfilled=%d", __func__
, priv
->wrfilled
);
427 blksize
= ((int)priv
->wrbuf
[2]<<8)+priv
->wrbuf
[1]+3;
429 if (priv
->wrsent
>= priv
->wrfilled
||
430 priv
->wrsent
>= blksize
) {
431 dbg("%s - buffer cleaned", __func__
);
432 memset(priv
->wrbuf
, 0, sizeof(priv
->wrbuf
));
439 spin_unlock(&priv
->lock
);
440 usb_serial_port_softint(port
);
443 module_usb_serial_driver(serial_drivers
, id_table
);
445 MODULE_AUTHOR(DRIVER_AUTHOR
);
446 MODULE_DESCRIPTION(DRIVER_DESC
);
447 MODULE_VERSION(DRIVER_VERSION
);
448 MODULE_LICENSE("GPL");
450 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
451 MODULE_PARM_DESC(debug
, "Debug enabled or not");