iPXE
usb.c File Reference

Universal Serial Bus (USB). More...

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <byteswap.h>
#include <ipxe/usb.h>
#include <ipxe/cdc.h>

Go to the source code of this file.

Data Structures

struct  usb_control_pseudo_header
 USB control transfer pseudo-header. More...

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static const char * usb_speed_name (unsigned int speed)
 Get USB speed name (for debugging).
static const char * usb_bcd (uint16_t bcd)
 Transcribe USB BCD-coded value (for debugging).
static struct usb_interface_association_descriptor * usb_interface_association_descriptor (struct usb_configuration_descriptor *config, unsigned int first)
 Locate USB interface association descriptor.
struct usb_interface_descriptor * usb_interface_descriptor (struct usb_configuration_descriptor *config, unsigned int interface, unsigned int alternate)
 Locate USB interface descriptor.
struct usb_endpoint_descriptor * usb_endpoint_descriptor (struct usb_configuration_descriptor *config, struct usb_interface_descriptor *interface, unsigned int type, unsigned int index)
 Locate USB endpoint descriptor.
struct usb_endpoint_companion_descriptor * usb_endpoint_companion_descriptor (struct usb_configuration_descriptor *config, struct usb_endpoint_descriptor *desc)
 Locate USB endpoint companion descriptor.
const char * usb_endpoint_name (struct usb_endpoint *ep)
 Get USB endpoint name (for debugging).
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.
int usb_endpoint_open (struct usb_endpoint *ep)
 Open USB endpoint.
static int usb_endpoint_clear_tt (struct usb_endpoint *ep)
 Clear transaction translator (if applicable).
int usb_endpoint_clear_halt (struct usb_endpoint *ep)
 Clear endpoint halt (if applicable).
void usb_endpoint_close (struct usb_endpoint *ep)
 Close USB endpoint.
static int usb_endpoint_reset (struct usb_endpoint *ep)
 Reset USB endpoint.
static int usb_endpoint_mtu (struct usb_endpoint *ep, size_t mtu)
 Update endpoint MTU.
int usb_message (struct usb_endpoint *ep, unsigned int request, unsigned int value, unsigned int index, struct io_buffer *iobuf)
 Enqueue USB message transfer.
int usb_stream (struct usb_endpoint *ep, struct io_buffer *iobuf, int terminate)
 Enqueue USB stream transfer.
void usb_complete_err (struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
 Complete transfer (possibly with error).
int usb_prefill (struct usb_endpoint *ep)
 Prefill endpoint recycled buffer list.
int usb_refill_limit (struct usb_endpoint *ep, unsigned int max)
 Refill endpoint up to specified limit.
int usb_refill (struct usb_endpoint *ep)
 Refill endpoint.
void usb_flush (struct usb_endpoint *ep)
 Discard endpoint recycled buffer list.
static void usb_control_complete (struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
 Complete USB control transfer.
int usb_control (struct usb_device *usb, unsigned int request, unsigned int value, unsigned int index, void *data, size_t len)
 Issue USB control transaction.
static unsigned int usb_get_default_language (struct usb_device *usb)
 Get default language ID.
int usb_get_string_descriptor (struct usb_device *usb, unsigned int index, unsigned int language, char *buf, size_t len)
 Get USB string descriptor.
static int usb_config_descriptor (struct usb_device *usb, unsigned int index, struct usb_configuration_descriptor **config)
 Get USB configuration descriptor.
static int usb_describe (struct usb_device *usb, struct usb_configuration_descriptor *config, unsigned int first, uint8_t *interfaces, struct usb_function_descriptor *desc)
 Describe USB function.
static int usb_used (struct usb_device *usb, unsigned int count, uint8_t *interface, uint8_t *used)
 Update list of used interface.
struct usb_driverusb_find_driver (struct usb_function_descriptor *desc, struct usb_device_id **id)
 Find USB device driver.
static int usb_score (struct usb_device *usb, struct usb_configuration_descriptor *config)
 Get USB device configuration score.
static int usb_probe (struct usb_function *func, struct usb_configuration_descriptor *config)
 Probe USB device driver.
static void usb_remove (struct usb_function *func)
 Remove USB device driver.
static void usb_probe_all (struct usb_device *usb, struct usb_configuration_descriptor *config)
 Probe all USB device drivers.
static void usb_remove_all (struct usb_device *usb)
 Remove all device drivers.
static void usb_deconfigure (struct usb_device *usb)
 Clear USB device configuration.
static int usb_autoconfigure (struct usb_device *usb)
 Choose our preferred USB device configuration.
static struct usb_devicealloc_usb (struct usb_port *port)
 Allocate USB device.
static int register_usb (struct usb_device *usb)
 Register USB device.
static void unregister_usb (struct usb_device *usb)
 Unregister USB device.
static void free_usb (struct usb_device *usb)
 Free USB device.
struct usb_devicefind_usb (struct usb_bus *bus, unsigned int address)
 Find USB device by address.
static int usb_attached (struct usb_port *port)
 Handle newly attached USB device.
static void usb_detached (struct usb_port *port)
 Handle newly detached USB device.
static int usb_hotplugged (struct usb_port *port)
 Handle newly attached or detached USB device.
void usb_port_changed (struct usb_port *port)
 Report port status change.
static void usb_hotplug (void)
 Handle newly attached or detached USB device.
static void usb_step (struct process *process __unused)
 USB process.
 PERMANENT_PROCESS (usb_process, usb_step)
 USB process.
struct usb_huballoc_usb_hub (struct usb_bus *bus, struct usb_device *usb, unsigned int ports, struct usb_hub_driver_operations *driver)
 Allocate USB hub.
int register_usb_hub (struct usb_hub *hub)
 Register USB hub.
void unregister_usb_hub (struct usb_hub *hub)
 Unregister USB hub.
void free_usb_hub (struct usb_hub *hub)
 Free USB hub.
struct usb_busalloc_usb_bus (struct device *dev, unsigned int ports, size_t mtu, struct usb_host_operations *op)
 Allocate USB bus.
int register_usb_bus (struct usb_bus *bus)
 Register USB bus.
void unregister_usb_bus (struct usb_bus *bus)
 Unregister USB bus.
void free_usb_bus (struct usb_bus *bus)
 Free USB bus.
struct usb_busfind_usb_bus (unsigned int address)
 Find USB bus by address.
struct usb_busfind_usb_bus_by_location (unsigned int bus_type, unsigned int location)
 Find USB bus by device location.
int usb_alloc_address (struct usb_bus *bus)
 Allocate device address.
void usb_free_address (struct usb_bus *bus, unsigned int address)
 Free device address.
int usb_find_next (struct usb_device **usb, uint16_t *busdev)
 Find next USB device.
unsigned int usb_route_string (struct usb_device *usb)
 Get USB route string.
struct usb_portusb_root_hub_port (struct usb_device *usb)
 Get USB root hub port.
struct usb_portusb_transaction_translator (struct usb_device *usb)
 Get USB transaction translator.
 REQUIRING_SYMBOL (register_usb_bus)
 REQUIRE_OBJECT (config_usb)
 REQUIRE_OBJECT (usbhub)

Variables

struct list_head usb_buses = LIST_HEAD_INIT ( usb_buses )
 List of USB buses.
static struct list_head usb_changed = LIST_HEAD_INIT ( usb_changed )
 List of changed ports.
static struct list_head usb_halted = LIST_HEAD_INIT ( usb_halted )
 List of halted endpoints.
static struct usb_endpoint_driver_operations usb_control_operations
 USB control endpoint driver operations.

Detailed Description

Universal Serial Bus (USB).

Definition in file usb.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ usb_speed_name()

const char * usb_speed_name ( unsigned int speed)
inlinestatic

Get USB speed name (for debugging).

Parameters
speedSpeed
Return values
nameSpeed name

Definition at line 66 of file usb.c.

66 {
67 static const char *exponents[4] = { "", "k", "M", "G" };
68 static char buf[ 10 /* "xxxxxXbps" + NUL */ ];
69 unsigned int mantissa;
70 unsigned int exponent;
71
72 /* Extract mantissa and exponent */
73 mantissa = USB_SPEED_MANTISSA ( speed );
74 exponent = USB_SPEED_EXPONENT ( speed );
75
76 /* Name speed */
77 switch ( speed ) {
78 case USB_SPEED_NONE: return "DETACHED";
79 case USB_SPEED_LOW: return "low";
80 case USB_SPEED_FULL: return "full";
81 case USB_SPEED_HIGH: return "high";
82 case USB_SPEED_SUPER: return "super";
83 default:
84 snprintf ( buf, sizeof ( buf ), "%d%sbps",
85 mantissa, exponents[exponent] );
86 return buf;
87 }
88}
#define USB_SPEED_MANTISSA(speed)
Extract USB speed mantissa.
Definition usb.h:39
@ USB_SPEED_LOW
Low speed (1.5Mbps).
Definition usb.h:49
@ USB_SPEED_FULL
Full speed (12Mbps).
Definition usb.h:51
@ USB_SPEED_HIGH
High speed (480Mbps).
Definition usb.h:53
@ USB_SPEED_NONE
Not connected.
Definition usb.h:47
@ USB_SPEED_SUPER
Super speed (5Gbps).
Definition usb.h:55
#define USB_SPEED_EXPONENT(speed)
Extract USB speed exponent.
Definition usb.h:42
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition vsprintf.c:383

References snprintf(), USB_SPEED_EXPONENT, USB_SPEED_FULL, USB_SPEED_HIGH, USB_SPEED_LOW, USB_SPEED_MANTISSA, USB_SPEED_NONE, and USB_SPEED_SUPER.

Referenced by register_usb().

◆ usb_bcd()

const char * usb_bcd ( uint16_t bcd)
inlinestatic

Transcribe USB BCD-coded value (for debugging).

Parameters
bcdBCD-coded value
Return values
stringTranscribed value

Definition at line 96 of file usb.c.

96 {
97 static char buf[ 6 /* "xx.xx" + NUL */ ];
98 uint8_t high = ( bcd >> 8 );
99 uint8_t low = ( bcd >> 0 );
100
101 snprintf ( buf, sizeof ( buf ), "%x.%02x", high, low );
102 return buf;
103}
unsigned char uint8_t
Definition stdint.h:10
uint32_t high
High 32 bits of address.
Definition myson.h:1
uint32_t low
Low 16 bits of address.
Definition myson.h:0

References high, low, and snprintf().

Referenced by register_usb().

◆ usb_interface_association_descriptor()

struct usb_interface_association_descriptor * usb_interface_association_descriptor ( struct usb_configuration_descriptor * config,
unsigned int first )
static

Locate USB interface association descriptor.

Parameters
configConfiguraton descriptor
firstFirst interface number
Return values
descInterface association descriptor, or NULL if not found

Definition at line 120 of file usb.c.

122 {
124
125 /* Find a matching interface association descriptor */
128 if ( desc->first == first )
129 return desc;
130 }
131 return NULL;
132}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
struct ena_llq_option desc
Descriptor counts.
Definition ena.h:9
#define USB_INTERFACE_ASSOCIATION_DESCRIPTOR
A USB interface association descriptor.
Definition usb.h:343
#define for_each_config_descriptor(desc, config, typeval)
Iterate over all configuration descriptors.
Definition usb.h:386
uint32_t first
First block in range.
Definition pccrr.h:1
A USB interface association descriptor.
Definition usb.h:329

References desc, first, for_each_config_descriptor, NULL, and USB_INTERFACE_ASSOCIATION_DESCRIPTOR.

Referenced by usb_describe().

◆ usb_interface_descriptor()

struct usb_interface_descriptor * usb_interface_descriptor ( struct usb_configuration_descriptor * config,
unsigned int interface,
unsigned int alternate )

Locate USB interface descriptor.

Parameters
configConfiguraton descriptor
interfaceInterface number
alternateAlternate setting
Return values
descInterface descriptor, or NULL if not found

Definition at line 143 of file usb.c.

144 {
146
147 /* Find a matching interface descriptor */
149 if ( ( desc->interface == interface ) &&
150 ( desc->alternate == alternate ) )
151 return desc;
152 }
153 return NULL;
154}
struct ib_cm_path alternate
Alternate path.
Definition ib_mad.h:31
#define USB_INTERFACE_DESCRIPTOR
A USB interface descriptor.
Definition usb.h:261
An object interface.
Definition interface.h:125
A USB interface descriptor.
Definition usb.h:245

References alternate, desc, for_each_config_descriptor, NULL, and USB_INTERFACE_DESCRIPTOR.

Referenced by ecm_probe(), efi_usb_get_interface_descriptor(), ncm_probe(), usbblk_probe(), usbhid_describe(), usbnet_comms_describe(), and usbnet_data_describe().

◆ usb_endpoint_descriptor()

struct usb_endpoint_descriptor * usb_endpoint_descriptor ( struct usb_configuration_descriptor * config,
struct usb_interface_descriptor * interface,
unsigned int type,
unsigned int index )

Locate USB endpoint descriptor.

Parameters
configConfiguration descriptor
interfaceInterface descriptor
typeEndpoint (internal) type
indexEndpoint index
Return values
descDescriptor, or NULL if not found

Definition at line 166 of file usb.c.

