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