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