usb serial: support Alcor Micro Corp. USB 2.0 TO RS-232 through pl2303 driver
[linux-2.6] / 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: support@reiner-sct.com (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  *  Please note that later models of the cyberjack reader family are
25  *  supported by a libusb-based userspace device driver.
26  *
27  *  Homepage: http://www.reiner-sct.de/support/treiber_cyberjack.php#linux
28  */
29
30
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>
43
44 #define CYBERJACK_LOCAL_BUF_SIZE 32
45
46 static int debug;
47
48 /*
49  * Version Information
50  */
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"
54
55
56 #define CYBERJACK_VENDOR_ID     0x0C4B
57 #define CYBERJACK_PRODUCT_ID    0x0100
58
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);
69
70 static struct usb_device_id id_table [] = {
71         { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
72         { }                     /* Terminating entry */
73 };
74
75 MODULE_DEVICE_TABLE (usb, id_table);
76
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,
83 };
84
85 static struct usb_serial_driver cyberjack_device = {
86         .driver = {
87                 .owner =        THIS_MODULE,
88                 .name =         "cyberjack",
89         },
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,
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 already 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         int i;
120
121         dbg("%s", __FUNCTION__);
122
123         /* allocate the private data structure */
124         priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
125         if (!priv)
126                 return -ENOMEM;
127
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);
134
135         init_waitqueue_head(&serial->port[0]->write_wait);
136
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__);
145         }
146
147         return( 0 );
148 }
149
150 static void cyberjack_shutdown (struct usb_serial *serial)
151 {
152         int i;
153         
154         dbg("%s", __FUNCTION__);
155
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);
161         }
162 }
163         
164 static int  cyberjack_open (struct usb_serial_port *port, struct file *filp)
165 {
166         struct cyberjack_private *priv;
167         unsigned long flags;
168         int result = 0;
169
170         dbg("%s - port %d", __FUNCTION__, port->number);
171
172         dbg("%s - usb_clear_halt", __FUNCTION__ );
173         usb_clear_halt(port->serial->dev, port->write_urb->pipe);
174
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.
178          */
179         port->tty->low_latency = 1;
180
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);
187
188         return result;
189 }
190
191 static void cyberjack_close (struct usb_serial_port *port, struct file *filp)
192 {
193         dbg("%s - port %d", __FUNCTION__, port->number);
194
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);
199         }
200 }
201
202 static int cyberjack_write (struct usb_serial_port *port, const unsigned char *buf, int count)
203 {
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;
209
210         dbg("%s - port %d", __FUNCTION__, port->number);
211
212         if (count == 0) {
213                 dbg("%s - write request of 0 bytes", __FUNCTION__);
214                 return (0);
215         }
216
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;
222         }
223         port->write_urb_busy = 1;
224         spin_unlock(&port->lock);
225
226         spin_lock_irqsave(&priv->lock, flags);
227
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);
234         }
235
236         /* Copy data */
237         memcpy (priv->wrbuf+priv->wrfilled, buf, count);
238
239         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
240                 priv->wrbuf+priv->wrfilled);
241         priv->wrfilled += count;
242
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);
248         }
249
250         if( priv->wrfilled >= wrexpected ) {
251                 /* We have enough data to begin transmission */
252                 int length;
253
254                 dbg("%s - transmitting data (frame 1)", __FUNCTION__);
255                 length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected;
256
257                 memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length );
258                 priv->wrsent=length;
259
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);
268
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;
279                 }
280
281                 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
282                 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
283
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;
289                 }
290         }
291
292         spin_unlock_irqrestore(&priv->lock, flags);
293
294         return (count);
295
296
297 static int cyberjack_write_room( struct usb_serial_port *port )
298 {
299         return CYBERJACK_LOCAL_BUF_SIZE;
300 }
301
302 static void cyberjack_read_int_callback( struct urb *urb, struct pt_regs *regs )
303 {
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;
308
309         dbg("%s - port %d", __FUNCTION__, port->number);
310
311         /* the urb might have been killed. */
312         if (urb->status)
313                 return;
314
315         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
316
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;
321
322                 /* This is a announcement of coming bulk_ins. */
323                 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
324
325                 spin_lock(&priv->lock);
326
327                 old_rdtodo = priv->rdtodo;
328
329                 if( (old_rdtodo+size)<(old_rdtodo) ) {
330                         dbg( "To many bulk_in urbs to do." );
331                         spin_unlock(&priv->lock);
332                         goto resubmit;
333                 }
334
335                 /* "+=" is probably more fault tollerant than "=" */
336                 priv->rdtodo += size;
337
338                 dbg("%s - rdtodo: %d", __FUNCTION__, priv->rdtodo);
339
340                 spin_unlock(&priv->lock);
341
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__);
348                 }
349         }
350
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__);
357 }
358
359 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
360 {
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;
367
368         dbg("%s - port %d", __FUNCTION__, port->number);
369         
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;
374         }
375
376         tty = port->tty;
377         if (!tty) {
378                 dbg("%s - ignoring since device not open\n", __FUNCTION__);
379                 return;
380         }
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);
385         }
386
387         spin_lock(&priv->lock);
388
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;
394
395         spin_unlock(&priv->lock);
396
397         dbg("%s - rdtodo: %d", __FUNCTION__, todo);
398
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__);
406         }
407 }
408
409 static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
410 {
411         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
412         struct cyberjack_private *priv = usb_get_serial_port_data(port);
413
414         dbg("%s - port %d", __FUNCTION__, port->number);
415
416         port->write_urb_busy = 0;
417         if (urb->status) {
418                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
419                 return;
420         }
421
422         spin_lock(&priv->lock);
423
424         /* only do something if we have more data to send */
425         if( priv->wrfilled ) {
426                 int length, blksize, result;
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         usb_serial_port_softint(port);
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");