]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <inttypes.h> | |
23 | #include <glib.h> | |
24 | #include <libusb.h> | |
25 | #include "config.h" | |
26 | #include "sigrok.h" | |
27 | #include "sigrok-internal.h" | |
28 | #include "fx2lafw.h" | |
29 | #include "command.h" | |
30 | ||
31 | static struct fx2lafw_profile supported_fx2[] = { | |
32 | /* USBee AX */ | |
33 | { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL, 8 }, | |
34 | { 0, 0, 0, 0, 0, 0 } | |
35 | }; | |
36 | ||
37 | static int fx2lafw_capabilities[] = { | |
38 | SR_HWCAP_LOGIC_ANALYZER, | |
39 | SR_HWCAP_SAMPLERATE, | |
40 | ||
41 | /* These are really implemented in the driver, not the hardware. */ | |
42 | SR_HWCAP_LIMIT_SAMPLES, | |
43 | SR_HWCAP_CONTINUOUS, | |
44 | 0, | |
45 | }; | |
46 | ||
47 | static const char *fx2lafw_probe_names[] = { | |
48 | "D0", | |
49 | "D1", | |
50 | "D2", | |
51 | "D3", | |
52 | "D4", | |
53 | "D5", | |
54 | "D6", | |
55 | "D7", | |
56 | NULL, | |
57 | }; | |
58 | ||
59 | static uint64_t fx2lafw_supported_samplerates[] = { | |
60 | SR_MHZ(1), | |
61 | SR_MHZ(2), | |
62 | SR_MHZ(3), | |
63 | SR_MHZ(4), | |
64 | SR_MHZ(6), | |
65 | SR_MHZ(8), | |
66 | SR_MHZ(12), | |
67 | SR_MHZ(16), | |
68 | SR_MHZ(24), | |
69 | }; | |
70 | ||
71 | static struct sr_samplerates fx2lafw_samplerates = { | |
72 | SR_MHZ(1), | |
73 | SR_MHZ(24), | |
74 | SR_HZ(0), | |
75 | fx2lafw_supported_samplerates, | |
76 | }; | |
77 | ||
78 | static GSList *dev_insts = NULL; | |
79 | static libusb_context *usb_context = NULL; | |
80 | ||
81 | static int hw_dev_config_set(int dev_index, int hwcap, void *value); | |
82 | static int hw_dev_acquisition_stop(int dev_index, void *session_dev_id); | |
83 | ||
84 | /** | |
85 | * Check the USB configuration to determine if this is an fx2lafw device. | |
86 | * | |
87 | * @return TRUE if the device's configuration profile match fx2lafw | |
88 | * configuration, FALSE otherwise. | |
89 | */ | |
90 | static gboolean check_conf_profile(libusb_device *dev) | |
91 | { | |
92 | struct libusb_device_descriptor des; | |
93 | struct libusb_config_descriptor *conf_dsc = NULL; | |
94 | const struct libusb_interface_descriptor *intf_dsc; | |
95 | gboolean ret = FALSE; | |
96 | ||
97 | while (!ret) { | |
98 | /* Assume it's not a Saleae Logic unless proven wrong. */ | |
99 | ret = 0; | |
100 | ||
101 | if (libusb_get_device_descriptor(dev, &des) != 0) | |
102 | break; | |
103 | ||
104 | if (des.bNumConfigurations != 1) | |
105 | /* Need exactly 1 configuration. */ | |
106 | break; | |
107 | ||
108 | if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0) | |
109 | break; | |
110 | ||
111 | if (conf_dsc->bNumInterfaces != 1) | |
112 | /* Need exactly 1 interface. */ | |
113 | break; | |
114 | ||
115 | if (conf_dsc->interface[0].num_altsetting != 1) | |
116 | /* Need just one alternate setting. */ | |
117 | break; | |
118 | ||
119 | intf_dsc = &(conf_dsc->interface[0].altsetting[0]); | |
120 | if (intf_dsc->bNumEndpoints != 2) | |
121 | /* Need exactly 2 end points. */ | |
122 | break; | |
123 | ||
124 | if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) != | |
125 | (2 | LIBUSB_ENDPOINT_IN)) // 0x82 | |
126 | /* The first endpoint should be 2 (inbound). */ | |
127 | break; | |
128 | ||
129 | /* TODO: Check the debug channel... */ | |
130 | ||
131 | /* If we made it here, it must be an fx2lafw. */ | |
132 | ret = TRUE; | |
133 | } | |
134 | ||
135 | if (conf_dsc) | |
136 | libusb_free_config_descriptor(conf_dsc); | |
137 | ||
138 | return ret; | |
139 | } | |
140 | ||
141 | static int fx2lafw_open_dev(int dev_index) | |
142 | { | |
143 | libusb_device **devlist; | |
144 | struct libusb_device_descriptor des; | |
145 | struct sr_dev_inst *sdi; | |
146 | struct context *ctx; | |
147 | int err, skip, i; | |
148 | ||
149 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
150 | return SR_ERR; | |
151 | ctx = sdi->priv; | |
152 | ||
153 | if (sdi->status == SR_ST_ACTIVE) | |
154 | /* already in use */ | |
155 | return SR_ERR; | |
156 | ||
157 | skip = 0; | |
158 | libusb_get_device_list(usb_context, &devlist); | |
159 | for (i = 0; devlist[i]; i++) { | |
160 | if ((err = libusb_get_device_descriptor(devlist[i], &des))) { | |
161 | sr_err("fx2lafw: failed to get device descriptor: %d", err); | |
162 | continue; | |
163 | } | |
164 | ||
165 | if (des.idVendor != ctx->profile->vid | |
166 | || des.idProduct != ctx->profile->pid) | |
167 | continue; | |
168 | ||
169 | if (sdi->status == SR_ST_INITIALIZING) { | |
170 | if (skip != dev_index) { | |
171 | /* Skip devices of this type that aren't the one we want. */ | |
172 | skip += 1; | |
173 | continue; | |
174 | } | |
175 | } else if (sdi->status == SR_ST_INACTIVE) { | |
176 | /* | |
177 | * This device is fully enumerated, so we need to find | |
178 | * this device by vendor, product, bus and address. | |
179 | */ | |
180 | if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus | |
181 | || libusb_get_device_address(devlist[i]) != ctx->usb->address) | |
182 | /* this is not the one */ | |
183 | continue; | |
184 | } | |
185 | ||
186 | if (!(err = libusb_open(devlist[i], &ctx->usb->devhdl))) { | |
187 | if (ctx->usb->address == 0xff) | |
188 | /* | |
189 | * first time we touch this device after firmware upload, | |
190 | * so we don't know the address yet. | |
191 | */ | |
192 | ctx->usb->address = libusb_get_device_address(devlist[i]); | |
193 | ||
194 | sdi->status = SR_ST_ACTIVE; | |
195 | sr_info("fx2lafw: opened device %d on %d.%d interface %d", | |
196 | sdi->index, ctx->usb->bus, | |
197 | ctx->usb->address, USB_INTERFACE); | |
198 | } else { | |
199 | sr_err("fx2lafw: failed to open device: %d", err); | |
200 | } | |
201 | ||
202 | /* if we made it here, we handled the device one way or another */ | |
203 | break; | |
204 | } | |
205 | libusb_free_device_list(devlist, 1); | |
206 | ||
207 | if (sdi->status != SR_ST_ACTIVE) | |
208 | return SR_ERR; | |
209 | ||
210 | return SR_OK; | |
211 | } | |
212 | ||
213 | static void close_dev(struct sr_dev_inst *sdi) | |
214 | { | |
215 | struct context *ctx; | |
216 | ||
217 | ctx = sdi->priv; | |
218 | ||
219 | if (ctx->usb->devhdl == NULL) | |
220 | return; | |
221 | ||
222 | sr_info("fx2lafw: closing device %d on %d.%d interface %d", sdi->index, | |
223 | ctx->usb->bus, ctx->usb->address, USB_INTERFACE); | |
224 | libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE); | |
225 | libusb_close(ctx->usb->devhdl); | |
226 | ctx->usb->devhdl = NULL; | |
227 | sdi->status = SR_ST_INACTIVE; | |
228 | } | |
229 | ||
230 | static struct context *fx2lafw_device_new(void) | |
231 | { | |
232 | struct context *ctx; | |
233 | ||
234 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { | |
235 | sr_err("fx2lafw: %s: ctx malloc failed", __func__); | |
236 | return NULL; | |
237 | } | |
238 | ||
239 | return ctx; | |
240 | } | |
241 | ||
242 | /* | |
243 | * API callbacks | |
244 | */ | |
245 | ||
246 | static int hw_init(const char *deviceinfo) | |
247 | { | |
248 | struct sr_dev_inst *sdi; | |
249 | struct libusb_device_descriptor des; | |
250 | struct fx2lafw_profile *fx2lafw_prof; | |
251 | struct context *ctx; | |
252 | libusb_device **devlist; | |
253 | int err; | |
254 | int devcnt = 0; | |
255 | int i, j; | |
256 | ||
257 | /* Avoid compiler warnings. */ | |
258 | (void)deviceinfo; | |
259 | ||
260 | if (libusb_init(&usb_context) != 0) { | |
261 | sr_warn("Failed to initialize USB."); | |
262 | return 0; | |
263 | } | |
264 | ||
265 | /* Find all fx2lafw compatible devices and upload firware to all of them. */ | |
266 | libusb_get_device_list(usb_context, &devlist); | |
267 | for (i = 0; devlist[i]; i++) { | |
268 | ||
269 | if ((err = libusb_get_device_descriptor( | |
270 | devlist[i], &des)) != 0) { | |
271 | sr_warn("failed to get device descriptor: %d", err); | |
272 | continue; | |
273 | } | |
274 | ||
275 | fx2lafw_prof = NULL; | |
276 | for (j = 0; supported_fx2[j].vid; j++) { | |
277 | if (des.idVendor == supported_fx2[j].vid && | |
278 | des.idProduct == supported_fx2[j].pid) { | |
279 | fx2lafw_prof = &supported_fx2[j]; | |
280 | } | |
281 | } | |
282 | ||
283 | /* Skip if the device was not found */ | |
284 | if(!fx2lafw_prof) | |
285 | continue; | |
286 | ||
287 | sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING, | |
288 | fx2lafw_prof->vendor, fx2lafw_prof->model, | |
289 | fx2lafw_prof->model_version); | |
290 | if(!sdi) | |
291 | return 0; | |
292 | ||
293 | ctx = fx2lafw_device_new(); | |
294 | ctx->profile = fx2lafw_prof; | |
295 | sdi->priv = ctx; | |
296 | dev_insts = g_slist_append(dev_insts, sdi); | |
297 | ||
298 | if (check_conf_profile(devlist[i])) { | |
299 | /* Already has the firmware, so fix the new address. */ | |
300 | sr_dbg("fx2lafw: Found a fx2lafw device."); | |
301 | sdi->status = SR_ST_INACTIVE; | |
302 | ctx->usb = sr_usb_dev_inst_new | |
303 | (libusb_get_bus_number(devlist[i]), | |
304 | libusb_get_device_address(devlist[i]), NULL); | |
305 | } else { | |
306 | if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FIRMWARE) == SR_OK) | |
307 | /* Remember when the firmware on this device was updated */ | |
308 | g_get_current_time(&ctx->fw_updated); | |
309 | else | |
310 | sr_err("fx2lafw: firmware upload failed for " | |
311 | "device %d", devcnt); | |
312 | ctx->usb = sr_usb_dev_inst_new | |
313 | (libusb_get_bus_number(devlist[i]), 0xff, NULL); | |
314 | } | |
315 | ||
316 | devcnt++; | |
317 | } | |
318 | libusb_free_device_list(devlist, 1); | |
319 | ||
320 | return devcnt; | |
321 | } | |
322 | ||
323 | static int hw_dev_open(int dev_index) | |
324 | { | |
325 | GTimeVal cur_time; | |
326 | struct sr_dev_inst *sdi; | |
327 | struct context *ctx; | |
328 | int timediff, err; | |
329 | ||
330 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
331 | return SR_ERR; | |
332 | ctx = sdi->priv; | |
333 | ||
334 | /* | |
335 | * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms | |
336 | * for the FX2 to renumerate | |
337 | */ | |
338 | err = 0; | |
339 | if (GTV_TO_MSEC(ctx->fw_updated) > 0) { | |
340 | sr_info("fx2lafw: waiting for device to reset"); | |
341 | /* takes at least 300ms for the FX2 to be gone from the USB bus */ | |
342 | g_usleep(300 * 1000); | |
343 | timediff = 0; | |
344 | while (timediff < MAX_RENUM_DELAY) { | |
345 | if ((err = fx2lafw_open_dev(dev_index)) == SR_OK) | |
346 | break; | |
347 | g_usleep(100 * 1000); | |
348 | g_get_current_time(&cur_time); | |
349 | timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated); | |
350 | } | |
351 | sr_info("fx2lafw: device came back after %d ms", timediff); | |
352 | } else { | |
353 | err = fx2lafw_open_dev(dev_index); | |
354 | } | |
355 | ||
356 | if (err != SR_OK) { | |
357 | sr_err("fx2lafw: unable to open device"); | |
358 | return SR_ERR; | |
359 | } | |
360 | ctx = sdi->priv; | |
361 | ||
362 | err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE); | |
363 | if (err != 0) { | |
364 | sr_err("fx2lafw: Unable to claim interface: %d", err); | |
365 | return SR_ERR; | |
366 | } | |
367 | ||
368 | if (ctx->cur_samplerate == 0) { | |
369 | /* Samplerate hasn't been set; default to the slowest one. */ | |
370 | if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE, | |
371 | &fx2lafw_supported_samplerates[0]) == SR_ERR) | |
372 | return SR_ERR; | |
373 | } | |
374 | ||
375 | return SR_OK; | |
376 | } | |
377 | ||
378 | static int hw_dev_close(int dev_index) | |
379 | { | |
380 | struct sr_dev_inst *sdi; | |
381 | ||
382 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
383 | sr_err("fx2lafw: %s: sdi was NULL", __func__); | |
384 | return SR_ERR_BUG; | |
385 | } | |
386 | ||
387 | /* TODO */ | |
388 | close_dev(sdi); | |
389 | ||
390 | return SR_OK; | |
391 | } | |
392 | ||
393 | static int hw_cleanup(void) | |
394 | { | |
395 | GSList *l; | |
396 | struct sr_dev_inst *sdi; | |
397 | struct context *ctx; | |
398 | int ret = SR_OK; | |
399 | ||
400 | for(l = dev_insts; l; l = l->next) { | |
401 | if (!(sdi = l->data)) { | |
402 | /* Log error, but continue cleaning up the rest. */ | |
403 | sr_err("fx2lafw: %s: sdi was NULL, continuing", __func__); | |
404 | ret = SR_ERR_BUG; | |
405 | continue; | |
406 | } | |
407 | if (!(ctx = sdi->priv)) { | |
408 | /* Log error, but continue cleaning up the rest. */ | |
409 | sr_err("fx2lafw: %s: sdi->priv was NULL, continuing", | |
410 | __func__); | |
411 | ret = SR_ERR_BUG; | |
412 | continue; | |
413 | } | |
414 | close_dev(sdi); | |
415 | sdi = l->data; | |
416 | sr_dev_inst_free(sdi); | |
417 | } | |
418 | ||
419 | g_slist_free(dev_insts); | |
420 | dev_insts = NULL; | |
421 | ||
422 | if(usb_context) | |
423 | libusb_exit(usb_context); | |
424 | usb_context = NULL; | |
425 | ||
426 | return ret; | |
427 | } | |
428 | ||
429 | static void *hw_dev_info_get(int dev_index, int dev_info_id) | |
430 | { | |
431 | struct sr_dev_inst *sdi; | |
432 | struct context *ctx; | |
433 | ||
434 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
435 | return NULL; | |
436 | ctx = sdi->priv; | |
437 | ||
438 | switch (dev_info_id) { | |
439 | case SR_DI_INST: | |
440 | return sdi; | |
441 | case SR_DI_NUM_PROBES: | |
442 | return GINT_TO_POINTER(ctx->profile->num_probes); | |
443 | case SR_DI_PROBE_NAMES: | |
444 | return fx2lafw_probe_names; | |
445 | case SR_DI_SAMPLERATES: | |
446 | return &fx2lafw_samplerates; | |
447 | case SR_DI_TRIGGER_TYPES: | |
448 | return TRIGGER_TYPES; | |
449 | case SR_DI_CUR_SAMPLERATE: | |
450 | return &ctx->cur_samplerate; | |
451 | } | |
452 | ||
453 | return NULL; | |
454 | } | |
455 | ||
456 | static int hw_dev_status_get(int dev_index) | |
457 | { | |
458 | const struct sr_dev_inst *const sdi = | |
459 | sr_dev_inst_get(dev_insts, dev_index); | |
460 | ||
461 | if (!sdi) | |
462 | return SR_ST_NOT_FOUND; | |
463 | ||
464 | return sdi->status; | |
465 | } | |
466 | ||
467 | static int *hw_hwcap_get_all(void) | |
468 | { | |
469 | return fx2lafw_capabilities; | |
470 | } | |
471 | ||
472 | static int hw_dev_config_set(int dev_index, int hwcap, void *value) | |
473 | { | |
474 | struct sr_dev_inst *sdi; | |
475 | struct context *ctx; | |
476 | int ret; | |
477 | ||
478 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
479 | return SR_ERR; | |
480 | ctx = sdi->priv; | |
481 | ||
482 | if (hwcap == SR_HWCAP_SAMPLERATE) { | |
483 | ctx->cur_samplerate = *(uint64_t *)value; | |
484 | ret = SR_OK; | |
485 | } else if (hwcap == SR_HWCAP_PROBECONFIG) { | |
486 | ret = SR_OK; | |
487 | } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) { | |
488 | ctx->limit_samples = *(uint64_t *)value; | |
489 | ret = SR_OK; | |
490 | } else { | |
491 | ret = SR_ERR; | |
492 | } | |
493 | ||
494 | return ret; | |
495 | } | |
496 | ||
497 | static int receive_data(int fd, int revents, void *cb_data) | |
498 | { | |
499 | struct timeval tv; | |
500 | ||
501 | /* Avoid compiler warnings. */ | |
502 | (void)fd; | |
503 | (void)revents; | |
504 | (void)cb_data; | |
505 | ||
506 | tv.tv_sec = tv.tv_usec = 0; | |
507 | libusb_handle_events_timeout(usb_context, &tv); | |
508 | ||
509 | return TRUE; | |
510 | } | |
511 | ||
512 | static void abort_acquisition(struct context *ctx) | |
513 | { | |
514 | struct sr_datafeed_packet packet; | |
515 | ||
516 | packet.type = SR_DF_END; | |
517 | sr_session_send(ctx->session_dev_id, &packet); | |
518 | ||
519 | ctx->num_samples = -1; | |
520 | ||
521 | /* TODO: Need to cancel and free any queued up transfers. */ | |
522 | } | |
523 | ||
524 | static void receive_transfer(struct libusb_transfer *transfer) | |
525 | { | |
526 | /* TODO: These statics have to move to the ctx struct. */ | |
527 | static int empty_transfer_count = 0; | |
528 | struct sr_datafeed_packet packet; | |
529 | struct sr_datafeed_logic logic; | |
530 | struct context *ctx = transfer->user_data; | |
531 | int cur_buflen; | |
532 | unsigned char *cur_buf, *new_buf; | |
533 | ||
534 | /* | |
535 | * If acquisition has already ended, just free any queued up | |
536 | * transfer that come in. | |
537 | */ | |
538 | if (ctx->num_samples == -1) { | |
539 | if (transfer) | |
540 | libusb_free_transfer(transfer); | |
541 | return; | |
542 | } | |
543 | ||
544 | sr_info("fx2lafw: receive_transfer(): status %d received %d bytes", | |
545 | transfer->status, transfer->actual_length); | |
546 | ||
547 | /* Save incoming transfer before reusing the transfer struct. */ | |
548 | cur_buf = transfer->buffer; | |
549 | cur_buflen = transfer->actual_length; | |
550 | ctx = transfer->user_data; | |
551 | ||
552 | /* Fire off a new request. */ | |
553 | if (!(new_buf = g_try_malloc(4096))) { | |
554 | sr_err("fx2lafw: %s: new_buf malloc failed", __func__); | |
555 | return; /* TODO: SR_ERR_MALLOC */ | |
556 | } | |
557 | ||
558 | transfer->buffer = new_buf; | |
559 | transfer->length = 4096; | |
560 | if (libusb_submit_transfer(transfer) != 0) { | |
561 | /* TODO: Stop session? */ | |
562 | /* TODO: Better error message. */ | |
563 | sr_err("fx2lafw: %s: libusb_submit_transfer error", __func__); | |
564 | } | |
565 | ||
566 | if (cur_buflen == 0) { | |
567 | empty_transfer_count++; | |
568 | if (empty_transfer_count > MAX_EMPTY_TRANSFERS) { | |
569 | /* | |
570 | * The FX2 gave up. End the acquisition, the frontend | |
571 | * will work out that the samplecount is short. | |
572 | */ | |
573 | abort_acquisition(ctx); | |
574 | } | |
575 | return; | |
576 | } else { | |
577 | empty_transfer_count = 0; | |
578 | } | |
579 | ||
580 | /* Send the incoming transfer to the session bus. */ | |
581 | packet.type = SR_DF_LOGIC; | |
582 | packet.payload = &logic; | |
583 | logic.length = cur_buflen; | |
584 | logic.unitsize = 1; | |
585 | logic.data = cur_buf; | |
586 | sr_session_send(ctx->session_dev_id, &packet); | |
587 | g_free(cur_buf); | |
588 | ||
589 | ctx->num_samples += cur_buflen; | |
590 | if (ctx->limit_samples && | |
591 | (unsigned int) ctx->num_samples > ctx->limit_samples) { | |
592 | abort_acquisition(ctx); | |
593 | } | |
594 | } | |
595 | ||
596 | static int hw_dev_acquisition_start(int dev_index, void *cb_data) | |
597 | { | |
598 | struct sr_dev_inst *sdi; | |
599 | struct sr_datafeed_packet *packet; | |
600 | struct sr_datafeed_header *header; | |
601 | struct context *ctx; | |
602 | struct libusb_transfer *transfer; | |
603 | const struct libusb_pollfd **lupfd; | |
604 | int err, size, i; | |
605 | unsigned char *buf; | |
606 | ||
607 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
608 | return SR_ERR; | |
609 | ctx = sdi->priv; | |
610 | ctx->session_dev_id = cb_data; | |
611 | ctx->num_samples = 0; | |
612 | ||
613 | if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { | |
614 | sr_err("fx2lafw: %s: packet malloc failed", __func__); | |
615 | return SR_ERR_MALLOC; | |
616 | } | |
617 | ||
618 | if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { | |
619 | sr_err("fx2lafw: %s: header malloc failed", __func__); | |
620 | return SR_ERR_MALLOC; | |
621 | } | |
622 | ||
623 | /* Start with 2K transfer, subsequently increased to 4K. */ | |
624 | size = 2048; | |
625 | for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) { | |
626 | if (!(buf = g_try_malloc(size))) { | |
627 | sr_err("fx2lafw: %s: buf malloc failed", __func__); | |
628 | return SR_ERR_MALLOC; | |
629 | } | |
630 | transfer = libusb_alloc_transfer(0); | |
631 | libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl, | |
632 | 2 | LIBUSB_ENDPOINT_IN, buf, size, | |
633 | receive_transfer, ctx, 40); | |
634 | if (libusb_submit_transfer(transfer) != 0) { | |
635 | /* TODO: Free them all. */ | |
636 | libusb_free_transfer(transfer); | |
637 | g_free(buf); | |
638 | return SR_ERR; | |
639 | } | |
640 | size = 4096; | |
641 | } | |
642 | ||
643 | lupfd = libusb_get_pollfds(usb_context); | |
644 | for (i = 0; lupfd[i]; i++) | |
645 | sr_source_add(lupfd[i]->fd, lupfd[i]->events, | |
646 | 40, receive_data, NULL); | |
647 | free(lupfd); /* NOT g_free()! */ | |
648 | ||
649 | packet->type = SR_DF_HEADER; | |
650 | packet->payload = header; | |
651 | header->feed_version = 1; | |
652 | gettimeofday(&header->starttime, NULL); | |
653 | header->samplerate = ctx->cur_samplerate; | |
654 | header->num_logic_probes = ctx->profile->num_probes; | |
655 | sr_session_send(cb_data, packet); | |
656 | g_free(header); | |
657 | g_free(packet); | |
658 | ||
659 | if ((err = command_start_acquisition (ctx->usb->devhdl, | |
660 | ctx->cur_samplerate)) != SR_OK) { | |
661 | return err; | |
662 | } | |
663 | ||
664 | return SR_OK; | |
665 | } | |
666 | ||
667 | /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */ | |
668 | static int hw_dev_acquisition_stop(int dev_index, void *cb_data) | |
669 | { | |
670 | struct sr_dev_inst *sdi; | |
671 | ||
672 | /* unused parameter */ | |
673 | (void)cb_data; | |
674 | ||
675 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
676 | return SR_ERR; | |
677 | ||
678 | abort_acquisition(sdi->priv); | |
679 | ||
680 | return SR_OK; | |
681 | } | |
682 | ||
683 | SR_PRIV struct sr_dev_driver fx2lafw_driver_info = { | |
684 | .name = "fx2lafw", | |
685 | .longname = "fx2lafw", | |
686 | .api_version = 1, | |
687 | .init = hw_init, | |
688 | .cleanup = hw_cleanup, | |
689 | .dev_open = hw_dev_open, | |
690 | .dev_close = hw_dev_close, | |
691 | .dev_info_get = hw_dev_info_get, | |
692 | .dev_status_get = hw_dev_status_get, | |
693 | .hwcap_get_all = hw_hwcap_get_all, | |
694 | .dev_config_set = hw_dev_config_set, | |
695 | .dev_acquisition_start = hw_dev_acquisition_start, | |
696 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
697 | }; |