VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / usb / serial / cyberjack.c
1 /*
2  *  REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
3  *
4  *  Copyright (C) 2001  REINER SCT
5  *  Author: Matthias Bruestle
6  *
7  *  Contact: linux-usb@sii.li (see MAINTAINERS)
8  *
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.
12  *
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.
17  *
18  *  Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
19  *  patience.
20  *
21  *  In case of problems, please write to the contact e-mail address
22  *  mentioned above.
23  */
24
25
26 #include <linux/config.h>
27 #include <linux/kernel.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/tty.h>
32 #include <linux/tty_driver.h>
33 #include <linux/tty_flip.h>
34 #include <linux/module.h>
35 #include <linux/spinlock.h>
36 #include <asm/uaccess.h>
37 #include <linux/usb.h>
38 #include "usb-serial.h"
39
40 #define CYBERJACK_LOCAL_BUF_SIZE 32
41
42 static int debug;
43
44 /*
45  * Version Information
46  */
47 #define DRIVER_VERSION "v1.0"
48 #define DRIVER_AUTHOR "Matthias Bruestle"
49 #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
50
51
52 #define CYBERJACK_VENDOR_ID     0x0C4B
53 #define CYBERJACK_PRODUCT_ID    0x0100
54
55 /* Function prototypes */
56 static int cyberjack_startup (struct usb_serial *serial);
57 static void cyberjack_shutdown (struct usb_serial *serial);
58 static int  cyberjack_open (struct usb_serial_port *port, struct file *filp);
59 static void cyberjack_close (struct usb_serial_port *port, struct file *filp);
60 static int cyberjack_write (struct usb_serial_port *port, int from_user,
61         const unsigned char *buf, int count);
62 static int cyberjack_write_room( struct usb_serial_port *port );
63 static void cyberjack_read_int_callback (struct urb *urb, struct pt_regs *regs);
64 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
65 static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
66
67 static struct usb_device_id id_table [] = {
68         { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
69         { }                     /* Terminating entry */
70 };
71
72 MODULE_DEVICE_TABLE (usb, id_table);
73
74 static struct usb_driver cyberjack_driver = {
75         .owner =        THIS_MODULE,
76         .name =         "cyberjack",
77         .probe =        usb_serial_probe,
78         .disconnect =   usb_serial_disconnect,
79         .id_table =     id_table,
80 };
81
82 static struct usb_serial_device_type cyberjack_device = {
83         .owner =                THIS_MODULE,
84         .name =                 "Reiner SCT Cyberjack USB card reader",
85         .short_name =           "cyberjack",
86         .id_table =             id_table,
87         .num_interrupt_in =     1,
88         .num_bulk_in =          1,
89         .num_bulk_out =         1,
90         .num_ports =            1,
91         .attach =               cyberjack_startup,
92         .shutdown =             cyberjack_shutdown,
93         .open =                 cyberjack_open,
94         .close =                cyberjack_close,
95         .write =                cyberjack_write,
96         .write_room =   cyberjack_write_room,
97         .read_int_callback =    cyberjack_read_int_callback,
98         .read_bulk_callback =   cyberjack_read_bulk_callback,
99         .write_bulk_callback =  cyberjack_write_bulk_callback,
100 };
101
102 struct cyberjack_private {
103         spinlock_t      lock;           /* Lock for SMP */
104         short           rdtodo;         /* Bytes still to read */
105         unsigned char   wrbuf[5*64];    /* Buffer for collecting data to write */
106         short           wrfilled;       /* Overall data size we already got */
107         short           wrsent;         /* Data already sent */
108 };
109
110 /* do some startup allocations not currently performed by usb_serial_probe() */
111 static int cyberjack_startup (struct usb_serial *serial)
112 {
113         struct cyberjack_private *priv;
114
115         dbg("%s", __FUNCTION__);
116
117         /* allocate the private data structure */
118         priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
119         if (!priv)
120                 return -ENOMEM;
121
122         /* set initial values */
123         spin_lock_init(&priv->lock);
124         priv->rdtodo = 0;
125         priv->wrfilled = 0;
126         priv->wrsent = 0;
127         usb_set_serial_port_data(serial->port[0], priv);
128
129         init_waitqueue_head(&serial->port[0]->write_wait);
130
131         return( 0 );
132 }
133
134 static void cyberjack_shutdown (struct usb_serial *serial)
135 {
136         int i;
137         
138         dbg("%s", __FUNCTION__);
139
140         for (i=0; i < serial->num_ports; ++i) {
141                 /* My special items, the standard routines free my urbs */
142                 kfree(usb_get_serial_port_data(serial->port[i]));
143                 usb_set_serial_port_data(serial->port[i], NULL);
144         }
145 }
146         
147 static int  cyberjack_open (struct usb_serial_port *port, struct file *filp)
148 {
149         struct cyberjack_private *priv;
150         unsigned long flags;
151         int result = 0;
152
153         dbg("%s - port %d", __FUNCTION__, port->number);
154
155         dbg("%s - usb_clear_halt", __FUNCTION__ );
156         usb_clear_halt(port->serial->dev, port->write_urb->pipe);
157
158         /* force low_latency on so that our tty_push actually forces
159          * the data through, otherwise it is scheduled, and with high
160          * data rates (like with OHCI) data can get lost.
161          */
162         port->tty->low_latency = 1;
163
164         priv = usb_get_serial_port_data(port);
165         spin_lock_irqsave(&priv->lock, flags);
166         priv->rdtodo = 0;
167         priv->wrfilled = 0;
168         priv->wrsent = 0;
169         spin_unlock_irqrestore(&priv->lock, flags);
170
171         /* shutdown any bulk reads that might be going on */
172         usb_unlink_urb (port->write_urb);
173         usb_unlink_urb (port->read_urb);
174         usb_unlink_urb (port->interrupt_in_urb);
175
176         port->interrupt_in_urb->dev = port->serial->dev;
177         result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
178         if (result)
179                 err(" usb_submit_urb(read int) failed");
180         dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
181
182         return result;
183 }
184
185 static void cyberjack_close (struct usb_serial_port *port, struct file *filp)
186 {
187         dbg("%s - port %d", __FUNCTION__, port->number);
188
189         if (port->serial->dev) {
190                 /* shutdown any bulk reads that might be going on */
191                 usb_unlink_urb (port->write_urb);
192                 usb_unlink_urb (port->read_urb);
193                 usb_unlink_urb (port->interrupt_in_urb);
194                 dbg("%s - usb_clear_halt", __FUNCTION__ );
195                 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
196                 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
197                 usb_clear_halt(port->serial->dev, port->interrupt_in_urb->pipe);
198         }
199 }
200
201 static int cyberjack_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
202 {
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;
208
209         dbg("%s - port %d", __FUNCTION__, port->number);
210         dbg("%s - from_user %d", __FUNCTION__, from_user);
211
212         if (count == 0) {
213                 dbg("%s - write request of 0 bytes", __FUNCTION__);
214                 return (0);
215         }
216
217         if (port->write_urb->status == -EINPROGRESS) {
218                 dbg("%s - already writing", __FUNCTION__);
219                 return (0);
220         }
221
222         spin_lock_irqsave(&priv->lock, flags);
223
224         if( (count+priv->wrfilled)>sizeof(priv->wrbuf) ) {
225                 /* To much data for buffer. Reset buffer. */
226                 priv->wrfilled=0;
227                 spin_unlock_irqrestore(&priv->lock, flags);
228                 return (0);
229         }
230
231         /* Copy data */
232         if (from_user) {
233                 if (copy_from_user(priv->wrbuf+priv->wrfilled, buf, count)) {
234                         spin_unlock_irqrestore(&priv->lock, flags);
235                         return -EFAULT;
236                 }
237         } else {
238                 memcpy (priv->wrbuf+priv->wrfilled, buf, count);
239         }  
240
241         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
242                 priv->wrbuf+priv->wrfilled);
243         priv->wrfilled += count;
244
245         if( priv->wrfilled >= 3 ) {
246                 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
247                 dbg("%s - expected data: %d", __FUNCTION__, wrexpected);
248         } else {
249                 wrexpected = sizeof(priv->wrbuf);
250         }
251
252         if( priv->wrfilled >= wrexpected ) {
253                 /* We have enough data to begin transmission */
254                 int length;
255
256                 dbg("%s - transmitting data (frame 1)", __FUNCTION__);
257                 length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected;
258
259                 memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length );
260                 priv->wrsent=length;
261
262                 /* set up our urb */
263                 usb_fill_bulk_urb(port->write_urb, serial->dev, 
264                               usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
265                               port->write_urb->transfer_buffer, length,
266                               ((serial->type->write_bulk_callback) ? 
267                                serial->type->write_bulk_callback : 
268                                cyberjack_write_bulk_callback), 
269                               port);
270
271                 /* send the data out the bulk port */
272                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
273                 if (result) {
274                         err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
275                         /* Throw away data. No better idea what to do with it. */
276                         priv->wrfilled=0;
277                         priv->wrsent=0;
278                         spin_unlock_irqrestore(&priv->lock, flags);
279                         return 0;
280                 }
281
282                 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
283                 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
284
285                 if( priv->wrsent>=priv->wrfilled ) {
286                         dbg("%s - buffer cleaned", __FUNCTION__);
287                         memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
288                         priv->wrfilled=0;
289                         priv->wrsent=0;
290                 }
291         }
292
293         spin_unlock_irqrestore(&priv->lock, flags);
294
295         return (count);
296
297
298 static int cyberjack_write_room( struct usb_serial_port *port )
299 {
300         return CYBERJACK_LOCAL_BUF_SIZE;
301 }
302
303 static void cyberjack_read_int_callback( struct urb *urb, struct pt_regs *regs )
304 {
305         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
306         struct cyberjack_private *priv = usb_get_serial_port_data(port);
307         unsigned char *data = urb->transfer_buffer;
308         int result;
309
310         dbg("%s - port %d", __FUNCTION__, port->number);
311
312         /* the urb might have been killed. */
313         if (urb->status)
314                 return;
315
316         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
317
318         /* React only to interrupts signaling a bulk_in transfer */
319         if( (urb->actual_length==4) && (data[0]==0x01) ) {
320                 short old_rdtodo;
321                 int result;
322
323                 /* This is a announcement of coming bulk_ins. */
324                 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
325
326                 spin_lock(&priv->lock);
327
328                 old_rdtodo = priv->rdtodo;
329
330                 if( (old_rdtodo+size)<(old_rdtodo) ) {
331                         dbg( "To many bulk_in urbs to do." );
332                         spin_unlock(&priv->lock);
333                         goto resubmit;
334                 }
335
336                 /* "+=" is probably more fault tollerant than "=" */
337                 priv->rdtodo += size;
338
339                 dbg("%s - rdtodo: %d", __FUNCTION__, priv->rdtodo);
340
341                 spin_unlock(&priv->lock);
342
343                 if( !old_rdtodo ) {
344                         port->read_urb->dev = port->serial->dev;
345                         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
346                         if( result )
347                                 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
348                         dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
349                 }
350         }
351
352 resubmit:
353         port->interrupt_in_urb->dev = port->serial->dev;
354         result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
355         if (result)
356                 err(" usb_submit_urb(read int) failed");
357         dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
358 }
359
360 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
361 {
362         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
363         struct cyberjack_private *priv = usb_get_serial_port_data(port);
364         struct tty_struct *tty;
365         unsigned char *data = urb->transfer_buffer;
366         short todo;
367         int i;
368         int result;
369
370         dbg("%s - port %d", __FUNCTION__, port->number);
371         
372         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
373         if (urb->status) {
374                 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
375                 return;
376         }
377
378         tty = port->tty;
379         if (urb->actual_length) {
380                 for (i = 0; i < urb->actual_length ; ++i) {
381                         /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
382                         if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
383                                 tty_flip_buffer_push(tty);
384                         }
385                         /* this doesn't actually push the data through unless tty->low_latency is set */
386                         tty_insert_flip_char(tty, data[i], 0);
387                 }
388                 tty_flip_buffer_push(tty);
389         }
390
391         spin_lock(&priv->lock);
392
393         /* Reduce urbs to do by one. */
394         priv->rdtodo-=urb->actual_length;
395         /* Just to be sure */
396         if ( priv->rdtodo<0 ) priv->rdtodo = 0;
397         todo = priv->rdtodo;
398
399         spin_unlock(&priv->lock);
400
401         dbg("%s - rdtodo: %d", __FUNCTION__, todo);
402
403         /* Continue to read if we have still urbs to do. */
404         if( todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/ ) {
405                 port->read_urb->dev = port->serial->dev;
406                 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
407                 if (result)
408                         err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
409                 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
410         }
411 }
412
413 static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
414 {
415         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
416         struct cyberjack_private *priv = usb_get_serial_port_data(port);
417
418         dbg("%s - port %d", __FUNCTION__, port->number);
419         
420         if (urb->status) {
421                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
422                 return;
423         }
424
425         spin_lock(&priv->lock);
426
427         /* only do something if we have more data to send */
428         if( priv->wrfilled ) {
429                 int length, blksize, result;
430
431                 if (port->write_urb->status == -EINPROGRESS) {
432                         dbg("%s - already writing", __FUNCTION__);
433                         spin_unlock(&priv->lock);
434                         return;
435                 }
436
437                 dbg("%s - transmitting data (frame n)", __FUNCTION__);
438
439                 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
440                         port->bulk_out_size : (priv->wrfilled - priv->wrsent);
441
442                 memcpy (port->write_urb->transfer_buffer, priv->wrbuf + priv->wrsent,
443                         length );
444                 priv->wrsent+=length;
445
446                 /* set up our urb */
447                 usb_fill_bulk_urb(port->write_urb, port->serial->dev, 
448                               usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
449                               port->write_urb->transfer_buffer, length,
450                               ((port->serial->type->write_bulk_callback) ? 
451                                port->serial->type->write_bulk_callback : 
452                                cyberjack_write_bulk_callback), 
453                               port);
454
455                 /* send the data out the bulk port */
456                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
457                 if (result) {
458                         err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
459                         /* Throw away data. No better idea what to do with it. */
460                         priv->wrfilled=0;
461                         priv->wrsent=0;
462                         goto exit;
463                 }
464
465                 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
466                 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
467
468                 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
469
470                 if( (priv->wrsent>=priv->wrfilled) || (priv->wrsent>=blksize) ) {
471                         dbg("%s - buffer cleaned", __FUNCTION__);
472                         memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
473                         priv->wrfilled=0;
474                         priv->wrsent=0;
475                 }
476         }
477
478 exit:
479         spin_unlock(&priv->lock);
480         schedule_work(&port->work);
481 }
482
483 static int __init cyberjack_init (void)
484 {
485         int retval;
486         retval  = usb_serial_register(&cyberjack_device);
487         if (retval)
488                 goto failed_usb_serial_register;
489         retval = usb_register(&cyberjack_driver);
490         if (retval) 
491                 goto failed_usb_register;
492
493         info(DRIVER_VERSION " " DRIVER_AUTHOR);
494         info(DRIVER_DESC);
495
496         return 0;
497 failed_usb_register:
498         usb_serial_deregister(&cyberjack_device);
499 failed_usb_serial_register:
500         return retval;
501 }
502
503 static void __exit cyberjack_exit (void)
504 {
505         usb_deregister (&cyberjack_driver);
506         usb_serial_deregister (&cyberjack_device);
507 }
508
509 module_init(cyberjack_init);
510 module_exit(cyberjack_exit);
511
512 MODULE_AUTHOR( DRIVER_AUTHOR );
513 MODULE_DESCRIPTION( DRIVER_DESC );
514 MODULE_LICENSE("GPL");
515
516 module_param(debug, bool, S_IRUGO | S_IWUSR);
517 MODULE_PARM_DESC(debug, "Debug enabled or not");