Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 42 | #include <linux/usb/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 ); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 66 | static void cyberjack_read_int_callback (struct urb *urb); |
| 67 | static void cyberjack_read_bulk_callback (struct urb *urb); |
| 68 | static void cyberjack_write_bulk_callback (struct urb *urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | .name = "cyberjack", |
| 79 | .probe = usb_serial_probe, |
| 80 | .disconnect = usb_serial_disconnect, |
| 81 | .id_table = id_table, |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 82 | .no_dynamic_id = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 85 | static struct usb_serial_driver cyberjack_device = { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 86 | .driver = { |
| 87 | .owner = THIS_MODULE, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 88 | .name = "cyberjack", |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 89 | }, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 90 | .description = "Reiner SCT Cyberjack USB card reader", |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 91 | .usb_driver = &cyberjack_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | .id_table = id_table, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | .num_ports = 1, |
| 94 | .attach = cyberjack_startup, |
| 95 | .shutdown = cyberjack_shutdown, |
| 96 | .open = cyberjack_open, |
| 97 | .close = cyberjack_close, |
| 98 | .write = cyberjack_write, |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 99 | .write_room = cyberjack_write_room, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | .read_int_callback = cyberjack_read_int_callback, |
| 101 | .read_bulk_callback = cyberjack_read_bulk_callback, |
| 102 | .write_bulk_callback = cyberjack_write_bulk_callback, |
| 103 | }; |
| 104 | |
| 105 | struct cyberjack_private { |
| 106 | spinlock_t lock; /* Lock for SMP */ |
| 107 | short rdtodo; /* Bytes still to read */ |
| 108 | unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */ |
| 109 | short wrfilled; /* Overall data size we already got */ |
| 110 | short wrsent; /* Data already sent */ |
| 111 | }; |
| 112 | |
| 113 | /* do some startup allocations not currently performed by usb_serial_probe() */ |
| 114 | static int cyberjack_startup (struct usb_serial *serial) |
| 115 | { |
| 116 | struct cyberjack_private *priv; |
| 117 | int i; |
| 118 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 119 | dbg("%s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | /* allocate the private data structure */ |
| 122 | priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL); |
| 123 | if (!priv) |
| 124 | return -ENOMEM; |
| 125 | |
| 126 | /* set initial values */ |
| 127 | spin_lock_init(&priv->lock); |
| 128 | priv->rdtodo = 0; |
| 129 | priv->wrfilled = 0; |
| 130 | priv->wrsent = 0; |
| 131 | usb_set_serial_port_data(serial->port[0], priv); |
| 132 | |
| 133 | init_waitqueue_head(&serial->port[0]->write_wait); |
| 134 | |
| 135 | for (i = 0; i < serial->num_ports; ++i) { |
| 136 | int result; |
| 137 | serial->port[i]->interrupt_in_urb->dev = serial->dev; |
| 138 | result = usb_submit_urb(serial->port[i]->interrupt_in_urb, |
| 139 | GFP_KERNEL); |
| 140 | if (result) |
| 141 | err(" usb_submit_urb(read int) failed"); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 142 | dbg("%s - usb_submit_urb(int urb)", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | return( 0 ); |
| 146 | } |
| 147 | |
| 148 | static void cyberjack_shutdown (struct usb_serial *serial) |
| 149 | { |
| 150 | int i; |
| 151 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 152 | dbg("%s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame^] | 154 | for (i = 0; i < serial->num_ports; ++i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | usb_kill_urb(serial->port[i]->interrupt_in_urb); |
| 156 | /* My special items, the standard routines free my urbs */ |
| 157 | kfree(usb_get_serial_port_data(serial->port[i])); |
| 158 | usb_set_serial_port_data(serial->port[i], NULL); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static int cyberjack_open (struct usb_serial_port *port, struct file *filp) |
| 163 | { |
| 164 | struct cyberjack_private *priv; |
| 165 | unsigned long flags; |
| 166 | int result = 0; |
| 167 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 168 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 170 | dbg("%s - usb_clear_halt", __func__ ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | usb_clear_halt(port->serial->dev, port->write_urb->pipe); |
| 172 | |
| 173 | /* force low_latency on so that our tty_push actually forces |
| 174 | * the data through, otherwise it is scheduled, and with high |
| 175 | * data rates (like with OHCI) data can get lost. |
| 176 | */ |
| 177 | port->tty->low_latency = 1; |
| 178 | |
| 179 | priv = usb_get_serial_port_data(port); |
| 180 | spin_lock_irqsave(&priv->lock, flags); |
| 181 | priv->rdtodo = 0; |
| 182 | priv->wrfilled = 0; |
| 183 | priv->wrsent = 0; |
| 184 | spin_unlock_irqrestore(&priv->lock, flags); |
| 185 | |
| 186 | return result; |
| 187 | } |
| 188 | |
| 189 | static void cyberjack_close (struct usb_serial_port *port, struct file *filp) |
| 190 | { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 191 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
| 193 | if (port->serial->dev) { |
| 194 | /* shutdown any bulk reads that might be going on */ |
| 195 | usb_kill_urb(port->write_urb); |
| 196 | usb_kill_urb(port->read_urb); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | static int cyberjack_write (struct usb_serial_port *port, const unsigned char *buf, int count) |
| 201 | { |
| 202 | struct usb_serial *serial = port->serial; |
| 203 | struct cyberjack_private *priv = usb_get_serial_port_data(port); |
| 204 | unsigned long flags; |
| 205 | int result; |
| 206 | int wrexpected; |
| 207 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 208 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | if (count == 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 211 | dbg("%s - write request of 0 bytes", __func__); |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame^] | 212 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Peter Zijlstra | e81ee63 | 2006-09-25 12:51:41 +0200 | [diff] [blame] | 215 | spin_lock_bh(&port->lock); |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 216 | if (port->write_urb_busy) { |
Peter Zijlstra | e81ee63 | 2006-09-25 12:51:41 +0200 | [diff] [blame] | 217 | spin_unlock_bh(&port->lock); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 218 | dbg("%s - already writing", __func__); |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 219 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 221 | port->write_urb_busy = 1; |
Peter Zijlstra | e81ee63 | 2006-09-25 12:51:41 +0200 | [diff] [blame] | 222 | spin_unlock_bh(&port->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | spin_lock_irqsave(&priv->lock, flags); |
| 225 | |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame^] | 226 | if( (count+priv->wrfilled) > sizeof(priv->wrbuf) ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | /* To much data for buffer. Reset buffer. */ |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame^] | 228 | priv->wrfilled = 0; |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 229 | port->write_urb_busy = 0; |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame^] | 230 | spin_unlock_irqrestore(&priv->lock, flags); |
| 231 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* Copy data */ |
| 235 | memcpy (priv->wrbuf+priv->wrfilled, buf, count); |
| 236 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 237 | usb_serial_debug_data(debug, &port->dev, __func__, count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | priv->wrbuf+priv->wrfilled); |
| 239 | priv->wrfilled += count; |
| 240 | |
| 241 | if( priv->wrfilled >= 3 ) { |
| 242 | wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 243 | dbg("%s - expected data: %d", __func__, wrexpected); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } else { |
| 245 | wrexpected = sizeof(priv->wrbuf); |
| 246 | } |
| 247 | |
| 248 | if( priv->wrfilled >= wrexpected ) { |
| 249 | /* We have enough data to begin transmission */ |
| 250 | int length; |
| 251 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 252 | dbg("%s - transmitting data (frame 1)", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected; |
| 254 | |
| 255 | memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length ); |
| 256 | priv->wrsent=length; |
| 257 | |
| 258 | /* set up our urb */ |
| 259 | usb_fill_bulk_urb(port->write_urb, serial->dev, |
| 260 | usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress), |
| 261 | port->write_urb->transfer_buffer, length, |
| 262 | ((serial->type->write_bulk_callback) ? |
| 263 | serial->type->write_bulk_callback : |
| 264 | cyberjack_write_bulk_callback), |
| 265 | port); |
| 266 | |
| 267 | /* send the data out the bulk port */ |
| 268 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
| 269 | if (result) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 270 | err("%s - failed submitting write urb, error %d", __func__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | /* Throw away data. No better idea what to do with it. */ |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame^] | 272 | priv->wrfilled = 0; |
| 273 | priv->wrsent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | spin_unlock_irqrestore(&priv->lock, flags); |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 275 | port->write_urb_busy = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 279 | dbg("%s - priv->wrsent=%d", __func__,priv->wrsent); |
| 280 | dbg("%s - priv->wrfilled=%d", __func__,priv->wrfilled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
| 282 | if( priv->wrsent>=priv->wrfilled ) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 283 | dbg("%s - buffer cleaned", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | memset( priv->wrbuf, 0, sizeof(priv->wrbuf) ); |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame^] | 285 | priv->wrfilled = 0; |
| 286 | priv->wrsent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | spin_unlock_irqrestore(&priv->lock, flags); |
| 291 | |
| 292 | return (count); |
| 293 | } |
| 294 | |
| 295 | static int cyberjack_write_room( struct usb_serial_port *port ) |
| 296 | { |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame^] | 297 | /* FIXME: .... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | return CYBERJACK_LOCAL_BUF_SIZE; |
| 299 | } |
| 300 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 301 | static void cyberjack_read_int_callback( struct urb *urb ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Greg Kroah-Hartman | 7dcc85c | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 306 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | int result; |
| 308 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 309 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | /* the urb might have been killed. */ |
Greg Kroah-Hartman | 7dcc85c | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 312 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | return; |
| 314 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 315 | usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | /* React only to interrupts signaling a bulk_in transfer */ |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame^] | 318 | if( (urb->actual_length == 4) && (data[0] == 0x01) ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | short old_rdtodo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 337 | dbg("%s - rdtodo: %d", __func__, priv->rdtodo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 ) |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 345 | err("%s - failed resubmitting read urb, error %d", __func__, result); |
| 346 | dbg("%s - usb_submit_urb(read urb)", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 355 | dbg("%s - usb_submit_urb(int urb)", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } |
| 357 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 358 | static void cyberjack_read_bulk_callback (struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | int result; |
Greg Kroah-Hartman | 7dcc85c | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 366 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 368 | dbg("%s - port %d", __func__, port->number); |
Greg Kroah-Hartman | 7dcc85c | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 369 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 370 | usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length, data); |
Greg Kroah-Hartman | 7dcc85c | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 371 | if (status) { |
| 372 | dbg("%s - nonzero read bulk status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 373 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | return; |
| 375 | } |
| 376 | |
| 377 | tty = port->tty; |
| 378 | if (!tty) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 379 | dbg("%s - ignoring since device not open\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | return; |
| 381 | } |
| 382 | if (urb->actual_length) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 383 | tty_buffer_request_room(tty, urb->actual_length); |
| 384 | tty_insert_flip_string(tty, data, urb->actual_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | tty_flip_buffer_push(tty); |
| 386 | } |
| 387 | |
| 388 | spin_lock(&priv->lock); |
| 389 | |
| 390 | /* Reduce urbs to do by one. */ |
| 391 | priv->rdtodo-=urb->actual_length; |
| 392 | /* Just to be sure */ |
| 393 | if ( priv->rdtodo<0 ) priv->rdtodo = 0; |
| 394 | todo = priv->rdtodo; |
| 395 | |
| 396 | spin_unlock(&priv->lock); |
| 397 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 398 | dbg("%s - rdtodo: %d", __func__, todo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
| 400 | /* Continue to read if we have still urbs to do. */ |
| 401 | if( todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/ ) { |
| 402 | port->read_urb->dev = port->serial->dev; |
| 403 | result = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
| 404 | if (result) |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 405 | err("%s - failed resubmitting read urb, error %d", __func__, result); |
| 406 | dbg("%s - usb_submit_urb(read urb)", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 410 | static void cyberjack_write_bulk_callback (struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
| 412 | struct usb_serial_port *port = (struct usb_serial_port *)urb->context; |
| 413 | struct cyberjack_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 7dcc85c | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 414 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 416 | dbg("%s - port %d", __func__, port->number); |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 417 | |
| 418 | port->write_urb_busy = 0; |
Greg Kroah-Hartman | 7dcc85c | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 419 | if (status) { |
| 420 | dbg("%s - nonzero write bulk status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 421 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
| 425 | spin_lock(&priv->lock); |
| 426 | |
| 427 | /* only do something if we have more data to send */ |
| 428 | if( priv->wrfilled ) { |
| 429 | int length, blksize, result; |
| 430 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 431 | dbg("%s - transmitting data (frame n)", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ? |
| 434 | port->bulk_out_size : (priv->wrfilled - priv->wrsent); |
| 435 | |
| 436 | memcpy (port->write_urb->transfer_buffer, priv->wrbuf + priv->wrsent, |
| 437 | length ); |
| 438 | priv->wrsent+=length; |
| 439 | |
| 440 | /* set up our urb */ |
| 441 | usb_fill_bulk_urb(port->write_urb, port->serial->dev, |
| 442 | usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress), |
| 443 | port->write_urb->transfer_buffer, length, |
| 444 | ((port->serial->type->write_bulk_callback) ? |
| 445 | port->serial->type->write_bulk_callback : |
| 446 | cyberjack_write_bulk_callback), |
| 447 | port); |
| 448 | |
| 449 | /* send the data out the bulk port */ |
| 450 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
| 451 | if (result) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 452 | err("%s - failed submitting write urb, error %d", __func__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | /* Throw away data. No better idea what to do with it. */ |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame^] | 454 | priv->wrfilled = 0; |
| 455 | priv->wrsent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | goto exit; |
| 457 | } |
| 458 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 459 | dbg("%s - priv->wrsent=%d", __func__,priv->wrsent); |
| 460 | dbg("%s - priv->wrfilled=%d", __func__,priv->wrfilled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
| 462 | blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3; |
| 463 | |
| 464 | if( (priv->wrsent>=priv->wrfilled) || (priv->wrsent>=blksize) ) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 465 | dbg("%s - buffer cleaned", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | memset( priv->wrbuf, 0, sizeof(priv->wrbuf) ); |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame^] | 467 | priv->wrfilled = 0; |
| 468 | priv->wrsent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
| 472 | exit: |
| 473 | spin_unlock(&priv->lock); |
Pete Zaitcev | cf2c748 | 2006-05-22 21:58:49 -0700 | [diff] [blame] | 474 | usb_serial_port_softint(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static int __init cyberjack_init (void) |
| 478 | { |
| 479 | int retval; |
| 480 | retval = usb_serial_register(&cyberjack_device); |
| 481 | if (retval) |
| 482 | goto failed_usb_serial_register; |
| 483 | retval = usb_register(&cyberjack_driver); |
| 484 | if (retval) |
| 485 | goto failed_usb_register; |
| 486 | |
| 487 | info(DRIVER_VERSION " " DRIVER_AUTHOR); |
| 488 | info(DRIVER_DESC); |
| 489 | |
| 490 | return 0; |
| 491 | failed_usb_register: |
| 492 | usb_serial_deregister(&cyberjack_device); |
| 493 | failed_usb_serial_register: |
| 494 | return retval; |
| 495 | } |
| 496 | |
| 497 | static void __exit cyberjack_exit (void) |
| 498 | { |
| 499 | usb_deregister (&cyberjack_driver); |
| 500 | usb_serial_deregister (&cyberjack_device); |
| 501 | } |
| 502 | |
| 503 | module_init(cyberjack_init); |
| 504 | module_exit(cyberjack_exit); |
| 505 | |
| 506 | MODULE_AUTHOR( DRIVER_AUTHOR ); |
| 507 | MODULE_DESCRIPTION( DRIVER_DESC ); |
| 508 | MODULE_VERSION( DRIVER_VERSION ); |
| 509 | MODULE_LICENSE("GPL"); |
| 510 | |
| 511 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 512 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |