Typos. Mostly by Lintian
[dahdi/tools.git] / xpp / astribank_hexload.c
1 /*
2  * Written by Oron Peled <oron@actcom.co.il>
3  * Copyright (C) 2008, Xorcom
4  *
5  * All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <errno.h>
29 #include <assert.h>
30 #include <arpa/inet.h>
31 #include <autoconfig.h>
32 #include <xtalk/debug.h>
33 #include <xtalk/xusb.h>
34 #include "hexfile.h"
35 #include "mpptalk.h"
36 #include "astribank.h"
37 #include "pic_loader.h"
38 #include "echo_loader.h"
39
40 #define DBG_MASK        0x80
41 #define MAX_HEX_LINES   64000
42 #define HAVE_OCTASIC    1
43 #define DEF_SPAN_SPEC_FORMAT    "*:%c1" /* %c: 'E' or 'T' */
44
45 static char     *progname;
46
47 static void usage()
48 {
49         fprintf(stderr, "Usage: %s [options...] -D {/proc/bus/usb|/dev/bus/usb}/<bus>/<dev> hexfile...\n", progname);
50         fprintf(stderr, "\tOptions: {-F|-p}\n");
51         fprintf(stderr, "\t\t[-E]               # Burn to EEPROM\n");
52 #if HAVE_OCTASIC
53         fprintf(stderr, "\t\t[-O]               # Load Octasic firmware\n");
54         fprintf(stderr, "\t\t[-o]               # Show Octasic version\n");
55         fprintf(stderr, "\t\t[-S <pri-spec>]    # Set PRI type specification string\n");
56 #endif
57         fprintf(stderr, "\t\t[-F]               # Load FPGA firmware\n");
58         fprintf(stderr, "\t\t[-p]               # Load PIC firmware\n");
59         fprintf(stderr, "\t\t[-v]               # Increase verbosity\n");
60         fprintf(stderr, "\t\t[-A]               # Set A-Law for 1st module\n");
61         fprintf(stderr, "\t\t[-d mask]          # Debug mask (0xFF for everything)\n");
62         exit(1);
63 }
64
65 int handle_hexline(struct mpp_device *mpp, struct hexline *hexline)
66 {
67         uint16_t        len;
68         uint16_t        offset_dummy;
69         uint8_t         *data;
70         int             ret;
71
72         assert(hexline);
73         assert(mpp);
74         if(hexline->d.content.header.tt != TT_DATA) {
75                 DBG("Non data record type = %d\n", hexline->d.content.header.tt);
76                 return 0;
77         }
78         len = hexline->d.content.header.ll;
79         offset_dummy = hexline->d.content.header.offset;
80         data = hexline->d.content.tt_data.data;
81         if((ret = mpp_send_seg(mpp, data, offset_dummy, len)) < 0) {
82                 ERR("Failed hexfile send line: %d\n", ret);
83                 return -EINVAL;
84         }
85         return 0;
86 }
87
88
89 #ifdef  __GNUC__
90 static void print_parse_errors(int level, const char *msg, ...) __attribute__((format(printf,2,3)));
91 #endif
92
93 static void print_parse_errors(int level, const char *msg, ...)
94 {
95         va_list ap;
96
97         if (verbose > level) {
98                 va_start (ap, msg);
99                 vfprintf (stderr, msg, ap);
100                 va_end (ap);
101         }
102 }
103
104 static int load_hexfile(struct mpp_device *mpp, const char *hexfile, enum dev_dest dest)
105 {
106         struct hexdata          *hexdata = NULL;
107         int                     finished = 0;
108         int                     ret;
109         unsigned                i;
110         char                    star[] = "+\\+|+/+-";
111         const char              *devstr;
112         struct xusb_device *xusb_device;
113         struct xusb_iface *xusb_iface;
114
115
116         parse_hexfile_set_reporting(print_parse_errors);
117         if((hexdata  = parse_hexfile(hexfile, MAX_HEX_LINES)) == NULL) {
118                 perror(hexfile);
119                 return -errno;
120         }
121         xusb_iface = xubs_iface_of_mpp(mpp);
122         xusb_device = xusb_deviceof(xusb_iface);
123         devstr = xusb_devpath(xusb_device);
124         INFO("%s [%s]: Loading %s Firmware: %s (version %s)\n",
125                 devstr,
126                 xusb_serial(xusb_device),
127                 dev_dest2str(dest),
128                 hexdata->fname, hexdata->version_info);
129         if((ret = mpp_send_start(mpp, dest, hexdata->version_info)) < 0) {
130                 ERR("%s: Failed hexfile send start: %d\n", devstr, ret);
131                 return ret;
132         }
133         for(i = 0; i < hexdata->maxlines; i++) {
134                 struct hexline  *hexline = hexdata->lines[i];
135
136                 if(!hexline)
137                         break;
138                 if(verbose > LOG_INFO) {
139                         printf("Sending: %4d%%    %c\r", (100 * i) / hexdata->last_line, star[i % sizeof(star)]);
140                         fflush(stdout);
141                 }
142                 if(finished) {
143                         ERR("%s: Extra data after End Of Data Record (line %d)\n", devstr, i);
144                         return 0;
145                 }
146                 if(hexline->d.content.header.tt == TT_EOF) {
147                         DBG("End of data\n");
148                         finished = 1;
149                         continue;
150                 }
151                 if((ret = handle_hexline(mpp, hexline)) < 0) {
152                         ERR("%s: Failed hexfile sending in lineno %d (ret=%d)\n", devstr, i, ret);;
153                         return ret;
154                 }
155         }
156         if(verbose > LOG_INFO) {
157                 putchar('\n');
158                 fflush(stdout);
159         }
160         if((ret = mpp_send_end(mpp)) < 0) {
161                 ERR("%s: Failed hexfile send end: %d\n", devstr, ret);
162                 return ret;
163         }
164 #if 0
165         fclose(fp);
166 #endif
167         free_hexdata(hexdata);
168         DBG("hexfile loaded successfully\n");
169         return 0;
170 }
171
172 int main(int argc, char *argv[])
173 {
174         char                    *devpath = NULL;
175         int                     opt_pic = 0;
176         int                     opt_echo = 0;
177         int                     opt_ecver = 0;
178 #if HAVE_OCTASIC
179         int                     opt_alaw = 0;
180         const char              *span_spec = NULL;
181         char                    def_span_spec[sizeof(DEF_SPAN_SPEC_FORMAT)];
182 #endif
183         int                     opt_dest = 0;
184         int                     opt_sum = 0;
185         enum dev_dest           dest = DEST_NONE;
186         const char              options[] = "vd:D:EFOopAS:";
187         int                     ret;
188
189         progname = argv[0];
190         while (1) {
191                 int     c;
192
193                 c = getopt (argc, argv, options);
194                 if (c == -1)
195                         break;
196
197                 switch (c) {
198                         case 'D':
199                                 devpath = optarg;
200                                 break;
201                         case 'E':
202                                 if(dest != DEST_NONE) {
203                                         ERR("The -F and -E options are mutually exclusive.\n");
204                                         usage();
205                                 }
206                                 opt_dest++;
207                                 dest = DEST_EEPROM;
208                                 break;
209                         case 'F':
210                                 if(dest != DEST_NONE) {
211                                         ERR("The -F and -E options are mutually exclusive.\n");
212                                         usage();
213                                 }
214                                 opt_dest++;
215                                 dest = DEST_FPGA;
216                                 break;
217 #if HAVE_OCTASIC
218                         case 'O':
219                                 opt_echo = 1;
220                                 break;
221                         case 'o':
222                                 opt_ecver = 1;
223                                 break;
224                         case 'A':
225                                 opt_alaw = 1;
226                                 break;
227                         case 'S':
228                                 span_spec = optarg;
229                                 break;
230 #endif
231                         case 'p':
232                                 opt_pic = 1;
233                                 break;
234                         case 'v':
235                                 verbose++;
236                                 break;
237                         case 'd':
238                                 debug_mask = strtoul(optarg, NULL, 0);
239                                 break;
240                         case 'h':
241                         default:
242                                 ERR("Unknown option '%c'\n", c);
243                                 usage();
244                 }
245         }
246         opt_sum = opt_dest + opt_pic + opt_echo;
247         if(opt_sum > 1 || (opt_sum == 0 && opt_ecver == 0)) {
248                 ERR("The -F, -E"
249 #if HAVE_OCTASIC
250                         ", -O"
251 #endif
252                         " and -p options are mutually exclusive, if neither is used then -o should present\n");
253                 usage();
254         }
255         if(!opt_pic && !opt_ecver) {
256                 if(optind != argc - 1) {
257                         ERR("Got %d hexfile names (Need exactly one hexfile)\n",
258                                 argc - 1 - optind);
259                         usage();
260                 }
261         }
262         if(!devpath) {
263                 ERR("Missing device path.\n");
264                 usage();
265         }
266 # ifdef HAVE_OCTASIC
267         if (!span_spec) {
268                 snprintf(def_span_spec, sizeof(def_span_spec),
269                                 DEF_SPAN_SPEC_FORMAT, opt_alaw? 'E' : 'T');
270                 span_spec = def_span_spec;
271         }
272 #endif
273         if(opt_dest) {
274                 /*
275                  * MPP Interface
276                  */
277                 struct astribank *astribank;
278                 struct mpp_device *mpp;
279
280                 astribank = astribank_new(devpath);
281                 if(!astribank) {
282                         ERR("%s: Opening astribank failed\n", devpath);
283                         return 1;
284                 }
285                 mpp = astribank_mpp_open(astribank);
286                 if(!mpp) {
287                         ERR("%s: Opening astribank XPP interface failed\n", devpath);
288                         return 1;
289                 }
290                 show_astribank_info(astribank);
291                 if(load_hexfile(mpp, argv[optind], dest) < 0) {
292                         ERR("%s: Loading firmware to %s failed\n", devpath, dev_dest2str(dest));
293                         return 1;
294                 }
295                 astribank_destroy(astribank);
296         } else if(opt_pic || opt_echo || opt_ecver) {
297                 /*
298                  * XPP Interface
299                  */
300                 struct astribank *astribank;
301                 struct xusb_iface *xpp_iface;
302
303                 astribank = astribank_new(devpath);
304                 if (!astribank) {
305                         ERR("%s: Opening astribank failed\n", devpath);
306                         return 1;
307                 }
308                 xpp_iface = astribank_xpp_open(astribank);
309                 if(!xpp_iface) {
310                         ERR("%s: Opening astribank XPP interface failed\n", devpath);
311                         return 1;
312                 }
313                 show_astribank_info(astribank);
314 #if HAVE_OCTASIC
315                 if (opt_ecver) {
316                         if((ret = echo_ver(astribank)) < 0) {
317                                 ERR("%s: Get Octasic version failed (Is Echo canceller card connected?)\n", devpath);
318                                 return 1;
319                         } else 
320                                 INFO("Octasic version: 0x%0X\n", ret);
321                 }
322 #endif
323                 if (opt_pic) {
324                         if ((ret = load_pic(astribank, argc - optind, argv + optind)) < 0) {
325                                 ERR("%s: Loading PIC's failed\n", devpath);
326                                 return 1;
327                         }
328 #if HAVE_OCTASIC
329                 } else if (opt_echo) {
330                         if((ret = load_echo(astribank, argv[optind], opt_alaw, span_spec)) < 0) {
331                                 ERR("%s: Loading ECHO's failed\n", devpath);
332                                 return 1;
333                         }
334 #endif
335                 }
336                 astribank_destroy(astribank);
337         }
338         return 0;
339 }