]> sigrok.org Git - libsigrok.git/blame - hardware/openbench-logic-sniffer/ols.c
ols: Minor whitespace and coding style fixes.
[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;
baf1d714 596 if (*tmp_u64 > ols->max_samples)
22130421 597 sr_warn("ols: sample limit exceeds hw max");
4fe9a6da 598 ols->limit_samples = *tmp_u64;
b08024a8 599 sr_info("ols: sample limit %" PRIu64, ols->limit_samples);
e46b8fb1 600 ret = SR_OK;
a803c0db 601 break;
5a2326a7 602 case SR_HWCAP_CAPTURE_RATIO:
a803c0db 603 tmp_u64 = value;
4fe9a6da
BV
604 ols->capture_ratio = *tmp_u64;
605 if (ols->capture_ratio < 0 || ols->capture_ratio > 100) {
606 ols->capture_ratio = 0;
e46b8fb1 607 ret = SR_ERR;
43fc7885 608 } else
e46b8fb1 609 ret = SR_OK;
a803c0db 610 break;
3a4d09c0 611 case SR_HWCAP_RLE:
baf1d714 612 if (!strcmp(value, "on")) {
3a4d09c0
GM
613 sr_info("ols: enabling RLE");
614 ols->flag_reg |= FLAG_RLE;
615 }
616 ret = SR_OK;
617 break;
a803c0db 618 default:
e46b8fb1 619 ret = SR_ERR;
43fc7885 620 }
a1bb33af
UH
621
622 return ret;
623}
624
9c939c51 625static int receive_data(int fd, int revents, void *session_data)
a1bb33af 626{
b9c735a2 627 struct sr_datafeed_packet packet;
9c939c51 628 struct sr_datafeed_logic logic;
4fe9a6da
BV
629 struct sr_device_instance *sdi;
630 struct ols_device *ols;
631 GSList *l;
3a4d09c0
GM
632 int num_channels, offset, i, j;
633 unsigned char byte;
a1bb33af 634
4fe9a6da
BV
635 /* find this device's ols_device struct by its fd */
636 ols = NULL;
637 for (l = device_instances; l; l = l->next) {
638 sdi = l->data;
639 if (sdi->serial->fd == fd) {
640 ols = sdi->priv;
641 break;
642 }
643 }
644 if (!ols)
645 /* shouldn't happen */
646 return TRUE;
647
648 if (ols->num_transfers++ == 0) {
43fc7885
UH
649 /*
650 * First time round, means the device started sending data,
651 * and will not stop until done. If it stops sending for
652 * longer than it takes to send a byte, that means it's
653 * finished. We'll double that to 30ms to be sure...
a1bb33af 654 */
6f1be0a2 655 sr_source_remove(fd);
9c939c51 656 sr_source_add(fd, G_IO_IN, 30, receive_data, session_data);
c0a4b971
UH
657 ols->raw_sample_buf = g_try_malloc(ols->limit_samples * 4);
658 if (!ols->raw_sample_buf) {
659 sr_err("ols: %s: ols->raw_sample_buf malloc failed",
660 __func__);
661 return FALSE;
662 }
a803c0db 663 /* fill with 1010... for debugging */
4fe9a6da 664 memset(ols->raw_sample_buf, 0x82, ols->limit_samples * 4);
a1bb33af
UH
665 }
666
6937bb75 667 num_channels = 0;
43fc7885 668 for (i = 0x20; i > 0x02; i /= 2) {
4fe9a6da 669 if ((ols->flag_reg & i) == 0)
6937bb75 670 num_channels++;
43fc7885 671 }
6937bb75 672
3a4d09c0 673 if (revents == G_IO_IN) {
2119ab03 674 if (serial_read(fd, &byte, 1) != 1)
a1bb33af
UH
675 return FALSE;
676
baf1d714
UH
677 /* Ignore it if we've read enough. */
678 if (ols->num_samples >= ols->limit_samples)
679 return TRUE;
3a4d09c0 680
4fe9a6da 681 ols->sample[ols->num_bytes++] = byte;
b08024a8 682 sr_dbg("ols: received byte 0x%.2x", byte);
4fe9a6da 683 if (ols->num_bytes == num_channels) {
43fc7885 684 /* Got a full sample. */
b08024a8 685 sr_dbg("ols: received sample 0x%.*x",
baf1d714 686 ols->num_bytes * 2, *(int *)ols->sample);
4fe9a6da 687 if (ols->flag_reg & FLAG_RLE) {
43fc7885
UH
688 /*
689 * In RLE mode -1 should never come in as a
690 * sample, because bit 31 is the "count" flag.
43fc7885 691 */
3a4d09c0 692 if (ols->sample[ols->num_bytes - 1] & 0x80) {
baf1d714
UH
693 ols->sample[ols->num_bytes - 1] &= 0x7f;
694 /*
695 * FIXME: This will only work on
696 * little-endian systems.
a1bb33af 697 */
baf1d714 698 ols->rle_count = *(int *)(ols->sample);
3a4d09c0
GM
699 sr_dbg("ols: RLE count = %d", ols->rle_count);
700 ols->num_bytes = 0;
701 return TRUE;
baf1d714 702 }
3a4d09c0
GM
703 }
704 ols->num_samples += ols->rle_count + 1;
baf1d714
UH
705 if (ols->num_samples > ols->limit_samples) {
706 /* Save us from overrunning the buffer. */
3a4d09c0
GM
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);
baf1d714 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 741 offset = (ols->limit_samples - ols->num_samples) * 4;
baf1d714
UH
742 for (i = 0; i <= ols->rle_count; i++) {
743 memcpy(ols->raw_sample_buf + offset + (i * 4),
744 ols->sample, 4);
745 }
4fe9a6da
BV
746 memset(ols->sample, 0, 4);
747 ols->num_bytes = 0;
3a4d09c0 748 ols->rle_count = 0;
a1bb33af 749 }
43fc7885
UH
750 } else {
751 /*
752 * This is the main loop telling us a timeout was reached, or
753 * we've acquired all the samples we asked for -- we're done.
a803c0db 754 * Send the (properly-ordered) buffer to the frontend.
43fc7885 755 */
4fe9a6da 756 if (ols->trigger_at != -1) {
a803c0db
BV
757 /* a trigger was set up, so we need to tell the frontend
758 * about it.
759 */
4fe9a6da 760 if (ols->trigger_at > 0) {
a803c0db 761 /* there are pre-trigger samples, send those first */
5a2326a7 762 packet.type = SR_DF_LOGIC;
9c939c51
BV
763 packet.timeoffset = 0;
764 packet.duration = ols->trigger_at * ols->period_ps;
765 packet.payload = &logic;
766 logic.length = ols->trigger_at * 4;
767 logic.unitsize = 4;
baf1d714 768 logic.data = ols->raw_sample_buf +
22130421 769 (ols->limit_samples - ols->num_samples) * 4;
9c939c51 770 sr_session_bus(session_data, &packet);
a803c0db
BV
771 }
772
9c939c51 773 /* send the trigger */
5a2326a7 774 packet.type = SR_DF_TRIGGER;
9c939c51
BV
775 packet.timeoffset = ols->trigger_at * ols->period_ps;
776 packet.duration = 0;
777 sr_session_bus(session_data, &packet);
a803c0db 778
9c939c51 779 /* send post-trigger samples */
5a2326a7 780 packet.type = SR_DF_LOGIC;
9c939c51 781 packet.timeoffset = ols->trigger_at * ols->period_ps;
22130421 782 packet.duration = (ols->num_samples - ols->trigger_at) * ols->period_ps;
9c939c51 783 packet.payload = &logic;
22130421 784 logic.length = (ols->num_samples * 4) - (ols->trigger_at * 4);
9c939c51 785 logic.unitsize = 4;
22130421
GM
786 logic.data = ols->raw_sample_buf + ols->trigger_at * 4 +
787 (ols->limit_samples - ols->num_samples) * 4;
9c939c51 788 sr_session_bus(session_data, &packet);
a803c0db 789 } else {
9c939c51 790 /* no trigger was used */
5a2326a7 791 packet.type = SR_DF_LOGIC;
9c939c51 792 packet.timeoffset = 0;
22130421 793 packet.duration = ols->num_samples * ols->period_ps;
9c939c51 794 packet.payload = &logic;
22130421 795 logic.length = ols->num_samples * 4;
9c939c51 796 logic.unitsize = 4;
22130421
GM
797 logic.data = ols->raw_sample_buf +
798 (ols->limit_samples - ols->num_samples) * 4;
9c939c51 799 sr_session_bus(session_data, &packet);
a803c0db 800 }
c0a4b971 801 g_free(ols->raw_sample_buf);
a803c0db 802
06d64eb8 803 serial_flush(fd);
d02a535e 804 serial_close(fd);
5a2326a7 805 packet.type = SR_DF_END;
22130421 806 packet.timeoffset = ols->num_samples * ols->period_ps;
9c939c51
BV
807 packet.duration = 0;
808 sr_session_bus(session_data, &packet);
a1bb33af
UH
809 }
810
811 return TRUE;
812}
813
9c939c51 814static int hw_start_acquisition(int device_index, gpointer session_data)
a1bb33af 815{
b9c735a2
UH
816 struct sr_datafeed_packet *packet;
817 struct sr_datafeed_header *header;
a00ba012 818 struct sr_device_instance *sdi;
4fe9a6da 819 struct ols_device *ols;
a803c0db 820 uint32_t trigger_config[4];
a1bb33af 821 uint32_t data;
6937bb75
BV
822 uint16_t readcount, delaycount;
823 uint8_t changrp_mask;
22130421 824 int num_channels;
4fe9a6da 825 int i;
a1bb33af 826
d32d961d 827 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 828 return SR_ERR;
c0a4b971 829
4fe9a6da 830 ols = sdi->priv;
a1bb33af 831
5a2326a7 832 if (sdi->status != SR_ST_ACTIVE)
e46b8fb1 833 return SR_ERR;
a1bb33af 834
22130421
GM
835 /*
836 * Enable/disable channel groups in the flag register according to the
baf1d714 837 * probe mask. Calculate this here, because num_channels is needed
22130421
GM
838 * to limit readcount.
839 */
840 changrp_mask = 0;
841 num_channels = 0;
842 for (i = 0; i < 4; i++) {
843 if (ols->probe_mask & (0xff << (i * 8))) {
844 changrp_mask |= (1 << i);
845 num_channels++;
846 }
847 }
848
baf1d714
UH
849 /*
850 * Limit readcount to prevent reading past the end of the hardware
22130421
GM
851 * buffer.
852 */
853 readcount = MIN(ols->max_samples / num_channels, ols->limit_samples) / 4;
a803c0db
BV
854
855 memset(trigger_config, 0, 16);
4fe9a6da
BV
856 trigger_config[ols->num_stages - 1] |= 0x08;
857 if (ols->trigger_mask[0]) {
858 delaycount = readcount * (1 - ols->capture_ratio / 100.0);
859 ols->trigger_at = (readcount - delaycount) * 4 - ols->num_stages;
a803c0db 860
43fc7885 861 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
4fe9a6da 862 reverse32(ols->trigger_mask[0])) != SR_OK)
e46b8fb1 863 return SR_ERR;
a803c0db 864 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
4fe9a6da 865 reverse32(ols->trigger_value[0])) != SR_OK)
e46b8fb1 866 return SR_ERR;
a803c0db 867 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
e46b8fb1
UH
868 trigger_config[0]) != SR_OK)
869 return SR_ERR;
6937bb75 870
a803c0db 871 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
4fe9a6da 872 reverse32(ols->trigger_mask[1])) != SR_OK)
e46b8fb1 873 return SR_ERR;
43fc7885 874 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
4fe9a6da 875 reverse32(ols->trigger_value[1])) != SR_OK)
e46b8fb1 876 return SR_ERR;
a803c0db 877 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
e46b8fb1
UH
878 trigger_config[1]) != SR_OK)
879 return SR_ERR;
6937bb75 880
a803c0db 881 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
4fe9a6da 882 reverse32(ols->trigger_mask[2])) != SR_OK)
e46b8fb1 883 return SR_ERR;
a803c0db 884 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
4fe9a6da 885 reverse32(ols->trigger_value[2])) != SR_OK)
e46b8fb1 886 return SR_ERR;
43fc7885 887 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
e46b8fb1
UH
888 trigger_config[2]) != SR_OK)
889 return SR_ERR;
a803c0db
BV
890
891 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
4fe9a6da 892 reverse32(ols->trigger_mask[3])) != SR_OK)
e46b8fb1 893 return SR_ERR;
a803c0db 894 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
4fe9a6da 895 reverse32(ols->trigger_value[3])) != SR_OK)
e46b8fb1 896 return SR_ERR;
43fc7885 897 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
e46b8fb1
UH
898 trigger_config[3]) != SR_OK)
899 return SR_ERR;
6937bb75 900 } else {
43fc7885 901 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
4fe9a6da 902 ols->trigger_mask[0]) != SR_OK)
e46b8fb1 903 return SR_ERR;
43fc7885 904 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
4fe9a6da 905 ols->trigger_value[0]) != SR_OK)
e46b8fb1 906 return SR_ERR;
43fc7885 907 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
e46b8fb1
UH
908 0x00000008) != SR_OK)
909 return SR_ERR;
a803c0db 910 delaycount = readcount;
6937bb75 911 }
a1bb33af 912
b08024a8
UH
913 sr_info("ols: setting samplerate to %" PRIu64 " Hz (divider %u, "
914 "demux %s)", ols->cur_samplerate, ols->cur_samplerate_divider,
915 ols->flag_reg & FLAG_DEMUX ? "on" : "off");
4fe9a6da
BV
916 if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER,
917 reverse32(ols->cur_samplerate_divider)) != SR_OK)
918 return SR_ERR;
a1bb33af 919
43fc7885 920 /* Send sample limit and pre/post-trigger capture ratio. */
a803c0db
BV
921 data = ((readcount - 1) & 0xffff) << 16;
922 data |= (delaycount - 1) & 0xffff;
e46b8fb1
UH
923 if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SR_OK)
924 return SR_ERR;
a1bb33af 925
43fc7885 926 /* The flag register wants them here, and 1 means "disable channel". */
4fe9a6da
BV
927 ols->flag_reg |= ~(changrp_mask << 2) & 0x3c;
928 ols->flag_reg |= FLAG_FILTER;
3a4d09c0
GM
929 ols->rle_count = 0;
930 data = (ols->flag_reg << 24) | ((ols->flag_reg << 8) & 0xff0000);
e46b8fb1
UH
931 if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SR_OK)
932 return SR_ERR;
a1bb33af 933
43fc7885 934 /* Start acquisition on the device. */
e46b8fb1
UH
935 if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SR_OK)
936 return SR_ERR;
a1bb33af 937
6f1be0a2 938 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
9c939c51 939 session_data);
a1bb33af 940
b53738ba
UH
941 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
942 sr_err("ols: %s: packet malloc failed", __func__);
943 return SR_ERR_MALLOC;
944 }
945
946 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
947 sr_err("ols: %s: header malloc failed", __func__);
c0a4b971 948 g_free(packet);
b53738ba
UH
949 return SR_ERR_MALLOC;
950 }
951
43fc7885 952 /* Send header packet to the session bus. */
5a2326a7 953 packet->type = SR_DF_HEADER;
43fc7885 954 packet->payload = (unsigned char *)header;
a1bb33af
UH
955 header->feed_version = 1;
956 gettimeofday(&header->starttime, NULL);
4fe9a6da 957 header->samplerate = ols->cur_samplerate;
c2616fb9
DR
958 header->num_logic_probes = NUM_PROBES;
959 header->num_analog_probes = 0;
9c939c51 960 sr_session_bus(session_data, packet);
c0a4b971 961
a1bb33af
UH
962 g_free(header);
963 g_free(packet);
964
e46b8fb1 965 return SR_OK;
a1bb33af
UH
966}
967
a1bb33af
UH
968static void hw_stop_acquisition(int device_index, gpointer session_device_id)
969{
b9c735a2 970 struct sr_datafeed_packet packet;
a1bb33af 971
17e1afcb 972 /* Avoid compiler warnings. */
afc8e4de
UH
973 device_index = device_index;
974
5a2326a7 975 packet.type = SR_DF_END;
8a2efef2 976 sr_session_bus(session_device_id, &packet);
a1bb33af
UH
977}
978
5c2d46d1 979struct sr_device_plugin ols_plugin_info = {
e519ba86
UH
980 .name = "ols",
981 .longname = "Openbench Logic Sniffer",
982 .api_version = 1,
983 .init = hw_init,
984 .cleanup = hw_cleanup,
86f5e3d8
UH
985 .opendev = hw_opendev,
986 .closedev = hw_closedev,
e519ba86
UH
987 .get_device_info = hw_get_device_info,
988 .get_status = hw_get_status,
989 .get_capabilities = hw_get_capabilities,
990 .set_configuration = hw_set_configuration,
991 .start_acquisition = hw_start_acquisition,
992 .stop_acquisition = hw_stop_acquisition,
a1bb33af 993};