]> sigrok.org Git - libsigrok.git/blob - hardware/zeroplus-logic-cube/zeroplus.c
sr: remove obsolete SR_DI_INST
[libsigrok.git] / hardware / zeroplus-logic-cube / zeroplus.c
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
46 typedef 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  */
59 static 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
72 static 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  */
87 static 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(). */
108 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
109 static struct sr_dev_driver *zdi = &zeroplus_logic_cube_driver_info;
110
111 static 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  */
122 static 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
144 static const struct sr_samplerates samplerates = {
145         0,
146         0,
147         0,
148         supported_samplerates,
149 };
150
151 /* Private, per-device-instance driver context. */
152 struct 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
166 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
167                 const void *value);
168 static int hw_dev_close(struct sr_dev_inst *sdi);
169
170 static 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
184 static 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);
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
225 static 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
246 static 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
257 static 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
335 static 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
425 static 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
448 static 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
460 static 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_HWCAPS:
467                 *data = hwcaps;
468                 break;
469         case SR_DI_NUM_PROBES:
470                 if (sdi) {
471                         ctx = sdi->priv;
472                         *data = GINT_TO_POINTER(ctx->num_channels);
473                         sr_spew("zp: %s: Returning number of channels: %d.",
474                                         __func__, ctx->num_channels);
475                 } else
476                         return SR_ERR;
477                 break;
478         case SR_DI_PROBE_NAMES:
479                 *data = probe_names;
480                 sr_spew("zp: %s: Returning probenames.", __func__);
481                 break;
482         case SR_DI_SAMPLERATES:
483                 *data = &samplerates;
484                 sr_spew("zp: %s: Returning samplerates.", __func__);
485                 break;
486         case SR_DI_TRIGGER_TYPES:
487                 *data = TRIGGER_TYPES;
488                 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, TRIGGER_TYPES);
489                 break;
490         case SR_DI_CUR_SAMPLERATE:
491                 if (sdi) {
492                         ctx = sdi->priv;
493                         *data = &ctx->cur_samplerate;
494                         sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
495                                 __func__, ctx->cur_samplerate);
496                 } else
497                         return SR_ERR;
498                 break;
499         default:
500                 return SR_ERR_ARG;
501         }
502
503         return SR_OK;
504 }
505
506 static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
507 {
508         struct context *ctx;
509
510         if (!sdi) {
511                 sr_err("zp: %s: sdi was NULL", __func__);
512                 return SR_ERR_ARG;
513         }
514
515         if (!(ctx = sdi->priv)) {
516                 sr_err("zp: %s: sdi->priv was NULL", __func__);
517                 return SR_ERR_ARG;
518         }
519
520         sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
521
522         if (samplerate > SR_MHZ(1))
523                 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
524         else if (samplerate > SR_KHZ(1))
525                 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
526         else
527                 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
528
529         ctx->cur_samplerate = samplerate;
530
531         return SR_OK;
532 }
533
534 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
535                 const void *value)
536 {
537         struct context *ctx;
538
539         if (!(ctx = sdi->priv)) {
540                 sr_err("zp: %s: sdi->priv was NULL", __func__);
541                 return SR_ERR_ARG;
542         }
543
544         switch (hwcap) {
545         case SR_HWCAP_SAMPLERATE:
546                 return set_samplerate(sdi, *(const uint64_t *)value);
547         case SR_HWCAP_PROBECONFIG:
548                 return configure_probes(sdi, (const GSList *)value);
549         case SR_HWCAP_LIMIT_SAMPLES:
550                 ctx->limit_samples = *(const uint64_t *)value;
551                 return SR_OK;
552         default:
553                 return SR_ERR;
554         }
555 }
556
557 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
558                 void *cb_data)
559 {
560         struct sr_datafeed_packet packet;
561         struct sr_datafeed_logic logic;
562         struct sr_datafeed_header header;
563         struct sr_datafeed_meta_logic meta;
564         uint64_t samples_read;
565         int res;
566         unsigned int packet_num;
567         unsigned char *buf;
568         struct context *ctx;
569
570         if (!(ctx = sdi->priv)) {
571                 sr_err("zp: %s: sdi->priv was NULL", __func__);
572                 return SR_ERR_ARG;
573         }
574
575         /* push configured settings to device */
576         analyzer_configure(ctx->usb->devhdl);
577
578         analyzer_start(ctx->usb->devhdl);
579         sr_info("zp: Waiting for data");
580         analyzer_wait_data(ctx->usb->devhdl);
581
582         sr_info("zp: Stop address    = 0x%x",
583                 analyzer_get_stop_address(ctx->usb->devhdl));
584         sr_info("zp: Now address     = 0x%x",
585                 analyzer_get_now_address(ctx->usb->devhdl));
586         sr_info("zp: Trigger address = 0x%x",
587                 analyzer_get_trigger_address(ctx->usb->devhdl));
588
589         packet.type = SR_DF_HEADER;
590         packet.payload = &header;
591         header.feed_version = 1;
592         gettimeofday(&header.starttime, NULL);
593         sr_session_send(cb_data, &packet);
594
595         /* Send metadata about the SR_DF_LOGIC packets to come. */
596         packet.type = SR_DF_META_LOGIC;
597         packet.payload = &meta;
598         meta.samplerate = ctx->cur_samplerate;
599         meta.num_probes = ctx->num_channels;
600         sr_session_send(cb_data, &packet);
601
602         if (!(buf = g_try_malloc(PACKET_SIZE))) {
603                 sr_err("zp: %s: buf malloc failed", __func__);
604                 return SR_ERR_MALLOC;
605         }
606
607         samples_read = 0;
608         analyzer_read_start(ctx->usb->devhdl);
609         /* Send the incoming transfer to the session bus. */
610         for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
611              packet_num++) {
612                 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
613                 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
614                         PACKET_SIZE, res);
615
616                 packet.type = SR_DF_LOGIC;
617                 packet.payload = &logic;
618                 logic.length = PACKET_SIZE;
619                 logic.unitsize = 4;
620                 logic.data = buf;
621                 sr_session_send(cb_data, &packet);
622                 samples_read += res / 4;
623         }
624         analyzer_read_stop(ctx->usb->devhdl);
625         g_free(buf);
626
627         packet.type = SR_DF_END;
628         sr_session_send(cb_data, &packet);
629
630         return SR_OK;
631 }
632
633 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
634 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
635                 void *cb_data)
636 {
637         struct sr_datafeed_packet packet;
638         struct context *ctx;
639
640         packet.type = SR_DF_END;
641         sr_session_send(cb_data, &packet);
642
643         if (!(ctx = sdi->priv)) {
644                 sr_err("zp: %s: sdi->priv was NULL", __func__);
645                 return SR_ERR_BUG;
646         }
647
648         analyzer_reset(ctx->usb->devhdl);
649         /* TODO: Need to cancel and free any queued up transfers. */
650
651         return SR_OK;
652 }
653
654 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
655         .name = "zeroplus-logic-cube",
656         .longname = "ZEROPLUS Logic Cube LAP-C series",
657         .api_version = 1,
658         .init = hw_init,
659         .cleanup = hw_cleanup,
660         .scan = hw_scan,
661         .dev_open = hw_dev_open,
662         .dev_close = hw_dev_close,
663         .info_get = hw_info_get,
664         .dev_config_set = hw_dev_config_set,
665         .dev_acquisition_start = hw_dev_acquisition_start,
666         .dev_acquisition_stop = hw_dev_acquisition_stop,
667         .instances = NULL,
668 };