suncore.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/init.h>
  20. #include <asm/prom.h>
  21. #include "suncore.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 || of_console_device != dp)
  50. return 0;
  51. if (!ignore_line) {
  52. int off = 0;
  53. if (of_console_options &&
  54. *of_console_options == 'b')
  55. off = 1;
  56. if ((line & 1) != off)
  57. return 0;
  58. }
  59. con->index = line;
  60. drv->cons = con;
  61. if (!console_set_on_cmdline)
  62. add_preferred_console(con->name, line, NULL);
  63. return 1;
  64. }
  65. EXPORT_SYMBOL(sunserial_console_match);
  66. void sunserial_console_termios(struct console *con, struct device_node *uart_dp)
  67. {
  68. const char *mode, *s;
  69. char mode_prop[] = "ttyX-mode";
  70. int baud, bits, stop, cflag;
  71. char parity;
  72. if (!strcmp(uart_dp->name, "rsc") ||
  73. !strcmp(uart_dp->name, "rsc-console") ||
  74. !strcmp(uart_dp->name, "rsc-control")) {
  75. mode = of_get_property(uart_dp,
  76. "ssp-console-modes", NULL);
  77. if (!mode)
  78. mode = "115200,8,n,1,-";
  79. } else if (!strcmp(uart_dp->name, "lom-console")) {
  80. mode = "9600,8,n,1,-";
  81. } else {
  82. struct device_node *dp;
  83. char c;
  84. c = 'a';
  85. if (of_console_options)
  86. c = *of_console_options;
  87. mode_prop[3] = c;
  88. dp = of_find_node_by_path("/options");
  89. mode = of_get_property(dp, mode_prop, NULL);
  90. if (!mode)
  91. mode = "9600,8,n,1,-";
  92. }
  93. cflag = CREAD | HUPCL | CLOCAL;
  94. s = mode;
  95. baud = simple_strtoul(s, NULL, 0);
  96. s = strchr(s, ',');
  97. bits = simple_strtoul(++s, NULL, 0);
  98. s = strchr(s, ',');
  99. parity = *(++s);
  100. s = strchr(s, ',');
  101. stop = simple_strtoul(++s, NULL, 0);
  102. s = strchr(s, ',');
  103. /* XXX handshake is not handled here. */
  104. switch (baud) {
  105. case 150: cflag |= B150; break;
  106. case 300: cflag |= B300; break;
  107. case 600: cflag |= B600; break;
  108. case 1200: cflag |= B1200; break;
  109. case 2400: cflag |= B2400; break;
  110. case 4800: cflag |= B4800; break;
  111. case 9600: cflag |= B9600; break;
  112. case 19200: cflag |= B19200; break;
  113. case 38400: cflag |= B38400; break;
  114. case 57600: cflag |= B57600; break;
  115. case 115200: cflag |= B115200; break;
  116. case 230400: cflag |= B230400; break;
  117. case 460800: cflag |= B460800; break;
  118. default: baud = 9600; cflag |= B9600; break;
  119. }
  120. switch (bits) {
  121. case 5: cflag |= CS5; break;
  122. case 6: cflag |= CS6; break;
  123. case 7: cflag |= CS7; break;
  124. case 8: cflag |= CS8; break;
  125. default: cflag |= CS8; break;
  126. }
  127. switch (parity) {
  128. case 'o': cflag |= (PARENB | PARODD); break;
  129. case 'e': cflag |= PARENB; break;
  130. case 'n': default: break;
  131. }
  132. switch (stop) {
  133. case 2: cflag |= CSTOPB; break;
  134. case 1: default: break;
  135. }
  136. con->cflag = cflag;
  137. }
  138. /* Sun serial MOUSE auto baud rate detection. */
  139. static struct mouse_baud_cflag {
  140. int baud;
  141. unsigned int cflag;
  142. } mouse_baud_table[] = {
  143. { 1200, B1200 },
  144. { 2400, B2400 },
  145. { 4800, B4800 },
  146. { 9600, B9600 },
  147. { -1, ~0 },
  148. { -1, ~0 },
  149. };
  150. unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
  151. {
  152. int i;
  153. for (i = 0; mouse_baud_table[i].baud != -1; i++)
  154. if (mouse_baud_table[i].cflag == (cflag & CBAUD))
  155. break;
  156. i += 1;
  157. if (mouse_baud_table[i].baud == -1)
  158. i = 0;
  159. *new_baud = mouse_baud_table[i].baud;
  160. return mouse_baud_table[i].cflag;
  161. }
  162. EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
  163. /* Basically, when the baud rate is wrong the mouse spits out
  164. * breaks to us.
  165. */
  166. int suncore_mouse_baud_detection(unsigned char ch, int is_break)
  167. {
  168. static int mouse_got_break = 0;
  169. static int ctr = 0;
  170. if (is_break) {
  171. /* Let a few normal bytes go by before we jump the gun
  172. * and say we need to try another baud rate.
  173. */
  174. if (mouse_got_break && ctr < 8)
  175. return 1;
  176. /* Ok, we need to try another baud. */
  177. ctr = 0;
  178. mouse_got_break = 1;
  179. return 2;
  180. }
  181. if (mouse_got_break) {
  182. ctr++;
  183. if (ch == 0x87) {
  184. /* Correct baud rate determined. */
  185. mouse_got_break = 0;
  186. }
  187. return 1;
  188. }
  189. return 0;
  190. }
  191. EXPORT_SYMBOL(suncore_mouse_baud_detection);
  192. static int __init suncore_init(void)
  193. {
  194. return 0;
  195. }
  196. static void __exit suncore_exit(void)
  197. {
  198. }
  199. module_init(suncore_init);
  200. module_exit(suncore_exit);
  201. MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
  202. MODULE_DESCRIPTION("Sun serial common layer");
  203. MODULE_LICENSE("GPL");