3 * Copyright 2003,2009 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>
29 #include <libusb-1.0/libusb.h>
38 #define MINIMIZE_TX_BUFFERING true
40 static const int MAX_BLOCK_SIZE = fusb_sysconfig::max_block_size();
41 static const int DEFAULT_BLOCK_SIZE = MAX_BLOCK_SIZE;
42 static const int DEFAULT_BUFFER_SIZE = 4 * (1L << 20); // 4 MB endpoint
43 static const int LIBUSB_TIMEOUT = 0; // no timeout
45 inline static fusb_ephandle_libusb1 *
46 lut_get_ephandle (libusb_transfer *lut)
48 return (fusb_ephandle_libusb1 *) lut->user_data;
51 // ------------------------------------------------------------------------
52 // libusb_transfer allocation, deallocation, and callback
53 // ------------------------------------------------------------------------
56 free_lut (libusb_transfer *lut)
59 // if this was an input transfer, free the buffer
60 if (lut->endpoint & 0x80)
61 delete [] ((unsigned char *) lut->buffer);
63 libusb_free_transfer(lut);
68 * The callback means the libusb_transfer is completed whether sent, cancelled,
69 * or failed. Move the libusb_transfer from the pending list to the
70 * completed list. If the cancel is from the destructor then free the
71 * transfer instead; normally this won't happen since all endpoints should be
72 * destroyed first leaving the pending list empty.
76 generic_callback(struct libusb_transfer *lut)
79 // Fish out devhandle from endpoint
80 fusb_devhandle_libusb1* dev_handle =
81 lut_get_ephandle(lut)->get_fusb_devhandle_libusb1();
83 dev_handle->pending_remove(lut);
85 if (lut->status == LIBUSB_TRANSFER_CANCELLED && dev_handle->_teardown() == 1)
91 lut_get_ephandle(lut)->completed_list_add(lut);
95 static libusb_transfer*
96 alloc_lut (fusb_ephandle_libusb1 *self, int buffer_length, int endpoint,
97 bool input_p, unsigned char *write_buffer,
98 fusb_devhandle_libusb1 *dh)
101 struct libusb_transfer* lut = libusb_alloc_transfer(0);
103 endpoint = (endpoint & 0x7f) | (input_p ? 0x80 : 0);
106 write_buffer = new unsigned char [buffer_length];
108 // We need the base class libusb_device_handle
109 libusb_device_handle *dev_handle = dh->get_usb_dev_handle();
111 // Load the libusb_transfer for bulk transfer
112 libusb_fill_bulk_transfer (lut, // transfer
113 dev_handle, // dev_handle
114 endpoint, // endpoint
115 write_buffer, // buffer
116 buffer_length, // length
117 generic_callback, // callback
119 LIBUSB_TIMEOUT); // timeout
124 // ------------------------------------------------------------------------
126 // ------------------------------------------------------------------------
128 fusb_devhandle_libusb1::fusb_devhandle_libusb1 (libusb_device_handle *udh,
130 : fusb_devhandle (udh), d_ctx (ctx), d_teardown (false)
135 fusb_devhandle_libusb1::~fusb_devhandle_libusb1 ()
139 std::list<libusb_transfer*>::reverse_iterator it;
141 // After cancellation the libusb_transfer is still active so delay freeing
142 // transfer until callback occurs. In most cases the pending list should
143 // already be empty by the time this destructor is called.
145 for (it = d_pending_rqsts.rbegin (); it != d_pending_rqsts.rend (); it++) {
149 // Wait for pending list to empty
150 _wait_for_completion ();
155 fusb_devhandle_libusb1::make_ephandle (int endpoint, bool input_p,
156 int block_size, int nblocks)
158 return new fusb_ephandle_libusb1 (this, endpoint, input_p,
159 block_size, nblocks);
163 * devhandle list manipulators
167 fusb_devhandle_libusb1::pending_add (libusb_transfer *lut)
169 d_pending_rqsts.push_back (lut);
174 * Attempt to cancel all transations associated with eph
178 fusb_devhandle_libusb1::_cancel_pending_rqsts (fusb_ephandle_libusb1 *eph)
180 std::list<libusb_transfer*>::reverse_iterator it;
182 for (it = d_pending_rqsts.rbegin (); it != d_pending_rqsts.rend (); it++){
183 if (lut_get_ephandle (*it) == eph)
189 * Pull from the pending list
193 fusb_devhandle_libusb1::pending_get ()
195 if (d_pending_rqsts.empty ())
198 libusb_transfer *lut = d_pending_rqsts.front ();
199 d_pending_rqsts.pop_front ();
204 * Match libusb_tranfer with the pending list and erase
205 * Return true if found, false otherwise
209 fusb_devhandle_libusb1::pending_remove (libusb_transfer *lut)
211 std::list<libusb_transfer*>::iterator result;
212 result = find (d_pending_rqsts.begin (), d_pending_rqsts.end (), lut);
214 if (result == d_pending_rqsts.end ()) {
215 fprintf (stderr, "fusb::pending_remove: failed to find lut in pending_rqsts: %p\n", lut);
219 d_pending_rqsts.erase (result);
224 * Submit the libusb_transfer to libusb
225 * iff successful, the transfer will be placed on the devhandle pending list.
229 fusb_devhandle_libusb1::_submit_lut (libusb_transfer *lut)
232 int ret = libusb_submit_transfer (lut);
234 fprintf(stderr, "fusb::_submit_lut %d", ret);
244 * Attempt to cancel any pending libusb_transfer transactions.
245 * Return true in the absence of errors, which does not mean that the transfer
246 * is cancelled. Cancellation can be checked after the callback is fired off
251 fusb_devhandle_libusb1::_cancel_lut (libusb_transfer *lut)
254 int ret = libusb_cancel_transfer (lut);
256 fprintf (stderr, "fusb::_cancel_lut");
264 * Reimplementing _reap for context use and compatibiliy with libusb-0.12.
266 * Returns false on timeout or error, true if an event was handled
268 * If ok_to_block_p is false then handle already pending events and return
271 * If ok_to_block_p is true then call libusb_handle_events_timeout with default
272 * timeout value of 2 seconds, which waits and returns on event arrival or
277 fusb_devhandle_libusb1::_reap (bool ok_to_block_p)
291 if ((ret = libusb_handle_events_timeout(d_ctx, &tv)) < 0) {
292 fprintf (stderr, "fusb::_reap libusb_handle_events() %i\n", ret);
300 fusb_devhandle_libusb1::_wait_for_completion ()
303 while (!d_pending_rqsts.empty ())
309 // ------------------------------------------------------------------------
311 // ------------------------------------------------------------------------
313 fusb_ephandle_libusb1::fusb_ephandle_libusb1 (fusb_devhandle_libusb1 *dh,
314 int endpoint, bool input_p,
315 int block_size, int nblocks)
316 : fusb_ephandle (endpoint, input_p, block_size, nblocks),
318 d_write_work_in_progress (0), d_write_buffer (0),
319 d_read_work_in_progress (0), d_read_buffer (0), d_read_buffer_end (0)
322 if (d_block_size < 0 || d_block_size > MAX_BLOCK_SIZE)
323 throw std::out_of_range ("fusb_ephandle_libusb1: block_size");
326 throw std::out_of_range ("fusb_ephandle_libusb1: nblocks");
328 if (d_block_size == 0)
329 d_block_size = DEFAULT_BLOCK_SIZE;
332 d_nblocks = std::max (1, DEFAULT_BUFFER_SIZE / d_block_size);
335 if (!MINIMIZE_TX_BUFFERING)
336 d_write_buffer = new unsigned char [d_block_size];
339 fprintf(stderr, "fusb_ephandle_libusb1::ctor: d_block_size = %d d_nblocks = %d\n",
340 d_block_size, d_nblocks);
342 // allocate libusb_transfers
343 for (int i = 0; i < d_nblocks; i++)
344 d_free_list.push_back (alloc_lut (this, d_block_size, d_endpoint,
345 d_input_p, d_write_buffer, d_devhandle));
348 fusb_ephandle_libusb1::~fusb_ephandle_libusb1 ()
353 libusb_transfer *lut;
355 while ((lut = free_list_get ()) != 0)
358 while ((lut = completed_list_get ()) != 0)
361 if (d_write_work_in_progress)
362 free_lut (d_write_work_in_progress);
364 delete [] d_write_buffer;
366 if (d_read_work_in_progress)
367 free_lut (d_read_work_in_progress);
372 fusb_ephandle_libusb1::start ()
380 libusb_transfer *lut;
383 while ((lut = free_list_get ()) !=0 && nerrors < d_nblocks) {
384 if (!submit_lut (lut))
394 * Cancel all transfers in progress or pending and return to initial state
398 fusb_ephandle_libusb1::stop ()
404 if (d_write_work_in_progress){
405 free_list_add (d_write_work_in_progress);
406 d_write_work_in_progress = 0;
409 if (d_read_work_in_progress){
410 free_list_add (d_read_work_in_progress);
411 d_read_work_in_progress = 0;
413 d_read_buffer_end = 0;
416 d_devhandle->_cancel_pending_rqsts (this);
417 d_devhandle->_reap (false);
420 libusb_transfer *lut;
421 while ((lut = completed_list_get ()) != 0)
424 if (d_free_list.size () == (unsigned) d_nblocks)
427 if (!d_devhandle->_reap(true))
436 // ------------------------------------------------------------------------
437 // routines for writing
438 // ------------------------------------------------------------------------
440 #if (MINIMIZE_TX_BUFFERING)
443 fusb_ephandle_libusb1::write (const void *buffer, int nbytes)
445 if (!d_started) // doesn't matter here, but keeps semantics constant
451 assert(nbytes % 512 == 0);
453 unsigned char *src = (unsigned char *) buffer;
458 struct libusb_transfer *lut = get_write_work_in_progress();
461 assert(lut->actual_length == 0);
462 int m = std::min(nbytes - n, MAX_BLOCK_SIZE);
469 if (!submit_lut(lut))
472 d_write_work_in_progress = 0;
481 fusb_ephandle_libusb1::write (const void *buffer, int nbytes)
489 unsigned char *src = (unsigned char *) buffer;
494 libusb_transfer *lut = get_write_work_in_progress ();
497 unsigned char *dst = (unsigned char *) lut->buffer;
498 int m = std::min (nbytes - n, lut->length - lut->actual_length);
500 memcpy (&dst[lut->actual_length], &src[n], m);
501 lut->actual_length += m;
504 if (lut->actual_length == lut->length){
505 if (!submit_lut (lut))
507 d_write_work_in_progress = 0;
516 struct libusb_transfer *
517 fusb_ephandle_libusb1::get_write_work_in_progress ()
519 if (d_write_work_in_progress)
520 return d_write_work_in_progress;
524 reap_complete_writes ();
526 struct libusb_transfer *lut = free_list_get ();
529 assert (lut->actual_length == 0);
530 d_write_work_in_progress = lut;
534 if (!d_devhandle->_reap (true))
540 fusb_ephandle_libusb1::reap_complete_writes ()
542 // take a look at the completed list and xfer to free list after
543 // checking for errors.
545 libusb_transfer *lut;
547 while ((lut = completed_list_get ()) != 0) {
549 // Check for any errors or short writes that were reporetd in the transfer.
550 // libusb1 sets status, actual_length.
552 if (lut->status != LIBUSB_TRANSFER_COMPLETED) {
553 fprintf (stderr, "fusb: (status %d) \n", lut->status );
555 else if (lut->actual_length != lut->length){
556 fprintf (stderr, "fusb: short write xfer: %d != %d\n",
557 lut->actual_length, lut->length);
565 fusb_ephandle_libusb1::wait_for_completion ()
567 d_devhandle->_wait_for_completion ();
570 // ------------------------------------------------------------------------
571 // routines for reading
572 // ------------------------------------------------------------------------
575 fusb_ephandle_libusb1::read (void *buffer, int nbytes)
577 if (!d_started) // doesn't matter here, but keeps semantics constant
583 unsigned char *dst = (unsigned char *) buffer;
588 if (d_read_buffer >= d_read_buffer_end)
589 if (!reload_read_buffer ())
592 int m = std::min (nbytes - n, (int) (d_read_buffer_end - d_read_buffer));
594 memcpy (&dst[n], d_read_buffer, m);
604 fusb_ephandle_libusb1::reload_read_buffer ()
606 assert (d_read_buffer >= d_read_buffer_end);
608 libusb_transfer *lut;
610 if (d_read_work_in_progress) {
611 lut = d_read_work_in_progress;
612 d_read_work_in_progress = 0;
614 d_read_buffer_end = 0;
615 lut->actual_length = 0;
616 if (!submit_lut (lut))
622 while ((lut = completed_list_get ()) == 0 )
623 if (!d_devhandle->_reap(true))
626 if (lut->status != LIBUSB_TRANSFER_COMPLETED) {
627 fprintf (stderr, "fust: (rd status %d) %s\n", lut->status,
628 strerror (-lut->status));
629 lut->actual_length = 0;
634 d_read_work_in_progress = lut;
635 d_read_buffer = (unsigned char *) lut->buffer;
636 d_read_buffer_end = d_read_buffer + lut->actual_length;
644 * ephandle list manipulation
649 fusb_ephandle_libusb1::free_list_add (libusb_transfer *lut)
651 assert (lut_get_ephandle (lut) == this);
652 lut->actual_length = 0;
653 d_free_list.push_back (lut);
657 fusb_ephandle_libusb1::free_list_get ()
659 if (d_free_list.empty ())
662 libusb_transfer *lut = d_free_list.front ();
663 d_free_list.pop_front ();
668 fusb_ephandle_libusb1::completed_list_add (libusb_transfer *lut)
670 assert (lut_get_ephandle (lut) == this);
671 d_completed_list.push_back (lut);
675 fusb_ephandle_libusb1::completed_list_get ()
677 if (d_completed_list.empty ())
680 libusb_transfer *lut = d_completed_list.front ();
681 d_completed_list.pop_front ();
686 fusb_ephandle_libusb1::submit_lut (libusb_transfer *lut)
688 if (!d_devhandle->_submit_lut (lut)) {
689 fprintf (stderr, "_submit_lut failed\n");