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

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

80-column fixes

  • Property mode set to 100644
File size: 16.7 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 UNSUPPORTED("register_endpoint");
154
155 return ENOTSUP;
156}
157/*----------------------------------------------------------------------------*/
158/** Unregister endpoint (free some bandwidth reservation).
159 *
160 * @param[in] fun Device function the action was invoked on.
161 * @param[in] address USB address of the device.
162 * @param[in] endpoint Endpoint number.
163 * @param[in] direction Endpoint data direction.
164 * @return Error code.
165 */
166static int unregister_endpoint(
167 ddf_fun_t *fun, usb_address_t address,
168 usb_endpoint_t endpoint, usb_direction_t direction)
169{
170 UNSUPPORTED("unregister_endpoint");
171
172 return ENOTSUP;
173}
174/*----------------------------------------------------------------------------*/
175/** Schedule interrupt out transfer.
176 *
177 * The callback is supposed to be called once the transfer (on the wire) is
178 * complete regardless of the outcome.
179 * However, the callback could be called only when this function returns
180 * with success status (i.e. returns EOK).
181 *
182 * @param[in] fun Device function the action was invoked on.
183 * @param[in] target Target pipe (address and endpoint number) specification.
184 * @param[in] max_packet_size Max packet size for the transfer.
185 * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
186 * by the caller).
187 * @param[in] size Size of the @p data buffer in bytes.
188 * @param[in] callback Callback to be issued once the transfer is complete.
189 * @param[in] arg Pass-through argument to the callback.
190 * @return Error code.
191 */
192static int interrupt_out(
193 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
194 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
195{
196 assert(fun);
197 hc_t *hc = fun_to_hc(fun);
198 assert(hc);
199 usb_speed_t speed =
200 usb_device_keeper_get_speed(&hc->manager, target.address);
201
202 usb_log_debug("Interrupt OUT %d:%d %zu(%zu).\n",
203 target.address, target.endpoint, size, max_packet_size);
204
205 usb_transfer_batch_t *batch =
206 batch_get(fun, target, USB_TRANSFER_INTERRUPT, max_packet_size,
207 speed, data, size, NULL, 0, NULL, callback, arg, &hc->manager);
208 if (!batch)
209 return ENOMEM;
210 batch_interrupt_out(batch);
211 const int ret = hc_schedule(hc, batch);
212 if (ret != EOK) {
213 batch_dispose(batch);
214 }
215 return ret;
216}
217/*----------------------------------------------------------------------------*/
218/** Schedule interrupt in transfer.
219 *
220 * The callback is supposed to be called once the transfer (on the wire) is
221 * complete regardless of the outcome.
222 * However, the callback could be called only when this function returns
223 * with success status (i.e. returns EOK).
224 *
225 * @param[in] fun Device function the action was invoked on.
226 * @param[in] target Target pipe (address and endpoint number) specification.
227 * @param[in] max_packet_size Max packet size for the transfer.
228 * @param[in] data Buffer where to store the data (in USB endianess,
229 * allocated and deallocated by the caller).
230 * @param[in] size Size of the @p data buffer in bytes.
231 * @param[in] callback Callback to be issued once the transfer is complete.
232 * @param[in] arg Pass-through argument to the callback.
233 * @return Error code.
234 */
235static int interrupt_in(
236 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
237 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
238{
239 assert(fun);
240 hc_t *hc = fun_to_hc(fun);
241 assert(hc);
242 usb_speed_t speed =
243 usb_device_keeper_get_speed(&hc->manager, target.address);
244 usb_log_debug("Interrupt IN %d:%d %zu(%zu).\n",
245 target.address, target.endpoint, size, max_packet_size);
246
247 usb_transfer_batch_t *batch =
248 batch_get(fun, target, USB_TRANSFER_INTERRUPT, max_packet_size,
249 speed, data, size, NULL, 0, callback, NULL, arg, &hc->manager);
250 if (!batch)
251 return ENOMEM;
252 batch_interrupt_in(batch);
253 const int ret = hc_schedule(hc, batch);
254 if (ret != EOK) {
255 batch_dispose(batch);
256 }
257 return ret;
258}
259/*----------------------------------------------------------------------------*/
260/** Schedule bulk out transfer.
261 *
262 * The callback is supposed to be called once the transfer (on the wire) is
263 * complete regardless of the outcome.
264 * However, the callback could be called only when this function returns
265 * with success status (i.e. returns EOK).
266 *
267 * @param[in] fun Device function the action was invoked on.
268 * @param[in] target Target pipe (address and endpoint number) specification.
269 * @param[in] max_packet_size Max packet size for the transfer.
270 * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
271 * by the caller).
272 * @param[in] size Size of the @p data buffer in bytes.
273 * @param[in] callback Callback to be issued once the transfer is complete.
274 * @param[in] arg Pass-through argument to the callback.
275 * @return Error code.
276 */
277static int bulk_out(
278 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
279 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
280{
281 assert(fun);
282 hc_t *hc = fun_to_hc(fun);
283 assert(hc);
284 usb_speed_t speed =
285 usb_device_keeper_get_speed(&hc->manager, target.address);
286
287 usb_log_debug("Bulk OUT %d:%d %zu(%zu).\n",
288 target.address, target.endpoint, size, max_packet_size);
289
290 usb_transfer_batch_t *batch =
291 batch_get(fun, target, USB_TRANSFER_BULK, max_packet_size, speed,
292 data, size, NULL, 0, NULL, callback, arg, &hc->manager);
293 if (!batch)
294 return ENOMEM;
295 batch_bulk_out(batch);
296 const int ret = hc_schedule(hc, batch);
297 if (ret != EOK) {
298 batch_dispose(batch);
299 }
300 return ret;
301}
302/*----------------------------------------------------------------------------*/
303/** Schedule bulk in transfer.
304 *
305 * The callback is supposed to be called once the transfer (on the wire) is
306 * complete regardless of the outcome.
307 * However, the callback could be called only when this function returns
308 * with success status (i.e. returns EOK).
309 *
310 * @param[in] fun Device function the action was invoked on.
311 * @param[in] target Target pipe (address and endpoint number) specification.
312 * @param[in] max_packet_size Max packet size for the transfer.
313 * @param[in] data Buffer where to store the data (in USB endianess,
314 * allocated and deallocated by the caller).
315 * @param[in] size Size of the @p data buffer in bytes.
316 * @param[in] callback Callback to be issued once the transfer is complete.
317 * @param[in] arg Pass-through argument to the callback.
318 * @return Error code.
319 */
320static int bulk_in(
321 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
322 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
323{
324 assert(fun);
325 hc_t *hc = fun_to_hc(fun);
326 assert(hc);
327 usb_speed_t speed =
328 usb_device_keeper_get_speed(&hc->manager, target.address);
329 usb_log_debug("Bulk IN %d:%d %zu(%zu).\n",
330 target.address, target.endpoint, size, max_packet_size);
331
332 usb_transfer_batch_t *batch =
333 batch_get(fun, target, USB_TRANSFER_BULK, max_packet_size, speed,
334 data, size, NULL, 0, callback, NULL, arg, &hc->manager);
335 if (!batch)
336 return ENOMEM;
337 batch_bulk_in(batch);
338 const int ret = hc_schedule(hc, batch);
339 if (ret != EOK) {
340 batch_dispose(batch);
341 }
342 return ret;
343}
344/*----------------------------------------------------------------------------*/
345/** Schedule control write transfer.
346 *
347 * The callback is supposed to be called once the transfer (on the wire) is
348 * complete regardless of the outcome.
349 * However, the callback could be called only when this function returns
350 * with success status (i.e. returns EOK).
351 *
352 * @param[in] fun Device function the action was invoked on.
353 * @param[in] target Target pipe (address and endpoint number) specification.
354 * @param[in] max_packet_size Max packet size for the transfer.
355 * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
356 * and deallocated by the caller).
357 * @param[in] setup_packet_size Size of @p setup_packet buffer in bytes.
358 * @param[in] data_buffer Data buffer (in USB endianess, allocated and
359 * deallocated by the caller).
360 * @param[in] data_buffer_size Size of @p data_buffer buffer in bytes.
361 * @param[in] callback Callback to be issued once the transfer is complete.
362 * @param[in] arg Pass-through argument to the callback.
363 * @return Error code.
364 */
365static int control_write(
366 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size,
367 void *setup_data, size_t setup_size, void *data, size_t size,
368 usbhc_iface_transfer_out_callback_t callback, void *arg)
369{
370 assert(fun);
371 hc_t *hc = fun_to_hc(fun);
372 assert(hc);
373 usb_speed_t speed =
374 usb_device_keeper_get_speed(&hc->manager, target.address);
375 usb_log_debug("Control WRITE (%d) %d:%d %zu(%zu).\n",
376 speed, target.address, target.endpoint, size, max_packet_size);
377
378 if (setup_size != 8)
379 return EINVAL;
380
381 usb_transfer_batch_t *batch =
382 batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size,
383 speed, data, size, setup_data, setup_size, NULL, callback, arg,
384 &hc->manager);
385 if (!batch)
386 return ENOMEM;
387 usb_device_keeper_reset_if_need(&hc->manager, target, setup_data);
388 batch_control_write(batch);
389 const int ret = hc_schedule(hc, batch);
390 if (ret != EOK) {
391 batch_dispose(batch);
392 }
393 return ret;
394}
395/*----------------------------------------------------------------------------*/
396/** Schedule control read transfer.
397 *
398 * The callback is supposed to be called once the transfer (on the wire) is
399 * complete regardless of the outcome.
400 * However, the callback could be called only when this function returns
401 * with success status (i.e. returns EOK).
402 *
403 * @param[in] fun Device function the action was invoked on.
404 * @param[in] target Target pipe (address and endpoint number) specification.
405 * @param[in] max_packet_size Max packet size for the transfer.
406 * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
407 * and deallocated by the caller).
408 * @param[in] setup_packet_size Size of @p setup_packet buffer in bytes.
409 * @param[in] data_buffer Buffer where to store the data (in USB endianess,
410 * allocated and deallocated by the caller).
411 * @param[in] data_buffer_size Size of @p data_buffer buffer in bytes.
412 * @param[in] callback Callback to be issued once the transfer is complete.
413 * @param[in] arg Pass-through argument to the callback.
414 * @return Error code.
415 */
416static int control_read(
417 ddf_fun_t *fun, usb_target_t target, size_t max_packet_size,
418 void *setup_data, size_t setup_size, void *data, size_t size,
419 usbhc_iface_transfer_in_callback_t callback, void *arg)
420{
421 assert(fun);
422 hc_t *hc = fun_to_hc(fun);
423 assert(hc);
424 usb_speed_t speed =
425 usb_device_keeper_get_speed(&hc->manager, target.address);
426
427 usb_log_debug("Control READ(%d) %d:%d %zu(%zu).\n",
428 speed, target.address, target.endpoint, size, max_packet_size);
429 usb_transfer_batch_t *batch =
430 batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size,
431 speed, data, size, setup_data, setup_size, callback, NULL, arg,
432 &hc->manager);
433 if (!batch)
434 return ENOMEM;
435 batch_control_read(batch);
436 const int ret = hc_schedule(hc, batch);
437 if (ret != EOK) {
438 batch_dispose(batch);
439 }
440 return ret;
441}
442/*----------------------------------------------------------------------------*/
443/** Host controller interface implementation for OHCI. */
444usbhc_iface_t hc_iface = {
445 .reserve_default_address = reserve_default_address,
446 .release_default_address = release_default_address,
447 .request_address = request_address,
448 .bind_address = bind_address,
449 .release_address = release_address,
450
451 .register_endpoint = register_endpoint,
452 .unregister_endpoint = unregister_endpoint,
453
454 .interrupt_out = interrupt_out,
455 .interrupt_in = interrupt_in,
456
457 .bulk_out = bulk_out,
458 .bulk_in = bulk_in,
459
460 .control_write = control_write,
461 .control_read = control_read,
462};
463
464/**
465 * @}
466 */
Note: See TracBrowser for help on using the repository browser.