]> sigrok.org Git - libsigrok.git/blame - src/hardware/fx2lafw/protocol.c
Add udev rule for DSLogic.
[libsigrok.git] / src / hardware / fx2lafw / protocol.c
CommitLineData
2f937611
UH
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
a7d7f93c
BV
21#include <glib.h>
22#include <glib/gstdio.h>
2f937611
UH
23#include "protocol.h"
24
2f937611
UH
25#pragma pack(push, 1)
26
27struct version_info {
28 uint8_t major;
29 uint8_t minor;
30};
31
32struct cmd_start_acquisition {
33 uint8_t flags;
34 uint8_t sample_delay_h;
35 uint8_t sample_delay_l;
36};
37
38#pragma pack(pop)
39
40static int command_get_fw_version(libusb_device_handle *devhdl,
41 struct version_info *vi)
42{
43 int ret;
44
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);
48
49 if (ret < 0) {
50 sr_err("Unable to get version info: %s.",
51 libusb_error_name(ret));
52 return SR_ERR;
53 }
54
55 return SR_OK;
56}
57
a54edb1d 58static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid)
2f937611 59{
a7d7f93c 60 struct dev_context *devc = sdi->priv;
a54edb1d
ML
61 struct sr_usb_dev_inst *usb = sdi->conn;
62 libusb_device_handle *devhdl = usb->devhdl;
a7d7f93c 63 int cmd, ret;
2f937611 64
a7d7f93c 65 cmd = devc->dslogic ? CMD_DSLOGIC_GET_REVID_VERSION : CMD_GET_REVID_VERSION;
2f937611 66 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
a7d7f93c 67 LIBUSB_ENDPOINT_IN, cmd, 0x0000, 0x0000, revid, 1, 100);
2f937611
UH
68
69 if (ret < 0) {
70 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
71 return SR_ERR;
72 }
73
74 return SR_OK;
75}
76
a54edb1d 77SR_PRIV int fx2lafw_command_start_acquisition(const struct sr_dev_inst *sdi)
2f937611 78{
81a34976
BV
79 struct dev_context *devc;
80 struct sr_usb_dev_inst *usb;
81 uint64_t samplerate;
82 struct cmd_start_acquisition cmd;
83 int delay, ret;
84
85 devc = sdi->priv;
86 usb = sdi->conn;
87 samplerate = devc->cur_samplerate;
2f937611
UH
88
89 /* Compute the sample rate. */
81a34976 90 if (devc->sample_wide && samplerate > MAX_16BIT_SAMPLE_RATE) {
2f937611
UH
91 sr_err("Unable to sample at %" PRIu64 "Hz "
92 "when collecting 16-bit samples.", samplerate);
93 return SR_ERR;
94 }
95
81a34976
BV
96 delay = 0;
97 cmd.flags = cmd.sample_delay_h = cmd.sample_delay_l = 0;
2f937611
UH
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)
102 delay = 0;
103 }
104
105 if (delay == 0 && (SR_MHZ(30) % samplerate) == 0) {
106 cmd.flags = CMD_START_FLAGS_CLK_30MHZ;
107 delay = SR_MHZ(30) / samplerate - 1;
108 }
109
110 sr_info("GPIF delay = %d, clocksource = %sMHz.", delay,
111 (cmd.flags & CMD_START_FLAGS_CLK_48MHZ) ? "48" : "30");
112
113 if (delay <= 0 || delay > MAX_SAMPLE_DELAY) {
114 sr_err("Unable to sample at %" PRIu64 "Hz.", samplerate);
115 return SR_ERR;
116 }
117
118 cmd.sample_delay_h = (delay >> 8) & 0xff;
119 cmd.sample_delay_l = delay & 0xff;
120
121 /* Select the sampling width. */
81a34976 122 cmd.flags |= devc->sample_wide ? CMD_START_FLAGS_SAMPLE_16BIT :
2f937611
UH
123 CMD_START_FLAGS_SAMPLE_8BIT;
124
125 /* Send the control message. */
81a34976 126 ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
2f937611
UH
127 LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000,
128 (unsigned char *)&cmd, sizeof(cmd), 100);
129 if (ret < 0) {
130 sr_err("Unable to send start command: %s.",
131 libusb_error_name(ret));
132 return SR_ERR;
133 }
134
135 return SR_OK;
136}
137
138/**
139 * Check the USB configuration to determine if this is an fx2lafw device.
140 *
a7d7f93c
BV
141 * @return TRUE if the device's configuration profile matches fx2lafw
142 * configuration, FALSE otherwise.
2f937611 143 */
a7d7f93c
BV
144SR_PRIV gboolean match_manuf_prod(libusb_device *dev, const char *manufacturer,
145 const char *product)
2f937611
UH
146{
147 struct libusb_device_descriptor des;
148 struct libusb_device_handle *hdl;
149 gboolean ret;
150 unsigned char strdesc[64];
151
152 hdl = NULL;
153 ret = FALSE;
154 while (!ret) {
155 /* Assume the FW has not been loaded, unless proven wrong. */
156 if (libusb_get_device_descriptor(dev, &des) != 0)
157 break;
158
159 if (libusb_open(dev, &hdl) != 0)
160 break;
161
162 if (libusb_get_string_descriptor_ascii(hdl,
a7d7f93c 163 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
2f937611 164 break;
a7d7f93c 165 if (strncmp((const char *)strdesc, manufacturer, 6))
2f937611
UH
166 break;
167
168 if (libusb_get_string_descriptor_ascii(hdl,
169 des.iProduct, strdesc, sizeof(strdesc)) < 0)
170 break;
a7d7f93c 171 if (strncmp((const char *)strdesc, product, 7))
2f937611
UH
172 break;
173
2f937611
UH
174 ret = TRUE;
175 }
176 if (hdl)
177 libusb_close(hdl);
178
179 return ret;
180}
181
182SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
183{
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;
5e2c86eb 190 int ret, i, device_count;
2f937611 191 uint8_t revid;
5e2c86eb 192 char connection_id[64];
2f937611
UH
193
194 drvc = di->priv;
195 devc = sdi->priv;
196 usb = sdi->conn;
197
198 if (sdi->status == SR_ST_ACTIVE)
199 /* Device is already in use. */
200 return SR_ERR;
201
2f937611
UH
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));
206 return SR_ERR;
207 }
208
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));
213 continue;
214 }
215
216 if (des.idVendor != devc->profile->vid
217 || des.idProduct != devc->profile->pid)
218 continue;
219
5e2c86eb
SA
220 if ((sdi->status == SR_ST_INITIALIZING) ||
221 (sdi->status == SR_ST_INACTIVE)) {
2f937611 222 /*
5e2c86eb 223 * Check device by its physical USB bus/port address.
2f937611 224 */
5e2c86eb
SA
225 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
226 if (strcmp(sdi->connection_id, connection_id))
2f937611
UH
227 /* This is not the one. */
228 continue;
229 }
230
231 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
232 if (usb->address == 0xff)
233 /*
234 * First time we touch this device after FW
235 * upload, so we don't know the address yet.
236 */
237 usb->address = libusb_get_device_address(devlist[i]);
238 } else {
239 sr_err("Failed to open device: %s.",
240 libusb_error_name(ret));
241 break;
242 }
243
244 ret = command_get_fw_version(usb->devhdl, &vi);
245 if (ret != SR_OK) {
246 sr_err("Failed to get firmware version.");
247 break;
248 }
249
a54edb1d 250 ret = command_get_revid_version(sdi, &revid);
2f937611
UH
251 if (ret != SR_OK) {
252 sr_err("Failed to get REVID.");
253 break;
254 }
255
256 /*
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.
260 */
261 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
262 sr_err("Expected firmware version %d.x, "
263 "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
264 vi.major, vi.minor);
265 break;
266 }
267
268 sdi->status = SR_ST_ACTIVE;
5e2c86eb 269 sr_info("Opened device on %d.%d (logical) / %s (physical), "
2f937611 270 "interface %d, firmware %d.%d.",
5e2c86eb 271 usb->bus, usb->address, connection_id,
2f937611
UH
272 USB_INTERFACE, vi.major, vi.minor);
273
274 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
275 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
276
277 break;
278 }
279 libusb_free_device_list(devlist, 1);
280
281 if (sdi->status != SR_ST_ACTIVE)
282 return SR_ERR;
283
284 return SR_OK;
285}
286
2f937611
UH
287SR_PRIV struct dev_context *fx2lafw_dev_new(void)
288{
289 struct dev_context *devc;
290
f57d8ffe 291 devc = g_malloc0(sizeof(struct dev_context));
2f937611
UH
292 devc->profile = NULL;
293 devc->fw_updated = 0;
294 devc->cur_samplerate = 0;
295 devc->limit_samples = 0;
7bfcb25c 296 devc->capture_ratio = 0;
87b545fb 297 devc->sample_wide = FALSE;
335122f0 298 devc->stl = NULL;
2f937611
UH
299
300 return devc;
301}
302
303SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc)
304{
305 int i;
306
b0ccd64d 307 devc->acq_aborted = TRUE;
2f937611
UH
308
309 for (i = devc->num_transfers - 1; i >= 0; i--) {
310 if (devc->transfers[i])
311 libusb_cancel_transfer(devc->transfers[i]);
312 }
313}
314
102f1239 315static void finish_acquisition(struct sr_dev_inst *sdi)
2f937611
UH
316{
317 struct sr_datafeed_packet packet;
102f1239
BV
318 struct dev_context *devc;
319
320 devc = sdi->priv;
2f937611
UH
321
322 /* Terminate session. */
323 packet.type = SR_DF_END;
102f1239 324 sr_session_send(sdi, &packet);
2f937611
UH
325
326 /* Remove fds from polling. */
102f1239 327 usb_source_remove(sdi->session, devc->ctx);
2f937611
UH
328
329 devc->num_transfers = 0;
330 g_free(devc->transfers);
335122f0
BV
331
332 if (devc->stl) {
333 soft_trigger_logic_free(devc->stl);
334 devc->stl = NULL;
335 }
2f937611
UH
336}
337
338static void free_transfer(struct libusb_transfer *transfer)
339{
9615eeb5 340 struct sr_dev_inst *sdi;
2f937611
UH
341 struct dev_context *devc;
342 unsigned int i;
343
9615eeb5
BV
344 sdi = transfer->user_data;
345 devc = sdi->priv;
2f937611
UH
346
347 g_free(transfer->buffer);
348 transfer->buffer = NULL;
349 libusb_free_transfer(transfer);
350
351 for (i = 0; i < devc->num_transfers; i++) {
352 if (devc->transfers[i] == transfer) {
353 devc->transfers[i] = NULL;
354 break;
355 }
356 }
357
358 devc->submitted_transfers--;
359 if (devc->submitted_transfers == 0)
102f1239 360 finish_acquisition(sdi);
2f937611
UH
361}
362
363static void resubmit_transfer(struct libusb_transfer *transfer)
364{
365 int ret;
366
367 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
368 return;
369
9615eeb5 370 sr_err("%s: %s", __func__, libusb_error_name(ret));
2f937611 371 free_transfer(transfer);
2f937611 372
9615eeb5
BV
373}
374
2f937611
UH
375SR_PRIV void fx2lafw_receive_transfer(struct libusb_transfer *transfer)
376{
9615eeb5
BV
377 struct sr_dev_inst *sdi;
378 struct dev_context *devc;
2f937611
UH
379 gboolean packet_has_error = FALSE;
380 struct sr_datafeed_packet packet;
381 struct sr_datafeed_logic logic;
9615eeb5
BV
382 unsigned int num_samples;
383 int trigger_offset, cur_sample_count, unitsize;
7bfcb25c 384 int pre_trigger_samples;
2f937611 385
9615eeb5
BV
386 sdi = transfer->user_data;
387 devc = sdi->priv;
2f937611
UH
388
389 /*
390 * If acquisition has already ended, just free any queued up
391 * transfer that come in.
392 */
b0ccd64d 393 if (devc->acq_aborted) {
2f937611
UH
394 free_transfer(transfer);
395 return;
396 }
397
398 sr_info("receive_transfer(): status %d received %d bytes.",
399 transfer->status, transfer->actual_length);
400
401 /* Save incoming transfer before reusing the transfer struct. */
9615eeb5
BV
402 unitsize = devc->sample_wide ? 2 : 1;
403 cur_sample_count = transfer->actual_length / unitsize;
2f937611
UH
404
405 switch (transfer->status) {
406 case LIBUSB_TRANSFER_NO_DEVICE:
407 fx2lafw_abort_acquisition(devc);
408 free_transfer(transfer);
409 return;
410 case LIBUSB_TRANSFER_COMPLETED:
411 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
412 break;
413 default:
414 packet_has_error = TRUE;
415 break;
416 }
417
418 if (transfer->actual_length == 0 || packet_has_error) {
419 devc->empty_transfer_count++;
420 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
421 /*
422 * The FX2 gave up. End the acquisition, the frontend
423 * will work out that the samplecount is short.
424 */
425 fx2lafw_abort_acquisition(devc);
426 free_transfer(transfer);
427 } else {
428 resubmit_transfer(transfer);
429 }
430 return;
431 } else {
432 devc->empty_transfer_count = 0;
433 }
434
9615eeb5 435 if (devc->trigger_fired) {
2f663c82 436 if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) {
9615eeb5
BV
437 /* Send the incoming transfer to the session bus. */
438 packet.type = SR_DF_LOGIC;
439 packet.payload = &logic;
2f663c82 440 if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples)
9615eeb5
BV
441 num_samples = devc->limit_samples - devc->sent_samples;
442 else
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);
649e9e16 448 devc->sent_samples += num_samples;
9615eeb5
BV
449 }
450 } else {
335122f0 451 trigger_offset = soft_trigger_logic_check(devc->stl,
7bfcb25c 452 transfer->buffer, transfer->actual_length, &pre_trigger_samples);
9615eeb5 453 if (trigger_offset > -1) {
7bfcb25c 454 devc->sent_samples += pre_trigger_samples;
9615eeb5
BV
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;
466
467 devc->trigger_fired = TRUE;
2f937611 468 }
06b5d7f7
BV
469 }
470
471 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
472 fx2lafw_abort_acquisition(devc);
473 free_transfer(transfer);
9615eeb5
BV
474 } else
475 resubmit_transfer(transfer);
2f937611
UH
476}
477
478static unsigned int to_bytes_per_ms(unsigned int samplerate)
479{
480 return samplerate / 1000;
481}
482
483SR_PRIV size_t fx2lafw_get_buffer_size(struct dev_context *devc)
484{
485 size_t s;
486
487 /*
488 * The buffer should be large enough to hold 10ms of data and
489 * a multiple of 512.
490 */
491 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
492 return (s + 511) & ~511;
493}
494
495SR_PRIV unsigned int fx2lafw_get_number_of_transfers(struct dev_context *devc)
496{
497 unsigned int n;
498
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));
502
503 if (n > NUM_SIMUL_TRANSFERS)
504 return NUM_SIMUL_TRANSFERS;
505
506 return n;
507}
508
509SR_PRIV unsigned int fx2lafw_get_timeout(struct dev_context *devc)
510{
511 size_t total_size;
512 unsigned int timeout;
513
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. */
518}