| 1 |
/* |
| 2 |
* Written by Oron Peled <[email protected]> |
| 3 |
* Copyright (C) 2008-2011, 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 <getopt.h> |
| 25 |
#include <string.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <errno.h> |
| 28 |
#include <assert.h> |
| 29 |
#include <arpa/inet.h> |
| 30 |
#include <xtalk/debug.h> |
| 31 |
#include <xtalk/xusb.h> |
| 32 |
#include <xtalk/proto_sync.h> |
| 33 |
#include <autoconfig.h> |
| 34 |
|
| 35 |
#define DBG_MASK 0x80 |
| 36 |
|
| 37 |
static char *progname; |
| 38 |
static int timeout = 500; /* msec */ |
| 39 |
static int iface_num = 1; |
| 40 |
|
| 41 |
static void usage() |
| 42 |
{ |
| 43 |
fprintf(stderr, "Usage: %s [options...] -D <busnum>/<devnum> [hexnum ....]\n", |
| 44 |
progname); |
| 45 |
fprintf(stderr, "\tOptions:\n"); |
| 46 |
fprintf(stderr, |
| 47 |
"\t\t[-I<iface_num>] # Interface number (default %d)\n", |
| 48 |
iface_num); |
| 49 |
fprintf(stderr, |
| 50 |
"\t\t[-t<timeout>] # Timeout (default %d)\n", |
| 51 |
timeout); |
| 52 |
fprintf(stderr, "\t\t[-Q] # Query protocol version\n"); |
| 53 |
fprintf(stderr, "\t\t[-v] # Increase verbosity\n"); |
| 54 |
fprintf(stderr, |
| 55 |
"\t\t[-d mask] # Debug mask (0xFF for everything)\n"); |
| 56 |
fprintf(stderr, "\t\t[-h] # This help\n"); |
| 57 |
exit(1); |
| 58 |
} |
| 59 |
|
| 60 |
/***** XTALK Interface *****************************************************/ |
| 61 |
static const struct xtalk_protocol xtalk_protocol = { |
| 62 |
.name = "XTALK-DERIVED", |
| 63 |
.proto_version = 0, |
| 64 |
.commands = { |
| 65 |
}, |
| 66 |
.ack_statuses = { |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
/***** XUSB Interface ******************************************************/ |
| 72 |
|
| 73 |
static int sendto_device(struct xusb_iface *iface, int nargs, char *args[]) |
| 74 |
{ |
| 75 |
char *reply = NULL; |
| 76 |
char *buf = NULL; |
| 77 |
int ret = 0; |
| 78 |
int i; |
| 79 |
|
| 80 |
assert(nargs >= 0); |
| 81 |
if (!nargs) |
| 82 |
goto out; |
| 83 |
buf = malloc(nargs); |
| 84 |
if (!buf) { |
| 85 |
ERR("Out of memory for %d command bytes\n", nargs); |
| 86 |
ret = -ENOMEM; |
| 87 |
goto out; |
| 88 |
} |
| 89 |
for (i = 0; i < nargs; i++) { |
| 90 |
int val = strtoul(args[i], NULL, 16); |
| 91 |
printf("%d> 0x%02X\n", i, val); |
| 92 |
buf[i] = val; |
| 93 |
} |
| 94 |
ret = xusb_send(iface, buf, nargs, timeout); |
| 95 |
if (ret < 0) { |
| 96 |
ERR("xusb_send failed ret=%d\n", ret); |
| 97 |
goto out; |
| 98 |
} |
| 99 |
reply = malloc(PACKET_SIZE); |
| 100 |
if (!reply) { |
| 101 |
ERR("Out of memory\n"); |
| 102 |
ret = -ENOMEM; |
| 103 |
goto out; |
| 104 |
} |
| 105 |
ret = xusb_recv(iface, reply, PACKET_SIZE, timeout); |
| 106 |
if (ret < 0) { |
| 107 |
ERR("Receive from usb failed.\n"); |
| 108 |
goto out; |
| 109 |
} |
| 110 |
dump_packet(LOG_INFO, 0, "REPLY", reply, ret); |
| 111 |
ret = 0; |
| 112 |
out: |
| 113 |
if (reply) |
| 114 |
free(reply); |
| 115 |
if (buf) |
| 116 |
free(buf); |
| 117 |
return ret; |
| 118 |
} |
| 119 |
|
| 120 |
static int show_protocol(struct xtalk_sync *xtalk_sync, struct xusb_iface *iface) |
| 121 |
{ |
| 122 |
int ret; |
| 123 |
|
| 124 |
ret = xtalk_sync_set_protocol(xtalk_sync, &xtalk_protocol); |
| 125 |
if (ret < 0) { |
| 126 |
ERR("%s Protocol registration failed: %s\n", |
| 127 |
xtalk_protocol.name, strerror(-ret)); |
| 128 |
return ret; |
| 129 |
} |
| 130 |
ret = xtalk_proto_query(xtalk_sync); |
| 131 |
if (ret < 0) { |
| 132 |
ERR("Protocol query error: %s\n", strerror(-ret)); |
| 133 |
return ret; |
| 134 |
} |
| 135 |
INFO("usb:%s: Protocol version 0x%X\n", xusb_devpath(xusb_deviceof(iface)), ret); |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
int main(int argc, char *argv[]) |
| 140 |
{ |
| 141 |
char *devpath = NULL; |
| 142 |
struct xusb_device *xusb_device; |
| 143 |
struct xtalk_base *xtalk_base; |
| 144 |
struct xtalk_sync *xtalk_sync; |
| 145 |
struct xusb_iface *iface; |
| 146 |
const char options[] = "vd:D:t:I:i:o:Q"; |
| 147 |
int query = 0; |
| 148 |
int ret; |
| 149 |
|
| 150 |
progname = argv[0]; |
| 151 |
while (1) { |
| 152 |
int c; |
| 153 |
|
| 154 |
c = getopt(argc, argv, options); |
| 155 |
if (c == -1) |
| 156 |
break; |
| 157 |
|
| 158 |
switch (c) { |
| 159 |
case 'D': |
| 160 |
devpath = optarg; |
| 161 |
break; |
| 162 |
case 'I': |
| 163 |
iface_num = strtoul(optarg, NULL, 0); |
| 164 |
break; |
| 165 |
case 't': |
| 166 |
timeout = strtoul(optarg, NULL, 0); |
| 167 |
break; |
| 168 |
case 'Q': |
| 169 |
query++; |
| 170 |
break; |
| 171 |
case 'v': |
| 172 |
verbose++; |
| 173 |
break; |
| 174 |
case 'd': |
| 175 |
debug_mask = strtoul(optarg, NULL, 0); |
| 176 |
break; |
| 177 |
case 'h': |
| 178 |
default: |
| 179 |
ERR("Unknown option '%c'\n", c); |
| 180 |
usage(); |
| 181 |
} |
| 182 |
} |
| 183 |
if (!devpath) { |
| 184 |
ERR("Missing device path\n"); |
| 185 |
usage(); |
| 186 |
} |
| 187 |
xusb_device = xusb_find_bypath(devpath); |
| 188 |
if (!xusb_device) { |
| 189 |
ERR("No XUSB device found\n"); |
| 190 |
return 1; |
| 191 |
} |
| 192 |
ret = xusb_claim(xusb_device, iface_num, &iface); |
| 193 |
if (ret < 0) { |
| 194 |
ERR("Claiming interface #%d failed (ret = %d)\n", iface_num, ret); |
| 195 |
return 1; |
| 196 |
} |
| 197 |
xusb_showinfo(xusb_deviceof(iface)); |
| 198 |
xtalk_base = xtalk_base_new_on_xusb(iface); |
| 199 |
if (!xtalk_base) { |
| 200 |
ERR("Failed creating the xtalk device abstraction\n"); |
| 201 |
return 1; |
| 202 |
} |
| 203 |
xtalk_sync = xtalk_sync_new(xtalk_base); |
| 204 |
if (!xtalk_sync) { |
| 205 |
ERR("Failed creating the xtalk device abstraction\n"); |
| 206 |
return 1; |
| 207 |
} |
| 208 |
if (query) { |
| 209 |
ret = show_protocol(xtalk_sync, iface); |
| 210 |
if (ret < 0) |
| 211 |
return 1; |
| 212 |
} |
| 213 |
ret = sendto_device(iface, argc - optind, argv + optind); |
| 214 |
if (ret < 0) |
| 215 |
ERR("Command failed: %d\n", ret); |
| 216 |
xtalk_sync_delete(xtalk_sync); |
| 217 |
xtalk_base_delete(xtalk_base); |
| 218 |
xusb_destroy(xusb_deviceof(iface)); |
| 219 |
return 0; |
| 220 |
} |