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)
283 int pnd_size = d_pending_rqsts.size();
294 if ((ret = libusb_handle_events_timeout(d_ctx, &tv)) < 0) {
295 fprintf (stderr, "fusb::_reap libusb_handle_events() %i\n", ret);
299 // Check that a pending transfer was removed
300 if (pnd_size > d_pending_rqsts.size())
308 fusb_devhandle_libusb1::_wait_for_completion ()
311 while (!d_pending_rqsts.empty ())
317 // ------------------------------------------------------------------------
319 // ------------------------------------------------------------------------
321 fusb_ephandle_libusb1::fusb_ephandle_libusb1 (fusb_devhandle_libusb1 *dh,
322 int endpoint, bool input_p,
323 int block_size, int nblocks)
324 : fusb_ephandle (endpoint, input_p, block_size, nblocks),
326 d_write_work_in_progress (0), d_write_buffer (0),
327 d_read_work_in_progress (0), d_read_buffer (0), d_read_buffer_end (0)
330 if (d_block_size < 0 || d_block_size > MAX_BLOCK_SIZE)
331 throw std::out_of_range ("fusb_ephandle_libusb1: block_size");
334 throw std::out_of_range ("fusb_ephandle_libusb1: nblocks");
336 if (d_block_size == 0)
337 d_block_size = DEFAULT_BLOCK_SIZE;
340 d_nblocks = std::max (1, DEFAULT_BUFFER_SIZE / d_block_size);
343 if (!MINIMIZE_TX_BUFFERING)
344 d_write_buffer = new unsigned char [d_block_size];
347 fprintf(stderr, "fusb_ephandle_libusb1::ctor: d_block_size = %d d_nblocks = %d\n",
348 d_block_size, d_nblocks);
350 // allocate libusb_transfers
351 for (int i = 0; i < d_nblocks; i++)
352 d_free_list.push_back (alloc_lut (this, d_block_size, d_endpoint,
353 d_input_p, d_write_buffer, d_devhandle));
356 fusb_ephandle_libusb1::~fusb_ephandle_libusb1 ()
361 libusb_transfer *lut;
363 while ((lut = free_list_get ()) != 0)
366 while ((lut = completed_list_get ()) != 0)
369 if (d_write_work_in_progress)
370 free_lut (d_write_work_in_progress);
372 delete [] d_write_buffer;
374 if (d_read_work_in_progress)
375 free_lut (d_read_work_in_progress);
380 fusb_ephandle_libusb1::start ()
388 libusb_transfer *lut;
391 while ((lut = free_list_get ()) !=0 && nerrors < d_nblocks) {
392 if (!submit_lut (lut))
402 * Cancel all transfers in progress or pending and return to initial state
406 fusb_ephandle_libusb1::stop ()
412 if (d_write_work_in_progress){
413 free_list_add (d_write_work_in_progress);
414 d_write_work_in_progress = 0;
417 if (d_read_work_in_progress){
418 free_list_add (d_read_work_in_progress);
419 d_read_work_in_progress = 0;
421 d_read_buffer_end = 0;
424 d_devhandle->_cancel_pending_rqsts (this);
425 d_devhandle->_reap (false);
428 libusb_transfer *lut;
429 while ((lut = completed_list_get ()) != 0)
432 if (d_free_list.size () == (unsigned) d_nblocks)
435 if (!d_devhandle->_reap(true))
444 // ------------------------------------------------------------------------
445 // routines for writing
446 // ------------------------------------------------------------------------
448 #if (MINIMIZE_TX_BUFFERING)
451 fusb_ephandle_libusb1::write (const void *buffer, int nbytes)
453 if (!d_started) // doesn't matter here, but keeps semantics constant
459 assert(nbytes % 512 == 0);
461 unsigned char *src = (unsigned char *) buffer;
466 struct libusb_transfer *lut = get_write_work_in_progress();
469 assert(lut->actual_length == 0);
470 int m = std::min(nbytes - n, MAX_BLOCK_SIZE);
477 if (!submit_lut(lut))
480 d_write_work_in_progress = 0;
489 fusb_ephandle_libusb1::write (const void *buffer, int nbytes)
497 unsigned char *src = (unsigned char *) buffer;
502 libusb_transfer *lut = get_write_work_in_progress ();
505 unsigned char *dst = (unsigned char *) lut->buffer;
506 int m = std::min (nbytes - n, lut->length - lut->actual_length);
508 memcpy (&dst[lut->actual_length], &src[n], m);
509 lut->actual_length += m;
512 if (lut->actual_length == lut->length){
513 if (!submit_lut (lut))
515 d_write_work_in_progress = 0;
524 struct libusb_transfer *
525 fusb_ephandle_libusb1::get_write_work_in_progress ()
527 if (d_write_work_in_progress)
528 return d_write_work_in_progress;
532 reap_complete_writes ();
534 struct libusb_transfer *lut = free_list_get ();
537 assert (lut->actual_length == 0);
538 d_write_work_in_progress = lut;
542 if (!d_devhandle->_reap (true))
548 fusb_ephandle_libusb1::reap_complete_writes ()
550 // take a look at the completed list and xfer to free list after
551 // checking for errors.
553 libusb_transfer *lut;
555 while ((lut = completed_list_get ()) != 0) {
557 // Check for any errors or short writes that were reporetd in the transfer.
558 // libusb1 sets status, actual_length.
560 if (lut->status != LIBUSB_TRANSFER_COMPLETED) {
561 fprintf (stderr, "fusb: (status %d) \n", lut->status );
563 else if (lut->actual_length != lut->length){
564 fprintf (stderr, "fusb: short write xfer: %d != %d\n",
565 lut->actual_length, lut->length);
573 fusb_ephandle_libusb1::wait_for_completion ()
575 d_devhandle->_wait_for_completion ();
578 // ------------------------------------------------------------------------
579 // routines for reading
580 // ------------------------------------------------------------------------
583 fusb_ephandle_libusb1::read (void *buffer, int nbytes)
585 if (!d_started) // doesn't matter here, but keeps semantics constant
591 unsigned char *dst = (unsigned char *) buffer;
596 if (d_read_buffer >= d_read_buffer_end)
597 if (!reload_read_buffer ())
600 int m = std::min (nbytes - n, (int) (d_read_buffer_end - d_read_buffer));
602 memcpy (&dst[n], d_read_buffer, m);
612 fusb_ephandle_libusb1::reload_read_buffer ()
614 assert (d_read_buffer >= d_read_buffer_end);
616 libusb_transfer *lut;
618 if (d_read_work_in_progress) {
619 lut = d_read_work_in_progress;
620 d_read_work_in_progress = 0;
622 d_read_buffer_end = 0;
623 lut->actual_length = 0;
624 if (!submit_lut (lut))
630 while ((lut = completed_list_get ()) == 0 )
631 if (!d_devhandle->_reap(true))
634 if (lut->status != LIBUSB_TRANSFER_COMPLETED) {
635 fprintf (stderr, "fust: (rd status %d) %s\n", lut->status,
636 strerror (-lut->status));
637 lut->actual_length = 0;
642 d_read_work_in_progress = lut;
643 d_read_buffer = (unsigned char *) lut->buffer;
644 d_read_buffer_end = d_read_buffer + lut->actual_length;
652 * ephandle list manipulation
657 fusb_ephandle_libusb1::free_list_add (libusb_transfer *lut)
659 assert (lut_get_ephandle (lut) == this);
660 lut->actual_length = 0;
661 d_free_list.push_back (lut);
665 fusb_ephandle_libusb1::free_list_get ()
667 if (d_free_list.empty ())
670 libusb_transfer *lut = d_free_list.front ();
671 d_free_list.pop_front ();
676 fusb_ephandle_libusb1::completed_list_add (libusb_transfer *lut)
678 assert (lut_get_ephandle (lut) == this);
679 d_completed_list.push_back (lut);
683 fusb_ephandle_libusb1::completed_list_get ()
685 if (d_completed_list.empty ())
688 libusb_transfer *lut = d_completed_list.front ();
689 d_completed_list.pop_front ();
694 fusb_ephandle_libusb1::submit_lut (libusb_transfer *lut)
696 if (!d_devhandle->_submit_lut (lut)) {
697 fprintf (stderr, "_submit_lut failed\n");