carl9170 firmware: import 1.7.0
[carl9170fw.git] / tools / carlu / src / main.c
1 /*
2  * carl9170user - userspace testing utility for ar9170 devices
3  *
4  * main program routine
5  *
6  * Copyright 2009, 2010 Christian Lamparter <chunkeey@googlemail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (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 along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include "SDL.h"
34 #include <SDL_version.h>
35
36 #include "debug.h"
37 #include "carlu.h"
38 #include "usb.h"
39 #include "frame.h"
40 #include "test.h"
41
42 void *carlu_alloc_driver(size_t size)
43 {
44         unsigned int i;
45         struct carlu *ar;
46
47         if (size < sizeof(*ar)) {
48                 err("bogus driver context request.");
49                 return NULL;
50         }
51
52         ar = malloc(size);
53         if (ar == NULL) {
54                 err("failed to alloc driver context.");
55                 return NULL;
56         }
57         memset(ar, 0, size);
58
59         for (i = 0; i < __AR9170_NUM_TXQ; i++)
60                 frame_queue_init(&ar->tx_sent_queue[i]);
61         ar->resp_lock = SDL_CreateMutex();
62         ar->mem_lock = SDL_CreateMutex();
63         ar->resp_pend = SDL_CreateCond();
64         ar->tx_pending = 0;
65         return ar;
66 }
67
68 void carlu_free_driver(struct carlu *ar)
69 {
70         unsigned int i;
71
72         dbg("destroy driver struct.\n");
73         SDL_DestroyMutex(ar->resp_lock);
74         SDL_DestroyMutex(ar->mem_lock);
75         SDL_DestroyCond(ar->resp_pend);
76
77         for (i = 0; i < __AR9170_NUM_TXQ; i++)
78                 frame_queue_kill(&ar->tx_sent_queue[i]);
79
80         free(ar);
81 }
82
83 static int carlu_init()
84 {
85         struct SDL_version compiled;
86         int ret;
87
88         SDL_VERSION(&compiled);
89         dbg("=== SDL %d.%d.%d ===\n", compiled.major, compiled.minor, compiled.patch);
90
91         ret = SDL_Init(SDL_INIT_TIMER);
92         if (ret != 0) {
93                 err("Unable to initialize SDL: (%s)\n", SDL_GetError());
94                 return EXIT_FAILURE;
95         }
96
97         return usb_init();
98 }
99
100 static void carlu_exit()
101 {
102         SDL_Quit();
103         usb_exit();
104 }
105
106 static int carlu_dump_eeprom(void)
107 {
108         struct carlu *carl = NULL;
109         uint8_t data[8192] = { 0 };
110         int err;
111
112         err = carlu_init();
113         if (err)
114                 goto out;
115
116         carl = carlusb_probe();
117         if (IS_ERR_OR_NULL(carl)) {
118                 err = PTR_ERR(carl);
119                 goto out;
120         }
121
122         err = carlu_cmd_mem_dump(carl, 0, sizeof(data), &data);
123         if (err)
124                 goto out_close;
125
126         print_hex_dump_bytes(INFO, "EEPROM:", data, sizeof(data));
127
128 out_close:
129         carlusb_close(carl);
130
131 out:
132         carlu_exit();
133         return err ? EXIT_FAILURE : EXIT_SUCCESS;
134 }
135
136 static int carlu_run_loop_test(void)
137 {
138         struct carlu *carl;
139         int err;
140
141         err = carlu_init();
142         if (err)
143                 return EXIT_FAILURE;
144
145         carl = carlusb_probe();
146         if (IS_ERR_OR_NULL(carl)) {
147                 err = PTR_ERR(carl);
148                 goto out;
149         }
150
151         carlu_cmd_write_mem(carl, AR9170_MAC_REG_BCN_PERIOD, 0xFFFFFFFF);
152         carlu_cmd_write_mem(carl, AR9170_MAC_REG_PRETBTT, 0xFFFFFFFF);
153
154         /* different payload test */
155         carlu_loopback_test(carl, 9000, 1000, 1566, 1566);
156         carlusb_close(carl);
157
158 out:
159         return err ? EXIT_FAILURE : EXIT_SUCCESS;
160 }
161
162 static int carlu_probe_all(void)
163 {
164         struct carlu *carl[32] = { 0 };
165         unsigned int devs;
166         int ret;
167
168         ret = carlu_init();
169         if (ret)
170                 return EXIT_FAILURE;
171
172         for (devs = 0; devs < ARRAY_SIZE(carl); devs++) {
173                 carl[devs] = carlusb_probe();
174                 if (IS_ERR_OR_NULL(carl[devs]))
175                         break;
176         }
177
178         info("Found %d devices\n", devs);
179
180         for (; devs > 0; devs--)
181                 carlusb_close(carl[devs - 1]);
182
183         carlu_exit();
184         return EXIT_SUCCESS;
185 }
186
187 struct menu_struct {
188         char option;
189         unsigned int parameters;
190         int (*function)(void);
191         char help_text[80];
192 };
193
194 #define MENU_ITEM(op, func, helpme)     \
195         {                               \
196                 .option = op,           \
197                 .parameters = 0,        \
198                 .function = func,       \
199                 .help_text = helpme,    \
200         }
201
202 static int show_help(void);
203
204 static const struct menu_struct menu[] = {
205         [0] = MENU_ITEM('h', show_help, "shows this useless help message text."),       /* keep this entry at 0! */
206               MENU_ITEM('e', carlu_dump_eeprom, "hexdumps eeprom content to stdout."),
207               MENU_ITEM('l', carlusb_print_known_devices, "list of all known ar9170 usb devices."),
208               MENU_ITEM('p', carlu_probe_all, "probe all possible devices."),
209               MENU_ITEM('t', carlu_run_loop_test, "run tx/rx test."),
210 };
211
212 static int show_help(void)
213 {
214         unsigned int i;
215         char parameters[ARRAY_SIZE(menu) + 1];
216
217         for (i = 0; i < ARRAY_SIZE(menu); i++)
218                 parameters[i] = menu[i].option;
219
220         parameters[ARRAY_SIZE(menu)] = '\0';
221
222         info("usage: ar9170user -[%s]\n", parameters);
223
224         for (i = 0; i < ARRAY_SIZE(menu); i++)
225                 info("\t-%c\t%s\n", menu[i].option, menu[i].help_text);
226
227         return EXIT_FAILURE;
228 }
229
230 static int select_menu_item(const char arg)
231 {
232         unsigned int i;
233
234         for (i = ARRAY_SIZE(menu) - 1; i != 0; i--) {
235                 if (arg == menu[i].option)
236                         break;
237         }
238
239         return menu[i].function();
240 }
241
242 int main(int argc, char *argv[])
243 {
244         init_debug();
245
246         if (argc != 2 || strlen(argv[1]) != 2 || argv[1][0] != '-')
247                 return show_help();
248
249         return select_menu_item(argv[1][1]);
250 }