Imported Upstream version 3.2.2
[debian/gnuradio] / usrp / host / lib / legacy / fusb_generic.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2003 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
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)
10  * any later version.
11  * 
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.
16  * 
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <fusb_generic.h>
28 #include <usb.h>
29
30
31 static const int USB_TIMEOUT = 1000;    // in milliseconds
32
33
34 fusb_devhandle_generic::fusb_devhandle_generic (usb_dev_handle *udh)
35   : fusb_devhandle (udh)
36 {
37   // that's it
38 }
39
40 fusb_devhandle_generic::~fusb_devhandle_generic ()
41 {
42   // nop
43 }
44
45 fusb_ephandle *
46 fusb_devhandle_generic::make_ephandle (int endpoint, bool input_p,
47                                        int block_size, int nblocks)
48 {
49   return new fusb_ephandle_generic (this, endpoint, input_p,
50                                     block_size, nblocks);
51 }
52
53 // ----------------------------------------------------------------
54
55 fusb_ephandle_generic::fusb_ephandle_generic (fusb_devhandle_generic *dh,
56                                               int endpoint, bool input_p,
57                                               int block_size, int nblocks)
58   : fusb_ephandle (endpoint, input_p, block_size, nblocks),
59     d_devhandle (dh)
60 {
61   // that's it
62 }
63
64 fusb_ephandle_generic::~fusb_ephandle_generic ()
65 {
66   // nop
67 }
68
69 bool
70 fusb_ephandle_generic::start ()
71 {
72   d_started = true;
73   return true;
74 }
75
76 bool
77 fusb_ephandle_generic::stop ()
78 {
79   d_started = false;
80   return true;
81 }
82
83 int
84 fusb_ephandle_generic::write (const void *buffer, int nbytes)
85 {
86   if (!d_started)       // doesn't matter here, but keeps semantics constant
87     return -1;
88   
89   if (d_input_p)
90     return -1;
91   
92   return usb_bulk_write (d_devhandle->get_usb_dev_handle (),
93                          d_endpoint, (char *) buffer, nbytes, USB_TIMEOUT);
94 }
95
96 int
97 fusb_ephandle_generic::read (void *buffer, int nbytes)
98 {
99   if (!d_started)       // doesn't matter here, but keeps semantics constant
100     return -1;
101
102   if (!d_input_p)
103     return -1;
104
105   return usb_bulk_read (d_devhandle->get_usb_dev_handle (),
106                         d_endpoint|USB_ENDPOINT_IN, (char *) buffer, nbytes,
107                         USB_TIMEOUT);
108 }