i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / usb / serial / cyberjack.c
blob6bc3802a581af7f43239869864146e69bc20c3e4
1 /*
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
19 * patience.
21 * In case of problems, please write to the contact e-mail address
22 * mentioned above.
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
46 static bool debug;
49 * Version Information
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 = {
81 .name = "cyberjack",
82 .probe = usb_serial_probe,
83 .disconnect = usb_serial_disconnect,
84 .id_table = id_table,
85 .no_dynamic_id = 1,
88 static struct usb_serial_driver cyberjack_device = {
89 .driver = {
90 .owner = THIS_MODULE,
91 .name = "cyberjack",
93 .description = "Reiner SCT Cyberjack USB card reader",
94 .usb_driver = &cyberjack_driver,
95 .id_table = id_table,
96 .num_ports = 1,
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;
121 int i;
123 dbg("%s", __func__);
125 /* allocate the private data structure */
126 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
127 if (!priv)
128 return -ENOMEM;
130 /* set initial values */
131 spin_lock_init(&priv->lock);
132 priv->rdtodo = 0;
133 priv->wrfilled = 0;
134 priv->wrsent = 0;
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) {
140 int result;
141 result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
142 GFP_KERNEL);
143 if (result)
144 dev_err(&serial->dev->dev,
145 "usb_submit_urb(read int) failed\n");
146 dbg("%s - usb_submit_urb(int urb)", __func__);
149 return 0;
152 static void cyberjack_disconnect(struct usb_serial *serial)
154 int i;
156 dbg("%s", __func__);
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)
164 int i;
166 dbg("%s", __func__);
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;
178 unsigned long flags;
179 int result = 0;
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);
188 priv->rdtodo = 0;
189 priv->wrfilled = 0;
190 priv->wrsent = 0;
191 spin_unlock_irqrestore(&priv->lock, flags);
193 return result;
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);
211 unsigned long flags;
212 int result;
213 int wrexpected;
215 dbg("%s - port %d", __func__, port->number);
217 if (count == 0) {
218 dbg("%s - write request of 0 bytes", __func__);
219 return 0;
222 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
223 dbg("%s - already writing", __func__);
224 return 0;
227 spin_lock_irqsave(&priv->lock, flags);
229 if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
230 /* To much data for buffer. Reset buffer. */
231 priv->wrfilled = 0;
232 spin_unlock_irqrestore(&priv->lock, flags);
233 set_bit(0, &port->write_urbs_free);
234 return 0;
237 /* Copy data */
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);
247 } else
248 wrexpected = sizeof(priv->wrbuf);
250 if (priv->wrfilled >= wrexpected) {
251 /* We have enough data to begin transmission */
252 int length;
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;
261 /* set up our urb */
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);
266 if (result) {
267 dev_err(&port->dev,
268 "%s - failed submitting write urb, error %d",
269 __func__, result);
270 /* Throw away data. No better idea what to do with it. */
271 priv->wrfilled = 0;
272 priv->wrsent = 0;
273 spin_unlock_irqrestore(&priv->lock, flags);
274 set_bit(0, &port->write_urbs_free);
275 return 0;
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));
284 priv->wrfilled = 0;
285 priv->wrsent = 0;
289 spin_unlock_irqrestore(&priv->lock, flags);
291 return count;
294 static int cyberjack_write_room(struct tty_struct *tty)
296 /* FIXME: .... */
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;
306 int result;
308 dbg("%s - port %d", __func__, port->number);
310 /* the urb might have been killed. */
311 if (status)
312 return;
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) {
319 short old_rdtodo;
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);
331 goto resubmit;
334 /* "+=" is probably more fault tollerant than "=" */
335 priv->rdtodo += size;
337 dbg("%s - rdtodo: %d", __func__, priv->rdtodo);
339 spin_unlock(&priv->lock);
341 if (!old_rdtodo) {
342 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
343 if (result)
344 dev_err(&port->dev, "%s - failed resubmitting "
345 "read urb, error %d\n",
346 __func__, result);
347 dbg("%s - usb_submit_urb(read urb)", __func__);
351 resubmit:
352 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
353 if (result)
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;
364 short todo;
365 int result;
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);
372 if (status) {
373 dbg("%s - nonzero read bulk status received: %d",
374 __func__, status);
375 return;
378 tty = tty_port_tty_get(&port->port);
379 if (!tty) {
380 dbg("%s - ignoring since device not open", __func__);
381 return;
383 if (urb->actual_length) {
384 tty_insert_flip_string(tty, data, urb->actual_length);
385 tty_flip_buffer_push(tty);
387 tty_kref_put(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)
395 priv->rdtodo = 0;
396 todo = priv->rdtodo;
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);
405 if (result)
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);
421 if (status) {
422 dbg("%s - nonzero write bulk status received: %d",
423 __func__, status);
424 return;
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;
442 /* set up our urb */
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);
447 if (result) {
448 dev_err(&port->dev,
449 "%s - failed submitting write urb, error %d\n",
450 __func__, result);
451 /* Throw away data. No better idea what to do with it. */
452 priv->wrfilled = 0;
453 priv->wrsent = 0;
454 goto exit;
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));
466 priv->wrfilled = 0;
467 priv->wrsent = 0;
471 exit:
472 spin_unlock(&priv->lock);
473 usb_serial_port_softint(port);
476 static int __init cyberjack_init(void)
478 int retval;
479 retval = usb_serial_register(&cyberjack_device);
480 if (retval)
481 goto failed_usb_serial_register;
482 retval = usb_register(&cyberjack_driver);
483 if (retval)
484 goto failed_usb_register;
486 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION " "
487 DRIVER_AUTHOR "\n");
488 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
490 return 0;
491 failed_usb_register:
492 usb_serial_deregister(&cyberjack_device);
493 failed_usb_serial_register:
494 return retval;
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");