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 2, 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 #include <linux/compiler.h>
31 #include <linux/usbdevice_fs.h> // interface to kernel portion of user mode usb driver
32 #include <sys/ioctl.h>
39 #define MINIMIZE_TX_BUFFERING 1 // must be defined to 0 or 1
42 static const int MAX_BLOCK_SIZE = fusb_sysconfig::max_block_size(); // hard limit
43 static const int DEFAULT_BLOCK_SIZE = MAX_BLOCK_SIZE;
44 static const int DEFAULT_BUFFER_SIZE = 4 * (1L << 20); // 4 MB / endpoint
47 // Totally evil and fragile extraction of file descriptor from
48 // guts of libusb. They don't install usbi.h, which is what we'd need
51 // FIXME if everything breaks someday in the future, look here...
54 fd_from_usb_dev_handle (usb_dev_handle *udh)
56 return *((int *) udh);
60 urb_set_ephandle (usbdevfs_urb *urb, fusb_ephandle_linux *handle)
62 urb->usercontext = handle;
65 inline static fusb_ephandle_linux *
66 urb_get_ephandle (usbdevfs_urb *urb)
68 return (fusb_ephandle_linux *) urb->usercontext;
71 // ------------------------------------------------------------------------
72 // USB request block (urb) allocation
73 // ------------------------------------------------------------------------
76 alloc_urb (fusb_ephandle_linux *self, int buffer_length, int endpoint,
77 bool input_p, unsigned char *write_buffer)
79 usbdevfs_urb *urb = new usbdevfs_urb;
80 memset (urb, 0, sizeof (*urb));
82 urb->buffer_length = buffer_length;
84 // We allocate dedicated memory only for input buffers.
85 // For output buffers we reuse the same buffer (the kernel
86 // copies the data at submital time)
89 urb->buffer = new unsigned char [buffer_length];
91 urb->buffer = write_buffer;
95 urb->type = USBDEVFS_URB_TYPE_BULK;
96 urb->endpoint = (endpoint & 0x7f) | (input_p ? 0x80 : 0);
98 // USBDEVFS_URB_QUEUE_BULK goes away in linux 2.5, but is needed if
99 // we are using a 2.4 usb-uhci host controller driver. This is
100 // unlikely since we're almost always going to be plugged into a
101 // high speed host controller (ehci)
102 #if 0 && defined (USBDEVFS_URB_QUEUE_BULK)
103 urb->flags = USBDEVFS_URB_QUEUE_BULK;
107 urb_set_ephandle (urb, self);
113 free_urb (usbdevfs_urb *urb)
115 // if this was an input urb, free the buffer
116 if (urb->endpoint & 0x80)
117 delete [] ((unsigned char *) urb->buffer);
122 // ------------------------------------------------------------------------
124 // ------------------------------------------------------------------------
126 fusb_devhandle_linux::fusb_devhandle_linux (usb_dev_handle *udh)
127 : fusb_devhandle (udh)
132 fusb_devhandle_linux::~fusb_devhandle_linux ()
134 // if there are any pending requests, cancel them and free the urbs.
136 std::list<usbdevfs_urb*>::reverse_iterator it;
138 for (it = d_pending_rqsts.rbegin (); it != d_pending_rqsts.rend (); it++){
145 fusb_devhandle_linux::make_ephandle (int endpoint, bool input_p,
146 int block_size, int nblocks)
148 return new fusb_ephandle_linux (this, endpoint, input_p,
149 block_size, nblocks);
153 // Attempt to cancel all transactions associated with eph.
156 fusb_devhandle_linux::_cancel_pending_rqsts (fusb_ephandle_linux *eph)
158 std::list<usbdevfs_urb*>::reverse_iterator it;
160 for (it = d_pending_rqsts.rbegin (); it != d_pending_rqsts.rend (); it++){
161 if (urb_get_ephandle (*it) == eph)
167 fusb_devhandle_linux::pending_add (usbdevfs_urb *urb)
169 d_pending_rqsts.push_back (urb);
173 fusb_devhandle_linux::pending_get ()
175 if (d_pending_rqsts.empty ())
178 usbdevfs_urb *urb = d_pending_rqsts.front ();
179 d_pending_rqsts.pop_front ();
184 fusb_devhandle_linux::pending_remove (usbdevfs_urb *urb)
186 std::list<usbdevfs_urb*>::iterator result = find (d_pending_rqsts.begin (),
187 d_pending_rqsts.end (),
189 if (result == d_pending_rqsts.end ()){
190 fprintf (stderr, "fusb::pending_remove: failed to find urb in pending_rqsts: %p\n", urb);
193 d_pending_rqsts.erase (result);
198 * Submit the urb to the kernel.
199 * iff successful, the urb will be placed on the devhandle's pending list.
202 fusb_devhandle_linux::_submit_urb (usbdevfs_urb *urb)
206 ret = ioctl (fd_from_usb_dev_handle (d_udh), USBDEVFS_SUBMITURB, urb);
208 perror ("fusb::_submit_urb");
217 * Attempt to cancel the in pending or in-progress urb transaction.
218 * Return true iff transaction was sucessfully cancelled.
220 * Failure to cancel should not be considered a problem. This frequently
221 * occurs if the transaction has already completed in the kernel but hasn't
222 * yet been reaped by the user mode code.
224 * urbs which were cancelled have their status field set to -ENOENT when
228 fusb_devhandle_linux::_cancel_urb (usbdevfs_urb *urb)
230 int ret = ioctl (fd_from_usb_dev_handle (d_udh), USBDEVFS_DISCARDURB, urb);
232 // perror ("fusb::_cancel_urb");
239 * Check with the kernel and see if any of our outstanding requests
240 * have completed. For each completed transaction, remove it from the
241 * devhandle's pending list and append it to the completed list for
242 * the corresponding endpoint.
244 * If any transactions are reaped return true.
246 * If ok_to_block_p is true, then this will block until at least one
247 * transaction completes.
250 fusb_devhandle_linux::_reap (bool ok_to_block_p)
254 usbdevfs_urb *urb = 0;
256 int fd = fd_from_usb_dev_handle (d_udh);
258 // try to reap as many as possible without blocking...
260 while ((ret = ioctl (fd, USBDEVFS_REAPURBNDELAY, &urb)) == 0){
261 if (urb->status != 0 && urb->status != -ENOENT){
262 fprintf (stderr, "_reap: usb->status = %d, actual_length = %5d\n",
263 urb->status, urb->actual_length);
265 pending_remove (urb);
266 urb_get_ephandle (urb)->completed_list_add (urb);
270 if (nreaped > 0) // if we got any, return w/o blocking
276 ret = ioctl (fd, USBDEVFS_REAPURB, &urb);
278 perror ("fusb::_reap");
282 pending_remove (urb);
283 urb_get_ephandle (urb)->completed_list_add (urb);
288 fusb_devhandle_linux::_wait_for_completion ()
290 while (!d_pending_rqsts.empty ())
293 \f// ------------------------------------------------------------------------
295 // ------------------------------------------------------------------------
297 fusb_ephandle_linux::fusb_ephandle_linux (fusb_devhandle_linux *devhandle,
300 int block_size, int nblocks)
301 : fusb_ephandle (endpoint, input_p, block_size, nblocks),
302 d_devhandle (devhandle),
303 d_write_work_in_progress (0), d_write_buffer (0),
304 d_read_work_in_progress (0), d_read_buffer (0), d_read_buffer_end (0)
307 if (d_block_size < 0 || d_block_size > MAX_BLOCK_SIZE)
308 throw std::out_of_range ("fusb_ephandle_linux: block_size");
311 throw std::out_of_range ("fusb_ephandle_linux: nblocks");
313 if (d_block_size == 0)
314 d_block_size = DEFAULT_BLOCK_SIZE;
317 d_nblocks = std::max (1, DEFAULT_BUFFER_SIZE / d_block_size);
320 if (!MINIMIZE_TX_BUFFERING)
321 d_write_buffer = new unsigned char [d_block_size];
324 fprintf(stderr, "fusb_ephandle_linux::ctor: d_block_size = %d d_nblocks = %d\n",
325 d_block_size, d_nblocks);
329 for (int i = 0; i < d_nblocks; i++)
330 d_free_list.push_back (alloc_urb (this, d_block_size, d_endpoint,
331 d_input_p, d_write_buffer));
334 fusb_ephandle_linux::~fusb_ephandle_linux ()
340 while ((urb = free_list_get ()) != 0)
343 while ((urb = completed_list_get ()) != 0)
346 if (d_write_work_in_progress)
347 free_urb (d_write_work_in_progress);
349 delete [] d_write_buffer;
351 if (d_read_work_in_progress)
352 free_urb (d_read_work_in_progress);
355 // ----------------------------------------------------------------
358 fusb_ephandle_linux::start ()
361 return true; // already running
365 if (d_input_p){ // fire off all the reads
369 while ((urb = free_list_get ()) != 0 && nerrors < d_nblocks){
370 if (!submit_urb (urb))
379 // kill all i/o in progress.
380 // kill any completed but unprocessed transactions.
383 fusb_ephandle_linux::stop ()
388 d_devhandle->_cancel_pending_rqsts (this);
389 d_devhandle->_reap (false);
393 while ((urb = completed_list_get ()) != 0)
396 if (d_write_work_in_progress){
397 free_list_add (d_write_work_in_progress);
398 d_write_work_in_progress = 0;
401 if (d_read_work_in_progress){
402 free_list_add (d_read_work_in_progress);
403 d_read_work_in_progress = 0;
405 d_read_buffer_end = 0;
408 if (d_free_list.size () != (unsigned) d_nblocks)
409 fprintf (stderr, "d_free_list.size () = %d, d_nblocks = %d\n",
410 d_free_list.size (), d_nblocks);
412 assert (d_free_list.size () == (unsigned) d_nblocks);
418 // ----------------------------------------------------------------
419 // routines for writing
420 // ----------------------------------------------------------------
422 #if (MINIMIZE_TX_BUFFERING)
425 fusb_ephandle_linux::write(const void *buffer, int nbytes)
433 assert(nbytes % 512 == 0);
435 unsigned char *src = (unsigned char *) buffer;
440 usbdevfs_urb *urb = get_write_work_in_progress();
441 assert(urb->actual_length == 0);
442 int m = std::min(nbytes - n, MAX_BLOCK_SIZE);
444 urb->buffer_length = m;
449 if (!submit_urb(urb))
452 d_write_work_in_progress = 0;
461 fusb_ephandle_linux::write (const void *buffer, int nbytes)
469 unsigned char *src = (unsigned char *) buffer;
474 usbdevfs_urb *urb = get_write_work_in_progress ();
475 unsigned char *dst = (unsigned char *) urb->buffer;
476 int m = std::min (nbytes - n, urb->buffer_length - urb->actual_length);
478 memcpy (&dst[urb->actual_length], &src[n], m);
479 urb->actual_length += m;
482 if (urb->actual_length == urb->buffer_length){
483 if (!submit_urb (urb))
485 d_write_work_in_progress = 0;
495 fusb_ephandle_linux::get_write_work_in_progress ()
497 // if we've already got some work in progress, return it
499 if (d_write_work_in_progress)
500 return d_write_work_in_progress;
504 reap_complete_writes ();
506 usbdevfs_urb *urb = free_list_get ();
509 assert (urb->actual_length == 0);
510 d_write_work_in_progress = urb;
514 // The free list is empty. Tell the device handle to reap.
515 // Anything it reaps for us will end up on our completed list.
517 d_devhandle->_reap (true);
522 fusb_ephandle_linux::reap_complete_writes ()
524 // take a look at the completed_list and xfer to free list after
525 // checking for errors.
529 while ((urb = completed_list_get ()) != 0){
531 // Check for any errors or short writes that were reported in the urb.
532 // The kernel sets status, actual_length and error_count.
533 // error_count is only used for ISO xfers.
534 // status is 0 if successful, else is an errno kind of thing
536 if (urb->status != 0){
537 fprintf (stderr, "fusb: (status %d) %s\n", urb->status, strerror (-urb->status));
539 else if (urb->actual_length != urb->buffer_length){
540 fprintf (stderr, "fusb: short write xfer: %d != %d\n",
541 urb->actual_length, urb->buffer_length);
549 fusb_ephandle_linux::wait_for_completion ()
551 d_devhandle->_wait_for_completion ();
554 // ----------------------------------------------------------------
555 // routines for reading
556 // ----------------------------------------------------------------
559 fusb_ephandle_linux::read (void *buffer, int nbytes)
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);
587 fusb_ephandle_linux::reload_read_buffer ()
589 assert (d_read_buffer >= d_read_buffer_end);
593 if (d_read_work_in_progress){
594 // We're done with this urb. Fire off a read to refill it.
595 urb = d_read_work_in_progress;
596 d_read_work_in_progress = 0;
598 d_read_buffer_end = 0;
599 urb->actual_length = 0;
600 if (!submit_urb (urb))
606 while ((urb = completed_list_get ()) == 0)
607 d_devhandle->_reap (true);
609 // check result of completed read
611 if (urb->status != 0){
612 // We've got a problem.
613 // Report the problem and resubmit.
614 fprintf (stderr, "fusb: (rd status %d) %s\n", urb->status, strerror (-urb->status));
615 urb->actual_length = 0;
616 if (!submit_urb (urb))
622 // we've got a happy urb, full of data...
624 d_read_work_in_progress = urb;
625 d_read_buffer = (unsigned char *) urb->buffer;
626 d_read_buffer_end = d_read_buffer + urb->actual_length;
632 // ----------------------------------------------------------------
635 fusb_ephandle_linux::free_list_add (usbdevfs_urb *urb)
637 assert (urb_get_ephandle (urb) == this);
638 urb->actual_length = 0;
639 d_free_list.push_back (urb);
643 fusb_ephandle_linux::free_list_get ()
645 if (d_free_list.empty ())
648 usbdevfs_urb *urb = d_free_list.front ();
649 d_free_list.pop_front ();
654 fusb_ephandle_linux::completed_list_add (usbdevfs_urb *urb)
656 assert (urb_get_ephandle (urb) == this);
657 d_completed_list.push_back (urb);
661 fusb_ephandle_linux::completed_list_get ()
663 if (d_completed_list.empty ())
666 usbdevfs_urb *urb = d_completed_list.front ();
667 d_completed_list.pop_front ();
672 * Submit the urb. If successful the urb ends up on the devhandle's
673 * pending list, otherwise, it's back on our free list.
676 fusb_ephandle_linux::submit_urb (usbdevfs_urb *urb)
678 if (!d_devhandle->_submit_urb (urb)){ // FIXME record the problem somewhere
679 fprintf (stderr, "_submit_urb failed\n");