console.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/config.h>
  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/usb.h>
  20. static int debug;
  21. #include "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 __init 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 = 0;
  61. struct tty_struct *tty;
  62. struct termios *termios;
  63. dbg ("%s", __FUNCTION__);
  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. /* build a cflag setting */
  77. switch (baud) {
  78. case 1200:
  79. cflag |= B1200;
  80. break;
  81. case 2400:
  82. cflag |= B2400;
  83. break;
  84. case 4800:
  85. cflag |= B4800;
  86. break;
  87. case 19200:
  88. cflag |= B19200;
  89. break;
  90. case 38400:
  91. cflag |= B38400;
  92. break;
  93. case 57600:
  94. cflag |= B57600;
  95. break;
  96. case 115200:
  97. cflag |= B115200;
  98. break;
  99. case 9600:
  100. default:
  101. cflag |= B9600;
  102. /*
  103. * Set this to a sane value to prevent a divide error
  104. */
  105. baud = 9600;
  106. break;
  107. }
  108. switch (bits) {
  109. case 7:
  110. cflag |= CS7;
  111. break;
  112. default:
  113. case 8:
  114. cflag |= CS8;
  115. break;
  116. }
  117. switch (parity) {
  118. case 'o': case 'O':
  119. cflag |= PARODD;
  120. break;
  121. case 'e': case 'E':
  122. cflag |= PARENB;
  123. break;
  124. }
  125. co->cflag = cflag;
  126. /* grab the first serial port that happens to be connected */
  127. serial = usb_serial_get_by_index(0);
  128. if (serial == NULL) {
  129. /* no device is connected yet, sorry :( */
  130. err ("No USB device connected to ttyUSB0");
  131. return -ENODEV;
  132. }
  133. port = serial->port[0];
  134. port->tty = NULL;
  135. info->port = port;
  136. ++port->open_count;
  137. if (port->open_count == 1) {
  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(port, NULL);
  142. else
  143. retval = usb_serial_generic_open(port, NULL);
  144. if (retval)
  145. port->open_count = 0;
  146. }
  147. if (retval) {
  148. err ("could not open USB console port");
  149. return retval;
  150. }
  151. if (serial->type->set_termios) {
  152. /* build up a fake tty structure so that the open call has something
  153. * to look at to get the cflag value */
  154. tty = kmalloc (sizeof (*tty), GFP_KERNEL);
  155. if (!tty) {
  156. err ("no more memory");
  157. return -ENOMEM;
  158. }
  159. termios = kmalloc (sizeof (*termios), GFP_KERNEL);
  160. if (!termios) {
  161. err ("no more memory");
  162. kfree (tty);
  163. return -ENOMEM;
  164. }
  165. memset (tty, 0x00, sizeof(*tty));
  166. memset (termios, 0x00, sizeof(*termios));
  167. termios->c_cflag = cflag;
  168. tty->termios = termios;
  169. port->tty = tty;
  170. /* set up the initial termios settings */
  171. serial->type->set_termios(port, NULL);
  172. port->tty = NULL;
  173. kfree (termios);
  174. kfree (tty);
  175. }
  176. return retval;
  177. }
  178. static void usb_console_write(struct console *co, const char *buf, unsigned count)
  179. {
  180. static struct usbcons_info *info = &usbcons_info;
  181. struct usb_serial_port *port = info->port;
  182. struct usb_serial *serial;
  183. int retval = -ENODEV;
  184. if (!port)
  185. return;
  186. serial = port->serial;
  187. if (count == 0)
  188. return;
  189. dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
  190. if (!port->open_count) {
  191. dbg ("%s - port not opened", __FUNCTION__);
  192. goto exit;
  193. }
  194. /* pass on to the driver specific version of this function if it is available */
  195. if (serial->type->write)
  196. retval = serial->type->write(port, buf, count);
  197. else
  198. retval = usb_serial_generic_write(port, buf, count);
  199. exit:
  200. dbg("%s - return value (if we had one): %d", __FUNCTION__, retval);
  201. }
  202. static struct console usbcons = {
  203. .name = "ttyUSB",
  204. .write = usb_console_write,
  205. .setup = usb_console_setup,
  206. .flags = CON_PRINTBUFFER,
  207. .index = -1,
  208. };
  209. void usb_serial_console_init (int serial_debug, int minor)
  210. {
  211. debug = serial_debug;
  212. if (minor == 0) {
  213. /*
  214. * Call register_console() if this is the first device plugged
  215. * in. If we call it earlier, then the callback to
  216. * console_setup() will fail, as there is not a device seen by
  217. * the USB subsystem yet.
  218. */
  219. /*
  220. * Register console.
  221. * NOTES:
  222. * console_setup() is called (back) immediately (from register_console).
  223. * console_write() is called immediately from register_console iff
  224. * CON_PRINTBUFFER is set in flags.
  225. */
  226. dbg ("registering the USB serial console.");
  227. register_console(&usbcons);
  228. }
  229. }
  230. void usb_serial_console_exit (void)
  231. {
  232. unregister_console(&usbcons);
  233. }