[PATCH] USB: make registering a usb driver automatically set the module owner
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / serial / cyberjack.c
blob8c10e40049054bdcf96758df86f1de3a6fea974c
1 /*
2 * REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
4 * Copyright (C) 2001 REINER SCT
5 * Author: Matthias Bruestle
7 * Contact: support@reiner-sct.com (see MAINTAINERS)
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.
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.
18 * Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
19 * patience.
21 * In case of problems, please write to the contact e-mail address
22 * mentioned above.
24 * Please note that later models of the cyberjack reader family are
25 * supported by a libusb-based userspace device driver.
27 * Homepage: http://www.reiner-sct.de/support/treiber_cyberjack.php#linux
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"
45 #define CYBERJACK_LOCAL_BUF_SIZE 32
47 static int debug;
50 * Version Information
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"
57 #define CYBERJACK_VENDOR_ID 0x0C4B
58 #define CYBERJACK_PRODUCT_ID 0x0100
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);
71 static struct usb_device_id id_table [] = {
72 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
73 { } /* Terminating entry */
76 MODULE_DEVICE_TABLE (usb, id_table);
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 .no_dynamic_id = 1,
87 static struct usb_serial_driver cyberjack_device = {
88 .driver = {
89 .owner = THIS_MODULE,
90 .name = "cyberjack",
92 .description = "Reiner SCT Cyberjack USB card reader",
93 .id_table = id_table,
94 .num_interrupt_in = 1,
95 .num_bulk_in = 1,
96 .num_bulk_out = 1,
97 .num_ports = 1,
98 .attach = cyberjack_startup,
99 .shutdown = cyberjack_shutdown,
100 .open = cyberjack_open,
101 .close = cyberjack_close,
102 .write = cyberjack_write,
103 .write_room = cyberjack_write_room,
104 .read_int_callback = cyberjack_read_int_callback,
105 .read_bulk_callback = cyberjack_read_bulk_callback,
106 .write_bulk_callback = cyberjack_write_bulk_callback,
109 struct cyberjack_private {
110 spinlock_t lock; /* Lock for SMP */
111 short rdtodo; /* Bytes still to read */
112 unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
113 short wrfilled; /* Overall data size we already got */
114 short wrsent; /* Data already sent */
117 /* do some startup allocations not currently performed by usb_serial_probe() */
118 static int cyberjack_startup (struct usb_serial *serial)
120 struct cyberjack_private *priv;
121 int i;
123 dbg("%s", __FUNCTION__);
125 /* allocate the private data structure */
126 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
127 if (!priv)
128 return -ENOMEM;
130 /* set initial values */
131 spin_lock_init(&priv->lock);
132 priv->rdtodo = 0;
133 priv->wrfilled = 0;
134 priv->wrsent = 0;
135 usb_set_serial_port_data(serial->port[0], priv);
137 init_waitqueue_head(&serial->port[0]->write_wait);
139 for (i = 0; i < serial->num_ports; ++i) {
140 int result;
141 serial->port[i]->interrupt_in_urb->dev = serial->dev;
142 result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
143 GFP_KERNEL);
144 if (result)
145 err(" usb_submit_urb(read int) failed");
146 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
149 return( 0 );
152 static void cyberjack_shutdown (struct usb_serial *serial)
154 int i;
156 dbg("%s", __FUNCTION__);
158 for (i=0; i < serial->num_ports; ++i) {
159 usb_kill_urb(serial->port[i]->interrupt_in_urb);
160 /* My special items, the standard routines free my urbs */
161 kfree(usb_get_serial_port_data(serial->port[i]));
162 usb_set_serial_port_data(serial->port[i], NULL);
166 static int cyberjack_open (struct usb_serial_port *port, struct file *filp)
168 struct cyberjack_private *priv;
169 unsigned long flags;
170 int result = 0;
172 dbg("%s - port %d", __FUNCTION__, port->number);
174 dbg("%s - usb_clear_halt", __FUNCTION__ );
175 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
177 /* force low_latency on so that our tty_push actually forces
178 * the data through, otherwise it is scheduled, and with high
179 * data rates (like with OHCI) data can get lost.
181 port->tty->low_latency = 1;
183 priv = usb_get_serial_port_data(port);
184 spin_lock_irqsave(&priv->lock, flags);
185 priv->rdtodo = 0;
186 priv->wrfilled = 0;
187 priv->wrsent = 0;
188 spin_unlock_irqrestore(&priv->lock, flags);
190 return result;
193 static void cyberjack_close (struct usb_serial_port *port, struct file *filp)
195 dbg("%s - port %d", __FUNCTION__, port->number);
197 if (port->serial->dev) {
198 /* shutdown any bulk reads that might be going on */
199 usb_kill_urb(port->write_urb);
200 usb_kill_urb(port->read_urb);
204 static int cyberjack_write (struct usb_serial_port *port, const unsigned char *buf, int count)
206 struct usb_serial *serial = port->serial;
207 struct cyberjack_private *priv = usb_get_serial_port_data(port);
208 unsigned long flags;
209 int result;
210 int wrexpected;
212 dbg("%s - port %d", __FUNCTION__, port->number);
214 if (count == 0) {
215 dbg("%s - write request of 0 bytes", __FUNCTION__);
216 return (0);
219 spin_lock(&port->lock);
220 if (port->write_urb_busy) {
221 spin_unlock(&port->lock);
222 dbg("%s - already writing", __FUNCTION__);
223 return 0;
225 port->write_urb_busy = 1;
226 spin_unlock(&port->lock);
228 spin_lock_irqsave(&priv->lock, flags);
230 if( (count+priv->wrfilled)>sizeof(priv->wrbuf) ) {
231 /* To much data for buffer. Reset buffer. */
232 priv->wrfilled=0;
233 spin_unlock_irqrestore(&priv->lock, flags);
234 port->write_urb_busy = 0;
235 return (0);
238 /* Copy data */
239 memcpy (priv->wrbuf+priv->wrfilled, buf, count);
241 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
242 priv->wrbuf+priv->wrfilled);
243 priv->wrfilled += count;
245 if( priv->wrfilled >= 3 ) {
246 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
247 dbg("%s - expected data: %d", __FUNCTION__, wrexpected);
248 } else {
249 wrexpected = sizeof(priv->wrbuf);
252 if( priv->wrfilled >= wrexpected ) {
253 /* We have enough data to begin transmission */
254 int length;
256 dbg("%s - transmitting data (frame 1)", __FUNCTION__);
257 length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected;
259 memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length );
260 priv->wrsent=length;
262 /* set up our urb */
263 usb_fill_bulk_urb(port->write_urb, serial->dev,
264 usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
265 port->write_urb->transfer_buffer, length,
266 ((serial->type->write_bulk_callback) ?
267 serial->type->write_bulk_callback :
268 cyberjack_write_bulk_callback),
269 port);
271 /* send the data out the bulk port */
272 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
273 if (result) {
274 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
275 /* Throw away data. No better idea what to do with it. */
276 priv->wrfilled=0;
277 priv->wrsent=0;
278 spin_unlock_irqrestore(&priv->lock, flags);
279 port->write_urb_busy = 0;
280 return 0;
283 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
284 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
286 if( priv->wrsent>=priv->wrfilled ) {
287 dbg("%s - buffer cleaned", __FUNCTION__);
288 memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
289 priv->wrfilled=0;
290 priv->wrsent=0;
294 spin_unlock_irqrestore(&priv->lock, flags);
296 return (count);
299 static int cyberjack_write_room( struct usb_serial_port *port )
301 return CYBERJACK_LOCAL_BUF_SIZE;
304 static void cyberjack_read_int_callback( struct urb *urb, struct pt_regs *regs )
306 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
307 struct cyberjack_private *priv = usb_get_serial_port_data(port);
308 unsigned char *data = urb->transfer_buffer;
309 int result;
311 dbg("%s - port %d", __FUNCTION__, port->number);
313 /* the urb might have been killed. */
314 if (urb->status)
315 return;
317 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
319 /* React only to interrupts signaling a bulk_in transfer */
320 if( (urb->actual_length==4) && (data[0]==0x01) ) {
321 short old_rdtodo;
322 int result;
324 /* This is a announcement of coming bulk_ins. */
325 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
327 spin_lock(&priv->lock);
329 old_rdtodo = priv->rdtodo;
331 if( (old_rdtodo+size)<(old_rdtodo) ) {
332 dbg( "To many bulk_in urbs to do." );
333 spin_unlock(&priv->lock);
334 goto resubmit;
337 /* "+=" is probably more fault tollerant than "=" */
338 priv->rdtodo += size;
340 dbg("%s - rdtodo: %d", __FUNCTION__, priv->rdtodo);
342 spin_unlock(&priv->lock);
344 if( !old_rdtodo ) {
345 port->read_urb->dev = port->serial->dev;
346 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
347 if( result )
348 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
349 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
353 resubmit:
354 port->interrupt_in_urb->dev = port->serial->dev;
355 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
356 if (result)
357 err(" usb_submit_urb(read int) failed");
358 dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
361 static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
363 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
364 struct cyberjack_private *priv = usb_get_serial_port_data(port);
365 struct tty_struct *tty;
366 unsigned char *data = urb->transfer_buffer;
367 short todo;
368 int i;
369 int result;
371 dbg("%s - port %d", __FUNCTION__, port->number);
373 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
374 if (urb->status) {
375 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
376 return;
379 tty = port->tty;
380 if (!tty) {
381 dbg("%s - ignoring since device not open\n", __FUNCTION__);
382 return;
384 if (urb->actual_length) {
385 for (i = 0; i < urb->actual_length ; ++i) {
386 /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
387 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
388 tty_flip_buffer_push(tty);
390 /* this doesn't actually push the data through unless tty->low_latency is set */
391 tty_insert_flip_char(tty, data[i], 0);
393 tty_flip_buffer_push(tty);
396 spin_lock(&priv->lock);
398 /* Reduce urbs to do by one. */
399 priv->rdtodo-=urb->actual_length;
400 /* Just to be sure */
401 if ( priv->rdtodo<0 ) priv->rdtodo = 0;
402 todo = priv->rdtodo;
404 spin_unlock(&priv->lock);
406 dbg("%s - rdtodo: %d", __FUNCTION__, todo);
408 /* Continue to read if we have still urbs to do. */
409 if( todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/ ) {
410 port->read_urb->dev = port->serial->dev;
411 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
412 if (result)
413 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
414 dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
418 static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
420 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
421 struct cyberjack_private *priv = usb_get_serial_port_data(port);
423 dbg("%s - port %d", __FUNCTION__, port->number);
425 port->write_urb_busy = 0;
426 if (urb->status) {
427 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
428 return;
431 spin_lock(&priv->lock);
433 /* only do something if we have more data to send */
434 if( priv->wrfilled ) {
435 int length, blksize, result;
437 dbg("%s - transmitting data (frame n)", __FUNCTION__);
439 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
440 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
442 memcpy (port->write_urb->transfer_buffer, priv->wrbuf + priv->wrsent,
443 length );
444 priv->wrsent+=length;
446 /* set up our urb */
447 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
448 usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
449 port->write_urb->transfer_buffer, length,
450 ((port->serial->type->write_bulk_callback) ?
451 port->serial->type->write_bulk_callback :
452 cyberjack_write_bulk_callback),
453 port);
455 /* send the data out the bulk port */
456 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
457 if (result) {
458 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
459 /* Throw away data. No better idea what to do with it. */
460 priv->wrfilled=0;
461 priv->wrsent=0;
462 goto exit;
465 dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
466 dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
468 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
470 if( (priv->wrsent>=priv->wrfilled) || (priv->wrsent>=blksize) ) {
471 dbg("%s - buffer cleaned", __FUNCTION__);
472 memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
473 priv->wrfilled=0;
474 priv->wrsent=0;
478 exit:
479 spin_unlock(&priv->lock);
480 schedule_work(&port->work);
483 static int __init cyberjack_init (void)
485 int retval;
486 retval = usb_serial_register(&cyberjack_device);
487 if (retval)
488 goto failed_usb_serial_register;
489 retval = usb_register(&cyberjack_driver);
490 if (retval)
491 goto failed_usb_register;
493 info(DRIVER_VERSION " " DRIVER_AUTHOR);
494 info(DRIVER_DESC);
496 return 0;
497 failed_usb_register:
498 usb_serial_deregister(&cyberjack_device);
499 failed_usb_serial_register:
500 return retval;
503 static void __exit cyberjack_exit (void)
505 usb_deregister (&cyberjack_driver);
506 usb_serial_deregister (&cyberjack_device);
509 module_init(cyberjack_init);
510 module_exit(cyberjack_exit);
512 MODULE_AUTHOR( DRIVER_AUTHOR );
513 MODULE_DESCRIPTION( DRIVER_DESC );
514 MODULE_VERSION( DRIVER_VERSION );
515 MODULE_LICENSE("GPL");
517 module_param(debug, bool, S_IRUGO | S_IWUSR);
518 MODULE_PARM_DESC(debug, "Debug enabled or not");