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, |
| 66 | .shutdown = usb_serial_generic_shutdown, |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 67 | .throttle = usb_serial_generic_throttle, |
| 68 | .unthrottle = usb_serial_generic_unthrottle, |
Oliver Neukum | ec22559 | 2007-04-27 20:54:57 +0200 | [diff] [blame] | 69 | .resume = usb_serial_generic_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | static int generic_probe(struct usb_interface *interface, |
| 73 | const struct usb_device_id *id) |
| 74 | { |
| 75 | const struct usb_device_id *id_pattern; |
| 76 | |
| 77 | id_pattern = usb_match_id(interface, generic_device_ids); |
| 78 | if (id_pattern != NULL) |
| 79 | return usb_serial_probe(interface, id); |
| 80 | return -ENODEV; |
| 81 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #endif |
| 83 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 84 | int usb_serial_generic_register(int _debug) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | int retval = 0; |
| 87 | |
| 88 | debug = _debug; |
| 89 | #ifdef CONFIG_USB_SERIAL_GENERIC |
| 90 | generic_device_ids[0].idVendor = vendor; |
| 91 | generic_device_ids[0].idProduct = product; |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 92 | generic_device_ids[0].match_flags = |
| 93 | USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | /* register our generic driver with ourselves */ |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 96 | retval = usb_serial_register(&usb_serial_generic_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (retval) |
| 98 | goto exit; |
| 99 | retval = usb_register(&generic_driver); |
| 100 | if (retval) |
| 101 | usb_serial_deregister(&usb_serial_generic_device); |
| 102 | exit: |
| 103 | #endif |
| 104 | return retval; |
| 105 | } |
| 106 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 107 | void usb_serial_generic_deregister(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
| 109 | #ifdef CONFIG_USB_SERIAL_GENERIC |
| 110 | /* remove our generic driver */ |
| 111 | usb_deregister(&generic_driver); |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 112 | usb_serial_deregister(&usb_serial_generic_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | #endif |
| 114 | } |
| 115 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 116 | int usb_serial_generic_open(struct tty_struct *tty, |
| 117 | struct usb_serial_port *port, struct file *filp) |
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 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 125 | /* force low_latency on so that our tty_push actually forces the data |
| 126 | through, otherwise it is scheduled, and with high data rates (like |
| 127 | with OHCI) data can get lost. */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 128 | if (tty) |
| 129 | tty->low_latency = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 131 | /* clear the throttle flags */ |
| 132 | spin_lock_irqsave(&port->lock, flags); |
| 133 | port->throttled = 0; |
| 134 | port->throttle_req = 0; |
| 135 | spin_unlock_irqrestore(&port->lock, flags); |
| 136 | |
| 137 | /* if we have a bulk endpoint, start reading from it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | if (serial->num_bulk_in) { |
| 139 | /* Start reading from the device */ |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 140 | usb_fill_bulk_urb(port->read_urb, serial->dev, |
| 141 | usb_rcvbulkpipe(serial->dev, |
| 142 | port->bulk_in_endpointAddress), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | port->read_urb->transfer_buffer, |
| 144 | port->read_urb->transfer_buffer_length, |
| 145 | ((serial->type->read_bulk_callback) ? |
| 146 | serial->type->read_bulk_callback : |
| 147 | usb_serial_generic_read_bulk_callback), |
| 148 | port); |
| 149 | result = usb_submit_urb(port->read_urb, GFP_KERNEL); |
| 150 | if (result) |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 151 | dev_err(&port->dev, |
| 152 | "%s - failed resubmitting read urb, error %d\n", |
| 153 | __func__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | return result; |
| 157 | } |
Greg Kroah-Hartman | 815ddc9 | 2006-05-12 11:05:29 -0700 | [diff] [blame] | 158 | EXPORT_SYMBOL_GPL(usb_serial_generic_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 160 | static void generic_cleanup(struct usb_serial_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
| 162 | struct usb_serial *serial = port->serial; |
| 163 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 164 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | if (serial->dev) { |
| 167 | /* shutdown any bulk reads that might be going on */ |
| 168 | if (serial->num_bulk_out) |
| 169 | usb_kill_urb(port->write_urb); |
| 170 | if (serial->num_bulk_in) |
| 171 | usb_kill_urb(port->read_urb); |
| 172 | } |
| 173 | } |
| 174 | |
Oliver Neukum | ec22559 | 2007-04-27 20:54:57 +0200 | [diff] [blame] | 175 | int usb_serial_generic_resume(struct usb_serial *serial) |
| 176 | { |
| 177 | struct usb_serial_port *port; |
| 178 | int i, c = 0, r; |
| 179 | |
| 180 | for (i = 0; i < serial->num_ports; i++) { |
| 181 | port = serial->port[i]; |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 182 | if (port->port.count && port->read_urb) { |
Oliver Neukum | ec22559 | 2007-04-27 20:54:57 +0200 | [diff] [blame] | 183 | r = usb_submit_urb(port->read_urb, GFP_NOIO); |
| 184 | if (r < 0) |
| 185 | c++; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return c ? -EIO : 0; |
| 190 | } |
Oliver Neukum | 81d043c | 2009-02-06 15:37:14 +0100 | [diff] [blame] | 191 | EXPORT_SYMBOL_GPL(usb_serial_generic_resume); |
Oliver Neukum | ec22559 | 2007-04-27 20:54:57 +0200 | [diff] [blame] | 192 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 193 | void usb_serial_generic_close(struct tty_struct *tty, |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 194 | struct usb_serial_port *port, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 196 | dbg("%s - port %d", __func__, port->number); |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 197 | generic_cleanup(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 200 | int usb_serial_generic_write(struct tty_struct *tty, |
| 201 | struct usb_serial_port *port, const unsigned char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 203 | struct usb_serial *serial = port->serial; |
| 204 | int result; |
| 205 | unsigned char *data; |
| 206 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 207 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | if (count == 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 210 | dbg("%s - write request of 0 bytes", __func__); |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 211 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* only do something if we have a bulk out endpoint */ |
| 215 | if (serial->num_bulk_out) { |
Jiri Kosina | acd2a84 | 2007-10-20 00:05:19 +0200 | [diff] [blame] | 216 | unsigned long flags; |
| 217 | spin_lock_irqsave(&port->lock, flags); |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 218 | if (port->write_urb_busy) { |
Jiri Kosina | acd2a84 | 2007-10-20 00:05:19 +0200 | [diff] [blame] | 219 | spin_unlock_irqrestore(&port->lock, flags); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 220 | dbg("%s - already writing", __func__); |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 221 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 223 | port->write_urb_busy = 1; |
Jiri Kosina | acd2a84 | 2007-10-20 00:05:19 +0200 | [diff] [blame] | 224 | spin_unlock_irqrestore(&port->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 226 | count = (count > port->bulk_out_size) ? |
| 227 | port->bulk_out_size : count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 229 | memcpy(port->write_urb->transfer_buffer, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | data = port->write_urb->transfer_buffer; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 231 | usb_serial_debug_data(debug, &port->dev, __func__, count, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
| 233 | /* set up our urb */ |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 234 | usb_fill_bulk_urb(port->write_urb, serial->dev, |
| 235 | usb_sndbulkpipe(serial->dev, |
| 236 | port->bulk_out_endpointAddress), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | port->write_urb->transfer_buffer, count, |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 238 | ((serial->type->write_bulk_callback) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | serial->type->write_bulk_callback : |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 240 | usb_serial_generic_write_bulk_callback), |
| 241 | port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | /* send the data out the bulk port */ |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 244 | port->write_urb_busy = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 246 | if (result) { |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 247 | dev_err(&port->dev, |
| 248 | "%s - failed submitting write urb, error %d\n", |
| 249 | __func__, result); |
| 250 | /* don't have to grab the lock here, as we will |
| 251 | retry if != 0 */ |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 252 | port->write_urb_busy = 0; |
| 253 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | result = count; |
| 255 | |
| 256 | return result; |
| 257 | } |
| 258 | |
| 259 | /* no bulk out, so return 0 bytes written */ |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 260 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 263 | int usb_serial_generic_write_room(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 265 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | struct usb_serial *serial = port->serial; |
| 267 | int room = 0; |
| 268 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 269 | dbg("%s - port %d", __func__, port->number); |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 270 | |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 271 | /* FIXME: Locking */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | if (serial->num_bulk_out) { |
Randall Nortman | 7a3ca7d | 2005-10-14 17:21:50 -0700 | [diff] [blame] | 273 | if (!(port->write_urb_busy)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | room = port->bulk_out_size; |
| 275 | } |
| 276 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 277 | dbg("%s - returns %d", __func__, room); |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 278 | return room; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 281 | int usb_serial_generic_chars_in_buffer(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 283 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | struct usb_serial *serial = port->serial; |
| 285 | int chars = 0; |
| 286 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 287 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 289 | /* FIXME: Locking */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | if (serial->num_bulk_out) { |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 291 | if (port->write_urb_busy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | chars = port->write_urb->transfer_buffer_length; |
| 293 | } |
| 294 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 295 | dbg("%s - returns %d", __func__, chars); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 296 | return chars; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 299 | |
| 300 | static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 302 | struct urb *urb = port->read_urb; |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 303 | struct usb_serial *serial = port->serial; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | int result; |
| 305 | |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 306 | /* Continue reading from device */ |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 307 | usb_fill_bulk_urb(urb, serial->dev, |
| 308 | usb_rcvbulkpipe(serial->dev, |
| 309 | port->bulk_in_endpointAddress), |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 310 | urb->transfer_buffer, |
| 311 | urb->transfer_buffer_length, |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 312 | ((serial->type->read_bulk_callback) ? |
| 313 | serial->type->read_bulk_callback : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | usb_serial_generic_read_bulk_callback), port); |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 315 | result = usb_submit_urb(urb, mem_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | if (result) |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 317 | dev_err(&port->dev, |
| 318 | "%s - failed resubmitting read urb, error %d\n", |
| 319 | __func__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 321 | |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 322 | /* Push data to tty layer and resubmit the bulk read URB */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 323 | static void flush_and_resubmit_read_urb(struct usb_serial_port *port) |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 324 | { |
| 325 | struct urb *urb = port->read_urb; |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 326 | struct tty_struct *tty = tty_port_tty_get(&port->port); |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 327 | int room; |
| 328 | |
| 329 | /* Push data to tty */ |
| 330 | if (tty && urb->actual_length) { |
| 331 | room = tty_buffer_request_room(tty, urb->actual_length); |
| 332 | if (room) { |
| 333 | tty_insert_flip_string(tty, urb->transfer_buffer, room); |
Pete Zaitcev | b507cc9 | 2008-03-04 23:28:42 -0800 | [diff] [blame] | 334 | tty_flip_buffer_push(tty); |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 335 | } |
| 336 | } |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 337 | tty_kref_put(tty); |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 338 | |
| 339 | resubmit_read_urb(port, GFP_ATOMIC); |
| 340 | } |
| 341 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 342 | void usb_serial_generic_read_bulk_callback(struct urb *urb) |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 343 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 344 | struct usb_serial_port *port = urb->context; |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 345 | unsigned char *data = urb->transfer_buffer; |
Greg Kroah-Hartman | fbd27225 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 346 | int status = urb->status; |
Borislav Petkov | bfaeafc | 2007-10-28 13:24:16 +0100 | [diff] [blame] | 347 | unsigned long flags; |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 348 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 349 | dbg("%s - port %d", __func__, port->number); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 350 | |
Greg Kroah-Hartman | fbd27225 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 351 | if (unlikely(status != 0)) { |
| 352 | dbg("%s - nonzero read bulk status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 353 | __func__, status); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 354 | return; |
| 355 | } |
| 356 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 357 | usb_serial_debug_data(debug, &port->dev, __func__, |
| 358 | urb->actual_length, data); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 359 | |
| 360 | /* Throttle the device if requested by tty */ |
Borislav Petkov | bfaeafc | 2007-10-28 13:24:16 +0100 | [diff] [blame] | 361 | spin_lock_irqsave(&port->lock, flags); |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 362 | port->throttled = port->throttle_req; |
| 363 | if (!port->throttled) { |
Pete Zaitcev | b507cc9 | 2008-03-04 23:28:42 -0800 | [diff] [blame] | 364 | spin_unlock_irqrestore(&port->lock, flags); |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 365 | flush_and_resubmit_read_urb(port); |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 366 | } else |
Pete Zaitcev | b507cc9 | 2008-03-04 23:28:42 -0800 | [diff] [blame] | 367 | spin_unlock_irqrestore(&port->lock, flags); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 368 | } |
Luiz Fernando N. Capitulino | 166ffcc | 2006-07-11 14:19:25 -0300 | [diff] [blame] | 369 | EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 371 | void usb_serial_generic_write_bulk_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 373 | struct usb_serial_port *port = urb->context; |
Greg Kroah-Hartman | fbd27225 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 374 | int status = urb->status; |
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 - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Greg Kroah-Hartman | 507ca9b | 2005-04-23 12:49:16 -0700 | [diff] [blame] | 378 | port->write_urb_busy = 0; |
Greg Kroah-Hartman | fbd27225 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 379 | if (status) { |
| 380 | dbg("%s - nonzero write bulk status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 381 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | return; |
| 383 | } |
Pete Zaitcev | cf2c748 | 2006-05-22 21:58:49 -0700 | [diff] [blame] | 384 | usb_serial_port_softint(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
Greg Kroah-Hartman | bb83398 | 2005-11-17 09:48:18 -0800 | [diff] [blame] | 386 | EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 388 | void usb_serial_generic_throttle(struct tty_struct *tty) |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 389 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 390 | struct usb_serial_port *port = tty->driver_data; |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 391 | unsigned long flags; |
| 392 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 393 | dbg("%s - port %d", __func__, port->number); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 394 | |
| 395 | /* Set the throttle request flag. It will be picked up |
| 396 | * by usb_serial_generic_read_bulk_callback(). */ |
| 397 | spin_lock_irqsave(&port->lock, flags); |
| 398 | port->throttle_req = 1; |
| 399 | spin_unlock_irqrestore(&port->lock, flags); |
| 400 | } |
| 401 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 402 | void usb_serial_generic_unthrottle(struct tty_struct *tty) |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 403 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 404 | struct usb_serial_port *port = tty->driver_data; |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 405 | int was_throttled; |
| 406 | unsigned long flags; |
| 407 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 408 | dbg("%s - port %d", __func__, port->number); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 409 | |
| 410 | /* Clear the throttle flags */ |
| 411 | spin_lock_irqsave(&port->lock, flags); |
| 412 | was_throttled = port->throttled; |
| 413 | port->throttled = port->throttle_req = 0; |
| 414 | spin_unlock_irqrestore(&port->lock, flags); |
| 415 | |
| 416 | if (was_throttled) { |
Oliver Neukum | 1abdeeb | 2007-05-07 12:09:33 +0200 | [diff] [blame] | 417 | /* Resume reading from device */ |
| 418 | resubmit_read_urb(port, GFP_KERNEL); |
Joris van Rantwijk | 253ca92 | 2007-02-01 20:08:18 +0100 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 422 | void usb_serial_generic_shutdown(struct usb_serial *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
| 424 | int i; |
| 425 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 426 | dbg("%s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | /* stop reads and writes on all ports */ |
Alan Cox | ae64387 | 2008-07-22 11:11:55 +0100 | [diff] [blame] | 429 | for (i = 0; i < serial->num_ports; ++i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | generic_cleanup(serial->port[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
| 432 | |