]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/zeroplus-logic-cube/zeroplus.c
sr/drivers: remove driver API call dev_status_get()
[libsigrok.git] / hardware / zeroplus-logic-cube / zeroplus.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/time.h>
24#include <inttypes.h>
25#include <glib.h>
26#include <libusb.h>
27#include "config.h"
28#include "libsigrok.h"
29#include "libsigrok-internal.h"
30#include "analyzer.h"
31
32#define USB_VENDOR 0x0c12
33
34#define VENDOR_NAME "ZEROPLUS"
35#define MODEL_NAME "Logic Cube LAP-C"
36#define MODEL_VERSION NULL
37
38#define NUM_PROBES 16
39#define USB_INTERFACE 0
40#define USB_CONFIGURATION 1
41#define NUM_TRIGGER_STAGES 4
42#define TRIGGER_TYPES "01"
43
44#define PACKET_SIZE 2048 /* ?? */
45
46typedef struct {
47 unsigned short vid;
48 unsigned short pid;
49 char *model_name;
50 unsigned int channels;
51 unsigned int sample_depth; /* In Ksamples/channel */
52 unsigned int max_sampling_freq;
53} model_t;
54
55/*
56 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
57 * same 128K sample depth.
58 */
59static model_t zeroplus_models[] = {
60 {0x0c12, 0x7009, "LAP-C(16064)", 16, 64, 100},
61 {0x0c12, 0x700A, "LAP-C(16128)", 16, 128, 200},
62 /* TODO: we don't know anything about these
63 {0x0c12, 0x700B, "LAP-C(32128)", 32, 128, 200},
64 {0x0c12, 0x700C, "LAP-C(321000)", 32, 1024, 200},
65 {0x0c12, 0x700D, "LAP-C(322000)", 32, 2048, 200},
66 */
67 {0x0c12, 0x700E, "LAP-C(16032)", 16, 32, 100},
68 {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200},
69 { 0, 0, 0, 0, 0, 0 }
70};
71
72static const int hwcaps[] = {
73 SR_HWCAP_LOGIC_ANALYZER,
74 SR_HWCAP_SAMPLERATE,
75 SR_HWCAP_PROBECONFIG,
76 SR_HWCAP_CAPTURE_RATIO,
77
78 /* These are really implemented in the driver, not the hardware. */
79 SR_HWCAP_LIMIT_SAMPLES,
80 0,
81};
82
83/*
84 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
85 * We currently ignore other untested/unsupported devices here.
86 */
87static const char *probe_names[NUM_PROBES + 1] = {
88 "A0",
89 "A1",
90 "A2",
91 "A3",
92 "A4",
93 "A5",
94 "A6",
95 "A7",
96 "B0",
97 "B1",
98 "B2",
99 "B3",
100 "B4",
101 "B5",
102 "B6",
103 "B7",
104 NULL,
105};
106
107/* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
108SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
109static struct sr_dev_driver *zdi = &zeroplus_logic_cube_driver_info;
110
111static libusb_context *usb_context = NULL;
112
113/*
114 * The hardware supports more samplerates than these, but these are the
115 * options hardcoded into the vendor's Windows GUI.
116 */
117
118/*
119 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
120 * that high.
121 */
122static const uint64_t supported_samplerates[] = {
123 SR_HZ(100),
124 SR_HZ(500),
125 SR_KHZ(1),
126 SR_KHZ(5),
127 SR_KHZ(25),
128 SR_KHZ(50),
129 SR_KHZ(100),
130 SR_KHZ(200),
131 SR_KHZ(400),
132 SR_KHZ(800),
133 SR_MHZ(1),
134 SR_MHZ(10),
135 SR_MHZ(25),
136 SR_MHZ(50),
137 SR_MHZ(80),
138 SR_MHZ(100),
139 SR_MHZ(150),
140 SR_MHZ(200),
141 0,
142};
143
144static const struct sr_samplerates samplerates = {
145 0,
146 0,
147 0,
148 supported_samplerates,
149};
150
151/* Private, per-device-instance driver context. */
152struct context {
153 uint64_t cur_samplerate;
154 uint64_t limit_samples;
155 int num_channels; /* TODO: This isn't initialized before it's needed :( */
156 uint64_t memory_size;
157 uint8_t probe_mask;
158 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
159 uint8_t trigger_value[NUM_TRIGGER_STAGES];
160 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
161
162 /* TODO: this belongs in the device instance */
163 struct sr_usb_dev_inst *usb;
164};
165
166static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
167 const void *value);
168static int hw_dev_close(struct sr_dev_inst *sdi);
169
170static unsigned int get_memory_size(int type)
171{
172 if (type == MEMORY_SIZE_8K)
173 return 8 * 1024;
174 else if (type == MEMORY_SIZE_64K)
175 return 64 * 1024;
176 else if (type == MEMORY_SIZE_128K)
177 return 128 * 1024;
178 else if (type == MEMORY_SIZE_512K)
179 return 512 * 1024;
180 else
181 return 0;
182}
183
184static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes)
185{
186 struct context *ctx;
187 const struct sr_probe *probe;
188 const GSList *l;
189 int probe_bit, stage, i;
190 char *tc;
191
192 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
193 ctx = sdi->priv;
194
195 ctx->probe_mask = 0;
196 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
197 ctx->trigger_mask[i] = 0;
198 ctx->trigger_value[i] = 0;
199 }
200
201 stage = -1;
202 for (l = probes; l; l = l->next) {
203 probe = (struct sr_probe *)l->data;
204 if (probe->enabled == FALSE)
205 continue;
206 probe_bit = 1 << (probe->index - 1);
207 ctx->probe_mask |= probe_bit;
208
209 if (probe->trigger) {
210 stage = 0;
211 for (tc = probe->trigger; *tc; tc++) {
212 ctx->trigger_mask[stage] |= probe_bit;
213 if (*tc == '1')
214 ctx->trigger_value[stage] |= probe_bit;
215 stage++;
216 if (stage > NUM_TRIGGER_STAGES)
217 return SR_ERR;
218 }
219 }
220 }
221
222 return SR_OK;
223}
224
225static void clear_instances(void)
226{
227 GSList *l;
228 struct sr_dev_inst *sdi;
229
230 for (l = zdi->instances; l; l = l->next) {
231 sdi = l->data;
232 /* Properly close all devices... */
233 hw_dev_close(sdi);
234 /* ...and free all their memory. */
235 sr_dev_inst_free(sdi);
236 }
237 g_slist_free(zdi->instances);
238 zdi->instances = NULL;
239
240}
241
242/*
243 * API callbacks
244 */
245
246static int hw_init(void)
247{
248
249 if (libusb_init(&usb_context) != 0) {
250 sr_err("zp: Failed to initialize USB.");
251 return 0;
252 }
253
254 return SR_OK;
255}
256
257static GSList *hw_scan(GSList *options)
258{
259 struct sr_dev_inst *sdi;
260 struct sr_probe *probe;
261 struct context *ctx;
262 model_t *prof;
263 struct libusb_device_descriptor des;
264 libusb_device **devlist;
265 GSList *devices;
266 int ret, devcnt, i, j;
267
268 (void)options;
269 devices = NULL;
270
271 clear_instances();
272
273 /* Find all ZEROPLUS analyzers and add them to device list. */
274 devcnt = 0;
275 libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
276
277 for (i = 0; devlist[i]; i++) {
278 ret = libusb_get_device_descriptor(devlist[i], &des);
279 if (ret != 0) {
280 sr_err("zp: failed to get device descriptor: %d", ret);
281 continue;
282 }
283
284 prof = NULL;
285 for (j = 0; j < zeroplus_models[j].vid; j++) {
286 if (des.idVendor == zeroplus_models[j].vid &&
287 des.idProduct == zeroplus_models[j].pid) {
288 prof = &zeroplus_models[j];
289 }
290 }
291 /* Skip if the device was not found */
292 if (!prof)
293 continue;
294 sr_info("zp: Found ZEROPLUS model %s", prof->model_name);
295
296 /* Register the device with libsigrok. */
297 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
298 VENDOR_NAME, prof->model_name, NULL))) {
299 sr_err("zp: %s: sr_dev_inst_new failed", __func__);
300 return NULL;
301 }
302 sdi->driver = zdi;
303
304 /* Allocate memory for our private driver context. */
305 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
306 sr_err("zp: %s: ctx malloc failed", __func__);
307 return 0;
308 }
309 sdi->priv = ctx;
310 ctx->num_channels = prof->channels;
311 ctx->memory_size = prof->sample_depth * 1024;
312 // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES);
313
314 /* Fill in probelist according to this device's profile. */
315 for (j = 0; j < ctx->num_channels; j++) {
316 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
317 probe_names[j])))
318 return NULL;
319 sdi->probes = g_slist_append(sdi->probes, probe);
320 }
321
322 devices = g_slist_append(devices, sdi);
323 zdi->instances = g_slist_append(zdi->instances, sdi);
324 ctx->usb = sr_usb_dev_inst_new(
325 libusb_get_bus_number(devlist[i]),
326 libusb_get_device_address(devlist[i]), NULL);
327 devcnt++;
328
329 }
330 libusb_free_device_list(devlist, 1);
331
332 return devices;
333}
334
335static int hw_dev_open(struct sr_dev_inst *sdi)
336{
337 struct context *ctx;
338 libusb_device **devlist, *dev;
339 struct libusb_device_descriptor des;
340 int device_count, ret, i;
341
342 if (!(ctx = sdi->priv)) {
343 sr_err("zp: %s: sdi->priv was NULL", __func__);
344 return SR_ERR_ARG;
345 }
346
347 device_count = libusb_get_device_list(usb_context, &devlist);
348 if (device_count < 0) {
349 sr_err("zp: Failed to retrieve device list");
350 return SR_ERR;
351 }
352
353 dev = NULL;
354 for (i = 0; i < device_count; i++) {
355 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
356 sr_err("fx2lafw: Failed to get device descriptor: %d.",
357 ret);
358 continue;
359 }
360 if (libusb_get_bus_number(devlist[i]) == ctx->usb->bus
361 && libusb_get_device_address(devlist[i]) == ctx->usb->address) {
362 dev = devlist[i];
363 break;
364 }
365 }
366 if (!dev) {
367 sr_err("device on bus %d address %d disappeared!",
368 ctx->usb->bus, ctx->usb->address);
369 return SR_ERR;
370 }
371
372 if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) {
373 sdi->status = SR_ST_ACTIVE;
374 sr_info("zp: opened device %d on %d.%d interface %d",
375 sdi->index, ctx->usb->bus,
376 ctx->usb->address, USB_INTERFACE);
377 } else {
378 sr_err("zp: failed to open device: %d", ret);
379 return SR_ERR;
380 }
381
382 ret = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
383 if (ret < 0) {
384 sr_err("zp: Unable to set USB configuration %d: %d",
385 USB_CONFIGURATION, ret);
386 return SR_ERR;
387 }
388
389 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
390 if (ret != 0) {
391 sr_err("zp: Unable to claim interface: %d", ret);
392 return SR_ERR;
393 }
394
395 analyzer_reset(ctx->usb->devhdl);
396 analyzer_initialize(ctx->usb->devhdl);
397
398 analyzer_set_memory_size(MEMORY_SIZE_512K);
399 // analyzer_set_freq(g_freq, g_freq_scale);
400 analyzer_set_trigger_count(1);
401 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
402 // * get_memory_size(g_memory_size)) / 100) >> 2);
403 analyzer_set_ramsize_trigger_address(
404 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
405
406#if 0
407 if (g_double_mode == 1)
408 analyzer_set_compression(COMPRESSION_DOUBLE);
409 else if (g_compression == 1)
410 analyzer_set_compression(COMPRESSION_ENABLE);
411 else
412#endif
413 analyzer_set_compression(COMPRESSION_NONE);
414
415 if (ctx->cur_samplerate == 0) {
416 /* Samplerate hasn't been set. Default to the slowest one. */
417 if (hw_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
418 &samplerates.list[0]) == SR_ERR)
419 return SR_ERR;
420 }
421
422 return SR_OK;
423}
424
425static int hw_dev_close(struct sr_dev_inst *sdi)
426{
427 struct context *ctx;
428
429 if (!(ctx = sdi->priv)) {
430 sr_err("zp: %s: sdi->priv was NULL", __func__);
431 return SR_ERR;
432 }
433
434 if (!ctx->usb->devhdl)
435 return SR_ERR;
436
437 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
438 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
439 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
440 libusb_reset_device(ctx->usb->devhdl);
441 libusb_close(ctx->usb->devhdl);
442 ctx->usb->devhdl = NULL;
443 sdi->status = SR_ST_INACTIVE;
444
445 return SR_OK;
446}
447
448static int hw_cleanup(void)
449{
450
451 clear_instances();
452
453 if (usb_context)
454 libusb_exit(usb_context);
455 usb_context = NULL;
456
457 return SR_OK;
458}
459
460static int hw_info_get(int info_id, const void **data,
461 const struct sr_dev_inst *sdi)
462{
463 struct context *ctx;
464
465 switch (info_id) {
466 case SR_DI_INST:
467 *data = sdi;
468 sr_spew("zp: %s: Returning sdi.", __func__);
469 break;
470 case SR_DI_HWCAPS:
471 *data = hwcaps;
472 break;
473 case SR_DI_NUM_PROBES:
474 if (sdi) {
475 ctx = sdi->priv;
476 *data = GINT_TO_POINTER(ctx->num_channels);
477 sr_spew("zp: %s: Returning number of channels: %d.",
478 __func__, ctx->num_channels);
479 } else
480 return SR_ERR;
481 break;
482 case SR_DI_PROBE_NAMES:
483 *data = probe_names;
484 sr_spew("zp: %s: Returning probenames.", __func__);
485 break;
486 case SR_DI_SAMPLERATES:
487 *data = &samplerates;
488 sr_spew("zp: %s: Returning samplerates.", __func__);
489 break;
490 case SR_DI_TRIGGER_TYPES:
491 *data = TRIGGER_TYPES;
492 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, TRIGGER_TYPES);
493 break;
494 case SR_DI_CUR_SAMPLERATE:
495 if (sdi) {
496 ctx = sdi->priv;
497 *data = &ctx->cur_samplerate;
498 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
499 __func__, ctx->cur_samplerate);
500 } else
501 return SR_ERR;
502 break;
503 default:
504 return SR_ERR_ARG;
505 }
506
507 return SR_OK;
508}
509
510static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
511{
512 struct context *ctx;
513
514 if (!sdi) {
515 sr_err("zp: %s: sdi was NULL", __func__);
516 return SR_ERR_ARG;
517 }
518
519 if (!(ctx = sdi->priv)) {
520 sr_err("zp: %s: sdi->priv was NULL", __func__);
521 return SR_ERR_ARG;
522 }
523
524 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
525
526 if (samplerate > SR_MHZ(1))
527 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
528 else if (samplerate > SR_KHZ(1))
529 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
530 else
531 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
532
533 ctx->cur_samplerate = samplerate;
534
535 return SR_OK;
536}
537
538static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
539 const void *value)
540{
541 struct context *ctx;
542
543 if (!(ctx = sdi->priv)) {
544 sr_err("zp: %s: sdi->priv was NULL", __func__);
545 return SR_ERR_ARG;
546 }
547
548 switch (hwcap) {
549 case SR_HWCAP_SAMPLERATE:
550 return set_samplerate(sdi, *(const uint64_t *)value);
551 case SR_HWCAP_PROBECONFIG:
552 return configure_probes(sdi, (const GSList *)value);
553 case SR_HWCAP_LIMIT_SAMPLES:
554 ctx->limit_samples = *(const uint64_t *)value;
555 return SR_OK;
556 default:
557 return SR_ERR;
558 }
559}
560
561static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
562 void *cb_data)
563{
564 struct sr_datafeed_packet packet;
565 struct sr_datafeed_logic logic;
566 struct sr_datafeed_header header;
567 struct sr_datafeed_meta_logic meta;
568 uint64_t samples_read;
569 int res;
570 unsigned int packet_num;
571 unsigned char *buf;
572 struct context *ctx;
573
574 if (!(ctx = sdi->priv)) {
575 sr_err("zp: %s: sdi->priv was NULL", __func__);
576 return SR_ERR_ARG;
577 }
578
579 /* push configured settings to device */
580 analyzer_configure(ctx->usb->devhdl);
581
582 analyzer_start(ctx->usb->devhdl);
583 sr_info("zp: Waiting for data");
584 analyzer_wait_data(ctx->usb->devhdl);
585
586 sr_info("zp: Stop address = 0x%x",
587 analyzer_get_stop_address(ctx->usb->devhdl));
588 sr_info("zp: Now address = 0x%x",
589 analyzer_get_now_address(ctx->usb->devhdl));
590 sr_info("zp: Trigger address = 0x%x",
591 analyzer_get_trigger_address(ctx->usb->devhdl));
592
593 packet.type = SR_DF_HEADER;
594 packet.payload = &header;
595 header.feed_version = 1;
596 gettimeofday(&header.starttime, NULL);
597 sr_session_send(cb_data, &packet);
598
599 /* Send metadata about the SR_DF_LOGIC packets to come. */
600 packet.type = SR_DF_META_LOGIC;
601 packet.payload = &meta;
602 meta.samplerate = ctx->cur_samplerate;
603 meta.num_probes = ctx->num_channels;
604 sr_session_send(cb_data, &packet);
605
606 if (!(buf = g_try_malloc(PACKET_SIZE))) {
607 sr_err("zp: %s: buf malloc failed", __func__);
608 return SR_ERR_MALLOC;
609 }
610
611 samples_read = 0;
612 analyzer_read_start(ctx->usb->devhdl);
613 /* Send the incoming transfer to the session bus. */
614 for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
615 packet_num++) {
616 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
617 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
618 PACKET_SIZE, res);
619
620 packet.type = SR_DF_LOGIC;
621 packet.payload = &logic;
622 logic.length = PACKET_SIZE;
623 logic.unitsize = 4;
624 logic.data = buf;
625 sr_session_send(cb_data, &packet);
626 samples_read += res / 4;
627 }
628 analyzer_read_stop(ctx->usb->devhdl);
629 g_free(buf);
630
631 packet.type = SR_DF_END;
632 sr_session_send(cb_data, &packet);
633
634 return SR_OK;
635}
636
637/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
638static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
639 void *cb_data)
640{
641 struct sr_datafeed_packet packet;
642 struct context *ctx;
643
644 packet.type = SR_DF_END;
645 sr_session_send(cb_data, &packet);
646
647 if (!(ctx = sdi->priv)) {
648 sr_err("zp: %s: sdi->priv was NULL", __func__);
649 return SR_ERR_BUG;
650 }
651
652 analyzer_reset(ctx->usb->devhdl);
653 /* TODO: Need to cancel and free any queued up transfers. */
654
655 return SR_OK;
656}
657
658SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
659 .name = "zeroplus-logic-cube",
660 .longname = "ZEROPLUS Logic Cube LAP-C series",
661 .api_version = 1,
662 .init = hw_init,
663 .cleanup = hw_cleanup,
664 .scan = hw_scan,
665 .dev_open = hw_dev_open,
666 .dev_close = hw_dev_close,
667 .info_get = hw_info_get,
668 .dev_config_set = hw_dev_config_set,
669 .dev_acquisition_start = hw_dev_acquisition_start,
670 .dev_acquisition_stop = hw_dev_acquisition_stop,
671 .instances = NULL,
672};