diff options
Diffstat (limited to 'drivers/usb/serial/safe_serial.c')
-rw-r--r-- | drivers/usb/serial/safe_serial.c | 231 |
1 files changed, 60 insertions, 171 deletions
diff --git a/drivers/usb/serial/safe_serial.c b/drivers/usb/serial/safe_serial.c index 43a0cadd5782..a36e2313eed0 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); |
@@ -212,191 +213,80 @@ static __u16 __inline__ fcs_compute10(unsigned char *sp, int len, __u16 fcs) | |||
212 | return fcs; | 213 | return fcs; |
213 | } | 214 | } |
214 | 215 | ||
215 | static void safe_read_bulk_callback(struct urb *urb) | 216 | static void safe_process_read_urb(struct urb *urb) |
216 | { | 217 | { |
217 | struct usb_serial_port *port = urb->context; | 218 | struct usb_serial_port *port = urb->context; |
218 | unsigned char *data = urb->transfer_buffer; | 219 | unsigned char *data = urb->transfer_buffer; |
219 | unsigned char length = urb->actual_length; | 220 | unsigned char length = urb->actual_length; |
221 | int actual_length; | ||
220 | struct tty_struct *tty; | 222 | struct tty_struct *tty; |
221 | int result; | 223 | __u16 fcs; |
222 | int status = urb->status; | ||
223 | 224 | ||
224 | dbg("%s", __func__); | 225 | if (!length) |
225 | |||
226 | if (status) { | ||
227 | dbg("%s - nonzero read bulk status received: %d", | ||
228 | __func__, status); | ||
229 | return; | 226 | return; |
230 | } | ||
231 | 227 | ||
232 | dbg("safe_read_bulk_callback length: %d", | ||
233 | port->read_urb->actual_length); | ||
234 | #ifdef ECHO_RCV | ||
235 | { | ||
236 | int i; | ||
237 | unsigned char *cp = port->read_urb->transfer_buffer; | ||
238 | for (i = 0; i < port->read_urb->actual_length; i++) { | ||
239 | if ((i % 32) == 0) | ||
240 | printk("\nru[%02x] ", i); | ||
241 | printk("%02x ", *cp++); | ||
242 | } | ||
243 | printk("\n"); | ||
244 | } | ||
245 | #endif | ||
246 | tty = tty_port_tty_get(&port->port); | 228 | tty = tty_port_tty_get(&port->port); |
247 | if (safe) { | 229 | if (!tty) |
248 | __u16 fcs; | 230 | return; |
249 | fcs = fcs_compute10(data, length, CRC10_INITFCS); | ||
250 | if (!fcs) { | ||
251 | int actual_length = data[length - 2] >> 2; | ||
252 | if (actual_length <= (length - 2)) { | ||
253 | dev_info(&urb->dev->dev, "%s - actual: %d\n", | ||
254 | __func__, actual_length); | ||
255 | tty_insert_flip_string(tty, | ||
256 | data, actual_length); | ||
257 | tty_flip_buffer_push(tty); | ||
258 | } else { | ||
259 | dev_err(&port->dev, | ||
260 | "%s - inconsistent lengths %d:%d\n", | ||
261 | __func__, actual_length, length); | ||
262 | } | ||
263 | } else { | ||
264 | dev_err(&port->dev, "%s - bad CRC %x\n", __func__, fcs); | ||
265 | } | ||
266 | } else { | ||
267 | tty_insert_flip_string(tty, data, length); | ||
268 | tty_flip_buffer_push(tty); | ||
269 | } | ||
270 | tty_kref_put(tty); | ||
271 | |||
272 | /* Continue trying to always read */ | ||
273 | usb_fill_bulk_urb(urb, port->serial->dev, | ||
274 | usb_rcvbulkpipe(port->serial->dev, | ||
275 | port->bulk_in_endpointAddress), | ||
276 | urb->transfer_buffer, urb->transfer_buffer_length, | ||
277 | safe_read_bulk_callback, port); | ||
278 | |||
279 | result = usb_submit_urb(urb, GFP_ATOMIC); | ||
280 | if (result) | ||
281 | dev_err(&port->dev, | ||
282 | "%s - failed resubmitting read urb, error %d\n", | ||
283 | __func__, result); | ||
284 | /* FIXME: Need a mechanism to retry later if this happens */ | ||
285 | } | ||
286 | |||
287 | static int safe_write(struct tty_struct *tty, struct usb_serial_port *port, | ||
288 | const unsigned char *buf, int count) | ||
289 | { | ||
290 | unsigned char *data; | ||
291 | int result; | ||
292 | int i; | ||
293 | int packet_length; | ||
294 | |||
295 | dbg("safe_write port: %p %d urb: %p count: %d", | ||
296 | port, port->number, port->write_urb, count); | ||
297 | |||
298 | if (!port->write_urb) { | ||
299 | dbg("%s - write urb NULL", __func__); | ||
300 | return 0; | ||
301 | } | ||
302 | |||
303 | dbg("safe_write write_urb: %d transfer_buffer_length", | ||
304 | port->write_urb->transfer_buffer_length); | ||
305 | |||
306 | if (!port->write_urb->transfer_buffer_length) { | ||
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 | 231 | ||
345 | /* compute fcs and insert into trailer */ | 232 | if (!safe) |
346 | fcs = fcs_compute10(data, packet_length, CRC10_INITFCS); | 233 | goto out; |
347 | data[packet_length - 2] |= fcs >> 8; | ||
348 | data[packet_length - 1] |= fcs & 0xff; | ||
349 | 234 | ||
350 | /* set length to send */ | 235 | fcs = fcs_compute10(data, length, CRC10_INITFCS); |
351 | port->write_urb->transfer_buffer_length = packet_length; | 236 | if (fcs) { |
352 | } else { | 237 | dev_err(&port->dev, "%s - bad CRC %x\n", __func__, fcs); |
353 | port->write_urb->transfer_buffer_length = count; | 238 | goto err; |
354 | } | 239 | } |
355 | 240 | ||
356 | usb_serial_debug_data(debug, &port->dev, __func__, count, | 241 | actual_length = data[length - 2] >> 2; |
357 | port->write_urb->transfer_buffer); | 242 | if (actual_length > (length - 2)) { |
358 | #ifdef ECHO_TX | 243 | dev_err(&port->dev, "%s - inconsistent lengths %d:%d\n", |
359 | { | 244 | __func__, actual_length, length); |
360 | int i; | 245 | goto err; |
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 | } | ||
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 | } | 246 | } |
379 | dbg("%s urb: %p submitted", __func__, port->write_urb); | 247 | dev_info(&urb->dev->dev, "%s - actual: %d\n", __func__, actual_length); |
380 | 248 | length = actual_length; | |
381 | return count; | 249 | out: |
250 | tty_insert_flip_string(tty, data, length); | ||
251 | tty_flip_buffer_push(tty); | ||
252 | err: | ||
253 | tty_kref_put(tty); | ||
382 | } | 254 | } |
383 | 255 | ||
384 | static int safe_write_room(struct tty_struct *tty) | 256 | static int safe_prepare_write_buffer(struct usb_serial_port *port, |
257 | void *dest, size_t size) | ||
385 | { | 258 | { |
386 | struct usb_serial_port *port = tty->driver_data; | 259 | unsigned char *buf = dest; |
387 | int room = 0; /* Default: no room */ | 260 | int count; |
388 | unsigned long flags; | 261 | int trailer_len; |
262 | int pkt_len; | ||
263 | __u16 fcs; | ||
264 | |||
265 | trailer_len = safe ? 2 : 0; | ||
266 | |||
267 | count = kfifo_out_locked(&port->write_fifo, buf, size - trailer_len, | ||
268 | &port->lock); | ||
269 | if (!safe) | ||
270 | return count; | ||
271 | |||
272 | /* pad if necessary */ | ||
273 | if (padded) { | ||
274 | pkt_len = size; | ||
275 | memset(buf + count, '0', pkt_len - count - trailer_len); | ||
276 | } else { | ||
277 | pkt_len = count + trailer_len; | ||
278 | } | ||
389 | 279 | ||
390 | dbg("%s", __func__); | 280 | /* set count */ |
281 | buf[pkt_len - 2] = count << 2; | ||
282 | buf[pkt_len - 1] = 0; | ||
391 | 283 | ||
392 | spin_lock_irqsave(&port->lock, flags); | 284 | /* compute fcs and insert into trailer */ |
393 | if (port->write_urb_busy) | 285 | fcs = fcs_compute10(buf, pkt_len, CRC10_INITFCS); |
394 | room = port->bulk_out_size - (safe ? 2 : 0); | 286 | buf[pkt_len - 2] |= fcs >> 8; |
395 | spin_unlock_irqrestore(&port->lock, flags); | 287 | buf[pkt_len - 1] |= fcs & 0xff; |
396 | 288 | ||
397 | if (room) | 289 | return pkt_len; |
398 | dbg("safe_write_room returns %d", room); | ||
399 | return room; | ||
400 | } | 290 | } |
401 | 291 | ||
402 | static int safe_startup(struct usb_serial *serial) | 292 | static int safe_startup(struct usb_serial *serial) |
@@ -421,9 +311,8 @@ static struct usb_serial_driver safe_device = { | |||
421 | .id_table = id_table, | 311 | .id_table = id_table, |
422 | .usb_driver = &safe_driver, | 312 | .usb_driver = &safe_driver, |
423 | .num_ports = 1, | 313 | .num_ports = 1, |
424 | .write = safe_write, | 314 | .process_read_urb = safe_process_read_urb, |
425 | .write_room = safe_write_room, | 315 | .prepare_write_buffer = safe_prepare_write_buffer, |
426 | .read_bulk_callback = safe_read_bulk_callback, | ||
427 | .attach = safe_startup, | 316 | .attach = safe_startup, |
428 | }; | 317 | }; |
429 | 318 | ||