Commonized more usrp_prims code and renamed libusb-0.12 files to libusb0
[debian/gnuradio] / usrp / host / lib / fusb.h
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 // Fast USB interface
24
25 #ifndef _FUSB_H_
26 #define _FUSB_H_
27
28 /*
29  * This is bad, but it works for now. The fusb header files are not installed.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #ifdef HAVE_LIBUSB_1 
37 struct  libusb_device_handle;
38 #else
39 struct  usb_dev_handle;
40 typedef usb_dev_handle libusb_device_handle;
41 #endif
42
43 struct  libusb_context;
44 class   fusb_ephandle;
45
46 /*!
47  * \brief abstract usb device handle
48  */
49 class fusb_devhandle {
50 private:
51   // NOT IMPLEMENTED
52   fusb_devhandle (const fusb_devhandle &rhs);             // no copy constructor
53   fusb_devhandle &operator= (const fusb_devhandle &rhs);  // no assignment operator
54
55 protected:
56   libusb_device_handle          *d_udh;
57
58 public:
59   // CREATORS
60   fusb_devhandle (libusb_device_handle *udh);
61   virtual ~fusb_devhandle ();
62
63   // MANIPULATORS
64   
65   /*!
66    * \brief return an ephandle of the correct subtype
67    */
68   virtual fusb_ephandle *make_ephandle (int endpoint, bool input_p,
69                                         int block_size = 0, int nblocks = 0) = 0;
70   
71   // ACCESSORS
72   libusb_device_handle *get_usb_dev_handle () const { return d_udh; }
73 };
74
75
76 /*!
77  * \brief abstract usb end point handle
78  */
79 class fusb_ephandle {
80 private:
81   // NOT IMPLEMENTED
82   fusb_ephandle (const fusb_ephandle &rhs);             // no copy constructor
83   fusb_ephandle &operator= (const fusb_ephandle &rhs);  // no assignment operator
84
85 protected:
86   int                           d_endpoint;
87   bool                          d_input_p;
88   int                           d_block_size;
89   int                           d_nblocks;
90   bool                          d_started;
91
92 public:
93   fusb_ephandle (int endpoint, bool input_p,
94                  int block_size = 0, int nblocks = 0);
95   virtual ~fusb_ephandle ();
96
97   virtual bool start () = 0;    //!< begin streaming i/o
98   virtual bool stop () = 0;     //!< stop streaming i/o
99
100   /*!
101    * \returns \p nbytes if write was successfully enqueued, else -1.
102    * Will block if no free buffers available.
103    */
104   virtual int write (const void *buffer, int nbytes) = 0;
105
106   /*!
107    * \returns number of bytes read or -1 if error.
108    * number of bytes read will be <= nbytes.
109    * Will block if no input available.
110    */
111   virtual int read (void *buffer, int nbytes) = 0;
112
113   /*
114    * block until all outstanding writes have completed
115    */
116   virtual void wait_for_completion () = 0;
117
118   /*!
119    * \brief returns current block size.
120    */
121   int block_size () { return d_block_size; };
122 };
123
124
125 /*!
126  * \brief factory for creating concrete instances of the appropriate subtype.
127  */
128 class fusb_sysconfig {
129 public:
130   /*!
131    * \brief returns fusb_devhandle or throws if trouble
132    */
133   static fusb_devhandle *make_devhandle (libusb_device_handle *udh,
134                                          libusb_context *ctx = 0);
135
136   /*!
137    * \brief Returns max block size in bytes (hard limit).
138    */
139   static int max_block_size ();
140
141   /*!
142    * \brief Returns default block size in bytes.
143    */
144   static int default_block_size ();
145
146   /*!
147    * \brief Returns the default buffer size in bytes.
148    */
149   static int default_buffer_size ();
150
151 };
152
153 #endif /* _FUSB_H_ */