]> sigrok.org Git - libsigrok.git/blame - src/hardware/ipdbg-la/protocol.c
Backport recent changes from mainline.
[libsigrok.git] / src / hardware / ipdbg-la / protocol.c
CommitLineData
8cd15dd4
UH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2016 Eva Kissling <eva.kissling@bluewin.ch>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <config.h>
21
22#ifdef _WIN32
23#define _WIN32_WINNT 0x0501
24#include <winsock2.h>
25#include <ws2tcpip.h>
26#else
27#include <sys/socket.h>
28#include <netinet/in.h>
29#include <arpa/inet.h>
30#include <netdb.h>
31#include <sys/ioctl.h>
32#endif
33#include <string.h>
34#include <unistd.h>
35#include <errno.h>
36#include "protocol.h"
37
38#define BUFFER_SIZE 4
39
40/* Top-level command opcodes */
41#define CMD_SET_TRIGGER 0x00
42#define CMD_CFG_TRIGGER 0xF0
43#define CMD_CFG_LA 0x0F
44#define CMD_START 0xFE
45#define CMD_RESET 0xEE
46
47#define CMD_GET_BUS_WIDTHS 0xAA
48#define CMD_GET_LA_ID 0xBB
49#define CMD_ESCAPE 0x55
50
51/* Trigger subfunction command opcodes */
52#define CMD_TRIG_MASKS 0xF1
53#define CMD_TRIG_MASK 0xF3
54#define CMD_TRIG_VALUE 0xF7
55
56#define CMD_TRIG_MASKS_LAST 0xF9
57#define CMD_TRIG_MASK_LAST 0xFB
58#define CMD_TRIG_VALUE_LAST 0xFF
59
60#define CMD_TRIG_SELECT_EDGE_MASK 0xF5
61#define CMD_TRIG_SET_EDGE_MASK 0xF6
62
63/* LA subfunction command opcodes */
64#define CMD_LA_DELAY 0x1F
65
66static gboolean data_available(struct ipdbg_la_tcp *tcp)
67{
68#ifdef _WIN32
69 u_long bytes_available;
e4204b17 70 if (ioctlsocket(tcp->socket, FIONREAD, &bytes_available) != 0) {
8cd15dd4 71#else
e4204b17
UH
72 int bytes_available;
73 if (ioctl(tcp->socket, FIONREAD, &bytes_available) < 0) { /* TIOCMGET */
74#endif
8cd15dd4
UH
75 sr_err("FIONREAD failed: %s\n", g_strerror(errno));
76 return FALSE;
77 }
e4204b17 78 return (bytes_available > 0);
8cd15dd4
UH
79}
80
81SR_PRIV struct ipdbg_la_tcp *ipdbg_la_tcp_new(void)
82{
83 struct ipdbg_la_tcp *tcp;
84
85 tcp = g_malloc0(sizeof(struct ipdbg_la_tcp));
86 tcp->address = NULL;
87 tcp->port = NULL;
88 tcp->socket = -1;
89
90 return tcp;
91}
92
93SR_PRIV void ipdbg_la_tcp_free(struct ipdbg_la_tcp *tcp)
94{
95 g_free(tcp->address);
96 g_free(tcp->port);
97}
98
99SR_PRIV int ipdbg_la_tcp_open(struct ipdbg_la_tcp *tcp)
100{
101 struct addrinfo hints;
102 struct addrinfo *results, *res;
103 int err;
104
105 memset(&hints, 0, sizeof(hints));
106 hints.ai_family = AF_UNSPEC;
107 hints.ai_socktype = SOCK_STREAM;
108 hints.ai_protocol = IPPROTO_TCP;
109
110 err = getaddrinfo(tcp->address, tcp->port, &hints, &results);
111
112 if (err) {
113 sr_err("Address lookup failed: %s:%s: %s", tcp->address,
114 tcp->port, gai_strerror(err));
115 return SR_ERR;
116 }
117
118 for (res = results; res; res = res->ai_next) {
119 if ((tcp->socket = socket(res->ai_family, res->ai_socktype,
120 res->ai_protocol)) < 0)
121 continue;
122 if (connect(tcp->socket, res->ai_addr, res->ai_addrlen) != 0) {
123 close(tcp->socket);
124 tcp->socket = -1;
125 continue;
126 }
127 break;
128 }
129
130 freeaddrinfo(results);
131
132 if (tcp->socket < 0) {
133 sr_err("Failed to connect to %s:%s: %s", tcp->address, tcp->port,
134 g_strerror(errno));
135 return SR_ERR;
136 }
137
138 return SR_OK;
139}
140
141SR_PRIV int ipdbg_la_tcp_close(struct ipdbg_la_tcp *tcp)
142{
143 int ret = SR_OK;
144
e4204b17
UH
145#ifdef _WIN32
146 if (shutdown(tcp->socket, SD_SEND) != SOCKET_ERROR) {
147 char recvbuf[16];
148 int recvbuflen = 16;
149 /* Receive until the peer closes the connection. */
150 while (recv(tcp->socket, recvbuf, recvbuflen, 0) > 0);
151 }
152#endif
8cd15dd4
UH
153 if (close(tcp->socket) < 0)
154 ret = SR_ERR;
155
156 tcp->socket = -1;
157
158 return ret;
159}
160
161static int tcp_send(struct ipdbg_la_tcp *tcp, const uint8_t *buf, size_t len)
162{
163 int out;
164 out = send(tcp->socket, (const char *)buf, len, 0);
165
166 if (out < 0) {
167 sr_err("Send error: %s", g_strerror(errno));
168 return SR_ERR;
169 }
170
171 if (out < (int)len)
172 sr_dbg("Only sent %d/%d bytes of data.", out, (int)len);
173
174 return SR_OK;
175}
176
177static int tcp_receive_blocking(struct ipdbg_la_tcp *tcp,
178 uint8_t *buf, int bufsize)
179{
180 int received = 0;
181 int error_count = 0;
182
e4204b17
UH
183 /* Timeout after 2s of not receiving data. */
184 /* Increase timeout in case lab is not just beside the office. */
185 while ((received < bufsize) && (error_count < 2000)) {
186 int recd = ipdbg_la_tcp_receive(tcp, buf, bufsize - received);
187 if (recd > 0) {
188 buf += recd;
189 received += recd;
8cd15dd4
UH
190 } else {
191 error_count++;
e4204b17 192 g_usleep(1000);
8cd15dd4
UH
193 }
194 }
195
196 return received;
197}
198
199SR_PRIV int ipdbg_la_tcp_receive(struct ipdbg_la_tcp *tcp,
e4204b17 200 uint8_t *buf, size_t bufsize)
8cd15dd4
UH
201{
202 int received = 0;
e4204b17
UH
203 if (data_available(tcp))
204 received = recv(tcp->socket, (char *)buf, bufsize, 0);
205 if (received < 0) {
206 sr_err("Receive error: %s", g_strerror(errno));
8cd15dd4 207 return -1;
e4204b17
UH
208 } else
209 return received;
8cd15dd4
UH
210}
211
212SR_PRIV int ipdbg_la_convert_trigger(const struct sr_dev_inst *sdi)
213{
214 struct dev_context *devc;
215 struct sr_trigger *trigger;
216 struct sr_trigger_stage *stage;
217 struct sr_trigger_match *match;
218 const GSList *l, *m;
219
220 devc = sdi->priv;
221
222 devc->num_stages = 0;
223 devc->num_transfers = 0;
224 devc->raw_sample_buf = NULL;
225
226 for (uint64_t i = 0; i < devc->data_width_bytes; i++) {
227 devc->trigger_mask[i] = 0;
228 devc->trigger_value[i] = 0;
229 devc->trigger_mask_last[i] = 0;
230 devc->trigger_value_last[i] = 0;
231 devc->trigger_edge_mask[i] = 0;
232 }
233
234 if (!(trigger = sr_session_trigger_get(sdi->session)))
235 return SR_OK;
236
237 for (l = trigger->stages; l; l = l->next) {
238 stage = l->data;
239 for (m = stage->matches; m; m = m->next) {
240 match = m->data;
241 int byte_idx = match->channel->index / 8;
242 uint8_t match_bit = 1 << (match->channel->index % 8);
243
244 if (!match->channel->enabled)
245 /* Ignore disabled channels with a trigger. */
246 continue;
247
248 if (match->match == SR_TRIGGER_ONE) {
249 devc->trigger_value[byte_idx] |= match_bit;
250 devc->trigger_mask[byte_idx] |= match_bit;
251 devc->trigger_mask_last[byte_idx] &= ~match_bit;
252 devc->trigger_edge_mask[byte_idx] &= ~match_bit;
253 } else if (match->match == SR_TRIGGER_ZERO) {
254 devc->trigger_value[byte_idx] &= ~match_bit;
255 devc->trigger_mask[byte_idx] |= match_bit;
256 devc->trigger_mask_last[byte_idx] &= ~match_bit;
257 devc->trigger_edge_mask[byte_idx] &= ~match_bit;
258 } else if (match->match == SR_TRIGGER_RISING) {
259 devc->trigger_value[byte_idx] |= match_bit;
260 devc->trigger_value_last[byte_idx] &=
261 ~match_bit;
262 devc->trigger_mask[byte_idx] |= match_bit;
263 devc->trigger_mask_last[byte_idx] |= match_bit;
264 devc->trigger_edge_mask[byte_idx] &= ~match_bit;
265 } else if (match->match == SR_TRIGGER_FALLING) {
266 devc->trigger_value[byte_idx] &= ~match_bit;
267 devc->trigger_value_last[byte_idx] |= match_bit;
268 devc->trigger_mask[byte_idx] |= match_bit;
269 devc->trigger_mask_last[byte_idx] |= match_bit;
270 devc->trigger_edge_mask[byte_idx] &= ~match_bit;
271 } else if (match->match == SR_TRIGGER_EDGE) {
272 devc->trigger_mask[byte_idx] &= ~match_bit;
273 devc->trigger_mask_last[byte_idx] &= ~match_bit;
274 devc->trigger_edge_mask[byte_idx] |= match_bit;
275 }
276 }
277 }
278
279 return SR_OK;
280}
281
282SR_PRIV int ipdbg_la_receive_data(int fd, int revents, void *cb_data)
283{
284 const struct sr_dev_inst *sdi;
285 struct dev_context *devc;
286
287 (void)fd;
288 (void)revents;
289
290 sdi = cb_data;
291 if (!sdi)
292 return FALSE;
293
294 if (!(devc = sdi->priv))
295 return FALSE;
296
297 struct ipdbg_la_tcp *tcp = sdi->conn;
298 struct sr_datafeed_packet packet;
299 struct sr_datafeed_logic logic;
300
301 if (!devc->raw_sample_buf) {
302 devc->raw_sample_buf =
303 g_try_malloc(devc->limit_samples * devc->data_width_bytes);
304 if (!devc->raw_sample_buf) {
305 sr_err("Sample buffer malloc failed.");
306 return FALSE;
307 }
308 }
309
310 if (devc->num_transfers <
311 (devc->limit_samples_max * devc->data_width_bytes)) {
e4204b17
UH
312 const size_t bufsize = 1024;
313 uint8_t buffer[bufsize];
314
315 const int recd = ipdbg_la_tcp_receive(tcp, buffer, bufsize);
316 if ( recd > 0) {
317 int num_move = (((devc->num_transfers + recd) <=
318 (devc->limit_samples * devc->data_width_bytes))
319 ?
320 recd
321 :
322 (int)((devc->limit_samples * devc->data_width_bytes) -
323 devc->num_transfers));
324 if ( num_move > 0 )
325 memcpy(&(devc->raw_sample_buf[devc->num_transfers]),
326 buffer, num_move);
327 devc->num_transfers += recd;
8cd15dd4
UH
328 }
329 } else {
330 if (devc->delay_value > 0) {
331 /* There are pre-trigger samples, send those first. */
332 packet.type = SR_DF_LOGIC;
333 packet.payload = &logic;
334 logic.length = devc->delay_value * devc->data_width_bytes;
335 logic.unitsize = devc->data_width_bytes;
336 logic.data = devc->raw_sample_buf;
337 sr_session_send(cb_data, &packet);
338 }
339
340 /* Send the trigger. */
341 packet.type = SR_DF_TRIGGER;
342 sr_session_send(cb_data, &packet);
343
344 /* Send post-trigger samples. */
345 packet.type = SR_DF_LOGIC;
346 packet.payload = &logic;
347 logic.length = (devc->limit_samples - devc->delay_value) *
348 devc->data_width_bytes;
349 logic.unitsize = devc->data_width_bytes;
350 logic.data = devc->raw_sample_buf +
351 (devc->delay_value * devc->data_width_bytes);
352 sr_session_send(cb_data, &packet);
353
354 g_free(devc->raw_sample_buf);
355 devc->raw_sample_buf = NULL;
356
357 ipdbg_la_abort_acquisition(sdi);
358 }
359
360 return TRUE;
361}
362
363static int send_escaping(struct ipdbg_la_tcp *tcp, uint8_t *data_to_send,
364 uint32_t length)
365{
366 uint8_t escape = CMD_ESCAPE;
367
368 while (length--) {
369 uint8_t payload = *data_to_send++;
370
371 if (payload == CMD_RESET)
372 if (tcp_send(tcp, &escape, 1) != SR_OK)
373 sr_warn("Couldn't send escape");
374
375 if (payload == CMD_ESCAPE)
376 if (tcp_send(tcp, &escape, 1) != SR_OK)
377 sr_warn("Couldn't send escape");
378
379 if (tcp_send(tcp, &payload, 1) != SR_OK)
380 sr_warn("Couldn't send data");
381 }
382
383 return SR_OK;
384}
385
386SR_PRIV int ipdbg_la_send_delay(struct dev_context *devc,
387 struct ipdbg_la_tcp *tcp)
388{
e4204b17 389 devc->delay_value = ((devc->limit_samples - 1) / 100.0) * devc->capture_ratio;
8cd15dd4
UH
390
391 uint8_t buf;
392 buf = CMD_CFG_LA;
393 tcp_send(tcp, &buf, 1);
394 buf = CMD_LA_DELAY;
395 tcp_send(tcp, &buf, 1);
396
397 uint8_t delay_buf[4] = { devc->delay_value & 0x000000ff,
398 (devc->delay_value >> 8) & 0x000000ff,
399 (devc->delay_value >> 16) & 0x000000ff,
400 (devc->delay_value >> 24) & 0x000000ff
401 };
402
403 for (uint64_t i = 0; i < devc->addr_width_bytes; i++)
404 send_escaping(tcp, &(delay_buf[devc->addr_width_bytes - 1 - i]), 1);
405
406 return SR_OK;
407}
408
409SR_PRIV int ipdbg_la_send_trigger(struct dev_context *devc,
410 struct ipdbg_la_tcp *tcp)
411{
412 uint8_t buf;
413
414 /* Mask */
415 buf = CMD_CFG_TRIGGER;
416 tcp_send(tcp, &buf, 1);
417 buf = CMD_TRIG_MASKS;
418 tcp_send(tcp, &buf, 1);
419 buf = CMD_TRIG_MASK;
420 tcp_send(tcp, &buf, 1);
421
422 for (size_t i = 0; i < devc->data_width_bytes; i++)
423 send_escaping(tcp,
424 devc->trigger_mask + devc->data_width_bytes - 1 - i, 1);
425
426 /* Value */
427 buf = CMD_CFG_TRIGGER;
428 tcp_send(tcp, &buf, 1);
429 buf = CMD_TRIG_MASKS;
430 tcp_send(tcp, &buf, 1);
431 buf = CMD_TRIG_VALUE;
432 tcp_send(tcp, &buf, 1);
433
434 for (size_t i = 0; i < devc->data_width_bytes; i++)
435 send_escaping(tcp,
436 devc->trigger_value + devc->data_width_bytes - 1 - i, 1);
437
438 /* Mask_last */
439 buf = CMD_CFG_TRIGGER;
440 tcp_send(tcp, &buf, 1);
441 buf = CMD_TRIG_MASKS_LAST;
442 tcp_send(tcp, &buf, 1);
443 buf = CMD_TRIG_MASK_LAST;
444 tcp_send(tcp, &buf, 1);
445
446 for (size_t i = 0; i < devc->data_width_bytes; i++)
447 send_escaping(tcp,
448 devc->trigger_mask_last + devc->data_width_bytes - 1 - i, 1);
449
450 /* Value_last */
451 buf = CMD_CFG_TRIGGER;
452 tcp_send(tcp, &buf, 1);
453 buf = CMD_TRIG_MASKS_LAST;
454 tcp_send(tcp, &buf, 1);
455 buf = CMD_TRIG_VALUE_LAST;
456 tcp_send(tcp, &buf, 1);
457
458 for (size_t i = 0; i < devc->data_width_bytes; i++)
459 send_escaping(tcp,
460 devc->trigger_value_last + devc->data_width_bytes - 1 - i, 1);
461
462 /* Edge_mask */
463 buf = CMD_CFG_TRIGGER;
464 tcp_send(tcp, &buf, 1);
465 buf = CMD_TRIG_SELECT_EDGE_MASK;
466 tcp_send(tcp, &buf, 1);
467 buf = CMD_TRIG_SET_EDGE_MASK;
468 tcp_send(tcp, &buf, 1);
469
470 for (size_t i = 0; i < devc->data_width_bytes; i++)
471 send_escaping(tcp,
472 devc->trigger_edge_mask + devc->data_width_bytes - 1 - i, 1);
473
474 return SR_OK;
475}
476
477SR_PRIV void ipdbg_la_get_addrwidth_and_datawidth(
478 struct ipdbg_la_tcp *tcp, struct dev_context *devc)
479{
480 uint8_t buf[8];
481 uint8_t read_cmd = CMD_GET_BUS_WIDTHS;
482
483 if (tcp_send(tcp, &read_cmd, 1) != SR_OK)
484 sr_warn("Can't send read command");
485
486 if (tcp_receive_blocking(tcp, buf, 8) != 8)
487 sr_warn("Can't get address and data width from device");
488
489 devc->data_width = buf[0] & 0x000000FF;
490 devc->data_width |= (buf[1] << 8) & 0x0000FF00;
491 devc->data_width |= (buf[2] << 16) & 0x00FF0000;
492 devc->data_width |= (buf[3] << 24) & 0xFF000000;
493
494 devc->addr_width = buf[4] & 0x000000FF;
495 devc->addr_width |= (buf[5] << 8) & 0x0000FF00;
496 devc->addr_width |= (buf[6] << 16) & 0x00FF0000;
497 devc->addr_width |= (buf[7] << 24) & 0xFF000000;
498
499 const uint8_t host_word_size = 8;
500
501 devc->data_width_bytes =
502 (devc->data_width + host_word_size - 1) / host_word_size;
503 devc->addr_width_bytes =
504 (devc->addr_width + host_word_size - 1) / host_word_size;
505
506 devc->limit_samples_max = (0x01 << devc->addr_width);
507 devc->limit_samples = devc->limit_samples_max;
508
509 devc->trigger_mask = g_malloc0(devc->data_width_bytes);
510 devc->trigger_value = g_malloc0(devc->data_width_bytes);
511 devc->trigger_mask_last = g_malloc0(devc->data_width_bytes);
512 devc->trigger_value_last = g_malloc0(devc->data_width_bytes);
513 devc->trigger_edge_mask = g_malloc0(devc->data_width_bytes);
514}
515
516SR_PRIV struct dev_context *ipdbg_la_dev_new(void)
517{
518 struct dev_context *devc;
519
520 devc = g_malloc0(sizeof(struct dev_context));
521 devc->capture_ratio = 50;
522
523 return devc;
524}
525
526SR_PRIV int ipdbg_la_send_reset(struct ipdbg_la_tcp *tcp)
527{
528 uint8_t buf = CMD_RESET;
529 if (tcp_send(tcp, &buf, 1) != SR_OK)
530 sr_warn("Couldn't send reset");
531
532 return SR_OK;
533}
534
535SR_PRIV int ipdbg_la_request_id(struct ipdbg_la_tcp *tcp)
536{
537 uint8_t buf = CMD_GET_LA_ID;
538 if (tcp_send(tcp, &buf, 1) != SR_OK)
539 sr_warn("Couldn't send ID request");
540
541 char id[4];
542 if (tcp_receive_blocking(tcp, (uint8_t *)id, 4) != 4) {
543 sr_err("Couldn't read device ID");
544 return SR_ERR;
545 }
546
547 if (strncmp(id, "IDBG", 4)) {
548 sr_err("Invalid device ID: expected 'IDBG', got '%c%c%c%c'.",
549 id[0], id[1], id[2], id[3]);
550 return SR_ERR;
551 }
552
553 return SR_OK;
554}
555
556SR_PRIV void ipdbg_la_abort_acquisition(const struct sr_dev_inst *sdi)
557{
558 struct ipdbg_la_tcp *tcp = sdi->conn;
559
560 sr_session_source_remove(sdi->session, tcp->socket);
561
562 std_session_send_df_end(sdi);
563}
564
565SR_PRIV int ipdbg_la_send_start(struct ipdbg_la_tcp *tcp)
566{
567 uint8_t buf = CMD_START;
568
569 if (tcp_send(tcp, &buf, 1) != SR_OK)
570 sr_warn("Couldn't send start");
571
572 return SR_OK;
573}