part/dev_desc: Add log2 of blocksize to block_dev_desc data struct
[kernel/u-boot.git] / common / modem.c
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
24 #include <common.h>
25
26 /* 'inline' - We have to do it fast */
27 static inline void mdm_readline(char *buf, int bufsiz)
28 {
29         char c;
30         char *p;
31         int n;
32
33         n = 0;
34         p = buf;
35         for(;;) {
36                 c = serial_getc();
37
38                 /*              dbg("(%c)", c); */
39
40                 switch(c) {
41                 case '\r':
42                         break;
43                 case '\n':
44                         *p = '\0';
45                         return;
46
47                 default:
48                         if(n++ > bufsiz) {
49                                 *p = '\0';
50                                 return; /* sanity check */
51                         }
52                         *p = c;
53                         p++;
54                         break;
55                 }
56         }
57 }
58
59 extern void  dbg(const char *fmt, ...);
60 int mdm_init (void)
61 {
62         char env_str[16];
63         char *init_str;
64         int i;
65         extern void enable_putc(void);
66         extern int hwflow_onoff(int);
67
68         enable_putc(); /* enable serial_putc() */
69
70 #ifdef CONFIG_HWFLOW
71         init_str = getenv("mdm_flow_control");
72         if (init_str && (strcmp(init_str, "rts/cts") == 0))
73                 hwflow_onoff (1);
74         else
75                 hwflow_onoff(-1);
76 #endif
77
78         for (i = 1;;i++) {
79                 sprintf(env_str, "mdm_init%d", i);
80                 if ((init_str = getenv(env_str)) != NULL) {
81                         serial_puts(init_str);
82                         serial_puts("\n");
83                         for(;;) {
84                                 mdm_readline(console_buffer, CONFIG_SYS_CBSIZE);
85                                 dbg("ini%d: [%s]", i, console_buffer);
86
87                                 if ((strcmp(console_buffer, "OK") == 0) ||
88                                         (strcmp(console_buffer, "ERROR") == 0)) {
89                                         dbg("ini%d: cmd done", i);
90                                         break;
91                                 } else /* in case we are originating call ... */
92                                         if (strncmp(console_buffer, "CONNECT", 7) == 0) {
93                                                 dbg("ini%d: connect", i);
94                                                 return 0;
95                                         }
96                         }
97                 } else
98                         break; /* no init string - stop modem init */
99
100                 udelay(100000);
101         }
102
103         udelay(100000);
104
105         /* final stage - wait for connect */
106         for(;i > 1;) { /* if 'i' > 1 - wait for connection
107                                   message from modem */
108                 mdm_readline(console_buffer, CONFIG_SYS_CBSIZE);
109                 dbg("ini_f: [%s]", console_buffer);
110                 if (strncmp(console_buffer, "CONNECT", 7) == 0) {
111                         dbg("ini_f: connected");
112                         return 0;
113                 }
114         }
115
116         return 0;
117 }