fbd651204a5121a47890af1588f01a0d7b646093
[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 struct  libusb_device_handle;
30 struct  libusb_context;
31 class   fusb_ephandle;
32
33 /*!
34  * \brief abstract usb device handle
35  */
36 class fusb_devhandle {
37 private:
38   // NOT IMPLEMENTED
39   fusb_devhandle (const fusb_devhandle &rhs);             // no copy constructor
40   fusb_devhandle &operator= (const fusb_devhandle &rhs);  // no assignment operator
41
42 protected:
43   libusb_device_handle          *d_udh;
44
45 public:
46   // CREATORS
47   fusb_devhandle (libusb_device_handle *udh);
48   virtual ~fusb_devhandle ();
49
50   // MANIPULATORS
51   
52   /*!
53    * \brief return an ephandle of the correct subtype
54    */
55   virtual fusb_ephandle *make_ephandle (int endpoint, bool input_p,
56                                         int block_size = 0, int nblocks = 0) = 0;
57   
58   // ACCESSORS
59   libusb_device_handle *get_libusb_device_handle () const { return d_udh; }
60 };
61
62
63 /*!
64  * \brief abstract usb end point handle
65  */
66 class fusb_ephandle {
67 private:
68   // NOT IMPLEMENTED
69   fusb_ephandle (const fusb_ephandle &rhs);             // no copy constructor
70   fusb_ephandle &operator= (const fusb_ephandle &rhs);  // no assignment operator
71
72 protected:
73   int                           d_endpoint;
74   bool                          d_input_p;
75   int                           d_block_size;
76   int                           d_nblocks;
77   bool                          d_started;
78
79 public:
80   fusb_ephandle (int endpoint, bool input_p,
81                  int block_size = 0, int nblocks = 0);
82   virtual ~fusb_ephandle ();
83
84   virtual bool start () = 0;    //!< begin streaming i/o
85   virtual bool stop () = 0;     //!< stop streaming i/o
86
87   /*!
88    * \returns \p nbytes if write was successfully enqueued, else -1.
89    * Will block if no free buffers available.
90    */
91   virtual int write (const void *buffer, int nbytes) = 0;
92
93   /*!
94    * \returns number of bytes read or -1 if error.
95    * number of bytes read will be <= nbytes.
96    * Will block if no input available.
97    */
98   virtual int read (void *buffer, int nbytes) = 0;
99
100   /*
101    * block until all outstanding writes have completed
102    */
103   virtual void wait_for_completion () = 0;
104
105   /*!
106    * \brief returns current block size.
107    */
108   int block_size () { return d_block_size; };
109 };
110
111
112 /*!
113  * \brief factory for creating concrete instances of the appropriate subtype.
114  */
115 class fusb_sysconfig {
116 public:
117   /*!
118    * \brief returns fusb_devhandle or throws if trouble
119    */
120   static fusb_devhandle *make_devhandle (libusb_device_handle *udh,
121                                          libusb_context *ctx);
122
123   /*!
124    * \brief Returns max block size in bytes (hard limit).
125    */
126   static int max_block_size ();
127
128   /*!
129    * \brief Returns default block size in bytes.
130    */
131   static int default_block_size ();
132
133   /*!
134    * \brief Returns the default buffer size in bytes.
135    */
136   static int default_buffer_size ();
137
138 };
139
140 #endif /* _FUSB_H_ */