API: Fix compilation warnings in api_examples/demo.c.
[oweals/u-boot.git] / api_examples / demo.c
1 /*
2  * (C) Copyright 2007 Semihalf
3  *
4  * Written by: Rafal Jaworowski <raj@semihalf.com>
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  *
24  */
25
26 #include <common.h>
27 #include <linux/types.h>
28 #include <api_public.h>
29
30 #include "glue.h"
31
32 #define errf(fmt, args...) do { printf("ERROR @ %s(): ", __func__); printf(fmt, ##args); } while (0)
33
34 void    test_dump_si(struct sys_info *);
35 void    test_dump_di(int);
36 void    test_dump_sig(struct api_signature *);
37
38 char buf[2048];
39
40 #define WAIT_SECS 5
41
42 int main(int argc, char *argv[])
43 {
44         int rv = 0;
45         int h, i, j;
46         int devs_no;
47         struct api_signature *sig = NULL;
48         ulong start, now;
49         struct device_info *di;
50
51         if (!api_search_sig(&sig))
52                 return -1;
53
54         syscall_ptr = sig->syscall;
55         if (syscall_ptr == NULL)
56                 return -2;
57
58         if (sig->version > API_SIG_VERSION)
59                 return -3;
60
61         printf("API signature found @%x\n", (unsigned int)sig);
62         test_dump_sig(sig);
63
64         printf("\n*** Consumer API test ***\n");
65         printf("syscall ptr 0x%08x@%08x\n", (unsigned int)syscall_ptr,
66                 (unsigned int)&syscall_ptr);
67
68         /* console activities */
69         ub_putc('B');
70
71         printf("*** Press any key to continue ***\n");
72         printf("got char 0x%x\n", ub_getc());
73
74         /* system info */
75         test_dump_si(ub_get_sys_info());
76
77         /* timing */
78         printf("\n*** Timing - wait a couple of secs ***\n");
79         start = ub_get_timer(0);
80         printf("\ntime: start %lu\n\n", start);
81         for (i = 0; i < WAIT_SECS; i++)
82                 for (j = 0; j < 1000; j++)
83                         ub_udelay(1000);        /* wait 1 ms */
84
85         /* this is the number of milliseconds that passed from ub_get_timer(0) */
86         now = ub_get_timer(start);
87         printf("\ntime: now %lu\n\n", now);
88
89         /* enumerate devices */
90         printf("\n*** Enumerate devices ***\n");
91         devs_no = ub_dev_enum();
92
93         printf("Number of devices found: %d\n", devs_no);
94         if (devs_no == 0)
95                 return -1;
96
97
98         printf("\n*** Show devices ***\n");
99         for (i = 0; i < devs_no; i++) {
100                 test_dump_di(i);
101                 printf("\n");
102         }
103
104         printf("\n*** Operations on devices ***\n");
105
106         /* test opening a device already opened */
107         h = 0;
108         if ((rv = ub_dev_open(h)) != 0) {
109                 errf("open device %d error %d\n", h, rv);
110                 return -1;
111         }
112         if ((rv = ub_dev_open(h)) != 0)
113                 errf("open device %d error %d\n", h, rv);
114
115         ub_dev_close(h);
116
117         /* test storage */
118         printf("Trying storage devices...\n");
119         for (i = 0; i < devs_no; i++) {
120                 di = ub_dev_get(i);
121
122                 if (di->type & DEV_TYP_STOR)
123                         break;
124
125         }
126         if (i == devs_no)
127                 printf("No storage devices available\n");
128         else {
129                 if ((rv = ub_dev_open(i)) != 0)
130                         errf("open device %d error %d\n", i, rv);
131                 else if ((rv = ub_dev_read(i, &buf, 200, 20)) != 0)
132                         errf("could not read from device %d, error %d\n", i, rv);
133
134                 ub_dev_close(i);
135         }
136
137         /* test networking */
138         printf("Trying network devices...\n");
139         for (i = 0; i < devs_no; i++) {
140                 di = ub_dev_get(i);
141
142                 if (di->type == DEV_TYP_NET)
143                         break;
144
145         }
146         if (i == devs_no)
147                 printf("No network devices available\n");
148         else {
149                 if ((rv = ub_dev_open(i)) != 0)
150                         errf("open device %d error %d\n", i, rv);
151                 else if ((rv = ub_dev_send(i, &buf, 2048)) != 0)
152                         errf("could not send to device %d, error %d\n", i, rv);
153
154                 ub_dev_close(i);
155         }
156
157         if (ub_dev_close(h) != 0)
158                 errf("could not close device %d\n", h);
159
160         printf("\n*** Env vars ***\n");
161
162         printf("ethact = %s\n", ub_env_get("ethact"));
163         printf("old fileaddr = %s\n", ub_env_get("fileaddr"));
164         ub_env_set("fileaddr", "deadbeef");
165         printf("new fileaddr = %s\n", ub_env_get("fileaddr"));
166
167         const char *env = NULL;
168
169         while ((env = ub_env_enum(env)) != NULL)
170                 printf("%s = %s\n", env, ub_env_get(env));
171
172         /* reset */
173         ub_reset();
174         printf("\nHmm, reset returned...?!\n");
175
176         return rv;
177 }
178
179 void test_dump_sig(struct api_signature *sig)
180 {
181         printf("signature:\n");
182         printf("  version\t= %d\n", sig->version);
183         printf("  checksum\t= 0x%08x\n", sig->checksum);
184         printf("  sc entry\t= 0x%08x\n", (unsigned int)sig->syscall);
185 }
186
187 void test_dump_si(struct sys_info *si)
188 {
189         int i;
190
191         printf("sys info:\n");
192         printf("  clkbus\t= 0x%08x\n", (unsigned int)si->clk_bus);
193         printf("  clkcpu\t= 0x%08x\n", (unsigned int)si->clk_cpu);
194         printf("  bar\t\t= 0x%08x\n", (unsigned int)si->bar);
195
196         printf("---\n");
197         for (i = 0; i < si->mr_no; i++) {
198                 if (si->mr[i].flags == 0)
199                         break;
200
201                 printf("  start\t= 0x%08lx\n", si->mr[i].start);
202                 printf("  size\t= 0x%08lx\n", si->mr[i].size);
203
204                 switch(si->mr[i].flags & 0x000F) {
205                         case MR_ATTR_FLASH:
206                                 printf("  type FLASH\n");
207                                 break;
208                         case MR_ATTR_DRAM:
209                                 printf("  type DRAM\n");
210                                 break;
211                         case MR_ATTR_SRAM:
212                                 printf("  type SRAM\n");
213                                 break;
214                         default:
215                                 printf("  type UNKNOWN\n");
216                 }
217                 printf("---\n");
218         }
219 }
220
221 static char * test_stor_typ(int type)
222 {
223         if (type & DT_STOR_IDE)
224                 return "IDE";
225
226         if (type & DT_STOR_SCSI)
227                 return "SCSI";
228
229         if (type & DT_STOR_USB)
230                 return "USB";
231
232         if (type & DT_STOR_MMC);
233                 return "MMC";
234
235         return "Unknown";
236 }
237
238 void test_dump_di(int handle)
239 {
240         int i;
241         struct device_info *di = ub_dev_get(handle);
242
243         printf("device info (%d):\n", handle);
244         printf("  cookie\t= 0x%08x\n", (uint32_t)di->cookie);
245         printf("  type\t\t= 0x%08x\n", di->type);
246
247         if (di->type == DEV_TYP_NET) {
248                 printf("  hwaddr\t= ");
249                 for (i = 0; i < 6; i++)
250                         printf("%02x ", di->di_net.hwaddr[i]);
251
252                 printf("\n");
253
254         } else if (di->type & DEV_TYP_STOR) {
255                 printf("  type\t\t= %s\n", test_stor_typ(di->type));
256                 printf("  blk size\t\t= %d\n", (unsigned int)di->di_stor.block_size);
257                 printf("  blk count\t\t= %d\n", (unsigned int)di->di_stor.block_count);
258         }
259 }