source: mainline/uspace/lib/usbhost/src/endpoint.c@ bd1fab90

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bd1fab90 was 32fb6bce, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usbhost: refactoring

This commit moves interrupt, status and schedule to bus
operations. Then the purpose of hcd_t is better defined, and split into
hc_driver_t and hc_device_t. hc_driver_t is used to wrap driver
implementation by the library (similar to how usb_driver_t is used to
wrap usb device drivers). hc_device_t is used as a parent for hc_t
inside drivers, and is allocated inside the DDF device node.

To support these changes, some local identifiers were renamed, some
functions were moved and/or renamed and their arguments changed. The
most notable one being hcd_send_batch → bus_device_send_batch.

  • Property mode set to 100644
File size: 7.0 KB
RevLine 
[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
[6a32665d]37#include <assert.h>
[f527f58]38#include <atomic.h>
[41924f30]39#include <mem.h>
40#include <stdlib.h>
[32fb6bce]41#include <str_error.h>
42#include <usb/debug.h>
43#include <usb/host/hcd.h>
[6ce42e85]44
[64fea02]45#include "usb_transfer_batch.h"
46#include "bus.h"
47
48#include "endpoint.h"
49
[41924f30]50/** Initialize provided endpoint structure.
[17412546]51 */
[6832245]52void endpoint_init(endpoint_t *ep, device_t *dev, const usb_endpoint_desc_t *desc)
[6ce42e85]53{
[41924f30]54 memset(ep, 0, sizeof(endpoint_t));
[a76b01b4]55
[6832245]56 assert(dev);
57 ep->device = dev;
58
[41924f30]59 atomic_set(&ep->refcnt, 0);
60 link_initialize(&ep->link);
61 fibril_mutex_initialize(&ep->guard);
62 fibril_condvar_initialize(&ep->avail);
[6832245]63
64 ep->endpoint = desc->endpoint_no;
65 ep->direction = desc->direction;
66 ep->transfer_type = desc->transfer_type;
67 ep->max_packet_size = desc->max_packet_size;
68 ep->packets = desc->packets;
69
70 ep->bandwidth = endpoint_count_bw(ep, desc->max_packet_size);
71}
72
73static inline const bus_ops_t *get_bus_ops(endpoint_t *ep)
74{
75 return ep->device->bus->ops;
[6ce42e85]76}
[a76b01b4]77
[41924f30]78void endpoint_add_ref(endpoint_t *ep)
[f527f58]79{
[41924f30]80 atomic_inc(&ep->refcnt);
[f527f58]81}
82
[6832245]83static inline void endpoint_destroy(endpoint_t *ep)
84{
85 const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_destroy);
86 if (ops) {
87 ops->endpoint_destroy(ep);
88 } else {
89 assert(ep->active_batch == NULL);
90
91 /* Assume mostly the eps will be allocated by malloc. */
92 free(ep);
93 }
94}
95
[41924f30]96void endpoint_del_ref(endpoint_t *ep)
[f527f58]97{
[41924f30]98 if (atomic_predec(&ep->refcnt) == 0) {
[6832245]99 endpoint_destroy(ep);
[41924f30]100 }
[3d932af6]101}
[a76b01b4]102
[17412546]103/** Mark the endpoint as active and block access for further fibrils.
[41924f30]104 * @param ep endpoint_t structure.
[17412546]105 */
[17873ac7]106void endpoint_activate_locked(endpoint_t *ep, usb_transfer_batch_t *batch)
[6a32665d]107{
[41924f30]108 assert(ep);
[17873ac7]109 assert(batch);
110 assert(batch->ep == ep);
111 assert(fibril_mutex_is_locked(&ep->guard));
112
113 while (ep->active_batch != NULL)
[41924f30]114 fibril_condvar_wait(&ep->avail, &ep->guard);
[17873ac7]115 ep->active_batch = batch;
[6a32665d]116}
[a76b01b4]117
[17412546]118/** Mark the endpoint as inactive and allow access for further fibrils.
[41924f30]119 * @param ep endpoint_t structure.
[17412546]120 */
[17873ac7]121void endpoint_deactivate_locked(endpoint_t *ep)
[6a32665d]122{
[41924f30]123 assert(ep);
[17873ac7]124 assert(fibril_mutex_is_locked(&ep->guard));
125
126 if (ep->active_batch && ep->active_batch->error == EOK)
127 usb_transfer_batch_reset_toggle(ep->active_batch);
128
129 ep->active_batch = NULL;
130 fibril_condvar_signal(&ep->avail);
131}
132
133/** Abort an active batch on endpoint, if any.
134 *
135 * @param[in] ep endpoint_t structure.
136 */
137void endpoint_abort(endpoint_t *ep)
138{
139 assert(ep);
140
[41924f30]141 fibril_mutex_lock(&ep->guard);
[17873ac7]142 usb_transfer_batch_t *batch = ep->active_batch;
143 endpoint_deactivate_locked(ep);
[41924f30]144 fibril_mutex_unlock(&ep->guard);
[17873ac7]145
146 if (batch)
147 usb_transfer_batch_abort(batch);
[6a32665d]148}
[a76b01b4]149
[41924f30]150/** Get the value of toggle bit. Either uses the toggle_get op, or just returns
151 * the value of the toggle.
152 * @param ep endpoint_t structure.
[17412546]153 */
[41924f30]154int endpoint_toggle_get(endpoint_t *ep)
[f567bcf]155{
[41924f30]156 assert(ep);
[17873ac7]157
[6832245]158 const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_get_toggle);
159 return ops
160 ? ops->endpoint_get_toggle(ep)
[41924f30]161 : ep->toggle;
[f567bcf]162}
[a76b01b4]163
[41924f30]164/** Set the value of toggle bit. Either uses the toggle_set op, or just sets
165 * the toggle inside.
166 * @param ep endpoint_t structure.
[17412546]167 */
[17873ac7]168void endpoint_toggle_set(endpoint_t *ep, bool toggle)
[f567bcf]169{
[41924f30]170 assert(ep);
[17873ac7]171
[6832245]172 const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_set_toggle);
173 if (ops) {
174 ops->endpoint_set_toggle(ep, toggle);
[41924f30]175 }
176 else {
177 ep->toggle = toggle;
178 }
[f567bcf]179}
[f527f58]180
[6832245]181ssize_t endpoint_count_bw(endpoint_t *ep, size_t packet_size)
182{
183 assert(ep);
184
185 const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_count_bw);
186 if (!ops)
187 return 0;
188
189 return ops->endpoint_count_bw(ep, packet_size);
190}
191
[32fb6bce]192/** Prepare generic usb_transfer_batch and schedule it.
193 * @param ep Endpoint for which the batch shall be created.
194 * @param target address and endpoint number.
195 * @param setup_data Data to use in setup stage (Control communication type)
196 * @param in Callback for device to host communication.
197 * @param out Callback for host to device communication.
198 * @param arg Callback parameter.
199 * @param name Communication identifier (for nicer output).
200 * @return Error code.
201 */
202int endpoint_send_batch(endpoint_t *ep, usb_target_t target,
203 usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
204 usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
205{
206 usb_log_debug2("%s %d:%d %zu(%zu).\n",
207 name, target.address, target.endpoint, size, ep->max_packet_size);
208
209 bus_t *bus = endpoint_get_bus(ep);
210 const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, batch_schedule);
211 if (!ops) {
212 usb_log_error("HCD does not implement scheduler.\n");
213 return ENOTSUP;
214 }
215
216 const size_t bw = endpoint_count_bw(ep, size);
217 /* Check if we have enough bandwidth reserved */
218 if (ep->bandwidth < bw) {
219 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
220 "but only %zu is reserved.\n",
221 ep->device->address, ep->endpoint, name, bw, ep->bandwidth);
222 return ENOSPC;
223 }
224
225 usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
226 if (!batch) {
227 usb_log_error("Failed to create transfer batch.\n");
228 return ENOMEM;
229 }
230
231 batch->target = target;
232 batch->buffer = data;
233 batch->buffer_size = size;
234 batch->setup.packed = setup_data;
235 batch->dir = direction;
236 batch->on_complete = on_complete;
237 batch->on_complete_data = arg;
238
239 /* Check for commands that reset toggle bit */
240 if (ep->transfer_type == USB_TRANSFER_CONTROL)
241 batch->toggle_reset_mode
242 = hcd_get_request_toggle_reset_mode(&batch->setup.packet);
243
244 const int ret = ops->batch_schedule(batch);
245 if (ret != EOK) {
246 usb_log_warning("Batch %p failed to schedule: %s", batch, str_error(ret));
247 usb_transfer_batch_destroy(batch);
248 }
249
250 return ret;
251}
252
[6ce42e85]253/**
254 * @}
255 */
Note: See TracBrowser for help on using the repository browser.