console.c 7.6 KB

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