console.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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/init.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. /*
  45. * The parsing of the command line works exactly like the
  46. * serial.c code, except that the specifier is "ttyUSB" instead
  47. * of "ttyS".
  48. */
  49. static int usb_console_setup(struct console *co, char *options)
  50. {
  51. struct usbcons_info *info = &usbcons_info;
  52. int baud = 9600;
  53. int bits = 8;
  54. int parity = 'n';
  55. int doflow = 0;
  56. int cflag = CREAD | HUPCL | CLOCAL;
  57. char *s;
  58. struct usb_serial *serial;
  59. struct usb_serial_port *port;
  60. int retval;
  61. struct tty_struct *tty = NULL;
  62. struct ktermios dummy;
  63. if (options) {
  64. baud = simple_strtoul(options, NULL, 10);
  65. s = options;
  66. while (*s >= '0' && *s <= '9')
  67. s++;
  68. if (*s)
  69. parity = *s++;
  70. if (*s)
  71. bits = *s++ - '0';
  72. if (*s)
  73. doflow = (*s++ == 'r');
  74. }
  75. /* Sane default */
  76. if (baud == 0)
  77. baud = 9600;
  78. switch (bits) {
  79. case 7:
  80. cflag |= CS7;
  81. break;
  82. default:
  83. case 8:
  84. cflag |= CS8;
  85. break;
  86. }
  87. switch (parity) {
  88. case 'o': case 'O':
  89. cflag |= PARODD;
  90. break;
  91. case 'e': case 'E':
  92. cflag |= PARENB;
  93. break;
  94. }
  95. co->cflag = cflag;
  96. /*
  97. * no need to check the index here: if the index is wrong, console
  98. * code won't call us
  99. */
  100. serial = usb_serial_get_by_index(co->index);
  101. if (serial == 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. retval = usb_autopm_get_interface(serial->interface);
  107. if (retval)
  108. goto error_get_interface;
  109. port = serial->port[co->index - serial->minor];
  110. tty_port_tty_set(&port->port, NULL);
  111. info->port = port;
  112. ++port->port.count;
  113. if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags)) {
  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. dev_err(&port->dev, "no more memory\n");
  124. goto reset_open_count;
  125. }
  126. kref_init(&tty->kref);
  127. tty_port_tty_set(&port->port, tty);
  128. tty->driver = usb_serial_tty_driver;
  129. tty->index = co->index;
  130. if (tty_init_termios(tty)) {
  131. retval = -ENOMEM;
  132. dev_err(&port->dev, "no more memory\n");
  133. goto free_tty;
  134. }
  135. }
  136. /* only call the device specific open if this
  137. * is the first time the port is opened */
  138. if (serial->type->open)
  139. retval = serial->type->open(NULL, port);
  140. else
  141. retval = usb_serial_generic_open(NULL, port);
  142. if (retval) {
  143. dev_err(&port->dev, "could not open USB console port\n");
  144. goto fail;
  145. }
  146. if (serial->type->set_termios) {
  147. tty->termios.c_cflag = cflag;
  148. tty_termios_encode_baud_rate(&tty->termios, baud, baud);
  149. memset(&dummy, 0, sizeof(struct ktermios));
  150. serial->type->set_termios(tty, port, &dummy);
  151. tty_port_tty_set(&port->port, NULL);
  152. kfree(tty);
  153. }
  154. set_bit(ASYNCB_INITIALIZED, &port->port.flags);
  155. }
  156. /* Now that any required fake tty operations are completed restore
  157. * the tty port count */
  158. --port->port.count;
  159. /* The console is special in terms of closing the device so
  160. * indicate this port is now acting as a system console. */
  161. port->port.console = 1;
  162. mutex_unlock(&serial->disc_mutex);
  163. return retval;
  164. fail:
  165. tty_port_tty_set(&port->port, NULL);
  166. free_tty:
  167. kfree(tty);
  168. reset_open_count:
  169. port->port.count = 0;
  170. usb_autopm_put_interface(serial->interface);
  171. error_get_interface:
  172. usb_serial_put(serial);
  173. mutex_unlock(&serial->disc_mutex);
  174. return retval;
  175. }
  176. static void usb_console_write(struct console *co,
  177. const char *buf, unsigned count)
  178. {
  179. static struct usbcons_info *info = &usbcons_info;
  180. struct usb_serial_port *port = info->port;
  181. struct usb_serial *serial;
  182. int retval = -ENODEV;
  183. if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
  184. return;
  185. serial = port->serial;
  186. if (count == 0)
  187. return;
  188. pr_debug("%s - port %d, %d byte(s)\n", __func__, port->number, count);
  189. if (!port->port.console) {
  190. pr_debug("%s - port not opened\n", __func__);
  191. return;
  192. }
  193. while (count) {
  194. unsigned int i;
  195. unsigned int lf;
  196. /* search for LF so we can insert CR if necessary */
  197. for (i = 0, lf = 0 ; i < count ; i++) {
  198. if (*(buf + i) == 10) {
  199. lf = 1;
  200. i++;
  201. break;
  202. }
  203. }
  204. /* pass on to the driver specific version of this function if
  205. it is available */
  206. if (serial->type->write)
  207. retval = serial->type->write(NULL, port, buf, i);
  208. else
  209. retval = usb_serial_generic_write(NULL, port, buf, i);
  210. pr_debug("%s - return value : %d\n", __func__, retval);
  211. if (lf) {
  212. /* append CR after LF */
  213. unsigned char cr = 13;
  214. if (serial->type->write)
  215. retval = serial->type->write(NULL,
  216. port, &cr, 1);
  217. else
  218. retval = usb_serial_generic_write(NULL,
  219. port, &cr, 1);
  220. pr_debug("%s - return value : %d\n", __func__, retval);
  221. }
  222. buf += i;
  223. count -= i;
  224. }
  225. }
  226. static struct tty_driver *usb_console_device(struct console *co, int *index)
  227. {
  228. struct tty_driver **p = (struct tty_driver **)co->data;
  229. if (!*p)
  230. return NULL;
  231. *index = co->index;
  232. return *p;
  233. }
  234. static struct console usbcons = {
  235. .name = "ttyUSB",
  236. .write = usb_console_write,
  237. .device = usb_console_device,
  238. .setup = usb_console_setup,
  239. .flags = CON_PRINTBUFFER,
  240. .index = -1,
  241. .data = &usb_serial_tty_driver,
  242. };
  243. void usb_serial_console_disconnect(struct usb_serial *serial)
  244. {
  245. if (serial && serial->port && serial->port[0]
  246. && serial->port[0] == usbcons_info.port) {
  247. usb_serial_console_exit();
  248. usb_serial_put(serial);
  249. }
  250. }
  251. void usb_serial_console_init(int minor)
  252. {
  253. if (minor == 0) {
  254. /*
  255. * Call register_console() if this is the first device plugged
  256. * in. If we call it earlier, then the callback to
  257. * console_setup() will fail, as there is not a device seen by
  258. * the USB subsystem yet.
  259. */
  260. /*
  261. * Register console.
  262. * NOTES:
  263. * console_setup() is called (back) immediately (from
  264. * register_console). console_write() is called immediately
  265. * from register_console iff CON_PRINTBUFFER is set in flags.
  266. */
  267. pr_debug("registering the USB serial console.\n");
  268. register_console(&usbcons);
  269. }
  270. }
  271. void usb_serial_console_exit(void)
  272. {
  273. if (usbcons_info.port) {
  274. unregister_console(&usbcons);
  275. usbcons_info.port->port.console = 0;
  276. usbcons_info.port = NULL;
  277. }
  278. }