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