suncore.c 4.4 KB

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