]> sigrok.org Git - libsigrok.git/blob - hardware/zeroplus-logic-cube/zeroplus.c
libsigrok: closedev() now has a return code.
[libsigrok.git] / hardware / zeroplus-logic-cube / zeroplus.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 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 "config.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/time.h>
24 #include <inttypes.h>
25 #include <glib.h>
26 #include <libusb.h>
27 #include <sigrok.h>
28 #include <sigrok-internal.h>
29 #include "analyzer.h"
30
31 #define USB_VENDOR                      0x0c12
32 #define USB_VENDOR_NAME                 "Zeroplus"
33 #define USB_MODEL_NAME                  "Logic Cube"
34 #define USB_MODEL_VERSION               ""
35
36 #define USB_INTERFACE                   0
37 #define USB_CONFIGURATION               1
38 #define NUM_TRIGGER_STAGES              4
39 #define TRIGGER_TYPES                   "01"
40
41 #define PACKET_SIZE                     2048    /* ?? */
42
43 typedef struct {
44         unsigned short pid;
45         char model_name[64];
46         unsigned int channels;
47         unsigned int sample_depth;      /* In Ksamples/channel */
48         unsigned int max_sampling_freq;
49 } model_t;
50
51 /*
52  * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
53  * same 128K sample depth.
54  */
55 static model_t zeroplus_models[] = {
56         {0x7009, "LAP-C(16064)",  16, 64,   100},
57         {0x700A, "LAP-C(16128)",  16, 128,  200},
58         {0x700B, "LAP-C(32128)",  32, 128,  200},
59         {0x700C, "LAP-C(321000)", 32, 1024, 200},
60         {0x700D, "LAP-C(322000)", 32, 2048, 200},
61         {0x700E, "LAP-C(16032)",  16, 32,   100},
62         {0x7016, "LAP-C(162000)", 16, 2048, 200},
63 };
64
65 static int capabilities[] = {
66         SR_HWCAP_LOGIC_ANALYZER,
67         SR_HWCAP_SAMPLERATE,
68         SR_HWCAP_PROBECONFIG,
69         SR_HWCAP_CAPTURE_RATIO,
70
71         /* These are really implemented in the driver, not the hardware. */
72         SR_HWCAP_LIMIT_SAMPLES,
73         0,
74 };
75
76 /* List of struct sr_device_instance, maintained by opendev()/closedev(). */
77 static GSList *device_instances = NULL;
78
79 static libusb_context *usb_context = NULL;
80
81 /*
82  * The hardware supports more samplerates than these, but these are the
83  * options hardcoded into the vendor's Windows GUI.
84  */
85
86 /*
87  * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
88  * that high.
89  */
90 static uint64_t supported_samplerates[] = {
91         SR_HZ(100),
92         SR_HZ(500),
93         SR_KHZ(1),
94         SR_KHZ(5),
95         SR_KHZ(25),
96         SR_KHZ(50),
97         SR_KHZ(100),
98         SR_KHZ(200),
99         SR_KHZ(400),
100         SR_KHZ(800),
101         SR_MHZ(1),
102         SR_MHZ(10),
103         SR_MHZ(25),
104         SR_MHZ(50),
105         SR_MHZ(80),
106         SR_MHZ(100),
107         SR_MHZ(150),
108         SR_MHZ(200),
109         0,
110 };
111
112 static struct sr_samplerates samplerates = {
113         SR_HZ(0),
114         SR_HZ(0),
115         SR_HZ(0),
116         supported_samplerates,
117 };
118
119 /* TODO: All of these should go in a device-specific struct. */
120 static uint64_t cur_samplerate = 0;
121 static uint64_t limit_samples = 0;
122 static int num_channels = 32; /* TODO: This isn't initialized before it's needed :( */
123 static uint64_t memory_size = 0;
124 static uint8_t probe_mask = 0;
125 static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
126 static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
127
128 // static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
129
130 static int hw_set_configuration(int device_index, int capability, void *value);
131
132 static unsigned int get_memory_size(int type)
133 {
134         if (type == MEMORY_SIZE_8K)
135                 return 8 * 1024;
136         else if (type == MEMORY_SIZE_64K)
137                 return 64 * 1024;
138         else if (type == MEMORY_SIZE_128K)
139                 return 128 * 1024;
140         else if (type == MEMORY_SIZE_512K)
141                 return 512 * 1024;
142         else
143                 return 0;
144 }
145
146 static int opendev4(struct sr_device_instance **sdi, libusb_device *dev,
147                     struct libusb_device_descriptor *des)
148 {
149         unsigned int i;
150         int err;
151
152         if ((err = libusb_get_device_descriptor(dev, des))) {
153                 sr_warn("failed to get device descriptor: %d", err);
154                 return -1;
155         }
156
157         if (des->idVendor != USB_VENDOR)
158                 return 0;
159
160         if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
161             && libusb_get_device_address(dev) == (*sdi)->usb->address) {
162
163                 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
164                         if (!(des->idProduct == zeroplus_models[i].pid))
165                                 continue;
166
167                         sr_info("Found PID=%04X (%s)", des->idProduct,
168                                 zeroplus_models[i].model_name);
169                         num_channels = zeroplus_models[i].channels;
170                         memory_size = zeroplus_models[i].sample_depth * 1024;
171                         break;
172                 }
173
174                 if (num_channels == 0) {
175                         sr_warn("Unknown ZeroPlus device %04X", des->idProduct);
176                         return -2;
177                 }
178
179                 /* Found it. */
180                 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
181                         (*sdi)->status = SR_ST_ACTIVE;
182                         sr_info("opened device %d on %d.%d interface %d",
183                                 (*sdi)->index, (*sdi)->usb->bus,
184                                 (*sdi)->usb->address, USB_INTERFACE);
185                 } else {
186                         sr_warn("failed to open device: %d", err);
187                         *sdi = NULL;
188                 }
189         }
190
191         return 0;
192 }
193
194 static struct sr_device_instance *zp_open_device(int device_index)
195 {
196         struct sr_device_instance *sdi;
197         libusb_device **devlist;
198         struct libusb_device_descriptor des;
199         int err, i;
200
201         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
202                 return NULL;
203
204         libusb_get_device_list(usb_context, &devlist);
205         if (sdi->status == SR_ST_INACTIVE) {
206                 /* Find the device by vendor, product, bus and address. */
207                 libusb_get_device_list(usb_context, &devlist);
208                 for (i = 0; devlist[i]; i++) {
209                         /* TODO: Error handling. */
210                         err = opendev4(&sdi, devlist[i], &des);
211                 }
212         } else {
213                 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
214                 sdi = NULL;
215         }
216         libusb_free_device_list(devlist, 1);
217
218         if (sdi && sdi->status != SR_ST_ACTIVE)
219                 sdi = NULL;
220
221         return sdi;
222 }
223
224 static void close_device(struct sr_device_instance *sdi)
225 {
226         if (!sdi->usb->devhdl)
227                 return;
228
229         sr_info("closing device %d on %d.%d interface %d", sdi->index,
230                 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
231         libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
232         libusb_close(sdi->usb->devhdl);
233         sdi->usb->devhdl = NULL;
234         sdi->status = SR_ST_INACTIVE;
235 }
236
237 static int configure_probes(GSList *probes)
238 {
239         struct sr_probe *probe;
240         GSList *l;
241         int probe_bit, stage, i;
242         char *tc;
243
244         probe_mask = 0;
245         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
246                 trigger_mask[i] = 0;
247                 trigger_value[i] = 0;
248         }
249
250         stage = -1;
251         for (l = probes; l; l = l->next) {
252                 probe = (struct sr_probe *)l->data;
253                 if (probe->enabled == FALSE)
254                         continue;
255                 probe_bit = 1 << (probe->index - 1);
256                 probe_mask |= probe_bit;
257
258                 if (probe->trigger) {
259                         stage = 0;
260                         for (tc = probe->trigger; *tc; tc++) {
261                                 trigger_mask[stage] |= probe_bit;
262                                 if (*tc == '1')
263                                         trigger_value[stage] |= probe_bit;
264                                 stage++;
265                                 if (stage > NUM_TRIGGER_STAGES)
266                                         return SR_ERR;
267                         }
268                 }
269         }
270
271         return SR_OK;
272 }
273
274 /*
275  * API callbacks
276  */
277
278 static int hw_init(const char *deviceinfo)
279 {
280         struct sr_device_instance *sdi;
281         struct libusb_device_descriptor des;
282         libusb_device **devlist;
283         int err, devcnt, i;
284
285         /* Avoid compiler warnings. */
286         deviceinfo = deviceinfo;
287
288         if (libusb_init(&usb_context) != 0) {
289                 sr_warn("Failed to initialize USB.");
290                 return 0;
291         }
292
293         /* Find all ZeroPlus analyzers and add them to device list. */
294         devcnt = 0;
295         libusb_get_device_list(usb_context, &devlist);
296
297         for (i = 0; devlist[i]; i++) {
298                 err = libusb_get_device_descriptor(devlist[i], &des);
299                 if (err != 0) {
300                         sr_warn("failed to get device descriptor: %d", err);
301                         continue;
302                 }
303
304                 if (des.idVendor == USB_VENDOR) {
305                         /*
306                          * Definitely a Zeroplus.
307                          * TODO: Any way to detect specific model/version in
308                          * the zeroplus range?
309                          */
310                         sdi = sr_device_instance_new(devcnt,
311                                         SR_ST_INACTIVE, USB_VENDOR_NAME,
312                                         USB_MODEL_NAME, USB_MODEL_VERSION);
313                         if (!sdi)
314                                 return 0;
315                         device_instances =
316                             g_slist_append(device_instances, sdi);
317                         sdi->usb = sr_usb_device_instance_new(
318                                 libusb_get_bus_number(devlist[i]),
319                                 libusb_get_device_address(devlist[i]), NULL);
320                         devcnt++;
321                 }
322         }
323         libusb_free_device_list(devlist, 1);
324
325         return devcnt;
326 }
327
328 static int hw_opendev(int device_index)
329 {
330         struct sr_device_instance *sdi;
331         int err;
332
333         if (!(sdi = zp_open_device(device_index))) {
334                 sr_warn("unable to open device");
335                 return SR_ERR;
336         }
337
338         err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
339         if (err != 0) {
340                 sr_warn("Unable to claim interface: %d", err);
341                 return SR_ERR;
342         }
343         analyzer_reset(sdi->usb->devhdl);
344         analyzer_initialize(sdi->usb->devhdl);
345
346         analyzer_set_memory_size(MEMORY_SIZE_512K);
347         // analyzer_set_freq(g_freq, g_freq_scale);
348         analyzer_set_trigger_count(1);
349         // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
350         // * get_memory_size(g_memory_size)) / 100) >> 2);
351         analyzer_set_ramsize_trigger_address(
352                 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
353
354 #if 0
355         if (g_double_mode == 1)
356                 analyzer_set_compression(COMPRESSION_DOUBLE);
357         else if (g_compression == 1)
358                 analyzer_set_compression(COMPRESSION_ENABLE);
359         else
360 #endif
361         analyzer_set_compression(COMPRESSION_NONE);
362
363         if (cur_samplerate == 0) {
364                 /* Samplerate hasn't been set. Default to the slowest one. */
365                 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
366                      &samplerates.low) == SR_ERR)
367                         return SR_ERR;
368         }
369
370         return SR_OK;
371 }
372
373 static int hw_closedev(int device_index)
374 {
375         struct sr_device_instance *sdi;
376
377         if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
378                 sr_err("lap-c: %s: sdi was NULL", __func__);
379                 return SR_ERR; /* TODO: SR_ERR_ARG? */
380         }
381
382         /* TODO */
383         close_device(sdi);
384
385         return SR_OK;
386 }
387
388 static void hw_cleanup(void)
389 {
390         GSList *l;
391
392         /* Properly close all devices... */
393         for (l = device_instances; l; l = l->next)
394                 close_device((struct sr_device_instance *)l->data);
395
396         /* ...and free all their memory. */
397         for (l = device_instances; l; l = l->next)
398                 g_free(l->data);
399         g_slist_free(device_instances);
400         device_instances = NULL;
401
402         if (usb_context)
403                 libusb_exit(usb_context);
404         usb_context = NULL;
405 }
406
407 static void *hw_get_device_info(int device_index, int device_info_id)
408 {
409         struct sr_device_instance *sdi;
410         void *info = NULL;
411
412         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
413                 return NULL;
414
415         switch (device_info_id) {
416         case SR_DI_INSTANCE:
417                 info = sdi;
418                 break;
419         case SR_DI_NUM_PROBES:
420                 info = GINT_TO_POINTER(num_channels);
421                 break;
422         case SR_DI_SAMPLERATES:
423                 info = &samplerates;
424                 break;
425         case SR_DI_TRIGGER_TYPES:
426                 info = TRIGGER_TYPES;
427                 break;
428         case SR_DI_CUR_SAMPLERATE:
429                 info = &cur_samplerate;
430                 break;
431         }
432
433         return info;
434 }
435
436 static int hw_get_status(int device_index)
437 {
438         struct sr_device_instance *sdi;
439
440         sdi = sr_get_device_instance(device_instances, device_index);
441         if (sdi)
442                 return sdi->status;
443         else
444                 return SR_ST_NOT_FOUND;
445 }
446
447 static int *hw_get_capabilities(void)
448 {
449         return capabilities;
450 }
451
452 /* TODO: This will set the same samplerate for all devices. */
453 static int set_configuration_samplerate(uint64_t samplerate)
454 {
455         sr_info("%s(%" PRIu64 ")", __FUNCTION__, samplerate);
456         if (samplerate > SR_MHZ(1))
457                 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
458         else if (samplerate > SR_KHZ(1))
459                 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
460         else
461                 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
462
463         cur_samplerate = samplerate;
464
465         return SR_OK;
466 }
467
468 static int hw_set_configuration(int device_index, int capability, void *value)
469 {
470         struct sr_device_instance *sdi;
471         uint64_t *tmp_u64;
472
473         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
474                 return SR_ERR;
475
476         switch (capability) {
477         case SR_HWCAP_SAMPLERATE:
478                 tmp_u64 = value;
479                 return set_configuration_samplerate(*tmp_u64);
480         case SR_HWCAP_PROBECONFIG:
481                 return configure_probes((GSList *) value);
482         case SR_HWCAP_LIMIT_SAMPLES:
483                 tmp_u64 = value;
484                 limit_samples = *tmp_u64;
485                 return SR_OK;
486         default:
487                 return SR_ERR;
488         }
489 }
490
491 static int hw_start_acquisition(int device_index, gpointer session_device_id)
492 {
493         struct sr_device_instance *sdi;
494         struct sr_datafeed_packet packet;
495         struct sr_datafeed_header header;
496         int res;
497         unsigned int packet_num;
498         unsigned char *buf;
499
500         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
501                 return SR_ERR;
502
503         /* push configured settings to device */
504         analyzer_configure(sdi->usb->devhdl);
505
506         analyzer_start(sdi->usb->devhdl);
507         sr_info("Waiting for data");
508         analyzer_wait_data(sdi->usb->devhdl);
509
510         sr_info("Stop address    = 0x%x",
511                 analyzer_get_stop_address(sdi->usb->devhdl));
512         sr_info("Now address     = 0x%x",
513                 analyzer_get_now_address(sdi->usb->devhdl));
514         sr_info("Trigger address = 0x%x",
515                 analyzer_get_trigger_address(sdi->usb->devhdl));
516
517         packet.type = SR_DF_HEADER;
518         packet.length = sizeof(struct sr_datafeed_header);
519         packet.payload = (unsigned char *)&header;
520         header.feed_version = 1;
521         gettimeofday(&header.starttime, NULL);
522         header.samplerate = cur_samplerate;
523         header.protocol_id = SR_PROTO_RAW;
524         header.num_logic_probes = num_channels;
525         header.num_analog_probes = 0;
526         sr_session_bus(session_device_id, &packet);
527
528         if (!(buf = g_try_malloc(PACKET_SIZE))) {
529                 sr_err("zeroplus: %s: buf malloc failed", __func__);
530                 return SR_ERR_MALLOC;
531         }
532
533         analyzer_read_start(sdi->usb->devhdl);
534         /* Send the incoming transfer to the session bus. */
535         for (packet_num = 0; packet_num < (memory_size * 4 / PACKET_SIZE);
536              packet_num++) {
537                 res = analyzer_read_data(sdi->usb->devhdl, buf, PACKET_SIZE);
538 #if 0
539                 sr_info("Tried to read %llx bytes, actually read %x bytes",
540                         PACKET_SIZE, res);
541 #endif
542
543                 packet.type = SR_DF_LOGIC;
544                 packet.length = PACKET_SIZE;
545                 packet.unitsize = 4;
546                 packet.payload = buf;
547                 sr_session_bus(session_device_id, &packet);
548         }
549         analyzer_read_stop(sdi->usb->devhdl);
550         g_free(buf);
551
552         packet.type = SR_DF_END;
553         sr_session_bus(session_device_id, &packet);
554
555         return SR_OK;
556 }
557
558 /* This stops acquisition on ALL devices, ignoring device_index. */
559 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
560 {
561         struct sr_datafeed_packet packet;
562         struct sr_device_instance *sdi;
563
564         packet.type = SR_DF_END;
565         sr_session_bus(session_device_id, &packet);
566
567         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
568                 return; /* TODO: Cry? */
569
570         analyzer_reset(sdi->usb->devhdl);
571         /* TODO: Need to cancel and free any queued up transfers. */
572 }
573
574 struct sr_device_plugin zeroplus_logic_cube_plugin_info = {
575         .name = "zeroplus-logic-cube",
576         .longname = "Zeroplus Logic Cube LAP-C series",
577         .api_version = 1,
578         .init = hw_init,
579         .cleanup = hw_cleanup,
580         .opendev = hw_opendev,
581         .closedev = hw_closedev,
582         .get_device_info = hw_get_device_info,
583         .get_status = hw_get_status,
584         .get_capabilities = hw_get_capabilities,
585         .set_configuration = hw_set_configuration,
586         .start_acquisition = hw_start_acquisition,
587         .stop_acquisition = hw_stop_acquisition,
588 };