]> sigrok.org Git - libsigrok.git/blame - hardware/openbench-logic-sniffer/ols.c
Run-Length Encoding support for the OLS.
[libsigrok.git] / hardware / openbench-logic-sniffer / ols.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
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 <stdio.h>
21#include <stdint.h>
22#include <stdlib.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <unistd.h>
a9f54bcd
UH
27#ifdef _WIN32
28#include <windows.h>
29#else
a1bb33af 30#include <termios.h>
926b866c 31#endif
a1bb33af
UH
32#include <string.h>
33#include <sys/time.h>
34#include <inttypes.h>
926b866c
UH
35#ifdef _WIN32
36/* TODO */
37#else
6937bb75 38#include <arpa/inet.h>
926b866c 39#endif
a1bb33af 40#include <glib.h>
43fc7885 41#include <sigrok.h>
1483577e 42#include <sigrok-internal.h>
4fe9a6da 43#include "ols.h"
a1bb33af 44
1fdb75e1
UH
45#ifdef _WIN32
46#define O_NONBLOCK FIONBIO
47#endif
48
a1bb33af 49static int capabilities[] = {
5a2326a7
UH
50 SR_HWCAP_LOGIC_ANALYZER,
51 SR_HWCAP_SAMPLERATE,
52 SR_HWCAP_CAPTURE_RATIO,
53 SR_HWCAP_LIMIT_SAMPLES,
3a4d09c0 54 SR_HWCAP_RLE,
43fc7885 55 0,
a1bb33af
UH
56};
57
4fe9a6da 58/* default supported samplerates, can be overridden by device metadata */
60679b18 59static struct sr_samplerates samplerates = {
c9140419 60 SR_HZ(10),
59df0c77 61 SR_MHZ(200),
c9140419
UH
62 SR_HZ(1),
63 NULL,
a1bb33af
UH
64};
65
6c290072 66/* List of struct sr_serial_device_instance */
a1bb33af
UH
67static GSList *device_instances = NULL;
68
6937bb75 69static int send_shortcommand(int fd, uint8_t command)
a1bb33af
UH
70{
71 char buf[1];
72
b08024a8 73 sr_dbg("ols: sending cmd 0x%.2x", command);
a1bb33af 74 buf[0] = command;
2119ab03 75 if (serial_write(fd, buf, 1) != 1)
e46b8fb1 76 return SR_ERR;
a1bb33af 77
e46b8fb1 78 return SR_OK;
a1bb33af
UH
79}
80
6937bb75 81static int send_longcommand(int fd, uint8_t command, uint32_t data)
a1bb33af
UH
82{
83 char buf[5];
84
b08024a8 85 sr_dbg("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
a1bb33af 86 buf[0] = command;
6937bb75
BV
87 buf[1] = (data & 0xff000000) >> 24;
88 buf[2] = (data & 0xff0000) >> 16;
89 buf[3] = (data & 0xff00) >> 8;
90 buf[4] = data & 0xff;
2119ab03 91 if (serial_write(fd, buf, 5) != 5)
e46b8fb1 92 return SR_ERR;
a1bb33af 93
e46b8fb1 94 return SR_OK;
a1bb33af
UH
95}
96
4fe9a6da 97static int configure_probes(struct ols_device *ols, GSList *probes)
a1bb33af 98{
1afe8989 99 struct sr_probe *probe;
a1bb33af 100 GSList *l;
6937bb75 101 int probe_bit, stage, i;
a1bb33af
UH
102 char *tc;
103
4fe9a6da 104 ols->probe_mask = 0;
43fc7885 105 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
4fe9a6da
BV
106 ols->trigger_mask[i] = 0;
107 ols->trigger_value[i] = 0;
a1bb33af
UH
108 }
109
4fe9a6da 110 ols->num_stages = 0;
43fc7885 111 for (l = probes; l; l = l->next) {
1afe8989 112 probe = (struct sr_probe *)l->data;
43fc7885 113 if (!probe->enabled)
6937bb75
BV
114 continue;
115
43fc7885
UH
116 /*
117 * Set up the probe mask for later configuration into the
118 * flag register.
119 */
a1bb33af 120 probe_bit = 1 << (probe->index - 1);
4fe9a6da 121 ols->probe_mask |= probe_bit;
6937bb75 122
a803c0db 123 if (!probe->trigger)
6937bb75
BV
124 continue;
125
43fc7885 126 /* Configure trigger mask and value. */
6937bb75 127 stage = 0;
43fc7885 128 for (tc = probe->trigger; tc && *tc; tc++) {
4fe9a6da 129 ols->trigger_mask[stage] |= probe_bit;
43fc7885 130 if (*tc == '1')
4fe9a6da 131 ols->trigger_value[stage] |= probe_bit;
6937bb75 132 stage++;
43fc7885
UH
133 if (stage > 3)
134 /*
135 * TODO: Only supporting parallel mode, with
136 * up to 4 stages.
137 */
e46b8fb1 138 return SR_ERR;
a1bb33af 139 }
4fe9a6da
BV
140 if (stage > ols->num_stages)
141 ols->num_stages = stage;
a1bb33af
UH
142 }
143
e46b8fb1 144 return SR_OK;
a1bb33af
UH
145}
146
a803c0db 147static uint32_t reverse16(uint32_t in)
6937bb75
BV
148{
149 uint32_t out;
150
a803c0db
BV
151 out = (in & 0xff) << 8;
152 out |= (in & 0xff00) >> 8;
153 out |= (in & 0xff0000) << 8;
154 out |= (in & 0xff000000) >> 8;
155
156 return out;
157}
158
159static uint32_t reverse32(uint32_t in)
160{
161 uint32_t out;
162
163 out = (in & 0xff) << 24;
164 out |= (in & 0xff00) << 8;
165 out |= (in & 0xff0000) >> 8;
166 out |= (in & 0xff000000) >> 24;
167
168 return out;
6937bb75
BV
169}
170
4fe9a6da
BV
171static struct ols_device *ols_device_new(void)
172{
173 struct ols_device *ols;
174
c0a4b971 175 /* TODO: Is 'ols' ever g_free()'d? */
b53738ba
UH
176 if (!(ols = g_try_malloc0(sizeof(struct ols_device)))) {
177 sr_err("ols: %s: ols malloc failed", __func__);
178 return NULL;
179 }
180
4fe9a6da
BV
181 ols->trigger_at = -1;
182 ols->probe_mask = 0xffffffff;
183 ols->cur_samplerate = SR_KHZ(200);
9c939c51 184 ols->period_ps = 5000000;
4fe9a6da
BV
185
186 return ols;
187}
188
189static struct sr_device_instance *get_metadata(int fd)
190{
191 struct sr_device_instance *sdi;
192 struct ols_device *ols;
193 uint32_t tmp_int;
194 uint8_t key, type, token;
195 GString *tmp_str, *devicename, *version;
196 gchar tmp_c;
197
198 sdi = sr_device_instance_new(0, SR_ST_INACTIVE, NULL, NULL, NULL);
199 ols = ols_device_new();
200 sdi->priv = ols;
201
202 devicename = g_string_new("");
203 version = g_string_new("");
204
205 key = 0xff;
206 while (key) {
207 if (serial_read(fd, &key, 1) != 1 || key == 0x00)
208 break;
209 type = key >> 5;
210 token = key & 0x1f;
211 switch (type) {
212 case 0:
213 /* NULL-terminated string */
214 tmp_str = g_string_new("");
215 while (serial_read(fd, &tmp_c, 1) == 1 && tmp_c != '\0')
216 g_string_append_c(tmp_str, tmp_c);
b08024a8
UH
217 sr_dbg("ols: got metadata key 0x%.2x value '%s'",
218 key, tmp_str->str);
4fe9a6da
BV
219 switch (token) {
220 case 0x01:
221 /* Device name */
222 devicename = g_string_append(devicename, tmp_str->str);
223 break;
224 case 0x02:
225 /* FPGA firmware version */
226 if (version->len)
227 g_string_append(version, ", ");
228 g_string_append(version, "FPGA version ");
229 g_string_append(version, tmp_str->str);
230 break;
231 case 0x03:
232 /* Ancillary version */
233 if (version->len)
234 g_string_append(version, ", ");
235 g_string_append(version, "Ancillary version ");
236 g_string_append(version, tmp_str->str);
237 break;
238 default:
b08024a8
UH
239 sr_info("ols: unknown token 0x%.2x: '%s'",
240 token, tmp_str->str);
4fe9a6da
BV
241 break;
242 }
243 g_string_free(tmp_str, TRUE);
244 break;
245 case 1:
246 /* 32-bit unsigned integer */
247 if (serial_read(fd, &tmp_int, 4) != 4)
248 break;
249 tmp_int = reverse32(tmp_int);
b08024a8
UH
250 sr_dbg("ols: got metadata key 0x%.2x value 0x%.8x",
251 key, tmp_int);
4fe9a6da
BV
252 switch (token) {
253 case 0x00:
254 /* Number of usable probes */
255 ols->num_probes = tmp_int;
256 break;
257 case 0x01:
258 /* Amount of sample memory available (bytes) */
259 ols->max_samples = tmp_int;
260 break;
261 case 0x02:
262 /* Amount of dynamic memory available (bytes) */
263 /* what is this for? */
264 break;
265 case 0x03:
266 /* Maximum sample rate (hz) */
267 ols->max_samplerate = tmp_int;
268 break;
269 case 0x04:
270 /* protocol version */
271 ols->protocol_version = tmp_int;
272 break;
273 default:
b08024a8
UH
274 sr_info("ols: unknown token 0x%.2x: 0x%.8x",
275 token, tmp_int);
4fe9a6da
BV
276 break;
277 }
278 break;
279 case 2:
280 /* 8-bit unsigned integer */
281 if (serial_read(fd, &tmp_c, 1) != 1)
282 break;
b08024a8
UH
283 sr_dbg("ols: got metadata key 0x%.2x value 0x%.2x",
284 key, tmp_c);
4fe9a6da
BV
285 switch (token) {
286 case 0x00:
287 /* Number of usable probes */
288 ols->num_probes = tmp_c;
289 break;
290 case 0x01:
291 /* protocol version */
292 ols->protocol_version = tmp_c;
293 break;
294 default:
b08024a8
UH
295 sr_info("ols: unknown token 0x%.2x: 0x%.2x",
296 token, tmp_c);
4fe9a6da
BV
297 break;
298 }
299 break;
300 default:
301 /* unknown type */
302 break;
303 }
304 }
305
306 sdi->model = devicename->str;
307 sdi->version = version->str;
308 g_string_free(devicename, FALSE);
309 g_string_free(version, FALSE);
310
311 return sdi;
312}
313
54ac5277 314static int hw_init(const char *deviceinfo)
a1bb33af 315{
a00ba012 316 struct sr_device_instance *sdi;
4fe9a6da 317 struct ols_device *ols;
a1bb33af 318 GSList *ports, *l;
4fe9a6da 319 GPollFD *fds, probefd;
6937bb75 320 int devcnt, final_devcnt, num_ports, fd, ret, i;
d02a535e 321 char buf[8], **device_names, **serial_params;
a1bb33af 322
c0a4b971
UH
323 final_devcnt = 0;
324
43fc7885 325 if (deviceinfo)
6937bb75 326 ports = g_slist_append(NULL, strdup(deviceinfo));
a1bb33af 327 else
43fc7885 328 /* No specific device given, so scan all serial ports. */
a1bb33af
UH
329 ports = list_serial_ports();
330
331 num_ports = g_slist_length(ports);
c0a4b971
UH
332
333 if (!(fds = g_try_malloc0(num_ports * sizeof(GPollFD)))) {
334 sr_err("ols: %s: fds malloc failed", __func__);
335 goto hw_init_free_ports; /* TODO: SR_ERR_MALLOC. */
336 }
337
338 if (!(device_names = g_try_malloc(num_ports * sizeof(char *)))) {
339 sr_err("ols: %s: device_names malloc failed", __func__);
340 goto hw_init_free_fds; /* TODO: SR_ERR_MALLOC. */
341 }
342
343 if (!(serial_params = g_try_malloc(num_ports * sizeof(char *)))) {
344 sr_err("ols: %s: serial_params malloc failed", __func__);
345 goto hw_init_free_device_names; /* TODO: SR_ERR_MALLOC. */
346 }
347
a1bb33af 348 devcnt = 0;
43fc7885
UH
349 for (l = ports; l; l = l->next) {
350 /* The discovery procedure is like this: first send the Reset
351 * command (0x00) 5 times, since the device could be anywhere
352 * in a 5-byte command. Then send the ID command (0x02).
353 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
354 * have a match.
355 *
356 * Since it may take the device a while to respond at 115Kb/s,
357 * we do all the sending first, then wait for all of them to
358 * respond with g_poll().
a1bb33af 359 */
b08024a8 360 sr_info("ols: probing %s...", (char *)l->data);
d02a535e 361 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
43fc7885 362 if (fd != -1) {
d02a535e
BV
363 serial_params[devcnt] = serial_backup_params(fd);
364 serial_set_params(fd, 115200, 8, 0, 1, 2);
e46b8fb1 365 ret = SR_OK;
43fc7885
UH
366 for (i = 0; i < 5; i++) {
367 if ((ret = send_shortcommand(fd,
e46b8fb1 368 CMD_RESET)) != SR_OK) {
43fc7885 369 /* Serial port is not writable. */
6937bb75
BV
370 break;
371 }
a1bb33af 372 }
e46b8fb1 373 if (ret != SR_OK) {
43fc7885
UH
374 serial_restore_params(fd,
375 serial_params[devcnt]);
d02a535e 376 serial_close(fd);
6937bb75 377 continue;
d02a535e 378 }
6937bb75
BV
379 send_shortcommand(fd, CMD_ID);
380 fds[devcnt].fd = fd;
381 fds[devcnt].events = G_IO_IN;
382 device_names[devcnt] = strdup(l->data);
383 devcnt++;
a1bb33af 384 }
6937bb75 385 free(l->data);
a1bb33af
UH
386 }
387
5b15b41e
PS
388 /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */
389 usleep(10000);
a1bb33af 390
a1bb33af 391 g_poll(fds, devcnt, 1);
4fe9a6da 392
43fc7885 393 for (i = 0; i < devcnt; i++) {
4fe9a6da
BV
394 if (fds[i].revents != G_IO_IN)
395 continue;
396 if (serial_read(fds[i].fd, buf, 4) != 4)
397 continue;
398 if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4))
399 continue;
400
401 /* definitely using the OLS protocol, check if it supports
402 * the metadata command
403 */
404 send_shortcommand(fds[i].fd, CMD_METADATA);
405 probefd.fd = fds[i].fd;
406 probefd.events = G_IO_IN;
407 if (g_poll(&probefd, 1, 10) > 0) {
408 /* got metadata */
409 sdi = get_metadata(fds[i].fd);
410 sdi->index = final_devcnt;
411 } else {
412 /* not an OLS -- some other board that uses the sump protocol */
413 sdi = sr_device_instance_new(final_devcnt, SR_ST_INACTIVE,
414 "Sump", "Logic Analyzer", "v1.0");
415 ols = ols_device_new();
416 ols->num_probes = 32;
417 sdi->priv = ols;
a1bb33af 418 }
4fe9a6da
BV
419 sdi->serial = sr_serial_device_instance_new(device_names[i], -1);
420 device_instances = g_slist_append(device_instances, sdi);
421 final_devcnt++;
422 serial_close(fds[i].fd);
423 fds[i].fd = 0;
4fe9a6da
BV
424 }
425
426 /* clean up after all the probing */
427 for (i = 0; i < devcnt; i++) {
43fc7885 428 if (fds[i].fd != 0) {
d02a535e
BV
429 serial_restore_params(fds[i].fd, serial_params[i]);
430 serial_close(fds[i].fd);
6937bb75 431 }
d02a535e 432 free(serial_params[i]);
4fe9a6da 433 free(device_names[i]);
a1bb33af
UH
434 }
435
c0a4b971
UH
436 g_free(serial_params);
437hw_init_free_device_names:
438 g_free(device_names);
439hw_init_free_fds:
440 g_free(fds);
441hw_init_free_ports:
a1bb33af
UH
442 g_slist_free(ports);
443
444 return final_devcnt;
445}
446
a1bb33af
UH
447static int hw_opendev(int device_index)
448{
a00ba012 449 struct sr_device_instance *sdi;
a1bb33af 450
d32d961d 451 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 452 return SR_ERR;
a1bb33af 453
d02a535e 454 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
43fc7885 455 if (sdi->serial->fd == -1)
e46b8fb1 456 return SR_ERR;
a1bb33af 457
5a2326a7 458 sdi->status = SR_ST_ACTIVE;
a1bb33af 459
e46b8fb1 460 return SR_OK;
a1bb33af
UH
461}
462
697785d1 463static int hw_closedev(int device_index)
a1bb33af 464{
a00ba012 465 struct sr_device_instance *sdi;
a1bb33af 466
697785d1
UH
467 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
468 sr_err("ols: %s: sdi was NULL", __func__);
469 return SR_ERR; /* TODO: SR_ERR_ARG? */
470 }
a1bb33af 471
697785d1 472 /* TODO */
43fc7885 473 if (sdi->serial->fd != -1) {
d02a535e 474 serial_close(sdi->serial->fd);
a1bb33af 475 sdi->serial->fd = -1;
5a2326a7 476 sdi->status = SR_ST_INACTIVE;
a1bb33af 477 }
697785d1
UH
478
479 return SR_OK;
a1bb33af
UH
480}
481
a1bb33af
UH
482static void hw_cleanup(void)
483{
484 GSList *l;
a00ba012 485 struct sr_device_instance *sdi;
a1bb33af 486
8722c31e 487 /* Properly close and free all devices. */
43fc7885 488 for (l = device_instances; l; l = l->next) {
a1bb33af 489 sdi = l->data;
43fc7885 490 if (sdi->serial->fd != -1)
d02a535e 491 serial_close(sdi->serial->fd);
a00ba012 492 sr_device_instance_free(sdi);
a1bb33af
UH
493 }
494 g_slist_free(device_instances);
495 device_instances = NULL;
a1bb33af
UH
496}
497
a1bb33af
UH
498static void *hw_get_device_info(int device_index, int device_info_id)
499{
a00ba012 500 struct sr_device_instance *sdi;
4fe9a6da 501 struct ols_device *ols;
a1bb33af
UH
502 void *info;
503
d32d961d 504 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
a1bb33af 505 return NULL;
4fe9a6da 506 ols = sdi->priv;
a1bb33af
UH
507
508 info = NULL;
43fc7885 509 switch (device_info_id) {
5a2326a7 510 case SR_DI_INSTANCE:
a1bb33af
UH
511 info = sdi;
512 break;
5a2326a7 513 case SR_DI_NUM_PROBES:
a1bb33af
UH
514 info = GINT_TO_POINTER(NUM_PROBES);
515 break;
5a2326a7 516 case SR_DI_SAMPLERATES:
a1bb33af
UH
517 info = &samplerates;
518 break;
5a2326a7 519 case SR_DI_TRIGGER_TYPES:
43fc7885 520 info = (char *)TRIGGER_TYPES;
a1bb33af 521 break;
5a2326a7 522 case SR_DI_CUR_SAMPLERATE:
4fe9a6da 523 info = &ols->cur_samplerate;
a1bb33af
UH
524 break;
525 }
526
527 return info;
528}
529
a1bb33af
UH
530static int hw_get_status(int device_index)
531{
a00ba012 532 struct sr_device_instance *sdi;
a1bb33af 533
d32d961d 534 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
5a2326a7 535 return SR_ST_NOT_FOUND;
a1bb33af
UH
536
537 return sdi->status;
538}
539
a1bb33af
UH
540static int *hw_get_capabilities(void)
541{
a1bb33af
UH
542 return capabilities;
543}
544
a00ba012 545static int set_configuration_samplerate(struct sr_device_instance *sdi,
43fc7885 546 uint64_t samplerate)
a1bb33af 547{
4fe9a6da 548 struct ols_device *ols;
a1bb33af 549
4fe9a6da
BV
550 ols = sdi->priv;
551 if (ols->max_samplerate) {
552 if (samplerate > ols->max_samplerate)
553 return SR_ERR_SAMPLERATE;
554 } else if (samplerate < samplerates.low || samplerate > samplerates.high)
e46b8fb1 555 return SR_ERR_SAMPLERATE;
a1bb33af 556
4fe9a6da 557 ols->cur_samplerate = samplerate;
9c939c51 558 ols->period_ps = 1000000000000 / samplerate;
43fc7885 559 if (samplerate > CLOCK_RATE) {
4fe9a6da
BV
560 ols->flag_reg |= FLAG_DEMUX;
561 ols->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
43fc7885 562 } else {
4fe9a6da
BV
563 ols->flag_reg &= ~FLAG_DEMUX;
564 ols->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
a1bb33af 565 }
a1bb33af 566
e46b8fb1 567 return SR_OK;
a1bb33af
UH
568}
569
a1bb33af
UH
570static int hw_set_configuration(int device_index, int capability, void *value)
571{
a00ba012 572 struct sr_device_instance *sdi;
4fe9a6da 573 struct ols_device *ols;
a1bb33af
UH
574 int ret;
575 uint64_t *tmp_u64;
576
d32d961d 577 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 578 return SR_ERR;
4fe9a6da 579 ols = sdi->priv;
a1bb33af 580
5a2326a7 581 if (sdi->status != SR_ST_ACTIVE)
e46b8fb1 582 return SR_ERR;
a1bb33af 583
a803c0db 584 switch (capability) {
5a2326a7 585 case SR_HWCAP_SAMPLERATE:
a1bb33af
UH
586 tmp_u64 = value;
587 ret = set_configuration_samplerate(sdi, *tmp_u64);
a803c0db 588 break;
5a2326a7 589 case SR_HWCAP_PROBECONFIG:
4fe9a6da 590 ret = configure_probes(ols, (GSList *) value);
a803c0db 591 break;
5a2326a7 592 case SR_HWCAP_LIMIT_SAMPLES:
2458ea65 593 tmp_u64 = value;
574ce498 594 if (*tmp_u64 < MIN_NUM_SAMPLES)
e46b8fb1 595 return SR_ERR;
22130421
GM
596 if (*tmp_u64 > ols->max_samples)
597 sr_warn("ols: sample limit exceeds hw max");
598
4fe9a6da 599 ols->limit_samples = *tmp_u64;
b08024a8 600 sr_info("ols: sample limit %" PRIu64, ols->limit_samples);
e46b8fb1 601 ret = SR_OK;
a803c0db 602 break;
5a2326a7 603 case SR_HWCAP_CAPTURE_RATIO:
a803c0db 604 tmp_u64 = value;
4fe9a6da
BV
605 ols->capture_ratio = *tmp_u64;
606 if (ols->capture_ratio < 0 || ols->capture_ratio > 100) {
607 ols->capture_ratio = 0;
e46b8fb1 608 ret = SR_ERR;
43fc7885 609 } else
e46b8fb1 610 ret = SR_OK;
a803c0db 611 break;
3a4d09c0
GM
612 case SR_HWCAP_RLE:
613 if(strcmp(value, "on") == 0) {
614 sr_info("ols: enabling RLE");
615 ols->flag_reg |= FLAG_RLE;
616 }
617 ret = SR_OK;
618 break;
a803c0db 619 default:
e46b8fb1 620 ret = SR_ERR;
43fc7885 621 }
a1bb33af
UH
622
623 return ret;
624}
625
9c939c51 626static int receive_data(int fd, int revents, void *session_data)
a1bb33af 627{
b9c735a2 628 struct sr_datafeed_packet packet;
9c939c51 629 struct sr_datafeed_logic logic;
4fe9a6da
BV
630 struct sr_device_instance *sdi;
631 struct ols_device *ols;
632 GSList *l;
3a4d09c0
GM
633 int num_channels, offset, i, j;
634 unsigned char byte;
a1bb33af 635
4fe9a6da
BV
636 /* find this device's ols_device struct by its fd */
637 ols = NULL;
638 for (l = device_instances; l; l = l->next) {
639 sdi = l->data;
640 if (sdi->serial->fd == fd) {
641 ols = sdi->priv;
642 break;
643 }
644 }
645 if (!ols)
646 /* shouldn't happen */
647 return TRUE;
648
649 if (ols->num_transfers++ == 0) {
43fc7885
UH
650 /*
651 * First time round, means the device started sending data,
652 * and will not stop until done. If it stops sending for
653 * longer than it takes to send a byte, that means it's
654 * finished. We'll double that to 30ms to be sure...
a1bb33af 655 */
6f1be0a2 656 sr_source_remove(fd);
9c939c51 657 sr_source_add(fd, G_IO_IN, 30, receive_data, session_data);
c0a4b971
UH
658 ols->raw_sample_buf = g_try_malloc(ols->limit_samples * 4);
659 if (!ols->raw_sample_buf) {
660 sr_err("ols: %s: ols->raw_sample_buf malloc failed",
661 __func__);
662 return FALSE;
663 }
a803c0db 664 /* fill with 1010... for debugging */
4fe9a6da 665 memset(ols->raw_sample_buf, 0x82, ols->limit_samples * 4);
a1bb33af
UH
666 }
667
6937bb75 668 num_channels = 0;
43fc7885 669 for (i = 0x20; i > 0x02; i /= 2) {
4fe9a6da 670 if ((ols->flag_reg & i) == 0)
6937bb75 671 num_channels++;
43fc7885 672 }
6937bb75 673
3a4d09c0 674 if (revents == G_IO_IN) {
2119ab03 675 if (serial_read(fd, &byte, 1) != 1)
a1bb33af
UH
676 return FALSE;
677
3a4d09c0
GM
678 /* Ignore it if we've read enough */
679 if(ols->num_samples >= ols->limit_samples)
680 return TRUE;
681
4fe9a6da 682 ols->sample[ols->num_bytes++] = byte;
b08024a8 683 sr_dbg("ols: received byte 0x%.2x", byte);
4fe9a6da 684 if (ols->num_bytes == num_channels) {
43fc7885 685 /* Got a full sample. */
b08024a8 686 sr_dbg("ols: received sample 0x%.*x",
3a4d09c0 687 ols->num_bytes * 2, *(int*)ols->sample);
4fe9a6da 688 if (ols->flag_reg & FLAG_RLE) {
43fc7885
UH
689 /*
690 * In RLE mode -1 should never come in as a
691 * sample, because bit 31 is the "count" flag.
43fc7885 692 */
3a4d09c0
GM
693 if (ols->sample[ols->num_bytes - 1] & 0x80) {
694 ols->sample[ols->num_bytes - 1] &= 0x7F;
695 /* FIXME: This will only work on
696 * little-endian systems
a1bb33af 697 */
3a4d09c0
GM
698 ols->rle_count = *(int*)(ols->sample);
699 sr_dbg("ols: RLE count = %d", ols->rle_count);
700 ols->num_bytes = 0;
701 return TRUE;
702 }
703 }
704 ols->num_samples += ols->rle_count + 1;
705 if(ols->num_samples > ols->limit_samples) {
706 /* Save us from overrunning the buffer */
707 ols->rle_count -= ols->num_samples - ols->limit_samples;
708 ols->num_samples = ols->limit_samples;
a1bb33af
UH
709 }
710
43fc7885
UH
711 if (num_channels < 4) {
712 /*
713 * Some channel groups may have been turned
714 * off, to speed up transfer between the
715 * hardware and the PC. Expand that here before
716 * submitting it over the session bus --
717 * whatever is listening on the bus will be
718 * expecting a full 32-bit sample, based on
719 * the number of probes.
6937bb75
BV
720 */
721 j = 0;
4fe9a6da 722 memset(ols->tmp_sample, 0, 4);
43fc7885 723 for (i = 0; i < 4; i++) {
4fe9a6da 724 if (((ols->flag_reg >> 2) & (1 << i)) == 0) {
43fc7885
UH
725 /*
726 * This channel group was
727 * enabled, copy from received
728 * sample.
729 */
4fe9a6da 730 ols->tmp_sample[i] = ols->sample[j++];
6937bb75
BV
731 }
732 }
4fe9a6da 733 memcpy(ols->sample, ols->tmp_sample, 4);
3a4d09c0 734 sr_dbg("ols: full sample 0x%.8x", *(int*)ols->sample);
6937bb75
BV
735 }
736
a803c0db
BV
737 /* the OLS sends its sample buffer backwards.
738 * store it in reverse order here, so we can dump
739 * this on the session bus later.
740 */
3a4d09c0
GM
741 offset = (ols->limit_samples - ols->num_samples) * 4;
742 for(i = 0; i <= ols->rle_count; i++)
743 memcpy(ols->raw_sample_buf + offset + (i*4), ols->sample, 4);
a1bb33af 744
4fe9a6da
BV
745 memset(ols->sample, 0, 4);
746 ols->num_bytes = 0;
3a4d09c0 747 ols->rle_count = 0;
a1bb33af 748 }
43fc7885
UH
749 } else {
750 /*
751 * This is the main loop telling us a timeout was reached, or
752 * we've acquired all the samples we asked for -- we're done.
a803c0db 753 * Send the (properly-ordered) buffer to the frontend.
43fc7885 754 */
4fe9a6da 755 if (ols->trigger_at != -1) {
a803c0db
BV
756 /* a trigger was set up, so we need to tell the frontend
757 * about it.
758 */
4fe9a6da 759 if (ols->trigger_at > 0) {
a803c0db 760 /* there are pre-trigger samples, send those first */
5a2326a7 761 packet.type = SR_DF_LOGIC;
9c939c51
BV
762 packet.timeoffset = 0;
763 packet.duration = ols->trigger_at * ols->period_ps;
764 packet.payload = &logic;
765 logic.length = ols->trigger_at * 4;
766 logic.unitsize = 4;
22130421
GM
767 logic.data = ols->raw_sample_buf +
768 (ols->limit_samples - ols->num_samples) * 4;
9c939c51 769 sr_session_bus(session_data, &packet);
a803c0db
BV
770 }
771
9c939c51 772 /* send the trigger */
5a2326a7 773 packet.type = SR_DF_TRIGGER;
9c939c51
BV
774 packet.timeoffset = ols->trigger_at * ols->period_ps;
775 packet.duration = 0;
776 sr_session_bus(session_data, &packet);
a803c0db 777
9c939c51 778 /* send post-trigger samples */
5a2326a7 779 packet.type = SR_DF_LOGIC;
9c939c51 780 packet.timeoffset = ols->trigger_at * ols->period_ps;
22130421 781 packet.duration = (ols->num_samples - ols->trigger_at) * ols->period_ps;
9c939c51 782 packet.payload = &logic;
22130421 783 logic.length = (ols->num_samples * 4) - (ols->trigger_at * 4);
9c939c51 784 logic.unitsize = 4;
22130421
GM
785 logic.data = ols->raw_sample_buf + ols->trigger_at * 4 +
786 (ols->limit_samples - ols->num_samples) * 4;
9c939c51 787 sr_session_bus(session_data, &packet);
a803c0db 788 } else {
9c939c51 789 /* no trigger was used */
5a2326a7 790 packet.type = SR_DF_LOGIC;
9c939c51 791 packet.timeoffset = 0;
22130421 792 packet.duration = ols->num_samples * ols->period_ps;
9c939c51 793 packet.payload = &logic;
22130421 794 logic.length = ols->num_samples * 4;
9c939c51 795 logic.unitsize = 4;
22130421
GM
796 logic.data = ols->raw_sample_buf +
797 (ols->limit_samples - ols->num_samples) * 4;
9c939c51 798 sr_session_bus(session_data, &packet);
a803c0db 799 }
c0a4b971 800 g_free(ols->raw_sample_buf);
a803c0db 801
06d64eb8 802 serial_flush(fd);
d02a535e 803 serial_close(fd);
5a2326a7 804 packet.type = SR_DF_END;
22130421 805 packet.timeoffset = ols->num_samples * ols->period_ps;
9c939c51
BV
806 packet.duration = 0;
807 sr_session_bus(session_data, &packet);
a1bb33af
UH
808 }
809
810 return TRUE;
811}
812
9c939c51 813static int hw_start_acquisition(int device_index, gpointer session_data)
a1bb33af 814{
b9c735a2
UH
815 struct sr_datafeed_packet *packet;
816 struct sr_datafeed_header *header;
a00ba012 817 struct sr_device_instance *sdi;
4fe9a6da 818 struct ols_device *ols;
a803c0db 819 uint32_t trigger_config[4];
a1bb33af 820 uint32_t data;
6937bb75
BV
821 uint16_t readcount, delaycount;
822 uint8_t changrp_mask;
22130421 823 int num_channels;
4fe9a6da 824 int i;
a1bb33af 825
d32d961d 826 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 827 return SR_ERR;
c0a4b971 828
4fe9a6da 829 ols = sdi->priv;
a1bb33af 830
5a2326a7 831 if (sdi->status != SR_ST_ACTIVE)
e46b8fb1 832 return SR_ERR;
a1bb33af 833
22130421
GM
834 /*
835 * Enable/disable channel groups in the flag register according to the
836 * probe mask. Calculate this here, because num_channels is needed
837 * to limit readcount.
838 */
839 changrp_mask = 0;
840 num_channels = 0;
841 for (i = 0; i < 4; i++) {
842 if (ols->probe_mask & (0xff << (i * 8))) {
843 changrp_mask |= (1 << i);
844 num_channels++;
845 }
846 }
847
848 /* Limit readcount to prevent reading past the end of the hardware
849 * buffer.
850 */
851 readcount = MIN(ols->max_samples / num_channels, ols->limit_samples) / 4;
a803c0db
BV
852
853 memset(trigger_config, 0, 16);
4fe9a6da
BV
854 trigger_config[ols->num_stages - 1] |= 0x08;
855 if (ols->trigger_mask[0]) {
856 delaycount = readcount * (1 - ols->capture_ratio / 100.0);
857 ols->trigger_at = (readcount - delaycount) * 4 - ols->num_stages;
a803c0db 858
43fc7885 859 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
4fe9a6da 860 reverse32(ols->trigger_mask[0])) != SR_OK)
e46b8fb1 861 return SR_ERR;
a803c0db 862 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
4fe9a6da 863 reverse32(ols->trigger_value[0])) != SR_OK)
e46b8fb1 864 return SR_ERR;
a803c0db 865 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
e46b8fb1
UH
866 trigger_config[0]) != SR_OK)
867 return SR_ERR;
6937bb75 868
a803c0db 869 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
4fe9a6da 870 reverse32(ols->trigger_mask[1])) != SR_OK)
e46b8fb1 871 return SR_ERR;
43fc7885 872 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
4fe9a6da 873 reverse32(ols->trigger_value[1])) != SR_OK)
e46b8fb1 874 return SR_ERR;
a803c0db 875 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
e46b8fb1
UH
876 trigger_config[1]) != SR_OK)
877 return SR_ERR;
6937bb75 878
a803c0db 879 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
4fe9a6da 880 reverse32(ols->trigger_mask[2])) != SR_OK)
e46b8fb1 881 return SR_ERR;
a803c0db 882 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
4fe9a6da 883 reverse32(ols->trigger_value[2])) != SR_OK)
e46b8fb1 884 return SR_ERR;
43fc7885 885 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
e46b8fb1
UH
886 trigger_config[2]) != SR_OK)
887 return SR_ERR;
a803c0db
BV
888
889 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
4fe9a6da 890 reverse32(ols->trigger_mask[3])) != SR_OK)
e46b8fb1 891 return SR_ERR;
a803c0db 892 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
4fe9a6da 893 reverse32(ols->trigger_value[3])) != SR_OK)
e46b8fb1 894 return SR_ERR;
43fc7885 895 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
e46b8fb1
UH
896 trigger_config[3]) != SR_OK)
897 return SR_ERR;
6937bb75 898 } else {
43fc7885 899 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
4fe9a6da 900 ols->trigger_mask[0]) != SR_OK)
e46b8fb1 901 return SR_ERR;
43fc7885 902 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
4fe9a6da 903 ols->trigger_value[0]) != SR_OK)
e46b8fb1 904 return SR_ERR;
43fc7885 905 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
e46b8fb1
UH
906 0x00000008) != SR_OK)
907 return SR_ERR;
a803c0db 908 delaycount = readcount;
6937bb75 909 }
a1bb33af 910
b08024a8
UH
911 sr_info("ols: setting samplerate to %" PRIu64 " Hz (divider %u, "
912 "demux %s)", ols->cur_samplerate, ols->cur_samplerate_divider,
913 ols->flag_reg & FLAG_DEMUX ? "on" : "off");
4fe9a6da
BV
914 if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER,
915 reverse32(ols->cur_samplerate_divider)) != SR_OK)
916 return SR_ERR;
a1bb33af 917
43fc7885 918 /* Send sample limit and pre/post-trigger capture ratio. */
a803c0db
BV
919 data = ((readcount - 1) & 0xffff) << 16;
920 data |= (delaycount - 1) & 0xffff;
e46b8fb1
UH
921 if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SR_OK)
922 return SR_ERR;
a1bb33af 923
43fc7885 924 /* The flag register wants them here, and 1 means "disable channel". */
4fe9a6da
BV
925 ols->flag_reg |= ~(changrp_mask << 2) & 0x3c;
926 ols->flag_reg |= FLAG_FILTER;
3a4d09c0
GM
927 ols->rle_count = 0;
928 data = (ols->flag_reg << 24) | ((ols->flag_reg << 8) & 0xff0000);
e46b8fb1
UH
929 if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SR_OK)
930 return SR_ERR;
a1bb33af 931
43fc7885 932 /* Start acquisition on the device. */
e46b8fb1
UH
933 if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SR_OK)
934 return SR_ERR;
a1bb33af 935
6f1be0a2 936 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
9c939c51 937 session_data);
a1bb33af 938
b53738ba
UH
939 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
940 sr_err("ols: %s: packet malloc failed", __func__);
941 return SR_ERR_MALLOC;
942 }
943
944 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
945 sr_err("ols: %s: header malloc failed", __func__);
c0a4b971 946 g_free(packet);
b53738ba
UH
947 return SR_ERR_MALLOC;
948 }
949
43fc7885 950 /* Send header packet to the session bus. */
5a2326a7 951 packet->type = SR_DF_HEADER;
43fc7885 952 packet->payload = (unsigned char *)header;
a1bb33af
UH
953 header->feed_version = 1;
954 gettimeofday(&header->starttime, NULL);
4fe9a6da 955 header->samplerate = ols->cur_samplerate;
c2616fb9
DR
956 header->num_logic_probes = NUM_PROBES;
957 header->num_analog_probes = 0;
9c939c51 958 sr_session_bus(session_data, packet);
c0a4b971 959
a1bb33af
UH
960 g_free(header);
961 g_free(packet);
962
e46b8fb1 963 return SR_OK;
a1bb33af
UH
964}
965
a1bb33af
UH
966static void hw_stop_acquisition(int device_index, gpointer session_device_id)
967{
b9c735a2 968 struct sr_datafeed_packet packet;
a1bb33af 969
17e1afcb 970 /* Avoid compiler warnings. */
afc8e4de
UH
971 device_index = device_index;
972
5a2326a7 973 packet.type = SR_DF_END;
8a2efef2 974 sr_session_bus(session_device_id, &packet);
a1bb33af
UH
975}
976
5c2d46d1 977struct sr_device_plugin ols_plugin_info = {
e519ba86
UH
978 .name = "ols",
979 .longname = "Openbench Logic Sniffer",
980 .api_version = 1,
981 .init = hw_init,
982 .cleanup = hw_cleanup,
86f5e3d8
UH
983 .opendev = hw_opendev,
984 .closedev = hw_closedev,
e519ba86
UH
985 .get_device_info = hw_get_device_info,
986 .get_status = hw_get_status,
987 .get_capabilities = hw_get_capabilities,
988 .set_configuration = hw_set_configuration,
989 .start_acquisition = hw_start_acquisition,
990 .stop_acquisition = hw_stop_acquisition,
a1bb33af 991};