Debian Codegraph @

xpp/xtalk/xtalk_test.c

raw
1 #include <errno.h>
2 #include <getopt.h>
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <string.h>
6 #include <xtalk/debug.h>
7 #include <xtalk/xusb.h>
8 #include <xtalk/proto_sync.h>
9 #include <autoconfig.h>
10
11 static char *progname;
12
13 #define DBG_MASK 0x10
14
15 static void usage()
16 {
17 fprintf(stderr, "Usage: %s [options...] -D <device> [hexnum ....]\n",
18 progname);
19 fprintf(stderr, "\tOptions:\n");
20 fprintf(stderr, "\t\t[-v] # Increase verbosity\n");
21 fprintf(stderr,
22 "\t\t[-d mask] # Debug mask (0xFF for everything)\n");
23 fprintf(stderr, "\tDevice:\n");
24 fprintf(stderr, "\t\t/proc/bus/usb/<bus>/<dev>\n");
25 fprintf(stderr, "\t\t/dev/bus/usb/<bus>/<dev>\n");
26 exit(1);
27 }
28
29 static void xusb_destructor(void *data)
30 {
31 struct xusb_device *xusb_device = data;
32 xusb_destroy(xusb_device);
33 }
34
35 #define KNOWN_DEV(p, i, v, d) \
36 { SPEC_HEAD(0xe4e4, (p), (d)), (i), (v) }
37
38 static const struct test_struct {
39 struct xusb_spec spec;
40 int interface_num;
41 uint8_t proto_version;
42 } known_devices[] = {
43 /* PROD I V NAME */
44 KNOWN_DEV(0x1161, 1, 0x14, "astribank2-USB"),
45 KNOWN_DEV(0x1162, 1, 0x14, "astribank2-FPGA"),
46 KNOWN_DEV(0x1191, 1, 0x10, "xpanel"),
47 KNOWN_DEV(0x1183, 1, 0x11, "multi-ps"),
48 KNOWN_DEV(0x11a3, 0, 0x01, "auth-dongle"),
49 KNOWN_DEV(0xbb01, 1, 0x10, "iwc"),
50 KNOWN_DEV(0, 0, 0, NULL),
51 };
52
53 int proto_get_reply_cb(
54 const struct xtalk_base *xtalk_base,
55 const struct xtalk_command_desc *cmd_desc,
56 struct xtalk_command *cmd)
57 {
58 INFO("CALLBACK: op=0x%X (%s): len=%d\n",
59 cmd_desc->op, cmd_desc->name, cmd->header.len);
60 return 0;
61 }
62
63 /*
64 * Not const, because we override proto_version for testing
65 */
66 static struct xtalk_protocol xtalk_test_base = {
67 .name = "XTALK_TEST",
68 .proto_version = 0, /* Modified in test_device() */
69 .commands = {
70 },
71 .ack_statuses = {
72 }
73 };
74
75 static int test_device(struct xusb_iface *xusb_iface, uint8_t wanted_version, int timeout)
76 {
77 struct xtalk_base *xtalk_base;
78 struct xtalk_sync *xtalk_sync;
79 int proto_version;
80 int ret;
81
82 xtalk_base = xtalk_base_new_on_xusb(xusb_iface);
83 if (!xtalk_base) {
84 ERR("Failed creating the xtalk device abstraction\n");
85 return -ENOMEM;
86 }
87 xtalk_sync = xtalk_sync_new(xtalk_base);
88 if (!xtalk_sync) {
89 ERR("Failed creating the xtalk sync device abstraction\n");
90 return -ENOMEM;
91 }
92 ret = xtalk_set_timeout(xtalk_base, timeout);
93 INFO("Original timeout=%d, now set to %d\n", ret, timeout);
94 /* override constness for testing */
95 xtalk_test_base.proto_version = wanted_version;
96 ret = xtalk_sync_set_protocol(xtalk_sync, &xtalk_test_base);
97 if (ret < 0) {
98 ERR("%s Protocol registration failed: %d\n",
99 xtalk_test_base.name, ret);
100 return -EPROTO;
101 }
102 ret = xtalk_cmd_callback(xtalk_base, XTALK_OP(XTALK, PROTO_GET_REPLY), proto_get_reply_cb, NULL);
103 if (ret < 0) {
104 ERR("%s Callback registration failed: %d\n",
105 xtalk_test_base.name, ret);
106 return -EPROTO;
107 }
108 proto_version = xtalk_proto_query(xtalk_sync);
109 if (proto_version < 0) {
110 ERR("Protocol query error: %s\n", strerror(-proto_version));
111 return proto_version;
112 }
113 if (proto_version != xtalk_test_base.proto_version) {
114 ERR("Bad protocol version: 0x%02x\n", proto_version);
115 return -EPROTO;
116 }
117 INFO("Device and Protocol are ready (proto_version=0x%X)\n",
118 proto_version);
119 xtalk_sync_delete(xtalk_sync);
120 xtalk_base_delete(xtalk_base);
121 return 0;
122 }
123
124 static int run_spec(int i, xusb_filter_t filter, char *devpath, int timeout)
125 {
126 const struct xusb_spec *s = &known_devices[i].spec;
127 int interface_num = known_devices[i].interface_num;
128 uint8_t proto_version = known_devices[i].proto_version;
129 struct xlist_node *xlist;
130 struct xlist_node *curr;
131 struct xusb_device *xusb_device;
132 int success = 1;
133
134 if (!s->name)
135 return 0;
136 xlist = xusb_find_byproduct(s, 1, filter, devpath);
137 if (!xlist_length(xlist))
138 return 1;
139 INFO("total %zd devices of type %s\n", xlist_length(xlist), s->name);
140 for (curr = xlist_shift(xlist); curr; curr = xlist_shift(xlist)) {
141 struct xusb_iface *xusb_iface;
142 int ret;
143
144 xusb_device = curr->data;
145 xusb_showinfo(xusb_device);
146 INFO("Testing interface %d\n", interface_num);
147 ret = xusb_claim(xusb_device, interface_num, &xusb_iface);
148 if (ret == 0) {
149 ret = test_device(xusb_iface, proto_version, timeout);
150 if (ret < 0)
151 success = 0;
152 }
153 xusb_destroy(xusb_device);
154 }
155 xlist_destroy(xlist, xusb_destructor);
156 return success;
157 }
158
159 int main(int argc, char *argv[])
160 {
161 char *devpath = NULL;
162 const char options[] = "vd:D:EFpt:";
163 xusb_filter_t filter = NULL;
164 int timeout = 500; /* millies */
165 int i;
166
167 progname = argv[0];
168 while (1) {
169 int c;
170
171 c = getopt(argc, argv, options);
172 if (c == -1)
173 break;
174
175 switch (c) {
176 case 'D':
177 devpath = optarg;
178 filter = xusb_filter_bypath;
179 break;
180 case 'v':
181 verbose++;
182 break;
183 case 't':
184 timeout = strtoul(optarg, NULL, 0);
185 break;
186 case 'd':
187 debug_mask = strtoul(optarg, NULL, 0);
188 break;
189 case 'h':
190 default:
191 ERR("Unknown option '%c'\n", c);
192 usage();
193 }
194 }
195 i = 0;
196 while (run_spec(i, filter, devpath, timeout))
197 i++;
198 return 0;
199 }