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

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

Don't keep endpoints in two separate structures

Fixes crash "[HelenOS-USB-devel] uhci root hub" email
Move toggle resert testing ugliness to endpoint manager

  • 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_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 }
235 return ret;
236}
237/*----------------------------------------------------------------------------*/
238static int unregister_endpoint(
239 ddf_fun_t *fun, usb_address_t address,
240 usb_endpoint_t endpoint, usb_direction_t direction)
241{
242 hc_t *hc = fun_to_hc(fun);
243 assert(hc);
244 usb_log_debug("Unregister endpoint %d:%d %d.\n",
245 address, endpoint, direction);
246 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
247 endpoint, direction);
248}
249/*----------------------------------------------------------------------------*/
250/** Interrupt out transaction interface function
251 *
252 * @param[in] fun DDF function that was called.
253 * @param[in] target USB device to write to.
254 * @param[in] data Source of data.
255 * @param[in] size Size of data source.
256 * @param[in] callback Function to call on transaction completion
257 * @param[in] arg Additional for callback function.
258 * @return Error code.
259 */
260static int interrupt_out(
261 ddf_fun_t *fun, usb_target_t target, void *data,
262 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
263{
264 usb_transfer_batch_t *batch = NULL;
265 hc_t *hc = NULL;
266 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
267 NULL, 0, NULL, callback, arg, "Interrupt OUT", &hc, &batch);
268 if (ret != EOK)
269 return ret;
270 batch_interrupt_out(batch);
271 ret = hc_schedule(hc, batch);
272 if (ret != EOK) {
273 batch_dispose(batch);
274 }
275 return ret;
276}
277/*----------------------------------------------------------------------------*/
278/** Interrupt in transaction interface function
279 *
280 * @param[in] fun DDF function that was called.
281 * @param[in] target USB device to write to.
282 * @param[out] data Data destination.
283 * @param[in] size Size of data source.
284 * @param[in] callback Function to call on transaction completion
285 * @param[in] arg Additional for callback function.
286 * @return Error code.
287 */
288static int interrupt_in(
289 ddf_fun_t *fun, usb_target_t target, void *data,
290 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
291{
292 usb_transfer_batch_t *batch = NULL;
293 hc_t *hc = NULL;
294 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
295 NULL, 0, callback, NULL, arg, "Interrupt IN", &hc, &batch);
296 if (ret != EOK)
297 return ret;
298 batch_interrupt_in(batch);
299 ret = hc_schedule(hc, batch);
300 if (ret != EOK) {
301 batch_dispose(batch);
302 }
303 return ret;
304}
305/*----------------------------------------------------------------------------*/
306/** Bulk out transaction interface function
307 *
308 * @param[in] fun DDF function that was called.
309 * @param[in] target USB device to write to.
310 * @param[in] data Source of data.
311 * @param[in] size Size of data source.
312 * @param[in] callback Function to call on transaction completion
313 * @param[in] arg Additional for callback function.
314 * @return Error code.
315 */
316static int bulk_out(
317 ddf_fun_t *fun, usb_target_t target, void *data,
318 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
319{
320 usb_transfer_batch_t *batch = NULL;
321 hc_t *hc = NULL;
322 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
323 NULL, 0, NULL, callback, arg, "Bulk OUT", &hc, &batch);
324 if (ret != EOK)
325 return ret;
326 batch_bulk_out(batch);
327 ret = hc_schedule(hc, batch);
328 if (ret != EOK) {
329 batch_dispose(batch);
330 }
331 return ret;
332}
333/*----------------------------------------------------------------------------*/
334/** Bulk in transaction interface function
335 *
336 * @param[in] fun DDF function that was called.
337 * @param[in] target USB device to write to.
338 * @param[out] data Data destination.
339 * @param[in] size Size of data source.
340 * @param[in] callback Function to call on transaction completion
341 * @param[in] arg Additional for callback function.
342 * @return Error code.
343 */
344static int bulk_in(
345 ddf_fun_t *fun, usb_target_t target, void *data,
346 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
347{
348 usb_transfer_batch_t *batch = NULL;
349 hc_t *hc = NULL;
350 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
351 NULL, 0, callback, NULL, arg, "Bulk IN", &hc, &batch);
352 if (ret != EOK)
353 return ret;
354 batch_bulk_in(batch);
355 ret = hc_schedule(hc, batch);
356 if (ret != EOK) {
357 batch_dispose(batch);
358 }
359 return ret;
360}
361/*----------------------------------------------------------------------------*/
362/** Control write transaction interface function
363 *
364 * @param[in] fun DDF function that was called.
365 * @param[in] target USB device to write to.
366 * @param[in] setup_data Data to send with SETUP transfer.
367 * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
368 * @param[in] data Source of data.
369 * @param[in] size Size of data source.
370 * @param[in] callback Function to call on transaction completion.
371 * @param[in] arg Additional for callback function.
372 * @return Error code.
373 */
374static int control_write(
375 ddf_fun_t *fun, usb_target_t target,
376 void *setup_data, size_t setup_size, void *data, size_t size,
377 usbhc_iface_transfer_out_callback_t callback, void *arg)
378{
379 usb_transfer_batch_t *batch = NULL;
380 hc_t *hc = NULL;
381 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
382 setup_data, setup_size, NULL, callback, arg, "Control WRITE",
383 &hc, &batch);
384 if (ret != EOK)
385 return ret;
386 usb_endpoint_manager_reset_if_need(&hc->ep_manager, target, setup_data);
387 batch_control_write(batch);
388 ret = hc_schedule(hc, batch);
389 if (ret != EOK) {
390 batch_dispose(batch);
391 }
392 return ret;
393}
394/*----------------------------------------------------------------------------*/
395/** Control read transaction interface function
396 *
397 * @param[in] fun DDF function that was called.
398 * @param[in] target USB device to write to.
399 * @param[in] setup_data Data to send with SETUP packet.
400 * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
401 * @param[out] data Source of data.
402 * @param[in] size Size of data source.
403 * @param[in] callback Function to call on transaction completion.
404 * @param[in] arg Additional for callback function.
405 * @return Error code.
406 */
407static int control_read(
408 ddf_fun_t *fun, usb_target_t target,
409 void *setup_data, size_t setup_size, void *data, size_t size,
410 usbhc_iface_transfer_in_callback_t callback, void *arg)
411{
412 usb_transfer_batch_t *batch = NULL;
413 hc_t *hc = NULL;
414 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
415 setup_data, setup_size, callback, NULL, arg, "Control READ",
416 &hc, &batch);
417 if (ret != EOK)
418 return ret;
419 batch_control_read(batch);
420 ret = hc_schedule(hc, batch);
421 if (ret != EOK) {
422 batch_dispose(batch);
423 }
424 return ret;
425}
426/*----------------------------------------------------------------------------*/
427usbhc_iface_t hc_iface = {
428 .reserve_default_address = reserve_default_address,
429 .release_default_address = release_default_address,
430 .request_address = request_address,
431 .bind_address = bind_address,
432 .release_address = release_address,
433
434 .register_endpoint = register_endpoint,
435 .unregister_endpoint = unregister_endpoint,
436
437 .interrupt_out = interrupt_out,
438 .interrupt_in = interrupt_in,
439
440 .bulk_out = bulk_out,
441 .bulk_in = bulk_in,
442
443 .control_write = control_write,
444 .control_read = control_read,
445};
446/**
447 * @}
448 */
Note: See TracBrowser for help on using the repository browser.