Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * USB Serial Converter Generic functions |
| 3 | * |
| 4 | * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License version |
| 8 | * 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/tty.h> |
| 16 | #include <linux/tty_flip.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/moduleparam.h> |
| 19 | #include <linux/usb.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 20 | #include <linux/usb/serial.h> |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 21 | #include <linux/uaccess.h> |
David VomLehn | 8e8dce0 | 2009-08-28 12:54:27 -0700 | [diff] [blame^] | 22 | #include <linux/kfifo.h> |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | static int debug; |
| 25 | |
| 26 | #ifdef CONFIG_USB_SERIAL_GENERIC |
David Brownell | b46d60f | 2007-03-23 12:51:55 -0700 | [diff] [blame] | 27 | |
| 28 | static int generic_probe(struct usb_interface *interface, |
| 29 | const struct usb_device_id *id); |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | static __u16 vendor = 0x05f9; |
| 32 | static __u16 product = 0xffff; |
| 33 | |
| 34 | module_param(vendor, ushort, 0); |
| 35 | MODULE_PARM_DESC(vendor, "User specified USB idVendor"); |
| 36 | |
| 37 | module_param(product, ushort, 0); |
| 38 | MODULE_PARM_DESC(product, "User specified USB idProduct"); |
| 39 | |
| 40 | static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */ |
| 41 | |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 42 | /* we want to look at all devices, as the vendor/product id can change |
| 43 | * depending on the command line argument */ |
| 44 | static struct usb_device_id generic_serial_ids[] = { |
| 45 | {.driver_info = 42}, |
| 46 | {} |
| 47 | }; |
| 48 | |
| 49 | static struct usb_driver generic_driver = { |
| 50 | .name = "usbserial_generic", |
| 51 | .probe = generic_probe, |
| 52 | .disconnect = usb_serial_disconnect, |
| 53 | .id_table = generic_serial_ids, |
| 54 | .no_dynamic_id = 1, |
| 55 | }; |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* All of the device info needed for the Generic Serial Converter */ |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 58 | struct usb_serial_driver usb_serial_generic_device = { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 59 | .driver = { |
| 60 | .owner = THIS_MODULE, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 61 | .name = "generic", |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 62 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | .id_table = generic_device_ids, |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 64 | .usb_driver = &generic_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | .num_ports = 1, |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 66 | .disconnect = usb_serial_generic_disconnect, |
| 67 | .release = usb_serial_generic_release, |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 68 | .throttle = usb_serial_generic_throttle, |
| 69 | .unthrottle = usb_serial_generic_unthrottle, |
Oliver Neukum | ec22559 | 2007-04-27 20:54:57 +0200 | [diff] [blame] | 70 | .resume = usb_serial_generic_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | static int generic_probe(struct usb_interface *interface, |
| 74 | const struct usb_device_id *id) |
| 75 | { |
| 76 | const struct usb_device_id *id_pattern; |
| 77 | |
| 78 | id_pattern = usb_match_id(interface, generic_device_ids); |
| 79 | if (id_pattern != NULL) |
| 80 | return usb_serial_probe(interface, id); |
| 81 | return -ENODEV; |
| 82 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #endif |
| 84 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 85 | int usb_serial_generic_register(int _debug) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
| 87 | int retval = 0; |
| 88 | |
| 89 | debug = _debug; |
| 90 | #ifdef CONFIG_USB_SERIAL_GENERIC |
| 91 | generic_device_ids[0].idVendor = vendor; |
| 92 | generic_device_ids[0].idProduct = product; |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 93 | generic_device_ids[0].match_flags = |
| 94 | USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | /* register our generic driver with ourselves */ |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 97 | retval = usb_serial_register(&usb_serial_generic_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | if (retval) |
| 99 | goto exit; |
| 100 | retval = usb_register(&generic_driver); |
| 101 | if (retval) |
| 102 | usb_serial_deregister(&usb_serial_generic_device); |
| 103 | exit: |
| 104 | #endif |
| 105 | return retval; |
| 106 | } |
| 107 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 108 | void usb_serial_generic_deregister(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
| 110 | #ifdef CONFIG_USB_SERIAL_GENERIC |
| 111 | /* remove our generic driver */ |
| 112 | usb_deregister(&generic_driver); |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 113 | usb_serial_deregister(&usb_serial_generic_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | #endif |
| 115 | } |
| 116 | |
Alan Cox | a509a7e | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 117 | int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
| 119 | struct usb_serial *serial = port->serial; |
| 120 | int result = 0; |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 121 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 123 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 125 | /* clear the throttle flags */ |
| 126 | spin_lock_irqsave(&port->lock, flags); |
| 127 | port->throttled = 0; |
| 128 | port->throttle_req = 0; |
| 129 | spin_unlock_irqrestore(&port->lock, flags); |
| 130 | |
| 131 | /* if we have a bulk endpoint, start reading from it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | if (serial->num_bulk_in) { |
| 133 | /* Start reading from the device */ |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 134 | usb_fill_bulk_urb(port->read_urb, serial->dev, |
| 135 | usb_rcvbulkpipe(serial->dev, |
| 136 | port->bulk_in_endpointAddress), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | port->read_urb->transfer_buffer, |
| 138 | port->read_urb->transfer_buffer_length, |
| 139 | ((serial->type->read_bulk_callback) ? |
| 140 | serial->type->read_bulk_callback : |
| 141 | usb_serial_generic_read_bulk_callback), |
| 142 | port); |
| 143 | result = usb_submit_urb(port->read_urb, GFP_KERNEL); |
| 144 | if (result) |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 145 | dev_err(&port->dev, |
| 146 | "%s - failed resubmitting read urb, error %d\n", |
| 147 | __func__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return result; |
| 151 | } |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 152 | EXPORT_SYMBOL_GPL(usb_serial_generic_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 154 | static void generic_cleanup(struct usb_serial_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | { |
| 156 | struct usb_serial *serial = port->serial; |
| 157 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 158 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | if (serial->dev) { |
| 161 | /* shutdown any bulk reads that might be going on */ |
| 162 | if (serial->num_bulk_out) |
| 163 | usb_kill_urb(port->write_urb); |
| 164 | if (serial->num_bulk_in) |
| 165 | usb_kill_urb(port->read_urb); |
| 166 | } |
| 167 | } |
| 168 | |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 169 | void usb_serial_generic_close(struct usb_serial_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 171 | dbg("%s - port %d", __func__, port->number); |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 172 | generic_cleanup(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 175 | static int usb_serial_multi_urb_write(struct tty_struct *tty, |
| 176 | struct usb_serial_port *port, const unsigned char *buf, int count) |
| 177 | { |
| 178 | unsigned long flags; |
| 179 | struct urb *urb; |
| 180 | unsigned char *buffer; |
| 181 | int status; |
| 182 | int towrite; |
| 183 | int bwrite = 0; |
| 184 | |
| 185 | dbg("%s - port %d", __func__, port->number); |
| 186 | |
| 187 | if (count == 0) |
| 188 | dbg("%s - write request of 0 bytes", __func__); |
| 189 | |
| 190 | while (count > 0) { |
| 191 | towrite = (count > port->bulk_out_size) ? |
| 192 | port->bulk_out_size : count; |
| 193 | spin_lock_irqsave(&port->lock, flags); |
| 194 | if (port->urbs_in_flight > |
| 195 | port->serial->type->max_in_flight_urbs) { |
| 196 | spin_unlock_irqrestore(&port->lock, flags); |
| 197 | dbg("%s - write limit hit\n", __func__); |
| 198 | return bwrite; |
| 199 | } |
| 200 | port->tx_bytes_flight += towrite; |
| 201 | port->urbs_in_flight++; |
| 202 | spin_unlock_irqrestore(&port->lock, flags); |
| 203 | |
| 204 | buffer = kmalloc(towrite, GFP_ATOMIC); |
| 205 | if (!buffer) { |
| 206 | dev_err(&port->dev, |
| 207 | "%s ran out of kernel memory for urb ...\n", __func__); |
| 208 | goto error_no_buffer; |
| 209 | } |
| 210 | |
| 211 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 212 | if (!urb) { |
| 213 | dev_err(&port->dev, "%s - no more free urbs\n", |
| 214 | __func__); |
| 215 | goto error_no_urb; |
| 216 | } |
| 217 | |
| 218 | /* Copy data */ |
| 219 | memcpy(buffer, buf + bwrite, towrite); |
| 220 | usb_serial_debug_data(debug, &port->dev, __func__, |
| 221 | towrite, buffer); |
| 222 | /* fill the buffer and send it */ |
| 223 | usb_fill_bulk_urb(urb, port->serial->dev, |
| 224 | usb_sndbulkpipe(port->serial->dev, |
| 225 | port->bulk_out_endpointAddress), |
| 226 | buffer, towrite, |
| 227 | usb_serial_generic_write_bulk_callback, port); |
| 228 | |
| 229 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 230 | if (status) { |
| 231 | dev_err(&port->dev, |
| 232 | "%s - failed submitting write urb, error %d\n", |
| 233 | __func__, status); |
| 234 | goto error; |
| 235 | } |
| 236 | |
| 237 | /* This urb is the responsibility of the host driver now */ |
| 238 | usb_free_urb(urb); |
| 239 | dbg("%s write: %d", __func__, towrite); |
| 240 | count -= towrite; |
| 241 | bwrite += towrite; |
| 242 | } |
| 243 | return bwrite; |
| 244 | |
| 245 | error: |
| 246 | usb_free_urb(urb); |
| 247 | error_no_urb: |
| 248 | kfree(buffer); |
| 249 | error_no_buffer: |
| 250 | spin_lock_irqsave(&port->lock, flags); |
| 251 | port->urbs_in_flight--; |
| 252 | port->tx_bytes_flight -= towrite; |
| 253 | spin_unlock_irqrestore(&port->lock, flags); |
| 254 | return bwrite; |
| 255 | } |
| 256 | |
David VomLehn | 8e8dce0 | 2009-08-28 12:54:27 -0700 | [diff] [blame^] | 257 | /** |
| 258 | * usb_serial_generic_write_start - kick off an URB write |
| 259 | * @port: Pointer to the &struct usb_serial_port data |
| 260 | * |
| 261 | * Returns the number of bytes queued on success. This will be zero if there |
| 262 | * was nothing to send. Otherwise, it returns a negative errno value |
| 263 | */ |
| 264 | static int usb_serial_generic_write_start(struct usb_serial_port *port) |
| 265 | { |
| 266 | struct usb_serial *serial = port->serial; |
| 267 | unsigned char *data; |
| 268 | int result; |
| 269 | int count; |
| 270 | unsigned long flags; |
| 271 | bool start_io; |
| 272 | |
| 273 | /* Atomically determine whether we can and need to start a USB |
| 274 | * operation. */ |
| 275 | spin_lock_irqsave(&port->lock, flags); |
| 276 | if (port->write_urb_busy) |
| 277 | start_io = false; |
| 278 | else { |
| 279 | start_io = (__kfifo_len(port->write_fifo) != 0); |
| 280 | port->write_urb_busy = start_io; |
| 281 | } |
| 282 | spin_unlock_irqrestore(&port->lock, flags); |
| 283 | |
| 284 | if (!start_io) |
| 285 | return 0; |
| 286 | |
| 287 | data = port->write_urb->transfer_buffer; |
| 288 | count = kfifo_get(port->write_fifo, data, port->bulk_out_size); |
| 289 | usb_serial_debug_data(debug, &port->dev, __func__, count, data); |
| 290 | |
| 291 | /* set up our urb */ |
| 292 | usb_fill_bulk_urb(port->write_urb, serial->dev, |
| 293 | usb_sndbulkpipe(serial->dev, |
| 294 | port->bulk_out_endpointAddress), |
| 295 | port->write_urb->transfer_buffer, count, |
| 296 | ((serial->type->write_bulk_callback) ? |
| 297 | serial->type->write_bulk_callback : |
| 298 | usb_serial_generic_write_bulk_callback), |
| 299 | port); |
| 300 | |
| 301 | /* send the data out the bulk port */ |
| 302 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
| 303 | if (result) { |
| 304 | dev_err(&port->dev, |
| 305 | "%s - failed submitting write urb, error %d\n", |
| 306 | __func__, result); |
| 307 | /* don't have to grab the lock here, as we will |
| 308 | retry if != 0 */ |
| 309 | port->write_urb_busy = 0; |
| 310 | } else |
| 311 | result = count; |
| 312 | |
| 313 | return result; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * usb_serial_generic_write - generic write function for serial USB devices |
| 318 | * @tty: Pointer to &struct tty_struct for the device |
| 319 | * @port: Pointer to the &usb_serial_port structure for the device |
| 320 | * @buf: Pointer to the data to write |
| 321 | * @count: Number of bytes to write |
| 322 | * |
| 323 | * Returns the number of characters actually written, which may be anything |
| 324 | * from zero to @count. If an error occurs, it returns the negative errno |
| 325 | * value. |
| 326 | */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 327 | int usb_serial_generic_write(struct tty_struct *tty, |
| 328 | struct usb_serial_port *port, const unsigned char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
| 330 | struct usb_serial *serial = port->serial; |
| 331 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 333 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | if (count == 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 336 | dbg("%s - write request of 0 bytes", __func__); |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 337 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /* only do something if we have a bulk out endpoint */ |
David VomLehn | 8e8dce0 | 2009-08-28 12:54:27 -0700 | [diff] [blame^] | 341 | if (!serial->num_bulk_out) |
| 342 | return 0; |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 343 | |
David VomLehn | 8e8dce0 | 2009-08-28 12:54:27 -0700 | [diff] [blame^] | 344 | if (serial->type->max_in_flight_urbs) |
| 345 | return usb_serial_multi_urb_write(tty, port, |
| 346 | buf, count); |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 347 | |
David VomLehn | 8e8dce0 | 2009-08-28 12:54:27 -0700 | [diff] [blame^] | 348 | count = kfifo_put(port->write_fifo, buf, count); |
| 349 | result = usb_serial_generic_write_start(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
David VomLehn | 8e8dce0 | 2009-08-28 12:54:27 -0700 | [diff] [blame^] | 351 | if (result >= 0) |
| 352 | result = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
David VomLehn | 8e8dce0 | 2009-08-28 12:54:27 -0700 | [diff] [blame^] | 354 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 356 | EXPORT_SYMBOL_GPL(usb_serial_generic_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 358 | int usb_serial_generic_write_room(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 360 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | struct usb_serial *serial = port->serial; |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 362 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | int room = 0; |
| 364 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 365 | dbg("%s - port %d", __func__, port->number); |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 366 | spin_lock_irqsave(&port->lock, flags); |
| 367 | if (serial->type->max_in_flight_urbs) { |
| 368 | if (port->urbs_in_flight < serial->type->max_in_flight_urbs) |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 369 | room = port->bulk_out_size * |
| 370 | (serial->type->max_in_flight_urbs - |
| 371 | port->urbs_in_flight); |
David VomLehn | 8e8dce0 | 2009-08-28 12:54:27 -0700 | [diff] [blame^] | 372 | } else if (serial->num_bulk_out) |
| 373 | room = port->write_fifo->size - __kfifo_len(port->write_fifo); |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 374 | spin_unlock_irqrestore(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 376 | dbg("%s - returns %d", __func__, room); |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 377 | return room; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 380 | int usb_serial_generic_chars_in_buffer(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 382 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | struct usb_serial *serial = port->serial; |
| 384 | int chars = 0; |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 385 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 387 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 389 | if (serial->type->max_in_flight_urbs) { |
| 390 | spin_lock_irqsave(&port->lock, flags); |
| 391 | chars = port->tx_bytes_flight; |
| 392 | spin_unlock_irqrestore(&port->lock, flags); |
David VomLehn | 8e8dce0 | 2009-08-28 12:54:27 -0700 | [diff] [blame^] | 393 | } else if (serial->num_bulk_out) |
| 394 | chars = kfifo_len(port->write_fifo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 396 | dbg("%s - returns %d", __func__, chars); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 397 | return chars; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 400 | |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 401 | void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port, |
| 402 | gfp_t mem_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 404 | struct urb *urb = port->read_urb; |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 405 | struct usb_serial *serial = port->serial; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | int result; |
| 407 | |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 408 | /* Continue reading from device */ |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 409 | usb_fill_bulk_urb(urb, serial->dev, |
| 410 | usb_rcvbulkpipe(serial->dev, |
| 411 | port->bulk_in_endpointAddress), |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 412 | urb->transfer_buffer, |
| 413 | urb->transfer_buffer_length, |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 414 | ((serial->type->read_bulk_callback) ? |
| 415 | serial->type->read_bulk_callback : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | usb_serial_generic_read_bulk_callback), port); |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 417 | result = usb_submit_urb(urb, mem_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | if (result) |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 419 | dev_err(&port->dev, |
| 420 | "%s - failed resubmitting read urb, error %d\n", |
| 421 | __func__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 423 | EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 424 | |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 425 | /* Push data to tty layer and resubmit the bulk read URB */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 426 | static void flush_and_resubmit_read_urb(struct usb_serial_port *port) |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 427 | { |
| 428 | struct urb *urb = port->read_urb; |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 429 | struct tty_struct *tty = tty_port_tty_get(&port->port); |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 430 | char *ch = (char *)urb->transfer_buffer; |
| 431 | int i; |
| 432 | |
| 433 | if (!tty) |
| 434 | goto done; |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 435 | |
Alan Cox | 4cd1de0 | 2009-07-09 13:35:52 +0100 | [diff] [blame] | 436 | /* The per character mucking around with sysrq path it too slow for |
| 437 | stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases |
| 438 | where the USB serial is not a console anyway */ |
| 439 | if (!port->console || !port->sysrq) |
| 440 | tty_insert_flip_string(tty, ch, urb->actual_length); |
| 441 | else { |
| 442 | /* Push data to tty */ |
| 443 | for (i = 0; i < urb->actual_length; i++, ch++) { |
Alan Cox | 24a15a6 | 2009-07-09 13:36:22 +0100 | [diff] [blame] | 444 | if (!usb_serial_handle_sysrq_char(tty, port, *ch)) |
Alan Cox | 4cd1de0 | 2009-07-09 13:35:52 +0100 | [diff] [blame] | 445 | tty_insert_flip_char(tty, *ch, TTY_NORMAL); |
| 446 | } |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 447 | } |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 448 | tty_flip_buffer_push(tty); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 449 | tty_kref_put(tty); |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 450 | done: |
| 451 | usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC); |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 454 | void usb_serial_generic_read_bulk_callback(struct urb *urb) |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 455 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 456 | struct usb_serial_port *port = urb->context; |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 457 | unsigned char *data = urb->transfer_buffer; |
Greg Kroah-Hartman | fbd27225 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 458 | int status = urb->status; |
Borislav Petkov | bfaeafc | 2007-10-28 13:24:16 +0100 | [diff] [blame] | 459 | unsigned long flags; |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 460 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 461 | dbg("%s - port %d", __func__, port->number); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 462 | |
Greg Kroah-Hartman | fbd27225 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 463 | if (unlikely(status != 0)) { |
| 464 | dbg("%s - nonzero read bulk status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 465 | __func__, status); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 466 | return; |
| 467 | } |
| 468 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 469 | usb_serial_debug_data(debug, &port->dev, __func__, |
| 470 | urb->actual_length, data); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 471 | |
| 472 | /* Throttle the device if requested by tty */ |
Borislav Petkov | bfaeafc | 2007-10-28 13:24:16 +0100 | [diff] [blame] | 473 | spin_lock_irqsave(&port->lock, flags); |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 474 | port->throttled = port->throttle_req; |
| 475 | if (!port->throttled) { |
Pete Zaitcev | b507cc9 | 2008-03-04 23:28:42 -0800 | [diff] [blame] | 476 | spin_unlock_irqrestore(&port->lock, flags); |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 477 | flush_and_resubmit_read_urb(port); |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 478 | } else |
Pete Zaitcev | b507cc9 | 2008-03-04 23:28:42 -0800 | [diff] [blame] | 479 | spin_unlock_irqrestore(&port->lock, flags); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 480 | } |
Luiz Fernando N. Capitulino | 166ffcc | 2006-07-11 14:19:25 -0300 | [diff] [blame] | 481 | EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 483 | void usb_serial_generic_write_bulk_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 485 | unsigned long flags; |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 486 | struct usb_serial_port *port = urb->context; |
Greg Kroah-Hartman | fbd27225 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 487 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 489 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 491 | if (port->serial->type->max_in_flight_urbs) { |
| 492 | spin_lock_irqsave(&port->lock, flags); |
| 493 | --port->urbs_in_flight; |
| 494 | port->tx_bytes_flight -= urb->transfer_buffer_length; |
| 495 | if (port->urbs_in_flight < 0) |
| 496 | port->urbs_in_flight = 0; |
| 497 | spin_unlock_irqrestore(&port->lock, flags); |
David VomLehn | 8e8dce0 | 2009-08-28 12:54:27 -0700 | [diff] [blame^] | 498 | |
| 499 | if (status) { |
| 500 | dbg("%s - nonzero multi-urb write bulk status " |
| 501 | "received: %d", __func__, status); |
| 502 | return; |
| 503 | } |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 504 | } else { |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 505 | port->write_urb_busy = 0; |
David VomLehn | 8e8dce0 | 2009-08-28 12:54:27 -0700 | [diff] [blame^] | 506 | |
| 507 | if (status) { |
| 508 | dbg("%s - nonzero multi-urb write bulk status " |
| 509 | "received: %d", __func__, status); |
| 510 | kfifo_reset(port->write_fifo); |
| 511 | } else |
| 512 | usb_serial_generic_write_start(port); |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 513 | } |
| 514 | |
Pete Zaitcev | cf2c748 | 2006-05-22 21:58:49 -0700 | [diff] [blame] | 515 | usb_serial_port_softint(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
Greg Kroah-Hartman | bb83398 | 2005-11-17 09:48:18 -0800 | [diff] [blame] | 517 | EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 519 | void usb_serial_generic_throttle(struct tty_struct *tty) |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 520 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 521 | struct usb_serial_port *port = tty->driver_data; |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 522 | unsigned long flags; |
| 523 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 524 | dbg("%s - port %d", __func__, port->number); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 525 | |
| 526 | /* Set the throttle request flag. It will be picked up |
| 527 | * by usb_serial_generic_read_bulk_callback(). */ |
| 528 | spin_lock_irqsave(&port->lock, flags); |
| 529 | port->throttle_req = 1; |
| 530 | spin_unlock_irqrestore(&port->lock, flags); |
| 531 | } |
| 532 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 533 | void usb_serial_generic_unthrottle(struct tty_struct *tty) |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 534 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 535 | struct usb_serial_port *port = tty->driver_data; |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 536 | int was_throttled; |
| 537 | unsigned long flags; |
| 538 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 539 | dbg("%s - port %d", __func__, port->number); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 540 | |
| 541 | /* Clear the throttle flags */ |
| 542 | spin_lock_irqsave(&port->lock, flags); |
| 543 | was_throttled = port->throttled; |
| 544 | port->throttled = port->throttle_req = 0; |
| 545 | spin_unlock_irqrestore(&port->lock, flags); |
| 546 | |
| 547 | if (was_throttled) { |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 548 | /* Resume reading from device */ |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 549 | usb_serial_generic_resubmit_read_urb(port, GFP_KERNEL); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Alan Cox | 24a15a6 | 2009-07-09 13:36:22 +0100 | [diff] [blame] | 553 | int usb_serial_handle_sysrq_char(struct tty_struct *tty, |
| 554 | struct usb_serial_port *port, unsigned int ch) |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 555 | { |
Jason Wessel | 568d422 | 2009-05-29 13:34:17 -0500 | [diff] [blame] | 556 | if (port->sysrq && port->console) { |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 557 | if (ch && time_before(jiffies, port->sysrq)) { |
Alan Cox | 24a15a6 | 2009-07-09 13:36:22 +0100 | [diff] [blame] | 558 | handle_sysrq(ch, tty); |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 559 | port->sysrq = 0; |
| 560 | return 1; |
| 561 | } |
| 562 | port->sysrq = 0; |
| 563 | } |
| 564 | return 0; |
| 565 | } |
| 566 | EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char); |
| 567 | |
| 568 | int usb_serial_handle_break(struct usb_serial_port *port) |
| 569 | { |
| 570 | if (!port->sysrq) { |
| 571 | port->sysrq = jiffies + HZ*5; |
| 572 | return 1; |
| 573 | } |
| 574 | port->sysrq = 0; |
| 575 | return 0; |
| 576 | } |
| 577 | EXPORT_SYMBOL_GPL(usb_serial_handle_break); |
| 578 | |
David VomLehn | 8e8dce0 | 2009-08-28 12:54:27 -0700 | [diff] [blame^] | 579 | int usb_serial_generic_resume(struct usb_serial *serial) |
| 580 | { |
| 581 | struct usb_serial_port *port; |
| 582 | int i, c = 0, r; |
| 583 | |
| 584 | for (i = 0; i < serial->num_ports; i++) { |
| 585 | port = serial->port[i]; |
| 586 | if (!port->port.count) |
| 587 | continue; |
| 588 | |
| 589 | if (port->read_urb) { |
| 590 | r = usb_submit_urb(port->read_urb, GFP_NOIO); |
| 591 | if (r < 0) |
| 592 | c++; |
| 593 | } |
| 594 | |
| 595 | if (port->write_urb) { |
| 596 | r = usb_serial_generic_write_start(port); |
| 597 | if (r < 0) |
| 598 | c++; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | return c ? -EIO : 0; |
| 603 | } |
| 604 | EXPORT_SYMBOL_GPL(usb_serial_generic_resume); |
| 605 | |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 606 | void usb_serial_generic_disconnect(struct usb_serial *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
| 608 | int i; |
| 609 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 610 | dbg("%s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
| 612 | /* stop reads and writes on all ports */ |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 613 | for (i = 0; i < serial->num_ports; ++i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | generic_cleanup(serial->port[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 617 | void usb_serial_generic_release(struct usb_serial *serial) |
| 618 | { |
| 619 | dbg("%s", __func__); |
| 620 | } |