]>
Commit | Line | Data |
---|---|---|
b908f067 UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de> | |
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 2 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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <ftdi.h> | |
22 | #include <glib.h> | |
23 | #include <string.h> | |
45c59c8b BV |
24 | #include "libsigrok.h" |
25 | #include "libsigrok-internal.h" | |
b908f067 UH |
26 | #include "driver.h" |
27 | ||
28 | static GSList *dev_insts = NULL; | |
29 | ||
30 | /* Function prototypes. */ | |
31 | static int hw_dev_acquisition_stop(int dev_index, void *cb_data); | |
32 | ||
33 | static int hw_init(const char *devinfo) | |
34 | { | |
35 | int ret; | |
36 | struct sr_dev_inst *sdi; | |
37 | struct context *ctx; | |
38 | ||
39 | /* Avoid compiler errors. */ | |
40 | (void)devinfo; | |
41 | ||
42 | /* Allocate memory for our private driver context. */ | |
43 | if (!(ctx = g_try_malloc(sizeof(struct context)))) { | |
44 | sr_err("la8: %s: struct context malloc failed", __func__); | |
45 | goto err_free_nothing; | |
46 | } | |
47 | ||
48 | /* Set some sane defaults. */ | |
49 | ctx->ftdic = NULL; | |
50 | ctx->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */ | |
51 | ctx->limit_msec = 0; | |
52 | ctx->limit_samples = 0; | |
53 | ctx->session_dev_id = NULL; | |
54 | memset(ctx->mangled_buf, 0, BS); | |
55 | ctx->final_buf = NULL; | |
56 | ctx->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */ | |
57 | ctx->trigger_mask = 0x00; /* All probes are "don't care". */ | |
58 | ctx->trigger_timeout = 10; /* Default to 10s trigger timeout. */ | |
59 | ctx->trigger_found = 0; | |
60 | ctx->done = 0; | |
61 | ctx->block_counter = 0; | |
62 | ctx->divcount = 0; /* 10ns sample period == 100MHz samplerate */ | |
63 | ||
64 | /* Allocate memory where we'll store the de-mangled data. */ | |
65 | if (!(ctx->final_buf = g_try_malloc(SDRAM_SIZE))) { | |
66 | sr_err("la8: %s: final_buf malloc failed", __func__); | |
67 | goto err_free_ctx; | |
68 | } | |
69 | ||
70 | /* Allocate memory for the FTDI context (ftdic) and initialize it. */ | |
71 | if (!(ctx->ftdic = ftdi_new())) { | |
72 | sr_err("la8: %s: ftdi_new failed", __func__); | |
73 | goto err_free_final_buf; | |
74 | } | |
75 | ||
76 | /* Check for the device and temporarily open it. */ | |
77 | if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID, | |
78 | USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) { | |
79 | (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */ | |
80 | goto err_free_ftdic; | |
81 | } | |
82 | sr_dbg("la8: Found LA8 device (%04x:%04x).", USB_VENDOR_ID, | |
83 | USB_PRODUCT_ID); | |
84 | ||
85 | /* Register the device with libsigrok. */ | |
86 | sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING, | |
87 | USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION); | |
88 | if (!sdi) { | |
89 | sr_err("la8: %s: sr_dev_inst_new failed", __func__); | |
90 | goto err_close_ftdic; | |
91 | } | |
92 | ||
93 | sdi->priv = ctx; | |
94 | ||
95 | dev_insts = g_slist_append(dev_insts, sdi); | |
96 | ||
97 | sr_spew("la8: Device init successful."); | |
98 | ||
99 | /* Close device. We'll reopen it again when we need it. */ | |
100 | (void) la8_close(ctx); /* Log, but ignore errors. */ | |
101 | ||
102 | return 1; | |
103 | ||
104 | err_close_ftdic: | |
105 | (void) la8_close(ctx); /* Log, but ignore errors. */ | |
106 | err_free_ftdic: | |
107 | free(ctx->ftdic); /* NOT g_free()! */ | |
108 | err_free_final_buf: | |
109 | g_free(ctx->final_buf); | |
110 | err_free_ctx: | |
111 | g_free(ctx); | |
112 | err_free_nothing: | |
113 | ||
114 | return 0; | |
115 | } | |
116 | ||
117 | static int hw_dev_open(int dev_index) | |
118 | { | |
119 | int ret; | |
120 | struct sr_dev_inst *sdi; | |
121 | struct context *ctx; | |
122 | ||
123 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
124 | sr_err("la8: %s: sdi was NULL", __func__); | |
125 | return SR_ERR_BUG; | |
126 | } | |
127 | ||
128 | if (!(ctx = sdi->priv)) { | |
129 | sr_err("la8: %s: sdi->priv was NULL", __func__); | |
130 | return SR_ERR_BUG; | |
131 | } | |
132 | ||
133 | sr_dbg("la8: Opening LA8 device (%04x:%04x).", USB_VENDOR_ID, | |
134 | USB_PRODUCT_ID); | |
135 | ||
136 | /* Open the device. */ | |
137 | if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID, | |
138 | USB_PRODUCT_ID, USB_DESCRIPTION, NULL)) < 0) { | |
139 | sr_err("la8: %s: ftdi_usb_open_desc: (%d) %s", | |
140 | __func__, ret, ftdi_get_error_string(ctx->ftdic)); | |
141 | (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */ | |
142 | return SR_ERR; | |
143 | } | |
144 | sr_dbg("la8: Device opened successfully."); | |
145 | ||
146 | /* Purge RX/TX buffers in the FTDI chip. */ | |
147 | if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0) { | |
148 | sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s", | |
149 | __func__, ret, ftdi_get_error_string(ctx->ftdic)); | |
150 | (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */ | |
151 | goto err_dev_open_close_ftdic; | |
152 | } | |
153 | sr_dbg("la8: FTDI buffers purged successfully."); | |
154 | ||
155 | /* Enable flow control in the FTDI chip. */ | |
156 | if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) { | |
157 | sr_err("la8: %s: ftdi_setflowcontrol: (%d) %s", | |
158 | __func__, ret, ftdi_get_error_string(ctx->ftdic)); | |
159 | (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */ | |
160 | goto err_dev_open_close_ftdic; | |
161 | } | |
162 | sr_dbg("la8: FTDI flow control enabled successfully."); | |
163 | ||
164 | /* Wait 100ms. */ | |
165 | g_usleep(100 * 1000); | |
166 | ||
167 | sdi->status = SR_ST_ACTIVE; | |
168 | ||
169 | return SR_OK; | |
170 | ||
171 | err_dev_open_close_ftdic: | |
172 | (void) la8_close(ctx); /* Log, but ignore errors. */ | |
173 | return SR_ERR; | |
174 | } | |
175 | ||
176 | static int hw_dev_close(int dev_index) | |
177 | { | |
178 | struct sr_dev_inst *sdi; | |
179 | struct context *ctx; | |
180 | ||
181 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
182 | sr_err("la8: %s: sdi was NULL", __func__); | |
183 | return SR_ERR_BUG; | |
184 | } | |
185 | ||
186 | if (!(ctx = sdi->priv)) { | |
187 | sr_err("la8: %s: sdi->priv was NULL", __func__); | |
188 | return SR_ERR_BUG; | |
189 | } | |
190 | ||
191 | sr_dbg("la8: Closing device."); | |
192 | ||
193 | if (sdi->status == SR_ST_ACTIVE) { | |
194 | sr_dbg("la8: Status ACTIVE, closing device."); | |
195 | /* TODO: Really ignore errors here, or return SR_ERR? */ | |
196 | (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */ | |
197 | } else { | |
198 | sr_spew("la8: Status not ACTIVE, nothing to do."); | |
199 | } | |
200 | ||
201 | sdi->status = SR_ST_INACTIVE; | |
202 | ||
203 | sr_dbg("la8: Freeing sample buffer."); | |
204 | g_free(ctx->final_buf); | |
205 | ||
206 | return SR_OK; | |
207 | } | |
208 | ||
209 | static int hw_cleanup(void) | |
210 | { | |
211 | GSList *l; | |
212 | struct sr_dev_inst *sdi; | |
213 | int ret = SR_OK; | |
214 | ||
215 | /* Properly close all devices. */ | |
216 | for (l = dev_insts; l; l = l->next) { | |
217 | if (!(sdi = l->data)) { | |
218 | /* Log error, but continue cleaning up the rest. */ | |
219 | sr_err("la8: %s: sdi was NULL, continuing", __func__); | |
220 | ret = SR_ERR_BUG; | |
221 | continue; | |
222 | } | |
223 | sr_dev_inst_free(sdi); /* Returns void. */ | |
224 | } | |
225 | g_slist_free(dev_insts); /* Returns void. */ | |
226 | dev_insts = NULL; | |
227 | ||
228 | return ret; | |
229 | } | |
230 | ||
231 | static const void *hw_dev_info_get(int dev_index, int dev_info_id) | |
232 | { | |
233 | struct sr_dev_inst *sdi; | |
234 | struct context *ctx; | |
235 | const void *info; | |
236 | ||
237 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
238 | sr_err("la8: %s: sdi was NULL", __func__); | |
239 | return NULL; | |
240 | } | |
241 | ||
242 | if (!(ctx = sdi->priv)) { | |
243 | sr_err("la8: %s: sdi->priv was NULL", __func__); | |
244 | return NULL; | |
245 | } | |
246 | ||
247 | sr_spew("la8: %s: dev_index %d, dev_info_id %d.", __func__, | |
248 | dev_index, dev_info_id); | |
249 | ||
250 | switch (dev_info_id) { | |
251 | case SR_DI_INST: | |
252 | info = sdi; | |
253 | sr_spew("la8: %s: Returning sdi.", __func__); | |
254 | break; | |
255 | case SR_DI_NUM_PROBES: | |
256 | info = GINT_TO_POINTER(NUM_PROBES); | |
257 | sr_spew("la8: %s: Returning number of probes: %d.", __func__, | |
258 | NUM_PROBES); | |
259 | break; | |
260 | case SR_DI_PROBE_NAMES: | |
261 | info = probe_names; | |
262 | sr_spew("la8: %s: Returning probenames.", __func__); | |
263 | break; | |
264 | case SR_DI_SAMPLERATES: | |
265 | fill_supported_samplerates_if_needed(); | |
266 | info = &samplerates; | |
267 | sr_spew("la8: %s: Returning samplerates.", __func__); | |
268 | break; | |
269 | case SR_DI_TRIGGER_TYPES: | |
270 | info = (char *)TRIGGER_TYPES; | |
271 | sr_spew("la8: %s: Returning trigger types: %s.", __func__, | |
272 | TRIGGER_TYPES); | |
273 | break; | |
274 | case SR_DI_CUR_SAMPLERATE: | |
275 | info = &ctx->cur_samplerate; | |
276 | sr_spew("la8: %s: Returning samplerate: %" PRIu64 "Hz.", | |
277 | __func__, ctx->cur_samplerate); | |
278 | break; | |
279 | default: | |
280 | /* Unknown device info ID, return NULL. */ | |
281 | sr_err("la8: %s: Unknown device info ID", __func__); | |
282 | info = NULL; | |
283 | break; | |
284 | } | |
285 | ||
286 | return info; | |
287 | } | |
288 | ||
289 | static int hw_dev_status_get(int dev_index) | |
290 | { | |
291 | struct sr_dev_inst *sdi; | |
292 | ||
293 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
294 | sr_err("la8: %s: sdi was NULL, device not found", __func__); | |
295 | return SR_ST_NOT_FOUND; | |
296 | } | |
297 | ||
298 | sr_dbg("la8: Returning status: %d.", sdi->status); | |
299 | ||
300 | return sdi->status; | |
301 | } | |
302 | ||
303 | static const int *hw_hwcap_get_all(void) | |
304 | { | |
305 | sr_spew("la8: Returning list of device capabilities."); | |
306 | ||
307 | return hwcaps; | |
308 | } | |
309 | ||
310 | static int hw_dev_config_set(int dev_index, int hwcap, const void *value) | |
311 | { | |
312 | struct sr_dev_inst *sdi; | |
313 | struct context *ctx; | |
314 | ||
315 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
316 | sr_err("la8: %s: sdi was NULL", __func__); | |
317 | return SR_ERR_BUG; | |
318 | } | |
319 | ||
320 | if (!(ctx = sdi->priv)) { | |
321 | sr_err("la8: %s: sdi->priv was NULL", __func__); | |
322 | return SR_ERR_BUG; | |
323 | } | |
324 | ||
325 | sr_spew("la8: %s: dev_index %d, hwcap %d", __func__, dev_index, hwcap); | |
326 | ||
327 | switch (hwcap) { | |
328 | case SR_HWCAP_SAMPLERATE: | |
329 | if (set_samplerate(sdi, *(const uint64_t *)value) == SR_ERR) { | |
330 | sr_err("la8: %s: setting samplerate failed.", __func__); | |
331 | return SR_ERR; | |
332 | } | |
333 | sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate); | |
334 | break; | |
335 | case SR_HWCAP_PROBECONFIG: | |
336 | if (configure_probes(ctx, (const GSList *)value) != SR_OK) { | |
337 | sr_err("la8: %s: probe config failed.", __func__); | |
338 | return SR_ERR; | |
339 | } | |
340 | break; | |
341 | case SR_HWCAP_LIMIT_MSEC: | |
342 | if (*(const uint64_t *)value == 0) { | |
343 | sr_err("la8: %s: LIMIT_MSEC can't be 0.", __func__); | |
344 | return SR_ERR; | |
345 | } | |
346 | ctx->limit_msec = *(const uint64_t *)value; | |
347 | sr_dbg("la8: LIMIT_MSEC = %" PRIu64, ctx->limit_msec); | |
348 | break; | |
349 | case SR_HWCAP_LIMIT_SAMPLES: | |
350 | if (*(const uint64_t *)value < MIN_NUM_SAMPLES) { | |
351 | sr_err("la8: %s: LIMIT_SAMPLES too small.", __func__); | |
352 | return SR_ERR; | |
353 | } | |
354 | ctx->limit_samples = *(const uint64_t *)value; | |
355 | sr_dbg("la8: LIMIT_SAMPLES = %" PRIu64, ctx->limit_samples); | |
356 | break; | |
357 | default: | |
358 | /* Unknown capability, return SR_ERR. */ | |
359 | sr_err("la8: %s: Unknown capability.", __func__); | |
360 | return SR_ERR; | |
361 | break; | |
362 | } | |
363 | ||
364 | return SR_OK; | |
365 | } | |
366 | ||
367 | static int receive_data(int fd, int revents, void *cb_data) | |
368 | { | |
369 | int i, ret; | |
370 | struct sr_dev_inst *sdi; | |
371 | struct context *ctx; | |
372 | ||
373 | /* Avoid compiler errors. */ | |
374 | (void)fd; | |
375 | (void)revents; | |
376 | ||
377 | if (!(sdi = cb_data)) { | |
378 | sr_err("la8: %s: cb_data was NULL", __func__); | |
379 | return FALSE; | |
380 | } | |
381 | ||
382 | if (!(ctx = sdi->priv)) { | |
383 | sr_err("la8: %s: sdi->priv was NULL", __func__); | |
384 | return FALSE; | |
385 | } | |
386 | ||
387 | if (!ctx->ftdic) { | |
388 | sr_err("la8: %s: ctx->ftdic was NULL", __func__); | |
389 | return FALSE; | |
390 | } | |
391 | ||
392 | /* Get one block of data. */ | |
393 | if ((ret = la8_read_block(ctx)) < 0) { | |
394 | sr_err("la8: %s: la8_read_block error: %d", __func__, ret); | |
395 | hw_dev_acquisition_stop(sdi->index, sdi); | |
396 | return FALSE; | |
397 | } | |
398 | ||
399 | /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */ | |
400 | if (ctx->block_counter != (NUM_BLOCKS - 1)) { | |
401 | ctx->block_counter++; | |
402 | return TRUE; | |
403 | } | |
404 | ||
405 | sr_dbg("la8: Sampling finished, sending data to session bus now."); | |
406 | ||
407 | /* All data was received and demangled, send it to the session bus. */ | |
408 | for (i = 0; i < NUM_BLOCKS; i++) | |
409 | send_block_to_session_bus(ctx, i); | |
410 | ||
411 | hw_dev_acquisition_stop(sdi->index, sdi); | |
412 | ||
413 | // return FALSE; /* FIXME? */ | |
414 | return TRUE; | |
415 | } | |
416 | ||
417 | static int hw_dev_acquisition_start(int dev_index, void *cb_data) | |
418 | { | |
419 | struct sr_dev_inst *sdi; | |
420 | struct context *ctx; | |
421 | struct sr_datafeed_packet packet; | |
422 | struct sr_datafeed_header header; | |
423 | struct sr_datafeed_meta_logic meta; | |
424 | uint8_t buf[4]; | |
425 | int bytes_written; | |
426 | ||
427 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
428 | sr_err("la8: %s: sdi was NULL", __func__); | |
429 | return SR_ERR_BUG; | |
430 | } | |
431 | ||
432 | if (!(ctx = sdi->priv)) { | |
433 | sr_err("la8: %s: sdi->priv was NULL", __func__); | |
434 | return SR_ERR_BUG; | |
435 | } | |
436 | ||
437 | if (!ctx->ftdic) { | |
438 | sr_err("la8: %s: ctx->ftdic was NULL", __func__); | |
439 | return SR_ERR_BUG; | |
440 | } | |
441 | ||
442 | ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate); | |
443 | if (ctx->divcount == 0xff) { | |
444 | sr_err("la8: %s: invalid divcount/samplerate", __func__); | |
445 | return SR_ERR; | |
446 | } | |
447 | ||
448 | sr_dbg("la8: Starting acquisition."); | |
449 | ||
450 | /* Fill acquisition parameters into buf[]. */ | |
451 | buf[0] = ctx->divcount; | |
452 | buf[1] = 0xff; /* This byte must always be 0xff. */ | |
453 | buf[2] = ctx->trigger_pattern; | |
454 | buf[3] = ctx->trigger_mask; | |
455 | ||
456 | /* Start acquisition. */ | |
457 | bytes_written = la8_write(ctx, buf, 4); | |
458 | ||
459 | if (bytes_written < 0) { | |
460 | sr_err("la8: Acquisition failed to start."); | |
461 | return SR_ERR; | |
462 | } else if (bytes_written != 4) { | |
463 | sr_err("la8: Acquisition failed to start."); | |
464 | return SR_ERR; /* TODO: Other error and return code? */ | |
465 | } | |
466 | ||
467 | sr_dbg("la8: Acquisition started successfully."); | |
468 | ||
469 | ctx->session_dev_id = cb_data; | |
470 | ||
471 | /* Send header packet to the session bus. */ | |
472 | sr_dbg("la8: Sending SR_DF_HEADER."); | |
473 | packet.type = SR_DF_HEADER; | |
474 | packet.payload = &header; | |
475 | header.feed_version = 1; | |
476 | gettimeofday(&header.starttime, NULL); | |
477 | sr_session_send(ctx->session_dev_id, &packet); | |
478 | ||
479 | /* Send metadata about the SR_DF_LOGIC packets to come. */ | |
480 | packet.type = SR_DF_META_LOGIC; | |
481 | packet.payload = &meta; | |
482 | meta.samplerate = ctx->cur_samplerate; | |
483 | meta.num_probes = NUM_PROBES; | |
484 | sr_session_send(ctx->session_dev_id, &packet); | |
485 | ||
486 | /* Time when we should be done (for detecting trigger timeouts). */ | |
487 | ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL) | |
488 | + ctx->trigger_timeout; | |
489 | ctx->block_counter = 0; | |
490 | ctx->trigger_found = 0; | |
491 | ||
492 | /* Hook up a dummy handler to receive data from the LA8. */ | |
493 | sr_source_add(-1, G_IO_IN, 0, receive_data, sdi); | |
494 | ||
495 | return SR_OK; | |
496 | } | |
497 | ||
498 | static int hw_dev_acquisition_stop(int dev_index, void *cb_data) | |
499 | { | |
500 | struct sr_dev_inst *sdi; | |
501 | struct context *ctx; | |
502 | struct sr_datafeed_packet packet; | |
503 | ||
504 | sr_dbg("la8: Stopping acquisition."); | |
505 | ||
506 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
507 | sr_err("la8: %s: sdi was NULL", __func__); | |
508 | return SR_ERR_BUG; | |
509 | } | |
510 | ||
511 | if (!(ctx = sdi->priv)) { | |
512 | sr_err("la8: %s: sdi->priv was NULL", __func__); | |
513 | return SR_ERR_BUG; | |
514 | } | |
515 | ||
516 | /* Send end packet to the session bus. */ | |
517 | sr_dbg("la8: Sending SR_DF_END."); | |
518 | packet.type = SR_DF_END; | |
519 | sr_session_send(cb_data, &packet); | |
520 | ||
521 | return SR_OK; | |
522 | } | |
523 | ||
524 | SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = { | |
525 | .name = "chronovu-la8", | |
526 | .longname = "ChronoVu LA8", | |
527 | .api_version = 1, | |
528 | .init = hw_init, | |
529 | .cleanup = hw_cleanup, | |
530 | .dev_open = hw_dev_open, | |
531 | .dev_close = hw_dev_close, | |
532 | .dev_info_get = hw_dev_info_get, | |
533 | .dev_status_get = hw_dev_status_get, | |
534 | .hwcap_get_all = hw_hwcap_get_all, | |
535 | .dev_config_set = hw_dev_config_set, | |
536 | .dev_acquisition_start = hw_dev_acquisition_start, | |
537 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
538 | }; |