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_linux.h>
28 #include <usb.h> // libusb header
30 #ifdef HAVE_LINUX_COMPILER_H
31 #include <linux/compiler.h>
33 #include <linux/usbdevice_fs.h> // interface to kernel portion of user mode usb driver
34 #include <sys/ioctl.h>
41 #define MINIMIZE_TX_BUFFERING 1 // must be defined to 0 or 1
44 static const int MAX_BLOCK_SIZE = fusb_sysconfig::max_block_size(); // hard limit
45 static const int DEFAULT_BLOCK_SIZE = MAX_BLOCK_SIZE;
46 static const int DEFAULT_BUFFER_SIZE = 4 * (1L << 20); // 4 MB / endpoint
49 // Totally evil and fragile extraction of file descriptor from
50 // guts of libusb. They don't install usbi.h, which is what we'd need
53 // FIXME if everything breaks someday in the future, look here...
56 fd_from_usb_dev_handle (usb_dev_handle *udh)
58 return *((int *) udh);
62 urb_set_ephandle (usbdevfs_urb *urb, fusb_ephandle_linux *handle)
64 urb->usercontext = handle;
67 inline static fusb_ephandle_linux *
68 urb_get_ephandle (usbdevfs_urb *urb)
70 return (fusb_ephandle_linux *) urb->usercontext;
73 // ------------------------------------------------------------------------
74 // USB request block (urb) allocation
75 // ------------------------------------------------------------------------
78 alloc_urb (fusb_ephandle_linux *self, int buffer_length, int endpoint,
79 bool input_p, unsigned char *write_buffer)
81 usbdevfs_urb *urb = new usbdevfs_urb;
82 memset (urb, 0, sizeof (*urb));
84 urb->buffer_length = buffer_length;
86 // We allocate dedicated memory only for input buffers.
87 // For output buffers we reuse the same buffer (the kernel
88 // copies the data at submital time)
91 urb->buffer = new unsigned char [buffer_length];
93 urb->buffer = write_buffer;
97 urb->type = USBDEVFS_URB_TYPE_BULK;
98 urb->endpoint = (endpoint & 0x7f) | (input_p ? 0x80 : 0);
100 // USBDEVFS_URB_QUEUE_BULK goes away in linux 2.5, but is needed if
101 // we are using a 2.4 usb-uhci host controller driver. This is
102 // unlikely since we're almost always going to be plugged into a
103 // high speed host controller (ehci)
104 #if 0 && defined (USBDEVFS_URB_QUEUE_BULK)
105 urb->flags = USBDEVFS_URB_QUEUE_BULK;
109 urb_set_ephandle (urb, self);
115 free_urb (usbdevfs_urb *urb)
117 // if this was an input urb, free the buffer
118 if (urb->endpoint & 0x80)
119 delete [] ((unsigned char *) urb->buffer);
124 // ------------------------------------------------------------------------
126 // ------------------------------------------------------------------------
128 fusb_devhandle_linux::fusb_devhandle_linux (usb_dev_handle *udh)
129 : fusb_devhandle (udh)
134 fusb_devhandle_linux::~fusb_devhandle_linux ()
136 // if there are any pending requests, cancel them and free the urbs.
138 std::list<usbdevfs_urb*>::reverse_iterator it;
140 for (it = d_pending_rqsts.rbegin (); it != d_pending_rqsts.rend (); it++){
147 fusb_devhandle_linux::make_ephandle (int endpoint, bool input_p,
148 int block_size, int nblocks)
150 return new fusb_ephandle_linux (this, endpoint, input_p,
151 block_size, nblocks);
155 // Attempt to cancel all transactions associated with eph.
158 fusb_devhandle_linux::_cancel_pending_rqsts (fusb_ephandle_linux *eph)
160 std::list<usbdevfs_urb*>::reverse_iterator it;
162 for (it = d_pending_rqsts.rbegin (); it != d_pending_rqsts.rend (); it++){
163 if (urb_get_ephandle (*it) == eph)
169 fusb_devhandle_linux::pending_add (usbdevfs_urb *urb)
171 d_pending_rqsts.push_back (urb);
175 fusb_devhandle_linux::pending_get ()
177 if (d_pending_rqsts.empty ())
180 usbdevfs_urb *urb = d_pending_rqsts.front ();
181 d_pending_rqsts.pop_front ();
186 fusb_devhandle_linux::pending_remove (usbdevfs_urb *urb)
188 std::list<usbdevfs_urb*>::iterator result = find (d_pending_rqsts.begin (),
189 d_pending_rqsts.end (),
191 if (result == d_pending_rqsts.end ()){
192 fprintf (stderr, "fusb::pending_remove: failed to find urb in pending_rqsts: %p\n", urb);
195 d_pending_rqsts.erase (result);
200 * Submit the urb to the kernel.
201 * iff successful, the urb will be placed on the devhandle's pending list.
204 fusb_devhandle_linux::_submit_urb (usbdevfs_urb *urb)
208 ret = ioctl (fd_from_usb_dev_handle (d_udh), USBDEVFS_SUBMITURB, urb);
210 perror ("fusb::_submit_urb");
219 * Attempt to cancel the in pending or in-progress urb transaction.
220 * Return true iff transaction was sucessfully cancelled.
222 * Failure to cancel should not be considered a problem. This frequently
223 * occurs if the transaction has already completed in the kernel but hasn't
224 * yet been reaped by the user mode code.
226 * urbs which were cancelled have their status field set to -ENOENT when
230 fusb_devhandle_linux::_cancel_urb (usbdevfs_urb *urb)
232 int ret = ioctl (fd_from_usb_dev_handle (d_udh), USBDEVFS_DISCARDURB, urb);
234 // perror ("fusb::_cancel_urb");
241 * Check with the kernel and see if any of our outstanding requests
242 * have completed. For each completed transaction, remove it from the
243 * devhandle's pending list and append it to the completed list for
244 * the corresponding endpoint.
246 * If any transactions are reaped return true.
248 * If ok_to_block_p is true, then this will block until at least one
249 * transaction completes.
252 fusb_devhandle_linux::_reap (bool ok_to_block_p)
256 usbdevfs_urb *urb = 0;
258 int fd = fd_from_usb_dev_handle (d_udh);
260 // try to reap as many as possible without blocking...
262 while ((ret = ioctl (fd, USBDEVFS_REAPURBNDELAY, &urb)) == 0){
263 if (urb->status != 0 && urb->status != -ENOENT){
264 fprintf (stderr, "_reap: usb->status = %d, actual_length = %5d\n",
265 urb->status, urb->actual_length);
267 pending_remove (urb);
268 urb_get_ephandle (urb)->completed_list_add (urb);
272 if (nreaped > 0) // if we got any, return w/o blocking
278 ret = ioctl (fd, USBDEVFS_REAPURB, &urb);
280 perror ("fusb::_reap");
284 pending_remove (urb);
285 urb_get_ephandle (urb)->completed_list_add (urb);
290 fusb_devhandle_linux::_wait_for_completion ()
292 while (!d_pending_rqsts.empty ())
295 \f// ------------------------------------------------------------------------
297 // ------------------------------------------------------------------------
299 fusb_ephandle_linux::fusb_ephandle_linux (fusb_devhandle_linux *devhandle,
302 int block_size, int nblocks)
303 : fusb_ephandle (endpoint, input_p, block_size, nblocks),
304 d_devhandle (devhandle),
305 d_write_work_in_progress (0), d_write_buffer (0),
306 d_read_work_in_progress (0), d_read_buffer (0), d_read_buffer_end (0)
309 if (d_block_size < 0 || d_block_size > MAX_BLOCK_SIZE)
310 throw std::out_of_range ("fusb_ephandle_linux: block_size");
313 throw std::out_of_range ("fusb_ephandle_linux: nblocks");
315 if (d_block_size == 0)
316 d_block_size = DEFAULT_BLOCK_SIZE;
319 d_nblocks = std::max (1, DEFAULT_BUFFER_SIZE / d_block_size);
322 if (!MINIMIZE_TX_BUFFERING)
323 d_write_buffer = new unsigned char [d_block_size];
326 fprintf(stderr, "fusb_ephandle_linux::ctor: d_block_size = %d d_nblocks = %d\n",
327 d_block_size, d_nblocks);
331 for (int i = 0; i < d_nblocks; i++)
332 d_free_list.push_back (alloc_urb (this, d_block_size, d_endpoint,
333 d_input_p, d_write_buffer));
336 fusb_ephandle_linux::~fusb_ephandle_linux ()
342 while ((urb = free_list_get ()) != 0)
345 while ((urb = completed_list_get ()) != 0)
348 if (d_write_work_in_progress)
349 free_urb (d_write_work_in_progress);
351 delete [] d_write_buffer;
353 if (d_read_work_in_progress)
354 free_urb (d_read_work_in_progress);
357 // ----------------------------------------------------------------
360 fusb_ephandle_linux::start ()
363 return true; // already running
367 if (d_input_p){ // fire off all the reads
371 while ((urb = free_list_get ()) != 0 && nerrors < d_nblocks){
372 if (!submit_urb (urb))
381 // kill all i/o in progress.
382 // kill any completed but unprocessed transactions.
385 fusb_ephandle_linux::stop ()
390 d_devhandle->_cancel_pending_rqsts (this);
391 d_devhandle->_reap (false);
395 while ((urb = completed_list_get ()) != 0)
398 if (d_write_work_in_progress){
399 free_list_add (d_write_work_in_progress);
400 d_write_work_in_progress = 0;
403 if (d_read_work_in_progress){
404 free_list_add (d_read_work_in_progress);
405 d_read_work_in_progress = 0;
407 d_read_buffer_end = 0;
410 if (d_free_list.size () != (unsigned) d_nblocks)
411 fprintf (stderr, "d_free_list.size () = %d, d_nblocks = %d\n",
412 d_free_list.size (), d_nblocks);
414 assert (d_free_list.size () == (unsigned) d_nblocks);
420 // ----------------------------------------------------------------
421 // routines for writing
422 // ----------------------------------------------------------------
424 #if (MINIMIZE_TX_BUFFERING)
427 fusb_ephandle_linux::write(const void *buffer, int nbytes)
435 assert(nbytes % 512 == 0);
437 unsigned char *src = (unsigned char *) buffer;
442 usbdevfs_urb *urb = get_write_work_in_progress();
443 assert(urb->actual_length == 0);
444 int m = std::min(nbytes - n, MAX_BLOCK_SIZE);
446 urb->buffer_length = m;
451 if (!submit_urb(urb))
454 d_write_work_in_progress = 0;
463 fusb_ephandle_linux::write (const void *buffer, int nbytes)
471 unsigned char *src = (unsigned char *) buffer;
476 usbdevfs_urb *urb = get_write_work_in_progress ();
477 unsigned char *dst = (unsigned char *) urb->buffer;
478 int m = std::min (nbytes - n, urb->buffer_length - urb->actual_length);
480 memcpy (&dst[urb->actual_length], &src[n], m);
481 urb->actual_length += m;
484 if (urb->actual_length == urb->buffer_length){
485 if (!submit_urb (urb))
487 d_write_work_in_progress = 0;
497 fusb_ephandle_linux::get_write_work_in_progress ()
499 // if we've already got some work in progress, return it
501 if (d_write_work_in_progress)
502 return d_write_work_in_progress;
506 reap_complete_writes ();
508 usbdevfs_urb *urb = free_list_get ();
511 assert (urb->actual_length == 0);
512 d_write_work_in_progress = urb;
516 // The free list is empty. Tell the device handle to reap.
517 // Anything it reaps for us will end up on our completed list.
519 d_devhandle->_reap (true);
524 fusb_ephandle_linux::reap_complete_writes ()
526 // take a look at the completed_list and xfer to free list after
527 // checking for errors.
531 while ((urb = completed_list_get ()) != 0){
533 // Check for any errors or short writes that were reported in the urb.
534 // The kernel sets status, actual_length and error_count.
535 // error_count is only used for ISO xfers.
536 // status is 0 if successful, else is an errno kind of thing
538 if (urb->status != 0){
539 fprintf (stderr, "fusb: (status %d) %s\n", urb->status, strerror (-urb->status));
541 else if (urb->actual_length != urb->buffer_length){
542 fprintf (stderr, "fusb: short write xfer: %d != %d\n",
543 urb->actual_length, urb->buffer_length);
551 fusb_ephandle_linux::wait_for_completion ()
553 d_devhandle->_wait_for_completion ();
556 // ----------------------------------------------------------------
557 // routines for reading
558 // ----------------------------------------------------------------
561 fusb_ephandle_linux::read (void *buffer, int nbytes)
569 unsigned char *dst = (unsigned char *) buffer;
574 if (d_read_buffer >= d_read_buffer_end)
575 if (!reload_read_buffer ())
578 int m = std::min (nbytes - n, (int) (d_read_buffer_end - d_read_buffer));
580 memcpy (&dst[n], d_read_buffer, m);
589 fusb_ephandle_linux::reload_read_buffer ()
591 assert (d_read_buffer >= d_read_buffer_end);
595 if (d_read_work_in_progress){
596 // We're done with this urb. Fire off a read to refill it.
597 urb = d_read_work_in_progress;
598 d_read_work_in_progress = 0;
600 d_read_buffer_end = 0;
601 urb->actual_length = 0;
602 if (!submit_urb (urb))
608 while ((urb = completed_list_get ()) == 0)
609 d_devhandle->_reap (true);
611 // check result of completed read
613 if (urb->status != 0){
614 // We've got a problem.
615 // Report the problem and resubmit.
616 fprintf (stderr, "fusb: (rd status %d) %s\n", urb->status, strerror (-urb->status));
617 urb->actual_length = 0;
618 if (!submit_urb (urb))
624 // we've got a happy urb, full of data...
626 d_read_work_in_progress = urb;
627 d_read_buffer = (unsigned char *) urb->buffer;
628 d_read_buffer_end = d_read_buffer + urb->actual_length;
634 // ----------------------------------------------------------------
637 fusb_ephandle_linux::free_list_add (usbdevfs_urb *urb)
639 assert (urb_get_ephandle (urb) == this);
640 urb->actual_length = 0;
641 d_free_list.push_back (urb);
645 fusb_ephandle_linux::free_list_get ()
647 if (d_free_list.empty ())
650 usbdevfs_urb *urb = d_free_list.front ();
651 d_free_list.pop_front ();
656 fusb_ephandle_linux::completed_list_add (usbdevfs_urb *urb)
658 assert (urb_get_ephandle (urb) == this);
659 d_completed_list.push_back (urb);
663 fusb_ephandle_linux::completed_list_get ()
665 if (d_completed_list.empty ())
668 usbdevfs_urb *urb = d_completed_list.front ();
669 d_completed_list.pop_front ();
674 * Submit the urb. If successful the urb ends up on the devhandle's
675 * pending list, otherwise, it's back on our free list.
678 fusb_ephandle_linux::submit_urb (usbdevfs_urb *urb)
680 if (!d_devhandle->_submit_urb (urb)){ // FIXME record the problem somewhere
681 fprintf (stderr, "_submit_urb failed\n");