]> sigrok.org Git - libsigrok.git/blame - src/hardware/greatfet/api.c
greatfet: feed the session with constant width sample data
[libsigrok.git] / src / hardware / greatfet / api.c
CommitLineData
f594b3b0
GS
1/*
2 * This file is part of the libsigrok project.
3 *
208fcedc
GS
4 * Copyright (C) 2019 Katherine J. Temkin <k@ktemkin.com>
5 * Copyright (C) 2019 Mikaela Szekely <qyriad@gmail.com>
f594b3b0
GS
6 * Copyright (C) 2023 Gerhard Sittig <gerhard.sittig@gmx.net>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
208fcedc
GS
22#include "config.h"
23
f594b3b0
GS
24#include "protocol.h"
25
208fcedc
GS
26#define DEFAULT_CONN "1d50.60e6"
27#define CONTROL_INTERFACE 0
28#define SAMPLES_INTERFACE 1
29
30#define VENDOR_TEXT "Great Scott Gadgets"
31#define MODEL_TEXT "GreatFET"
32
33#define BUFFER_SIZE (4 * 1024 * 1024)
34
35#define DEFAULT_SAMPLERATE SR_KHZ(34000)
36#define BANDWIDTH_THRESHOLD (SR_MHZ(42) * 8)
37
208fcedc
GS
38static const uint32_t scanopts[] = {
39 SR_CONF_CONN,
40 SR_CONF_PROBE_NAMES,
41};
42
43static const uint32_t drvopts[] = {
44 SR_CONF_LOGIC_ANALYZER,
45};
46
47static const uint32_t devopts[] = {
48 SR_CONF_CONTINUOUS | SR_CONF_GET,
49 SR_CONF_CONN | SR_CONF_GET,
50 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
51 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
52 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
53};
54
55static const uint32_t devopts_cg[] = {
56 /* EMPTY */
57};
58
59static const char *channel_names[] = {
60 "SGPIO0", "SGPIO1", "SGPIO2", "SGPIO3",
61 "SGPIO4", "SGPIO5", "SGPIO6", "SGPIO7",
208fcedc
GS
62 "SGPIO8", "SGPIO9", "SGPIO10", "SGPIO11",
63 "SGPIO12", "SGPIO13", "SGPIO14", "SGPIO15",
208fcedc
GS
64};
65
66/*
67 * The seemingly odd samplerates result from the 204MHz base clock and
68 * a 12bit integer divider. Theoretical minimum could be 50kHz but we
69 * don't bother to provide so low a selection item here.
70 *
71 * When users specify different samplerates, device firmware will pick
72 * the minimum rate which satisfies the user's request.
73 */
74static const uint64_t samplerates[] = {
75 SR_KHZ(1000), /* 1.0MHz */
76 SR_KHZ(2000), /* 2.0MHz */
77 SR_KHZ(4000), /* 4.0MHz */
78 SR_KHZ(8500), /* 8.5MHz */
79 SR_KHZ(10200), /* 10.2MHz */
80 SR_KHZ(12000), /* 12.0MHz */
81 SR_KHZ(17000), /* 17.0MHz */
82 SR_KHZ(20400), /* 20.4MHz, the maximum for 16 channels */
83 SR_KHZ(25500), /* 25.5MHz */
84 SR_KHZ(34000), /* 34.0MHz */
85 SR_KHZ(40800), /* 40.8MHz, the maximum for 8 channels */
86 SR_KHZ(51000), /* 51.0MHz */
87 SR_KHZ(68000), /* 68.0MHz, the maximum for 4 channels */
88 SR_KHZ(102000), /* 102.0MHz, the maximum for 2 channels */
89 SR_KHZ(204000), /* 204.0MHz, the maximum for 1 channel */
90};
91
92static void greatfet_free_devc(struct dev_context *devc)
93{
94
95 if (!devc)
96 return;
97
98 if (devc->sdi)
99 devc->sdi->priv = NULL;
100
101 g_string_free(devc->usb_comm_buffer, TRUE);
102 g_free(devc->firmware_version);
103 g_free(devc->serial_number);
104 sr_free_probe_names(devc->channel_names);
105 feed_queue_logic_free(devc->acquisition.feed_queue);
106 g_free(devc->transfers.transfers);
107 g_free(devc->transfers.transfer_buffer);
108 /*
109 * USB transfers should not have been allocated when we get here
110 * during device probe/scan, or during shutdown after acquisition
111 * has terminated.
112 */
113
114 g_free(devc);
115}
116
117static void greatfet_free_sdi(struct sr_dev_inst *sdi)
118{
119 struct sr_usb_dev_inst *usb;
120 struct dev_context *devc;
121
122 if (!sdi)
123 return;
124
125 usb = sdi->conn;
126 sdi->conn = NULL;
127 if (usb && usb->devhdl)
128 sr_usb_close(usb);
129 sr_usb_dev_inst_free(usb);
130
131 devc = sdi->priv;
132 sdi->priv = NULL;
133 greatfet_free_devc(devc);
134
135 sr_dev_inst_free(sdi);
136}
f594b3b0
GS
137
138static GSList *scan(struct sr_dev_driver *di, GSList *options)
139{
140 struct drv_context *drvc;
208fcedc 141 struct sr_context *ctx;
f594b3b0 142 GSList *devices;
208fcedc
GS
143 const char *conn, *probe_names;
144 const char *want_snr;
145 struct sr_config *src;
146 GSList *conn_devices, *l;
147 struct sr_dev_inst *sdi;
148 struct dev_context *devc;
149 struct sr_usb_dev_inst *usb;
150 gboolean skip_device;
151 struct libusb_device *dev;
152 struct libusb_device_descriptor des;
153 char *match;
154 char serno_txt[64], conn_id[64];
155 int ret;
156 size_t ch_off, ch_max, ch_idx;
157 gboolean enabled;
158 struct sr_channel *ch;
159 struct sr_channel_group *cg;
f594b3b0 160
208fcedc
GS
161 drvc = di->context;
162 ctx = drvc->sr_ctx;
f594b3b0
GS
163
164 devices = NULL;
f594b3b0 165
208fcedc
GS
166 /* Accept user specs for conn= and probe names. */
167 conn = DEFAULT_CONN;
168 probe_names = NULL;
169 for (l = options; l; l = l->next) {
170 src = l->data;
171 switch (src->key) {
172 case SR_CONF_CONN:
173 conn = g_variant_get_string(src->data, NULL);
174 break;
175 case SR_CONF_PROBE_NAMES:
176 probe_names = g_variant_get_string(src->data, NULL);
177 break;
178 }
179 }
180
181 /*
182 * By default search for all devices with the expected VID/PID.
183 * Accept external specs in either "bus.addr" or "vid.pid" form.
184 * As an alternative accept "sn=..." specs and keep using the
185 * default VID/PID in that case. This should result in maximum
186 * usability while still using a maximum amount of common code.
187 */
188 want_snr = NULL;
189 if (g_str_has_prefix(conn, "sn=")) {
190 want_snr = conn + strlen("sn=");
191 conn = DEFAULT_CONN;
192 sr_info("Searching default %s and serial number %s.",
193 conn, want_snr);
194 }
195 conn_devices = sr_usb_find(ctx->libusb_ctx, conn);
196 if (!conn_devices)
197 return devices;
198
199 /*
200 * Iterate over all devices that have the matching VID/PID.
201 * Skip those which we cannot open. Skip those which don't
202 * match additional serial number conditions. Allocate the
203 * structs for found devices "early", to re-use common code
204 * for communication to the firmware. Release these structs
205 * when identification fails or the device does not match.
206 *
207 * Notice that the scan for devices uses the USB string for
208 * the serial number, and does a weak check (partial match).
209 * This allows users to either use lsusb(8) or gf(1) output
210 * as well as match lazily when only part of the serial nr is
211 * known and becomes unique. Matching against serial nr and
212 * finding multiple devices is as acceptable, just might be a
213 * rare use case. Failure in this stage is silent, there are
214 * legal reasons why we cannot access a device during scan.
215 *
216 * Once a device was found usable, we get its serial number
217 * and version details by means of firmware communication.
218 * To verify that the firmware is operational and that the
219 * protocol works to a minimum degree. And to present data
220 * in --scan output which matches the vendor's gf(1) utility.
221 * This version detail is _not_ checked against conn= specs
222 * because users may specify the longer text string with
223 * more leading digits from lsusb(8) output. That test would
224 * fail when executed against the shorter firmware output.
225 */
226 for (l = conn_devices; l; l = l->next) {
227 usb = l->data;
228
229 ret = sr_usb_open(ctx->libusb_ctx, usb);
230 if (ret != SR_OK)
231 continue;
232
233 skip_device = FALSE;
234 if (want_snr) do {
235 dev = libusb_get_device(usb->devhdl);
236 ret = libusb_get_device_descriptor(dev, &des);
237 if (ret != 0 || !des.iSerialNumber) {
238 skip_device = TRUE;
239 break;
240 }
241 ret = libusb_get_string_descriptor_ascii(usb->devhdl,
242 des.iSerialNumber,
243 (uint8_t *)serno_txt, sizeof(serno_txt));
244 if (ret < 0) {
245 skip_device = TRUE;
246 break;
247 }
248 match = strstr(serno_txt, want_snr);
249 skip_device = !match;
250 sr_dbg("got serno %s, checking %s, match %d",
251 serno_txt, want_snr, !!match);
252 } while (0);
253 if (skip_device) {
254 sr_usb_close(usb);
255 continue;
256 }
257
258 sdi = g_malloc0(sizeof(*sdi));
259 sdi->conn = usb;
260 sdi->inst_type = SR_INST_USB;
261 sdi->status = SR_ST_INACTIVE;
262 devc = g_malloc0(sizeof(*devc));
263 sdi->priv = devc;
264 devc->sdi = sdi;
265 devc->usb_comm_buffer = NULL;
266
267 /*
268 * Get the serial number by way of device communication.
269 * Get the firmware version. Failure is fatal.
270 */
271 ret = greatfet_get_serial_number(sdi);
272 if (ret != SR_OK || !devc->serial_number) {
273 sr_err("Cannot get serial number.");
274 greatfet_free_sdi(sdi);
275 continue;
276 }
277 ret = greatfet_get_version_number(sdi);
278 if (ret != SR_OK || !devc->firmware_version) {
279 sr_err("Cannot get firmware version.");
280 greatfet_free_sdi(sdi);
281 continue;
282 }
283
284 /* Continue filling in sdi and devc. */
285 snprintf(conn_id, sizeof(conn_id), "%u.%u",
286 usb->bus, usb->address);
287 sdi->connection_id = g_strdup(conn_id);
288 sr_usb_close(usb);
289
290 sdi->vendor = g_strdup(VENDOR_TEXT);
291 sdi->model = g_strdup(MODEL_TEXT);
292 sdi->version = g_strdup(devc->firmware_version);
293 sdi->serial_num = g_strdup(devc->serial_number);
294
295 /* Create the "Logic" channel group. */
296 ch_off = 0;
297 ch_max = ARRAY_SIZE(channel_names);
298 devc->channel_names = sr_parse_probe_names(probe_names,
299 channel_names, ch_max, ch_max, &ch_max);
300 devc->channel_count = ch_max;
301 cg = sr_channel_group_new(sdi, "Logic", NULL);
302 for (ch_idx = 0; ch_idx < ch_max; ch_idx++) {
303 enabled = ch_idx < 8;
304 ch = sr_channel_new(sdi, ch_off,
305 SR_CHANNEL_LOGIC, enabled,
306 devc->channel_names[ch_idx]);
307 ch_off++;
308 cg->channels = g_slist_append(cg->channels, ch);
309 }
66041863 310 devc->feed_unit_size = (ch_max + 8 - 1) / 8;
208fcedc
GS
311
312 sr_sw_limits_init(&devc->sw_limits);
313 devc->samplerate = DEFAULT_SAMPLERATE;
314 devc->acquisition.bandwidth_threshold = BANDWIDTH_THRESHOLD;
315 devc->acquisition.control_interface = CONTROL_INTERFACE;
316 devc->acquisition.samples_interface = SAMPLES_INTERFACE;
317 devc->acquisition.acquisition_state = ACQ_IDLE;
318
319 devices = g_slist_append(devices, sdi);
320 }
321 g_slist_free(conn_devices);
f594b3b0 322
208fcedc 323 return std_scan_complete(di, devices);
f594b3b0
GS
324}
325
326static int dev_open(struct sr_dev_inst *sdi)
327{
208fcedc
GS
328 struct sr_dev_driver *di;
329 struct drv_context *drvc;
330 struct sr_context *ctx;
331 struct sr_usb_dev_inst *usb;
f594b3b0 332
208fcedc
GS
333 di = sdi->driver;
334 drvc = di->context;
335 ctx = drvc->sr_ctx;
336 usb = sdi->conn;
f594b3b0 337
208fcedc 338 return sr_usb_open(ctx->libusb_ctx, usb);
f594b3b0
GS
339}
340
341static int dev_close(struct sr_dev_inst *sdi)
342{
208fcedc
GS
343 struct sr_usb_dev_inst *usb;
344 struct dev_context *devc;
345 struct dev_acquisition_t *acq;
346
347 if (!sdi)
348 return SR_ERR_ARG;
349 usb = sdi->conn;
350 devc = sdi->priv;
351 if (!devc)
352 return SR_ERR_ARG;
353 acq = &devc->acquisition;
354
355 greatfet_release_resources(sdi);
356
357 if (!usb->devhdl)
358 return SR_ERR_BUG;
359
360 sr_info("Closing device on %s interface %d.",
361 sdi->connection_id, acq->control_interface);
362 if (acq->control_interface_claimed) {
363 libusb_release_interface(usb->devhdl, acq->control_interface);
364 acq->control_interface_claimed = FALSE;
365 }
366 sr_usb_close(usb);
f594b3b0
GS
367
368 return SR_OK;
369}
370
208fcedc
GS
371static void clear_helper(struct dev_context *devc)
372{
373 greatfet_free_devc(devc);
374}
375
376static int dev_clear(const struct sr_dev_driver *driver)
377{
378 return std_dev_clear_with_callback(driver,
379 (std_dev_clear_callback)clear_helper);
380}
381
f594b3b0
GS
382static int config_get(uint32_t key, GVariant **data,
383 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
384{
208fcedc
GS
385 struct dev_context *devc;
386
387 if (!sdi)
388 return SR_ERR_ARG;
389 devc = sdi->priv;
390
391 /* Handle requests for the "Logic" channel group. */
392 if (cg) {
393 switch (key) {
394 default:
395 return SR_ERR_NA;
396 }
397 }
f594b3b0 398
208fcedc 399 /* Handle global options for the device. */
f594b3b0 400 switch (key) {
208fcedc
GS
401 case SR_CONF_CONN:
402 if (!sdi->connection_id)
403 return SR_ERR_NA;
404 *data = g_variant_new_string(sdi->connection_id);
405 return SR_OK;
406 case SR_CONF_CONTINUOUS:
407 *data = g_variant_new_boolean(TRUE);
408 return SR_OK;
409 case SR_CONF_SAMPLERATE:
410 if (!devc)
411 return SR_ERR_NA;
412 *data = g_variant_new_uint64(devc->samplerate);
413 return SR_OK;
414 case SR_CONF_LIMIT_SAMPLES:
415 case SR_CONF_LIMIT_MSEC:
416 if (!devc)
417 return SR_ERR_NA;
418 return sr_sw_limits_config_get(&devc->sw_limits, key, data);
f594b3b0
GS
419 default:
420 return SR_ERR_NA;
421 }
f594b3b0
GS
422}
423
424static int config_set(uint32_t key, GVariant *data,
425 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
426{
208fcedc 427 struct dev_context *devc;
f594b3b0 428
208fcedc 429 devc = sdi->priv;
f594b3b0 430
208fcedc
GS
431 /* Handle requests for the "Logic" channel group. */
432 if (cg) {
433 switch (key) {
434 default:
435 return SR_ERR_NA;
436 }
437 }
438
439 /* Handle global options for the device. */
f594b3b0 440 switch (key) {
208fcedc
GS
441 case SR_CONF_SAMPLERATE:
442 if (!devc)
443 return SR_ERR_NA;
444 devc->samplerate = g_variant_get_uint64(data);
445 return SR_OK;
446 case SR_CONF_LIMIT_SAMPLES:
447 case SR_CONF_LIMIT_MSEC:
448 if (!devc)
449 return SR_ERR_NA;
450 return sr_sw_limits_config_set(&devc->sw_limits, key, data);
f594b3b0 451 default:
208fcedc 452 return SR_ERR_NA;
f594b3b0 453 }
f594b3b0
GS
454}
455
456static int config_list(uint32_t key, GVariant **data,
457 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
458{
f594b3b0 459
208fcedc
GS
460 /* Handle requests for the "Logic" channel group. */
461 if (cg) {
462 switch (key) {
463 case SR_CONF_DEVICE_OPTIONS:
464 if (ARRAY_SIZE(devopts_cg) == 0)
465 return SR_ERR_NA;
466 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
467 ARRAY_AND_SIZE(devopts_cg),
468 sizeof(devopts_cg[0]));
469 return SR_OK;
470 default:
471 return SR_ERR_NA;
472 }
473 }
f594b3b0 474
208fcedc 475 /* Handle global options for the device. */
f594b3b0 476 switch (key) {
208fcedc
GS
477 case SR_CONF_SCAN_OPTIONS:
478 case SR_CONF_DEVICE_OPTIONS:
479 return STD_CONFIG_LIST(key, data, sdi, cg,
480 scanopts, drvopts, devopts);
481 case SR_CONF_SAMPLERATE:
482 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
483 return SR_OK;
f594b3b0
GS
484 default:
485 return SR_ERR_NA;
486 }
f594b3b0
GS
487}
488
489static int dev_acquisition_start(const struct sr_dev_inst *sdi)
490{
208fcedc
GS
491 struct sr_dev_driver *di;
492 struct drv_context *drvc;
493 struct sr_context *ctx;
494 struct dev_context *devc;
495 struct dev_acquisition_t *acq;
496 int ret;
f594b3b0 497
208fcedc
GS
498 if (!sdi || !sdi->driver || !sdi->priv)
499 return SR_ERR_ARG;
500 di = sdi->driver;
501 drvc = di->context;
502 ctx = drvc->sr_ctx;
503 devc = sdi->priv;
504 acq = &devc->acquisition;
505
506 acq->acquisition_state = ACQ_PREPARE;
507
508 ret = greatfet_setup_acquisition(sdi);
509 if (ret != SR_OK)
510 return ret;
511
512 if (!acq->feed_queue) {
513 acq->feed_queue = feed_queue_logic_alloc(sdi,
66041863 514 BUFFER_SIZE, devc->feed_unit_size);
208fcedc
GS
515 if (!acq->feed_queue) {
516 sr_err("Cannot allocate session feed buffer.");
517 return SR_ERR_MALLOC;
518 }
519 }
520
521 sr_sw_limits_acquisition_start(&devc->sw_limits);
522
523 ret = greatfet_start_acquisition(sdi);
524 acq->start_req_sent = ret == SR_OK;
525 if (ret != SR_OK) {
526 greatfet_abort_acquisition(sdi);
527 feed_queue_logic_free(acq->feed_queue);
528 acq->feed_queue = NULL;
529 return ret;
530 }
531 acq->acquisition_state = ACQ_RECEIVE;
532
533 usb_source_add(sdi->session, ctx, 50,
534 greatfet_receive_data, (void *)sdi);
535
536 ret = std_session_send_df_header(sdi);
537 acq->frame_begin_sent = ret == SR_OK;
538 (void)sr_session_send_meta(sdi, SR_CONF_SAMPLERATE,
539 g_variant_new_uint64(acq->capture_samplerate));
f594b3b0
GS
540
541 return SR_OK;
542}
543
544static int dev_acquisition_stop(struct sr_dev_inst *sdi)
545{
208fcedc 546 greatfet_abort_acquisition(sdi);
f594b3b0
GS
547 return SR_OK;
548}
549
550static struct sr_dev_driver greatfet_driver_info = {
551 .name = "greatfet",
208fcedc 552 .longname = "Great Scott Gadgets GreatFET One",
f594b3b0
GS
553 .api_version = 1,
554 .init = std_init,
555 .cleanup = std_cleanup,
556 .scan = scan,
557 .dev_list = std_dev_list,
208fcedc 558 .dev_clear = dev_clear,
f594b3b0
GS
559 .config_get = config_get,
560 .config_set = config_set,
561 .config_list = config_list,
562 .dev_open = dev_open,
563 .dev_close = dev_close,
564 .dev_acquisition_start = dev_acquisition_start,
565 .dev_acquisition_stop = dev_acquisition_stop,
566 .context = NULL,
567};
568SR_REGISTER_DEV_DRIVER(greatfet_driver_info);