2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <glib/gstdio.h>
33 struct cmd_start_acquisition {
35 uint8_t sample_delay_h;
36 uint8_t sample_delay_l;
41 static int command_get_fw_version(libusb_device_handle *devhdl,
42 struct version_info *vi)
46 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
47 LIBUSB_ENDPOINT_IN, CMD_GET_FW_VERSION, 0x0000, 0x0000,
48 (unsigned char *)vi, sizeof(struct version_info), 100);
51 sr_err("Unable to get version info: %s.",
52 libusb_error_name(ret));
59 static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid)
61 struct dev_context *devc = sdi->priv;
62 struct sr_usb_dev_inst *usb = sdi->conn;
63 libusb_device_handle *devhdl = usb->devhdl;
66 cmd = devc->dslogic ? DS_CMD_GET_REVID_VERSION : CMD_GET_REVID_VERSION;
67 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
68 LIBUSB_ENDPOINT_IN, cmd, 0x0000, 0x0000, revid, 1, 100);
71 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
78 SR_PRIV int fx2lafw_command_start_acquisition(const struct sr_dev_inst *sdi)
80 struct dev_context *devc;
81 struct sr_usb_dev_inst *usb;
83 struct cmd_start_acquisition cmd;
88 samplerate = devc->cur_samplerate;
90 /* Compute the sample rate. */
91 if (devc->sample_wide && samplerate > MAX_16BIT_SAMPLE_RATE) {
92 sr_err("Unable to sample at %" PRIu64 "Hz "
93 "when collecting 16-bit samples.", samplerate);
98 cmd.flags = cmd.sample_delay_h = cmd.sample_delay_l = 0;
99 if ((SR_MHZ(48) % samplerate) == 0) {
100 cmd.flags = CMD_START_FLAGS_CLK_48MHZ;
101 delay = SR_MHZ(48) / samplerate - 1;
102 if (delay > MAX_SAMPLE_DELAY)
106 if (delay == 0 && (SR_MHZ(30) % samplerate) == 0) {
107 cmd.flags = CMD_START_FLAGS_CLK_30MHZ;
108 delay = SR_MHZ(30) / samplerate - 1;
111 sr_dbg("GPIF delay = %d, clocksource = %sMHz.", delay,
112 (cmd.flags & CMD_START_FLAGS_CLK_48MHZ) ? "48" : "30");
114 if (delay <= 0 || delay > MAX_SAMPLE_DELAY) {
115 sr_err("Unable to sample at %" PRIu64 "Hz.", samplerate);
119 cmd.sample_delay_h = (delay >> 8) & 0xff;
120 cmd.sample_delay_l = delay & 0xff;
122 /* Select the sampling width. */
123 cmd.flags |= devc->sample_wide ? CMD_START_FLAGS_SAMPLE_16BIT :
124 CMD_START_FLAGS_SAMPLE_8BIT;
126 /* Send the control message. */
127 ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
128 LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000,
129 (unsigned char *)&cmd, sizeof(cmd), 100);
131 sr_err("Unable to send start command: %s.",
132 libusb_error_name(ret));
140 * Check the USB configuration to determine if this is an fx2lafw device.
142 * @return TRUE if the device's configuration profile matches fx2lafw
143 * configuration, FALSE otherwise.
145 SR_PRIV gboolean match_manuf_prod(libusb_device *dev, const char *manufacturer,
148 struct libusb_device_descriptor des;
149 struct libusb_device_handle *hdl;
151 unsigned char strdesc[64];
156 /* Assume the FW has not been loaded, unless proven wrong. */
157 if (libusb_get_device_descriptor(dev, &des) != 0)
160 if (libusb_open(dev, &hdl) != 0)
163 if (libusb_get_string_descriptor_ascii(hdl,
164 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
166 if (strcmp((const char *)strdesc, manufacturer))
169 if (libusb_get_string_descriptor_ascii(hdl,
170 des.iProduct, strdesc, sizeof(strdesc)) < 0)
172 if (strcmp((const char *)strdesc, product))
183 SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
185 libusb_device **devlist;
186 struct sr_usb_dev_inst *usb;
187 struct libusb_device_descriptor des;
188 struct dev_context *devc;
189 struct drv_context *drvc;
190 struct version_info vi;
191 int ret, i, device_count;
193 char connection_id[64];
199 if (sdi->status == SR_ST_ACTIVE)
200 /* Device is already in use. */
203 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
204 if (device_count < 0) {
205 sr_err("Failed to get device list: %s.",
206 libusb_error_name(device_count));
210 for (i = 0; i < device_count; i++) {
211 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
212 sr_err("Failed to get device descriptor: %s.",
213 libusb_error_name(ret));
217 if (des.idVendor != devc->profile->vid
218 || des.idProduct != devc->profile->pid)
221 if ((sdi->status == SR_ST_INITIALIZING) ||
222 (sdi->status == SR_ST_INACTIVE)) {
224 * Check device by its physical USB bus/port address.
226 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
227 if (strcmp(sdi->connection_id, connection_id))
228 /* This is not the one. */
232 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
233 if (usb->address == 0xff)
235 * First time we touch this device after FW
236 * upload, so we don't know the address yet.
238 usb->address = libusb_get_device_address(devlist[i]);
240 sr_err("Failed to open device: %s.",
241 libusb_error_name(ret));
245 ret = command_get_fw_version(usb->devhdl, &vi);
247 sr_err("Failed to get firmware version.");
251 ret = command_get_revid_version(sdi, &revid);
253 sr_err("Failed to get REVID.");
258 * Changes in major version mean incompatible/API changes, so
259 * bail out if we encounter an incompatible version.
260 * Different minor versions are OK, they should be compatible.
262 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
263 sr_err("Expected firmware version %d.x, "
264 "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
269 sdi->status = SR_ST_ACTIVE;
270 sr_info("Opened device on %d.%d (logical) / %s (physical), "
271 "interface %d, firmware %d.%d.",
272 usb->bus, usb->address, connection_id,
273 USB_INTERFACE, vi.major, vi.minor);
275 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
276 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
280 libusb_free_device_list(devlist, 1);
282 if (sdi->status != SR_ST_ACTIVE)
288 SR_PRIV struct dev_context *fx2lafw_dev_new(void)
290 struct dev_context *devc;
292 devc = g_malloc0(sizeof(struct dev_context));
293 devc->profile = NULL;
294 devc->fw_updated = 0;
295 devc->cur_samplerate = 0;
296 devc->limit_samples = 0;
297 devc->capture_ratio = 0;
298 devc->sample_wide = FALSE;
304 SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc)
308 devc->acq_aborted = TRUE;
310 for (i = devc->num_transfers - 1; i >= 0; i--) {
311 if (devc->transfers[i])
312 libusb_cancel_transfer(devc->transfers[i]);
316 static void finish_acquisition(struct sr_dev_inst *sdi)
318 struct sr_datafeed_packet packet;
319 struct dev_context *devc;
323 /* Terminate session. */
324 packet.type = SR_DF_END;
325 sr_session_send(sdi, &packet);
327 /* Remove fds from polling. */
328 usb_source_remove(sdi->session, devc->ctx);
330 devc->num_transfers = 0;
331 g_free(devc->transfers);
334 soft_trigger_logic_free(devc->stl);
339 static void free_transfer(struct libusb_transfer *transfer)
341 struct sr_dev_inst *sdi;
342 struct dev_context *devc;
345 sdi = transfer->user_data;
348 g_free(transfer->buffer);
349 transfer->buffer = NULL;
350 libusb_free_transfer(transfer);
352 for (i = 0; i < devc->num_transfers; i++) {
353 if (devc->transfers[i] == transfer) {
354 devc->transfers[i] = NULL;
359 devc->submitted_transfers--;
360 if (devc->submitted_transfers == 0)
361 finish_acquisition(sdi);
364 static void resubmit_transfer(struct libusb_transfer *transfer)
368 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
371 sr_err("%s: %s", __func__, libusb_error_name(ret));
372 free_transfer(transfer);
376 SR_PRIV void fx2lafw_receive_transfer(struct libusb_transfer *transfer)
378 struct sr_dev_inst *sdi;
379 struct dev_context *devc;
380 gboolean packet_has_error = FALSE;
381 struct sr_datafeed_packet packet;
382 struct sr_datafeed_logic logic;
383 unsigned int num_samples;
384 int trigger_offset, cur_sample_count, unitsize;
385 int pre_trigger_samples;
387 sdi = transfer->user_data;
391 * If acquisition has already ended, just free any queued up
392 * transfer that come in.
394 if (devc->acq_aborted) {
395 free_transfer(transfer);
399 sr_dbg("receive_transfer(): status %d received %d bytes.",
400 transfer->status, transfer->actual_length);
402 /* Save incoming transfer before reusing the transfer struct. */
403 unitsize = devc->sample_wide ? 2 : 1;
404 cur_sample_count = transfer->actual_length / unitsize;
406 switch (transfer->status) {
407 case LIBUSB_TRANSFER_NO_DEVICE:
408 fx2lafw_abort_acquisition(devc);
409 free_transfer(transfer);
411 case LIBUSB_TRANSFER_COMPLETED:
412 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
415 packet_has_error = TRUE;
419 if (transfer->actual_length == 0 || packet_has_error) {
420 devc->empty_transfer_count++;
421 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
423 * The FX2 gave up. End the acquisition, the frontend
424 * will work out that the samplecount is short.
426 fx2lafw_abort_acquisition(devc);
427 free_transfer(transfer);
429 resubmit_transfer(transfer);
433 devc->empty_transfer_count = 0;
436 if (devc->trigger_fired) {
437 if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) {
438 /* Send the incoming transfer to the session bus. */
439 packet.type = SR_DF_LOGIC;
440 packet.payload = &logic;
441 if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples)
442 num_samples = devc->limit_samples - devc->sent_samples;
444 num_samples = cur_sample_count;
445 logic.length = num_samples * unitsize;
446 logic.unitsize = unitsize;
447 logic.data = transfer->buffer;
448 sr_session_send(devc->cb_data, &packet);
449 devc->sent_samples += num_samples;
452 trigger_offset = soft_trigger_logic_check(devc->stl,
453 transfer->buffer, transfer->actual_length, &pre_trigger_samples);
454 if (trigger_offset > -1) {
455 devc->sent_samples += pre_trigger_samples;
456 packet.type = SR_DF_LOGIC;
457 packet.payload = &logic;
458 num_samples = cur_sample_count - trigger_offset;
459 if (devc->limit_samples &&
460 num_samples > devc->limit_samples - devc->sent_samples)
461 num_samples = devc->limit_samples - devc->sent_samples;
462 logic.length = num_samples * unitsize;
463 logic.unitsize = unitsize;
464 logic.data = transfer->buffer + trigger_offset * unitsize;
465 sr_session_send(devc->cb_data, &packet);
466 devc->sent_samples += num_samples;
468 devc->trigger_fired = TRUE;
472 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
473 fx2lafw_abort_acquisition(devc);
474 free_transfer(transfer);
476 resubmit_transfer(transfer);
479 static unsigned int to_bytes_per_ms(unsigned int samplerate)
481 return samplerate / 1000;
484 SR_PRIV size_t fx2lafw_get_buffer_size(struct dev_context *devc)
489 * The buffer should be large enough to hold 10ms of data and
492 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
493 return (s + 511) & ~511;
496 SR_PRIV unsigned int fx2lafw_get_number_of_transfers(struct dev_context *devc)
500 /* Total buffer size should be able to hold about 500ms of data. */
501 n = (500 * to_bytes_per_ms(devc->cur_samplerate) /
502 fx2lafw_get_buffer_size(devc));
504 if (n > NUM_SIMUL_TRANSFERS)
505 return NUM_SIMUL_TRANSFERS;
510 SR_PRIV unsigned int fx2lafw_get_timeout(struct dev_context *devc)
513 unsigned int timeout;
515 total_size = fx2lafw_get_buffer_size(devc) *
516 fx2lafw_get_number_of_transfers(devc);
517 timeout = total_size / to_bytes_per_ms(devc->cur_samplerate);
518 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */