提交 eaa3bcb0 编写于 作者: J Johan Hovold 提交者: Greg Kroah-Hartman

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: NJohan Hovold <jhovold@gmail.com>
Signed-off-by: NGreg Kroah-Hartman <gregkh@suse.de>
上级 25d514ca
...@@ -167,12 +167,35 @@ void usb_serial_generic_close(struct usb_serial_port *port) ...@@ -167,12 +167,35 @@ void usb_serial_generic_close(struct usb_serial_port *port)
} }
EXPORT_SYMBOL_GPL(usb_serial_generic_close); 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, static int usb_serial_multi_urb_write(struct tty_struct *tty,
struct usb_serial_port *port, const unsigned char *buf, int count) struct usb_serial_port *port, const unsigned char *buf, int count)
{ {
unsigned long flags; unsigned long flags;
struct urb *urb; struct urb *urb;
unsigned char *buffer; void *buffer;
int status; int status;
spin_lock_irqsave(&port->lock, flags); spin_lock_irqsave(&port->lock, flags);
...@@ -191,16 +214,14 @@ static int usb_serial_multi_urb_write(struct tty_struct *tty, ...@@ -191,16 +214,14 @@ static int usb_serial_multi_urb_write(struct tty_struct *tty,
goto err_urb; goto err_urb;
} }
buffer = NULL;
count = min_t(int, count, PAGE_SIZE); count = min_t(int, count, PAGE_SIZE);
buffer = kmalloc(count, GFP_ATOMIC); count = port->serial->type->prepare_write_buffer(port, &buffer, 0,
if (!buffer) { buf, count);
dev_err(&port->dev, "%s - could not allocate buffer\n", if (count < 0) {
__func__); status = count;
status = -ENOMEM;
goto err_buf; goto err_buf;
} }
memcpy(buffer, buf, count);
usb_serial_debug_data(debug, &port->dev, __func__, count, buffer); usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
usb_fill_bulk_urb(urb, port->serial->dev, usb_fill_bulk_urb(urb, port->serial->dev,
usb_sndbulkpipe(port->serial->dev, usb_sndbulkpipe(port->serial->dev,
...@@ -242,7 +263,6 @@ static int usb_serial_multi_urb_write(struct tty_struct *tty, ...@@ -242,7 +263,6 @@ static int usb_serial_multi_urb_write(struct tty_struct *tty,
*/ */
static int usb_serial_generic_write_start(struct usb_serial_port *port) static int usb_serial_generic_write_start(struct usb_serial_port *port)
{ {
unsigned char *data;
int result; int result;
int count; int count;
unsigned long flags; unsigned long flags;
...@@ -255,10 +275,11 @@ static int usb_serial_generic_write_start(struct usb_serial_port *port) ...@@ -255,10 +275,11 @@ static int usb_serial_generic_write_start(struct usb_serial_port *port)
port->write_urb_busy = 1; port->write_urb_busy = 1;
spin_unlock_irqrestore(&port->lock, flags); spin_unlock_irqrestore(&port->lock, flags);
data = port->write_urb->transfer_buffer; count = port->serial->type->prepare_write_buffer(port,
count = kfifo_out_locked(&port->write_fifo, data, port->bulk_out_size, &port->lock); &port->write_urb->transfer_buffer,
usb_serial_debug_data(debug, &port->dev, __func__, count, data); 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; port->write_urb->transfer_buffer_length = count;
/* send the data out the bulk port */ /* send the data out the bulk port */
......
...@@ -1299,6 +1299,7 @@ static void fixup_generic(struct usb_serial_driver *device) ...@@ -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, disconnect);
set_to_generic_if_null(device, release); set_to_generic_if_null(device, release);
set_to_generic_if_null(device, process_read_urb); 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) int usb_serial_register(struct usb_serial_driver *driver)
......
...@@ -277,6 +277,9 @@ struct usb_serial_driver { ...@@ -277,6 +277,9 @@ struct usb_serial_driver {
void (*write_bulk_callback)(struct urb *urb); void (*write_bulk_callback)(struct urb *urb);
/* Called by the generic read bulk callback */ /* Called by the generic read bulk callback */
void (*process_read_urb)(struct urb *urb); 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) \ #define to_usb_serial_driver(d) \
container_of(d, struct usb_serial_driver, driver) container_of(d, struct usb_serial_driver, driver)
...@@ -329,6 +332,8 @@ extern void usb_serial_generic_deregister(void); ...@@ -329,6 +332,8 @@ extern void usb_serial_generic_deregister(void);
extern int usb_serial_generic_submit_read_urb(struct usb_serial_port *port, extern int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
gfp_t mem_flags); gfp_t mem_flags);
extern void usb_serial_generic_process_read_urb(struct urb *urb); 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, extern int usb_serial_handle_sysrq_char(struct tty_struct *tty,
struct usb_serial_port *port, struct usb_serial_port *port,
unsigned int ch); unsigned int ch);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册