summaryrefslogtreecommitdiff
path: root/xpp/xtalk/xtalk.h
blob: 88f0260c3008fb536fa27e2ff82a9a8f8d0fa023 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#ifndef	XTALK_H
#define	XTALK_H
/*
 * Written by Oron Peled <oron@actcom.co.il>
 * Copyright (C) 2009, Xorcom
 *
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

/*
 * XTALK - Base protocol for our USB devices
 *         It is meant to provide a common base for layered
 *         protocols (dialects)
 */

#include <stdint.h>
#include <stdlib.h>
/* Definitions common to the firmware (in include/ directory) */
#include <xtalk_defs.h>

#ifdef	__GNUC__
#define	PACKED	__attribute__((packed))
#else
#error "We do not know how your compiler packs structures"
#endif

struct xtalk_device;
struct xtalk_command_desc;

typedef int (*xtalk_cmd_callback_t)(
	struct xtalk_device *xtalk_dev,
	struct xtalk_command_desc *xtalk_cmd);

/* Describe a single xtalk command */
struct xtalk_command_desc {
	uint8_t			op;
	const char		*name;
	xtalk_cmd_callback_t	callback;
	uint16_t		len;	/* Minimal length */
};

/* Define a complete protocol */
struct xtalk_protocol {
	const char			*name;
	uint8_t				proto_version;
	struct xtalk_command_desc	commands[MAX_OPS];
	const char			*ack_statuses[MAX_STATUS];
};

/*
 * The common header of every xtalk command
 * in every xtalk dialect.
 */
struct xtalk_header {
	uint16_t	len;
	uint16_t	seq;
	uint8_t		op;	/* MSB: 0 - to device, 1 - from device */
} PACKED;

struct xtalk_command {
	/* Common part */
	struct xtalk_header	header;
	/* Each dialect has its own data members */
	union private_data {
		uint8_t	raw_data[0];
	} PACKED alt;
} PACKED;

/*
 * Macros to unify access to protocol packets and fields:
 *   p		- signify the dialect prefix (XTALK for base protocol)
 *   o		- signify command op (e.g: ACK)
 *   cmd	- A pointer to struct xtalk_command
 *   field	- field name (e.g: raw_data)
 */
#define XTALK_STRUCT(p, o)	p ## _struct_ ## o
#define XTALK_PDATA(o)		xtalk_privdata_ ## o
#define	XTALK_CMD_PTR(cmd, p)	((union XTALK_PDATA(p)*)&((cmd)->alt))
#define	CMD_FIELD(cmd, p, o, field) \
		(XTALK_CMD_PTR(cmd, p)->XTALK_STRUCT(p, o).field)
#define CMD_DEF(p, o, ...)	struct XTALK_STRUCT(p, o) {	\
					__VA_ARGS__		\
				} PACKED XTALK_STRUCT(p, o)
#define	MEMBER(p, o)	struct XTALK_STRUCT(p, o) XTALK_STRUCT(p, o)

/* Wrappers for transport (xusb) functions */
struct xtalk_ops {
	int	(*send_func)(void *transport_priv, void *data, size_t len,
			int timeout);
	int	(*recv_func)(void *transport_priv, void *data, size_t maxlen,
			int timeout);
	int	(*close_func)(void *transport_priv);
};

/*
 * Base XTALK device. A pointer to this struct
 * should be included in the struct representing
 * the dialect.
 */
struct xtalk_device;

/* high-level */
struct xtalk_device *xtalk_new(const struct xtalk_ops *ops,
		size_t packet_size, void *transport_priv);
void xtalk_delete(struct xtalk_device *dev);
int xtalk_set_protocol(struct xtalk_device *xtalk_dev,
		const struct xtalk_protocol *xproto);
int xtalk_proto_query(struct xtalk_device *dev);
void xtalk_dump_command(struct xtalk_command *cmd);

/* low-level */
int process_command(
	struct xtalk_device *dev,
	struct xtalk_command *cmd,
	struct xtalk_command **reply_ref);
struct xtalk_command *new_command(
	const struct xtalk_device *xtalk_dev,
	uint8_t op, uint16_t extra_data);
void free_command(struct xtalk_command *cmd);

/*
 * Convenience macros to define entries in a protocol command table:
 *   p		- signify the dialect prefix (XTALK for base protocol)
 *   o		- signify command op (e.g: ACK)
 *   cb		- A callback function (type xtalk_cmd_callback_t)
 */
#define	CMD_RECV(p, o, cb)	\
	[p ## _ ## o | XTALK_REPLY_MASK] = {		\
		.op = (p ## _ ## o) | XTALK_REPLY_MASK,	\
		.name = (#o "_reply"),			\
		.callback = (cb),			\
		.len =					\
			sizeof(struct xtalk_header) +	\
			sizeof(struct XTALK_STRUCT(p, o)),	\
	}

#define	CMD_SEND(p, o)	\
	[p ## _ ## o] = {		\
		.op = (p ## _ ## o),	\
		.name = (#o),		\
		.callback = NULL,	\
		.len =					\
			sizeof(struct xtalk_header) +	\
			sizeof(struct XTALK_STRUCT(p, o)),	\
	}

/*
 * Convenience macro to define statuses:
 *   x		- status code (e.g: OK)
 *   m		- status message (const char *)
 */
#define	ACK_STAT(x, m)	[STAT_ ## x] = (m)

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif	/* XTALK_H */