[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / usb / serial / cyberjack.c
blob46a204cd40e19c614a795407e63b4c91debb6a64
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/config.h>
32 #include <linux/kernel.h>
33 #include <linux/errno.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
36 #include <linux/tty.h>
37 #include <linux/tty_driver.h>
38 #include <linux/tty_flip.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <asm/uaccess.h>
42 #include <linux/usb.h>
43 #include "usb-serial.h"
45 #define CYBERJACK_LOCAL_BUF_SIZE 32
47 static int debug;
50 * Version Information
52 #define DRIVER_VERSION "v1.01"
53 #define DRIVER_AUTHOR "Matthias Bruestle"
54 #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
57 #define CYBERJACK_VENDOR_ID 0x0C4B
58 #define CYBERJACK_PRODUCT_ID 0x0100
60 /* Function prototypes */
61 static int cyberjack_startup (struct usb_serial *serial);
62 static void cyberjack_shutdown (struct usb_serial *serial);
63 static int cyberjack_open (struct usb_serial_port *port, struct file *filp);
64 static void cyberjack_close (struct usb_serial_port *port, struct file *filp);
65 static int cyberjack_write (struct usb_serial_port *port, const unsigned char *buf, int count);
66 static int cyberjack_write_room( struct usb_serial_port *port );
67 static void cyberjack_read_int_callback (struct urb *urb, struct pt_regs *regs);
68 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
69 static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
71 static struct usb_device_id id_table [] = {
72 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
73 { } /* Terminating entry */
76 MODULE_DEVICE_TABLE (usb, id_table);
78 static struct usb_driver cyberjack_driver = {
79 .owner = THIS_MODULE,
80 .name = "cyberjack",
81 .probe = usb_serial_probe,
82 .disconnect = usb_serial_disconnect,
83 .id_table = id_table,
86 static struct usb_serial_device_type cyberjack_device = {
87 .owner = THIS_MODULE,
88 .name = "Reiner SCT Cyberjack USB card reader",
89 .short_name = "cyberjack",
90 .id_table = id_table,
91 .num_interrupt_in = 1,
92 .num_bulk_in = 1,
93 .num_bulk_out = 1,
94 .num_ports = 1,
95 .attach = cyberjack_startup,
96 .shutdown = cyberjack_shutdown,
97 .open = cyberjack_open,
98 .close = cyberjack_close,
99 .write = cyberjack_write,
100 .write_room = cyberjack_write_room,
101 .read_int_callback = cyberjack_read_int_callback,
102 .read_bulk_callback = cyberjack_read_bulk_callback,
103 .write_bulk_callback = cyberjack_write_bulk_callback,
106 struct cyberjack_private {
107 spinlock_t lock; /* Lock for SMP */
108 short rdtodo; /* Bytes still to read */
109 unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
110 short wrfilled; /* Overall data size we already got */
111 short wrsent; /* Data already sent */
114 /* do some startup allocations not currently performed by usb_serial_probe() */
115 static int cyberjack_startup (struct usb_serial *serial)
117 struct cyberjack_private *priv;
118 int i;
120 dbg("%s", __FUNCTION__);
122 /* allocate the private data structure */
123 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
124 if (!priv)
125 return -ENOMEM;
127 /* set initial values */
128 spin_lock_init(&priv->lock);
129 priv->rdtodo = 0;
130 priv->wrfilled = 0;
131 priv->wrsent = 0;
132 usb_set_serial_port_data(serial->port[0], priv);
134 init_waitqueue_head(&serial->port[0]->write_wait);
136 for (i = 0; i < serial->num_ports; ++i) {
137 int result;
138 serial->port[i]->interrupt_in_urb->dev = serial->dev;
139 result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
140 GFP_KERNEL);
141 if (result)
142 err(" usb_submit_urb(read int) failed");
143 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
146 return( 0 );
149 static void cyberjack_shutdown (struct usb_serial *serial)
151 int i;
153 dbg("%s", __FUNCTION__);
155 for (i=0; i < serial->num_ports; ++i) {
156 usb_kill_urb(serial->port[i]->interrupt_in_urb);
157 /* My special items, the standard routines free my urbs */
158 kfree(usb_get_serial_port_data(serial->port[i]));
159 usb_set_serial_port_data(serial->port[i], NULL);
163 static int cyberjack_open (struct usb_serial_port *port, struct file *filp)
165 struct cyberjack_private *priv;
166 unsigned long flags;
167 int result = 0;
169 dbg("%s - port %d", __FUNCTION__, port->number);
171 dbg("%s - usb_clear_halt", __FUNCTION__ );
172 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
174 /* force low_latency on so that our tty_push actually forces
175 * the data through, otherwise it is scheduled, and with high
176 * data rates (like with OHCI) data can get lost.
178 port->tty->low_latency = 1;
180 priv = usb_get_serial_port_data(port);
181 spin_lock_irqsave(&priv->lock, flags);
182 priv->rdtodo = 0;
183 priv->wrfilled = 0;
184 priv->wrsent = 0;
185 spin_unlock_irqrestore(&priv->lock, flags);
187 return result;
190 static void cyberjack_close (struct usb_serial_port *port, struct file *filp)
192 dbg("%s - port %d", __FUNCTION__, port->number);
194 if (port->serial->dev) {
195 /* shutdown any bulk reads that might be going on */
196 usb_kill_urb(port->write_urb);
197 usb_kill_urb(port->read_urb);
201 static int cyberjack_write (struct usb_serial_port *port, const unsigned char *buf, int count)
203 struct usb_serial *serial = port->serial;
204 struct cyberjack_private *priv = usb_get_serial_port_data(port);
205 unsigned long flags;
206 int result;
207 int wrexpected;
209 dbg("%s - port %d", __FUNCTION__, port->number);
211 if (count == 0) {
212 dbg("%s - write request of 0 bytes", __FUNCTION__);
213 return (0);
216 if (port->write_urb->status == -EINPROGRESS) {
217 dbg("%s - already writing", __FUNCTION__);
218 return (0);
221 spin_lock_irqsave(&priv->lock, flags);
223 if( (count+priv->wrfilled)>sizeof(priv->wrbuf) ) {
224 /* To much data for buffer. Reset buffer. */
225 priv->wrfilled=0;
226 spin_unlock_irqrestore(&priv->lock, flags);
227 return (0);
230 /* Copy data */
231 memcpy (priv->wrbuf+priv->wrfilled, buf, count);
233 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
234 priv->wrbuf+priv->wrfilled);
235 priv->wrfilled += count;
237 if( priv->wrfilled >= 3 ) {
238 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
239 dbg("%s - expected data: %d", __FUNCTION__, wrexpected);
240 } else {
241 wrexpected = sizeof(priv->wrbuf);
244 if( priv->wrfilled >= wrexpected ) {
245 /* We have enough data to begin transmission */
246 int length;
248 dbg("%s - transmitting data (frame 1)", __FUNCTION__);
249 length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected;
251 memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length );
252 priv->wrsent=length;
254 /* set up our urb */
255 usb_fill_bulk_urb(port->write_urb, serial->dev,
256 usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
257 port->write_urb->transfer_buffer, length,
258 ((serial->type->write_bulk_callback) ?
259 serial->type->write_bulk_callback :
260 cyberjack_write_bulk_callback),
261 port);
263 /* send the data out the bulk port */
264 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
265 if (result) {
266 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
267 /* Throw away data. No better idea what to do with it. */
268 priv->wrfilled=0;
269 priv->wrsent=0;
270 spin_unlock_irqrestore(&priv->lock, flags);
271 return 0;
274 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
275 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
277 if( priv->wrsent>=priv->wrfilled ) {
278 dbg("%s - buffer cleaned", __FUNCTION__);
279 memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
280 priv->wrfilled=0;
281 priv->wrsent=0;
285 spin_unlock_irqrestore(&priv->lock, flags);
287 return (count);
290 static int cyberjack_write_room( struct usb_serial_port *port )
292 return CYBERJACK_LOCAL_BUF_SIZE;
295 static void cyberjack_read_int_callback( struct urb *urb, struct pt_regs *regs )
297 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
298 struct cyberjack_private *priv = usb_get_serial_port_data(port);
299 unsigned char *data = urb->transfer_buffer;
300 int result;
302 dbg("%s - port %d", __FUNCTION__, port->number);
304 /* the urb might have been killed. */
305 if (urb->status)
306 return;
308 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
310 /* React only to interrupts signaling a bulk_in transfer */
311 if( (urb->actual_length==4) && (data[0]==0x01) ) {
312 short old_rdtodo;
313 int result;
315 /* This is a announcement of coming bulk_ins. */
316 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
318 spin_lock(&priv->lock);
320 old_rdtodo = priv->rdtodo;
322 if( (old_rdtodo+size)<(old_rdtodo) ) {
323 dbg( "To many bulk_in urbs to do." );
324 spin_unlock(&priv->lock);
325 goto resubmit;
328 /* "+=" is probably more fault tollerant than "=" */
329 priv->rdtodo += size;
331 dbg("%s - rdtodo: %d", __FUNCTION__, priv->rdtodo);
333 spin_unlock(&priv->lock);
335 if( !old_rdtodo ) {
336 port->read_urb->dev = port->serial->dev;
337 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
338 if( result )
339 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
340 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
344 resubmit:
345 port->interrupt_in_urb->dev = port->serial->dev;
346 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
347 if (result)
348 err(" usb_submit_urb(read int) failed");
349 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
352 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
354 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
355 struct cyberjack_private *priv = usb_get_serial_port_data(port);
356 struct tty_struct *tty;
357 unsigned char *data = urb->transfer_buffer;
358 short todo;
359 int i;
360 int result;
362 dbg("%s - port %d", __FUNCTION__, port->number);
364 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
365 if (urb->status) {
366 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
367 return;
370 tty = port->tty;
371 if (!tty) {
372 dbg("%s - ignoring since device not open\n", __FUNCTION__);
373 return;
375 if (urb->actual_length) {
376 for (i = 0; i < urb->actual_length ; ++i) {
377 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
378 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
379 tty_flip_buffer_push(tty);
381 /* this doesn't actually push the data through unless tty->low_latency is set */
382 tty_insert_flip_char(tty, data[i], 0);
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;
393 todo = priv->rdtodo;
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);
403 if (result)
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, struct pt_regs *regs)
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 if (urb->status) {
417 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
418 return;
421 spin_lock(&priv->lock);
423 /* only do something if we have more data to send */
424 if( priv->wrfilled ) {
425 int length, blksize, result;
427 if (port->write_urb->status == -EINPROGRESS) {
428 dbg("%s - already writing", __FUNCTION__);
429 spin_unlock(&priv->lock);
430 return;
433 dbg("%s - transmitting data (frame n)", __FUNCTION__);
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, priv->wrbuf + priv->wrsent,
439 length );
440 priv->wrsent+=length;
442 /* set up our urb */
443 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
444 usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
445 port->write_urb->transfer_buffer, length,
446 ((port->serial->type->write_bulk_callback) ?
447 port->serial->type->write_bulk_callback :
448 cyberjack_write_bulk_callback),
449 port);
451 /* send the data out the bulk port */
452 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
453 if (result) {
454 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
455 /* Throw away data. No better idea what to do with it. */
456 priv->wrfilled=0;
457 priv->wrsent=0;
458 goto exit;
461 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
462 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
464 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
466 if( (priv->wrsent>=priv->wrfilled) || (priv->wrsent>=blksize) ) {
467 dbg("%s - buffer cleaned", __FUNCTION__);
468 memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
469 priv->wrfilled=0;
470 priv->wrsent=0;
474 exit:
475 spin_unlock(&priv->lock);
476 schedule_work(&port->work);
479 static int __init cyberjack_init (void)
481 int retval;
482 retval = usb_serial_register(&cyberjack_device);
483 if (retval)
484 goto failed_usb_serial_register;
485 retval = usb_register(&cyberjack_driver);
486 if (retval)
487 goto failed_usb_register;
489 info(DRIVER_VERSION " " DRIVER_AUTHOR);
490 info(DRIVER_DESC);
492 return 0;
493 failed_usb_register:
494 usb_serial_deregister(&cyberjack_device);
495 failed_usb_serial_register:
496 return retval;
499 static void __exit cyberjack_exit (void)
501 usb_deregister (&cyberjack_driver);
502 usb_serial_deregister (&cyberjack_device);
505 module_init(cyberjack_init);
506 module_exit(cyberjack_exit);
508 MODULE_AUTHOR( DRIVER_AUTHOR );
509 MODULE_DESCRIPTION( DRIVER_DESC );
510 MODULE_VERSION( DRIVER_VERSION );
511 MODULE_LICENSE("GPL");
513 module_param(debug, bool, S_IRUGO | S_IWUSR);
514 MODULE_PARM_DESC(debug, "Debug enabled or not");