Lines Matching refs: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
98 * The port structure holds info for each port, one for each minor number
102 struct tty_port port;
126 bool suspended; /* port suspended */
135 struct gs_port *port;
206 gs_send_packet(struct gs_port *port, char *packet, unsigned size)
210 len = kfifo_len(&port->port_write_buf);
214 size = kfifo_out(&port->port_write_buf, packet, size);
229 static int gs_start_tx(struct gs_port *port)
231 __releases(&port->port_lock)
232 __acquires(&port->port_lock)
235 struct list_head *pool = &port->write_pool;
240 if (!port->port_usb)
243 in = port->port_usb->in;
245 while (!port->write_busy && !list_empty(pool)) {
249 if (port->write_started >= QUEUE_SIZE)
253 len = gs_send_packet(port, req->buf, in->maxpacket);
255 wake_up_interruptible(&port->drain_wait);
262 req->zero = kfifo_is_empty(&port->port_write_buf);
265 port->port_num, len, *((u8 *)req->buf),
275 port->write_busy = true;
276 spin_unlock(&port->port_lock);
278 spin_lock(&port->port_lock);
279 port->write_busy = false;
288 port->write_started++;
291 if (!port->port_usb)
295 if (do_tty_wake && port->port.tty)
296 tty_wakeup(port->port.tty);
303 static unsigned gs_start_rx(struct gs_port *port)
305 __releases(&port->port_lock)
306 __acquires(&port->port_lock)
309 struct list_head *pool = &port->read_pool;
310 struct usb_ep *out = port->port_usb->out;
318 tty = port->port.tty;
322 if (port->read_started >= QUEUE_SIZE)
332 spin_unlock(&port->port_lock);
334 spin_lock(&port->port_lock);
342 port->read_started++;
345 if (!port->port_usb)
348 return port->read_started;
364 struct gs_port *port = container_of(w, struct gs_port, push);
366 struct list_head *queue = &port->read_queue;
371 spin_lock_irq(&port->port_lock);
372 tty = port->port.tty;
385 pr_vdebug("ttyGS%d: shutdown\n", port->port_num);
391 port->port_num, req->status);
406 n = port->n_read;
412 count = tty_insert_flip_string(&port->port, packet,
418 port->n_read += count;
420 port->port_num, count, req->actual);
423 port->n_read = 0;
426 list_move(&req->list, &port->read_pool);
427 port->read_started--;
434 tty_flip_buffer_push(&port->port);
445 schedule_delayed_work(&port->push, 1);
448 if (!disconnect && port->port_usb)
449 gs_start_rx(port);
451 spin_unlock_irq(&port->port_lock);
456 struct gs_port *port = ep->driver_data;
459 spin_lock(&port->port_lock);
460 list_add_tail(&req->list, &port->read_queue);
461 schedule_delayed_work(&port->push, 0);
462 spin_unlock(&port->port_lock);
467 struct gs_port *port = ep->driver_data;
469 spin_lock(&port->port_lock);
470 list_add(&req->list, &port->write_pool);
471 port->write_started--;
481 gs_start_tx(port);
490 spin_unlock(&port->port_lock);
533 * @port: port to use
537 * this port. If nothing is listening on the host side, we may
540 static int gs_start_io(struct gs_port *port)
542 struct list_head *head = &port->read_pool;
543 struct usb_ep *ep = port->port_usb->out;
550 * configurations may use different endpoints with a given port;
554 &port->read_allocated);
558 status = gs_alloc_requests(port->port_usb->in, &port->write_pool,
559 gs_write_complete, &port->write_allocated);
561 gs_free_requests(ep, head, &port->read_allocated);
566 port->n_read = 0;
567 started = gs_start_rx(port);
570 gs_start_tx(port);
573 tty_wakeup(port->port.tty);
575 gs_free_requests(ep, head, &port->read_allocated);
576 gs_free_requests(port->port_usb->in, &port->write_pool,
577 &port->write_allocated);
596 struct gs_port *port;
600 port = ports[port_num].port;
601 if (!port) {
606 spin_lock_irq(&port->port_lock);
609 if (!kfifo_initialized(&port->port_write_buf)) {
611 spin_unlock_irq(&port->port_lock);
618 status = kfifo_alloc(&port->port_write_buf,
626 spin_lock_irq(&port->port_lock);
630 if (port->port.count++)
633 tty->driver_data = port;
634 port->port.tty = tty;
637 if (port->port_usb) {
638 /* if port is suspended, wait resume to start I/0 stream */
639 if (!port->suspended) {
640 struct gserial *gser = port->port_usb;
642 pr_debug("gs_open: start ttyGS%d\n", port->port_num);
643 gs_start_io(port);
648 pr_debug("delay start of ttyGS%d\n", port->port_num);
649 port->start_delayed = true;
653 pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file);
656 spin_unlock_irq(&port->port_lock);
669 p->port.count > 1;
677 struct gs_port *port = tty->driver_data;
680 spin_lock_irq(&port->port_lock);
682 if (port->port.count != 1) {
684 if (port->port.count == 0)
687 --port->port.count;
691 pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file);
693 gser = port->port_usb;
694 if (gser && !port->suspended && gser->disconnect)
700 if (kfifo_len(&port->port_write_buf) > 0 && gser) {
701 spin_unlock_irq(&port->port_lock);
702 wait_event_interruptible_timeout(port->drain_wait,
703 gs_close_flush_done(port),
705 spin_lock_irq(&port->port_lock);
707 if (port->port.count != 1)
710 gser = port->port_usb;
718 kfifo_free(&port->port_write_buf);
720 kfifo_reset(&port->port_write_buf);
722 port->start_delayed = false;
723 port->port.count = 0;
724 port->port.tty = NULL;
727 port->port_num, tty, file);
729 wake_up(&port->close_wait);
731 spin_unlock_irq(&port->port_lock);
736 struct gs_port *port = tty->driver_data;
740 port->port_num, tty, count);
742 spin_lock_irqsave(&port->port_lock, flags);
744 count = kfifo_in(&port->port_write_buf, buf, count);
746 if (port->port_usb)
747 gs_start_tx(port);
748 spin_unlock_irqrestore(&port->port_lock, flags);
755 struct gs_port *port = tty->driver_data;
760 port->port_num, tty, ch, __builtin_return_address(0));
762 spin_lock_irqsave(&port->port_lock, flags);
763 status = kfifo_put(&port->port_write_buf, ch);
764 spin_unlock_irqrestore(&port->port_lock, flags);
771 struct gs_port *port = tty->driver_data;
774 pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
776 spin_lock_irqsave(&port->port_lock, flags);
777 if (port->port_usb)
778 gs_start_tx(port);
779 spin_unlock_irqrestore(&port->port_lock, flags);
784 struct gs_port *port = tty->driver_data;
788 spin_lock_irqsave(&port->port_lock, flags);
789 if (port->port_usb)
790 room = kfifo_avail(&port->port_write_buf);
791 spin_unlock_irqrestore(&port->port_lock, flags);
794 port->port_num, tty, room);
801 struct gs_port *port = tty->driver_data;
805 spin_lock_irqsave(&port->port_lock, flags);
806 chars = kfifo_len(&port->port_write_buf);
807 spin_unlock_irqrestore(&port->port_lock, flags);
810 port->port_num, tty, chars);
818 struct gs_port *port = tty->driver_data;
821 spin_lock_irqsave(&port->port_lock, flags);
822 if (port->port_usb) {
827 pr_vdebug("ttyGS%d: unthrottle\n", port->port_num);
828 schedule_delayed_work(&port->push, 0);
830 spin_unlock_irqrestore(&port->port_lock, flags);
835 struct gs_port *port = tty->driver_data;
840 port->port_num, duration);
842 spin_lock_irq(&port->port_lock);
843 gser = port->port_usb;
846 spin_unlock_irq(&port->port_lock);
963 static int gs_console_connect(struct gs_port *port)
965 struct gs_console *cons = port->console;
972 ep = port->port_usb->in;
985 pr_debug("ttyGS%d: console connected!\n", port->port_num);
992 static void gs_console_disconnect(struct gs_port *port)
994 struct gs_console *cons = port->console;
1016 static int gs_console_init(struct gs_port *port)
1021 if (port->console)
1024 cons = kzalloc(sizeof(*port->console), GFP_KERNEL);
1032 cons->console.index = port->port_num;
1039 pr_err("ttyGS%d: allocate console buffer failed\n", port->port_num);
1044 port->console = cons;
1047 spin_lock_irq(&port->port_lock);
1048 if (port->port_usb)
1049 gs_console_connect(port);
1050 spin_unlock_irq(&port->port_lock);
1055 static void gs_console_exit(struct gs_port *port)
1057 struct gs_console *cons = port->console;
1064 spin_lock_irq(&port->port_lock);
1066 gs_console_disconnect(port);
1067 spin_unlock_irq(&port->port_lock);
1072 port->console = NULL;
1077 struct gs_port *port;
1086 port = ports[port_num].port;
1088 if (WARN_ON(port == NULL)) {
1094 ret = gs_console_init(port);
1096 gs_console_exit(port);
1106 struct gs_port *port;
1110 port = ports[port_num].port;
1112 if (WARN_ON(port == NULL))
1115 ret = sprintf(page, "%u\n", !!port->console);
1125 static int gs_console_connect(struct gs_port *port)
1130 static void gs_console_disconnect(struct gs_port *port)
1134 static int gs_console_init(struct gs_port *port)
1139 static void gs_console_exit(struct gs_port *port)
1148 struct gs_port *port;
1152 if (ports[port_num].port) {
1157 port = kzalloc(sizeof(struct gs_port), GFP_KERNEL);
1158 if (port == NULL) {
1163 tty_port_init(&port->port);
1164 spin_lock_init(&port->port_lock);
1165 init_waitqueue_head(&port->drain_wait);
1166 init_waitqueue_head(&port->close_wait);
1168 INIT_DELAYED_WORK(&port->push, gs_rx_push);
1170 INIT_LIST_HEAD(&port->read_pool);
1171 INIT_LIST_HEAD(&port->read_queue);
1172 INIT_LIST_HEAD(&port->write_pool);
1174 port->port_num = port_num;
1175 port->port_line_coding = *coding;
1177 ports[port_num].port = port;
1183 static int gs_closed(struct gs_port *port)
1187 spin_lock_irq(&port->port_lock);
1188 cond = port->port.count == 0;
1189 spin_unlock_irq(&port->port_lock);
1194 static void gserial_free_port(struct gs_port *port)
1196 cancel_delayed_work_sync(&port->push);
1198 wait_event(port->close_wait, gs_closed(port));
1199 WARN_ON(port->port_usb != NULL);
1200 tty_port_destroy(&port->port);
1201 kfree(port);
1206 struct gs_port *port;
1209 if (WARN_ON(!ports[port_num].port)) {
1213 port = ports[port_num].port;
1214 gs_console_exit(port);
1215 ports[port_num].port = NULL;
1218 gserial_free_port(port);
1226 struct gs_port *port;
1249 port = ports[port_num].port;
1250 tty_dev = tty_port_register_device(&port->port,
1253 pr_err("%s: failed to register tty for port %d, err %ld\n",
1258 ports[port_num].port = NULL;
1260 gserial_free_port(port);
1274 gs_console_init(ports[*line_num].port);
1283 * @port_num: which port is active
1303 struct gs_port *port;
1310 port = ports[port_num].port;
1311 if (!port) {
1315 if (port->port_usb) {
1324 gser->in->driver_data = port;
1329 gser->out->driver_data = port;
1332 spin_lock_irqsave(&port->port_lock, flags);
1333 gser->ioport = port;
1334 port->port_usb = gser;
1339 gser->port_line_coding = port->port_line_coding;
1346 if (port->port.count) {
1347 pr_debug("gserial_connect: start ttyGS%d\n", port->port_num);
1348 gs_start_io(port);
1356 status = gs_console_connect(port);
1357 spin_unlock_irqrestore(&port->port_lock, flags);
1379 struct gs_port *port = gser->ioport;
1382 if (!port)
1388 spin_lock(&port->port_lock);
1390 gs_console_disconnect(port);
1393 port->port_line_coding = gser->port_line_coding;
1395 port->port_usb = NULL;
1397 if (port->port.count > 0) {
1398 wake_up_interruptible(&port->drain_wait);
1399 if (port->port.tty)
1400 tty_hangup(port->port.tty);
1402 port->suspended = false;
1403 spin_unlock(&port->port_lock);
1411 spin_lock_irqsave(&port->port_lock, flags);
1412 if (port->port.count == 0)
1413 kfifo_free(&port->port_write_buf);
1414 gs_free_requests(gser->out, &port->read_pool, NULL);
1415 gs_free_requests(gser->out, &port->read_queue, NULL);
1416 gs_free_requests(gser->in, &port->write_pool, NULL);
1418 port->read_allocated = port->read_started =
1419 port->write_allocated = port->write_started = 0;
1421 spin_unlock_irqrestore(&port->port_lock, flags);
1427 struct gs_port *port;
1431 port = gser->ioport;
1433 if (!port) {
1438 spin_lock(&port->port_lock);
1440 port->suspended = true;
1441 spin_unlock_irqrestore(&port->port_lock, flags);
1447 struct gs_port *port;
1451 port = gser->ioport;
1453 if (!port) {
1458 spin_lock(&port->port_lock);
1460 port->suspended = false;
1461 if (!port->start_delayed) {
1462 spin_unlock_irqrestore(&port->port_lock, flags);
1466 pr_debug("delayed start ttyGS%d\n", port->port_num);
1467 gs_start_io(port);
1470 port->start_delayed = false;
1471 spin_unlock_irqrestore(&port->port_lock, flags);