Merge commit 'v3.3.0' into upstream
[debian/gnuradio] / usrp / host / lib / fusb_libusb1.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2003,2009 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 #ifndef _FUSB_LIBUSB1_H_
24 #define _FUSB_LIBUSB1_H_
25
26 #include <fusb.h>
27 #include <list>
28
29 struct libusb_transfer;
30 struct libusb_context;
31
32 class fusb_ephandle_libusb1;
33
34 /*!
35  * \brief libusb1 implementation of fusb_devhandle
36  */
37 class fusb_devhandle_libusb1 : public fusb_devhandle
38 {
39 private:
40   std::list<libusb_transfer*>    d_pending_rqsts;
41   libusb_context                *d_ctx;
42
43   void pending_add (struct libusb_transfer *lut);
44   struct libusb_transfer * pending_get ();
45
46   bool d_teardown;
47
48 public:
49   // CREATORS
50   fusb_devhandle_libusb1 (libusb_device_handle *udh, libusb_context *ctx);
51   virtual ~fusb_devhandle_libusb1 ();
52
53   // MANIPULATORS
54   virtual fusb_ephandle *make_ephandle (int endpoint, bool input_p,
55                                         int block_size = 0, int nblocks = 0);
56   // internal use only
57   bool _submit_lut (libusb_transfer *);
58   bool _cancel_lut (libusb_transfer *);
59   void _cancel_pending_rqsts (fusb_ephandle_libusb1 *eph);
60   bool _reap (bool ok_to_block_p);
61   void _wait_for_completion ();
62
63   // accessors to work from callback context
64   bool pending_remove (struct libusb_transfer *lut);
65   inline bool _teardown() { return d_teardown; }
66
67 };
68
69
70 /*!
71  * \brief libusb1 implementation of fusb_ephandle
72  */
73 class fusb_ephandle_libusb1 : public fusb_ephandle
74 {
75 private:
76   fusb_devhandle_libusb1         *d_devhandle;
77   std::list<libusb_transfer*>     d_free_list;
78   std::list<libusb_transfer*>     d_completed_list;
79   libusb_transfer                *d_write_work_in_progress;
80   unsigned char                  *d_write_buffer;
81   libusb_transfer                *d_read_work_in_progress;
82   unsigned char                  *d_read_buffer;
83   unsigned char                  *d_read_buffer_end;
84
85   libusb_transfer *get_write_work_in_progress ();
86   void reap_complete_writes ();
87   bool reload_read_buffer ();
88   bool submit_lut (libusb_transfer *lut);
89
90 public:
91   // CREATORS
92   fusb_ephandle_libusb1 (fusb_devhandle_libusb1 *dh, int endpoint, bool input_p,
93                          int block_size = 0, int nblocks = 0);
94   virtual ~fusb_ephandle_libusb1 ();
95
96   // MANIPULATORS
97
98   virtual bool start ();        //!< begin streaming i/o
99   virtual bool stop ();         //!< stop streaming i/o
100
101   /*!
102    * \returns \p nbytes if write was successfully enqueued, else -1.
103    * Will block if no free buffers available.
104    */
105   virtual int write (const void *buffer, int nbytes);
106
107   /*!
108    * \returns number of bytes read or -1 if error.
109    * number of bytes read will be <= nbytes.
110    * Will block if no input available.
111    */
112   virtual int read (void *buffer, int nbytes);
113
114   /*
115    * block until all outstanding writes have completed
116    */
117   virtual void wait_for_completion ();
118
119   void free_list_add (struct libusb_transfer *lut);
120   void completed_list_add (struct libusb_transfer *lut);
121   struct libusb_transfer *free_list_get ();
122   struct libusb_transfer *completed_list_get ();
123
124   // accessor to work from callback context
125   fusb_devhandle_libusb1* get_fusb_devhandle_libusb1 () const {
126     return d_devhandle;
127   }
128 };
129
130 #endif /* _FUSB_LINUX1_H_ */
131