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/>.
23 #include <glib/gstdio.h>
34 struct cmd_start_acquisition {
36 uint8_t sample_delay_h;
37 uint8_t sample_delay_l;
42 #define USB_TIMEOUT 100
44 static int command_get_fw_version(libusb_device_handle *devhdl,
45 struct version_info *vi)
49 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
50 LIBUSB_ENDPOINT_IN, DS_CMD_GET_FW_VERSION, 0x0000, 0x0000,
51 (unsigned char *)vi, sizeof(struct version_info), USB_TIMEOUT);
54 sr_err("Unable to get version info: %s.",
55 libusb_error_name(ret));
62 static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid)
64 struct sr_usb_dev_inst *usb = sdi->conn;
65 libusb_device_handle *devhdl = usb->devhdl;
68 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
69 LIBUSB_ENDPOINT_IN, DS_CMD_GET_REVID_VERSION, 0x0000, 0x0000,
70 revid, 1, USB_TIMEOUT);
73 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
80 SR_PRIV int dslogic_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
82 libusb_device **devlist;
83 struct sr_usb_dev_inst *usb;
84 struct libusb_device_descriptor des;
85 struct dev_context *devc;
86 struct drv_context *drvc;
87 struct version_info vi;
88 int ret, i, device_count;
90 char connection_id[64];
96 if (sdi->status == SR_ST_ACTIVE)
97 /* Device is already in use. */
100 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
101 if (device_count < 0) {
102 sr_err("Failed to get device list: %s.",
103 libusb_error_name(device_count));
107 for (i = 0; i < device_count; i++) {
108 libusb_get_device_descriptor(devlist[i], &des);
110 if (des.idVendor != devc->profile->vid
111 || des.idProduct != devc->profile->pid)
114 if ((sdi->status == SR_ST_INITIALIZING) ||
115 (sdi->status == SR_ST_INACTIVE)) {
117 * Check device by its physical USB bus/port address.
119 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
120 if (strcmp(sdi->connection_id, connection_id))
121 /* This is not the one. */
125 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
126 if (usb->address == 0xff)
128 * First time we touch this device after FW
129 * upload, so we don't know the address yet.
131 usb->address = libusb_get_device_address(devlist[i]);
133 sr_err("Failed to open device: %s.",
134 libusb_error_name(ret));
138 if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) {
139 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
140 if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
141 sr_err("Failed to detach kernel driver: %s.",
142 libusb_error_name(ret));
148 ret = command_get_fw_version(usb->devhdl, &vi);
150 sr_err("Failed to get firmware version.");
154 ret = command_get_revid_version(sdi, &revid);
156 sr_err("Failed to get REVID.");
161 * Changes in major version mean incompatible/API changes, so
162 * bail out if we encounter an incompatible version.
163 * Different minor versions are OK, they should be compatible.
165 if (vi.major != DSLOGIC_REQUIRED_VERSION_MAJOR) {
166 sr_err("Expected firmware version %d.x, "
167 "got %d.%d.", DSLOGIC_REQUIRED_VERSION_MAJOR,
172 sdi->status = SR_ST_ACTIVE;
173 sr_info("Opened device on %d.%d (logical) / %s (physical), "
174 "interface %d, firmware %d.%d.",
175 usb->bus, usb->address, connection_id,
176 USB_INTERFACE, vi.major, vi.minor);
178 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
179 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
183 libusb_free_device_list(devlist, 1);
185 if (sdi->status != SR_ST_ACTIVE)
191 SR_PRIV struct dev_context *dslogic_dev_new(void)
193 struct dev_context *devc;
195 devc = g_malloc0(sizeof(struct dev_context));
196 devc->profile = NULL;
197 devc->fw_updated = 0;
198 devc->cur_samplerate = 0;
199 devc->limit_samples = 0;
200 devc->capture_ratio = 0;
201 devc->continuous_mode = FALSE;
202 devc->clock_edge = DS_EDGE_RISING;
207 SR_PRIV void dslogic_abort_acquisition(struct dev_context *devc)
211 devc->acq_aborted = TRUE;
213 for (i = devc->num_transfers - 1; i >= 0; i--) {
214 if (devc->transfers[i])
215 libusb_cancel_transfer(devc->transfers[i]);
219 static void finish_acquisition(struct sr_dev_inst *sdi)
221 struct dev_context *devc;
225 std_session_send_df_end(sdi);
227 usb_source_remove(sdi->session, devc->ctx);
229 devc->num_transfers = 0;
230 g_free(devc->transfers);
233 static void free_transfer(struct libusb_transfer *transfer)
235 struct sr_dev_inst *sdi;
236 struct dev_context *devc;
239 sdi = transfer->user_data;
242 g_free(transfer->buffer);
243 transfer->buffer = NULL;
244 libusb_free_transfer(transfer);
246 for (i = 0; i < devc->num_transfers; i++) {
247 if (devc->transfers[i] == transfer) {
248 devc->transfers[i] = NULL;
253 devc->submitted_transfers--;
254 if (devc->submitted_transfers == 0)
255 finish_acquisition(sdi);
258 static void resubmit_transfer(struct libusb_transfer *transfer)
262 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
265 sr_err("%s: %s", __func__, libusb_error_name(ret));
266 free_transfer(transfer);
270 SR_PRIV void dslogic_send_data(struct sr_dev_inst *sdi,
271 uint8_t *data, size_t length, size_t sample_width)
273 const struct sr_datafeed_logic logic = {
275 .unitsize = sample_width,
279 const struct sr_datafeed_packet packet = {
284 sr_session_send(sdi, &packet);
287 SR_PRIV void LIBUSB_CALL dslogic_receive_transfer(struct libusb_transfer *transfer)
289 struct sr_dev_inst *sdi;
290 struct dev_context *devc;
291 gboolean packet_has_error = FALSE;
292 struct sr_datafeed_packet packet;
293 unsigned int num_samples;
294 int trigger_offset, cur_sample_count;
295 const int unitsize = 2;
297 sdi = transfer->user_data;
301 * If acquisition has already ended, just free any queued up
302 * transfer that come in.
304 if (devc->acq_aborted) {
305 free_transfer(transfer);
309 sr_dbg("receive_transfer(): status %s received %d bytes.",
310 libusb_error_name(transfer->status), transfer->actual_length);
312 /* Save incoming transfer before reusing the transfer struct. */
313 cur_sample_count = transfer->actual_length / unitsize;
315 switch (transfer->status) {
316 case LIBUSB_TRANSFER_NO_DEVICE:
317 dslogic_abort_acquisition(devc);
318 free_transfer(transfer);
320 case LIBUSB_TRANSFER_COMPLETED:
321 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
324 packet_has_error = TRUE;
328 if (transfer->actual_length == 0 || packet_has_error) {
329 devc->empty_transfer_count++;
330 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
332 * The FX2 gave up. End the acquisition, the frontend
333 * will work out that the samplecount is short.
335 dslogic_abort_acquisition(devc);
336 free_transfer(transfer);
338 resubmit_transfer(transfer);
342 devc->empty_transfer_count = 0;
344 if (devc->trigger_fired) {
345 if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) {
346 /* Send the incoming transfer to the session bus. */
347 if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples)
348 num_samples = devc->limit_samples - devc->sent_samples;
350 num_samples = cur_sample_count;
352 if (devc->trigger_pos > devc->sent_samples
353 && devc->trigger_pos <= devc->sent_samples + num_samples) {
354 /* DSLogic trigger in this block. Send trigger position. */
355 trigger_offset = devc->trigger_pos - devc->sent_samples;
356 /* Pre-trigger samples. */
357 dslogic_send_data(sdi, (uint8_t *)transfer->buffer,
358 trigger_offset * unitsize, unitsize);
359 devc->sent_samples += trigger_offset;
360 /* Trigger position. */
361 devc->trigger_pos = 0;
362 packet.type = SR_DF_TRIGGER;
363 packet.payload = NULL;
364 sr_session_send(sdi, &packet);
365 /* Post trigger samples. */
366 num_samples -= trigger_offset;
367 dslogic_send_data(sdi, (uint8_t *)transfer->buffer
368 + trigger_offset * unitsize, num_samples * unitsize, unitsize);
369 devc->sent_samples += num_samples;
371 dslogic_send_data(sdi, (uint8_t *)transfer->buffer,
372 num_samples * unitsize, unitsize);
373 devc->sent_samples += num_samples;
378 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
379 dslogic_abort_acquisition(devc);
380 free_transfer(transfer);
382 resubmit_transfer(transfer);
385 static unsigned int to_bytes_per_ms(unsigned int samplerate)
387 return samplerate / 1000;
390 SR_PRIV size_t dslogic_get_buffer_size(struct dev_context *devc)
395 * The buffer should be large enough to hold 10ms of data and
398 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
399 return (s + 511) & ~511;
402 SR_PRIV unsigned int dslogic_get_timeout(struct dev_context *devc)
405 unsigned int timeout;
407 total_size = dslogic_get_buffer_size(devc) *
408 dslogic_get_number_of_transfers(devc);
409 timeout = total_size / to_bytes_per_ms(devc->cur_samplerate);
410 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */