| [6ce42e85] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| [41924f30] | 3 | * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
|
|---|
| [6ce42e85] | 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| [f527f58] | 29 |
|
|---|
| [17412546] | 30 | /** @addtogroup libusbhost
|
|---|
| [6ce42e85] | 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | * @brief UHCI host controller driver structure
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| [8d2e251] | 37 | #include <usb/host/endpoint.h>
|
|---|
| [41924f30] | 38 | #include <usb/host/bus.h>
|
|---|
| [8d2e251] | 39 |
|
|---|
| [6a32665d] | 40 | #include <assert.h>
|
|---|
| [f527f58] | 41 | #include <atomic.h>
|
|---|
| [41924f30] | 42 | #include <mem.h>
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| [6ce42e85] | 44 |
|
|---|
| [41924f30] | 45 | /** Initialize provided endpoint structure.
|
|---|
| [17412546] | 46 | */
|
|---|
| [41924f30] | 47 | void endpoint_init(endpoint_t *ep, bus_t *bus)
|
|---|
| [6ce42e85] | 48 | {
|
|---|
| [41924f30] | 49 | memset(ep, 0, sizeof(endpoint_t));
|
|---|
| [a76b01b4] | 50 |
|
|---|
| [41924f30] | 51 | ep->bus = bus;
|
|---|
| 52 | atomic_set(&ep->refcnt, 0);
|
|---|
| 53 | link_initialize(&ep->link);
|
|---|
| 54 | fibril_mutex_initialize(&ep->guard);
|
|---|
| 55 | fibril_condvar_initialize(&ep->avail);
|
|---|
| [6ce42e85] | 56 | }
|
|---|
| [a76b01b4] | 57 |
|
|---|
| [41924f30] | 58 | void endpoint_add_ref(endpoint_t *ep)
|
|---|
| [f527f58] | 59 | {
|
|---|
| [41924f30] | 60 | atomic_inc(&ep->refcnt);
|
|---|
| [f527f58] | 61 | }
|
|---|
| 62 |
|
|---|
| [41924f30] | 63 | void endpoint_del_ref(endpoint_t *ep)
|
|---|
| [f527f58] | 64 | {
|
|---|
| [41924f30] | 65 | if (atomic_predec(&ep->refcnt) == 0) {
|
|---|
| 66 | if (ep->bus->ops.destroy_endpoint) {
|
|---|
| 67 | ep->bus->ops.destroy_endpoint(ep);
|
|---|
| 68 | }
|
|---|
| 69 | else {
|
|---|
| 70 | assert(!ep->active);
|
|---|
| [f527f58] | 71 |
|
|---|
| [41924f30] | 72 | /* Assume mostly the eps will be allocated by malloc. */
|
|---|
| 73 | free(ep);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| [3d932af6] | 76 | }
|
|---|
| [a76b01b4] | 77 |
|
|---|
| [17412546] | 78 | /** Mark the endpoint as active and block access for further fibrils.
|
|---|
| [41924f30] | 79 | * @param ep endpoint_t structure.
|
|---|
| [17412546] | 80 | */
|
|---|
| [41924f30] | 81 | void endpoint_use(endpoint_t *ep)
|
|---|
| [6a32665d] | 82 | {
|
|---|
| [41924f30] | 83 | assert(ep);
|
|---|
| [f527f58] | 84 | /* Add reference for active endpoint. */
|
|---|
| [41924f30] | 85 | endpoint_add_ref(ep);
|
|---|
| 86 | fibril_mutex_lock(&ep->guard);
|
|---|
| 87 | while (ep->active)
|
|---|
| 88 | fibril_condvar_wait(&ep->avail, &ep->guard);
|
|---|
| 89 | ep->active = true;
|
|---|
| 90 | fibril_mutex_unlock(&ep->guard);
|
|---|
| [6a32665d] | 91 | }
|
|---|
| [a76b01b4] | 92 |
|
|---|
| [17412546] | 93 | /** Mark the endpoint as inactive and allow access for further fibrils.
|
|---|
| [41924f30] | 94 | * @param ep endpoint_t structure.
|
|---|
| [17412546] | 95 | */
|
|---|
| [41924f30] | 96 | void endpoint_release(endpoint_t *ep)
|
|---|
| [6a32665d] | 97 | {
|
|---|
| [41924f30] | 98 | assert(ep);
|
|---|
| 99 | fibril_mutex_lock(&ep->guard);
|
|---|
| 100 | ep->active = false;
|
|---|
| 101 | fibril_mutex_unlock(&ep->guard);
|
|---|
| 102 | fibril_condvar_signal(&ep->avail);
|
|---|
| [f527f58] | 103 | /* Drop reference for active endpoint. */
|
|---|
| [41924f30] | 104 | endpoint_del_ref(ep);
|
|---|
| [6a32665d] | 105 | }
|
|---|
| [a76b01b4] | 106 |
|
|---|
| [41924f30] | 107 | /** Get the value of toggle bit. Either uses the toggle_get op, or just returns
|
|---|
| 108 | * the value of the toggle.
|
|---|
| 109 | * @param ep endpoint_t structure.
|
|---|
| [17412546] | 110 | */
|
|---|
| [41924f30] | 111 | int endpoint_toggle_get(endpoint_t *ep)
|
|---|
| [f567bcf] | 112 | {
|
|---|
| [41924f30] | 113 | assert(ep);
|
|---|
| 114 | fibril_mutex_lock(&ep->guard);
|
|---|
| 115 | const int ret = ep->bus->ops.endpoint_get_toggle
|
|---|
| 116 | ? ep->bus->ops.endpoint_get_toggle(ep)
|
|---|
| 117 | : ep->toggle;
|
|---|
| 118 | fibril_mutex_unlock(&ep->guard);
|
|---|
| [17412546] | 119 | return ret;
|
|---|
| [f567bcf] | 120 | }
|
|---|
| [a76b01b4] | 121 |
|
|---|
| [41924f30] | 122 | /** Set the value of toggle bit. Either uses the toggle_set op, or just sets
|
|---|
| 123 | * the toggle inside.
|
|---|
| 124 | * @param ep endpoint_t structure.
|
|---|
| [17412546] | 125 | */
|
|---|
| [41924f30] | 126 | void endpoint_toggle_set(endpoint_t *ep, unsigned toggle)
|
|---|
| [f567bcf] | 127 | {
|
|---|
| [41924f30] | 128 | assert(ep);
|
|---|
| [f567bcf] | 129 | assert(toggle == 0 || toggle == 1);
|
|---|
| [41924f30] | 130 | fibril_mutex_lock(&ep->guard);
|
|---|
| 131 | if (ep->bus->ops.endpoint_set_toggle) {
|
|---|
| 132 | ep->bus->ops.endpoint_set_toggle(ep, toggle);
|
|---|
| 133 | }
|
|---|
| 134 | else {
|
|---|
| 135 | ep->toggle = toggle;
|
|---|
| 136 | }
|
|---|
| 137 | fibril_mutex_unlock(&ep->guard);
|
|---|
| [f567bcf] | 138 | }
|
|---|
| [f527f58] | 139 |
|
|---|
| [41924f30] | 140 |
|
|---|
| [6ce42e85] | 141 | /**
|
|---|
| 142 | * @}
|
|---|
| 143 | */
|
|---|