| 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 <xtalk/proto_raw.h> |
| 31 |
#include <autoconfig.h> |
| 32 |
#include "xtalk_base.h" |
| 33 |
|
| 34 |
#define DBG_MASK 0x02 |
| 35 |
|
| 36 |
/* |
| 37 |
* Base XTALK device. A pointer to this struct |
| 38 |
* should be included in the struct representing |
| 39 |
* the dialect. |
| 40 |
*/ |
| 41 |
struct xtalk_raw { |
| 42 |
struct xtalk_base *xtalk_base; |
| 43 |
}; |
| 44 |
|
| 45 |
CMD_DEF(XTALK, ACK, |
| 46 |
uint8_t stat; |
| 47 |
); |
| 48 |
|
| 49 |
union XTALK_PDATA(XTALK) { |
| 50 |
MEMBER(XTALK, ACK); |
| 51 |
} PACKED; |
| 52 |
|
| 53 |
const struct xtalk_protocol xtalk_raw_proto = { |
| 54 |
.name = "XTALK-RAW", |
| 55 |
.proto_version = 0, |
| 56 |
.commands = { |
| 57 |
CMD_RECV(XTALK, ACK), |
| 58 |
}, |
| 59 |
.ack_statuses = { |
| 60 |
ACK_STAT(OK, "Acknowledges previous command"), |
| 61 |
ACK_STAT(FAIL, "Last command failed"), |
| 62 |
ACK_STAT(RESET_FAIL, "reset failed"), |
| 63 |
ACK_STAT(NODEST, "No destination is selected"), |
| 64 |
ACK_STAT(MISMATCH, "Data mismatch"), |
| 65 |
ACK_STAT(NOACCESS, "No access"), |
| 66 |
ACK_STAT(BAD_CMD, "Bad command"), |
| 67 |
ACK_STAT(TOO_SHORT, "Packet is too short"), |
| 68 |
ACK_STAT(ERROFFS, "Offset error (not used)"), |
| 69 |
ACK_STAT(NO_LEEPROM, "Large EEPROM was not found"), |
| 70 |
ACK_STAT(NO_EEPROM, "No EEPROM was found"), |
| 71 |
ACK_STAT(WRITE_FAIL, "Writing to device failed"), |
| 72 |
ACK_STAT(NOPWR_ERR, "No power on USB connector"), |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
struct xtalk_raw *xtalk_raw_new(struct xtalk_base *xtalk_base) |
| 77 |
{ |
| 78 |
struct xtalk_raw *xtalk_raw; |
| 79 |
int ret; |
| 80 |
|
| 81 |
assert(xtalk_base); |
| 82 |
xtalk_raw = calloc(1, sizeof(*xtalk_raw)); |
| 83 |
if (!xtalk_raw) { |
| 84 |
ERR("Allocating XTALK device memory failed\n"); |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
xtalk_raw->xtalk_base = xtalk_base; |
| 88 |
ret = xtalk_set_protocol(xtalk_raw->xtalk_base, &xtalk_raw_proto, NULL); |
| 89 |
if (ret < 0) { |
| 90 |
ERR("GLOBAL Protocol registration failed: %d\n", ret); |
| 91 |
goto err; |
| 92 |
} |
| 93 |
DBG("%s: xtalk_raw=%p\n", __func__, xtalk_raw); |
| 94 |
return xtalk_raw; |
| 95 |
err: |
| 96 |
xtalk_raw_delete(xtalk_raw); |
| 97 |
return NULL; |
| 98 |
} |
| 99 |
|
| 100 |
void xtalk_raw_delete(struct xtalk_raw *xtalk_raw) |
| 101 |
{ |
| 102 |
if (xtalk_raw) { |
| 103 |
memset(xtalk_raw, 0, sizeof(*xtalk_raw)); |
| 104 |
free(xtalk_raw); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
int xtalk_raw_set_protocol(struct xtalk_raw *xtalk_raw, |
| 109 |
const struct xtalk_protocol *xproto) |
| 110 |
{ |
| 111 |
return xtalk_set_protocol(xtalk_raw->xtalk_base, &xtalk_raw_proto, xproto); |
| 112 |
} |
| 113 |
|
| 114 |
int xtalk_raw_cmd_send(struct xtalk_raw *xtalk_raw, const char *buf, int len, |
| 115 |
uint16_t *tx_seq) |
| 116 |
{ |
| 117 |
struct xtalk_command *cmd; |
| 118 |
char *p; |
| 119 |
int ret; |
| 120 |
|
| 121 |
p = malloc(len); |
| 122 |
if (!p) { |
| 123 |
ERR("allocation failed (%d bytes)\n", len); |
| 124 |
return -ENOMEM; |
| 125 |
} |
| 126 |
cmd = (struct xtalk_command *)p; |
| 127 |
memcpy(p, buf, len); |
| 128 |
cmd->header.len = len; |
| 129 |
|
| 130 |
ret = send_command(xtalk_raw->xtalk_base, cmd, tx_seq); |
| 131 |
if (ret < 0) { |
| 132 |
DBG("%s: send_command(%d bytes) failed ret=%d\n", |
| 133 |
__func__, len, ret); |
| 134 |
goto out; |
| 135 |
} |
| 136 |
DBG("%s(%d bytes, tx_seq=%d)\n", __func__, len, *tx_seq); |
| 137 |
out: |
| 138 |
free(p); |
| 139 |
return ret; |
| 140 |
} |
| 141 |
|
| 142 |
__attribute__((warn_unused_result)) |
| 143 |
int xtalk_raw_cmd_recv(struct xtalk_raw *xtalk_raw, |
| 144 |
struct xtalk_command **reply_ref) |
| 145 |
{ |
| 146 |
struct xtalk_base *xtalk_base; |
| 147 |
const struct xtalk_protocol *xproto; |
| 148 |
struct xtalk_command *reply = NULL; |
| 149 |
const struct xtalk_command_desc *reply_desc; |
| 150 |
const char *protoname; |
| 151 |
int ret; |
| 152 |
xtalk_cmd_callback_t callback; |
| 153 |
|
| 154 |
xtalk_base = xtalk_raw->xtalk_base; |
| 155 |
xproto = &xtalk_base->xproto; |
| 156 |
protoname = (xproto) ? xproto->name : "GLOBAL"; |
| 157 |
/* So the caller knows if a reply was received */ |
| 158 |
if (reply_ref) |
| 159 |
*reply_ref = NULL; |
| 160 |
ret = recv_command(xtalk_base, &reply); |
| 161 |
if (ret <= 0) { |
| 162 |
DBG("%s: failed (xproto = %s, ret = %d)\n", |
| 163 |
__func__, protoname, ret); |
| 164 |
goto err; |
| 165 |
} |
| 166 |
DBG("REPLY OP: 0x%X\n", reply->header.op); |
| 167 |
if (debug_mask & DBG_MASK) |
| 168 |
xtalk_dump_command(reply); |
| 169 |
/* reply_desc may be NULL (raw reply to application) */ |
| 170 |
reply_desc = get_command_desc(xproto, reply->header.op); |
| 171 |
if (reply->header.op == XTALK_ACK) { |
| 172 |
int status = CMD_FIELD(reply, XTALK, ACK, stat); |
| 173 |
|
| 174 |
if (status != STAT_OK) { |
| 175 |
ERR("Got NACK(seq=%d): %d %s\n", |
| 176 |
reply->header.seq, |
| 177 |
status, |
| 178 |
ack_status_msg(xproto, status)); |
| 179 |
} |
| 180 |
/* Good expected ACK ... */ |
| 181 |
} |
| 182 |
DBG("got reply seq=%d op=0x%X (%d bytes)\n", |
| 183 |
reply->header.seq, |
| 184 |
reply->header.op, |
| 185 |
ret); |
| 186 |
/* Find if there is associated callback */ |
| 187 |
ret = xtalk_cmd_callback(xtalk_base, reply->header.op, NULL, &callback); |
| 188 |
if (ret < 0) { |
| 189 |
ERR("Failed getting callback for op=0x%X\n", reply->header.op); |
| 190 |
goto err; |
| 191 |
} |
| 192 |
if (callback) { |
| 193 |
/* Override return value with callback return value */ |
| 194 |
ret = callback(xtalk_base, reply_desc, reply); |
| 195 |
DBG("%s: callback for 0x%X returned %d\n", __func__, |
| 196 |
reply->header.op, ret); |
| 197 |
} |
| 198 |
if (reply_ref) |
| 199 |
*reply_ref = reply; |
| 200 |
return 0; |
| 201 |
err: |
| 202 |
if (reply) |
| 203 |
free_command(reply); |
| 204 |
return ret; |
| 205 |
} |
| 206 |
|
| 207 |
int xtalk_raw_buffer_send(struct xtalk_raw *xraw, const char *buf, int len) |
| 208 |
{ |
| 209 |
dump_packet(LOG_DEBUG, DBG_MASK, __func__, buf, len); |
| 210 |
return send_buffer(xraw->xtalk_base, buf, len); |
| 211 |
} |
| 212 |
|
| 213 |
int xtalk_raw_buffer_recv(struct xtalk_raw *xraw, char *buf, int len) |
| 214 |
{ |
| 215 |
int ret; |
| 216 |
|
| 217 |
ret = recv_buffer(xraw->xtalk_base, buf, len); |
| 218 |
if (ret >= 0) |
| 219 |
dump_packet(LOG_DEBUG, DBG_MASK, __func__, buf, ret); |
| 220 |
return ret; |
| 221 |
} |