console.c 6.9 KB

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