console.c 7.2 KB

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