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