Commit 241c80ea authored by Johan Hovold's avatar Johan Hovold Committed by Greg Kroah-Hartman

USB: safe_serial: reimplement write using generic framework

Kill custom single-urb write implementation.

Note that this driver still depended on the write callback from the old
generic framework.

Tested against original read processing using a cp210x device in a
loopback setup.
Signed-off-by: default avatarJohan Hovold <jhovold@gmail.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 39f2f080
/* /*
* Safe Encapsulated USB Serial Driver * Safe Encapsulated USB Serial Driver
* *
* Copyright (C) 2010 Johan Hovold <jhovold@gmail.com>
* Copyright (C) 2001 Lineo * Copyright (C) 2001 Lineo
* Copyright (C) 2001 Hewlett-Packard * Copyright (C) 2001 Hewlett-Packard
* *
...@@ -84,8 +85,8 @@ static int debug; ...@@ -84,8 +85,8 @@ static int debug;
static int safe = 1; static int safe = 1;
static int padded = CONFIG_USB_SERIAL_SAFE_PADDED; static int padded = CONFIG_USB_SERIAL_SAFE_PADDED;
#define DRIVER_VERSION "v0.0b" #define DRIVER_VERSION "v0.1"
#define DRIVER_AUTHOR "sl@lineo.com, tbr@lineo.com" #define DRIVER_AUTHOR "sl@lineo.com, tbr@lineo.com, Johan Hovold <jhovold@gmail.com>"
#define DRIVER_DESC "USB Safe Encapsulated Serial" #define DRIVER_DESC "USB Safe Encapsulated Serial"
MODULE_AUTHOR(DRIVER_AUTHOR); MODULE_AUTHOR(DRIVER_AUTHOR);
...@@ -284,119 +285,40 @@ static void safe_read_bulk_callback(struct urb *urb) ...@@ -284,119 +285,40 @@ static void safe_read_bulk_callback(struct urb *urb)
/* FIXME: Need a mechanism to retry later if this happens */ /* FIXME: Need a mechanism to retry later if this happens */
} }
static int safe_write(struct tty_struct *tty, struct usb_serial_port *port, static int safe_prepare_write_buffer(struct usb_serial_port *port,
const unsigned char *buf, int count) void *dest, size_t size)
{ {
unsigned char *data; unsigned char *buf = dest;
int result; int count;
int i; int trailer_len;
int packet_length; int pkt_len;
__u16 fcs;
dbg("safe_write port: %p %d urb: %p count: %d",
port, port->number, port->write_urb, count); trailer_len = safe ? 2 : 0;
if (!port->write_urb) { count = kfifo_out_locked(&port->write_fifo, buf, size - trailer_len,
dbg("%s - write urb NULL", __func__); &port->lock);
return 0; if (!safe)
} return count;
dbg("safe_write write_urb: %d transfer_buffer_length", /* pad if necessary */
port->write_urb->transfer_buffer_length); if (padded) {
pkt_len = size;
if (!port->write_urb->transfer_buffer_length) { memset(buf + count, '0', pkt_len - count - trailer_len);
dbg("%s - write urb transfer_buffer_length zero", __func__);
return 0;
}
if (count == 0) {
dbg("%s - write request of 0 bytes", __func__);
return 0;
}
spin_lock_bh(&port->lock);
if (port->write_urb_busy) {
spin_unlock_bh(&port->lock);
dbg("%s - already writing", __func__);
return 0;
}
port->write_urb_busy = 1;
spin_unlock_bh(&port->lock);
packet_length = port->bulk_out_size; /* get max packetsize */
i = packet_length - (safe ? 2 : 0); /* get bytes to send */
count = (count > i) ? i : count;
/* get the data into the transfer buffer */
data = port->write_urb->transfer_buffer;
memset(data, '0', packet_length);
memcpy(data, buf, count);
if (safe) {
__u16 fcs;
/* pad if necessary */
if (!padded)
packet_length = count + 2;
/* set count */
data[packet_length - 2] = count << 2;
data[packet_length - 1] = 0;
/* compute fcs and insert into trailer */
fcs = fcs_compute10(data, packet_length, CRC10_INITFCS);
data[packet_length - 2] |= fcs >> 8;
data[packet_length - 1] |= fcs & 0xff;
/* set length to send */
port->write_urb->transfer_buffer_length = packet_length;
} else { } else {
port->write_urb->transfer_buffer_length = count; pkt_len = count + trailer_len;
}
usb_serial_debug_data(debug, &port->dev, __func__, count,
port->write_urb->transfer_buffer);
#ifdef ECHO_TX
{
int i;
unsigned char *cp = port->write_urb->transfer_buffer;
for (i = 0; i < port->write_urb->transfer_buffer_length; i++) {
if ((i % 32) == 0)
printk("\nsu[%02x] ", i);
printk("%02x ", *cp++);
}
printk("\n");
} }
#endif
port->write_urb->dev = port->serial->dev;
result = usb_submit_urb(port->write_urb, GFP_KERNEL);
if (result) {
port->write_urb_busy = 0;
dev_err(&port->dev,
"%s - failed submitting write urb, error %d\n",
__func__, result);
return 0;
}
dbg("%s urb: %p submitted", __func__, port->write_urb);
return count; /* set count */
} buf[pkt_len - 2] = count << 2;
buf[pkt_len - 1] = 0;
static int safe_write_room(struct tty_struct *tty)
{
struct usb_serial_port *port = tty->driver_data;
int room = 0; /* Default: no room */
unsigned long flags;
dbg("%s", __func__);
spin_lock_irqsave(&port->lock, flags); /* compute fcs and insert into trailer */
if (port->write_urb_busy) fcs = fcs_compute10(buf, pkt_len, CRC10_INITFCS);
room = port->bulk_out_size - (safe ? 2 : 0); buf[pkt_len - 2] |= fcs >> 8;
spin_unlock_irqrestore(&port->lock, flags); buf[pkt_len - 1] |= fcs & 0xff;
if (room) return pkt_len;
dbg("safe_write_room returns %d", room);
return room;
} }
static int safe_startup(struct usb_serial *serial) static int safe_startup(struct usb_serial *serial)
...@@ -421,9 +343,8 @@ static struct usb_serial_driver safe_device = { ...@@ -421,9 +343,8 @@ static struct usb_serial_driver safe_device = {
.id_table = id_table, .id_table = id_table,
.usb_driver = &safe_driver, .usb_driver = &safe_driver,
.num_ports = 1, .num_ports = 1,
.write = safe_write,
.write_room = safe_write_room,
.read_bulk_callback = safe_read_bulk_callback, .read_bulk_callback = safe_read_bulk_callback,
.prepare_write_buffer = safe_prepare_write_buffer,
.attach = safe_startup, .attach = safe_startup,
}; };
......
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