configure.ac: add libusb/libusbx support (for xpp)
[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 <debug.h>
32 #include "hexfile.h"
33 #include "mpptalk.h"
34 #include "pic_loader.h"
35 #include "echo_loader.h"
36 #include "astribank_usb.h"
37 #include "../autoconfig.h"
38
39 #define DBG_MASK        0x80
40 #define MAX_HEX_LINES   64000
41 #define HAVE_OCTASIC    1
42 #define DEF_SPAN_SPEC_FORMAT    "*:%c1" /* %c: 'E' or 'T' */
43
44 static char     *progname;
45
46 static void usage()
47 {
48         fprintf(stderr, "Usage: %s [options...] -D {/proc/bus/usb|/dev/bus/usb}/<bus>/<dev> hexfile...\n", progname);
49         fprintf(stderr, "\tOptions: {-F|-p}\n");
50         fprintf(stderr, "\t\t[-E]               # Burn to EEPROM\n");
51 #if HAVE_OCTASIC
52         fprintf(stderr, "\t\t[-O]               # Load Octasic firmware\n");
53         fprintf(stderr, "\t\t[-o]               # Show Octasic version\n");
54         fprintf(stderr, "\t\t[-S <pri-spec>]    # Set PRI type specification string\n");
55 #endif
56         fprintf(stderr, "\t\t[-F]               # Load FPGA firmware\n");
57         fprintf(stderr, "\t\t[-p]               # Load PIC firmware\n");
58         fprintf(stderr, "\t\t[-v]               # Increase verbosity\n");
59         fprintf(stderr, "\t\t[-A]               # Set A-Law for 1st module\n");
60         fprintf(stderr, "\t\t[-d mask]          # Debug mask (0xFF for everything)\n");
61         exit(1);
62 }
63
64 int handle_hexline(struct astribank_device *astribank, struct hexline *hexline)
65 {
66         uint16_t        len;
67         uint16_t        offset_dummy;
68         uint8_t         *data;
69         int             ret;
70
71         assert(hexline);
72         assert(astribank);
73         if(hexline->d.content.header.tt != TT_DATA) {
74                 DBG("Non data record type = %d\n", hexline->d.content.header.tt);
75                 return 0;
76         }
77         len = hexline->d.content.header.ll;
78         offset_dummy = hexline->d.content.header.offset;
79         data = hexline->d.content.tt_data.data;
80         if((ret = mpp_send_seg(astribank, data, offset_dummy, len)) < 0) {
81                 ERR("Failed hexfile send line: %d\n", ret);
82                 return -EINVAL;
83         }
84         return 0;
85 }
86
87
88 #ifdef  __GNUC__
89 static void print_parse_errors(int level, const char *msg, ...) __attribute__((format(printf,2,3)));
90 #endif
91
92 static void print_parse_errors(int level, const char *msg, ...)
93 {
94         va_list ap;
95
96         if (verbose > level) {
97                 va_start (ap, msg);
98                 vfprintf (stderr, msg, ap);
99                 va_end (ap);
100         }
101 }
102
103 static int load_hexfile(struct astribank_device *astribank, const char *hexfile, enum dev_dest dest)
104 {
105         struct hexdata          *hexdata = NULL;
106         int                     finished = 0;
107         int                     ret;
108         unsigned                i;
109         char                    star[] = "+\\+|+/+-";
110         const char              *devstr;
111
112         parse_hexfile_set_reporting(print_parse_errors);
113         if((hexdata  = parse_hexfile(hexfile, MAX_HEX_LINES)) == NULL) {
114                 perror(hexfile);
115                 return -errno;
116         }
117         devstr = xusb_devpath(astribank->xusb);
118         INFO("%s [%s]: Loading %s Firmware: %s (version %s)\n",
119                 devstr,
120                 xusb_serial(astribank->xusb),
121                 dev_dest2str(dest),
122                 hexdata->fname, hexdata->version_info);
123         if((ret = mpp_send_start(astribank, dest, hexdata->version_info)) < 0) {
124                 ERR("%s: Failed hexfile send start: %d\n", devstr, ret);
125                 return ret;
126         }
127         for(i = 0; i < hexdata->maxlines; i++) {
128                 struct hexline  *hexline = hexdata->lines[i];
129
130                 if(!hexline)
131                         break;
132                 if(verbose > LOG_INFO) {
133                         printf("Sending: %4d%%    %c\r", (100 * i) / hexdata->last_line, star[i % sizeof(star)]);
134                         fflush(stdout);
135                 }
136                 if(finished) {
137                         ERR("%s: Extra data after End Of Data Record (line %d)\n", devstr, i);
138                         return 0;
139                 }
140                 if(hexline->d.content.header.tt == TT_EOF) {
141                         DBG("End of data\n");
142                         finished = 1;
143                         continue;
144                 }
145                 if((ret = handle_hexline(astribank, hexline)) < 0) {
146                         ERR("%s: Failed hexfile sending in lineno %d (ret=%d)\n", devstr, i, ret);;
147                         return ret;
148                 }
149         }
150         if(verbose > LOG_INFO) {
151                 putchar('\n');
152                 fflush(stdout);
153         }
154         if((ret = mpp_send_end(astribank)) < 0) {
155                 ERR("%s: Failed hexfile send end: %d\n", devstr, ret);
156                 return ret;
157         }
158 #if 0
159         fclose(fp);
160 #endif
161         free_hexdata(hexdata);
162         DBG("hexfile loaded successfully\n");
163         return 0;
164 }
165
166 int main(int argc, char *argv[])
167 {
168         char                    *devpath = NULL;
169         int                     opt_pic = 0;
170         int                     opt_echo = 0;
171         int                     opt_ecver = 0;
172 #if HAVE_OCTASIC
173         int                     opt_alaw = 0;
174         const char              *span_spec = NULL;
175         char                    def_span_spec[sizeof(DEF_SPAN_SPEC_FORMAT)];
176 #endif
177         int                     opt_dest = 0;
178         int                     opt_sum = 0;
179         enum dev_dest           dest = DEST_NONE;
180         const char              options[] = "vd:D:EFOopAS:";
181         int                     iface_num;
182         int                     ret;
183
184         progname = argv[0];
185         while (1) {
186                 int     c;
187
188                 c = getopt (argc, argv, options);
189                 if (c == -1)
190                         break;
191
192                 switch (c) {
193                         case 'D':
194                                 devpath = optarg;
195                                 break;
196                         case 'E':
197                                 if(dest != DEST_NONE) {
198                                         ERR("The -F and -E options are mutually exclusive.\n");
199                                         usage();
200                                 }
201                                 opt_dest++;
202                                 dest = DEST_EEPROM;
203                                 break;
204                         case 'F':
205                                 if(dest != DEST_NONE) {
206                                         ERR("The -F and -E options are mutually exclusive.\n");
207                                         usage();
208                                 }
209                                 opt_dest++;
210                                 dest = DEST_FPGA;
211                                 break;
212 #if HAVE_OCTASIC
213                         case 'O':
214                                 opt_echo = 1;
215                                 break;
216                         case 'o':
217                                 opt_ecver = 1;
218                                 break;
219                         case 'A':
220                                 opt_alaw = 1;
221                                 break;
222                         case 'S':
223                                 span_spec = optarg;
224                                 break;
225 #endif
226                         case 'p':
227                                 opt_pic = 1;
228                                 break;
229                         case 'v':
230                                 verbose++;
231                                 break;
232                         case 'd':
233                                 debug_mask = strtoul(optarg, NULL, 0);
234                                 break;
235                         case 'h':
236                         default:
237                                 ERR("Unknown option '%c'\n", c);
238                                 usage();
239                 }
240         }
241         opt_sum = opt_dest + opt_pic + opt_echo;
242         if(opt_sum > 1 || (opt_sum == 0 && opt_ecver == 0)) {
243                 ERR("The -F, -E"
244 #if HAVE_OCTASIC
245                         ", -O"
246 #endif
247                         " and -p options are mutually exclusive, if neither is used then -o should present\n");
248                 usage();
249         }
250         iface_num = (opt_dest) ? 1 : 0;
251         if(!opt_pic && !opt_ecver) {
252                 if(optind != argc - 1) {
253                         ERR("Got %d hexfile names (Need exactly one hexfile)\n",
254                                 argc - 1 - optind);
255                         usage();
256                 }
257         }
258         if(!devpath) {
259                 ERR("Missing device path.\n");
260                 usage();
261         }
262 # ifdef HAVE_OCTASIC
263         if (!span_spec) {
264                 snprintf(def_span_spec, sizeof(def_span_spec),
265                                 DEF_SPAN_SPEC_FORMAT, opt_alaw? 'E' : 'T');
266                 span_spec = def_span_spec;
267         }
268 #endif
269         if(opt_dest) {
270                 /*
271                  * MPP Interface
272                  */
273                 struct astribank_device *astribank;
274
275                 if((astribank = mpp_init(devpath, iface_num)) == NULL) {
276                         ERR("%s: Opening astribank failed\n", devpath);
277                         return 1;
278                 }
279                 //show_astribank_info(astribank);
280                 if(load_hexfile(astribank, argv[optind], dest) < 0) {
281                         ERR("%s: Loading firmware to %s failed\n", devpath, dev_dest2str(dest));
282                         return 1;
283                 }
284                 astribank_close(astribank, 0);
285         } else if(opt_pic || opt_echo || opt_ecver) {
286                 /*
287                  * XPP Interface
288                  */
289                 struct astribank_device *astribank;
290
291                 if((astribank = astribank_open(devpath, iface_num)) == NULL) {
292                         ERR("%s: Opening astribank failed\n", devpath);
293                         return 1;
294                 }
295                 //show_astribank_info(astribank);
296 #if HAVE_OCTASIC
297                 if (opt_ecver) {
298                         if((ret = echo_ver(astribank)) < 0) {
299                                 ERR("%s: Get Octasic version failed (Is Echo canceller card connected?)\n", devpath);
300                                 return 1;
301                         } else 
302                                 INFO("Octasic version: 0x%0X\n", ret);
303                 }
304 #endif
305                 if (opt_pic) {
306                         if ((ret = load_pic(astribank, argc - optind, argv + optind)) < 0) {
307                                 ERR("%s: Loading PIC's failed\n", devpath);
308                                 return 1;
309                         }
310 #if HAVE_OCTASIC
311                 } else if (opt_echo) {
312                         if((ret = load_echo(astribank, argv[optind], opt_alaw, span_spec)) < 0) {
313                                 ERR("%s: Loading ECHO's failed\n", devpath);
314                                 return 1;
315                         }
316 #endif
317                 }
318                 astribank_close(astribank, 0);
319         }
320         return 0;
321 }