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>
32 struct cmd_start_acquisition {
34 uint8_t sample_delay_h;
35 uint8_t sample_delay_l;
40 static int command_get_fw_version(libusb_device_handle *devhdl,
41 struct version_info *vi)
45 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
46 LIBUSB_ENDPOINT_IN, CMD_GET_FW_VERSION, 0x0000, 0x0000,
47 (unsigned char *)vi, sizeof(struct version_info), 100);
50 sr_err("Unable to get version info: %s.",
51 libusb_error_name(ret));
58 static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid)
60 struct dev_context *devc = sdi->priv;
61 struct sr_usb_dev_inst *usb = sdi->conn;
62 libusb_device_handle *devhdl = usb->devhdl;
65 cmd = devc->dslogic ? CMD_DSLOGIC_GET_REVID_VERSION : CMD_GET_REVID_VERSION;
66 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
67 LIBUSB_ENDPOINT_IN, cmd, 0x0000, 0x0000, revid, 1, 100);
70 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
77 SR_PRIV int fx2lafw_command_start_acquisition(const struct sr_dev_inst *sdi)
79 struct dev_context *devc;
80 struct sr_usb_dev_inst *usb;
82 struct cmd_start_acquisition cmd;
87 samplerate = devc->cur_samplerate;
89 /* Compute the sample rate. */
90 if (devc->sample_wide && samplerate > MAX_16BIT_SAMPLE_RATE) {
91 sr_err("Unable to sample at %" PRIu64 "Hz "
92 "when collecting 16-bit samples.", samplerate);
97 cmd.flags = cmd.sample_delay_h = cmd.sample_delay_l = 0;
98 if ((SR_MHZ(48) % samplerate) == 0) {
99 cmd.flags = CMD_START_FLAGS_CLK_48MHZ;
100 delay = SR_MHZ(48) / samplerate - 1;
101 if (delay > MAX_SAMPLE_DELAY)
105 if (delay == 0 && (SR_MHZ(30) % samplerate) == 0) {
106 cmd.flags = CMD_START_FLAGS_CLK_30MHZ;
107 delay = SR_MHZ(30) / samplerate - 1;
110 sr_info("GPIF delay = %d, clocksource = %sMHz.", delay,
111 (cmd.flags & CMD_START_FLAGS_CLK_48MHZ) ? "48" : "30");
113 if (delay <= 0 || delay > MAX_SAMPLE_DELAY) {
114 sr_err("Unable to sample at %" PRIu64 "Hz.", samplerate);
118 cmd.sample_delay_h = (delay >> 8) & 0xff;
119 cmd.sample_delay_l = delay & 0xff;
121 /* Select the sampling width. */
122 cmd.flags |= devc->sample_wide ? CMD_START_FLAGS_SAMPLE_16BIT :
123 CMD_START_FLAGS_SAMPLE_8BIT;
125 /* Send the control message. */
126 ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
127 LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000,
128 (unsigned char *)&cmd, sizeof(cmd), 100);
130 sr_err("Unable to send start command: %s.",
131 libusb_error_name(ret));
139 * Check the USB configuration to determine if this is an fx2lafw device.
141 * @return TRUE if the device's configuration profile matches fx2lafw
142 * configuration, FALSE otherwise.
144 SR_PRIV gboolean match_manuf_prod(libusb_device *dev, const char *manufacturer,
147 struct libusb_device_descriptor des;
148 struct libusb_device_handle *hdl;
150 unsigned char strdesc[64];
155 /* Assume the FW has not been loaded, unless proven wrong. */
156 if (libusb_get_device_descriptor(dev, &des) != 0)
159 if (libusb_open(dev, &hdl) != 0)
162 if (libusb_get_string_descriptor_ascii(hdl,
163 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
165 if (strncmp((const char *)strdesc, manufacturer, 6))
168 if (libusb_get_string_descriptor_ascii(hdl,
169 des.iProduct, strdesc, sizeof(strdesc)) < 0)
171 if (strncmp((const char *)strdesc, product, 7))
182 SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
184 libusb_device **devlist;
185 struct sr_usb_dev_inst *usb;
186 struct libusb_device_descriptor des;
187 struct dev_context *devc;
188 struct drv_context *drvc;
189 struct version_info vi;
190 int ret, i, device_count;
192 char connection_id[64];
198 if (sdi->status == SR_ST_ACTIVE)
199 /* Device is already in use. */
202 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
203 if (device_count < 0) {
204 sr_err("Failed to get device list: %s.",
205 libusb_error_name(device_count));
209 for (i = 0; i < device_count; i++) {
210 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
211 sr_err("Failed to get device descriptor: %s.",
212 libusb_error_name(ret));
216 if (des.idVendor != devc->profile->vid
217 || des.idProduct != devc->profile->pid)
220 if ((sdi->status == SR_ST_INITIALIZING) ||
221 (sdi->status == SR_ST_INACTIVE)) {
223 * Check device by its physical USB bus/port address.
225 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
226 if (strcmp(sdi->connection_id, connection_id))
227 /* This is not the one. */
231 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
232 if (usb->address == 0xff)
234 * First time we touch this device after FW
235 * upload, so we don't know the address yet.
237 usb->address = libusb_get_device_address(devlist[i]);
239 sr_err("Failed to open device: %s.",
240 libusb_error_name(ret));
244 ret = command_get_fw_version(usb->devhdl, &vi);
246 sr_err("Failed to get firmware version.");
250 ret = command_get_revid_version(sdi, &revid);
252 sr_err("Failed to get REVID.");
257 * Changes in major version mean incompatible/API changes, so
258 * bail out if we encounter an incompatible version.
259 * Different minor versions are OK, they should be compatible.
261 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
262 sr_err("Expected firmware version %d.x, "
263 "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
268 sdi->status = SR_ST_ACTIVE;
269 sr_info("Opened device on %d.%d (logical) / %s (physical), "
270 "interface %d, firmware %d.%d.",
271 usb->bus, usb->address, connection_id,
272 USB_INTERFACE, vi.major, vi.minor);
274 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
275 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
279 libusb_free_device_list(devlist, 1);
281 if (sdi->status != SR_ST_ACTIVE)
287 SR_PRIV struct dev_context *fx2lafw_dev_new(void)
289 struct dev_context *devc;
291 devc = g_malloc0(sizeof(struct dev_context));
292 devc->profile = NULL;
293 devc->fw_updated = 0;
294 devc->cur_samplerate = 0;
295 devc->limit_samples = 0;
296 devc->capture_ratio = 0;
297 devc->sample_wide = FALSE;
303 SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc)
307 devc->acq_aborted = TRUE;
309 for (i = devc->num_transfers - 1; i >= 0; i--) {
310 if (devc->transfers[i])
311 libusb_cancel_transfer(devc->transfers[i]);
315 static void finish_acquisition(struct sr_dev_inst *sdi)
317 struct sr_datafeed_packet packet;
318 struct dev_context *devc;
322 /* Terminate session. */
323 packet.type = SR_DF_END;
324 sr_session_send(sdi, &packet);
326 /* Remove fds from polling. */
327 usb_source_remove(sdi->session, devc->ctx);
329 devc->num_transfers = 0;
330 g_free(devc->transfers);
333 soft_trigger_logic_free(devc->stl);
338 static void free_transfer(struct libusb_transfer *transfer)
340 struct sr_dev_inst *sdi;
341 struct dev_context *devc;
344 sdi = transfer->user_data;
347 g_free(transfer->buffer);
348 transfer->buffer = NULL;
349 libusb_free_transfer(transfer);
351 for (i = 0; i < devc->num_transfers; i++) {
352 if (devc->transfers[i] == transfer) {
353 devc->transfers[i] = NULL;
358 devc->submitted_transfers--;
359 if (devc->submitted_transfers == 0)
360 finish_acquisition(sdi);
363 static void resubmit_transfer(struct libusb_transfer *transfer)
367 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
370 sr_err("%s: %s", __func__, libusb_error_name(ret));
371 free_transfer(transfer);
375 SR_PRIV void fx2lafw_receive_transfer(struct libusb_transfer *transfer)
377 struct sr_dev_inst *sdi;
378 struct dev_context *devc;
379 gboolean packet_has_error = FALSE;
380 struct sr_datafeed_packet packet;
381 struct sr_datafeed_logic logic;
382 unsigned int num_samples;
383 int trigger_offset, cur_sample_count, unitsize;
384 int pre_trigger_samples;
386 sdi = transfer->user_data;
390 * If acquisition has already ended, just free any queued up
391 * transfer that come in.
393 if (devc->acq_aborted) {
394 free_transfer(transfer);
398 sr_info("receive_transfer(): status %d received %d bytes.",
399 transfer->status, transfer->actual_length);
401 /* Save incoming transfer before reusing the transfer struct. */
402 unitsize = devc->sample_wide ? 2 : 1;
403 cur_sample_count = transfer->actual_length / unitsize;
405 switch (transfer->status) {
406 case LIBUSB_TRANSFER_NO_DEVICE:
407 fx2lafw_abort_acquisition(devc);
408 free_transfer(transfer);
410 case LIBUSB_TRANSFER_COMPLETED:
411 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
414 packet_has_error = TRUE;
418 if (transfer->actual_length == 0 || packet_has_error) {
419 devc->empty_transfer_count++;
420 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
422 * The FX2 gave up. End the acquisition, the frontend
423 * will work out that the samplecount is short.
425 fx2lafw_abort_acquisition(devc);
426 free_transfer(transfer);
428 resubmit_transfer(transfer);
432 devc->empty_transfer_count = 0;
435 if (devc->trigger_fired) {
436 if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) {
437 /* Send the incoming transfer to the session bus. */
438 packet.type = SR_DF_LOGIC;
439 packet.payload = &logic;
440 if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples)
441 num_samples = devc->limit_samples - devc->sent_samples;
443 num_samples = cur_sample_count;
444 logic.length = num_samples * unitsize;
445 logic.unitsize = unitsize;
446 logic.data = transfer->buffer;
447 sr_session_send(devc->cb_data, &packet);
448 devc->sent_samples += num_samples;
451 trigger_offset = soft_trigger_logic_check(devc->stl,
452 transfer->buffer, transfer->actual_length, &pre_trigger_samples);
453 if (trigger_offset > -1) {
454 devc->sent_samples += pre_trigger_samples;
455 packet.type = SR_DF_LOGIC;
456 packet.payload = &logic;
457 num_samples = cur_sample_count - trigger_offset;
458 if (devc->limit_samples &&
459 num_samples > devc->limit_samples - devc->sent_samples)
460 num_samples = devc->limit_samples - devc->sent_samples;
461 logic.length = num_samples * unitsize;
462 logic.unitsize = unitsize;
463 logic.data = transfer->buffer + trigger_offset * unitsize;
464 sr_session_send(devc->cb_data, &packet);
465 devc->sent_samples += num_samples;
467 devc->trigger_fired = TRUE;
471 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
472 fx2lafw_abort_acquisition(devc);
473 free_transfer(transfer);
475 resubmit_transfer(transfer);
478 static unsigned int to_bytes_per_ms(unsigned int samplerate)
480 return samplerate / 1000;
483 SR_PRIV size_t fx2lafw_get_buffer_size(struct dev_context *devc)
488 * The buffer should be large enough to hold 10ms of data and
491 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
492 return (s + 511) & ~511;
495 SR_PRIV unsigned int fx2lafw_get_number_of_transfers(struct dev_context *devc)
499 /* Total buffer size should be able to hold about 500ms of data. */
500 n = (500 * to_bytes_per_ms(devc->cur_samplerate) /
501 fx2lafw_get_buffer_size(devc));
503 if (n > NUM_SIMUL_TRANSFERS)
504 return NUM_SIMUL_TRANSFERS;
509 SR_PRIV unsigned int fx2lafw_get_timeout(struct dev_context *devc)
512 unsigned int timeout;
514 total_size = fx2lafw_get_buffer_size(devc) *
515 fx2lafw_get_number_of_transfers(devc);
516 timeout = total_size / to_bytes_per_ms(devc->cur_samplerate);
517 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */