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 #define USB_TIMEOUT 100
43 static int command_get_fw_version(libusb_device_handle *devhdl,
44 struct version_info *vi)
48 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
49 LIBUSB_ENDPOINT_IN, CMD_GET_FW_VERSION, 0x0000, 0x0000,
50 (unsigned char *)vi, sizeof(struct version_info), USB_TIMEOUT);
53 sr_err("Unable to get version info: %s.",
54 libusb_error_name(ret));
61 static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid)
63 struct dev_context *devc = sdi->priv;
64 struct sr_usb_dev_inst *usb = sdi->conn;
65 libusb_device_handle *devhdl = usb->devhdl;
68 cmd = devc->dslogic ? DS_CMD_GET_REVID_VERSION : CMD_GET_REVID_VERSION;
69 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
70 LIBUSB_ENDPOINT_IN, cmd, 0x0000, 0x0000, revid, 1, USB_TIMEOUT);
73 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
80 SR_PRIV int fx2lafw_command_start_acquisition(const struct sr_dev_inst *sdi)
82 struct dev_context *devc;
83 struct sr_usb_dev_inst *usb;
85 struct cmd_start_acquisition cmd;
90 samplerate = devc->cur_samplerate;
92 /* Compute the sample rate. */
93 if (devc->sample_wide && samplerate > MAX_16BIT_SAMPLE_RATE) {
94 sr_err("Unable to sample at %" PRIu64 "Hz "
95 "when collecting 16-bit samples.", samplerate);
100 cmd.flags = cmd.sample_delay_h = cmd.sample_delay_l = 0;
101 if ((SR_MHZ(48) % samplerate) == 0) {
102 cmd.flags = CMD_START_FLAGS_CLK_48MHZ;
103 delay = SR_MHZ(48) / samplerate - 1;
104 if (delay > MAX_SAMPLE_DELAY)
108 if (delay == 0 && (SR_MHZ(30) % samplerate) == 0) {
109 cmd.flags = CMD_START_FLAGS_CLK_30MHZ;
110 delay = SR_MHZ(30) / samplerate - 1;
113 sr_dbg("GPIF delay = %d, clocksource = %sMHz.", delay,
114 (cmd.flags & CMD_START_FLAGS_CLK_48MHZ) ? "48" : "30");
116 if (delay <= 0 || delay > MAX_SAMPLE_DELAY) {
117 sr_err("Unable to sample at %" PRIu64 "Hz.", samplerate);
121 cmd.sample_delay_h = (delay >> 8) & 0xff;
122 cmd.sample_delay_l = delay & 0xff;
124 /* Select the sampling width. */
125 cmd.flags |= devc->sample_wide ? CMD_START_FLAGS_SAMPLE_16BIT :
126 CMD_START_FLAGS_SAMPLE_8BIT;
128 /* Send the control message. */
129 ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
130 LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000,
131 (unsigned char *)&cmd, sizeof(cmd), USB_TIMEOUT);
133 sr_err("Unable to send start command: %s.",
134 libusb_error_name(ret));
142 * Check the USB configuration to determine if this is an fx2lafw device.
144 * @return TRUE if the device's configuration profile matches fx2lafw
145 * configuration, FALSE otherwise.
147 SR_PRIV gboolean match_manuf_prod(libusb_device *dev, const char *manufacturer,
150 struct libusb_device_descriptor des;
151 struct libusb_device_handle *hdl;
153 unsigned char strdesc[64];
158 /* Assume the FW has not been loaded, unless proven wrong. */
159 if (libusb_get_device_descriptor(dev, &des) != 0)
162 if (libusb_open(dev, &hdl) != 0)
165 if (libusb_get_string_descriptor_ascii(hdl,
166 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
168 if (strcmp((const char *)strdesc, manufacturer))
171 if (libusb_get_string_descriptor_ascii(hdl,
172 des.iProduct, strdesc, sizeof(strdesc)) < 0)
174 if (strcmp((const char *)strdesc, product))
185 SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
187 libusb_device **devlist;
188 struct sr_usb_dev_inst *usb;
189 struct libusb_device_descriptor des;
190 struct dev_context *devc;
191 struct drv_context *drvc;
192 struct version_info vi;
193 int ret, i, device_count;
195 char connection_id[64];
201 if (sdi->status == SR_ST_ACTIVE)
202 /* Device is already in use. */
205 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
206 if (device_count < 0) {
207 sr_err("Failed to get device list: %s.",
208 libusb_error_name(device_count));
212 for (i = 0; i < device_count; i++) {
213 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
214 sr_err("Failed to get device descriptor: %s.",
215 libusb_error_name(ret));
219 if (des.idVendor != devc->profile->vid
220 || des.idProduct != devc->profile->pid)
223 if ((sdi->status == SR_ST_INITIALIZING) ||
224 (sdi->status == SR_ST_INACTIVE)) {
226 * Check device by its physical USB bus/port address.
228 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
229 if (strcmp(sdi->connection_id, connection_id))
230 /* This is not the one. */
234 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
235 if (usb->address == 0xff)
237 * First time we touch this device after FW
238 * upload, so we don't know the address yet.
240 usb->address = libusb_get_device_address(devlist[i]);
242 sr_err("Failed to open device: %s.",
243 libusb_error_name(ret));
247 if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) {
248 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
249 if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
250 sr_err("Failed to detach kernel driver: %s.",
251 libusb_error_name(ret));
257 ret = command_get_fw_version(usb->devhdl, &vi);
259 sr_err("Failed to get firmware version.");
263 ret = command_get_revid_version(sdi, &revid);
265 sr_err("Failed to get REVID.");
270 * Changes in major version mean incompatible/API changes, so
271 * bail out if we encounter an incompatible version.
272 * Different minor versions are OK, they should be compatible.
274 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
275 sr_err("Expected firmware version %d.x, "
276 "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
281 sdi->status = SR_ST_ACTIVE;
282 sr_info("Opened device on %d.%d (logical) / %s (physical), "
283 "interface %d, firmware %d.%d.",
284 usb->bus, usb->address, connection_id,
285 USB_INTERFACE, vi.major, vi.minor);
287 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
288 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
292 libusb_free_device_list(devlist, 1);
294 if (sdi->status != SR_ST_ACTIVE)
300 SR_PRIV struct dev_context *fx2lafw_dev_new(void)
302 struct dev_context *devc;
304 devc = g_malloc0(sizeof(struct dev_context));
305 devc->profile = NULL;
306 devc->fw_updated = 0;
307 devc->cur_samplerate = 0;
308 devc->limit_samples = 0;
309 devc->capture_ratio = 0;
310 devc->sample_wide = FALSE;
316 SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc)
320 devc->acq_aborted = TRUE;
322 for (i = devc->num_transfers - 1; i >= 0; i--) {
323 if (devc->transfers[i])
324 libusb_cancel_transfer(devc->transfers[i]);
328 static void finish_acquisition(struct sr_dev_inst *sdi)
330 struct sr_datafeed_packet packet;
331 struct dev_context *devc;
335 /* Terminate session. */
336 packet.type = SR_DF_END;
337 sr_session_send(sdi, &packet);
339 /* Remove fds from polling. */
340 usb_source_remove(sdi->session, devc->ctx);
342 devc->num_transfers = 0;
343 g_free(devc->transfers);
346 soft_trigger_logic_free(devc->stl);
351 static void free_transfer(struct libusb_transfer *transfer)
353 struct sr_dev_inst *sdi;
354 struct dev_context *devc;
357 sdi = transfer->user_data;
360 g_free(transfer->buffer);
361 transfer->buffer = NULL;
362 libusb_free_transfer(transfer);
364 for (i = 0; i < devc->num_transfers; i++) {
365 if (devc->transfers[i] == transfer) {
366 devc->transfers[i] = NULL;
371 devc->submitted_transfers--;
372 if (devc->submitted_transfers == 0)
373 finish_acquisition(sdi);
376 static void resubmit_transfer(struct libusb_transfer *transfer)
380 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
383 sr_err("%s: %s", __func__, libusb_error_name(ret));
384 free_transfer(transfer);
388 SR_PRIV void LIBUSB_CALL fx2lafw_receive_transfer(struct libusb_transfer *transfer)
390 struct sr_dev_inst *sdi;
391 struct dev_context *devc;
392 gboolean packet_has_error = FALSE;
393 struct sr_datafeed_packet packet;
394 struct sr_datafeed_logic logic;
395 unsigned int num_samples;
396 int trigger_offset, cur_sample_count, unitsize;
397 int pre_trigger_samples;
399 sdi = transfer->user_data;
403 * If acquisition has already ended, just free any queued up
404 * transfer that come in.
406 if (devc->acq_aborted) {
407 free_transfer(transfer);
411 sr_dbg("receive_transfer(): status %s received %d bytes.",
412 libusb_error_name(transfer->status), transfer->actual_length);
414 /* Save incoming transfer before reusing the transfer struct. */
415 unitsize = devc->sample_wide ? 2 : 1;
416 cur_sample_count = transfer->actual_length / unitsize;
418 switch (transfer->status) {
419 case LIBUSB_TRANSFER_NO_DEVICE:
420 fx2lafw_abort_acquisition(devc);
421 free_transfer(transfer);
423 case LIBUSB_TRANSFER_COMPLETED:
424 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
427 packet_has_error = TRUE;
431 if (transfer->actual_length == 0 || packet_has_error) {
432 devc->empty_transfer_count++;
433 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
435 * The FX2 gave up. End the acquisition, the frontend
436 * will work out that the samplecount is short.
438 fx2lafw_abort_acquisition(devc);
439 free_transfer(transfer);
441 resubmit_transfer(transfer);
445 devc->empty_transfer_count = 0;
448 if (devc->trigger_fired) {
449 if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) {
450 /* Send the incoming transfer to the session bus. */
451 packet.type = SR_DF_LOGIC;
452 packet.payload = &logic;
453 if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples)
454 num_samples = devc->limit_samples - devc->sent_samples;
456 num_samples = cur_sample_count;
457 logic.length = num_samples * unitsize;
458 logic.unitsize = unitsize;
459 logic.data = transfer->buffer;
460 sr_session_send(devc->cb_data, &packet);
461 devc->sent_samples += num_samples;
464 trigger_offset = soft_trigger_logic_check(devc->stl,
465 transfer->buffer, transfer->actual_length, &pre_trigger_samples);
466 if (trigger_offset > -1) {
467 devc->sent_samples += pre_trigger_samples;
468 packet.type = SR_DF_LOGIC;
469 packet.payload = &logic;
470 num_samples = cur_sample_count - trigger_offset;
471 if (devc->limit_samples &&
472 num_samples > devc->limit_samples - devc->sent_samples)
473 num_samples = devc->limit_samples - devc->sent_samples;
474 logic.length = num_samples * unitsize;
475 logic.unitsize = unitsize;
476 logic.data = transfer->buffer + trigger_offset * unitsize;
477 sr_session_send(devc->cb_data, &packet);
478 devc->sent_samples += num_samples;
480 devc->trigger_fired = TRUE;
484 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
485 fx2lafw_abort_acquisition(devc);
486 free_transfer(transfer);
488 resubmit_transfer(transfer);
491 static unsigned int to_bytes_per_ms(unsigned int samplerate)
493 return samplerate / 1000;
496 SR_PRIV size_t fx2lafw_get_buffer_size(struct dev_context *devc)
501 * The buffer should be large enough to hold 10ms of data and
504 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
505 return (s + 511) & ~511;
508 SR_PRIV unsigned int fx2lafw_get_number_of_transfers(struct dev_context *devc)
512 /* Total buffer size should be able to hold about 500ms of data. */
513 n = (500 * to_bytes_per_ms(devc->cur_samplerate) /
514 fx2lafw_get_buffer_size(devc));
516 if (n > NUM_SIMUL_TRANSFERS)
517 return NUM_SIMUL_TRANSFERS;
522 SR_PRIV unsigned int fx2lafw_get_timeout(struct dev_context *devc)
525 unsigned int timeout;
527 total_size = fx2lafw_get_buffer_size(devc) *
528 fx2lafw_get_number_of_transfers(devc);
529 timeout = total_size / to_bytes_per_ms(devc->cur_samplerate);
530 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */