aboutsummaryrefslogtreecommitdiffstats
path: root/linux/1394ethbridge.c
blob: c7fffcff5cd5ef6140b0263e54925439daf1dcaf (plain) (blame)
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
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <poll.h>
#include <libraw1394/raw1394.h>

enum {
	SERVER_FD = 0,
	IEEE_FD = 1,
	CLIENT_FD = 2,
	DESCRIPTORS = 3
};

static struct msg_state client_state;
static raw1394handle_t ieee_handle;
static struct pollfd pollfds[DESCRIPTORS] = {{0}};

static void verr(char *fmt, va_list va) {
	int n = 0;
	if (fmt)
		n = vfprintf(stderr, fmt, va);

	if (errno)
		fprintf(stderr, "%s%s\n", n ? ": " : "", strerror(errno));
	else
		fprintf(stderr, "\n");
	exit(1);
}

static void err(char *fmt, ...) {
	va_list va;
	va_start(va, fmt);
	verr(fmt, va);
	va_end(va);
}

static void server_accept(void) {
	int fd = accept(pollfds[SERVER_FD], NULL, NULL);
	if (pollfds[CLIENT_FD].fd >= 0) {
		close(fd);
		return;
	}

	if (fd < 0) {
		if (errno != ECONNABORTED)
			err("accept");
		else
			return;
	}

	pollfds[CLIENT_FD].fd = fd;
	client_state.state = PARSE_VERSION_MESSAGE;
	client_state.type = MESSAGE_INVALID;
	if (client_state.buf) {
		sdsclear(client_state.buf);
	} else {
		client_state.buf = sdsempty();
	}
}

static void iterate(void) {
	if (raw1394_loop_iterate(ieee_handle) < 0)
		err("raw1394_loop_iterate");
}

/* 0b111111 = 0x3F
 * if tags[x] < 0, then tag is not allocated
 */
#define TAG_NUM 0x40
static long tag_to_handle[TAG_NUM];

static void handle_send_async(union msg *msg) {
	switch (msg->send_async.tcode) {
	case TCODE_WRITE_QUADLET_REQUEST:
	case TCODE_WRITE_BLOCK_REQUEST:
		raw1394_start_write(ieee_handle, msg->send_async.node,
		                        msg->send_async.addr, msg->send_async.len,
		                        msg->send_async.buf, msg->send_async.tlabel);
		break;
	case TCODE_READ_QUADLET_REQUEST:
	case TCODE_READ_BLOCK_REQUEST:
		raw1394_start_read(ieee_handle, msg->send_async.node,
		                       msg->send_async.addr, msg->send_async.len,
		                       msg->send_async.buf, msg->send_async.tlabel);
		break;
	case TCODE_WRITE_RESONSE: case TCODE_READ_QUADLET_RESPONSE:
	case TCODE_READ_BLOCK_RESPONSE:
		if (msg->send_async.tlabel < TAG_NUM &&
		    tags_to_handle[msg->send_async.tlabel] >= 0) {
			raw1394_send_rw_response(ieee_handle, msg->send_async.tcode,
		                             msg->send_async.buf, msg->send_async.len,
		                             tags_to_handle[msg->send_async.tlabel]);
			tags_to_handle[msg->send_async.tlabel] = 0;
		}
		break;
	/* FIXME: implement lock request */
	}
}

static void handle_send_iso(union msg *msg) {
	/* do nothing */
}

static int arm_handler(raw1394handle_t handle, raw1394_arm_allocation_t *arm,
                        int tcode, unsigned long long offset, int source_node_id,
                        int card, unsigned kernel_handle, size_t length, void *data)
{
	(void)handle;
	uint8_t tag;

	/* Allocate tags.
	 * Tags are temporarily allocated since the real tag is stored in the
	 * kernel in the kernel_handle.
	 *
	 * firewire-cdev: bus is only 1023?
	 */
	for (tag = 0; tag < MAX_TAG; tag++) {
		if (tag_to_handle[tag] < 0)
			break;
	}
	if (tag == MAX_TAG)
		return -1;

	tag_to_handle[tag] = kernel_handle;
	if (msg_send_async(pollfds[SERVER_FD].fd, 1023, source_node_id, offset,
	               tcode, tag, length, data) < 0)
		tag_to_handle[tag] = -1;
	/* TODO: return an error response */
	return 0;
}

