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 static GSList *dev_insts = NULL;
106 static libusb_context *usb_context = NULL;
109 * The hardware supports more samplerates than these, but these are the
110 * options hardcoded into the vendor's Windows GUI.
114 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
117 static const uint64_t supported_samplerates[] = {
139 static const struct sr_samplerates samplerates = {
143 supported_samplerates,
146 /* Private, per-device-instance driver context. */
148 uint64_t cur_samplerate;
149 uint64_t limit_samples;
150 int num_channels; /* TODO: This isn't initialized before it's needed :( */
151 uint64_t memory_size;
153 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
154 uint8_t trigger_value[NUM_TRIGGER_STAGES];
155 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
157 struct sr_usb_dev_inst *usb;
160 static int hw_dev_config_set(int dev_index, int hwcap, const void *value);
162 static unsigned int get_memory_size(int type)
164 if (type == MEMORY_SIZE_8K)
166 else if (type == MEMORY_SIZE_64K)
168 else if (type == MEMORY_SIZE_128K)
170 else if (type == MEMORY_SIZE_512K)
176 static int opendev4(struct sr_dev_inst **sdi, libusb_device *dev,
177 struct libusb_device_descriptor *des)
183 /* Note: sdi is non-NULL, the caller already checked this. */
185 if (!(ctx = (*sdi)->priv)) {
186 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
190 if ((ret = libusb_get_device_descriptor(dev, des))) {
191 sr_err("zp: failed to get device descriptor: %d", ret);
195 if (des->idVendor != USB_VENDOR)
198 if (libusb_get_bus_number(dev) == ctx->usb->bus
199 && libusb_get_device_address(dev) == ctx->usb->address) {
201 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
202 if (!(des->idProduct == zeroplus_models[i].pid))
205 sr_info("zp: Found ZEROPLUS device 0x%04x (%s)",
206 des->idProduct, zeroplus_models[i].model_name);
207 ctx->num_channels = zeroplus_models[i].channels;
208 ctx->memory_size = zeroplus_models[i].sample_depth * 1024;
212 if (ctx->num_channels == 0) {
213 sr_err("zp: Unknown ZEROPLUS device 0x%04x",
219 if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) {
220 (*sdi)->status = SR_ST_ACTIVE;
221 sr_info("zp: opened device %d on %d.%d interface %d",
222 (*sdi)->index, ctx->usb->bus,
223 ctx->usb->address, USB_INTERFACE);
225 sr_err("zp: failed to open device: %d", ret);
233 static struct sr_dev_inst *zp_open_dev(int dev_index)
235 struct sr_dev_inst *sdi;
236 libusb_device **devlist;
237 struct libusb_device_descriptor des;
240 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
243 libusb_get_device_list(usb_context, &devlist);
244 if (sdi->status == SR_ST_INACTIVE) {
245 /* Find the device by vendor, product, bus and address. */
246 libusb_get_device_list(usb_context, &devlist);
247 for (i = 0; devlist[i]; i++) {
248 /* TODO: Error handling. */
249 opendev4(&sdi, devlist[i], &des);
252 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
255 libusb_free_device_list(devlist, 1);
257 if (sdi && sdi->status != SR_ST_ACTIVE)
263 static void close_dev(struct sr_dev_inst *sdi)
267 if (!(ctx = sdi->priv)) {
268 sr_err("zp: %s: sdi->priv was NULL", __func__);
272 if (!ctx->usb->devhdl)
275 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
276 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
277 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
278 libusb_reset_device(ctx->usb->devhdl);
279 libusb_close(ctx->usb->devhdl);
280 ctx->usb->devhdl = NULL;
281 /* TODO: Call libusb_exit() here or only in hw_cleanup()? */
282 sdi->status = SR_ST_INACTIVE;
285 static int configure_probes(struct sr_dev_inst *sdi, const GSList *probes)
288 const struct sr_probe *probe;
290 int probe_bit, stage, i;
293 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
297 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
298 ctx->trigger_mask[i] = 0;
299 ctx->trigger_value[i] = 0;
303 for (l = probes; l; l = l->next) {
304 probe = (struct sr_probe *)l->data;
305 if (probe->enabled == FALSE)
307 probe_bit = 1 << (probe->index - 1);
308 ctx->probe_mask |= probe_bit;
310 if (probe->trigger) {
312 for (tc = probe->trigger; *tc; tc++) {
313 ctx->trigger_mask[stage] |= probe_bit;
315 ctx->trigger_value[stage] |= probe_bit;
317 if (stage > NUM_TRIGGER_STAGES)
330 static int hw_init(void)
332 struct sr_dev_inst *sdi;
333 struct libusb_device_descriptor des;
334 libusb_device **devlist;
338 /* Allocate memory for our private driver context. */
339 if (!(ctx = g_try_malloc(sizeof(struct context)))) {
340 sr_err("zp: %s: ctx malloc failed", __func__);
344 /* Set some sane defaults. */
345 ctx->cur_samplerate = 0;
346 ctx->limit_samples = 0;
347 /* TODO: num_channels isn't initialized before it's needed :( */
348 ctx->num_channels = NUM_PROBES;
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 ret = libusb_get_device_descriptor(devlist[i], &des);
367 sr_err("zp: failed to get device descriptor: %d", ret);
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, VENDOR_NAME,
380 MODEL_NAME, 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 ret = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
421 sr_err("zp: Unable to set USB configuration %d: %d",
422 USB_CONFIGURATION, ret);
426 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
428 sr_err("zp: Unable to claim interface: %d", ret);
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 const 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 sr_spew("zp: %s: dev_index %d, dev_info_id %d.", __func__,
516 dev_index, dev_info_id);
518 switch (dev_info_id) {
521 sr_spew("zp: %s: Returning sdi.", __func__);
523 case SR_DI_NUM_PROBES:
524 info = GINT_TO_POINTER(ctx->num_channels);
525 sr_spew("zp: %s: Returning number of probes: %d.", __func__,
528 case SR_DI_PROBE_NAMES:
530 sr_spew("zp: %s: Returning probenames.", __func__);
532 case SR_DI_SAMPLERATES:
534 sr_spew("zp: %s: Returning samplerates.", __func__);
536 case SR_DI_TRIGGER_TYPES:
537 info = TRIGGER_TYPES;
538 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, info);
540 case SR_DI_CUR_SAMPLERATE:
541 info = &ctx->cur_samplerate;
542 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
543 __func__, ctx->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 const int *hw_hwcap_get_all(void)
571 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
576 sr_err("zp: %s: sdi was NULL", __func__);
580 if (!(ctx = 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 ctx->cur_samplerate = samplerate;
599 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
601 struct sr_dev_inst *sdi;
604 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
605 sr_err("zp: %s: sdi was NULL", __func__);
609 if (!(ctx = sdi->priv)) {
610 sr_err("zp: %s: sdi->priv was NULL", __func__);
615 case SR_HWCAP_SAMPLERATE:
616 return set_samplerate(sdi, *(const uint64_t *)value);
617 case SR_HWCAP_PROBECONFIG:
618 return configure_probes(sdi, (const GSList *)value);
619 case SR_HWCAP_LIMIT_SAMPLES:
620 ctx->limit_samples = *(const uint64_t *)value;
627 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
629 struct sr_dev_inst *sdi;
630 struct sr_datafeed_packet packet;
631 struct sr_datafeed_logic logic;
632 struct sr_datafeed_header header;
633 struct sr_datafeed_meta_logic meta;
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 sr_session_send(cb_data, &packet);
670 /* Send metadata about the SR_DF_LOGIC packets to come. */
671 packet.type = SR_DF_META_LOGIC;
672 packet.payload = &meta;
673 meta.samplerate = ctx->cur_samplerate;
674 meta.num_probes = ctx->num_channels;
675 sr_session_send(cb_data, &packet);
677 if (!(buf = g_try_malloc(PACKET_SIZE))) {
678 sr_err("zp: %s: buf malloc failed", __func__);
679 return SR_ERR_MALLOC;
683 analyzer_read_start(ctx->usb->devhdl);
684 /* Send the incoming transfer to the session bus. */
685 for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
687 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
688 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
691 packet.type = SR_DF_LOGIC;
692 packet.payload = &logic;
693 logic.length = PACKET_SIZE;
696 sr_session_send(cb_data, &packet);
697 samples_read += res / 4;
699 analyzer_read_stop(ctx->usb->devhdl);
702 packet.type = SR_DF_END;
703 sr_session_send(cb_data, &packet);
708 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
709 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
711 struct sr_datafeed_packet packet;
712 struct sr_dev_inst *sdi;
715 packet.type = SR_DF_END;
716 sr_session_send(cb_data, &packet);
718 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
719 sr_err("zp: %s: sdi was NULL", __func__);
723 if (!(ctx = sdi->priv)) {
724 sr_err("zp: %s: sdi->priv was NULL", __func__);
728 analyzer_reset(ctx->usb->devhdl);
729 /* TODO: Need to cancel and free any queued up transfers. */
734 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
735 .name = "zeroplus-logic-cube",
736 .longname = "ZEROPLUS Logic Cube LAP-C series",
739 .cleanup = hw_cleanup,
740 .dev_open = hw_dev_open,
741 .dev_close = hw_dev_close,
742 .dev_info_get = hw_dev_info_get,
743 .dev_status_get = hw_dev_status_get,
744 .hwcap_get_all = hw_hwcap_get_all,
745 .dev_config_set = hw_dev_config_set,
746 .dev_acquisition_start = hw_dev_acquisition_start,
747 .dev_acquisition_stop = hw_dev_acquisition_stop,