]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/zeroplus-logic-cube/zeroplus.c
sr: properly free probes when freeing their device instance
[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 driver context. */
152struct drv_context {
153 GSList *instances;
154};
155
156/* Private, per-device-instance driver context. */
157struct dev_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;
162 uint8_t probe_mask;
163 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
164 uint8_t trigger_value[NUM_TRIGGER_STAGES];
165 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
166
167 /* TODO: this belongs in the device instance */
168 struct sr_usb_dev_inst *usb;
169};
170
171static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
172 const void *value);
173static int hw_dev_close(struct sr_dev_inst *sdi);
174
175static unsigned int get_memory_size(int type)
176{
177 if (type == MEMORY_SIZE_8K)
178 return 8 * 1024;
179 else if (type == MEMORY_SIZE_64K)
180 return 64 * 1024;
181 else if (type == MEMORY_SIZE_128K)
182 return 128 * 1024;
183 else if (type == MEMORY_SIZE_512K)
184 return 512 * 1024;
185 else
186 return 0;
187}
188
189static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes)
190{
191 struct dev_context *devc;
192 const struct sr_probe *probe;
193 const GSList *l;
194 int probe_bit, stage, i;
195 char *tc;
196
197 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
198 devc = sdi->priv;
199
200 devc->probe_mask = 0;
201 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
202 devc->trigger_mask[i] = 0;
203 devc->trigger_value[i] = 0;
204 }
205
206 stage = -1;
207 for (l = probes; l; l = l->next) {
208 probe = (struct sr_probe *)l->data;
209 if (probe->enabled == FALSE)
210 continue;
211 probe_bit = 1 << (probe->index);
212 devc->probe_mask |= probe_bit;
213
214 if (probe->trigger) {
215 stage = 0;
216 for (tc = probe->trigger; *tc; tc++) {
217 devc->trigger_mask[stage] |= probe_bit;
218 if (*tc == '1')
219 devc->trigger_value[stage] |= probe_bit;
220 stage++;
221 if (stage > NUM_TRIGGER_STAGES)
222 return SR_ERR;
223 }
224 }
225 }
226
227 return SR_OK;
228}
229
230static void clear_instances(void)
231{
232 GSList *l;
233 struct sr_dev_inst *sdi;
234 struct drv_context *drvc;
235
236 drvc = zdi->priv;
237 for (l = drvc->instances; l; l = l->next) {
238 sdi = l->data;
239 /* Properly close all devices... */
240 hw_dev_close(sdi);
241 /* ...and free all their memory. */
242 sr_dev_inst_free(sdi);
243 }
244 g_slist_free(drvc->instances);
245 drvc->instances = NULL;
246
247}
248
249/*
250 * API callbacks
251 */
252
253static int hw_init(void)
254{
255 struct drv_context *drvc;
256
257 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
258 sr_err("zeroplus: driver context malloc failed.");
259 return SR_ERR;
260 }
261 zdi->priv = drvc;
262
263 if (libusb_init(&usb_context) != 0) {
264 sr_err("zp: Failed to initialize USB.");
265 return 0;
266 }
267
268 return SR_OK;
269}
270
271static GSList *hw_scan(GSList *options)
272{
273 struct sr_dev_inst *sdi;
274 struct sr_probe *probe;
275 struct drv_context *drvc;
276 struct dev_context *devc;
277 model_t *prof;
278 struct libusb_device_descriptor des;
279 libusb_device **devlist;
280 GSList *devices;
281 int ret, devcnt, i, j;
282
283 (void)options;
284 drvc = zdi->priv;
285 devices = NULL;
286
287 clear_instances();
288
289 /* Find all ZEROPLUS analyzers and add them to device list. */
290 devcnt = 0;
291 libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
292
293 for (i = 0; devlist[i]; i++) {
294 ret = libusb_get_device_descriptor(devlist[i], &des);
295 if (ret != 0) {
296 sr_err("zp: failed to get device descriptor: %d", ret);
297 continue;
298 }
299
300 prof = NULL;
301 for (j = 0; j < zeroplus_models[j].vid; j++) {
302 if (des.idVendor == zeroplus_models[j].vid &&
303 des.idProduct == zeroplus_models[j].pid) {
304 prof = &zeroplus_models[j];
305 }
306 }
307 /* Skip if the device was not found */
308 if (!prof)
309 continue;
310 sr_info("zp: Found ZEROPLUS model %s", prof->model_name);
311
312 /* Register the device with libsigrok. */
313 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
314 VENDOR_NAME, prof->model_name, NULL))) {
315 sr_err("zp: %s: sr_dev_inst_new failed", __func__);
316 return NULL;
317 }
318 sdi->driver = zdi;
319
320 /* Allocate memory for our private driver context. */
321 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
322 sr_err("zp: %s: devc malloc failed", __func__);
323 return 0;
324 }
325 sdi->priv = devc;
326 devc->num_channels = prof->channels;
327 devc->memory_size = prof->sample_depth * 1024;
328 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
329
330 /* Fill in probelist according to this device's profile. */
331 for (j = 0; j < devc->num_channels; j++) {
332 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
333 probe_names[j])))
334 return NULL;
335 sdi->probes = g_slist_append(sdi->probes, probe);
336 }
337
338 devices = g_slist_append(devices, sdi);
339 drvc->instances = g_slist_append(drvc->instances, sdi);
340 devc->usb = sr_usb_dev_inst_new(
341 libusb_get_bus_number(devlist[i]),
342 libusb_get_device_address(devlist[i]), NULL);
343 devcnt++;
344
345 }
346 libusb_free_device_list(devlist, 1);
347
348 return devices;
349}
350
351static int hw_dev_open(struct sr_dev_inst *sdi)
352{
353 struct dev_context *devc;
354 libusb_device **devlist, *dev;
355 struct libusb_device_descriptor des;
356 int device_count, ret, i;
357
358 if (!(devc = sdi->priv)) {
359 sr_err("zp: %s: sdi->priv was NULL", __func__);
360 return SR_ERR_ARG;
361 }
362
363 device_count = libusb_get_device_list(usb_context, &devlist);
364 if (device_count < 0) {
365 sr_err("zp: Failed to retrieve device list");
366 return SR_ERR;
367 }
368
369 dev = NULL;
370 for (i = 0; i < device_count; i++) {
371 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
372 sr_err("fx2lafw: Failed to get device descriptor: %d.",
373 ret);
374 continue;
375 }
376 if (libusb_get_bus_number(devlist[i]) == devc->usb->bus
377 && libusb_get_device_address(devlist[i]) == devc->usb->address) {
378 dev = devlist[i];
379 break;
380 }
381 }
382 if (!dev) {
383 sr_err("device on bus %d address %d disappeared!",
384 devc->usb->bus, devc->usb->address);
385 return SR_ERR;
386 }
387
388 if (!(ret = libusb_open(dev, &(devc->usb->devhdl)))) {
389 sdi->status = SR_ST_ACTIVE;
390 sr_info("zp: opened device %d on %d.%d interface %d",
391 sdi->index, devc->usb->bus,
392 devc->usb->address, USB_INTERFACE);
393 } else {
394 sr_err("zp: failed to open device: %d", ret);
395 return SR_ERR;
396 }
397
398 ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION);
399 if (ret < 0) {
400 sr_err("zp: Unable to set USB configuration %d: %d",
401 USB_CONFIGURATION, ret);
402 return SR_ERR;
403 }
404
405 ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
406 if (ret != 0) {
407 sr_err("zp: Unable to claim interface: %d", ret);
408 return SR_ERR;
409 }
410
411 analyzer_reset(devc->usb->devhdl);
412 analyzer_initialize(devc->usb->devhdl);
413
414 analyzer_set_memory_size(MEMORY_SIZE_512K);
415 // analyzer_set_freq(g_freq, g_freq_scale);
416 analyzer_set_trigger_count(1);
417 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
418 // * get_memory_size(g_memory_size)) / 100) >> 2);
419 analyzer_set_ramsize_trigger_address(
420 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
421
422#if 0
423 if (g_double_mode == 1)
424 analyzer_set_compression(COMPRESSION_DOUBLE);
425 else if (g_compression == 1)
426 analyzer_set_compression(COMPRESSION_ENABLE);
427 else
428#endif
429 analyzer_set_compression(COMPRESSION_NONE);
430
431 if (devc->cur_samplerate == 0) {
432 /* Samplerate hasn't been set. Default to the slowest one. */
433 if (hw_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
434 &samplerates.list[0]) == SR_ERR)
435 return SR_ERR;
436 }
437
438 return SR_OK;
439}
440
441static int hw_dev_close(struct sr_dev_inst *sdi)
442{
443 struct dev_context *devc;
444
445 if (!(devc = sdi->priv)) {
446 sr_err("zp: %s: sdi->priv was NULL", __func__);
447 return SR_ERR;
448 }
449
450 if (!devc->usb->devhdl)
451 return SR_ERR;
452
453 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
454 devc->usb->bus, devc->usb->address, USB_INTERFACE);
455 libusb_release_interface(devc->usb->devhdl, USB_INTERFACE);
456 libusb_reset_device(devc->usb->devhdl);
457 libusb_close(devc->usb->devhdl);
458 devc->usb->devhdl = NULL;
459 sdi->status = SR_ST_INACTIVE;
460
461 return SR_OK;
462}
463
464static int hw_cleanup(void)
465{
466 struct drv_context *drvc;
467
468 if (!(drvc = zdi->priv))
469 return SR_OK;
470
471 clear_instances();
472
473 if (usb_context)
474 libusb_exit(usb_context);
475 usb_context = NULL;
476
477 return SR_OK;
478}
479
480static int hw_info_get(int info_id, const void **data,
481 const struct sr_dev_inst *sdi)
482{
483 struct dev_context *devc;
484
485 switch (info_id) {
486 case SR_DI_HWCAPS:
487 *data = hwcaps;
488 break;
489 case SR_DI_NUM_PROBES:
490 if (sdi) {
491 devc = sdi->priv;
492 *data = GINT_TO_POINTER(devc->num_channels);
493 sr_spew("zp: %s: Returning number of channels: %d.",
494 __func__, devc->num_channels);
495 } else
496 return SR_ERR;
497 break;
498 case SR_DI_PROBE_NAMES:
499 *data = probe_names;
500 sr_spew("zp: %s: Returning probenames.", __func__);
501 break;
502 case SR_DI_SAMPLERATES:
503 *data = &samplerates;
504 sr_spew("zp: %s: Returning samplerates.", __func__);
505 break;
506 case SR_DI_TRIGGER_TYPES:
507 *data = TRIGGER_TYPES;
508 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, TRIGGER_TYPES);
509 break;
510 case SR_DI_CUR_SAMPLERATE:
511 if (sdi) {
512 devc = sdi->priv;
513 *data = &devc->cur_samplerate;
514 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
515 __func__, devc->cur_samplerate);
516 } else
517 return SR_ERR;
518 break;
519 default:
520 return SR_ERR_ARG;
521 }
522
523 return SR_OK;
524}
525
526static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
527{
528 struct dev_context *devc;
529
530 if (!sdi) {
531 sr_err("zp: %s: sdi was NULL", __func__);
532 return SR_ERR_ARG;
533 }
534
535 if (!(devc = sdi->priv)) {
536 sr_err("zp: %s: sdi->priv was NULL", __func__);
537 return SR_ERR_ARG;
538 }
539
540 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
541
542 if (samplerate > SR_MHZ(1))
543 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
544 else if (samplerate > SR_KHZ(1))
545 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
546 else
547 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
548
549 devc->cur_samplerate = samplerate;
550
551 return SR_OK;
552}
553
554static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
555 const void *value)
556{
557 struct dev_context *devc;
558
559 if (!(devc = sdi->priv)) {
560 sr_err("zp: %s: sdi->priv was NULL", __func__);
561 return SR_ERR_ARG;
562 }
563
564 switch (hwcap) {
565 case SR_HWCAP_SAMPLERATE:
566 return set_samplerate(sdi, *(const uint64_t *)value);
567 case SR_HWCAP_PROBECONFIG:
568 return configure_probes(sdi, (const GSList *)value);
569 case SR_HWCAP_LIMIT_SAMPLES:
570 devc->limit_samples = *(const uint64_t *)value;
571 return SR_OK;
572 default:
573 return SR_ERR;
574 }
575}
576
577static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
578 void *cb_data)
579{
580 struct sr_datafeed_packet packet;
581 struct sr_datafeed_logic logic;
582 struct sr_datafeed_header header;
583 struct sr_datafeed_meta_logic meta;
584 uint64_t samples_read;
585 int res;
586 unsigned int packet_num;
587 unsigned char *buf;
588 struct dev_context *devc;
589
590 if (!(devc = sdi->priv)) {
591 sr_err("zp: %s: sdi->priv was NULL", __func__);
592 return SR_ERR_ARG;
593 }
594
595 /* push configured settings to device */
596 analyzer_configure(devc->usb->devhdl);
597
598 analyzer_start(devc->usb->devhdl);
599 sr_info("zp: Waiting for data");
600 analyzer_wait_data(devc->usb->devhdl);
601
602 sr_info("zp: Stop address = 0x%x",
603 analyzer_get_stop_address(devc->usb->devhdl));
604 sr_info("zp: Now address = 0x%x",
605 analyzer_get_now_address(devc->usb->devhdl));
606 sr_info("zp: Trigger address = 0x%x",
607 analyzer_get_trigger_address(devc->usb->devhdl));
608
609 packet.type = SR_DF_HEADER;
610 packet.payload = &header;
611 header.feed_version = 1;
612 gettimeofday(&header.starttime, NULL);
613 sr_session_send(cb_data, &packet);
614
615 /* Send metadata about the SR_DF_LOGIC packets to come. */
616 packet.type = SR_DF_META_LOGIC;
617 packet.payload = &meta;
618 meta.samplerate = devc->cur_samplerate;
619 meta.num_probes = devc->num_channels;
620 sr_session_send(cb_data, &packet);
621
622 if (!(buf = g_try_malloc(PACKET_SIZE))) {
623 sr_err("zp: %s: buf malloc failed", __func__);
624 return SR_ERR_MALLOC;
625 }
626
627 samples_read = 0;
628 analyzer_read_start(devc->usb->devhdl);
629 /* Send the incoming transfer to the session bus. */
630 for (packet_num = 0; packet_num < (devc->memory_size * 4 / PACKET_SIZE);
631 packet_num++) {
632 res = analyzer_read_data(devc->usb->devhdl, buf, PACKET_SIZE);
633 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
634 PACKET_SIZE, res);
635
636 packet.type = SR_DF_LOGIC;
637 packet.payload = &logic;
638 logic.length = PACKET_SIZE;
639 logic.unitsize = 4;
640 logic.data = buf;
641 sr_session_send(cb_data, &packet);
642 samples_read += res / 4;
643 }
644 analyzer_read_stop(devc->usb->devhdl);
645 g_free(buf);
646
647 packet.type = SR_DF_END;
648 sr_session_send(cb_data, &packet);
649
650 return SR_OK;
651}
652
653/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
654static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
655 void *cb_data)
656{
657 struct sr_datafeed_packet packet;
658 struct dev_context *devc;
659
660 packet.type = SR_DF_END;
661 sr_session_send(cb_data, &packet);
662
663 if (!(devc = sdi->priv)) {
664 sr_err("zp: %s: sdi->priv was NULL", __func__);
665 return SR_ERR_BUG;
666 }
667
668 analyzer_reset(devc->usb->devhdl);
669 /* TODO: Need to cancel and free any queued up transfers. */
670
671 return SR_OK;
672}
673
674SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
675 .name = "zeroplus-logic-cube",
676 .longname = "ZEROPLUS Logic Cube LAP-C series",
677 .api_version = 1,
678 .init = hw_init,
679 .cleanup = hw_cleanup,
680 .scan = hw_scan,
681 .dev_open = hw_dev_open,
682 .dev_close = hw_dev_close,
683 .info_get = hw_info_get,
684 .dev_config_set = hw_dev_config_set,
685 .dev_acquisition_start = hw_dev_acquisition_start,
686 .dev_acquisition_stop = hw_dev_acquisition_stop,
687 .priv = NULL,
688};