modem.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * (C) Copyright 2002-2009
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. /* 'inline' - We have to do it fast */
  25. static inline void mdm_readline(char *buf, int bufsiz)
  26. {
  27. char c;
  28. char *p;
  29. int n;
  30. n = 0;
  31. p = buf;
  32. for(;;) {
  33. c = serial_getc();
  34. /* dbg("(%c)", c); */
  35. switch(c) {
  36. case '\r':
  37. break;
  38. case '\n':
  39. *p = '\0';
  40. return;
  41. default:
  42. if(n++ > bufsiz) {
  43. *p = '\0';
  44. return; /* sanity check */
  45. }
  46. *p = c;
  47. p++;
  48. break;
  49. }
  50. }
  51. }
  52. extern void dbg(const char *fmt, ...);
  53. int mdm_init (void)
  54. {
  55. char env_str[16];
  56. char *init_str;
  57. int i;
  58. extern char console_buffer[];
  59. extern void enable_putc(void);
  60. extern int hwflow_onoff(int);
  61. enable_putc(); /* enable serial_putc() */
  62. #ifdef CONFIG_HWFLOW
  63. init_str = getenv("mdm_flow_control");
  64. if (init_str && (strcmp(init_str, "rts/cts") == 0))
  65. hwflow_onoff (1);
  66. else
  67. hwflow_onoff(-1);
  68. #endif
  69. for (i = 1;;i++) {
  70. sprintf(env_str, "mdm_init%d", i);
  71. if ((init_str = getenv(env_str)) != NULL) {
  72. serial_puts(init_str);
  73. serial_puts("\n");
  74. for(;;) {
  75. mdm_readline(console_buffer, CONFIG_SYS_CBSIZE);
  76. dbg("ini%d: [%s]", i, console_buffer);
  77. if ((strcmp(console_buffer, "OK") == 0) ||
  78. (strcmp(console_buffer, "ERROR") == 0)) {
  79. dbg("ini%d: cmd done", i);
  80. break;
  81. } else /* in case we are originating call ... */
  82. if (strncmp(console_buffer, "CONNECT", 7) == 0) {
  83. dbg("ini%d: connect", i);
  84. return 0;
  85. }
  86. }
  87. } else
  88. break; /* no init string - stop modem init */
  89. udelay(100000);
  90. }
  91. udelay(100000);
  92. /* final stage - wait for connect */
  93. for(;i > 1;) { /* if 'i' > 1 - wait for connection
  94. message from modem */
  95. mdm_readline(console_buffer, CONFIG_SYS_CBSIZE);
  96. dbg("ini_f: [%s]", console_buffer);
  97. if (strncmp(console_buffer, "CONNECT", 7) == 0) {
  98. dbg("ini_f: connected");
  99. return 0;
  100. }
  101. }
  102. return 0;
  103. }