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