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

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

Do not send max packet size with each transfer

See ticket #177, #121 and partly #49.

  • Property mode set to 100644
File size: 16.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
43/** Reserve default address interface function
44 *
45 * @param[in] fun DDF function that was called.
46 * @param[in] speed Speed to associate with the new default address.
47 * @return Error code.
48 */
49static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
50{
51 assert(fun);
52 hc_t *hc = fun_to_hc(fun);
53 assert(hc);
54 usb_log_debug("Default address request with speed %d.\n", speed);
55 usb_device_keeper_reserve_default_address(&hc->manager, speed);
56 return EOK;
57#if 0
58 endpoint_t *ep = malloc(sizeof(endpoint_t));
59 if (ep == NULL)
60 return ENOMEM;
61 const size_t max_packet_size = speed == USB_SPEED_LOW ? 8 : 64;
62 endpoint_init(ep, USB_TRANSFER_CONTROL, speed, max_packet_size);
63 int ret;
64try_retgister:
65 ret = usb_endpoint_manager_register_ep(&hc->ep_manager,
66 USB_ADDRESS_DEFAULT, 0, USB_DIRECTION_BOTH, ep, endpoint_destroy, 0);
67 if (ret == EEXISTS) {
68 async_usleep(1000);
69 goto try_retgister;
70 }
71 if (ret != EOK) {
72 endpoint_destroy(ep);
73 }
74 return ret;
75#endif
76}
77/*----------------------------------------------------------------------------*/
78/** Release default address interface function
79 *
80 * @param[in] fun DDF function that was called.
81 * @return Error code.
82 */
83static int release_default_address(ddf_fun_t *fun)
84{
85 assert(fun);
86 hc_t *hc = fun_to_hc(fun);
87 assert(hc);
88 usb_log_debug("Default address release.\n");
89// return usb_endpoint_manager_unregister_ep(&hc->ep_manager,
90// USB_ADDRESS_DEFAULT, 0, USB_DIRECTION_BOTH);
91 usb_device_keeper_release_default_address(&hc->manager);
92 return EOK;
93}
94/*----------------------------------------------------------------------------*/
95/** Request address interface function
96 *
97 * @param[in] fun DDF function that was called.
98 * @param[in] speed Speed to associate with the new default address.
99 * @param[out] address Place to write a new address.
100 * @return Error code.
101 */
102static int request_address(
103 ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
104{
105 assert(fun);
106 hc_t *hc = fun_to_hc(fun);
107 assert(hc);
108 assert(address);
109
110 usb_log_debug("Address request with speed %d.\n", speed);
111 *address = device_keeper_get_free_address(&hc->manager, speed);
112 usb_log_debug("Address request with result: %d.\n", *address);
113 if (*address <= 0)
114 return *address;
115 return EOK;
116}
117/*----------------------------------------------------------------------------*/
118/** Bind address interface function
119 *
120 * @param[in] fun DDF function that was called.
121 * @param[in] address Address of the device
122 * @param[in] handle Devman handle of the device driver.
123 * @return Error code.
124 */
125static int bind_address(
126 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
127{
128 assert(fun);
129 hc_t *hc = fun_to_hc(fun);
130 assert(hc);
131 usb_log_debug("Address bind %d-%d.\n", address, handle);
132 usb_device_keeper_bind(&hc->manager, address, handle);
133 return EOK;
134}
135/*----------------------------------------------------------------------------*/
136/** Release address interface function
137 *
138 * @param[in] fun DDF function that was called.
139 * @param[in] address USB address to be released.
140 * @return Error code.
141 */
142static int release_address(ddf_fun_t *fun, usb_address_t address)
143{
144 assert(fun);
145 hc_t *hc = fun_to_hc(fun);
146 assert(hc);
147 usb_log_debug("Address release %d.\n", address);
148 usb_device_keeper_release(&hc->manager, address);
149 return EOK;
150}
151/*----------------------------------------------------------------------------*/
152static int register_endpoint(
153 ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
154 usb_transfer_type_t transfer_type, usb_direction_t direction,
155 size_t max_packet_size, unsigned int interval)
156{
157 hc_t *hc = fun_to_hc(fun);
158 assert(hc);
159 const usb_speed_t speed =
160 usb_device_keeper_get_speed(&hc->manager, address);
161 const size_t size =
162 (transfer_type == USB_TRANSFER_INTERRUPT
163 || transfer_type == USB_TRANSFER_ISOCHRONOUS) ?
164 max_packet_size : 0;
165 int ret;
166
167 endpoint_t *ep = malloc(sizeof(endpoint_t));
168 if (ep == NULL)
169 return ENOMEM;
170 ret = endpoint_init(ep, address, endpoint, direction,
171 transfer_type, speed, max_packet_size);
172 if (ret != EOK) {
173 free(ep);
174 return ret;
175 }
176
177 usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
178 address, endpoint, usb_str_transfer_type(transfer_type),
179 usb_str_speed(speed), direction, size, max_packet_size, interval);
180
181 ret = usb_endpoint_manager_register_ep(&hc->ep_manager, ep, size);
182 if (ret != EOK) {
183 endpoint_destroy(ep);
184 } else {
185 usb_device_keeper_add_ep(&hc->manager, address, ep);
186 }
187 return ret;
188}
189/*----------------------------------------------------------------------------*/
190static int unregister_endpoint(
191 ddf_fun_t *fun, usb_address_t address,
192 usb_endpoint_t endpoint, usb_direction_t direction)
193{
194 hc_t *hc = fun_to_hc(fun);
195 assert(hc);
196 usb_log_debug("Unregister endpoint %d:%d %d.\n",
197 address, endpoint, direction);
198 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
199 endpoint, direction);
200}
201/*----------------------------------------------------------------------------*/
202/** Interrupt out transaction interface function
203 *
204 * @param[in] fun DDF function that was called.
205 * @param[in] target USB device to write to.
206 * @param[in] data Source of data.
207 * @param[in] size Size of data source.
208 * @param[in] callback Function to call on transaction completion
209 * @param[in] arg Additional for callback function.
210 * @return Error code.
211 */
212static int interrupt_out(
213 ddf_fun_t *fun, usb_target_t target, void *data,
214 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
215{
216 assert(fun);
217 hc_t *hc = fun_to_hc(fun);
218 assert(hc);
219
220 usb_log_debug("Interrupt OUT %d:%d %zu.\n",
221 target.address, target.endpoint, size);
222
223 size_t res_bw;
224 endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
225 target.address, target.endpoint, USB_DIRECTION_OUT, &res_bw);
226 if (ep == NULL) {
227 usb_log_error("Endpoint(%d:%d) not registered for INT OUT.\n",
228 target.address, target.endpoint);
229 return ENOENT;
230 }
231 const size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
232 size, ep->max_packet_size);
233 if (res_bw < bw)
234 {
235 usb_log_error("Endpoint(%d:%d) INT IN needs %zu bw "
236 "but only %zu is reserved.\n",
237 target.address, target.endpoint, bw, res_bw);
238 return ENOENT;
239 }
240 assert(ep->speed ==
241 usb_device_keeper_get_speed(&hc->manager, target.address));
242 assert(ep->transfer_type == USB_TRANSFER_INTERRUPT);
243
244 usb_transfer_batch_t *batch =
245 batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
246 ep->speed, data, size, NULL, 0, NULL, callback, arg, ep);
247 if (!batch)
248 return ENOMEM;
249 batch_interrupt_out(batch);
250 const int ret = hc_schedule(hc, batch);
251 if (ret != EOK) {
252 batch_dispose(batch);
253 }
254 return ret;
255}
256/*----------------------------------------------------------------------------*/
257/** Interrupt in transaction interface function
258 *
259 * @param[in] fun DDF function that was called.
260 * @param[in] target USB device to write to.
261 * @param[out] data Data destination.
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_in(
268 ddf_fun_t *fun, usb_target_t target, void *data,
269 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
270{
271 assert(fun);
272 hc_t *hc = fun_to_hc(fun);
273 assert(hc);
274
275 usb_log_debug("Interrupt IN %d:%d %zu.\n",
276 target.address, target.endpoint, size);
277
278 size_t res_bw;
279 endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
280 target.address, target.endpoint, USB_DIRECTION_IN, &res_bw);
281 if (ep == NULL) {
282 usb_log_error("Endpoint(%d:%d) not registered for INT IN.\n",
283 target.address, target.endpoint);
284 return ENOENT;
285 }
286 const size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
287 size, ep->max_packet_size);
288 if (res_bw < bw)
289 {
290 usb_log_error("Endpoint(%d:%d) INT IN needs %zu bw "
291 "but only %zu bw is reserved.\n",
292 target.address, target.endpoint, bw, res_bw);
293 return ENOENT;
294 }
295
296 assert(ep->speed ==
297 usb_device_keeper_get_speed(&hc->manager, target.address));
298 assert(ep->transfer_type == USB_TRANSFER_INTERRUPT);
299
300 usb_transfer_batch_t *batch =
301 batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
302 ep->speed, data, size, NULL, 0, callback, NULL, arg, ep);
303 if (!batch)
304 return ENOMEM;
305 batch_interrupt_in(batch);
306 const int 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 assert(fun);
328 hc_t *hc = fun_to_hc(fun);
329 assert(hc);
330
331 usb_log_debug("Bulk OUT %d:%d %zu.\n",
332 target.address, target.endpoint, size);
333
334 endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
335 target.address, target.endpoint, USB_DIRECTION_OUT, NULL);
336 if (ep == NULL) {
337 usb_log_error("Endpoint(%d:%d) not registered for BULK OUT.\n",
338 target.address, target.endpoint);
339 return ENOENT;
340 }
341 assert(ep->speed ==
342 usb_device_keeper_get_speed(&hc->manager, target.address));
343 assert(ep->transfer_type == USB_TRANSFER_BULK);
344
345 usb_transfer_batch_t *batch =
346 batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
347 ep->speed, data, size, NULL, 0, NULL, callback, arg, ep);
348 if (!batch)
349 return ENOMEM;
350 batch_bulk_out(batch);
351 const int ret = hc_schedule(hc, batch);
352 if (ret != EOK) {
353 batch_dispose(batch);
354 }
355 return ret;
356}
357/*----------------------------------------------------------------------------*/
358/** Bulk in transaction interface function
359 *
360 * @param[in] fun DDF function that was called.
361 * @param[in] target USB device to write to.
362 * @param[out] data Data destination.
363 * @param[in] size Size of data source.
364 * @param[in] callback Function to call on transaction completion
365 * @param[in] arg Additional for callback function.
366 * @return Error code.
367 */
368static int bulk_in(
369 ddf_fun_t *fun, usb_target_t target, void *data,
370 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
371{
372 assert(fun);
373 hc_t *hc = fun_to_hc(fun);
374 assert(hc);
375 usb_log_debug("Bulk IN %d:%d %zu.\n",
376 target.address, target.endpoint, size);
377
378 endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
379 target.address, target.endpoint, USB_DIRECTION_IN, NULL);
380 if (ep == NULL) {
381 usb_log_error("Endpoint(%d:%d) not registered for BULK IN.\n",
382 target.address, target.endpoint);
383 return ENOENT;
384 }
385 assert(ep->speed ==
386 usb_device_keeper_get_speed(&hc->manager, target.address));
387 assert(ep->transfer_type == USB_TRANSFER_BULK);
388
389 usb_transfer_batch_t *batch =
390 batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
391 ep->speed, data, size, NULL, 0, callback, NULL, arg, ep);
392 if (!batch)
393 return ENOMEM;
394 batch_bulk_in(batch);
395 const int ret = hc_schedule(hc, batch);
396 if (ret != EOK) {
397 batch_dispose(batch);
398 }
399 return ret;
400}
401/*----------------------------------------------------------------------------*/
402/** Control write 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 transfer.
407 * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
408 * @param[in] 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_write(
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_out_callback_t callback, void *arg)
418{
419 assert(fun);
420 hc_t *hc = fun_to_hc(fun);
421 assert(hc);
422 usb_speed_t speed =
423 usb_device_keeper_get_speed(&hc->manager, target.address);
424 usb_log_debug("Control WRITE (%d) %d:%d %zu.\n",
425 speed, target.address, target.endpoint, size);
426 endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
427 target.address, target.endpoint, USB_DIRECTION_BOTH, NULL);
428 if (ep == NULL) {
429 usb_log_warning("Endpoint(%d:%d) not registered for CONTROL.\n",
430 target.address, target.endpoint);
431 }
432
433 if (setup_size != 8)
434 return EINVAL;
435
436 usb_transfer_batch_t *batch =
437 batch_get(fun, target, USB_TRANSFER_CONTROL, ep->max_packet_size, speed,
438 data, size, setup_data, setup_size, NULL, callback, arg, ep);
439 if (!batch)
440 return ENOMEM;
441 usb_device_keeper_reset_if_need(&hc->manager, target, setup_data);
442 batch_control_write(batch);
443 const int ret = hc_schedule(hc, batch);
444 if (ret != EOK) {
445 batch_dispose(batch);
446 }
447 return ret;
448}
449/*----------------------------------------------------------------------------*/
450/** Control read transaction interface function
451 *
452 * @param[in] fun DDF function that was called.
453 * @param[in] target USB device to write to.
454 * @param[in] setup_data Data to send with SETUP packet.
455 * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
456 * @param[out] data Source of data.
457 * @param[in] size Size of data source.
458 * @param[in] callback Function to call on transaction completion.
459 * @param[in] arg Additional for callback function.
460 * @return Error code.
461 */
462static int control_read(
463 ddf_fun_t *fun, usb_target_t target,
464 void *setup_data, size_t setup_size, void *data, size_t size,
465 usbhc_iface_transfer_in_callback_t callback, void *arg)
466{
467 assert(fun);
468 hc_t *hc = fun_to_hc(fun);
469 assert(hc);
470 usb_speed_t speed =
471 usb_device_keeper_get_speed(&hc->manager, target.address);
472
473 usb_log_debug("Control READ(%d) %d:%d %zu.\n",
474 speed, target.address, target.endpoint, size);
475 endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
476 target.address, target.endpoint, USB_DIRECTION_BOTH, NULL);
477 if (ep == NULL) {
478 usb_log_warning("Endpoint(%d:%d) not registered for CONTROL.\n",
479 target.address, target.endpoint);
480 }
481 usb_transfer_batch_t *batch =
482 batch_get(fun, target, USB_TRANSFER_CONTROL, ep->max_packet_size, speed,
483 data, size, setup_data, setup_size, callback, NULL, arg, ep);
484 if (!batch)
485 return ENOMEM;
486 batch_control_read(batch);
487 const int ret = hc_schedule(hc, batch);
488 if (ret != EOK) {
489 batch_dispose(batch);
490 }
491 return ret;
492}
493/*----------------------------------------------------------------------------*/
494usbhc_iface_t hc_iface = {
495 .reserve_default_address = reserve_default_address,
496 .release_default_address = release_default_address,
497 .request_address = request_address,
498 .bind_address = bind_address,
499 .release_address = release_address,
500
501 .register_endpoint = register_endpoint,
502 .unregister_endpoint = unregister_endpoint,
503
504 .interrupt_out = interrupt_out,
505 .interrupt_in = interrupt_in,
506
507 .bulk_out = bulk_out,
508 .bulk_in = bulk_in,
509
510 .control_write = control_write,
511 .control_read = control_read,
512};
513/**
514 * @}
515 */
Note: See TracBrowser for help on using the repository browser.