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.
23 //#ifdef HAVE_CONFIG_H
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_usb_dev_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,
129 : fusb_devhandle (udh), d_ctx (ctx), d_teardown (false)
134 fusb_devhandle_libusb1::~fusb_devhandle_libusb1 ()
138 std::list<libusb_transfer*>::reverse_iterator it;
140 // After cancellation the libusb_transfer is still active so delay freeing
141 // transfer until callback occurs. In most cases the pending list should
142 // already be empty by the time this destructor is called.
144 for (it = d_pending_rqsts.rbegin (); it != d_pending_rqsts.rend (); it++) {
148 // Wait for pending list to empty
149 _wait_for_completion ();
154 fusb_devhandle_libusb1::make_ephandle (int endpoint, bool input_p,
155 int block_size, int nblocks)
157 return new fusb_ephandle_libusb1 (this, endpoint, input_p,
158 block_size, nblocks);
162 * devhandle list manipulators
166 fusb_devhandle_libusb1::pending_add (libusb_transfer *lut)
168 d_pending_rqsts.push_back (lut);
173 * Attempt to cancel all transations associated with eph
177 fusb_devhandle_libusb1::_cancel_pending_rqsts (fusb_ephandle_libusb1 *eph)
179 std::list<libusb_transfer*>::reverse_iterator it;
181 for (it = d_pending_rqsts.rbegin (); it != d_pending_rqsts.rend (); it++){
182 if (lut_get_ephandle (*it) == eph)
188 * Pull from the pending list
192 fusb_devhandle_libusb1::pending_get ()
194 if (d_pending_rqsts.empty ())
197 libusb_transfer *lut = d_pending_rqsts.front ();
198 d_pending_rqsts.pop_front ();
203 * Match libusb_tranfer with the pending list and erase
204 * Return true if found, false otherwise
208 fusb_devhandle_libusb1::pending_remove (libusb_transfer *lut)
210 std::list<libusb_transfer*>::iterator result;
211 result = find (d_pending_rqsts.begin (), d_pending_rqsts.end (), lut);
213 if (result == d_pending_rqsts.end ()) {
214 fprintf (stderr, "fusb::pending_remove: failed to find lut in pending_rqsts: %p\n", lut);
218 d_pending_rqsts.erase (result);
223 * Submit the libusb_transfer to libusb
224 * iff successful, the transfer will be placed on the devhandle pending list.
228 fusb_devhandle_libusb1::_submit_lut (libusb_transfer *lut)
231 int ret = libusb_submit_transfer (lut);
233 fprintf(stderr, "fusb::_submit_lut %d", ret);
243 * Attempt to cancel any pending libusb_transfer transactions.
244 * Return true in the absence of errors, which does not mean that the transfer
245 * is cancelled. Cancellation can be checked after the callback is fired off
250 fusb_devhandle_libusb1::_cancel_lut (libusb_transfer *lut)
253 int ret = libusb_cancel_transfer (lut);
255 fprintf (stderr, "fusb::_cancel_lut");
263 * Reimplementing _reap for context use and compatibiliy with libusb-0.12.
265 * Returns false on timeout or error, true if an event was handled
267 * If ok_to_block_p is false then handle already pending events and return
270 * If ok_to_block_p is true then call libusb_handle_events_timeout with default
271 * timeout value of 2 seconds, which waits and returns on event arrival or
276 fusb_devhandle_libusb1::_reap (bool ok_to_block_p)
290 if ((ret = libusb_handle_events_timeout(d_ctx, &tv)) < 0) {
291 fprintf (stderr, "fusb::_reap libusb_handle_events() %i\n", ret);
299 fusb_devhandle_libusb1::_wait_for_completion ()
302 while (!d_pending_rqsts.empty ())
308 // ------------------------------------------------------------------------
310 // ------------------------------------------------------------------------
312 fusb_ephandle_libusb1::fusb_ephandle_libusb1 (fusb_devhandle_libusb1 *dh,
313 int endpoint, bool input_p,
314 int block_size, int nblocks)
315 : fusb_ephandle (endpoint, input_p, block_size, nblocks),
317 d_write_work_in_progress (0), d_write_buffer (0),
318 d_read_work_in_progress (0), d_read_buffer (0), d_read_buffer_end (0)
321 if (d_block_size < 0 || d_block_size > MAX_BLOCK_SIZE)
322 throw std::out_of_range ("fusb_ephandle_libusb1: block_size");
325 throw std::out_of_range ("fusb_ephandle_libusb1: nblocks");
327 if (d_block_size == 0)
328 d_block_size = DEFAULT_BLOCK_SIZE;
331 d_nblocks = std::max (1, DEFAULT_BUFFER_SIZE / d_block_size);
334 if (!MINIMIZE_TX_BUFFERING)
335 d_write_buffer = new unsigned char [d_block_size];
338 fprintf(stderr, "fusb_ephandle_libusb1::ctor: d_block_size = %d d_nblocks = %d\n",
339 d_block_size, d_nblocks);
341 // allocate libusb_transfers
342 for (int i = 0; i < d_nblocks; i++)
343 d_free_list.push_back (alloc_lut (this, d_block_size, d_endpoint,
344 d_input_p, d_write_buffer, d_devhandle));
347 fusb_ephandle_libusb1::~fusb_ephandle_libusb1 ()
352 libusb_transfer *lut;
354 while ((lut = free_list_get ()) != 0)
357 while ((lut = completed_list_get ()) != 0)
360 if (d_write_work_in_progress)
361 free_lut (d_write_work_in_progress);
363 delete [] d_write_buffer;
365 if (d_read_work_in_progress)
366 free_lut (d_read_work_in_progress);
371 fusb_ephandle_libusb1::start ()
379 libusb_transfer *lut;
382 while ((lut = free_list_get ()) !=0 && nerrors < d_nblocks) {
383 if (!submit_lut (lut))
393 * Cancel all transfers in progress or pending and return to initial state
397 fusb_ephandle_libusb1::stop ()
403 if (d_write_work_in_progress){
404 free_list_add (d_write_work_in_progress);
405 d_write_work_in_progress = 0;
408 if (d_read_work_in_progress){
409 free_list_add (d_read_work_in_progress);
410 d_read_work_in_progress = 0;
412 d_read_buffer_end = 0;
415 d_devhandle->_cancel_pending_rqsts (this);
416 d_devhandle->_reap (false);
419 libusb_transfer *lut;
420 while ((lut = completed_list_get ()) != 0)
423 if (d_free_list.size () == (unsigned) d_nblocks)
426 if (!d_devhandle->_reap(true))
435 // ------------------------------------------------------------------------
436 // routines for writing
437 // ------------------------------------------------------------------------
439 #if (MINIMIZE_TX_BUFFERING)
442 fusb_ephandle_libusb1::write (const void *buffer, int nbytes)
444 if (!d_started) // doesn't matter here, but keeps semantics constant
450 assert(nbytes % 512 == 0);
452 unsigned char *src = (unsigned char *) buffer;
457 struct libusb_transfer *lut = get_write_work_in_progress();
460 assert(lut->actual_length == 0);
461 int m = std::min(nbytes - n, MAX_BLOCK_SIZE);
468 if (!submit_lut(lut))
471 d_write_work_in_progress = 0;
480 fusb_ephandle_libusb1::write (const void *buffer, int nbytes)
488 unsigned char *src = (unsigned char *) buffer;
493 libusb_transfer *lut = get_write_work_in_progress ();
496 unsigned char *dst = (unsigned char *) lut->buffer;
497 int m = std::min (nbytes - n, lut->length - lut->actual_length);
499 memcpy (&dst[lut->actual_length], &src[n], m);
500 lut->actual_length += m;
503 if (lut->actual_length == lut->length){
504 if (!submit_lut (lut))
506 d_write_work_in_progress = 0;
515 struct libusb_transfer *
516 fusb_ephandle_libusb1::get_write_work_in_progress ()
518 if (d_write_work_in_progress)
519 return d_write_work_in_progress;
523 reap_complete_writes ();
525 struct libusb_transfer *lut = free_list_get ();
528 assert (lut->actual_length == 0);
529 d_write_work_in_progress = lut;
533 if (!d_devhandle->_reap (true))
539 fusb_ephandle_libusb1::reap_complete_writes ()
541 // take a look at the completed list and xfer to free list after
542 // checking for errors.
544 libusb_transfer *lut;
546 while ((lut = completed_list_get ()) != 0) {
548 // Check for any errors or short writes that were reporetd in the transfer.
549 // libusb1 sets status, actual_length.
551 if (lut->status != LIBUSB_TRANSFER_COMPLETED) {
552 fprintf (stderr, "fusb: (status %d) \n", lut->status );
554 else if (lut->actual_length != lut->length){
555 fprintf (stderr, "fusb: short write xfer: %d != %d\n",
556 lut->actual_length, lut->length);
564 fusb_ephandle_libusb1::wait_for_completion ()
566 d_devhandle->_wait_for_completion ();
569 // ------------------------------------------------------------------------
570 // routines for reading
571 // ------------------------------------------------------------------------
574 fusb_ephandle_libusb1::read (void *buffer, int nbytes)
576 if (!d_started) // doesn't matter here, but keeps semantics constant
582 unsigned char *dst = (unsigned char *) buffer;
587 if (d_read_buffer >= d_read_buffer_end)
588 if (!reload_read_buffer ())
591 int m = std::min (nbytes - n, (int) (d_read_buffer_end - d_read_buffer));
593 memcpy (&dst[n], d_read_buffer, m);
603 fusb_ephandle_libusb1::reload_read_buffer ()
605 assert (d_read_buffer >= d_read_buffer_end);
607 libusb_transfer *lut;
609 if (d_read_work_in_progress) {
610 lut = d_read_work_in_progress;
611 d_read_work_in_progress = 0;
613 d_read_buffer_end = 0;
614 lut->actual_length = 0;
615 if (!submit_lut (lut))
621 while ((lut = completed_list_get ()) == 0 )
622 if (!d_devhandle->_reap(true))
625 if (lut->status != LIBUSB_TRANSFER_COMPLETED) {
626 fprintf (stderr, "fust: (rd status %d) %s\n", lut->status,
627 strerror (-lut->status));
628 lut->actual_length = 0;
633 d_read_work_in_progress = lut;
634 d_read_buffer = (unsigned char *) lut->buffer;
635 d_read_buffer_end = d_read_buffer + lut->actual_length;
643 * ephandle list manipulation
648 fusb_ephandle_libusb1::free_list_add (libusb_transfer *lut)
650 assert (lut_get_ephandle (lut) == this);
651 lut->actual_length = 0;
652 d_free_list.push_back (lut);
656 fusb_ephandle_libusb1::free_list_get ()
658 if (d_free_list.empty ())
661 libusb_transfer *lut = d_free_list.front ();
662 d_free_list.pop_front ();
667 fusb_ephandle_libusb1::completed_list_add (libusb_transfer *lut)
669 assert (lut_get_ephandle (lut) == this);
670 d_completed_list.push_back (lut);
674 fusb_ephandle_libusb1::completed_list_get ()
676 if (d_completed_list.empty ())
679 libusb_transfer *lut = d_completed_list.front ();
680 d_completed_list.pop_front ();
685 fusb_ephandle_libusb1::submit_lut (libusb_transfer *lut)
687 if (!d_devhandle->_submit_lut (lut)) {
688 fprintf (stderr, "_submit_lut failed\n");