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