168 {
170 unsigned int attributes = ( type & USB_ENDPOINT_ATTR_TYPE_MASK );
171 unsigned int direction = ( type & USB_DIR_IN );
172
173 /* Find a matching endpoint descriptor */
176 if ( ( ( desc->attributes &
178 ( ( desc->endpoint & USB_DIR_IN ) == direction ) &&
179 ( index-- == 0 ) )
180 return desc;
181 }
182 return NULL;
183}
long index
Definition bigint.h:30
uint8_t direction
Direction.
Definition ena.h:3
uint32_t type
Operating system type.
Definition ena.h:1
#define USB_ENDPOINT_DESCRIPTOR
A USB endpoint descriptor.
Definition usb.h:278
#define USB_DIR_IN
Data transfer is from device to host.
Definition usb.h:98
#define USB_ENDPOINT_ATTR_TYPE_MASK
Endpoint attribute transfer type mask.
Definition usb.h:281
#define for_each_interface_descriptor(desc, config, interface, typeval)
Iterate over all configuration descriptors within an interface descriptor.
Definition usb.h:399
A USB endpoint descriptor.
Definition usb.h:264
uint8_t attributes
Attributes.
Definition usb.h:270

References usb_endpoint_descriptor::attributes, desc, direction, for_each_interface_descriptor, index, NULL, type, USB_DIR_IN, USB_ENDPOINT_ATTR_TYPE_MASK, and USB_ENDPOINT_DESCRIPTOR.

Referenced by usb_endpoint_described().

◆ usb_endpoint_companion_descriptor()

struct usb_endpoint_companion_descriptor * usb_endpoint_companion_descriptor ( struct usb_configuration_descriptor * config,
struct usb_endpoint_descriptor * desc )

Locate USB endpoint companion descriptor.

Parameters
configConfiguration descriptor
descEndpoint descriptor
Return values
descxCompanion descriptor, or NULL if not found

Definition at line 193 of file usb.c.

194 {
196
197 /* Get companion descriptor, if present */
198 descx = container_of ( usb_next_descriptor ( &desc->header ),
200 header );
201 return ( ( usb_is_within_config ( config, &descx->header ) &&
202 ( descx->header.len >= sizeof ( *descx ) ) &&
204 ? descx : NULL );
205}
struct ena_llq_option header
Header locations.
Definition ena.h:5
static int usb_is_within_config(struct usb_configuration_descriptor *config, struct usb_descriptor_header *desc)
Check that descriptor lies within a configuration descriptor.
Definition usb.h:371
#define USB_ENDPOINT_COMPANION_DESCRIPTOR
A USB endpoint companion descriptor.
Definition usb.h:326
static struct usb_descriptor_header * usb_next_descriptor(struct usb_descriptor_header *desc)
Get next USB descriptor.
Definition usb.h:358
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
uint8_t type
Descriptor type.
Definition usb.h:177
uint8_t len
Length of descriptor.
Definition usb.h:175
A USB endpoint companion descriptor.
Definition usb.h:314
struct usb_descriptor_header header
Descriptor header.
Definition usb.h:316

References container_of, desc, header, usb_endpoint_companion_descriptor::header, usb_descriptor_header::len, NULL, usb_descriptor_header::type, USB_ENDPOINT_COMPANION_DESCRIPTOR, usb_is_within_config(), and usb_next_descriptor().

Referenced by usb_endpoint_described().

◆ usb_endpoint_name()

const char * usb_endpoint_name ( struct usb_endpoint * ep)

Get USB endpoint name (for debugging).

Parameters
epUSB endpoint
Return values
nameEndpoint name

Definition at line 220 of file usb.c.

220 {
221 static char buf[ 9 /* "EPxx OUT" + NUL */ ];
222 unsigned int address = ep->address;
223
224 snprintf ( buf, sizeof ( buf ), "EP%d%s",
226 ( address ?
227 ( ( address & USB_ENDPOINT_IN ) ? " IN" : " OUT" ) : "" ));
228 return buf;
229}
uint64_t address
Base address.
Definition ena.h:13
#define USB_ENDPOINT_MAX
Maximum endpoint number.
Definition usb.h:532
#define USB_ENDPOINT_IN
Endpoint direction is in.
Definition usb.h:535
unsigned int address
Endpoint address.
Definition usb.h:418

References address, usb_endpoint::address, snprintf(), USB_ENDPOINT_IN, and USB_ENDPOINT_MAX.

Referenced by efi_usb_async_start(), efi_usb_close(), efi_usb_open(), efi_usb_sync_transfer(), ehci_endpoint_close(), ehci_endpoint_poll(), ehci_root_clear_tt(), uhci_endpoint_poll(), uhci_root_clear_tt(), usb_complete_err(), usb_endpoint_clear_halt(), usb_endpoint_clear_tt(), usb_endpoint_mtu(), usb_endpoint_open(), usb_endpoint_reset(), usb_message(), usb_stream(), usbio_bulk_in_poll(), usbio_bulk_out_poll(), usbio_control_poll(), usbio_endpoint_open(), usbio_interface(), usbio_interrupt_callback(), usbio_interrupt_open(), usbkbd_probe(), and xhci_root_clear_tt().

◆ usb_endpoint_described()

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.

Parameters
epUSB endpoint
configConfiguration descriptor
interfaceInterface descriptor
typeEndpoint (internal) type
indexEndpoint index
Return values
rcReturn status code

Definition at line 241 of file usb.c.

244 {
245 struct usb_device *usb = ep->usb;
248 unsigned int sizes;
249 unsigned int burst;
250 unsigned int interval;
251 size_t mtu;
252
253 /* Locate endpoint descriptor */
255 if ( ! desc )
256 return -ENOENT;
257
258 /* Locate companion descriptor, if any */
259 descx = usb_endpoint_companion_descriptor ( config, desc );
260
261 /* Calculate MTU and burst size */
262 sizes = le16_to_cpu ( desc->sizes );
263 mtu = USB_ENDPOINT_MTU ( sizes );
264 burst = ( descx ? descx->burst : USB_ENDPOINT_BURST ( sizes ) );
265
266 /* Calculate interval */
269 if ( usb->speed >= USB_SPEED_HIGH ) {
270 /* 2^(desc->interval-1) is a microframe count */
271 interval = ( 1 << ( desc->interval - 1 ) );
272 } else {
273 /* desc->interval is a (whole) frame count */
274 interval = ( desc->interval << 3 );
275 }
276 } else {
277 /* desc->interval is a microframe count */
278 interval = desc->interval;
279 }
280
281 /* Describe endpoint */
282 usb_endpoint_describe ( ep, desc->endpoint, desc->attributes,
283 mtu, burst, interval );
284 return 0;
285}
uint16_t burst
Maximum burst size.
Definition ena.h:17
uint32_t mtu
Maximum MTU.
Definition ena.h:17
#define ENOENT
No such file or directory.
Definition errno.h:558
#define le16_to_cpu(value)
Definition byteswap.h:113
#define USB_ENDPOINT_ATTR_INTERRUPT
Interrupt endpoint transfer type.
Definition usb.h:293
static void usb_endpoint_describe(struct usb_endpoint *ep, unsigned int address, unsigned int attributes, size_t mtu, unsigned int burst, unsigned int interval)
Describe USB endpoint.
Definition usb.h:568
#define USB_ENDPOINT_MTU(sizes)
USB endpoint MTU.
Definition usb.h:308
#define USB_ENDPOINT_BURST(sizes)
USB endpoint maximum burst size.
Definition usb.h:311
A USB device.
Definition usb.h:733
struct usb_endpoint * ep[32]
Endpoint list.
Definition usb.h:755
unsigned int speed
Device speed.
Definition usb.h:739
uint8_t burst
Maximum burst size.
Definition usb.h:318
struct usb_device * usb
USB device.
Definition usb.h:416
struct usb_endpoint_companion_descriptor * usb_endpoint_companion_descriptor(struct usb_configuration_descriptor *config, struct usb_endpoint_descriptor *desc)
Locate USB endpoint companion descriptor.
Definition usb.c:193
struct usb_endpoint_descriptor * usb_endpoint_descriptor(struct usb_configuration_descriptor *config, struct usb_interface_descriptor *interface, unsigned int type, unsigned int index)
Locate USB endpoint descriptor.
Definition usb.c:166

References burst, usb_endpoint_companion_descriptor::burst, desc, ENOENT, usb_device::ep, index, le16_to_cpu, mtu, usb_device::speed, type, usb_endpoint::usb, USB_ENDPOINT_ATTR_INTERRUPT, USB_ENDPOINT_ATTR_TYPE_MASK, USB_ENDPOINT_BURST, usb_endpoint_companion_descriptor(), usb_endpoint_describe(), usb_endpoint_descriptor(), USB_ENDPOINT_MTU, and USB_SPEED_HIGH.

Referenced by hub_probe(), usbblk_probe(), usbhid_describe(), usbnet_comms_describe(), and usbnet_data_describe().

◆ usb_endpoint_open()

int usb_endpoint_open ( struct usb_endpoint * ep)

Open USB endpoint.

Parameters
epUSB endpoint
Return values
rcReturn status code

Definition at line 293 of file usb.c.

293 {
294 struct usb_device *usb = ep->usb;
295 unsigned int idx = USB_ENDPOINT_IDX ( ep->address );
296 int rc;
297
298 /* Populate host controller operations */
299 ep->host = &usb->port->hub->bus->op->endpoint;
300
301 /* Add to endpoint list */
302 if ( usb->ep[idx] != NULL ) {
303 DBGC ( usb, "USB %s %s is already open\n",
304 usb->name, usb_endpoint_name ( ep ) );
305 rc = -EALREADY;
306 goto err_already;
307 }
308 usb->ep[idx] = ep;
310
311 /* Open endpoint */
312 if ( ( rc = ep->host->open ( ep ) ) != 0 ) {
313 DBGC ( usb, "USB %s %s could not open: %s\n", usb->name,
314 usb_endpoint_name ( ep ), strerror ( rc ) );
315 goto err_open;
316 }
317 ep->open = 1;
318
319 DBGC2 ( usb, "USB %s %s opened with MTU %zd, burst %d, interval %d\n",
320 usb->name, usb_endpoint_name ( ep ), ep->mtu, ep->burst,
321 ep->interval );
322 return 0;
323
324 ep->open = 0;
325 ep->host->close ( ep );
326 err_open:
327 usb->ep[idx] = NULL;
328 err_already:
329 if ( ep->max )
330 usb_flush ( ep );
331 return rc;
332}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
#define DBGC2(...)
Definition compiler.h:547
#define DBGC(...)
Definition compiler.h:530
#define EALREADY
Connection already in progress.
Definition errno.h:367
#define USB_ENDPOINT_IDX(address)
Construct endpoint index from endpoint address.
Definition usb.h:538
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition list.h:46
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
struct usb_host_operations * op
Host controller operations set.
Definition usb.h:982
char name[32]
Name.
Definition usb.h:735
struct usb_port * port
USB port.
Definition usb.h:737
int(* open)(struct usb_endpoint *ep)
Open endpoint.
Definition usb.h:460
void(* close)(struct usb_endpoint *ep)
Close endpoint.
Definition usb.h:465
size_t mtu
Maximum transfer size.
Definition usb.h:422
unsigned int max
Maximum fill level.
Definition usb.h:450
struct list_head halted
List of halted endpoints.
Definition usb.h:434
unsigned int burst
Maximum burst size.
Definition usb.h:424
int open
Endpoint is open.
Definition usb.h:429
struct usb_endpoint_host_operations * host
Host controller operations.
Definition usb.h:437
unsigned int interval
Interval (in microframes).
Definition usb.h:426
struct usb_endpoint_host_operations endpoint
Endpoint operations.
Definition usb.h:1043
struct usb_bus * bus
USB bus.
Definition usb.h:855
struct usb_hub * hub
USB hub.
Definition usb.h:825
void usb_flush(struct usb_endpoint *ep)
Discard endpoint recycled buffer list.
Definition usb.c:719
const char * usb_endpoint_name(struct usb_endpoint *ep)
Get USB endpoint name (for debugging).
Definition usb.c:220

References usb_endpoint::address, usb_endpoint::burst, usb_hub::bus, usb_endpoint_host_operations::close, DBGC, DBGC2, EALREADY, usb_host_operations::endpoint, usb_device::ep, usb_endpoint::halted, usb_endpoint::host, usb_port::hub, INIT_LIST_HEAD, usb_endpoint::interval, usb_endpoint::max, usb_endpoint::mtu, usb_device::name, NULL, usb_bus::op, usb_endpoint::open, usb_endpoint_host_operations::open, usb_device::port, rc, strerror(), usb_endpoint::usb, USB_ENDPOINT_IDX, usb_endpoint_name(), and usb_flush().

Referenced by efi_usb_open(), hub_open(), register_usb(), usbblk_open(), usbhid_open(), and usbnet_open().

◆ usb_endpoint_clear_tt()

int usb_endpoint_clear_tt ( struct usb_endpoint * ep)
static

Clear transaction translator (if applicable).

Parameters
epUSB endpoint
Return values
rcReturn status code

Definition at line 340 of file usb.c.

340 {
341 struct usb_device *usb = ep->usb;
342 struct usb_port *tt;
343 int rc;
344
345 /* Do nothing if this is a periodic endpoint */
347 return 0;
348
349 /* Do nothing if this endpoint is not behind a transaction translator */
351 if ( ! tt )
352 return 0;
353
354 /* Clear transaction translator buffer */
355 if ( ( rc = tt->hub->driver->clear_tt ( tt->hub, tt, ep ) ) != 0 ) {
356 DBGC ( usb, "USB %s %s could not clear transaction translator: "
357 "%s\n", usb->name, usb_endpoint_name ( ep ),
358 strerror ( rc ) );
359 return rc;
360 }
361
362 return 0;
363}
#define USB_ENDPOINT_ATTR_PERIODIC
Endpoint periodic type.
Definition usb.h:284
unsigned int attributes
Attributes.
Definition usb.h:420
int(* clear_tt)(struct usb_hub *hub, struct usb_port *port, struct usb_endpoint *ep)
Clear transaction translator buffer.
Definition usb.h:936
struct usb_hub_driver_operations * driver
Driver operations.
Definition usb.h:869
A USB port.
Definition usb.h:823
struct usb_device * usb
Currently attached device (if in use).
Definition usb.h:845
struct usb_port * usb_transaction_translator(struct usb_device *usb)
Get USB transaction translator.
Definition usb.c:2371

References usb_endpoint::attributes, usb_hub_driver_operations::clear_tt, DBGC, usb_hub::driver, usb_device::ep, usb_port::hub, usb_device::name, rc, strerror(), usb_endpoint::usb, usb_port::usb, USB_ENDPOINT_ATTR_PERIODIC, usb_endpoint_name(), and usb_transaction_translator().

Referenced by usb_endpoint_clear_halt(), and usb_endpoint_close().

◆ usb_endpoint_clear_halt()

int usb_endpoint_clear_halt ( struct usb_endpoint * ep)

Clear endpoint halt (if applicable).

Parameters
epUSB endpoint
Return values
rcReturn status code

Definition at line 371 of file usb.c.

371 {
372 struct usb_device *usb = ep->usb;
373 unsigned int type;
374 int rc;
375
376 /* Clear transaction translator, if applicable */
377 if ( ( rc = usb_endpoint_clear_tt ( ep ) ) != 0 )
378 return rc;
379
380 /* Clear endpoint halt (if applicable) */
382 if ( ( type != USB_ENDPOINT_ATTR_CONTROL ) &&
385 ep->address ) ) != 0 ) ) {
386 DBGC ( usb, "USB %s %s could not clear endpoint halt: %s\n",
387 usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
388 return rc;
389 }
390
391 return 0;
392}
#define USB_RECIP_ENDPOINT
Request recipient is an endpoint.
Definition usb.h:119
static int usb_clear_feature(struct usb_device *usb, unsigned int type, unsigned int feature, unsigned int index)
Clear feature.
Definition usb.h:1137
#define USB_ENDPOINT_HALT
Endpoint halt feature.
Definition usb.h:157
#define USB_ENDPOINT_ATTR_CONTROL
Control endpoint transfer type.
Definition usb.h:287
static int usb_endpoint_clear_tt(struct usb_endpoint *ep)
Clear transaction translator (if applicable).
Definition usb.c:340

References usb_endpoint::address, usb_endpoint::attributes, DBGC, usb_device::ep, usb_device::name, rc, strerror(), type, usb_endpoint::usb, usb_clear_feature(), USB_ENDPOINT_ATTR_CONTROL, USB_ENDPOINT_ATTR_TYPE_MASK, usb_endpoint_clear_tt(), USB_ENDPOINT_HALT, usb_endpoint_name(), and USB_RECIP_ENDPOINT.

Referenced by usb_endpoint_reset(), and usbblk_open().

◆ usb_endpoint_close()

void usb_endpoint_close ( struct usb_endpoint * ep)

Close USB endpoint.

Parameters
epUSB endpoint

Definition at line 399 of file usb.c.

399 {
400 struct usb_device *usb = ep->usb;
401 unsigned int idx = USB_ENDPOINT_IDX ( ep->address );
402
403 /* Sanity checks */
404 assert ( usb->ep[idx] == ep );
405
406 /* Close endpoint */
407 ep->open = 0;
408 ep->host->close ( ep );
409 assert ( ep->fill == 0 );
410
411 /* Remove from endpoint list */
412 usb->ep[idx] = NULL;
413 list_del ( &ep->halted );
414
415 /* Discard any recycled buffers, if applicable */
416 if ( ep->max )
417 usb_flush ( ep );
418
419 /* Clear transaction translator, if applicable */
421}
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:61
#define list_del(list)
Delete an entry from a list.
Definition list.h:120
unsigned int fill
Buffer fill level.
Definition usb.h:431

References usb_endpoint::address, assert, usb_endpoint_host_operations::close, usb_device::ep, usb_endpoint::fill, usb_endpoint::halted, usb_endpoint::host, list_del, usb_endpoint::max, NULL, usb_endpoint::open, usb_endpoint::usb, usb_endpoint_clear_tt(), USB_ENDPOINT_IDX, and usb_flush().

Referenced by efi_usb_close(), efi_usb_open(), hub_close(), hub_open(), register_usb(), unregister_usb(), usbblk_close(), usbblk_open(), usbhid_close(), usbhid_open(), usbnet_close(), and usbnet_open().

◆ usb_endpoint_reset()

int usb_endpoint_reset ( struct usb_endpoint * ep)
static

Reset USB endpoint.

Parameters
epUSB endpoint
Return values
rcReturn status code

Definition at line 429 of file usb.c.

