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/>.
29 #include "sigrok-internal.h"
32 #define USB_VENDOR 0x0c12
33 #define USB_VENDOR_NAME "Zeroplus"
34 #define USB_MODEL_NAME "Logic Cube"
35 #define USB_MODEL_VERSION ""
37 #define USB_INTERFACE 0
38 #define USB_CONFIGURATION 1
39 #define NUM_TRIGGER_STAGES 4
40 #define TRIGGER_TYPES "01"
42 #define PACKET_SIZE 2048 /* ?? */
47 unsigned int channels;
48 unsigned int sample_depth; /* In Ksamples/channel */
49 unsigned int max_sampling_freq;
53 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
54 * same 128K sample depth.
56 static model_t zeroplus_models[] = {
57 {0x7009, "LAP-C(16064)", 16, 64, 100},
58 {0x700A, "LAP-C(16128)", 16, 128, 200},
59 {0x700B, "LAP-C(32128)", 32, 128, 200},
60 {0x700C, "LAP-C(321000)", 32, 1024, 200},
61 {0x700D, "LAP-C(322000)", 32, 2048, 200},
62 {0x700E, "LAP-C(16032)", 16, 32, 100},
63 {0x7016, "LAP-C(162000)", 16, 2048, 200},
66 static int hwcaps[] = {
67 SR_HWCAP_LOGIC_ANALYZER,
70 SR_HWCAP_CAPTURE_RATIO,
72 /* These are really implemented in the driver, not the hardware. */
73 SR_HWCAP_LIMIT_SAMPLES,
77 static const char *probe_names[] = {
113 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
114 static GSList *dev_insts = NULL;
116 static libusb_context *usb_context = NULL;
119 * The hardware supports more samplerates than these, but these are the
120 * options hardcoded into the vendor's Windows GUI.
124 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
127 static uint64_t supported_samplerates[] = {
149 static struct sr_samplerates samplerates = {
153 supported_samplerates,
156 /* Private, per-device-instance driver context. */
158 uint64_t cur_samplerate;
159 uint64_t limit_samples;
160 int num_channels; /* TODO: This isn't initialized before it's needed :( */
161 uint64_t memory_size;
163 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
164 uint8_t trigger_value[NUM_TRIGGER_STAGES];
165 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
167 struct sr_usb_dev_inst *usb;
170 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
172 static unsigned int get_memory_size(int type)
174 if (type == MEMORY_SIZE_8K)
176 else if (type == MEMORY_SIZE_64K)
178 else if (type == MEMORY_SIZE_128K)
180 else if (type == MEMORY_SIZE_512K)
186 static int opendev4(struct sr_dev_inst **sdi, libusb_device *dev,
187 struct libusb_device_descriptor *des)
193 /* Note: sdi is non-NULL, the caller already checked this. */
195 if (!(ctx = (*sdi)->priv)) {
196 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
200 if ((err = libusb_get_device_descriptor(dev, des))) {
201 sr_err("zp: failed to get device descriptor: %d", err);
205 if (des->idVendor != USB_VENDOR)
208 if (libusb_get_bus_number(dev) == ctx->usb->bus
209 && libusb_get_device_address(dev) == ctx->usb->address) {
211 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
212 if (!(des->idProduct == zeroplus_models[i].pid))
215 sr_info("zp: Found ZeroPlus device 0x%04x (%s)",
216 des->idProduct, zeroplus_models[i].model_name);
217 ctx->num_channels = zeroplus_models[i].channels;
218 ctx->memory_size = zeroplus_models[i].sample_depth * 1024;
222 if (ctx->num_channels == 0) {
223 sr_err("zp: Unknown ZeroPlus device 0x%04x",
229 if (!(err = libusb_open(dev, &(ctx->usb->devhdl)))) {
230 (*sdi)->status = SR_ST_ACTIVE;
231 sr_info("zp: opened device %d on %d.%d interface %d",
232 (*sdi)->index, ctx->usb->bus,
233 ctx->usb->address, USB_INTERFACE);
235 sr_err("zp: failed to open device: %d", err);
243 static struct sr_dev_inst *zp_open_dev(int dev_index)
245 struct sr_dev_inst *sdi;
246 libusb_device **devlist;
247 struct libusb_device_descriptor des;
250 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
253 libusb_get_device_list(usb_context, &devlist);
254 if (sdi->status == SR_ST_INACTIVE) {
255 /* Find the device by vendor, product, bus and address. */
256 libusb_get_device_list(usb_context, &devlist);
257 for (i = 0; devlist[i]; i++) {
258 /* TODO: Error handling. */
259 err = opendev4(&sdi, devlist[i], &des);
262 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
265 libusb_free_device_list(devlist, 1);
267 if (sdi && sdi->status != SR_ST_ACTIVE)
273 static void close_dev(struct sr_dev_inst *sdi)
277 if (!(ctx = sdi->priv)) {
278 sr_err("zp: %s: sdi->priv was NULL", __func__);
282 if (!ctx->usb->devhdl)
285 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
286 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
287 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
288 libusb_reset_device(ctx->usb->devhdl);
289 libusb_close(ctx->usb->devhdl);
290 ctx->usb->devhdl = NULL;
291 /* TODO: Call libusb_exit() here or only in hw_cleanup()? */
292 sdi->status = SR_ST_INACTIVE;
295 static int configure_probes(struct sr_dev_inst *sdi, GSList *probes)
298 struct sr_probe *probe;
300 int probe_bit, stage, i;
303 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
307 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
308 ctx->trigger_mask[i] = 0;
309 ctx->trigger_value[i] = 0;
313 for (l = probes; l; l = l->next) {
314 probe = (struct sr_probe *)l->data;
315 if (probe->enabled == FALSE)
317 probe_bit = 1 << (probe->index - 1);
318 ctx->probe_mask |= probe_bit;
320 if (probe->trigger) {
322 for (tc = probe->trigger; *tc; tc++) {
323 ctx->trigger_mask[stage] |= probe_bit;
325 ctx->trigger_value[stage] |= probe_bit;
327 if (stage > NUM_TRIGGER_STAGES)
340 static int hw_init(const char *devinfo)
342 struct sr_dev_inst *sdi;
343 struct libusb_device_descriptor des;
344 libusb_device **devlist;
348 /* Avoid compiler warnings. */
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 ctx->num_channels = 32; /* TODO: This isn't initialized before it's needed :( */
361 ctx->memory_size = 0;
363 memset(ctx->trigger_mask, 0, NUM_TRIGGER_STAGES);
364 memset(ctx->trigger_value, 0, NUM_TRIGGER_STAGES);
365 // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES);
367 if (libusb_init(&usb_context) != 0) {
368 sr_err("zp: Failed to initialize USB.");
372 /* Find all ZeroPlus analyzers and add them to device list. */
374 libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
376 for (i = 0; devlist[i]; i++) {
377 err = libusb_get_device_descriptor(devlist[i], &des);
379 sr_err("zp: failed to get device descriptor: %d", err);
383 if (des.idVendor == USB_VENDOR) {
385 * Definitely a Zeroplus.
386 * TODO: Any way to detect specific model/version in
387 * the zeroplus range?
389 /* Register the device with libsigrok. */
390 if (!(sdi = sr_dev_inst_new(devcnt,
391 SR_ST_INACTIVE, USB_VENDOR_NAME,
392 USB_MODEL_NAME, USB_MODEL_VERSION))) {
393 sr_err("zp: %s: sr_dev_inst_new failed",
401 g_slist_append(dev_insts, sdi);
402 ctx->usb = sr_usb_dev_inst_new(
403 libusb_get_bus_number(devlist[i]),
404 libusb_get_device_address(devlist[i]), NULL);
408 libusb_free_device_list(devlist, 1);
413 static int hw_dev_open(int dev_index)
415 struct sr_dev_inst *sdi;
419 if (!(sdi = zp_open_dev(dev_index))) {
420 sr_err("zp: unable to open device");
424 /* TODO: Note: sdi is retrieved in zp_open_dev(). */
426 if (!(ctx = sdi->priv)) {
427 sr_err("zp: %s: sdi->priv was NULL", __func__);
431 err = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
433 sr_err("zp: Unable to set USB configuration %d: %d",
434 USB_CONFIGURATION, err);
438 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
440 sr_err("zp: Unable to claim interface: %d", err);
444 analyzer_reset(ctx->usb->devhdl);
445 analyzer_initialize(ctx->usb->devhdl);
447 analyzer_set_memory_size(MEMORY_SIZE_512K);
448 // analyzer_set_freq(g_freq, g_freq_scale);
449 analyzer_set_trigger_count(1);
450 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
451 // * get_memory_size(g_memory_size)) / 100) >> 2);
452 analyzer_set_ramsize_trigger_address(
453 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
456 if (g_double_mode == 1)
457 analyzer_set_compression(COMPRESSION_DOUBLE);
458 else if (g_compression == 1)
459 analyzer_set_compression(COMPRESSION_ENABLE);
462 analyzer_set_compression(COMPRESSION_NONE);
464 if (ctx->cur_samplerate == 0) {
465 /* Samplerate hasn't been set. Default to the slowest one. */
466 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
467 &samplerates.list[0]) == SR_ERR)
474 static int hw_dev_close(int dev_index)
476 struct sr_dev_inst *sdi;
478 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
479 sr_err("zp: %s: sdi was NULL", __func__);
480 return SR_ERR; /* TODO: SR_ERR_ARG? */
489 static int hw_cleanup(void)
492 struct sr_dev_inst *sdi;
494 for (l = dev_insts; l; l = l->next) {
496 /* Properly close all devices... */
498 /* ...and free all their memory. */
499 sr_dev_inst_free(sdi);
501 g_slist_free(dev_insts);
505 libusb_exit(usb_context);
511 static void *hw_dev_info_get(int dev_index, int dev_info_id)
513 struct sr_dev_inst *sdi;
517 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
518 sr_err("zp: %s: sdi was NULL", __func__);
522 if (!(ctx = sdi->priv)) {
523 sr_err("zp: %s: sdi->priv was NULL", __func__);
527 switch (dev_info_id) {
531 case SR_DI_NUM_PROBES:
532 info = GINT_TO_POINTER(ctx->num_channels);
534 case SR_DI_PROBE_NAMES:
537 case SR_DI_SAMPLERATES:
540 case SR_DI_TRIGGER_TYPES:
541 info = TRIGGER_TYPES;
543 case SR_DI_CUR_SAMPLERATE:
544 info = &ctx->cur_samplerate;
547 /* Unknown device info ID, return NULL. */
548 sr_err("zp: %s: Unknown device info ID", __func__);
556 static int hw_dev_status_get(int dev_index)
558 struct sr_dev_inst *sdi;
560 sdi = sr_dev_inst_get(dev_insts, dev_index);
564 return SR_ST_NOT_FOUND;
567 static int *hw_hwcap_get_all(void)
572 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
577 sr_err("zp: %s: sdi was NULL", __func__);
581 if (!(ctx = sdi->priv)) {
582 sr_err("zp: %s: sdi->priv was NULL", __func__);
586 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
588 if (samplerate > SR_MHZ(1))
589 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
590 else if (samplerate > SR_KHZ(1))
591 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
593 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
595 ctx->cur_samplerate = samplerate;
600 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
602 struct sr_dev_inst *sdi;
605 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
606 sr_err("zp: %s: sdi was NULL", __func__);
610 if (!(ctx = sdi->priv)) {
611 sr_err("zp: %s: sdi->priv was NULL", __func__);
616 case SR_HWCAP_SAMPLERATE:
617 return set_samplerate(sdi, *(uint64_t *)value);
618 case SR_HWCAP_PROBECONFIG:
619 return configure_probes(sdi, (GSList *)value);
620 case SR_HWCAP_LIMIT_SAMPLES:
621 ctx->limit_samples = *(uint64_t *)value;
628 static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
630 struct sr_dev_inst *sdi;
631 struct sr_datafeed_packet packet;
632 struct sr_datafeed_logic logic;
633 struct sr_datafeed_header header;
634 uint64_t samples_read;
636 unsigned int packet_num;
640 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
641 sr_err("zp: %s: sdi was NULL", __func__);
645 if (!(ctx = sdi->priv)) {
646 sr_err("zp: %s: sdi->priv was NULL", __func__);
650 /* push configured settings to device */
651 analyzer_configure(ctx->usb->devhdl);
653 analyzer_start(ctx->usb->devhdl);
654 sr_info("zp: Waiting for data");
655 analyzer_wait_data(ctx->usb->devhdl);
657 sr_info("zp: Stop address = 0x%x",
658 analyzer_get_stop_address(ctx->usb->devhdl));
659 sr_info("zp: Now address = 0x%x",
660 analyzer_get_now_address(ctx->usb->devhdl));
661 sr_info("zp: Trigger address = 0x%x",
662 analyzer_get_trigger_address(ctx->usb->devhdl));
664 packet.type = SR_DF_HEADER;
665 packet.payload = &header;
666 header.feed_version = 1;
667 gettimeofday(&header.starttime, NULL);
668 header.samplerate = ctx->cur_samplerate;
669 header.num_logic_probes = ctx->num_channels;
670 sr_session_bus(session_data, &packet);
672 if (!(buf = g_try_malloc(PACKET_SIZE))) {
673 sr_err("zp: %s: buf malloc failed", __func__);
674 return SR_ERR_MALLOC;
678 analyzer_read_start(ctx->usb->devhdl);
679 /* Send the incoming transfer to the session bus. */
680 for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
682 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
683 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
686 packet.type = SR_DF_LOGIC;
687 packet.payload = &logic;
688 logic.length = PACKET_SIZE;
691 sr_session_bus(session_data, &packet);
692 samples_read += res / 4;
694 analyzer_read_stop(ctx->usb->devhdl);
697 packet.type = SR_DF_END;
698 sr_session_bus(session_data, &packet);
703 /* This stops acquisition on ALL devices, ignoring dev_index. */
704 static int hw_dev_acquisition_stop(int dev_index, gpointer session_dev_id)
706 struct sr_datafeed_packet packet;
707 struct sr_dev_inst *sdi;
710 packet.type = SR_DF_END;
711 sr_session_bus(session_dev_id, &packet);
713 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
714 sr_err("zp: %s: sdi was NULL", __func__);
718 if (!(ctx = sdi->priv)) {
719 sr_err("zp: %s: sdi->priv was NULL", __func__);
723 analyzer_reset(ctx->usb->devhdl);
724 /* TODO: Need to cancel and free any queued up transfers. */
729 SR_PRIV struct sr_dev_plugin zeroplus_logic_cube_plugin_info = {
730 .name = "zeroplus-logic-cube",
731 .longname = "Zeroplus Logic Cube LAP-C series",
734 .cleanup = hw_cleanup,
735 .dev_open = hw_dev_open,
736 .dev_close = hw_dev_close,
737 .dev_info_get = hw_dev_info_get,
738 .dev_status_get = hw_dev_status_get,
739 .hwcap_get_all = hw_hwcap_get_all,
740 .dev_config_set = hw_dev_config_set,
741 .dev_acquisition_start = hw_dev_acquisition_start,
742 .dev_acquisition_stop = hw_dev_acquisition_stop,