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 static const uint32_t scanopts[] = {
43 static const uint32_t drvopts[] = {
44 SR_CONF_LOGIC_ANALYZER,
47 static 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,
55 static const uint32_t devopts_cg[] = {
59 static const char *channel_names[] = {
60 "SGPIO0", "SGPIO1", "SGPIO2", "SGPIO3",
61 "SGPIO4", "SGPIO5", "SGPIO6", "SGPIO7",
62 "SGPIO8", "SGPIO9", "SGPIO10", "SGPIO11",
63 "SGPIO12", "SGPIO13", "SGPIO14", "SGPIO15",
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.
71 * When users specify different samplerates, device firmware will pick
72 * the minimum rate which satisfies the user's request.
74 static 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 */
92 static void greatfet_free_devc(struct dev_context *devc)
99 devc->sdi->priv = NULL;
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);
109 * USB transfers should not have been allocated when we get here
110 * during device probe/scan, or during shutdown after acquisition
117 static void greatfet_free_sdi(struct sr_dev_inst *sdi)
119 struct sr_usb_dev_inst *usb;
120 struct dev_context *devc;
127 if (usb && usb->devhdl)
129 sr_usb_dev_inst_free(usb);
133 greatfet_free_devc(devc);
135 sr_dev_inst_free(sdi);
138 static GSList *scan(struct sr_dev_driver *di, GSList *options)
140 struct drv_context *drvc;
141 struct sr_context *ctx;
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;
154 char serno_txt[64], conn_id[64];
156 size_t ch_off, ch_max, ch_idx;
158 struct sr_channel *ch;
159 struct sr_channel_group *cg;
166 /* Accept user specs for conn= and probe names. */
169 for (l = options; l; l = l->next) {
173 conn = g_variant_get_string(src->data, NULL);
175 case SR_CONF_PROBE_NAMES:
176 probe_names = g_variant_get_string(src->data, NULL);
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.
189 if (g_str_has_prefix(conn, "sn=")) {
190 want_snr = conn + strlen("sn=");
192 sr_info("Searching default %s and serial number %s.",
195 conn_devices = sr_usb_find(ctx->libusb_ctx, conn);
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.
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.
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.
226 for (l = conn_devices; l; l = l->next) {
229 ret = sr_usb_open(ctx->libusb_ctx, usb);
235 dev = libusb_get_device(usb->devhdl);
236 ret = libusb_get_device_descriptor(dev, &des);
237 if (ret != 0 || !des.iSerialNumber) {
241 ret = libusb_get_string_descriptor_ascii(usb->devhdl,
243 (uint8_t *)serno_txt, sizeof(serno_txt));
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);
258 sdi = g_malloc0(sizeof(*sdi));
260 sdi->inst_type = SR_INST_USB;
261 sdi->status = SR_ST_INACTIVE;
262 devc = g_malloc0(sizeof(*devc));
265 devc->usb_comm_buffer = NULL;
268 * Get the serial number by way of device communication.
269 * Get the firmware version. Failure is fatal.
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);
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);
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);
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);
295 /* Create the "Logic" channel group. */
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]);
308 cg->channels = g_slist_append(cg->channels, ch);
310 devc->feed_unit_size = (ch_max + 8 - 1) / 8;
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;
319 devices = g_slist_append(devices, sdi);
321 g_slist_free(conn_devices);
323 return std_scan_complete(di, devices);
326 static int dev_open(struct sr_dev_inst *sdi)
328 struct sr_dev_driver *di;
329 struct drv_context *drvc;
330 struct sr_context *ctx;
331 struct sr_usb_dev_inst *usb;
338 return sr_usb_open(ctx->libusb_ctx, usb);
341 static int dev_close(struct sr_dev_inst *sdi)
343 struct sr_usb_dev_inst *usb;
344 struct dev_context *devc;
345 struct dev_acquisition_t *acq;
353 acq = &devc->acquisition;
355 greatfet_release_resources(sdi);
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;
371 static void clear_helper(struct dev_context *devc)
373 greatfet_free_devc(devc);
376 static int dev_clear(const struct sr_dev_driver *driver)
378 return std_dev_clear_with_callback(driver,
379 (std_dev_clear_callback)clear_helper);
382 static int config_get(uint32_t key, GVariant **data,
383 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
385 struct dev_context *devc;
391 /* Handle requests for the "Logic" channel group. */
399 /* Handle global options for the device. */
402 if (!sdi->connection_id)
404 *data = g_variant_new_string(sdi->connection_id);
406 case SR_CONF_CONTINUOUS:
407 *data = g_variant_new_boolean(TRUE);
409 case SR_CONF_SAMPLERATE:
412 *data = g_variant_new_uint64(devc->samplerate);
414 case SR_CONF_LIMIT_SAMPLES:
415 case SR_CONF_LIMIT_MSEC:
418 return sr_sw_limits_config_get(&devc->sw_limits, key, data);
424 static int config_set(uint32_t key, GVariant *data,
425 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
427 struct dev_context *devc;
431 /* Handle requests for the "Logic" channel group. */
439 /* Handle global options for the device. */
441 case SR_CONF_SAMPLERATE:
444 devc->samplerate = g_variant_get_uint64(data);
446 case SR_CONF_LIMIT_SAMPLES:
447 case SR_CONF_LIMIT_MSEC:
450 return sr_sw_limits_config_set(&devc->sw_limits, key, data);
456 static int config_list(uint32_t key, GVariant **data,
457 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
460 /* Handle requests for the "Logic" channel group. */
463 case SR_CONF_DEVICE_OPTIONS:
464 if (ARRAY_SIZE(devopts_cg) == 0)
466 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
467 ARRAY_AND_SIZE(devopts_cg),
468 sizeof(devopts_cg[0]));
475 /* Handle global options for the device. */
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));
489 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
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;
498 if (!sdi || !sdi->driver || !sdi->priv)
504 acq = &devc->acquisition;
506 acq->acquisition_state = ACQ_PREPARE;
508 ret = greatfet_setup_acquisition(sdi);
512 if (!acq->feed_queue) {
513 acq->feed_queue = feed_queue_logic_alloc(sdi,
514 BUFFER_SIZE, devc->feed_unit_size);
515 if (!acq->feed_queue) {
516 sr_err("Cannot allocate session feed buffer.");
517 return SR_ERR_MALLOC;
521 sr_sw_limits_acquisition_start(&devc->sw_limits);
523 ret = greatfet_start_acquisition(sdi);
524 acq->start_req_sent = ret == SR_OK;
526 greatfet_abort_acquisition(sdi);
527 feed_queue_logic_free(acq->feed_queue);
528 acq->feed_queue = NULL;
531 acq->acquisition_state = ACQ_RECEIVE;
533 usb_source_add(sdi->session, ctx, 50,
534 greatfet_receive_data, (void *)sdi);
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));
544 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
546 greatfet_abort_acquisition(sdi);
550 static struct sr_dev_driver greatfet_driver_info = {
552 .longname = "Great Scott Gadgets GreatFET One",
555 .cleanup = std_cleanup,
557 .dev_list = std_dev_list,
558 .dev_clear = dev_clear,
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,
568 SR_REGISTER_DEV_DRIVER(greatfet_driver_info);