static void handle_allocation(union msg *msg) {
	/* may fail due to overlap: ignore */
	raw1394_arm_register(ieee_handle, msg->allocate.start, msg->allocate.length,
	                     ARM_READ | ARM_WRITE | ARM_LOCK,
	                     ARM_READ | ARM_WRITE | ARM_LOCK,
	                     0 /* ignored */);
}

static bool read_client(void) {
	char buf[512];
	ssize_t len;
	int msgtype;

	len = read(pollfds[CLIENT_FD].fd, buf, sizeof(buf));
	if (len <= 0)
		return false;

	client_state.buf = sdscatlen(client_state.buf, buf, len);
	while (!parse_packet(fdstate));

	return true;
}

static bool client_process(void) {
	if (fd->revents & POLLERR)
		return false;

	if (fd->revents & POLLIN)
		if (!read_client(fd, fdstate))
			return false;

	if (!(fd->revents & POLLIN) && fd->revents & POLLHUP)
		return false;
	return true;
}

static int open_server(const char *port) {
	int r;
	struct addrinfo *serv;
	int sock;

	if ((r = getaddrinfo(NULL, port, &(struct addrinfo){
			.ai_family = AF_INET,
			.ai_socktype = SOCK_STREAM,
			.ai_flags = AI_PASSIVE
		}, &serv) != 0) {
		err("getaddrinfo: %s", gai_strerror(r));
	}

	for (struct addrinfo *p = serv; p; p = p->ai_next) {
		sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
		if (sock < 0)
			continue;
		if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR) < 0)
			continue;
		if (bind(sock, p->ai_addr, p->ai_addrlen) < 0)
			continue;
		break;
	}
	if (!p)
		err("socket");

	freeaddrinfo(serv);

	if (listen(p, 5) < 0)
		err("listen");

	return sock;
}

static int setup_port(int port) {
	int num;
	int fd;
	static struct raw1394_portinfo ports[PORTSNUM] = {0};

	ieee_handle = raw1394_new_handle();
	if (!ieee_handle)
		err("raw1394_new_handle");

	num = raw1394_get_port_info(ieee_handle, ports, PORTSNUM);

	do {
		for (int i = 0; i < num && i < PORTSNUM; i++)
			fprintf(stderr, "[%d/%d]: %s w/ %d\n", i + 1, num,
			        ports[i].name, ports[i].nodes);
		errno = 0;
		raw1394_set_port(ieee_handle, port);
	} while (errno != ESTALE);

	if (errno)
		die("raw1394_set_port");

	fd = raw1394_get_fd(ieee_handle);
	if (fd < 0)
		die("raw1394_get_fd");

	return fd;
}

int main(void) {
	pollfds[IEEE_FD].fd = setup_port(0);
	pollfds[IEEE_FD].events = POLLIN | POLLPRI;
	pollfds[SERVER_FD].fd = open_server("1394");
	pollfds[SERVER_FD].events = POLLIN | POLLPRI;
	pollfds[CLIENT_FD].fd = -1;

	for (int i = 0; i < TAG_NUM; i++)
		tag_to_handle[i] = -1;

	client_state.on_read[PARSE_SEND_ASYNC] = ieee_process;

	while (1) {
		if (poll(pollfds, DESCRIPTORS, -1) <= 0)
			err("poll");

		if (pollfds[SERVER_FD].revents & (POLLIN | POLLPRI)) {
			server_accept();
		} else if (pollfds[IEEE_FD].revents & (POLLIN | POLLPRI)) {
			ieee_process();
		}
		if (!client_process()) {
			close(pollfds[CLIENT_FD].fd);
			pollfds[CLIENT_FD].fd = -1;

			// Reset Firewire
			raw1394_destroy_handle(ieee_handle);
			pollfds[IEEE_FD].fd = setup_port(0);
		}
	}
}