suncore.c 5.0 KB

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