console.c 7.3 KB

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