429 {
430 struct usb_device *usb = ep->usb;
431 int rc;
432
433 /* Sanity check */
434 assert ( ! list_empty ( &ep->halted ) );
435
436 /* Clear device halt, if applicable */
437 if ( ( rc = usb_endpoint_clear_halt ( ep ) ) != 0 )
438 return rc;
439
440 /* Reset endpoint */
441 if ( ( rc = ep->host->reset ( ep ) ) != 0 ) {
442 DBGC ( usb, "USB %s %s could not reset: %s\n",
443 usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
444 return rc;
445 }
446
447 /* Remove from list of halted endpoints */
448 list_del ( &ep->halted );
450
451 DBGC ( usb, "USB %s %s reset\n",
452 usb->name, usb_endpoint_name ( ep ) );
453 return 0;
454}
#define list_empty(list)
Test whether a list is empty.
Definition list.h:137
int(* reset)(struct usb_endpoint *ep)
Reset endpoint.
Definition usb.h:472
int usb_endpoint_clear_halt(struct usb_endpoint *ep)
Clear endpoint halt (if applicable).
Definition usb.c:371

References assert, DBGC, usb_device::ep, usb_endpoint::halted, usb_endpoint::host, INIT_LIST_HEAD, list_del, list_empty, usb_device::name, rc, usb_endpoint_host_operations::reset, strerror(), usb_endpoint::usb, usb_endpoint_clear_halt(), and usb_endpoint_name().

Referenced by usb_control(), usb_message(), usb_step(), and usb_stream().

◆ usb_endpoint_mtu()

int usb_endpoint_mtu ( struct usb_endpoint * ep,
size_t mtu )
static

Update endpoint MTU.

Parameters
epUSB endpoint
mtuNew MTU
Return values
rcReturn status code

Definition at line 463 of file usb.c.

463 {
464 struct usb_device *usb = ep->usb;
465 int rc;
466
467 /* Update MTU */
468 ep->mtu = mtu;
469 if ( ( rc = ep->host->mtu ( ep ) ) != 0 ) {
470 DBGC ( usb, "USB %s %s could not update MTU: %s\n",
471 usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
472 return rc;
473 }
474
475 return 0;
476}
int(* mtu)(struct usb_endpoint *ep)
Update MTU.
Definition usb.h:478

References DBGC, usb_device::ep, usb_endpoint::host, mtu, usb_endpoint::mtu, usb_endpoint_host_operations::mtu, usb_device::name, rc, strerror(), usb_endpoint::usb, and usb_endpoint_name().

Referenced by register_usb().

◆ usb_message()

int usb_message ( struct usb_endpoint * ep,
unsigned int request,
unsigned int value,
unsigned int index,
struct io_buffer * iobuf )

Enqueue USB message transfer.

Parameters
epUSB endpoint
requestRequest
valueValue parameter
indexIndex parameter
iobufI/O buffer
Return values
rcReturn status code

The I/O buffer must have sufficient headroom to contain a setup packet.

Definition at line 491 of file usb.c.

493 {
494 struct usb_device *usb = ep->usb;
495 struct usb_port *port = usb->port;
496 struct usb_setup_packet *packet;
497 size_t len = iob_len ( iobuf );
498 int rc;
499
500 /* Sanity check */
501 assert ( iob_headroom ( iobuf ) >= sizeof ( *packet ) );
502
503 /* Fail immediately if device has been unplugged */
504 if ( port->disconnected )
505 return -ENODEV;
506
507 /* Reset endpoint if required */
508 if ( ( ! list_empty ( &ep->halted ) ) &&
509 ( ( rc = usb_endpoint_reset ( ep ) ) != 0 ) )
510 return rc;
511
512 /* Zero input data buffer (if applicable) */
513 if ( request & USB_DIR_IN )
514 memset ( iobuf->data, 0, len );
515
516 /* Construct setup packet */
517 packet = iob_push ( iobuf, sizeof ( *packet ) );
518 packet->request = cpu_to_le16 ( request );
519 packet->value = cpu_to_le16 ( value );
520 packet->index = cpu_to_le16 ( index );
521 packet->len = cpu_to_le16 ( len );
522
523 /* Enqueue message transfer */
524 if ( ( rc = ep->host->message ( ep, iobuf ) ) != 0 ) {
525 DBGC ( usb, "USB %s %s could not enqueue message transfer: "
526 "%s\n", usb->name, usb_endpoint_name ( ep ),
527 strerror ( rc ) );
528 return rc;
529 }
530
531 /* Increment fill level */
532 ep->fill++;
533
534 return 0;
535}
u8 port
Port number.
Definition CIB_PRM.h:3
pseudo_bit_t value[0x00020]
Definition arbel.h:2
ring len
Length.
Definition dwmac.h:226
#define ENODEV
No such device.
Definition errno.h:553
u8 request[0]
List of IEs requested.
Definition ieee80211.h:2
#define cpu_to_le16(value)
Definition byteswap.h:107
void * memset(void *dest, int character, size_t len) __nonnull
static size_t iob_headroom(struct io_buffer *iobuf)
Calculate available space at start of an I/O buffer.
Definition iobuf.h:230
#define iob_push(iobuf, len)
Definition iobuf.h:149
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition iobuf.h:220
void * data
Start of data.
Definition iobuf.h:113
int(* message)(struct usb_endpoint *ep, struct io_buffer *iobuf)
Enqueue message transfer.
Definition usb.h:485
A USB setup data packet.
Definition usb.h:83
uint16_t request
Request.
Definition usb.h:85
uint16_t value
Value parameter.
Definition usb.h:87
uint16_t index
Index parameter.
Definition usb.h:89
uint16_t len
Length of data stage.
Definition usb.h:91
static int usb_endpoint_reset(struct usb_endpoint *ep)
Reset USB endpoint.
Definition usb.c:429

References assert, cpu_to_le16, io_buffer::data, DBGC, ENODEV, usb_device::ep, usb_endpoint::fill, usb_endpoint::halted, usb_endpoint::host, index, usb_setup_packet::index, iob_headroom(), iob_len(), iob_push, len, usb_setup_packet::len, list_empty, memset(), usb_endpoint_host_operations::message, usb_device::name, port, usb_device::port, rc, request, usb_setup_packet::request, strerror(), usb_endpoint::usb, usb_port::usb, USB_DIR_IN, usb_endpoint_name(), usb_endpoint_reset(), usb_setup_packet::value, and value.

Referenced by usb_control().

◆ usb_stream()

int usb_stream ( struct usb_endpoint * ep,
struct io_buffer * iobuf,
int terminate )

Enqueue USB stream transfer.

Parameters
epUSB endpoint
iobufI/O buffer
terminateTerminate using a short packet
Return values
rcReturn status code

Definition at line 545 of file usb.c.

546 {
547 struct usb_device *usb = ep->usb;
548 struct usb_port *port = usb->port;
549 int zlp;
550 int rc;
551
552 /* Fail immediately if device has been unplugged */
553 if ( port->disconnected )
554 return -ENODEV;
555
556 /* Reset endpoint if required */
557 if ( ( ! list_empty ( &ep->halted ) ) &&
558 ( ( rc = usb_endpoint_reset ( ep ) ) != 0 ) )
559 return rc;
560
561 /* Append a zero-length packet if necessary */
562 zlp = terminate;
563 if ( iob_len ( iobuf ) & ( ep->mtu - 1 ) )
564 zlp = 0;
565
566 /* Enqueue stream transfer */
567 if ( ( rc = ep->host->stream ( ep, iobuf, zlp ) ) != 0 ) {
568 DBGC ( usb, "USB %s %s could not enqueue stream transfer: %s\n",
569 usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
570 return rc;
571 }
572
573 /* Increment fill level */
574 ep->fill++;
575
576 return 0;
577}
int(* stream)(struct usb_endpoint *ep, struct io_buffer *iobuf, int zlp)
Enqueue stream transfer.
Definition usb.h:494

References DBGC, ENODEV, usb_device::ep, usb_endpoint::fill, usb_endpoint::halted, usb_endpoint::host, iob_len(), list_empty, usb_endpoint::mtu, usb_device::name, port, usb_device::port, rc, usb_endpoint_host_operations::stream, strerror(), usb_endpoint::usb, usb_port::usb, usb_endpoint_name(), and usb_endpoint_reset().

Referenced by acm_out_transmit(), axge_out_transmit(), dm96xx_out_transmit(), ecm_out_transmit(), efi_usb_sync_transfer(), imux_tx(), iphone_out_transmit(), ncm_out_transmit(), smsc75xx_out_transmit(), smsc95xx_out_transmit(), usb_refill_limit(), usbblk_out_command(), and usbblk_out_data().

◆ usb_complete_err()

void usb_complete_err ( struct usb_endpoint * ep,
struct io_buffer * iobuf,
int rc )

Complete transfer (possibly with error).

Parameters
epUSB endpoint
iobufI/O buffer
rcCompletion status code

Definition at line 586 of file usb.c.

587 {
588 struct usb_device *usb = ep->usb;
589
590 /* Decrement fill level */
591 assert ( ep->fill > 0 );
592 ep->fill--;
593
594 /* Schedule reset, if applicable */
595 if ( ( rc != 0 ) && ep->open ) {
596 DBGC ( usb, "USB %s %s completion failed: %s\n",
597 usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
598 list_del ( &ep->halted );
600 }
601
602 /* Report completion */
603 ep->driver->complete ( ep, iobuf, rc );
604}
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition list.h:94
void(* complete)(struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
Complete transfer.
Definition usb.h:506
struct usb_endpoint_driver_operations * driver
Driver operations.
Definition usb.h:441
static struct list_head usb_halted
List of halted endpoints.
Definition usb.c:51

References assert, usb_endpoint_driver_operations::complete, DBGC, usb_endpoint::driver, usb_device::ep, usb_endpoint::fill, usb_endpoint::halted, list_add_tail, list_del, usb_device::name, usb_endpoint::open, rc, strerror(), usb_endpoint::usb, usb_endpoint_name(), and usb_halted.

Referenced by ehci_endpoint_close(), ehci_endpoint_poll(), uhci_endpoint_close(), uhci_endpoint_poll(), usb_complete(), usbio_bulk_in_poll(), usbio_bulk_out_poll(), usbio_control_poll(), usbio_endpoint_close(), xhci_endpoint_close(), and xhci_transfer().

◆ usb_prefill()

int usb_prefill ( struct usb_endpoint * ep)

Prefill endpoint recycled buffer list.

Parameters
epUSB endpoint
Return values
rcReturn status code

Definition at line 619 of file usb.c.

619 {
620 struct io_buffer *iobuf;
621 size_t reserve = ep->reserve;
622 size_t len = ( ep->len ? ep->len : ep->mtu );
623 unsigned int fill;
624 int rc;
625
626 /* Sanity checks */
627 assert ( ep->fill == 0 );
628 assert ( ep->max > 0 );
629 assert ( list_empty ( &ep->recycled ) );
630
631 /* Fill recycled buffer list */
632 for ( fill = 0 ; fill < ep->max ; fill++ ) {
633
634 /* Allocate I/O buffer */
635 iobuf = alloc_iob ( reserve + len );
636 if ( ! iobuf ) {
637 rc = -ENOMEM;
638 goto err_alloc;
639 }
640 iob_reserve ( iobuf, reserve );
641
642 /* Add to recycled buffer list */
643 list_add_tail ( &iobuf->list, &ep->recycled );
644 }
645
646 return 0;
647
648 err_alloc:
649 usb_flush ( ep );
650 return rc;
651}
static int fill
Definition string.h:209
#define ENOMEM
Not enough space.
Definition errno.h:578
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition iobuf.c:131
#define iob_reserve(iobuf, len)
Definition iobuf.h:132
A persistent I/O buffer.
Definition iobuf.h:98
struct list_head list
List of which this buffer is a member.
Definition iobuf.h:105
size_t len
Refill buffer payload length.
Definition usb.h:448
size_t reserve
Refill buffer reserved header length.
Definition usb.h:446
struct list_head recycled
Recycled I/O buffer list.
Definition usb.h:444

References alloc_iob(), assert, ENOMEM, fill, usb_endpoint::fill, iob_reserve, len, usb_endpoint::len, io_buffer::list, list_add_tail, list_empty, usb_endpoint::max, usb_endpoint::mtu, rc, usb_endpoint::recycled, usb_endpoint::reserve, and usb_flush().

Referenced by efi_usb_async_start(), and ncm_in_prefill().

◆ usb_refill_limit()

int usb_refill_limit ( struct usb_endpoint * ep,
unsigned int max )

Refill endpoint up to specified limit.

Parameters
epUSB endpoint
maxFill limit
Return values
rcReturn status code

Definition at line 660 of file usb.c.

660 {
661 struct io_buffer *iobuf;
662 size_t reserve = ep->reserve;
663 size_t len = ( ep->len ? ep->len : ep->mtu );
664 int rc;
665
666 /* Sanity checks */
667 assert ( ep->open );
668 assert ( ep->max > 0 );
669
670 /* Refill endpoint */
671 if ( max > ep->max )
672 max = ep->max;
673 while ( ep->fill < max ) {
674
675 /* Get or allocate buffer */
676 if ( list_empty ( &ep->recycled ) ) {
677 /* Recycled buffer list is empty; allocate new buffer */
678 iobuf = alloc_iob ( reserve + len );
679 if ( ! iobuf )
680 return -ENOMEM;
681 iob_reserve ( iobuf, reserve );
682 } else {
683 /* Get buffer from recycled buffer list */
684 iobuf = list_first_entry ( &ep->recycled,
685 struct io_buffer, list );
686 assert ( iobuf != NULL );
687 list_del ( &iobuf->list );
688 }
689
690 /* Reset buffer to maximum size */
691 assert ( iob_len ( iobuf ) <= len );
692 iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );
693
694 /* Enqueue buffer */
695 if ( ( rc = usb_stream ( ep, iobuf, 0 ) ) != 0 ) {
696 list_add ( &iobuf->list, &ep->recycled );
697 return rc;
698 }
699 }
700
701 return 0;
702}
#define max(x, y)
Definition ath.h:41
#define iob_put(iobuf, len)
Definition iobuf.h:185
#define list_first_entry(list, type, member)
Get the container of the first entry in a list.
Definition list.h:334
#define list_add(new, head)
Add a new entry to the head of a list.
Definition list.h:70
int usb_stream(struct usb_endpoint *ep, struct io_buffer *iobuf, int terminate)
Enqueue USB stream transfer.
Definition usb.c:545

References alloc_iob(), assert, ENOMEM, usb_endpoint::fill, iob_len(), iob_put, iob_reserve, len, usb_endpoint::len, io_buffer::list, list_add, list_del, list_empty, list_first_entry, max, usb_endpoint::max, usb_endpoint::mtu, NULL, usb_endpoint::open, rc, usb_endpoint::recycled, usb_endpoint::reserve, and usb_stream().

Referenced by usb_refill(), and usbblk_in_refill().

◆ usb_refill()

int usb_refill ( struct usb_endpoint * ep)

Refill endpoint.

Parameters
epUSB endpoint
Return values
rcReturn status code

Definition at line 710 of file usb.c.

710 {
711 return usb_refill_limit ( ep, ep->max );
712}
int usb_refill_limit(struct usb_endpoint *ep, unsigned int max)
Refill endpoint up to specified limit.
Definition usb.c:660

References usb_endpoint::max, and usb_refill_limit().

Referenced by efi_usb_timer(), hub_refill(), usbhid_open(), usbhid_refill(), usbkbd_iskey(), usbnet_open(), and usbnet_refill().

◆ usb_flush()

void usb_flush ( struct usb_endpoint * ep)

Discard endpoint recycled buffer list.

Parameters
epUSB endpoint

Definition at line 719 of file usb.c.

719 {
720 struct io_buffer *iobuf;
721 struct io_buffer *tmp;
722
723 /* Sanity checks */
724 assert ( ! ep->open );
725 assert ( ep->max > 0 );
726
727 /* Free all I/O buffers */
728 list_for_each_entry_safe ( iobuf, tmp, &ep->recycled, list ) {
729 list_del ( &iobuf->list );
730 free_iob ( iobuf );
731 }
732}
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition iobuf.c:153
unsigned long tmp
Definition linux_pci.h:65
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
Definition list.h:459

References assert, free_iob(), io_buffer::list, list_del, list_for_each_entry_safe, usb_endpoint::max, usb_endpoint::open, usb_endpoint::recycled, and tmp.

Referenced by ncm_open(), usb_endpoint_close(), usb_endpoint_open(), and usb_prefill().

◆ usb_control_complete()

void usb_control_complete ( struct usb_endpoint * ep,
struct io_buffer * iobuf,
int rc )
static

Complete USB control transfer.

Parameters
epUSB endpoint
iobufI/O buffer
rcCompletion status code

Definition at line 754 of file usb.c.

755 {
756 struct usb_device *usb = ep->usb;
757 struct usb_control_pseudo_header *pshdr;
758
759 /* Record completion status in buffer */
760 pshdr = iob_push ( iobuf, sizeof ( *pshdr ) );
761 pshdr->rc = rc;
762
763 /* Add to list of completed I/O buffers */
764 list_add_tail ( &iobuf->list, &usb->complete );
765}
USB control transfer pseudo-header.
Definition usb.c:742
int rc
Completion status.
Definition usb.c:744
struct list_head complete
Completed control transfers.
Definition usb.h:760

References usb_device::complete, usb_device::ep, iob_push, io_buffer::list, list_add_tail, rc, usb_control_pseudo_header::rc, and usb_endpoint::usb.

◆ usb_control()

int usb_control ( struct usb_device * usb,
unsigned int request,
unsigned int value,
unsigned int index,
void * data,
size_t len )

Issue USB control transaction.

Parameters
usbUSB device
requestRequest
valueValue parameter
indexIndex parameter
dataData buffer (if any)
lenLength of data
Return values
rcReturn status code

Definition at line 783 of file usb.c.

785 {
786 struct usb_bus *bus = usb->port->hub->bus;
787 struct usb_endpoint *ep = &usb->control;
788 struct io_buffer *iobuf;
789 struct io_buffer *cmplt;
790 union {
791 struct usb_setup_packet setup;
792 struct usb_control_pseudo_header pshdr;
793 } *headroom;
794 struct usb_control_pseudo_header *pshdr;
795 unsigned int i;
796 int rc;
797
798 /* Allocate I/O buffer */
799 iobuf = alloc_iob ( sizeof ( *headroom ) + len );
800 if ( ! iobuf ) {
801 rc = -ENOMEM;
802 goto err_alloc;
803 }
804 iob_reserve ( iobuf, sizeof ( *headroom ) );
805 iob_put ( iobuf, len );
806 if ( request & USB_DIR_IN ) {
807 memset ( data, 0, len );
808 } else {
809 memcpy ( iobuf->data, data, len );
810 }
811
812 /* Enqueue message */
813 if ( ( rc = usb_message ( ep, request, value, index, iobuf ) ) != 0 )
814 goto err_message;
815
816 /* Wait for completion */
817 for ( i = 0 ; i < USB_CONTROL_MAX_WAIT_MS ; i++ ) {
818
819 /* Poll bus */
820 usb_poll ( bus );
821
822 /* Check for completion */
823 while ( ( cmplt = list_first_entry ( &usb->complete,
824 struct io_buffer,
825 list ) ) ) {
826
827 /* Remove from completion list */
828 list_del ( &cmplt->list );
829
830 /* Extract and strip completion status */
831 pshdr = cmplt->data;
832 iob_pull ( cmplt, sizeof ( *pshdr ) );
833 rc = pshdr->rc;
834
835 /* Discard stale completions */
836 if ( cmplt != iobuf ) {
837 DBGC ( usb, "USB %s stale control completion: "
838 "%s\n", usb->name, strerror ( rc ) );
839 DBGC_HDA ( usb, 0, cmplt->data,
840 iob_len ( cmplt ) );
841 free_iob ( cmplt );
842 continue;
843 }
844
845 /* Fail immediately if completion was in error */
846 if ( rc != 0 ) {
847 DBGC ( usb, "USB %s control %04x:%04x:%04x "
848 "failed: %s\n", usb->name, request,
849 value, index, strerror ( rc ) );
850 free_iob ( cmplt );
851 usb_endpoint_reset ( ep );
852 return rc;
853 }
854
855 /* Copy completion to data buffer, if applicable */
856 assert ( iob_len ( cmplt ) <= len );
857 if ( request & USB_DIR_IN )
858 memcpy ( data, cmplt->data, iob_len ( cmplt ) );
859 free_iob ( cmplt );
860 return 0;
861 }
862
863 /* Delay */
864 mdelay ( 1 );
865 }
866
867 DBGC ( usb, "USB %s timed out waiting for control %04x:%04x:%04x\n",
868 usb->name, request, value, index );
869 return -ETIMEDOUT;
870
871 err_message:
872 free_iob ( iobuf );
873 err_alloc:
874 return rc;
875}
uint8_t bus
Bus.
Definition edd.h:1
uint8_t data[48]
Additional event data.
Definition ena.h:11
#define DBGC_HDA(...)
Definition compiler.h:531
#define ETIMEDOUT
Connection timed out.
Definition errno.h:713
#define USB_CONTROL_MAX_WAIT_MS
Maximum time to wait for a control transaction to complete.
Definition usb.h:1352
static void usb_poll(struct usb_bus *bus)
Poll USB bus.
Definition usb.h:1082
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define iob_pull(iobuf, len)
Definition iobuf.h:167
A USB bus.
Definition usb.h:976
struct usb_endpoint control
Control endpoint.
Definition usb.h:758
A USB endpoint.
Definition usb.h:414
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition timer.c:79
int usb_message(struct usb_endpoint *ep, unsigned int request, unsigned int value, unsigned int index, struct io_buffer *iobuf)
Enqueue USB message transfer.
Definition usb.c:491

References alloc_iob(), assert, bus, usb_hub::bus, usb_device::complete, usb_device::control, data, io_buffer::data, DBGC, DBGC_HDA, ENOMEM, ETIMEDOUT, free_iob(), usb_port::hub, index, iob_len(), iob_pull, iob_put, iob_reserve, len, io_buffer::list, list_del, list_first_entry, mdelay(), memcpy(), memset(), usb_device::name, usb_device::port, rc, usb_control_pseudo_header::rc, request, strerror(), usb_endpoint::usb, USB_CONTROL_MAX_WAIT_MS, USB_DIR_IN, usb_endpoint_reset(), usb_message(), usb_poll(), and value.

Referenced by axge_read_register(), axge_write_register(), cdc_get_encapsulated_response(), cdc_send_encapsulated_command(), dm96xx_read_registers(), dm96xx_write_register(), dm96xx_write_registers(), ecm_open(), efi_usb_control_transfer(), iphone_check_link(), iphone_probe(), ncm_open(), ncm_probe(), smscusb_get_statistics(), smscusb_raw_readl(), smscusb_raw_writel(), usb_clear_feature(), usb_get_descriptor(), usb_get_status(), usb_hub_clear_tt_buffer(), usb_hub_set_hub_depth(), usb_set_address(), usb_set_configuration(), usb_set_feature(), usb_set_interface(), usbblk_open(), usbhid_set_idle(), usbhid_set_protocol(), and usbhid_set_report().

◆ usb_get_default_language()

unsigned int usb_get_default_language ( struct usb_device * usb)
static

Get default language ID.

Parameters
usbUSB device
Return values
languageLanguage ID

Definition at line 883 of file usb.c.

883 {
884 struct {
886 uint16_t language[1];
887 } __attribute__ (( packed )) desc;
888 unsigned int language;
889 int rc;
890
891 /* Get descriptor */
892 if ( ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, 0, 0,
893 &desc.header, sizeof ( desc ) ) ) !=0){
894 DBGC ( usb, "USB %s has no default language: %s\n",
895 usb->name, strerror ( rc ) );
896 return USB_LANG_ENGLISH;
897 }
898
899 /* Use first language ID */
900 language = le16_to_cpu ( desc.language[0] );
901 DBGC2 ( usb, "USB %s default language %#04x\n", usb->name, language );
902 return language;
903}
unsigned short uint16_t
Definition stdint.h:11
#define __attribute__(x)
Definition compiler.h:10
#define USB_STRING_DESCRIPTOR
A USB string descriptor.
Definition usb.h:239
#define USB_LANG_ENGLISH
Language ID for English.
Definition usb.h:242
static int usb_get_descriptor(struct usb_device *usb, unsigned int type, unsigned int desc, unsigned int index, unsigned int language, struct usb_descriptor_header *data, size_t len)
Get USB descriptor.
Definition usb.h:1187
A USB descriptor header.
Definition usb.h:173

References __attribute__, DBGC, DBGC2, desc, header, le16_to_cpu, usb_device::name, rc, strerror(), usb_get_descriptor(), USB_LANG_ENGLISH, and USB_STRING_DESCRIPTOR.

Referenced by usb_get_string_descriptor().

◆ usb_get_string_descriptor()

int usb_get_string_descriptor ( struct usb_device * usb,
unsigned int index,
unsigned int language,
char * buf,
size_t len )

Get USB string descriptor.

Parameters
usbUSB device
indexString index
languageLanguage ID, or 0 to use default
bufData buffer
lenLength of buffer
Return values
lenString length (excluding NUL), or negative error

Definition at line 915 of file usb.c.

916 {
917 struct {
919 uint16_t character[len];
920 } __attribute__ (( packed )) *desc;
921 unsigned int actual;
922 unsigned int i;
923 int rc;
924
925 /* Use default language ID, if applicable */
926 if ( ( language == 0 ) && ( index != 0 ) ) {
927 if ( ! usb->language )
928 usb->language = usb_get_default_language ( usb );
929 language = usb->language;
930 }
931
932 /* Allocate buffer for string */
933 desc = malloc ( sizeof ( *desc ) );
934 if ( ! desc ) {
935 rc = -ENOMEM;
936 goto err_alloc;
937 }
938
939 /* Get descriptor */
941 language, &desc->header,
942 sizeof ( *desc ) ) ) != 0 )
943 goto err_get_descriptor;
944
945 /* Calculate string length */
946 if ( desc->header.len < sizeof ( desc->header ) ) {
947 rc = -EINVAL;
948 goto err_len;
949 }
950 actual = ( ( desc->header.len - sizeof ( desc->header ) ) /
951 sizeof ( desc->character[0] ) );
952
953 /* Copy to buffer */
954 memset ( buf, 0, len );
955 for ( i = 0 ; ( ( i < actual ) && ( i < len ) ) ; i++ )
956 buf[i] = le16_to_cpu ( desc->character[i] );
957
958 /* Free buffer */
959 free ( desc );
960
961 return actual;
962
963 err_len:
964 err_get_descriptor:
965 free ( desc );
966 err_alloc:
967 return rc;
968}
#define EINVAL
Invalid argument.
Definition errno.h:472
void * malloc(size_t size)
Allocate memory.
Definition malloc.c:677
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
unsigned int language
Default language ID (if known).
Definition usb.h:763
static unsigned int usb_get_default_language(struct usb_device *usb)
Get default language ID.
Definition usb.c:883

