2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "libsigrok.h"
29 #include "libsigrok-internal.h"
32 #define USB_VENDOR 0x0c12
34 #define VENDOR_NAME "ZEROPLUS"
35 #define MODEL_NAME "Logic Cube LAP-C"
36 #define MODEL_VERSION NULL
39 #define USB_INTERFACE 0
40 #define USB_CONFIGURATION 1
41 #define NUM_TRIGGER_STAGES 4
42 #define TRIGGER_TYPES "01"
44 #define PACKET_SIZE 2048 /* ?? */
49 unsigned int channels;
50 unsigned int sample_depth; /* In Ksamples/channel */
51 unsigned int max_sampling_freq;
55 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
56 * same 128K sample depth.
58 static model_t zeroplus_models[] = {
59 {0x7009, "LAP-C(16064)", 16, 64, 100},
60 {0x700A, "LAP-C(16128)", 16, 128, 200},
61 {0x700B, "LAP-C(32128)", 32, 128, 200},
62 {0x700C, "LAP-C(321000)", 32, 1024, 200},
63 {0x700D, "LAP-C(322000)", 32, 2048, 200},
64 {0x700E, "LAP-C(16032)", 16, 32, 100},
65 {0x7016, "LAP-C(162000)", 16, 2048, 200},
68 static const int hwcaps[] = {
69 SR_HWCAP_LOGIC_ANALYZER,
72 SR_HWCAP_CAPTURE_RATIO,
74 /* These are really implemented in the driver, not the hardware. */
75 SR_HWCAP_LIMIT_SAMPLES,
80 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
81 * We currently ignore other untested/unsupported devices here.
83 static const char *probe_names[NUM_PROBES + 1] = {
103 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
104 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
105 static struct sr_dev_driver *zdi = &zeroplus_logic_cube_driver_info;
107 static libusb_context *usb_context = NULL;
110 * The hardware supports more samplerates than these, but these are the
111 * options hardcoded into the vendor's Windows GUI.
115 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
118 static const uint64_t supported_samplerates[] = {
140 static const struct sr_samplerates samplerates = {
144 supported_samplerates,
147 /* Private, per-device-instance driver context. */
149 uint64_t cur_samplerate;
150 uint64_t limit_samples;
151 int num_channels; /* TODO: This isn't initialized before it's needed :( */
152 uint64_t memory_size;
154 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
155 uint8_t trigger_value[NUM_TRIGGER_STAGES];
156 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
158 struct sr_usb_dev_inst *usb;
161 static int hw_dev_config_set(int dev_index, int hwcap, const void *value);
163 static unsigned int get_memory_size(int type)
165 if (type == MEMORY_SIZE_8K)
167 else if (type == MEMORY_SIZE_64K)
169 else if (type == MEMORY_SIZE_128K)
171 else if (type == MEMORY_SIZE_512K)
177 static int opendev4(struct sr_dev_inst **sdi, libusb_device *dev,
178 struct libusb_device_descriptor *des)
184 /* Note: sdi is non-NULL, the caller already checked this. */
186 if (!(ctx = (*sdi)->priv)) {
187 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
191 if ((ret = libusb_get_device_descriptor(dev, des))) {
192 sr_err("zp: failed to get device descriptor: %d", ret);
196 if (des->idVendor != USB_VENDOR)
199 if (libusb_get_bus_number(dev) == ctx->usb->bus
200 && libusb_get_device_address(dev) == ctx->usb->address) {
202 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
203 if (!(des->idProduct == zeroplus_models[i].pid))
206 sr_info("zp: Found ZEROPLUS device 0x%04x (%s)",
207 des->idProduct, zeroplus_models[i].model_name);
208 ctx->num_channels = zeroplus_models[i].channels;
209 ctx->memory_size = zeroplus_models[i].sample_depth * 1024;
213 if (ctx->num_channels == 0) {
214 sr_err("zp: Unknown ZEROPLUS device 0x%04x",
220 if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) {
221 (*sdi)->status = SR_ST_ACTIVE;
222 sr_info("zp: opened device %d on %d.%d interface %d",
223 (*sdi)->index, ctx->usb->bus,
224 ctx->usb->address, USB_INTERFACE);
226 sr_err("zp: failed to open device: %d", ret);
234 static struct sr_dev_inst *zp_open_dev(int dev_index)
236 struct sr_dev_inst *sdi;
237 libusb_device **devlist;
238 struct libusb_device_descriptor des;
241 if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index)))
244 libusb_get_device_list(usb_context, &devlist);
245 if (sdi->status == SR_ST_INACTIVE) {
246 /* Find the device by vendor, product, bus and address. */
247 libusb_get_device_list(usb_context, &devlist);
248 for (i = 0; devlist[i]; i++) {
249 /* TODO: Error handling. */
250 opendev4(&sdi, devlist[i], &des);
253 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
256 libusb_free_device_list(devlist, 1);
258 if (sdi && sdi->status != SR_ST_ACTIVE)
264 static void close_dev(struct sr_dev_inst *sdi)
268 if (!(ctx = sdi->priv)) {
269 sr_err("zp: %s: sdi->priv was NULL", __func__);
273 if (!ctx->usb->devhdl)
276 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
277 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
278 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
279 libusb_reset_device(ctx->usb->devhdl);
280 libusb_close(ctx->usb->devhdl);
281 ctx->usb->devhdl = NULL;
282 /* TODO: Call libusb_exit() here or only in hw_cleanup()? */
283 sdi->status = SR_ST_INACTIVE;
286 static int configure_probes(struct sr_dev_inst *sdi, const GSList *probes)
289 const struct sr_probe *probe;
291 int probe_bit, stage, i;
294 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
298 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
299 ctx->trigger_mask[i] = 0;
300 ctx->trigger_value[i] = 0;
304 for (l = probes; l; l = l->next) {
305 probe = (struct sr_probe *)l->data;
306 if (probe->enabled == FALSE)
308 probe_bit = 1 << (probe->index - 1);
309 ctx->probe_mask |= probe_bit;
311 if (probe->trigger) {
313 for (tc = probe->trigger; *tc; tc++) {
314 ctx->trigger_mask[stage] |= probe_bit;
316 ctx->trigger_value[stage] |= probe_bit;
318 if (stage > NUM_TRIGGER_STAGES)
331 static int hw_init(void)
339 static GSList *hw_scan(GSList *options)
341 struct sr_dev_inst *sdi;
342 struct libusb_device_descriptor des;
344 libusb_device **devlist;
351 /* Allocate memory for our private driver context. */
352 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
353 sr_err("zp: %s: ctx malloc failed", __func__);
357 /* Set some sane defaults. */
358 ctx->cur_samplerate = 0;
359 ctx->limit_samples = 0;
360 /* TODO: num_channels isn't initialized before it's needed :( */
361 ctx->num_channels = NUM_PROBES;
362 ctx->memory_size = 0;
364 memset(ctx->trigger_mask, 0, NUM_TRIGGER_STAGES);
365 memset(ctx->trigger_value, 0, NUM_TRIGGER_STAGES);
366 // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES);
368 if (libusb_init(&usb_context) != 0) {
369 sr_err("zp: Failed to initialize USB.");
373 /* Find all ZEROPLUS analyzers and add them to device list. */
375 libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
377 for (i = 0; devlist[i]; i++) {
378 ret = libusb_get_device_descriptor(devlist[i], &des);
380 sr_err("zp: failed to get device descriptor: %d", ret);
384 if (des.idVendor == USB_VENDOR) {
386 * Definitely a ZEROPLUS.
387 * TODO: Any way to detect specific model/version in
388 * the ZEROPLUS range?
390 /* Register the device with libsigrok. */
391 if (!(sdi = sr_dev_inst_new(devcnt,
392 SR_ST_INACTIVE, VENDOR_NAME,
393 MODEL_NAME, MODEL_VERSION))) {
394 sr_err("zp: %s: sr_dev_inst_new failed",
401 devices = g_slist_append(devices, sdi);
402 zdi->instances = g_slist_append(zdi->instances, sdi);
403 ctx->usb = sr_usb_dev_inst_new(
404 libusb_get_bus_number(devlist[i]),
405 libusb_get_device_address(devlist[i]), NULL);
409 libusb_free_device_list(devlist, 1);
414 static int hw_dev_open(int dev_index)
416 struct sr_dev_inst *sdi;
420 if (!(sdi = zp_open_dev(dev_index))) {
421 sr_err("zp: unable to open device");
425 /* TODO: Note: sdi is retrieved in zp_open_dev(). */
427 if (!(ctx = sdi->priv)) {
428 sr_err("zp: %s: sdi->priv was NULL", __func__);
432 ret = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
434 sr_err("zp: Unable to set USB configuration %d: %d",
435 USB_CONFIGURATION, ret);
439 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
441 sr_err("zp: Unable to claim interface: %d", ret);
445 analyzer_reset(ctx->usb->devhdl);
446 analyzer_initialize(ctx->usb->devhdl);
448 analyzer_set_memory_size(MEMORY_SIZE_512K);
449 // analyzer_set_freq(g_freq, g_freq_scale);
450 analyzer_set_trigger_count(1);
451 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
452 // * get_memory_size(g_memory_size)) / 100) >> 2);
453 analyzer_set_ramsize_trigger_address(
454 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
457 if (g_double_mode == 1)
458 analyzer_set_compression(COMPRESSION_DOUBLE);
459 else if (g_compression == 1)
460 analyzer_set_compression(COMPRESSION_ENABLE);
463 analyzer_set_compression(COMPRESSION_NONE);
465 if (ctx->cur_samplerate == 0) {
466 /* Samplerate hasn't been set. Default to the slowest one. */
467 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
468 &samplerates.list[0]) == SR_ERR)
475 static int hw_dev_close(int dev_index)
477 struct sr_dev_inst *sdi;
479 if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
480 sr_err("zp: %s: sdi was NULL", __func__);
481 return SR_ERR; /* TODO: SR_ERR_ARG? */
490 static int hw_cleanup(void)
493 struct sr_dev_inst *sdi;
495 for (l = zdi->instances; l; l = l->next) {
497 /* Properly close all devices... */
499 /* ...and free all their memory. */
500 sr_dev_inst_free(sdi);
502 g_slist_free(zdi->instances);
503 zdi->instances = NULL;
506 libusb_exit(usb_context);
512 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
514 struct sr_dev_inst *sdi;
518 if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
519 sr_err("zp: %s: sdi was NULL", __func__);
523 if (!(ctx = sdi->priv)) {
524 sr_err("zp: %s: sdi->priv was NULL", __func__);
528 sr_spew("zp: %s: dev_index %d, dev_info_id %d.", __func__,
529 dev_index, dev_info_id);
531 switch (dev_info_id) {
534 sr_spew("zp: %s: Returning sdi.", __func__);
536 case SR_DI_NUM_PROBES:
537 info = GINT_TO_POINTER(ctx->num_channels);
538 sr_spew("zp: %s: Returning number of probes: %d.", __func__,
541 case SR_DI_PROBE_NAMES:
543 sr_spew("zp: %s: Returning probenames.", __func__);
545 case SR_DI_SAMPLERATES:
547 sr_spew("zp: %s: Returning samplerates.", __func__);
549 case SR_DI_TRIGGER_TYPES:
550 info = TRIGGER_TYPES;
551 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, info);
553 case SR_DI_CUR_SAMPLERATE:
554 info = &ctx->cur_samplerate;
555 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
556 __func__, ctx->cur_samplerate);
559 /* Unknown device info ID, return NULL. */
560 sr_err("zp: %s: Unknown device info ID", __func__);
568 static int hw_dev_status_get(int dev_index)
570 struct sr_dev_inst *sdi;
572 sdi = sr_dev_inst_get(zdi->instances, dev_index);
576 return SR_ST_NOT_FOUND;
579 static const int *hw_hwcap_get_all(void)
584 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
589 sr_err("zp: %s: sdi was NULL", __func__);
593 if (!(ctx = sdi->priv)) {
594 sr_err("zp: %s: sdi->priv was NULL", __func__);
598 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
600 if (samplerate > SR_MHZ(1))
601 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
602 else if (samplerate > SR_KHZ(1))
603 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
605 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
607 ctx->cur_samplerate = samplerate;
612 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
614 struct sr_dev_inst *sdi;
617 if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
618 sr_err("zp: %s: sdi was NULL", __func__);
622 if (!(ctx = sdi->priv)) {
623 sr_err("zp: %s: sdi->priv was NULL", __func__);
628 case SR_HWCAP_SAMPLERATE:
629 return set_samplerate(sdi, *(const uint64_t *)value);
630 case SR_HWCAP_PROBECONFIG:
631 return configure_probes(sdi, (const GSList *)value);
632 case SR_HWCAP_LIMIT_SAMPLES:
633 ctx->limit_samples = *(const uint64_t *)value;
640 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
642 struct sr_dev_inst *sdi;
643 struct sr_datafeed_packet packet;
644 struct sr_datafeed_logic logic;
645 struct sr_datafeed_header header;
646 struct sr_datafeed_meta_logic meta;
647 uint64_t samples_read;
649 unsigned int packet_num;
653 if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
654 sr_err("zp: %s: sdi was NULL", __func__);
658 if (!(ctx = sdi->priv)) {
659 sr_err("zp: %s: sdi->priv was NULL", __func__);
663 /* push configured settings to device */
664 analyzer_configure(ctx->usb->devhdl);
666 analyzer_start(ctx->usb->devhdl);
667 sr_info("zp: Waiting for data");
668 analyzer_wait_data(ctx->usb->devhdl);
670 sr_info("zp: Stop address = 0x%x",
671 analyzer_get_stop_address(ctx->usb->devhdl));
672 sr_info("zp: Now address = 0x%x",
673 analyzer_get_now_address(ctx->usb->devhdl));
674 sr_info("zp: Trigger address = 0x%x",
675 analyzer_get_trigger_address(ctx->usb->devhdl));
677 packet.type = SR_DF_HEADER;
678 packet.payload = &header;
679 header.feed_version = 1;
680 gettimeofday(&header.starttime, NULL);
681 sr_session_send(cb_data, &packet);
683 /* Send metadata about the SR_DF_LOGIC packets to come. */
684 packet.type = SR_DF_META_LOGIC;
685 packet.payload = &meta;
686 meta.samplerate = ctx->cur_samplerate;
687 meta.num_probes = ctx->num_channels;
688 sr_session_send(cb_data, &packet);
690 if (!(buf = g_try_malloc(PACKET_SIZE))) {
691 sr_err("zp: %s: buf malloc failed", __func__);
692 return SR_ERR_MALLOC;
696 analyzer_read_start(ctx->usb->devhdl);
697 /* Send the incoming transfer to the session bus. */
698 for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
700 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
701 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
704 packet.type = SR_DF_LOGIC;
705 packet.payload = &logic;
706 logic.length = PACKET_SIZE;
709 sr_session_send(cb_data, &packet);
710 samples_read += res / 4;
712 analyzer_read_stop(ctx->usb->devhdl);
715 packet.type = SR_DF_END;
716 sr_session_send(cb_data, &packet);
721 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
722 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
724 struct sr_datafeed_packet packet;
725 struct sr_dev_inst *sdi;
728 packet.type = SR_DF_END;
729 sr_session_send(cb_data, &packet);
731 if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
732 sr_err("zp: %s: sdi was NULL", __func__);
736 if (!(ctx = sdi->priv)) {
737 sr_err("zp: %s: sdi->priv was NULL", __func__);
741 analyzer_reset(ctx->usb->devhdl);
742 /* TODO: Need to cancel and free any queued up transfers. */
747 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
748 .name = "zeroplus-logic-cube",
749 .longname = "ZEROPLUS Logic Cube LAP-C series",
752 .cleanup = hw_cleanup,
754 .dev_open = hw_dev_open,
755 .dev_close = hw_dev_close,
756 .dev_info_get = hw_dev_info_get,
757 .dev_status_get = hw_dev_status_get,
758 .hwcap_get_all = hw_hwcap_get_all,
759 .dev_config_set = hw_dev_config_set,
760 .dev_acquisition_start = hw_dev_acquisition_start,
761 .dev_acquisition_stop = hw_dev_acquisition_stop,