Commit b9a44bc1 authored by Elina Pasheva's avatar Elina Pasheva Committed by Linus Torvalds

sierra: driver urb handling improvements

[Folded from eight patches into one as the original set according to the
 author "All of the patches need to be applied to obtain a working product"
 so keeping them split seems unhelpful

 Merge fixes done versus other conflicting changes and moved the
 spin_lock_init from open to setup time -- Alan]

Summary of the changes and code re-organization in this patch:

- The memory for urbs is allocated and urbs are submitted only for the active
  interfaces (instead of pre-allocating these for all interfaces). This will
  save memory especially in the case of using composite devices.
- The code has been re-organized and functionality has been extracted from
  sierra_startup(), sierra_shutdown(), sierra_open(), sierra_close() and added
  in helper functions sierra_release_urb(), sierra_stop_rx_urbs(),
  sierra_submit_rx_urbs() and sierra_setup_urb()

- Added function sierra_release_urb() to free an urb and its transfer
buffer.
- Removed unecessary include file reference and comment.
- Added function sierra_stop_rx_urbs() that takes care of the release of
receive and interrupt urbs. This function is to be called by sierra_close()
whenever an interface is de-activated.
- Added new function sierra_submit_rx_urbs() that handles the submission of
receive urbs and interrupt urbs (if any) during the interface activation.
This function is to be called by sierra_open(). Added a second parameter to
pass the memory allocation (as suggested by Oliver Neukum) so that this
function can be used in post_reset() and resume().
- Added new function sierra_setup_urb() that contains the functionality to
allocate an urb, fill bulk urb using the supplied memory allocation flag
and release urb upon error. Added parameter so that the caller pass the
memory allocation flag for flexibility.
- Moved sierra_close() before sierra_open() to resolve dependencies
introduced by the code reorganization.
- Modified sierra_close() to call sierra_stop_rx_urbs() and
sierra_release_urb() functions added in previous patch.
- Modified sierra_open() to call sierra_setup_urb() and sierra_submit_rx_urbs()
functions; note urbs are allocated and submitted for each activated interface.
- Modified sierra_startup() so that allocation of urbs happens whenever an
interface is activated (urb allocation is moved to sierra_open()).
- Modified sierra_shutdown() so that urbs are freed whenever an interface is
de-activated (urb freeing moved to sierra_close() as shown in previous patch
from the series)
- Removed unecessary data structure from sierra_port_private_data
- Suppress an entry in logs by not re-submitting an urb when usb_submit_urb()
returns -EPERM, as this shows that usb_kill_urb() is running (as suggested by
Oliver Neukum)
Signed-off-by: default avatarElina Pasheva <epasheva@sierrawireless.com>
Signed-off-by: default avatarAlan Cox <alan.cox@linux.intel.com>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 00b040de
...@@ -26,12 +26,10 @@ ...@@ -26,12 +26,10 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/usb.h> #include <linux/usb.h>
#include <linux/usb/serial.h> #include <linux/usb/serial.h>
#include <linux/usb/ch9.h>
#define SWIMS_USB_REQUEST_SetPower 0x00 #define SWIMS_USB_REQUEST_SetPower 0x00
#define SWIMS_USB_REQUEST_SetNmea 0x07 #define SWIMS_USB_REQUEST_SetNmea 0x07
/* per port private data */
#define N_IN_URB 4 #define N_IN_URB 4
#define N_OUT_URB 4 #define N_OUT_URB 4
#define IN_BUFLEN 4096 #define IN_BUFLEN 4096
...@@ -229,7 +227,6 @@ struct sierra_port_private { ...@@ -229,7 +227,6 @@ struct sierra_port_private {
/* Input endpoints and buffers for this port */ /* Input endpoints and buffers for this port */
struct urb *in_urbs[N_IN_URB]; struct urb *in_urbs[N_IN_URB];
char *in_buffer[N_IN_URB];
/* Settings for the port */ /* Settings for the port */
int rts_state; /* Handshaking pins (outputs) */ int rts_state; /* Handshaking pins (outputs) */
...@@ -334,6 +331,17 @@ static int sierra_tiocmset(struct tty_struct *tty, struct file *file, ...@@ -334,6 +331,17 @@ static int sierra_tiocmset(struct tty_struct *tty, struct file *file,
return sierra_send_setup(port); return sierra_send_setup(port);
} }
static void sierra_release_urb(struct urb *urb)
{
struct usb_serial_port *port;
if (urb) {
port = urb->context;
dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
kfree(urb->transfer_buffer);
usb_free_urb(urb);
}
}
static void sierra_outdat_callback(struct urb *urb) static void sierra_outdat_callback(struct urb *urb)
{ {
struct usb_serial_port *port = urb->context; struct usb_serial_port *port = urb->context;
...@@ -458,7 +466,7 @@ static void sierra_indat_callback(struct urb *urb) ...@@ -458,7 +466,7 @@ static void sierra_indat_callback(struct urb *urb)
" received", __func__); " received", __func__);
/* Resubmit urb so we continue receiving */ /* Resubmit urb so we continue receiving */
if (port->port.count && status != -ESHUTDOWN) { if (port->port.count && status != -ESHUTDOWN && status != -EPERM) {
err = usb_submit_urb(urb, GFP_ATOMIC); err = usb_submit_urb(urb, GFP_ATOMIC);
if (err) if (err)
dev_err(&port->dev, "resubmit read urb failed." dev_err(&port->dev, "resubmit read urb failed."
...@@ -550,100 +558,184 @@ static int sierra_write_room(struct tty_struct *tty) ...@@ -550,100 +558,184 @@ static int sierra_write_room(struct tty_struct *tty)
return 2048; return 2048;
} }
static int sierra_open(struct tty_struct *tty, static void sierra_stop_rx_urbs(struct usb_serial_port *port)
struct usb_serial_port *port, struct file *filp)
{ {
struct sierra_port_private *portdata;
struct usb_serial *serial = port->serial;
int i; int i;
struct urb *urb; struct sierra_port_private *portdata = usb_get_serial_port_data(port);
int result;
portdata = usb_get_serial_port_data(port); for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++)
usb_kill_urb(portdata->in_urbs[i]);
dev_dbg(&port->dev, "%s", __func__); usb_kill_urb(port->interrupt_in_urb);
}
/* Set some sane defaults */ static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
portdata->rts_state = 1; {
portdata->dtr_state = 1; int ok_cnt;
int err = -EINVAL;
int i;
struct urb *urb;
struct sierra_port_private *portdata = usb_get_serial_port_data(port);
/* Reset low level data toggle and start reading from endpoints */ ok_cnt = 0;
for (i = 0; i < N_IN_URB; i++) { for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
urb = portdata->in_urbs[i]; urb = portdata->in_urbs[i];
if (!urb) if (!urb)
continue; continue;
if (urb->dev != serial->dev) { err = usb_submit_urb(urb, mem_flags);
dev_dbg(&port->dev, "%s: dev %p != %p", if (err) {
__func__, urb->dev, serial->dev); dev_err(&port->dev, "%s: submit urb failed: %d\n",
continue; __func__, err);
} else {
ok_cnt++;
} }
}
/* if (ok_cnt && port->interrupt_in_urb) {
* make sure endpoint data toggle is synchronized with the err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
* device if (err) {
*/ dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
usb_clear_halt(urb->dev, urb->pipe); __func__, err);
result = usb_submit_urb(urb, GFP_KERNEL);
if (result) {
dev_err(&port->dev, "submit urb %d failed (%d) %d\n",
i, result, urb->transfer_buffer_length);
} }
} }
sierra_send_setup(port); if (ok_cnt > 0) /* at least one rx urb submitted */
return 0;
else
return err;
}
static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
int dir, void *ctx, int len,
gfp_t mem_flags,
usb_complete_t callback)
{
struct urb *urb;
u8 *buf;
if (endpoint == -1)
return NULL;
/* start up the interrupt endpoint if we have one */ urb = usb_alloc_urb(0, mem_flags);
if (port->interrupt_in_urb) { if (urb == NULL) {
result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); dev_dbg(&serial->dev->dev, "%s: alloc for endpoint %d failed\n",
if (result) __func__, endpoint);
dev_err(&port->dev, "submit irq_in urb failed %d\n", return NULL;
result);
} }
return 0;
buf = kmalloc(len, mem_flags);
if (buf) {
/* Fill URB using supplied data */
usb_fill_bulk_urb(urb, serial->dev,
usb_sndbulkpipe(serial->dev, endpoint) | dir,
buf, len, callback, ctx);
/* debug */
dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
} else {
dev_dbg(&serial->dev->dev, "%s %c u:%p d:%p\n", __func__,
dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
sierra_release_urb(urb);
urb = NULL;
}
return urb;
} }
static void sierra_dtr_rts(struct usb_serial_port *port, int on) static void sierra_close(struct usb_serial_port *port)
{ {
int i;
struct usb_serial *serial = port->serial; struct usb_serial *serial = port->serial;
struct sierra_port_private *portdata; struct sierra_port_private *portdata;
dev_dbg(&port->dev, "%s\n", __func__);
portdata = usb_get_serial_port_data(port); portdata = usb_get_serial_port_data(port);
portdata->rts_state = on;
portdata->dtr_state = on; portdata->rts_state = 0;
portdata->dtr_state = 0;
if (serial->dev) { if (serial->dev) {
mutex_lock(&serial->disc_mutex); mutex_lock(&serial->disc_mutex);
if (!serial->disconnected) if (!serial->disconnected)
sierra_send_setup(port); sierra_send_setup(port);
mutex_unlock(&serial->disc_mutex); mutex_unlock(&serial->disc_mutex);
/* Stop reading urbs */
sierra_stop_rx_urbs(port);
/* .. and release them */
for (i = 0; i < N_IN_URB; i++) {
sierra_release_urb(portdata->in_urbs[i]);
portdata->in_urbs[i] = NULL;
}
} }
} }
static void sierra_close(struct usb_serial_port *port) static int sierra_open(struct tty_struct *tty,
struct usb_serial_port *port, struct file *filp)
{ {
struct sierra_port_private *portdata;
struct usb_serial *serial = port->serial;
int i; int i;
int err;
int endpoint;
struct urb *urb;
portdata = usb_get_serial_port_data(port);
dev_dbg(&port->dev, "%s", __func__);
/* Set some sane defaults */
portdata->rts_state = 1;
portdata->dtr_state = 1;
endpoint = port->bulk_in_endpointAddress;
for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
IN_BUFLEN, GFP_KERNEL,
sierra_indat_callback);
portdata->in_urbs[i] = urb;
}
/* clear halt condition */
usb_clear_halt(serial->dev,
usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
err = sierra_submit_rx_urbs(port, GFP_KERNEL);
if (err) {
/* get rid of everything as in close */
sierra_close(port);
return err;
}
sierra_send_setup(port);
return 0;
}
static void sierra_dtr_rts(struct usb_serial_port *port, int on)
{
struct usb_serial *serial = port->serial; struct usb_serial *serial = port->serial;
struct sierra_port_private *portdata; struct sierra_port_private *portdata;
dev_dbg(&port->dev, "%s", __func__);
portdata = usb_get_serial_port_data(port); portdata = usb_get_serial_port_data(port);
portdata->rts_state = on;
portdata->dtr_state = on;
if (serial->dev) { if (serial->dev) {
/* Stop reading/writing urbs */ mutex_lock(&serial->disc_mutex);
for (i = 0; i < N_IN_URB; i++) if (!serial->disconnected)
usb_kill_urb(portdata->in_urbs[i]); sierra_send_setup(port);
mutex_unlock(&serial->disc_mutex);
} }
usb_kill_urb(port->interrupt_in_urb);
} }
static int sierra_startup(struct usb_serial *serial) static int sierra_startup(struct usb_serial *serial)
{ {
struct usb_serial_port *port; struct usb_serial_port *port;
struct sierra_port_private *portdata; struct sierra_port_private *portdata;
struct urb *urb;
int i; int i;
int j;
dev_dbg(&serial->dev->dev, "%s", __func__); dev_dbg(&serial->dev->dev, "%s", __func__);
...@@ -665,34 +757,8 @@ static int sierra_startup(struct usb_serial *serial) ...@@ -665,34 +757,8 @@ static int sierra_startup(struct usb_serial *serial)
return -ENOMEM; return -ENOMEM;
} }
spin_lock_init(&portdata->lock); spin_lock_init(&portdata->lock);
for (j = 0; j < N_IN_URB; j++) { /* Set the port private data pointer */
portdata->in_buffer[j] = kmalloc(IN_BUFLEN, GFP_KERNEL);
if (!portdata->in_buffer[j]) {
for (--j; j >= 0; j--)
kfree(portdata->in_buffer[j]);
kfree(portdata);
return -ENOMEM;
}
}
usb_set_serial_port_data(port, portdata); usb_set_serial_port_data(port, portdata);
/* initialize the in urbs */
for (j = 0; j < N_IN_URB; ++j) {
urb = usb_alloc_urb(0, GFP_KERNEL);
if (urb == NULL) {
dev_dbg(&port->dev, "%s: alloc for in "
"port failed.", __func__);
continue;
}
/* Fill URB using supplied data. */
usb_fill_bulk_urb(urb, serial->dev,
usb_rcvbulkpipe(serial->dev,
port->bulk_in_endpointAddress),
portdata->in_buffer[j], IN_BUFLEN,
sierra_indat_callback, port);
portdata->in_urbs[j] = urb;
}
} }
return 0; return 0;
...@@ -700,7 +766,7 @@ static int sierra_startup(struct usb_serial *serial) ...@@ -700,7 +766,7 @@ static int sierra_startup(struct usb_serial *serial)
static void sierra_shutdown(struct usb_serial *serial) static void sierra_shutdown(struct usb_serial *serial)
{ {
int i, j; int i;
struct usb_serial_port *port; struct usb_serial_port *port;
struct sierra_port_private *portdata; struct sierra_port_private *portdata;
...@@ -713,12 +779,6 @@ static void sierra_shutdown(struct usb_serial *serial) ...@@ -713,12 +779,6 @@ static void sierra_shutdown(struct usb_serial *serial)
portdata = usb_get_serial_port_data(port); portdata = usb_get_serial_port_data(port);
if (!portdata) if (!portdata)
continue; continue;
for (j = 0; j < N_IN_URB; j++) {
usb_kill_urb(portdata->in_urbs[j]);
usb_free_urb(portdata->in_urbs[j]);
kfree(portdata->in_buffer[j]);
}
kfree(portdata); kfree(portdata);
usb_set_serial_port_data(port, NULL); usb_set_serial_port_data(port, NULL);
} }
......
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