suncore.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/init.h>
  19. #include <asm/oplib.h>
  20. #include "suncore.h"
  21. int sunserial_current_minor = 64;
  22. EXPORT_SYMBOL(sunserial_current_minor);
  23. void
  24. sunserial_console_termios(struct console *con)
  25. {
  26. char mode[16], buf[16], *s;
  27. char *mode_prop = "ttyX-mode";
  28. char *cd_prop = "ttyX-ignore-cd";
  29. char *dtr_prop = "ttyX-rts-dtr-off";
  30. char *ssp_console_modes_prop = "ssp-console-modes";
  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. switch (serial_console) {
  39. case PROMDEV_OTTYA:
  40. mode_prop[3] = 'a';
  41. cd_prop[3] = 'a';
  42. dtr_prop[3] = 'a';
  43. break;
  44. case PROMDEV_OTTYB:
  45. mode_prop[3] = 'b';
  46. cd_prop[3] = 'b';
  47. dtr_prop[3] = 'b';
  48. break;
  49. case PROMDEV_ORSC:
  50. nd = prom_pathtoinode("rsc");
  51. if (!nd) {
  52. strcpy(mode, "115200,8,n,1,-");
  53. goto no_options;
  54. }
  55. if (!prom_node_has_property(nd, ssp_console_modes_prop)) {
  56. strcpy(mode, "115200,8,n,1,-");
  57. goto no_options;
  58. }
  59. memset(mode, 0, sizeof(mode));
  60. prom_getstring(nd, ssp_console_modes_prop, mode, sizeof(mode));
  61. goto no_options;
  62. default:
  63. strcpy(mode, "9600,8,n,1,-");
  64. goto no_options;
  65. }
  66. topnd = prom_getchild(prom_root_node);
  67. nd = prom_searchsiblings(topnd, "options");
  68. if (!nd) {
  69. strcpy(mode, "9600,8,n,1,-");
  70. goto no_options;
  71. }
  72. if (!prom_node_has_property(nd, mode_prop)) {
  73. strcpy(mode, "9600,8,n,1,-");
  74. goto no_options;
  75. }
  76. memset(mode, 0, sizeof(mode));
  77. prom_getstring(nd, mode_prop, mode, sizeof(mode));
  78. if (prom_node_has_property(nd, cd_prop)) {
  79. memset(buf, 0, sizeof(buf));
  80. prom_getstring(nd, cd_prop, buf, sizeof(buf));
  81. if (!strcmp(buf, "false"))
  82. carrier = 1;
  83. /* XXX: this is unused below. */
  84. }
  85. if (prom_node_has_property(nd, dtr_prop)) {
  86. memset(buf, 0, sizeof(buf));
  87. prom_getstring(nd, dtr_prop, buf, sizeof(buf));
  88. if (!strcmp(buf, "false"))
  89. rtsdtr = 0;
  90. /* XXX: this is unused below. */
  91. }
  92. no_options:
  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. EXPORT_SYMBOL(sunserial_console_termios);
  139. /* Sun serial MOUSE auto baud rate detection. */
  140. static struct mouse_baud_cflag {
  141. int baud;
  142. unsigned int cflag;
  143. } mouse_baud_table[] = {
  144. { 1200, B1200 },
  145. { 2400, B2400 },
  146. { 4800, B4800 },
  147. { 9600, B9600 },
  148. { -1, ~0 },
  149. { -1, ~0 },
  150. };
  151. unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
  152. {
  153. int i;
  154. for (i = 0; mouse_baud_table[i].baud != -1; i++)
  155. if (mouse_baud_table[i].cflag == (cflag & CBAUD))
  156. break;
  157. i += 1;
  158. if (mouse_baud_table[i].baud == -1)
  159. i = 0;
  160. *new_baud = mouse_baud_table[i].baud;
  161. return mouse_baud_table[i].cflag;
  162. }
  163. EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
  164. /* Basically, when the baud rate is wrong the mouse spits out
  165. * breaks to us.
  166. */
  167. int suncore_mouse_baud_detection(unsigned char ch, int is_break)
  168. {
  169. static int mouse_got_break = 0;
  170. static int ctr = 0;
  171. if (is_break) {
  172. /* Let a few normal bytes go by before we jump the gun
  173. * and say we need to try another baud rate.
  174. */
  175. if (mouse_got_break && ctr < 8)
  176. return 1;
  177. /* Ok, we need to try another baud. */
  178. ctr = 0;
  179. mouse_got_break = 1;
  180. return 2;
  181. }
  182. if (mouse_got_break) {
  183. ctr++;
  184. if (ch == 0x87) {
  185. /* Correct baud rate determined. */
  186. mouse_got_break = 0;
  187. }
  188. return 1;
  189. }
  190. return 0;
  191. }
  192. EXPORT_SYMBOL(suncore_mouse_baud_detection);
  193. static int __init suncore_init(void)
  194. {
  195. return 0;
  196. }
  197. static void __exit suncore_exit(void)
  198. {
  199. }
  200. module_init(suncore_init);
  201. module_exit(suncore_exit);
  202. MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
  203. MODULE_DESCRIPTION("Sun serial common layer");
  204. MODULE_LICENSE("GPL");