^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * USB Serial Console driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2001 - 2002 Greg Kroah-Hartman (greg@kroah.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Thanks to Randy Dunlap for the original version of this code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/console.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/usb/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct usbcons_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int break_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct usb_serial_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static struct usbcons_info usbcons_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static struct console usbcons;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * ------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * USB Serial console driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * Much of the code here is copied from drivers/char/serial.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * and implements a phony serial console in the same way that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * serial.c does so that in case some software queries it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * it will get the same results.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * Things that are different from the way the serial port code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * does things, is that we call the lower level usb-serial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * driver code to initialize the device, and we set the initial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * console speeds based on the command line arguments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * ------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static const struct tty_operations usb_console_fake_tty_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * The parsing of the command line works exactly like the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * serial.c code, except that the specifier is "ttyUSB" instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * of "ttyS".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int usb_console_setup(struct console *co, char *options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct usbcons_info *info = &usbcons_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int baud = 9600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int bits = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int parity = 'n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int doflow = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int cflag = CREAD | HUPCL | CLOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) char *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct usb_serial *serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct usb_serial_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct tty_struct *tty = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct ktermios dummy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (options) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) baud = simple_strtoul(options, NULL, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) s = options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) while (*s >= '0' && *s <= '9')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (*s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) parity = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (*s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) bits = *s++ - '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (*s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) doflow = (*s++ == 'r');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* Sane default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (baud == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) baud = 9600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) switch (bits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) case 7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) cflag |= CS7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) cflag |= CS8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) switch (parity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) case 'o': case 'O':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) cflag |= PARODD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) case 'e': case 'E':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) cflag |= PARENB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (doflow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) cflag |= CRTSCTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * no need to check the index here: if the index is wrong, console
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * code won't call us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) port = usb_serial_port_get_by_minor(co->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (port == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* no device is connected yet, sorry :( */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) pr_err("No USB device connected to ttyUSB%i\n", co->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) retval = usb_autopm_get_interface(serial->interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto error_get_interface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) tty_port_tty_set(&port->port, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) info->port = port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) ++port->port.count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!tty_port_initialized(&port->port)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (serial->type->set_termios) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * allocate a fake tty so the driver can initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * the termios structure, then later call set_termios to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * configure according to command line arguments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) tty = kzalloc(sizeof(*tty), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!tty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) goto reset_open_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) kref_init(&tty->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) tty->driver = usb_serial_tty_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) tty->index = co->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) init_ldsem(&tty->ldisc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) spin_lock_init(&tty->files_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) INIT_LIST_HEAD(&tty->tty_files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) kref_get(&tty->driver->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) __module_get(tty->driver->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) tty->ops = &usb_console_fake_tty_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) tty_init_termios(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) tty_port_tty_set(&port->port, tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* only call the device specific open if this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * is the first time the port is opened */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) retval = serial->type->open(NULL, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) dev_err(&port->dev, "could not open USB console port\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (serial->type->set_termios) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) tty->termios.c_cflag = cflag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) tty_termios_encode_baud_rate(&tty->termios, baud, baud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) memset(&dummy, 0, sizeof(struct ktermios));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) serial->type->set_termios(tty, port, &dummy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) tty_port_tty_set(&port->port, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) tty_save_termios(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) tty_kref_put(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) tty_port_set_initialized(&port->port, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* Now that any required fake tty operations are completed restore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * the tty port count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) --port->port.count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* The console is special in terms of closing the device so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * indicate this port is now acting as a system console. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) port->port.console = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) mutex_unlock(&serial->disc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) tty_port_tty_set(&port->port, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) tty_kref_put(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) reset_open_count:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) port->port.count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) info->port = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) usb_autopm_put_interface(serial->interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) error_get_interface:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) usb_serial_put(serial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) mutex_unlock(&serial->disc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static void usb_console_write(struct console *co,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) const char *buf, unsigned count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static struct usbcons_info *info = &usbcons_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct usb_serial_port *port = info->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct usb_serial *serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) dev_dbg(&port->dev, "%s - %d byte(s)\n", __func__, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (!port->port.console) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) dev_dbg(&port->dev, "%s - port not opened\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) while (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) unsigned int lf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /* search for LF so we can insert CR if necessary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) for (i = 0, lf = 0 ; i < count ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (*(buf + i) == 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) lf = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /* pass on to the driver specific version of this function if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) it is available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) retval = serial->type->write(NULL, port, buf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) dev_dbg(&port->dev, "%s - write: %d\n", __func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (lf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* append CR after LF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) unsigned char cr = 13;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) retval = serial->type->write(NULL, port, &cr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) dev_dbg(&port->dev, "%s - write cr: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) __func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) buf += i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) count -= i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static struct tty_driver *usb_console_device(struct console *co, int *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct tty_driver **p = (struct tty_driver **)co->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (!*p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) *index = co->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static struct console usbcons = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) .name = "ttyUSB",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) .write = usb_console_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) .device = usb_console_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) .setup = usb_console_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) .flags = CON_PRINTBUFFER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) .index = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) .data = &usb_serial_tty_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) void usb_serial_console_disconnect(struct usb_serial *serial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (serial->port[0] && serial->port[0] == usbcons_info.port) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) usb_serial_console_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) usb_serial_put(serial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) void usb_serial_console_init(int minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (minor == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * Call register_console() if this is the first device plugged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * in. If we call it earlier, then the callback to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * console_setup() will fail, as there is not a device seen by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * the USB subsystem yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * Register console.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * NOTES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * console_setup() is called (back) immediately (from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * register_console). console_write() is called immediately
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * from register_console iff CON_PRINTBUFFER is set in flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) pr_debug("registering the USB serial console.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) register_console(&usbcons);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) void usb_serial_console_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (usbcons_info.port) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) unregister_console(&usbcons);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) usbcons_info.port->port.console = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) usbcons_info.port = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)