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 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_win32.h>
33 static const int MAX_BLOCK_SIZE = fusb_sysconfig::max_block_size();
34 static const int DEFAULT_BLOCK_SIZE = MAX_BLOCK_SIZE;
35 static const int DEFAULT_BUFFER_SIZE = 16 * (1L << 20); // 16 MB / endpoint
38 static const int USB_TIMEOUT = 1000; // in milliseconds
41 fusb_devhandle_win32::fusb_devhandle_win32 (usb_dev_handle *udh)
42 : fusb_devhandle (udh)
47 fusb_devhandle_win32::~fusb_devhandle_win32 ()
53 fusb_devhandle_win32::make_ephandle (int endpoint, bool input_p,
54 int block_size, int nblocks)
56 return new fusb_ephandle_win32 (this, endpoint, input_p,
60 // ----------------------------------------------------------------
62 fusb_ephandle_win32::fusb_ephandle_win32 (fusb_devhandle_win32 *dh,
63 int endpoint, bool input_p,
64 int block_size, int nblocks)
65 : fusb_ephandle (endpoint, input_p, block_size, nblocks),
66 d_devhandle (dh), d_input_leftover(0),d_output_short(0)
68 if (d_block_size < 0 || d_block_size > MAX_BLOCK_SIZE)
69 throw std::out_of_range ("fusb_ephandle_win32: block_size");
72 throw std::out_of_range ("fusb_ephandle_win32: nblocks");
74 if (d_block_size == 0)
75 d_block_size = DEFAULT_BLOCK_SIZE;
78 d_nblocks = std::max (1, DEFAULT_BUFFER_SIZE / d_block_size);
80 d_buffer = new char [d_block_size*d_nblocks];
81 d_context = new void * [d_nblocks];
85 usb_dev_handle *dev = dh->get_usb_dev_handle ();
89 endpoint |= USB_ENDPOINT_IN;
91 for (i=0; i<d_nblocks; i++)
92 usb_bulk_setup_async(dev, &d_context[i], endpoint);
95 fusb_ephandle_win32::~fusb_ephandle_win32 ()
101 for (i=0; i<d_nblocks; i++)
102 usb_free_async(&d_context[i]);
109 fusb_ephandle_win32::start ()
112 return true; // already running
116 d_curr = d_nblocks-1;
117 d_outstanding_write = 0;
121 if (d_input_p){ // fire off all the reads
124 for (i=0; i<d_nblocks; i++) {
125 usb_submit_async(d_context[i], (char * ) d_buffer+i*d_block_size,
134 fusb_ephandle_win32::stop ()
140 wait_for_completion ();
147 fusb_ephandle_win32::write (const void *buffer, int nbytes)
152 if (!d_started) // doesn't matter here, but keeps semantics constant
158 int bytes_to_write = nbytes;
161 if (d_output_short != 0) {
163 buf = &d_buffer[d_curr*d_block_size + d_block_size - d_output_short];
164 a = std::min(nbytes, d_output_short);
165 memcpy(buf, buffer, a);
169 if (d_output_short == 0)
170 usb_submit_async(d_context[d_curr],
171 &d_buffer[d_curr*d_block_size], d_block_size);
174 while (bytes_to_write > 0) {
175 d_curr = (d_curr+1)%d_nblocks;
176 buf = &d_buffer[d_curr*d_block_size];
178 if (d_outstanding_write != d_nblocks) {
179 d_outstanding_write++;
181 retval = usb_reap_async(d_context[d_curr], USB_TIMEOUT);
183 fprintf(stderr, "%s: usb_reap_async: %s\n",
184 __FUNCTION__, usb_strerror());
189 int ncopy = std::min(bytes_to_write, d_block_size);
190 memcpy(buf, (void *) &(((char*)buffer)[a]), ncopy);
191 bytes_to_write -= ncopy;
194 d_output_short = d_block_size - ncopy;
195 if (d_output_short == 0)
196 usb_submit_async(d_context[d_curr], buf, d_block_size);
199 return retval < 0 ? retval : nbytes;
203 fusb_ephandle_win32::read (void *buffer, int nbytes)
208 if (!d_started) // doesn't matter here, but keeps semantics constant
214 int bytes_to_read = nbytes;
217 if (d_input_leftover != 0) {
219 buf = &d_buffer[d_curr*d_block_size + d_block_size - d_input_leftover];
220 a = std::min(nbytes, d_input_leftover);
221 memcpy(buffer, buf, a);
223 d_input_leftover -= a;
225 if (d_input_leftover == 0)
226 usb_submit_async(d_context[d_curr],
227 &d_buffer[d_curr*d_block_size], d_block_size);
230 while (bytes_to_read > 0) {
232 d_curr = (d_curr+1)%d_nblocks;
233 buf = &d_buffer[d_curr*d_block_size];
235 retval = usb_reap_async(d_context[d_curr], USB_TIMEOUT);
237 fprintf(stderr, "%s: usb_reap_async: %s\n",
238 __FUNCTION__, usb_strerror());
240 int ncopy = std::min(bytes_to_read, d_block_size);
241 memcpy((void *) &(((char*)buffer)[a]), buf, ncopy);
242 bytes_to_read -= ncopy;
245 d_input_leftover = d_block_size - ncopy;
246 if (d_input_leftover == 0)
247 usb_submit_async(d_context[d_curr], buf, d_block_size);
250 return retval < 0 ? retval : nbytes;
254 fusb_ephandle_win32::wait_for_completion ()
258 for (i=0; i<d_outstanding_write; i++) {
261 context_num = (d_curr+d_outstanding_write+i+1)%d_nblocks;
262 usb_reap_async(d_context[context_num], USB_TIMEOUT);
265 d_outstanding_write = 0;