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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cfe4852 was a1f83a3, checked in by Aearsis <Hlavaty.Ondrej@…>, 8 years ago

usbhost: remove dead code

  • Property mode set to 100644
File size: 5.6 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
62/** Prepare generic usb_transfer_batch and schedule it.
63 * @param hcd Host controller driver.
64 * @param target address and endpoint number.
65 * @param setup_data Data to use in setup stage (Control communication type)
66 * @param in Callback for device to host communication.
67 * @param out Callback for host to device communication.
68 * @param arg Callback parameter.
69 * @param name Communication identifier (for nicer output).
70 * @return Error code.
71 */
72int hcd_send_batch(hcd_t *hcd, device_t *device, usb_target_t target,
73 usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
74 usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
75{
76 assert(hcd);
77 assert(device->address == target.address);
78
79 if (!hcd->ops.schedule) {
80 usb_log_error("HCD does not implement scheduler.\n");
81 return ENOTSUP;
82 }
83
84 endpoint_t *ep = bus_find_endpoint(hcd->bus, device, target, direction);
85 if (ep == NULL) {
86 usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
87 device->address, target.endpoint, name);
88 return ENOENT;
89 }
90
91 // TODO cut here aka provide helper to call with instance of endpoint_t in hand
92
93 usb_log_debug2("%s %d:%d %zu(%zu).\n",
94 name, target.address, target.endpoint, size, ep->max_packet_size);
95
96 const size_t bw = bus_count_bw(ep, size);
97 /* Check if we have enough bandwidth reserved */
98 if (ep->bandwidth < bw) {
99 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
100 "but only %zu is reserved.\n",
101 device->address, ep->endpoint, name, bw, ep->bandwidth);
102 return ENOSPC;
103 }
104
105 usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
106 if (!batch) {
107 usb_log_error("Failed to create transfer batch.\n");
108 return ENOMEM;
109 }
110
111 batch->target = target;
112 batch->buffer = data;
113 batch->buffer_size = size;
114 batch->setup.packed = setup_data;
115 batch->dir = direction;
116 batch->on_complete = on_complete;
117 batch->on_complete_data = arg;
118
119 /* Check for commands that reset toggle bit */
120 if (ep->transfer_type == USB_TRANSFER_CONTROL)
121 batch->toggle_reset_mode
122 = usb_request_get_toggle_reset_mode(&batch->setup.packet);
123
124 const int ret = hcd->ops.schedule(hcd, batch);
125 if (ret != EOK) {
126 usb_log_warning("Batch %p failed to schedule: %s", batch, str_error(ret));
127 usb_transfer_batch_destroy(batch);
128 }
129
130 /* Drop our own reference to ep. */
131 endpoint_del_ref(ep);
132
133 return ret;
134}
135
136typedef struct {
137 fibril_mutex_t done_mtx;
138 fibril_condvar_t done_cv;
139 unsigned done;
140
141 size_t transfered_size;
142 int error;
143} sync_data_t;
144
145static int sync_transfer_complete(void *arg, int error, size_t transfered_size)
146{
147 sync_data_t *d = arg;
148 assert(d);
149 d->transfered_size = transfered_size;
150 d->error = error;
151 fibril_mutex_lock(&d->done_mtx);
152 d->done = 1;
153 fibril_condvar_broadcast(&d->done_cv);
154 fibril_mutex_unlock(&d->done_mtx);
155 return EOK;
156}
157
158ssize_t hcd_send_batch_sync(hcd_t *hcd, device_t *device, usb_target_t target,
159 usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
160 const char *name)
161{
162 assert(hcd);
163 sync_data_t sd = { .done = 0 };
164 fibril_mutex_initialize(&sd.done_mtx);
165 fibril_condvar_initialize(&sd.done_cv);
166
167 const int ret = hcd_send_batch(hcd, device, target, direction,
168 data, size, setup_data,
169 sync_transfer_complete, &sd, name);
170 if (ret != EOK)
171 return ret;
172
173 fibril_mutex_lock(&sd.done_mtx);
174 while (!sd.done)
175 fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
176 fibril_mutex_unlock(&sd.done_mtx);
177
178 return (sd.error == EOK)
179 ? (ssize_t) sd.transfered_size
180 : (ssize_t) sd.error;
181}
182
183
184/**
185 * @}
186 */
Note: See TracBrowser for help on using the repository browser.