[PATCH] USB: move usb-serial.h to include/linux/usb/
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / serial / cyberjack.c
blob6286aba86fae2340beaed87c5f2a970050053939
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 <asm/uaccess.h>
41 #include <linux/usb.h>
42 #include <linux/usb/serial.h>
44 #define CYBERJACK_LOCAL_BUF_SIZE 32
46 static int 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_shutdown (struct usb_serial *serial);
62 static int cyberjack_open (struct usb_serial_port *port, struct file *filp);
63 static void cyberjack_close (struct usb_serial_port *port, struct file *filp);
64 static int cyberjack_write (struct usb_serial_port *port, const unsigned char *buf, int count);
65 static int cyberjack_write_room( struct usb_serial_port *port );
66 static void cyberjack_read_int_callback (struct urb *urb, struct pt_regs *regs);
67 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
68 static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
70 static struct usb_device_id id_table [] = {
71 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
72 { } /* Terminating entry */
75 MODULE_DEVICE_TABLE (usb, id_table);
77 static struct usb_driver cyberjack_driver = {
78 .name = "cyberjack",
79 .probe = usb_serial_probe,
80 .disconnect = usb_serial_disconnect,
81 .id_table = id_table,
82 .no_dynamic_id = 1,
85 static struct usb_serial_driver cyberjack_device = {
86 .driver = {
87 .owner = THIS_MODULE,
88 .name = "cyberjack",
90 .description = "Reiner SCT Cyberjack USB card reader",
91 .id_table = id_table,
92 .num_interrupt_in = 1,
93 .num_bulk_in = 1,
94 .num_bulk_out = 1,
95 .num_ports = 1,
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;
119 int i;
121 dbg("%s", __FUNCTION__);
123 /* allocate the private data structure */
124 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
125 if (!priv)
126 return -ENOMEM;
128 /* set initial values */
129 spin_lock_init(&priv->lock);
130 priv->rdtodo = 0;
131 priv->wrfilled = 0;
132 priv->wrsent = 0;
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) {
138 int result;
139 serial->port[i]->interrupt_in_urb->dev = serial->dev;
140 result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
141 GFP_KERNEL);
142 if (result)
143 err(" usb_submit_urb(read int) failed");
144 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
147 return( 0 );
150 static void cyberjack_shutdown (struct usb_serial *serial)
152 int i;
154 dbg("%s", __FUNCTION__);
156 for (i=0; i < serial->num_ports; ++i) {
157 usb_kill_urb(serial->port[i]->interrupt_in_urb);
158 /* My special items, the standard routines free my urbs */
159 kfree(usb_get_serial_port_data(serial->port[i]));
160 usb_set_serial_port_data(serial->port[i], NULL);
164 static int cyberjack_open (struct usb_serial_port *port, struct file *filp)
166 struct cyberjack_private *priv;
167 unsigned long flags;
168 int result = 0;
170 dbg("%s - port %d", __FUNCTION__, port->number);
172 dbg("%s - usb_clear_halt", __FUNCTION__ );
173 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
175 /* force low_latency on so that our tty_push actually forces
176 * the data through, otherwise it is scheduled, and with high
177 * data rates (like with OHCI) data can get lost.
179 port->tty->low_latency = 1;
181 priv = usb_get_serial_port_data(port);
182 spin_lock_irqsave(&priv->lock, flags);
183 priv->rdtodo = 0;
184 priv->wrfilled = 0;
185 priv->wrsent = 0;
186 spin_unlock_irqrestore(&priv->lock, flags);
188 return result;
191 static void cyberjack_close (struct usb_serial_port *port, struct file *filp)
193 dbg("%s - port %d", __FUNCTION__, port->number);
195 if (port->serial->dev) {
196 /* shutdown any bulk reads that might be going on */
197 usb_kill_urb(port->write_urb);
198 usb_kill_urb(port->read_urb);
202 static int cyberjack_write (struct usb_serial_port *port, const unsigned char *buf, int count)
204 struct usb_serial *serial = port->serial;
205 struct cyberjack_private *priv = usb_get_serial_port_data(port);
206 unsigned long flags;
207 int result;
208 int wrexpected;
210 dbg("%s - port %d", __FUNCTION__, port->number);
212 if (count == 0) {
213 dbg("%s - write request of 0 bytes", __FUNCTION__);
214 return (0);
217 spin_lock(&port->lock);
218 if (port->write_urb_busy) {
219 spin_unlock(&port->lock);
220 dbg("%s - already writing", __FUNCTION__);
221 return 0;
223 port->write_urb_busy = 1;
224 spin_unlock(&port->lock);
226 spin_lock_irqsave(&priv->lock, flags);
228 if( (count+priv->wrfilled)>sizeof(priv->wrbuf) ) {
229 /* To much data for buffer. Reset buffer. */
230 priv->wrfilled=0;
231 spin_unlock_irqrestore(&priv->lock, flags);
232 port->write_urb_busy = 0;
233 return (0);
236 /* Copy data */
237 memcpy (priv->wrbuf+priv->wrfilled, buf, count);
239 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
240 priv->wrbuf+priv->wrfilled);
241 priv->wrfilled += count;
243 if( priv->wrfilled >= 3 ) {
244 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
245 dbg("%s - expected data: %d", __FUNCTION__, wrexpected);
246 } else {
247 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)", __FUNCTION__);
255 length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected;
257 memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length );
258 priv->wrsent=length;
260 /* set up our urb */
261 usb_fill_bulk_urb(port->write_urb, serial->dev,
262 usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
263 port->write_urb->transfer_buffer, length,
264 ((serial->type->write_bulk_callback) ?
265 serial->type->write_bulk_callback :
266 cyberjack_write_bulk_callback),
267 port);
269 /* send the data out the bulk port */
270 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
271 if (result) {
272 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
273 /* Throw away data. No better idea what to do with it. */
274 priv->wrfilled=0;
275 priv->wrsent=0;
276 spin_unlock_irqrestore(&priv->lock, flags);
277 port->write_urb_busy = 0;
278 return 0;
281 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
282 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
284 if( priv->wrsent>=priv->wrfilled ) {
285 dbg("%s - buffer cleaned", __FUNCTION__);
286 memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
287 priv->wrfilled=0;
288 priv->wrsent=0;
292 spin_unlock_irqrestore(&priv->lock, flags);
294 return (count);
297 static int cyberjack_write_room( struct usb_serial_port *port )
299 return CYBERJACK_LOCAL_BUF_SIZE;
302 static void cyberjack_read_int_callback( struct urb *urb, struct pt_regs *regs )
304 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
305 struct cyberjack_private *priv = usb_get_serial_port_data(port);
306 unsigned char *data = urb->transfer_buffer;
307 int result;
309 dbg("%s - port %d", __FUNCTION__, port->number);
311 /* the urb might have been killed. */
312 if (urb->status)
313 return;
315 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, 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;
320 int result;
322 /* This is a announcement of coming bulk_ins. */
323 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
325 spin_lock(&priv->lock);
327 old_rdtodo = priv->rdtodo;
329 if( (old_rdtodo+size)<(old_rdtodo) ) {
330 dbg( "To many bulk_in urbs to do." );
331 spin_unlock(&priv->lock);
332 goto resubmit;
335 /* "+=" is probably more fault tollerant than "=" */
336 priv->rdtodo += size;
338 dbg("%s - rdtodo: %d", __FUNCTION__, priv->rdtodo);
340 spin_unlock(&priv->lock);
342 if( !old_rdtodo ) {
343 port->read_urb->dev = port->serial->dev;
344 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
345 if( result )
346 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
347 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
351 resubmit:
352 port->interrupt_in_urb->dev = port->serial->dev;
353 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
354 if (result)
355 err(" usb_submit_urb(read int) failed");
356 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
359 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
361 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
362 struct cyberjack_private *priv = usb_get_serial_port_data(port);
363 struct tty_struct *tty;
364 unsigned char *data = urb->transfer_buffer;
365 short todo;
366 int result;
368 dbg("%s - port %d", __FUNCTION__, port->number);
370 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
371 if (urb->status) {
372 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
373 return;
376 tty = port->tty;
377 if (!tty) {
378 dbg("%s - ignoring since device not open\n", __FUNCTION__);
379 return;
381 if (urb->actual_length) {
382 tty_buffer_request_room(tty, urb->actual_length);
383 tty_insert_flip_string(tty, data, urb->actual_length);
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 port->write_urb_busy = 0;
417 if (urb->status) {
418 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
419 return;
422 spin_lock(&priv->lock);
424 /* only do something if we have more data to send */
425 if( priv->wrfilled ) {
426 int length, blksize, result;
428 dbg("%s - transmitting data (frame n)", __FUNCTION__);
430 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
431 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
433 memcpy (port->write_urb->transfer_buffer, priv->wrbuf + priv->wrsent,
434 length );
435 priv->wrsent+=length;
437 /* set up our urb */
438 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
439 usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
440 port->write_urb->transfer_buffer, length,
441 ((port->serial->type->write_bulk_callback) ?
442 port->serial->type->write_bulk_callback :
443 cyberjack_write_bulk_callback),
444 port);
446 /* send the data out the bulk port */
447 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
448 if (result) {
449 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
450 /* Throw away data. No better idea what to do with it. */
451 priv->wrfilled=0;
452 priv->wrsent=0;
453 goto exit;
456 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
457 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
459 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
461 if( (priv->wrsent>=priv->wrfilled) || (priv->wrsent>=blksize) ) {
462 dbg("%s - buffer cleaned", __FUNCTION__);
463 memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
464 priv->wrfilled=0;
465 priv->wrsent=0;
469 exit:
470 spin_unlock(&priv->lock);
471 usb_serial_port_softint(port);
474 static int __init cyberjack_init (void)
476 int retval;
477 retval = usb_serial_register(&cyberjack_device);
478 if (retval)
479 goto failed_usb_serial_register;
480 retval = usb_register(&cyberjack_driver);
481 if (retval)
482 goto failed_usb_register;
484 info(DRIVER_VERSION " " DRIVER_AUTHOR);
485 info(DRIVER_DESC);
487 return 0;
488 failed_usb_register:
489 usb_serial_deregister(&cyberjack_device);
490 failed_usb_serial_register:
491 return retval;
494 static void __exit cyberjack_exit (void)
496 usb_deregister (&cyberjack_driver);
497 usb_serial_deregister (&cyberjack_device);
500 module_init(cyberjack_init);
501 module_exit(cyberjack_exit);
503 MODULE_AUTHOR( DRIVER_AUTHOR );
504 MODULE_DESCRIPTION( DRIVER_DESC );
505 MODULE_VERSION( DRIVER_VERSION );
506 MODULE_LICENSE("GPL");
508 module_param(debug, bool, S_IRUGO | S_IWUSR);
509 MODULE_PARM_DESC(debug, "Debug enabled or not");