diff options
Diffstat (limited to 'drivers/usb/serial/generic.c')
-rw-r--r-- | drivers/usb/serial/generic.c | 211 |
1 files changed, 127 insertions, 84 deletions
diff --git a/drivers/usb/serial/generic.c b/drivers/usb/serial/generic.c index ce57f6a32bdf..bbe005cefcfb 100644 --- a/drivers/usb/serial/generic.c +++ b/drivers/usb/serial/generic.c | |||
@@ -19,7 +19,7 @@ | |||
19 | #include <linux/usb.h> | 19 | #include <linux/usb.h> |
20 | #include <linux/usb/serial.h> | 20 | #include <linux/usb/serial.h> |
21 | #include <linux/uaccess.h> | 21 | #include <linux/uaccess.h> |
22 | 22 | #include <linux/kfifo.h> | |
23 | 23 | ||
24 | static int debug; | 24 | static int debug; |
25 | 25 | ||
@@ -114,8 +114,7 @@ void usb_serial_generic_deregister(void) | |||
114 | #endif | 114 | #endif |
115 | } | 115 | } |
116 | 116 | ||
117 | int usb_serial_generic_open(struct tty_struct *tty, | 117 | int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port) |
118 | struct usb_serial_port *port, struct file *filp) | ||
119 | { | 118 | { |
120 | struct usb_serial *serial = port->serial; | 119 | struct usb_serial *serial = port->serial; |
121 | int result = 0; | 120 | int result = 0; |
@@ -167,24 +166,6 @@ static void generic_cleanup(struct usb_serial_port *port) | |||
167 | } | 166 | } |
168 | } | 167 | } |
169 | 168 | ||
170 | int usb_serial_generic_resume(struct usb_serial *serial) | ||
171 | { | ||
172 | struct usb_serial_port *port; | ||
173 | int i, c = 0, r; | ||
174 | |||
175 | for (i = 0; i < serial->num_ports; i++) { | ||
176 | port = serial->port[i]; | ||
177 | if (port->port.count && port->read_urb) { | ||
178 | r = usb_submit_urb(port->read_urb, GFP_NOIO); | ||
179 | if (r < 0) | ||
180 | c++; | ||
181 | } | ||
182 | } | ||
183 | |||
184 | return c ? -EIO : 0; | ||
185 | } | ||
186 | EXPORT_SYMBOL_GPL(usb_serial_generic_resume); | ||
187 | |||
188 | void usb_serial_generic_close(struct usb_serial_port *port) | 169 | void usb_serial_generic_close(struct usb_serial_port *port) |
189 | { | 170 | { |
190 | dbg("%s - port %d", __func__, port->number); | 171 | dbg("%s - port %d", __func__, port->number); |
@@ -273,12 +254,81 @@ error_no_buffer: | |||
273 | return bwrite; | 254 | return bwrite; |
274 | } | 255 | } |
275 | 256 | ||
257 | /** | ||
258 | * usb_serial_generic_write_start - kick off an URB write | ||
259 | * @port: Pointer to the &struct usb_serial_port data | ||
260 | * | ||
261 | * Returns the number of bytes queued on success. This will be zero if there | ||
262 | * was nothing to send. Otherwise, it returns a negative errno value | ||
263 | */ | ||
264 | static int usb_serial_generic_write_start(struct usb_serial_port *port) | ||
265 | { | ||
266 | struct usb_serial *serial = port->serial; | ||
267 | unsigned char *data; | ||
268 | int result; | ||
269 | int count; | ||
270 | unsigned long flags; | ||
271 | bool start_io; | ||
272 | |||
273 | /* Atomically determine whether we can and need to start a USB | ||
274 | * operation. */ | ||
275 | spin_lock_irqsave(&port->lock, flags); | ||
276 | if (port->write_urb_busy) | ||
277 | start_io = false; | ||
278 | else { | ||
279 | start_io = (__kfifo_len(port->write_fifo) != 0); | ||
280 | port->write_urb_busy = start_io; | ||
281 | } | ||
282 | spin_unlock_irqrestore(&port->lock, flags); | ||
283 | |||
284 | if (!start_io) | ||
285 | return 0; | ||
286 | |||
287 | data = port->write_urb->transfer_buffer; | ||
288 | count = kfifo_get(port->write_fifo, data, port->bulk_out_size); | ||
289 | usb_serial_debug_data(debug, &port->dev, __func__, count, data); | ||
290 | |||
291 | /* set up our urb */ | ||
292 | usb_fill_bulk_urb(port->write_urb, serial->dev, | ||
293 | usb_sndbulkpipe(serial->dev, | ||
294 | port->bulk_out_endpointAddress), | ||
295 | port->write_urb->transfer_buffer, count, | ||
296 | ((serial->type->write_bulk_callback) ? | ||
297 | serial->type->write_bulk_callback : | ||
298 | usb_serial_generic_write_bulk_callback), | ||
299 | port); | ||
300 | |||
301 | /* send the data out the bulk port */ | ||
302 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); | ||
303 | if (result) { | ||
304 | dev_err(&port->dev, | ||
305 | "%s - failed submitting write urb, error %d\n", | ||
306 | __func__, result); | ||
307 | /* don't have to grab the lock here, as we will | ||
308 | retry if != 0 */ | ||
309 | port->write_urb_busy = 0; | ||
310 | } else | ||
311 | result = count; | ||
312 | |||
313 | return result; | ||
314 | } | ||
315 | |||
316 | /** | ||
317 | * usb_serial_generic_write - generic write function for serial USB devices | ||
318 | * @tty: Pointer to &struct tty_struct for the device | ||
319 | * @port: Pointer to the &usb_serial_port structure for the device | ||
320 | * @buf: Pointer to the data to write | ||
321 | * @count: Number of bytes to write | ||
322 | * | ||
323 | * Returns the number of characters actually written, which may be anything | ||
324 | * from zero to @count. If an error occurs, it returns the negative errno | ||
325 | * value. | ||
326 | */ | ||
276 | int usb_serial_generic_write(struct tty_struct *tty, | 327 | int usb_serial_generic_write(struct tty_struct *tty, |
277 | struct usb_serial_port *port, const unsigned char *buf, int count) | 328 | struct usb_serial_port *port, const unsigned char *buf, int count) |
278 | { | 329 | { |
279 | struct usb_serial *serial = port->serial; | 330 | struct usb_serial *serial = port->serial; |
280 | int result; | 331 | int result; |
281 | unsigned char *data; | ||
282 | 332 | ||
283 | dbg("%s - port %d", __func__, port->number); | 333 | dbg("%s - port %d", __func__, port->number); |
284 | 334 | ||
@@ -288,57 +338,20 @@ int usb_serial_generic_write(struct tty_struct *tty, | |||
288 | } | 338 | } |
289 | 339 | ||
290 | /* only do something if we have a bulk out endpoint */ | 340 | /* only do something if we have a bulk out endpoint */ |
291 | if (serial->num_bulk_out) { | 341 | if (!serial->num_bulk_out) |
292 | unsigned long flags; | 342 | return 0; |
293 | |||
294 | if (serial->type->max_in_flight_urbs) | ||
295 | return usb_serial_multi_urb_write(tty, port, | ||
296 | buf, count); | ||
297 | |||
298 | spin_lock_irqsave(&port->lock, flags); | ||
299 | if (port->write_urb_busy) { | ||
300 | spin_unlock_irqrestore(&port->lock, flags); | ||
301 | dbg("%s - already writing", __func__); | ||
302 | return 0; | ||
303 | } | ||
304 | port->write_urb_busy = 1; | ||
305 | spin_unlock_irqrestore(&port->lock, flags); | ||
306 | |||
307 | count = (count > port->bulk_out_size) ? | ||
308 | port->bulk_out_size : count; | ||
309 | |||
310 | memcpy(port->write_urb->transfer_buffer, buf, count); | ||
311 | data = port->write_urb->transfer_buffer; | ||
312 | usb_serial_debug_data(debug, &port->dev, __func__, count, data); | ||
313 | 343 | ||
314 | /* set up our urb */ | 344 | if (serial->type->max_in_flight_urbs) |
315 | usb_fill_bulk_urb(port->write_urb, serial->dev, | 345 | return usb_serial_multi_urb_write(tty, port, |
316 | usb_sndbulkpipe(serial->dev, | 346 | buf, count); |
317 | port->bulk_out_endpointAddress), | ||
318 | port->write_urb->transfer_buffer, count, | ||
319 | ((serial->type->write_bulk_callback) ? | ||
320 | serial->type->write_bulk_callback : | ||
321 | usb_serial_generic_write_bulk_callback), | ||
322 | port); | ||
323 | 347 | ||
324 | /* send the data out the bulk port */ | 348 | count = kfifo_put(port->write_fifo, buf, count); |
325 | port->write_urb_busy = 1; | 349 | result = usb_serial_generic_write_start(port); |
326 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); | ||
327 | if (result) { | ||
328 | dev_err(&port->dev, | ||
329 | "%s - failed submitting write urb, error %d\n", | ||
330 | __func__, result); | ||
331 | /* don't have to grab the lock here, as we will | ||
332 | retry if != 0 */ | ||
333 | port->write_urb_busy = 0; | ||
334 | } else | ||
335 | result = count; | ||
336 | 350 | ||
337 | return result; | 351 | if (result >= 0) |
338 | } | 352 | result = count; |
339 | 353 | ||
340 | /* no bulk out, so return 0 bytes written */ | 354 | return result; |
341 | return 0; | ||
342 | } | 355 | } |
343 | EXPORT_SYMBOL_GPL(usb_serial_generic_write); | 356 | EXPORT_SYMBOL_GPL(usb_serial_generic_write); |
344 | 357 | ||
@@ -356,9 +369,8 @@ int usb_serial_generic_write_room(struct tty_struct *tty) | |||
356 | room = port->bulk_out_size * | 369 | room = port->bulk_out_size * |
357 | (serial->type->max_in_flight_urbs - | 370 | (serial->type->max_in_flight_urbs - |
358 | port->urbs_in_flight); | 371 | port->urbs_in_flight); |
359 | } else if (serial->num_bulk_out && !(port->write_urb_busy)) { | 372 | } else if (serial->num_bulk_out) |
360 | room = port->bulk_out_size; | 373 | room = port->write_fifo->size - __kfifo_len(port->write_fifo); |
361 | } | ||
362 | spin_unlock_irqrestore(&port->lock, flags); | 374 | spin_unlock_irqrestore(&port->lock, flags); |
363 | 375 | ||
364 | dbg("%s - returns %d", __func__, room); | 376 | dbg("%s - returns %d", __func__, room); |
@@ -378,11 +390,8 @@ int usb_serial_generic_chars_in_buffer(struct tty_struct *tty) | |||
378 | spin_lock_irqsave(&port->lock, flags); | 390 | spin_lock_irqsave(&port->lock, flags); |
379 | chars = port->tx_bytes_flight; | 391 | chars = port->tx_bytes_flight; |
380 | spin_unlock_irqrestore(&port->lock, flags); | 392 | spin_unlock_irqrestore(&port->lock, flags); |
381 | } else if (serial->num_bulk_out) { | 393 | } else if (serial->num_bulk_out) |
382 | /* FIXME: Locking */ | 394 | chars = kfifo_len(port->write_fifo); |
383 | if (port->write_urb_busy) | ||
384 | chars = port->write_urb->transfer_buffer_length; | ||
385 | } | ||
386 | 395 | ||
387 | dbg("%s - returns %d", __func__, chars); | 396 | dbg("%s - returns %d", __func__, chars); |
388 | return chars; | 397 | return chars; |
@@ -486,16 +495,23 @@ void usb_serial_generic_write_bulk_callback(struct urb *urb) | |||
486 | if (port->urbs_in_flight < 0) | 495 | if (port->urbs_in_flight < 0) |
487 | port->urbs_in_flight = 0; | 496 | port->urbs_in_flight = 0; |
488 | spin_unlock_irqrestore(&port->lock, flags); | 497 | spin_unlock_irqrestore(&port->lock, flags); |
498 | |||
499 | if (status) { | ||
500 | dbg("%s - nonzero multi-urb write bulk status " | ||
501 | "received: %d", __func__, status); | ||
502 | return; | ||
503 | } | ||
489 | } else { | 504 | } else { |
490 | /* Handle the case for single urb mode */ | ||
491 | port->write_urb_busy = 0; | 505 | port->write_urb_busy = 0; |
492 | } | ||
493 | 506 | ||
494 | if (status) { | 507 | if (status) { |
495 | dbg("%s - nonzero write bulk status received: %d", | 508 | dbg("%s - nonzero multi-urb write bulk status " |
496 | __func__, status); | 509 | "received: %d", __func__, status); |
497 | return; | 510 | kfifo_reset(port->write_fifo); |
511 | } else | ||
512 | usb_serial_generic_write_start(port); | ||
498 | } | 513 | } |
514 | |||
499 | usb_serial_port_softint(port); | 515 | usb_serial_port_softint(port); |
500 | } | 516 | } |
501 | EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback); | 517 | EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback); |
@@ -530,7 +546,7 @@ void usb_serial_generic_unthrottle(struct tty_struct *tty) | |||
530 | 546 | ||
531 | if (was_throttled) { | 547 | if (was_throttled) { |
532 | /* Resume reading from device */ | 548 | /* Resume reading from device */ |
533 | usb_serial_generic_resubmit_read_urb(port, GFP_KERNEL); | 549 | flush_and_resubmit_read_urb(port); |
534 | } | 550 | } |
535 | } | 551 | } |
536 | 552 | ||
@@ -560,6 +576,33 @@ int usb_serial_handle_break(struct usb_serial_port *port) | |||
560 | } | 576 | } |
561 | EXPORT_SYMBOL_GPL(usb_serial_handle_break); | 577 | EXPORT_SYMBOL_GPL(usb_serial_handle_break); |
562 | 578 | ||
579 | int usb_serial_generic_resume(struct usb_serial *serial) | ||
580 | { | ||
581 | struct usb_serial_port *port; | ||
582 | int i, c = 0, r; | ||
583 | |||
584 | for (i = 0; i < serial->num_ports; i++) { | ||
585 | port = serial->port[i]; | ||
586 | if (!port->port.count) | ||
587 | continue; | ||
588 | |||
589 | if (port->read_urb) { | ||
590 | r = usb_submit_urb(port->read_urb, GFP_NOIO); | ||
591 | if (r < 0) | ||
592 | c++; | ||
593 | } | ||
594 | |||
595 | if (port->write_urb) { | ||
596 | r = usb_serial_generic_write_start(port); | ||
597 | if (r < 0) | ||
598 | c++; | ||
599 | } | ||
600 | } | ||
601 | |||
602 | return c ? -EIO : 0; | ||
603 | } | ||
604 | EXPORT_SYMBOL_GPL(usb_serial_generic_resume); | ||
605 | |||
563 | void usb_serial_generic_disconnect(struct usb_serial *serial) | 606 | void usb_serial_generic_disconnect(struct usb_serial *serial) |
564 | { | 607 | { |
565 | int i; | 608 | int i; |