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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
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 | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 117 | int usb_serial_generic_open(struct tty_struct *tty, |
| 118 | struct usb_serial_port *port, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
| 120 | struct usb_serial *serial = port->serial; |
| 121 | int result = 0; |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 122 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 124 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 126 | /* clear the throttle flags */ |
| 127 | spin_lock_irqsave(&port->lock, flags); |
| 128 | port->throttled = 0; |
| 129 | port->throttle_req = 0; |
| 130 | spin_unlock_irqrestore(&port->lock, flags); |
| 131 | |
| 132 | /* if we have a bulk endpoint, start reading from it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | if (serial->num_bulk_in) { |
| 134 | /* Start reading from the device */ |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 135 | usb_fill_bulk_urb(port->read_urb, serial->dev, |
| 136 | usb_rcvbulkpipe(serial->dev, |
| 137 | port->bulk_in_endpointAddress), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | port->read_urb->transfer_buffer, |
| 139 | port->read_urb->transfer_buffer_length, |
| 140 | ((serial->type->read_bulk_callback) ? |
| 141 | serial->type->read_bulk_callback : |
| 142 | usb_serial_generic_read_bulk_callback), |
| 143 | port); |
| 144 | result = usb_submit_urb(port->read_urb, GFP_KERNEL); |
| 145 | if (result) |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 146 | dev_err(&port->dev, |
| 147 | "%s - failed resubmitting read urb, error %d\n", |
| 148 | __func__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | return result; |
| 152 | } |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 153 | EXPORT_SYMBOL_GPL(usb_serial_generic_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 155 | static void generic_cleanup(struct usb_serial_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
| 157 | struct usb_serial *serial = port->serial; |
| 158 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 159 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | if (serial->dev) { |
| 162 | /* shutdown any bulk reads that might be going on */ |
| 163 | if (serial->num_bulk_out) |
| 164 | usb_kill_urb(port->write_urb); |
| 165 | if (serial->num_bulk_in) |
| 166 | usb_kill_urb(port->read_urb); |
| 167 | } |
| 168 | } |
| 169 | |
Oliver Neukum | ec22559 | 2007-04-27 20:54:57 +0200 | [diff] [blame] | 170 | int usb_serial_generic_resume(struct usb_serial *serial) |
| 171 | { |
| 172 | struct usb_serial_port *port; |
| 173 | int i, c = 0, r; |
| 174 | |
| 175 | for (i = 0; i < serial->num_ports; i++) { |
| 176 | port = serial->port[i]; |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 177 | if (port->port.count && port->read_urb) { |
Oliver Neukum | ec22559 | 2007-04-27 20:54:57 +0200 | [diff] [blame] | 178 | r = usb_submit_urb(port->read_urb, GFP_NOIO); |
| 179 | if (r < 0) |
| 180 | c++; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return c ? -EIO : 0; |
| 185 | } |
Oliver Neukum | 81d043c | 2009-02-06 15:37:14 +0100 | [diff] [blame] | 186 | EXPORT_SYMBOL_GPL(usb_serial_generic_resume); |
Oliver Neukum | ec22559 | 2007-04-27 20:54:57 +0200 | [diff] [blame] | 187 | |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 188 | void usb_serial_generic_close(struct usb_serial_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 190 | dbg("%s - port %d", __func__, port->number); |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 191 | generic_cleanup(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 194 | static int usb_serial_multi_urb_write(struct tty_struct *tty, |
| 195 | struct usb_serial_port *port, const unsigned char *buf, int count) |
| 196 | { |
| 197 | unsigned long flags; |
| 198 | struct urb *urb; |
| 199 | unsigned char *buffer; |
| 200 | int status; |
| 201 | int towrite; |
| 202 | int bwrite = 0; |
| 203 | |
| 204 | dbg("%s - port %d", __func__, port->number); |
| 205 | |
| 206 | if (count == 0) |
| 207 | dbg("%s - write request of 0 bytes", __func__); |
| 208 | |
| 209 | while (count > 0) { |
| 210 | towrite = (count > port->bulk_out_size) ? |
| 211 | port->bulk_out_size : count; |
| 212 | spin_lock_irqsave(&port->lock, flags); |
| 213 | if (port->urbs_in_flight > |
| 214 | port->serial->type->max_in_flight_urbs) { |
| 215 | spin_unlock_irqrestore(&port->lock, flags); |
| 216 | dbg("%s - write limit hit\n", __func__); |
| 217 | return bwrite; |
| 218 | } |
| 219 | port->tx_bytes_flight += towrite; |
| 220 | port->urbs_in_flight++; |
| 221 | spin_unlock_irqrestore(&port->lock, flags); |
| 222 | |
| 223 | buffer = kmalloc(towrite, GFP_ATOMIC); |
| 224 | if (!buffer) { |
| 225 | dev_err(&port->dev, |
| 226 | "%s ran out of kernel memory for urb ...\n", __func__); |
| 227 | goto error_no_buffer; |
| 228 | } |
| 229 | |
| 230 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 231 | if (!urb) { |
| 232 | dev_err(&port->dev, "%s - no more free urbs\n", |
| 233 | __func__); |
| 234 | goto error_no_urb; |
| 235 | } |
| 236 | |
| 237 | /* Copy data */ |
| 238 | memcpy(buffer, buf + bwrite, towrite); |
| 239 | usb_serial_debug_data(debug, &port->dev, __func__, |
| 240 | towrite, buffer); |
| 241 | /* fill the buffer and send it */ |
| 242 | usb_fill_bulk_urb(urb, port->serial->dev, |
| 243 | usb_sndbulkpipe(port->serial->dev, |
| 244 | port->bulk_out_endpointAddress), |
| 245 | buffer, towrite, |
| 246 | usb_serial_generic_write_bulk_callback, port); |
| 247 | |
| 248 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 249 | if (status) { |
| 250 | dev_err(&port->dev, |
| 251 | "%s - failed submitting write urb, error %d\n", |
| 252 | __func__, status); |
| 253 | goto error; |
| 254 | } |
| 255 | |
| 256 | /* This urb is the responsibility of the host driver now */ |
| 257 | usb_free_urb(urb); |
| 258 | dbg("%s write: %d", __func__, towrite); |
| 259 | count -= towrite; |
| 260 | bwrite += towrite; |
| 261 | } |
| 262 | return bwrite; |
| 263 | |
| 264 | error: |
| 265 | usb_free_urb(urb); |
| 266 | error_no_urb: |
| 267 | kfree(buffer); |
| 268 | error_no_buffer: |
| 269 | spin_lock_irqsave(&port->lock, flags); |
| 270 | port->urbs_in_flight--; |
| 271 | port->tx_bytes_flight -= towrite; |
| 272 | spin_unlock_irqrestore(&port->lock, flags); |
| 273 | return bwrite; |
| 274 | } |
| 275 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 276 | int usb_serial_generic_write(struct tty_struct *tty, |
| 277 | struct usb_serial_port *port, const unsigned char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
| 279 | struct usb_serial *serial = port->serial; |
| 280 | int result; |
| 281 | unsigned char *data; |
| 282 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 283 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | if (count == 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 286 | dbg("%s - write request of 0 bytes", __func__); |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 287 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* only do something if we have a bulk out endpoint */ |
| 291 | if (serial->num_bulk_out) { |
Jiri Kosina | acd2a84 | 2007-10-20 00:05:19 +0200 | [diff] [blame] | 292 | unsigned long flags; |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 293 | |
| 294 | if (serial->type->max_in_flight_urbs) |
| 295 | return usb_serial_multi_urb_write(tty, port, |
| 296 | buf, count); |
| 297 | |
Jiri Kosina | acd2a84 | 2007-10-20 00:05:19 +0200 | [diff] [blame] | 298 | spin_lock_irqsave(&port->lock, flags); |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 299 | if (port->write_urb_busy) { |
Jiri Kosina | acd2a84 | 2007-10-20 00:05:19 +0200 | [diff] [blame] | 300 | spin_unlock_irqrestore(&port->lock, flags); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 301 | dbg("%s - already writing", __func__); |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 302 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 304 | port->write_urb_busy = 1; |
Jiri Kosina | acd2a84 | 2007-10-20 00:05:19 +0200 | [diff] [blame] | 305 | spin_unlock_irqrestore(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 307 | count = (count > port->bulk_out_size) ? |
| 308 | port->bulk_out_size : count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 310 | memcpy(port->write_urb->transfer_buffer, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | data = port->write_urb->transfer_buffer; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 312 | usb_serial_debug_data(debug, &port->dev, __func__, count, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | /* set up our urb */ |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 315 | usb_fill_bulk_urb(port->write_urb, serial->dev, |
| 316 | usb_sndbulkpipe(serial->dev, |
| 317 | port->bulk_out_endpointAddress), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | port->write_urb->transfer_buffer, count, |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 319 | ((serial->type->write_bulk_callback) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | serial->type->write_bulk_callback : |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 321 | usb_serial_generic_write_bulk_callback), |
| 322 | port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
| 324 | /* send the data out the bulk port */ |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 325 | port->write_urb_busy = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 327 | if (result) { |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 328 | dev_err(&port->dev, |
| 329 | "%s - failed submitting write urb, error %d\n", |
| 330 | __func__, result); |
| 331 | /* don't have to grab the lock here, as we will |
| 332 | retry if != 0 */ |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 333 | port->write_urb_busy = 0; |
| 334 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | result = count; |
| 336 | |
| 337 | return result; |
| 338 | } |
| 339 | |
| 340 | /* no bulk out, so return 0 bytes written */ |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 341 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 343 | EXPORT_SYMBOL_GPL(usb_serial_generic_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 345 | int usb_serial_generic_write_room(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 347 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | struct usb_serial *serial = port->serial; |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 349 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | int room = 0; |
| 351 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 352 | dbg("%s - port %d", __func__, port->number); |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 353 | spin_lock_irqsave(&port->lock, flags); |
| 354 | if (serial->type->max_in_flight_urbs) { |
| 355 | if (port->urbs_in_flight < serial->type->max_in_flight_urbs) |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 356 | room = port->bulk_out_size * |
| 357 | (serial->type->max_in_flight_urbs - |
| 358 | port->urbs_in_flight); |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 359 | } else if (serial->num_bulk_out && !(port->write_urb_busy)) { |
| 360 | room = port->bulk_out_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 362 | spin_unlock_irqrestore(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 364 | dbg("%s - returns %d", __func__, room); |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 365 | return room; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 368 | int usb_serial_generic_chars_in_buffer(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 370 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | struct usb_serial *serial = port->serial; |
| 372 | int chars = 0; |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 373 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 375 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 377 | if (serial->type->max_in_flight_urbs) { |
| 378 | spin_lock_irqsave(&port->lock, flags); |
| 379 | chars = port->tx_bytes_flight; |
| 380 | spin_unlock_irqrestore(&port->lock, flags); |
| 381 | } else if (serial->num_bulk_out) { |
| 382 | /* FIXME: Locking */ |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 383 | if (port->write_urb_busy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | chars = port->write_urb->transfer_buffer_length; |
| 385 | } |
| 386 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 387 | dbg("%s - returns %d", __func__, chars); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 388 | return chars; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 391 | |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 392 | void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port, |
| 393 | gfp_t mem_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 395 | struct urb *urb = port->read_urb; |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 396 | struct usb_serial *serial = port->serial; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | int result; |
| 398 | |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 399 | /* Continue reading from device */ |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 400 | usb_fill_bulk_urb(urb, serial->dev, |
| 401 | usb_rcvbulkpipe(serial->dev, |
| 402 | port->bulk_in_endpointAddress), |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 403 | urb->transfer_buffer, |
| 404 | urb->transfer_buffer_length, |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 405 | ((serial->type->read_bulk_callback) ? |
| 406 | serial->type->read_bulk_callback : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | usb_serial_generic_read_bulk_callback), port); |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 408 | result = usb_submit_urb(urb, mem_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | if (result) |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 410 | dev_err(&port->dev, |
| 411 | "%s - failed resubmitting read urb, error %d\n", |
| 412 | __func__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 414 | EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 415 | |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 416 | /* Push data to tty layer and resubmit the bulk read URB */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 417 | static void flush_and_resubmit_read_urb(struct usb_serial_port *port) |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 418 | { |
| 419 | struct urb *urb = port->read_urb; |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 420 | struct tty_struct *tty = tty_port_tty_get(&port->port); |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 421 | char *ch = (char *)urb->transfer_buffer; |
| 422 | int i; |
| 423 | |
| 424 | if (!tty) |
| 425 | goto done; |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 426 | |
Alan Cox | 4cd1de0 | 2009-07-09 13:35:52 +0100 | [diff] [blame] | 427 | /* The per character mucking around with sysrq path it too slow for |
| 428 | stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases |
| 429 | where the USB serial is not a console anyway */ |
| 430 | if (!port->console || !port->sysrq) |
| 431 | tty_insert_flip_string(tty, ch, urb->actual_length); |
| 432 | else { |
| 433 | /* Push data to tty */ |
| 434 | for (i = 0; i < urb->actual_length; i++, ch++) { |
Alan Cox | 24a15a6 | 2009-07-09 13:36:22 +0100 | [diff] [blame^] | 435 | if (!usb_serial_handle_sysrq_char(tty, port, *ch)) |
Alan Cox | 4cd1de0 | 2009-07-09 13:35:52 +0100 | [diff] [blame] | 436 | tty_insert_flip_char(tty, *ch, TTY_NORMAL); |
| 437 | } |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 438 | } |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 439 | tty_flip_buffer_push(tty); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 440 | tty_kref_put(tty); |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 441 | done: |
| 442 | usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC); |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 445 | void usb_serial_generic_read_bulk_callback(struct urb *urb) |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 446 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 447 | struct usb_serial_port *port = urb->context; |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 448 | unsigned char *data = urb->transfer_buffer; |
Greg Kroah-Hartman | fbd27225 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 449 | int status = urb->status; |
Borislav Petkov | bfaeafc | 2007-10-28 13:24:16 +0100 | [diff] [blame] | 450 | unsigned long flags; |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 451 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 452 | dbg("%s - port %d", __func__, port->number); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 453 | |
Greg Kroah-Hartman | fbd27225 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 454 | if (unlikely(status != 0)) { |
| 455 | dbg("%s - nonzero read bulk status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 456 | __func__, status); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 460 | usb_serial_debug_data(debug, &port->dev, __func__, |
| 461 | urb->actual_length, data); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 462 | |
| 463 | /* Throttle the device if requested by tty */ |
Borislav Petkov | bfaeafc | 2007-10-28 13:24:16 +0100 | [diff] [blame] | 464 | spin_lock_irqsave(&port->lock, flags); |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 465 | port->throttled = port->throttle_req; |
| 466 | if (!port->throttled) { |
Pete Zaitcev | b507cc9 | 2008-03-04 23:28:42 -0800 | [diff] [blame] | 467 | spin_unlock_irqrestore(&port->lock, flags); |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 468 | flush_and_resubmit_read_urb(port); |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 469 | } else |
Pete Zaitcev | b507cc9 | 2008-03-04 23:28:42 -0800 | [diff] [blame] | 470 | spin_unlock_irqrestore(&port->lock, flags); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 471 | } |
Luiz Fernando N. Capitulino | 166ffcc | 2006-07-11 14:19:25 -0300 | [diff] [blame] | 472 | EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 474 | void usb_serial_generic_write_bulk_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 476 | unsigned long flags; |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 477 | struct usb_serial_port *port = urb->context; |
Greg Kroah-Hartman | fbd27225 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 478 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 480 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
Jason Wessel | 715b1dc | 2009-05-11 15:24:07 -0500 | [diff] [blame] | 482 | if (port->serial->type->max_in_flight_urbs) { |
| 483 | spin_lock_irqsave(&port->lock, flags); |
| 484 | --port->urbs_in_flight; |
| 485 | port->tx_bytes_flight -= urb->transfer_buffer_length; |
| 486 | if (port->urbs_in_flight < 0) |
| 487 | port->urbs_in_flight = 0; |
| 488 | spin_unlock_irqrestore(&port->lock, flags); |
| 489 | } else { |
| 490 | /* Handle the case for single urb mode */ |
| 491 | port->write_urb_busy = 0; |
| 492 | } |
| 493 | |
Greg Kroah-Hartman | fbd27225 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 494 | if (status) { |
| 495 | dbg("%s - nonzero write bulk status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 496 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | return; |
| 498 | } |
Pete Zaitcev | cf2c748 | 2006-05-22 21:58:49 -0700 | [diff] [blame] | 499 | usb_serial_port_softint(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
Greg Kroah-Hartman | bb83398 | 2005-11-17 09:48:18 -0800 | [diff] [blame] | 501 | EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 503 | void usb_serial_generic_throttle(struct tty_struct *tty) |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 504 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 505 | struct usb_serial_port *port = tty->driver_data; |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 506 | unsigned long flags; |
| 507 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 508 | dbg("%s - port %d", __func__, port->number); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 509 | |
| 510 | /* Set the throttle request flag. It will be picked up |
| 511 | * by usb_serial_generic_read_bulk_callback(). */ |
| 512 | spin_lock_irqsave(&port->lock, flags); |
| 513 | port->throttle_req = 1; |
| 514 | spin_unlock_irqrestore(&port->lock, flags); |
| 515 | } |
| 516 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 517 | void usb_serial_generic_unthrottle(struct tty_struct *tty) |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 518 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 519 | struct usb_serial_port *port = tty->driver_data; |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 520 | int was_throttled; |
| 521 | unsigned long flags; |
| 522 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 523 | dbg("%s - port %d", __func__, port->number); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 524 | |
| 525 | /* Clear the throttle flags */ |
| 526 | spin_lock_irqsave(&port->lock, flags); |
| 527 | was_throttled = port->throttled; |
| 528 | port->throttled = port->throttle_req = 0; |
| 529 | spin_unlock_irqrestore(&port->lock, flags); |
| 530 | |
| 531 | if (was_throttled) { |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 532 | /* Resume reading from device */ |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 533 | usb_serial_generic_resubmit_read_urb(port, GFP_KERNEL); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
Alan Cox | 24a15a6 | 2009-07-09 13:36:22 +0100 | [diff] [blame^] | 537 | int usb_serial_handle_sysrq_char(struct tty_struct *tty, |
| 538 | struct usb_serial_port *port, unsigned int ch) |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 539 | { |
Jason Wessel | 568d422 | 2009-05-29 13:34:17 -0500 | [diff] [blame] | 540 | if (port->sysrq && port->console) { |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 541 | if (ch && time_before(jiffies, port->sysrq)) { |
Alan Cox | 24a15a6 | 2009-07-09 13:36:22 +0100 | [diff] [blame^] | 542 | handle_sysrq(ch, tty); |
Jason Wessel | 98fcb5f | 2009-05-11 15:24:09 -0500 | [diff] [blame] | 543 | port->sysrq = 0; |
| 544 | return 1; |
| 545 | } |
| 546 | port->sysrq = 0; |
| 547 | } |
| 548 | return 0; |
| 549 | } |
| 550 | EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char); |
| 551 | |
| 552 | int usb_serial_handle_break(struct usb_serial_port *port) |
| 553 | { |
| 554 | if (!port->sysrq) { |
| 555 | port->sysrq = jiffies + HZ*5; |
| 556 | return 1; |
| 557 | } |
| 558 | port->sysrq = 0; |
| 559 | return 0; |
| 560 | } |
| 561 | EXPORT_SYMBOL_GPL(usb_serial_handle_break); |
| 562 | |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 563 | void usb_serial_generic_disconnect(struct usb_serial *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
| 565 | int i; |
| 566 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 567 | dbg("%s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
| 569 | /* stop reads and writes on all ports */ |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 570 | for (i = 0; i < serial->num_ports; ++i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | generic_cleanup(serial->port[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 574 | void usb_serial_generic_release(struct usb_serial *serial) |
| 575 | { |
| 576 | dbg("%s", __func__); |
| 577 | } |