]> sigrok.org Git - libsigrok.git/blame - src/hardware/greatfet/api.c
greatfet: support capture of the upper pin bank (first pin 8)
[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 }
310
311 sr_sw_limits_init(&devc->sw_limits);
312 devc->samplerate = DEFAULT_SAMPLERATE;
313 devc->acquisition.bandwidth_threshold = BANDWIDTH_THRESHOLD;
314 devc->acquisition.control_interface = CONTROL_INTERFACE;
315 devc->acquisition.samples_interface = SAMPLES_INTERFACE;
316 devc->acquisition.acquisition_state = ACQ_IDLE;
317
318 devices = g_slist_append(devices, sdi);
319 }
320 g_slist_free(conn_devices);
f594b3b0 321
208fcedc 322 return std_scan_complete(di, devices);
f594b3b0
GS
323}
324
325static int dev_open(struct sr_dev_inst *sdi)
326{
208fcedc
GS
327 struct sr_dev_driver *di;
328 struct drv_context *drvc;
329 struct sr_context *ctx;
330 struct sr_usb_dev_inst *usb;
f594b3b0 331
208fcedc
GS
332 di = sdi->driver;
333 drvc = di->context;
334 ctx = drvc->sr_ctx;
335 usb = sdi->conn;
f594b3b0 336
208fcedc 337 return sr_usb_open(ctx->libusb_ctx, usb);
f594b3b0
GS
338}
339
340static int dev_close(struct sr_dev_inst *sdi)
341{
208fcedc
GS
342 struct sr_usb_dev_inst *usb;
343 struct dev_context *devc;
344 struct dev_acquisition_t *acq;
345
346 if (!sdi)
347 return SR_ERR_ARG;
348 usb = sdi->conn;
349 devc = sdi->priv;
350 if (!devc)
351 return SR_ERR_ARG;
352 acq = &devc->acquisition;
353
354 greatfet_release_resources(sdi);
355
356 if (!usb->devhdl)
357 return SR_ERR_BUG;
358
359 sr_info("Closing device on %s interface %d.",
360 sdi->connection_id, acq->control_interface);
361 if (acq->control_interface_claimed) {
362 libusb_release_interface(usb->devhdl, acq->control_interface);
363 acq->control_interface_claimed = FALSE;
364 }
365 sr_usb_close(usb);
f594b3b0
GS
366
367 return SR_OK;
368}
369
208fcedc
GS
370static void clear_helper(struct dev_context *devc)
371{
372 greatfet_free_devc(devc);
373}
374
375static int dev_clear(const struct sr_dev_driver *driver)
376{
377 return std_dev_clear_with_callback(driver,
378 (std_dev_clear_callback)clear_helper);
379}
380
f594b3b0
GS
381static int config_get(uint32_t key, GVariant **data,
382 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
383{
208fcedc
GS
384 struct dev_context *devc;
385
386 if (!sdi)
387 return SR_ERR_ARG;
388 devc = sdi->priv;
389
390 /* Handle requests for the "Logic" channel group. */
391 if (cg) {
392 switch (key) {
393 default:
394 return SR_ERR_NA;
395 }
396 }
f594b3b0 397
208fcedc 398 /* Handle global options for the device. */
f594b3b0 399 switch (key) {
208fcedc
GS
400 case SR_CONF_CONN:
401 if (!sdi->connection_id)
402 return SR_ERR_NA;
403 *data = g_variant_new_string(sdi->connection_id);
404 return SR_OK;
405 case SR_CONF_CONTINUOUS:
406 *data = g_variant_new_boolean(TRUE);
407 return SR_OK;
408 case SR_CONF_SAMPLERATE:
409 if (!devc)
410 return SR_ERR_NA;
411 *data = g_variant_new_uint64(devc->samplerate);
412 return SR_OK;
413 case SR_CONF_LIMIT_SAMPLES:
414 case SR_CONF_LIMIT_MSEC:
415 if (!devc)
416 return SR_ERR_NA;
417 return sr_sw_limits_config_get(&devc->sw_limits, key, data);
f594b3b0
GS
418 default:
419 return SR_ERR_NA;
420 }
f594b3b0
GS
421}
422
423static int config_set(uint32_t key, GVariant *data,
424 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
425{
208fcedc 426 struct dev_context *devc;
f594b3b0 427
208fcedc 428 devc = sdi->priv;
f594b3b0 429
208fcedc
GS
430 /* Handle requests for the "Logic" channel group. */
431 if (cg) {
432 switch (key) {
433 default:
434 return SR_ERR_NA;
435 }
436 }
437
438 /* Handle global options for the device. */
f594b3b0 439 switch (key) {
208fcedc
GS
440 case SR_CONF_SAMPLERATE:
441 if (!devc)
442 return SR_ERR_NA;
443 devc->samplerate = g_variant_get_uint64(data);
444 return SR_OK;
445 case SR_CONF_LIMIT_SAMPLES:
446 case SR_CONF_LIMIT_MSEC:
447 if (!devc)
448 return SR_ERR_NA;
449 return sr_sw_limits_config_set(&devc->sw_limits, key, data);
f594b3b0 450 default:
208fcedc 451 return SR_ERR_NA;
f594b3b0 452 }
f594b3b0
GS
453}
454
455static int config_list(uint32_t key, GVariant **data,
456 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
457{
f594b3b0 458
208fcedc
GS
459 /* Handle requests for the "Logic" channel group. */
460 if (cg) {
461 switch (key) {
462 case SR_CONF_DEVICE_OPTIONS:
463 if (ARRAY_SIZE(devopts_cg) == 0)
464 return SR_ERR_NA;
465 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
466 ARRAY_AND_SIZE(devopts_cg),
467 sizeof(devopts_cg[0]));
468 return SR_OK;
469 default:
470 return SR_ERR_NA;
471 }
472 }
f594b3b0 473
208fcedc 474 /* Handle global options for the device. */
f594b3b0 475 switch (key) {
208fcedc
GS
476 case SR_CONF_SCAN_OPTIONS:
477 case SR_CONF_DEVICE_OPTIONS:
478 return STD_CONFIG_LIST(key, data, sdi, cg,
479 scanopts, drvopts, devopts);
480 case SR_CONF_SAMPLERATE:
481 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
482 return SR_OK;
f594b3b0
GS
483 default:
484 return SR_ERR_NA;
485 }
f594b3b0
GS
486}
487
488static int dev_acquisition_start(const struct sr_dev_inst *sdi)
489{
208fcedc
GS
490 struct sr_dev_driver *di;
491 struct drv_context *drvc;
492 struct sr_context *ctx;
493 struct dev_context *devc;
494 struct dev_acquisition_t *acq;
495 int ret;
f594b3b0 496
208fcedc
GS
497 if (!sdi || !sdi->driver || !sdi->priv)
498 return SR_ERR_ARG;
499 di = sdi->driver;
500 drvc = di->context;
501 ctx = drvc->sr_ctx;
502 devc = sdi->priv;
503 acq = &devc->acquisition;
504
505 acq->acquisition_state = ACQ_PREPARE;
506
507 ret = greatfet_setup_acquisition(sdi);
508 if (ret != SR_OK)
509 return ret;
510
511 if (!acq->feed_queue) {
512 acq->feed_queue = feed_queue_logic_alloc(sdi,
513 BUFFER_SIZE, acq->unit_size);
514 if (!acq->feed_queue) {
515 sr_err("Cannot allocate session feed buffer.");
516 return SR_ERR_MALLOC;
517 }
518 }
519
520 sr_sw_limits_acquisition_start(&devc->sw_limits);
521
522 ret = greatfet_start_acquisition(sdi);
523 acq->start_req_sent = ret == SR_OK;
524 if (ret != SR_OK) {
525 greatfet_abort_acquisition(sdi);
526 feed_queue_logic_free(acq->feed_queue);
527 acq->feed_queue = NULL;
528 return ret;
529 }
530 acq->acquisition_state = ACQ_RECEIVE;
531
532 usb_source_add(sdi->session, ctx, 50,
533 greatfet_receive_data, (void *)sdi);
534
535 ret = std_session_send_df_header(sdi);
536 acq->frame_begin_sent = ret == SR_OK;
537 (void)sr_session_send_meta(sdi, SR_CONF_SAMPLERATE,
538 g_variant_new_uint64(acq->capture_samplerate));
f594b3b0
GS
539
540 return SR_OK;
541}
542
543static int dev_acquisition_stop(struct sr_dev_inst *sdi)
544{
208fcedc 545 greatfet_abort_acquisition(sdi);
f594b3b0
GS
546 return SR_OK;
547}
548
549static struct sr_dev_driver greatfet_driver_info = {
550 .name = "greatfet",
208fcedc 551 .longname = "Great Scott Gadgets GreatFET One",
f594b3b0
GS
552 .api_version = 1,
553 .init = std_init,
554 .cleanup = std_cleanup,
555 .scan = scan,
556 .dev_list = std_dev_list,
208fcedc 557 .dev_clear = dev_clear,
f594b3b0
GS
558 .config_get = config_get,
559 .config_set = config_set,
560 .config_list = config_list,
561 .dev_open = dev_open,
562 .dev_close = dev_close,
563 .dev_acquisition_start = dev_acquisition_start,
564 .dev_acquisition_stop = dev_acquisition_stop,
565 .context = NULL,
566};
567SR_REGISTER_DEV_DRIVER(greatfet_driver_info);