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_shutdown(struct usb_serial *serial);
62 static int cyberjack_open(struct tty_struct *tty,
63 struct usb_serial_port *port, struct file *filp);
64 static void cyberjack_close(struct usb_serial_port *port);
65 static int cyberjack_write(struct tty_struct *tty,
66 struct usb_serial_port *port, const unsigned char *buf, int count);
67 static int cyberjack_write_room(struct tty_struct *tty);
68 static void cyberjack_read_int_callback(struct urb *urb);
69 static void cyberjack_read_bulk_callback(struct urb *urb);
70 static void cyberjack_write_bulk_callback(struct urb *urb);
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 = {
81 .probe = usb_serial_probe,
82 .disconnect = usb_serial_disconnect,
87 static struct usb_serial_driver cyberjack_device = {
92 .description = "Reiner SCT Cyberjack USB card reader",
93 .usb_driver = &cyberjack_driver,
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;
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 dev_err(&serial->dev->dev,
144 "usb_submit_urb(read int) failed\n");
145 dbg("%s - usb_submit_urb(int urb)", __func__);
151 static void cyberjack_shutdown(struct usb_serial *serial)
157 for (i = 0; i < serial->num_ports; ++i) {
158 usb_kill_urb(serial->port[i]->interrupt_in_urb);
159 /* My special items, the standard routines free my urbs */
160 kfree(usb_get_serial_port_data(serial->port[i]));
161 usb_set_serial_port_data(serial->port[i], NULL);
165 static int cyberjack_open(struct tty_struct *tty,
166 struct usb_serial_port *port, struct file *filp)
168 struct cyberjack_private *priv;
172 dbg("%s - port %d", __func__, port->number);
174 dbg("%s - usb_clear_halt", __func__);
175 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
177 priv = usb_get_serial_port_data(port);
178 spin_lock_irqsave(&priv->lock, flags);
182 spin_unlock_irqrestore(&priv->lock, flags);
187 static void cyberjack_close(struct usb_serial_port *port)
189 dbg("%s - port %d", __func__, port->number);
191 if (port->serial->dev) {
192 /* shutdown any bulk reads that might be going on */
193 usb_kill_urb(port->write_urb);
194 usb_kill_urb(port->read_urb);
198 static int cyberjack_write(struct tty_struct *tty,
199 struct usb_serial_port *port, const unsigned char *buf, int count)
201 struct usb_serial *serial = port->serial;
202 struct cyberjack_private *priv = usb_get_serial_port_data(port);
207 dbg("%s - port %d", __func__, port->number);
210 dbg("%s - write request of 0 bytes", __func__);
214 spin_lock_bh(&port->lock);
215 if (port->write_urb_busy) {
216 spin_unlock_bh(&port->lock);
217 dbg("%s - already writing", __func__);
220 port->write_urb_busy = 1;
221 spin_unlock_bh(&port->lock);
223 spin_lock_irqsave(&priv->lock, flags);
225 if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
226 /* To much data for buffer. Reset buffer. */
228 port->write_urb_busy = 0;
229 spin_unlock_irqrestore(&priv->lock, flags);
234 memcpy(priv->wrbuf + priv->wrfilled, buf, count);
236 usb_serial_debug_data(debug, &port->dev, __func__, count,
237 priv->wrbuf + priv->wrfilled);
238 priv->wrfilled += count;
240 if (priv->wrfilled >= 3) {
241 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
242 dbg("%s - expected data: %d", __func__, wrexpected);
244 wrexpected = sizeof(priv->wrbuf);
246 if (priv->wrfilled >= wrexpected) {
247 /* We have enough data to begin transmission */
250 dbg("%s - transmitting data (frame 1)", __func__);
251 length = (wrexpected > port->bulk_out_size) ?
252 port->bulk_out_size : wrexpected;
254 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
255 priv->wrsent = length;
258 usb_fill_bulk_urb(port->write_urb, serial->dev,
259 usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
260 port->write_urb->transfer_buffer, length,
261 ((serial->type->write_bulk_callback) ?
262 serial->type->write_bulk_callback :
263 cyberjack_write_bulk_callback),
266 /* send the data out the bulk port */
267 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
270 "%s - failed submitting write urb, error %d",
272 /* Throw away data. No better idea what to do with it. */
275 spin_unlock_irqrestore(&priv->lock, flags);
276 port->write_urb_busy = 0;
280 dbg("%s - priv->wrsent=%d", __func__, priv->wrsent);
281 dbg("%s - priv->wrfilled=%d", __func__, priv->wrfilled);
283 if (priv->wrsent >= priv->wrfilled) {
284 dbg("%s - buffer cleaned", __func__);
285 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
291 spin_unlock_irqrestore(&priv->lock, flags);
296 static int cyberjack_write_room(struct tty_struct *tty)
299 return CYBERJACK_LOCAL_BUF_SIZE;
302 static void cyberjack_read_int_callback(struct urb *urb)
304 struct usb_serial_port *port = urb->context;
305 struct cyberjack_private *priv = usb_get_serial_port_data(port);
306 unsigned char *data = urb->transfer_buffer;
307 int status = urb->status;
310 dbg("%s - port %d", __func__, port->number);
312 /* the urb might have been killed. */
316 usb_serial_debug_data(debug, &port->dev, __func__,
317 urb->actual_length, data);
319 /* React only to interrupts signaling a bulk_in transfer */
320 if (urb->actual_length == 4 && data[0] == 0x01) {
323 /* This is a announcement of coming bulk_ins. */
324 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
326 spin_lock(&priv->lock);
328 old_rdtodo = priv->rdtodo;
330 if (old_rdtodo + size < old_rdtodo) {
331 dbg("To many bulk_in urbs to do.");
332 spin_unlock(&priv->lock);
336 /* "+=" is probably more fault tollerant than "=" */
337 priv->rdtodo += size;
339 dbg("%s - rdtodo: %d", __func__, priv->rdtodo);
341 spin_unlock(&priv->lock);
344 port->read_urb->dev = port->serial->dev;
345 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
347 dev_err(&port->dev, "%s - failed resubmitting "
348 "read urb, error %d\n",
350 dbg("%s - usb_submit_urb(read urb)", __func__);
355 port->interrupt_in_urb->dev = port->serial->dev;
356 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
358 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
359 dbg("%s - usb_submit_urb(int urb)", __func__);
362 static void cyberjack_read_bulk_callback(struct urb *urb)
364 struct usb_serial_port *port = urb->context;
365 struct cyberjack_private *priv = usb_get_serial_port_data(port);
366 struct tty_struct *tty;
367 unsigned char *data = urb->transfer_buffer;
370 int status = urb->status;
372 dbg("%s - port %d", __func__, port->number);
374 usb_serial_debug_data(debug, &port->dev, __func__,
375 urb->actual_length, data);
377 dbg("%s - nonzero read bulk status received: %d",
382 tty = tty_port_tty_get(&port->port);
384 dbg("%s - ignoring since device not open\n", __func__);
387 if (urb->actual_length) {
388 tty_buffer_request_room(tty, urb->actual_length);
389 tty_insert_flip_string(tty, data, urb->actual_length);
390 tty_flip_buffer_push(tty);
394 spin_lock(&priv->lock);
396 /* Reduce urbs to do by one. */
397 priv->rdtodo -= urb->actual_length;
398 /* Just to be sure */
399 if (priv->rdtodo < 0)
403 spin_unlock(&priv->lock);
405 dbg("%s - rdtodo: %d", __func__, todo);
407 /* Continue to read if we have still urbs to do. */
408 if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
409 port->read_urb->dev = port->serial->dev;
410 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
412 dev_err(&port->dev, "%s - failed resubmitting read "
413 "urb, error %d\n", __func__, result);
414 dbg("%s - usb_submit_urb(read urb)", __func__);
418 static void cyberjack_write_bulk_callback(struct urb *urb)
420 struct usb_serial_port *port = urb->context;
421 struct cyberjack_private *priv = usb_get_serial_port_data(port);
422 int status = urb->status;
424 dbg("%s - port %d", __func__, port->number);
426 port->write_urb_busy = 0;
428 dbg("%s - nonzero write bulk status received: %d",
433 spin_lock(&priv->lock);
435 /* only do something if we have more data to send */
436 if (priv->wrfilled) {
437 int length, blksize, result;
439 dbg("%s - transmitting data (frame n)", __func__);
441 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
442 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
444 memcpy(port->write_urb->transfer_buffer,
445 priv->wrbuf + priv->wrsent, length);
446 priv->wrsent += length;
449 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
450 usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
451 port->write_urb->transfer_buffer, length,
452 ((port->serial->type->write_bulk_callback) ?
453 port->serial->type->write_bulk_callback :
454 cyberjack_write_bulk_callback),
457 /* send the data out the bulk port */
458 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
461 "%s - failed submitting write urb, error %d\n",
463 /* Throw away data. No better idea what to do with it. */
469 dbg("%s - priv->wrsent=%d", __func__, priv->wrsent);
470 dbg("%s - priv->wrfilled=%d", __func__, priv->wrfilled);
472 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
474 if (priv->wrsent >= priv->wrfilled ||
475 priv->wrsent >= blksize) {
476 dbg("%s - buffer cleaned", __func__);
477 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
484 spin_unlock(&priv->lock);
485 usb_serial_port_softint(port);
488 static int __init cyberjack_init(void)
491 retval = usb_serial_register(&cyberjack_device);
493 goto failed_usb_serial_register;
494 retval = usb_register(&cyberjack_driver);
496 goto failed_usb_register;
498 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION " "
500 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
504 usb_serial_deregister(&cyberjack_device);
505 failed_usb_serial_register:
509 static void __exit cyberjack_exit(void)
511 usb_deregister(&cyberjack_driver);
512 usb_serial_deregister(&cyberjack_device);
515 module_init(cyberjack_init);
516 module_exit(cyberjack_exit);
518 MODULE_AUTHOR(DRIVER_AUTHOR);
519 MODULE_DESCRIPTION(DRIVER_DESC);
520 MODULE_VERSION(DRIVER_VERSION);
521 MODULE_LICENSE("GPL");
523 module_param(debug, bool, S_IRUGO | S_IWUSR);
524 MODULE_PARM_DESC(debug, "Debug enabled or not");