References __attribute__, desc, EINVAL, ENOMEM, free, header, index, usb_device::language, le16_to_cpu, len, malloc(), memset(), rc, usb_get_default_language(), usb_get_descriptor(), and USB_STRING_DESCRIPTOR.

Referenced by ecm_fetch_mac(), and usb_settings_fetch().

◆ usb_config_descriptor()

int usb_config_descriptor ( struct usb_device * usb,
unsigned int index,
struct usb_configuration_descriptor ** config )
static

Get USB configuration descriptor.

Parameters
usbUSB device
indexConfiguration index
Return values
configConfiguration descriptor
rcReturn status code

The configuration descriptor is dynamically allocated and must eventually be freed by the caller.

Definition at line 989 of file usb.c.

990 {
991 struct usb_configuration_descriptor partial;
992 size_t len;
993 int rc;
994
995 /* Read first part of configuration descriptor to get size */
996 if ( ( rc = usb_get_config_descriptor ( usb, index, &partial,
997 sizeof ( partial ) ) ) != 0 ) {
998 DBGC ( usb, "USB %s could not get configuration descriptor %d: "
999 "%s\n", usb->name, index, strerror ( rc ) );
1000 goto err_get_partial;
1001 }
1002 len = le16_to_cpu ( partial.len );
1003 if ( len < sizeof ( partial ) ) {
1004 DBGC ( usb, "USB %s underlength configuraton descriptor %d\n",
1005 usb->name, index );
1006 rc = -EINVAL;
1007 goto err_partial_len;
1008 }
1009
1010 /* Allocate buffer for whole configuration descriptor */
1011 *config = malloc ( len );
1012 if ( ! *config ) {
1013 rc = -ENOMEM;
1014 goto err_alloc_config;
1015 }
1016
1017 /* Read whole configuration descriptor */
1018 if ( ( rc = usb_get_config_descriptor ( usb, index, *config,
1019 len ) ) != 0 ) {
1020 DBGC ( usb, "USB %s could not get configuration descriptor %d: "
1021 "%s\n", usb->name, index, strerror ( rc ) );
1022 goto err_get_config_descriptor;
1023 }
1024 if ( (*config)->len != partial.len ) {
1025 DBGC ( usb, "USB %s bad configuration descriptor %d length\n",
1026 usb->name, index );
1027 rc = -EINVAL;
1028 goto err_config_len;
1029 }
1030
1031 return 0;
1032
1033 err_config_len:
1034 err_get_config_descriptor:
1035 free ( *config );
1036 err_alloc_config:
1037 err_partial_len:
1038 err_get_partial:
1039 return rc;
1040}
static int usb_get_config_descriptor(struct usb_device *usb, unsigned int index, struct usb_configuration_descriptor *data, size_t len)
Get USB configuration descriptor.
Definition usb.h:1236
A USB configuration descriptor.
Definition usb.h:210
uint8_t config
Configuration value.
Definition usb.h:218

References usb_configuration_descriptor::config, DBGC, EINVAL, ENOMEM, free, index, le16_to_cpu, len, usb_configuration_descriptor::len, malloc(), usb_device::name, rc, strerror(), and usb_get_config_descriptor().

Referenced by usb_autoconfigure().

◆ usb_describe()

int usb_describe ( struct usb_device * usb,
struct usb_configuration_descriptor * config,
unsigned int first,
uint8_t * interfaces,
struct usb_function_descriptor * desc )
static

Describe USB function.

Parameters
usbUSB device
configConfiguration descriptor
firstFirst interface number
interfacesInterface list to fill in
descFunction descriptor to fill in
Return values
rcReturn status code

Definition at line 1052 of file usb.c.

1055 {
1056 struct usb_interface_association_descriptor *association;
1058 struct cdc_union_descriptor *cdc_union;
1059 unsigned int i;
1060
1061 /* Fill in vendor and product ID */
1062 memset ( desc, 0, sizeof ( *desc ) );
1063 desc->vendor = le16_to_cpu ( usb->device.vendor );
1064 desc->product = le16_to_cpu ( usb->device.product );
1065
1066 /* First, look for an interface association descriptor */
1067 association = usb_interface_association_descriptor ( config, first );
1068 if ( association ) {
1069
1070 /* Sanity check */
1071 assert ( association->first == first );
1072 if ( ( first + association->count ) > config->interfaces ) {
1073 DBGC ( usb, "USB %s has invalid association [%d-%d)\n",
1074 usb->name, first, ( first + association->count));
1075 return -ERANGE;
1076 }
1077
1078 /* Describe function */
1079 memcpy ( &desc->class.class, &association->class,
1080 sizeof ( desc->class.class ) );
1081 desc->count = association->count;
1082 for ( i = 0 ; i < association->count ; i++ )
1083 interfaces[i] = ( first + i );
1084 return 0;
1085 }
1086
1087 /* Next, look for an interface descriptor */
1088 interface = usb_interface_descriptor ( config, first, 0 );
1089 if ( ! interface ) {
1090 DBGC ( usb, "USB %s has no descriptor for interface %d\n",
1091 usb->name, first );
1092 return -ENOENT;
1093 }
1094
1095 /* Describe function */
1096 memcpy ( &desc->class.class, &interface->class,
1097 sizeof ( desc->class.class ) );
1098 desc->count = 1;
1099 interfaces[0] = first;
1100
1101 /* Look for a CDC union descriptor, if applicable */
1102 if ( ( desc->class.class.class == USB_CLASS_CDC ) &&
1103 ( cdc_union = cdc_union_descriptor ( config, interface ) ) ) {
1104
1105 /* Determine interface count */
1106 desc->count = ( ( cdc_union->header.len -
1107 offsetof ( typeof ( *cdc_union ),
1108 interface[0] ) ) /
1109 sizeof ( cdc_union->interface[0] ) );
1110 if ( desc->count > config->interfaces ) {
1111 DBGC ( usb, "USB %s has invalid union functional "
1112 "descriptor with %d interfaces\n",
1113 usb->name, desc->count );
1114 return -ERANGE;
1115 }
1116
1117 /* Describe function */
1118 for ( i = 0 ; i < desc->count ; i++ ) {
1119 if ( cdc_union->interface[i] >= config->interfaces ) {
1120 DBGC ( usb, "USB %s has invalid union "
1121 "functional descriptor covering "
1122 "interface %d\n", usb->name,
1123 cdc_union->interface[i] );
1124 return -ERANGE;
1125 }
1126 interfaces[i] = cdc_union->interface[i];
1127 }
1128
1129 return 0;
1130 }
1131
1132 return 0;
1133}
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition acpi.c:48
#define USB_CLASS_CDC
Class code for communications devices.
Definition cdc.h:16
#define ERANGE
Result too large.
Definition errno.h:683
static const char * interfaces[2]
Definition smc9000.c:52
#define offsetof(type, field)
Get offset of a field within a structure.
Definition stddef.h:25
Union functional descriptor.
Definition cdc.h:29
uint8_t interface[1]
Interfaces (variable-length).
Definition cdc.h:35
struct usb_descriptor_header header
Descriptor header.
Definition cdc.h:31
uint8_t interfaces
Number of interfaces.
Definition usb.h:216
uint16_t product
Product ID.
Definition usb.h:193
uint16_t vendor
Vendor ID.
Definition usb.h:191
struct usb_device_descriptor device
Device descriptor.
Definition usb.h:745
struct usb_class class
Association class.
Definition usb.h:337
uint8_t count
Interface count.
Definition usb.h:335
uint8_t first
First interface number.
Definition usb.h:333
static struct usb_interface_association_descriptor * usb_interface_association_descriptor(struct usb_configuration_descriptor *config, unsigned int first)
Locate USB interface association descriptor.
Definition usb.c:120

References assert, usb_interface_association_descriptor::class, usb_interface_association_descriptor::count, DBGC, desc, usb_device::device, ENOENT, ERANGE, first, usb_interface_association_descriptor::first, cdc_union_descriptor::header, cdc_union_descriptor::interface, interfaces, usb_configuration_descriptor::interfaces, le16_to_cpu, usb_descriptor_header::len, memcpy(), memset(), usb_device::name, offsetof, usb_device_descriptor::product, typeof(), USB_CLASS_CDC, usb_interface_association_descriptor(), and usb_device_descriptor::vendor.

Referenced by usb_probe_all(), and usb_score().

◆ usb_used()

int usb_used ( struct usb_device * usb,
unsigned int count,
uint8_t * interface,
uint8_t * used )
static

Update list of used interface.

Parameters
usbUSB device
countNumber of interfaces
interfaceList of interfaces
usedList of already-used interfaces
Return values
rcReturn status code

Definition at line 1144 of file usb.c.

1145 {
1146 unsigned int i;
1147
1148 for ( i = 0 ; i < count ; i++ ) {
1149 if ( used[interface[i]] ) {
1150 DBGC ( usb, "USB %s interface %d already in use\n",
1151 usb->name, interface[i] );
1152 return -EINVAL;
1153 }
1154 used[interface[i]] = 1;
1155 }
1156 return 0;
1157}
static unsigned int count
Number of entries.
Definition dwmac.h:220

References count, DBGC, EINVAL, and usb_device::name.

Referenced by usb_probe_all(), and usb_score().

◆ usb_find_driver()

struct usb_driver * usb_find_driver ( struct usb_function_descriptor * desc,
struct usb_device_id ** id )

Find USB device driver.

Parameters
descFunction descriptor
Return values
idUSB device ID, or NULL
driverUSB device driver, or NULL

Definition at line 1166 of file usb.c.

1167 {
1168 struct usb_driver *driver;
1169 unsigned int i;
1170
1171 /* Look for a matching driver */
1172 for_each_table_entry ( driver, USB_DRIVERS ) {
1173 for ( i = 0 ; i < driver->id_count ; i++ ) {
1174
1175 /* Ignore non-matching driver class */
1176 if ( ( driver->class.class.scalar ^ desc->class.scalar )
1177 & driver->class.mask.scalar )
1178 continue;
1179
1180 /* Look for a matching ID */
1181 *id = &driver->ids[i];
1182 if ( ( ( (*id)->vendor == desc->vendor ) ||
1183 ( (*id)->vendor == USB_ANY_ID ) ) &&
1184 ( ( (*id)->product == desc->product ) ||
1185 ( (*id)->product == USB_ANY_ID ) ) )
1186 return driver;
1187 }
1188 }
1189
1190 /* Not found */
1191 *id = NULL;
1192 return NULL;
1193}
#define USB_ANY_ID
Match-anything ID.
Definition usb.h:1395
#define USB_DRIVERS
USB driver table.
Definition usb.h:1460
union usb_class_descriptor class
Class.
Definition usb.h:1400
union usb_class_descriptor mask
Class mask.
Definition usb.h:1402
A USB driver.
Definition usb.h:1429
struct usb_class_id class
Class ID.
Definition usb.h:1435
unsigned int id_count
Number of entries in ID table.
Definition usb.h:1433
struct usb_device_id * ids
USB ID table.
Definition usb.h:1431
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition tables.h:386
uint32_t scalar
Scalar value.
Definition usb.h:658

References usb_class_id::class, usb_driver::class, desc, for_each_table_entry, usb_driver::id_count, usb_driver::ids, usb_class_id::mask, NULL, usb_class_descriptor::scalar, USB_ANY_ID, and USB_DRIVERS.

Referenced by usb_probe(), usb_score(), and usbio_supported().

◆ usb_score()

int usb_score ( struct usb_device * usb,
struct usb_configuration_descriptor * config )
static

Get USB device configuration score.

Parameters
usbUSB device
configConfiguration descriptor
Return values
scoreDevice configuration score, or negative error

Definition at line 1202 of file usb.c.

1203 {
1204 uint8_t used[config->interfaces];
1205 uint8_t interface[config->interfaces];
1207 struct usb_driver *driver;
1208 struct usb_device_id *id;
1209 unsigned int first;
1210 unsigned int score = 0;
1211 int rc;
1212
1213 /* Identify each function in turn */
1214 memset ( used, 0, sizeof ( used ) );
1215 for ( first = 0 ; first < config->interfaces ; first++ ) {
1216
1217 /* Skip interfaces already used */
1218 if ( used[first] )
1219 continue;
1220
1221 /* Describe function */
1222 if ( ( rc = usb_describe ( usb, config, first, interface,
1223 &desc ) ) != 0 )
1224 return rc;
1225
1226 /* Update used interfaces */
1227 if ( ( rc = usb_used ( usb, desc.count, interface,
1228 used ) ) != 0 )
1229 return rc;
1230
1231 /* Look for a driver for this function */
1232 driver = usb_find_driver ( &desc, &id );
1233 if ( driver )
1234 score += driver->score;
1235 }
1236
1237 return score;
1238}
uint8_t id
Request identifier.
Definition ena.h:1
A USB device ID.
Definition usb.h:1371
unsigned int score
Driver score.
Definition usb.h:1441
A USB function descriptor.
Definition usb.h:667
static int usb_used(struct usb_device *usb, unsigned int count, uint8_t *interface, uint8_t *used)
Update list of used interface.
Definition usb.c:1144
struct usb_driver * usb_find_driver(struct usb_function_descriptor *desc, struct usb_device_id **id)
Find USB device driver.
Definition usb.c:1166
static int usb_describe(struct usb_device *usb, struct usb_configuration_descriptor *config, unsigned int first, uint8_t *interfaces, struct usb_function_descriptor *desc)
Describe USB function.
Definition usb.c:1052

References desc, first, id, usb_configuration_descriptor::interfaces, memset(), rc, usb_driver::score, usb_describe(), usb_find_driver(), and usb_used().

Referenced by usb_autoconfigure().

◆ usb_probe()

int usb_probe ( struct usb_function * func,
struct usb_configuration_descriptor * config )
static

Probe USB device driver.

Parameters
funcUSB function
configConfiguration descriptor
Return values
rcReturn status code

Definition at line 1247 of file usb.c.

1248 {
1249 struct usb_device *usb = func->usb;
1250 struct usb_driver *driver;
1251 struct usb_device_id *id;
1252 int rc;
1253
1254 /* Identify driver */
1255 driver = usb_find_driver ( &func->desc, &id );
1256 if ( ! driver ) {
1257 DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d has no driver\n",
1258 func->name, func->desc.vendor, func->desc.product,
1259 func->desc.class.class.class,
1260 func->desc.class.class.subclass,
1261 func->desc.class.class.protocol );
1262 return -ENOENT;
1263 }
1264
1265 /* Record driver */
1266 func->driver = driver;
1267 func->id = id;
1268 func->dev.driver_name = id->name;
1269
1270 /* Probe driver */
1271 if ( ( rc = driver->probe ( func, config ) ) != 0 ) {
1272 DBGC ( usb, "USB %s failed to probe driver %s: %s\n",
1273 func->name, id->name, strerror ( rc ) );
1274 return rc;
1275 }
1276
1277 return 0;
1278}
const char * driver_name
Driver name.
Definition device.h:81
uint8_t protocol
Protocol code.
Definition usb.h:166
uint8_t class
Class code.
Definition usb.h:162
uint8_t subclass
Subclass code.
Definition usb.h:164
int(* probe)(struct usb_function *func, struct usb_configuration_descriptor *config)
Probe device.
Definition usb.h:1449
union usb_class_descriptor class
Class.
Definition usb.h:673
uint16_t product
Product ID.
Definition usb.h:671
uint16_t vendor
Vendor ID.
Definition usb.h:669
struct usb_device * usb
USB device.
Definition usb.h:688
struct usb_function_descriptor desc
Function descriptor.
Definition usb.h:690
struct usb_device_id * id
Driver device ID.
Definition usb.h:701
struct usb_driver * driver
Driver.
Definition usb.h:697
struct device dev
Generic device.
Definition usb.h:692
const char * name
Name.
Definition usb.h:686
struct usb_class class
Class.
Definition usb.h:656

References usb_class::class, usb_class_descriptor::class, usb_function_descriptor::class, DBGC, usb_function::desc, usb_function::dev, usb_function::driver, device::driver_name, ENOENT, id, usb_function::id, usb_function::name, usb_driver::probe, usb_function_descriptor::product, usb_class::protocol, rc, strerror(), usb_class::subclass, usb_function::usb, usb_find_driver(), and usb_function_descriptor::vendor.

Referenced by usb_probe_all().

◆ usb_remove()

void usb_remove ( struct usb_function * func)
static

Remove USB device driver.

Parameters
funcUSB function

Definition at line 1285 of file usb.c.

1285 {
1286
1287 /* Remove driver */
1288 func->driver->remove ( func );
1289}
void(* remove)(struct usb_function *func)
Remove device.
Definition usb.h:1456

References usb_function::driver, and usb_driver::remove.

Referenced by usb_probe_all(), and usb_remove_all().

◆ usb_probe_all()

void usb_probe_all ( struct usb_device * usb,
struct usb_configuration_descriptor * config )
static

Probe all USB device drivers.

Parameters
usbUSB device
configConfiguration descriptor

Definition at line 1298 of file usb.c.

1299 {
1300 struct usb_bus *bus = usb->port->hub->bus;
1301 struct usb_function *func;
1302 uint8_t used[config->interfaces];
1303 unsigned int first;
1304 unsigned int i;
1305 int rc;
1306
1307 /* Identify each function in turn */
1308 memset ( used, 0, sizeof ( used ) );
1309 for ( first = 0 ; first < config->interfaces ; first++ ) {
1310
1311 /* Skip interfaces already used */
1312 if ( used[first] )
1313 continue;
1314
1315 /* Allocate and initialise structure */
1316 func = zalloc ( sizeof ( *func ) +
1317 ( config->interfaces *
1318 sizeof ( func->interface[0] ) ) );
1319 if ( ! func )
1320 goto err_alloc;
1321 func->name = func->dev.name;
1322 func->usb = usb;
1323 func->dev.desc.bus_type = BUS_TYPE_USB;
1324 func->dev.desc.location =
1325 USB_BUSDEV ( bus->address, usb->address );
1326 func->dev.desc.vendor = le16_to_cpu ( usb->device.vendor );
1328 snprintf ( func->dev.name, sizeof ( func->dev.name ),
1329 "%s-%d.%d", usb->name, config->config, first );
1330 INIT_LIST_HEAD ( &func->dev.children );
1331 func->dev.parent = bus->dev;
1332 list_add_tail ( &func->list, &usb->functions );
1333
1334 /* Identify function */
1335 if ( ( rc = usb_describe ( usb, config, first, func->interface,
1336 &func->desc ) ) != 0 )
1337 goto err_describe;
1338 assert ( func->desc.count <= config->interfaces );
1339
1340 /* Mark interfaces as used */
1341 if ( ( rc = usb_used ( usb, func->desc.count, func->interface,
1342 used ) ) != 0 )
1343 goto err_used;
1344
1345 /* Probe device driver */
1346 if ( ( rc = usb_probe ( func, config ) ) != 0 )
1347 goto err_probe;
1348 DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d interfaces ",
1349 func->name, func->desc.vendor, func->desc.product,
1350 func->desc.class.class.class,
1351 func->desc.class.class.subclass,
1352 func->desc.class.class.protocol );
1353 for ( i = 0 ; i < func->desc.count ; i++ )
1354 DBGC ( usb, "%s%d", ( i ? "," : "" ),
1355 func->interface[i] );
1356 DBGC ( usb, " using driver %s\n", func->dev.driver_name );
1357
1358 /* Add to device hierarchy */
1359 list_add_tail ( &func->dev.siblings, &bus->dev->children );
1360
1361 continue;
1362
1363 list_del ( &func->dev.siblings );
1364 usb_remove ( func );
1365 err_probe:
1366 err_used:
1367 err_describe:
1368 list_del ( &func->list );
1369 free ( func );
1370 err_alloc:
1371 /* Continue registering other functions */
1372 continue;
1373 }
1374}
#define BUS_TYPE_USB
USB bus type.
Definition device.h:71
#define USB_BUSDEV(bus, dev)
Define a USB bus:device address.
Definition usb.h:64
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:718
unsigned int bus_type
Bus type.
Definition device.h:25
unsigned int device
Device ID.
Definition device.h:34
unsigned int vendor
Vendor ID.
Definition device.h:32
unsigned int location
Location.
Definition device.h:30
struct device_description desc
Device description.
Definition device.h:83
struct device * parent
Bus device.
Definition device.h:89
struct list_head children
Devices attached to this device.
Definition device.h:87
struct list_head siblings
Devices on the same bus.
Definition device.h:85
char name[40]
Name.
Definition device.h:79
struct list_head functions
List of functions.
Definition usb.h:747
unsigned int address
Device address, if assigned.
Definition usb.h:743
unsigned int count
Number of interfaces.
Definition usb.h:675
A USB function.
Definition usb.h:684
uint8_t interface[0]
List of interface numbers.
Definition usb.h:707
struct list_head list
List of functions within this USB device.
Definition usb.h:694
static int usb_probe(struct usb_function *func, struct usb_configuration_descriptor *config)
Probe USB device driver.
Definition usb.c:1247
static void usb_remove(struct usb_function *func)
Remove USB device driver.
Definition usb.c:1285

