3 * Copyright 2003 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio 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, or (at your option)
12 * GNU Radio 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.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
27 #include <fusb_libusb1.h>
28 #include <libusb-1.0/libusb.h>
37 #define MINIMIZE_TX_BUFFERING true
39 static const int MAX_BLOCK_SIZE = fusb_sysconfig::max_block_size();
40 static const int DEFAULT_BLOCK_SIZE = MAX_BLOCK_SIZE;
41 static const int DEFAULT_BUFFER_SIZE = 4 * (1L << 20); // 4 MB endpoint
42 static const int LIBUSB_TIMEOUT = 0; // no timeout
44 inline static fusb_ephandle_libusb1*
45 lut_get_ephandle (libusb_transfer *lut)
47 return (fusb_ephandle_libusb1 *) lut->user_data;
50 // ------------------------------------------------------------------------
51 // libusb_transfer allocation, deallocation, and callback
52 // ------------------------------------------------------------------------
55 free_lut (libusb_transfer *lut)
58 // if this was an input transfer, free the buffer
59 if (lut->endpoint & 0x80)
60 delete [] ((unsigned char *) lut->buffer);
62 libusb_free_transfer(lut);
67 * The callback means the libusb_transfer is completed whether sent, cancelled,
68 * or failed. Move the libusb_transfer from the pending list to the
69 * completed list. If the cancel is from the destructor then free the
70 * transfer instead; normally this won't happen since all endpoints should be
71 * destroyed first leaving the pending list empty.
75 generic_callback(struct libusb_transfer *lut)
78 // Fish out devhandle from endpoint
79 fusb_devhandle_libusb1* dev_handle =
80 lut_get_ephandle(lut)->get_fusb_devhandle_libusb1();
82 dev_handle->pending_remove(lut);
84 if (lut->status == LIBUSB_TRANSFER_CANCELLED && dev_handle->_teardown() == 1)
90 lut_get_ephandle(lut)->completed_list_add(lut);
94 static libusb_transfer*
95 alloc_lut (fusb_ephandle_libusb1 *self, int buffer_length, int endpoint,
96 bool input_p, unsigned char *write_buffer,
97 fusb_devhandle_libusb1 *dh)
100 struct libusb_transfer* lut = libusb_alloc_transfer(0);
102 endpoint = (endpoint & 0x7f) | (input_p ? 0x80 : 0);
105 write_buffer = new unsigned char [buffer_length];
107 // We need the base class libusb_device_handle
108 libusb_device_handle *dev_handle = dh->get_libusb_device_handle();
110 // Load the libusb_transfer for bulk transfer
111 libusb_fill_bulk_transfer (lut, // transfer
112 dev_handle, // dev_handle
113 endpoint, // endpoint
114 write_buffer, // buffer
115 buffer_length, // length
116 generic_callback, // callback
118 LIBUSB_TIMEOUT); // timeout
123 // ------------------------------------------------------------------------
125 // ------------------------------------------------------------------------
127 fusb_devhandle_libusb1::fusb_devhandle_libusb1 (libusb_device_handle *udh)
128 : fusb_devhandle (udh), d_teardown (false)
133 fusb_devhandle_libusb1::~fusb_devhandle_libusb1 ()
137 std::list<libusb_transfer*>::reverse_iterator it;
139 // After cancellation the libusb_transfer is still active so delay freeing
140 // transfer until callback occurs. In most cases the pending list should
141 // already be empty by the time this destructor is called.
143 for (it = d_pending_rqsts.rbegin (); it != d_pending_rqsts.rend (); it++) {
147 // Wait for pending list to empty
148 _wait_for_completion ();
153 fusb_devhandle_libusb1::make_ephandle (int endpoint, bool input_p,
154 int block_size, int nblocks)
156 return new fusb_ephandle_libusb1 (this, endpoint, input_p,
157 block_size, nblocks);
161 * devhandle list manipulators
165 fusb_devhandle_libusb1::pending_add (libusb_transfer *lut)
167 d_pending_rqsts.push_back (lut);
172 * Attempt to cancel all transations associated with eph
176 fusb_devhandle_libusb1::_cancel_pending_rqsts (fusb_ephandle_libusb1 *eph)
178 std::list<libusb_transfer*>::reverse_iterator it;
180 for (it = d_pending_rqsts.rbegin (); it != d_pending_rqsts.rend (); it++){
181 if (lut_get_ephandle (*it) == eph)
187 * Pull from the pending list
191 fusb_devhandle_libusb1::pending_get ()
193 if (d_pending_rqsts.empty ())
196 libusb_transfer *lut = d_pending_rqsts.front ();
197 d_pending_rqsts.pop_front ();
202 * Match libusb_tranfer with the pending list and erase
203 * Return true if found, false otherwise
207 fusb_devhandle_libusb1::pending_remove (libusb_transfer *lut)
209 std::list<libusb_transfer*>::iterator result;
210 result = find (d_pending_rqsts.begin (), d_pending_rqsts.end (), lut);
212 if (result == d_pending_rqsts.end ()) {
213 fprintf (stderr, "fusb::pending_remove: failed to find lut in pending_rqsts: %p\n", lut);
217 d_pending_rqsts.erase (result);
222 * Submit the libusb_transfer to libusb
223 * iff successful, the transfer will be placed on the devhandle pending list.
227 fusb_devhandle_libusb1::_submit_lut (libusb_transfer *lut)
230 int ret = libusb_submit_transfer (lut);
232 fprintf(stderr, "fusb::_submit_lut %d", ret);
242 * Attempt to cancel any pending libusb_transfer transactions.
243 * Return true in the absence of errors, which does not mean that the transfer
244 * is cancelled. Cancellation can be checked after the callback is fired off
249 fusb_devhandle_libusb1::_cancel_lut (libusb_transfer *lut)
252 int ret = libusb_cancel_transfer (lut);
254 fprintf (stderr, "fusb::_cancel_lut");
262 fusb_devhandle_libusb1::_wait_for_completion ()
270 // The regular libusb_handle_events sets a hardcoded timeout of 2
271 // seconds. Most of these calls should be changed to appropriate block / non-
272 // blocking version using libusb_handle_events_timeout. This was just a test
275 while (!d_pending_rqsts.empty ()) {
276 if ((ret = libusb_handle_events_timeout(NULL, &tv)) < 0) {
277 fprintf (stderr, "fusb: libusb_handle_events error %d\n", ret);
284 // ------------------------------------------------------------------------
286 // ------------------------------------------------------------------------
288 fusb_ephandle_libusb1::fusb_ephandle_libusb1 (fusb_devhandle_libusb1 *dh,
289 int endpoint, bool input_p,
290 int block_size, int nblocks)
291 : fusb_ephandle (endpoint, input_p, block_size, nblocks),
293 d_write_work_in_progress (0), d_write_buffer (0),
294 d_read_work_in_progress (0), d_read_buffer (0), d_read_buffer_end (0)
297 if (d_block_size < 0 || d_block_size > MAX_BLOCK_SIZE)
298 throw std::out_of_range ("fusb_ephandle_libusb1: block_size");
301 throw std::out_of_range ("fusb_ephandle_libusb1: nblocks");
303 if (d_block_size == 0)
304 d_block_size = DEFAULT_BLOCK_SIZE;
307 d_nblocks = std::max (1, DEFAULT_BUFFER_SIZE / d_block_size);
310 if (!MINIMIZE_TX_BUFFERING)
311 d_write_buffer = new unsigned char [d_block_size];
314 fprintf(stderr, "fusb_ephandle_libusb1::ctor: d_block_size = %d d_nblocks = %d\n",
315 d_block_size, d_nblocks);
317 // allocate libusb_transfers
318 for (int i = 0; i < d_nblocks; i++)
319 d_free_list.push_back (alloc_lut (this, d_block_size, d_endpoint,
320 d_input_p, d_write_buffer, d_devhandle));
323 fusb_ephandle_libusb1::~fusb_ephandle_libusb1 ()
328 libusb_transfer *lut;
330 while ((lut = free_list_get ()) != 0)
333 while ((lut = completed_list_get ()) != 0)
336 if (d_write_work_in_progress)
337 free_lut (d_write_work_in_progress);
339 delete [] d_write_buffer;
341 if (d_read_work_in_progress)
342 free_lut (d_read_work_in_progress);
347 fusb_ephandle_libusb1::start ()
356 libusb_transfer *lut;
359 while ((lut = free_list_get ()) !=0 && nerrors < d_nblocks) {
360 if (!submit_lut (lut))
370 * Cancel all transfers in progress or pending and return to initial state
374 fusb_ephandle_libusb1::stop ()
380 if (d_write_work_in_progress){
381 free_list_add (d_write_work_in_progress);
382 d_write_work_in_progress = 0;
385 if (d_read_work_in_progress){
386 free_list_add (d_read_work_in_progress);
387 d_read_work_in_progress = 0;
389 d_read_buffer_end = 0;
392 d_devhandle->_cancel_pending_rqsts (this);
394 // Do work, reap transfers, etc.
395 if (libusb_handle_events(NULL) < 0) {
396 perror ("fusb::libusb_handle_events");
401 libusb_transfer *lut;
402 while ((lut = completed_list_get ()) != 0)
405 if (d_free_list.size () == (unsigned) d_nblocks)
408 if (libusb_handle_events(NULL) < 0) {
409 perror ("fusb::libusb_handle_events");
419 // ------------------------------------------------------------------------
420 // routines for writing
421 // ------------------------------------------------------------------------
423 #if (MINIMIZE_TX_BUFFERING)
426 fusb_ephandle_libusb1::write (const void *buffer, int nbytes)
429 if (!d_started) // doesn't matter here, but keeps semantics constant
435 assert(nbytes % 512 == 0);
437 unsigned char *src = (unsigned char *) buffer;
442 struct libusb_transfer *lut = get_write_work_in_progress();
445 assert(lut->actual_length == 0);
446 int m = std::min(nbytes - n, MAX_BLOCK_SIZE);
453 if (!submit_lut(lut))
456 d_write_work_in_progress = 0;
465 fusb_ephandle_libusb1::write (const void *buffer, int nbytes)
473 unsigned char *src = (unsigned char *) buffer;
478 libusb_transfer *lut = get_write_work_in_progress ();
481 unsigned char *dst = (unsigned char *) lut->buffer;
482 int m = std::min (nbytes - n, lut->length - lut->actual_length);
484 memcpy (&dst[lut->actual_length], &src[n], m);
485 lut->actual_length += m;
488 if (lut->actual_length == lut->length){
489 if (!submit_lut (lut))
491 d_write_work_in_progress = 0;
500 struct libusb_transfer *
501 fusb_ephandle_libusb1::get_write_work_in_progress ()
503 if (d_write_work_in_progress)
504 return d_write_work_in_progress;
508 reap_complete_writes ();
510 struct libusb_transfer *lut = free_list_get ();
513 assert (lut->actual_length == 0);
514 d_write_work_in_progress = lut;
518 // Do work, reap transfers, etc.
519 libusb_handle_events(NULL);
524 fusb_ephandle_libusb1::reap_complete_writes ()
526 // take a look at the completed list and xfer to free list after
527 // checking for errors.
529 libusb_transfer *lut;
531 while ((lut = completed_list_get ()) != 0) {
533 // Check for any errors or short writes that were reporetd in the transfer.
534 // libusb1 sets status, actual_length.
536 if (lut->status != LIBUSB_TRANSFER_COMPLETED) {
537 fprintf (stderr, "fusb: (status %d) \n", lut->status );
539 else if (lut->actual_length != lut->length){
540 fprintf (stderr, "fusb: short write xfer: %d != %d\n",
541 lut->actual_length, lut->length);
549 fusb_ephandle_libusb1::wait_for_completion ()
551 d_devhandle->_wait_for_completion ();
554 // ------------------------------------------------------------------------
555 // routines for reading
556 // ------------------------------------------------------------------------
559 fusb_ephandle_libusb1::read (void *buffer, int nbytes)
561 if (!d_started) // doesn't matter here, but keeps semantics constant
567 unsigned char *dst = (unsigned char *) buffer;
572 if (d_read_buffer >= d_read_buffer_end)
573 if (!reload_read_buffer ())
576 int m = std::min (nbytes - n, (int) (d_read_buffer_end - d_read_buffer));
578 memcpy (&dst[n], d_read_buffer, m);
588 fusb_ephandle_libusb1::reload_read_buffer ()
590 assert (d_read_buffer >= d_read_buffer_end);
592 libusb_transfer *lut;
594 if (d_read_work_in_progress) {
595 lut = d_read_work_in_progress;
596 d_read_work_in_progress = 0;
598 d_read_buffer_end = 0;
599 lut->actual_length = 0;
600 if (!submit_lut (lut))
606 while ((lut = completed_list_get ()) == 0 ) {
607 if (libusb_handle_events(NULL) < 0)
608 fprintf (stderr, "fusb: libusb_handle_events\n");
611 if (lut->status != LIBUSB_TRANSFER_COMPLETED) {
612 fprintf (stderr, "fust: (rd status %d) %s\n", lut->status,
613 strerror (-lut->status));
614 lut->actual_length = 0;
619 d_read_work_in_progress = lut;
620 d_read_buffer = (unsigned char *) lut->buffer;
621 d_read_buffer_end = d_read_buffer + lut->actual_length;
629 * ephandle list manipulation
634 fusb_ephandle_libusb1::free_list_add (libusb_transfer *lut)
636 assert (lut_get_ephandle (lut) == this);
637 lut->actual_length = 0;
638 d_free_list.push_back (lut);
642 fusb_ephandle_libusb1::free_list_get ()
644 if (d_free_list.empty ())
647 libusb_transfer *lut = d_free_list.front ();
648 d_free_list.pop_front ();
653 fusb_ephandle_libusb1::completed_list_add (libusb_transfer *lut)
655 assert (lut_get_ephandle (lut) == this);
656 d_completed_list.push_back (lut);
660 fusb_ephandle_libusb1::completed_list_get ()
662 if (d_completed_list.empty ())
665 libusb_transfer *lut = d_completed_list.front ();
666 d_completed_list.pop_front ();
671 fusb_ephandle_libusb1::submit_lut (libusb_transfer *lut)
673 if (!d_devhandle->_submit_lut (lut)) {
674 fprintf (stderr, "_submit_lut failed\n");