HelenOS sources
This source file includes following definitions.
- usb_polling_init
- usb_polling_fini
- polling_fibril
- usb_polling_start
- usb_polling_join
#include <usb/dev/device.h>
#include <usb/dev/pipes.h>
#include <usb/dev/poll.h>
#include <usb/dev/request.h>
#include <usb/classes/classes.h>
#include <usb/debug.h>
#include <usb/descriptor.h>
#include <usb/usb.h>
#include <assert.h>
#include <async.h>
#include <errno.h>
#include <fibril.h>
#include <fibril_synch.h>
#include <stdbool.h>
#include <stdlib.h>
#include <str_error.h>
#include <stddef.h>
#include <stdint.h>
int usb_polling_init(usb_polling_t *polling)
{
if (!polling)
return EBADMEM;
memset(polling, 0, sizeof(usb_polling_t));
fibril_mutex_initialize(&polling->guard);
fibril_condvar_initialize(&polling->cv);
polling->auto_clear_halt = true;
polling->delay = -1;
polling->max_failures = 3;
return EOK;
}
void usb_polling_fini(usb_polling_t *polling)
{
assert(polling);
assert(!polling->running);
}
static errno_t polling_fibril(void *arg)
{
assert(arg);
usb_polling_t *polling = arg;
fibril_mutex_lock(&polling->guard);
polling->running = true;
fibril_mutex_unlock(&polling->guard);
usb_pipe_t *pipe = &polling->ep_mapping->pipe;
if (polling->debug > 0) {
const usb_endpoint_mapping_t *mapping =
polling->ep_mapping;
usb_log_debug("Poll (%p): started polling of `%s' - "
"interface %d (%s,%d,%d), %zuB/%zu.\n",
polling, usb_device_get_name(polling->device),
(int) mapping->interface->interface_number,
usb_str_class(mapping->interface->interface_class),
(int) mapping->interface->interface_subclass,
(int) mapping->interface->interface_protocol,
polling->request_size, pipe->desc.max_transfer_size);
}
size_t failed_attempts = 0;
while (failed_attempts <= polling->max_failures) {
size_t actual_size;
const errno_t rc = usb_pipe_read(pipe, polling->buffer,
polling->request_size, &actual_size);
if (rc == EOK) {
if (polling->debug > 1) {
usb_log_debug(
"Poll%p: received: '%s' (%zuB).\n",
polling,
usb_debug_str_buffer(polling->buffer,
actual_size, 16),
actual_size);
}
} else {
usb_log_debug(
"Poll%p: polling failed: %s.\n",
polling, str_error(rc));
}
if (rc == ESTALL && polling->auto_clear_halt) {
usb_pipe_clear_halt(
usb_device_get_default_pipe(polling->device), pipe);
}
if (rc != EOK) {
++failed_attempts;
const bool carry_on = !polling->on_error ? true :
polling->on_error(polling->device, rc, polling->arg);
if (!carry_on || polling->joining) {
failed_attempts = 0;
break;
}
continue;
}
assert(polling->on_data);
const bool carry_on = polling->on_data(polling->device,
polling->buffer, actual_size, polling->arg);
if (!carry_on) {
failed_attempts = 0;
break;
}
failed_attempts = 0;
fibril_usleep(polling->delay);
}
const bool failed = failed_attempts > 0;
if (polling->on_polling_end)
polling->on_polling_end(polling->device, failed, polling->arg);
if (polling->debug > 0) {
if (failed) {
usb_log_error("Polling of device `%s' terminated: "
"recurring failures.\n",
usb_device_get_name(polling->device));
} else {
usb_log_debug("Polling of device `%s' terminated: "
"driver request.\n",
usb_device_get_name(polling->device));
}
}
fibril_mutex_lock(&polling->guard);
polling->running = false;
fibril_mutex_unlock(&polling->guard);
fibril_condvar_broadcast(&polling->cv);
return EOK;
}
errno_t usb_polling_start(usb_polling_t *polling)
{
if (!polling || !polling->device || !polling->ep_mapping || !polling->on_data)
return EBADMEM;
if (!polling->request_size)
return EINVAL;
if (!polling->ep_mapping || (polling->ep_mapping->pipe.desc.transfer_type != USB_TRANSFER_INTERRUPT) ||
(polling->ep_mapping->pipe.desc.direction != USB_DIRECTION_IN))
return EINVAL;
if (polling->delay < 0)
polling->delay = polling->ep_mapping->descriptor->poll_interval;
polling->fibril = fibril_create(polling_fibril, polling);
if (!polling->fibril)
return ENOMEM;
fibril_add_ready(polling->fibril);
return EOK;
}
errno_t usb_polling_join(usb_polling_t *polling)
{
errno_t rc;
if (!polling)
return EBADMEM;
if (!polling->running)
return EOK;
polling->joining = true;
rc = usb_device_unmap_ep(polling->ep_mapping);
if (rc != EOK && rc != ENOENT && rc != EHANGUP)
return rc;
fibril_mutex_lock(&polling->guard);
while (polling->running)
fibril_condvar_wait(&polling->cv, &polling->guard);
fibril_mutex_unlock(&polling->guard);
return EOK;
}
HelenOS homepage, sources at GitHub