2 * This file is part of the libsigrok project.
4 * Copyright (C) 2019 Katherine J. Temkin <k@ktemkin.com>
5 * Copyright (C) 2019 Mikaela Szekely <qyriad@gmail.com>
6 * Copyright (C) 2023 Gerhard Sittig <gerhard.sittig@gmx.net>
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.
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.
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/>.
26 #define DEFAULT_CONN "1d50.60e6"
27 #define CONTROL_INTERFACE 0
28 #define SAMPLES_INTERFACE 1
30 #define VENDOR_TEXT "Great Scott Gadgets"
31 #define MODEL_TEXT "GreatFET"
33 #define BUFFER_SIZE (4 * 1024 * 1024)
35 #define DEFAULT_SAMPLERATE SR_KHZ(34000)
36 #define BANDWIDTH_THRESHOLD (SR_MHZ(42) * 8)
38 #define WITH_16CHAN_SUPPORT 0
40 static const uint32_t scanopts[] = {
45 static const uint32_t drvopts[] = {
46 SR_CONF_LOGIC_ANALYZER,
49 static const uint32_t devopts[] = {
50 SR_CONF_CONTINUOUS | SR_CONF_GET,
51 SR_CONF_CONN | SR_CONF_GET,
52 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
53 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
54 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
57 static const uint32_t devopts_cg[] = {
61 static const char *channel_names[] = {
62 "SGPIO0", "SGPIO1", "SGPIO2", "SGPIO3",
63 "SGPIO4", "SGPIO5", "SGPIO6", "SGPIO7",
64 #if WITH_16CHAN_SUPPORT
65 "SGPIO8", "SGPIO9", "SGPIO10", "SGPIO11",
66 "SGPIO12", "SGPIO13", "SGPIO14", "SGPIO15",
71 * The seemingly odd samplerates result from the 204MHz base clock and
72 * a 12bit integer divider. Theoretical minimum could be 50kHz but we
73 * don't bother to provide so low a selection item here.
75 * When users specify different samplerates, device firmware will pick
76 * the minimum rate which satisfies the user's request.
78 static const uint64_t samplerates[] = {
79 SR_KHZ(1000), /* 1.0MHz */
80 SR_KHZ(2000), /* 2.0MHz */
81 SR_KHZ(4000), /* 4.0MHz */
82 SR_KHZ(8500), /* 8.5MHz */
83 SR_KHZ(10200), /* 10.2MHz */
84 SR_KHZ(12000), /* 12.0MHz */
85 SR_KHZ(17000), /* 17.0MHz */
86 SR_KHZ(20400), /* 20.4MHz, the maximum for 16 channels */
87 SR_KHZ(25500), /* 25.5MHz */
88 SR_KHZ(34000), /* 34.0MHz */
89 SR_KHZ(40800), /* 40.8MHz, the maximum for 8 channels */
90 SR_KHZ(51000), /* 51.0MHz */
91 SR_KHZ(68000), /* 68.0MHz, the maximum for 4 channels */
92 SR_KHZ(102000), /* 102.0MHz, the maximum for 2 channels */
93 SR_KHZ(204000), /* 204.0MHz, the maximum for 1 channel */
96 static void greatfet_free_devc(struct dev_context *devc)
103 devc->sdi->priv = NULL;
105 g_string_free(devc->usb_comm_buffer, TRUE);
106 g_free(devc->firmware_version);
107 g_free(devc->serial_number);
108 sr_free_probe_names(devc->channel_names);
109 feed_queue_logic_free(devc->acquisition.feed_queue);
110 g_free(devc->transfers.transfers);
111 g_free(devc->transfers.transfer_buffer);
113 * USB transfers should not have been allocated when we get here
114 * during device probe/scan, or during shutdown after acquisition
121 static void greatfet_free_sdi(struct sr_dev_inst *sdi)
123 struct sr_usb_dev_inst *usb;
124 struct dev_context *devc;
131 if (usb && usb->devhdl)
133 sr_usb_dev_inst_free(usb);
137 greatfet_free_devc(devc);
139 sr_dev_inst_free(sdi);
142 static GSList *scan(struct sr_dev_driver *di, GSList *options)
144 struct drv_context *drvc;
145 struct sr_context *ctx;
147 const char *conn, *probe_names;
148 const char *want_snr;
149 struct sr_config *src;
150 GSList *conn_devices, *l;
151 struct sr_dev_inst *sdi;
152 struct dev_context *devc;
153 struct sr_usb_dev_inst *usb;
154 gboolean skip_device;
155 struct libusb_device *dev;
156 struct libusb_device_descriptor des;
158 char serno_txt[64], conn_id[64];
160 size_t ch_off, ch_max, ch_idx;
162 struct sr_channel *ch;
163 struct sr_channel_group *cg;
170 /* Accept user specs for conn= and probe names. */
173 for (l = options; l; l = l->next) {
177 conn = g_variant_get_string(src->data, NULL);
179 case SR_CONF_PROBE_NAMES:
180 probe_names = g_variant_get_string(src->data, NULL);
186 * By default search for all devices with the expected VID/PID.
187 * Accept external specs in either "bus.addr" or "vid.pid" form.
188 * As an alternative accept "sn=..." specs and keep using the
189 * default VID/PID in that case. This should result in maximum
190 * usability while still using a maximum amount of common code.
193 if (g_str_has_prefix(conn, "sn=")) {
194 want_snr = conn + strlen("sn=");
196 sr_info("Searching default %s and serial number %s.",
199 conn_devices = sr_usb_find(ctx->libusb_ctx, conn);
204 * Iterate over all devices that have the matching VID/PID.
205 * Skip those which we cannot open. Skip those which don't
206 * match additional serial number conditions. Allocate the
207 * structs for found devices "early", to re-use common code
208 * for communication to the firmware. Release these structs
209 * when identification fails or the device does not match.
211 * Notice that the scan for devices uses the USB string for
212 * the serial number, and does a weak check (partial match).
213 * This allows users to either use lsusb(8) or gf(1) output
214 * as well as match lazily when only part of the serial nr is
215 * known and becomes unique. Matching against serial nr and
216 * finding multiple devices is as acceptable, just might be a
217 * rare use case. Failure in this stage is silent, there are
218 * legal reasons why we cannot access a device during scan.
220 * Once a device was found usable, we get its serial number
221 * and version details by means of firmware communication.
222 * To verify that the firmware is operational and that the
223 * protocol works to a minimum degree. And to present data
224 * in --scan output which matches the vendor's gf(1) utility.
225 * This version detail is _not_ checked against conn= specs
226 * because users may specify the longer text string with
227 * more leading digits from lsusb(8) output. That test would
228 * fail when executed against the shorter firmware output.
230 for (l = conn_devices; l; l = l->next) {
233 ret = sr_usb_open(ctx->libusb_ctx, usb);
239 dev = libusb_get_device(usb->devhdl);
240 ret = libusb_get_device_descriptor(dev, &des);
241 if (ret != 0 || !des.iSerialNumber) {
245 ret = libusb_get_string_descriptor_ascii(usb->devhdl,
247 (uint8_t *)serno_txt, sizeof(serno_txt));
252 match = strstr(serno_txt, want_snr);
253 skip_device = !match;
254 sr_dbg("got serno %s, checking %s, match %d",
255 serno_txt, want_snr, !!match);
262 sdi = g_malloc0(sizeof(*sdi));
264 sdi->inst_type = SR_INST_USB;
265 sdi->status = SR_ST_INACTIVE;
266 devc = g_malloc0(sizeof(*devc));
269 devc->usb_comm_buffer = NULL;
272 * Get the serial number by way of device communication.
273 * Get the firmware version. Failure is fatal.
275 ret = greatfet_get_serial_number(sdi);
276 if (ret != SR_OK || !devc->serial_number) {
277 sr_err("Cannot get serial number.");
278 greatfet_free_sdi(sdi);
281 ret = greatfet_get_version_number(sdi);
282 if (ret != SR_OK || !devc->firmware_version) {
283 sr_err("Cannot get firmware version.");
284 greatfet_free_sdi(sdi);
288 /* Continue filling in sdi and devc. */
289 snprintf(conn_id, sizeof(conn_id), "%u.%u",
290 usb->bus, usb->address);
291 sdi->connection_id = g_strdup(conn_id);
294 sdi->vendor = g_strdup(VENDOR_TEXT);
295 sdi->model = g_strdup(MODEL_TEXT);
296 sdi->version = g_strdup(devc->firmware_version);
297 sdi->serial_num = g_strdup(devc->serial_number);
299 /* Create the "Logic" channel group. */
301 ch_max = ARRAY_SIZE(channel_names);
302 devc->channel_names = sr_parse_probe_names(probe_names,
303 channel_names, ch_max, ch_max, &ch_max);
304 devc->channel_count = ch_max;
305 cg = sr_channel_group_new(sdi, "Logic", NULL);
306 for (ch_idx = 0; ch_idx < ch_max; ch_idx++) {
307 enabled = ch_idx < 8;
308 ch = sr_channel_new(sdi, ch_off,
309 SR_CHANNEL_LOGIC, enabled,
310 devc->channel_names[ch_idx]);
312 cg->channels = g_slist_append(cg->channels, ch);
315 sr_sw_limits_init(&devc->sw_limits);
316 devc->samplerate = DEFAULT_SAMPLERATE;
317 devc->acquisition.bandwidth_threshold = BANDWIDTH_THRESHOLD;
318 devc->acquisition.control_interface = CONTROL_INTERFACE;
319 devc->acquisition.samples_interface = SAMPLES_INTERFACE;
320 devc->acquisition.acquisition_state = ACQ_IDLE;
322 devices = g_slist_append(devices, sdi);
324 g_slist_free(conn_devices);
326 return std_scan_complete(di, devices);
329 static int dev_open(struct sr_dev_inst *sdi)
331 struct sr_dev_driver *di;
332 struct drv_context *drvc;
333 struct sr_context *ctx;
334 struct sr_usb_dev_inst *usb;
341 return sr_usb_open(ctx->libusb_ctx, usb);
344 static int dev_close(struct sr_dev_inst *sdi)
346 struct sr_usb_dev_inst *usb;
347 struct dev_context *devc;
348 struct dev_acquisition_t *acq;
356 acq = &devc->acquisition;
358 greatfet_release_resources(sdi);
363 sr_info("Closing device on %s interface %d.",
364 sdi->connection_id, acq->control_interface);
365 if (acq->control_interface_claimed) {
366 libusb_release_interface(usb->devhdl, acq->control_interface);
367 acq->control_interface_claimed = FALSE;
374 static void clear_helper(struct dev_context *devc)
376 greatfet_free_devc(devc);
379 static int dev_clear(const struct sr_dev_driver *driver)
381 return std_dev_clear_with_callback(driver,
382 (std_dev_clear_callback)clear_helper);
385 static int config_get(uint32_t key, GVariant **data,
386 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
388 struct dev_context *devc;
394 /* Handle requests for the "Logic" channel group. */
402 /* Handle global options for the device. */
405 if (!sdi->connection_id)
407 *data = g_variant_new_string(sdi->connection_id);
409 case SR_CONF_CONTINUOUS:
410 *data = g_variant_new_boolean(TRUE);
412 case SR_CONF_SAMPLERATE:
415 *data = g_variant_new_uint64(devc->samplerate);
417 case SR_CONF_LIMIT_SAMPLES:
418 case SR_CONF_LIMIT_MSEC:
421 return sr_sw_limits_config_get(&devc->sw_limits, key, data);
427 static int config_set(uint32_t key, GVariant *data,
428 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
430 struct dev_context *devc;
434 /* Handle requests for the "Logic" channel group. */
442 /* Handle global options for the device. */
444 case SR_CONF_SAMPLERATE:
447 devc->samplerate = g_variant_get_uint64(data);
449 case SR_CONF_LIMIT_SAMPLES:
450 case SR_CONF_LIMIT_MSEC:
453 return sr_sw_limits_config_set(&devc->sw_limits, key, data);
459 static int config_list(uint32_t key, GVariant **data,
460 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
463 /* Handle requests for the "Logic" channel group. */
466 case SR_CONF_DEVICE_OPTIONS:
467 if (ARRAY_SIZE(devopts_cg) == 0)
469 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
470 ARRAY_AND_SIZE(devopts_cg),
471 sizeof(devopts_cg[0]));
478 /* Handle global options for the device. */
480 case SR_CONF_SCAN_OPTIONS:
481 case SR_CONF_DEVICE_OPTIONS:
482 return STD_CONFIG_LIST(key, data, sdi, cg,
483 scanopts, drvopts, devopts);
484 case SR_CONF_SAMPLERATE:
485 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
492 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
494 struct sr_dev_driver *di;
495 struct drv_context *drvc;
496 struct sr_context *ctx;
497 struct dev_context *devc;
498 struct dev_acquisition_t *acq;
501 if (!sdi || !sdi->driver || !sdi->priv)
507 acq = &devc->acquisition;
509 acq->acquisition_state = ACQ_PREPARE;
511 ret = greatfet_setup_acquisition(sdi);
515 if (!acq->feed_queue) {
516 acq->feed_queue = feed_queue_logic_alloc(sdi,
517 BUFFER_SIZE, acq->unit_size);
518 if (!acq->feed_queue) {
519 sr_err("Cannot allocate session feed buffer.");
520 return SR_ERR_MALLOC;
524 sr_sw_limits_acquisition_start(&devc->sw_limits);
526 ret = greatfet_start_acquisition(sdi);
527 acq->start_req_sent = ret == SR_OK;
529 greatfet_abort_acquisition(sdi);
530 feed_queue_logic_free(acq->feed_queue);
531 acq->feed_queue = NULL;
534 acq->acquisition_state = ACQ_RECEIVE;
536 usb_source_add(sdi->session, ctx, 50,
537 greatfet_receive_data, (void *)sdi);
539 ret = std_session_send_df_header(sdi);
540 acq->frame_begin_sent = ret == SR_OK;
541 (void)sr_session_send_meta(sdi, SR_CONF_SAMPLERATE,
542 g_variant_new_uint64(acq->capture_samplerate));
547 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
549 greatfet_abort_acquisition(sdi);
553 static struct sr_dev_driver greatfet_driver_info = {
555 .longname = "Great Scott Gadgets GreatFET One",
558 .cleanup = std_cleanup,
560 .dev_list = std_dev_list,
561 .dev_clear = dev_clear,
562 .config_get = config_get,
563 .config_set = config_set,
564 .config_list = config_list,
565 .dev_open = dev_open,
566 .dev_close = dev_close,
567 .dev_acquisition_start = dev_acquisition_start,
568 .dev_acquisition_stop = dev_acquisition_stop,
571 SR_REGISTER_DEV_DRIVER(greatfet_driver_info);