diff options
author | Johan Hovold <jhovold@gmail.com> | 2010-05-18 18:01:30 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2010-05-20 16:21:49 -0400 |
commit | 241c80ea72be4828c63f5dd44b142e54d0a12f5d (patch) | |
tree | feeea46c468b0cd71596ef44a7767c3d2e3082b5 /drivers/usb/serial/safe_serial.c | |
parent | 39f2f080b39ceb7ccbc0da4bc13fea5698f7fa3a (diff) |
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: Johan Hovold <jhovold@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/usb/serial/safe_serial.c')
-rw-r--r-- | drivers/usb/serial/safe_serial.c | 143 |
1 files changed, 32 insertions, 111 deletions
diff --git a/drivers/usb/serial/safe_serial.c b/drivers/usb/serial/safe_serial.c index 43a0cadd5782..915c094ef92c 100644 --- a/drivers/usb/serial/safe_serial.c +++ b/drivers/usb/serial/safe_serial.c | |||
@@ -1,6 +1,7 @@ | |||
1 | /* | 1 | /* |
2 | * Safe Encapsulated USB Serial Driver | 2 | * Safe Encapsulated USB Serial Driver |
3 | * | 3 | * |
4 | * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com> | ||
4 | * Copyright (C) 2001 Lineo | 5 | * Copyright (C) 2001 Lineo |
5 | * Copyright (C) 2001 Hewlett-Packard | 6 | * Copyright (C) 2001 Hewlett-Packard |
6 | * | 7 | * |
@@ -84,8 +85,8 @@ static int debug; | |||
84 | static int safe = 1; | 85 | static int safe = 1; |
85 | static int padded = CONFIG_USB_SERIAL_SAFE_PADDED; | 86 | static int padded = CONFIG_USB_SERIAL_SAFE_PADDED; |
86 | 87 | ||
87 | #define DRIVER_VERSION "v0.0b" | 88 | #define DRIVER_VERSION "v0.1" |
88 | #define DRIVER_AUTHOR "sl@lineo.com, tbr@lineo.com" | 89 | #define DRIVER_AUTHOR "sl@lineo.com, tbr@lineo.com, Johan Hovold <jhovold@gmail.com>" |
89 | #define DRIVER_DESC "USB Safe Encapsulated Serial" | 90 | #define DRIVER_DESC "USB Safe Encapsulated Serial" |
90 | 91 | ||
91 | MODULE_AUTHOR(DRIVER_AUTHOR); | 92 | MODULE_AUTHOR(DRIVER_AUTHOR); |
@@ -284,119 +285,40 @@ static void safe_read_bulk_callback(struct urb *urb) | |||
284 | /* FIXME: Need a mechanism to retry later if this happens */ | 285 | /* FIXME: Need a mechanism to retry later if this happens */ |
285 | } | 286 | } |
286 | 287 | ||
287 | static int safe_write(struct tty_struct *tty, struct usb_serial_port *port, | 288 | static int safe_prepare_write_buffer(struct usb_serial_port *port, |
288 | const unsigned char *buf, int count) | 289 | void *dest, size_t size) |
289 | { | 290 | { |
290 | unsigned char *data; | 291 | unsigned char *buf = dest; |
291 | int result; | 292 | int count; |
292 | int i; | 293 | int trailer_len; |
293 | int packet_length; | 294 | int pkt_len; |
294 | 295 | __u16 fcs; | |
295 | dbg("safe_write port: %p %d urb: %p count: %d", | 296 | |
296 | port, port->number, port->write_urb, count); | 297 | trailer_len = safe ? 2 : 0; |
297 | 298 | ||
298 | if (!port->write_urb) { | 299 | count = kfifo_out_locked(&port->write_fifo, buf, size - trailer_len, |
299 | dbg("%s - write urb NULL", __func__); | 300 | &port->lock); |
300 | return 0; | 301 | if (!safe) |
301 | } | 302 | return count; |
302 | 303 | ||
303 | dbg("safe_write write_urb: %d transfer_buffer_length", | 304 | /* pad if necessary */ |
304 | port->write_urb->transfer_buffer_length); | 305 | if (padded) { |
305 | 306 | pkt_len = size; | |
306 | if (!port->write_urb->transfer_buffer_length) { | 307 | memset(buf + count, '0', pkt_len - count - trailer_len); |
307 | dbg("%s - write urb transfer_buffer_length zero", __func__); | ||
308 | return 0; | ||
309 | } | ||
310 | if (count == 0) { | ||
311 | dbg("%s - write request of 0 bytes", __func__); | ||
312 | return 0; | ||
313 | } | ||
314 | spin_lock_bh(&port->lock); | ||
315 | if (port->write_urb_busy) { | ||
316 | spin_unlock_bh(&port->lock); | ||
317 | dbg("%s - already writing", __func__); | ||
318 | return 0; | ||
319 | } | ||
320 | port->write_urb_busy = 1; | ||
321 | spin_unlock_bh(&port->lock); | ||
322 | |||
323 | packet_length = port->bulk_out_size; /* get max packetsize */ | ||
324 | |||
325 | i = packet_length - (safe ? 2 : 0); /* get bytes to send */ | ||
326 | count = (count > i) ? i : count; | ||
327 | |||
328 | |||
329 | /* get the data into the transfer buffer */ | ||
330 | data = port->write_urb->transfer_buffer; | ||
331 | memset(data, '0', packet_length); | ||
332 | |||
333 | memcpy(data, buf, count); | ||
334 | |||
335 | if (safe) { | ||
336 | __u16 fcs; | ||
337 | |||
338 | /* pad if necessary */ | ||
339 | if (!padded) | ||
340 | packet_length = count + 2; | ||
341 | /* set count */ | ||
342 | data[packet_length - 2] = count << 2; | ||
343 | data[packet_length - 1] = 0; | ||
344 | |||
345 | /* compute fcs and insert into trailer */ | ||
346 | fcs = fcs_compute10(data, packet_length, CRC10_INITFCS); | ||
347 | data[packet_length - 2] |= fcs >> 8; | ||
348 | data[packet_length - 1] |= fcs & 0xff; | ||
349 | |||
350 | /* set length to send */ | ||
351 | port->write_urb->transfer_buffer_length = packet_length; | ||
352 | } else { | 308 | } else { |
353 | port->write_urb->transfer_buffer_length = count; | 309 | pkt_len = count + trailer_len; |
354 | } | ||
355 | |||
356 | usb_serial_debug_data(debug, &port->dev, __func__, count, | ||
357 | port->write_urb->transfer_buffer); | ||
358 | #ifdef ECHO_TX | ||
359 | { | ||
360 | int i; | ||
361 | unsigned char *cp = port->write_urb->transfer_buffer; | ||
362 | for (i = 0; i < port->write_urb->transfer_buffer_length; i++) { | ||
363 | if ((i % 32) == 0) | ||
364 | printk("\nsu[%02x] ", i); | ||
365 | printk("%02x ", *cp++); | ||
366 | } | ||
367 | printk("\n"); | ||
368 | } | 310 | } |
369 | #endif | ||
370 | port->write_urb->dev = port->serial->dev; | ||
371 | result = usb_submit_urb(port->write_urb, GFP_KERNEL); | ||
372 | if (result) { | ||
373 | port->write_urb_busy = 0; | ||
374 | dev_err(&port->dev, | ||
375 | "%s - failed submitting write urb, error %d\n", | ||
376 | __func__, result); | ||
377 | return 0; | ||
378 | } | ||
379 | dbg("%s urb: %p submitted", __func__, port->write_urb); | ||
380 | 311 | ||
381 | return count; | 312 | /* set count */ |
382 | } | 313 | buf[pkt_len - 2] = count << 2; |
383 | 314 | buf[pkt_len - 1] = 0; | |
384 | static int safe_write_room(struct tty_struct *tty) | ||
385 | { | ||
386 | struct usb_serial_port *port = tty->driver_data; | ||
387 | int room = 0; /* Default: no room */ | ||
388 | unsigned long flags; | ||
389 | |||
390 | dbg("%s", __func__); | ||
391 | 315 | ||
392 | spin_lock_irqsave(&port->lock, flags); | 316 | /* compute fcs and insert into trailer */ |
393 | if (port->write_urb_busy) | 317 | fcs = fcs_compute10(buf, pkt_len, CRC10_INITFCS); |
394 | room = port->bulk_out_size - (safe ? 2 : 0); | 318 | buf[pkt_len - 2] |= fcs >> 8; |
395 | spin_unlock_irqrestore(&port->lock, flags); | 319 | buf[pkt_len - 1] |= fcs & 0xff; |
396 | 320 | ||
397 | if (room) | 321 | return pkt_len; |
398 | dbg("safe_write_room returns %d", room); | ||
399 | return room; | ||
400 | } | 322 | } |
401 | 323 | ||
402 | static int safe_startup(struct usb_serial *serial) | 324 | static int safe_startup(struct usb_serial *serial) |
@@ -421,9 +343,8 @@ static struct usb_serial_driver safe_device = { | |||
421 | .id_table = id_table, | 343 | .id_table = id_table, |
422 | .usb_driver = &safe_driver, | 344 | .usb_driver = &safe_driver, |
423 | .num_ports = 1, | 345 | .num_ports = 1, |
424 | .write = safe_write, | ||
425 | .write_room = safe_write_room, | ||
426 | .read_bulk_callback = safe_read_bulk_callback, | 346 | .read_bulk_callback = safe_read_bulk_callback, |
347 | .prepare_write_buffer = safe_prepare_write_buffer, | ||
427 | .attach = safe_startup, | 348 | .attach = safe_startup, |
428 | }; | 349 | }; |
429 | 350 | ||