Lines Matching full:port
3 * u_serial.c - utilities for USB gadget "serial port"/TTY support
36 * "serial port" functionality through the USB gadget stack. Each such
37 * port is exposed through a /dev/ttyGS* node.
39 * After this module has been loaded, the individual TTY port can be requested
43 * host issues a config change event. Data can only flow when the port is
46 * A given TTY port can be made available in multiple configurations.
53 * Configurations may expose more than one TTY port. For example, if
95 * The port structure holds info for each port, one for each minor number
99 struct tty_port port; member
123 bool suspended; /* port suspended */
132 struct gs_port *port; member
203 gs_send_packet(struct gs_port *port, char *packet, unsigned size) in gs_send_packet() argument
207 len = kfifo_len(&port->port_write_buf); in gs_send_packet()
211 size = kfifo_out(&port->port_write_buf, packet, size); in gs_send_packet()
226 static int gs_start_tx(struct gs_port *port) in gs_start_tx() argument
228 __releases(&port->port_lock) in gs_start_tx()
229 __acquires(&port->port_lock) in gs_start_tx()
232 struct list_head *pool = &port->write_pool; in gs_start_tx()
237 if (!port->port_usb) in gs_start_tx()
240 in = port->port_usb->in; in gs_start_tx()
242 while (!port->write_busy && !list_empty(pool)) { in gs_start_tx()
246 if (port->write_started >= QUEUE_SIZE) in gs_start_tx()
250 len = gs_send_packet(port, req->buf, in->maxpacket); in gs_start_tx()
252 wake_up_interruptible(&port->drain_wait); in gs_start_tx()
259 req->zero = kfifo_is_empty(&port->port_write_buf); in gs_start_tx()
261 pr_vdebug("ttyGS%d: tx len=%d, %3ph ...\n", port->port_num, len, req->buf); in gs_start_tx()
270 port->write_busy = true; in gs_start_tx()
271 spin_unlock(&port->port_lock); in gs_start_tx()
273 spin_lock(&port->port_lock); in gs_start_tx()
274 port->write_busy = false; in gs_start_tx()
283 port->write_started++; in gs_start_tx()
286 if (!port->port_usb) in gs_start_tx()
290 if (do_tty_wake && port->port.tty) in gs_start_tx()
291 tty_wakeup(port->port.tty); in gs_start_tx()
298 static unsigned gs_start_rx(struct gs_port *port) in gs_start_rx() argument
300 __releases(&port->port_lock) in gs_start_rx()
301 __acquires(&port->port_lock) in gs_start_rx()
304 struct list_head *pool = &port->read_pool; in gs_start_rx()
305 struct usb_ep *out = port->port_usb->out; in gs_start_rx()
313 tty = port->port.tty; in gs_start_rx()
317 if (port->read_started >= QUEUE_SIZE) in gs_start_rx()
327 spin_unlock(&port->port_lock); in gs_start_rx()
329 spin_lock(&port->port_lock); in gs_start_rx()
337 port->read_started++; in gs_start_rx()
340 if (!port->port_usb) in gs_start_rx()
343 return port->read_started; in gs_start_rx()
359 struct gs_port *port = container_of(w, struct gs_port, push); in gs_rx_push() local
361 struct list_head *queue = &port->read_queue; in gs_rx_push()
366 spin_lock_irq(&port->port_lock); in gs_rx_push()
367 tty = port->port.tty; in gs_rx_push()
380 pr_vdebug("ttyGS%d: shutdown\n", port->port_num); in gs_rx_push()
386 port->port_num, req->status); in gs_rx_push()
401 n = port->n_read; in gs_rx_push()
407 count = tty_insert_flip_string(&port->port, packet, in gs_rx_push()
413 port->n_read += count; in gs_rx_push()
415 port->port_num, count, req->actual); in gs_rx_push()
418 port->n_read = 0; in gs_rx_push()
421 list_move(&req->list, &port->read_pool); in gs_rx_push()
422 port->read_started--; in gs_rx_push()
429 tty_flip_buffer_push(&port->port); in gs_rx_push()
440 schedule_delayed_work(&port->push, 1); in gs_rx_push()
443 if (!disconnect && port->port_usb) in gs_rx_push()
444 gs_start_rx(port); in gs_rx_push()
446 spin_unlock_irq(&port->port_lock); in gs_rx_push()
451 struct gs_port *port = ep->driver_data; in gs_read_complete() local
454 spin_lock(&port->port_lock); in gs_read_complete()
455 list_add_tail(&req->list, &port->read_queue); in gs_read_complete()
456 schedule_delayed_work(&port->push, 0); in gs_read_complete()
457 spin_unlock(&port->port_lock); in gs_read_complete()
462 struct gs_port *port = ep->driver_data; in gs_write_complete() local
464 spin_lock(&port->port_lock); in gs_write_complete()
465 list_add(&req->list, &port->write_pool); in gs_write_complete()
466 port->write_started--; in gs_write_complete()
476 gs_start_tx(port); in gs_write_complete()
485 spin_unlock(&port->port_lock); in gs_write_complete()
528 * @port: port to use
532 * this port. If nothing is listening on the host side, we may
535 static int gs_start_io(struct gs_port *port) in gs_start_io() argument
537 struct list_head *head = &port->read_pool; in gs_start_io()
538 struct usb_ep *ep = port->port_usb->out; in gs_start_io()
545 * configurations may use different endpoints with a given port; in gs_start_io()
549 &port->read_allocated); in gs_start_io()
553 status = gs_alloc_requests(port->port_usb->in, &port->write_pool, in gs_start_io()
554 gs_write_complete, &port->write_allocated); in gs_start_io()
556 gs_free_requests(ep, head, &port->read_allocated); in gs_start_io()
561 port->n_read = 0; in gs_start_io()
562 started = gs_start_rx(port); in gs_start_io()
565 gs_start_tx(port); in gs_start_io()
568 tty_wakeup(port->port.tty); in gs_start_io()
570 gs_free_requests(ep, head, &port->read_allocated); in gs_start_io()
571 gs_free_requests(port->port_usb->in, &port->write_pool, in gs_start_io()
572 &port->write_allocated); in gs_start_io()
591 struct gs_port *port; in gs_open() local
595 port = ports[port_num].port; in gs_open()
596 if (!port) { in gs_open()
601 spin_lock_irq(&port->port_lock); in gs_open()
604 if (!kfifo_initialized(&port->port_write_buf)) { in gs_open()
606 spin_unlock_irq(&port->port_lock); in gs_open()
613 status = kfifo_alloc(&port->port_write_buf, in gs_open()
621 spin_lock_irq(&port->port_lock); in gs_open()
625 if (port->port.count++) in gs_open()
628 tty->driver_data = port; in gs_open()
629 port->port.tty = tty; in gs_open()
632 if (port->port_usb) { in gs_open()
633 /* if port is suspended, wait resume to start I/0 stream */ in gs_open()
634 if (!port->suspended) { in gs_open()
635 struct gserial *gser = port->port_usb; in gs_open()
637 pr_debug("gs_open: start ttyGS%d\n", port->port_num); in gs_open()
638 gs_start_io(port); in gs_open()
643 pr_debug("delay start of ttyGS%d\n", port->port_num); in gs_open()
644 port->start_delayed = true; in gs_open()
648 pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file); in gs_open()
651 spin_unlock_irq(&port->port_lock); in gs_open()
664 p->port.count > 1; in gs_close_flush_done()
672 struct gs_port *port = tty->driver_data; in gs_close() local
675 spin_lock_irq(&port->port_lock); in gs_close()
677 if (port->port.count != 1) { in gs_close()
679 if (port->port.count == 0) in gs_close()
682 --port->port.count; in gs_close()
686 pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file); in gs_close()
688 gser = port->port_usb; in gs_close()
689 if (gser && !port->suspended && gser->disconnect) in gs_close()
695 if (kfifo_len(&port->port_write_buf) > 0 && gser) { in gs_close()
696 spin_unlock_irq(&port->port_lock); in gs_close()
697 wait_event_interruptible_timeout(port->drain_wait, in gs_close()
698 gs_close_flush_done(port), in gs_close()
700 spin_lock_irq(&port->port_lock); in gs_close()
702 if (port->port.count != 1) in gs_close()
705 gser = port->port_usb; in gs_close()
713 kfifo_free(&port->port_write_buf); in gs_close()
715 kfifo_reset(&port->port_write_buf); in gs_close()
717 port->start_delayed = false; in gs_close()
718 port->port.count = 0; in gs_close()
719 port->port.tty = NULL; in gs_close()
722 port->port_num, tty, file); in gs_close()
724 wake_up(&port->close_wait); in gs_close()
726 spin_unlock_irq(&port->port_lock); in gs_close()
731 struct gs_port *port = tty->driver_data; in gs_write() local
735 port->port_num, tty, count); in gs_write()
737 spin_lock_irqsave(&port->port_lock, flags); in gs_write()
739 count = kfifo_in(&port->port_write_buf, buf, count); in gs_write()
741 if (port->port_usb) in gs_write()
742 gs_start_tx(port); in gs_write()
743 spin_unlock_irqrestore(&port->port_lock, flags); in gs_write()
750 struct gs_port *port = tty->driver_data; in gs_put_char() local
755 port->port_num, tty, ch, __builtin_return_address(0)); in gs_put_char()
757 spin_lock_irqsave(&port->port_lock, flags); in gs_put_char()
758 status = kfifo_put(&port->port_write_buf, ch); in gs_put_char()
759 spin_unlock_irqrestore(&port->port_lock, flags); in gs_put_char()
766 struct gs_port *port = tty->driver_data; in gs_flush_chars() local
769 pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty); in gs_flush_chars()
771 spin_lock_irqsave(&port->port_lock, flags); in gs_flush_chars()
772 if (port->port_usb) in gs_flush_chars()
773 gs_start_tx(port); in gs_flush_chars()
774 spin_unlock_irqrestore(&port->port_lock, flags); in gs_flush_chars()
779 struct gs_port *port = tty->driver_data; in gs_write_room() local
783 spin_lock_irqsave(&port->port_lock, flags); in gs_write_room()
784 if (port->port_usb) in gs_write_room()
785 room = kfifo_avail(&port->port_write_buf); in gs_write_room()
786 spin_unlock_irqrestore(&port->port_lock, flags); in gs_write_room()
789 port->port_num, tty, room); in gs_write_room()
796 struct gs_port *port = tty->driver_data; in gs_chars_in_buffer() local
800 spin_lock_irqsave(&port->port_lock, flags); in gs_chars_in_buffer()
801 chars = kfifo_len(&port->port_write_buf); in gs_chars_in_buffer()
802 spin_unlock_irqrestore(&port->port_lock, flags); in gs_chars_in_buffer()
805 port->port_num, tty, chars); in gs_chars_in_buffer()
813 struct gs_port *port = tty->driver_data; in gs_unthrottle() local
816 spin_lock_irqsave(&port->port_lock, flags); in gs_unthrottle()
817 if (port->port_usb) { in gs_unthrottle()
822 pr_vdebug("ttyGS%d: unthrottle\n", port->port_num); in gs_unthrottle()
823 schedule_delayed_work(&port->push, 0); in gs_unthrottle()
825 spin_unlock_irqrestore(&port->port_lock, flags); in gs_unthrottle()
830 struct gs_port *port = tty->driver_data; in gs_break_ctl() local
835 port->port_num, duration); in gs_break_ctl()
837 spin_lock_irq(&port->port_lock); in gs_break_ctl()
838 gser = port->port_usb; in gs_break_ctl()
841 spin_unlock_irq(&port->port_lock); in gs_break_ctl()
955 static int gs_console_connect(struct gs_port *port) in gs_console_connect() argument
957 struct gs_console *cons = port->console; in gs_console_connect()
964 ep = port->port_usb->in; in gs_console_connect()
977 pr_debug("ttyGS%d: console connected!\n", port->port_num); in gs_console_connect()
984 static void gs_console_disconnect(struct gs_port *port) in gs_console_disconnect() argument
986 struct gs_console *cons = port->console; in gs_console_disconnect()
1008 static int gs_console_init(struct gs_port *port) in gs_console_init() argument
1013 if (port->console) in gs_console_init()
1016 cons = kzalloc(sizeof(*port->console), GFP_KERNEL); in gs_console_init()
1024 cons->console.index = port->port_num; in gs_console_init()
1031 pr_err("ttyGS%d: allocate console buffer failed\n", port->port_num); in gs_console_init()
1036 port->console = cons; in gs_console_init()
1039 spin_lock_irq(&port->port_lock); in gs_console_init()
1040 if (port->port_usb) in gs_console_init()
1041 gs_console_connect(port); in gs_console_init()
1042 spin_unlock_irq(&port->port_lock); in gs_console_init()
1047 static void gs_console_exit(struct gs_port *port) in gs_console_exit() argument
1049 struct gs_console *cons = port->console; in gs_console_exit()
1056 spin_lock_irq(&port->port_lock); in gs_console_exit()
1058 gs_console_disconnect(port); in gs_console_exit()
1059 spin_unlock_irq(&port->port_lock); in gs_console_exit()
1064 port->console = NULL; in gs_console_exit()
1069 struct gs_port *port; in gserial_set_console() local
1078 port = ports[port_num].port; in gserial_set_console()
1080 if (WARN_ON(port == NULL)) { in gserial_set_console()
1086 ret = gs_console_init(port); in gserial_set_console()
1088 gs_console_exit(port); in gserial_set_console()
1098 struct gs_port *port; in gserial_get_console() local
1102 port = ports[port_num].port; in gserial_get_console()
1104 if (WARN_ON(port == NULL)) in gserial_get_console()
1107 ret = sprintf(page, "%u\n", !!port->console); in gserial_get_console()
1117 static int gs_console_connect(struct gs_port *port) in gs_console_connect() argument
1122 static void gs_console_disconnect(struct gs_port *port) in gs_console_disconnect() argument
1126 static int gs_console_init(struct gs_port *port) in gs_console_init() argument
1131 static void gs_console_exit(struct gs_port *port) in gs_console_exit() argument
1140 struct gs_port *port; in gs_port_alloc() local
1144 if (ports[port_num].port) { in gs_port_alloc()
1149 port = kzalloc(sizeof(struct gs_port), GFP_KERNEL); in gs_port_alloc()
1150 if (port == NULL) { in gs_port_alloc()
1155 tty_port_init(&port->port); in gs_port_alloc()
1156 spin_lock_init(&port->port_lock); in gs_port_alloc()
1157 init_waitqueue_head(&port->drain_wait); in gs_port_alloc()
1158 init_waitqueue_head(&port->close_wait); in gs_port_alloc()
1160 INIT_DELAYED_WORK(&port->push, gs_rx_push); in gs_port_alloc()
1162 INIT_LIST_HEAD(&port->read_pool); in gs_port_alloc()
1163 INIT_LIST_HEAD(&port->read_queue); in gs_port_alloc()
1164 INIT_LIST_HEAD(&port->write_pool); in gs_port_alloc()
1166 port->port_num = port_num; in gs_port_alloc()
1167 port->port_line_coding = *coding; in gs_port_alloc()
1169 ports[port_num].port = port; in gs_port_alloc()
1175 static int gs_closed(struct gs_port *port) in gs_closed() argument
1179 spin_lock_irq(&port->port_lock); in gs_closed()
1180 cond = port->port.count == 0; in gs_closed()
1181 spin_unlock_irq(&port->port_lock); in gs_closed()
1186 static void gserial_free_port(struct gs_port *port) in gserial_free_port() argument
1188 cancel_delayed_work_sync(&port->push); in gserial_free_port()
1190 wait_event(port->close_wait, gs_closed(port)); in gserial_free_port()
1191 WARN_ON(port->port_usb != NULL); in gserial_free_port()
1192 tty_port_destroy(&port->port); in gserial_free_port()
1193 kfree(port); in gserial_free_port()
1198 struct gs_port *port; in gserial_free_line() local
1201 if (!ports[port_num].port) { in gserial_free_line()
1205 port = ports[port_num].port; in gserial_free_line()
1206 gs_console_exit(port); in gserial_free_line()
1207 ports[port_num].port = NULL; in gserial_free_line()
1210 gserial_free_port(port); in gserial_free_line()
1218 struct gs_port *port; in gserial_alloc_line_no_console() local
1241 port = ports[port_num].port; in gserial_alloc_line_no_console()
1242 tty_dev = tty_port_register_device(&port->port, in gserial_alloc_line_no_console()
1245 pr_err("%s: failed to register tty for port %d, err %ld\n", in gserial_alloc_line_no_console()
1250 ports[port_num].port = NULL; in gserial_alloc_line_no_console()
1252 gserial_free_port(port); in gserial_alloc_line_no_console()
1266 gs_console_init(ports[*line_num].port); in gserial_alloc_line()
1275 * @port_num: which port is active
1295 struct gs_port *port; in gserial_connect() local
1302 port = ports[port_num].port; in gserial_connect()
1303 if (!port) { in gserial_connect()
1307 if (port->port_usb) { in gserial_connect()
1316 gser->in->driver_data = port; in gserial_connect()
1321 gser->out->driver_data = port; in gserial_connect()
1324 spin_lock_irqsave(&port->port_lock, flags); in gserial_connect()
1325 gser->ioport = port; in gserial_connect()
1326 port->port_usb = gser; in gserial_connect()
1331 gser->port_line_coding = port->port_line_coding; in gserial_connect()
1338 if (port->port.count) { in gserial_connect()
1339 pr_debug("gserial_connect: start ttyGS%d\n", port->port_num); in gserial_connect()
1340 gs_start_io(port); in gserial_connect()
1348 status = gs_console_connect(port); in gserial_connect()
1349 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_connect()
1371 struct gs_port *port = gser->ioport; in gserial_disconnect() local
1374 if (!port) in gserial_disconnect()
1378 spin_lock_irqsave(&port->port_lock, flags); in gserial_disconnect()
1380 gs_console_disconnect(port); in gserial_disconnect()
1383 port->port_line_coding = gser->port_line_coding; in gserial_disconnect()
1385 port->port_usb = NULL; in gserial_disconnect()
1387 if (port->port.count > 0) { in gserial_disconnect()
1388 wake_up_interruptible(&port->drain_wait); in gserial_disconnect()
1389 if (port->port.tty) in gserial_disconnect()
1390 tty_hangup(port->port.tty); in gserial_disconnect()
1392 port->suspended = false; in gserial_disconnect()
1393 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_disconnect()
1400 spin_lock_irqsave(&port->port_lock, flags); in gserial_disconnect()
1401 if (port->port.count == 0) in gserial_disconnect()
1402 kfifo_free(&port->port_write_buf); in gserial_disconnect()
1403 gs_free_requests(gser->out, &port->read_pool, NULL); in gserial_disconnect()
1404 gs_free_requests(gser->out, &port->read_queue, NULL); in gserial_disconnect()
1405 gs_free_requests(gser->in, &port->write_pool, NULL); in gserial_disconnect()
1407 port->read_allocated = port->read_started = in gserial_disconnect()
1408 port->write_allocated = port->write_started = 0; in gserial_disconnect()
1410 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_disconnect()
1416 struct gs_port *port = gser->ioport; in gserial_suspend() local
1419 spin_lock_irqsave(&port->port_lock, flags); in gserial_suspend()
1420 port->suspended = true; in gserial_suspend()
1421 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_suspend()
1427 struct gs_port *port = gser->ioport; in gserial_resume() local
1430 spin_lock_irqsave(&port->port_lock, flags); in gserial_resume()
1431 port->suspended = false; in gserial_resume()
1432 if (!port->start_delayed) { in gserial_resume()
1433 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_resume()
1437 pr_debug("delayed start ttyGS%d\n", port->port_num); in gserial_resume()
1438 gs_start_io(port); in gserial_resume()
1441 port->start_delayed = false; in gserial_resume()
1442 spin_unlock_irqrestore(&port->port_lock, flags); in gserial_resume()