source: mainline/uspace/lib/usbhost/src/hcd.c@ 4594baa

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

xhci: use device_t for bookkeeping

This started as a little refactoring to move active transfer batch to endpoint. Finding the EP in handler needs devices indexed by slot id. Then I found out we do not use the device_t extendable mechanism. Then there were a lot of errors found while doing all this…

  • Property mode set to 100644
File size: 5.9 KB
Line 
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 */
35
36#include <usb/debug.h>
37#include <usb/request.h>
38
39#include <assert.h>
40#include <async.h>
41#include <errno.h>
42#include <usb_iface.h>
43#include <str_error.h>
44
45#include "hcd.h"
46
47
48/** Initialize hcd_t structure.
49 * Initializes device and endpoint managers. Sets data and hook pointer to NULL.
50 *
51 * @param hcd hcd_t structure to initialize, non-null.
52 * @param max_speed Maximum supported USB speed (full, high).
53 * @param bandwidth Available bandwidth, passed to endpoint manager.
54 * @param bw_count Bandwidth compute function, passed to endpoint manager.
55 */
56void hcd_init(hcd_t *hcd) {
57 assert(hcd);
58
59 hcd_set_implementation(hcd, NULL, NULL, NULL);
60}
61
62usb_address_t hcd_request_address(hcd_t *hcd, usb_speed_t speed)
63{
64 assert(hcd);
65 usb_address_t address = 0;
66 const int ret = bus_request_address(hcd->bus, &address, false, speed);
67 if (ret != EOK)
68 return ret;
69 return address;
70}
71
72/** Prepare generic usb_transfer_batch and schedule it.
73 * @param hcd Host controller driver.
74 * @param target address and endpoint number.
75 * @param setup_data Data to use in setup stage (Control communication type)
76 * @param in Callback for device to host communication.
77 * @param out Callback for host to device communication.
78 * @param arg Callback parameter.
79 * @param name Communication identifier (for nicer output).
80 * @return Error code.
81 */
82int hcd_send_batch(hcd_t *hcd, usb_target_t target, usb_direction_t direction,
83 void *data, size_t size, uint64_t setup_data,
84 usbhc_iface_transfer_in_callback_t in, usbhc_iface_transfer_out_callback_t out,
85 void *arg, const char *name)
86{
87 assert(hcd);
88
89 endpoint_t *ep = bus_find_endpoint(hcd->bus, target, direction);
90 if (ep == NULL) {
91 usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
92 target.address, target.endpoint, name);
93 return ENOENT;
94 }
95
96 usb_log_debug2("%s %d:%d %zu(%zu).\n",
97 name, target.address, target.endpoint, size, ep->max_packet_size);
98
99 const size_t bw = bus_count_bw(ep, size);
100 /* Check if we have enough bandwidth reserved */
101 if (ep->bandwidth < bw) {
102 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
103 "but only %zu is reserved.\n",
104 ep->target.address, ep->target.endpoint, name, bw, ep->bandwidth);
105 return ENOSPC;
106 }
107 if (!hcd->ops.schedule) {
108 usb_log_error("HCD does not implement scheduler.\n");
109 return ENOTSUP;
110 }
111
112 usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
113 if (!batch) {
114 usb_log_error("Failed to create transfer batch.\n");
115 return ENOMEM;
116 }
117
118 batch->buffer = data;
119 batch->buffer_size = size;
120 batch->setup.packed = setup_data;
121 batch->dir = direction;
122
123 /* Check for commands that reset toggle bit */
124 if (ep->transfer_type == USB_TRANSFER_CONTROL)
125 batch->toggle_reset_mode
126 = usb_request_get_toggle_reset_mode(&batch->setup.packet);
127
128 usb_transfer_batch_set_old_handlers(batch, in, out, arg);
129
130 const int ret = hcd->ops.schedule(hcd, batch);
131 if (ret != EOK) {
132 usb_log_warning("Batch %p failed to schedule: %s", batch, str_error(ret));
133 usb_transfer_batch_destroy(batch);
134 }
135
136 /* Drop our own reference to ep. */
137 endpoint_del_ref(ep);
138
139 return ret;
140}
141
142typedef struct {
143 fibril_mutex_t done_mtx;
144 fibril_condvar_t done_cv;
145 unsigned done;
146 int ret;
147 size_t size;
148} sync_data_t;
149
150static void transfer_in_cb(int ret, size_t size, void* data)
151{
152 sync_data_t *d = data;
153 assert(d);
154 d->ret = ret;
155 d->size = size;
156 fibril_mutex_lock(&d->done_mtx);
157 d->done = 1;
158 fibril_condvar_broadcast(&d->done_cv);
159 fibril_mutex_unlock(&d->done_mtx);
160}
161
162static void transfer_out_cb(int ret, void* data)
163{
164 sync_data_t *d = data;
165 assert(data);
166 d->ret = ret;
167 fibril_mutex_lock(&d->done_mtx);
168 d->done = 1;
169 fibril_condvar_broadcast(&d->done_cv);
170 fibril_mutex_unlock(&d->done_mtx);
171}
172
173/** this is really ugly version of sync usb communication */
174ssize_t hcd_send_batch_sync(
175 hcd_t *hcd, usb_target_t target, usb_direction_t dir,
176 void *data, size_t size, uint64_t setup_data, const char* name)
177{
178 assert(hcd);
179 sync_data_t sd = { .done = 0, .ret = EBUSY, .size = size };
180 fibril_mutex_initialize(&sd.done_mtx);
181 fibril_condvar_initialize(&sd.done_cv);
182
183 const int ret = hcd_send_batch(hcd, target, dir, data, size, setup_data,
184 dir == USB_DIRECTION_IN ? transfer_in_cb : NULL,
185 dir == USB_DIRECTION_OUT ? transfer_out_cb : NULL, &sd, name);
186 if (ret != EOK)
187 return ret;
188
189 fibril_mutex_lock(&sd.done_mtx);
190 while (!sd.done)
191 fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
192 fibril_mutex_unlock(&sd.done_mtx);
193
194 if (sd.ret == EOK)
195 return sd.size;
196 return sd.ret;
197}
198
199
200/**
201 * @}
202 */
Note: See TracBrowser for help on using the repository browser.