References usb_device::address, assert, bus, usb_hub::bus, device_description::bus_type, BUS_TYPE_USB, device::children, usb_class::class, usb_class_descriptor::class, usb_function_descriptor::class, usb_configuration_descriptor::config, usb_function_descriptor::count, DBGC, device::desc, usb_function::desc, usb_function::dev, device_description::device, usb_device::device, device::driver_name, first, free, usb_device::functions, usb_port::hub, INIT_LIST_HEAD, usb_function::interface, usb_configuration_descriptor::interfaces, le16_to_cpu, usb_function::list, list_add_tail, list_del, device_description::location, memset(), device::name, usb_device::name, usb_function::name, device::parent, usb_device::port, usb_device_descriptor::product, usb_function_descriptor::product, usb_class::protocol, rc, device::siblings, snprintf(), usb_class::subclass, usb_function::usb, USB_BUSDEV, usb_describe(), usb_probe(), usb_remove(), usb_used(), device_description::vendor, usb_device_descriptor::vendor, usb_function_descriptor::vendor, and zalloc().

Referenced by usb_autoconfigure().

◆ usb_remove_all()

void usb_remove_all ( struct usb_device * usb)
static

Remove all device drivers.

Parameters
usbUSB device

Definition at line 1381 of file usb.c.

1381 {
1382 struct usb_function *func;
1383 struct usb_function *tmp;
1384
1385 /* Remove all functions */
1387
1388 /* Remove device driver */
1389 usb_remove ( func );
1390
1391 /* Remove from device hierarchy */
1392 assert ( list_empty ( &func->dev.children ) );
1393 list_del ( &func->dev.siblings );
1394
1395 /* Remove from list of functions */
1396 list_del ( &func->list );
1397
1398 /* Free function */
1399 free ( func );
1400 }
1401}

References assert, device::children, usb_function::dev, free, usb_device::functions, usb_function::list, list_del, list_empty, list_for_each_entry_safe, device::siblings, tmp, usb_function::usb, and usb_remove().

Referenced by usb_autoconfigure(), and usb_deconfigure().

◆ usb_deconfigure()

void usb_deconfigure ( struct usb_device * usb)
static

Clear USB device configuration.

Parameters
usbUSB device

Definition at line 1408 of file usb.c.

1408 {
1409 unsigned int i;
1410
1411 /* Remove device drivers */
1412 usb_remove_all ( usb );
1413
1414 /* Sanity checks */
1415 for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++){
1416 if ( i != USB_ENDPOINT_IDX ( USB_EP0_ADDRESS ) )
1417 assert ( usb->ep[i] == NULL );
1418 }
1419
1420 /* Clear device configuration */
1421 usb_set_configuration ( usb, 0 );
1422}
static int usb_set_configuration(struct usb_device *usb, unsigned int index)
Set USB configuration.
Definition usb.h:1252
#define USB_EP0_ADDRESS
Control endpoint address.
Definition usb.h:511
static void usb_remove_all(struct usb_device *usb)
Remove all device drivers.
Definition usb.c:1381

References assert, usb_device::ep, NULL, usb_function::usb, USB_ENDPOINT_IDX, USB_EP0_ADDRESS, usb_remove_all(), and usb_set_configuration().

Referenced by register_usb(), and unregister_usb().

◆ usb_autoconfigure()

int usb_autoconfigure ( struct usb_device * usb)
static

Choose our preferred USB device configuration.

Parameters
usbUSB device
Return values
rcReturn status code

Definition at line 1430 of file usb.c.

1430 {
1432 unsigned int preferred = 0;
1433 unsigned int index;
1434 int score;
1435 int best = 0;
1436 int rc;
1437
1438 /* Calculate driver score for each configuration index */
1439 for ( index = 0 ; index < usb->device.configurations ; index++ ) {
1440
1441 /* Read configuration descriptor */
1442 if ( ( rc = usb_config_descriptor ( usb, index,
1443 &config ) ) != 0 )
1444 goto err_config;
1445
1446 /* Get score for this configuration */
1447 score = usb_score ( usb, config );
1448 if ( score < 0 ) {
1449 rc = score;
1450 goto err_score;
1451 }
1452 DBGC2 ( usb, "USB %s configuration %d score %d\n",
1453 usb->name, config->config, score );
1454
1455 /* Record as preferred configuration, if applicable */
1456 if ( score > best ) {
1457 best = score;
1458 preferred = index;
1459 }
1460
1461 /* Free configuration descriptor */
1462 free ( config );
1463 config = NULL;
1464 }
1465
1466 /* Read preferred configuration descriptor */
1467 if ( ( rc = usb_config_descriptor ( usb, preferred, &config ) ) != 0 )
1468 goto err_preferred;
1469
1470 /* Set configuration */
1471 if ( ( rc = usb_set_configuration ( usb, config->config ) ) != 0){
1472 DBGC ( usb, "USB %s could not set configuration %d: %s\n",
1473 usb->name, config->config, strerror ( rc ) );
1474 goto err_set_configuration;
1475 }
1476
1477 /* Probe USB device drivers */
1478 usb_probe_all ( usb, config );
1479
1480 /* Free configuration descriptor */
1481 free ( config );
1482
1483 return 0;
1484
1485 usb_remove_all ( usb );
1486 usb_set_configuration ( usb, 0 );
1487 err_set_configuration:
1488 free ( config );
1489 err_preferred:
1490 return rc;
1491
1492 err_score:
1493 free ( config );
1494 err_config:
1495 return rc;
1496}
uint8_t configurations
Number of possible configurations.
Definition usb.h:203
static int usb_score(struct usb_device *usb, struct usb_configuration_descriptor *config)
Get USB device configuration score.
Definition usb.c:1202
static int usb_config_descriptor(struct usb_device *usb, unsigned int index, struct usb_configuration_descriptor **config)
Get USB configuration descriptor.
Definition usb.c:989
static void usb_probe_all(struct usb_device *usb, struct usb_configuration_descriptor *config)
Probe all USB device drivers.
Definition usb.c:1298

References usb_configuration_descriptor::config, usb_device_descriptor::configurations, DBGC, DBGC2, usb_device::device, free, index, usb_device::name, NULL, rc, strerror(), usb_config_descriptor(), usb_probe_all(), usb_remove_all(), usb_score(), and usb_set_configuration().

Referenced by register_usb().

◆ alloc_usb()

struct usb_device * alloc_usb ( struct usb_port * port)
static

Allocate USB device.

Parameters
portUSB port
Return values
usbUSB device, or NULL on allocation failure

Definition at line 1511 of file usb.c.

1511 {
1512 struct usb_hub *hub = port->hub;
1513 struct usb_bus *bus = hub->bus;
1514 struct usb_device *usb;
1515
1516 /* Allocate and initialise structure */
1517 usb = zalloc ( sizeof ( *usb ) );
1518 if ( ! usb )
1519 return NULL;
1520 snprintf ( usb->name, sizeof ( usb->name ), "%s%c%d", hub->name,
1521 ( hub->usb ? '.' : '-' ), port->address );
1522 usb->port = port;
1523 INIT_LIST_HEAD ( &usb->functions );
1524 usb->host = &bus->op->device;
1526 INIT_LIST_HEAD ( &usb->complete );
1527
1528 return usb;
1529}
static void usb_endpoint_init(struct usb_endpoint *ep, struct usb_device *usb, struct usb_endpoint_driver_operations *driver)
Initialise USB endpoint.
Definition usb.h:550
struct usb_hub * hub
Root hub.
Definition usb.h:1005
struct usb_device_host_operations * host
Host controller operations.
Definition usb.h:750
A USB hub.
Definition usb.h:851
const char * name
Name.
Definition usb.h:853
struct usb_device * usb
Underlying USB device, if any.
Definition usb.h:857
static struct usb_endpoint_driver_operations usb_control_operations
USB control endpoint driver operations.
Definition usb.c:768

References bus, usb_hub::bus, usb_device::complete, usb_device::control, usb_device::functions, usb_device::host, usb_bus::hub, INIT_LIST_HEAD, usb_device::name, usb_hub::name, NULL, port, usb_device::port, snprintf(), usb_hub::usb, usb_control_operations, usb_endpoint_init(), and zalloc().

Referenced by usb_attached().

◆ register_usb()

int register_usb ( struct usb_device * usb)
static

Register USB device.

Parameters
usbUSB device
Return values
rcReturn status code

Definition at line 1537 of file usb.c.

1537 {
1538 struct usb_port *port = usb->port;
1539 struct usb_hub *hub = port->hub;
1540 struct usb_bus *bus = hub->bus;
1541 unsigned int protocol;
1542 size_t mtu;
1543 int rc;
1544
1545 /* Add to port */
1546 if ( port->usb != NULL ) {
1547 DBGC ( hub, "USB hub %s port %d is already registered to %s\n",
1548 hub->name, port->address, port->usb->name );
1549 rc = -EALREADY;
1550 goto err_already;
1551 }
1552 port->usb = usb;
1553
1554 /* Add to bus device list */
1555 list_add_tail ( &usb->list, &bus->devices );
1556
1557 /* Enable device */
1558 if ( ( rc = hub->driver->enable ( hub, port ) ) != 0 ) {
1559 DBGC ( hub, "USB hub %s port %d could not enable: %s\n",
1560 hub->name, port->address, strerror ( rc ) );
1561 goto err_enable;
1562 }
1563
1564 /* Allow recovery interval since port may have been reset */
1566
1567 /* Get device speed */
1568 if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
1569 DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
1570 hub->name, port->address, strerror ( rc ) );
1571 goto err_speed;
1572 }
1573 usb->speed = port->speed;
1574 DBGC2 ( usb, "USB %s attached as %s-speed device\n",
1575 usb->name, usb_speed_name ( usb->speed ) );
1576
1577 /* Open device */
1578 if ( ( rc = usb->host->open ( usb ) ) != 0 ) {
1579 DBGC ( usb, "USB %s could not open: %s\n",
1580 usb->name, strerror ( rc ) );
1581 goto err_open;
1582 }
1583
1584 /* Describe control endpoint */
1585 mtu = USB_EP0_DEFAULT_MTU ( usb->speed );
1589
1590 /* Open control endpoint */
1591 if ( ( rc = usb_endpoint_open ( &usb->control ) ) != 0 )
1592 goto err_open_control;
1593 assert ( usb_endpoint ( usb, USB_EP0_ADDRESS ) == &usb->control );
1594
1595 /* Assign device address */
1596 if ( ( rc = usb->host->address ( usb ) ) != 0 ) {
1597 DBGC ( usb, "USB %s could not set address: %s\n",
1598 usb->name, strerror ( rc ) );
1599 goto err_address;
1600 }
1601 DBGC2 ( usb, "USB %s assigned address %d\n", usb->name, usb->address );
1602
1603 /* Allow recovery interval after Set Address command */
1605
1606 /* Read first part of device descriptor to get EP0 MTU */
1607 if ( ( rc = usb_get_mtu ( usb, &usb->device ) ) != 0 ) {
1608 DBGC ( usb, "USB %s could not get MTU: %s\n",
1609 usb->name, strerror ( rc ) );
1610 goto err_get_mtu;
1611 }
1612
1613 /* Calculate EP0 MTU */
1615 mtu = ( ( protocol < USB_PROTO_3_0 ) ?
1616 usb->device.mtu : ( 1 << usb->device.mtu ) );
1617 DBGC2 ( usb, "USB %s has control MTU %zd (guessed %zd)\n",
1618 usb->name, mtu, usb->control.mtu );
1619
1620 /* Update MTU */
1621 if ( ( rc = usb_endpoint_mtu ( &usb->control, mtu ) ) != 0 )
1622 goto err_mtu;
1623
1624 /* Read whole device descriptor */
1625 if ( ( rc = usb_get_device_descriptor ( usb, &usb->device ) ) != 0 ) {
1626 DBGC ( usb, "USB %s could not get device descriptor: %s\n",
1627 usb->name, strerror ( rc ) );
1628 goto err_get_device_descriptor;
1629 }
1630 DBGC ( usb, "USB %s addr %d %04x:%04x class %d:%d:%d (v%s, %s-speed, "
1631 "MTU %zd)\n", usb->name, usb->address,
1632 le16_to_cpu ( usb->device.vendor ),
1633 le16_to_cpu ( usb->device.product ), usb->device.class.class,
1635 usb_bcd ( le16_to_cpu ( usb->device.protocol ) ),
1636 usb_speed_name ( usb->speed ), usb->control.mtu );
1637
1638 /* Configure device */
1639 if ( ( rc = usb_autoconfigure ( usb ) ) != 0 )
1640 goto err_autoconfigure;
1641
1642 return 0;
1643
1644 usb_deconfigure ( usb );
1645 err_autoconfigure:
1646 err_get_device_descriptor:
1647 err_mtu:
1648 err_get_mtu:
1649 err_address:
1650 usb_endpoint_close ( &usb->control );
1651 err_open_control:
1652 usb->host->close ( usb );
1653 err_open:
1654 err_speed:
1655 /* Leave port enabled on failure, to avoid an endless loop of
1656 * failed device registrations.
1657 */
1658 err_enable:
1659 list_del ( &usb->list );
1660 port->usb = NULL;
1661 err_already:
1662 return rc;
1663}
@ USB_PROTO_3_0
USB 3.0.
Definition usb.h:25
#define USB_EP0_BURST
Control endpoint maximum burst size.
Definition usb.h:526
#define USB_EP0_INTERVAL
Control endpoint interval.
Definition usb.h:529
static int usb_get_mtu(struct usb_device *usb, struct usb_device_descriptor *data)
Get first part of USB device descriptor (up to and including MTU).
Definition usb.h:1204
#define USB_RESET_RECOVER_DELAY_MS
Reset recovery time.
Definition usb.h:1345
#define USB_SET_ADDRESS_RECOVER_DELAY_MS
Set address recovery time.
Definition usb.h:1359
#define USB_EP0_DEFAULT_MTU(speed)
Calculate default MTU based on device speed.
Definition usb.h:521
static int usb_get_device_descriptor(struct usb_device *usb, struct usb_device_descriptor *data)
Get USB device descriptor.
Definition usb.h:1220
#define USB_EP0_ATTRIBUTES
Control endpoint attributes.
Definition usb.h:514
uint16_t protocol
Protocol ID.
Definition stp.h:7
uint16_t protocol
USB specification release number in BCD.
Definition usb.h:185
struct usb_class class
Device class.
Definition usb.h:187
uint8_t mtu
Maximum packet size for endpoint zero.
Definition usb.h:189
void(* close)(struct usb_device *usb)
Close device.
Definition usb.h:778
int(* address)(struct usb_device *usb)
Assign device address.
Definition usb.h:784
int(* open)(struct usb_device *usb)
Open device.
Definition usb.h:773
struct list_head list
List of devices on this bus.
Definition usb.h:741
int(* enable)(struct usb_hub *hub, struct usb_port *port)
Enable port.
Definition usb.h:914
int(* speed)(struct usb_hub *hub, struct usb_port *port)
Update port speed.
Definition usb.h:928
int usb_endpoint_open(struct usb_endpoint *ep)
Open USB endpoint.
Definition usb.c:293
static const char * usb_bcd(uint16_t bcd)
Transcribe USB BCD-coded value (for debugging).
Definition usb.c:96
void usb_endpoint_close(struct usb_endpoint *ep)
Close USB endpoint.
Definition usb.c:399
static int usb_endpoint_mtu(struct usb_endpoint *ep, size_t mtu)
Update endpoint MTU.
Definition usb.c:463
static int usb_autoconfigure(struct usb_device *usb)
Choose our preferred USB device configuration.
Definition usb.c:1430
static const char * usb_speed_name(unsigned int speed)
Get USB speed name (for debugging).
Definition usb.c:66
static void usb_deconfigure(struct usb_device *usb)
Clear USB device configuration.
Definition usb.c:1408

