3 * Copyright 2003,2006 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_ra_wb.h>
34 #include <sys/event.h>
35 #include <dev/usb/usb.h>
37 static const int USB_TIMEOUT = 1000; // in milliseconds
39 // the following comment and function is from fusb_linux.cc
41 // Totally evil and fragile extraction of file descriptor from
42 // guts of libusb. They don't install usbi.h, which is what we'd need
45 // FIXME if everything breaks someday in the future, look here...
48 fd_from_usb_dev_handle (usb_dev_handle *udh)
50 return *((int *) udh);
54 // the control endpoint doesn't actually do us any good so here is a
55 // new "fragile extraction"
57 ep_fd_from_usb_dev_handle (usb_dev_handle *udh, int endpoint)
59 struct usb_dev_handle_kludge2 { // see also usrp_prims.cc
62 struct usb_device *device;
68 struct bsd_usb_dev_handle_info_kludge {
69 int ep_fd[USB_MAX_ENDPOINTS];
71 struct bsd_usb_dev_handle_info_kludge *info
72 = (struct bsd_usb_dev_handle_info_kludge *)
73 ((struct usb_dev_handle_kludge2 *)udh)->impl_info;
74 return info->ep_fd[UE_GET_ADDR(endpoint)];
78 fusb_devhandle_ra_wb::fusb_devhandle_ra_wb (usb_dev_handle *udh)
79 : fusb_devhandle (udh)
84 fusb_devhandle_ra_wb::~fusb_devhandle_ra_wb ()
90 fusb_devhandle_ra_wb::make_ephandle (int endpoint, bool input_p,
91 int block_size, int nblocks)
93 return new fusb_ephandle_ra_wb (this, endpoint, input_p,
97 // ----------------------------------------------------------------
99 fusb_ephandle_ra_wb::fusb_ephandle_ra_wb (fusb_devhandle_ra_wb *dh,
100 int endpoint, bool input_p,
101 int block_size, int nblocks)
102 : fusb_ephandle (endpoint, input_p, block_size, nblocks),
103 d_devhandle (dh), d_ra_wb_on (false)
108 fusb_ephandle_ra_wb::~fusb_ephandle_ra_wb ()
114 fusb_ephandle_ra_wb::start ()
121 // this is to cause libusb to open the endpoint
124 fd = ep_fd_from_usb_dev_handle (d_devhandle->get_usb_dev_handle(),
129 fd = ep_fd_from_usb_dev_handle (d_devhandle->get_usb_dev_handle(),
130 d_endpoint|USB_ENDPOINT_IN);
133 // enable read ahead/write behind
135 struct usb_bulk_ra_wb_opt opts;
138 opts.ra_wb_buffer_size = d_block_size*d_nblocks;
139 opts.ra_wb_request_size = d_block_size;
140 // fprintf (stderr, "setting buffer size to %d, request size to %d\n",
141 // opts.ra_wb_buffer_size, opts.ra_wb_request_size);
143 ret = ioctl (fd, USB_SET_BULK_WB_OPT, &opts);
145 fprintf (stderr, "USB_SET_BULK_WB_OPT: %s\n", strerror(errno));
147 ret = ioctl (fd, USB_SET_BULK_WB, &enable);
149 fprintf (stderr, "USB_SET_BULK_WB: %s\n", strerror(errno));
155 ret = ioctl (fd, USB_SET_BULK_RA_OPT, &opts);
157 fprintf (stderr, "USB_SET_BULK_RA_OPT: %s\n", strerror(errno));
159 ret = ioctl (fd, USB_SET_BULK_RA, &enable);
161 fprintf (stderr, "USB_SET_BULK_RA: %s\n", strerror(errno));
171 fusb_ephandle_ra_wb::stop ()
178 fd = ep_fd_from_usb_dev_handle (d_devhandle->get_usb_dev_handle(),
180 ret = ioctl (fd, USB_SET_BULK_WB, &enable);
182 fprintf (stderr, "USB_SET_BULK_WB: %s\n", strerror(errno));
187 fd = ep_fd_from_usb_dev_handle (d_devhandle->get_usb_dev_handle(),
188 d_endpoint|USB_ENDPOINT_IN);
189 ret = ioctl (fd, USB_SET_BULK_RA, &enable);
191 fprintf (stderr, "USB_SET_BULK_RA: %s\n", strerror(errno));
202 fusb_ephandle_ra_wb::write (const void *buffer, int nbytes)
210 return usb_bulk_write (d_devhandle->get_usb_dev_handle (),
211 d_endpoint, (char *) buffer, nbytes, USB_TIMEOUT);
215 fusb_ephandle_ra_wb::read (void *buffer, int nbytes)
223 return usb_bulk_read (d_devhandle->get_usb_dev_handle (),
224 d_endpoint|USB_ENDPOINT_IN, (char *) buffer, nbytes,
229 fusb_ephandle_ra_wb::wait_for_completion ()
231 // as the driver is implemented this only makes sense for write
232 if (d_ra_wb_on && !d_input_p) {
233 int fd = ep_fd_from_usb_dev_handle (d_devhandle->get_usb_dev_handle(),
240 EV_SET (&evt, fd, EVFILT_WRITE, EV_ADD | EV_ENABLE, 0, 0, 0/*NULL*/);
241 nevents = kevent (kq, &evt, 1, &evt, 1, NULL);
246 while (!(evt.flags & EV_ERROR) && evt.data < (d_block_size*d_nblocks)) {
247 // it's a busy loop, but that's all I can do at the moment
248 nevents = kevent (kq, NULL, 0, &evt, 1, NULL);
249 // let's see if this improves the test_usrp_standard_tx throughput &
250 // "CPU usage" by looping less frequently
251 struct timeval timeout;
253 timeout.tv_usec = 1000; // 1 ms
254 select (0, NULL, NULL, NULL, &timeout);