| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup libusbhost
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | *
|
|---|
| 34 | * Endpoint structure is tightly coupled to the bus. The bus controls the
|
|---|
| 35 | * life-cycle of endpoint. In order to keep endpoints lightweight, operations
|
|---|
| 36 | * on endpoints are part of the bus structure.
|
|---|
| 37 | *
|
|---|
| 38 | */
|
|---|
| 39 | #ifndef LIBUSBHOST_HOST_ENDPOINT_H
|
|---|
| 40 | #define LIBUSBHOST_HOST_ENDPOINT_H
|
|---|
| 41 |
|
|---|
| 42 | #include <adt/list.h>
|
|---|
| 43 | #include <atomic.h>
|
|---|
| 44 | #include <fibril_synch.h>
|
|---|
| 45 | #include <stdbool.h>
|
|---|
| 46 | #include <sys/time.h>
|
|---|
| 47 | #include <usb/usb.h>
|
|---|
| 48 | #include <usb/host/bus.h>
|
|---|
| 49 | #include <usbhc_iface.h>
|
|---|
| 50 |
|
|---|
| 51 | typedef struct bus bus_t;
|
|---|
| 52 | typedef struct device device_t;
|
|---|
| 53 | typedef struct transfer_request transfer_request_t;
|
|---|
| 54 | typedef struct usb_transfer_batch usb_transfer_batch_t;
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Host controller side endpoint structure.
|
|---|
| 58 | *
|
|---|
| 59 | * This structure, though reference-counted, is very fragile. It is responsible
|
|---|
| 60 | * for synchronizing transfer batch scheduling and completion.
|
|---|
| 61 | *
|
|---|
| 62 | * To avoid situations, in which two locks must be obtained to schedule/finish
|
|---|
| 63 | * a transfer, the endpoint inherits a lock from the outside. Because the
|
|---|
| 64 | * concrete instance of mutex can be unknown at the time of initialization,
|
|---|
| 65 | * the HC shall pass the right lock at the time of onlining the endpoint.
|
|---|
| 66 | *
|
|---|
| 67 | * The fields used for scheduling (online, active_batch) are to be used only
|
|---|
| 68 | * under that guard and by functions designed for this purpose. The driver can
|
|---|
| 69 | * also completely avoid using this mechanism, in which case it is on its own in
|
|---|
| 70 | * question of transfer aborting.
|
|---|
| 71 | *
|
|---|
| 72 | * Relevant information can be found in the documentation of HelenOS xHCI
|
|---|
| 73 | * project.
|
|---|
| 74 | */
|
|---|
| 75 | typedef struct endpoint {
|
|---|
| 76 | /** USB device */
|
|---|
| 77 | device_t *device;
|
|---|
| 78 | /** Reference count. */
|
|---|
| 79 | atomic_t refcnt;
|
|---|
| 80 |
|
|---|
| 81 | /** An inherited guard */
|
|---|
| 82 | fibril_mutex_t *guard;
|
|---|
| 83 | /** Whether it's allowed to schedule on this endpoint */
|
|---|
| 84 | bool online;
|
|---|
| 85 | /** The currently active transfer batch. */
|
|---|
| 86 | usb_transfer_batch_t *active_batch;
|
|---|
| 87 | /** Signals change of active status. */
|
|---|
| 88 | fibril_condvar_t avail;
|
|---|
| 89 |
|
|---|
| 90 | /** Endpoint number */
|
|---|
| 91 | usb_endpoint_t endpoint;
|
|---|
| 92 | /** Communication direction. */
|
|---|
| 93 | usb_direction_t direction;
|
|---|
| 94 | /** USB transfer type. */
|
|---|
| 95 | usb_transfer_type_t transfer_type;
|
|---|
| 96 | /** Maximum size of one packet */
|
|---|
| 97 | size_t max_packet_size;
|
|---|
| 98 |
|
|---|
| 99 | /** Maximum size of one transfer */
|
|---|
| 100 | size_t max_transfer_size;
|
|---|
| 101 |
|
|---|
| 102 | /* Policies for transfer buffers */
|
|---|
| 103 | dma_policy_t transfer_buffer_policy; /**< A hint for optimal performance. */
|
|---|
| 104 | dma_policy_t required_transfer_buffer_policy; /**< Enforced by the library. */
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Number of packets that can be sent in one service interval
|
|---|
| 108 | * (not necessarily uframe, despite its name)
|
|---|
| 109 | */
|
|---|
| 110 | unsigned packets_per_uframe;
|
|---|
| 111 |
|
|---|
| 112 | /* This structure is meant to be extended by overriding. */
|
|---|
| 113 | } endpoint_t;
|
|---|
| 114 |
|
|---|
| 115 | extern void endpoint_init(endpoint_t *, device_t *, const usb_endpoint_descriptors_t *);
|
|---|
| 116 |
|
|---|
| 117 | extern void endpoint_add_ref(endpoint_t *);
|
|---|
| 118 | extern void endpoint_del_ref(endpoint_t *);
|
|---|
| 119 |
|
|---|
| 120 | extern void endpoint_set_online(endpoint_t *, fibril_mutex_t *);
|
|---|
| 121 | extern void endpoint_set_offline_locked(endpoint_t *);
|
|---|
| 122 |
|
|---|
| 123 | extern void endpoint_wait_timeout_locked(endpoint_t *ep, suseconds_t);
|
|---|
| 124 | extern int endpoint_activate_locked(endpoint_t *, usb_transfer_batch_t *);
|
|---|
| 125 | extern void endpoint_deactivate_locked(endpoint_t *);
|
|---|
| 126 |
|
|---|
| 127 | int endpoint_send_batch(endpoint_t *, const transfer_request_t *);
|
|---|
| 128 |
|
|---|
| 129 | static inline bus_t *endpoint_get_bus(endpoint_t *ep)
|
|---|
| 130 | {
|
|---|
| 131 | device_t * const device = ep->device;
|
|---|
| 132 | return device ? device->bus : NULL;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | #endif
|
|---|
| 136 |
|
|---|
| 137 | /**
|
|---|
| 138 | * @}
|
|---|
| 139 | */
|
|---|