| 1 |
/* |
| 2 |
* Written by Oron Peled <[email protected]> |
| 3 |
* Copyright (C) 2009, 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 <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include <assert.h> |
| 27 |
#include <errno.h> |
| 28 |
#include <arpa/inet.h> |
| 29 |
#include <xtalk/debug.h> |
| 30 |
#include <autoconfig.h> |
| 31 |
#include "xtalk_base.h" |
| 32 |
|
| 33 |
#define DBG_MASK 0x02 |
| 34 |
|
| 35 |
void free_command(struct xtalk_command *cmd) |
| 36 |
{ |
| 37 |
if (!cmd) |
| 38 |
return; |
| 39 |
memset(cmd, 0, cmd->header.len); |
| 40 |
free(cmd); |
| 41 |
} |
| 42 |
|
| 43 |
const struct xtalk_command_desc *get_command_desc( |
| 44 |
const struct xtalk_protocol *xproto, uint8_t op) |
| 45 |
{ |
| 46 |
const struct xtalk_command_desc *desc; |
| 47 |
|
| 48 |
if (!xproto) |
| 49 |
return NULL; |
| 50 |
desc = &xproto->commands[op]; |
| 51 |
if (!desc->name) |
| 52 |
return NULL; |
| 53 |
#if 0 |
| 54 |
DBG("%s version=%d, op=0x%X (%s)\n", |
| 55 |
xproto->name, xproto->proto_version, |
| 56 |
op, desc->name); |
| 57 |
#endif |
| 58 |
return desc; |
| 59 |
} |
| 60 |
|
| 61 |
const char *ack_status_msg(const struct xtalk_protocol *xproto, |
| 62 |
uint8_t status) |
| 63 |
{ |
| 64 |
const char *ack_status; |
| 65 |
|
| 66 |
if (!xproto) |
| 67 |
return NULL; |
| 68 |
ack_status = xproto->ack_statuses[status]; |
| 69 |
DBG("%s status=0x%X (%s)\n", xproto->name, status, ack_status); |
| 70 |
return ack_status; |
| 71 |
} |
| 72 |
|
| 73 |
const char *xtalk_protocol_name(const struct xtalk_base *xtalk_base) |
| 74 |
{ |
| 75 |
const struct xtalk_protocol *xproto; |
| 76 |
|
| 77 |
xproto = &xtalk_base->xproto; |
| 78 |
return (xproto) ? xproto->name : ""; |
| 79 |
} |
| 80 |
|
| 81 |
int xtalk_set_protocol(struct xtalk_base *xtalk_base, |
| 82 |
const struct xtalk_protocol *xproto_base, |
| 83 |
const struct xtalk_protocol *xproto) |
| 84 |
{ |
| 85 |
const char *protoname = (xproto) ? xproto->name : "GLOBAL"; |
| 86 |
int i; |
| 87 |
|
| 88 |
DBG("%s\n", protoname); |
| 89 |
memset(&xtalk_base->xproto, 0, sizeof(xtalk_base->xproto)); |
| 90 |
for (i = 0; i < MAX_OPS; i++) { |
| 91 |
const struct xtalk_command_desc *desc; |
| 92 |
|
| 93 |
desc = get_command_desc(xproto, i); |
| 94 |
if (desc) { |
| 95 |
if (!IS_PRIVATE_OP(i)) { |
| 96 |
ERR("Bad op=0x%X " |
| 97 |
"(should be in the range [0x%X-0x%X]\n", |
| 98 |
i, PRIVATE_OP_FIRST, PRIVATE_OP_LAST); |
| 99 |
return -EINVAL; |
| 100 |
} |
| 101 |
xtalk_base->xproto.commands[i] = *desc; |
| 102 |
DBG("private: op=0x%X (%s)\n", i, desc->name); |
| 103 |
} else { |
| 104 |
if (!IS_PRIVATE_OP(i)) { |
| 105 |
const char *name; |
| 106 |
|
| 107 |
xtalk_base->xproto.commands[i] = |
| 108 |
xproto_base->commands[i]; |
| 109 |
name = xtalk_base->xproto.commands[i].name; |
| 110 |
if (name) |
| 111 |
DBG("global: op=0x%X (%s)\n", i, name); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
for (i = 0; i < MAX_STATUS; i++) { |
| 116 |
const char *stat_msg; |
| 117 |
|
| 118 |
stat_msg = (xproto) ? xproto->ack_statuses[i] : NULL; |
| 119 |
if (stat_msg) { |
| 120 |
xtalk_base->xproto.ack_statuses[i] = stat_msg; |
| 121 |
DBG("private: status=0x%X (%s)\n", i, stat_msg); |
| 122 |
} else { |
| 123 |
const char *global_msg; |
| 124 |
|
| 125 |
xtalk_base->xproto.ack_statuses[i] = |
| 126 |
xproto_base->ack_statuses[i]; |
| 127 |
global_msg = xtalk_base->xproto.ack_statuses[i]; |
| 128 |
if (global_msg) |
| 129 |
DBG("global: status=0x%X (%s)\n", |
| 130 |
i, global_msg); |
| 131 |
} |
| 132 |
} |
| 133 |
xtalk_base->xproto.name = protoname; |
| 134 |
xtalk_base->xproto.proto_version = (xproto) ? xproto->proto_version : 0; |
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
int xtalk_cmd_callback(struct xtalk_base *xtalk_base, int op, |
| 139 |
xtalk_cmd_callback_t callback, |
| 140 |
xtalk_cmd_callback_t *old_callback) |
| 141 |
{ |
| 142 |
const struct xtalk_protocol *xproto; |
| 143 |
const struct xtalk_command_desc *desc; |
| 144 |
|
| 145 |
xproto = &xtalk_base->xproto; |
| 146 |
desc = get_command_desc(xproto, op); |
| 147 |
if (!desc) |
| 148 |
DBG("Unknown op=0x%X.\n", op); |
| 149 |
if (old_callback) |
| 150 |
*old_callback = xtalk_base->callbacks[op]; |
| 151 |
if (callback) { |
| 152 |
xtalk_base->callbacks[op] = callback; |
| 153 |
DBG("OP=0x%X [%s] -- set callback to %p\n", |
| 154 |
op, |
| 155 |
(desc) ? desc->name : "", |
| 156 |
callback); |
| 157 |
} |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
struct xtalk_command *new_command( |
| 162 |
const struct xtalk_base *xtalk_base, |
| 163 |
uint8_t op, uint16_t extra_data) |
| 164 |
{ |
| 165 |
const struct xtalk_protocol *xproto; |
| 166 |
struct xtalk_command *cmd; |
| 167 |
const struct xtalk_command_desc *desc; |
| 168 |
uint16_t len; |
| 169 |
|
| 170 |
xproto = &xtalk_base->xproto; |
| 171 |
desc = get_command_desc(xproto, op); |
| 172 |
if (!desc) { |
| 173 |
ERR("Unknown op=0x%X.\n", op); |
| 174 |
return NULL; |
| 175 |
} |
| 176 |
DBG("OP=0x%X [%s] (extra_data %d)\n", op, desc->name, extra_data); |
| 177 |
len = desc->len + extra_data; |
| 178 |
cmd = malloc(len); |
| 179 |
if (!cmd) { |
| 180 |
ERR("Out of memory\n"); |
| 181 |
return NULL; |
| 182 |
} |
| 183 |
if (extra_data) { |
| 184 |
uint8_t *ptr = (uint8_t *)cmd; |
| 185 |
|
| 186 |
DBG("clear extra_data (%d bytes)\n", extra_data); |
| 187 |
memset(ptr + desc->len, 0, extra_data); |
| 188 |
} |
| 189 |
cmd->header.op = op; |
| 190 |
cmd->header.len = len; |
| 191 |
cmd->header.seq = 0; /* Overwritten in send_usb() */ |
| 192 |
return cmd; |
| 193 |
} |
| 194 |
|
| 195 |
void xtalk_dump_command(struct xtalk_command *cmd) |
| 196 |
{ |
| 197 |
uint16_t len; |
| 198 |
int i; |
| 199 |
|
| 200 |
len = cmd->header.len; |
| 201 |
if (len < sizeof(struct xtalk_header)) { |
| 202 |
ERR("Command too short (%d)\n", len); |
| 203 |
return; |
| 204 |
} |
| 205 |
INFO("DUMP: OP=0x%X len=%d seq=%d\n", |
| 206 |
cmd->header.op, cmd->header.len, cmd->header.seq); |
| 207 |
for (i = 0; i < len - sizeof(struct xtalk_header); i++) |
| 208 |
INFO(" %2d. 0x%X\n", i, cmd->alt.raw_data[i]); |
| 209 |
} |
| 210 |
|
| 211 |
int xtalk_set_timeout(struct xtalk_base *dev, int new_timeout) |
| 212 |
{ |
| 213 |
int old_timeout = dev->default_timeout; |
| 214 |
dev->default_timeout = new_timeout; |
| 215 |
return old_timeout; |
| 216 |
} |
| 217 |
|
| 218 |
int send_buffer(struct xtalk_base *xtalk_base, const char *buf, int len) |
| 219 |
{ |
| 220 |
int ret; |
| 221 |
void *priv = xtalk_base->transport_priv; |
| 222 |
int timeout = xtalk_base->default_timeout; |
| 223 |
|
| 224 |
ret = xtalk_base->ops.send_func(priv, buf, len, timeout); |
| 225 |
if (ret < 0) |
| 226 |
DBG("%s: failed ret=%d\n", __func__, ret); |
| 227 |
return ret; |
| 228 |
} |
| 229 |
|
| 230 |
int recv_buffer(struct xtalk_base *xtalk_base, char *buf, int len) |
| 231 |
{ |
| 232 |
void *priv = xtalk_base->transport_priv; |
| 233 |
int timeout = xtalk_base->default_timeout; |
| 234 |
int ret; |
| 235 |
|
| 236 |
ret = xtalk_base->ops.recv_func(priv, buf, len, timeout); |
| 237 |
if (ret < 0) { |
| 238 |
DBG("Receive from usb failed (ret=%d)\n", ret); |
| 239 |
goto out; |
| 240 |
} else if (ret == 0) { |
| 241 |
DBG("No reply\n"); |
| 242 |
goto out; /* No reply */ |
| 243 |
} |
| 244 |
DBG("Received %d bytes\n", ret); |
| 245 |
out: |
| 246 |
return ret; |
| 247 |
} |
| 248 |
|
| 249 |
int send_command(struct xtalk_base *xtalk_base, |
| 250 |
struct xtalk_command *cmd, uint16_t *tx_seq) |
| 251 |
{ |
| 252 |
int ret; |
| 253 |
int len; |
| 254 |
|
| 255 |
len = cmd->header.len; |
| 256 |
cmd->header.seq = xtalk_base->tx_sequenceno; |
| 257 |
ret = send_buffer(xtalk_base, (const char *)cmd, len); |
| 258 |
if (ret < 0) |
| 259 |
DBG("%s: failed ret=%d\n", __func__, ret); |
| 260 |
else if (tx_seq) |
| 261 |
*tx_seq = xtalk_base->tx_sequenceno++; |
| 262 |
return ret; |
| 263 |
} |
| 264 |
|
| 265 |
int recv_command(struct xtalk_base *xtalk_base, |
| 266 |
struct xtalk_command **reply_ref) |
| 267 |
{ |
| 268 |
struct xtalk_command *reply; |
| 269 |
size_t psize = xtalk_base->packet_size; |
| 270 |
int ret; |
| 271 |
|
| 272 |
reply = malloc(psize); |
| 273 |
if (!reply) { |
| 274 |
ERR("Out of memory\n"); |
| 275 |
ret = -ENOMEM; |
| 276 |
goto err; |
| 277 |
} |
| 278 |
reply->header.len = 0; |
| 279 |
ret = recv_buffer(xtalk_base, (char *)reply, psize); |
| 280 |
if (ret < 0) { |
| 281 |
DBG("%s: calling recv_buffer() failed (ret=%d)\n", __func__, ret); |
| 282 |
goto err; |
| 283 |
} else if (ret == 0) { |
| 284 |
goto err; /* No reply */ |
| 285 |
} |
| 286 |
if (ret != reply->header.len) { |
| 287 |
ERR("Wrong length received: got %d bytes, " |
| 288 |
"but length field says %d bytes%s\n", |
| 289 |
ret, reply->header.len, |
| 290 |
(ret == 1) ? ". Old USB firmware?" : ""); |
| 291 |
goto err; |
| 292 |
} |
| 293 |
/* dump_packet(LOG_DEBUG, DBG_MASK, __func__, (char *)reply, ret); */ |
| 294 |
*reply_ref = reply; |
| 295 |
return ret; |
| 296 |
err: |
| 297 |
if (reply) { |
| 298 |
memset(reply, 0, psize); |
| 299 |
free_command(reply); |
| 300 |
*reply_ref = NULL; |
| 301 |
} |
| 302 |
return ret; |
| 303 |
} |
| 304 |
|
| 305 |
/* |
| 306 |
* Wrappers |
| 307 |
*/ |
| 308 |
|
| 309 |
struct xtalk_base *xtalk_base_new(const struct xtalk_ops *ops, |
| 310 |
size_t packet_size, void *priv) |
| 311 |
{ |
| 312 |
struct xtalk_base *xtalk_base; |
| 313 |
|
| 314 |
DBG("\n"); |
| 315 |
assert(ops != NULL); |
| 316 |
xtalk_base = calloc(1, sizeof(*xtalk_base)); |
| 317 |
if (!xtalk_base) { |
| 318 |
ERR("Allocating XTALK device memory failed\n"); |
| 319 |
return NULL; |
| 320 |
} |
| 321 |
memcpy((void *)&xtalk_base->ops, (const void *)ops, |
| 322 |
sizeof(xtalk_base->ops)); |
| 323 |
xtalk_base->packet_size = packet_size; |
| 324 |
xtalk_base->transport_priv = priv; |
| 325 |
xtalk_base->tx_sequenceno = 1; |
| 326 |
xtalk_base->default_timeout = 2000; /* millies */ |
| 327 |
return xtalk_base; |
| 328 |
} |
| 329 |
|
| 330 |
void xtalk_base_delete(struct xtalk_base *xtalk_base) |
| 331 |
{ |
| 332 |
void *priv; |
| 333 |
|
| 334 |
if (!xtalk_base) |
| 335 |
return; |
| 336 |
DBG("\n"); |
| 337 |
priv = xtalk_base->transport_priv; |
| 338 |
assert(priv); |
| 339 |
xtalk_base->tx_sequenceno = 0; |
| 340 |
assert(&xtalk_base->ops != NULL); |
| 341 |
assert(&xtalk_base->ops.close_func != NULL); |
| 342 |
xtalk_base->ops.close_func(priv); |
| 343 |
memset(xtalk_base, 0, sizeof(*xtalk_base)); |
| 344 |
free(xtalk_base); |
| 345 |
} |
| 346 |
|
| 347 |
struct xusb_iface *xusb_iface_of_xtalk_base(const struct xtalk_base *xtalk_base) |
| 348 |
{ |
| 349 |
return xtalk_base->transport_priv; |
| 350 |
} |