source: mainline/uspace/drv/ohci/iface.c@ a546687

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

Use sane key management in usb_endpoint_manager

  • Property mode set to 100644
File size: 17.3 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
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 drvusbohci
29 * @{
30 */
31/** @file
32 * USB-HC interface implementation.
33 */
34#include <ddf/driver.h>
35#include <errno.h>
36
37#include <usb/debug.h>
38
39#include "iface.h"
40#include "hc.h"
41
42#define UNSUPPORTED(methodname) \
43 usb_log_warning("Unsupported interface method `%s()' in %s:%d.\n", \
44 methodname, __FILE__, __LINE__)
45
46/** Reserve default address.
47 *
48 * This function may block the caller.
49 *
50 * @param[in] fun Device function the action was invoked on.
51 * @param[in] speed Speed of the device for which the default address is
52 * reserved.
53 * @return Error code.
54 */
55static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
56{
57 assert(fun);
58 hc_t *hc = fun_to_hc(fun);
59 assert(hc);
60 usb_log_debug("Default address request with speed %d.\n", speed);
61 usb_device_keeper_reserve_default_address(&hc->manager, speed);
62 return EOK;
63}
64/*----------------------------------------------------------------------------*/
65/** Release default address.
66 *
67 * @param[in] fun Device function the action was invoked on.
68 * @return Error code.
69 */
70static int release_default_address(ddf_fun_t *fun)
71{
72 assert(fun);
73 hc_t *hc = fun_to_hc(fun);
74 assert(hc);
75 usb_log_debug("Default address release.\n");
76 usb_device_keeper_release_default_address(&hc->manager);
77 return EOK;
78}
79/*----------------------------------------------------------------------------*/
80/** Found free USB address.
81 *
82 * @param[in] fun Device function the action was invoked on.
83 * @param[in] speed Speed of the device that will get this address.
84 * @param[out] address Non-null pointer where to store the free address.
85 * @return Error code.
86 */
87static int request_address(
88 ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
89{
90 assert(fun);
91 hc_t *hc = fun_to_hc(fun);
92 assert(hc);
93 assert(address);
94
95 usb_log_debug("Address request with speed %d.\n", speed);
96 *address = device_keeper_get_free_address(&hc->manager, speed);
97 usb_log_debug("Address request with result: %d.\n", *address);
98 if (*address <= 0)
99 return *address;
100 return EOK;
101}
102/*----------------------------------------------------------------------------*/
103/** Bind USB address with device devman handle.
104 *
105 * @param[in] fun Device function the action was invoked on.
106 * @param[in] address USB address of the device.
107 * @param[in] handle Devman handle of the device.
108 * @return Error code.
109 */
110static int bind_address(
111 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
112{
113 assert(fun);
114 hc_t *hc = fun_to_hc(fun);
115 assert(hc);
116 usb_log_debug("Address bind %d-%d.\n", address, handle);
117 usb_device_keeper_bind(&hc->manager, address, handle);
118 return EOK;
119}
120/*----------------------------------------------------------------------------*/
121/** Release previously requested address.
122 *
123 * @param[in] fun Device function the action was invoked on.
124 * @param[in] address USB address to be released.
125 * @return Error code.
126 */
127static int release_address(ddf_fun_t *fun, usb_address_t address)
128{
129 assert(fun);
130 hc_t *hc = fun_to_hc(fun);
131 assert(hc);
132 usb_log_debug("Address release %d.\n", address);
133 usb_device_keeper_release(&hc->manager, address);
134 return EOK;
135}
136/*----------------------------------------------------------------------------*/
137/** Register endpoint for bandwidth reservation.
138 *
139 * @param[in] fun Device function the action was invoked on.
140 * @param[in] address USB address of the device.
141 * @param[in] endpoint Endpoint number.
142 * @param[in] transfer_type USB transfer type.
143 * @param[in] direction Endpoint data direction.
144 * @param[in] max_packet_size Max packet size of the endpoint.
145 * @param[in] interval Polling interval.
146 * @return Error code.
147 */
148static int register_endpoint(
149 ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
150 usb_transfer_type_t transfer_type, usb_direction_t direction,
151 size_t max_packet_size, unsigned int interval)
152{
153 assert(fun);
154 hc_t *hc = fun_to_hc(fun);
155 assert(hc);
156 if (address == hc->rh.address)
157 return EOK;
158 const usb_speed_t speed =
159 usb_device_keeper_get_speed(&hc->manager, address);
160 const size_t size = max_packet_size;
161 usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
162 address, endpoint, usb_str_transfer_type(transfer_type),
163 usb_str_speed(speed), direction, size, max_packet_size, interval);
164 // TODO use real endpoint here!
165 return usb_endpoint_manager_register_ep(&hc->ep_manager,NULL, 0);
166}
167/*----------------------------------------------------------------------------*/
168/** Unregister endpoint (free some bandwidth reservation).
169 *
170 * @param[in] fun Device function the action was invoked on.
171 * @param[in] address USB address of the device.
172 * @param[in] endpoint Endpoint number.
173 * @param[in] direction Endpoint data direction.
174 * @return Error code.
175 */
176static int unregister_endpoint(
177 ddf_fun_t *fun, usb_address_t address,
178 usb_endpoint_t endpoint, usb_direction_t direction)
179{
180 assert(fun);
181 hc_t *hc = fun_to_hc(fun);
182 assert(hc);
183 usb_log_debug("Unregister endpoint %d:%d %d.\n",
184 address, endpoint, direction);
185 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
186 endpoint, direction);
187}
188/*----------------------------------------------------------------------------*/
189/** Schedule interrupt out transfer.
190 *
191 * The callback is supposed to be called once the transfer (on the wire) is
192 * complete regardless of the outcome.
193 * However, the callback could be called only when this function returns
194 * with success status (i.e. returns EOK).
195 *
196 * @param[in] fun Device function the action was invoked on.
197 * @param[in] target Target pipe (address and endpoint number) specification.
198 * @param[in] max_packet_size Max packet size for the transfer.
199 * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
200 * by the caller).
201 * @param[in] size Size of the @p data buffer in bytes.
202 * @param[in] callback Callback to be issued once the transfer is complete.
203 * @param[in] arg Pass-through argument to the callback.
204 * @return Error code.
205 */
206static int interrupt_out(
207 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
208 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
209{
210 assert(fun);
211 hc_t *hc = fun_to_hc(fun);
212 assert(hc);
213 usb_speed_t speed =
214 usb_device_keeper_get_speed(&hc->manager, target.address);
215
216 usb_log_debug("Interrupt OUT %d:%d %zu(%zu).\n",
217 target.address, target.endpoint, size, max_packet_size);
218
219 usb_transfer_batch_t *batch =
220 batch_get(fun, target, USB_TRANSFER_INTERRUPT, max_packet_size,
221 speed, data, size, NULL, 0, NULL, callback, arg, &hc->manager);
222 if (!batch)
223 return ENOMEM;
224 batch_interrupt_out(batch);
225 const int ret = hc_schedule(hc, batch);
226 if (ret != EOK) {
227 batch_dispose(batch);
228 }
229 return ret;
230}
231/*----------------------------------------------------------------------------*/
232/** Schedule interrupt in transfer.
233 *
234 * The callback is supposed to be called once the transfer (on the wire) is
235 * complete regardless of the outcome.
236 * However, the callback could be called only when this function returns
237 * with success status (i.e. returns EOK).
238 *
239 * @param[in] fun Device function the action was invoked on.
240 * @param[in] target Target pipe (address and endpoint number) specification.
241 * @param[in] max_packet_size Max packet size for the transfer.
242 * @param[in] data Buffer where to store the data (in USB endianess,
243 * allocated and deallocated by the caller).
244 * @param[in] size Size of the @p data buffer in bytes.
245 * @param[in] callback Callback to be issued once the transfer is complete.
246 * @param[in] arg Pass-through argument to the callback.
247 * @return Error code.
248 */
249static int interrupt_in(
250 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
251 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
252{
253 assert(fun);
254 hc_t *hc = fun_to_hc(fun);
255 assert(hc);
256 usb_speed_t speed =
257 usb_device_keeper_get_speed(&hc->manager, target.address);
258 usb_log_debug("Interrupt IN %d:%d %zu(%zu).\n",
259 target.address, target.endpoint, size, max_packet_size);
260
261 usb_transfer_batch_t *batch =
262 batch_get(fun, target, USB_TRANSFER_INTERRUPT, max_packet_size,
263 speed, data, size, NULL, 0, callback, NULL, arg, &hc->manager);
264 if (!batch)
265 return ENOMEM;
266 batch_interrupt_in(batch);
267 const int ret = hc_schedule(hc, batch);
268 if (ret != EOK) {
269 batch_dispose(batch);
270 }
271 return ret;
272}
273/*----------------------------------------------------------------------------*/
274/** Schedule bulk out transfer.
275 *
276 * The callback is supposed to be called once the transfer (on the wire) is
277 * complete regardless of the outcome.
278 * However, the callback could be called only when this function returns
279 * with success status (i.e. returns EOK).
280 *
281 * @param[in] fun Device function the action was invoked on.
282 * @param[in] target Target pipe (address and endpoint number) specification.
283 * @param[in] max_packet_size Max packet size for the transfer.
284 * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
285 * by the caller).
286 * @param[in] size Size of the @p data buffer in bytes.
287 * @param[in] callback Callback to be issued once the transfer is complete.
288 * @param[in] arg Pass-through argument to the callback.
289 * @return Error code.
290 */
291static int bulk_out(
292 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
293 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
294{
295 assert(fun);
296 hc_t *hc = fun_to_hc(fun);
297 assert(hc);
298 usb_speed_t speed =
299 usb_device_keeper_get_speed(&hc->manager, target.address);
300
301 usb_log_debug("Bulk OUT %d:%d %zu(%zu).\n",
302 target.address, target.endpoint, size, max_packet_size);
303
304 usb_transfer_batch_t *batch =
305 batch_get(fun, target, USB_TRANSFER_BULK, max_packet_size, speed,
306 data, size, NULL, 0, NULL, callback, arg, &hc->manager);
307 if (!batch)
308 return ENOMEM;
309 batch_bulk_out(batch);
310 const int ret = hc_schedule(hc, batch);
311 if (ret != EOK) {
312 batch_dispose(batch);
313 }
314 return ret;
315}
316/*----------------------------------------------------------------------------*/
317/** Schedule bulk in transfer.
318 *
319 * The callback is supposed to be called once the transfer (on the wire) is
320 * complete regardless of the outcome.
321 * However, the callback could be called only when this function returns
322 * with success status (i.e. returns EOK).
323 *
324 * @param[in] fun Device function the action was invoked on.
325 * @param[in] target Target pipe (address and endpoint number) specification.
326 * @param[in] max_packet_size Max packet size for the transfer.
327 * @param[in] data Buffer where to store the data (in USB endianess,
328 * allocated and deallocated by the caller).
329 * @param[in] size Size of the @p data buffer in bytes.
330 * @param[in] callback Callback to be issued once the transfer is complete.
331 * @param[in] arg Pass-through argument to the callback.
332 * @return Error code.
333 */
334static int bulk_in(
335 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
336 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
337{
338 assert(fun);
339 hc_t *hc = fun_to_hc(fun);
340 assert(hc);
341 usb_speed_t speed =
342 usb_device_keeper_get_speed(&hc->manager, target.address);
343 usb_log_debug("Bulk IN %d:%d %zu(%zu).\n",
344 target.address, target.endpoint, size, max_packet_size);
345
346 usb_transfer_batch_t *batch =
347 batch_get(fun, target, USB_TRANSFER_BULK, max_packet_size, speed,
348 data, size, NULL, 0, callback, NULL, arg, &hc->manager);
349 if (!batch)
350 return ENOMEM;
351 batch_bulk_in(batch);
352 const int ret = hc_schedule(hc, batch);
353 if (ret != EOK) {
354 batch_dispose(batch);
355 }
356 return ret;
357}
358/*----------------------------------------------------------------------------*/
359/** Schedule control write transfer.
360 *
361 * The callback is supposed to be called once the transfer (on the wire) is
362 * complete regardless of the outcome.
363 * However, the callback could be called only when this function returns
364 * with success status (i.e. returns EOK).
365 *
366 * @param[in] fun Device function the action was invoked on.
367 * @param[in] target Target pipe (address and endpoint number) specification.
368 * @param[in] max_packet_size Max packet size for the transfer.
369 * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
370 * and deallocated by the caller).
371 * @param[in] setup_packet_size Size of @p setup_packet buffer in bytes.
372 * @param[in] data_buffer Data buffer (in USB endianess, allocated and
373 * deallocated by the caller).
374 * @param[in] data_buffer_size Size of @p data_buffer buffer in bytes.
375 * @param[in] callback Callback to be issued once the transfer is complete.
376 * @param[in] arg Pass-through argument to the callback.
377 * @return Error code.
378 */
379static int control_write(
380 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size,
381 void *setup_data, size_t setup_size, void *data, size_t size,
382 usbhc_iface_transfer_out_callback_t callback, void *arg)
383{
384 assert(fun);
385 hc_t *hc = fun_to_hc(fun);
386 assert(hc);
387 usb_speed_t speed =
388 usb_device_keeper_get_speed(&hc->manager, target.address);
389 usb_log_debug("Control WRITE (%d) %d:%d %zu(%zu).\n",
390 speed, target.address, target.endpoint, size, max_packet_size);
391
392 if (setup_size != 8)
393 return EINVAL;
394
395 usb_transfer_batch_t *batch =
396 batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size,
397 speed, data, size, setup_data, setup_size, NULL, callback, arg,
398 &hc->manager);
399 if (!batch)
400 return ENOMEM;
401 usb_device_keeper_reset_if_need(&hc->manager, target, setup_data);
402 batch_control_write(batch);
403 const int ret = hc_schedule(hc, batch);
404 if (ret != EOK) {
405 batch_dispose(batch);
406 }
407 return ret;
408}
409/*----------------------------------------------------------------------------*/
410/** Schedule control read transfer.
411 *
412 * The callback is supposed to be called once the transfer (on the wire) is
413 * complete regardless of the outcome.
414 * However, the callback could be called only when this function returns
415 * with success status (i.e. returns EOK).
416 *
417 * @param[in] fun Device function the action was invoked on.
418 * @param[in] target Target pipe (address and endpoint number) specification.
419 * @param[in] max_packet_size Max packet size for the transfer.
420 * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
421 * and deallocated by the caller).
422 * @param[in] setup_packet_size Size of @p setup_packet buffer in bytes.
423 * @param[in] data_buffer Buffer where to store the data (in USB endianess,
424 * allocated and deallocated by the caller).
425 * @param[in] data_buffer_size Size of @p data_buffer buffer in bytes.
426 * @param[in] callback Callback to be issued once the transfer is complete.
427 * @param[in] arg Pass-through argument to the callback.
428 * @return Error code.
429 */
430static int control_read(
431 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size,
432 void *setup_data, size_t setup_size, void *data, size_t size,
433 usbhc_iface_transfer_in_callback_t callback, void *arg)
434{
435 assert(fun);
436 hc_t *hc = fun_to_hc(fun);
437 assert(hc);
438 usb_speed_t speed =
439 usb_device_keeper_get_speed(&hc->manager, target.address);
440
441 usb_log_debug("Control READ(%d) %d:%d %zu(%zu).\n",
442 speed, target.address, target.endpoint, size, max_packet_size);
443 usb_transfer_batch_t *batch =
444 batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size,
445 speed, data, size, setup_data, setup_size, callback, NULL, arg,
446 &hc->manager);
447 if (!batch)
448 return ENOMEM;
449 batch_control_read(batch);
450 const int ret = hc_schedule(hc, batch);
451 if (ret != EOK) {
452 batch_dispose(batch);
453 }
454 return ret;
455}
456/*----------------------------------------------------------------------------*/
457/** Host controller interface implementation for OHCI. */
458usbhc_iface_t hc_iface = {
459 .reserve_default_address = reserve_default_address,
460 .release_default_address = release_default_address,
461 .request_address = request_address,
462 .bind_address = bind_address,
463 .release_address = release_address,
464
465 .register_endpoint = register_endpoint,
466 .unregister_endpoint = unregister_endpoint,
467
468 .interrupt_out = interrupt_out,
469 .interrupt_in = interrupt_in,
470
471 .bulk_out = bulk_out,
472 .bulk_in = bulk_in,
473
474 .control_write = control_write,
475 .control_read = control_read,
476};
477
478/**
479 * @}
480 */
Note: See TracBrowser for help on using the repository browser.