-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgbsim.h
337 lines (287 loc) · 13.1 KB
/
gbsim.h
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Greybus Simulator
*
* Copyright 2014 Google Inc.
* Copyright 2014 Linaro Ltd.
*
* Provided under the three clause BSD license found in the LICENSE file.
*/
/* Required for build, as greybus core uses __packed */
#ifndef __GBSIM_H
#define __GBSIM_H
#define __packed __attribute__((__packed__))
#include <endian.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/queue.h>
#include <stdint.h>
#include <linux/types.h>
#ifndef BIT
#define BIT(n) (1UL << (n))
#endif
#include "greybus_manifest.h"
#include "greybus_protocols.h"
/* Wouldn't support types larger than 4 bytes */
#define _ALIGNBYTES (sizeof(uint32_t) - 1)
#define ALIGN(p) ((typeof(p))(((unsigned)(p) + _ALIGNBYTES) & ~_ALIGNBYTES))
extern int bbb_backend;
extern int i2c_adapter;
extern int uart_portno;
extern int uart_count;
extern int verbose;
extern char *hotplug_basedir;
/* Matches up with the Greybus Protocol specification document */
#define GB_REQUEST_TYPE_PROTOCOL_VERSION 0x01
#define GREYBUS_VERSION_MAJOR 0x00
#define GREYBUS_VERSION_MINOR 0x01
/* Fixed values of AP's interface id and endo id */
#define ENDO_ID 0x4755
#define AP_INTF_ID 0x5
extern int control;
extern int to_ap;
extern int from_ap;
struct gbsim_connection {
TAILQ_ENTRY(gbsim_connection) cnode;
uint16_t cport_id;
uint16_t hd_cport_id;
int protocol;
struct gbsim_interface *intf;
};
/* CPorts */
#define PROTOCOL_STATUS_SUCCESS 0x00
#define PROTOCOL_STATUS_INVALID 0x01
#define PROTOCOL_STATUS_NOMEM 0x02
#define PROTOCOL_STATUS_BUSY 0x03
#define PROTOCOL_STATUS_RETRY 0x04
#define PROTOCOL_STATUS_BAD 0xff
/* Ops */
struct op_msg {
struct gb_operation_msg_hdr header;
union {
struct gb_control_version_response control_version_rsp;
struct gb_control_get_manifest_size_response control_msize_rsp;
struct gb_control_get_manifest_response control_manifest_rsp;
struct gb_control_bundle_pm_response control_bundle_pm_rsp;
struct gb_control_intf_pm_response control_intf_pm_rsp;
struct gb_svc_version_request svc_version_request;
struct gb_svc_version_response svc_version_response;
struct gb_svc_hello_request hello_request;
struct gb_svc_intf_device_id_request svc_intf_device_id_request;
struct gb_svc_conn_create_request svc_conn_create_request;
struct gb_svc_conn_destroy_request svc_conn_destroy_request;
struct gb_svc_module_inserted_request svc_module_inserted_request;
struct gb_svc_module_removed_request svc_module_removed_request;
struct gb_svc_intf_reset_request svc_intf_reset_request;
struct gb_svc_dme_peer_get_request svc_dme_peer_get_request;
struct gb_svc_dme_peer_get_response svc_dme_peer_get_response;
struct gb_svc_dme_peer_set_request svc_dme_peer_set_request;
struct gb_svc_dme_peer_set_response svc_dme_peer_set_response;
struct gb_svc_route_create_request svc_route_create_request;
struct gb_svc_route_destroy_request svc_route_destroy_request;
struct gb_svc_pwrmon_rail_count_get_response svc_pwrmon_rail_count_get_response;
struct gb_svc_intf_vsys_request svc_intf_vsys_request;
struct gb_svc_intf_vsys_response svc_intf_vsys_response;
struct gb_svc_intf_refclk_response svc_intf_refclk_response;
struct gb_svc_intf_unipro_response svc_intf_unipro_response;
struct gb_svc_intf_activate_response svc_intf_activate_response;
struct gb_svc_intf_resume_response svc_intf_resume_response;
struct gb_svc_intf_set_pwrm_response svc_intf_set_pwrm_response;
struct gb_gpio_line_count_response gpio_lc_rsp;
struct gb_gpio_activate_request gpio_act_req;
struct gb_gpio_deactivate_request gpio_deact_req;
struct gb_gpio_get_direction_request gpio_get_dir_req;
struct gb_gpio_get_direction_response gpio_get_dir_rsp;
struct gb_gpio_direction_in_request gpio_dir_input_req;
struct gb_gpio_direction_out_request gpio_dir_output_req;
struct gb_gpio_get_value_request gpio_get_val_req;
struct gb_gpio_get_value_response gpio_get_val_rsp;
struct gb_gpio_set_value_request gpio_set_val_req;
struct gb_gpio_set_debounce_request gpio_set_db_req;
struct gb_gpio_irq_type_request gpio_irq_type_req;
struct gb_gpio_irq_mask_request gpio_irq_mask_req;
struct gb_gpio_irq_unmask_request gpio_irq_unmask_req;
struct gb_gpio_irq_event_request gpio_irq_event_req;
struct gb_i2c_functionality_response i2c_fcn_rsp;
struct gb_i2c_transfer_request i2c_xfer_req;
struct gb_i2c_transfer_response i2c_xfer_rsp;
struct gb_pwm_count_response pwm_cnt_rsp;
struct gb_pwm_activate_request pwm_act_req;
struct gb_pwm_deactivate_request pwm_deact_req;
struct gb_pwm_config_request pwm_cfg_req;
struct gb_pwm_polarity_request pwm_pol_req;
struct gb_pwm_enable_request pwm_enb_req;
struct gb_pwm_disable_request pwm_dis_req;
struct gb_uart_send_data_request uart_send_data_req;
struct gb_uart_recv_data_request uart_recv_data_rsp;
struct gb_uart_set_break_request uart_sb_req;
struct gb_uart_serial_state_request uart_ss_resp;
struct gb_uart_set_line_coding_request uart_slc_req;
struct gb_uart_set_control_line_state_request uart_sls_req;
struct gb_sdio_get_caps_response sdio_caps_rsp;
struct gb_sdio_event_request sdio_event_req;
struct gb_sdio_command_request sdio_cmd_req;
struct gb_sdio_command_response sdio_cmd_rsp;
struct gb_sdio_transfer_request sdio_xfer_req;
struct gb_sdio_transfer_response sdio_xfer_rsp;
struct gb_loopback_transfer_request loopback_xfer_req;
struct gb_loopback_transfer_response loopback_xfer_resp;
struct gb_bootrom_version_response fw_version_response;
struct gb_bootrom_firmware_size_request fw_size_req;
struct gb_bootrom_firmware_size_response fw_size_resp;
struct gb_bootrom_get_firmware_request fw_get_firmware_req;
struct gb_bootrom_get_firmware_response fw_get_firmware_resp;
struct gb_bootrom_ready_to_boot_request fw_rbt_req;
struct gb_lights_blink_request lights_blink_req;
struct gb_lights_get_lights_response lights_gl_rsp;
struct gb_lights_event_request lights_gl_event_req;
struct gb_lights_set_brightness_request lights_glc_bright_req;
struct gb_lights_set_fade_request lights_glc_fade_req;
struct gb_lights_set_color_request lights_glc_color_req;
struct gb_lights_blink_request lights_glc_blink_req;
struct gb_lights_get_light_config_request lights_gl_conf_req;
struct gb_lights_get_light_config_response lights_gl_conf_rsp;
struct gb_lights_get_channel_config_request lights_glc_conf_req;
struct gb_lights_get_channel_config_response lights_glc_conf_rsp;
struct gb_lights_get_channel_flash_config_request lights_glc_fconf_req;
struct gb_lights_get_channel_flash_config_response lights_glc_fconf_rsp;
struct gb_lights_set_flash_intensity_request lights_glc_fint_req;
struct gb_lights_set_flash_strobe_request lights_glc_fstrobe_req;
struct gb_lights_set_flash_timeout_request lights_glc_ftimeout_req;
struct gb_lights_get_flash_fault_request lights_glc_ffault_req;
struct gb_lights_get_flash_fault_response lights_glc_ffault_rsp;
struct gb_power_supply_get_supplies_response psy_get_supplies_rsp;
struct gb_power_supply_get_description_request psy_get_desc_req;
struct gb_power_supply_get_description_response psy_get_desc_rsp;
struct gb_power_supply_get_property_descriptors_request psy_get_props_req;
struct gb_power_supply_get_property_descriptors_response psy_get_props_rsp;
struct gb_power_supply_get_property_request psy_get_prop_req;
struct gb_power_supply_get_property_response psy_get_prop_rsp;
struct gb_power_supply_set_property_request psy_set_prop_req;
struct gb_spi_master_config_response spi_mc_rsp;
struct gb_spi_device_config_request spi_dc_req;
struct gb_spi_device_config_response spi_dc_rsp;
struct gb_spi_transfer_request spi_xfer_req;
struct gb_spi_transfer_response spi_xfer_rsp;
struct gb_fw_download_find_firmware_request fw_download_find_req;
struct gb_fw_download_find_firmware_response fw_download_find_rsp;
struct gb_fw_download_fetch_firmware_request fw_download_fetch_req;
struct gb_fw_download_fetch_firmware_response fw_download_fetch_rsp;
struct gb_fw_download_release_firmware_request fw_download_release_req;
struct gb_fw_mgmt_interface_fw_version_response fw_mgmt_intf_fw_version_rsp;
struct gb_fw_mgmt_load_and_validate_fw_request fw_mgmt_load_validate_fw_req;
struct gb_fw_mgmt_loaded_fw_request fw_mgmt_loaded_fw_req;
struct gb_fw_mgmt_backend_fw_version_request fw_mgmt_backend_fw_ver_req;
struct gb_fw_mgmt_backend_fw_version_response fw_mgmt_backend_fw_ver_rsp;
struct gb_fw_mgmt_backend_fw_update_request fw_mgmt_backend_fw_update_req;
struct gb_fw_mgmt_backend_fw_updated_request fw_mgmt_backend_fw_updated_req;
};
};
#define OP_RESPONSE 0x80
/* debug/info/error macros */
#define gbsim_debug(fmt, ...) \
do { if (verbose) { fprintf(stdout, "[D] GBSIM: " fmt, \
##__VA_ARGS__); fflush(stdout); } } while (0)
#define gbsim_info(fmt, ...) \
do { fprintf(stdout, "[I] GBSIM: " fmt, ##__VA_ARGS__); fflush(stdout); } while (0)
#define gbsim_error(fmt, ...) \
do { fprintf(stderr, "[E] GBSIM: " fmt, ##__VA_ARGS__); fflush(stderr); } while (0)
static inline void gbsim_dump(void *data, size_t size)
{
uint8_t *buf = data;
int i;
fprintf(stdout, "[R] GBSIM: DUMP ->");
for (i = 0; i < size; i++)
fprintf(stdout, " %02hhx", buf[i]);
fprintf(stdout, "\n");
fflush(stdout);
}
static inline uint8_t cport_to_module_id(uint16_t cport_id)
{
/* FIXME can identify based on registered cport module */
return 1;
}
struct gbsim_connection *connection_find(uint16_t cport_id);
struct gbsim_connection *allocate_connection(struct gbsim_interface *intf,
uint16_t cport_id,
uint16_t hd_cport_id);
void connection_set_protocol(struct gbsim_connection *connection,
uint16_t cport_id);
uint16_t find_hd_cport_for_protocol(int protocol_id);
void free_connection(struct gbsim_connection *connections);
struct gbsim_interface {
TAILQ_ENTRY(gbsim_interface) intf_node;
uint8_t interface_id;
uint8_t features;
char *vendor_id;
char *product_id;
uint32_t serial_number;
void *manifest;
size_t manifest_size;
unsigned long manifest_fname_hash;
struct gbsim_connection *control_conn;
struct gbsim_svc *svc;
TAILQ_HEAD(chead, gbsim_connection) connections;
};
struct gbsim_svc {
struct gbsim_interface *intf;
TAILQ_HEAD(intf_head, gbsim_interface) intfs;
};
int inotify_start(struct gbsim_svc *svc, char *base_dir);
int svc_handler(struct gbsim_connection *, void *, size_t, void *, size_t);
int svc_request_send(uint8_t, uint8_t);
char *svc_get_operation(uint8_t type);
int svc_get_next_intf_id(struct gbsim_svc *svc);
int svc_init(void);
void svc_exit(void);
struct gbsim_interface *interface_alloc(struct gbsim_svc *svc, uint8_t id);
struct gbsim_interface *interface_get_by_id(struct gbsim_svc *svc, uint8_t id);
struct gbsim_interface *interface_get_by_hash(struct gbsim_svc *svc,
uint32_t hash);
void interface_free(struct gbsim_svc *svc, struct gbsim_interface *intf);
void *recv_thread(void *);
void recv_thread_cleanup(void *);
int control_handler(struct gbsim_connection *, void *, size_t, void *, size_t);
char *control_get_operation(uint8_t type);
int gpio_handler(struct gbsim_connection *, void *, size_t, void *, size_t);
char *gpio_get_operation(uint8_t type);
void gpio_init(void);
int i2c_handler(struct gbsim_connection *, void *, size_t, void *, size_t);
char *i2c_get_operation(uint8_t type);
void i2c_init(void);
int pwm_handler(struct gbsim_connection *, void *, size_t, void *, size_t);
char *pwm_get_operation(uint8_t type);
void pwm_init(void);
int sdio_handler(struct gbsim_connection *, void *, size_t, void *, size_t);
char *sdio_get_operation(uint8_t type);
void sdio_init(void);
int spi_handler(struct gbsim_connection *, void *, size_t, void *, size_t);
char *spi_get_operation(uint8_t type);
int lights_handler(struct gbsim_connection *, void *, size_t, void *, size_t);
char *lights_get_operation(uint8_t type);
int power_supply_handler(struct gbsim_connection *, void *, size_t, void *, size_t);
char *power_supply_get_operation(uint8_t type);
int uart_handler(struct gbsim_connection *, void *, size_t, void *, size_t);
char *uart_get_operation(uint8_t type);
void uart_init(void);
void uart_cleanup(void);
int loopback_handler(struct gbsim_connection *, void *, size_t, void *, size_t);
char *loopback_get_operation(uint8_t type);
void loopback_init(void);
void loopback_cleanup(void);
int bootrom_handler(struct gbsim_connection *, void *, size_t, void *, size_t);
char *bootrom_get_operation(uint8_t type);
int fw_mgmt_handler(struct gbsim_connection *, void *, size_t, void *, size_t);
char *fw_mgmt_get_operation(uint8_t type);
int fw_download_handler(struct gbsim_connection *, void *, size_t, void *, size_t);
char *fw_download_get_operation(uint8_t type);
int download_firmware(char *tag, uint16_t hd_cport_id, void (*func)(void));
bool manifest_parse(struct gbsim_svc *svc, int intf_id, void *data,
size_t size);
int cport_get_protocol(struct gbsim_interface *intf, uint16_t cport_id);
int send_response(uint16_t hd_cport_id,
struct op_msg *message, uint16_t message_size,
uint16_t operation_id, uint8_t type, uint8_t result);
int send_request(uint16_t hd_cport_id,
struct op_msg *message, uint16_t message_size,
uint16_t operation_id, uint8_t type);
#endif /* __GBSIM_H */