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,
157 uint64_t cur_samplerate;
158 uint64_t limit_samples;
159 int num_channels; /* TODO: This isn't initialized before it's needed :( */
160 uint64_t memory_size;
162 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
163 uint8_t trigger_value[NUM_TRIGGER_STAGES];
164 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
166 struct sr_usb_dev_inst *usb;
169 static int hw_config_set(int dev_index, int hwcap, void *value);
171 static unsigned int get_memory_size(int type)
173 if (type == MEMORY_SIZE_8K)
175 else if (type == MEMORY_SIZE_64K)
177 else if (type == MEMORY_SIZE_128K)
179 else if (type == MEMORY_SIZE_512K)
185 static int opendev4(struct sr_dev_inst **sdi, libusb_device *dev,
186 struct libusb_device_descriptor *des)
192 /* Note: sdi is non-NULL, the caller already checked this. */
194 if (!(zp = (*sdi)->priv)) {
195 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
199 if ((err = libusb_get_device_descriptor(dev, des))) {
200 sr_err("zp: failed to get device descriptor: %d", err);
204 if (des->idVendor != USB_VENDOR)
207 if (libusb_get_bus_number(dev) == zp->usb->bus
208 && libusb_get_device_address(dev) == zp->usb->address) {
210 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
211 if (!(des->idProduct == zeroplus_models[i].pid))
214 sr_info("zp: Found ZeroPlus device 0x%04x (%s)",
215 des->idProduct, zeroplus_models[i].model_name);
216 zp->num_channels = zeroplus_models[i].channels;
217 zp->memory_size = zeroplus_models[i].sample_depth * 1024;
221 if (zp->num_channels == 0) {
222 sr_err("zp: Unknown ZeroPlus device 0x%04x",
228 if (!(err = libusb_open(dev, &(zp->usb->devhdl)))) {
229 (*sdi)->status = SR_ST_ACTIVE;
230 sr_info("zp: opened device %d on %d.%d interface %d",
231 (*sdi)->index, zp->usb->bus,
232 zp->usb->address, USB_INTERFACE);
234 sr_err("zp: failed to open device: %d", err);
242 static struct sr_dev_inst *zp_open_dev(int dev_index)
244 struct sr_dev_inst *sdi;
245 libusb_device **devlist;
246 struct libusb_device_descriptor des;
249 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
252 libusb_get_device_list(usb_context, &devlist);
253 if (sdi->status == SR_ST_INACTIVE) {
254 /* Find the device by vendor, product, bus and address. */
255 libusb_get_device_list(usb_context, &devlist);
256 for (i = 0; devlist[i]; i++) {
257 /* TODO: Error handling. */
258 err = opendev4(&sdi, devlist[i], &des);
261 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
264 libusb_free_device_list(devlist, 1);
266 if (sdi && sdi->status != SR_ST_ACTIVE)
272 static void close_dev(struct sr_dev_inst *sdi)
276 if (!(zp = sdi->priv)) {
277 sr_err("zp: %s: sdi->priv was NULL", __func__);
281 if (!zp->usb->devhdl)
284 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
285 zp->usb->bus, zp->usb->address, USB_INTERFACE);
286 libusb_release_interface(zp->usb->devhdl, USB_INTERFACE);
287 libusb_reset_device(zp->usb->devhdl);
288 libusb_close(zp->usb->devhdl);
289 zp->usb->devhdl = NULL;
290 /* TODO: Call libusb_exit() here or only in hw_cleanup()? */
291 sdi->status = SR_ST_INACTIVE;
294 static int configure_probes(struct sr_dev_inst *sdi, GSList *probes)
297 struct sr_probe *probe;
299 int probe_bit, stage, i;
302 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
306 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
307 zp->trigger_mask[i] = 0;
308 zp->trigger_value[i] = 0;
312 for (l = probes; l; l = l->next) {
313 probe = (struct sr_probe *)l->data;
314 if (probe->enabled == FALSE)
316 probe_bit = 1 << (probe->index - 1);
317 zp->probe_mask |= probe_bit;
319 if (probe->trigger) {
321 for (tc = probe->trigger; *tc; tc++) {
322 zp->trigger_mask[stage] |= probe_bit;
324 zp->trigger_value[stage] |= probe_bit;
326 if (stage > NUM_TRIGGER_STAGES)
339 static int hw_init(const char *devinfo)
341 struct sr_dev_inst *sdi;
342 struct libusb_device_descriptor des;
343 libusb_device **devlist;
347 /* Avoid compiler warnings. */
350 /* Allocate memory for our private driver context. */
351 if (!(zp = g_try_malloc(sizeof(struct zp)))) {
352 sr_err("zp: %s: struct zp malloc failed", __func__);
356 /* Set some sane defaults. */
357 zp->cur_samplerate = 0;
358 zp->limit_samples = 0;
359 zp->num_channels = 32; /* TODO: This isn't initialized before it's needed :( */
362 memset(zp->trigger_mask, 0, NUM_TRIGGER_STAGES);
363 memset(zp->trigger_value, 0, NUM_TRIGGER_STAGES);
364 // memset(zp->trigger_buffer, 0, NUM_TRIGGER_STAGES);
366 if (libusb_init(&usb_context) != 0) {
367 sr_err("zp: Failed to initialize USB.");
371 /* Find all ZeroPlus analyzers and add them to device list. */
373 libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
375 for (i = 0; devlist[i]; i++) {
376 err = libusb_get_device_descriptor(devlist[i], &des);
378 sr_err("zp: failed to get device descriptor: %d", err);
382 if (des.idVendor == USB_VENDOR) {
384 * Definitely a Zeroplus.
385 * TODO: Any way to detect specific model/version in
386 * the zeroplus range?
388 /* Register the device with libsigrok. */
389 if (!(sdi = sr_dev_inst_new(devcnt,
390 SR_ST_INACTIVE, USB_VENDOR_NAME,
391 USB_MODEL_NAME, USB_MODEL_VERSION))) {
392 sr_err("zp: %s: sr_dev_inst_new failed",
400 g_slist_append(dev_insts, sdi);
401 zp->usb = sr_usb_dev_inst_new(
402 libusb_get_bus_number(devlist[i]),
403 libusb_get_device_address(devlist[i]), NULL);
407 libusb_free_device_list(devlist, 1);
412 static int hw_dev_open(int dev_index)
414 struct sr_dev_inst *sdi;
418 if (!(sdi = zp_open_dev(dev_index))) {
419 sr_err("zp: unable to open device");
423 /* TODO: Note: sdi is retrieved in zp_open_dev(). */
425 if (!(zp = sdi->priv)) {
426 sr_err("zp: %s: sdi->priv was NULL", __func__);
430 err = libusb_set_configuration(zp->usb->devhdl, USB_CONFIGURATION);
432 sr_err("zp: Unable to set USB configuration %d: %d",
433 USB_CONFIGURATION, err);
437 err = libusb_claim_interface(zp->usb->devhdl, USB_INTERFACE);
439 sr_err("zp: Unable to claim interface: %d", err);
443 analyzer_reset(zp->usb->devhdl);
444 analyzer_initialize(zp->usb->devhdl);
446 analyzer_set_memory_size(MEMORY_SIZE_512K);
447 // analyzer_set_freq(g_freq, g_freq_scale);
448 analyzer_set_trigger_count(1);
449 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
450 // * get_memory_size(g_memory_size)) / 100) >> 2);
451 analyzer_set_ramsize_trigger_address(
452 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
455 if (g_double_mode == 1)
456 analyzer_set_compression(COMPRESSION_DOUBLE);
457 else if (g_compression == 1)
458 analyzer_set_compression(COMPRESSION_ENABLE);
461 analyzer_set_compression(COMPRESSION_NONE);
463 if (zp->cur_samplerate == 0) {
464 /* Samplerate hasn't been set. Default to the slowest one. */
465 if (hw_config_set(dev_index, SR_HWCAP_SAMPLERATE,
466 &samplerates.list[0]) == SR_ERR)
473 static int hw_dev_close(int dev_index)
475 struct sr_dev_inst *sdi;
477 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
478 sr_err("zp: %s: sdi was NULL", __func__);
479 return SR_ERR; /* TODO: SR_ERR_ARG? */
488 static int hw_cleanup(void)
491 struct sr_dev_inst *sdi;
493 for (l = dev_insts; l; l = l->next) {
495 /* Properly close all devices... */
497 /* ...and free all their memory. */
498 sr_dev_inst_free(sdi);
500 g_slist_free(dev_insts);
504 libusb_exit(usb_context);
510 static void *hw_dev_info_get(int dev_index, int dev_info_id)
512 struct sr_dev_inst *sdi;
516 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
517 sr_err("zp: %s: sdi was NULL", __func__);
521 if (!(zp = sdi->priv)) {
522 sr_err("zp: %s: sdi->priv was NULL", __func__);
526 switch (dev_info_id) {
530 case SR_DI_NUM_PROBES:
531 info = GINT_TO_POINTER(zp->num_channels);
533 case SR_DI_PROBE_NAMES:
536 case SR_DI_SAMPLERATES:
539 case SR_DI_TRIGGER_TYPES:
540 info = TRIGGER_TYPES;
542 case SR_DI_CUR_SAMPLERATE:
543 info = &zp->cur_samplerate;
546 /* Unknown device info ID, return NULL. */
547 sr_err("zp: %s: Unknown device info ID", __func__);
555 static int hw_dev_status_get(int dev_index)
557 struct sr_dev_inst *sdi;
559 sdi = sr_dev_inst_get(dev_insts, dev_index);
563 return SR_ST_NOT_FOUND;
566 static int *hw_hwcap_get_all(void)
571 static int config_set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
576 sr_err("zp: %s: sdi was NULL", __func__);
580 if (!(zp = sdi->priv)) {
581 sr_err("zp: %s: sdi->priv was NULL", __func__);
585 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
587 if (samplerate > SR_MHZ(1))
588 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
589 else if (samplerate > SR_KHZ(1))
590 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
592 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
594 zp->cur_samplerate = samplerate;
599 static int hw_config_set(int dev_index, int hwcap, void *value)
601 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 (!(zp = sdi->priv)) {
611 sr_err("zp: %s: sdi->priv was NULL", __func__);
616 case SR_HWCAP_SAMPLERATE:
618 return config_set_samplerate(sdi, *tmp_u64);
619 case SR_HWCAP_PROBECONFIG:
620 return configure_probes(sdi, (GSList *)value);
621 case SR_HWCAP_LIMIT_SAMPLES:
623 zp->limit_samples = *tmp_u64;
630 static int hw_acquisition_start(int dev_index, gpointer session_data)
632 struct sr_dev_inst *sdi;
633 struct sr_datafeed_packet packet;
634 struct sr_datafeed_logic logic;
635 struct sr_datafeed_header header;
636 uint64_t samples_read;
638 unsigned int packet_num;
642 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
643 sr_err("zp: %s: sdi was NULL", __func__);
647 if (!(zp = sdi->priv)) {
648 sr_err("zp: %s: sdi->priv was NULL", __func__);
652 /* push configured settings to device */
653 analyzer_configure(zp->usb->devhdl);
655 analyzer_start(zp->usb->devhdl);
656 sr_info("zp: Waiting for data");
657 analyzer_wait_data(zp->usb->devhdl);
659 sr_info("zp: Stop address = 0x%x",
660 analyzer_get_stop_address(zp->usb->devhdl));
661 sr_info("zp: Now address = 0x%x",
662 analyzer_get_now_address(zp->usb->devhdl));
663 sr_info("zp: Trigger address = 0x%x",
664 analyzer_get_trigger_address(zp->usb->devhdl));
666 packet.type = SR_DF_HEADER;
667 packet.payload = &header;
668 header.feed_version = 1;
669 gettimeofday(&header.starttime, NULL);
670 header.samplerate = zp->cur_samplerate;
671 header.num_logic_probes = zp->num_channels;
672 sr_session_bus(session_data, &packet);
674 if (!(buf = g_try_malloc(PACKET_SIZE))) {
675 sr_err("zp: %s: buf malloc failed", __func__);
676 return SR_ERR_MALLOC;
680 analyzer_read_start(zp->usb->devhdl);
681 /* Send the incoming transfer to the session bus. */
682 for (packet_num = 0; packet_num < (zp->memory_size * 4 / PACKET_SIZE);
684 res = analyzer_read_data(zp->usb->devhdl, buf, PACKET_SIZE);
685 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
688 packet.type = SR_DF_LOGIC;
689 packet.payload = &logic;
690 logic.length = PACKET_SIZE;
693 sr_session_bus(session_data, &packet);
694 samples_read += res / 4;
696 analyzer_read_stop(zp->usb->devhdl);
699 packet.type = SR_DF_END;
700 sr_session_bus(session_data, &packet);
705 /* This stops acquisition on ALL devices, ignoring dev_index. */
706 static int hw_acquisition_stop(int dev_index, gpointer session_dev_id)
708 struct sr_datafeed_packet packet;
709 struct sr_dev_inst *sdi;
712 packet.type = SR_DF_END;
713 sr_session_bus(session_dev_id, &packet);
715 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
716 sr_err("zp: %s: sdi was NULL", __func__);
720 if (!(zp = sdi->priv)) {
721 sr_err("zp: %s: sdi->priv was NULL", __func__);
725 analyzer_reset(zp->usb->devhdl);
726 /* TODO: Need to cancel and free any queued up transfers. */
731 SR_PRIV struct sr_dev_plugin zeroplus_logic_cube_plugin_info = {
732 .name = "zeroplus-logic-cube",
733 .longname = "Zeroplus Logic Cube LAP-C series",
736 .cleanup = hw_cleanup,
737 .dev_open = hw_dev_open,
738 .dev_close = hw_dev_close,
739 .dev_info_get = hw_dev_info_get,
740 .dev_status_get = hw_dev_status_get,
741 .hwcap_get_all = hw_hwcap_get_all,
742 .config_set = hw_config_set,
743 .acquisition_start = hw_acquisition_start,
744 .acquisition_stop = hw_acquisition_stop,