source: mainline/uspace/drv/uhci_hcd/iface.c@ 4a4c8bcf

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4a4c8bcf was 8d6c1f1, checked in by Jakub Jermar <jakub@…>, 15 years ago

Merge USB support.

Changes from bzr://helenos-usb.bzr.sourceforge.net/bzrroot/helenos-usb/mainline:

  • replaced '-' with '_' in new driver names
  • USB libs are built for each architecture
  • devman starts early
  • sys_thread_udelay() uses generic delay()
  • sys_as_create_area() now creates cacheable areas by default
  • Property mode set to 100644
File size: 13.2 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"
[563ead9]41#include "batch.h"
[c01cd32]42#include "hc.h"
[b4875e67]43
[2e6bbcf]44static inline int setup_batch(
45 ddf_fun_t *fun, usb_target_t target, usb_direction_t direction,
46 void *data, size_t size, void * setup_data, size_t setup_size,
47 usbhc_iface_transfer_in_callback_t in,
48 usbhc_iface_transfer_out_callback_t out, void *arg, const char* name,
49 hc_t **hc, usb_transfer_batch_t **batch)
50{
51 assert(hc);
52 assert(batch);
53 assert(fun);
54 *hc = fun_to_hc(fun);
55 assert(*hc);
56
57 size_t res_bw;
58 endpoint_t *ep = usb_endpoint_manager_get_ep(&(*hc)->ep_manager,
59 target.address, target.endpoint, direction, &res_bw);
60 if (ep == NULL) {
61 usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
62 target.address, target.endpoint, name);
63 return ENOENT;
64 }
65
[0ede0c3]66 usb_log_debug("%s %d:%d %zu(%zu).\n",
67 name, target.address, target.endpoint, size, ep->max_packet_size);
68
[2e6bbcf]69 const size_t bw = bandwidth_count_usb11(
70 ep->speed, ep->transfer_type, size, ep->max_packet_size);
71 if (res_bw < bw) {
72 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
73 "but only %zu is reserved.\n",
[86b4ee0]74 target.address, target.endpoint, name, bw, res_bw);
[2e6bbcf]75 return ENOSPC;
76 }
77
[a19a2d7]78 *batch = batch_get(
79 fun, ep, data, size, setup_data, setup_size, in, out, arg);
80 if (!*batch)
[fb8927d]81 return ENOMEM;
[86b39f7e]82 return EOK;
83}
84/*----------------------------------------------------------------------------*/
[a7e2f0d]85/** Request address interface function
86 *
87 * @param[in] fun DDF function that was called.
88 * @param[in] speed Speed to associate with the new default address.
89 * @param[out] address Place to write a new address.
90 * @return Error code.
91 */
[b9fa0a9]92static int request_address(
93 ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
[86b39f7e]94{
[eb1a2f4]95 assert(fun);
[c01cd32]96 hc_t *hc = fun_to_hc(fun);
[86b39f7e]97 assert(hc);
[1a93bb0]98 assert(address);
99
100 usb_log_debug("Address request with speed %d.\n", speed);
[62ed5bc]101 *address = device_keeper_get_free_address(&hc->manager, speed);
[1a93bb0]102 usb_log_debug("Address request with result: %d.\n", *address);
[86b39f7e]103 if (*address <= 0)
[1c6a45f]104 return *address;
[86b39f7e]105 return EOK;
106}
107/*----------------------------------------------------------------------------*/
[a7e2f0d]108/** Bind address interface function
109 *
110 * @param[in] fun DDF function that was called.
111 * @param[in] address Address of the device
112 * @param[in] handle Devman handle of the device driver.
113 * @return Error code.
114 */
[86b39f7e]115static int bind_address(
[eb1a2f4]116 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
[86b39f7e]117{
[eb1a2f4]118 assert(fun);
[c01cd32]119 hc_t *hc = fun_to_hc(fun);
[86b39f7e]120 assert(hc);
[4125b7d]121 usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
[62ed5bc]122 usb_device_keeper_bind(&hc->manager, address, handle);
[86b39f7e]123 return EOK;
124}
[563ead9]125/*----------------------------------------------------------------------------*/
[df25ab6]126/** Find device handle by address interface function.
127 *
128 * @param[in] fun DDF function that was called.
129 * @param[in] address Address in question.
130 * @param[out] handle Where to store device handle if found.
131 * @return Error code.
132 */
133static int find_by_address(ddf_fun_t *fun, usb_address_t address,
134 devman_handle_t *handle)
135{
136 assert(fun);
137 hc_t *hc = fun_to_hc(fun);
138 assert(hc);
[563ead9]139 const bool found =
[df25ab6]140 usb_device_keeper_find_by_address(&hc->manager, address, handle);
141 return found ? EOK : ENOENT;
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{
[563ead9]166 assert(fun);
[a1313b8c]167 hc_t *hc = fun_to_hc(fun);
168 assert(hc);
[86b4ee0]169 const size_t size = max_packet_size;
[1998bcd]170 usb_speed_t speed = usb_device_keeper_get_speed(&hc->manager, address);
171 if (speed >= USB_SPEED_MAX) {
172 speed = ep_speed;
173 }
[86b4ee0]174 usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
175 address, endpoint, usb_str_transfer_type(transfer_type),
176 usb_str_speed(speed), direction, size, max_packet_size, interval);
177
[a81736d5]178 return usb_endpoint_manager_add_ep(&hc->ep_manager, address, endpoint,
179 direction, transfer_type, speed, max_packet_size, size);
[8870230]180}
181/*----------------------------------------------------------------------------*/
182static int unregister_endpoint(
183 ddf_fun_t *fun, usb_address_t address,
184 usb_endpoint_t endpoint, usb_direction_t direction)
185{
[563ead9]186 assert(fun);
[a1313b8c]187 hc_t *hc = fun_to_hc(fun);
188 assert(hc);
189 usb_log_debug("Unregister endpoint %d:%d %d.\n",
190 address, endpoint, direction);
[6ce42e85]191 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
192 endpoint, direction);
[8870230]193}
194/*----------------------------------------------------------------------------*/
[a7e2f0d]195/** Interrupt out transaction interface function
196 *
197 * @param[in] fun DDF function that was called.
198 * @param[in] target USB device to write to.
199 * @param[in] data Source of data.
200 * @param[in] size Size of data source.
201 * @param[in] callback Function to call on transaction completion
202 * @param[in] arg Additional for callback function.
203 * @return Error code.
204 */
[b9fa0a9]205static int interrupt_out(
[7dfc06fa]206 ddf_fun_t *fun, usb_target_t target, void *data,
[b9fa0a9]207 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
[4317827]208{
[2e6bbcf]209 usb_transfer_batch_t *batch = NULL;
210 hc_t *hc = NULL;
211 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
212 NULL, 0, NULL, callback, arg, "Interrupt OUT", &hc, &batch);
213 if (ret != EOK)
214 return ret;
[563ead9]215 assert(batch);
216 assert(hc);
[5620bd4]217 batch_interrupt_out(batch);
[2e6bbcf]218 ret = hc_schedule(hc, batch);
[3bd96bb]219 if (ret != EOK) {
[2cc6e97]220 usb_transfer_batch_dispose(batch);
[3bd96bb]221 }
[b9fa0a9]222 return ret;
[4317827]223}
[3515533]224/*----------------------------------------------------------------------------*/
[a7e2f0d]225/** Interrupt in transaction interface function
226 *
227 * @param[in] fun DDF function that was called.
228 * @param[in] target USB device to write to.
229 * @param[out] data Data destination.
230 * @param[in] size Size of data source.
231 * @param[in] callback Function to call on transaction completion
232 * @param[in] arg Additional for callback function.
233 * @return Error code.
234 */
[b9fa0a9]235static int interrupt_in(
[7dfc06fa]236 ddf_fun_t *fun, usb_target_t target, void *data,
[b9fa0a9]237 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
[4317827]238{
[2e6bbcf]239 usb_transfer_batch_t *batch = NULL;
240 hc_t *hc = NULL;
241 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
242 NULL, 0, callback, NULL, arg, "Interrupt IN", &hc, &batch);
243 if (ret != EOK)
244 return ret;
[563ead9]245 assert(batch);
246 assert(hc);
[5620bd4]247 batch_interrupt_in(batch);
[2e6bbcf]248 ret = hc_schedule(hc, batch);
[3bd96bb]249 if (ret != EOK) {
[2cc6e97]250 usb_transfer_batch_dispose(batch);
[3bd96bb]251 }
[b9fa0a9]252 return ret;
[4317827]253}
[3515533]254/*----------------------------------------------------------------------------*/
[a7e2f0d]255/** Bulk out transaction interface function
256 *
257 * @param[in] fun DDF function that was called.
258 * @param[in] target USB device to write to.
259 * @param[in] data Source of data.
260 * @param[in] size Size of data source.
261 * @param[in] callback Function to call on transaction completion
262 * @param[in] arg Additional for callback function.
263 * @return Error code.
264 */
[b9fa0a9]265static int bulk_out(
[7dfc06fa]266 ddf_fun_t *fun, usb_target_t target, void *data,
[b9fa0a9]267 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
[0e06a14]268{
[2e6bbcf]269 usb_transfer_batch_t *batch = NULL;
270 hc_t *hc = NULL;
271 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
272 NULL, 0, NULL, callback, arg, "Bulk OUT", &hc, &batch);
273 if (ret != EOK)
274 return ret;
[563ead9]275 assert(batch);
276 assert(hc);
[5620bd4]277 batch_bulk_out(batch);
[2e6bbcf]278 ret = hc_schedule(hc, batch);
[3bd96bb]279 if (ret != EOK) {
[2cc6e97]280 usb_transfer_batch_dispose(batch);
[3bd96bb]281 }
[b9fa0a9]282 return ret;
[0e06a14]283}
284/*----------------------------------------------------------------------------*/
[a7e2f0d]285/** Bulk in transaction interface function
286 *
287 * @param[in] fun DDF function that was called.
288 * @param[in] target USB device to write to.
289 * @param[out] data Data destination.
290 * @param[in] size Size of data source.
291 * @param[in] callback Function to call on transaction completion
292 * @param[in] arg Additional for callback function.
293 * @return Error code.
294 */
[b9fa0a9]295static int bulk_in(
[7dfc06fa]296 ddf_fun_t *fun, usb_target_t target, void *data,
[b9fa0a9]297 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
[0e06a14]298{
[2e6bbcf]299 usb_transfer_batch_t *batch = NULL;
300 hc_t *hc = NULL;
301 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
302 NULL, 0, callback, NULL, arg, "Bulk IN", &hc, &batch);
303 if (ret != EOK)
304 return ret;
[563ead9]305 assert(batch);
306 assert(hc);
[5620bd4]307 batch_bulk_in(batch);
[2e6bbcf]308 ret = hc_schedule(hc, batch);
[3bd96bb]309 if (ret != EOK) {
[2cc6e97]310 usb_transfer_batch_dispose(batch);
[3bd96bb]311 }
[b9fa0a9]312 return ret;
[0e06a14]313}
314/*----------------------------------------------------------------------------*/
[a7e2f0d]315/** Control write transaction interface function
316 *
317 * @param[in] fun DDF function that was called.
318 * @param[in] target USB device to write to.
[c61338a]319 * @param[in] setup_data Data to send with SETUP transfer.
320 * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
[a7e2f0d]321 * @param[in] data Source of data.
322 * @param[in] size Size of data source.
323 * @param[in] callback Function to call on transaction completion.
324 * @param[in] arg Additional for callback function.
325 * @return Error code.
326 */
[b9fa0a9]327static int control_write(
[7dfc06fa]328 ddf_fun_t *fun, usb_target_t target,
[a72620d]329 void *setup_data, size_t setup_size, void *data, size_t size,
330 usbhc_iface_transfer_out_callback_t callback, void *arg)
331{
[2e6bbcf]332 usb_transfer_batch_t *batch = NULL;
333 hc_t *hc = NULL;
334 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
335 setup_data, setup_size, NULL, callback, arg, "Control WRITE",
336 &hc, &batch);
337 if (ret != EOK)
338 return ret;
[563ead9]339 assert(batch);
340 assert(hc);
[ba038f4]341 usb_endpoint_manager_reset_if_need(&hc->ep_manager, target, setup_data);
[83c439c]342 batch_control_write(batch);
[2e6bbcf]343 ret = hc_schedule(hc, batch);
[3bd96bb]344 if (ret != EOK) {
[2cc6e97]345 usb_transfer_batch_dispose(batch);
[3bd96bb]346 }
[b9fa0a9]347 return ret;
[a72620d]348}
349/*----------------------------------------------------------------------------*/
[a7e2f0d]350/** Control read transaction interface function
351 *
352 * @param[in] fun DDF function that was called.
353 * @param[in] target USB device to write to.
354 * @param[in] setup_data Data to send with SETUP packet.
355 * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
356 * @param[out] data Source of data.
357 * @param[in] size Size of data source.
358 * @param[in] callback Function to call on transaction completion.
359 * @param[in] arg Additional for callback function.
360 * @return Error code.
361 */
[b9fa0a9]362static int control_read(
[7dfc06fa]363 ddf_fun_t *fun, usb_target_t target,
[a72620d]364 void *setup_data, size_t setup_size, void *data, size_t size,
365 usbhc_iface_transfer_in_callback_t callback, void *arg)
366{
[2e6bbcf]367 usb_transfer_batch_t *batch = NULL;
368 hc_t *hc = NULL;
369 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
370 setup_data, setup_size, callback, NULL, arg, "Control READ",
371 &hc, &batch);
372 if (ret != EOK)
373 return ret;
[563ead9]374 assert(batch);
375 assert(hc);
[83c439c]376 batch_control_read(batch);
[2e6bbcf]377 ret = hc_schedule(hc, batch);
[3bd96bb]378 if (ret != EOK) {
[2cc6e97]379 usb_transfer_batch_dispose(batch);
[3bd96bb]380 }
[b9fa0a9]381 return ret;
[a72620d]382}
[9e80904]383/*----------------------------------------------------------------------------*/
[c01cd32]384usbhc_iface_t hc_iface = {
[86b39f7e]385 .request_address = request_address,
386 .bind_address = bind_address,
[df25ab6]387 .find_by_address = find_by_address,
[86b39f7e]388 .release_address = release_address,
[3515533]389
[8870230]390 .register_endpoint = register_endpoint,
391 .unregister_endpoint = unregister_endpoint,
392
[4317827]393 .interrupt_out = interrupt_out,
394 .interrupt_in = interrupt_in,
[3515533]395
[0e06a14]396 .bulk_out = bulk_out,
[1c6a45f]397 .bulk_in = bulk_in,
[0e06a14]398
[a72620d]399 .control_write = control_write,
[1c6a45f]400 .control_read = control_read,
[4317827]401};
[c56dbe0]402/**
403 * @}
404 */
Note: See TracBrowser for help on using the repository browser.