3 * Copyright 2003,2005 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_win32.h>
32 static const int MAX_BLOCK_SIZE = fusb_sysconfig::max_block_size();
33 static const int DEFAULT_BLOCK_SIZE = MAX_BLOCK_SIZE;
34 static const int DEFAULT_BUFFER_SIZE = 16 * (1L << 20); // 16 MB / endpoint
37 static const int USB_TIMEOUT = 1000; // in milliseconds
40 fusb_devhandle_win32::fusb_devhandle_win32 (usb_dev_handle *udh)
41 : fusb_devhandle (udh)
46 fusb_devhandle_win32::~fusb_devhandle_win32 ()
52 fusb_devhandle_win32::make_ephandle (int endpoint, bool input_p,
53 int block_size, int nblocks)
55 return new fusb_ephandle_win32 (this, endpoint, input_p,
59 // ----------------------------------------------------------------
61 fusb_ephandle_win32::fusb_ephandle_win32 (fusb_devhandle_win32 *dh,
62 int endpoint, bool input_p,
63 int block_size, int nblocks)
64 : fusb_ephandle (endpoint, input_p, block_size, nblocks),
65 d_devhandle (dh), d_input_leftover(0),d_output_short(0)
67 if (d_block_size < 0 || d_block_size > MAX_BLOCK_SIZE)
68 throw std::out_of_range ("fusb_ephandle_win32: block_size");
71 throw std::out_of_range ("fusb_ephandle_win32: nblocks");
73 if (d_block_size == 0)
74 d_block_size = DEFAULT_BLOCK_SIZE;
77 d_nblocks = std::max (1, DEFAULT_BUFFER_SIZE / d_block_size);
79 d_buffer = new char [d_block_size*d_nblocks];
80 d_context = new void * [d_nblocks];
84 usb_dev_handle *dev = dh->get_usb_dev_handle ();
88 endpoint |= USB_ENDPOINT_IN;
90 for (i=0; i<d_nblocks; i++)
91 usb_bulk_setup_async(dev, &d_context[i], endpoint);
94 fusb_ephandle_win32::~fusb_ephandle_win32 ()
100 for (i=0; i<d_nblocks; i++)
101 usb_free_async(&d_context[i]);
108 fusb_ephandle_win32::start ()
111 return true; // already running
115 d_curr = d_nblocks-1;
116 d_outstanding_write = 0;
120 if (d_input_p){ // fire off all the reads
123 for (i=0; i<d_nblocks; i++) {
124 usb_submit_async(d_context[i], (char * ) d_buffer+i*d_block_size,
133 fusb_ephandle_win32::stop ()
139 wait_for_completion ();
146 fusb_ephandle_win32::write (const void *buffer, int nbytes)
151 if (!d_started) // doesn't matter here, but keeps semantics constant
157 int bytes_to_write = nbytes;
160 if (d_output_short != 0) {
162 buf = &d_buffer[d_curr*d_block_size + d_block_size - d_output_short];
163 a = std::min(nbytes, d_output_short);
164 memcpy(buf, buffer, a);
168 if (d_output_short == 0)
169 usb_submit_async(d_context[d_curr],
170 &d_buffer[d_curr*d_block_size], d_block_size);
172 if (bytes_to_write == 0)
175 assert(d_output_short == 0);
178 d_curr = (d_curr+1)%d_nblocks;
179 buf = &d_buffer[d_curr*d_block_size];
181 if (d_outstanding_write != d_nblocks) {
182 d_outstanding_write++;
184 retval = usb_reap_async(d_context[d_curr], USB_TIMEOUT);
186 fprintf(stderr, "%s: usb_reap_async: %s\n",
187 __FUNCTION__, usb_strerror());
192 memcpy(buf, (void *) &(((char*)buffer)[a]), bytes_to_write);
194 d_output_short = d_block_size - bytes_to_write;
195 if (d_output_short == 0)
196 usb_submit_async(d_context[d_curr], buf, d_block_size);
198 return retval < 0 ? retval : nbytes;
202 fusb_ephandle_win32::read (void *buffer, int nbytes)
207 if (!d_started) // doesn't matter here, but keeps semantics constant
213 int bytes_to_read = nbytes;
216 if (d_input_leftover != 0) {
218 buf = &d_buffer[d_curr*d_block_size + d_block_size - d_input_leftover];
219 a = std::min(nbytes, d_input_leftover);
220 memcpy(buffer, buf, a);
222 d_input_leftover -= a;
224 if (d_input_leftover == 0)
225 usb_submit_async(d_context[d_curr],
226 &d_buffer[d_curr*d_block_size], d_block_size);
228 if (bytes_to_read == 0)
231 assert(d_input_leftover == 0);
235 d_curr = (d_curr+1)%d_nblocks;
236 buf = &d_buffer[d_curr*d_block_size];
238 retval = usb_reap_async(d_context[d_curr], USB_TIMEOUT);
240 fprintf(stderr, "%s: usb_reap_async: %s\n",
241 __FUNCTION__, usb_strerror());
243 memcpy((void *) &(((char*)buffer)[a]), buf, bytes_to_read);
245 d_input_leftover = d_block_size - bytes_to_read;
246 if (d_input_leftover == 0)
247 usb_submit_async(d_context[d_curr], buf, d_block_size);
249 return retval < 0 ? retval : nbytes;
253 fusb_ephandle_win32::wait_for_completion ()
257 for (i=0; i<d_outstanding_write; i++) {
260 context_num = (d_curr+d_outstanding_write+i+1)%d_nblocks;
261 usb_reap_async(d_context[context_num], USB_TIMEOUT);
264 d_outstanding_write = 0;