[c56dbe0] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 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 usb
|
---|
| 29 | * @{
|
---|
| 30 | */
|
---|
| 31 | /** @file
|
---|
| 32 | * @brief UHCI driver
|
---|
| 33 | */
|
---|
[89a0485a] | 34 | #include <errno.h>
|
---|
[db7ed07] | 35 | #include <mem.h>
|
---|
[89a0485a] | 36 |
|
---|
[afcd86e] | 37 | #include <usb/debug.h>
|
---|
| 38 |
|
---|
[89a0485a] | 39 | #include "callback.h"
|
---|
| 40 | int callback_init(callback_t *instance, device_t *dev,
|
---|
| 41 | void *buffer, size_t size, usbhc_iface_transfer_in_callback_t func_in,
|
---|
| 42 | usbhc_iface_transfer_out_callback_t func_out, void *arg)
|
---|
| 43 | {
|
---|
| 44 | assert(instance);
|
---|
| 45 | assert(func_in == NULL || func_out == NULL);
|
---|
[44d8853] | 46 | if (size > 0) {
|
---|
| 47 | instance->new_buffer = malloc32(size);
|
---|
| 48 | if (!instance->new_buffer) {
|
---|
[afcd86e] | 49 | usb_log_error("Failed to allocate device acessible buffer.\n");
|
---|
[44d8853] | 50 | return ENOMEM;
|
---|
| 51 | }
|
---|
| 52 | if (func_out)
|
---|
| 53 | memcpy(instance->new_buffer, buffer, size);
|
---|
| 54 | } else {
|
---|
| 55 | instance->new_buffer = NULL;
|
---|
[89a0485a] | 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | instance->callback_out = func_out;
|
---|
| 60 | instance->callback_in = func_in;
|
---|
| 61 | instance->old_buffer = buffer;
|
---|
| 62 | instance->buffer_size = size;
|
---|
| 63 | instance->dev = dev;
|
---|
[0b68b7c] | 64 | instance->arg = arg;
|
---|
[89a0485a] | 65 | return EOK;
|
---|
| 66 | }
|
---|
[db7ed07] | 67 | /*----------------------------------------------------------------------------*/
|
---|
| 68 | void callback_run(
|
---|
| 69 | callback_t *instance, usb_transaction_outcome_t outcome, size_t act_size)
|
---|
| 70 | {
|
---|
| 71 | assert(instance);
|
---|
| 72 |
|
---|
| 73 | /* update the old buffer */
|
---|
[d956aa5] | 74 | if (instance->new_buffer &&
|
---|
| 75 | (instance->new_buffer != instance->old_buffer)) {
|
---|
| 76 | memcpy(instance->old_buffer, instance->new_buffer, instance->buffer_size);
|
---|
[de0e6b3] | 77 | free32(instance->new_buffer);
|
---|
[db7ed07] | 78 | instance->new_buffer = NULL;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | if (instance->callback_in) {
|
---|
| 82 | assert(instance->callback_out == NULL);
|
---|
[afcd86e] | 83 | usb_log_debug("Callback in: %p %x %d.\n",
|
---|
[0b68b7c] | 84 | instance->callback_in, outcome, act_size);
|
---|
[db7ed07] | 85 | instance->callback_in(
|
---|
[67a1b78] | 86 | instance->dev, outcome, act_size, instance->arg);
|
---|
[0b68b7c] | 87 | } else {
|
---|
| 88 | assert(instance->callback_out);
|
---|
| 89 | assert(instance->callback_in == NULL);
|
---|
[afcd86e] | 90 | usb_log_debug("Callback out: %p %p %x %p .\n",
|
---|
[0b68b7c] | 91 | instance->callback_out, instance->dev, outcome, instance->arg);
|
---|
| 92 | instance->callback_out(
|
---|
| 93 | instance->dev, outcome, instance->arg);
|
---|
[db7ed07] | 94 | }
|
---|
| 95 | }
|
---|
[c56dbe0] | 96 | /**
|
---|
| 97 | * @}
|
---|
| 98 | */
|
---|