Commit 253ca923 authored by Joris van Rantwijk's avatar Joris van Rantwijk Committed by Greg Kroah-Hartman

USB: add flow control to usb-serial generic driver.

I added two fields to struct usb_serial_port to keep track of the
throttle state. Other usb-serial drivers typically use private data for
such things, but the generic driver can not really do that because some
of its code is also used by other drivers (which may have their own
private data needs).

As it is, I am not sure that this patch is useful in all scenarios.
It is certainly helpful for low-bandwidth devices that can hold their
data in response to throttling. But for devices that pump data in
real-time as fast as possible (webcam, A/D converter, etc), throttling
may actually cause more data loss.

From: Joris van Rantwijk <jorispubl@xs4all.nl>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent b544d749
...@@ -66,6 +66,8 @@ struct usb_serial_driver usb_serial_generic_device = { ...@@ -66,6 +66,8 @@ struct usb_serial_driver usb_serial_generic_device = {
.num_bulk_out = NUM_DONT_CARE, .num_bulk_out = NUM_DONT_CARE,
.num_ports = 1, .num_ports = 1,
.shutdown = usb_serial_generic_shutdown, .shutdown = usb_serial_generic_shutdown,
.throttle = usb_serial_generic_throttle,
.unthrottle = usb_serial_generic_unthrottle,
}; };
static int generic_probe(struct usb_interface *interface, static int generic_probe(struct usb_interface *interface,
...@@ -115,6 +117,7 @@ int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp) ...@@ -115,6 +117,7 @@ int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
{ {
struct usb_serial *serial = port->serial; struct usb_serial *serial = port->serial;
int result = 0; int result = 0;
unsigned long flags;
dbg("%s - port %d", __FUNCTION__, port->number); dbg("%s - port %d", __FUNCTION__, port->number);
...@@ -124,7 +127,13 @@ int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp) ...@@ -124,7 +127,13 @@ int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
if (port->tty) if (port->tty)
port->tty->low_latency = 1; port->tty->low_latency = 1;
/* if we have a bulk interrupt, start reading from it */ /* clear the throttle flags */
spin_lock_irqsave(&port->lock, flags);
port->throttled = 0;
port->throttle_req = 0;
spin_unlock_irqrestore(&port->lock, flags);
/* if we have a bulk endpoint, start reading from it */
if (serial->num_bulk_in) { if (serial->num_bulk_in) {
/* Start reading from the device */ /* Start reading from the device */
usb_fill_bulk_urb (port->read_urb, serial->dev, usb_fill_bulk_urb (port->read_urb, serial->dev,
...@@ -253,31 +262,22 @@ int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port) ...@@ -253,31 +262,22 @@ int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
return (chars); return (chars);
} }
void usb_serial_generic_read_bulk_callback (struct urb *urb) /* Push data to tty layer and resubmit the bulk read URB */
static void flush_and_resubmit_read_urb (struct usb_serial_port *port)
{ {
struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
struct usb_serial *serial = port->serial; struct usb_serial *serial = port->serial;
struct tty_struct *tty; struct urb *urb = port->read_urb;
unsigned char *data = urb->transfer_buffer; struct tty_struct *tty = port->tty;
int result; int result;
dbg("%s - port %d", __FUNCTION__, port->number); /* Push data to tty */
if (urb->status) {
dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
return;
}
usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
tty = port->tty;
if (tty && urb->actual_length) { if (tty && urb->actual_length) {
tty_buffer_request_room(tty, urb->actual_length); tty_buffer_request_room(tty, urb->actual_length);
tty_insert_flip_string(tty, data, urb->actual_length); tty_insert_flip_string(tty, urb->transfer_buffer, urb->actual_length);
tty_flip_buffer_push(tty); tty_flip_buffer_push(tty); /* is this allowed from an URB callback ? */
} }
/* Continue trying to always read */ /* Continue reading from device */
usb_fill_bulk_urb (port->read_urb, serial->dev, usb_fill_bulk_urb (port->read_urb, serial->dev,
usb_rcvbulkpipe (serial->dev, usb_rcvbulkpipe (serial->dev,
port->bulk_in_endpointAddress), port->bulk_in_endpointAddress),
...@@ -290,6 +290,40 @@ void usb_serial_generic_read_bulk_callback (struct urb *urb) ...@@ -290,6 +290,40 @@ void usb_serial_generic_read_bulk_callback (struct urb *urb)
if (result) if (result)
dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result); dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
} }
void usb_serial_generic_read_bulk_callback (struct urb *urb)
{
struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
unsigned char *data = urb->transfer_buffer;
int is_throttled;
unsigned long flags;
dbg("%s - port %d", __FUNCTION__, port->number);
if (urb->status) {
dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
return;
}
usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
/* Throttle the device if requested by tty */
if (urb->actual_length) {
spin_lock_irqsave(&port->lock, flags);
is_throttled = port->throttled = port->throttle_req;
spin_unlock_irqrestore(&port->lock, flags);
if (is_throttled) {
/* Let the received data linger in the read URB;
* usb_serial_generic_unthrottle() will pick it
* up later. */
dbg("%s - throttling device", __FUNCTION__);
return;
}
}
/* Handle data and continue reading from device */
flush_and_resubmit_read_urb(port);
}
EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback); EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
void usb_serial_generic_write_bulk_callback (struct urb *urb) void usb_serial_generic_write_bulk_callback (struct urb *urb)
...@@ -308,6 +342,38 @@ void usb_serial_generic_write_bulk_callback (struct urb *urb) ...@@ -308,6 +342,38 @@ void usb_serial_generic_write_bulk_callback (struct urb *urb)
} }
EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback); EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
void usb_serial_generic_throttle (struct usb_serial_port *port)
{
unsigned long flags;
dbg("%s - port %d", __FUNCTION__, port->number);
/* Set the throttle request flag. It will be picked up
* by usb_serial_generic_read_bulk_callback(). */
spin_lock_irqsave(&port->lock, flags);
port->throttle_req = 1;
spin_unlock_irqrestore(&port->lock, flags);
}
void usb_serial_generic_unthrottle (struct usb_serial_port *port)
{
int was_throttled;
unsigned long flags;
dbg("%s - port %d", __FUNCTION__, port->number);
/* Clear the throttle flags */
spin_lock_irqsave(&port->lock, flags);
was_throttled = port->throttled;
port->throttled = port->throttle_req = 0;
spin_unlock_irqrestore(&port->lock, flags);
if (was_throttled) {
/* Handle pending data and resume reading from device */
flush_and_resubmit_read_urb(port);
}
}
void usb_serial_generic_shutdown (struct usb_serial *serial) void usb_serial_generic_shutdown (struct usb_serial *serial)
{ {
int i; int i;
......
...@@ -54,6 +54,8 @@ ...@@ -54,6 +54,8 @@
* @write_wait: a wait_queue_head_t used by the port. * @write_wait: a wait_queue_head_t used by the port.
* @work: work queue entry for the line discipline waking up. * @work: work queue entry for the line discipline waking up.
* @open_count: number of times this port has been opened. * @open_count: number of times this port has been opened.
* @throttled: nonzero if the read urb is inactive to throttle the device
* @throttle_req: nonzero if the tty wants to throttle us
* *
* This structure is used by the usb-serial core and drivers for the specific * This structure is used by the usb-serial core and drivers for the specific
* ports of a device. * ports of a device.
...@@ -88,6 +90,8 @@ struct usb_serial_port { ...@@ -88,6 +90,8 @@ struct usb_serial_port {
wait_queue_head_t write_wait; wait_queue_head_t write_wait;
struct work_struct work; struct work_struct work;
int open_count; int open_count;
char throttled;
char throttle_req;
struct device dev; struct device dev;
}; };
#define to_usb_serial_port(d) container_of(d, struct usb_serial_port, dev) #define to_usb_serial_port(d) container_of(d, struct usb_serial_port, dev)
...@@ -269,6 +273,8 @@ extern int usb_serial_generic_write_room (struct usb_serial_port *port); ...@@ -269,6 +273,8 @@ extern int usb_serial_generic_write_room (struct usb_serial_port *port);
extern int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port); extern int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port);
extern void usb_serial_generic_read_bulk_callback (struct urb *urb); extern void usb_serial_generic_read_bulk_callback (struct urb *urb);
extern void usb_serial_generic_write_bulk_callback (struct urb *urb); extern void usb_serial_generic_write_bulk_callback (struct urb *urb);
extern void usb_serial_generic_throttle (struct usb_serial_port *port);
extern void usb_serial_generic_unthrottle (struct usb_serial_port *port);
extern void usb_serial_generic_shutdown (struct usb_serial *serial); extern void usb_serial_generic_shutdown (struct usb_serial *serial);
extern int usb_serial_generic_register (int debug); extern int usb_serial_generic_register (int debug);
extern void usb_serial_generic_deregister (void); extern void usb_serial_generic_deregister (void);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment