]>
Commit | Line | Data |
---|---|---|
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 "sigrok.h" | |
29 | #include "sigrok-internal.h" | |
30 | #include "analyzer.h" | |
31 | ||
32 | #define USB_VENDOR 0x0c12 | |
33 | #define USB_VENDOR_NAME "Zeroplus" | |
34 | #define USB_MODEL_NAME "Logic Cube" | |
35 | #define USB_MODEL_VERSION "" | |
36 | ||
37 | #define USB_INTERFACE 0 | |
38 | #define USB_CONFIGURATION 1 | |
39 | #define NUM_TRIGGER_STAGES 4 | |
40 | #define TRIGGER_TYPES "01" | |
41 | ||
42 | #define PACKET_SIZE 2048 /* ?? */ | |
43 | ||
44 | typedef struct { | |
45 | unsigned short pid; | |
46 | char model_name[64]; | |
47 | unsigned int channels; | |
48 | unsigned int sample_depth; /* In Ksamples/channel */ | |
49 | unsigned int max_sampling_freq; | |
50 | } model_t; | |
51 | ||
52 | /* | |
53 | * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the | |
54 | * same 128K sample depth. | |
55 | */ | |
56 | static model_t zeroplus_models[] = { | |
57 | {0x7009, "LAP-C(16064)", 16, 64, 100}, | |
58 | {0x700A, "LAP-C(16128)", 16, 128, 200}, | |
59 | {0x700B, "LAP-C(32128)", 32, 128, 200}, | |
60 | {0x700C, "LAP-C(321000)", 32, 1024, 200}, | |
61 | {0x700D, "LAP-C(322000)", 32, 2048, 200}, | |
62 | {0x700E, "LAP-C(16032)", 16, 32, 100}, | |
63 | {0x7016, "LAP-C(162000)", 16, 2048, 200}, | |
64 | }; | |
65 | ||
66 | static int capabilities[] = { | |
67 | SR_HWCAP_LOGIC_ANALYZER, | |
68 | SR_HWCAP_SAMPLERATE, | |
69 | SR_HWCAP_PROBECONFIG, | |
70 | SR_HWCAP_CAPTURE_RATIO, | |
71 | ||
72 | /* These are really implemented in the driver, not the hardware. */ | |
73 | SR_HWCAP_LIMIT_SAMPLES, | |
74 | 0, | |
75 | }; | |
76 | ||
77 | static const char *probe_names[] = { | |
78 | "0", | |
79 | "1", | |
80 | "2", | |
81 | "3", | |
82 | "4", | |
83 | "5", | |
84 | "6", | |
85 | "7", | |
86 | "8", | |
87 | "9", | |
88 | "10", | |
89 | "11", | |
90 | "12", | |
91 | "13", | |
92 | "14", | |
93 | "15", | |
94 | "16", | |
95 | "17", | |
96 | "18", | |
97 | "19", | |
98 | "20", | |
99 | "21", | |
100 | "22", | |
101 | "23", | |
102 | "24", | |
103 | "25", | |
104 | "26", | |
105 | "27", | |
106 | "28", | |
107 | "29", | |
108 | "30", | |
109 | "31", | |
110 | NULL, | |
111 | }; | |
112 | ||
113 | /* List of struct sr_dev_inst, maintained by opendev()/closedev(). */ | |
114 | static GSList *dev_insts = NULL; | |
115 | ||
116 | static libusb_context *usb_context = NULL; | |
117 | ||
118 | /* | |
119 | * The hardware supports more samplerates than these, but these are the | |
120 | * options hardcoded into the vendor's Windows GUI. | |
121 | */ | |
122 | ||
123 | /* | |
124 | * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up | |
125 | * that high. | |
126 | */ | |
127 | static uint64_t supported_samplerates[] = { | |
128 | SR_HZ(100), | |
129 | SR_HZ(500), | |
130 | SR_KHZ(1), | |
131 | SR_KHZ(5), | |
132 | SR_KHZ(25), | |
133 | SR_KHZ(50), | |
134 | SR_KHZ(100), | |
135 | SR_KHZ(200), | |
136 | SR_KHZ(400), | |
137 | SR_KHZ(800), | |
138 | SR_MHZ(1), | |
139 | SR_MHZ(10), | |
140 | SR_MHZ(25), | |
141 | SR_MHZ(50), | |
142 | SR_MHZ(80), | |
143 | SR_MHZ(100), | |
144 | SR_MHZ(150), | |
145 | SR_MHZ(200), | |
146 | 0, | |
147 | }; | |
148 | ||
149 | static struct sr_samplerates samplerates = { | |
150 | SR_HZ(0), | |
151 | SR_HZ(0), | |
152 | SR_HZ(0), | |
153 | supported_samplerates, | |
154 | }; | |
155 | ||
156 | struct zp { | |
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 | struct sr_usb_dev_inst *usb; | |
167 | }; | |
168 | ||
169 | static int hw_set_configuration(int dev_index, int capability, void *value); | |
170 | ||
171 | static unsigned int get_memory_size(int type) | |
172 | { | |
173 | if (type == MEMORY_SIZE_8K) | |
174 | return 8 * 1024; | |
175 | else if (type == MEMORY_SIZE_64K) | |
176 | return 64 * 1024; | |
177 | else if (type == MEMORY_SIZE_128K) | |
178 | return 128 * 1024; | |
179 | else if (type == MEMORY_SIZE_512K) | |
180 | return 512 * 1024; | |
181 | else | |
182 | return 0; | |
183 | } | |
184 | ||
185 | static int opendev4(struct sr_dev_inst **sdi, libusb_device *dev, | |
186 | struct libusb_device_descriptor *des) | |
187 | { | |
188 | struct zp *zp; | |
189 | unsigned int i; | |
190 | int err; | |
191 | ||
192 | /* Note: sdi is non-NULL, the caller already checked this. */ | |
193 | ||
194 | if (!(zp = (*sdi)->priv)) { | |
195 | sr_err("zp: %s: (*sdi)->priv was NULL", __func__); | |
196 | return -1; | |
197 | } | |
198 | ||
199 | if ((err = libusb_get_device_descriptor(dev, des))) { | |
200 | sr_err("zp: failed to get device descriptor: %d", err); | |
201 | return -1; | |
202 | } | |
203 | ||
204 | if (des->idVendor != USB_VENDOR) | |
205 | return 0; | |
206 | ||
207 | if (libusb_get_bus_number(dev) == zp->usb->bus | |
208 | && libusb_get_device_address(dev) == zp->usb->address) { | |
209 | ||
210 | for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) { | |
211 | if (!(des->idProduct == zeroplus_models[i].pid)) | |
212 | continue; | |
213 | ||
214 | sr_info("zp: Found ZeroPlus device 0x%04x (%s)", | |
215 | des->idProduct, zeroplus_models[i].model_name); | |
216 | zp->num_channels = zeroplus_models[i].channels; | |
217 | zp->memory_size = zeroplus_models[i].sample_depth * 1024; | |
218 | break; | |
219 | } | |
220 | ||
221 | if (zp->num_channels == 0) { | |
222 | sr_err("zp: Unknown ZeroPlus device 0x%04x", | |
223 | des->idProduct); | |
224 | return -2; | |
225 | } | |
226 | ||
227 | /* Found it. */ | |
228 | if (!(err = libusb_open(dev, &(zp->usb->devhdl)))) { | |
229 | (*sdi)->status = SR_ST_ACTIVE; | |
230 | sr_info("zp: opened device %d on %d.%d interface %d", | |
231 | (*sdi)->index, zp->usb->bus, | |
232 | zp->usb->address, USB_INTERFACE); | |
233 | } else { | |
234 | sr_err("zp: failed to open device: %d", err); | |
235 | *sdi = NULL; | |
236 | } | |
237 | } | |
238 | ||
239 | return 0; | |
240 | } | |
241 | ||
242 | static struct sr_dev_inst *zp_open_dev(int dev_index) | |
243 | { | |
244 | struct sr_dev_inst *sdi; | |
245 | libusb_device **devlist; | |
246 | struct libusb_device_descriptor des; | |
247 | int err, i; | |
248 | ||
249 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
250 | return NULL; | |
251 | ||
252 | libusb_get_device_list(usb_context, &devlist); | |
253 | if (sdi->status == SR_ST_INACTIVE) { | |
254 | /* Find the device by vendor, product, bus and address. */ | |
255 | libusb_get_device_list(usb_context, &devlist); | |
256 | for (i = 0; devlist[i]; i++) { | |
257 | /* TODO: Error handling. */ | |
258 | err = opendev4(&sdi, devlist[i], &des); | |
259 | } | |
260 | } else { | |
261 | /* Status must be SR_ST_ACTIVE, i.e. already in use... */ | |
262 | sdi = NULL; | |
263 | } | |
264 | libusb_free_device_list(devlist, 1); | |
265 | ||
266 | if (sdi && sdi->status != SR_ST_ACTIVE) | |
267 | sdi = NULL; | |
268 | ||
269 | return sdi; | |
270 | } | |
271 | ||
272 | static void close_dev(struct sr_dev_inst *sdi) | |
273 | { | |
274 | struct zp *zp; | |
275 | ||
276 | if (!(zp = sdi->priv)) { | |
277 | sr_err("zp: %s: sdi->priv was NULL", __func__); | |
278 | return; /* FIXME */ | |
279 | } | |
280 | ||
281 | if (!zp->usb->devhdl) | |
282 | return; | |
283 | ||
284 | sr_info("zp: closing device %d on %d.%d interface %d", sdi->index, | |
285 | zp->usb->bus, zp->usb->address, USB_INTERFACE); | |
286 | libusb_release_interface(zp->usb->devhdl, USB_INTERFACE); | |
287 | libusb_reset_device(zp->usb->devhdl); | |
288 | libusb_close(zp->usb->devhdl); | |
289 | zp->usb->devhdl = NULL; | |
290 | /* TODO: Call libusb_exit() here or only in hw_cleanup()? */ | |
291 | sdi->status = SR_ST_INACTIVE; | |
292 | } | |
293 | ||
294 | static int configure_probes(struct sr_dev_inst *sdi, GSList *probes) | |
295 | { | |
296 | struct zp *zp; | |
297 | struct sr_probe *probe; | |
298 | GSList *l; | |
299 | int probe_bit, stage, i; | |
300 | char *tc; | |
301 | ||
302 | /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */ | |
303 | zp = sdi->priv; | |
304 | ||
305 | zp->probe_mask = 0; | |
306 | for (i = 0; i < NUM_TRIGGER_STAGES; i++) { | |
307 | zp->trigger_mask[i] = 0; | |
308 | zp->trigger_value[i] = 0; | |
309 | } | |
310 | ||
311 | stage = -1; | |
312 | for (l = probes; l; l = l->next) { | |
313 | probe = (struct sr_probe *)l->data; | |
314 | if (probe->enabled == FALSE) | |
315 | continue; | |
316 | probe_bit = 1 << (probe->index - 1); | |
317 | zp->probe_mask |= probe_bit; | |
318 | ||
319 | if (probe->trigger) { | |
320 | stage = 0; | |
321 | for (tc = probe->trigger; *tc; tc++) { | |
322 | zp->trigger_mask[stage] |= probe_bit; | |
323 | if (*tc == '1') | |
324 | zp->trigger_value[stage] |= probe_bit; | |
325 | stage++; | |
326 | if (stage > NUM_TRIGGER_STAGES) | |
327 | return SR_ERR; | |
328 | } | |
329 | } | |
330 | } | |
331 | ||
332 | return SR_OK; | |
333 | } | |
334 | ||
335 | /* | |
336 | * API callbacks | |
337 | */ | |
338 | ||
339 | static int hw_init(const char *devinfo) | |
340 | { | |
341 | struct sr_dev_inst *sdi; | |
342 | struct libusb_device_descriptor des; | |
343 | libusb_device **devlist; | |
344 | int err, devcnt, i; | |
345 | struct zp *zp; | |
346 | ||
347 | /* Avoid compiler warnings. */ | |
348 | (void)devinfo; | |
349 | ||
350 | /* Allocate memory for our private driver context. */ | |
351 | if (!(zp = g_try_malloc(sizeof(struct zp)))) { | |
352 | sr_err("zp: %s: struct zp malloc failed", __func__); | |
353 | return 0; | |
354 | } | |
355 | ||
356 | /* Set some sane defaults. */ | |
357 | zp->cur_samplerate = 0; | |
358 | zp->limit_samples = 0; | |
359 | zp->num_channels = 32; /* TODO: This isn't initialized before it's needed :( */ | |
360 | zp->memory_size = 0; | |
361 | zp->probe_mask = 0; | |
362 | memset(zp->trigger_mask, 0, NUM_TRIGGER_STAGES); | |
363 | memset(zp->trigger_value, 0, NUM_TRIGGER_STAGES); | |
364 | // memset(zp->trigger_buffer, 0, NUM_TRIGGER_STAGES); | |
365 | ||
366 | if (libusb_init(&usb_context) != 0) { | |
367 | sr_err("zp: Failed to initialize USB."); | |
368 | return 0; | |
369 | } | |
370 | ||
371 | /* Find all ZeroPlus analyzers and add them to device list. */ | |
372 | devcnt = 0; | |
373 | libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */ | |
374 | ||
375 | for (i = 0; devlist[i]; i++) { | |
376 | err = libusb_get_device_descriptor(devlist[i], &des); | |
377 | if (err != 0) { | |
378 | sr_err("zp: failed to get device descriptor: %d", err); | |
379 | continue; | |
380 | } | |
381 | ||
382 | if (des.idVendor == USB_VENDOR) { | |
383 | /* | |
384 | * Definitely a Zeroplus. | |
385 | * TODO: Any way to detect specific model/version in | |
386 | * the zeroplus range? | |
387 | */ | |
388 | /* Register the device with libsigrok. */ | |
389 | if (!(sdi = sr_dev_inst_new(devcnt, | |
390 | SR_ST_INACTIVE, USB_VENDOR_NAME, | |
391 | USB_MODEL_NAME, USB_MODEL_VERSION))) { | |
392 | sr_err("zp: %s: sr_dev_inst_new failed", | |
393 | __func__); | |
394 | return 0; | |
395 | } | |
396 | ||
397 | sdi->priv = zp; | |
398 | ||
399 | dev_insts = | |
400 | g_slist_append(dev_insts, sdi); | |
401 | zp->usb = sr_usb_dev_inst_new( | |
402 | libusb_get_bus_number(devlist[i]), | |
403 | libusb_get_device_address(devlist[i]), NULL); | |
404 | devcnt++; | |
405 | } | |
406 | } | |
407 | libusb_free_device_list(devlist, 1); | |
408 | ||
409 | return devcnt; | |
410 | } | |
411 | ||
412 | static int hw_opendev(int dev_index) | |
413 | { | |
414 | struct sr_dev_inst *sdi; | |
415 | struct zp *zp; | |
416 | int err; | |
417 | ||
418 | if (!(sdi = zp_open_dev(dev_index))) { | |
419 | sr_err("zp: unable to open device"); | |
420 | return SR_ERR; | |
421 | } | |
422 | ||
423 | /* TODO: Note: sdi is retrieved in zp_open_dev(). */ | |
424 | ||
425 | if (!(zp = sdi->priv)) { | |
426 | sr_err("zp: %s: sdi->priv was NULL", __func__); | |
427 | return SR_ERR_ARG; | |
428 | } | |
429 | ||
430 | err = libusb_set_configuration(zp->usb->devhdl, USB_CONFIGURATION); | |
431 | if (err < 0) { | |
432 | sr_err("zp: Unable to set USB configuration %d: %d", | |
433 | USB_CONFIGURATION, err); | |
434 | return SR_ERR; | |
435 | } | |
436 | ||
437 | err = libusb_claim_interface(zp->usb->devhdl, USB_INTERFACE); | |
438 | if (err != 0) { | |
439 | sr_err("zp: Unable to claim interface: %d", err); | |
440 | return SR_ERR; | |
441 | } | |
442 | ||
443 | analyzer_reset(zp->usb->devhdl); | |
444 | analyzer_initialize(zp->usb->devhdl); | |
445 | ||
446 | analyzer_set_memory_size(MEMORY_SIZE_512K); | |
447 | // analyzer_set_freq(g_freq, g_freq_scale); | |
448 | analyzer_set_trigger_count(1); | |
449 | // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger) | |
450 | // * get_memory_size(g_memory_size)) / 100) >> 2); | |
451 | analyzer_set_ramsize_trigger_address( | |
452 | (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2); | |
453 | ||
454 | #if 0 | |
455 | if (g_double_mode == 1) | |
456 | analyzer_set_compression(COMPRESSION_DOUBLE); | |
457 | else if (g_compression == 1) | |
458 | analyzer_set_compression(COMPRESSION_ENABLE); | |
459 | else | |
460 | #endif | |
461 | analyzer_set_compression(COMPRESSION_NONE); | |
462 | ||
463 | if (zp->cur_samplerate == 0) { | |
464 | /* Samplerate hasn't been set. Default to the slowest one. */ | |
465 | if (hw_set_configuration(dev_index, SR_HWCAP_SAMPLERATE, | |
466 | &samplerates.list[0]) == SR_ERR) | |
467 | return SR_ERR; | |
468 | } | |
469 | ||
470 | return SR_OK; | |
471 | } | |
472 | ||
473 | static int hw_closedev(int dev_index) | |
474 | { | |
475 | struct sr_dev_inst *sdi; | |
476 | ||
477 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
478 | sr_err("zp: %s: sdi was NULL", __func__); | |
479 | return SR_ERR; /* TODO: SR_ERR_ARG? */ | |
480 | } | |
481 | ||
482 | /* TODO */ | |
483 | close_dev(sdi); | |
484 | ||
485 | return SR_OK; | |
486 | } | |
487 | ||
488 | static int hw_cleanup(void) | |
489 | { | |
490 | GSList *l; | |
491 | struct sr_dev_inst *sdi; | |
492 | ||
493 | for (l = dev_insts; l; l = l->next) { | |
494 | sdi = l->data; | |
495 | /* Properly close all devices... */ | |
496 | close_dev(sdi); | |
497 | /* ...and free all their memory. */ | |
498 | sr_dev_inst_free(sdi); | |
499 | } | |
500 | g_slist_free(dev_insts); | |
501 | dev_insts = NULL; | |
502 | ||
503 | if (usb_context) | |
504 | libusb_exit(usb_context); | |
505 | usb_context = NULL; | |
506 | ||
507 | return SR_OK; | |
508 | } | |
509 | ||
510 | static void *hw_get_dev_info(int dev_index, int dev_info_id) | |
511 | { | |
512 | struct sr_dev_inst *sdi; | |
513 | struct zp *zp; | |
514 | void *info; | |
515 | ||
516 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
517 | sr_err("zp: %s: sdi was NULL", __func__); | |
518 | return NULL; | |
519 | } | |
520 | ||
521 | if (!(zp = sdi->priv)) { | |
522 | sr_err("zp: %s: sdi->priv was NULL", __func__); | |
523 | return NULL; | |
524 | } | |
525 | ||
526 | switch (dev_info_id) { | |
527 | case SR_DI_INSTANCE: | |
528 | info = sdi; | |
529 | break; | |
530 | case SR_DI_NUM_PROBES: | |
531 | info = GINT_TO_POINTER(zp->num_channels); | |
532 | break; | |
533 | case SR_DI_PROBE_NAMES: | |
534 | info = probe_names; | |
535 | break; | |
536 | case SR_DI_SAMPLERATES: | |
537 | info = &samplerates; | |
538 | break; | |
539 | case SR_DI_TRIGGER_TYPES: | |
540 | info = TRIGGER_TYPES; | |
541 | break; | |
542 | case SR_DI_CUR_SAMPLERATE: | |
543 | info = &zp->cur_samplerate; | |
544 | break; | |
545 | default: | |
546 | /* Unknown device info ID, return NULL. */ | |
547 | sr_err("zp: %s: Unknown device info ID", __func__); | |
548 | info = NULL; | |
549 | break; | |
550 | } | |
551 | ||
552 | return info; | |
553 | } | |
554 | ||
555 | static int hw_get_status(int dev_index) | |
556 | { | |
557 | struct sr_dev_inst *sdi; | |
558 | ||
559 | sdi = sr_dev_inst_get(dev_insts, dev_index); | |
560 | if (sdi) | |
561 | return sdi->status; | |
562 | else | |
563 | return SR_ST_NOT_FOUND; | |
564 | } | |
565 | ||
566 | static int *hw_get_capabilities(void) | |
567 | { | |
568 | return capabilities; | |
569 | } | |
570 | ||
571 | static int set_configuration_samplerate(struct sr_dev_inst *sdi, | |
572 | uint64_t samplerate) | |
573 | { | |
574 | struct zp *zp; | |
575 | ||
576 | if (!sdi) { | |
577 | sr_err("zp: %s: sdi was NULL", __func__); | |
578 | return SR_ERR_ARG; | |
579 | } | |
580 | ||
581 | if (!(zp = sdi->priv)) { | |
582 | sr_err("zp: %s: sdi->priv was NULL", __func__); | |
583 | return SR_ERR_ARG; | |
584 | } | |
585 | ||
586 | sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate); | |
587 | ||
588 | if (samplerate > SR_MHZ(1)) | |
589 | analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ); | |
590 | else if (samplerate > SR_KHZ(1)) | |
591 | analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ); | |
592 | else | |
593 | analyzer_set_freq(samplerate, FREQ_SCALE_HZ); | |
594 | ||
595 | zp->cur_samplerate = samplerate; | |
596 | ||
597 | return SR_OK; | |
598 | } | |
599 | ||
600 | static int hw_set_configuration(int dev_index, int capability, void *value) | |
601 | { | |
602 | struct sr_dev_inst *sdi; | |
603 | uint64_t *tmp_u64; | |
604 | struct zp *zp; | |
605 | ||
606 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
607 | sr_err("zp: %s: sdi was NULL", __func__); | |
608 | return SR_ERR; | |
609 | } | |
610 | ||
611 | if (!(zp = sdi->priv)) { | |
612 | sr_err("zp: %s: sdi->priv was NULL", __func__); | |
613 | return SR_ERR_ARG; | |
614 | } | |
615 | ||
616 | switch (capability) { | |
617 | case SR_HWCAP_SAMPLERATE: | |
618 | tmp_u64 = value; | |
619 | return set_configuration_samplerate(sdi, *tmp_u64); | |
620 | case SR_HWCAP_PROBECONFIG: | |
621 | return configure_probes(sdi, (GSList *)value); | |
622 | case SR_HWCAP_LIMIT_SAMPLES: | |
623 | tmp_u64 = value; | |
624 | zp->limit_samples = *tmp_u64; | |
625 | return SR_OK; | |
626 | default: | |
627 | return SR_ERR; | |
628 | } | |
629 | } | |
630 | ||
631 | static int hw_start_acquisition(int dev_index, gpointer session_data) | |
632 | { | |
633 | struct sr_dev_inst *sdi; | |
634 | struct sr_datafeed_packet packet; | |
635 | struct sr_datafeed_logic logic; | |
636 | struct sr_datafeed_header header; | |
637 | uint64_t samples_read; | |
638 | int res; | |
639 | unsigned int packet_num; | |
640 | unsigned char *buf; | |
641 | struct zp *zp; | |
642 | ||
643 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
644 | sr_err("zp: %s: sdi was NULL", __func__); | |
645 | return SR_ERR; | |
646 | } | |
647 | ||
648 | if (!(zp = sdi->priv)) { | |
649 | sr_err("zp: %s: sdi->priv was NULL", __func__); | |
650 | return SR_ERR_ARG; | |
651 | } | |
652 | ||
653 | /* push configured settings to device */ | |
654 | analyzer_configure(zp->usb->devhdl); | |
655 | ||
656 | analyzer_start(zp->usb->devhdl); | |
657 | sr_info("zp: Waiting for data"); | |
658 | analyzer_wait_data(zp->usb->devhdl); | |
659 | ||
660 | sr_info("zp: Stop address = 0x%x", | |
661 | analyzer_get_stop_address(zp->usb->devhdl)); | |
662 | sr_info("zp: Now address = 0x%x", | |
663 | analyzer_get_now_address(zp->usb->devhdl)); | |
664 | sr_info("zp: Trigger address = 0x%x", | |
665 | analyzer_get_trigger_address(zp->usb->devhdl)); | |
666 | ||
667 | packet.type = SR_DF_HEADER; | |
668 | packet.payload = &header; | |
669 | header.feed_version = 1; | |
670 | gettimeofday(&header.starttime, NULL); | |
671 | header.samplerate = zp->cur_samplerate; | |
672 | header.num_logic_probes = zp->num_channels; | |
673 | sr_session_bus(session_data, &packet); | |
674 | ||
675 | if (!(buf = g_try_malloc(PACKET_SIZE))) { | |
676 | sr_err("zp: %s: buf malloc failed", __func__); | |
677 | return SR_ERR_MALLOC; | |
678 | } | |
679 | ||
680 | samples_read = 0; | |
681 | analyzer_read_start(zp->usb->devhdl); | |
682 | /* Send the incoming transfer to the session bus. */ | |
683 | for (packet_num = 0; packet_num < (zp->memory_size * 4 / PACKET_SIZE); | |
684 | packet_num++) { | |
685 | res = analyzer_read_data(zp->usb->devhdl, buf, PACKET_SIZE); | |
686 | sr_info("zp: Tried to read %llx bytes, actually read %x bytes", | |
687 | PACKET_SIZE, res); | |
688 | ||
689 | packet.type = SR_DF_LOGIC; | |
690 | packet.payload = &logic; | |
691 | logic.length = PACKET_SIZE; | |
692 | logic.unitsize = 4; | |
693 | logic.data = buf; | |
694 | sr_session_bus(session_data, &packet); | |
695 | samples_read += res / 4; | |
696 | } | |
697 | analyzer_read_stop(zp->usb->devhdl); | |
698 | g_free(buf); | |
699 | ||
700 | packet.type = SR_DF_END; | |
701 | sr_session_bus(session_data, &packet); | |
702 | ||
703 | return SR_OK; | |
704 | } | |
705 | ||
706 | /* This stops acquisition on ALL devices, ignoring dev_index. */ | |
707 | static int hw_stop_acquisition(int dev_index, gpointer session_dev_id) | |
708 | { | |
709 | struct sr_datafeed_packet packet; | |
710 | struct sr_dev_inst *sdi; | |
711 | struct zp *zp; | |
712 | ||
713 | packet.type = SR_DF_END; | |
714 | sr_session_bus(session_dev_id, &packet); | |
715 | ||
716 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
717 | sr_err("zp: %s: sdi was NULL", __func__); | |
718 | return SR_ERR_BUG; | |
719 | } | |
720 | ||
721 | if (!(zp = sdi->priv)) { | |
722 | sr_err("zp: %s: sdi->priv was NULL", __func__); | |
723 | return SR_ERR_BUG; | |
724 | } | |
725 | ||
726 | analyzer_reset(zp->usb->devhdl); | |
727 | /* TODO: Need to cancel and free any queued up transfers. */ | |
728 | ||
729 | return SR_OK; | |
730 | } | |
731 | ||
732 | SR_PRIV struct sr_dev_plugin zeroplus_logic_cube_plugin_info = { | |
733 | .name = "zeroplus-logic-cube", | |
734 | .longname = "Zeroplus Logic Cube LAP-C series", | |
735 | .api_version = 1, | |
736 | .init = hw_init, | |
737 | .cleanup = hw_cleanup, | |
738 | .opendev = hw_opendev, | |
739 | .closedev = hw_closedev, | |
740 | .get_dev_info = hw_get_dev_info, | |
741 | .get_status = hw_get_status, | |
742 | .get_capabilities = hw_get_capabilities, | |
743 | .set_configuration = hw_set_configuration, | |
744 | .start_acquisition = hw_start_acquisition, | |
745 | .stop_acquisition = hw_stop_acquisition, | |
746 | }; |