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