]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
a1bb33af UH |
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> | |
c31e9ef4 | 22 | #include <string.h> |
a1bb33af UH |
23 | #include <sys/time.h> |
24 | #include <inttypes.h> | |
25 | #include <glib.h> | |
26 | #include <libusb.h> | |
45c59c8b BV |
27 | #include "libsigrok.h" |
28 | #include "libsigrok-internal.h" | |
a1bb33af UH |
29 | #include "analyzer.h" |
30 | ||
fed16f06 | 31 | #define USB_VENDOR 0x0c12 |
8fdecced UH |
32 | |
33 | #define VENDOR_NAME "ZEROPLUS" | |
34 | #define MODEL_NAME "Logic Cube LAP-C" | |
35 | #define MODEL_VERSION NULL | |
a1bb33af | 36 | |
6752905e | 37 | #define NUM_PROBES 16 |
a1bb33af UH |
38 | #define USB_INTERFACE 0 |
39 | #define USB_CONFIGURATION 1 | |
40 | #define NUM_TRIGGER_STAGES 4 | |
c50277a6 | 41 | #define TRIGGER_TYPE "01" |
a1bb33af | 42 | |
fed16f06 | 43 | #define PACKET_SIZE 2048 /* ?? */ |
a1bb33af | 44 | |
0ab0cb94 TY |
45 | //#define ZP_EXPERIMENTAL |
46 | ||
a1bb33af | 47 | typedef struct { |
428edbe1 | 48 | unsigned short vid; |
a1bb33af | 49 | unsigned short pid; |
428edbe1 | 50 | char *model_name; |
a1bb33af | 51 | unsigned int channels; |
fed16f06 | 52 | unsigned int sample_depth; /* In Ksamples/channel */ |
a1bb33af UH |
53 | unsigned int max_sampling_freq; |
54 | } model_t; | |
55 | ||
fed16f06 UH |
56 | /* |
57 | * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the | |
58 | * same 128K sample depth. | |
59 | */ | |
29cbfeaf | 60 | static model_t zeroplus_models[] = { |
428edbe1 BV |
61 | {0x0c12, 0x7009, "LAP-C(16064)", 16, 64, 100}, |
62 | {0x0c12, 0x700A, "LAP-C(16128)", 16, 128, 200}, | |
3f848bb7 | 63 | /* TODO: we don't know anything about these |
428edbe1 BV |
64 | {0x0c12, 0x700B, "LAP-C(32128)", 32, 128, 200}, |
65 | {0x0c12, 0x700C, "LAP-C(321000)", 32, 1024, 200}, | |
66 | {0x0c12, 0x700D, "LAP-C(322000)", 32, 2048, 200}, | |
3f848bb7 | 67 | */ |
428edbe1 BV |
68 | {0x0c12, 0x700E, "LAP-C(16032)", 16, 32, 100}, |
69 | {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200}, | |
70 | { 0, 0, 0, 0, 0, 0 } | |
a1bb33af UH |
71 | }; |
72 | ||
915f7cc8 | 73 | static const int hwcaps[] = { |
1953564a BV |
74 | SR_CONF_LOGIC_ANALYZER, |
75 | SR_CONF_SAMPLERATE, | |
76 | SR_CONF_CAPTURE_RATIO, | |
a1bb33af | 77 | |
fed16f06 | 78 | /* These are really implemented in the driver, not the hardware. */ |
1953564a | 79 | SR_CONF_LIMIT_SAMPLES, |
fed16f06 | 80 | 0, |
a1bb33af UH |
81 | }; |
82 | ||
d261dbbf UH |
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 | */ | |
6752905e | 87 | static const char *probe_names[NUM_PROBES + 1] = { |
78693401 UH |
88 | "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", |
89 | "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", | |
464d12c7 KS |
90 | NULL, |
91 | }; | |
92 | ||
e7eb703f | 93 | /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */ |
32756547 | 94 | SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info; |
a873c594 | 95 | static struct sr_dev_driver *di = &zeroplus_logic_cube_driver_info; |
a1bb33af | 96 | |
fed16f06 UH |
97 | /* |
98 | * The hardware supports more samplerates than these, but these are the | |
99 | * options hardcoded into the vendor's Windows GUI. | |
100 | */ | |
a1bb33af | 101 | |
fed16f06 UH |
102 | /* |
103 | * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up | |
104 | * that high. | |
105 | */ | |
a533743d | 106 | static const uint64_t supported_samplerates[] = { |
c9140419 UH |
107 | SR_HZ(100), |
108 | SR_HZ(500), | |
59df0c77 UH |
109 | SR_KHZ(1), |
110 | SR_KHZ(5), | |
111 | SR_KHZ(25), | |
112 | SR_KHZ(50), | |
113 | SR_KHZ(100), | |
114 | SR_KHZ(200), | |
115 | SR_KHZ(400), | |
116 | SR_KHZ(800), | |
117 | SR_MHZ(1), | |
118 | SR_MHZ(10), | |
119 | SR_MHZ(25), | |
120 | SR_MHZ(50), | |
121 | SR_MHZ(80), | |
122 | SR_MHZ(100), | |
123 | SR_MHZ(150), | |
124 | SR_MHZ(200), | |
fed16f06 | 125 | 0, |
a1bb33af UH |
126 | }; |
127 | ||
a533743d | 128 | static const struct sr_samplerates samplerates = { |
590b9f9a UH |
129 | 0, |
130 | 0, | |
131 | 0, | |
fed16f06 | 132 | supported_samplerates, |
a1bb33af UH |
133 | }; |
134 | ||
ea9cfed7 | 135 | /* Private, per-device-instance driver context. */ |
310e9e9b | 136 | struct dev_context { |
bf43ea23 | 137 | uint64_t cur_samplerate; |
0ab0cb94 | 138 | uint64_t max_samplerate; |
bf43ea23 UH |
139 | uint64_t limit_samples; |
140 | int num_channels; /* TODO: This isn't initialized before it's needed :( */ | |
0ab0cb94 TY |
141 | int memory_size; |
142 | unsigned int max_memory_size; | |
143 | //uint8_t probe_mask; | |
144 | //uint8_t trigger_mask[NUM_TRIGGER_STAGES]; | |
145 | //uint8_t trigger_value[NUM_TRIGGER_STAGES]; | |
bf43ea23 | 146 | // uint8_t trigger_buffer[NUM_TRIGGER_STAGES]; |
0ab0cb94 TY |
147 | int trigger; |
148 | unsigned int capture_ratio; | |
69890f73 | 149 | |
7da6f9d5 | 150 | /* TODO: this belongs in the device instance */ |
d68e2d1a | 151 | struct sr_usb_dev_inst *usb; |
bf43ea23 | 152 | }; |
a1bb33af | 153 | |
25a0f108 | 154 | static int hw_dev_close(struct sr_dev_inst *sdi); |
a1bb33af UH |
155 | |
156 | static unsigned int get_memory_size(int type) | |
157 | { | |
fed16f06 UH |
158 | if (type == MEMORY_SIZE_8K) |
159 | return 8 * 1024; | |
160 | else if (type == MEMORY_SIZE_64K) | |
161 | return 64 * 1024; | |
162 | else if (type == MEMORY_SIZE_128K) | |
163 | return 128 * 1024; | |
164 | else if (type == MEMORY_SIZE_512K) | |
165 | return 512 * 1024; | |
166 | else | |
167 | return 0; | |
a1bb33af UH |
168 | } |
169 | ||
0ab0cb94 | 170 | #if 0 |
014359e3 | 171 | static int configure_probes(const struct sr_dev_inst *sdi) |
a1bb33af | 172 | { |
310e9e9b | 173 | struct dev_context *devc; |
1b79df2f JH |
174 | const struct sr_probe *probe; |
175 | const GSList *l; | |
a1bb33af UH |
176 | int probe_bit, stage, i; |
177 | char *tc; | |
178 | ||
bf43ea23 | 179 | /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */ |
310e9e9b | 180 | devc = sdi->priv; |
bf43ea23 | 181 | |
310e9e9b | 182 | devc->probe_mask = 0; |
fed16f06 | 183 | for (i = 0; i < NUM_TRIGGER_STAGES; i++) { |
310e9e9b BV |
184 | devc->trigger_mask[i] = 0; |
185 | devc->trigger_value[i] = 0; | |
a1bb33af UH |
186 | } |
187 | ||
188 | stage = -1; | |
014359e3 | 189 | for (l = sdi->probes; l; l = l->next) { |
1afe8989 | 190 | probe = (struct sr_probe *)l->data; |
fed16f06 | 191 | if (probe->enabled == FALSE) |
a1bb33af | 192 | continue; |
b35c8293 | 193 | probe_bit = 1 << (probe->index); |
310e9e9b | 194 | devc->probe_mask |= probe_bit; |
fed16f06 UH |
195 | |
196 | if (probe->trigger) { | |
a1bb33af | 197 | stage = 0; |
fed16f06 | 198 | for (tc = probe->trigger; *tc; tc++) { |
310e9e9b | 199 | devc->trigger_mask[stage] |= probe_bit; |
fed16f06 | 200 | if (*tc == '1') |
310e9e9b | 201 | devc->trigger_value[stage] |= probe_bit; |
a1bb33af | 202 | stage++; |
fed16f06 | 203 | if (stage > NUM_TRIGGER_STAGES) |
e46b8fb1 | 204 | return SR_ERR; |
a1bb33af UH |
205 | } |
206 | } | |
207 | } | |
208 | ||
e46b8fb1 | 209 | return SR_OK; |
a1bb33af | 210 | } |
0ab0cb94 TY |
211 | #endif |
212 | ||
213 | static int configure_probes(const struct sr_dev_inst *sdi) | |
214 | { | |
215 | struct dev_context *devc; | |
216 | const GSList *l; | |
217 | const struct sr_probe *probe; | |
218 | char *tc; | |
219 | int type; | |
220 | ||
221 | /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */ | |
222 | devc = sdi->priv; | |
223 | ||
224 | for (l = sdi->probes; l; l = l->next) { | |
225 | probe = (struct sr_probe *)l->data; | |
226 | if (probe->enabled == FALSE) | |
227 | continue; | |
228 | ||
229 | if ((tc = probe->trigger)) { | |
230 | switch (*tc) { | |
231 | case '1': | |
232 | type = TRIGGER_HIGH; | |
233 | break; | |
234 | case '0': | |
235 | type = TRIGGER_LOW; | |
236 | break; | |
237 | #if 0 | |
238 | case 'r': | |
239 | type = TRIGGER_POSEDGE; | |
240 | break; | |
241 | case 'f': | |
242 | type = TRIGGER_NEGEDGE; | |
243 | break; | |
244 | case 'c': | |
245 | type = TRIGGER_ANYEDGE; | |
246 | break; | |
247 | #endif | |
248 | default: | |
249 | return SR_ERR; | |
250 | } | |
251 | analyzer_add_trigger(probe->index, type); | |
252 | devc->trigger = 1; | |
253 | } | |
254 | } | |
255 | ||
256 | return SR_OK; | |
257 | } | |
a1bb33af | 258 | |
811deee4 | 259 | static int clear_instances(void) |
7da6f9d5 BV |
260 | { |
261 | GSList *l; | |
262 | struct sr_dev_inst *sdi; | |
310e9e9b | 263 | struct drv_context *drvc; |
fabe59b3 | 264 | struct dev_context *devc; |
7da6f9d5 | 265 | |
a873c594 | 266 | drvc = di->priv; |
310e9e9b | 267 | for (l = drvc->instances; l; l = l->next) { |
7da6f9d5 | 268 | sdi = l->data; |
fabe59b3 BV |
269 | if (!(devc = sdi->priv)) { |
270 | /* Log error, but continue cleaning up the rest. */ | |
271 | sr_err("zeroplus: %s: sdi->priv was NULL, continuing", __func__); | |
272 | continue; | |
273 | } | |
274 | sr_usb_dev_inst_free(devc->usb); | |
7da6f9d5 | 275 | /* Properly close all devices... */ |
25a0f108 | 276 | hw_dev_close(sdi); |
7da6f9d5 BV |
277 | /* ...and free all their memory. */ |
278 | sr_dev_inst_free(sdi); | |
279 | } | |
310e9e9b BV |
280 | g_slist_free(drvc->instances); |
281 | drvc->instances = NULL; | |
7da6f9d5 | 282 | |
811deee4 | 283 | return SR_OK; |
7da6f9d5 BV |
284 | } |
285 | ||
a1bb33af UH |
286 | /* |
287 | * API callbacks | |
288 | */ | |
289 | ||
34f06b90 | 290 | static int hw_init(struct sr_context *sr_ctx) |
61136ea6 | 291 | { |
310e9e9b BV |
292 | struct drv_context *drvc; |
293 | ||
294 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
c0eea11c | 295 | sr_err("Driver context malloc failed."); |
886a52b6 | 296 | return SR_ERR_MALLOC; |
310e9e9b | 297 | } |
c0eea11c | 298 | |
1ebe4b4e | 299 | drvc->sr_ctx = sr_ctx; |
a873c594 | 300 | di->priv = drvc; |
61136ea6 | 301 | |
61136ea6 BV |
302 | return SR_OK; |
303 | } | |
304 | ||
4ca38984 | 305 | static GSList *hw_scan(GSList *options) |
a1bb33af | 306 | { |
d68e2d1a | 307 | struct sr_dev_inst *sdi; |
428edbe1 | 308 | struct sr_probe *probe; |
310e9e9b BV |
309 | struct drv_context *drvc; |
310 | struct dev_context *devc; | |
428edbe1 | 311 | model_t *prof; |
a1bb33af UH |
312 | struct libusb_device_descriptor des; |
313 | libusb_device **devlist; | |
428edbe1 BV |
314 | GSList *devices; |
315 | int ret, devcnt, i, j; | |
a1bb33af | 316 | |
4ca38984 | 317 | (void)options; |
64d33dc2 | 318 | |
a873c594 | 319 | drvc = di->priv; |
4b97c74e | 320 | |
4ca38984 BV |
321 | devices = NULL; |
322 | ||
7da6f9d5 BV |
323 | clear_instances(); |
324 | ||
8fdecced | 325 | /* Find all ZEROPLUS analyzers and add them to device list. */ |
a1bb33af | 326 | devcnt = 0; |
d4abb463 | 327 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */ |
fed16f06 UH |
328 | |
329 | for (i = 0; devlist[i]; i++) { | |
ebc34738 UH |
330 | ret = libusb_get_device_descriptor(devlist[i], &des); |
331 | if (ret != 0) { | |
d4928d71 PS |
332 | sr_err("zp: failed to get device descriptor: %s", |
333 | libusb_error_name(ret)); | |
a1bb33af UH |
334 | continue; |
335 | } | |
336 | ||
428edbe1 BV |
337 | prof = NULL; |
338 | for (j = 0; j < zeroplus_models[j].vid; j++) { | |
339 | if (des.idVendor == zeroplus_models[j].vid && | |
340 | des.idProduct == zeroplus_models[j].pid) { | |
341 | prof = &zeroplus_models[j]; | |
bf43ea23 | 342 | } |
a1bb33af | 343 | } |
428edbe1 BV |
344 | /* Skip if the device was not found */ |
345 | if (!prof) | |
346 | continue; | |
347 | sr_info("zp: Found ZEROPLUS model %s", prof->model_name); | |
348 | ||
349 | /* Register the device with libsigrok. */ | |
350 | if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE, | |
351 | VENDOR_NAME, prof->model_name, NULL))) { | |
352 | sr_err("zp: %s: sr_dev_inst_new failed", __func__); | |
353 | return NULL; | |
354 | } | |
a873c594 | 355 | sdi->driver = di; |
428edbe1 BV |
356 | |
357 | /* Allocate memory for our private driver context. */ | |
310e9e9b BV |
358 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { |
359 | sr_err("zp: %s: devc malloc failed", __func__); | |
886a52b6 | 360 | return NULL; |
428edbe1 | 361 | } |
310e9e9b BV |
362 | sdi->priv = devc; |
363 | devc->num_channels = prof->channels; | |
0ab0cb94 TY |
364 | #ifdef ZP_EXPERIMENTAL |
365 | devc->max_memory_size = 128 * 1024; | |
366 | devc->max_samplerate = 200; | |
367 | #else | |
368 | devc->max_memory_size = prof->sample_depth * 1024; | |
369 | devc->max_samplerate = prof->max_sampling_freq; | |
370 | #endif | |
371 | devc->max_samplerate *= SR_MHZ(1); | |
372 | devc->memory_size = MEMORY_SIZE_8K; | |
310e9e9b | 373 | // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES); |
428edbe1 BV |
374 | |
375 | /* Fill in probelist according to this device's profile. */ | |
310e9e9b | 376 | for (j = 0; j < devc->num_channels; j++) { |
428edbe1 BV |
377 | if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE, |
378 | probe_names[j]))) | |
379 | return NULL; | |
380 | sdi->probes = g_slist_append(sdi->probes, probe); | |
381 | } | |
382 | ||
383 | devices = g_slist_append(devices, sdi); | |
310e9e9b BV |
384 | drvc->instances = g_slist_append(drvc->instances, sdi); |
385 | devc->usb = sr_usb_dev_inst_new( | |
428edbe1 BV |
386 | libusb_get_bus_number(devlist[i]), |
387 | libusb_get_device_address(devlist[i]), NULL); | |
388 | devcnt++; | |
389 | ||
a1bb33af UH |
390 | } |
391 | libusb_free_device_list(devlist, 1); | |
392 | ||
4ca38984 | 393 | return devices; |
a1bb33af UH |
394 | } |
395 | ||
811deee4 BV |
396 | static GSList *hw_dev_list(void) |
397 | { | |
398 | struct drv_context *drvc; | |
399 | ||
a873c594 | 400 | drvc = di->priv; |
811deee4 BV |
401 | |
402 | return drvc->instances; | |
403 | } | |
404 | ||
25a0f108 | 405 | static int hw_dev_open(struct sr_dev_inst *sdi) |
a1bb33af | 406 | { |
310e9e9b | 407 | struct dev_context *devc; |
a873c594 | 408 | struct drv_context *drvc = di->priv; |
428edbe1 BV |
409 | libusb_device **devlist, *dev; |
410 | struct libusb_device_descriptor des; | |
411 | int device_count, ret, i; | |
a1bb33af | 412 | |
310e9e9b | 413 | if (!(devc = sdi->priv)) { |
bf43ea23 UH |
414 | sr_err("zp: %s: sdi->priv was NULL", __func__); |
415 | return SR_ERR_ARG; | |
416 | } | |
417 | ||
d4abb463 PS |
418 | device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, |
419 | &devlist); | |
428edbe1 BV |
420 | if (device_count < 0) { |
421 | sr_err("zp: Failed to retrieve device list"); | |
422 | return SR_ERR; | |
423 | } | |
424 | ||
425 | dev = NULL; | |
426 | for (i = 0; i < device_count; i++) { | |
427 | if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { | |
d4928d71 PS |
428 | sr_err("zp: Failed to get device descriptor: %s.", |
429 | libusb_error_name(ret)); | |
428edbe1 BV |
430 | continue; |
431 | } | |
310e9e9b BV |
432 | if (libusb_get_bus_number(devlist[i]) == devc->usb->bus |
433 | && libusb_get_device_address(devlist[i]) == devc->usb->address) { | |
428edbe1 BV |
434 | dev = devlist[i]; |
435 | break; | |
436 | } | |
437 | } | |
438 | if (!dev) { | |
439 | sr_err("device on bus %d address %d disappeared!", | |
310e9e9b | 440 | devc->usb->bus, devc->usb->address); |
428edbe1 BV |
441 | return SR_ERR; |
442 | } | |
443 | ||
310e9e9b | 444 | if (!(ret = libusb_open(dev, &(devc->usb->devhdl)))) { |
428edbe1 BV |
445 | sdi->status = SR_ST_ACTIVE; |
446 | sr_info("zp: opened device %d on %d.%d interface %d", | |
310e9e9b BV |
447 | sdi->index, devc->usb->bus, |
448 | devc->usb->address, USB_INTERFACE); | |
428edbe1 | 449 | } else { |
d4928d71 | 450 | sr_err("zp: failed to open device: %s", libusb_error_name(ret)); |
428edbe1 BV |
451 | return SR_ERR; |
452 | } | |
453 | ||
310e9e9b | 454 | ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION); |
ebc34738 | 455 | if (ret < 0) { |
d4928d71 PS |
456 | sr_err("zp: Unable to set USB configuration %d: %s", |
457 | USB_CONFIGURATION, libusb_error_name(ret)); | |
185ae2c5 UH |
458 | return SR_ERR; |
459 | } | |
460 | ||
310e9e9b | 461 | ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE); |
ebc34738 | 462 | if (ret != 0) { |
d4928d71 PS |
463 | sr_err("zp: Unable to claim interface: %s", |
464 | libusb_error_name(ret)); | |
e46b8fb1 | 465 | return SR_ERR; |
a1bb33af | 466 | } |
185ae2c5 | 467 | |
0ab0cb94 TY |
468 | /* Set default configuration after power on */ |
469 | if (analyzer_read_status(devc->usb->devhdl) == 0) | |
470 | analyzer_configure(devc->usb->devhdl); | |
471 | ||
310e9e9b BV |
472 | analyzer_reset(devc->usb->devhdl); |
473 | analyzer_initialize(devc->usb->devhdl); | |
a1bb33af | 474 | |
0ab0cb94 | 475 | //analyzer_set_memory_size(MEMORY_SIZE_512K); |
fed16f06 | 476 | // analyzer_set_freq(g_freq, g_freq_scale); |
a1bb33af | 477 | analyzer_set_trigger_count(1); |
408e7199 UH |
478 | // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger) |
479 | // * get_memory_size(g_memory_size)) / 100) >> 2); | |
a1bb33af | 480 | |
fed16f06 UH |
481 | #if 0 |
482 | if (g_double_mode == 1) | |
a1bb33af UH |
483 | analyzer_set_compression(COMPRESSION_DOUBLE); |
484 | else if (g_compression == 1) | |
485 | analyzer_set_compression(COMPRESSION_ENABLE); | |
fed16f06 UH |
486 | else |
487 | #endif | |
488 | analyzer_set_compression(COMPRESSION_NONE); | |
a1bb33af | 489 | |
310e9e9b | 490 | if (devc->cur_samplerate == 0) { |
0ab0cb94 TY |
491 | /* Samplerate hasn't been set. Default to 1MHz. */ |
492 | analyzer_set_freq(1, FREQ_SCALE_MHZ); | |
493 | devc->cur_samplerate = SR_MHZ(1); | |
a1bb33af UH |
494 | } |
495 | ||
e46b8fb1 | 496 | return SR_OK; |
a1bb33af UH |
497 | } |
498 | ||
25a0f108 | 499 | static int hw_dev_close(struct sr_dev_inst *sdi) |
a1bb33af | 500 | { |
310e9e9b | 501 | struct dev_context *devc; |
a1bb33af | 502 | |
310e9e9b | 503 | if (!(devc = sdi->priv)) { |
25a0f108 BV |
504 | sr_err("zp: %s: sdi->priv was NULL", __func__); |
505 | return SR_ERR; | |
697785d1 UH |
506 | } |
507 | ||
310e9e9b | 508 | if (!devc->usb->devhdl) |
25a0f108 BV |
509 | return SR_ERR; |
510 | ||
511 | sr_info("zp: closing device %d on %d.%d interface %d", sdi->index, | |
310e9e9b BV |
512 | devc->usb->bus, devc->usb->address, USB_INTERFACE); |
513 | libusb_release_interface(devc->usb->devhdl, USB_INTERFACE); | |
514 | libusb_reset_device(devc->usb->devhdl); | |
515 | libusb_close(devc->usb->devhdl); | |
516 | devc->usb->devhdl = NULL; | |
25a0f108 | 517 | sdi->status = SR_ST_INACTIVE; |
697785d1 UH |
518 | |
519 | return SR_OK; | |
a1bb33af UH |
520 | } |
521 | ||
57ab7d9f | 522 | static int hw_cleanup(void) |
a1bb33af | 523 | { |
310e9e9b BV |
524 | struct drv_context *drvc; |
525 | ||
a873c594 | 526 | if (!(drvc = di->priv)) |
310e9e9b | 527 | return SR_OK; |
a1bb33af | 528 | |
7da6f9d5 | 529 | clear_instances(); |
a1bb33af | 530 | |
57ab7d9f | 531 | return SR_OK; |
a1bb33af UH |
532 | } |
533 | ||
035a1078 | 534 | static int config_get(int id, const void **data, const struct sr_dev_inst *sdi) |
a1bb33af | 535 | { |
310e9e9b | 536 | struct dev_context *devc; |
a1bb33af | 537 | |
035a1078 | 538 | switch (id) { |
123e1313 | 539 | case SR_CONF_SAMPLERATE: |
626409ab | 540 | if (sdi) { |
310e9e9b BV |
541 | devc = sdi->priv; |
542 | *data = &devc->cur_samplerate; | |
626409ab | 543 | sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.", |
310e9e9b | 544 | __func__, devc->cur_samplerate); |
626409ab BV |
545 | } else |
546 | return SR_ERR; | |
bf43ea23 UH |
547 | break; |
548 | default: | |
626409ab | 549 | return SR_ERR_ARG; |
a1bb33af UH |
550 | } |
551 | ||
626409ab | 552 | return SR_OK; |
a1bb33af UH |
553 | } |
554 | ||
0ab0cb94 | 555 | static int set_samplerate(struct dev_context *devc, uint64_t samplerate) |
a1bb33af | 556 | { |
0ab0cb94 | 557 | int i; |
bf43ea23 | 558 | |
0ab0cb94 TY |
559 | for (i = 0; supported_samplerates[i]; i++) |
560 | if (samplerate == supported_samplerates[i]) | |
561 | break; | |
562 | if (!supported_samplerates[i] || samplerate > devc->max_samplerate) { | |
563 | sr_err("zp: %s: unsupported samplerate", __func__); | |
bf43ea23 UH |
564 | return SR_ERR_ARG; |
565 | } | |
566 | ||
73017cf9 UH |
567 | sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate); |
568 | ||
0ab0cb94 | 569 | if (samplerate >= SR_MHZ(1)) |
59df0c77 | 570 | analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ); |
0ab0cb94 | 571 | else if (samplerate >= SR_KHZ(1)) |
59df0c77 | 572 | analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ); |
a1bb33af | 573 | else |
fed16f06 | 574 | analyzer_set_freq(samplerate, FREQ_SCALE_HZ); |
a1bb33af | 575 | |
310e9e9b | 576 | devc->cur_samplerate = samplerate; |
a1bb33af | 577 | |
e46b8fb1 | 578 | return SR_OK; |
a1bb33af UH |
579 | } |
580 | ||
0ab0cb94 TY |
581 | static int set_limit_samples(struct dev_context *devc, uint64_t samples) |
582 | { | |
583 | devc->limit_samples = samples; | |
584 | ||
585 | if (samples <= 2 * 1024) | |
586 | devc->memory_size = MEMORY_SIZE_8K; | |
587 | else if (samples <= 16 * 1024) | |
588 | devc->memory_size = MEMORY_SIZE_64K; | |
589 | else if (samples <= 32 * 1024 || | |
590 | devc->max_memory_size <= 32 * 1024) | |
591 | devc->memory_size = MEMORY_SIZE_128K; | |
592 | else | |
593 | devc->memory_size = MEMORY_SIZE_512K; | |
594 | ||
595 | sr_info("zp: Setting memory size to %dK.", | |
596 | get_memory_size(devc->memory_size) / 1024); | |
597 | ||
598 | analyzer_set_memory_size(devc->memory_size); | |
599 | ||
600 | return SR_OK; | |
601 | } | |
602 | ||
603 | static int set_capture_ratio(struct dev_context *devc, uint64_t ratio) | |
604 | { | |
605 | if (ratio > 100) { | |
606 | sr_err("zp: %s: invalid capture ratio", __func__); | |
607 | return SR_ERR_ARG; | |
608 | } | |
609 | ||
610 | devc->capture_ratio = ratio; | |
611 | ||
612 | sr_info("zp: Setting capture ratio to %d%%.", devc->capture_ratio); | |
613 | ||
614 | return SR_OK; | |
615 | } | |
616 | ||
035a1078 | 617 | static int config_set(int id, const void *value, const struct sr_dev_inst *sdi) |
a1bb33af | 618 | { |
310e9e9b | 619 | struct dev_context *devc; |
a1bb33af | 620 | |
0ab0cb94 TY |
621 | if (!sdi) { |
622 | sr_err("zp: %s: sdi was NULL", __func__); | |
623 | return SR_ERR_ARG; | |
624 | } | |
625 | ||
310e9e9b | 626 | if (!(devc = sdi->priv)) { |
bf43ea23 UH |
627 | sr_err("zp: %s: sdi->priv was NULL", __func__); |
628 | return SR_ERR_ARG; | |
629 | } | |
a1bb33af | 630 | |
035a1078 | 631 | switch (id) { |
1953564a | 632 | case SR_CONF_SAMPLERATE: |
0ab0cb94 | 633 | return set_samplerate(devc, *(const uint64_t *)value); |
1953564a | 634 | case SR_CONF_LIMIT_SAMPLES: |
0ab0cb94 | 635 | return set_limit_samples(devc, *(const uint64_t *)value); |
1953564a | 636 | case SR_CONF_CAPTURE_RATIO: |
0ab0cb94 | 637 | return set_capture_ratio(devc, *(const uint64_t *)value); |
fed16f06 | 638 | default: |
e46b8fb1 | 639 | return SR_ERR; |
a1bb33af UH |
640 | } |
641 | } | |
642 | ||
a1c743fc BV |
643 | static int config_list(int key, const void **data, const struct sr_dev_inst *sdi) |
644 | { | |
645 | ||
646 | (void)sdi; | |
647 | ||
648 | switch (key) { | |
9a6517d1 BV |
649 | case SR_CONF_DEVICE_OPTIONS: |
650 | *data = hwcaps; | |
651 | break; | |
a1c743fc BV |
652 | case SR_CONF_SAMPLERATE: |
653 | *data = &samplerates; | |
654 | break; | |
c50277a6 BV |
655 | case SR_CONF_TRIGGER_TYPE: |
656 | *data = TRIGGER_TYPE; | |
657 | break; | |
a1c743fc BV |
658 | default: |
659 | return SR_ERR_ARG; | |
660 | } | |
661 | ||
662 | return SR_OK; | |
663 | } | |
664 | ||
0ab0cb94 TY |
665 | static void set_triggerbar(struct dev_context *devc) |
666 | { | |
667 | unsigned int ramsize; | |
668 | unsigned int n; | |
669 | unsigned int triggerbar; | |
670 | ||
671 | ramsize = get_memory_size(devc->memory_size) / 4; | |
672 | if (devc->trigger) { | |
673 | n = ramsize; | |
674 | if (devc->max_memory_size < n) | |
675 | n = devc->max_memory_size; | |
676 | if (devc->limit_samples < n) | |
677 | n = devc->limit_samples; | |
678 | n = n * devc->capture_ratio / 100; | |
679 | if (n > ramsize - 8) | |
680 | triggerbar = ramsize - 8; | |
681 | else | |
682 | triggerbar = n; | |
683 | } else { | |
684 | triggerbar = 0; | |
685 | } | |
686 | analyzer_set_triggerbar_address(triggerbar); | |
687 | analyzer_set_ramsize_trigger_address(ramsize - triggerbar); | |
688 | ||
689 | sr_dbg("zp: triggerbar_address = %d(0x%x)", triggerbar, triggerbar); | |
690 | sr_dbg("zp: ramsize_triggerbar_address = %d(0x%x)", | |
691 | ramsize - triggerbar, ramsize - triggerbar); | |
692 | } | |
693 | ||
3ffb6964 BV |
694 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
695 | void *cb_data) | |
a1bb33af | 696 | { |
b9c735a2 | 697 | struct sr_datafeed_packet packet; |
9c939c51 | 698 | struct sr_datafeed_logic logic; |
b9c735a2 | 699 | struct sr_datafeed_header header; |
0ab0cb94 | 700 | //uint64_t samples_read; |
a1bb33af | 701 | int res; |
afc8e4de | 702 | unsigned int packet_num; |
0ab0cb94 | 703 | unsigned int n; |
a1bb33af | 704 | unsigned char *buf; |
310e9e9b | 705 | struct dev_context *devc; |
a1bb33af | 706 | |
310e9e9b | 707 | if (!(devc = sdi->priv)) { |
bf43ea23 UH |
708 | sr_err("zp: %s: sdi->priv was NULL", __func__); |
709 | return SR_ERR_ARG; | |
710 | } | |
a1bb33af | 711 | |
014359e3 BV |
712 | if (configure_probes(sdi) != SR_OK) { |
713 | sr_err("zp: failed to configured probes"); | |
714 | return SR_ERR; | |
715 | } | |
716 | ||
0ab0cb94 TY |
717 | set_triggerbar(devc); |
718 | ||
a143e4e5 | 719 | /* push configured settings to device */ |
310e9e9b | 720 | analyzer_configure(devc->usb->devhdl); |
a143e4e5 | 721 | |
310e9e9b | 722 | analyzer_start(devc->usb->devhdl); |
7b48d6e1 | 723 | sr_info("zp: Waiting for data"); |
310e9e9b | 724 | analyzer_wait_data(devc->usb->devhdl); |
a1bb33af | 725 | |
7b48d6e1 | 726 | sr_info("zp: Stop address = 0x%x", |
310e9e9b | 727 | analyzer_get_stop_address(devc->usb->devhdl)); |
7b48d6e1 | 728 | sr_info("zp: Now address = 0x%x", |
310e9e9b | 729 | analyzer_get_now_address(devc->usb->devhdl)); |
7b48d6e1 | 730 | sr_info("zp: Trigger address = 0x%x", |
310e9e9b | 731 | analyzer_get_trigger_address(devc->usb->devhdl)); |
a1bb33af | 732 | |
5a2326a7 | 733 | packet.type = SR_DF_HEADER; |
9c939c51 | 734 | packet.payload = &header; |
a1bb33af UH |
735 | header.feed_version = 1; |
736 | gettimeofday(&header.starttime, NULL); | |
f366e86c BV |
737 | sr_session_send(cb_data, &packet); |
738 | ||
b53738ba | 739 | if (!(buf = g_try_malloc(PACKET_SIZE))) { |
bf43ea23 | 740 | sr_err("zp: %s: buf malloc failed", __func__); |
b53738ba UH |
741 | return SR_ERR_MALLOC; |
742 | } | |
743 | ||
0ab0cb94 | 744 | //samples_read = 0; |
310e9e9b | 745 | analyzer_read_start(devc->usb->devhdl); |
fed16f06 | 746 | /* Send the incoming transfer to the session bus. */ |
0ab0cb94 TY |
747 | n = get_memory_size(devc->memory_size); |
748 | if (devc->max_memory_size * 4 < n) | |
749 | n = devc->max_memory_size * 4; | |
750 | for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) { | |
310e9e9b | 751 | res = analyzer_read_data(devc->usb->devhdl, buf, PACKET_SIZE); |
0ab0cb94 | 752 | sr_info("zp: Tried to read %d bytes, actually read %d bytes", |
b08024a8 | 753 | PACKET_SIZE, res); |
a1bb33af | 754 | |
5a2326a7 | 755 | packet.type = SR_DF_LOGIC; |
9c939c51 BV |
756 | packet.payload = &logic; |
757 | logic.length = PACKET_SIZE; | |
758 | logic.unitsize = 4; | |
759 | logic.data = buf; | |
3cd3a20b | 760 | sr_session_send(cb_data, &packet); |
0ab0cb94 | 761 | //samples_read += res / 4; |
a1bb33af | 762 | } |
310e9e9b | 763 | analyzer_read_stop(devc->usb->devhdl); |
a1bb33af UH |
764 | g_free(buf); |
765 | ||
5a2326a7 | 766 | packet.type = SR_DF_END; |
3cd3a20b | 767 | sr_session_send(cb_data, &packet); |
a1bb33af | 768 | |
e46b8fb1 | 769 | return SR_OK; |
a1bb33af UH |
770 | } |
771 | ||
3cd3a20b | 772 | /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */ |
69b07d14 | 773 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
a1bb33af | 774 | { |
b9c735a2 | 775 | struct sr_datafeed_packet packet; |
310e9e9b | 776 | struct dev_context *devc; |
a1bb33af | 777 | |
5a2326a7 | 778 | packet.type = SR_DF_END; |
3cd3a20b | 779 | sr_session_send(cb_data, &packet); |
a1bb33af | 780 | |
310e9e9b | 781 | if (!(devc = sdi->priv)) { |
69890f73 | 782 | sr_err("zp: %s: sdi->priv was NULL", __func__); |
3010f21c | 783 | return SR_ERR_BUG; |
69890f73 | 784 | } |
a1bb33af | 785 | |
310e9e9b | 786 | analyzer_reset(devc->usb->devhdl); |
fed16f06 | 787 | /* TODO: Need to cancel and free any queued up transfers. */ |
3010f21c UH |
788 | |
789 | return SR_OK; | |
a1bb33af UH |
790 | } |
791 | ||
c09f0b57 | 792 | SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = { |
e519ba86 | 793 | .name = "zeroplus-logic-cube", |
8fdecced | 794 | .longname = "ZEROPLUS Logic Cube LAP-C series", |
e519ba86 UH |
795 | .api_version = 1, |
796 | .init = hw_init, | |
797 | .cleanup = hw_cleanup, | |
61136ea6 | 798 | .scan = hw_scan, |
811deee4 BV |
799 | .dev_list = hw_dev_list, |
800 | .dev_clear = hw_cleanup, | |
035a1078 BV |
801 | .config_get = config_get, |
802 | .config_set = config_set, | |
a1c743fc | 803 | .config_list = config_list, |
e7eb703f UH |
804 | .dev_open = hw_dev_open, |
805 | .dev_close = hw_dev_close, | |
69040b7c UH |
806 | .dev_acquisition_start = hw_dev_acquisition_start, |
807 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
310e9e9b | 808 | .priv = NULL, |
a1bb33af | 809 | }; |