console.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/tty.h>
  17. #include <linux/console.h>
  18. #include <linux/serial.h>
  19. #include <linux/usb.h>
  20. #include <linux/usb/serial.h>
  21. static int debug;
  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 *termios = NULL, dummy;
  63. dbg("%s", __func__);
  64. if (options) {
  65. baud = simple_strtoul(options, NULL, 10);
  66. s = options;
  67. while (*s >= '0' && *s <= '9')
  68. s++;
  69. if (*s)
  70. parity = *s++;
  71. if (*s)
  72. bits = *s++ - '0';
  73. if (*s)
  74. doflow = (*s++ == 'r');
  75. }
  76. /* Sane default */
  77. if (baud == 0)
  78. baud = 9600;
  79. switch (bits) {
  80. case 7:
  81. cflag |= CS7;
  82. break;
  83. default:
  84. case 8:
  85. cflag |= CS8;
  86. break;
  87. }
  88. switch (parity) {
  89. case 'o': case 'O':
  90. cflag |= PARODD;
  91. break;
  92. case 'e': case 'E':
  93. cflag |= PARENB;
  94. break;
  95. }
  96. co->cflag = cflag;
  97. /*
  98. * no need to check the index here: if the index is wrong, console
  99. * code won't call us
  100. */
  101. serial = usb_serial_get_by_index(co->index);
  102. if (serial == NULL) {
  103. /* no device is connected yet, sorry :( */
  104. err("No USB device connected to ttyUSB%i", co->index);
  105. return -ENODEV;
  106. }
  107. retval = usb_autopm_get_interface(serial->interface);
  108. if (retval)
  109. goto error_get_interface;
  110. port = serial->port[co->index - serial->minor];
  111. tty_port_tty_set(&port->port, NULL);
  112. info->port = port;
  113. ++port->port.count;
  114. if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags)) {
  115. if (serial->type->set_termios) {
  116. /*
  117. * allocate a fake tty so the driver can initialize
  118. * the termios structure, then later call set_termios to
  119. * configure according to command line arguments
  120. */
  121. tty = kzalloc(sizeof(*tty), GFP_KERNEL);
  122. if (!tty) {
  123. retval = -ENOMEM;
  124. err("no more memory");
  125. goto reset_open_count;
  126. }
  127. kref_init(&tty->kref);
  128. termios = kzalloc(sizeof(*termios), GFP_KERNEL);
  129. if (!termios) {
  130. retval = -ENOMEM;
  131. err("no more memory");
  132. goto free_tty;
  133. }
  134. memset(&dummy, 0, sizeof(struct ktermios));
  135. tty->termios = termios;
  136. tty_port_tty_set(&port->port, tty);
  137. }
  138. /* only call the device specific open if this
  139. * is the first time the port is opened */
  140. if (serial->type->open)
  141. retval = serial->type->open(NULL, port);
  142. else
  143. retval = usb_serial_generic_open(NULL, port);
  144. if (retval) {
  145. err("could not open USB console port");
  146. goto free_termios;
  147. }
  148. if (serial->type->set_termios) {
  149. termios->c_cflag = cflag;
  150. tty_termios_encode_baud_rate(termios, baud, baud);
  151. serial->type->set_termios(tty, port, &dummy);
  152. tty_port_tty_set(&port->port, NULL);
  153. kfree(termios);
  154. kfree(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->console = 1;
  164. mutex_unlock(&serial->disc_mutex);
  165. return retval;
  166. free_termios:
  167. kfree(termios);
  168. tty_port_tty_set(&port->port, NULL);
  169. free_tty:
  170. kfree(tty);
  171. reset_open_count:
  172. port->port.count = 0;
  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. dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
  192. if (!port->console) {
  193. dbg("%s - port not opened", __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. if (serial->type->write)
  210. retval = serial->type->write(NULL, port, buf, i);
  211. else
  212. retval = usb_serial_generic_write(NULL, port, buf, i);
  213. dbg("%s - return value : %d", __func__, retval);
  214. if (lf) {
  215. /* append CR after LF */
  216. unsigned char cr = 13;
  217. if (serial->type->write)
  218. retval = serial->type->write(NULL,
  219. port, &cr, 1);
  220. else
  221. retval = usb_serial_generic_write(NULL,
  222. port, &cr, 1);
  223. dbg("%s - return value : %d", __func__, retval);
  224. }
  225. buf += i;
  226. count -= i;
  227. }
  228. }
  229. static struct tty_driver *usb_console_device(struct console *co, int *index)
  230. {
  231. struct tty_driver **p = (struct tty_driver **)co->data;
  232. if (!*p)
  233. return NULL;
  234. *index = co->index;
  235. return *p;
  236. }
  237. static struct console usbcons = {
  238. .name = "ttyUSB",
  239. .write = usb_console_write,
  240. .device = usb_console_device,
  241. .setup = usb_console_setup,
  242. .flags = CON_PRINTBUFFER,
  243. .index = -1,
  244. .data = &usb_serial_tty_driver,
  245. };
  246. void usb_serial_console_disconnect(struct usb_serial *serial)
  247. {
  248. if (serial && serial->port && serial->port[0]
  249. && serial->port[0] == usbcons_info.port) {
  250. usb_serial_console_exit();
  251. usb_serial_put(serial);
  252. }
  253. }
  254. void usb_serial_console_init(int serial_debug, int minor)
  255. {
  256. debug = serial_debug;
  257. if (minor == 0) {
  258. /*
  259. * Call register_console() if this is the first device plugged
  260. * in. If we call it earlier, then the callback to
  261. * console_setup() will fail, as there is not a device seen by
  262. * the USB subsystem yet.
  263. */
  264. /*
  265. * Register console.
  266. * NOTES:
  267. * console_setup() is called (back) immediately (from
  268. * register_console). console_write() is called immediately
  269. * from register_console iff CON_PRINTBUFFER is set in flags.
  270. */
  271. dbg("registering the USB serial console.");
  272. register_console(&usbcons);
  273. }
  274. }
  275. void usb_serial_console_exit(void)
  276. {
  277. if (usbcons_info.port) {
  278. unregister_console(&usbcons);
  279. usbcons_info.port->console = 0;
  280. usbcons_info.port = NULL;
  281. }
  282. }