suncore.c 5.1 KB

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