References usb_device::address, usb_device_host_operations::address, assert, bus, usb_hub::bus, usb_class::class, usb_device_descriptor::class, usb_device_host_operations::close, usb_device::control, DBGC, DBGC2, usb_device::device, usb_hub::driver, EALREADY, usb_hub_driver_operations::enable, usb_device::host, usb_bus::hub, le16_to_cpu, usb_device::list, list_add_tail, list_del, mdelay(), mtu, usb_device_descriptor::mtu, usb_endpoint::mtu, usb_device::name, usb_hub::name, NULL, usb_device_host_operations::open, port, usb_device::port, usb_device_descriptor::product, protocol, usb_class::protocol, usb_device_descriptor::protocol, rc, usb_device::speed, usb_hub_driver_operations::speed, strerror(), usb_class::subclass, usb_port::usb, usb_autoconfigure(), usb_bcd(), usb_deconfigure(), usb_endpoint_close(), usb_endpoint_describe(), usb_endpoint_mtu(), usb_endpoint_open(), USB_EP0_ADDRESS, USB_EP0_ATTRIBUTES, USB_EP0_BURST, USB_EP0_DEFAULT_MTU, USB_EP0_INTERVAL, usb_get_device_descriptor(), usb_get_mtu(), USB_PROTO_3_0, USB_RESET_RECOVER_DELAY_MS, USB_SET_ADDRESS_RECOVER_DELAY_MS, usb_speed_name(), and usb_device_descriptor::vendor.

Referenced by usb_attached().

◆ unregister_usb()

void unregister_usb ( struct usb_device * usb)
static

Unregister USB device.

Parameters
usbUSB device

Definition at line 1670 of file usb.c.

1670 {
1671 struct usb_port *port = usb->port;
1672 struct usb_hub *hub = port->hub;
1673 struct io_buffer *iobuf;
1674 struct io_buffer *tmp;
1675
1676 DBGC ( usb, "USB %s addr %d %04x:%04x class %d:%d:%d removed\n",
1677 usb->name, usb->address, le16_to_cpu ( usb->device.vendor ),
1678 le16_to_cpu ( usb->device.product ), usb->device.class.class,
1680
1681 /* Sanity checks */
1682 assert ( port->usb == usb );
1683
1684 /* Clear device configuration */
1685 usb_deconfigure ( usb );
1686
1687 /* Close control endpoint */
1688 usb_endpoint_close ( &usb->control );
1689
1690 /* Discard any stale control completions */
1691 list_for_each_entry_safe ( iobuf, tmp, &usb->complete, list ) {
1692 list_del ( &iobuf->list );
1693 free_iob ( iobuf );
1694 }
1695
1696 /* Close device */
1697 usb->host->close ( usb );
1698
1699 /* Disable port */
1700 hub->driver->disable ( hub, port );
1701
1702 /* Remove from bus device list */
1703 list_del ( &usb->list );
1704
1705 /* Remove from port */
1706 port->usb = NULL;
1707}
int(* disable)(struct usb_hub *hub, struct usb_port *port)
Disable port.
Definition usb.h:921

References usb_device::address, assert, usb_class::class, usb_device_descriptor::class, usb_device_host_operations::close, usb_device::complete, usb_device::control, DBGC, usb_device::device, usb_hub_driver_operations::disable, usb_hub::driver, free_iob(), usb_device::host, le16_to_cpu, io_buffer::list, usb_device::list, list_del, list_for_each_entry_safe, usb_device::name, NULL, port, usb_device::port, usb_device_descriptor::product, usb_class::protocol, usb_class::subclass, tmp, usb_port::usb, usb_deconfigure(), usb_endpoint_close(), and usb_device_descriptor::vendor.

Referenced by usb_attached(), and usb_detached().

◆ free_usb()

void free_usb ( struct usb_device * usb)
static

Free USB device.

Parameters
usbUSB device

Definition at line 1714 of file usb.c.

1714 {
1715 unsigned int i;
1716
1717 /* Sanity checks */
1718 for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++ )
1719 assert ( usb->ep[i] == NULL );
1720 assert ( list_empty ( &usb->functions ) );
1721 assert ( list_empty ( &usb->complete ) );
1722
1723 /* Free device */
1724 free ( usb );
1725}

References assert, usb_device::complete, usb_device::ep, free, usb_device::functions, list_empty, and NULL.

Referenced by usb_attached(), and usb_detached().

◆ find_usb()

struct usb_device * find_usb ( struct usb_bus * bus,
unsigned int address )

Find USB device by address.

Parameters
busUSB bus
addressDevice address
Return values
usbUSB device, or NULL if not found

Definition at line 1734 of file usb.c.

1734 {
1735 struct usb_device *usb;
1736
1737 /* Search for a matching non-zero address */
1738 list_for_each_entry ( usb, &bus->devices, list ) {
1739 if ( address && ( usb->address == address ) )
1740 return usb;
1741 }
1742
1743 return NULL;
1744}
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition list.h:432

References address, usb_device::address, bus, usb_device::list, list_for_each_entry, and NULL.

Referenced by usb_find_next(), and usb_settings_fetch().

◆ usb_attached()

int usb_attached ( struct usb_port * port)
static

Handle newly attached USB device.

Parameters
portUSB port
Return values
rcReturn status code

Definition at line 1759 of file usb.c.

1759 {
1760 struct usb_device *usb;
1761 int rc;
1762
1763 /* Mark port as attached */
1764 port->attached = 1;
1765
1766 /* Sanity checks */
1767 assert ( port->usb == NULL );
1768
1769 /* Allocate USB device */
1770 usb = alloc_usb ( port );
1771 if ( ! usb ) {
1772 rc = -ENOMEM;
1773 goto err_alloc;
1774 }
1775
1776 /* Register USB device */
1777 if ( ( rc = register_usb ( usb ) ) != 0 )
1778 goto err_register;
1779
1780 return 0;
1781
1782 unregister_usb ( usb );
1783 err_register:
1784 free_usb ( usb );
1785 err_alloc:
1786 return rc;
1787}
static void unregister_usb(struct usb_device *usb)
Unregister USB device.
Definition usb.c:1670
static void free_usb(struct usb_device *usb)
Free USB device.
Definition usb.c:1714
static int register_usb(struct usb_device *usb)
Register USB device.
Definition usb.c:1537
static struct usb_device * alloc_usb(struct usb_port *port)
Allocate USB device.
Definition usb.c:1511

References alloc_usb(), assert, ENOMEM, free_usb(), NULL, port, rc, register_usb(), and unregister_usb().

Referenced by usb_hotplugged().

◆ usb_detached()

void usb_detached ( struct usb_port * port)
static

Handle newly detached USB device.

Parameters
portUSB port

Definition at line 1794 of file usb.c.

1794 {
1795 struct usb_device *usb = port->usb;
1796
1797 /* Mark port as detached */
1798 port->attached = 0;
1799
1800 /* Do nothing if we have no USB device */
1801 if ( ! usb )
1802 return;
1803
1804 /* Unregister USB device */
1805 unregister_usb ( usb );
1806
1807 /* Free USB device */
1808 free_usb ( usb );
1809}

References free_usb(), port, and unregister_usb().

Referenced by unregister_usb_hub(), and usb_hotplugged().

◆ usb_hotplugged()

int usb_hotplugged ( struct usb_port * port)
static

Handle newly attached or detached USB device.

Parameters
portUSB port
Return values
rcReturn status code

Definition at line 1817 of file usb.c.

1817 {
1818 struct usb_hub *hub = port->hub;
1819 int rc;
1820
1821 /* Get current port speed */
1822 if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
1823 DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
1824 hub->name, port->address, strerror ( rc ) );
1825 /* Treat as a disconnection */
1826 port->disconnected = 1;
1827 port->speed = USB_SPEED_NONE;
1828 }
1829
1830 /* Detach device, if applicable */
1831 if ( port->attached && ( port->disconnected || ! port->speed ) )
1832 usb_detached ( port );
1833
1834 /* Clear any recorded disconnections */
1835 port->disconnected = 0;
1836
1837 /* Attach device, if applicable */
1838 if ( port->speed && ( ! port->attached ) &&
1839 ( ( rc = usb_attached ( port ) ) != 0 ) )
1840 return rc;
1841
1842 return 0;
1843}
static void usb_detached(struct usb_port *port)
Handle newly detached USB device.
Definition usb.c:1794
static int usb_attached(struct usb_port *port)
Handle newly attached USB device.
Definition usb.c:1759

References DBGC, usb_hub::driver, usb_hub::name, port, rc, usb_hub_driver_operations::speed, strerror(), usb_attached(), usb_detached(), and USB_SPEED_NONE.

Referenced by usb_hotplug().

◆ usb_port_changed()

void usb_port_changed ( struct usb_port * port)

Report port status change.

Parameters
portUSB port

Definition at line 1857 of file usb.c.

1857 {
1858
1859 /* Record hub port status change */
1860 list_del ( &port->changed );
1861 list_add_tail ( &port->changed, &usb_changed );
1862}
static struct list_head usb_changed
List of changed ports.
Definition usb.c:48

References list_add_tail, list_del, port, and usb_changed.

Referenced by ehci_root_poll(), hub_complete(), register_usb_hub(), uhci_root_poll(), and xhci_port_status().

◆ usb_hotplug()

void usb_hotplug ( void )
static

Handle newly attached or detached USB device.

Definition at line 1868 of file usb.c.

1868 {
1869 struct usb_port *port;
1870
1871 /* Handle any changed ports, allowing for the fact that the
1872 * port list may change as we perform hotplug actions.
1873 */
1874 while ( ! list_empty ( &usb_changed ) ) {
1875
1876 /* Get first changed port */
1878 changed );
1879 assert ( port != NULL );
1880
1881 /* Remove from list of changed ports */
1882 list_del ( &port->changed );
1883 INIT_LIST_HEAD ( &port->changed );
1884
1885 /* Perform appropriate hotplug action */
1886 usb_hotplugged ( port );
1887 }
1888}
struct list_head changed
List of changed ports.
Definition usb.h:847
static int usb_hotplugged(struct usb_port *port)
Handle newly attached or detached USB device.
Definition usb.c:1817

References assert, usb_port::changed, INIT_LIST_HEAD, list_del, list_empty, list_first_entry, NULL, port, usb_changed, and usb_hotplugged().

Referenced by register_usb_bus(), and usb_step().

◆ usb_step()

void usb_step ( struct process *process __unused)
static

USB process.

Parameters
processUSB process

Definition at line 1895 of file usb.c.

1895 {
1896 struct usb_bus *bus;
1897 struct usb_endpoint *ep;
1898
1899 /* Poll all buses */
1901 usb_poll ( bus );
1902
1903 /* Attempt to reset first halted endpoint in list, if any. We
1904 * do not attempt to process the complete list, since this
1905 * would require extra code to allow for the facts that the
1906 * halted endpoint list may change as we do so, and that
1907 * resetting an endpoint may fail.
1908 */
1909 if ( ( ep = list_first_entry ( &usb_halted, struct usb_endpoint,
1910 halted ) ) != NULL )
1911 usb_endpoint_reset ( ep );
1912
1913 /* Handle any changed ports */
1914 usb_hotplug();
1915}
#define for_each_usb_bus(bus)
Iterate over all USB buses.
Definition usb.h:1087
static void usb_hotplug(void)
Handle newly attached or detached USB device.
Definition usb.c:1868

References __unused, bus, for_each_usb_bus, usb_endpoint::halted, list_first_entry, NULL, usb_endpoint_reset(), usb_halted, usb_hotplug(), and usb_poll().

Referenced by PERMANENT_PROCESS().

◆ PERMANENT_PROCESS()

PERMANENT_PROCESS ( usb_process ,
usb_step  )

USB process.

References usb_step().

◆ alloc_usb_hub()

struct usb_hub * alloc_usb_hub ( struct usb_bus * bus,
struct usb_device * usb,
unsigned int ports,
struct usb_hub_driver_operations * driver )

Allocate USB hub.

Parameters
busUSB bus
usbUnderlying USB device, if any
portsNumber of ports
driverHub driver operations
Return values
hubUSB hub, or NULL on allocation failure

Definition at line 1936 of file usb.c.

1938 {
1939 struct usb_hub *hub;
1940 struct usb_port *port;
1941 unsigned int i;
1942
1943 /* Allocate and initialise structure */
1944 hub = zalloc ( sizeof ( *hub ) + ( ports * sizeof ( hub->port[0] ) ) );
1945 if ( ! hub )
1946 return NULL;
1947 hub->name = ( usb ? usb->name : bus->name );
1948 hub->bus = bus;
1949 hub->usb = usb;
1950 if ( usb )
1952 hub->ports = ports;
1953 hub->driver = driver;
1954 hub->host = &bus->op->hub;
1955
1956 /* Initialise port list */
1957 for ( i = 1 ; i <= hub->ports ; i++ ) {
1958 port = usb_port ( hub, i );
1959 port->hub = hub;
1960 port->address = i;
1961 if ( usb )
1962 port->protocol = usb->port->protocol;
1963 INIT_LIST_HEAD ( &port->changed );
1964 }
1965
1966 return hub;
1967}
static struct usb_port * usb_port(struct usb_hub *hub, unsigned int address)
Get USB port.
Definition usb.h:970
unsigned int protocol
Hub protocol.
Definition usb.h:859
unsigned int ports
Number of ports.
Definition usb.h:861
struct usb_port port[0]
Port list.
Definition usb.h:877
struct usb_hub_host_operations * host
Host controller operations.
Definition usb.h:867
unsigned int protocol
Port protocol.
Definition usb.h:829

References bus, usb_hub::bus, usb_hub::driver, usb_hub::host, usb_port::hub, INIT_LIST_HEAD, usb_device::name, usb_hub::name, NULL, port, usb_device::port, usb_hub::port, usb_hub::ports, usb_hub::protocol, usb_port::protocol, usb_hub::usb, usb_port::usb, usb_port(), and zalloc().

Referenced by alloc_usb_bus(), and hub_probe().

◆ register_usb_hub()

int register_usb_hub ( struct usb_hub * hub)

Register USB hub.

Parameters
hubUSB hub
Return values
rcReturn status code

Definition at line 1975 of file usb.c.

1975 {
1976 struct usb_bus *bus = hub->bus;
1977 struct usb_port *port;
1978 unsigned int i;
1979 int rc;
1980
1981 /* Add to hub list */
1982 list_add_tail ( &hub->list, &bus->hubs );
1983
1984 /* Open hub (host controller) */
1985 if ( ( rc = hub->host->open ( hub ) ) != 0 ) {
1986 DBGC ( hub, "USB hub %s could not open: %s\n",
1987 hub->name, strerror ( rc ) );
1988 goto err_host_open;
1989 }
1990
1991 /* Open hub (driver) */
1992 if ( ( rc = hub->driver->open ( hub ) ) != 0 ) {
1993 DBGC ( hub, "USB hub %s could not open: %s\n",
1994 hub->name, strerror ( rc ) );
1995 goto err_driver_open;
1996 }
1997
1998 /* Delay to allow ports to stabilise */
2000
2001 /* Mark all ports as changed */
2002 for ( i = 1 ; i <= hub->ports ; i++ ) {
2003 port = usb_port ( hub, i );
2005 }
2006
2007 /* Some hubs seem to defer reporting device connections until
2008 * their interrupt endpoint is polled for the first time.
2009 * Poll the bus once now in order to pick up any such
2010 * connections.
2011 */
2012 usb_poll ( bus );
2013
2014 return 0;
2015
2016 hub->driver->close ( hub );
2017 err_driver_open:
2018 hub->host->close ( hub );
2019 err_host_open:
2020 list_del ( &hub->list );
2021 return rc;
2022}
#define USB_PORT_DELAY_MS
Time to wait for ports to stabilise.
Definition usb.h:1368
int(* open)(struct usb_hub *hub)
Open hub.
Definition usb.h:902
void(* close)(struct usb_hub *hub)
Close hub.
Definition usb.h:907
int(* open)(struct usb_hub *hub)
Open hub.
Definition usb.h:887
void(* close)(struct usb_hub *hub)
Close hub.
Definition usb.h:892
struct list_head list
List of hubs.
Definition usb.h:864
void usb_port_changed(struct usb_port *port)
Report port status change.
Definition usb.c:1857

References bus, usb_hub::bus, usb_hub_driver_operations::close, usb_hub_host_operations::close, DBGC, usb_hub::driver, usb_hub::host, usb_bus::hub, usb_port::hub, usb_hub::list, list_add_tail, list_del, mdelay(), usb_hub::name, usb_hub_driver_operations::open, usb_hub_host_operations::open, port, usb_hub::ports, rc, strerror(), usb_poll(), usb_port(), usb_port_changed(), and USB_PORT_DELAY_MS.

