Lines Matching +full:port +full:-

1 // SPDX-License-Identifier: GPL-2.0+
3 * u_serial.c - utilities for USB gadget "serial port"/TTY support
10 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman ([email protected])
39 * "serial port" functionality through the USB gadget stack. Each such
40 * port is exposed through a /dev/ttyGS* node.
42 * After this module has been loaded, the individual TTY port can be requested
46 * host issues a config change event. Data can only flow when the port is
49 * A given TTY port can be made available in multiple configurations.
56 * Configurations may expose more than one TTY port. For example, if
67 * gserial <---> gs_port ... links will be null when the USB link is
70 * gserial->ioport == usb_ep->driver_data ... gs_port
71 * gs_port->port_usb ... gserial
73 * gs_port <---> tty_struct ... links will be null when the TTY file
75 * gserial->port_tty ... tty_struct
76 * tty_struct->driver_data ... gserial
87 /* Prevents race conditions while accessing gser->ioport */
101 * The port structure holds info for each port, one for each minor number
105 struct tty_port port; member
129 bool suspended; /* port suspended */
134 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
139 struct gs_port *port; member
158 /*-------------------------------------------------------------------------*/
176 req->length = len; in gs_alloc_req()
177 req->buf = kmalloc(len, kmalloc_flags); in gs_alloc_req()
178 if (req->buf == NULL) { in gs_alloc_req()
195 kfree(req->buf); in gs_free_req()
210 gs_send_packet(struct gs_port *port, char *packet, unsigned size) in gs_send_packet() argument
214 len = kfifo_len(&port->port_write_buf); in gs_send_packet()
218 size = kfifo_out(&port->port_write_buf, packet, size); in gs_send_packet()
231 * Context: caller owns port_lock; port_usb is non-null.
233 static int gs_start_tx(struct gs_port *port) in gs_start_tx() argument
235 __releases(&port->port_lock) in gs_start_tx()
236 __acquires(&port->port_lock) in gs_start_tx()
239 struct list_head *pool = &port->write_pool; in gs_start_tx()
244 if (!port->port_usb) in gs_start_tx()
247 in = port->port_usb->in; in gs_start_tx()
249 while (!port->write_busy && !list_empty(pool)) { in gs_start_tx()
253 if (port->write_started >= QUEUE_SIZE) in gs_start_tx()
256 req = list_entry(pool->next, struct usb_request, list); in gs_start_tx()
257 len = gs_send_packet(port, req->buf, in->maxpacket); in gs_start_tx()
259 wake_up_interruptible(&port->drain_wait); in gs_start_tx()
263 port->icount.tx += len; in gs_start_tx()
265 req->length = len; in gs_start_tx()
266 list_del(&req->list); in gs_start_tx()
267 req->zero = kfifo_is_empty(&port->port_write_buf); in gs_start_tx()
269 pr_vdebug("ttyGS%d: tx len=%d, %3ph ...\n", port->port_num, len, req->buf); in gs_start_tx()
276 * the TTY closed (dev->ioport->port_tty is NULL). in gs_start_tx()
278 port->write_busy = true; in gs_start_tx()
279 spin_unlock(&port->port_lock); in gs_start_tx()
281 spin_lock(&port->port_lock); in gs_start_tx()
282 port->write_busy = false; in gs_start_tx()
286 __func__, "queue", in->name, status); in gs_start_tx()
287 list_add(&req->list, pool); in gs_start_tx()
291 port->write_started++; in gs_start_tx()
294 if (!port->port_usb) in gs_start_tx()
298 if (do_tty_wake && port->port.tty) in gs_start_tx()
299 tty_wakeup(port->port.tty); in gs_start_tx()
306 static unsigned gs_start_rx(struct gs_port *port) in gs_start_rx() argument
308 __releases(&port->port_lock) in gs_start_rx()
309 __acquires(&port->port_lock) in gs_start_rx()
312 struct list_head *pool = &port->read_pool; in gs_start_rx()
313 struct usb_ep *out = port->port_usb->out; in gs_start_rx()
321 tty = port->port.tty; in gs_start_rx()
325 if (port->read_started >= QUEUE_SIZE) in gs_start_rx()
328 req = list_entry(pool->next, struct usb_request, list); in gs_start_rx()
329 list_del(&req->list); in gs_start_rx()
330 req->length = out->maxpacket; in gs_start_rx()
335 spin_unlock(&port->port_lock); in gs_start_rx()
337 spin_lock(&port->port_lock); in gs_start_rx()
341 __func__, "queue", out->name, status); in gs_start_rx()
342 list_add(&req->list, pool); in gs_start_rx()
345 port->read_started++; in gs_start_rx()
348 if (!port->port_usb) in gs_start_rx()
351 return port->read_started; in gs_start_rx()
367 struct gs_port *port = container_of(w, struct gs_port, push); in gs_rx_push() local
369 struct list_head *queue = &port->read_queue; in gs_rx_push()
374 spin_lock_irq(&port->port_lock); in gs_rx_push()
375 tty = port->port.tty; in gs_rx_push()
385 switch (req->status) { in gs_rx_push()
386 case -ESHUTDOWN: in gs_rx_push()
388 pr_vdebug("ttyGS%d: shutdown\n", port->port_num); in gs_rx_push()
394 port->port_num, req->status); in gs_rx_push()
402 if (req->actual && tty) { in gs_rx_push()
403 char *packet = req->buf; in gs_rx_push()
404 unsigned size = req->actual; in gs_rx_push()
409 n = port->n_read; in gs_rx_push()
412 size -= n; in gs_rx_push()
415 port->icount.rx += size; in gs_rx_push()
416 count = tty_insert_flip_string(&port->port, packet, in gs_rx_push()
422 port->n_read += count; in gs_rx_push()
424 port->port_num, count, req->actual); in gs_rx_push()
427 port->n_read = 0; in gs_rx_push()
430 list_move(&req->list, &port->read_pool); in gs_rx_push()
431 port->read_started--; in gs_rx_push()
438 tty_flip_buffer_push(&port->port); in gs_rx_push()
445 * We may leave non-empty queue only when there is a tty, and in gs_rx_push()
449 schedule_delayed_work(&port->push, 1); in gs_rx_push()
452 if (!disconnect && port->port_usb) in gs_rx_push()
453 gs_start_rx(port); in gs_rx_push()
455 spin_unlock_irq(&port->port_lock); in gs_rx_push()
460 struct gs_port *port = ep->driver_data; in gs_read_complete() local
463 spin_lock(&port->port_lock); in gs_read_complete()
464 list_add_tail(&req->list, &port->read_queue); in gs_read_complete()
465 schedule_delayed_work(&port->push, 0); in gs_read_complete()
466 spin_unlock(&port->port_lock); in gs_read_complete()
471 struct gs_port *port = ep->driver_data; in gs_write_complete() local
473 spin_lock(&port->port_lock); in gs_write_complete()
474 list_add(&req->list, &port->write_pool); in gs_write_complete()
475 port->write_started--; in gs_write_complete()
477 switch (req->status) { in gs_write_complete()
481 __func__, ep->name, req->status); in gs_write_complete()
485 gs_start_tx(port); in gs_write_complete()
488 case -ESHUTDOWN: in gs_write_complete()
490 pr_vdebug("%s: %s shutdown\n", __func__, ep->name); in gs_write_complete()
494 spin_unlock(&port->port_lock); in gs_write_complete()
503 req = list_entry(head->next, struct usb_request, list); in gs_free_requests()
504 list_del(&req->list); in gs_free_requests()
507 (*allocated)--; in gs_free_requests()
517 int n = allocated ? QUEUE_SIZE - *allocated : QUEUE_SIZE; in gs_alloc_requests()
519 /* Pre-allocate up to QUEUE_SIZE transfers, but if we can't in gs_alloc_requests()
524 req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC); in gs_alloc_requests()
526 return list_empty(head) ? -ENOMEM : 0; in gs_alloc_requests()
527 req->complete = fn; in gs_alloc_requests()
528 list_add_tail(&req->list, head); in gs_alloc_requests()
536 * gs_start_io - start USB I/O streams
537 * @port: port to use
538 * Context: holding port_lock; port_tty and port_usb are non-null
541 * this port. If nothing is listening on the host side, we may
544 static int gs_start_io(struct gs_port *port) in gs_start_io() argument
546 struct list_head *head = &port->read_pool; in gs_start_io()
551 if (!port->port_usb || !port->port.tty) in gs_start_io()
552 return -EIO; in gs_start_io()
557 * configurations may use different endpoints with a given port; in gs_start_io()
560 ep = port->port_usb->out; in gs_start_io()
562 &port->read_allocated); in gs_start_io()
566 status = gs_alloc_requests(port->port_usb->in, &port->write_pool, in gs_start_io()
567 gs_write_complete, &port->write_allocated); in gs_start_io()
569 gs_free_requests(ep, head, &port->read_allocated); in gs_start_io()
574 port->n_read = 0; in gs_start_io()
575 started = gs_start_rx(port); in gs_start_io()
578 gs_start_tx(port); in gs_start_io()
581 tty_wakeup(port->port.tty); in gs_start_io()
584 if (port->port_usb) { in gs_start_io()
585 gs_free_requests(ep, head, &port->read_allocated); in gs_start_io()
586 gs_free_requests(port->port_usb->in, &port->write_pool, in gs_start_io()
587 &port->write_allocated); in gs_start_io()
589 status = -EIO; in gs_start_io()
595 /*-------------------------------------------------------------------------*/
606 int port_num = tty->index; in gs_open()
607 struct gs_port *port; in gs_open() local
611 port = ports[port_num].port; in gs_open()
612 if (!port) { in gs_open()
613 status = -ENODEV; in gs_open()
617 spin_lock_irq(&port->port_lock); in gs_open()
620 if (!kfifo_initialized(&port->port_write_buf)) { in gs_open()
622 spin_unlock_irq(&port->port_lock); in gs_open()
629 status = kfifo_alloc(&port->port_write_buf, in gs_open()
637 spin_lock_irq(&port->port_lock); in gs_open()
641 if (port->port.count++) in gs_open()
644 tty->driver_data = port; in gs_open()
645 port->port.tty = tty; in gs_open()
648 if (port->port_usb) { in gs_open()
649 /* if port is suspended, wait resume to start I/0 stream */ in gs_open()
650 if (!port->suspended) { in gs_open()
651 struct gserial *gser = port->port_usb; in gs_open()
653 pr_debug("gs_open: start ttyGS%d\n", port->port_num); in gs_open()
654 gs_start_io(port); in gs_open()
656 if (gser->connect) in gs_open()
657 gser->connect(gser); in gs_open()
659 pr_debug("delay start of ttyGS%d\n", port->port_num); in gs_open()
660 port->start_delayed = true; in gs_open()
664 pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file); in gs_open()
667 spin_unlock_irq(&port->port_lock); in gs_open()
678 spin_lock_irq(&p->port_lock); in gs_close_flush_done()
679 cond = p->port_usb == NULL || !kfifo_len(&p->port_write_buf) || in gs_close_flush_done()
680 p->port.count > 1; in gs_close_flush_done()
681 spin_unlock_irq(&p->port_lock); in gs_close_flush_done()
688 struct gs_port *port = tty->driver_data; in gs_close() local
691 spin_lock_irq(&port->port_lock); in gs_close()
693 if (port->port.count != 1) { in gs_close()
695 if (port->port.count == 0) in gs_close()
698 --port->port.count; in gs_close()
702 pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file); in gs_close()
704 gser = port->port_usb; in gs_close()
705 if (gser && !port->suspended && gser->disconnect) in gs_close()
706 gser->disconnect(gser); in gs_close()
711 if (kfifo_len(&port->port_write_buf) > 0 && gser) { in gs_close()
712 spin_unlock_irq(&port->port_lock); in gs_close()
713 wait_event_interruptible_timeout(port->drain_wait, in gs_close()
714 gs_close_flush_done(port), in gs_close()
716 spin_lock_irq(&port->port_lock); in gs_close()
718 if (port->port.count != 1) in gs_close()
721 gser = port->port_usb; in gs_close()
726 * let the push async work fire again until we're re-opened. in gs_close()
729 kfifo_free(&port->port_write_buf); in gs_close()
731 kfifo_reset(&port->port_write_buf); in gs_close()
733 port->start_delayed = false; in gs_close()
734 port->port.count = 0; in gs_close()
735 port->port.tty = NULL; in gs_close()
738 port->port_num, tty, file); in gs_close()
740 wake_up(&port->close_wait); in gs_close()
742 spin_unlock_irq(&port->port_lock); in gs_close()
747 struct gs_port *port = tty->driver_data; in gs_write() local
751 port->port_num, tty, count); in gs_write()
753 spin_lock_irqsave(&port->port_lock, flags); in gs_write()
755 count = kfifo_in(&port->port_write_buf, buf, count); in gs_write()
757 if (port->port_usb) in gs_write()
758 gs_start_tx(port); in gs_write()
759 spin_unlock_irqrestore(&port->port_lock, flags); in gs_write()
766 struct gs_port *port = tty->driver_data; in gs_put_char() local
771 port->port_num, tty, ch, __builtin_return_address(0)); in gs_put_char()
773 spin_lock_irqsave(&port->port_lock, flags); in gs_put_char()
774 status = kfifo_put(&port->port_write_buf, ch); in gs_put_char()
775 spin_unlock_irqrestore(&port->port_lock, flags); in gs_put_char()
782 struct gs_port *port = tty->driver_data; in gs_flush_chars() local
785 pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty); in gs_flush_chars()
787 spin_lock_irqsave(&port->port_lock, flags); in gs_flush_chars()
788 if (port->port_usb) in gs_flush_chars()
789 gs_start_tx(port); in gs_flush_chars()
790 spin_unlock_irqrestore(&port->port_lock, flags); in gs_flush_chars()
795 struct gs_port *port = tty->driver_data; in gs_write_room() local
799 spin_lock_irqsave(&port->port_lock, flags); in gs_write_room()
800 if (port->port_usb) in gs_write_room()
801 room = kfifo_avail(&port->port_write_buf); in gs_write_room()
802 spin_unlock_irqrestore(&port->port_lock, flags); in gs_write_room()
805 port->port_num, tty, room); in gs_write_room()
812 struct gs_port *port = tty->driver_data; in gs_chars_in_buffer() local
816 spin_lock_irqsave(&port->port_lock, flags); in gs_chars_in_buffer()
817 chars = kfifo_len(&port->port_write_buf); in gs_chars_in_buffer()
818 spin_unlock_irqrestore(&port->port_lock, flags); in gs_chars_in_buffer()
821 port->port_num, tty, chars); in gs_chars_in_buffer()
829 struct gs_port *port = tty->driver_data; in gs_unthrottle() local
832 spin_lock_irqsave(&port->port_lock, flags); in gs_unthrottle()
833 if (port->port_usb) { in gs_unthrottle()
838 pr_vdebug("ttyGS%d: unthrottle\n", port->port_num); in gs_unthrottle()
839 schedule_delayed_work(&port->push, 0); in gs_unthrottle()
841 spin_unlock_irqrestore(&port->port_lock, flags); in gs_unthrottle()
846 struct gs_port *port = tty->driver_data; in gs_break_ctl() local
851 port->port_num, duration); in gs_break_ctl()
853 spin_lock_irq(&port->port_lock); in gs_break_ctl()
854 gser = port->port_usb; in gs_break_ctl()
855 if (gser && gser->send_break) in gs_break_ctl()
856 status = gser->send_break(gser, duration); in gs_break_ctl()
857 spin_unlock_irq(&port->port_lock); in gs_break_ctl()
865 struct gs_port *port = tty->driver_data; in gs_get_icount() local
869 spin_lock_irqsave(&port->port_lock, flags); in gs_get_icount()
870 cnow = port->icount; in gs_get_icount()
871 spin_unlock_irqrestore(&port->port_lock, flags); in gs_get_icount()
873 icount->rx = cnow.rx; in gs_get_icount()
874 icount->tx = cnow.tx; in gs_get_icount()
892 /*-------------------------------------------------------------------------*/
900 struct gs_console *cons = req->context; in gs_console_complete_out()
902 switch (req->status) { in gs_console_complete_out()
905 __func__, ep->name, req->status); in gs_console_complete_out()
909 spin_lock(&cons->lock); in gs_console_complete_out()
910 req->length = 0; in gs_console_complete_out()
911 schedule_work(&cons->work); in gs_console_complete_out()
912 spin_unlock(&cons->lock); in gs_console_complete_out()
914 case -ECONNRESET: in gs_console_complete_out()
915 case -ESHUTDOWN: in gs_console_complete_out()
917 pr_vdebug("%s: %s shutdown\n", __func__, ep->name); in gs_console_complete_out()
924 struct usb_request *req = cons->req; in __gs_console_push()
931 if (req->length) in __gs_console_push()
934 ep = cons->console.data; in __gs_console_push()
935 size = kfifo_out(&cons->buf, req->buf, ep->maxpacket); in __gs_console_push()
939 if (cons->missed && ep->maxpacket >= 64) { in __gs_console_push()
943 len = sprintf(buf, "\n[missed %zu bytes]\n", cons->missed); in __gs_console_push()
944 kfifo_in(&cons->buf, buf, len); in __gs_console_push()
945 cons->missed = 0; in __gs_console_push()
948 req->length = size; in __gs_console_push()
950 spin_unlock_irq(&cons->lock); in __gs_console_push()
952 req->length = 0; in __gs_console_push()
953 spin_lock_irq(&cons->lock); in __gs_console_push()
960 spin_lock_irq(&cons->lock); in gs_console_work()
964 spin_unlock_irq(&cons->lock); in gs_console_work()
974 spin_lock_irqsave(&cons->lock, flags); in gs_console_write()
976 n = kfifo_in(&cons->buf, buf, count); in gs_console_write()
978 cons->missed += count - n; in gs_console_write()
980 if (cons->req && !cons->req->length) in gs_console_write()
981 schedule_work(&cons->work); in gs_console_write()
983 spin_unlock_irqrestore(&cons->lock, flags); in gs_console_write()
988 *index = co->index; in gs_console_device()
992 static int gs_console_connect(struct gs_port *port) in gs_console_connect() argument
994 struct gs_console *cons = port->console; in gs_console_connect()
1001 ep = port->port_usb->in; in gs_console_connect()
1002 req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC); in gs_console_connect()
1004 return -ENOMEM; in gs_console_connect()
1005 req->complete = gs_console_complete_out; in gs_console_connect()
1006 req->context = cons; in gs_console_connect()
1007 req->length = 0; in gs_console_connect()
1009 spin_lock(&cons->lock); in gs_console_connect()
1010 cons->req = req; in gs_console_connect()
1011 cons->console.data = ep; in gs_console_connect()
1012 spin_unlock(&cons->lock); in gs_console_connect()
1014 pr_debug("ttyGS%d: console connected!\n", port->port_num); in gs_console_connect()
1016 schedule_work(&cons->work); in gs_console_connect()
1021 static void gs_console_disconnect(struct gs_port *port) in gs_console_disconnect() argument
1023 struct gs_console *cons = port->console; in gs_console_disconnect()
1030 spin_lock(&cons->lock); in gs_console_disconnect()
1032 req = cons->req; in gs_console_disconnect()
1033 ep = cons->console.data; in gs_console_disconnect()
1034 cons->req = NULL; in gs_console_disconnect()
1036 spin_unlock(&cons->lock); in gs_console_disconnect()
1045 static int gs_console_init(struct gs_port *port) in gs_console_init() argument
1050 if (port->console) in gs_console_init()
1053 cons = kzalloc(sizeof(*port->console), GFP_KERNEL); in gs_console_init()
1055 return -ENOMEM; in gs_console_init()
1057 strcpy(cons->console.name, "ttyGS"); in gs_console_init()
1058 cons->console.write = gs_console_write; in gs_console_init()
1059 cons->console.device = gs_console_device; in gs_console_init()
1060 cons->console.flags = CON_PRINTBUFFER; in gs_console_init()
1061 cons->console.index = port->port_num; in gs_console_init()
1063 INIT_WORK(&cons->work, gs_console_work); in gs_console_init()
1064 spin_lock_init(&cons->lock); in gs_console_init()
1066 err = kfifo_alloc(&cons->buf, GS_CONSOLE_BUF_SIZE, GFP_KERNEL); in gs_console_init()
1068 pr_err("ttyGS%d: allocate console buffer failed\n", port->port_num); in gs_console_init()
1073 port->console = cons; in gs_console_init()
1074 register_console(&cons->console); in gs_console_init()
1076 spin_lock_irq(&port->port_lock); in gs_console_init()
1077 if (port->port_usb) in gs_console_init()
1078 gs_console_connect(port); in gs_console_init()
1079 spin_unlock_irq(&port->port_lock); in gs_console_init()
1084 static void gs_console_exit(struct gs_port *port) in gs_console_exit() argument
1086 struct gs_console *cons = port->console; in gs_console_exit()
1091 unregister_console(&cons->console); in gs_console_exit()
1093 spin_lock_irq(&port->port_lock); in gs_console_exit()
1094 if (cons->req) in gs_console_exit()
1095 gs_console_disconnect(port); in gs_console_exit()
1096 spin_unlock_irq(&port->port_lock); in gs_console_exit()
1098 cancel_work_sync(&cons->work); in gs_console_exit()
1099 kfifo_free(&cons->buf); in gs_console_exit()
1101 port->console = NULL; in gs_console_exit()
1106 struct gs_port *port; in gserial_set_console() local
1115 port = ports[port_num].port; in gserial_set_console()
1117 if (WARN_ON(port == NULL)) { in gserial_set_console()
1118 ret = -ENXIO; in gserial_set_console()
1123 ret = gs_console_init(port); in gserial_set_console()
1125 gs_console_exit(port); in gserial_set_console()
1135 struct gs_port *port; in gserial_get_console() local
1139 port = ports[port_num].port; in gserial_get_console()
1141 if (WARN_ON(port == NULL)) in gserial_get_console()
1142 ret = -ENXIO; in gserial_get_console()
1144 ret = sprintf(page, "%u\n", !!port->console); in gserial_get_console()
1154 static int gs_console_connect(struct gs_port *port) in gs_console_connect() argument
1159 static void gs_console_disconnect(struct gs_port *port) in gs_console_disconnect() argument
1163 static int gs_console_init(struct gs_port *port) in gs_console_init() argument
1165 return -ENOSYS; in gs_console_init()
1168 static void gs_console_exit(struct gs_port *port) in gs_console_exit() argument
1177 struct gs_port *port; in gs_port_alloc() local
1181 if (ports[port_num].port) { in gs_port_alloc()
1182 ret = -EBUSY; in gs_port_alloc()
1186 port = kzalloc(sizeof(struct gs_port), GFP_KERNEL); in gs_port_alloc()
1187 if (port == NULL) { in gs_port_alloc()
1188 ret = -ENOMEM; in gs_port_alloc()
1192 tty_port_init(&port->port); in gs_port_alloc()
1193 spin_lock_init(&port->port_lock); in gs_port_alloc()
1194 init_waitqueue_head(&port->drain_wait); in gs_port_alloc()
1195 init_waitqueue_head(&port->close_wait); in gs_port_alloc()
1197 INIT_DELAYED_WORK(&port->push, gs_rx_push); in gs_port_alloc()
1199 INIT_LIST_HEAD(&port->read_pool); in gs_port_alloc()
1200 INIT_LIST_HEAD(&port->read_queue); in gs_port_alloc()
1201 INIT_LIST_HEAD(&port->write_pool); in gs_port_alloc()
1203 port->port_num = port_num; in gs_port_alloc()
1204 port->port_line_coding = *coding; in gs_port_alloc()
1206 ports[port_num].port = port; in gs_port_alloc()
1212 static int gs_closed(struct gs_port *port) in gs_closed() argument
1216 spin_lock_irq(&port->port_lock); in gs_closed()
1217 cond = port->port.count == 0; in gs_closed()
1218 spin_unlock_irq(&port->port_lock); in gs_closed()
1223 static void gserial_free_port(struct gs_port *port) in gserial_free_port() argument
1225 cancel_delayed_work_sync(&port->push); in gserial_free_port()
1227 wait_event(port->close_wait, gs_closed(port)); in gserial_free_port()
1228 WARN_ON(port->port_usb != NULL); in gserial_free_port()
1229 tty_port_destroy(&port->port); in gserial_free_port()
1230 kfree(port); in gserial_free_port()
1235 struct gs_port *port; in gserial_free_line() local
1238 if (!ports[port_num].port) { in gserial_free_line()
1242 port = ports[port_num].port; in gserial_free_line()
1243 gs_console_exit(port); in gserial_free_line()
1244 ports[port_num].port = NULL; in gserial_free_line()
1247 gserial_free_port(port); in gserial_free_line()
1255 struct gs_port *port; in gserial_alloc_line_no_console() local
1267 if (ret == -EBUSY) in gserial_alloc_line_no_console()
1278 port = ports[port_num].port; in gserial_alloc_line_no_console()
1279 tty_dev = tty_port_register_device(&port->port, in gserial_alloc_line_no_console()
1282 pr_err("%s: failed to register tty for port %d, err %ld\n", in gserial_alloc_line_no_console()
1287 ports[port_num].port = NULL; in gserial_alloc_line_no_console()
1289 gserial_free_port(port); in gserial_alloc_line_no_console()
1303 gs_console_init(ports[*line_num].port); in gserial_alloc_line()
1310 * gserial_connect - notify TTY I/O glue that USB link is active
1312 * @port_num: which port is active
1323 * before calling this, as well as the appropriate (speed-specific)
1328 * On success, ep->driver_data will be overwritten.
1332 struct gs_port *port; in gserial_connect() local
1337 return -ENXIO; in gserial_connect()
1339 port = ports[port_num].port; in gserial_connect()
1340 if (!port) { in gserial_connect()
1342 return -EINVAL; in gserial_connect()
1344 if (port->port_usb) { in gserial_connect()
1346 return -EBUSY; in gserial_connect()
1350 status = usb_ep_enable(gser->in); in gserial_connect()
1353 gser->in->driver_data = port; in gserial_connect()
1355 status = usb_ep_enable(gser->out); in gserial_connect()
1358 gser->out->driver_data = port; in gserial_connect()
1361 spin_lock_irqsave(&port->port_lock, flags); in gserial_connect()
1362 gser->ioport = port; in gserial_connect()
1363 port->port_usb = gser; in gserial_connect()
1368 gser->port_line_coding = port->port_line_coding; in gserial_connect()
1375 if (port->port.count) { in gserial_connect()
1376 pr_debug("gserial_connect: start ttyGS%d\n", port->port_num); in gserial_connect()
1377 gs_start_io(port); in gserial_connect()
1378 if (gser->connect) in gserial_connect()
1379 gser->connect(gser); in gserial_connect()
1381 if (gser->disconnect) in gserial_connect()
1382 gser->disconnect(gser); in gserial_connect()
1385 status = gs_console_connect(port); in gserial_connect()
1386 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_connect()
1391 usb_ep_disable(gser->in); in gserial_connect()
1396 * gserial_disconnect - notify TTY I/O glue that USB link is inactive
1408 struct gs_port *port = gser->ioport; in gserial_disconnect() local
1411 if (!port) in gserial_disconnect()
1417 spin_lock(&port->port_lock); in gserial_disconnect()
1419 gs_console_disconnect(port); in gserial_disconnect()
1422 port->port_line_coding = gser->port_line_coding; in gserial_disconnect()
1424 port->port_usb = NULL; in gserial_disconnect()
1425 gser->ioport = NULL; in gserial_disconnect()
1426 if (port->port.count > 0) { in gserial_disconnect()
1427 wake_up_interruptible(&port->drain_wait); in gserial_disconnect()
1428 if (port->port.tty) in gserial_disconnect()
1429 tty_hangup(port->port.tty); in gserial_disconnect()
1431 port->suspended = false; in gserial_disconnect()
1432 spin_unlock(&port->port_lock); in gserial_disconnect()
1436 usb_ep_disable(gser->out); in gserial_disconnect()
1437 usb_ep_disable(gser->in); in gserial_disconnect()
1440 spin_lock_irqsave(&port->port_lock, flags); in gserial_disconnect()
1441 if (port->port.count == 0) in gserial_disconnect()
1442 kfifo_free(&port->port_write_buf); in gserial_disconnect()
1443 gs_free_requests(gser->out, &port->read_pool, NULL); in gserial_disconnect()
1444 gs_free_requests(gser->out, &port->read_queue, NULL); in gserial_disconnect()
1445 gs_free_requests(gser->in, &port->write_pool, NULL); in gserial_disconnect()
1447 port->read_allocated = port->read_started = in gserial_disconnect()
1448 port->write_allocated = port->write_started = 0; in gserial_disconnect()
1450 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_disconnect()
1456 struct gs_port *port; in gserial_suspend() local
1460 port = gser->ioport; in gserial_suspend()
1462 if (!port) { in gserial_suspend()
1467 spin_lock(&port->port_lock); in gserial_suspend()
1469 port->suspended = true; in gserial_suspend()
1470 port->start_delayed = true; in gserial_suspend()
1471 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_suspend()
1477 struct gs_port *port; in gserial_resume() local
1481 port = gser->ioport; in gserial_resume()
1483 if (!port) { in gserial_resume()
1488 spin_lock(&port->port_lock); in gserial_resume()
1490 port->suspended = false; in gserial_resume()
1491 if (!port->start_delayed) { in gserial_resume()
1492 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_resume()
1496 pr_debug("delayed start ttyGS%d\n", port->port_num); in gserial_resume()
1497 gs_start_io(port); in gserial_resume()
1498 if (gser->connect) in gserial_resume()
1499 gser->connect(gser); in gserial_resume()
1500 port->start_delayed = false; in gserial_resume()
1501 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_resume()
1516 driver->driver_name = "g_serial"; in userial_init()
1517 driver->name = "ttyGS"; in userial_init()
1520 driver->type = TTY_DRIVER_TYPE_SERIAL; in userial_init()
1521 driver->subtype = SERIAL_TYPE_NORMAL; in userial_init()
1522 driver->init_termios = tty_std_termios; in userial_init()
1524 /* 9600-8-N-1 ... matches defaults expected by "usbser.sys" on in userial_init()
1525 * MS-Windows. Otherwise, most of these flags shouldn't affect in userial_init()
1528 driver->init_termios.c_cflag = in userial_init()
1530 driver->init_termios.c_ispeed = 9600; in userial_init()
1531 driver->init_termios.c_ospeed = 9600; in userial_init()
1566 MODULE_DESCRIPTION("utilities for USB gadget \"serial port\"/TTY support");