]> sigrok.org Git - libsigrok.git/blame - src/hardware/greatfet/api.c
greatfet: first operational GreatFET One driver implementation
[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
38#define WITH_16CHAN_SUPPORT 0
39
40static const uint32_t scanopts[] = {
41 SR_CONF_CONN,
42 SR_CONF_PROBE_NAMES,
43};
44
45static const uint32_t drvopts[] = {
46 SR_CONF_LOGIC_ANALYZER,
47};
48
49static 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,
55};
56
57static const uint32_t devopts_cg[] = {
58 /* EMPTY */
59};
60
61static 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",
67#endif
68};
69
70/*
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.
74 *
75 * When users specify different samplerates, device firmware will pick
76 * the minimum rate which satisfies the user's request.
77 */
78static 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 */
94};
95
96static void greatfet_free_devc(struct dev_context *devc)
97{
98
99 if (!devc)
100 return;
101
102 if (devc->sdi)
103 devc->sdi->priv = NULL;
104
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);
112 /*
113 * USB transfers should not have been allocated when we get here
114 * during device probe/scan, or during shutdown after acquisition
115 * has terminated.
116 */
117
118 g_free(devc);
119}
120
121static void greatfet_free_sdi(struct sr_dev_inst *sdi)
122{
123 struct sr_usb_dev_inst *usb;
124 struct dev_context *devc;
125
126 if (!sdi)
127 return;
128
129 usb = sdi->conn;
130 sdi->conn = NULL;
131 if (usb && usb->devhdl)
132 sr_usb_close(usb);
133 sr_usb_dev_inst_free(usb);
134
135 devc = sdi->priv;
136 sdi->priv = NULL;
137 greatfet_free_devc(devc);
138
139 sr_dev_inst_free(sdi);
140}
f594b3b0
GS
141
142static GSList *scan(struct sr_dev_driver *di, GSList *options)
143{
144 struct drv_context *drvc;
208fcedc 145 struct sr_context *ctx;
f594b3b0 146 GSList *devices;
208fcedc
GS
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;
157 char *match;
158 char serno_txt[64], conn_id[64];
159 int ret;
160 size_t ch_off, ch_max, ch_idx;
161 gboolean enabled;
162 struct sr_channel *ch;
163 struct sr_channel_group *cg;
f594b3b0 164
208fcedc
GS
165 drvc = di->context;
166 ctx = drvc->sr_ctx;
f594b3b0
GS
167
168 devices = NULL;
f594b3b0 169
208fcedc
GS
170 /* Accept user specs for conn= and probe names. */
171 conn = DEFAULT_CONN;
172 probe_names = NULL;
173 for (l = options; l; l = l->next) {
174 src = l->data;
175 switch (src->key) {
176 case SR_CONF_CONN:
177 conn = g_variant_get_string(src->data, NULL);
178 break;
179 case SR_CONF_PROBE_NAMES:
180 probe_names = g_variant_get_string(src->data, NULL);
181 break;
182 }
183 }
184
185 /*
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.
191 */
192 want_snr = NULL;
193 if (g_str_has_prefix(conn, "sn=")) {
194 want_snr = conn + strlen("sn=");
195 conn = DEFAULT_CONN;
196 sr_info("Searching default %s and serial number %s.",
197 conn, want_snr);
198 }
199 conn_devices = sr_usb_find(ctx->libusb_ctx, conn);
200 if (!conn_devices)
201 return devices;
202
203 /*
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.
210 *
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.
219 *
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.
229 */
230 for (l = conn_devices; l; l = l->next) {
231 usb = l->data;
232
233 ret = sr_usb_open(ctx->libusb_ctx, usb);
234 if (ret != SR_OK)
235 continue;
236
237 skip_device = FALSE;
238 if (want_snr) do {
239 dev = libusb_get_device(usb->devhdl);
240 ret = libusb_get_device_descriptor(dev, &des);
241 if (ret != 0 || !des.iSerialNumber) {
242 skip_device = TRUE;
243 break;
244 }
245 ret = libusb_get_string_descriptor_ascii(usb->devhdl,
246 des.iSerialNumber,
247 (uint8_t *)serno_txt, sizeof(serno_txt));
248 if (ret < 0) {
249 skip_device = TRUE;
250 break;
251 }
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);
256 } while (0);
257 if (skip_device) {
258 sr_usb_close(usb);
259 continue;
260 }
261
262 sdi = g_malloc0(sizeof(*sdi));
263 sdi->conn = usb;
264 sdi->inst_type = SR_INST_USB;
265 sdi->status = SR_ST_INACTIVE;
266 devc = g_malloc0(sizeof(*devc));
267 sdi->priv = devc;
268 devc->sdi = sdi;
269 devc->usb_comm_buffer = NULL;
270
271 /*
272 * Get the serial number by way of device communication.
273 * Get the firmware version. Failure is fatal.
274 */
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);
279 continue;
280 }
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);
285 continue;
286 }
287
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);
292 sr_usb_close(usb);
293
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);
298
299 /* Create the "Logic" channel group. */
300 ch_off = 0;
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]);
311 ch_off++;
312 cg->channels = g_slist_append(cg->channels, ch);
313 }
314
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;
321
322 devices = g_slist_append(devices, sdi);
323 }
324 g_slist_free(conn_devices);
f594b3b0 325
208fcedc 326 return std_scan_complete(di, devices);
f594b3b0
GS
327}
328
329static int dev_open(struct sr_dev_inst *sdi)
330{
208fcedc
GS
331 struct sr_dev_driver *di;
332 struct drv_context *drvc;
333 struct sr_context *ctx;
334 struct sr_usb_dev_inst *usb;
f594b3b0 335
208fcedc
GS
336 di = sdi->driver;
337 drvc = di->context;
338 ctx = drvc->sr_ctx;
339 usb = sdi->conn;
f594b3b0 340
208fcedc 341 return sr_usb_open(ctx->libusb_ctx, usb);
f594b3b0
GS
342}
343
344static int dev_close(struct sr_dev_inst *sdi)
345{
208fcedc
GS
346 struct sr_usb_dev_inst *usb;
347 struct dev_context *devc;
348 struct dev_acquisition_t *acq;
349
350 if (!sdi)
351 return SR_ERR_ARG;
352 usb = sdi->conn;
353 devc = sdi->priv;
354 if (!devc)
355 return SR_ERR_ARG;
356 acq = &devc->acquisition;
357
358 greatfet_release_resources(sdi);
359
360 if (!usb->devhdl)
361 return SR_ERR_BUG;
362
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;
368 }
369 sr_usb_close(usb);
f594b3b0
GS
370
371 return SR_OK;
372}
373
208fcedc
GS
374static void clear_helper(struct dev_context *devc)
375{
376 greatfet_free_devc(devc);
377}
378
379static int dev_clear(const struct sr_dev_driver *driver)
380{
381 return std_dev_clear_with_callback(driver,
382 (std_dev_clear_callback)clear_helper);
383}
384
f594b3b0
GS
385static int config_get(uint32_t key, GVariant **data,
386 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
387{
208fcedc
GS
388 struct dev_context *devc;
389
390 if (!sdi)
391 return SR_ERR_ARG;
392 devc = sdi->priv;
393
394 /* Handle requests for the "Logic" channel group. */
395 if (cg) {
396 switch (key) {
397 default:
398 return SR_ERR_NA;
399 }
400 }
f594b3b0 401
208fcedc 402 /* Handle global options for the device. */
f594b3b0 403 switch (key) {
208fcedc
GS
404 case SR_CONF_CONN:
405 if (!sdi->connection_id)
406 return SR_ERR_NA;
407 *data = g_variant_new_string(sdi->connection_id);
408 return SR_OK;
409 case SR_CONF_CONTINUOUS:
410 *data = g_variant_new_boolean(TRUE);
411 return SR_OK;
412 case SR_CONF_SAMPLERATE:
413 if (!devc)
414 return SR_ERR_NA;
415 *data = g_variant_new_uint64(devc->samplerate);
416 return SR_OK;
417 case SR_CONF_LIMIT_SAMPLES:
418 case SR_CONF_LIMIT_MSEC:
419 if (!devc)
420 return SR_ERR_NA;
421 return sr_sw_limits_config_get(&devc->sw_limits, key, data);
f594b3b0
GS
422 default:
423 return SR_ERR_NA;
424 }
f594b3b0
GS
425}
426
427static int config_set(uint32_t key, GVariant *data,
428 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
429{
208fcedc 430 struct dev_context *devc;
f594b3b0 431
208fcedc 432 devc = sdi->priv;
f594b3b0 433
208fcedc
GS
434 /* Handle requests for the "Logic" channel group. */
435 if (cg) {
436 switch (key) {
437 default:
438 return SR_ERR_NA;
439 }
440 }
441
442 /* Handle global options for the device. */
f594b3b0 443 switch (key) {
208fcedc
GS
444 case SR_CONF_SAMPLERATE:
445 if (!devc)
446 return SR_ERR_NA;
447 devc->samplerate = g_variant_get_uint64(data);
448 return SR_OK;
449 case SR_CONF_LIMIT_SAMPLES:
450 case SR_CONF_LIMIT_MSEC:
451 if (!devc)
452 return SR_ERR_NA;
453 return sr_sw_limits_config_set(&devc->sw_limits, key, data);
f594b3b0 454 default:
208fcedc 455 return SR_ERR_NA;
f594b3b0 456 }
f594b3b0
GS
457}
458
459static int config_list(uint32_t key, GVariant **data,
460 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
461{
f594b3b0 462
208fcedc
GS
463 /* Handle requests for the "Logic" channel group. */
464 if (cg) {
465 switch (key) {
466 case SR_CONF_DEVICE_OPTIONS:
467 if (ARRAY_SIZE(devopts_cg) == 0)
468 return SR_ERR_NA;
469 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
470 ARRAY_AND_SIZE(devopts_cg),
471 sizeof(devopts_cg[0]));
472 return SR_OK;
473 default:
474 return SR_ERR_NA;
475 }
476 }
f594b3b0 477
208fcedc 478 /* Handle global options for the device. */
f594b3b0 479 switch (key) {
208fcedc
GS
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));
486 return SR_OK;
f594b3b0
GS
487 default:
488 return SR_ERR_NA;
489 }
f594b3b0
GS
490}
491
492static int dev_acquisition_start(const struct sr_dev_inst *sdi)
493{
208fcedc
GS
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;
499 int ret;
f594b3b0 500
208fcedc
GS
501 if (!sdi || !sdi->driver || !sdi->priv)
502 return SR_ERR_ARG;
503 di = sdi->driver;
504 drvc = di->context;
505 ctx = drvc->sr_ctx;
506 devc = sdi->priv;
507 acq = &devc->acquisition;
508
509 acq->acquisition_state = ACQ_PREPARE;
510
511 ret = greatfet_setup_acquisition(sdi);
512 if (ret != SR_OK)
513 return ret;
514
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;
521 }
522 }
523
524 sr_sw_limits_acquisition_start(&devc->sw_limits);
525
526 ret = greatfet_start_acquisition(sdi);
527 acq->start_req_sent = ret == SR_OK;
528 if (ret != SR_OK) {
529 greatfet_abort_acquisition(sdi);
530 feed_queue_logic_free(acq->feed_queue);
531 acq->feed_queue = NULL;
532 return ret;
533 }
534 acq->acquisition_state = ACQ_RECEIVE;
535
536 usb_source_add(sdi->session, ctx, 50,
537 greatfet_receive_data, (void *)sdi);
538
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));
f594b3b0
GS
543
544 return SR_OK;
545}
546
547static int dev_acquisition_stop(struct sr_dev_inst *sdi)
548{
208fcedc 549 greatfet_abort_acquisition(sdi);
f594b3b0
GS
550 return SR_OK;
551}
552
553static struct sr_dev_driver greatfet_driver_info = {
554 .name = "greatfet",
208fcedc 555 .longname = "Great Scott Gadgets GreatFET One",
f594b3b0
GS
556 .api_version = 1,
557 .init = std_init,
558 .cleanup = std_cleanup,
559 .scan = scan,
560 .dev_list = std_dev_list,
208fcedc 561 .dev_clear = dev_clear,
f594b3b0
GS
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,
569 .context = NULL,
570};
571SR_REGISTER_DEV_DRIVER(greatfet_driver_info);