]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/usb/serial/cyberjack.c
b5b431067b086bedabcf3f83851b5d55a2c2e214
[mirror_ubuntu-bionic-kernel.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: 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
47 static 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 */
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, const unsigned char *buf, int count);
66 static int cyberjack_write_room( struct usb_serial_port *port );
67 static void cyberjack_read_int_callback (struct urb *urb, struct pt_regs *regs);
68 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
69 static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
70
71 static struct usb_device_id id_table [] = {
72 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
73 { } /* Terminating entry */
74 };
75
76 MODULE_DEVICE_TABLE (usb, id_table);
77
78 static 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
86 static 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
106 struct 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() */
115 static 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
149 static 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
163 static 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
190 static 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
201 static 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 spin_lock(&port->lock);
217 if (port->write_urb_busy) {
218 spin_unlock(&port->lock);
219 dbg("%s - already writing", __FUNCTION__);
220 return 0;
221 }
222 port->write_urb_busy = 1;
223 spin_unlock(&port->lock);
224
225 spin_lock_irqsave(&priv->lock, flags);
226
227 if( (count+priv->wrfilled)>sizeof(priv->wrbuf) ) {
228 /* To much data for buffer. Reset buffer. */
229 priv->wrfilled=0;
230 spin_unlock_irqrestore(&priv->lock, flags);
231 port->write_urb_busy = 0;
232 return (0);
233 }
234
235 /* Copy data */
236 memcpy (priv->wrbuf+priv->wrfilled, buf, count);
237
238 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
239 priv->wrbuf+priv->wrfilled);
240 priv->wrfilled += count;
241
242 if( priv->wrfilled >= 3 ) {
243 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
244 dbg("%s - expected data: %d", __FUNCTION__, wrexpected);
245 } else {
246 wrexpected = sizeof(priv->wrbuf);
247 }
248
249 if( priv->wrfilled >= wrexpected ) {
250 /* We have enough data to begin transmission */
251 int length;
252
253 dbg("%s - transmitting data (frame 1)", __FUNCTION__);
254 length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected;
255
256 memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length );
257 priv->wrsent=length;
258
259 /* set up our urb */
260 usb_fill_bulk_urb(port->write_urb, serial->dev,
261 usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
262 port->write_urb->transfer_buffer, length,
263 ((serial->type->write_bulk_callback) ?
264 serial->type->write_bulk_callback :
265 cyberjack_write_bulk_callback),
266 port);
267
268 /* send the data out the bulk port */
269 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
270 if (result) {
271 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
272 /* Throw away data. No better idea what to do with it. */
273 priv->wrfilled=0;
274 priv->wrsent=0;
275 spin_unlock_irqrestore(&priv->lock, flags);
276 port->write_urb_busy = 0;
277 return 0;
278 }
279
280 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
281 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
282
283 if( priv->wrsent>=priv->wrfilled ) {
284 dbg("%s - buffer cleaned", __FUNCTION__);
285 memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
286 priv->wrfilled=0;
287 priv->wrsent=0;
288 }
289 }
290
291 spin_unlock_irqrestore(&priv->lock, flags);
292
293 return (count);
294 }
295
296 static int cyberjack_write_room( struct usb_serial_port *port )
297 {
298 return CYBERJACK_LOCAL_BUF_SIZE;
299 }
300
301 static void cyberjack_read_int_callback( struct urb *urb, struct pt_regs *regs )
302 {
303 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
304 struct cyberjack_private *priv = usb_get_serial_port_data(port);
305 unsigned char *data = urb->transfer_buffer;
306 int result;
307
308 dbg("%s - port %d", __FUNCTION__, port->number);
309
310 /* the urb might have been killed. */
311 if (urb->status)
312 return;
313
314 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
315
316 /* React only to interrupts signaling a bulk_in transfer */
317 if( (urb->actual_length==4) && (data[0]==0x01) ) {
318 short old_rdtodo;
319 int result;
320
321 /* This is a announcement of coming bulk_ins. */
322 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
323
324 spin_lock(&priv->lock);
325
326 old_rdtodo = priv->rdtodo;
327
328 if( (old_rdtodo+size)<(old_rdtodo) ) {
329 dbg( "To many bulk_in urbs to do." );
330 spin_unlock(&priv->lock);
331 goto resubmit;
332 }
333
334 /* "+=" is probably more fault tollerant than "=" */
335 priv->rdtodo += size;
336
337 dbg("%s - rdtodo: %d", __FUNCTION__, priv->rdtodo);
338
339 spin_unlock(&priv->lock);
340
341 if( !old_rdtodo ) {
342 port->read_urb->dev = port->serial->dev;
343 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
344 if( result )
345 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
346 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
347 }
348 }
349
350 resubmit:
351 port->interrupt_in_urb->dev = port->serial->dev;
352 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
353 if (result)
354 err(" usb_submit_urb(read int) failed");
355 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
356 }
357
358 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
359 {
360 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
361 struct cyberjack_private *priv = usb_get_serial_port_data(port);
362 struct tty_struct *tty;
363 unsigned char *data = urb->transfer_buffer;
364 short todo;
365 int i;
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 for (i = 0; i < urb->actual_length ; ++i) {
383 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
384 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
385 tty_flip_buffer_push(tty);
386 }
387 /* this doesn't actually push the data through unless tty->low_latency is set */
388 tty_insert_flip_char(tty, data[i], 0);
389 }
390 tty_flip_buffer_push(tty);
391 }
392
393 spin_lock(&priv->lock);
394
395 /* Reduce urbs to do by one. */
396 priv->rdtodo-=urb->actual_length;
397 /* Just to be sure */
398 if ( priv->rdtodo<0 ) priv->rdtodo = 0;
399 todo = priv->rdtodo;
400
401 spin_unlock(&priv->lock);
402
403 dbg("%s - rdtodo: %d", __FUNCTION__, todo);
404
405 /* Continue to read if we have still urbs to do. */
406 if( todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/ ) {
407 port->read_urb->dev = port->serial->dev;
408 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
409 if (result)
410 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
411 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
412 }
413 }
414
415 static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
416 {
417 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
418 struct cyberjack_private *priv = usb_get_serial_port_data(port);
419
420 dbg("%s - port %d", __FUNCTION__, port->number);
421
422 port->write_urb_busy = 0;
423 if (urb->status) {
424 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
425 return;
426 }
427
428 spin_lock(&priv->lock);
429
430 /* only do something if we have more data to send */
431 if( priv->wrfilled ) {
432 int length, blksize, result;
433
434 dbg("%s - transmitting data (frame n)", __FUNCTION__);
435
436 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
437 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
438
439 memcpy (port->write_urb->transfer_buffer, priv->wrbuf + priv->wrsent,
440 length );
441 priv->wrsent+=length;
442
443 /* set up our urb */
444 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
445 usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
446 port->write_urb->transfer_buffer, length,
447 ((port->serial->type->write_bulk_callback) ?
448 port->serial->type->write_bulk_callback :
449 cyberjack_write_bulk_callback),
450 port);
451
452 /* send the data out the bulk port */
453 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
454 if (result) {
455 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
456 /* Throw away data. No better idea what to do with it. */
457 priv->wrfilled=0;
458 priv->wrsent=0;
459 goto exit;
460 }
461
462 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
463 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
464
465 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
466
467 if( (priv->wrsent>=priv->wrfilled) || (priv->wrsent>=blksize) ) {
468 dbg("%s - buffer cleaned", __FUNCTION__);
469 memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
470 priv->wrfilled=0;
471 priv->wrsent=0;
472 }
473 }
474
475 exit:
476 spin_unlock(&priv->lock);
477 schedule_work(&port->work);
478 }
479
480 static int __init cyberjack_init (void)
481 {
482 int retval;
483 retval = usb_serial_register(&cyberjack_device);
484 if (retval)
485 goto failed_usb_serial_register;
486 retval = usb_register(&cyberjack_driver);
487 if (retval)
488 goto failed_usb_register;
489
490 info(DRIVER_VERSION " " DRIVER_AUTHOR);
491 info(DRIVER_DESC);
492
493 return 0;
494 failed_usb_register:
495 usb_serial_deregister(&cyberjack_device);
496 failed_usb_serial_register:
497 return retval;
498 }
499
500 static void __exit cyberjack_exit (void)
501 {
502 usb_deregister (&cyberjack_driver);
503 usb_serial_deregister (&cyberjack_device);
504 }
505
506 module_init(cyberjack_init);
507 module_exit(cyberjack_exit);
508
509 MODULE_AUTHOR( DRIVER_AUTHOR );
510 MODULE_DESCRIPTION( DRIVER_DESC );
511 MODULE_VERSION( DRIVER_VERSION );
512 MODULE_LICENSE("GPL");
513
514 module_param(debug, bool, S_IRUGO | S_IWUSR);
515 MODULE_PARM_DESC(debug, "Debug enabled or not");