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>
42 #define MINIMIZE_TX_BUFFERING 1 // must be defined to 0 or 1
45 static const int MAX_BLOCK_SIZE = fusb_sysconfig::max_block_size(); // hard limit
46 static const int DEFAULT_BLOCK_SIZE = MAX_BLOCK_SIZE;
47 static const int DEFAULT_BUFFER_SIZE = 4 * (1L << 20); // 4 MB / endpoint
50 // Totally evil and fragile extraction of file descriptor from
51 // guts of libusb. They don't install usbi.h, which is what we'd need
54 // FIXME if everything breaks someday in the future, look here...
57 fd_from_usb_dev_handle (usb_dev_handle *udh)
59 return *((int *) udh);
63 urb_set_ephandle (usbdevfs_urb *urb, fusb_ephandle_linux *handle)
65 urb->usercontext = handle;
68 inline static fusb_ephandle_linux *
69 urb_get_ephandle (usbdevfs_urb *urb)
71 return (fusb_ephandle_linux *) urb->usercontext;
74 // ------------------------------------------------------------------------
75 // USB request block (urb) allocation
76 // ------------------------------------------------------------------------
79 alloc_urb (fusb_ephandle_linux *self, int buffer_length, int endpoint,
80 bool input_p, unsigned char *write_buffer)
82 usbdevfs_urb *urb = new usbdevfs_urb;
83 memset (urb, 0, sizeof (*urb));
85 urb->buffer_length = buffer_length;
87 // We allocate dedicated memory only for input buffers.
88 // For output buffers we reuse the same buffer (the kernel
89 // copies the data at submital time)
92 urb->buffer = new unsigned char [buffer_length];
94 urb->buffer = write_buffer;
98 urb->type = USBDEVFS_URB_TYPE_BULK;
99 urb->endpoint = (endpoint & 0x7f) | (input_p ? 0x80 : 0);
101 // USBDEVFS_URB_QUEUE_BULK goes away in linux 2.5, but is needed if
102 // we are using a 2.4 usb-uhci host controller driver. This is
103 // unlikely since we're almost always going to be plugged into a
104 // high speed host controller (ehci)
105 #if 0 && defined (USBDEVFS_URB_QUEUE_BULK)
106 urb->flags = USBDEVFS_URB_QUEUE_BULK;
110 urb_set_ephandle (urb, self);
116 free_urb (usbdevfs_urb *urb)
118 // if this was an input urb, free the buffer
119 if (urb->endpoint & 0x80)
120 delete [] ((unsigned char *) urb->buffer);
125 // ------------------------------------------------------------------------
127 // ------------------------------------------------------------------------
129 fusb_devhandle_linux::fusb_devhandle_linux (usb_dev_handle *udh)
130 : fusb_devhandle (udh)
135 fusb_devhandle_linux::~fusb_devhandle_linux ()
137 // if there are any pending requests, cancel them and free the urbs.
139 std::list<usbdevfs_urb*>::reverse_iterator it;
141 for (it = d_pending_rqsts.rbegin (); it != d_pending_rqsts.rend (); it++){
148 fusb_devhandle_linux::make_ephandle (int endpoint, bool input_p,
149 int block_size, int nblocks)
151 return new fusb_ephandle_linux (this, endpoint, input_p,
152 block_size, nblocks);
156 // Attempt to cancel all transactions associated with eph.
159 fusb_devhandle_linux::_cancel_pending_rqsts (fusb_ephandle_linux *eph)
161 std::list<usbdevfs_urb*>::reverse_iterator it;
163 for (it = d_pending_rqsts.rbegin (); it != d_pending_rqsts.rend (); it++){
164 if (urb_get_ephandle (*it) == eph)
170 fusb_devhandle_linux::pending_add (usbdevfs_urb *urb)
172 d_pending_rqsts.push_back (urb);
176 fusb_devhandle_linux::pending_get ()
178 if (d_pending_rqsts.empty ())
181 usbdevfs_urb *urb = d_pending_rqsts.front ();
182 d_pending_rqsts.pop_front ();
187 fusb_devhandle_linux::pending_remove (usbdevfs_urb *urb)
189 std::list<usbdevfs_urb*>::iterator result = find (d_pending_rqsts.begin (),
190 d_pending_rqsts.end (),
192 if (result == d_pending_rqsts.end ()){
193 fprintf (stderr, "fusb::pending_remove: failed to find urb in pending_rqsts: %p\n", urb);
196 d_pending_rqsts.erase (result);
201 * Submit the urb to the kernel.
202 * iff successful, the urb will be placed on the devhandle's pending list.
205 fusb_devhandle_linux::_submit_urb (usbdevfs_urb *urb)
209 ret = ioctl (fd_from_usb_dev_handle (d_udh), USBDEVFS_SUBMITURB, urb);
211 perror ("fusb::_submit_urb");
220 * Attempt to cancel the in pending or in-progress urb transaction.
221 * Return true iff transaction was sucessfully cancelled.
223 * Failure to cancel should not be considered a problem. This frequently
224 * occurs if the transaction has already completed in the kernel but hasn't
225 * yet been reaped by the user mode code.
227 * urbs which were cancelled have their status field set to -ENOENT when
231 fusb_devhandle_linux::_cancel_urb (usbdevfs_urb *urb)
233 int ret = ioctl (fd_from_usb_dev_handle (d_udh), USBDEVFS_DISCARDURB, urb);
235 // perror ("fusb::_cancel_urb");
242 * Check with the kernel and see if any of our outstanding requests
243 * have completed. For each completed transaction, remove it from the
244 * devhandle's pending list and append it to the completed list for
245 * the corresponding endpoint.
247 * If any transactions are reaped return true.
249 * If ok_to_block_p is true, then this will block until at least one
250 * transaction completes or an unrecoverable error occurs.
253 fusb_devhandle_linux::_reap (bool ok_to_block_p)
257 usbdevfs_urb *urb = 0;
259 int fd = fd_from_usb_dev_handle (d_udh);
261 // try to reap as many as possible without blocking...
263 while ((ret = ioctl (fd, USBDEVFS_REAPURBNDELAY, &urb)) == 0){
264 if (urb->status != 0 && urb->status != -ENOENT){
265 fprintf (stderr, "_reap: usb->status = %d, actual_length = %5d\n",
266 urb->status, urb->actual_length);
268 pending_remove (urb);
269 urb_get_ephandle (urb)->completed_list_add (urb);
273 if (nreaped > 0) // if we got any, return w/o blocking
279 ret = ioctl (fd, USBDEVFS_REAPURB, &urb);
281 perror ("fusb::_reap");
285 pending_remove (urb);
286 urb_get_ephandle (urb)->completed_list_add (urb);
291 fusb_devhandle_linux::_wait_for_completion ()
293 while (!d_pending_rqsts.empty ())
297 \f// ------------------------------------------------------------------------
299 // ------------------------------------------------------------------------
301 fusb_ephandle_linux::fusb_ephandle_linux (fusb_devhandle_linux *devhandle,
304 int block_size, int nblocks)
305 : fusb_ephandle (endpoint, input_p, block_size, nblocks),
306 d_devhandle (devhandle),
307 d_write_work_in_progress (0), d_write_buffer (0),
308 d_read_work_in_progress (0), d_read_buffer (0), d_read_buffer_end (0)
311 if (d_block_size < 0 || d_block_size > MAX_BLOCK_SIZE)
312 throw std::out_of_range ("fusb_ephandle_linux: block_size");
315 throw std::out_of_range ("fusb_ephandle_linux: nblocks");
317 if (d_block_size == 0)
318 d_block_size = DEFAULT_BLOCK_SIZE;
321 d_nblocks = std::max (1, DEFAULT_BUFFER_SIZE / d_block_size);
324 if (!MINIMIZE_TX_BUFFERING)
325 d_write_buffer = new unsigned char [d_block_size];
328 fprintf(stderr, "fusb_ephandle_linux::ctor: d_block_size = %d d_nblocks = %d\n",
329 d_block_size, d_nblocks);
333 for (int i = 0; i < d_nblocks; i++)
334 d_free_list.push_back (alloc_urb (this, d_block_size, d_endpoint,
335 d_input_p, d_write_buffer));
338 fusb_ephandle_linux::~fusb_ephandle_linux ()
344 while ((urb = free_list_get ()) != 0)
347 while ((urb = completed_list_get ()) != 0)
350 if (d_write_work_in_progress)
351 free_urb (d_write_work_in_progress);
353 delete [] d_write_buffer;
355 if (d_read_work_in_progress)
356 free_urb (d_read_work_in_progress);
359 // ----------------------------------------------------------------
362 fusb_ephandle_linux::start ()
365 return true; // already running
369 if (d_input_p){ // fire off all the reads
373 while ((urb = free_list_get ()) != 0 && nerrors < d_nblocks){
374 if (!submit_urb (urb))
383 // kill all i/o in progress.
384 // kill any completed but unprocessed transactions.
387 fusb_ephandle_linux::stop ()
392 if (d_write_work_in_progress){
393 free_list_add (d_write_work_in_progress);
394 d_write_work_in_progress = 0;
397 if (d_read_work_in_progress){
398 free_list_add (d_read_work_in_progress);
399 d_read_work_in_progress = 0;
401 d_read_buffer_end = 0;
404 d_devhandle->_cancel_pending_rqsts (this);
405 d_devhandle->_reap (false);
409 while ((urb = completed_list_get ()) != 0)
412 if (d_free_list.size () == (unsigned) d_nblocks)
415 if (!d_devhandle->_reap(true))
423 // ----------------------------------------------------------------
424 // routines for writing
425 // ----------------------------------------------------------------
427 #if (MINIMIZE_TX_BUFFERING)
430 fusb_ephandle_linux::write(const void *buffer, int nbytes)
438 assert(nbytes % 512 == 0);
440 unsigned char *src = (unsigned char *) buffer;
445 usbdevfs_urb *urb = get_write_work_in_progress();
448 assert(urb->actual_length == 0);
449 int m = std::min(nbytes - n, MAX_BLOCK_SIZE);
451 urb->buffer_length = m;
456 if (!submit_urb(urb))
459 d_write_work_in_progress = 0;
468 fusb_ephandle_linux::write (const void *buffer, int nbytes)
476 unsigned char *src = (unsigned char *) buffer;
481 usbdevfs_urb *urb = get_write_work_in_progress ();
484 unsigned char *dst = (unsigned char *) urb->buffer;
485 int m = std::min (nbytes - n, urb->buffer_length - urb->actual_length);
487 memcpy (&dst[urb->actual_length], &src[n], m);
488 urb->actual_length += m;
491 if (urb->actual_length == urb->buffer_length){
492 if (!submit_urb (urb))
494 d_write_work_in_progress = 0;
504 fusb_ephandle_linux::get_write_work_in_progress ()
506 // if we've already got some work in progress, return it
508 if (d_write_work_in_progress)
509 return d_write_work_in_progress;
513 reap_complete_writes ();
515 usbdevfs_urb *urb = free_list_get ();
518 assert (urb->actual_length == 0);
519 d_write_work_in_progress = urb;
523 // The free list is empty. Tell the device handle to reap.
524 // Anything it reaps for us will end up on our completed list.
526 if (!d_devhandle->_reap (true))
532 fusb_ephandle_linux::reap_complete_writes ()
534 // take a look at the completed_list and xfer to free list after
535 // checking for errors.
539 while ((urb = completed_list_get ()) != 0){
541 // Check for any errors or short writes that were reported in the urb.
542 // The kernel sets status, actual_length and error_count.
543 // error_count is only used for ISO xfers.
544 // status is 0 if successful, else is an errno kind of thing
546 if (urb->status != 0){
547 fprintf (stderr, "fusb: (status %d) %s\n", urb->status, strerror (-urb->status));
549 else if (urb->actual_length != urb->buffer_length){
550 fprintf (stderr, "fusb: short write xfer: %d != %d\n",
551 urb->actual_length, urb->buffer_length);
559 fusb_ephandle_linux::wait_for_completion ()
561 d_devhandle->_wait_for_completion ();
564 // ----------------------------------------------------------------
565 // routines for reading
566 // ----------------------------------------------------------------
569 fusb_ephandle_linux::read (void *buffer, int nbytes)
577 unsigned char *dst = (unsigned char *) buffer;
582 if (d_read_buffer >= d_read_buffer_end)
583 if (!reload_read_buffer ())
586 int m = std::min (nbytes - n, (int) (d_read_buffer_end - d_read_buffer));
588 memcpy (&dst[n], d_read_buffer, m);
597 fusb_ephandle_linux::reload_read_buffer ()
599 assert (d_read_buffer >= d_read_buffer_end);
603 if (d_read_work_in_progress){
604 // We're done with this urb. Fire off a read to refill it.
605 urb = d_read_work_in_progress;
606 d_read_work_in_progress = 0;
608 d_read_buffer_end = 0;
609 urb->actual_length = 0;
610 if (!submit_urb (urb))
616 while ((urb = completed_list_get ()) == 0)
617 if (!d_devhandle->_reap (true))
620 // check result of completed read
622 if (urb->status != 0){
623 // We've got a problem. Report it and fail.
624 fprintf (stderr, "fusb: (rd status %d) %s\n", urb->status, strerror (-urb->status));
625 urb->actual_length = 0;
630 // we've got a happy urb, full of data...
632 d_read_work_in_progress = urb;
633 d_read_buffer = (unsigned char *) urb->buffer;
634 d_read_buffer_end = d_read_buffer + urb->actual_length;
640 // ----------------------------------------------------------------
643 fusb_ephandle_linux::free_list_add (usbdevfs_urb *urb)
645 assert (urb_get_ephandle (urb) == this);
646 urb->actual_length = 0;
647 d_free_list.push_back (urb);
651 fusb_ephandle_linux::free_list_get ()
653 if (d_free_list.empty ())
656 usbdevfs_urb *urb = d_free_list.front ();
657 d_free_list.pop_front ();
662 fusb_ephandle_linux::completed_list_add (usbdevfs_urb *urb)
664 assert (urb_get_ephandle (urb) == this);
665 d_completed_list.push_back (urb);
669 fusb_ephandle_linux::completed_list_get ()
671 if (d_completed_list.empty ())
674 usbdevfs_urb *urb = d_completed_list.front ();
675 d_completed_list.pop_front ();
680 * Submit the urb. If successful the urb ends up on the devhandle's
681 * pending list, otherwise, it's back on our free list.
684 fusb_ephandle_linux::submit_urb (usbdevfs_urb *urb)
686 if (!d_devhandle->_submit_urb (urb)){ // FIXME record the problem somewhere
687 fprintf (stderr, "_submit_urb failed\n");