These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / usb / usbnet.c
1 /*
2  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <string.h>
27 #include <errno.h>
28 #include <ipxe/usb.h>
29 #include <ipxe/usbnet.h>
30
31 /** @file
32  *
33  * USB network devices
34  *
35  * USB network devices use a variety of packet formats and interface
36  * descriptors, but tend to have several features in common:
37  *
38  *  - a single interrupt endpoint using the generic refill mechanism
39  *
40  *  - a single bulk IN endpoint using the generic refill mechanism
41  *
42  *  - a single bulk OUT endpoint
43  *
44  *  - optional use of an alternate setting to enable the data interface
45  *
46  */
47
48 /**
49  * Open USB network device
50  *
51  * @v usbnet            USB network device
52  * @ret rc              Return status code
53  */
54 int usbnet_open ( struct usbnet_device *usbnet ) {
55         struct usb_device *usb = usbnet->func->usb;
56         int rc;
57
58         /* Open interrupt endpoint */
59         if ( ( rc = usb_endpoint_open ( &usbnet->intr ) ) != 0 ) {
60                 DBGC ( usbnet, "USBNET %s could not open interrupt: %s\n",
61                        usbnet->func->name, strerror ( rc ) );
62                 goto err_open_intr;
63         }
64
65         /* Refill interrupt endpoint */
66         if ( ( rc = usb_refill ( &usbnet->intr ) ) != 0 ) {
67                 DBGC ( usbnet, "USBNET %s could not refill interrupt: %s\n",
68                        usbnet->func->name, strerror ( rc ) );
69                 goto err_refill_intr;
70         }
71
72         /* Select alternate setting for data interface, if applicable */
73         if ( usbnet->alternate &&
74              ( ( rc = usb_set_interface ( usb, usbnet->data,
75                                           usbnet->alternate ) ) != 0 ) ) {
76                 DBGC ( usbnet, "USBNET %s could not set alternate interface "
77                        "%d: %s\n", usbnet->func->name, usbnet->alternate,
78                        strerror ( rc ) );
79                 goto err_set_interface;
80         }
81
82         /* Open bulk IN endpoint */
83         if ( ( rc = usb_endpoint_open ( &usbnet->in ) ) != 0 ) {
84                 DBGC ( usbnet, "USBNET %s could not open bulk IN: %s\n",
85                        usbnet->func->name, strerror ( rc ) );
86                 goto err_open_in;
87         }
88
89         /* Open bulk OUT endpoint */
90         if ( ( rc = usb_endpoint_open ( &usbnet->out ) ) != 0 ) {
91                 DBGC ( usbnet, "USBNET %s could not open bulk OUT: %s\n",
92                        usbnet->func->name, strerror ( rc ) );
93                 goto err_open_out;
94         }
95
96         /* Refill bulk IN endpoint */
97         if ( ( rc = usb_refill ( &usbnet->in ) ) != 0 ) {
98                 DBGC ( usbnet, "USBNET %s could not refill bulk IN: %s\n",
99                        usbnet->func->name, strerror ( rc ) );
100                 goto err_refill_in;
101         }
102
103         return 0;
104
105  err_refill_in:
106         usb_endpoint_close ( &usbnet->out );
107  err_open_out:
108         usb_endpoint_close ( &usbnet->in );
109  err_open_in:
110         if ( usbnet->alternate )
111                 usb_set_interface ( usb, usbnet->data, 0 );
112  err_set_interface:
113  err_refill_intr:
114         usb_endpoint_close ( &usbnet->intr );
115  err_open_intr:
116         return rc;
117 }
118
119 /**
120  * Close USB network device
121  *
122  * @v usbnet            USB network device
123  */
124 void usbnet_close ( struct usbnet_device *usbnet ) {
125         struct usb_device *usb = usbnet->func->usb;
126
127         /* Close bulk OUT endpoint */
128         usb_endpoint_close ( &usbnet->out );
129
130         /* Close bulk IN endpoint */
131         usb_endpoint_close ( &usbnet->in );
132
133         /* Reset alternate setting for data interface, if applicable */
134         if ( usbnet->alternate )
135                 usb_set_interface ( usb, usbnet->data, 0 );
136
137         /* Close interrupt endpoint */
138         usb_endpoint_close ( &usbnet->intr );
139 }
140
141 /**
142  * Refill USB network device bulk IN and interrupt endpoints
143  *
144  * @v usbnet            USB network device
145  * @ret rc              Return status code
146  */
147 int usbnet_refill ( struct usbnet_device *usbnet ) {
148         int rc;
149
150         /* Refill bulk IN endpoint */
151         if ( ( rc = usb_refill ( &usbnet->in ) ) != 0 )
152                 return rc;
153
154         /* Refill interrupt endpoint */
155         if ( ( rc = usb_refill ( &usbnet->intr ) ) != 0 )
156                 return rc;
157
158         return 0;
159 }
160
161 /**
162  * Describe communications interface and interrupt endpoint
163  *
164  * @v usbnet            USB network device
165  * @v config            Configuration descriptor
166  * @ret rc              Return status code
167  */
168 static int usbnet_comms_describe ( struct usbnet_device *usbnet,
169                                    struct usb_configuration_descriptor *config){
170         struct usb_interface_descriptor *desc;
171         unsigned int comms;
172         unsigned int i;
173         int rc;
174
175         /* Iterate over all available interfaces */
176         for ( i = 0 ; i < usbnet->func->count ; i++ ) {
177
178                 /* Get interface number */
179                 comms = usbnet->func->interface[i];
180
181                 /* Locate interface descriptor */
182                 desc = usb_interface_descriptor ( config, comms, 0 );
183                 if ( ! desc )
184                         continue;
185
186                 /* Describe interrupt endpoint */
187                 if ( ( rc = usb_endpoint_described ( &usbnet->intr, config,
188                                                      desc, USB_INTERRUPT_IN,
189                                                      0 ) ) != 0 )
190                         continue;
191
192                 /* Record communications interface */
193                 usbnet->comms = comms;
194                 DBGC ( usbnet, "USBNET %s found communications interface %d\n",
195                        usbnet->func->name, comms );
196                 return 0;
197         }
198
199         DBGC ( usbnet, "USBNET %s found no communications interface\n",
200                usbnet->func->name );
201         return -ENOENT;
202 }
203
204 /**
205  * Describe data interface and bulk endpoints
206  *
207  * @v usbnet            USB network device
208  * @v config            Configuration descriptor
209  * @ret rc              Return status code
210  */
211 static int usbnet_data_describe ( struct usbnet_device *usbnet,
212                                   struct usb_configuration_descriptor *config ){
213         struct usb_interface_descriptor *desc;
214         unsigned int data;
215         unsigned int alt;
216         unsigned int i;
217         int rc;
218
219         /* Iterate over all available interfaces */
220         for ( i = 0 ; i < usbnet->func->count ; i++ ) {
221
222                 /* Get interface number */
223                 data = usbnet->func->interface[i];
224
225                 /* Iterate over all existent alternate settings */
226                 for ( alt = 0 ; ; alt++ ) {
227
228                         /* Locate interface descriptor */
229                         desc = usb_interface_descriptor ( config, data, alt );
230                         if ( ! desc )
231                                 break;
232
233                         /* Describe bulk IN endpoint */
234                         if ( ( rc = usb_endpoint_described ( &usbnet->in,
235                                                              config, desc,
236                                                              USB_BULK_IN,
237                                                              0 ) ) != 0 )
238                                 continue;
239
240                         /* Describe bulk OUT endpoint */
241                         if ( ( rc = usb_endpoint_described ( &usbnet->out,
242                                                              config, desc,
243                                                              USB_BULK_OUT,
244                                                              0 ) ) != 0 )
245                                 continue;
246
247                         /* Record data interface and alternate setting */
248                         usbnet->data = data;
249                         usbnet->alternate = alt;
250                         DBGC ( usbnet, "USBNET %s found data interface %d",
251                                usbnet->func->name, data );
252                         if ( alt )
253                                 DBGC ( usbnet, " using alternate %d", alt );
254                         DBGC ( usbnet, "\n" );
255                         return 0;
256                 }
257         }
258
259         DBGC ( usbnet, "USBNET %s found no data interface\n",
260                usbnet->func->name );
261         return -ENOENT;
262 }
263
264 /**
265  * Describe USB network device interfaces
266  *
267  * @v usbnet            USB network device
268  * @v config            Configuration descriptor
269  * @ret rc              Return status code
270  */
271 int usbnet_describe ( struct usbnet_device *usbnet,
272                       struct usb_configuration_descriptor *config ) {
273         int rc;
274
275         /* Describe communications interface */
276         if ( ( rc = usbnet_comms_describe ( usbnet, config ) ) != 0 )
277                 return rc;
278
279         /* Describe data interface */
280         if ( ( rc = usbnet_data_describe ( usbnet, config ) ) != 0 )
281                 return rc;
282
283         return 0;
284 }