Lines Matching refs:port
3 * u_serial.c - utilities for USB gadget "serial port"/TTY support
37 * "serial port" functionality through the USB gadget stack. Each such
38 * port is exposed through a /dev/ttyGS* node.
40 * After this module has been loaded, the individual TTY port can be requested
44 * host issues a config change event. Data can only flow when the port is
47 * A given TTY port can be made available in multiple configurations.
54 * Configurations may expose more than one TTY port. For example, if
99 * The port structure holds info for each port, one for each minor number
103 struct tty_port port;
127 bool suspended; /* port suspended */
136 struct gs_port *port;
207 gs_send_packet(struct gs_port *port, char *packet, unsigned size)
211 len = kfifo_len(&port->port_write_buf);
215 size = kfifo_out(&port->port_write_buf, packet, size);
230 static int gs_start_tx(struct gs_port *port)
232 __releases(&port->port_lock)
233 __acquires(&port->port_lock)
236 struct list_head *pool = &port->write_pool;
241 if (!port->port_usb)
244 in = port->port_usb->in;
246 while (!port->write_busy && !list_empty(pool)) {
250 if (port->write_started >= QUEUE_SIZE)
254 len = gs_send_packet(port, req->buf, in->maxpacket);
256 wake_up_interruptible(&port->drain_wait);
263 req->zero = kfifo_is_empty(&port->port_write_buf);
265 pr_vdebug("ttyGS%d: tx len=%d, %3ph ...\n", port->port_num, len, req->buf);
274 port->write_busy = true;
275 spin_unlock(&port->port_lock);
277 spin_lock(&port->port_lock);
278 port->write_busy = false;
287 port->write_started++;
290 if (!port->port_usb)
294 if (do_tty_wake && port->port.tty)
295 tty_wakeup(port->port.tty);
302 static unsigned gs_start_rx(struct gs_port *port)
304 __releases(&port->port_lock)
305 __acquires(&port->port_lock)
308 struct list_head *pool = &port->read_pool;
309 struct usb_ep *out = port->port_usb->out;
317 tty = port->port.tty;
321 if (port->read_started >= QUEUE_SIZE)
331 spin_unlock(&port->port_lock);
333 spin_lock(&port->port_lock);
341 port->read_started++;
344 if (!port->port_usb)
347 return port->read_started;
363 struct gs_port *port = container_of(w, struct gs_port, push);
365 struct list_head *queue = &port->read_queue;
370 spin_lock_irq(&port->port_lock);
371 tty = port->port.tty;
384 pr_vdebug("ttyGS%d: shutdown\n", port->port_num);
390 port->port_num, req->status);
405 n = port->n_read;
411 count = tty_insert_flip_string(&port->port, packet,
417 port->n_read += count;
419 port->port_num, count, req->actual);
422 port->n_read = 0;
425 list_move(&req->list, &port->read_pool);
426 port->read_started--;
433 tty_flip_buffer_push(&port->port);
444 schedule_delayed_work(&port->push, 1);
447 if (!disconnect && port->port_usb)
448 gs_start_rx(port);
450 spin_unlock_irq(&port->port_lock);
455 struct gs_port *port = ep->driver_data;
458 spin_lock(&port->port_lock);
459 list_add_tail(&req->list, &port->read_queue);
460 schedule_delayed_work(&port->push, 0);
461 spin_unlock(&port->port_lock);
466 struct gs_port *port = ep->driver_data;
468 spin_lock(&port->port_lock);
469 list_add(&req->list, &port->write_pool);
470 port->write_started--;
480 gs_start_tx(port);
489 spin_unlock(&port->port_lock);
532 * @port: port to use
536 * this port. If nothing is listening on the host side, we may
539 static int gs_start_io(struct gs_port *port)
541 struct list_head *head = &port->read_pool;
546 if (!port->port_usb || !port->port.tty)
552 * configurations may use different endpoints with a given port;
555 ep = port->port_usb->out;
557 &port->read_allocated);
561 status = gs_alloc_requests(port->port_usb->in, &port->write_pool,
562 gs_write_complete, &port->write_allocated);
564 gs_free_requests(ep, head, &port->read_allocated);
569 port->n_read = 0;
570 started = gs_start_rx(port);
573 gs_start_tx(port);
576 tty_wakeup(port->port.tty);
578 gs_free_requests(ep, head, &port->read_allocated);
579 gs_free_requests(port->port_usb->in, &port->write_pool,
580 &port->write_allocated);
599 struct gs_port *port;
603 port = ports[port_num].port;
604 if (!port) {
609 spin_lock_irq(&port->port_lock);
612 if (!kfifo_initialized(&port->port_write_buf)) {
614 spin_unlock_irq(&port->port_lock);
621 status = kfifo_alloc(&port->port_write_buf,
629 spin_lock_irq(&port->port_lock);
633 if (port->port.count++)
636 tty->driver_data = port;
637 port->port.tty = tty;
640 if (port->port_usb) {
641 /* if port is suspended, wait resume to start I/0 stream */
642 if (!port->suspended) {
643 struct gserial *gser = port->port_usb;
645 pr_debug("gs_open: start ttyGS%d\n", port->port_num);
646 gs_start_io(port);
651 pr_debug("delay start of ttyGS%d\n", port->port_num);
652 port->start_delayed = true;
656 pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file);
659 spin_unlock_irq(&port->port_lock);
672 p->port.count > 1;
680 struct gs_port *port = tty->driver_data;
683 spin_lock_irq(&port->port_lock);
685 if (port->port.count != 1) {
687 if (port->port.count == 0)
690 --port->port.count;
694 pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file);
696 gser = port->port_usb;
697 if (gser && !port->suspended && gser->disconnect)
703 if (kfifo_len(&port->port_write_buf) > 0 && gser) {
704 spin_unlock_irq(&port->port_lock);
705 wait_event_interruptible_timeout(port->drain_wait,
706 gs_close_flush_done(port),
708 spin_lock_irq(&port->port_lock);
710 if (port->port.count != 1)
713 gser = port->port_usb;
721 kfifo_free(&port->port_write_buf);
723 kfifo_reset(&port->port_write_buf);
725 port->start_delayed = false;
726 port->port.count = 0;
727 port->port.tty = NULL;
730 port->port_num, tty, file);
732 wake_up(&port->close_wait);
734 spin_unlock_irq(&port->port_lock);
739 struct gs_port *port = tty->driver_data;
743 port->port_num, tty, count);
745 spin_lock_irqsave(&port->port_lock, flags);
747 count = kfifo_in(&port->port_write_buf, buf, count);
749 if (port->port_usb)
750 gs_start_tx(port);
751 spin_unlock_irqrestore(&port->port_lock, flags);
758 struct gs_port *port = tty->driver_data;
763 port->port_num, tty, ch, __builtin_return_address(0));
765 spin_lock_irqsave(&port->port_lock, flags);
766 status = kfifo_put(&port->port_write_buf, ch);
767 spin_unlock_irqrestore(&port->port_lock, flags);
774 struct gs_port *port = tty->driver_data;
777 pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
779 spin_lock_irqsave(&port->port_lock, flags);
780 if (port->port_usb)
781 gs_start_tx(port);
782 spin_unlock_irqrestore(&port->port_lock, flags);
787 struct gs_port *port = tty->driver_data;
791 spin_lock_irqsave(&port->port_lock, flags);
792 if (port->port_usb)
793 room = kfifo_avail(&port->port_write_buf);
794 spin_unlock_irqrestore(&port->port_lock, flags);
797 port->port_num, tty, room);
804 struct gs_port *port = tty->driver_data;
808 spin_lock_irqsave(&port->port_lock, flags);
809 chars = kfifo_len(&port->port_write_buf);
810 spin_unlock_irqrestore(&port->port_lock, flags);
813 port->port_num, tty, chars);
821 struct gs_port *port = tty->driver_data;
824 spin_lock_irqsave(&port->port_lock, flags);
825 if (port->port_usb) {
830 pr_vdebug("ttyGS%d: unthrottle\n", port->port_num);
831 schedule_delayed_work(&port->push, 0);
833 spin_unlock_irqrestore(&port->port_lock, flags);
838 struct gs_port *port = tty->driver_data;
843 port->port_num, duration);
845 spin_lock_irq(&port->port_lock);
846 gser = port->port_usb;
849 spin_unlock_irq(&port->port_lock);
966 static int gs_console_connect(struct gs_port *port)
968 struct gs_console *cons = port->console;
975 ep = port->port_usb->in;
988 pr_debug("ttyGS%d: console connected!\n", port->port_num);
995 static void gs_console_disconnect(struct gs_port *port)
997 struct gs_console *cons = port->console;
1019 static int gs_console_init(struct gs_port *port)
1024 if (port->console)
1027 cons = kzalloc(sizeof(*port->console), GFP_KERNEL);
1035 cons->console.index = port->port_num;
1042 pr_err("ttyGS%d: allocate console buffer failed\n", port->port_num);
1047 port->console = cons;
1050 spin_lock_irq(&port->port_lock);
1051 if (port->port_usb)
1052 gs_console_connect(port);
1053 spin_unlock_irq(&port->port_lock);
1058 static void gs_console_exit(struct gs_port *port)
1060 struct gs_console *cons = port->console;
1067 spin_lock_irq(&port->port_lock);
1069 gs_console_disconnect(port);
1070 spin_unlock_irq(&port->port_lock);
1075 port->console = NULL;
1080 struct gs_port *port;
1089 port = ports[port_num].port;
1091 if (WARN_ON(port == NULL)) {
1097 ret = gs_console_init(port);
1099 gs_console_exit(port);
1109 struct gs_port *port;
1113 port = ports[port_num].port;
1115 if (WARN_ON(port == NULL))
1118 ret = sprintf(page, "%u\n", !!port->console);
1128 static int gs_console_connect(struct gs_port *port)
1133 static void gs_console_disconnect(struct gs_port *port)
1137 static int gs_console_init(struct gs_port *port)
1142 static void gs_console_exit(struct gs_port *port)
1151 struct gs_port *port;
1155 if (ports[port_num].port) {
1160 port = kzalloc(sizeof(struct gs_port), GFP_KERNEL);
1161 if (port == NULL) {
1166 tty_port_init(&port->port);
1167 spin_lock_init(&port->port_lock);
1168 init_waitqueue_head(&port->drain_wait);
1169 init_waitqueue_head(&port->close_wait);
1171 INIT_DELAYED_WORK(&port->push, gs_rx_push);
1173 INIT_LIST_HEAD(&port->read_pool);
1174 INIT_LIST_HEAD(&port->read_queue);
1175 INIT_LIST_HEAD(&port->write_pool);
1177 port->port_num = port_num;
1178 port->port_line_coding = *coding;
1180 ports[port_num].port = port;
1186 static int gs_closed(struct gs_port *port)
1190 spin_lock_irq(&port->port_lock);
1191 cond = port->port.count == 0;
1192 spin_unlock_irq(&port->port_lock);
1197 static void gserial_free_port(struct gs_port *port)
1199 cancel_delayed_work_sync(&port->push);
1201 wait_event(port->close_wait, gs_closed(port));
1202 WARN_ON(port->port_usb != NULL);
1203 tty_port_destroy(&port->port);
1204 kfree(port);
1209 struct gs_port *port;
1212 if (!ports[port_num].port) {
1216 port = ports[port_num].port;
1217 gs_console_exit(port);
1218 ports[port_num].port = NULL;
1221 gserial_free_port(port);
1229 struct gs_port *port;
1252 port = ports[port_num].port;
1253 tty_dev = tty_port_register_device(&port->port,
1256 pr_err("%s: failed to register tty for port %d, err %ld\n",
1261 ports[port_num].port = NULL;
1263 gserial_free_port(port);
1277 gs_console_init(ports[*line_num].port);
1286 * @port_num: which port is active
1306 struct gs_port *port;
1313 port = ports[port_num].port;
1314 if (!port) {
1318 if (port->port_usb) {
1327 gser->in->driver_data = port;
1332 gser->out->driver_data = port;
1335 spin_lock_irqsave(&port->port_lock, flags);
1336 gser->ioport = port;
1337 port->port_usb = gser;
1342 gser->port_line_coding = port->port_line_coding;
1349 if (port->port.count) {
1350 pr_debug("gserial_connect: start ttyGS%d\n", port->port_num);
1351 gs_start_io(port);
1359 status = gs_console_connect(port);
1360 spin_unlock_irqrestore(&port->port_lock, flags);
1382 struct gs_port *port = gser->ioport;
1385 if (!port)
1391 spin_lock(&port->port_lock);
1393 gs_console_disconnect(port);
1396 port->port_line_coding = gser->port_line_coding;
1398 port->port_usb = NULL;
1400 if (port->port.count > 0) {
1401 wake_up_interruptible(&port->drain_wait);
1402 if (port->port.tty)
1403 tty_hangup(port->port.tty);
1405 port->suspended = false;
1406 spin_unlock(&port->port_lock);
1414 spin_lock_irqsave(&port->port_lock, flags);
1415 if (port->port.count == 0)
1416 kfifo_free(&port->port_write_buf);
1417 gs_free_requests(gser->out, &port->read_pool, NULL);
1418 gs_free_requests(gser->out, &port->read_queue, NULL);
1419 gs_free_requests(gser->in, &port->write_pool, NULL);
1421 port->read_allocated = port->read_started =
1422 port->write_allocated = port->write_started = 0;
1424 spin_unlock_irqrestore(&port->port_lock, flags);
1430 struct gs_port *port;
1434 port = gser->ioport;
1436 if (!port) {
1441 spin_lock(&port->port_lock);
1443 port->suspended = true;
1444 spin_unlock_irqrestore(&port->port_lock, flags);
1450 struct gs_port *port;
1454 port = gser->ioport;
1456 if (!port) {
1461 spin_lock(&port->port_lock);
1463 port->suspended = false;
1464 if (!port->start_delayed) {
1465 spin_unlock_irqrestore(&port->port_lock, flags);
1469 pr_debug("delayed start ttyGS%d\n", port->port_num);
1470 gs_start_io(port);
1473 port->start_delayed = false;
1474 spin_unlock_irqrestore(&port->port_lock, flags);