source: mainline/uspace/drv/uhci-hcd/iface.c@ 2cc6e97

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2cc6e97 was 2cc6e97, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

Move more functionality to libUSB usb_transfer_batch_t

UHCI uses one device accessible buffer for both structures and data

  • Property mode set to 100644
File size: 12.4 KB
RevLine 
[4317827]1/*
[c56dbe0]2 * Copyright (c) 2011 Vojtech Horky, Jan Vesely
[4317827]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 */
[17ceb72]28/** @addtogroup drvusbuhcihc
[c56dbe0]29 * @{
30 */
31/** @file
[17ceb72]32 * @brief UHCI driver hc interface implementation
[c56dbe0]33 */
[eb1a2f4]34#include <ddf/driver.h>
[1c6a45f]35#include <errno.h>
[c56dbe0]36
[1669a73]37#include <usb/debug.h>
[8dc762e0]38#include <usb/host/endpoint.h>
[1669a73]39
[3515533]40#include "iface.h"
[c01cd32]41#include "hc.h"
[b4875e67]42
[2e6bbcf]43static inline int setup_batch(
44 ddf_fun_t *fun, usb_target_t target, usb_direction_t direction,
45 void *data, size_t size, void * setup_data, size_t setup_size,
46 usbhc_iface_transfer_in_callback_t in,
47 usbhc_iface_transfer_out_callback_t out, void *arg, const char* name,
48 hc_t **hc, usb_transfer_batch_t **batch)
49{
50 assert(hc);
51 assert(batch);
52 assert(fun);
53 *hc = fun_to_hc(fun);
54 assert(*hc);
55
56 size_t res_bw;
57 endpoint_t *ep = usb_endpoint_manager_get_ep(&(*hc)->ep_manager,
58 target.address, target.endpoint, direction, &res_bw);
59 if (ep == NULL) {
60 usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
61 target.address, target.endpoint, name);
62 return ENOENT;
63 }
64
[0ede0c3]65 usb_log_debug("%s %d:%d %zu(%zu).\n",
66 name, target.address, target.endpoint, size, ep->max_packet_size);
67
[2e6bbcf]68 const size_t bw = bandwidth_count_usb11(
69 ep->speed, ep->transfer_type, size, ep->max_packet_size);
70 if (res_bw < bw) {
71 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
72 "but only %zu is reserved.\n",
[86b4ee0]73 target.address, target.endpoint, name, bw, res_bw);
[2e6bbcf]74 return ENOSPC;
75 }
76
[a19a2d7]77 *batch = batch_get(
78 fun, ep, data, size, setup_data, setup_size, in, out, arg);
79 if (!*batch)
[fb8927d]80 return ENOMEM;
[86b39f7e]81 return EOK;
82}
83/*----------------------------------------------------------------------------*/
[a7e2f0d]84/** Request address interface function
85 *
86 * @param[in] fun DDF function that was called.
87 * @param[in] speed Speed to associate with the new default address.
88 * @param[out] address Place to write a new address.
89 * @return Error code.
90 */
[b9fa0a9]91static int request_address(
92 ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
[86b39f7e]93{
[eb1a2f4]94 assert(fun);
[c01cd32]95 hc_t *hc = fun_to_hc(fun);
[86b39f7e]96 assert(hc);
[1a93bb0]97 assert(address);
98
99 usb_log_debug("Address request with speed %d.\n", speed);
[62ed5bc]100 *address = device_keeper_get_free_address(&hc->manager, speed);
[1a93bb0]101 usb_log_debug("Address request with result: %d.\n", *address);
[86b39f7e]102 if (*address <= 0)
[1c6a45f]103 return *address;
[86b39f7e]104 return EOK;
105}
106/*----------------------------------------------------------------------------*/
[a7e2f0d]107/** Bind address interface function
108 *
109 * @param[in] fun DDF function that was called.
110 * @param[in] address Address of the device
111 * @param[in] handle Devman handle of the device driver.
112 * @return Error code.
113 */
[86b39f7e]114static int bind_address(
[eb1a2f4]115 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
[86b39f7e]116{
[eb1a2f4]117 assert(fun);
[c01cd32]118 hc_t *hc = fun_to_hc(fun);
[86b39f7e]119 assert(hc);
[1a93bb0]120 usb_log_debug("Address bind %d-%d.\n", address, handle);
[62ed5bc]121 usb_device_keeper_bind(&hc->manager, address, handle);
[86b39f7e]122 return EOK;
123}
124/*----------------------------------------------------------------------------*/
[a7e2f0d]125/** Release address interface function
126 *
127 * @param[in] fun DDF function that was called.
128 * @param[in] address USB address to be released.
129 * @return Error code.
130 */
[eb1a2f4]131static int release_address(ddf_fun_t *fun, usb_address_t address)
[86b39f7e]132{
[eb1a2f4]133 assert(fun);
[c01cd32]134 hc_t *hc = fun_to_hc(fun);
[86b39f7e]135 assert(hc);
[1a93bb0]136 usb_log_debug("Address release %d.\n", address);
[62ed5bc]137 usb_device_keeper_release(&hc->manager, address);
[86b39f7e]138 return EOK;
[4317827]139}
[3515533]140/*----------------------------------------------------------------------------*/
[8870230]141static int register_endpoint(
[1998bcd]142 ddf_fun_t *fun, usb_address_t address, usb_speed_t ep_speed,
143 usb_endpoint_t endpoint,
[8870230]144 usb_transfer_type_t transfer_type, usb_direction_t direction,
145 size_t max_packet_size, unsigned int interval)
146{
[a1313b8c]147 hc_t *hc = fun_to_hc(fun);
148 assert(hc);
[86b4ee0]149 const size_t size = max_packet_size;
[1998bcd]150 usb_speed_t speed = usb_device_keeper_get_speed(&hc->manager, address);
151 if (speed >= USB_SPEED_MAX) {
152 speed = ep_speed;
153 }
[86b4ee0]154 usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
155 address, endpoint, usb_str_transfer_type(transfer_type),
156 usb_str_speed(speed), direction, size, max_packet_size, interval);
157
[a81736d5]158 return usb_endpoint_manager_add_ep(&hc->ep_manager, address, endpoint,
159 direction, transfer_type, speed, max_packet_size, size);
[8870230]160}
161/*----------------------------------------------------------------------------*/
162static int unregister_endpoint(
163 ddf_fun_t *fun, usb_address_t address,
164 usb_endpoint_t endpoint, usb_direction_t direction)
165{
[a1313b8c]166 hc_t *hc = fun_to_hc(fun);
167 assert(hc);
168 usb_log_debug("Unregister endpoint %d:%d %d.\n",
169 address, endpoint, direction);
[6ce42e85]170 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
171 endpoint, direction);
[8870230]172}
173/*----------------------------------------------------------------------------*/
[a7e2f0d]174/** Interrupt out transaction interface function
175 *
176 * @param[in] fun DDF function that was called.
177 * @param[in] target USB device to write to.
178 * @param[in] data Source of data.
179 * @param[in] size Size of data source.
180 * @param[in] callback Function to call on transaction completion
181 * @param[in] arg Additional for callback function.
182 * @return Error code.
183 */
[b9fa0a9]184static int interrupt_out(
[7dfc06fa]185 ddf_fun_t *fun, usb_target_t target, void *data,
[b9fa0a9]186 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
[4317827]187{
[2e6bbcf]188 usb_transfer_batch_t *batch = NULL;
189 hc_t *hc = NULL;
190 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
191 NULL, 0, NULL, callback, arg, "Interrupt OUT", &hc, &batch);
192 if (ret != EOK)
193 return ret;
[5620bd4]194 batch_interrupt_out(batch);
[2e6bbcf]195 ret = hc_schedule(hc, batch);
[3bd96bb]196 if (ret != EOK) {
[2cc6e97]197 usb_transfer_batch_dispose(batch);
[3bd96bb]198 }
[b9fa0a9]199 return ret;
[4317827]200}
[3515533]201/*----------------------------------------------------------------------------*/
[a7e2f0d]202/** Interrupt in transaction interface function
203 *
204 * @param[in] fun DDF function that was called.
205 * @param[in] target USB device to write to.
206 * @param[out] data Data destination.
207 * @param[in] size Size of data source.
208 * @param[in] callback Function to call on transaction completion
209 * @param[in] arg Additional for callback function.
210 * @return Error code.
211 */
[b9fa0a9]212static int interrupt_in(
[7dfc06fa]213 ddf_fun_t *fun, usb_target_t target, void *data,
[b9fa0a9]214 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
[4317827]215{
[2e6bbcf]216 usb_transfer_batch_t *batch = NULL;
217 hc_t *hc = NULL;
218 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
219 NULL, 0, callback, NULL, arg, "Interrupt IN", &hc, &batch);
220 if (ret != EOK)
221 return ret;
[5620bd4]222 batch_interrupt_in(batch);
[2e6bbcf]223 ret = hc_schedule(hc, batch);
[3bd96bb]224 if (ret != EOK) {
[2cc6e97]225 usb_transfer_batch_dispose(batch);
[3bd96bb]226 }
[b9fa0a9]227 return ret;
[4317827]228}
[3515533]229/*----------------------------------------------------------------------------*/
[a7e2f0d]230/** Bulk out transaction interface function
231 *
232 * @param[in] fun DDF function that was called.
233 * @param[in] target USB device to write to.
234 * @param[in] data Source of data.
235 * @param[in] size Size of data source.
236 * @param[in] callback Function to call on transaction completion
237 * @param[in] arg Additional for callback function.
238 * @return Error code.
239 */
[b9fa0a9]240static int bulk_out(
[7dfc06fa]241 ddf_fun_t *fun, usb_target_t target, void *data,
[b9fa0a9]242 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
[0e06a14]243{
[2e6bbcf]244 usb_transfer_batch_t *batch = NULL;
245 hc_t *hc = NULL;
246 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
247 NULL, 0, NULL, callback, arg, "Bulk OUT", &hc, &batch);
248 if (ret != EOK)
249 return ret;
[5620bd4]250 batch_bulk_out(batch);
[2e6bbcf]251 ret = hc_schedule(hc, batch);
[3bd96bb]252 if (ret != EOK) {
[2cc6e97]253 usb_transfer_batch_dispose(batch);
[3bd96bb]254 }
[b9fa0a9]255 return ret;
[0e06a14]256}
257/*----------------------------------------------------------------------------*/
[a7e2f0d]258/** Bulk in transaction interface function
259 *
260 * @param[in] fun DDF function that was called.
261 * @param[in] target USB device to write to.
262 * @param[out] data Data destination.
263 * @param[in] size Size of data source.
264 * @param[in] callback Function to call on transaction completion
265 * @param[in] arg Additional for callback function.
266 * @return Error code.
267 */
[b9fa0a9]268static int bulk_in(
[7dfc06fa]269 ddf_fun_t *fun, usb_target_t target, void *data,
[b9fa0a9]270 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
[0e06a14]271{
[2e6bbcf]272 usb_transfer_batch_t *batch = NULL;
273 hc_t *hc = NULL;
274 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
275 NULL, 0, callback, NULL, arg, "Bulk IN", &hc, &batch);
276 if (ret != EOK)
277 return ret;
[5620bd4]278 batch_bulk_in(batch);
[2e6bbcf]279 ret = hc_schedule(hc, batch);
[3bd96bb]280 if (ret != EOK) {
[2cc6e97]281 usb_transfer_batch_dispose(batch);
[3bd96bb]282 }
[b9fa0a9]283 return ret;
[0e06a14]284}
285/*----------------------------------------------------------------------------*/
[a7e2f0d]286/** Control write transaction interface function
287 *
288 * @param[in] fun DDF function that was called.
289 * @param[in] target USB device to write to.
[c61338a]290 * @param[in] setup_data Data to send with SETUP transfer.
291 * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
[a7e2f0d]292 * @param[in] data Source of data.
293 * @param[in] size Size of data source.
294 * @param[in] callback Function to call on transaction completion.
295 * @param[in] arg Additional for callback function.
296 * @return Error code.
297 */
[b9fa0a9]298static int control_write(
[7dfc06fa]299 ddf_fun_t *fun, usb_target_t target,
[a72620d]300 void *setup_data, size_t setup_size, void *data, size_t size,
301 usbhc_iface_transfer_out_callback_t callback, void *arg)
302{
[2e6bbcf]303 usb_transfer_batch_t *batch = NULL;
304 hc_t *hc = NULL;
305 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
306 setup_data, setup_size, NULL, callback, arg, "Control WRITE",
307 &hc, &batch);
308 if (ret != EOK)
309 return ret;
[ba038f4]310 usb_endpoint_manager_reset_if_need(&hc->ep_manager, target, setup_data);
[83c439c]311 batch_control_write(batch);
[2e6bbcf]312 ret = hc_schedule(hc, batch);
[3bd96bb]313 if (ret != EOK) {
[2cc6e97]314 usb_transfer_batch_dispose(batch);
[3bd96bb]315 }
[b9fa0a9]316 return ret;
[a72620d]317}
318/*----------------------------------------------------------------------------*/
[a7e2f0d]319/** Control read transaction interface function
320 *
321 * @param[in] fun DDF function that was called.
322 * @param[in] target USB device to write to.
323 * @param[in] setup_data Data to send with SETUP packet.
324 * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
325 * @param[out] data Source of data.
326 * @param[in] size Size of data source.
327 * @param[in] callback Function to call on transaction completion.
328 * @param[in] arg Additional for callback function.
329 * @return Error code.
330 */
[b9fa0a9]331static int control_read(
[7dfc06fa]332 ddf_fun_t *fun, usb_target_t target,
[a72620d]333 void *setup_data, size_t setup_size, void *data, size_t size,
334 usbhc_iface_transfer_in_callback_t callback, void *arg)
335{
[2e6bbcf]336 usb_transfer_batch_t *batch = NULL;
337 hc_t *hc = NULL;
338 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
339 setup_data, setup_size, callback, NULL, arg, "Control READ",
340 &hc, &batch);
341 if (ret != EOK)
342 return ret;
[83c439c]343 batch_control_read(batch);
[2e6bbcf]344 ret = hc_schedule(hc, batch);
[3bd96bb]345 if (ret != EOK) {
[2cc6e97]346 usb_transfer_batch_dispose(batch);
[3bd96bb]347 }
[b9fa0a9]348 return ret;
[a72620d]349}
[9e80904]350/*----------------------------------------------------------------------------*/
[c01cd32]351usbhc_iface_t hc_iface = {
[86b39f7e]352 .request_address = request_address,
353 .bind_address = bind_address,
354 .release_address = release_address,
[3515533]355
[8870230]356 .register_endpoint = register_endpoint,
357 .unregister_endpoint = unregister_endpoint,
358
[4317827]359 .interrupt_out = interrupt_out,
360 .interrupt_in = interrupt_in,
[3515533]361
[0e06a14]362 .bulk_out = bulk_out,
[1c6a45f]363 .bulk_in = bulk_in,
[0e06a14]364
[a72620d]365 .control_write = control_write,
[1c6a45f]366 .control_read = control_read,
[4317827]367};
[c56dbe0]368/**
369 * @}
370 */
Note: See TracBrowser for help on using the repository browser.