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