]> sigrok.org Git - libsigrok.git/blame - src/hardware/dslogic/protocol.c
fx2lafw: Moved all protocol handling to protocol.c
[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
1ee70746
JH
80SR_PRIV int dslogic_set_voltage_threshold(const struct sr_dev_inst *sdi, double threshold)
81{
82 int ret;
83 struct dev_context *const devc = sdi->priv;
84 const struct sr_usb_dev_inst *const usb = sdi->conn;
85 const uint8_t value = (threshold / 5.0) * 255;
86 const uint16_t cmd = value | (DS_ADDR_VTH << 8);
87
88 /* Send the control command. */
89 ret = libusb_control_transfer(usb->devhdl,
90 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT,
91 DS_CMD_WR_REG, 0x0000, 0x0000,
92 (unsigned char *)&cmd, sizeof(cmd), 3000);
93 if (ret < 0) {
94 sr_err("Unable to set voltage-threshold register: %s.",
95 libusb_error_name(ret));
96 return SR_ERR;
97 }
98
99 devc->cur_threshold = threshold;
100
101 return SR_OK;
102}
103
adcb9951
JH
104SR_PRIV int dslogic_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
105{
106 libusb_device **devlist;
107 struct sr_usb_dev_inst *usb;
108 struct libusb_device_descriptor des;
109 struct dev_context *devc;
110 struct drv_context *drvc;
111 struct version_info vi;
112 int ret, i, device_count;
113 uint8_t revid;
114 char connection_id[64];
115
116 drvc = di->context;
117 devc = sdi->priv;
118 usb = sdi->conn;
119
120 if (sdi->status == SR_ST_ACTIVE)
121 /* Device is already in use. */
122 return SR_ERR;
123
124 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
125 if (device_count < 0) {
126 sr_err("Failed to get device list: %s.",
127 libusb_error_name(device_count));
128 return SR_ERR;
129 }
130
131 for (i = 0; i < device_count; i++) {
132 libusb_get_device_descriptor(devlist[i], &des);
133
134 if (des.idVendor != devc->profile->vid
135 || des.idProduct != devc->profile->pid)
136 continue;
137
138 if ((sdi->status == SR_ST_INITIALIZING) ||
139 (sdi->status == SR_ST_INACTIVE)) {
140 /*
141 * Check device by its physical USB bus/port address.
142 */
143 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
144 if (strcmp(sdi->connection_id, connection_id))
145 /* This is not the one. */
146 continue;
147 }
148
149 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
150 if (usb->address == 0xff)
151 /*
152 * First time we touch this device after FW
153 * upload, so we don't know the address yet.
154 */
155 usb->address = libusb_get_device_address(devlist[i]);
156 } else {
157 sr_err("Failed to open device: %s.",
158 libusb_error_name(ret));
159 break;
160 }
161
162 if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) {
163 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
164 if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
165 sr_err("Failed to detach kernel driver: %s.",
166 libusb_error_name(ret));
167 return SR_ERR;
168 }
169 }
170 }
171
172 ret = command_get_fw_version(usb->devhdl, &vi);
173 if (ret != SR_OK) {
174 sr_err("Failed to get firmware version.");
175 break;
176 }
177
178 ret = command_get_revid_version(sdi, &revid);
179 if (ret != SR_OK) {
180 sr_err("Failed to get REVID.");
181 break;
182 }
183
184 /*
185 * Changes in major version mean incompatible/API changes, so
186 * bail out if we encounter an incompatible version.
187 * Different minor versions are OK, they should be compatible.
188 */
189 if (vi.major != DSLOGIC_REQUIRED_VERSION_MAJOR) {
190 sr_err("Expected firmware version %d.x, "
191 "got %d.%d.", DSLOGIC_REQUIRED_VERSION_MAJOR,
192 vi.major, vi.minor);
193 break;
194 }
195
196 sdi->status = SR_ST_ACTIVE;
197 sr_info("Opened device on %d.%d (logical) / %s (physical), "
198 "interface %d, firmware %d.%d.",
199 usb->bus, usb->address, connection_id,
200 USB_INTERFACE, vi.major, vi.minor);
201
202 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
203 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
204
205 break;
206 }
207 libusb_free_device_list(devlist, 1);
208
209 if (sdi->status != SR_ST_ACTIVE)
210 return SR_ERR;
211
212 return SR_OK;
213}
214
215SR_PRIV struct dev_context *dslogic_dev_new(void)
216{
217 struct dev_context *devc;
218
219 devc = g_malloc0(sizeof(struct dev_context));
220 devc->profile = NULL;
221 devc->fw_updated = 0;
222 devc->cur_samplerate = 0;
223 devc->limit_samples = 0;
224 devc->capture_ratio = 0;
225 devc->continuous_mode = FALSE;
226 devc->clock_edge = DS_EDGE_RISING;
227
228 return devc;
229}
230
231SR_PRIV void dslogic_abort_acquisition(struct dev_context *devc)
232{
233 int i;
234
235 devc->acq_aborted = TRUE;
236
237 for (i = devc->num_transfers - 1; i >= 0; i--) {
238 if (devc->transfers[i])
239 libusb_cancel_transfer(devc->transfers[i]);
240 }
241}
242
243static void finish_acquisition(struct sr_dev_inst *sdi)
244{
245 struct dev_context *devc;
246
247 devc = sdi->priv;
248
249 std_session_send_df_end(sdi);
250
251 usb_source_remove(sdi->session, devc->ctx);
252
253 devc->num_transfers = 0;
254 g_free(devc->transfers);
255}
256
257static void free_transfer(struct libusb_transfer *transfer)
258{
259 struct sr_dev_inst *sdi;
260 struct dev_context *devc;
261 unsigned int i;
262
263 sdi = transfer->user_data;
264 devc = sdi->priv;
265
266 g_free(transfer->buffer);
267 transfer->buffer = NULL;
268 libusb_free_transfer(transfer);
269
270 for (i = 0; i < devc->num_transfers; i++) {
271 if (devc->transfers[i] == transfer) {
272 devc->transfers[i] = NULL;
273 break;
274 }
275 }
276
277 devc->submitted_transfers--;
278 if (devc->submitted_transfers == 0)
279 finish_acquisition(sdi);
280}
281
282static void resubmit_transfer(struct libusb_transfer *transfer)
283{
284 int ret;
285
286 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
287 return;
288
289 sr_err("%s: %s", __func__, libusb_error_name(ret));
290 free_transfer(transfer);
291
292}
293
294SR_PRIV void dslogic_send_data(struct sr_dev_inst *sdi,
295 uint8_t *data, size_t length, size_t sample_width)
296{
297 const struct sr_datafeed_logic logic = {
298 .length = length,
299 .unitsize = sample_width,
300 .data = data
301 };
302
303 const struct sr_datafeed_packet packet = {
304 .type = SR_DF_LOGIC,
305 .payload = &logic
306 };
307
308 sr_session_send(sdi, &packet);
309}
310
311SR_PRIV void LIBUSB_CALL dslogic_receive_transfer(struct libusb_transfer *transfer)
312{
313 struct sr_dev_inst *sdi;
314 struct dev_context *devc;
315 gboolean packet_has_error = FALSE;
316 struct sr_datafeed_packet packet;
317 unsigned int num_samples;
318 int trigger_offset, cur_sample_count;
319 const int unitsize = 2;
320
321 sdi = transfer->user_data;
322 devc = sdi->priv;
323
324 /*
325 * If acquisition has already ended, just free any queued up
326 * transfer that come in.
327 */
328 if (devc->acq_aborted) {
329 free_transfer(transfer);
330 return;
331 }
332
333 sr_dbg("receive_transfer(): status %s received %d bytes.",
334 libusb_error_name(transfer->status), transfer->actual_length);
335
336 /* Save incoming transfer before reusing the transfer struct. */
337 cur_sample_count = transfer->actual_length / unitsize;
338
339 switch (transfer->status) {
340 case LIBUSB_TRANSFER_NO_DEVICE:
341 dslogic_abort_acquisition(devc);
342 free_transfer(transfer);
343 return;
344 case LIBUSB_TRANSFER_COMPLETED:
345 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
346 break;
347 default:
348 packet_has_error = TRUE;
349 break;
350 }
351
352 if (transfer->actual_length == 0 || packet_has_error) {
353 devc->empty_transfer_count++;
354 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
355 /*
356 * The FX2 gave up. End the acquisition, the frontend
357 * will work out that the samplecount is short.
358 */
359 dslogic_abort_acquisition(devc);
360 free_transfer(transfer);
361 } else {
362 resubmit_transfer(transfer);
363 }
364 return;
365 } else {
366 devc->empty_transfer_count = 0;
367 }
368 if (devc->trigger_fired) {
369 if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) {
370 /* Send the incoming transfer to the session bus. */
371 if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples)
372 num_samples = devc->limit_samples - devc->sent_samples;
373 else
374 num_samples = cur_sample_count;
375
376 if (devc->trigger_pos > devc->sent_samples
377 && devc->trigger_pos <= devc->sent_samples + num_samples) {
378 /* DSLogic trigger in this block. Send trigger position. */
379 trigger_offset = devc->trigger_pos - devc->sent_samples;
380 /* Pre-trigger samples. */
381 dslogic_send_data(sdi, (uint8_t *)transfer->buffer,
382 trigger_offset * unitsize, unitsize);
383 devc->sent_samples += trigger_offset;
384 /* Trigger position. */
385 devc->trigger_pos = 0;
386 packet.type = SR_DF_TRIGGER;
387 packet.payload = NULL;
388 sr_session_send(sdi, &packet);
389 /* Post trigger samples. */
390 num_samples -= trigger_offset;
391 dslogic_send_data(sdi, (uint8_t *)transfer->buffer
392 + trigger_offset * unitsize, num_samples * unitsize, unitsize);
393 devc->sent_samples += num_samples;
394 } else {
395 dslogic_send_data(sdi, (uint8_t *)transfer->buffer,
396 num_samples * unitsize, unitsize);
397 devc->sent_samples += num_samples;
398 }
399 }
400 }
401
402 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
403 dslogic_abort_acquisition(devc);
404 free_transfer(transfer);
405 } else
406 resubmit_transfer(transfer);
407}
408
409static unsigned int to_bytes_per_ms(unsigned int samplerate)
410{
411 return samplerate / 1000;
412}
413
414SR_PRIV size_t dslogic_get_buffer_size(struct dev_context *devc)
415{
416 size_t s;
417
418 /*
419 * The buffer should be large enough to hold 10ms of data and
420 * a multiple of 512.
421 */
422 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
423 return (s + 511) & ~511;
424}
425
426SR_PRIV unsigned int dslogic_get_timeout(struct dev_context *devc)
427{
428 size_t total_size;
429 unsigned int timeout;
430
431 total_size = dslogic_get_buffer_size(devc) *
432 dslogic_get_number_of_transfers(devc);
433 timeout = total_size / to_bytes_per_ms(devc->cur_samplerate);
434 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
435}