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