configure.ac: add libusb/libusbx support (for xpp)
[dahdi/tools.git] / xpp / astribank_tool.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 <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <assert.h>
28 #include <getopt.h>
29 #include <sys/types.h>
30 #include <arpa/inet.h>
31 #include "astribank_usb.h"
32 #include "mpptalk.h"
33 #include <debug.h>
34 #include <xusb.h>
35
36 #define DBG_MASK        0x80
37 /* if enabled, adds support for resetting pre-MPP USB firmware - if we 
38  * failed opening a device and we were asked to reset it, try also the
39  * old protocol.
40  */
41 #define SUPPORT_OLD_RESET
42
43 static char     *progname;
44
45 static void usage()
46 {
47         fprintf(stderr, "Usage: %s [options] -D {/proc/bus/usb|/dev/bus/usb}/<bus>/<dev> [operation...]\n", progname);
48         fprintf(stderr, "\tOptions:\n");
49         fprintf(stderr, "\t\t[-v]               # Increase verbosity\n");
50         fprintf(stderr, "\t\t[-d mask]          # Debug mask (0xFF for everything)\n");
51         fprintf(stderr, "\tOperations:\n");
52         fprintf(stderr, "\t\t[-n]               # Renumerate device\n");
53         fprintf(stderr, "\t\t[-r kind]          # Reset: kind = {half|full}\n");
54         fprintf(stderr, "\t\t[-p port]          # TwinStar: USB port number [0, 1]\n");
55         fprintf(stderr, "\t\t[-w (0|1)]         # TwinStar: Watchdog off or on guard\n");
56         fprintf(stderr, "\t\t[-Q]               # Query device properties\n");
57         exit(1);
58 }
59
60 static int reset_kind(const char *arg)
61 {
62         static const struct {
63                 const char      *name;
64                 int             type_code;
65         } reset_kinds[] = {
66                 { "half",       0 },
67                 { "full",       1 },
68         };
69         int     i;
70
71         for(i = 0; i < sizeof(reset_kinds)/sizeof(reset_kinds[0]); i++) {
72                 if(strcasecmp(reset_kinds[i].name, arg) == 0)
73                         return reset_kinds[i].type_code;
74         }
75         ERR("Uknown reset kind '%s'\n", arg);
76         return -1;
77 }
78
79
80 static int show_hardware(struct astribank_device *astribank)
81 {
82         int     ret;
83         struct eeprom_table     eeprom_table;
84         struct capabilities     capabilities;
85         struct extrainfo        extrainfo;
86
87         ret = mpp_caps_get(astribank, &eeprom_table, &capabilities, NULL);
88         if(ret < 0)
89                 return ret;
90         show_eeprom(&eeprom_table, stdout);
91         show_astribank_status(astribank, stdout);
92         if(astribank->eeprom_type == EEPROM_TYPE_LARGE) {
93                 show_capabilities(&capabilities, stdout);
94                 if(STATUS_FPGA_LOADED(astribank->status)) {
95                         uint8_t unit;
96                         uint8_t card_status;
97                         uint8_t card_type;
98                         uint8_t fpga_configuration;
99                         uint8_t status;
100
101                         for(unit = 0; unit < 5; unit++) {
102                                 ret = mpps_card_info(astribank, unit, &card_type, &card_status);
103                                 if(ret < 0)
104                                         return ret;
105                                 printf("CARD %d: type=%x.%x %s\n", unit,
106                                                 ((card_type >> 4) & 0xF), (card_type & 0xF),
107                                                 ((card_status & 0x1) ? "PIC" : "NOPIC"));
108                         }
109                         ret = mpps_stat(astribank, unit, &fpga_configuration, &status);
110                         if (ret < 0)
111                                 return ret;
112                         printf("FPGA: %-17s: %d\n", "Configuration num", fpga_configuration);
113                         printf("FPGA: %-17s: %s\n", "Watchdog Timer",
114                                 (SER_STAT_WATCHDOG_READY(status)) ? "ready" : "expired");
115                         printf("FPGA: %-17s: %s\n", "XPD Alive",
116                                 (SER_STAT_XPD_ALIVE(status)) ? "yes" : "no");
117                 }
118                 ret = mpp_extrainfo_get(astribank, &extrainfo);
119                 if(ret < 0)
120                         return ret;
121                 show_extrainfo(&extrainfo, stdout);
122                 if(CAP_EXTRA_TWINSTAR(&capabilities)) {
123                         twinstar_show(astribank, stdout);
124                 }
125         }
126         return 0;
127 }
128
129 #ifdef SUPPORT_OLD_RESET
130 /* Try to reset a device using USB_FW.hex, up to Xorcom rev. 6885 */
131 int old_reset(const char* devpath)
132 {
133         struct astribank_device *astribank;
134         int ret;
135         struct {
136                 uint8_t         op;
137         } PACKED header = {0x20}; /* PT_RESET */
138         char *buf = (char*) &header;
139
140         /* Note that the function re-opens the connection to the Astribank
141          * as any reference to the previous connection was lost when mpp_open
142          * returned NULL as the astribank reference. */
143         astribank = astribank_open(devpath, 1);
144         if (!astribank) {
145                 DBG("Failed re-opening astribank device for old_reset\n");
146                 return -ENODEV;
147         }
148         ret = xusb_send(astribank->xusb, buf, 1, 5000);
149
150         /* If we just had a reenumeration, we may get -ENODEV */
151         if(ret < 0 && ret != -ENODEV)
152                         return ret;
153         /* We don't astribank_close(), as it has likely been
154          * reenumerated by now. */
155         return 0;
156 }       
157 #endif /* SUPPORT_OLD_RESET */
158
159 int main(int argc, char *argv[])
160 {
161         char                    *devpath = NULL;
162         struct astribank_device *astribank;
163         const char              options[] = "vd:D:nr:p:w:Q";
164         int                     opt_renumerate = 0;
165         char                    *opt_port = NULL;
166         char                    *opt_watchdog = NULL;
167         char                    *opt_reset = NULL;
168         int                     opt_query = 0;
169         int                     ret;
170
171         progname = argv[0];
172         while (1) {
173                 int     c;
174
175                 c = getopt (argc, argv, options);
176                 if (c == -1)
177                         break;
178
179                 switch (c) {
180                         case 'D':
181                                 devpath = optarg;
182                                 break;
183                         case 'n':
184                                 opt_renumerate++;
185                                 break;
186                         case 'p':
187                                 opt_port = optarg;
188                                 break;
189                         case 'w':
190                                 opt_watchdog = optarg;
191                                 break;
192                         case 'r':
193                                 opt_reset = optarg;
194                                 /*
195                                  * Sanity check so we can reject bad
196                                  * arguments before device access.
197                                  */
198                                 if(reset_kind(opt_reset) < 0)
199                                         usage();
200                                 break;
201                         case 'Q':
202                                 opt_query = 1;
203                                 break;
204                         case 'v':
205                                 verbose++;
206                                 break;
207                         case 'd':
208                                 debug_mask = strtoul(optarg, NULL, 0);
209                                 break;
210                         case 'h':
211                         default:
212                                 ERR("Unknown option '%c'\n", c);
213                                 usage();
214                 }
215         }
216         if(!devpath) {
217                 ERR("Missing device path\n");
218                 usage();
219         }
220         DBG("Startup %s\n", devpath);
221         if((astribank = mpp_init(devpath, 1)) == NULL) {
222                 ERR("Failed initializing MPP\n");
223 #ifdef SUPPORT_OLD_RESET
224                 DBG("opt_reset = %s\n", opt_reset);
225                 if (opt_reset) {
226                         DBG("Trying old reset method\n");
227                         if ((ret = old_reset(devpath)) != 0) {
228                                 ERR("Old reset method failed as well: %d\n", ret);
229                         }
230                 }
231 #endif /* SUPPORT_OLD_RESET */
232
233                 return 1;
234         }
235         /*
236          * First process reset options. We want to be able
237          * to reset minimal USB firmwares even if they don't
238          * implement the full MPP protocol (e.g: EEPROM_BURN)
239          */
240         if(opt_reset) {
241                 int     full_reset;
242
243                 if((full_reset = reset_kind(opt_reset)) < 0) {
244                         ERR("Bad reset kind '%s'\n", opt_reset);
245                         return 1;
246                 }
247                 DBG("Reseting (%s)\n", opt_reset);
248                 if((ret = mpp_reset(astribank, full_reset)) < 0) {
249                         ERR("%s Reseting astribank failed: %d\n",
250                                 (full_reset) ? "Full" : "Half", ret);
251                 }
252                 goto out;
253         }
254         show_astribank_info(astribank);
255         if(opt_query) {
256                 show_hardware(astribank);
257         } else if(opt_renumerate) {
258                 DBG("Renumerate\n");
259                 if((ret = mpp_renumerate(astribank)) < 0) {
260                         ERR("Renumerating astribank failed: %d\n", ret);
261                 }
262         } else if(opt_watchdog) {
263                 int     watchdogstate = strtoul(opt_watchdog, NULL, 0);
264
265                 DBG("TWINSTAR: Setting watchdog %s-guard\n",
266                         (watchdogstate) ? "on" : "off");
267                 if((ret = mpp_tws_setwatchdog(astribank, watchdogstate)) < 0) {
268                         ERR("Failed to set watchdog to %d\n", watchdogstate);
269                         return 1;
270                 }
271         } else if(opt_port) {
272                 int     new_portnum = strtoul(opt_port, NULL, 0);
273                 int     tws_portnum = mpp_tws_portnum(astribank);
274                 char    *msg = (new_portnum == tws_portnum)
275                                         ? " Same same, never mind..."
276                                         : "";
277
278                 DBG("TWINSTAR: Setting portnum to %d.%s\n", new_portnum, msg);
279                 if((ret = mpp_tws_setportnum(astribank, new_portnum)) < 0) {
280                         ERR("Failed to set USB portnum to %d\n", new_portnum);
281                         return 1;
282                 }
283         }
284 out:
285         mpp_exit(astribank);
286         return 0;
287 }