HelenOS sources
This source file includes following definitions.
- request_address
- release_address
- address_device
- usb2_bus_device_enumerate
- usb2_bus_device_gone
- usb2_bus_endpoint_register
- usb2_bus_endpoint_unregister
- usb2_bus_helper_init
#include <assert.h>
#include <errno.h>
#include <macros.h>
#include <stdbool.h>
#include <stdlib.h>
#include <str_error.h>
#include <usb/debug.h>
#include <usb/descriptor.h>
#include <usb/request.h>
#include <usb/host/utility.h>
#include <usb/usb.h>
#include "endpoint.h"
#include "ddf_helpers.h"
#include "usb2_bus.h"
static int request_address(usb2_bus_helper_t *helper, usb_address_t *addr)
{
usb_address_t new_address = helper->last_address;
do {
new_address = (new_address + 1) % USB_ADDRESS_COUNT;
if (new_address == USB_ADDRESS_DEFAULT)
new_address = 1;
if (new_address == helper->last_address)
return ENOSPC;
} while (helper->address_occupied[new_address]);
helper->last_address = new_address;
*addr = new_address;
helper->address_occupied[*addr] = true;
return EOK;
}
static void release_address(usb2_bus_helper_t *helper, usb_address_t address)
{
helper->address_occupied[address] = false;
}
static const usb_target_t usb2_default_target = {
{
.address = USB_ADDRESS_DEFAULT,
.endpoint = 0,
}
};
static int address_device(usb2_bus_helper_t *helper, device_t *dev)
{
int err;
dev->address = USB_ADDRESS_DEFAULT;
usb_address_t address = USB_ADDRESS_DEFAULT;
if ((err = request_address(helper, &address))) {
usb_log_error("Failed to reserve new address: %s.",
str_error(err));
return err;
}
usb_log_debug("Device(%d): Reserved new address.", address);
usb_log_debug("Device(%d): Adding default target (0:0)", address);
usb_endpoint_descriptors_t ep0_desc = {
.endpoint.max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
};
endpoint_t *default_ep;
err = bus_endpoint_add(dev, &ep0_desc, &default_ep);
if (err != EOK) {
usb_log_error("Device(%d): Failed to add default target: %s.",
address, str_error(err));
goto err_address;
}
if ((err = hc_get_ep0_max_packet_size(&ep0_desc.endpoint.max_packet_size, dev)))
goto err_address;
const usb_device_request_setup_packet_t set_address = SET_ADDRESS(address);
usb_log_debug("Device(%d): Setting USB address.", address);
err = bus_device_send_batch_sync(dev, usb2_default_target, USB_DIRECTION_OUT,
NULL, 0, set_address.raw, "set address", NULL);
if (err) {
usb_log_error("Device(%d): Failed to set new address: %s.",
address, str_error(err));
goto err_default_control_ep;
}
if ((err = bus_endpoint_remove(default_ep))) {
usb_log_error("Device(%d): Failed to unregister default target: %s", address, str_error(err));
goto err_address;
}
endpoint_del_ref(default_ep);
dev->address = address;
usb_log_debug("Device(%d): Registering control EP.", address);
err = bus_endpoint_add(dev, &ep0_desc, NULL);
if (err != EOK) {
usb_log_error("Device(%d): Failed to register EP0: %s",
address, str_error(err));
goto err_address;
}
return EOK;
err_default_control_ep:
bus_endpoint_remove(default_ep);
endpoint_del_ref(default_ep);
err_address:
release_address(helper, address);
return err;
}
int usb2_bus_device_enumerate(usb2_bus_helper_t *helper, device_t *dev)
{
int err;
usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
if ((err = address_device(helper, dev))) {
usb_log_error("Failed to setup address of the new device: %s", str_error(err));
return err;
}
if ((err = hc_device_explore(dev))) {
usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
release_address(helper, dev->address);
return err;
}
return EOK;
}
void usb2_bus_device_gone(usb2_bus_helper_t *helper, device_t *dev)
{
release_address(helper, dev->address);
}
int usb2_bus_endpoint_register(usb2_bus_helper_t *helper, endpoint_t *ep)
{
assert(ep);
assert(fibril_mutex_is_locked(&ep->device->guard));
size_t bw = helper->bw_accounting->count_bw(ep);
if (bw > helper->free_bw)
return ENOSPC;
helper->free_bw -= bw;
return EOK;
}
void usb2_bus_endpoint_unregister(usb2_bus_helper_t *helper, endpoint_t *ep)
{
assert(helper);
assert(ep);
helper->free_bw += helper->bw_accounting->count_bw(ep);
}
void usb2_bus_helper_init(usb2_bus_helper_t *helper, const bandwidth_accounting_t *bw_accounting)
{
assert(helper);
assert(bw_accounting);
helper->bw_accounting = bw_accounting;
helper->free_bw = bw_accounting->available_bandwidth;
helper->last_address = USB_ADDRESS_COUNT - 2;
}
HelenOS homepage, sources at GitHub