suncore.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* suncore.c
  2. *
  3. * Common SUN serial routines. Based entirely
  4. * upon drivers/sbus/char/sunserial.c which is:
  5. *
  6. * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
  7. *
  8. * Adaptation to new UART layer is:
  9. *
  10. * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/console.h>
  15. #include <linux/tty.h>
  16. #include <linux/errno.h>
  17. #include <linux/string.h>
  18. #include <linux/serial_core.h>
  19. #include <linux/sunserialcore.h>
  20. #include <linux/init.h>
  21. #include <asm/prom.h>
  22. static int sunserial_current_minor = 64;
  23. int sunserial_register_minors(struct uart_driver *drv, int count)
  24. {
  25. int err = 0;
  26. drv->minor = sunserial_current_minor;
  27. drv->nr += count;
  28. /* Register the driver on the first call */
  29. if (drv->nr == count)
  30. err = uart_register_driver(drv);
  31. if (err == 0) {
  32. sunserial_current_minor += count;
  33. drv->tty_driver->name_base = drv->minor - 64;
  34. }
  35. return err;
  36. }
  37. EXPORT_SYMBOL(sunserial_register_minors);
  38. void sunserial_unregister_minors(struct uart_driver *drv, int count)
  39. {
  40. drv->nr -= count;
  41. sunserial_current_minor -= count;
  42. if (drv->nr == 0)
  43. uart_unregister_driver(drv);
  44. }
  45. EXPORT_SYMBOL(sunserial_unregister_minors);
  46. int sunserial_console_match(struct console *con, struct device_node *dp,
  47. struct uart_driver *drv, int line, bool ignore_line)
  48. {
  49. if (!con)
  50. return 0;
  51. drv->cons = con;
  52. if (of_console_device != dp)
  53. return 0;
  54. if (!ignore_line) {
  55. int off = 0;
  56. if (of_console_options &&
  57. *of_console_options == 'b')
  58. off = 1;
  59. if ((line & 1) != off)
  60. return 0;
  61. }
  62. if (!console_set_on_cmdline) {
  63. con->index = line;
  64. add_preferred_console(con->name, line, NULL);
  65. }
  66. return 1;
  67. }
  68. EXPORT_SYMBOL(sunserial_console_match);
  69. void sunserial_console_termios(struct console *con, struct device_node *uart_dp)
  70. {
  71. const char *mode, *s;
  72. char mode_prop[] = "ttyX-mode";
  73. int baud, bits, stop, cflag;
  74. char parity;
  75. if (!strcmp(uart_dp->name, "rsc") ||
  76. !strcmp(uart_dp->name, "rsc-console") ||
  77. !strcmp(uart_dp->name, "rsc-control")) {
  78. mode = of_get_property(uart_dp,
  79. "ssp-console-modes", NULL);
  80. if (!mode)
  81. mode = "115200,8,n,1,-";
  82. } else if (!strcmp(uart_dp->name, "lom-console")) {
  83. mode = "9600,8,n,1,-";
  84. } else {
  85. struct device_node *dp;
  86. char c;
  87. c = 'a';
  88. if (of_console_options)
  89. c = *of_console_options;
  90. mode_prop[3] = c;
  91. dp = of_find_node_by_path("/options");
  92. mode = of_get_property(dp, mode_prop, NULL);
  93. if (!mode)
  94. mode = "9600,8,n,1,-";
  95. }
  96. cflag = CREAD | HUPCL | CLOCAL;
  97. s = mode;
  98. baud = simple_strtoul(s, NULL, 0);
  99. s = strchr(s, ',');
  100. bits = simple_strtoul(++s, NULL, 0);
  101. s = strchr(s, ',');
  102. parity = *(++s);
  103. s = strchr(s, ',');
  104. stop = simple_strtoul(++s, NULL, 0);
  105. s = strchr(s, ',');
  106. /* XXX handshake is not handled here. */
  107. switch (baud) {
  108. case 150: cflag |= B150; break;
  109. case 300: cflag |= B300; break;
  110. case 600: cflag |= B600; break;
  111. case 1200: cflag |= B1200; break;
  112. case 2400: cflag |= B2400; break;
  113. case 4800: cflag |= B4800; break;
  114. case 9600: cflag |= B9600; break;
  115. case 19200: cflag |= B19200; break;
  116. case 38400: cflag |= B38400; break;
  117. case 57600: cflag |= B57600; break;
  118. case 115200: cflag |= B115200; break;
  119. case 230400: cflag |= B230400; break;
  120. case 460800: cflag |= B460800; break;
  121. default: baud = 9600; cflag |= B9600; break;
  122. }
  123. switch (bits) {
  124. case 5: cflag |= CS5; break;
  125. case 6: cflag |= CS6; break;
  126. case 7: cflag |= CS7; break;
  127. case 8: cflag |= CS8; break;
  128. default: cflag |= CS8; break;
  129. }
  130. switch (parity) {
  131. case 'o': cflag |= (PARENB | PARODD); break;
  132. case 'e': cflag |= PARENB; break;
  133. case 'n': default: break;
  134. }
  135. switch (stop) {
  136. case 2: cflag |= CSTOPB; break;
  137. case 1: default: break;
  138. }
  139. con->cflag = cflag;
  140. }
  141. /* Sun serial MOUSE auto baud rate detection. */
  142. static struct mouse_baud_cflag {
  143. int baud;
  144. unsigned int cflag;
  145. } mouse_baud_table[] = {
  146. { 1200, B1200 },
  147. { 2400, B2400 },
  148. { 4800, B4800 },
  149. { 9600, B9600 },
  150. { -1, ~0 },
  151. { -1, ~0 },
  152. };
  153. unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
  154. {
  155. int i;
  156. for (i = 0; mouse_baud_table[i].baud != -1; i++)
  157. if (mouse_baud_table[i].cflag == (cflag & CBAUD))
  158. break;
  159. i += 1;
  160. if (mouse_baud_table[i].baud == -1)
  161. i = 0;
  162. *new_baud = mouse_baud_table[i].baud;
  163. return mouse_baud_table[i].cflag;
  164. }
  165. EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
  166. /* Basically, when the baud rate is wrong the mouse spits out
  167. * breaks to us.
  168. */
  169. int suncore_mouse_baud_detection(unsigned char ch, int is_break)
  170. {
  171. static int mouse_got_break = 0;
  172. static int ctr = 0;
  173. if (is_break) {
  174. /* Let a few normal bytes go by before we jump the gun
  175. * and say we need to try another baud rate.
  176. */
  177. if (mouse_got_break && ctr < 8)
  178. return 1;
  179. /* Ok, we need to try another baud. */
  180. ctr = 0;
  181. mouse_got_break = 1;
  182. return 2;
  183. }
  184. if (mouse_got_break) {
  185. ctr++;
  186. if (ch == 0x87) {
  187. /* Correct baud rate determined. */
  188. mouse_got_break = 0;
  189. }
  190. return 1;
  191. }
  192. return 0;
  193. }
  194. EXPORT_SYMBOL(suncore_mouse_baud_detection);
  195. static int __init suncore_init(void)
  196. {
  197. return 0;
  198. }
  199. static void __exit suncore_exit(void)
  200. {
  201. }
  202. module_init(suncore_init);
  203. module_exit(suncore_exit);
  204. MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
  205. MODULE_DESCRIPTION("Sun serial common layer");
  206. MODULE_LICENSE("GPL");