Typos. Mostly by Lintian
[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 <xtalk/debug.h>
32 #include <xtalk/xusb.h>
33 #include "mpptalk.h"
34 #include "astribank.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("Unknown reset kind '%s'\n", arg);
76         return -1;
77 }
78
79 int main(int argc, char *argv[])
80 {
81         char                    *devpath = NULL;
82         struct astribank *astribank;
83         struct mpp_device *mpp;
84         const char              options[] = "vd:D:nr:p:w:Q";
85         int                     opt_renumerate = 0;
86         char                    *opt_port = NULL;
87         char                    *opt_watchdog = NULL;
88         char                    *opt_reset = NULL;
89         int                     opt_query = 0;
90         int                     ret;
91
92         progname = argv[0];
93         while (1) {
94                 int     c;
95
96                 c = getopt (argc, argv, options);
97                 if (c == -1)
98                         break;
99
100                 switch (c) {
101                         case 'D':
102                                 devpath = optarg;
103                                 break;
104                         case 'n':
105                                 opt_renumerate++;
106                                 break;
107                         case 'p':
108                                 opt_port = optarg;
109                                 break;
110                         case 'w':
111                                 opt_watchdog = optarg;
112                                 break;
113                         case 'r':
114                                 opt_reset = optarg;
115                                 /*
116                                  * Sanity check so we can reject bad
117                                  * arguments before device access.
118                                  */
119                                 if(reset_kind(opt_reset) < 0)
120                                         usage();
121                                 break;
122                         case 'Q':
123                                 opt_query = 1;
124                                 break;
125                         case 'v':
126                                 verbose++;
127                                 break;
128                         case 'd':
129                                 debug_mask = strtoul(optarg, NULL, 0);
130                                 break;
131                         case 'h':
132                         default:
133                                 ERR("Unknown option '%c'\n", c);
134                                 usage();
135                 }
136         }
137         if(!devpath) {
138                 ERR("Missing device path\n");
139                 usage();
140         }
141         DBG("Startup %s\n", devpath);
142         astribank = astribank_new(devpath);
143         if(!astribank) {
144                 ERR("Failed initializing Astribank\n");
145                 return 1;
146         }
147         mpp = astribank_mpp_open(astribank);
148         /*
149          * First process reset options. We want to be able
150          * to reset minimal USB firmwares even if they don't
151          * implement the full MPP protocol (e.g: EEPROM_BURN)
152          */
153         if(opt_reset) {
154                 int     full_reset;
155
156                 if((full_reset = reset_kind(opt_reset)) < 0) {
157                         ERR("Bad reset kind '%s'\n", opt_reset);
158                         return 1;
159                 }
160                 DBG("Resetting (%s)\n", opt_reset);
161                 if((ret = mpp_reset(mpp, full_reset)) < 0) {
162                         ERR("%s Resetting astribank failed: %d\n",
163                                 (full_reset) ? "Full" : "Half", ret);
164                 }
165                 goto out;
166         }
167         show_astribank_info(astribank);
168         if(opt_query) {
169                 show_hardware(mpp);
170         } else if(opt_renumerate) {
171                 DBG("Renumerate\n");
172                 if((ret = mpp_renumerate(mpp)) < 0) {
173                         ERR("Renumerating astribank failed: %d\n", ret);
174                 }
175         } else if(opt_watchdog) {
176                 int     watchdogstate = strtoul(opt_watchdog, NULL, 0);
177
178                 DBG("TWINSTAR: Setting watchdog %s-guard\n",
179                         (watchdogstate) ? "on" : "off");
180                 if((ret = mpp_tws_setwatchdog(mpp, watchdogstate)) < 0) {
181                         ERR("Failed to set watchdog to %d\n", watchdogstate);
182                         return 1;
183                 }
184         } else if(opt_port) {
185                 int     new_portnum = strtoul(opt_port, NULL, 0);
186                 int     tws_portnum = mpp_tws_portnum(mpp);
187                 char    *msg = (new_portnum == tws_portnum)
188                                         ? " Same same, never mind..."
189                                         : "";
190
191                 DBG("TWINSTAR: Setting portnum to %d.%s\n", new_portnum, msg);
192                 if((ret = mpp_tws_setportnum(mpp, new_portnum)) < 0) {
193                         ERR("Failed to set USB portnum to %d\n", new_portnum);
194                         return 1;
195                 }
196         }
197 out:
198         astribank_destroy(astribank);
199         return 0;
200 }