console.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/usb.h>
  19. #include <linux/usb/serial.h>
  20. static int debug;
  21. struct usbcons_info {
  22. int magic;
  23. int break_flag;
  24. struct usb_serial_port *port;
  25. };
  26. static struct usbcons_info usbcons_info;
  27. static struct console usbcons;
  28. /*
  29. * ------------------------------------------------------------
  30. * USB Serial console driver
  31. *
  32. * Much of the code here is copied from drivers/char/serial.c
  33. * and implements a phony serial console in the same way that
  34. * serial.c does so that in case some software queries it,
  35. * it will get the same results.
  36. *
  37. * Things that are different from the way the serial port code
  38. * does things, is that we call the lower level usb-serial
  39. * driver code to initialize the device, and we set the initial
  40. * console speeds based on the command line arguments.
  41. * ------------------------------------------------------------
  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 = 0;
  60. struct tty_struct *tty = NULL;
  61. struct ktermios *termios = NULL, dummy;
  62. dbg("%s", __func__);
  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. err("No USB device connected to ttyUSB%i", co->index);
  104. return -ENODEV;
  105. }
  106. port = serial->port[0];
  107. tty_port_tty_set(&port->port, NULL);
  108. info->port = port;
  109. ++port->port.count;
  110. if (port->port.count == 1) {
  111. if (serial->type->set_termios) {
  112. /*
  113. * allocate a fake tty so the driver can initialize
  114. * the termios structure, then later call set_termios to
  115. * configure according to command line arguments
  116. */
  117. tty = kzalloc(sizeof(*tty), GFP_KERNEL);
  118. if (!tty) {
  119. retval = -ENOMEM;
  120. err("no more memory");
  121. goto reset_open_count;
  122. }
  123. kref_init(&tty->kref);
  124. termios = kzalloc(sizeof(*termios), GFP_KERNEL);
  125. if (!termios) {
  126. retval = -ENOMEM;
  127. err("no more memory");
  128. goto free_tty;
  129. }
  130. memset(&dummy, 0, sizeof(struct ktermios));
  131. tty->termios = termios;
  132. tty_port_tty_set(&port->port, tty);
  133. }
  134. /* only call the device specific open if this
  135. * is the first time the port is opened */
  136. if (serial->type->open)
  137. retval = serial->type->open(NULL, port, NULL);
  138. else
  139. retval = usb_serial_generic_open(NULL, port, NULL);
  140. if (retval) {
  141. err("could not open USB console port");
  142. goto free_termios;
  143. }
  144. if (serial->type->set_termios) {
  145. termios->c_cflag = cflag;
  146. tty_termios_encode_baud_rate(termios, baud, baud);
  147. serial->type->set_termios(tty, port, &dummy);
  148. tty_port_tty_set(&port->port, NULL);
  149. kfree(termios);
  150. kfree(tty);
  151. }
  152. }
  153. /* Now that any required fake tty operations are completed restore
  154. * the tty port count */
  155. --port->port.count;
  156. /* The console is special in terms of closing the device so
  157. * indicate this port is now acting as a system console. */
  158. port->console = 1;
  159. retval = 0;
  160. out:
  161. return retval;
  162. free_termios:
  163. kfree(termios);
  164. tty_port_tty_set(&port->port, NULL);
  165. free_tty:
  166. kfree(tty);
  167. reset_open_count:
  168. port->port.count = 0;
  169. goto out;
  170. }
  171. static void usb_console_write(struct console *co,
  172. const char *buf, unsigned count)
  173. {
  174. static struct usbcons_info *info = &usbcons_info;
  175. struct usb_serial_port *port = info->port;
  176. struct usb_serial *serial;
  177. int retval = -ENODEV;
  178. if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
  179. return;
  180. serial = port->serial;
  181. if (count == 0)
  182. return;
  183. dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
  184. if (!port->console) {
  185. dbg("%s - port not opened", __func__);
  186. return;
  187. }
  188. while (count) {
  189. unsigned int i;
  190. unsigned int lf;
  191. /* search for LF so we can insert CR if necessary */
  192. for (i = 0, lf = 0 ; i < count ; i++) {
  193. if (*(buf + i) == 10) {
  194. lf = 1;
  195. i++;
  196. break;
  197. }
  198. }
  199. /* pass on to the driver specific version of this function if
  200. it is available */
  201. if (serial->type->write)
  202. retval = serial->type->write(NULL, port, buf, i);
  203. else
  204. retval = usb_serial_generic_write(NULL, port, buf, i);
  205. dbg("%s - return value : %d", __func__, retval);
  206. if (lf) {
  207. /* append CR after LF */
  208. unsigned char cr = 13;
  209. if (serial->type->write)
  210. retval = serial->type->write(NULL,
  211. port, &cr, 1);
  212. else
  213. retval = usb_serial_generic_write(NULL,
  214. port, &cr, 1);
  215. dbg("%s - return value : %d", __func__, retval);
  216. }
  217. buf += i;
  218. count -= i;
  219. }
  220. }
  221. static struct tty_driver *usb_console_device(struct console *co, int *index)
  222. {
  223. struct tty_driver **p = (struct tty_driver **)co->data;
  224. if (!*p)
  225. return NULL;
  226. *index = co->index;
  227. return *p;
  228. }
  229. static struct console usbcons = {
  230. .name = "ttyUSB",
  231. .write = usb_console_write,
  232. .device = usb_console_device,
  233. .setup = usb_console_setup,
  234. .flags = CON_PRINTBUFFER,
  235. .index = -1,
  236. .data = &usb_serial_tty_driver,
  237. };
  238. void usb_serial_console_disconnect(struct usb_serial *serial)
  239. {
  240. if (serial && serial->port && serial->port[0]
  241. && serial->port[0] == usbcons_info.port) {
  242. usb_serial_console_exit();
  243. usb_serial_put(serial);
  244. }
  245. }
  246. void usb_serial_console_init(int serial_debug, int minor)
  247. {
  248. debug = serial_debug;
  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. dbg("registering the USB serial console.");
  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->console = 0;
  272. usbcons_info.port = NULL;
  273. }
  274. }