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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c7b5826 was df25ab6, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

All HC drivers supports "get handle by address"

  • Property mode set to 100644
File size: 12.9 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);
[4125b7d]120 usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
[62ed5bc]121 usb_device_keeper_bind(&hc->manager, address, handle);
[86b39f7e]122 return EOK;
123}
[df25ab6]124
125/** Find device handle by address interface function.
126 *
127 * @param[in] fun DDF function that was called.
128 * @param[in] address Address in question.
129 * @param[out] handle Where to store device handle if found.
130 * @return Error code.
131 */
132static int find_by_address(ddf_fun_t *fun, usb_address_t address,
133 devman_handle_t *handle)
134{
135 assert(fun);
136 hc_t *hc = fun_to_hc(fun);
137 assert(hc);
138 bool found =
139 usb_device_keeper_find_by_address(&hc->manager, address, handle);
140 return found ? EOK : ENOENT;
141}
142
[86b39f7e]143/*----------------------------------------------------------------------------*/
[a7e2f0d]144/** Release address interface function
145 *
146 * @param[in] fun DDF function that was called.
147 * @param[in] address USB address to be released.
148 * @return Error code.
149 */
[eb1a2f4]150static int release_address(ddf_fun_t *fun, usb_address_t address)
[86b39f7e]151{
[eb1a2f4]152 assert(fun);
[c01cd32]153 hc_t *hc = fun_to_hc(fun);
[86b39f7e]154 assert(hc);
[1a93bb0]155 usb_log_debug("Address release %d.\n", address);
[62ed5bc]156 usb_device_keeper_release(&hc->manager, address);
[86b39f7e]157 return EOK;
[4317827]158}
[3515533]159/*----------------------------------------------------------------------------*/
[8870230]160static int register_endpoint(
[1998bcd]161 ddf_fun_t *fun, usb_address_t address, usb_speed_t ep_speed,
162 usb_endpoint_t endpoint,
[8870230]163 usb_transfer_type_t transfer_type, usb_direction_t direction,
164 size_t max_packet_size, unsigned int interval)
165{
[a1313b8c]166 hc_t *hc = fun_to_hc(fun);
167 assert(hc);
[86b4ee0]168 const size_t size = max_packet_size;
[1998bcd]169 usb_speed_t speed = usb_device_keeper_get_speed(&hc->manager, address);
170 if (speed >= USB_SPEED_MAX) {
171 speed = ep_speed;
172 }
[86b4ee0]173 usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
174 address, endpoint, usb_str_transfer_type(transfer_type),
175 usb_str_speed(speed), direction, size, max_packet_size, interval);
176
[a81736d5]177 return usb_endpoint_manager_add_ep(&hc->ep_manager, address, endpoint,
178 direction, transfer_type, speed, max_packet_size, size);
[8870230]179}
180/*----------------------------------------------------------------------------*/
181static int unregister_endpoint(
182 ddf_fun_t *fun, usb_address_t address,
183 usb_endpoint_t endpoint, usb_direction_t direction)
184{
[a1313b8c]185 hc_t *hc = fun_to_hc(fun);
186 assert(hc);
187 usb_log_debug("Unregister endpoint %d:%d %d.\n",
188 address, endpoint, direction);
[6ce42e85]189 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
190 endpoint, direction);
[8870230]191}
192/*----------------------------------------------------------------------------*/
[a7e2f0d]193/** Interrupt out transaction interface function
194 *
195 * @param[in] fun DDF function that was called.
196 * @param[in] target USB device to write to.
197 * @param[in] data Source of data.
198 * @param[in] size Size of data source.
199 * @param[in] callback Function to call on transaction completion
200 * @param[in] arg Additional for callback function.
201 * @return Error code.
202 */
[b9fa0a9]203static int interrupt_out(
[7dfc06fa]204 ddf_fun_t *fun, usb_target_t target, void *data,
[b9fa0a9]205 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
[4317827]206{
[2e6bbcf]207 usb_transfer_batch_t *batch = NULL;
208 hc_t *hc = NULL;
209 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
210 NULL, 0, NULL, callback, arg, "Interrupt OUT", &hc, &batch);
211 if (ret != EOK)
212 return ret;
[5620bd4]213 batch_interrupt_out(batch);
[2e6bbcf]214 ret = hc_schedule(hc, batch);
[3bd96bb]215 if (ret != EOK) {
[2cc6e97]216 usb_transfer_batch_dispose(batch);
[3bd96bb]217 }
[b9fa0a9]218 return ret;
[4317827]219}
[3515533]220/*----------------------------------------------------------------------------*/
[a7e2f0d]221/** Interrupt in transaction interface function
222 *
223 * @param[in] fun DDF function that was called.
224 * @param[in] target USB device to write to.
225 * @param[out] data Data destination.
226 * @param[in] size Size of data source.
227 * @param[in] callback Function to call on transaction completion
228 * @param[in] arg Additional for callback function.
229 * @return Error code.
230 */
[b9fa0a9]231static int interrupt_in(
[7dfc06fa]232 ddf_fun_t *fun, usb_target_t target, void *data,
[b9fa0a9]233 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
[4317827]234{
[2e6bbcf]235 usb_transfer_batch_t *batch = NULL;
236 hc_t *hc = NULL;
237 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
238 NULL, 0, callback, NULL, arg, "Interrupt IN", &hc, &batch);
239 if (ret != EOK)
240 return ret;
[5620bd4]241 batch_interrupt_in(batch);
[2e6bbcf]242 ret = hc_schedule(hc, batch);
[3bd96bb]243 if (ret != EOK) {
[2cc6e97]244 usb_transfer_batch_dispose(batch);
[3bd96bb]245 }
[b9fa0a9]246 return ret;
[4317827]247}
[3515533]248/*----------------------------------------------------------------------------*/
[a7e2f0d]249/** Bulk out transaction interface function
250 *
251 * @param[in] fun DDF function that was called.
252 * @param[in] target USB device to write to.
253 * @param[in] data Source of data.
254 * @param[in] size Size of data source.
255 * @param[in] callback Function to call on transaction completion
256 * @param[in] arg Additional for callback function.
257 * @return Error code.
258 */
[b9fa0a9]259static int bulk_out(
[7dfc06fa]260 ddf_fun_t *fun, usb_target_t target, void *data,
[b9fa0a9]261 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
[0e06a14]262{
[2e6bbcf]263 usb_transfer_batch_t *batch = NULL;
264 hc_t *hc = NULL;
265 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
266 NULL, 0, NULL, callback, arg, "Bulk OUT", &hc, &batch);
267 if (ret != EOK)
268 return ret;
[5620bd4]269 batch_bulk_out(batch);
[2e6bbcf]270 ret = hc_schedule(hc, batch);
[3bd96bb]271 if (ret != EOK) {
[2cc6e97]272 usb_transfer_batch_dispose(batch);
[3bd96bb]273 }
[b9fa0a9]274 return ret;
[0e06a14]275}
276/*----------------------------------------------------------------------------*/
[a7e2f0d]277/** Bulk in transaction interface function
278 *
279 * @param[in] fun DDF function that was called.
280 * @param[in] target USB device to write to.
281 * @param[out] data Data destination.
282 * @param[in] size Size of data source.
283 * @param[in] callback Function to call on transaction completion
284 * @param[in] arg Additional for callback function.
285 * @return Error code.
286 */
[b9fa0a9]287static int bulk_in(
[7dfc06fa]288 ddf_fun_t *fun, usb_target_t target, void *data,
[b9fa0a9]289 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
[0e06a14]290{
[2e6bbcf]291 usb_transfer_batch_t *batch = NULL;
292 hc_t *hc = NULL;
293 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
294 NULL, 0, callback, NULL, arg, "Bulk IN", &hc, &batch);
295 if (ret != EOK)
296 return ret;
[5620bd4]297 batch_bulk_in(batch);
[2e6bbcf]298 ret = hc_schedule(hc, batch);
[3bd96bb]299 if (ret != EOK) {
[2cc6e97]300 usb_transfer_batch_dispose(batch);
[3bd96bb]301 }
[b9fa0a9]302 return ret;
[0e06a14]303}
304/*----------------------------------------------------------------------------*/
[a7e2f0d]305/** Control write transaction interface function
306 *
307 * @param[in] fun DDF function that was called.
308 * @param[in] target USB device to write to.
[c61338a]309 * @param[in] setup_data Data to send with SETUP transfer.
310 * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
[a7e2f0d]311 * @param[in] data Source of data.
312 * @param[in] size Size of data source.
313 * @param[in] callback Function to call on transaction completion.
314 * @param[in] arg Additional for callback function.
315 * @return Error code.
316 */
[b9fa0a9]317static int control_write(
[7dfc06fa]318 ddf_fun_t *fun, usb_target_t target,
[a72620d]319 void *setup_data, size_t setup_size, void *data, size_t size,
320 usbhc_iface_transfer_out_callback_t callback, void *arg)
321{
[2e6bbcf]322 usb_transfer_batch_t *batch = NULL;
323 hc_t *hc = NULL;
324 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
325 setup_data, setup_size, NULL, callback, arg, "Control WRITE",
326 &hc, &batch);
327 if (ret != EOK)
328 return ret;
[ba038f4]329 usb_endpoint_manager_reset_if_need(&hc->ep_manager, target, setup_data);
[83c439c]330 batch_control_write(batch);
[2e6bbcf]331 ret = hc_schedule(hc, batch);
[3bd96bb]332 if (ret != EOK) {
[2cc6e97]333 usb_transfer_batch_dispose(batch);
[3bd96bb]334 }
[b9fa0a9]335 return ret;
[a72620d]336}
337/*----------------------------------------------------------------------------*/
[a7e2f0d]338/** Control read transaction interface function
339 *
340 * @param[in] fun DDF function that was called.
341 * @param[in] target USB device to write to.
342 * @param[in] setup_data Data to send with SETUP packet.
343 * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
344 * @param[out] data Source of data.
345 * @param[in] size Size of data source.
346 * @param[in] callback Function to call on transaction completion.
347 * @param[in] arg Additional for callback function.
348 * @return Error code.
349 */
[b9fa0a9]350static int control_read(
[7dfc06fa]351 ddf_fun_t *fun, usb_target_t target,
[a72620d]352 void *setup_data, size_t setup_size, void *data, size_t size,
353 usbhc_iface_transfer_in_callback_t callback, void *arg)
354{
[2e6bbcf]355 usb_transfer_batch_t *batch = NULL;
356 hc_t *hc = NULL;
357 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
358 setup_data, setup_size, callback, NULL, arg, "Control READ",
359 &hc, &batch);
360 if (ret != EOK)
361 return ret;
[83c439c]362 batch_control_read(batch);
[2e6bbcf]363 ret = hc_schedule(hc, batch);
[3bd96bb]364 if (ret != EOK) {
[2cc6e97]365 usb_transfer_batch_dispose(batch);
[3bd96bb]366 }
[b9fa0a9]367 return ret;
[a72620d]368}
[9e80904]369/*----------------------------------------------------------------------------*/
[c01cd32]370usbhc_iface_t hc_iface = {
[86b39f7e]371 .request_address = request_address,
372 .bind_address = bind_address,
[df25ab6]373 .find_by_address = find_by_address,
[86b39f7e]374 .release_address = release_address,
[3515533]375
[8870230]376 .register_endpoint = register_endpoint,
377 .unregister_endpoint = unregister_endpoint,
378
[4317827]379 .interrupt_out = interrupt_out,
380 .interrupt_in = interrupt_in,
[3515533]381
[0e06a14]382 .bulk_out = bulk_out,
[1c6a45f]383 .bulk_in = bulk_in,
[0e06a14]384
[a72620d]385 .control_write = control_write,
[1c6a45f]386 .control_read = control_read,
[4317827]387};
[c56dbe0]388/**
389 * @}
390 */
Note: See TracBrowser for help on using the repository browser.