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

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

Size is always max_packet_size in endpoint registration

At least until the parameter is added
CONTROL and BULK do not use bandwidth, so this does not matter

  • Property mode set to 100644
File size: 14.9 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 = max_packet_size;
209 int ret;
210
211 endpoint_t *ep = malloc(sizeof(endpoint_t));
212 if (ep == NULL)
213 return ENOMEM;
214 ret = endpoint_init(ep, address, endpoint, direction,
215 transfer_type, speed, max_packet_size);
216 if (ret != EOK) {
217 free(ep);
218 return ret;
219 }
220
221 usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
222 address, endpoint, usb_str_transfer_type(transfer_type),
223 usb_str_speed(speed), direction, size, max_packet_size, interval);
224
225 ret = usb_endpoint_manager_register_ep(&hc->ep_manager, ep, size);
226 if (ret != EOK) {
227 endpoint_destroy(ep);
228 } else {
229 usb_device_keeper_add_ep(&hc->manager, address, ep);
230 }
231 return ret;
232}
233/*----------------------------------------------------------------------------*/
234static int unregister_endpoint(
235 ddf_fun_t *fun, usb_address_t address,
236 usb_endpoint_t endpoint, usb_direction_t direction)
237{
238 hc_t *hc = fun_to_hc(fun);
239 assert(hc);
240 usb_log_debug("Unregister endpoint %d:%d %d.\n",
241 address, endpoint, direction);
242 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
243 endpoint, direction);
244}
245/*----------------------------------------------------------------------------*/
246/** Interrupt out transaction interface function
247 *
248 * @param[in] fun DDF function that was called.
249 * @param[in] target USB device to write to.
250 * @param[in] max_packet_size maximum size of data packet the device accepts
251 * @param[in] data Source of data.
252 * @param[in] size Size of data source.
253 * @param[in] callback Function to call on transaction completion
254 * @param[in] arg Additional for callback function.
255 * @return Error code.
256 */
257static int interrupt_out(
258 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
259 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
260{
261 usb_transfer_batch_t *batch = NULL;
262 hc_t *hc = NULL;
263 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
264 NULL, 0, NULL, callback, arg, "Interrupt OUT", &hc, &batch);
265 if (ret != EOK)
266 return ret;
267 batch_interrupt_out(batch);
268 ret = hc_schedule(hc, batch);
269 if (ret != EOK) {
270 batch_dispose(batch);
271 }
272 return ret;
273}
274/*----------------------------------------------------------------------------*/
275/** Interrupt in transaction interface function
276 *
277 * @param[in] fun DDF function that was called.
278 * @param[in] target USB device to write to.
279 * @param[in] max_packet_size maximum size of data packet the device accepts
280 * @param[out] data Data destination.
281 * @param[in] size Size of data source.
282 * @param[in] callback Function to call on transaction completion
283 * @param[in] arg Additional for callback function.
284 * @return Error code.
285 */
286static int interrupt_in(
287 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
288 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
289{
290 usb_transfer_batch_t *batch = NULL;
291 hc_t *hc = NULL;
292 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
293 NULL, 0, callback, NULL, arg, "Interrupt IN", &hc, &batch);
294 if (ret != EOK)
295 return ret;
296 batch_interrupt_in(batch);
297 ret = hc_schedule(hc, batch);
298 if (ret != EOK) {
299 batch_dispose(batch);
300 }
301 return ret;
302}
303/*----------------------------------------------------------------------------*/
304/** Bulk out transaction interface function
305 *
306 * @param[in] fun DDF function that was called.
307 * @param[in] target USB device to write to.
308 * @param[in] max_packet_size maximum size of data packet the device accepts
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, size_t max_packet_size, 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[in] max_packet_size maximum size of data packet the device accepts
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, size_t max_packet_size, 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] max_packet_size maximum size of data packet the device accepts.
367 * @param[in] setup_data Data to send with SETUP transfer.
368 * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
369 * @param[in] data Source of data.
370 * @param[in] size Size of data source.
371 * @param[in] callback Function to call on transaction completion.
372 * @param[in] arg Additional for callback function.
373 * @return Error code.
374 */
375static int control_write(
376 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size,
377 void *setup_data, size_t setup_size, void *data, size_t size,
378 usbhc_iface_transfer_out_callback_t callback, void *arg)
379{
380 usb_transfer_batch_t *batch = NULL;
381 hc_t *hc = NULL;
382 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
383 setup_data, setup_size, NULL, callback, arg, "Control WRITE",
384 &hc, &batch);
385 if (ret != EOK)
386 return ret;
387 usb_device_keeper_reset_if_need(&hc->manager, target, setup_data);
388 batch_control_write(batch);
389 ret = hc_schedule(hc, batch);
390 if (ret != EOK) {
391 batch_dispose(batch);
392 }
393 return ret;
394}
395/*----------------------------------------------------------------------------*/
396/** Control read transaction interface function
397 *
398 * @param[in] fun DDF function that was called.
399 * @param[in] target USB device to write to.
400 * @param[in] max_packet_size maximum size of data packet the device accepts.
401 * @param[in] setup_data Data to send with SETUP packet.
402 * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
403 * @param[out] data Source of data.
404 * @param[in] size Size of data source.
405 * @param[in] callback Function to call on transaction completion.
406 * @param[in] arg Additional for callback function.
407 * @return Error code.
408 */
409static int control_read(
410 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size,
411 void *setup_data, size_t setup_size, void *data, size_t size,
412 usbhc_iface_transfer_in_callback_t callback, void *arg)
413{
414 usb_transfer_batch_t *batch = NULL;
415 hc_t *hc = NULL;
416 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
417 setup_data, setup_size, callback, NULL, arg, "Control READ",
418 &hc, &batch);
419 if (ret != EOK)
420 return ret;
421 batch_control_read(batch);
422 ret = hc_schedule(hc, batch);
423 if (ret != EOK) {
424 batch_dispose(batch);
425 }
426 return ret;
427}
428/*----------------------------------------------------------------------------*/
429usbhc_iface_t hc_iface = {
430 .reserve_default_address = reserve_default_address,
431 .release_default_address = release_default_address,
432 .request_address = request_address,
433 .bind_address = bind_address,
434 .release_address = release_address,
435
436 .register_endpoint = register_endpoint,
437 .unregister_endpoint = unregister_endpoint,
438
439 .interrupt_out = interrupt_out,
440 .interrupt_in = interrupt_in,
441
442 .bulk_out = bulk_out,
443 .bulk_in = bulk_in,
444
445 .control_write = control_write,
446 .control_read = control_read,
447};
448/**
449 * @}
450 */
Note: See TracBrowser for help on using the repository browser.