iPXE
usbnet.c
Go to the documentation of this file.
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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <string.h>
28#include <errno.h>
29#include <ipxe/usb.h>
30#include <ipxe/usbnet.h>
31
32/** @file
33 *
34 * USB network devices
35 *
36 * USB network devices use a variety of packet formats and interface
37 * descriptors, but tend to have several features in common:
38 *
39 * - a single bulk OUT endpoint
40 *
41 * - a single bulk IN endpoint using the generic refill mechanism
42 *
43 * - an optional interrupt endpoint using the generic refill mechanism
44 *
45 * - optional use of an alternate setting to enable the data interface
46 *
47 */
48
49/**
50 * Open USB network device
51 *
52 * @v usbnet USB network device
53 * @ret rc Return status code
54 */
55int usbnet_open ( struct usbnet_device *usbnet ) {
56 struct usb_device *usb = usbnet->func->usb;
57 int rc;
58
59 /* Open interrupt endpoint, if applicable */
60 if ( usbnet_has_intr ( usbnet ) &&
61 ( rc = usb_endpoint_open ( &usbnet->intr ) ) != 0 ) {
62 DBGC ( usbnet, "USBNET %s could not open interrupt: %s\n",
63 usbnet->func->name, strerror ( rc ) );
64 goto err_open_intr;
65 }
66
67 /* Refill interrupt endpoint, if applicable */
68 if ( usbnet_has_intr ( usbnet ) &&
69 ( rc = usb_refill ( &usbnet->intr ) ) != 0 ) {
70 DBGC ( usbnet, "USBNET %s could not refill interrupt: %s\n",
71 usbnet->func->name, strerror ( rc ) );
72 goto err_refill_intr;
73 }
74
75 /* Select alternate setting for data interface, if applicable */
76 if ( usbnet->alternate &&
77 ( ( rc = usb_set_interface ( usb, usbnet->data,
78 usbnet->alternate ) ) != 0 ) ) {
79 DBGC ( usbnet, "USBNET %s could not set alternate interface "
80 "%d: %s\n", usbnet->func->name, usbnet->alternate,
81 strerror ( rc ) );
82 goto err_set_interface;
83 }
84
85 /* Open bulk IN endpoint */
86 if ( ( rc = usb_endpoint_open ( &usbnet->in ) ) != 0 ) {
87 DBGC ( usbnet, "USBNET %s could not open bulk IN: %s\n",
88 usbnet->func->name, strerror ( rc ) );
89 goto err_open_in;
90 }
91
92 /* Open bulk OUT endpoint */
93 if ( ( rc = usb_endpoint_open ( &usbnet->out ) ) != 0 ) {
94 DBGC ( usbnet, "USBNET %s could not open bulk OUT: %s\n",
95 usbnet->func->name, strerror ( rc ) );
96 goto err_open_out;
97 }
98
99 /* Refill bulk IN endpoint */
100 if ( ( rc = usb_refill ( &usbnet->in ) ) != 0 ) {
101 DBGC ( usbnet, "USBNET %s could not refill bulk IN: %s\n",
102 usbnet->func->name, strerror ( rc ) );
103 goto err_refill_in;
104 }
105
106 return 0;
107
108 err_refill_in:
109 usb_endpoint_close ( &usbnet->out );
110 err_open_out:
111 usb_endpoint_close ( &usbnet->in );
112 err_open_in:
113 if ( usbnet->alternate )
114 usb_set_interface ( usb, usbnet->data, 0 );
115 err_set_interface:
116 err_refill_intr:
117 if ( usbnet_has_intr ( usbnet ) )
118 usb_endpoint_close ( &usbnet->intr );
119 err_open_intr:
120 return rc;
121}
122
123/**
124 * Close USB network device
125 *
126 * @v usbnet USB network device
127 */
128void usbnet_close ( struct usbnet_device *usbnet ) {
129 struct usb_device *usb = usbnet->func->usb;
130
131 /* Close bulk OUT endpoint */
132 usb_endpoint_close ( &usbnet->out );
133
134 /* Close bulk IN endpoint */
135 usb_endpoint_close ( &usbnet->in );
136
137 /* Reset alternate setting for data interface, if applicable */
138 if ( usbnet->alternate )
139 usb_set_interface ( usb, usbnet->data, 0 );
140
141 /* Close interrupt endpoint, if applicable */
142 if ( usbnet_has_intr ( usbnet ) )
143 usb_endpoint_close ( &usbnet->intr );
144}
145
146/**
147 * Refill USB network device bulk IN and interrupt endpoints
148 *
149 * @v usbnet USB network device
150 * @ret rc Return status code
151 */
152int usbnet_refill ( struct usbnet_device *usbnet ) {
153 int rc;
154
155 /* Refill bulk IN endpoint */
156 if ( ( rc = usb_refill ( &usbnet->in ) ) != 0 )
157 return rc;
158
159 /* Refill interrupt endpoint, if applicable */
160 if ( usbnet_has_intr ( usbnet ) &&
161 ( rc = usb_refill ( &usbnet->intr ) ) != 0 ) {
162 return rc;
163 }
164
165 return 0;
166}
167
168/**
169 * Describe communications interface and interrupt endpoint
170 *
171 * @v usbnet USB network device
172 * @v config Configuration descriptor
173 * @ret rc Return status code
174 */
175static int usbnet_comms_describe ( struct usbnet_device *usbnet,
176 struct usb_configuration_descriptor *config){
178 unsigned int comms;
179 unsigned int i;
180 int rc;
181
182 /* Iterate over all available interfaces */
183 for ( i = 0 ; i < usbnet->func->desc.count ; i++ ) {
184
185 /* Get interface number */
186 comms = usbnet->func->interface[i];
187
188 /* Locate interface descriptor */
189 desc = usb_interface_descriptor ( config, comms, 0 );
190 if ( ! desc )
191 continue;
192
193 /* Describe interrupt endpoint */
194 if ( ( rc = usb_endpoint_described ( &usbnet->intr, config,
196 0 ) ) != 0 )
197 continue;
198
199 /* Record communications interface */
200 usbnet->comms = comms;
201 DBGC ( usbnet, "USBNET %s found communications interface %d\n",
202 usbnet->func->name, comms );
203 return 0;
204 }
205
206 DBGC ( usbnet, "USBNET %s found no communications interface\n",
207 usbnet->func->name );
208 return -ENOENT;
209}
210
211/**
212 * Describe data interface and bulk endpoints
213 *
214 * @v usbnet USB network device
215 * @v config Configuration descriptor
216 * @ret rc Return status code
217 */
218static int usbnet_data_describe ( struct usbnet_device *usbnet,
219 struct usb_configuration_descriptor *config ){
221 unsigned int data;
222 unsigned int alt;
223 unsigned int i;
224 int rc;
225
226 /* Iterate over all available interfaces */
227 for ( i = 0 ; i < usbnet->func->desc.count ; i++ ) {
228
229 /* Get interface number */
230 data = usbnet->func->interface[i];
231
232 /* Iterate over all existent alternate settings */
233 for ( alt = 0 ; ; alt++ ) {
234
235 /* Locate interface descriptor */
236 desc = usb_interface_descriptor ( config, data, alt );
237 if ( ! desc )
238 break;
239
240 /* Describe bulk IN endpoint */
241 if ( ( rc = usb_endpoint_described ( &usbnet->in,
242 config, desc,
244 0 ) ) != 0 )
245 continue;
246
247 /* Describe bulk OUT endpoint */
248 if ( ( rc = usb_endpoint_described ( &usbnet->out,
249 config, desc,
251 0 ) ) != 0 )
252 continue;
253
254 /* Record data interface and alternate setting */
255 usbnet->data = data;
256 usbnet->alternate = alt;
257 DBGC ( usbnet, "USBNET %s found data interface %d",
258 usbnet->func->name, data );
259 if ( alt )
260 DBGC ( usbnet, " using alternate %d", alt );
261 DBGC ( usbnet, "\n" );
262 return 0;
263 }
264 }
265
266 DBGC ( usbnet, "USBNET %s found no data interface\n",
267 usbnet->func->name );
268 return -ENOENT;
269}
270
271/**
272 * Describe USB network device interfaces
273 *
274 * @v usbnet USB network device
275 * @v config Configuration descriptor
276 * @ret rc Return status code
277 */
278int usbnet_describe ( struct usbnet_device *usbnet,
279 struct usb_configuration_descriptor *config ) {
280 int rc;
281
282 /* Describe communications interface, if applicable */
283 if ( usbnet_has_intr ( usbnet ) &&
284 ( rc = usbnet_comms_describe ( usbnet, config ) ) != 0 ) {
285 return rc;
286 }
287
288 /* Describe data interface */
289 if ( ( rc = usbnet_data_describe ( usbnet, config ) ) != 0 )
290 return rc;
291
292 return 0;
293}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
uint8_t data[48]
Additional event data.
Definition ena.h:11
struct ena_llq_option desc
Descriptor counts.
Definition ena.h:9
Error codes.
#define DBGC(...)
Definition compiler.h:505
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define ENOENT
No such file or directory.
Definition errno.h:515
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
Universal Serial Bus (USB)
#define USB_BULK_OUT
Bulk OUT endpoint (internal) type.
Definition usb.h:296
static int usb_set_interface(struct usb_device *usb, unsigned int interface, unsigned int alternate)
Set USB interface alternate setting.
Definition usb.h:1256
#define USB_BULK_IN
Bulk IN endpoint (internal) type.
Definition usb.h:299
#define USB_INTERRUPT_IN
Interrupt IN endpoint (internal) type.
Definition usb.h:302
String functions.
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
A USB configuration descriptor.
Definition usb.h:210
A USB device.
Definition usb.h:723
unsigned int count
Number of interfaces.
Definition usb.h:665
struct usb_device * usb
USB device.
Definition usb.h:678
struct usb_function_descriptor desc
Function descriptor.
Definition usb.h:680
uint8_t interface[0]
List of interface numbers.
Definition usb.h:697
const char * name
Name.
Definition usb.h:676
A USB interface descriptor.
Definition usb.h:245
A USB network device.
Definition usbnet.h:16
unsigned int alternate
Alternate setting for data interface.
Definition usbnet.h:25
struct usb_function * func
USB function.
Definition usbnet.h:18
struct usb_endpoint out
Bulk OUT endpoint.
Definition usbnet.h:32
struct usb_endpoint intr
Interrupt endpoint.
Definition usbnet.h:28
unsigned int comms
Communications interface.
Definition usbnet.h:21
struct usb_endpoint in
Bulk IN endpoint.
Definition usbnet.h:30
unsigned int data
Data interface.
Definition usbnet.h:23
int usb_endpoint_open(struct usb_endpoint *ep)
Open USB endpoint.
Definition usb.c:294
void usb_endpoint_close(struct usb_endpoint *ep)
Close USB endpoint.
Definition usb.c:400
int usb_endpoint_described(struct usb_endpoint *ep, struct usb_configuration_descriptor *config, struct usb_interface_descriptor *interface, unsigned int type, unsigned int index)
Describe USB endpoint from device configuration.
Definition usb.c:242
int usb_refill(struct usb_endpoint *ep)
Refill endpoint.
Definition usb.c:711
struct usb_interface_descriptor * usb_interface_descriptor(struct usb_configuration_descriptor *config, unsigned int interface, unsigned int alternate)
Locate USB interface descriptor.
Definition usb.c:144
int usbnet_refill(struct usbnet_device *usbnet)
Refill USB network device bulk IN and interrupt endpoints.
Definition usbnet.c:152
static int usbnet_data_describe(struct usbnet_device *usbnet, struct usb_configuration_descriptor *config)
Describe data interface and bulk endpoints.
Definition usbnet.c:218
int usbnet_open(struct usbnet_device *usbnet)
Open USB network device.
Definition usbnet.c:55
void usbnet_close(struct usbnet_device *usbnet)
Close USB network device.
Definition usbnet.c:128
int usbnet_describe(struct usbnet_device *usbnet, struct usb_configuration_descriptor *config)
Describe USB network device interfaces.
Definition usbnet.c:278
static int usbnet_comms_describe(struct usbnet_device *usbnet, struct usb_configuration_descriptor *config)
Describe communications interface and interrupt endpoint.
Definition usbnet.c:175
USB network devices.
static int usbnet_has_intr(struct usbnet_device *usbnet)
Check if USB network device has an interrupt endpoint.
Definition usbnet.h:64