aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/serial/generic.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/serial/generic.c')
-rw-r--r--drivers/usb/serial/generic.c186
1 files changed, 165 insertions, 21 deletions
diff --git a/drivers/usb/serial/generic.c b/drivers/usb/serial/generic.c
index be82ea956720..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,
@@ -190,6 +191,88 @@ void usb_serial_generic_close(struct usb_serial_port *port)
190 generic_cleanup(port); 191 generic_cleanup(port);
191} 192}
192 193
194static 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
264error:
265 usb_free_urb(urb);
266error_no_urb:
267 kfree(buffer);
268error_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
193int usb_serial_generic_write(struct tty_struct *tty, 276int usb_serial_generic_write(struct tty_struct *tty,
194 struct usb_serial_port *port, const unsigned char *buf, int count) 277 struct usb_serial_port *port, const unsigned char *buf, int count)
195{ 278{
@@ -207,6 +290,11 @@ int usb_serial_generic_write(struct tty_struct *tty,
207 /* only do something if we have a bulk out endpoint */ 290 /* only do something if we have a bulk out endpoint */
208 if (serial->num_bulk_out) { 291 if (serial->num_bulk_out) {
209 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
210 spin_lock_irqsave(&port->lock, flags); 298 spin_lock_irqsave(&port->lock, flags);
211 if (port->write_urb_busy) { 299 if (port->write_urb_busy) {
212 spin_unlock_irqrestore(&port->lock, flags); 300 spin_unlock_irqrestore(&port->lock, flags);
@@ -252,20 +340,26 @@ int usb_serial_generic_write(struct tty_struct *tty,
252 /* no bulk out, so return 0 bytes written */ 340 /* no bulk out, so return 0 bytes written */
253 return 0; 341 return 0;
254} 342}
343EXPORT_SYMBOL_GPL(usb_serial_generic_write);
255 344
256int usb_serial_generic_write_room(struct tty_struct *tty) 345int usb_serial_generic_write_room(struct tty_struct *tty)
257{ 346{
258 struct usb_serial_port *port = tty->driver_data; 347 struct usb_serial_port *port = tty->driver_data;
259 struct usb_serial *serial = port->serial; 348 struct usb_serial *serial = port->serial;
349 unsigned long flags;
260 int room = 0; 350 int room = 0;
261 351
262 dbg("%s - port %d", __func__, port->number); 352 dbg("%s - port %d", __func__, port->number);
263 353 spin_lock_irqsave(&port->lock, flags);
264 /* FIXME: Locking */ 354 if (serial->type->max_in_flight_urbs) {
265 if (serial->num_bulk_out) { 355 if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
266 if (!(port->write_urb_busy)) 356 room = port->bulk_out_size *
267 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;
268 } 361 }
362 spin_unlock_irqrestore(&port->lock, flags);
269 363
270 dbg("%s - returns %d", __func__, room); 364 dbg("%s - returns %d", __func__, room);
271 return room; 365 return room;
@@ -276,11 +370,16 @@ int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
276 struct usb_serial_port *port = tty->driver_data; 370 struct usb_serial_port *port = tty->driver_data;
277 struct usb_serial *serial = port->serial; 371 struct usb_serial *serial = port->serial;
278 int chars = 0; 372 int chars = 0;
373 unsigned long flags;
279 374
280 dbg("%s - port %d", __func__, port->number); 375 dbg("%s - port %d", __func__, port->number);
281 376
282 /* FIXME: Locking */ 377 if (serial->type->max_in_flight_urbs) {
283 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 */
284 if (port->write_urb_busy) 383 if (port->write_urb_busy)
285 chars = port->write_urb->transfer_buffer_length; 384 chars = port->write_urb->transfer_buffer_length;
286 } 385 }
@@ -290,7 +389,8 @@ int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
290} 389}
291 390
292 391
293static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags) 392void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port,
393 gfp_t mem_flags)
294{ 394{
295 struct urb *urb = port->read_urb; 395 struct urb *urb = port->read_urb;
296 struct usb_serial *serial = port->serial; 396 struct usb_serial *serial = port->serial;
@@ -311,25 +411,28 @@ static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags)
311 "%s - failed resubmitting read urb, error %d\n", 411 "%s - failed resubmitting read urb, error %d\n",
312 __func__, result); 412 __func__, result);
313} 413}
414EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb);
314 415
315/* Push data to tty layer and resubmit the bulk read URB */ 416/* Push data to tty layer and resubmit the bulk read URB */
316static void flush_and_resubmit_read_urb(struct usb_serial_port *port) 417static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
317{ 418{
318 struct urb *urb = port->read_urb; 419 struct urb *urb = port->read_urb;
319 struct tty_struct *tty = tty_port_tty_get(&port->port); 420 struct tty_struct *tty = tty_port_tty_get(&port->port);
320 int room; 421 char *ch = (char *)urb->transfer_buffer;
422 int i;
423
424 if (!tty)
425 goto done;
321 426
322 /* Push data to tty */ 427 /* Push data to tty */
323 if (tty && urb->actual_length) { 428 for (i = 0; i < urb->actual_length; i++, ch++) {
324 room = tty_buffer_request_room(tty, urb->actual_length); 429 if (!usb_serial_handle_sysrq_char(port, *ch))
325 if (room) { 430 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
326 tty_insert_flip_string(tty, urb->transfer_buffer, room);
327 tty_flip_buffer_push(tty);
328 }
329 } 431 }
432 tty_flip_buffer_push(tty);
330 tty_kref_put(tty); 433 tty_kref_put(tty);
331 434done:
332 resubmit_read_urb(port, GFP_ATOMIC); 435 usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC);
333} 436}
334 437
335void usb_serial_generic_read_bulk_callback(struct urb *urb) 438void usb_serial_generic_read_bulk_callback(struct urb *urb)
@@ -363,12 +466,24 @@ EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
363 466
364void usb_serial_generic_write_bulk_callback(struct urb *urb) 467void usb_serial_generic_write_bulk_callback(struct urb *urb)
365{ 468{
469 unsigned long flags;
366 struct usb_serial_port *port = urb->context; 470 struct usb_serial_port *port = urb->context;
367 int status = urb->status; 471 int status = urb->status;
368 472
369 dbg("%s - port %d", __func__, port->number); 473 dbg("%s - port %d", __func__, port->number);
370 474
371 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
372 if (status) { 487 if (status) {
373 dbg("%s - nonzero write bulk status received: %d", 488 dbg("%s - nonzero write bulk status received: %d",
374 __func__, status); 489 __func__, status);
@@ -408,11 +523,36 @@ void usb_serial_generic_unthrottle(struct tty_struct *tty)
408 523
409 if (was_throttled) { 524 if (was_throttled) {
410 /* Resume reading from device */ 525 /* Resume reading from device */
411 resubmit_read_urb(port, GFP_KERNEL); 526 usb_serial_generic_resubmit_read_urb(port, GFP_KERNEL);
527 }
528}
529
530int 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}
542EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
543
544int usb_serial_handle_break(struct usb_serial_port *port)
545{
546 if (!port->sysrq) {
547 port->sysrq = jiffies + HZ*5;
548 return 1;
412 } 549 }
550 port->sysrq = 0;
551 return 0;
413} 552}
553EXPORT_SYMBOL_GPL(usb_serial_handle_break);
414 554
415void usb_serial_generic_shutdown(struct usb_serial *serial) 555void usb_serial_generic_disconnect(struct usb_serial *serial)
416{ 556{
417 int i; 557 int i;
418 558
@@ -423,3 +563,7 @@ void usb_serial_generic_shutdown(struct usb_serial *serial)
423 generic_cleanup(serial->port[i]); 563 generic_cleanup(serial->port[i]);
424} 564}
425 565
566void usb_serial_generic_release(struct usb_serial *serial)
567{
568 dbg("%s", __func__);
569}