]> sigrok.org Git - libsigrok.git/blame - src/hardware/dslogic/protocol.c
dslogic: Updated matching of device with loaded firmware
[libsigrok.git] / src / hardware / dslogic / protocol.c
CommitLineData
adcb9951
JH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21#include <config.h>
22#include <glib.h>
23#include <glib/gstdio.h>
24#include "protocol.h"
25#include "dslogic.h"
26
27#pragma pack(push, 1)
28
29struct version_info {
30 uint8_t major;
31 uint8_t minor;
32};
33
34struct cmd_start_acquisition {
35 uint8_t flags;
36 uint8_t sample_delay_h;
37 uint8_t sample_delay_l;
38};
39
40#pragma pack(pop)
41
42#define USB_TIMEOUT 100
43
44static int command_get_fw_version(libusb_device_handle *devhdl,
45 struct version_info *vi)
46{
47 int ret;
48
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);
52
53 if (ret < 0) {
54 sr_err("Unable to get version info: %s.",
55 libusb_error_name(ret));
56 return SR_ERR;
57 }
58
59 return SR_OK;
60}
61
62static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid)
63{
64 struct sr_usb_dev_inst *usb = sdi->conn;
65 libusb_device_handle *devhdl = usb->devhdl;
66 int ret;
67
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);
71
72 if (ret < 0) {
73 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
74 return SR_ERR;
75 }
76
77 return SR_OK;
78}
79
80SR_PRIV int dslogic_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
81{
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;
89 uint8_t revid;
90 char connection_id[64];
91
92 drvc = di->context;
93 devc = sdi->priv;
94 usb = sdi->conn;
95
96 if (sdi->status == SR_ST_ACTIVE)
97 /* Device is already in use. */
98 return SR_ERR;
99
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));
104 return SR_ERR;
105 }
106
107 for (i = 0; i < device_count; i++) {
108 libusb_get_device_descriptor(devlist[i], &des);
109
110 if (des.idVendor != devc->profile->vid
111 || des.idProduct != devc->profile->pid)
112 continue;
113
114 if ((sdi->status == SR_ST_INITIALIZING) ||
115 (sdi->status == SR_ST_INACTIVE)) {
116 /*
117 * Check device by its physical USB bus/port address.
118 */
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. */
122 continue;
123 }
124
125 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
126 if (usb->address == 0xff)
127 /*
128 * First time we touch this device after FW
129 * upload, so we don't know the address yet.
130 */
131 usb->address = libusb_get_device_address(devlist[i]);
132 } else {
133 sr_err("Failed to open device: %s.",
134 libusb_error_name(ret));
135 break;
136 }
137
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));
143 return SR_ERR;
144 }
145 }
146 }
147
148 ret = command_get_fw_version(usb->devhdl, &vi);
149 if (ret != SR_OK) {
150 sr_err("Failed to get firmware version.");
151 break;
152 }
153
154 ret = command_get_revid_version(sdi, &revid);
155 if (ret != SR_OK) {
156 sr_err("Failed to get REVID.");
157 break;
158 }
159
160 /*
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.
164 */
165 if (vi.major != DSLOGIC_REQUIRED_VERSION_MAJOR) {
166 sr_err("Expected firmware version %d.x, "
167 "got %d.%d.", DSLOGIC_REQUIRED_VERSION_MAJOR,
168 vi.major, vi.minor);
169 break;
170 }
171
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);
177
178 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
179 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
180
181 break;
182 }
183 libusb_free_device_list(devlist, 1);
184
185 if (sdi->status != SR_ST_ACTIVE)
186 return SR_ERR;
187
188 return SR_OK;
189}
190
191SR_PRIV struct dev_context *dslogic_dev_new(void)
192{
193 struct dev_context *devc;
194
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;
203
204 return devc;
205}
206
207SR_PRIV void dslogic_abort_acquisition(struct dev_context *devc)
208{
209 int i;
210
211 devc->acq_aborted = TRUE;
212
213 for (i = devc->num_transfers - 1; i >= 0; i--) {
214 if (devc->transfers[i])
215 libusb_cancel_transfer(devc->transfers[i]);
216 }
217}
218
219static void finish_acquisition(struct sr_dev_inst *sdi)
220{
221 struct dev_context *devc;
222
223 devc = sdi->priv;
224
225 std_session_send_df_end(sdi);
226
227 usb_source_remove(sdi->session, devc->ctx);
228
229 devc->num_transfers = 0;
230 g_free(devc->transfers);
231}
232
233static void free_transfer(struct libusb_transfer *transfer)
234{
235 struct sr_dev_inst *sdi;
236 struct dev_context *devc;
237 unsigned int i;
238
239 sdi = transfer->user_data;
240 devc = sdi->priv;
241
242 g_free(transfer->buffer);
243 transfer->buffer = NULL;
244 libusb_free_transfer(transfer);
245
246 for (i = 0; i < devc->num_transfers; i++) {
247 if (devc->transfers[i] == transfer) {
248 devc->transfers[i] = NULL;
249 break;
250 }
251 }
252
253 devc->submitted_transfers--;
254 if (devc->submitted_transfers == 0)
255 finish_acquisition(sdi);
256}
257
258static void resubmit_transfer(struct libusb_transfer *transfer)
259{
260 int ret;
261
262 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
263 return;
264
265 sr_err("%s: %s", __func__, libusb_error_name(ret));
266 free_transfer(transfer);
267
268}
269
270SR_PRIV void dslogic_send_data(struct sr_dev_inst *sdi,
271 uint8_t *data, size_t length, size_t sample_width)
272{
273 const struct sr_datafeed_logic logic = {
274 .length = length,
275 .unitsize = sample_width,
276 .data = data
277 };
278
279 const struct sr_datafeed_packet packet = {
280 .type = SR_DF_LOGIC,
281 .payload = &logic
282 };
283
284 sr_session_send(sdi, &packet);
285}
286
287SR_PRIV void LIBUSB_CALL dslogic_receive_transfer(struct libusb_transfer *transfer)
288{
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;
296
297 sdi = transfer->user_data;
298 devc = sdi->priv;
299
300 /*
301 * If acquisition has already ended, just free any queued up
302 * transfer that come in.
303 */
304 if (devc->acq_aborted) {
305 free_transfer(transfer);
306 return;
307 }
308
309 sr_dbg("receive_transfer(): status %s received %d bytes.",
310 libusb_error_name(transfer->status), transfer->actual_length);
311
312 /* Save incoming transfer before reusing the transfer struct. */
313 cur_sample_count = transfer->actual_length / unitsize;
314
315 switch (transfer->status) {
316 case LIBUSB_TRANSFER_NO_DEVICE:
317 dslogic_abort_acquisition(devc);
318 free_transfer(transfer);
319 return;
320 case LIBUSB_TRANSFER_COMPLETED:
321 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
322 break;
323 default:
324 packet_has_error = TRUE;
325 break;
326 }
327
328 if (transfer->actual_length == 0 || packet_has_error) {
329 devc->empty_transfer_count++;
330 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
331 /*
332 * The FX2 gave up. End the acquisition, the frontend
333 * will work out that the samplecount is short.
334 */
335 dslogic_abort_acquisition(devc);
336 free_transfer(transfer);
337 } else {
338 resubmit_transfer(transfer);
339 }
340 return;
341 } else {
342 devc->empty_transfer_count = 0;
343 }
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;
349 else
350 num_samples = cur_sample_count;
351
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;
370 } else {
371 dslogic_send_data(sdi, (uint8_t *)transfer->buffer,
372 num_samples * unitsize, unitsize);
373 devc->sent_samples += num_samples;
374 }
375 }
376 }
377
378 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
379 dslogic_abort_acquisition(devc);
380 free_transfer(transfer);
381 } else
382 resubmit_transfer(transfer);
383}
384
385static unsigned int to_bytes_per_ms(unsigned int samplerate)
386{
387 return samplerate / 1000;
388}
389
390SR_PRIV size_t dslogic_get_buffer_size(struct dev_context *devc)
391{
392 size_t s;
393
394 /*
395 * The buffer should be large enough to hold 10ms of data and
396 * a multiple of 512.
397 */
398 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
399 return (s + 511) & ~511;
400}
401
402SR_PRIV unsigned int dslogic_get_timeout(struct dev_context *devc)
403{
404 size_t total_size;
405 unsigned int timeout;
406
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. */
411}