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

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

Endpoint registration sends address as well

This is the first step towards using endpoint registration instead
of reservation of default address.

  • Property mode set to 100644
File size: 14.7 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_speed_t ep_speed,
201 usb_endpoint_t endpoint,
202 usb_transfer_type_t transfer_type, usb_direction_t direction,
203 size_t max_packet_size, unsigned int interval)
204{
205 hc_t *hc = fun_to_hc(fun);
206 assert(hc);
207 usb_speed_t speed = usb_device_keeper_get_speed(&hc->manager, address);
208 if (speed >= USB_SPEED_MAX) {
209 speed = ep_speed;
210 }
211 const size_t size =
212 (transfer_type == USB_TRANSFER_INTERRUPT
213 || transfer_type == USB_TRANSFER_ISOCHRONOUS) ?
214 max_packet_size : 0;
215 int ret;
216
217 endpoint_t *ep = malloc(sizeof(endpoint_t));
218 if (ep == NULL)
219 return ENOMEM;
220 ret = endpoint_init(ep, address, endpoint, direction,
221 transfer_type, speed, max_packet_size);
222 if (ret != EOK) {
223 free(ep);
224 return ret;
225 }
226
227 usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
228 address, endpoint, usb_str_transfer_type(transfer_type),
229 usb_str_speed(speed), direction, size, max_packet_size, interval);
230
231 ret = usb_endpoint_manager_register_ep(&hc->ep_manager, ep, size);
232 if (ret != EOK) {
233 endpoint_destroy(ep);
234 } else {
235 usb_device_keeper_add_ep(&hc->manager, address, ep);
236 }
237 return ret;
238}
239/*----------------------------------------------------------------------------*/
240static int unregister_endpoint(
241 ddf_fun_t *fun, usb_address_t address,
242 usb_endpoint_t endpoint, usb_direction_t direction)
243{
244 hc_t *hc = fun_to_hc(fun);
245 assert(hc);
246 usb_log_debug("Unregister endpoint %d:%d %d.\n",
247 address, endpoint, direction);
248 endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
249 address, endpoint, direction, NULL);
250 if (ep != NULL) {
251 usb_device_keeper_del_ep(&hc->manager, address, ep);
252 }
253 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
254 endpoint, direction);
255}
256/*----------------------------------------------------------------------------*/
257/** Interrupt out transaction interface function
258 *
259 * @param[in] fun DDF function that was called.
260 * @param[in] target USB device to write to.
261 * @param[in] data Source of data.
262 * @param[in] size Size of data source.
263 * @param[in] callback Function to call on transaction completion
264 * @param[in] arg Additional for callback function.
265 * @return Error code.
266 */
267static int interrupt_out(
268 ddf_fun_t *fun, usb_target_t target, void *data,
269 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
270{
271 usb_transfer_batch_t *batch = NULL;
272 hc_t *hc = NULL;
273 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
274 NULL, 0, NULL, callback, arg, "Interrupt OUT", &hc, &batch);
275 if (ret != EOK)
276 return ret;
277 batch_interrupt_out(batch);
278 ret = hc_schedule(hc, batch);
279 if (ret != EOK) {
280 batch_dispose(batch);
281 }
282 return ret;
283}
284/*----------------------------------------------------------------------------*/
285/** Interrupt 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 */
295static int interrupt_in(
296 ddf_fun_t *fun, usb_target_t target, void *data,
297 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
298{
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, "Interrupt IN", &hc, &batch);
303 if (ret != EOK)
304 return ret;
305 batch_interrupt_in(batch);
306 ret = hc_schedule(hc, batch);
307 if (ret != EOK) {
308 batch_dispose(batch);
309 }
310 return ret;
311}
312/*----------------------------------------------------------------------------*/
313/** Bulk out transaction interface function
314 *
315 * @param[in] fun DDF function that was called.
316 * @param[in] target USB device to write to.
317 * @param[in] data Source of data.
318 * @param[in] size Size of data source.
319 * @param[in] callback Function to call on transaction completion
320 * @param[in] arg Additional for callback function.
321 * @return Error code.
322 */
323static int bulk_out(
324 ddf_fun_t *fun, usb_target_t target, void *data,
325 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
326{
327 usb_transfer_batch_t *batch = NULL;
328 hc_t *hc = NULL;
329 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
330 NULL, 0, NULL, callback, arg, "Bulk OUT", &hc, &batch);
331 if (ret != EOK)
332 return ret;
333 batch_bulk_out(batch);
334 ret = hc_schedule(hc, batch);
335 if (ret != EOK) {
336 batch_dispose(batch);
337 }
338 return ret;
339}
340/*----------------------------------------------------------------------------*/
341/** Bulk in transaction interface function
342 *
343 * @param[in] fun DDF function that was called.
344 * @param[in] target USB device to write to.
345 * @param[out] data Data destination.
346 * @param[in] size Size of data source.
347 * @param[in] callback Function to call on transaction completion
348 * @param[in] arg Additional for callback function.
349 * @return Error code.
350 */
351static int bulk_in(
352 ddf_fun_t *fun, usb_target_t target, void *data,
353 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
354{
355 usb_transfer_batch_t *batch = NULL;
356 hc_t *hc = NULL;
357 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
358 NULL, 0, callback, NULL, arg, "Bulk IN", &hc, &batch);
359 if (ret != EOK)
360 return ret;
361 batch_bulk_in(batch);
362 ret = hc_schedule(hc, batch);
363 if (ret != EOK) {
364 batch_dispose(batch);
365 }
366 return ret;
367}
368/*----------------------------------------------------------------------------*/
369/** Control write transaction interface function
370 *
371 * @param[in] fun DDF function that was called.
372 * @param[in] target USB device to write to.
373 * @param[in] setup_data Data to send with SETUP transfer.
374 * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
375 * @param[in] data Source of data.
376 * @param[in] size Size of data source.
377 * @param[in] callback Function to call on transaction completion.
378 * @param[in] arg Additional for callback function.
379 * @return Error code.
380 */
381static int control_write(
382 ddf_fun_t *fun, usb_target_t target,
383 void *setup_data, size_t setup_size, void *data, size_t size,
384 usbhc_iface_transfer_out_callback_t callback, void *arg)
385{
386 usb_transfer_batch_t *batch = NULL;
387 hc_t *hc = NULL;
388 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
389 setup_data, setup_size, NULL, callback, arg, "Control WRITE",
390 &hc, &batch);
391 if (ret != EOK)
392 return ret;
393 usb_device_keeper_reset_if_need(&hc->manager, target, setup_data);
394 batch_control_write(batch);
395 ret = hc_schedule(hc, batch);
396 if (ret != EOK) {
397 batch_dispose(batch);
398 }
399 return ret;
400}
401/*----------------------------------------------------------------------------*/
402/** Control read transaction interface function
403 *
404 * @param[in] fun DDF function that was called.
405 * @param[in] target USB device to write to.
406 * @param[in] setup_data Data to send with SETUP packet.
407 * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
408 * @param[out] data Source of data.
409 * @param[in] size Size of data source.
410 * @param[in] callback Function to call on transaction completion.
411 * @param[in] arg Additional for callback function.
412 * @return Error code.
413 */
414static int control_read(
415 ddf_fun_t *fun, usb_target_t target,
416 void *setup_data, size_t setup_size, void *data, size_t size,
417 usbhc_iface_transfer_in_callback_t callback, void *arg)
418{
419 usb_transfer_batch_t *batch = NULL;
420 hc_t *hc = NULL;
421 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
422 setup_data, setup_size, callback, NULL, arg, "Control READ",
423 &hc, &batch);
424 if (ret != EOK)
425 return ret;
426 batch_control_read(batch);
427 ret = hc_schedule(hc, batch);
428 if (ret != EOK) {
429 batch_dispose(batch);
430 }
431 return ret;
432}
433/*----------------------------------------------------------------------------*/
434usbhc_iface_t hc_iface = {
435 .reserve_default_address = reserve_default_address,
436 .release_default_address = release_default_address,
437 .request_address = request_address,
438 .bind_address = bind_address,
439 .release_address = release_address,
440
441 .register_endpoint = register_endpoint,
442 .unregister_endpoint = unregister_endpoint,
443
444 .interrupt_out = interrupt_out,
445 .interrupt_in = interrupt_in,
446
447 .bulk_out = bulk_out,
448 .bulk_in = bulk_in,
449
450 .control_write = control_write,
451 .control_read = control_read,
452};
453/**
454 * @}
455 */
Note: See TracBrowser for help on using the repository browser.