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,
78 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
79 * We currently ignore other untested/unsupported devices here.
81 static const char *probe_names[] = {
101 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
102 static GSList *dev_insts = NULL;
104 static libusb_context *usb_context = NULL;
107 * The hardware supports more samplerates than these, but these are the
108 * options hardcoded into the vendor's Windows GUI.
112 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
115 static uint64_t supported_samplerates[] = {
137 static struct sr_samplerates samplerates = {
141 supported_samplerates,
144 /* Private, per-device-instance driver context. */
146 uint64_t cur_samplerate;
147 uint64_t limit_samples;
148 int num_channels; /* TODO: This isn't initialized before it's needed :( */
149 uint64_t memory_size;
151 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
152 uint8_t trigger_value[NUM_TRIGGER_STAGES];
153 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
155 struct sr_usb_dev_inst *usb;
158 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
160 static unsigned int get_memory_size(int type)
162 if (type == MEMORY_SIZE_8K)
164 else if (type == MEMORY_SIZE_64K)
166 else if (type == MEMORY_SIZE_128K)
168 else if (type == MEMORY_SIZE_512K)
174 static int opendev4(struct sr_dev_inst **sdi, libusb_device *dev,
175 struct libusb_device_descriptor *des)
181 /* Note: sdi is non-NULL, the caller already checked this. */
183 if (!(ctx = (*sdi)->priv)) {
184 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
188 if ((err = libusb_get_device_descriptor(dev, des))) {
189 sr_err("zp: failed to get device descriptor: %d", err);
193 if (des->idVendor != USB_VENDOR)
196 if (libusb_get_bus_number(dev) == ctx->usb->bus
197 && libusb_get_device_address(dev) == ctx->usb->address) {
199 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
200 if (!(des->idProduct == zeroplus_models[i].pid))
203 sr_info("zp: Found ZeroPlus device 0x%04x (%s)",
204 des->idProduct, zeroplus_models[i].model_name);
205 ctx->num_channels = zeroplus_models[i].channels;
206 ctx->memory_size = zeroplus_models[i].sample_depth * 1024;
210 if (ctx->num_channels == 0) {
211 sr_err("zp: Unknown ZeroPlus device 0x%04x",
217 if (!(err = libusb_open(dev, &(ctx->usb->devhdl)))) {
218 (*sdi)->status = SR_ST_ACTIVE;
219 sr_info("zp: opened device %d on %d.%d interface %d",
220 (*sdi)->index, ctx->usb->bus,
221 ctx->usb->address, USB_INTERFACE);
223 sr_err("zp: failed to open device: %d", err);
231 static struct sr_dev_inst *zp_open_dev(int dev_index)
233 struct sr_dev_inst *sdi;
234 libusb_device **devlist;
235 struct libusb_device_descriptor des;
238 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
241 libusb_get_device_list(usb_context, &devlist);
242 if (sdi->status == SR_ST_INACTIVE) {
243 /* Find the device by vendor, product, bus and address. */
244 libusb_get_device_list(usb_context, &devlist);
245 for (i = 0; devlist[i]; i++) {
246 /* TODO: Error handling. */
247 opendev4(&sdi, devlist[i], &des);
250 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
253 libusb_free_device_list(devlist, 1);
255 if (sdi && sdi->status != SR_ST_ACTIVE)
261 static void close_dev(struct sr_dev_inst *sdi)
265 if (!(ctx = sdi->priv)) {
266 sr_err("zp: %s: sdi->priv was NULL", __func__);
270 if (!ctx->usb->devhdl)
273 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
274 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
275 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
276 libusb_reset_device(ctx->usb->devhdl);
277 libusb_close(ctx->usb->devhdl);
278 ctx->usb->devhdl = NULL;
279 /* TODO: Call libusb_exit() here or only in hw_cleanup()? */
280 sdi->status = SR_ST_INACTIVE;
283 static int configure_probes(struct sr_dev_inst *sdi, GSList *probes)
286 struct sr_probe *probe;
288 int probe_bit, stage, i;
291 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
295 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
296 ctx->trigger_mask[i] = 0;
297 ctx->trigger_value[i] = 0;
301 for (l = probes; l; l = l->next) {
302 probe = (struct sr_probe *)l->data;
303 if (probe->enabled == FALSE)
305 probe_bit = 1 << (probe->index - 1);
306 ctx->probe_mask |= probe_bit;
308 if (probe->trigger) {
310 for (tc = probe->trigger; *tc; tc++) {
311 ctx->trigger_mask[stage] |= probe_bit;
313 ctx->trigger_value[stage] |= probe_bit;
315 if (stage > NUM_TRIGGER_STAGES)
328 static int hw_init(const char *devinfo)
330 struct sr_dev_inst *sdi;
331 struct libusb_device_descriptor des;
332 libusb_device **devlist;
336 /* Avoid compiler warnings. */
339 /* Allocate memory for our private driver context. */
340 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
341 sr_err("zp: %s: ctx malloc failed", __func__);
345 /* Set some sane defaults. */
346 ctx->cur_samplerate = 0;
347 ctx->limit_samples = 0;
348 ctx->num_channels = 32; /* TODO: This isn't initialized before it's needed :( */
349 ctx->memory_size = 0;
351 memset(ctx->trigger_mask, 0, NUM_TRIGGER_STAGES);
352 memset(ctx->trigger_value, 0, NUM_TRIGGER_STAGES);
353 // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES);
355 if (libusb_init(&usb_context) != 0) {
356 sr_err("zp: Failed to initialize USB.");
360 /* Find all ZeroPlus analyzers and add them to device list. */
362 libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
364 for (i = 0; devlist[i]; i++) {
365 err = libusb_get_device_descriptor(devlist[i], &des);
367 sr_err("zp: failed to get device descriptor: %d", err);
371 if (des.idVendor == USB_VENDOR) {
373 * Definitely a Zeroplus.
374 * TODO: Any way to detect specific model/version in
375 * the zeroplus range?
377 /* Register the device with libsigrok. */
378 if (!(sdi = sr_dev_inst_new(devcnt,
379 SR_ST_INACTIVE, USB_VENDOR_NAME,
380 USB_MODEL_NAME, USB_MODEL_VERSION))) {
381 sr_err("zp: %s: sr_dev_inst_new failed",
389 g_slist_append(dev_insts, sdi);
390 ctx->usb = sr_usb_dev_inst_new(
391 libusb_get_bus_number(devlist[i]),
392 libusb_get_device_address(devlist[i]), NULL);
396 libusb_free_device_list(devlist, 1);
401 static int hw_dev_open(int dev_index)
403 struct sr_dev_inst *sdi;
407 if (!(sdi = zp_open_dev(dev_index))) {
408 sr_err("zp: unable to open device");
412 /* TODO: Note: sdi is retrieved in zp_open_dev(). */
414 if (!(ctx = sdi->priv)) {
415 sr_err("zp: %s: sdi->priv was NULL", __func__);
419 err = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
421 sr_err("zp: Unable to set USB configuration %d: %d",
422 USB_CONFIGURATION, err);
426 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
428 sr_err("zp: Unable to claim interface: %d", err);
432 analyzer_reset(ctx->usb->devhdl);
433 analyzer_initialize(ctx->usb->devhdl);
435 analyzer_set_memory_size(MEMORY_SIZE_512K);
436 // analyzer_set_freq(g_freq, g_freq_scale);
437 analyzer_set_trigger_count(1);
438 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
439 // * get_memory_size(g_memory_size)) / 100) >> 2);
440 analyzer_set_ramsize_trigger_address(
441 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
444 if (g_double_mode == 1)
445 analyzer_set_compression(COMPRESSION_DOUBLE);
446 else if (g_compression == 1)
447 analyzer_set_compression(COMPRESSION_ENABLE);
450 analyzer_set_compression(COMPRESSION_NONE);
452 if (ctx->cur_samplerate == 0) {
453 /* Samplerate hasn't been set. Default to the slowest one. */
454 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
455 &samplerates.list[0]) == SR_ERR)
462 static int hw_dev_close(int dev_index)
464 struct sr_dev_inst *sdi;
466 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
467 sr_err("zp: %s: sdi was NULL", __func__);
468 return SR_ERR; /* TODO: SR_ERR_ARG? */
477 static int hw_cleanup(void)
480 struct sr_dev_inst *sdi;
482 for (l = dev_insts; l; l = l->next) {
484 /* Properly close all devices... */
486 /* ...and free all their memory. */
487 sr_dev_inst_free(sdi);
489 g_slist_free(dev_insts);
493 libusb_exit(usb_context);
499 static void *hw_dev_info_get(int dev_index, int dev_info_id)
501 struct sr_dev_inst *sdi;
505 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
506 sr_err("zp: %s: sdi was NULL", __func__);
510 if (!(ctx = sdi->priv)) {
511 sr_err("zp: %s: sdi->priv was NULL", __func__);
515 switch (dev_info_id) {
519 case SR_DI_NUM_PROBES:
520 info = GINT_TO_POINTER(ctx->num_channels);
522 case SR_DI_PROBE_NAMES:
525 case SR_DI_SAMPLERATES:
528 case SR_DI_TRIGGER_TYPES:
529 info = TRIGGER_TYPES;
531 case SR_DI_CUR_SAMPLERATE:
532 info = &ctx->cur_samplerate;
535 /* Unknown device info ID, return NULL. */
536 sr_err("zp: %s: Unknown device info ID", __func__);
544 static int hw_dev_status_get(int dev_index)
546 struct sr_dev_inst *sdi;
548 sdi = sr_dev_inst_get(dev_insts, dev_index);
552 return SR_ST_NOT_FOUND;
555 static int *hw_hwcap_get_all(void)
560 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
565 sr_err("zp: %s: sdi was NULL", __func__);
569 if (!(ctx = sdi->priv)) {
570 sr_err("zp: %s: sdi->priv was NULL", __func__);
574 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
576 if (samplerate > SR_MHZ(1))
577 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
578 else if (samplerate > SR_KHZ(1))
579 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
581 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
583 ctx->cur_samplerate = samplerate;
588 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
590 struct sr_dev_inst *sdi;
593 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
594 sr_err("zp: %s: sdi was NULL", __func__);
598 if (!(ctx = sdi->priv)) {
599 sr_err("zp: %s: sdi->priv was NULL", __func__);
604 case SR_HWCAP_SAMPLERATE:
605 return set_samplerate(sdi, *(uint64_t *)value);
606 case SR_HWCAP_PROBECONFIG:
607 return configure_probes(sdi, (GSList *)value);
608 case SR_HWCAP_LIMIT_SAMPLES:
609 ctx->limit_samples = *(uint64_t *)value;
616 static int hw_dev_acquisition_start(int dev_index, void *session_data)
618 struct sr_dev_inst *sdi;
619 struct sr_datafeed_packet packet;
620 struct sr_datafeed_logic logic;
621 struct sr_datafeed_header header;
622 uint64_t samples_read;
624 unsigned int packet_num;
628 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
629 sr_err("zp: %s: sdi was NULL", __func__);
633 if (!(ctx = sdi->priv)) {
634 sr_err("zp: %s: sdi->priv was NULL", __func__);
638 /* push configured settings to device */
639 analyzer_configure(ctx->usb->devhdl);
641 analyzer_start(ctx->usb->devhdl);
642 sr_info("zp: Waiting for data");
643 analyzer_wait_data(ctx->usb->devhdl);
645 sr_info("zp: Stop address = 0x%x",
646 analyzer_get_stop_address(ctx->usb->devhdl));
647 sr_info("zp: Now address = 0x%x",
648 analyzer_get_now_address(ctx->usb->devhdl));
649 sr_info("zp: Trigger address = 0x%x",
650 analyzer_get_trigger_address(ctx->usb->devhdl));
652 packet.type = SR_DF_HEADER;
653 packet.payload = &header;
654 header.feed_version = 1;
655 gettimeofday(&header.starttime, NULL);
656 header.samplerate = ctx->cur_samplerate;
657 header.num_logic_probes = ctx->num_channels;
658 sr_session_send(session_data, &packet);
660 if (!(buf = g_try_malloc(PACKET_SIZE))) {
661 sr_err("zp: %s: buf malloc failed", __func__);
662 return SR_ERR_MALLOC;
666 analyzer_read_start(ctx->usb->devhdl);
667 /* Send the incoming transfer to the session bus. */
668 for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
670 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
671 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
674 packet.type = SR_DF_LOGIC;
675 packet.payload = &logic;
676 logic.length = PACKET_SIZE;
679 sr_session_send(session_data, &packet);
680 samples_read += res / 4;
682 analyzer_read_stop(ctx->usb->devhdl);
685 packet.type = SR_DF_END;
686 sr_session_send(session_data, &packet);
691 /* This stops acquisition on ALL devices, ignoring dev_index. */
692 static int hw_dev_acquisition_stop(int dev_index, void *session_dev_id)
694 struct sr_datafeed_packet packet;
695 struct sr_dev_inst *sdi;
698 packet.type = SR_DF_END;
699 sr_session_send(session_dev_id, &packet);
701 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
702 sr_err("zp: %s: sdi was NULL", __func__);
706 if (!(ctx = sdi->priv)) {
707 sr_err("zp: %s: sdi->priv was NULL", __func__);
711 analyzer_reset(ctx->usb->devhdl);
712 /* TODO: Need to cancel and free any queued up transfers. */
717 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
718 .name = "zeroplus-logic-cube",
719 .longname = "Zeroplus Logic Cube LAP-C series",
722 .cleanup = hw_cleanup,
723 .dev_open = hw_dev_open,
724 .dev_close = hw_dev_close,
725 .dev_info_get = hw_dev_info_get,
726 .dev_status_get = hw_dev_status_get,
727 .hwcap_get_all = hw_hwcap_get_all,
728 .dev_config_set = hw_dev_config_set,
729 .dev_acquisition_start = hw_dev_acquisition_start,
730 .dev_acquisition_stop = hw_dev_acquisition_stop,