iPXE
usbnet.c File Reference

USB network devices. More...

#include <string.h>
#include <errno.h>
#include <ipxe/usb.h>
#include <ipxe/usbnet.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
int usbnet_open (struct usbnet_device *usbnet)
 Open USB network device.
void usbnet_close (struct usbnet_device *usbnet)
 Close USB network device.
int usbnet_refill (struct usbnet_device *usbnet)
 Refill USB network device bulk IN and interrupt endpoints.
static int usbnet_comms_describe (struct usbnet_device *usbnet, struct usb_configuration_descriptor *config)
 Describe communications interface and interrupt endpoint.
static int usbnet_data_describe (struct usbnet_device *usbnet, struct usb_configuration_descriptor *config)
 Describe data interface and bulk endpoints.
int usbnet_describe (struct usbnet_device *usbnet, struct usb_configuration_descriptor *config)
 Describe USB network device interfaces.

Detailed Description

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 bulk OUT endpoint
  • a single bulk IN endpoint using the generic refill mechanism
  • an optional interrupt endpoint using the generic refill mechanism
  • optional use of an alternate setting to enable the data interface

Definition in file usbnet.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ usbnet_open()

int usbnet_open ( struct usbnet_device * usbnet)

Open USB network device.

Parameters
usbnetUSB network device
Return values
rcReturn status code

Definition at line 55 of file usbnet.c.

55 {
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}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
#define DBGC(...)
Definition compiler.h:505
static int usb_set_interface(struct usb_device *usb, unsigned int interface, unsigned int alternate)
Set USB interface alternate setting.
Definition usb.h:1256
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
A USB device.
Definition usb.h:723
struct usb_device * usb
USB device.
Definition usb.h:678
const char * name
Name.
Definition usb.h:676
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
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_refill(struct usb_endpoint *ep)
Refill endpoint.
Definition usb.c:711
static int usbnet_has_intr(struct usbnet_device *usbnet)
Check if USB network device has an interrupt endpoint.
Definition usbnet.h:64

References usbnet_device::alternate, usbnet_device::data, DBGC, usbnet_device::func, usbnet_device::in, usbnet_device::intr, usb_function::name, usbnet_device::out, rc, strerror(), usb_function::usb, usb_endpoint_close(), usb_endpoint_open(), usb_refill(), usb_set_interface(), and usbnet_has_intr().

Referenced by acm_open(), axge_open(), dm96xx_open(), ecm_open(), imux_probe(), iphone_open(), lan78xx_open(), ncm_open(), smsc75xx_open(), and smsc95xx_open().

◆ usbnet_close()

void usbnet_close ( struct usbnet_device * usbnet)

Close USB network device.

Parameters
usbnetUSB network device

Definition at line 128 of file usbnet.c.

128 {
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}

References usbnet_device::alternate, usbnet_device::data, usbnet_device::func, usbnet_device::in, usbnet_device::intr, usbnet_device::out, usb_function::usb, usb_endpoint_close(), usb_set_interface(), and usbnet_has_intr().

Referenced by acm_close(), acm_open(), axge_close(), axge_open(), dm96xx_close(), dm96xx_open(), ecm_close(), ecm_open(), imux_shutdown(), iphone_close(), iphone_open(), lan78xx_close(), lan78xx_open(), ncm_close(), ncm_open(), smsc75xx_close(), smsc75xx_open(), smsc95xx_close(), and smsc95xx_open().

◆ usbnet_refill()

int usbnet_refill ( struct usbnet_device * usbnet)

Refill USB network device bulk IN and interrupt endpoints.

Parameters
usbnetUSB network device
Return values
rcReturn status code

Definition at line 152 of file usbnet.c.

152 {
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}

References usbnet_device::in, usbnet_device::intr, rc, usb_refill(), and usbnet_has_intr().

Referenced by acm_poll(), axge_poll(), dm96xx_poll(), ecm_poll(), imux_step(), iphone_poll(), ncm_poll(), smsc75xx_poll(), and smsc95xx_poll().

◆ usbnet_comms_describe()

int usbnet_comms_describe ( struct usbnet_device * usbnet,
struct usb_configuration_descriptor * config )
static

Describe communications interface and interrupt endpoint.

Parameters
usbnetUSB network device
configConfiguration descriptor
Return values
rcReturn status code

Definition at line 175 of file usbnet.c.

176 {
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}
struct ena_llq_option desc
Descriptor counts.
Definition ena.h:9
#define ENOENT
No such file or directory.
Definition errno.h:515
#define USB_INTERRUPT_IN
Interrupt IN endpoint (internal) type.
Definition usb.h:302
unsigned int count
Number of interfaces.
Definition usb.h:665
struct usb_function_descriptor desc
Function descriptor.
Definition usb.h:680
uint8_t interface[0]
List of interface numbers.
Definition usb.h:697
A USB interface descriptor.
Definition usb.h:245
unsigned int comms
Communications interface.
Definition usbnet.h:21
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
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

References usbnet_device::comms, usb_function_descriptor::count, DBGC, desc, usb_function::desc, ENOENT, usbnet_device::func, usb_function::interface, usbnet_device::intr, usb_function::name, rc, usb_endpoint_described(), usb_interface_descriptor(), and USB_INTERRUPT_IN.

Referenced by usbnet_describe().

◆ usbnet_data_describe()

int usbnet_data_describe ( struct usbnet_device * usbnet,
struct usb_configuration_descriptor * config )
static

Describe data interface and bulk endpoints.

Parameters
usbnetUSB network device
configConfiguration descriptor
Return values
rcReturn status code

Definition at line 218 of file usbnet.c.

219 {
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}
uint8_t data[48]
Additional event data.
Definition ena.h:11
#define USB_BULK_OUT
Bulk OUT endpoint (internal) type.
Definition usb.h:296
#define USB_BULK_IN
Bulk IN endpoint (internal) type.
Definition usb.h:299

References usbnet_device::alternate, usb_function_descriptor::count, data, usbnet_device::data, DBGC, desc, usb_function::desc, ENOENT, usbnet_device::func, usbnet_device::in, usb_function::interface, usb_function::name, usbnet_device::out, rc, USB_BULK_IN, USB_BULK_OUT, usb_endpoint_described(), and usb_interface_descriptor().

Referenced by usbnet_describe().

◆ usbnet_describe()

int usbnet_describe ( struct usbnet_device * usbnet,
struct usb_configuration_descriptor * config )

Describe USB network device interfaces.

Parameters
usbnetUSB network device
configConfiguration descriptor
Return values
rcReturn status code

Definition at line 278 of file usbnet.c.

279 {
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}
static int usbnet_data_describe(struct usbnet_device *usbnet, struct usb_configuration_descriptor *config)
Describe data interface and bulk endpoints.
Definition usbnet.c:218
static int usbnet_comms_describe(struct usbnet_device *usbnet, struct usb_configuration_descriptor *config)
Describe communications interface and interrupt endpoint.
Definition usbnet.c:175

References rc, usbnet_comms_describe(), usbnet_data_describe(), and usbnet_has_intr().

Referenced by acm_probe(), axge_probe(), dm96xx_probe(), ecm_probe(), imux_probe(), iphone_probe(), lan78xx_probe(), ncm_probe(), smsc75xx_probe(), and smsc95xx_probe().