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