diff options
Diffstat (limited to 'drivers/usb/serial/generic.c')
-rw-r--r-- | drivers/usb/serial/generic.c | 189 |
1 files changed, 166 insertions, 23 deletions
diff --git a/drivers/usb/serial/generic.c b/drivers/usb/serial/generic.c index 4cec9906ccf3..932d6241b787 100644 --- a/drivers/usb/serial/generic.c +++ b/drivers/usb/serial/generic.c | |||
@@ -63,7 +63,8 @@ struct usb_serial_driver usb_serial_generic_device = { | |||
63 | .id_table = generic_device_ids, | 63 | .id_table = generic_device_ids, |
64 | .usb_driver = &generic_driver, | 64 | .usb_driver = &generic_driver, |
65 | .num_ports = 1, | 65 | .num_ports = 1, |
66 | .shutdown = usb_serial_generic_shutdown, | 66 | .disconnect = usb_serial_generic_disconnect, |
67 | .release = usb_serial_generic_release, | ||
67 | .throttle = usb_serial_generic_throttle, | 68 | .throttle = usb_serial_generic_throttle, |
68 | .unthrottle = usb_serial_generic_unthrottle, | 69 | .unthrottle = usb_serial_generic_unthrottle, |
69 | .resume = usb_serial_generic_resume, | 70 | .resume = usb_serial_generic_resume, |
@@ -184,13 +185,94 @@ int usb_serial_generic_resume(struct usb_serial *serial) | |||
184 | } | 185 | } |
185 | EXPORT_SYMBOL_GPL(usb_serial_generic_resume); | 186 | EXPORT_SYMBOL_GPL(usb_serial_generic_resume); |
186 | 187 | ||
187 | void usb_serial_generic_close(struct tty_struct *tty, | 188 | void usb_serial_generic_close(struct usb_serial_port *port) |
188 | struct usb_serial_port *port, struct file *filp) | ||
189 | { | 189 | { |
190 | dbg("%s - port %d", __func__, port->number); | 190 | dbg("%s - port %d", __func__, port->number); |
191 | generic_cleanup(port); | 191 | generic_cleanup(port); |
192 | } | 192 | } |
193 | 193 | ||
194 | static int usb_serial_multi_urb_write(struct tty_struct *tty, | ||
195 | struct usb_serial_port *port, const unsigned char *buf, int count) | ||
196 | { | ||
197 | unsigned long flags; | ||
198 | struct urb *urb; | ||
199 | unsigned char *buffer; | ||
200 | int status; | ||
201 | int towrite; | ||
202 | int bwrite = 0; | ||
203 | |||
204 | dbg("%s - port %d", __func__, port->number); | ||
205 | |||
206 | if (count == 0) | ||
207 | dbg("%s - write request of 0 bytes", __func__); | ||
208 | |||
209 | while (count > 0) { | ||
210 | towrite = (count > port->bulk_out_size) ? | ||
211 | port->bulk_out_size : count; | ||
212 | spin_lock_irqsave(&port->lock, flags); | ||
213 | if (port->urbs_in_flight > | ||
214 | port->serial->type->max_in_flight_urbs) { | ||
215 | spin_unlock_irqrestore(&port->lock, flags); | ||
216 | dbg("%s - write limit hit\n", __func__); | ||
217 | return bwrite; | ||
218 | } | ||
219 | port->tx_bytes_flight += towrite; | ||
220 | port->urbs_in_flight++; | ||
221 | spin_unlock_irqrestore(&port->lock, flags); | ||
222 | |||
223 | buffer = kmalloc(towrite, GFP_ATOMIC); | ||
224 | if (!buffer) { | ||
225 | dev_err(&port->dev, | ||
226 | "%s ran out of kernel memory for urb ...\n", __func__); | ||
227 | goto error_no_buffer; | ||
228 | } | ||
229 | |||
230 | urb = usb_alloc_urb(0, GFP_ATOMIC); | ||
231 | if (!urb) { | ||
232 | dev_err(&port->dev, "%s - no more free urbs\n", | ||
233 | __func__); | ||
234 | goto error_no_urb; | ||
235 | } | ||
236 | |||
237 | /* Copy data */ | ||
238 | memcpy(buffer, buf + bwrite, towrite); | ||
239 | usb_serial_debug_data(debug, &port->dev, __func__, | ||
240 | towrite, buffer); | ||
241 | /* fill the buffer and send it */ | ||
242 | usb_fill_bulk_urb(urb, port->serial->dev, | ||
243 | usb_sndbulkpipe(port->serial->dev, | ||
244 | port->bulk_out_endpointAddress), | ||
245 | buffer, towrite, | ||
246 | usb_serial_generic_write_bulk_callback, port); | ||
247 | |||
248 | status = usb_submit_urb(urb, GFP_ATOMIC); | ||
249 | if (status) { | ||
250 | dev_err(&port->dev, | ||
251 | "%s - failed submitting write urb, error %d\n", | ||
252 | __func__, status); | ||
253 | goto error; | ||
254 | } | ||
255 | |||
256 | /* This urb is the responsibility of the host driver now */ | ||
257 | usb_free_urb(urb); | ||
258 | dbg("%s write: %d", __func__, towrite); | ||
259 | count -= towrite; | ||
260 | bwrite += towrite; | ||
261 | } | ||
262 | return bwrite; | ||
263 | |||
264 | error: | ||
265 | usb_free_urb(urb); | ||
266 | error_no_urb: | ||
267 | kfree(buffer); | ||
268 | error_no_buffer: | ||
269 | spin_lock_irqsave(&port->lock, flags); | ||
270 | port->urbs_in_flight--; | ||
271 | port->tx_bytes_flight -= towrite; | ||
272 | spin_unlock_irqrestore(&port->lock, flags); | ||
273 | return bwrite; | ||
274 | } | ||
275 | |||
194 | int usb_serial_generic_write(struct tty_struct *tty, | 276 | int usb_serial_generic_write(struct tty_struct *tty, |
195 | struct usb_serial_port *port, const unsigned char *buf, int count) | 277 | struct usb_serial_port *port, const unsigned char *buf, int count) |
196 | { | 278 | { |
@@ -208,6 +290,11 @@ int usb_serial_generic_write(struct tty_struct *tty, | |||
208 | /* only do something if we have a bulk out endpoint */ | 290 | /* only do something if we have a bulk out endpoint */ |
209 | if (serial->num_bulk_out) { | 291 | if (serial->num_bulk_out) { |
210 | unsigned long flags; | 292 | unsigned long flags; |
293 | |||
294 | if (serial->type->max_in_flight_urbs) | ||
295 | return usb_serial_multi_urb_write(tty, port, | ||
296 | buf, count); | ||
297 | |||
211 | spin_lock_irqsave(&port->lock, flags); | 298 | spin_lock_irqsave(&port->lock, flags); |
212 | if (port->write_urb_busy) { | 299 | if (port->write_urb_busy) { |
213 | spin_unlock_irqrestore(&port->lock, flags); | 300 | spin_unlock_irqrestore(&port->lock, flags); |
@@ -253,20 +340,26 @@ int usb_serial_generic_write(struct tty_struct *tty, | |||
253 | /* no bulk out, so return 0 bytes written */ | 340 | /* no bulk out, so return 0 bytes written */ |
254 | return 0; | 341 | return 0; |
255 | } | 342 | } |
343 | EXPORT_SYMBOL_GPL(usb_serial_generic_write); | ||
256 | 344 | ||
257 | int usb_serial_generic_write_room(struct tty_struct *tty) | 345 | int usb_serial_generic_write_room(struct tty_struct *tty) |
258 | { | 346 | { |
259 | struct usb_serial_port *port = tty->driver_data; | 347 | struct usb_serial_port *port = tty->driver_data; |
260 | struct usb_serial *serial = port->serial; | 348 | struct usb_serial *serial = port->serial; |
349 | unsigned long flags; | ||
261 | int room = 0; | 350 | int room = 0; |
262 | 351 | ||
263 | dbg("%s - port %d", __func__, port->number); | 352 | dbg("%s - port %d", __func__, port->number); |
264 | 353 | spin_lock_irqsave(&port->lock, flags); | |
265 | /* FIXME: Locking */ | 354 | if (serial->type->max_in_flight_urbs) { |
266 | if (serial->num_bulk_out) { | 355 | if (port->urbs_in_flight < serial->type->max_in_flight_urbs) |
267 | if (!(port->write_urb_busy)) | 356 | room = port->bulk_out_size * |
268 | room = port->bulk_out_size; | 357 | (serial->type->max_in_flight_urbs - |
358 | port->urbs_in_flight); | ||
359 | } else if (serial->num_bulk_out && !(port->write_urb_busy)) { | ||
360 | room = port->bulk_out_size; | ||
269 | } | 361 | } |
362 | spin_unlock_irqrestore(&port->lock, flags); | ||
270 | 363 | ||
271 | dbg("%s - returns %d", __func__, room); | 364 | dbg("%s - returns %d", __func__, room); |
272 | return room; | 365 | return room; |
@@ -277,11 +370,16 @@ int usb_serial_generic_chars_in_buffer(struct tty_struct *tty) | |||
277 | struct usb_serial_port *port = tty->driver_data; | 370 | struct usb_serial_port *port = tty->driver_data; |
278 | struct usb_serial *serial = port->serial; | 371 | struct usb_serial *serial = port->serial; |
279 | int chars = 0; | 372 | int chars = 0; |
373 | unsigned long flags; | ||
280 | 374 | ||
281 | dbg("%s - port %d", __func__, port->number); | 375 | dbg("%s - port %d", __func__, port->number); |
282 | 376 | ||
283 | /* FIXME: Locking */ | 377 | if (serial->type->max_in_flight_urbs) { |
284 | if (serial->num_bulk_out) { | 378 | spin_lock_irqsave(&port->lock, flags); |
379 | chars = port->tx_bytes_flight; | ||
380 | spin_unlock_irqrestore(&port->lock, flags); | ||
381 | } else if (serial->num_bulk_out) { | ||
382 | /* FIXME: Locking */ | ||
285 | if (port->write_urb_busy) | 383 | if (port->write_urb_busy) |
286 | chars = port->write_urb->transfer_buffer_length; | 384 | chars = port->write_urb->transfer_buffer_length; |
287 | } | 385 | } |
@@ -291,7 +389,8 @@ int usb_serial_generic_chars_in_buffer(struct tty_struct *tty) | |||
291 | } | 389 | } |
292 | 390 | ||
293 | 391 | ||
294 | static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags) | 392 | void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port, |
393 | gfp_t mem_flags) | ||
295 | { | 394 | { |
296 | struct urb *urb = port->read_urb; | 395 | struct urb *urb = port->read_urb; |
297 | struct usb_serial *serial = port->serial; | 396 | struct usb_serial *serial = port->serial; |
@@ -312,25 +411,28 @@ static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags) | |||
312 | "%s - failed resubmitting read urb, error %d\n", | 411 | "%s - failed resubmitting read urb, error %d\n", |
313 | __func__, result); | 412 | __func__, result); |
314 | } | 413 | } |
414 | EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb); | ||
315 | 415 | ||
316 | /* Push data to tty layer and resubmit the bulk read URB */ | 416 | /* Push data to tty layer and resubmit the bulk read URB */ |
317 | static void flush_and_resubmit_read_urb(struct usb_serial_port *port) | 417 | static void flush_and_resubmit_read_urb(struct usb_serial_port *port) |
318 | { | 418 | { |
319 | struct urb *urb = port->read_urb; | 419 | struct urb *urb = port->read_urb; |
320 | struct tty_struct *tty = tty_port_tty_get(&port->port); | 420 | struct tty_struct *tty = tty_port_tty_get(&port->port); |
321 | int room; | 421 | char *ch = (char *)urb->transfer_buffer; |
422 | int i; | ||
423 | |||
424 | if (!tty) | ||
425 | goto done; | ||
322 | 426 | ||
323 | /* Push data to tty */ | 427 | /* Push data to tty */ |
324 | if (tty && urb->actual_length) { | 428 | for (i = 0; i < urb->actual_length; i++, ch++) { |
325 | room = tty_buffer_request_room(tty, urb->actual_length); | 429 | if (!usb_serial_handle_sysrq_char(port, *ch)) |
326 | if (room) { | 430 | tty_insert_flip_char(tty, *ch, TTY_NORMAL); |
327 | tty_insert_flip_string(tty, urb->transfer_buffer, room); | ||
328 | tty_flip_buffer_push(tty); | ||
329 | } | ||
330 | } | 431 | } |
432 | tty_flip_buffer_push(tty); | ||
331 | tty_kref_put(tty); | 433 | tty_kref_put(tty); |
332 | 434 | done: | |
333 | resubmit_read_urb(port, GFP_ATOMIC); | 435 | usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC); |
334 | } | 436 | } |
335 | 437 | ||
336 | void usb_serial_generic_read_bulk_callback(struct urb *urb) | 438 | void usb_serial_generic_read_bulk_callback(struct urb *urb) |
@@ -364,12 +466,24 @@ EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback); | |||
364 | 466 | ||
365 | void usb_serial_generic_write_bulk_callback(struct urb *urb) | 467 | void usb_serial_generic_write_bulk_callback(struct urb *urb) |
366 | { | 468 | { |
469 | unsigned long flags; | ||
367 | struct usb_serial_port *port = urb->context; | 470 | struct usb_serial_port *port = urb->context; |
368 | int status = urb->status; | 471 | int status = urb->status; |
369 | 472 | ||
370 | dbg("%s - port %d", __func__, port->number); | 473 | dbg("%s - port %d", __func__, port->number); |
371 | 474 | ||
372 | port->write_urb_busy = 0; | 475 | if (port->serial->type->max_in_flight_urbs) { |
476 | spin_lock_irqsave(&port->lock, flags); | ||
477 | --port->urbs_in_flight; | ||
478 | port->tx_bytes_flight -= urb->transfer_buffer_length; | ||
479 | if (port->urbs_in_flight < 0) | ||
480 | port->urbs_in_flight = 0; | ||
481 | spin_unlock_irqrestore(&port->lock, flags); | ||
482 | } else { | ||
483 | /* Handle the case for single urb mode */ | ||
484 | port->write_urb_busy = 0; | ||
485 | } | ||
486 | |||
373 | if (status) { | 487 | if (status) { |
374 | dbg("%s - nonzero write bulk status received: %d", | 488 | dbg("%s - nonzero write bulk status received: %d", |
375 | __func__, status); | 489 | __func__, status); |
@@ -409,11 +523,36 @@ void usb_serial_generic_unthrottle(struct tty_struct *tty) | |||
409 | 523 | ||
410 | if (was_throttled) { | 524 | if (was_throttled) { |
411 | /* Resume reading from device */ | 525 | /* Resume reading from device */ |
412 | resubmit_read_urb(port, GFP_KERNEL); | 526 | usb_serial_generic_resubmit_read_urb(port, GFP_KERNEL); |
527 | } | ||
528 | } | ||
529 | |||
530 | int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch) | ||
531 | { | ||
532 | if (port->sysrq && port->console) { | ||
533 | if (ch && time_before(jiffies, port->sysrq)) { | ||
534 | handle_sysrq(ch, tty_port_tty_get(&port->port)); | ||
535 | port->sysrq = 0; | ||
536 | return 1; | ||
537 | } | ||
538 | port->sysrq = 0; | ||
539 | } | ||
540 | return 0; | ||
541 | } | ||
542 | EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char); | ||
543 | |||
544 | int usb_serial_handle_break(struct usb_serial_port *port) | ||
545 | { | ||
546 | if (!port->sysrq) { | ||
547 | port->sysrq = jiffies + HZ*5; | ||
548 | return 1; | ||
413 | } | 549 | } |
550 | port->sysrq = 0; | ||
551 | return 0; | ||
414 | } | 552 | } |
553 | EXPORT_SYMBOL_GPL(usb_serial_handle_break); | ||
415 | 554 | ||
416 | void usb_serial_generic_shutdown(struct usb_serial *serial) | 555 | void usb_serial_generic_disconnect(struct usb_serial *serial) |
417 | { | 556 | { |
418 | int i; | 557 | int i; |
419 | 558 | ||
@@ -424,3 +563,7 @@ void usb_serial_generic_shutdown(struct usb_serial *serial) | |||
424 | generic_cleanup(serial->port[i]); | 563 | generic_cleanup(serial->port[i]); |
425 | } | 564 | } |
426 | 565 | ||
566 | void usb_serial_generic_release(struct usb_serial *serial) | ||
567 | { | ||
568 | dbg("%s", __func__); | ||
569 | } | ||