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

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

Merge development/ changes

  • Property mode set to 100644
File size: 14.4 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky, 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/** @addtogroup drvusbuhcihc
29 * @{
30 */
31/** @file
32 * @brief UHCI driver hc interface implementation
33 */
34#include <ddf/driver.h>
35#include <errno.h>
36
37#include <usb/debug.h>
38#include <usb/host/endpoint.h>
39
40#include "iface.h"
41#include "hc.h"
42
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
65 const size_t bw = bandwidth_count_usb11(
66 ep->speed, ep->transfer_type, size, ep->max_packet_size);
67 if (res_bw < bw) {
68 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
69 "but only %zu is reserved.\n",
70 name, target.address, target.endpoint, bw, res_bw);
71 return ENOSPC;
72 }
73 usb_log_debug("%s %d:%d %zu(%zu).\n",
74 name, target.address, target.endpoint, size, ep->max_packet_size);
75
76 assert(ep->speed ==
77 usb_device_keeper_get_speed(&(*hc)->manager, target.address));
78// assert(ep->max_packet_size == max_packet_size);
79// assert(ep->transfer_type == USB_TRANSFER_CONTROL);
80
81 *batch =
82 batch_get(fun, ep, data, size, setup_data, setup_size,
83 in, out, arg);
84 if (!batch)
85 return ENOMEM;
86 return EOK;
87}
88
89
90/** Reserve default address interface function
91 *
92 * @param[in] fun DDF function that was called.
93 * @param[in] speed Speed to associate with the new default address.
94 * @return Error code.
95 */
96static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
97{
98 assert(fun);
99 hc_t *hc = fun_to_hc(fun);
100 assert(hc);
101 usb_log_debug("Default address request with speed %d.\n", speed);
102 usb_device_keeper_reserve_default_address(&hc->manager, speed);
103 return EOK;
104#if 0
105 endpoint_t *ep = malloc(sizeof(endpoint_t));
106 if (ep == NULL)
107 return ENOMEM;
108 const size_t max_packet_size = speed == USB_SPEED_LOW ? 8 : 64;
109 endpoint_init(ep, USB_TRANSFER_CONTROL, speed, max_packet_size);
110 int ret;
111try_retgister:
112 ret = usb_endpoint_manager_register_ep(&hc->ep_manager,
113 USB_ADDRESS_DEFAULT, 0, USB_DIRECTION_BOTH, ep, endpoint_destroy, 0);
114 if (ret == EEXISTS) {
115 async_usleep(1000);
116 goto try_retgister;
117 }
118 if (ret != EOK) {
119 endpoint_destroy(ep);
120 }
121 return ret;
122#endif
123}
124/*----------------------------------------------------------------------------*/
125/** Release default address interface function
126 *
127 * @param[in] fun DDF function that was called.
128 * @return Error code.
129 */
130static int release_default_address(ddf_fun_t *fun)
131{
132 assert(fun);
133 hc_t *hc = fun_to_hc(fun);
134 assert(hc);
135 usb_log_debug("Default address release.\n");
136// return usb_endpoint_manager_unregister_ep(&hc->ep_manager,
137// USB_ADDRESS_DEFAULT, 0, USB_DIRECTION_BOTH);
138 usb_device_keeper_release_default_address(&hc->manager);
139 return EOK;
140}
141/*----------------------------------------------------------------------------*/
142/** Request address interface function
143 *
144 * @param[in] fun DDF function that was called.
145 * @param[in] speed Speed to associate with the new default address.
146 * @param[out] address Place to write a new address.
147 * @return Error code.
148 */
149static int request_address(
150 ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
151{
152 assert(fun);
153 hc_t *hc = fun_to_hc(fun);
154 assert(hc);
155 assert(address);
156
157 usb_log_debug("Address request with speed %d.\n", speed);
158 *address = device_keeper_get_free_address(&hc->manager, speed);
159 usb_log_debug("Address request with result: %d.\n", *address);
160 if (*address <= 0)
161 return *address;
162 return EOK;
163}
164/*----------------------------------------------------------------------------*/
165/** Bind address interface function
166 *
167 * @param[in] fun DDF function that was called.
168 * @param[in] address Address of the device
169 * @param[in] handle Devman handle of the device driver.
170 * @return Error code.
171 */
172static int bind_address(
173 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
174{
175 assert(fun);
176 hc_t *hc = fun_to_hc(fun);
177 assert(hc);
178 usb_log_debug("Address bind %d-%d.\n", address, handle);
179 usb_device_keeper_bind(&hc->manager, address, handle);
180 return EOK;
181}
182/*----------------------------------------------------------------------------*/
183/** Release address interface function
184 *
185 * @param[in] fun DDF function that was called.
186 * @param[in] address USB address to be released.
187 * @return Error code.
188 */
189static int release_address(ddf_fun_t *fun, usb_address_t address)
190{
191 assert(fun);
192 hc_t *hc = fun_to_hc(fun);
193 assert(hc);
194 usb_log_debug("Address release %d.\n", address);
195 usb_device_keeper_release(&hc->manager, address);
196 return EOK;
197}
198/*----------------------------------------------------------------------------*/
199static int register_endpoint(
200 ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
201 usb_transfer_type_t transfer_type, usb_direction_t direction,
202 size_t max_packet_size, unsigned int interval)
203{
204 hc_t *hc = fun_to_hc(fun);
205 assert(hc);
206 const usb_speed_t speed =
207 usb_device_keeper_get_speed(&hc->manager, address);
208 const size_t size =
209 (transfer_type == USB_TRANSFER_INTERRUPT
210 || transfer_type == USB_TRANSFER_ISOCHRONOUS) ?
211 max_packet_size : 0;
212 int ret;
213
214 endpoint_t *ep = malloc(sizeof(endpoint_t));
215 if (ep == NULL)
216 return ENOMEM;
217 ret = endpoint_init(ep, address, endpoint, direction,
218 transfer_type, speed, max_packet_size);
219 if (ret != EOK) {
220 free(ep);
221 return ret;
222 }
223
224 usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
225 address, endpoint, usb_str_transfer_type(transfer_type),
226 usb_str_speed(speed), direction, size, max_packet_size, interval);
227
228 ret = usb_endpoint_manager_register_ep(&hc->ep_manager, ep, size);
229 if (ret != EOK) {
230 endpoint_destroy(ep);
231 } else {
232 usb_device_keeper_add_ep(&hc->manager, address, ep);
233 }
234 return ret;
235}
236/*----------------------------------------------------------------------------*/
237static int unregister_endpoint(
238 ddf_fun_t *fun, usb_address_t address,
239 usb_endpoint_t endpoint, usb_direction_t direction)
240{
241 hc_t *hc = fun_to_hc(fun);
242 assert(hc);
243 usb_log_debug("Unregister endpoint %d:%d %d.\n",
244 address, endpoint, direction);
245 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
246 endpoint, direction);
247}
248/*----------------------------------------------------------------------------*/
249/** Interrupt 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 */
259static int interrupt_out(
260 ddf_fun_t *fun, usb_target_t target, void *data,
261 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
262{
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, "Interrupt OUT", &hc, &batch);
267 if (ret != EOK)
268 return ret;
269 batch_interrupt_out(batch);
270 ret = hc_schedule(hc, batch);
271 if (ret != EOK) {
272 batch_dispose(batch);
273 }
274 return ret;
275}
276/*----------------------------------------------------------------------------*/
277/** Interrupt 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 */
287static int interrupt_in(
288 ddf_fun_t *fun, usb_target_t target, void *data,
289 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
290{
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, "Interrupt IN", &hc, &batch);
295 if (ret != EOK)
296 return ret;
297 batch_interrupt_in(batch);
298 ret = hc_schedule(hc, batch);
299 if (ret != EOK) {
300 batch_dispose(batch);
301 }
302 return ret;
303}
304/*----------------------------------------------------------------------------*/
305/** Bulk out transaction interface function
306 *
307 * @param[in] fun DDF function that was called.
308 * @param[in] target USB device to write to.
309 * @param[in] data Source of data.
310 * @param[in] size Size of data source.
311 * @param[in] callback Function to call on transaction completion
312 * @param[in] arg Additional for callback function.
313 * @return Error code.
314 */
315static int bulk_out(
316 ddf_fun_t *fun, usb_target_t target, void *data,
317 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
318{
319 usb_transfer_batch_t *batch = NULL;
320 hc_t *hc = NULL;
321 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
322 NULL, 0, NULL, callback, arg, "Bulk OUT", &hc, &batch);
323 if (ret != EOK)
324 return ret;
325 batch_bulk_out(batch);
326 ret = hc_schedule(hc, batch);
327 if (ret != EOK) {
328 batch_dispose(batch);
329 }
330 return ret;
331}
332/*----------------------------------------------------------------------------*/
333/** Bulk in transaction interface function
334 *
335 * @param[in] fun DDF function that was called.
336 * @param[in] target USB device to write to.
337 * @param[out] data Data destination.
338 * @param[in] size Size of data source.
339 * @param[in] callback Function to call on transaction completion
340 * @param[in] arg Additional for callback function.
341 * @return Error code.
342 */
343static int bulk_in(
344 ddf_fun_t *fun, usb_target_t target, void *data,
345 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
346{
347 usb_transfer_batch_t *batch = NULL;
348 hc_t *hc = NULL;
349 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
350 NULL, 0, callback, NULL, arg, "Bulk IN", &hc, &batch);
351 if (ret != EOK)
352 return ret;
353 batch_bulk_in(batch);
354 ret = hc_schedule(hc, batch);
355 if (ret != EOK) {
356 batch_dispose(batch);
357 }
358 return ret;
359}
360/*----------------------------------------------------------------------------*/
361/** Control write transaction interface function
362 *
363 * @param[in] fun DDF function that was called.
364 * @param[in] target USB device to write to.
365 * @param[in] setup_data Data to send with SETUP transfer.
366 * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
367 * @param[in] data Source of data.
368 * @param[in] size Size of data source.
369 * @param[in] callback Function to call on transaction completion.
370 * @param[in] arg Additional for callback function.
371 * @return Error code.
372 */
373static int control_write(
374 ddf_fun_t *fun, usb_target_t target,
375 void *setup_data, size_t setup_size, void *data, size_t size,
376 usbhc_iface_transfer_out_callback_t callback, void *arg)
377{
378 usb_transfer_batch_t *batch = NULL;
379 hc_t *hc = NULL;
380 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
381 setup_data, setup_size, NULL, callback, arg, "Control WRITE",
382 &hc, &batch);
383 if (ret != EOK)
384 return ret;
385 usb_device_keeper_reset_if_need(&hc->manager, target, setup_data);
386 batch_control_write(batch);
387 ret = hc_schedule(hc, batch);
388 if (ret != EOK) {
389 batch_dispose(batch);
390 }
391 return ret;
392}
393/*----------------------------------------------------------------------------*/
394/** Control read transaction interface function
395 *
396 * @param[in] fun DDF function that was called.
397 * @param[in] target USB device to write to.
398 * @param[in] setup_data Data to send with SETUP packet.
399 * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
400 * @param[out] data Source of data.
401 * @param[in] size Size of data source.
402 * @param[in] callback Function to call on transaction completion.
403 * @param[in] arg Additional for callback function.
404 * @return Error code.
405 */
406static int control_read(
407 ddf_fun_t *fun, usb_target_t target,
408 void *setup_data, size_t setup_size, void *data, size_t size,
409 usbhc_iface_transfer_in_callback_t callback, void *arg)
410{
411 usb_transfer_batch_t *batch = NULL;
412 hc_t *hc = NULL;
413 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
414 setup_data, setup_size, callback, NULL, arg, "Control READ",
415 &hc, &batch);
416 if (ret != EOK)
417 return ret;
418 batch_control_read(batch);
419 ret = hc_schedule(hc, batch);
420 if (ret != EOK) {
421 batch_dispose(batch);
422 }
423 return ret;
424}
425/*----------------------------------------------------------------------------*/
426usbhc_iface_t hc_iface = {
427 .reserve_default_address = reserve_default_address,
428 .release_default_address = release_default_address,
429 .request_address = request_address,
430 .bind_address = bind_address,
431 .release_address = release_address,
432
433 .register_endpoint = register_endpoint,
434 .unregister_endpoint = unregister_endpoint,
435
436 .interrupt_out = interrupt_out,
437 .interrupt_in = interrupt_in,
438
439 .bulk_out = bulk_out,
440 .bulk_in = bulk_in,
441
442 .control_write = control_write,
443 .control_read = control_read,
444};
445/**
446 * @}
447 */
Note: See TracBrowser for help on using the repository browser.