vserver 1.9.5.x5
[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.01"
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, const unsigned char *buf, int count);
61 static int cyberjack_write_room( struct usb_serial_port *port );
62 static void cyberjack_read_int_callback (struct urb *urb, struct pt_regs *regs);
63 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
64 static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
65
66 static struct usb_device_id id_table [] = {
67         { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
68         { }                     /* Terminating entry */
69 };
70
71 MODULE_DEVICE_TABLE (usb, id_table);
72
73 static struct usb_driver cyberjack_driver = {
74         .owner =        THIS_MODULE,
75         .name =         "cyberjack",
76         .probe =        usb_serial_probe,
77         .disconnect =   usb_serial_disconnect,
78         .id_table =     id_table,
79 };
80
81 static struct usb_serial_device_type cyberjack_device = {
82         .owner =                THIS_MODULE,
83         .name =                 "Reiner SCT Cyberjack USB card reader",
84         .short_name =           "cyberjack",
85         .id_table =             id_table,
86         .num_interrupt_in =     1,
87         .num_bulk_in =          1,
88         .num_bulk_out =         1,
89         .num_ports =            1,
90         .attach =               cyberjack_startup,
91         .shutdown =             cyberjack_shutdown,
92         .open =                 cyberjack_open,
93         .close =                cyberjack_close,
94         .write =                cyberjack_write,
95         .write_room =   cyberjack_write_room,
96         .read_int_callback =    cyberjack_read_int_callback,
97         .read_bulk_callback =   cyberjack_read_bulk_callback,
98         .write_bulk_callback =  cyberjack_write_bulk_callback,
99 };
100
101 struct cyberjack_private {
102         spinlock_t      lock;           /* Lock for SMP */
103         short           rdtodo;         /* Bytes still to read */
104         unsigned char   wrbuf[5*64];    /* Buffer for collecting data to write */
105         short           wrfilled;       /* Overall data size we already got */
106         short           wrsent;         /* Data already sent */
107 };
108
109 /* do some startup allocations not currently performed by usb_serial_probe() */
110 static int cyberjack_startup (struct usb_serial *serial)
111 {
112         struct cyberjack_private *priv;
113         int i;
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         for (i = 0; i < serial->num_ports; ++i) {
132                 int result;
133                 serial->port[i]->interrupt_in_urb->dev = serial->dev;
134                 result = usb_submit_urb(serial->port[i]->interrupt_in_urb, 
135                                         GFP_KERNEL);
136                 if (result)
137                         err(" usb_submit_urb(read int) failed");
138                 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
139         }
140
141         return( 0 );
142 }
143
144 static void cyberjack_shutdown (struct usb_serial *serial)
145 {
146         int i;
147         
148         dbg("%s", __FUNCTION__);
149
150         for (i=0; i < serial->num_ports; ++i) {
151                 usb_kill_urb(serial->port[i]->interrupt_in_urb);
152                 /* My special items, the standard routines free my urbs */
153                 kfree(usb_get_serial_port_data(serial->port[i]));
154                 usb_set_serial_port_data(serial->port[i], NULL);
155         }
156 }
157         
158 static int  cyberjack_open (struct usb_serial_port *port, struct file *filp)
159 {
160         struct cyberjack_private *priv;
161         unsigned long flags;
162         int result = 0;
163
164         dbg("%s - port %d", __FUNCTION__, port->number);
165
166         dbg("%s - usb_clear_halt", __FUNCTION__ );
167         usb_clear_halt(port->serial->dev, port->write_urb->pipe);
168
169         /* force low_latency on so that our tty_push actually forces
170          * the data through, otherwise it is scheduled, and with high
171          * data rates (like with OHCI) data can get lost.
172          */
173         port->tty->low_latency = 1;
174
175         priv = usb_get_serial_port_data(port);
176         spin_lock_irqsave(&priv->lock, flags);
177         priv->rdtodo = 0;
178         priv->wrfilled = 0;
179         priv->wrsent = 0;
180         spin_unlock_irqrestore(&priv->lock, flags);
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_kill_urb(port->write_urb);
192                 usb_kill_urb(port->read_urb);
193         }
194 }
195
196 static int cyberjack_write (struct usb_serial_port *port, const unsigned char *buf, int count)
197 {
198         struct usb_serial *serial = port->serial;
199         struct cyberjack_private *priv = usb_get_serial_port_data(port);
200         unsigned long flags;
201         int result;
202         int wrexpected;
203
204         dbg("%s - port %d", __FUNCTION__, port->number);
205
206         if (count == 0) {
207                 dbg("%s - write request of 0 bytes", __FUNCTION__);
208                 return (0);
209         }
210
211         if (port->write_urb->status == -EINPROGRESS) {
212                 dbg("%s - already writing", __FUNCTION__);
213                 return (0);
214         }
215
216         spin_lock_irqsave(&priv->lock, flags);
217
218         if( (count+priv->wrfilled)>sizeof(priv->wrbuf) ) {
219                 /* To much data for buffer. Reset buffer. */
220                 priv->wrfilled=0;
221                 spin_unlock_irqrestore(&priv->lock, flags);
222                 return (0);
223         }
224
225         /* Copy data */
226         memcpy (priv->wrbuf+priv->wrfilled, buf, count);
227
228         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
229                 priv->wrbuf+priv->wrfilled);
230         priv->wrfilled += count;
231
232         if( priv->wrfilled >= 3 ) {
233                 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
234                 dbg("%s - expected data: %d", __FUNCTION__, wrexpected);
235         } else {
236                 wrexpected = sizeof(priv->wrbuf);
237         }
238
239         if( priv->wrfilled >= wrexpected ) {
240                 /* We have enough data to begin transmission */
241                 int length;
242
243                 dbg("%s - transmitting data (frame 1)", __FUNCTION__);
244                 length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected;
245
246                 memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length );
247                 priv->wrsent=length;
248
249                 /* set up our urb */
250                 usb_fill_bulk_urb(port->write_urb, serial->dev, 
251                               usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
252                               port->write_urb->transfer_buffer, length,
253                               ((serial->type->write_bulk_callback) ? 
254                                serial->type->write_bulk_callback : 
255                                cyberjack_write_bulk_callback), 
256                               port);
257
258                 /* send the data out the bulk port */
259                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
260                 if (result) {
261                         err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
262                         /* Throw away data. No better idea what to do with it. */
263                         priv->wrfilled=0;
264                         priv->wrsent=0;
265                         spin_unlock_irqrestore(&priv->lock, flags);
266                         return 0;
267                 }
268
269                 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
270                 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
271
272                 if( priv->wrsent>=priv->wrfilled ) {
273                         dbg("%s - buffer cleaned", __FUNCTION__);
274                         memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
275                         priv->wrfilled=0;
276                         priv->wrsent=0;
277                 }
278         }
279
280         spin_unlock_irqrestore(&priv->lock, flags);
281
282         return (count);
283
284
285 static int cyberjack_write_room( struct usb_serial_port *port )
286 {
287         return CYBERJACK_LOCAL_BUF_SIZE;
288 }
289
290 static void cyberjack_read_int_callback( struct urb *urb, struct pt_regs *regs )
291 {
292         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
293         struct cyberjack_private *priv = usb_get_serial_port_data(port);
294         unsigned char *data = urb->transfer_buffer;
295         int result;
296
297         dbg("%s - port %d", __FUNCTION__, port->number);
298
299         /* the urb might have been killed. */
300         if (urb->status)
301                 return;
302
303         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
304
305         /* React only to interrupts signaling a bulk_in transfer */
306         if( (urb->actual_length==4) && (data[0]==0x01) ) {
307                 short old_rdtodo;
308                 int result;
309
310                 /* This is a announcement of coming bulk_ins. */
311                 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
312
313                 spin_lock(&priv->lock);
314
315                 old_rdtodo = priv->rdtodo;
316
317                 if( (old_rdtodo+size)<(old_rdtodo) ) {
318                         dbg( "To many bulk_in urbs to do." );
319                         spin_unlock(&priv->lock);
320                         goto resubmit;
321                 }
322
323                 /* "+=" is probably more fault tollerant than "=" */
324                 priv->rdtodo += size;
325
326                 dbg("%s - rdtodo: %d", __FUNCTION__, priv->rdtodo);
327
328                 spin_unlock(&priv->lock);
329
330                 if( !old_rdtodo ) {
331                         port->read_urb->dev = port->serial->dev;
332                         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
333                         if( result )
334                                 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
335                         dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
336                 }
337         }
338
339 resubmit:
340         port->interrupt_in_urb->dev = port->serial->dev;
341         result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
342         if (result)
343                 err(" usb_submit_urb(read int) failed");
344         dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
345 }
346
347 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
348 {
349         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
350         struct cyberjack_private *priv = usb_get_serial_port_data(port);
351         struct tty_struct *tty;
352         unsigned char *data = urb->transfer_buffer;
353         short todo;
354         int i;
355         int result;
356
357         dbg("%s - port %d", __FUNCTION__, port->number);
358         
359         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
360         if (urb->status) {
361                 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
362                 return;
363         }
364
365         tty = port->tty;
366         if (!tty) {
367                 dbg("%s - ignoring since device not open\n", __FUNCTION__);
368                 return;
369         }
370         if (urb->actual_length) {
371                 for (i = 0; i < urb->actual_length ; ++i) {
372                         /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
373                         if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
374                                 tty_flip_buffer_push(tty);
375                         }
376                         /* this doesn't actually push the data through unless tty->low_latency is set */
377                         tty_insert_flip_char(tty, data[i], 0);
378                 }
379                 tty_flip_buffer_push(tty);
380         }
381
382         spin_lock(&priv->lock);
383
384         /* Reduce urbs to do by one. */
385         priv->rdtodo-=urb->actual_length;
386         /* Just to be sure */
387         if ( priv->rdtodo<0 ) priv->rdtodo = 0;
388         todo = priv->rdtodo;
389
390         spin_unlock(&priv->lock);
391
392         dbg("%s - rdtodo: %d", __FUNCTION__, todo);
393
394         /* Continue to read if we have still urbs to do. */
395         if( todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/ ) {
396                 port->read_urb->dev = port->serial->dev;
397                 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
398                 if (result)
399                         err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
400                 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
401         }
402 }
403
404 static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
405 {
406         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
407         struct cyberjack_private *priv = usb_get_serial_port_data(port);
408
409         dbg("%s - port %d", __FUNCTION__, port->number);
410         
411         if (urb->status) {
412                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
413                 return;
414         }
415
416         spin_lock(&priv->lock);
417
418         /* only do something if we have more data to send */
419         if( priv->wrfilled ) {
420                 int length, blksize, result;
421
422                 if (port->write_urb->status == -EINPROGRESS) {
423                         dbg("%s - already writing", __FUNCTION__);
424                         spin_unlock(&priv->lock);
425                         return;
426                 }
427
428                 dbg("%s - transmitting data (frame n)", __FUNCTION__);
429
430                 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
431                         port->bulk_out_size : (priv->wrfilled - priv->wrsent);
432
433                 memcpy (port->write_urb->transfer_buffer, priv->wrbuf + priv->wrsent,
434                         length );
435                 priv->wrsent+=length;
436
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);
445
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;
454                 }
455
456                 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
457                 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
458
459                 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
460
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;
466                 }
467         }
468
469 exit:
470         spin_unlock(&priv->lock);
471         schedule_work(&port->work);
472 }
473
474 static int __init cyberjack_init (void)
475 {
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;
483
484         info(DRIVER_VERSION " " DRIVER_AUTHOR);
485         info(DRIVER_DESC);
486
487         return 0;
488 failed_usb_register:
489         usb_serial_deregister(&cyberjack_device);
490 failed_usb_serial_register:
491         return retval;
492 }
493
494 static void __exit cyberjack_exit (void)
495 {
496         usb_deregister (&cyberjack_driver);
497         usb_serial_deregister (&cyberjack_device);
498 }
499
500 module_init(cyberjack_init);
501 module_exit(cyberjack_exit);
502
503 MODULE_AUTHOR( DRIVER_AUTHOR );
504 MODULE_DESCRIPTION( DRIVER_DESC );
505 MODULE_VERSION( DRIVER_VERSION );
506 MODULE_LICENSE("GPL");
507
508 module_param(debug, bool, S_IRUGO | S_IWUSR);
509 MODULE_PARM_DESC(debug, "Debug enabled or not");