iPXE
Functions
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)
 
int usbnet_open (struct usbnet_device *usbnet)
 Open USB network device. More...
 
void usbnet_close (struct usbnet_device *usbnet)
 Close USB network device. More...
 
int usbnet_refill (struct usbnet_device *usbnet)
 Refill USB network device bulk IN and interrupt endpoints. More...
 
static int usbnet_comms_describe (struct usbnet_device *usbnet, struct usb_configuration_descriptor *config)
 Describe communications interface and interrupt endpoint. More...
 
static int usbnet_data_describe (struct usbnet_device *usbnet, struct usb_configuration_descriptor *config)
 Describe data interface and bulk endpoints. More...
 
int usbnet_describe (struct usbnet_device *usbnet, struct usb_configuration_descriptor *config)
 Describe USB network device interfaces. More...
 

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:

Definition in file usbnet.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ 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 54 of file usbnet.c.

54  {
55  struct usb_device *usb = usbnet->func->usb;
56  int rc;
57 
58  /* Open interrupt endpoint, if applicable */
59  if ( usbnet_has_intr ( usbnet ) &&
60  ( rc = usb_endpoint_open ( &usbnet->intr ) ) != 0 ) {
61  DBGC ( usbnet, "USBNET %s could not open interrupt: %s\n",
62  usbnet->func->name, strerror ( rc ) );
63  goto err_open_intr;
64  }
65 
66  /* Refill interrupt endpoint, if applicable */
67  if ( usbnet_has_intr ( usbnet ) &&
68  ( rc = usb_refill ( &usbnet->intr ) ) != 0 ) {
69  DBGC ( usbnet, "USBNET %s could not refill interrupt: %s\n",
70  usbnet->func->name, strerror ( rc ) );
71  goto err_refill_intr;
72  }
73 
74  /* Select alternate setting for data interface, if applicable */
75  if ( usbnet->alternate &&
76  ( ( rc = usb_set_interface ( usb, usbnet->data,
77  usbnet->alternate ) ) != 0 ) ) {
78  DBGC ( usbnet, "USBNET %s could not set alternate interface "
79  "%d: %s\n", usbnet->func->name, usbnet->alternate,
80  strerror ( rc ) );
81  goto err_set_interface;
82  }
83 
84  /* Open bulk IN endpoint */
85  if ( ( rc = usb_endpoint_open ( &usbnet->in ) ) != 0 ) {
86  DBGC ( usbnet, "USBNET %s could not open bulk IN: %s\n",
87  usbnet->func->name, strerror ( rc ) );
88  goto err_open_in;
89  }
90 
91  /* Open bulk OUT endpoint */
92  if ( ( rc = usb_endpoint_open ( &usbnet->out ) ) != 0 ) {
93  DBGC ( usbnet, "USBNET %s could not open bulk OUT: %s\n",
94  usbnet->func->name, strerror ( rc ) );
95  goto err_open_out;
96  }
97 
98  /* Refill bulk IN endpoint */
99  if ( ( rc = usb_refill ( &usbnet->in ) ) != 0 ) {
100  DBGC ( usbnet, "USBNET %s could not refill bulk IN: %s\n",
101  usbnet->func->name, strerror ( rc ) );
102  goto err_refill_in;
103  }
104 
105  return 0;
106 
107  err_refill_in:
108  usb_endpoint_close ( &usbnet->out );
109  err_open_out:
110  usb_endpoint_close ( &usbnet->in );
111  err_open_in:
112  if ( usbnet->alternate )
113  usb_set_interface ( usb, usbnet->data, 0 );
114  err_set_interface:
115  err_refill_intr:
116  if ( usbnet_has_intr ( usbnet ) )
117  usb_endpoint_close ( &usbnet->intr );
118  err_open_intr:
119  return rc;
120 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Name.
Definition: usb.h:661
#define DBGC(...)
Definition: compiler.h:505
unsigned int data
Data interface.
Definition: usbnet.h:22
int usb_endpoint_open(struct usb_endpoint *ep)
Open USB endpoint.
Definition: usb.c:293
void usb_endpoint_close(struct usb_endpoint *ep)
Close USB endpoint.
Definition: usb.c:399
struct usb_endpoint intr
Interrupt endpoint.
Definition: usbnet.h:27
int usb_refill(struct usb_endpoint *ep)
Refill endpoint.
Definition: usb.c:710
static int usb_set_interface(struct usb_device *usb, unsigned int interface, unsigned int alternate)
Set USB interface alternate setting.
Definition: usb.h:1235
struct usb_endpoint out
Bulk OUT endpoint.
Definition: usbnet.h:31
A USB device.
Definition: usb.h:708
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
unsigned int alternate
Alternate setting for data interface.
Definition: usbnet.h:24
struct usb_endpoint in
Bulk IN endpoint.
Definition: usbnet.h:29
struct usb_device * usb
USB device.
Definition: usb.h:663
static int usbnet_has_intr(struct usbnet_device *usbnet)
Check if USB network device has an interrupt endpoint.
Definition: usbnet.h:63
struct usb_function * func
USB function.
Definition: usbnet.h:17

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 127 of file usbnet.c.

127  {
128  struct usb_device *usb = usbnet->func->usb;
129 
130  /* Close bulk OUT endpoint */
131  usb_endpoint_close ( &usbnet->out );
132 
133  /* Close bulk IN endpoint */
134  usb_endpoint_close ( &usbnet->in );
135 
136  /* Reset alternate setting for data interface, if applicable */
137  if ( usbnet->alternate )
138  usb_set_interface ( usb, usbnet->data, 0 );
139 
140  /* Close interrupt endpoint, if applicable */
141  if ( usbnet_has_intr ( usbnet ) )
142  usb_endpoint_close ( &usbnet->intr );
143 }
unsigned int data
Data interface.
Definition: usbnet.h:22
void usb_endpoint_close(struct usb_endpoint *ep)
Close USB endpoint.
Definition: usb.c:399
struct usb_endpoint intr
Interrupt endpoint.
Definition: usbnet.h:27
static int usb_set_interface(struct usb_device *usb, unsigned int interface, unsigned int alternate)
Set USB interface alternate setting.
Definition: usb.h:1235
struct usb_endpoint out
Bulk OUT endpoint.
Definition: usbnet.h:31
A USB device.
Definition: usb.h:708
unsigned int alternate
Alternate setting for data interface.
Definition: usbnet.h:24
struct usb_endpoint in
Bulk IN endpoint.
Definition: usbnet.h:29
struct usb_device * usb
USB device.
Definition: usb.h:663
static int usbnet_has_intr(struct usbnet_device *usbnet)
Check if USB network device has an interrupt endpoint.
Definition: usbnet.h:63
struct usb_function * func
USB function.
Definition: usbnet.h:17

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 151 of file usbnet.c.

151  {
152  int rc;
153 
154  /* Refill bulk IN endpoint */
155  if ( ( rc = usb_refill ( &usbnet->in ) ) != 0 )
156  return rc;
157 
158  /* Refill interrupt endpoint, if applicable */
159  if ( usbnet_has_intr ( usbnet ) &&
160  ( rc = usb_refill ( &usbnet->intr ) ) != 0 ) {
161  return rc;
162  }
163 
164  return 0;
165 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct usb_endpoint intr
Interrupt endpoint.
Definition: usbnet.h:27
int usb_refill(struct usb_endpoint *ep)
Refill endpoint.
Definition: usb.c:710
struct usb_endpoint in
Bulk IN endpoint.
Definition: usbnet.h:29
static int usbnet_has_intr(struct usbnet_device *usbnet)
Check if USB network device has an interrupt endpoint.
Definition: usbnet.h:63

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()

static 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 174 of file usbnet.c.

175  {
177  unsigned int comms;
178  unsigned int i;
179  int rc;
180 
181  /* Iterate over all available interfaces */
182  for ( i = 0 ; i < usbnet->func->desc.count ; i++ ) {
183 
184  /* Get interface number */
185  comms = usbnet->func->interface[i];
186 
187  /* Locate interface descriptor */
188  desc = usb_interface_descriptor ( config, comms, 0 );
189  if ( ! desc )
190  continue;
191 
192  /* Describe interrupt endpoint */
193  if ( ( rc = usb_endpoint_described ( &usbnet->intr, config,
195  0 ) ) != 0 )
196  continue;
197 
198  /* Record communications interface */
199  usbnet->comms = comms;
200  DBGC ( usbnet, "USBNET %s found communications interface %d\n",
201  usbnet->func->name, comms );
202  return 0;
203  }
204 
205  DBGC ( usbnet, "USBNET %s found no communications interface\n",
206  usbnet->func->name );
207  return -ENOENT;
208 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned int comms
Communications interface.
Definition: usbnet.h:20
uint8_t interface[0]
List of interface numbers.
Definition: usb.h:682
const char * name
Name.
Definition: usb.h:661
unsigned int count
Number of interfaces.
Definition: usb.h:650
uint64_t desc
Microcode descriptor list physical address.
Definition: ucode.h:12
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define USB_INTERRUPT_IN
Interrupt IN endpoint (internal) type.
Definition: usb.h:287
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:241
struct usb_endpoint intr
Interrupt endpoint.
Definition: usbnet.h:27
A USB interface descriptor.
Definition: usb.h:230
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:143
struct usb_function_descriptor desc
Function descriptor.
Definition: usb.h:665
struct usb_function * func
USB function.
Definition: usbnet.h:17

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()

static 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 217 of file usbnet.c.

218  {
220  unsigned int data;
221  unsigned int alt;
222  unsigned int i;
223  int rc;
224 
225  /* Iterate over all available interfaces */
226  for ( i = 0 ; i < usbnet->func->desc.count ; i++ ) {
227 
228  /* Get interface number */
229  data = usbnet->func->interface[i];
230 
231  /* Iterate over all existent alternate settings */
232  for ( alt = 0 ; ; alt++ ) {
233 
234  /* Locate interface descriptor */
235  desc = usb_interface_descriptor ( config, data, alt );
236  if ( ! desc )
237  break;
238 
239  /* Describe bulk IN endpoint */
240  if ( ( rc = usb_endpoint_described ( &usbnet->in,
241  config, desc,
242  USB_BULK_IN,
243  0 ) ) != 0 )
244  continue;
245 
246  /* Describe bulk OUT endpoint */
247  if ( ( rc = usb_endpoint_described ( &usbnet->out,
248  config, desc,
249  USB_BULK_OUT,
250  0 ) ) != 0 )
251  continue;
252 
253  /* Record data interface and alternate setting */
254  usbnet->data = data;
255  usbnet->alternate = alt;
256  DBGC ( usbnet, "USBNET %s found data interface %d",
257  usbnet->func->name, data );
258  if ( alt )
259  DBGC ( usbnet, " using alternate %d", alt );
260  DBGC ( usbnet, "\n" );
261  return 0;
262  }
263  }
264 
265  DBGC ( usbnet, "USBNET %s found no data interface\n",
266  usbnet->func->name );
267  return -ENOENT;
268 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint8_t interface[0]
List of interface numbers.
Definition: usb.h:682
const char * name
Name.
Definition: usb.h:661
unsigned int count
Number of interfaces.
Definition: usb.h:650
uint64_t desc
Microcode descriptor list physical address.
Definition: ucode.h:12
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
unsigned int data
Data interface.
Definition: usbnet.h:22
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:241
struct usb_endpoint out
Bulk OUT endpoint.
Definition: usbnet.h:31
A USB interface descriptor.
Definition: usb.h:230
#define USB_BULK_OUT
Bulk OUT endpoint (internal) type.
Definition: usb.h:281
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:143
unsigned int alternate
Alternate setting for data interface.
Definition: usbnet.h:24
struct usb_endpoint in
Bulk IN endpoint.
Definition: usbnet.h:29
#define USB_BULK_IN
Bulk IN endpoint (internal) type.
Definition: usb.h:284
uint8_t data[48]
Additional event data.
Definition: ena.h:22
struct usb_function_descriptor desc
Function descriptor.
Definition: usb.h:665
struct usb_function * func
USB function.
Definition: usbnet.h:17

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 277 of file usbnet.c.

278  {
279  int rc;
280 
281  /* Describe communications interface, if applicable */
282  if ( usbnet_has_intr ( usbnet ) &&
283  ( rc = usbnet_comms_describe ( usbnet, config ) ) != 0 ) {
284  return rc;
285  }
286 
287  /* Describe data interface */
288  if ( ( rc = usbnet_data_describe ( usbnet, config ) ) != 0 )
289  return rc;
290 
291  return 0;
292 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int usbnet_comms_describe(struct usbnet_device *usbnet, struct usb_configuration_descriptor *config)
Describe communications interface and interrupt endpoint.
Definition: usbnet.c:174
static int usbnet_data_describe(struct usbnet_device *usbnet, struct usb_configuration_descriptor *config)
Describe data interface and bulk endpoints.
Definition: usbnet.c:217
static int usbnet_has_intr(struct usbnet_device *usbnet)
Check if USB network device has an interrupt endpoint.
Definition: usbnet.h:63

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().