Referenced by hub_probe(), and register_usb_bus().

◆ unregister_usb_hub()

void unregister_usb_hub ( struct usb_hub * hub)

Unregister USB hub.

Parameters
hubUSB hub

Definition at line 2029 of file usb.c.

2029 {
2030 struct usb_port *port;
2031 unsigned int i;
2032
2033 /* Detach all devices */
2034 for ( i = 1 ; i <= hub->ports ; i++ ) {
2035 port = usb_port ( hub, i );
2036 if ( port->attached )
2037 usb_detached ( port );
2038 }
2039
2040 /* Close hub (driver) */
2041 hub->driver->close ( hub );
2042
2043 /* Close hub (host controller) */
2044 hub->host->close ( hub );
2045
2046 /* Cancel any pending port status changes */
2047 for ( i = 1 ; i <= hub->ports ; i++ ) {
2048 port = usb_port ( hub, i );
2049 list_del ( &port->changed );
2050 INIT_LIST_HEAD ( &port->changed );
2051 }
2052
2053 /* Remove from hub list */
2054 list_del ( &hub->list );
2055}

References usb_hub_driver_operations::close, usb_hub_host_operations::close, usb_hub::driver, usb_hub::host, usb_port::hub, INIT_LIST_HEAD, usb_hub::list, list_del, port, usb_hub::ports, usb_detached(), and usb_port().

Referenced by hub_probe(), hub_remove(), register_usb_bus(), and unregister_usb_bus().

◆ free_usb_hub()

void free_usb_hub ( struct usb_hub * hub)

Free USB hub.

Parameters
hubUSB hub

Definition at line 2062 of file usb.c.

2062 {
2063 struct usb_port *port;
2064 unsigned int i;
2065
2066 /* Sanity checks */
2067 for ( i = 1 ; i <= hub->ports ; i++ ) {
2068 port = usb_port ( hub, i );
2069 assert ( ! port->attached );
2070 assert ( port->usb == NULL );
2071 assert ( list_empty ( &port->changed ) );
2072 }
2073
2074 /* Free hub */
2075 free ( hub );
2076}

References assert, free, usb_port::hub, list_empty, NULL, port, usb_hub::ports, and usb_port().

Referenced by alloc_usb_bus(), free_usb_bus(), hub_probe(), and hub_remove().

◆ alloc_usb_bus()

struct usb_bus * alloc_usb_bus ( struct device * dev,
unsigned int ports,
size_t mtu,
struct usb_host_operations * op )

Allocate USB bus.

Parameters
devUnderlying hardware device
portsNumber of root hub ports
mtuLargest transfer allowed on the bus
opHost controller operations
Return values
busUSB bus, or NULL on allocation failure

Definition at line 2094 of file usb.c.

2095 {
2096 struct usb_bus *bus;
2097
2098 /* Allocate and initialise structure */
2099 bus = zalloc ( sizeof ( *bus ) );
2100 if ( ! bus )
2101 goto err_alloc_bus;
2102 bus->name = dev->name;
2103 bus->dev = dev;
2104 bus->mtu = mtu;
2105 bus->op = op;
2106 INIT_LIST_HEAD ( &bus->devices );
2107 INIT_LIST_HEAD ( &bus->hubs );
2108 bus->host = &bus->op->bus;
2109
2110 /* Allocate root hub */
2111 bus->hub = alloc_usb_hub ( bus, NULL, ports, &op->root );
2112 if ( ! bus->hub )
2113 goto err_alloc_hub;
2114
2115 return bus;
2116
2117 free_usb_hub ( bus->hub );
2118 err_alloc_hub:
2119 free ( bus );
2120 err_alloc_bus:
2121 return NULL;
2122}
static uint16_t struct vmbus_xfer_pages_operations * op
Definition netvsc.h:327
struct device * dev
Underlying hardware device.
Definition usb.h:980
void free_usb_hub(struct usb_hub *hub)
Free USB hub.
Definition usb.c:2062
struct usb_hub * alloc_usb_hub(struct usb_bus *bus, struct usb_device *usb, unsigned int ports, struct usb_hub_driver_operations *driver)
Allocate USB hub.
Definition usb.c:1936

References alloc_usb_hub(), bus, usb_bus::dev, free, free_usb_hub(), INIT_LIST_HEAD, mtu, device::name, NULL, op, and zalloc().

Referenced by ehci_probe(), uhci_probe(), usbio_start(), and xhci_register().

◆ register_usb_bus()

int register_usb_bus ( struct usb_bus * bus)

Register USB bus.

Parameters
busUSB bus
Return values
rcReturn status code

Definition at line 2130 of file usb.c.

2130 {
2131 int rc;
2132
2133 /* Sanity checks */
2134 assert ( bus->hub != NULL );
2135
2136 /* Assign the first available bus address */
2137 bus->address = 0;
2138 while ( find_usb_bus ( bus->address ) != NULL )
2139 bus->address++;
2140
2141 /* Open bus */
2142 if ( ( rc = bus->host->open ( bus ) ) != 0 )
2143 goto err_open;
2144
2145 /* Add to list of USB buses */
2146 list_add_tail ( &bus->list, &usb_buses );
2147
2148 /* Register root hub */
2149 if ( ( rc = register_usb_hub ( bus->hub ) ) != 0 )
2150 goto err_register_hub;
2151
2152 /* Attach any devices already present */
2153 usb_hotplug();
2154
2155 return 0;
2156
2157 unregister_usb_hub ( bus->hub );
2158 err_register_hub:
2159 list_del ( &bus->list );
2160 bus->host->close ( bus );
2161 err_open:
2162 return rc;
2163}
struct list_head usb_buses
List of USB buses.
Definition usb.c:45
struct usb_bus * find_usb_bus(unsigned int address)
Find USB bus by address.
Definition usb.c:2219
void unregister_usb_hub(struct usb_hub *hub)
Unregister USB hub.
Definition usb.c:2029
int register_usb_hub(struct usb_hub *hub)
Register USB hub.
Definition usb.c:1975

References assert, bus, find_usb_bus(), list_add_tail, list_del, NULL, rc, register_usb_hub(), unregister_usb_hub(), usb_buses, and usb_hotplug().

Referenced by ehci_probe(), REQUIRING_SYMBOL(), uhci_probe(), usbio_start(), and xhci_register().

◆ unregister_usb_bus()

void unregister_usb_bus ( struct usb_bus * bus)

Unregister USB bus.

Parameters
busUSB bus

Definition at line 2170 of file usb.c.

2170 {
2171
2172 /* Sanity checks */
2173 assert ( bus->hub != NULL );
2174
2175 /* Unregister root hub */
2176 unregister_usb_hub ( bus->hub );
2177
2178 /* Remove from list of USB buses */
2179 list_del ( &bus->list );
2180
2181 /* Close bus */
2182 bus->host->close ( bus );
2183
2184 /* Sanity checks */
2185 assert ( list_empty ( &bus->devices ) );
2186 assert ( list_empty ( &bus->hubs ) );
2187}

References assert, bus, list_del, list_empty, NULL, and unregister_usb_hub().

Referenced by ehci_probe(), ehci_remove(), uhci_probe(), uhci_remove(), usbio_start(), usbio_stop(), xhci_register(), and xhci_unregister().

◆ free_usb_bus()

void free_usb_bus ( struct usb_bus * bus)

Free USB bus.

Parameters
busUSB bus

Definition at line 2194 of file usb.c.

2194 {
2195 struct usb_endpoint *ep;
2196 struct usb_port *port;
2197
2198 /* Sanity checks */
2199 assert ( list_empty ( &bus->devices ) );
2200 assert ( list_empty ( &bus->hubs ) );
2201 list_for_each_entry ( ep, &usb_halted, halted )
2202 assert ( ep->usb->port->hub->bus != bus );
2204 assert ( port->hub->bus != bus );
2205
2206 /* Free root hub */
2207 free_usb_hub ( bus->hub );
2208
2209 /* Free bus */
2210 free ( bus );
2211}

References assert, bus, usb_hub::bus, usb_port::changed, free, free_usb_hub(), usb_port::hub, list_empty, list_for_each_entry, port, usb_device::port, usb_endpoint::usb, usb_changed, and usb_halted.

Referenced by ehci_probe(), ehci_remove(), uhci_probe(), uhci_remove(), usbio_start(), usbio_stop(), xhci_register(), and xhci_unregister().

◆ find_usb_bus()

struct usb_bus * find_usb_bus ( unsigned int address)

Find USB bus by address.

Parameters
addressBus address
Return values
busUSB bus, or NULL

Definition at line 2219 of file usb.c.

2219 {
2220 struct usb_bus *bus;
2221
2222 for_each_usb_bus ( bus ) {
2223 if ( bus->address == address )
2224 return bus;
2225 }
2226
2227 return NULL;
2228}

References address, bus, for_each_usb_bus, and NULL.

Referenced by register_usb_bus(), usb_find_next(), and usb_settings_fetch().

◆ find_usb_bus_by_location()

struct usb_bus * find_usb_bus_by_location ( unsigned int bus_type,
unsigned int location )

Find USB bus by device location.

Parameters
bus_typeBus type
locationBus location
Return values
busUSB bus, or NULL

Definition at line 2237 of file usb.c.

2238 {
2239 struct usb_bus *bus;
2240
2241 for_each_usb_bus ( bus ) {
2242 if ( ( bus->dev->desc.bus_type == bus_type ) &&
2243 ( bus->dev->desc.location == location ) )
2244 return bus;
2245 }
2246
2247 return NULL;
2248}

References bus, for_each_usb_bus, and NULL.

Referenced by uhci_root_speed().

◆ usb_alloc_address()

int usb_alloc_address ( struct usb_bus * bus)

Allocate device address.

Parameters
busUSB bus
Return values
addressDevice address, or negative error

Definition at line 2263 of file usb.c.

2263 {
2264 unsigned int address;
2265
2266 /* Find first free device address */
2267 address = ffsll ( ~bus->addresses );
2268 if ( ! address )
2269 return -ENOENT;
2270
2271 /* Mark address as used */
2272 bus->addresses |= ( 1ULL << ( address - 1 ) );
2273
2274 return address;
2275}
#define ffsll(x)
Find first (i.e.
Definition strings.h:123

References address, bus, ENOENT, and ffsll.

Referenced by ehci_device_address(), and uhci_device_address().

◆ usb_free_address()

void usb_free_address ( struct usb_bus * bus,
unsigned int address )

Free device address.

Parameters
busUSB bus
addressDevice address

Definition at line 2283 of file usb.c.

2283 {
2284
2285 /* Sanity check */
2286 assert ( address > 0 );
2287 assert ( bus->addresses & ( 1ULL << ( address - 1 ) ) );
2288
2289 /* Mark address as free */
2290 bus->addresses &= ~( 1ULL << ( address - 1 ) );
2291}

References address, assert, and bus.

Referenced by ehci_device_address(), ehci_device_close(), uhci_device_address(), and uhci_device_close().

◆ usb_find_next()

int usb_find_next ( struct usb_device ** usb,
uint16_t * busdev )

Find next USB device.

Parameters
usbUSB device to fill in
busdevStarting bus:dev address
Return values
busdevBus:dev address of next USB device
rcReturn status code

Definition at line 2301 of file usb.c.

2301 {
2302 struct usb_bus *bus;
2303
2304 do {
2305 /* Find USB bus, if any */
2306 bus = find_usb_bus ( USB_BUS ( *busdev ) );
2307 if ( ! bus ) {
2308 *busdev |= ( USB_BUS ( 1 ) - 1 );
2309 continue;
2310 }
2311
2312 /* Find USB device, if any */
2313 *usb = find_usb ( bus, USB_DEV ( *busdev ) );
2314 if ( *usb )
2315 return 0;
2316
2317 } while ( ++(*busdev) );
2318
2319 return -ENODEV;
2320}
#define USB_BUS(busdev)
Extract USB bus address.
Definition usb.h:67
#define USB_DEV(busdev)
Extract USB device address.
Definition usb.h:70
struct usb_device * find_usb(struct usb_bus *bus, unsigned int address)
Find USB device by address.
Definition usb.c:1734

References bus, ENODEV, find_usb(), find_usb_bus(), USB_BUS, and USB_DEV.

Referenced by usbscan_exec().

◆ usb_route_string()

unsigned int usb_route_string ( struct usb_device * usb)

Get USB route string.

Parameters
usbUSB device
Return values
routeUSB route string

Definition at line 2335 of file usb.c.

2335 {
2336 struct usb_device *parent;
2337 unsigned int route;
2338
2339 /* Navigate up to root hub, constructing route string as we go */
2340 for ( route = 0 ; ( parent = usb->port->hub->usb ) ; usb = parent ) {
2341 route <<= 4;
2342 route |= ( ( usb->port->address > 0xf ) ?
2343 0xf : usb->port->address );
2344 }
2345
2346 return route;
2347}
void route(void)
Print routing table.
Definition route.c:40
unsigned int address
Port address.
Definition usb.h:827

References usb_port::address, usb_port::hub, usb_device::port, route(), and usb_hub::usb.

Referenced by xhci_device_address().

◆ usb_root_hub_port()

struct usb_port * usb_root_hub_port ( struct usb_device * usb)

Get USB root hub port.

Parameters
usbUSB device
Return values
portRoot hub port

Definition at line 2355 of file usb.c.

2355 {
2356 struct usb_device *parent;
2357
2358 /* Navigate up to root hub */
2359 while ( ( parent = usb->port->hub->usb ) )
2360 usb = parent;
2361
2362 return usb->port;
2363}

References usb_port::hub, usb_device::port, and usb_hub::usb.

Referenced by xhci_device_address(), and xhci_device_open().

◆ usb_transaction_translator()

struct usb_port * usb_transaction_translator ( struct usb_device * usb)

Get USB transaction translator.

Parameters
usbUSB device
Return values
portTransaction translator port, or NULL

Definition at line 2371 of file usb.c.

2371 {
2372 struct usb_device *parent;
2373
2374 /* Navigate up to root hub. If we find a low-speed or
2375 * full-speed device with a higher-speed parent hub, then that
2376 * device's port is the transaction translator.
2377 */
2378 for ( ; ( parent = usb->port->hub->usb ) ; usb = parent ) {
2379 if ( ( usb->speed <= USB_SPEED_FULL ) &&
2380 ( parent->speed > USB_SPEED_FULL ) )
2381 return usb->port;
2382 }
2383
2384 return NULL;
2385}

References usb_port::hub, NULL, usb_device::port, usb_device::speed, usb_hub::usb, and USB_SPEED_FULL.

Referenced by ehci_endpoint_capabilities(), usb_endpoint_clear_tt(), and xhci_device_open().

◆ REQUIRING_SYMBOL()

REQUIRING_SYMBOL ( register_usb_bus )

References register_usb_bus().

◆ REQUIRE_OBJECT() [1/2]

REQUIRE_OBJECT ( config_usb )

◆ REQUIRE_OBJECT() [2/2]

REQUIRE_OBJECT ( usbhub )

Variable Documentation

◆ usb_buses

struct list_head usb_buses = LIST_HEAD_INIT ( usb_buses )

List of USB buses.

Definition at line 45 of file usb.c.

Referenced by register_usb_bus().

◆ usb_changed

struct list_head usb_changed = LIST_HEAD_INIT ( usb_changed )
static

List of changed ports.

Definition at line 48 of file usb.c.

Referenced by free_usb_bus(), usb_hotplug(), and usb_port_changed().

◆ usb_halted

struct list_head usb_halted = LIST_HEAD_INIT ( usb_halted )
static

List of halted endpoints.

Definition at line 51 of file usb.c.

Referenced by free_usb_bus(), usb_complete_err(), and usb_step().

◆ usb_control_operations

struct usb_endpoint_driver_operations usb_control_operations
static
Initial value:
= {
.complete = usb_control_complete,
}
static void usb_control_complete(struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
Complete USB control transfer.
Definition usb.c:754

USB control endpoint driver operations.

Definition at line 768 of file usb.c.

768 {
769 .complete = usb_control_complete,
770};

Referenced by alloc_usb().