Imported Upstream version 3.0
[debian/gnuradio] / usrp / host / lib / fusb_linux.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 2, 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_LINUX_H_
26 #define _FUSB_LINUX_H_
27
28 #include <fusb.h>
29 #include <list>
30
31 struct  usbdevfs_urb;
32 class   fusb_ephandle_linux;
33
34 /*!
35  * \brief linux specific implementation of fusb_devhandle using usbdevice_fs
36  */
37 class fusb_devhandle_linux : public fusb_devhandle {
38 private:
39   std::list<usbdevfs_urb*>       d_pending_rqsts;
40
41   void pending_add (usbdevfs_urb *urb);
42   bool pending_remove (usbdevfs_urb *urb);
43   usbdevfs_urb * pending_get ();
44
45
46 public:
47   // CREATORS
48   fusb_devhandle_linux (usb_dev_handle *udh);
49   virtual ~fusb_devhandle_linux ();
50
51   // MANIPULATORS
52   virtual fusb_ephandle *make_ephandle (int endpoint, bool input_p,
53                                         int block_size = 0, int nblocks = 0);
54
55   // internal use only
56   bool _submit_urb (usbdevfs_urb *urb);
57   bool _cancel_urb (usbdevfs_urb *urb);
58   void _cancel_pending_rqsts (fusb_ephandle_linux *eph);
59   bool _reap (bool ok_to_block_p);
60   void _wait_for_completion ();
61 };
62
63 \f/*!
64  * \brief linux specific implementation of fusb_ephandle using usbdevice_fs
65  */
66
67 class fusb_ephandle_linux : public fusb_ephandle {
68 private:
69   fusb_devhandle_linux         *d_devhandle;
70   std::list<usbdevfs_urb*>      d_free_list;
71   std::list<usbdevfs_urb*>      d_completed_list;
72   usbdevfs_urb                 *d_write_work_in_progress;
73   unsigned char                *d_write_buffer;
74   usbdevfs_urb                 *d_read_work_in_progress;
75   unsigned char                *d_read_buffer;
76   unsigned char                *d_read_buffer_end;
77
78   usbdevfs_urb *get_write_work_in_progress ();
79   void reap_complete_writes ();
80   bool reload_read_buffer ();
81   bool submit_urb (usbdevfs_urb *urb);
82   
83 public:
84   fusb_ephandle_linux (fusb_devhandle_linux *dh, int endpoint, bool input_p,
85                        int block_size = 0, int nblocks = 0);
86   virtual ~fusb_ephandle_linux ();
87
88   virtual bool start ();        //!< begin streaming i/o
89   virtual bool stop ();         //!< stop streaming i/o
90
91   /*!
92    * \returns \p nbytes if write was successfully enqueued, else -1.
93    * Will block if no free buffers available.
94    */
95   virtual int write (const void *buffer, int nbytes);
96
97   /*!
98    * \returns number of bytes read or -1 if error.
99    * number of bytes read will be <= nbytes.
100    * Will block if no input available.
101    */
102   virtual int read (void *buffer, int nbytes);
103
104   /*
105    * block until all outstanding writes have completed
106    */
107   virtual void wait_for_completion ();
108
109   // internal use only
110   void free_list_add (usbdevfs_urb *urb);
111   void completed_list_add (usbdevfs_urb *urb);
112   usbdevfs_urb *free_list_get ();               // pop and return head of list or 0
113   usbdevfs_urb *completed_list_get ();          // pop and return head of list or 0
114 };
115
116 #endif /* _FUSB_LINUX_H_ */