suncore.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/config.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/console.h>
  16. #include <linux/tty.h>
  17. #include <linux/errno.h>
  18. #include <linux/string.h>
  19. #include <linux/init.h>
  20. #include <asm/oplib.h>
  21. #include "suncore.h"
  22. int sunserial_current_minor = 64;
  23. EXPORT_SYMBOL(sunserial_current_minor);
  24. void
  25. sunserial_console_termios(struct console *con)
  26. {
  27. char mode[16], buf[16], *s;
  28. char *mode_prop = "ttyX-mode";
  29. char *cd_prop = "ttyX-ignore-cd";
  30. char *dtr_prop = "ttyX-rts-dtr-off";
  31. int baud, bits, stop, cflag;
  32. char parity;
  33. int carrier = 0;
  34. int rtsdtr = 1;
  35. int topnd, nd;
  36. if (!serial_console)
  37. return;
  38. if (serial_console == 1) {
  39. mode_prop[3] = 'a';
  40. cd_prop[3] = 'a';
  41. dtr_prop[3] = 'a';
  42. } else {
  43. mode_prop[3] = 'b';
  44. cd_prop[3] = 'b';
  45. dtr_prop[3] = 'b';
  46. }
  47. topnd = prom_getchild(prom_root_node);
  48. nd = prom_searchsiblings(topnd, "options");
  49. if (!nd) {
  50. strcpy(mode, "9600,8,n,1,-");
  51. goto no_options;
  52. }
  53. if (!prom_node_has_property(nd, mode_prop)) {
  54. strcpy(mode, "9600,8,n,1,-");
  55. goto no_options;
  56. }
  57. memset(mode, 0, sizeof(mode));
  58. prom_getstring(nd, mode_prop, mode, sizeof(mode));
  59. if (prom_node_has_property(nd, cd_prop)) {
  60. memset(buf, 0, sizeof(buf));
  61. prom_getstring(nd, cd_prop, buf, sizeof(buf));
  62. if (!strcmp(buf, "false"))
  63. carrier = 1;
  64. /* XXX: this is unused below. */
  65. }
  66. if (prom_node_has_property(nd, dtr_prop)) {
  67. memset(buf, 0, sizeof(buf));
  68. prom_getstring(nd, dtr_prop, buf, sizeof(buf));
  69. if (!strcmp(buf, "false"))
  70. rtsdtr = 0;
  71. /* XXX: this is unused below. */
  72. }
  73. no_options:
  74. cflag = CREAD | HUPCL | CLOCAL;
  75. s = mode;
  76. baud = simple_strtoul(s, NULL, 0);
  77. s = strchr(s, ',');
  78. bits = simple_strtoul(++s, NULL, 0);
  79. s = strchr(s, ',');
  80. parity = *(++s);
  81. s = strchr(s, ',');
  82. stop = simple_strtoul(++s, NULL, 0);
  83. s = strchr(s, ',');
  84. /* XXX handshake is not handled here. */
  85. switch (baud) {
  86. case 150: cflag |= B150; break;
  87. case 300: cflag |= B300; break;
  88. case 600: cflag |= B600; break;
  89. case 1200: cflag |= B1200; break;
  90. case 2400: cflag |= B2400; break;
  91. case 4800: cflag |= B4800; break;
  92. case 9600: cflag |= B9600; break;
  93. case 19200: cflag |= B19200; break;
  94. case 38400: cflag |= B38400; break;
  95. default: baud = 9600; cflag |= B9600; break;
  96. }
  97. switch (bits) {
  98. case 5: cflag |= CS5; break;
  99. case 6: cflag |= CS6; break;
  100. case 7: cflag |= CS7; break;
  101. case 8: cflag |= CS8; break;
  102. default: cflag |= CS8; break;
  103. }
  104. switch (parity) {
  105. case 'o': cflag |= (PARENB | PARODD); break;
  106. case 'e': cflag |= PARENB; break;
  107. case 'n': default: break;
  108. }
  109. switch (stop) {
  110. case 2: cflag |= CSTOPB; break;
  111. case 1: default: break;
  112. }
  113. con->cflag = cflag;
  114. }
  115. EXPORT_SYMBOL(sunserial_console_termios);
  116. /* Sun serial MOUSE auto baud rate detection. */
  117. static struct mouse_baud_cflag {
  118. int baud;
  119. unsigned int cflag;
  120. } mouse_baud_table[] = {
  121. { 1200, B1200 },
  122. { 2400, B2400 },
  123. { 4800, B4800 },
  124. { 9600, B9600 },
  125. { -1, ~0 },
  126. { -1, ~0 },
  127. };
  128. unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
  129. {
  130. int i;
  131. for (i = 0; mouse_baud_table[i].baud != -1; i++)
  132. if (mouse_baud_table[i].cflag == (cflag & CBAUD))
  133. break;
  134. i += 1;
  135. if (mouse_baud_table[i].baud == -1)
  136. i = 0;
  137. *new_baud = mouse_baud_table[i].baud;
  138. return mouse_baud_table[i].cflag;
  139. }
  140. EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
  141. /* Basically, when the baud rate is wrong the mouse spits out
  142. * breaks to us.
  143. */
  144. int suncore_mouse_baud_detection(unsigned char ch, int is_break)
  145. {
  146. static int mouse_got_break = 0;
  147. static int ctr = 0;
  148. if (is_break) {
  149. /* Let a few normal bytes go by before we jump the gun
  150. * and say we need to try another baud rate.
  151. */
  152. if (mouse_got_break && ctr < 8)
  153. return 1;
  154. /* Ok, we need to try another baud. */
  155. ctr = 0;
  156. mouse_got_break = 1;
  157. return 2;
  158. }
  159. if (mouse_got_break) {
  160. ctr++;
  161. if (ch == 0x87) {
  162. /* Correct baud rate determined. */
  163. mouse_got_break = 0;
  164. }
  165. return 1;
  166. }
  167. return 0;
  168. }
  169. EXPORT_SYMBOL(suncore_mouse_baud_detection);
  170. static int __init suncore_init(void)
  171. {
  172. return 0;
  173. }
  174. static void __exit suncore_exit(void)
  175. {
  176. }
  177. module_init(suncore_init);
  178. module_exit(suncore_exit);
  179. MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
  180. MODULE_DESCRIPTION("Sun serial common layer");
  181. MODULE_LICENSE("GPL");