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 (greg@kroah.com)
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()
299 tty_port_tty_wakeup(&port->port); 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()
547 struct usb_ep *ep = port->port_usb->out; in gs_start_io()
554 * configurations may use different endpoints with a given port; in gs_start_io()
558 &port->read_allocated); in gs_start_io()
562 status = gs_alloc_requests(port->port_usb->in, &port->write_pool, in gs_start_io()
563 gs_write_complete, &port->write_allocated); in gs_start_io()
565 gs_free_requests(ep, head, &port->read_allocated); in gs_start_io()
570 port->n_read = 0; in gs_start_io()
571 started = gs_start_rx(port); in gs_start_io()
574 gs_start_tx(port); in gs_start_io()
577 tty_port_tty_wakeup(&port->port); in gs_start_io()
580 if (port->port_usb) { in gs_start_io()
581 gs_free_requests(ep, head, &port->read_allocated); in gs_start_io()
582 gs_free_requests(port->port_usb->in, &port->write_pool, in gs_start_io()
583 &port->write_allocated); in gs_start_io()
585 status = -EIO; in gs_start_io()
593 struct usb_function *func = &gser->func; in gserial_wakeup_host()
594 struct usb_gadget *gadget = func->config->cdev->gadget; in gserial_wakeup_host()
596 if (func->func_suspended) in gserial_wakeup_host()
602 /*-------------------------------------------------------------------------*/
613 int port_num = tty->index; in gs_open()
614 struct gs_port *port; in gs_open() local
618 port = ports[port_num].port; in gs_open()
619 if (!port) { in gs_open()
620 status = -ENODEV; in gs_open()
624 spin_lock_irq(&port->port_lock); in gs_open()
627 if (!kfifo_initialized(&port->port_write_buf)) { in gs_open()
629 spin_unlock_irq(&port->port_lock); in gs_open()
636 status = kfifo_alloc(&port->port_write_buf, in gs_open()
644 spin_lock_irq(&port->port_lock); in gs_open()
648 if (port->port.count++) in gs_open()
651 tty->driver_data = port; in gs_open()
652 port->port.tty = tty; in gs_open()
655 if (port->port_usb) { in gs_open()
656 /* if port is suspended, wait resume to start I/0 stream */ in gs_open()
657 if (!port->suspended) { in gs_open()
658 struct gserial *gser = port->port_usb; in gs_open()
660 pr_debug("gs_open: start ttyGS%d\n", port->port_num); in gs_open()
661 gs_start_io(port); in gs_open()
663 if (gser->connect) in gs_open()
664 gser->connect(gser); in gs_open()
666 pr_debug("delay start of ttyGS%d\n", port->port_num); in gs_open()
667 port->start_delayed = true; in gs_open()
671 pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file); in gs_open()
674 spin_unlock_irq(&port->port_lock); in gs_open()
685 spin_lock_irq(&p->port_lock); in gs_close_flush_done()
686 cond = p->port_usb == NULL || !kfifo_len(&p->port_write_buf) || in gs_close_flush_done()
687 p->port.count > 1; in gs_close_flush_done()
688 spin_unlock_irq(&p->port_lock); in gs_close_flush_done()
695 struct gs_port *port = tty->driver_data; in gs_close() local
698 spin_lock_irq(&port->port_lock); in gs_close()
700 if (port->port.count != 1) { in gs_close()
702 if (port->port.count == 0) in gs_close()
705 --port->port.count; in gs_close()
709 pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file); in gs_close()
711 gser = port->port_usb; in gs_close()
712 if (gser && !port->suspended && gser->disconnect) in gs_close()
713 gser->disconnect(gser); in gs_close()
718 if (kfifo_len(&port->port_write_buf) > 0 && gser) { in gs_close()
719 spin_unlock_irq(&port->port_lock); in gs_close()
720 wait_event_interruptible_timeout(port->drain_wait, in gs_close()
721 gs_close_flush_done(port), in gs_close()
723 spin_lock_irq(&port->port_lock); in gs_close()
725 if (port->port.count != 1) in gs_close()
728 gser = port->port_usb; in gs_close()
733 * let the push async work fire again until we're re-opened. in gs_close()
736 kfifo_free(&port->port_write_buf); in gs_close()
738 kfifo_reset(&port->port_write_buf); in gs_close()
740 port->start_delayed = false; in gs_close()
741 port->port.count = 0; in gs_close()
742 port->port.tty = NULL; in gs_close()
745 port->port_num, tty, file); in gs_close()
747 wake_up(&port->close_wait); in gs_close()
749 spin_unlock_irq(&port->port_lock); in gs_close()
754 struct gs_port *port = tty->driver_data; in gs_write() local
757 struct gserial *gser = port->port_usb; in gs_write()
760 port->port_num, tty, count); in gs_write()
762 spin_lock_irqsave(&port->port_lock, flags); in gs_write()
764 count = kfifo_in(&port->port_write_buf, buf, count); in gs_write()
766 if (port->suspended) { in gs_write()
767 spin_unlock_irqrestore(&port->port_lock, flags); in gs_write()
770 pr_debug("ttyGS%d: Remote wakeup failed:%d\n", port->port_num, ret); in gs_write()
773 spin_lock_irqsave(&port->port_lock, flags); in gs_write()
777 if (port->port_usb) in gs_write()
778 gs_start_tx(port); in gs_write()
779 spin_unlock_irqrestore(&port->port_lock, flags); in gs_write()
786 struct gs_port *port = tty->driver_data; in gs_put_char() local
791 port->port_num, tty, ch, __builtin_return_address(0)); in gs_put_char()
793 spin_lock_irqsave(&port->port_lock, flags); in gs_put_char()
794 status = kfifo_put(&port->port_write_buf, ch); in gs_put_char()
795 spin_unlock_irqrestore(&port->port_lock, flags); in gs_put_char()
802 struct gs_port *port = tty->driver_data; in gs_flush_chars() local
805 struct gserial *gser = port->port_usb; in gs_flush_chars()
807 pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty); in gs_flush_chars()
809 spin_lock_irqsave(&port->port_lock, flags); in gs_flush_chars()
810 if (port->suspended) { in gs_flush_chars()
811 spin_unlock_irqrestore(&port->port_lock, flags); in gs_flush_chars()
814 pr_debug("ttyGS%d: Remote wakeup failed:%d\n", port->port_num, ret); in gs_flush_chars()
817 spin_lock_irqsave(&port->port_lock, flags); in gs_flush_chars()
820 if (port->port_usb) in gs_flush_chars()
821 gs_start_tx(port); in gs_flush_chars()
822 spin_unlock_irqrestore(&port->port_lock, flags); in gs_flush_chars()
827 struct gs_port *port = tty->driver_data; in gs_write_room() local
831 spin_lock_irqsave(&port->port_lock, flags); in gs_write_room()
832 if (port->port_usb) in gs_write_room()
833 room = kfifo_avail(&port->port_write_buf); in gs_write_room()
834 spin_unlock_irqrestore(&port->port_lock, flags); in gs_write_room()
837 port->port_num, tty, room); in gs_write_room()
844 struct gs_port *port = tty->driver_data; in gs_chars_in_buffer() local
848 spin_lock_irqsave(&port->port_lock, flags); in gs_chars_in_buffer()
849 chars = kfifo_len(&port->port_write_buf); in gs_chars_in_buffer()
850 spin_unlock_irqrestore(&port->port_lock, flags); in gs_chars_in_buffer()
853 port->port_num, tty, chars); in gs_chars_in_buffer()
861 struct gs_port *port = tty->driver_data; in gs_unthrottle() local
864 spin_lock_irqsave(&port->port_lock, flags); in gs_unthrottle()
865 if (port->port_usb) { in gs_unthrottle()
870 pr_vdebug("ttyGS%d: unthrottle\n", port->port_num); in gs_unthrottle()
871 schedule_delayed_work(&port->push, 0); in gs_unthrottle()
873 spin_unlock_irqrestore(&port->port_lock, flags); in gs_unthrottle()
878 struct gs_port *port = tty->driver_data; in gs_break_ctl() local
883 port->port_num, duration); in gs_break_ctl()
885 spin_lock_irq(&port->port_lock); in gs_break_ctl()
886 gser = port->port_usb; in gs_break_ctl()
887 if (gser && gser->send_break) in gs_break_ctl()
888 status = gser->send_break(gser, duration); in gs_break_ctl()
889 spin_unlock_irq(&port->port_lock); in gs_break_ctl()
897 struct gs_port *port = tty->driver_data; in gs_get_icount() local
901 spin_lock_irqsave(&port->port_lock, flags); in gs_get_icount()
902 cnow = port->icount; in gs_get_icount()
903 spin_unlock_irqrestore(&port->port_lock, flags); in gs_get_icount()
905 icount->rx = cnow.rx; in gs_get_icount()
906 icount->tx = cnow.tx; in gs_get_icount()
924 /*-------------------------------------------------------------------------*/
932 struct gs_console *cons = req->context; in gs_console_complete_out()
934 switch (req->status) { in gs_console_complete_out()
937 __func__, ep->name, req->status); in gs_console_complete_out()
941 spin_lock(&cons->lock); in gs_console_complete_out()
942 req->length = 0; in gs_console_complete_out()
943 schedule_work(&cons->work); in gs_console_complete_out()
944 spin_unlock(&cons->lock); in gs_console_complete_out()
946 case -ECONNRESET: in gs_console_complete_out()
947 case -ESHUTDOWN: in gs_console_complete_out()
949 pr_vdebug("%s: %s shutdown\n", __func__, ep->name); in gs_console_complete_out()
956 struct usb_request *req = cons->req; in __gs_console_push()
963 if (req->length) in __gs_console_push()
966 ep = cons->console.data; in __gs_console_push()
967 size = kfifo_out(&cons->buf, req->buf, ep->maxpacket); in __gs_console_push()
971 if (cons->missed && ep->maxpacket >= 64) { in __gs_console_push()
975 len = sprintf(buf, "\n[missed %zu bytes]\n", cons->missed); in __gs_console_push()
976 kfifo_in(&cons->buf, buf, len); in __gs_console_push()
977 cons->missed = 0; in __gs_console_push()
980 req->length = size; in __gs_console_push()
982 spin_unlock_irq(&cons->lock); in __gs_console_push()
984 req->length = 0; in __gs_console_push()
985 spin_lock_irq(&cons->lock); in __gs_console_push()
992 spin_lock_irq(&cons->lock); in gs_console_work()
996 spin_unlock_irq(&cons->lock); in gs_console_work()
1006 spin_lock_irqsave(&cons->lock, flags); in gs_console_write()
1008 n = kfifo_in(&cons->buf, buf, count); in gs_console_write()
1010 cons->missed += count - n; in gs_console_write()
1012 if (cons->req && !cons->req->length) in gs_console_write()
1013 schedule_work(&cons->work); in gs_console_write()
1015 spin_unlock_irqrestore(&cons->lock, flags); in gs_console_write()
1020 *index = co->index; in gs_console_device()
1024 static int gs_console_connect(struct gs_port *port) in gs_console_connect() argument
1026 struct gs_console *cons = port->console; in gs_console_connect()
1033 ep = port->port_usb->in; in gs_console_connect()
1034 req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC); in gs_console_connect()
1036 return -ENOMEM; in gs_console_connect()
1037 req->complete = gs_console_complete_out; in gs_console_connect()
1038 req->context = cons; in gs_console_connect()
1039 req->length = 0; in gs_console_connect()
1041 spin_lock(&cons->lock); in gs_console_connect()
1042 cons->req = req; in gs_console_connect()
1043 cons->console.data = ep; in gs_console_connect()
1044 spin_unlock(&cons->lock); in gs_console_connect()
1046 pr_debug("ttyGS%d: console connected!\n", port->port_num); in gs_console_connect()
1048 schedule_work(&cons->work); in gs_console_connect()
1053 static void gs_console_disconnect(struct gs_port *port) in gs_console_disconnect() argument
1055 struct gs_console *cons = port->console; in gs_console_disconnect()
1062 spin_lock(&cons->lock); in gs_console_disconnect()
1064 req = cons->req; in gs_console_disconnect()
1065 ep = cons->console.data; in gs_console_disconnect()
1066 cons->req = NULL; in gs_console_disconnect()
1068 spin_unlock(&cons->lock); in gs_console_disconnect()
1077 static int gs_console_init(struct gs_port *port) in gs_console_init() argument
1082 if (port->console) in gs_console_init()
1085 cons = kzalloc(sizeof(*port->console), GFP_KERNEL); in gs_console_init()
1087 return -ENOMEM; in gs_console_init()
1089 strcpy(cons->console.name, "ttyGS"); in gs_console_init()
1090 cons->console.write = gs_console_write; in gs_console_init()
1091 cons->console.device = gs_console_device; in gs_console_init()
1092 cons->console.flags = CON_PRINTBUFFER; in gs_console_init()
1093 cons->console.index = port->port_num; in gs_console_init()
1095 INIT_WORK(&cons->work, gs_console_work); in gs_console_init()
1096 spin_lock_init(&cons->lock); in gs_console_init()
1098 err = kfifo_alloc(&cons->buf, GS_CONSOLE_BUF_SIZE, GFP_KERNEL); in gs_console_init()
1100 pr_err("ttyGS%d: allocate console buffer failed\n", port->port_num); in gs_console_init()
1105 port->console = cons; in gs_console_init()
1106 register_console(&cons->console); in gs_console_init()
1108 spin_lock_irq(&port->port_lock); in gs_console_init()
1109 if (port->port_usb) in gs_console_init()
1110 gs_console_connect(port); in gs_console_init()
1111 spin_unlock_irq(&port->port_lock); in gs_console_init()
1116 static void gs_console_exit(struct gs_port *port) in gs_console_exit() argument
1118 struct gs_console *cons = port->console; in gs_console_exit()
1123 unregister_console(&cons->console); in gs_console_exit()
1125 spin_lock_irq(&port->port_lock); in gs_console_exit()
1126 if (cons->req) in gs_console_exit()
1127 gs_console_disconnect(port); in gs_console_exit()
1128 spin_unlock_irq(&port->port_lock); in gs_console_exit()
1130 cancel_work_sync(&cons->work); in gs_console_exit()
1131 kfifo_free(&cons->buf); in gs_console_exit()
1133 port->console = NULL; in gs_console_exit()
1138 struct gs_port *port; in gserial_set_console() local
1147 port = ports[port_num].port; in gserial_set_console()
1149 if (WARN_ON(port == NULL)) { in gserial_set_console()
1150 ret = -ENXIO; in gserial_set_console()
1155 ret = gs_console_init(port); in gserial_set_console()
1157 gs_console_exit(port); in gserial_set_console()
1167 struct gs_port *port; in gserial_get_console() local
1171 port = ports[port_num].port; in gserial_get_console()
1173 if (WARN_ON(port == NULL)) in gserial_get_console()
1174 ret = -ENXIO; in gserial_get_console()
1176 ret = sprintf(page, "%u\n", !!port->console); in gserial_get_console()
1186 static int gs_console_connect(struct gs_port *port) in gs_console_connect() argument
1191 static void gs_console_disconnect(struct gs_port *port) in gs_console_disconnect() argument
1195 static int gs_console_init(struct gs_port *port) in gs_console_init() argument
1197 return -ENOSYS; in gs_console_init()
1200 static void gs_console_exit(struct gs_port *port) in gs_console_exit() argument
1209 struct gs_port *port; in gs_port_alloc() local
1213 if (ports[port_num].port) { in gs_port_alloc()
1214 ret = -EBUSY; in gs_port_alloc()
1218 port = kzalloc(sizeof(struct gs_port), GFP_KERNEL); in gs_port_alloc()
1219 if (port == NULL) { in gs_port_alloc()
1220 ret = -ENOMEM; in gs_port_alloc()
1224 tty_port_init(&port->port); in gs_port_alloc()
1225 spin_lock_init(&port->port_lock); in gs_port_alloc()
1226 init_waitqueue_head(&port->drain_wait); in gs_port_alloc()
1227 init_waitqueue_head(&port->close_wait); in gs_port_alloc()
1229 INIT_DELAYED_WORK(&port->push, gs_rx_push); in gs_port_alloc()
1231 INIT_LIST_HEAD(&port->read_pool); in gs_port_alloc()
1232 INIT_LIST_HEAD(&port->read_queue); in gs_port_alloc()
1233 INIT_LIST_HEAD(&port->write_pool); in gs_port_alloc()
1235 port->port_num = port_num; in gs_port_alloc()
1236 port->port_line_coding = *coding; in gs_port_alloc()
1238 ports[port_num].port = port; in gs_port_alloc()
1244 static int gs_closed(struct gs_port *port) in gs_closed() argument
1248 spin_lock_irq(&port->port_lock); in gs_closed()
1249 cond = port->port.count == 0; in gs_closed()
1250 spin_unlock_irq(&port->port_lock); in gs_closed()
1255 static void gserial_free_port(struct gs_port *port) in gserial_free_port() argument
1257 cancel_delayed_work_sync(&port->push); in gserial_free_port()
1259 wait_event(port->close_wait, gs_closed(port)); in gserial_free_port()
1260 WARN_ON(port->port_usb != NULL); in gserial_free_port()
1261 tty_port_destroy(&port->port); in gserial_free_port()
1262 kfree(port); in gserial_free_port()
1267 struct gs_port *port; in gserial_free_line() local
1270 if (!ports[port_num].port) { in gserial_free_line()
1274 port = ports[port_num].port; in gserial_free_line()
1275 gs_console_exit(port); in gserial_free_line()
1276 ports[port_num].port = NULL; in gserial_free_line()
1279 gserial_free_port(port); in gserial_free_line()
1287 struct gs_port *port; in gserial_alloc_line_no_console() local
1299 if (ret == -EBUSY) in gserial_alloc_line_no_console()
1310 port = ports[port_num].port; in gserial_alloc_line_no_console()
1311 tty_dev = tty_port_register_device(&port->port, in gserial_alloc_line_no_console()
1314 pr_err("%s: failed to register tty for port %d, err %ld\n", in gserial_alloc_line_no_console()
1319 ports[port_num].port = NULL; in gserial_alloc_line_no_console()
1321 gserial_free_port(port); in gserial_alloc_line_no_console()
1335 gs_console_init(ports[*line_num].port); in gserial_alloc_line()
1342 * gserial_connect - notify TTY I/O glue that USB link is active
1344 * @port_num: which port is active
1355 * before calling this, as well as the appropriate (speed-specific)
1360 * On success, ep->driver_data will be overwritten.
1364 struct gs_port *port; in gserial_connect() local
1369 return -ENXIO; in gserial_connect()
1371 port = ports[port_num].port; in gserial_connect()
1372 if (!port) { in gserial_connect()
1374 return -EINVAL; in gserial_connect()
1376 if (port->port_usb) { in gserial_connect()
1378 return -EBUSY; in gserial_connect()
1382 status = usb_ep_enable(gser->in); in gserial_connect()
1385 gser->in->driver_data = port; in gserial_connect()
1387 status = usb_ep_enable(gser->out); in gserial_connect()
1390 gser->out->driver_data = port; in gserial_connect()
1393 spin_lock_irqsave(&port->port_lock, flags); in gserial_connect()
1394 gser->ioport = port; in gserial_connect()
1395 port->port_usb = gser; in gserial_connect()
1400 gser->port_line_coding = port->port_line_coding; in gserial_connect()
1407 if (port->port.count) { in gserial_connect()
1408 pr_debug("gserial_connect: start ttyGS%d\n", port->port_num); in gserial_connect()
1409 gs_start_io(port); in gserial_connect()
1410 if (gser->connect) in gserial_connect()
1411 gser->connect(gser); in gserial_connect()
1413 if (gser->disconnect) in gserial_connect()
1414 gser->disconnect(gser); in gserial_connect()
1417 status = gs_console_connect(port); in gserial_connect()
1418 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_connect()
1423 usb_ep_disable(gser->in); in gserial_connect()
1428 * gserial_disconnect - notify TTY I/O glue that USB link is inactive
1440 struct gs_port *port = gser->ioport; in gserial_disconnect() local
1443 if (!port) in gserial_disconnect()
1449 spin_lock(&port->port_lock); in gserial_disconnect()
1451 gs_console_disconnect(port); in gserial_disconnect()
1454 port->port_line_coding = gser->port_line_coding; in gserial_disconnect()
1456 port->port_usb = NULL; in gserial_disconnect()
1457 gser->ioport = NULL; in gserial_disconnect()
1458 if (port->port.count > 0) { in gserial_disconnect()
1459 wake_up_interruptible(&port->drain_wait); in gserial_disconnect()
1460 if (port->port.tty) in gserial_disconnect()
1461 tty_hangup(port->port.tty); in gserial_disconnect()
1463 port->suspended = false; in gserial_disconnect()
1464 spin_unlock(&port->port_lock); in gserial_disconnect()
1468 usb_ep_disable(gser->out); in gserial_disconnect()
1469 usb_ep_disable(gser->in); in gserial_disconnect()
1472 spin_lock_irqsave(&port->port_lock, flags); in gserial_disconnect()
1473 if (port->port.count == 0) in gserial_disconnect()
1474 kfifo_free(&port->port_write_buf); in gserial_disconnect()
1475 gs_free_requests(gser->out, &port->read_pool, NULL); in gserial_disconnect()
1476 gs_free_requests(gser->out, &port->read_queue, NULL); in gserial_disconnect()
1477 gs_free_requests(gser->in, &port->write_pool, NULL); in gserial_disconnect()
1479 port->read_allocated = port->read_started = in gserial_disconnect()
1480 port->write_allocated = port->write_started = 0; in gserial_disconnect()
1482 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_disconnect()
1488 struct gs_port *port; in gserial_suspend() local
1492 port = gser->ioport; in gserial_suspend()
1494 if (!port) { in gserial_suspend()
1499 if (port->write_busy || port->write_started) { in gserial_suspend()
1507 spin_lock(&port->port_lock); in gserial_suspend()
1509 port->suspended = true; in gserial_suspend()
1510 port->start_delayed = true; in gserial_suspend()
1511 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_suspend()
1517 struct gs_port *port; in gserial_resume() local
1521 port = gser->ioport; in gserial_resume()
1523 if (!port) { in gserial_resume()
1528 spin_lock(&port->port_lock); in gserial_resume()
1530 port->suspended = false; in gserial_resume()
1531 if (!port->start_delayed) { in gserial_resume()
1532 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_resume()
1536 pr_debug("delayed start ttyGS%d\n", port->port_num); in gserial_resume()
1537 gs_start_io(port); in gserial_resume()
1538 if (gser->connect) in gserial_resume()
1539 gser->connect(gser); in gserial_resume()
1540 port->start_delayed = false; in gserial_resume()
1541 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_resume()
1556 driver->driver_name = "g_serial"; in userial_init()
1557 driver->name = "ttyGS"; in userial_init()
1560 driver->type = TTY_DRIVER_TYPE_SERIAL; in userial_init()
1561 driver->subtype = SERIAL_TYPE_NORMAL; in userial_init()
1562 driver->init_termios = tty_std_termios; in userial_init()
1564 /* 9600-8-N-1 ... matches defaults expected by "usbser.sys" on in userial_init()
1565 * MS-Windows. Otherwise, most of these flags shouldn't affect in userial_init()
1568 driver->init_termios.c_cflag = in userial_init()
1570 driver->init_termios.c_ispeed = 9600; in userial_init()
1571 driver->init_termios.c_ospeed = 9600; in userial_init()
1606 MODULE_DESCRIPTION("utilities for USB gadget \"serial port\"/TTY support");