From bd5afa9eac6daa408412a31a6c69e87e8bd28c7e Mon Sep 17 00:00:00 2001 From: Jason Wessel Date: Mon, 8 Mar 2010 21:50:12 -0600 Subject: usb-serial: Use tty_port version console instead of usb_serial_port Replace all instances of using the console variable in struct usb_serial_port with the struct tty_port version. CC: Alan Cox CC: Alan Stern CC: Oliver Neukum CC: Andrew Morton CC: linux-usb@vger.kernel.org CC: linux-kernel@vger.kernel.org Signed-off-by: Jason Wessel Signed-off-by: Greg Kroah-Hartman --- drivers/usb/serial/usb-serial.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'drivers/usb/serial/usb-serial.c') diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c index 3873660d8217..f3f65171de38 100644 --- a/drivers/usb/serial/usb-serial.c +++ b/drivers/usb/serial/usb-serial.c @@ -289,7 +289,7 @@ static void serial_down(struct tty_port *tport) * The console is magical. Do not hang up the console hardware * or there will be tears. */ - if (port->console) + if (port->port.console) return; if (drv->close) drv->close(port); @@ -328,7 +328,7 @@ static void serial_cleanup(struct tty_struct *tty) /* The console is magical. Do not hang up the console hardware * or there will be tears. */ - if (port->console) + if (port->port.console) return; dbg("%s - port %d", __func__, port->number); -- cgit v1.2.3 From bbcb2b907415a90334521a31a8767cd77462c716 Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Wed, 17 Mar 2010 23:00:37 +0100 Subject: USB: serial: allow drivers to define bulk buffer sizes Allow drivers to define custom bulk in/out buffer sizes in struct usb_serial_driver. If not set, fall back to the default buffer size which matches the endpoint size. Three drivers are currently freeing the pre-allocated buffers and allocating larger ones to achieve this at port probe (ftdi_sio) or even at port open (ipaq and iuu_phoenix), which needless to say is suboptimal. Signed-off-by: Johan Hovold Signed-off-by: Greg Kroah-Hartman --- drivers/usb/serial/ftdi_sio.c | 16 +--------------- drivers/usb/serial/ipaq.c | 28 ++-------------------------- drivers/usb/serial/iuu_phoenix.c | 30 ++---------------------------- drivers/usb/serial/usb-serial.c | 8 ++++++-- include/linux/usb/serial.h | 5 +++++ 5 files changed, 16 insertions(+), 71 deletions(-) (limited to 'drivers/usb/serial/usb-serial.c') diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c index 46a88ae9c46a..ab4ad18d6ef3 100644 --- a/drivers/usb/serial/ftdi_sio.c +++ b/drivers/usb/serial/ftdi_sio.c @@ -768,9 +768,6 @@ static const char *ftdi_chip_name[] = { }; -/* Constants for read urb and write urb */ -#define BUFSZ 512 - /* Used for TIOCMIWAIT */ #define FTDI_STATUS_B0_MASK (FTDI_RS0_CTS | FTDI_RS0_DSR | FTDI_RS0_RI | FTDI_RS0_RLSD) #define FTDI_STATUS_B1_MASK (FTDI_RS_BI) @@ -821,6 +818,7 @@ static struct usb_serial_driver ftdi_sio_device = { .usb_driver = &ftdi_driver, .id_table = id_table_combined, .num_ports = 1, + .bulk_in_size = 512, .probe = ftdi_sio_probe, .port_probe = ftdi_sio_port_probe, .port_remove = ftdi_sio_port_remove, @@ -1552,18 +1550,6 @@ static int ftdi_sio_port_probe(struct usb_serial_port *port) if (quirk && quirk->port_probe) quirk->port_probe(priv); - /* Increase the size of read buffers */ - kfree(port->bulk_in_buffer); - port->bulk_in_buffer = kmalloc(BUFSZ, GFP_KERNEL); - if (!port->bulk_in_buffer) { - kfree(priv); - return -ENOMEM; - } - if (port->read_urb) { - port->read_urb->transfer_buffer = port->bulk_in_buffer; - port->read_urb->transfer_buffer_length = BUFSZ; - } - priv->port = port; /* Free port's existing write urb and transfer buffer. */ diff --git a/drivers/usb/serial/ipaq.c b/drivers/usb/serial/ipaq.c index 3fea9298eb15..87b11461bf11 100644 --- a/drivers/usb/serial/ipaq.c +++ b/drivers/usb/serial/ipaq.c @@ -571,6 +571,8 @@ static struct usb_serial_driver ipaq_device = { .description = "PocketPC PDA", .usb_driver = &ipaq_driver, .id_table = ipaq_id_table, + .bulk_in_size = URBDATA_SIZE, + .bulk_out_size = URBDATA_SIZE, .open = ipaq_open, .close = ipaq_close, .attach = ipaq_startup, @@ -628,32 +630,6 @@ static int ipaq_open(struct tty_struct *tty, priv->free_len += PACKET_SIZE; } - /* - * Lose the small buffers usbserial provides. Make larger ones. - */ - - kfree(port->bulk_in_buffer); - kfree(port->bulk_out_buffer); - /* make sure the generic serial code knows */ - port->bulk_out_buffer = NULL; - - port->bulk_in_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL); - if (port->bulk_in_buffer == NULL) - goto enomem; - - port->bulk_out_buffer = kmalloc(URBDATA_SIZE, GFP_KERNEL); - if (port->bulk_out_buffer == NULL) { - /* the buffer is useless, free it */ - kfree(port->bulk_in_buffer); - port->bulk_in_buffer = NULL; - goto enomem; - } - port->read_urb->transfer_buffer = port->bulk_in_buffer; - port->write_urb->transfer_buffer = port->bulk_out_buffer; - port->read_urb->transfer_buffer_length = URBDATA_SIZE; - port->bulk_out_size = port->write_urb->transfer_buffer_length - = URBDATA_SIZE; - msleep(1000*initial_wait); /* diff --git a/drivers/usb/serial/iuu_phoenix.c b/drivers/usb/serial/iuu_phoenix.c index 43f13cf2f016..74551cb2e8ee 100644 --- a/drivers/usb/serial/iuu_phoenix.c +++ b/drivers/usb/serial/iuu_phoenix.c @@ -1044,34 +1044,6 @@ static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port) if (buf == NULL) return -ENOMEM; - /* fixup the endpoint buffer size */ - kfree(port->bulk_out_buffer); - port->bulk_out_buffer = kmalloc(512, GFP_KERNEL); - port->bulk_out_size = 512; - kfree(port->bulk_in_buffer); - port->bulk_in_buffer = kmalloc(512, GFP_KERNEL); - port->bulk_in_size = 512; - - if (!port->bulk_out_buffer || !port->bulk_in_buffer) { - kfree(port->bulk_out_buffer); - kfree(port->bulk_in_buffer); - kfree(buf); - return -ENOMEM; - } - - usb_fill_bulk_urb(port->write_urb, port->serial->dev, - usb_sndbulkpipe(port->serial->dev, - port->bulk_out_endpointAddress), - port->bulk_out_buffer, 512, - NULL, NULL); - - - usb_fill_bulk_urb(port->read_urb, port->serial->dev, - usb_rcvbulkpipe(port->serial->dev, - port->bulk_in_endpointAddress), - port->bulk_in_buffer, 512, - NULL, NULL); - priv->poll = 0; /* initialize writebuf */ @@ -1277,6 +1249,8 @@ static struct usb_serial_driver iuu_device = { }, .id_table = id_table, .num_ports = 1, + .bulk_in_size = 512, + .bulk_out_size = 512, .port_probe = iuu_create_sysfs_attrs, .port_remove = iuu_remove_sysfs_attrs, .open = iuu_open, diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c index f3f65171de38..538924627eba 100644 --- a/drivers/usb/serial/usb-serial.c +++ b/drivers/usb/serial/usb-serial.c @@ -901,7 +901,9 @@ int usb_serial_probe(struct usb_interface *interface, dev_err(&interface->dev, "No free urbs available\n"); goto probe_error; } - buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); + buffer_size = serial->type->bulk_in_size; + if (!buffer_size) + buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); port->bulk_in_size = buffer_size; port->bulk_in_endpointAddress = endpoint->bEndpointAddress; port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL); @@ -927,7 +929,9 @@ int usb_serial_probe(struct usb_interface *interface, } if (kfifo_alloc(&port->write_fifo, PAGE_SIZE, GFP_KERNEL)) goto probe_error; - buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); + buffer_size = serial->type->bulk_out_size; + if (!buffer_size) + buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); port->bulk_out_size = buffer_size; port->bulk_out_endpointAddress = endpoint->bEndpointAddress; port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL); diff --git a/include/linux/usb/serial.h b/include/linux/usb/serial.h index b7682fed1d9c..ab311dab3383 100644 --- a/include/linux/usb/serial.h +++ b/include/linux/usb/serial.h @@ -179,6 +179,8 @@ static inline void usb_set_serial_data(struct usb_serial *serial, void *data) * @id_table: pointer to a list of usb_device_id structures that define all * of the devices this structure can support. * @num_ports: the number of different ports this device will have. + * @bulk_in_size: bytes to allocate for bulk-in buffer (0 = end-point size) + * @bulk_out_size: bytes to allocate for bulk-out buffer (0 = end-point size) * @calc_num_ports: pointer to a function to determine how many ports this * device has dynamically. It will be called after the probe() * callback is called, but before attach() @@ -223,6 +225,9 @@ struct usb_serial_driver { struct usb_dynids dynids; int max_in_flight_urbs; + size_t bulk_in_size; + size_t bulk_out_size; + int (*probe)(struct usb_serial *serial, const struct usb_device_id *id); int (*attach)(struct usb_serial *serial); int (*calc_num_ports) (struct usb_serial *serial); -- cgit v1.2.3 From 231543206452f5c42bace54b5c13dfe5a0313812 Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Wed, 17 Mar 2010 23:05:57 +0100 Subject: USB: serial: generalise generic read implementation Add process_read_urb to usb_serial_driver so that a driver can rely on the generic read (and throttle) mechanism but still do device specific processing of incoming data (such as adding tty_flags before pushing to line discipline). The default generic implementation handles sysrq for consoles but otherwise simply pushes to tty. Signed-off-by: Johan Hovold Signed-off-by: Greg Kroah-Hartman --- drivers/usb/serial/generic.c | 5 +++-- drivers/usb/serial/usb-serial.c | 1 + include/linux/usb/serial.h | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) (limited to 'drivers/usb/serial/usb-serial.c') diff --git a/drivers/usb/serial/generic.c b/drivers/usb/serial/generic.c index e16c0b234cc9..176f1257b664 100644 --- a/drivers/usb/serial/generic.c +++ b/drivers/usb/serial/generic.c @@ -408,7 +408,7 @@ int usb_serial_generic_submit_read_urb(struct usb_serial_port *port, } EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urb); -static void usb_serial_generic_process_read_urb(struct urb *urb) +void usb_serial_generic_process_read_urb(struct urb *urb) { struct usb_serial_port *port = urb->context; struct tty_struct *tty; @@ -433,6 +433,7 @@ static void usb_serial_generic_process_read_urb(struct urb *urb) tty_flip_buffer_push(tty); tty_kref_put(tty); } +EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb); void usb_serial_generic_read_bulk_callback(struct urb *urb) { @@ -451,7 +452,7 @@ void usb_serial_generic_read_bulk_callback(struct urb *urb) usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length, data); - usb_serial_generic_process_read_urb(urb); + port->serial->type->process_read_urb(urb); /* Throttle the device if requested by tty */ spin_lock_irqsave(&port->lock, flags); diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c index 538924627eba..1b924425089f 100644 --- a/drivers/usb/serial/usb-serial.c +++ b/drivers/usb/serial/usb-serial.c @@ -1298,6 +1298,7 @@ static void fixup_generic(struct usb_serial_driver *device) set_to_generic_if_null(device, write_bulk_callback); set_to_generic_if_null(device, disconnect); set_to_generic_if_null(device, release); + set_to_generic_if_null(device, process_read_urb); } int usb_serial_register(struct usb_serial_driver *driver) diff --git a/include/linux/usb/serial.h b/include/linux/usb/serial.h index 53f6dc65e87e..ff8872eba3ac 100644 --- a/include/linux/usb/serial.h +++ b/include/linux/usb/serial.h @@ -272,6 +272,8 @@ struct usb_serial_driver { void (*write_int_callback)(struct urb *urb); void (*read_bulk_callback)(struct urb *urb); void (*write_bulk_callback)(struct urb *urb); + /* Called by the generic read bulk callback */ + void (*process_read_urb)(struct urb *urb); }; #define to_usb_serial_driver(d) \ container_of(d, struct usb_serial_driver, driver) @@ -323,6 +325,7 @@ extern int usb_serial_generic_register(int debug); extern void usb_serial_generic_deregister(void); extern int usb_serial_generic_submit_read_urb(struct usb_serial_port *port, gfp_t mem_flags); +extern void usb_serial_generic_process_read_urb(struct urb *urb); extern int usb_serial_handle_sysrq_char(struct tty_struct *tty, struct usb_serial_port *port, unsigned int ch); -- cgit v1.2.3 From eaa3bcb06aed1ac1d6d9e3edd3b5f72ea57a6ac0 Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Wed, 17 Mar 2010 23:06:08 +0100 Subject: USB: serial: generalise write buffer preparation Generalise write buffer preparation. This allows for drivers to manipulate (e.g. add headers) to bulk out data before it is sent. This adds a new function pointer to usb_serial_driver: int (*prepare_write_buffer)(struct usb_serial_port *port, void **dest, size_t size, const void *src, size_t count); The function is generic and can be used with either kfifo-based or multi-urb writes: If *dest is NULL the implementation should allocate dest. If src is NULL the implementation should use the port write fifo. If not set, a generic implementation is used which simply uses memcpy or kfifo_out. Signed-off-by: Johan Hovold Signed-off-by: Greg Kroah-Hartman --- drivers/usb/serial/generic.c | 47 +++++++++++++++++++++++++++++------------ drivers/usb/serial/usb-serial.c | 1 + include/linux/usb/serial.h | 5 +++++ 3 files changed, 40 insertions(+), 13 deletions(-) (limited to 'drivers/usb/serial/usb-serial.c') diff --git a/drivers/usb/serial/generic.c b/drivers/usb/serial/generic.c index ad4823bbfa19..1a134f9c64f3 100644 --- a/drivers/usb/serial/generic.c +++ b/drivers/usb/serial/generic.c @@ -167,12 +167,35 @@ void usb_serial_generic_close(struct usb_serial_port *port) } EXPORT_SYMBOL_GPL(usb_serial_generic_close); +int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port, + void **dest, size_t size, const void *src, size_t count) +{ + if (!*dest) { + size = count; + *dest = kmalloc(count, GFP_ATOMIC); + if (!*dest) { + dev_err(&port->dev, "%s - could not allocate buffer\n", + __func__); + return -ENOMEM; + } + } + if (src) { + count = size; + memcpy(*dest, src, size); + } else { + count = kfifo_out_locked(&port->write_fifo, *dest, size, + &port->lock); + } + return count; +} +EXPORT_SYMBOL_GPL(usb_serial_generic_prepare_write_buffer); + static int usb_serial_multi_urb_write(struct tty_struct *tty, struct usb_serial_port *port, const unsigned char *buf, int count) { unsigned long flags; struct urb *urb; - unsigned char *buffer; + void *buffer; int status; spin_lock_irqsave(&port->lock, flags); @@ -191,16 +214,14 @@ static int usb_serial_multi_urb_write(struct tty_struct *tty, goto err_urb; } + buffer = NULL; count = min_t(int, count, PAGE_SIZE); - buffer = kmalloc(count, GFP_ATOMIC); - if (!buffer) { - dev_err(&port->dev, "%s - could not allocate buffer\n", - __func__); - status = -ENOMEM; + count = port->serial->type->prepare_write_buffer(port, &buffer, 0, + buf, count); + if (count < 0) { + status = count; goto err_buf; } - - memcpy(buffer, buf, count); usb_serial_debug_data(debug, &port->dev, __func__, count, buffer); usb_fill_bulk_urb(urb, port->serial->dev, usb_sndbulkpipe(port->serial->dev, @@ -242,7 +263,6 @@ err_urb: */ static int usb_serial_generic_write_start(struct usb_serial_port *port) { - unsigned char *data; int result; int count; unsigned long flags; @@ -255,10 +275,11 @@ static int usb_serial_generic_write_start(struct usb_serial_port *port) port->write_urb_busy = 1; spin_unlock_irqrestore(&port->lock, flags); - data = port->write_urb->transfer_buffer; - count = kfifo_out_locked(&port->write_fifo, data, port->bulk_out_size, &port->lock); - usb_serial_debug_data(debug, &port->dev, __func__, count, data); - + count = port->serial->type->prepare_write_buffer(port, + &port->write_urb->transfer_buffer, + port->bulk_out_size, NULL, 0); + usb_serial_debug_data(debug, &port->dev, __func__, + count, port->write_urb->transfer_buffer); port->write_urb->transfer_buffer_length = count; /* send the data out the bulk port */ diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c index 1b924425089f..8249fd8381fb 100644 --- a/drivers/usb/serial/usb-serial.c +++ b/drivers/usb/serial/usb-serial.c @@ -1299,6 +1299,7 @@ static void fixup_generic(struct usb_serial_driver *device) set_to_generic_if_null(device, disconnect); set_to_generic_if_null(device, release); set_to_generic_if_null(device, process_read_urb); + set_to_generic_if_null(device, prepare_write_buffer); } int usb_serial_register(struct usb_serial_driver *driver) diff --git a/include/linux/usb/serial.h b/include/linux/usb/serial.h index 2a3283761600..a4c99ea390e7 100644 --- a/include/linux/usb/serial.h +++ b/include/linux/usb/serial.h @@ -277,6 +277,9 @@ struct usb_serial_driver { void (*write_bulk_callback)(struct urb *urb); /* Called by the generic read bulk callback */ void (*process_read_urb)(struct urb *urb); + /* Called by the generic write implementation */ + int (*prepare_write_buffer)(struct usb_serial_port *port, + void **dest, size_t size, const void *src, size_t count); }; #define to_usb_serial_driver(d) \ container_of(d, struct usb_serial_driver, driver) @@ -329,6 +332,8 @@ extern void usb_serial_generic_deregister(void); extern int usb_serial_generic_submit_read_urb(struct usb_serial_port *port, gfp_t mem_flags); extern void usb_serial_generic_process_read_urb(struct urb *urb); +extern int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port, + void **dest, size_t size, const void *src, size_t count); extern int usb_serial_handle_sysrq_char(struct tty_struct *tty, struct usb_serial_port *port, unsigned int ch); -- cgit v1.2.3 From 27c7acf22047fbe4ec4cc36b7c2610dba227697c Mon Sep 17 00:00:00 2001 From: Johan Hovold Date: Wed, 5 May 2010 23:57:37 +0200 Subject: USB: serial: reimplement generic fifo-based writes Reimplement fifo-based writes in the generic driver using a multiple pre-allocated urb scheme. In contrast to multi-urb writes, no allocations (of urbs or buffers) are made during run-time and there is less pressure on the host stack queues as currently only two urbs are used (implementation is generic and can handle more than two urbs as well, though). Initial tests using ftdi_sio show that the implementation achieves the same (maximum) throughput at high baudrates as multi-urb writes. The CPU usage is much lower than for multi-urb writes for small write requests and only slightly higher for large (e.g. 2k) requests (due to extra copy via fifo?). Also outperforms multi-urb writes for small write requests on an embedded arm-9 system, where multi-urb writes are CPU-bound at high baudrates (perf reveals that a lot of time is spent in the host stack enqueue function -- could perhaps be a bug as well). Keeping the original write_urb, buffer and flag for now as there are other drivers depending on them. Signed-off-by: Johan Hovold Signed-off-by: Greg Kroah-Hartman --- drivers/usb/serial/generic.c | 59 +++++++++++++++++++++++++++-------------- drivers/usb/serial/usb-serial.c | 33 +++++++++++++++++++++++ include/linux/usb/serial.h | 12 +++++++++ 3 files changed, 84 insertions(+), 20 deletions(-) (limited to 'drivers/usb/serial/usb-serial.c') diff --git a/drivers/usb/serial/generic.c b/drivers/usb/serial/generic.c index 1a134f9c64f3..3ae17840175c 100644 --- a/drivers/usb/serial/generic.c +++ b/drivers/usb/serial/generic.c @@ -1,6 +1,7 @@ /* * USB Serial Converter Generic functions * + * Copyright (C) 2010 Johan Hovold (jhovold@gmail.com) * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com) * * This program is free software; you can redistribute it and/or @@ -143,6 +144,7 @@ static void generic_cleanup(struct usb_serial_port *port) { struct usb_serial *serial = port->serial; unsigned long flags; + int i; dbg("%s - port %d", __func__, port->number); @@ -150,6 +152,8 @@ static void generic_cleanup(struct usb_serial_port *port) /* shutdown any bulk transfers that might be going on */ if (port->bulk_out_size) { usb_kill_urb(port->write_urb); + for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) + usb_kill_urb(port->write_urbs[i]); spin_lock_irqsave(&port->lock, flags); kfifo_reset_out(&port->write_fifo); @@ -258,46 +262,56 @@ err_urb: * usb_serial_generic_write_start - kick off an URB write * @port: Pointer to the &struct usb_serial_port data * - * Returns the number of bytes queued on success. This will be zero if there - * was nothing to send. Otherwise, it returns a negative errno value + * Returns zero on success, or a negative errno value */ static int usb_serial_generic_write_start(struct usb_serial_port *port) { - int result; - int count; + struct urb *urb; + int count, result; unsigned long flags; + int i; + if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags)) + return 0; +retry: spin_lock_irqsave(&port->lock, flags); - if (port->write_urb_busy || !kfifo_len(&port->write_fifo)) { + if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) { + clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags); spin_unlock_irqrestore(&port->lock, flags); return 0; } - port->write_urb_busy = 1; + i = (int)find_first_bit(&port->write_urbs_free, + ARRAY_SIZE(port->write_urbs)); spin_unlock_irqrestore(&port->lock, flags); + urb = port->write_urbs[i]; count = port->serial->type->prepare_write_buffer(port, - &port->write_urb->transfer_buffer, - port->bulk_out_size, NULL, 0); - usb_serial_debug_data(debug, &port->dev, __func__, - count, port->write_urb->transfer_buffer); - port->write_urb->transfer_buffer_length = count; - - /* send the data out the bulk port */ - result = usb_submit_urb(port->write_urb, GFP_ATOMIC); + &urb->transfer_buffer, + port->bulk_out_size, NULL, 0); + urb->transfer_buffer_length = count; + usb_serial_debug_data(debug, &port->dev, __func__, count, + urb->transfer_buffer); + result = usb_submit_urb(urb, GFP_ATOMIC); if (result) { dev_err(&port->dev, "%s - error submitting urb: %d\n", __func__, result); - /* don't have to grab the lock here, as we will - retry if != 0 */ - port->write_urb_busy = 0; + clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags); return result; } + clear_bit(i, &port->write_urbs_free); spin_lock_irqsave(&port->lock, flags); port->tx_bytes += count; spin_unlock_irqrestore(&port->lock, flags); - return count; + /* Try sending off another urb, unless in irq context (in which case + * there will be no free urb). */ + if (!in_irq()) + goto retry; + + clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags); + + return 0; } /** @@ -461,6 +475,7 @@ void usb_serial_generic_write_bulk_callback(struct urb *urb) unsigned long flags; struct usb_serial_port *port = urb->context; int status = urb->status; + int i; dbg("%s - port %d", __func__, port->number); @@ -472,9 +487,13 @@ void usb_serial_generic_write_bulk_callback(struct urb *urb) port->tx_urbs--; spin_unlock_irqrestore(&port->lock, flags); } else { + for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) + if (port->write_urbs[i] == urb) + break; + spin_lock_irqsave(&port->lock, flags); port->tx_bytes -= urb->transfer_buffer_length; - port->write_urb_busy = 0; + set_bit(i, &port->write_urbs_free); spin_unlock_irqrestore(&port->lock, flags); if (status) { @@ -576,7 +595,7 @@ int usb_serial_generic_resume(struct usb_serial *serial) c++; } - if (port->write_urb) { + if (port->bulk_out_size) { r = usb_serial_generic_write_start(port); if (r < 0) c++; diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c index 8249fd8381fb..941c2d409f85 100644 --- a/drivers/usb/serial/usb-serial.c +++ b/drivers/usb/serial/usb-serial.c @@ -548,8 +548,12 @@ static void usb_serial_port_work(struct work_struct *work) static void kill_traffic(struct usb_serial_port *port) { + int i; + usb_kill_urb(port->read_urb); usb_kill_urb(port->write_urb); + for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) + usb_kill_urb(port->write_urbs[i]); /* * This is tricky. * Some drivers submit the read_urb in the @@ -568,6 +572,7 @@ static void kill_traffic(struct usb_serial_port *port) static void port_release(struct device *dev) { struct usb_serial_port *port = to_usb_serial_port(dev); + int i; dbg ("%s - %s", __func__, dev_name(dev)); @@ -582,6 +587,10 @@ static void port_release(struct device *dev) usb_free_urb(port->write_urb); usb_free_urb(port->interrupt_in_urb); usb_free_urb(port->interrupt_out_urb); + for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) { + usb_free_urb(port->write_urbs[i]); + kfree(port->bulk_out_buffers[i]); + } kfifo_free(&port->write_fifo); kfree(port->bulk_in_buffer); kfree(port->bulk_out_buffer); @@ -920,6 +929,8 @@ int usb_serial_probe(struct usb_interface *interface, } for (i = 0; i < num_bulk_out; ++i) { + int j; + endpoint = bulk_out_endpoint[i]; port = serial->port[i]; port->write_urb = usb_alloc_urb(0, GFP_KERNEL); @@ -945,6 +956,28 @@ int usb_serial_probe(struct usb_interface *interface, endpoint->bEndpointAddress), port->bulk_out_buffer, buffer_size, serial->type->write_bulk_callback, port); + for (j = 0; j < ARRAY_SIZE(port->write_urbs); ++j) { + set_bit(j, &port->write_urbs_free); + port->write_urbs[j] = usb_alloc_urb(0, GFP_KERNEL); + if (!port->write_urbs[j]) { + dev_err(&interface->dev, + "No free urbs available\n"); + goto probe_error; + } + port->bulk_out_buffers[j] = kmalloc(buffer_size, + GFP_KERNEL); + if (!port->bulk_out_buffers[j]) { + dev_err(&interface->dev, + "Couldn't allocate bulk_out_buffer\n"); + goto probe_error; + } + usb_fill_bulk_urb(port->write_urbs[j], dev, + usb_sndbulkpipe(dev, + endpoint->bEndpointAddress), + port->bulk_out_buffers[j], buffer_size, + serial->type->write_bulk_callback, + port); + } } if (serial->type->read_int_callback) { diff --git a/include/linux/usb/serial.h b/include/linux/usb/serial.h index a4c99ea390e7..70b6d6b28997 100644 --- a/include/linux/usb/serial.h +++ b/include/linux/usb/serial.h @@ -35,6 +35,9 @@ enum port_dev_state { PORT_UNREGISTERING, }; +/* USB serial flags */ +#define USB_SERIAL_WRITE_BUSY 0 + /** * usb_serial_port: structure for the specific ports of a device. * @serial: pointer back to the struct usb_serial owner of this port. @@ -60,10 +63,14 @@ enum port_dev_state { * @write_urb: pointer to the bulk out struct urb for this port. * @write_fifo: kfifo used to buffer outgoing data * @write_urb_busy: port`s writing status + * @bulk_out_buffers: pointers to the bulk out buffers for this port + * @write_urbs: pointers to the bulk out urbs for this port + * @write_urbs_free: status bitmap the for bulk out urbs * @tx_bytes: number of bytes currently in host stack queues * @tx_urbs: number of urbs currently in host stack queues * @bulk_out_endpointAddress: endpoint address for the bulk out pipe for this * port. + * @flags: usb serial port flags * @write_wait: a wait_queue_head_t used by the port. * @work: work queue entry for the line discipline waking up. * @throttled: nonzero if the read urb is inactive to throttle the device @@ -98,11 +105,16 @@ struct usb_serial_port { struct urb *write_urb; struct kfifo write_fifo; int write_urb_busy; + + unsigned char *bulk_out_buffers[2]; + struct urb *write_urbs[2]; + unsigned long write_urbs_free; __u8 bulk_out_endpointAddress; int tx_bytes; int tx_urbs; + unsigned long flags; wait_queue_head_t write_wait; struct work_struct work; char throttled; -- cgit v1.2.3