[41b96b4] | 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 | */
|
---|
[81dce9f] | 28 | /** @addtogroup drvusbohci
|
---|
[41b96b4] | 29 | * @{
|
---|
| 30 | */
|
---|
| 31 | /** @file
|
---|
| 32 | * @brief OHCI driver USB transaction structure
|
---|
| 33 | */
|
---|
| 34 | #include <errno.h>
|
---|
| 35 | #include <str_error.h>
|
---|
| 36 |
|
---|
| 37 | #include <usb/usb.h>
|
---|
| 38 | #include <usb/debug.h>
|
---|
| 39 |
|
---|
| 40 | #include "batch.h"
|
---|
[2bf8f8c] | 41 | #include "utils/malloc32.h"
|
---|
[41b96b4] | 42 |
|
---|
[d328172] | 43 | static void batch_call_in_and_dispose(batch_t *instance);
|
---|
| 44 | static void batch_call_out_and_dispose(batch_t *instance);
|
---|
| 45 |
|
---|
[81dce9f] | 46 | #define DEFAULT_ERROR_COUNT 3
|
---|
[be7950e8] | 47 | batch_t * batch_get(
|
---|
| 48 | ddf_fun_t *fun,
|
---|
| 49 | usb_target_t target,
|
---|
| 50 | usb_transfer_type_t transfer_type,
|
---|
| 51 | size_t max_packet_size,
|
---|
| 52 | usb_speed_t speed,
|
---|
| 53 | char *buffer,
|
---|
[2bf8f8c] | 54 | size_t buffer_size,
|
---|
[be7950e8] | 55 | char *setup_buffer,
|
---|
| 56 | size_t setup_size,
|
---|
| 57 | usbhc_iface_transfer_in_callback_t func_in,
|
---|
| 58 | usbhc_iface_transfer_out_callback_t func_out,
|
---|
| 59 | void *arg,
|
---|
[68b5ed6e] | 60 | usb_device_keeper_t *manager
|
---|
[be7950e8] | 61 | )
|
---|
| 62 | {
|
---|
[2bf8f8c] | 63 | #define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
|
---|
| 64 | if (ptr == NULL) { \
|
---|
| 65 | usb_log_error(message); \
|
---|
| 66 | if (instance) { \
|
---|
| 67 | batch_dispose(instance); \
|
---|
| 68 | } \
|
---|
| 69 | return NULL; \
|
---|
| 70 | } else (void)0
|
---|
| 71 |
|
---|
| 72 | batch_t *instance = malloc(sizeof(batch_t));
|
---|
| 73 | CHECK_NULL_DISPOSE_RETURN(instance,
|
---|
| 74 | "Failed to allocate batch instance.\n");
|
---|
| 75 | batch_init(instance, target, transfer_type, speed, max_packet_size,
|
---|
| 76 | buffer, NULL, buffer_size, NULL, setup_size, func_in,
|
---|
| 77 | func_out, arg, fun, NULL);
|
---|
| 78 |
|
---|
| 79 | if (buffer_size > 0) {
|
---|
| 80 | instance->transport_buffer = malloc32(buffer_size);
|
---|
| 81 | CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer,
|
---|
| 82 | "Failed to allocate device accessible buffer.\n");
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | if (setup_size > 0) {
|
---|
| 86 | instance->setup_buffer = malloc32(setup_size);
|
---|
| 87 | CHECK_NULL_DISPOSE_RETURN(instance->setup_buffer,
|
---|
| 88 | "Failed to allocate device accessible setup buffer.\n");
|
---|
| 89 | memcpy(instance->setup_buffer, setup_buffer, setup_size);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | return instance;
|
---|
[be7950e8] | 94 | }
|
---|
| 95 | /*----------------------------------------------------------------------------*/
|
---|
| 96 | void batch_dispose(batch_t *instance)
|
---|
| 97 | {
|
---|
[2bf8f8c] | 98 | assert(instance);
|
---|
[d328172] | 99 | free32(instance->transport_buffer);
|
---|
| 100 | free32(instance->setup_buffer);
|
---|
| 101 | free(instance);
|
---|
[be7950e8] | 102 | }
|
---|
| 103 | /*----------------------------------------------------------------------------*/
|
---|
| 104 | void batch_control_write(batch_t *instance)
|
---|
| 105 | {
|
---|
[2bf8f8c] | 106 | assert(instance);
|
---|
[d328172] | 107 | /* We are data out, we are supposed to provide data */
|
---|
| 108 | memcpy(instance->transport_buffer, instance->buffer,
|
---|
| 109 | instance->buffer_size);
|
---|
| 110 | instance->next_step = batch_call_out_and_dispose;
|
---|
| 111 | /* TODO: implement */
|
---|
| 112 | usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
|
---|
[be7950e8] | 113 | }
|
---|
| 114 | /*----------------------------------------------------------------------------*/
|
---|
| 115 | void batch_control_read(batch_t *instance)
|
---|
| 116 | {
|
---|
[2bf8f8c] | 117 | assert(instance);
|
---|
[d328172] | 118 | instance->next_step = batch_call_in_and_dispose;
|
---|
| 119 | /* TODO: implement */
|
---|
| 120 | usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
|
---|
[be7950e8] | 121 | }
|
---|
| 122 | /*----------------------------------------------------------------------------*/
|
---|
| 123 | void batch_interrupt_in(batch_t *instance)
|
---|
| 124 | {
|
---|
[2bf8f8c] | 125 | assert(instance);
|
---|
[d328172] | 126 | instance->direction = USB_DIRECTION_IN;
|
---|
| 127 | instance->next_step = batch_call_in_and_dispose;
|
---|
| 128 | /* TODO: implement */
|
---|
| 129 | usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
|
---|
[be7950e8] | 130 | }
|
---|
| 131 | /*----------------------------------------------------------------------------*/
|
---|
| 132 | void batch_interrupt_out(batch_t *instance)
|
---|
| 133 | {
|
---|
[2bf8f8c] | 134 | assert(instance);
|
---|
[d328172] | 135 | instance->direction = USB_DIRECTION_OUT;
|
---|
| 136 | /* We are data out, we are supposed to provide data */
|
---|
| 137 | memcpy(instance->transport_buffer, instance->buffer,
|
---|
| 138 | instance->buffer_size);
|
---|
| 139 | instance->next_step = batch_call_out_and_dispose;
|
---|
| 140 | /* TODO: implement */
|
---|
| 141 | usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
|
---|
[be7950e8] | 142 | }
|
---|
| 143 | /*----------------------------------------------------------------------------*/
|
---|
| 144 | void batch_bulk_in(batch_t *instance)
|
---|
| 145 | {
|
---|
[2bf8f8c] | 146 | assert(instance);
|
---|
[d328172] | 147 | instance->direction = USB_DIRECTION_IN;
|
---|
| 148 | instance->next_step = batch_call_in_and_dispose;
|
---|
| 149 | /* TODO: implement */
|
---|
| 150 | usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
|
---|
[be7950e8] | 151 | }
|
---|
| 152 | /*----------------------------------------------------------------------------*/
|
---|
| 153 | void batch_bulk_out(batch_t *instance)
|
---|
| 154 | {
|
---|
[2bf8f8c] | 155 | assert(instance);
|
---|
[d328172] | 156 | instance->direction = USB_DIRECTION_IN;
|
---|
| 157 | instance->next_step = batch_call_in_and_dispose;
|
---|
| 158 | /* TODO: implement */
|
---|
| 159 | usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
|
---|
| 160 | }
|
---|
| 161 | /*----------------------------------------------------------------------------*/
|
---|
| 162 | /** Helper function calls callback and correctly disposes of batch structure.
|
---|
| 163 | *
|
---|
| 164 | * @param[in] instance Batch structure to use.
|
---|
| 165 | */
|
---|
| 166 | void batch_call_in_and_dispose(batch_t *instance)
|
---|
| 167 | {
|
---|
| 168 | assert(instance);
|
---|
| 169 | batch_call_in(instance);
|
---|
| 170 | batch_dispose(instance);
|
---|
| 171 | }
|
---|
| 172 | /*----------------------------------------------------------------------------*/
|
---|
| 173 | /** Helper function calls callback and correctly disposes of batch structure.
|
---|
| 174 | *
|
---|
| 175 | * @param[in] instance Batch structure to use.
|
---|
| 176 | */
|
---|
| 177 | void batch_call_out_and_dispose(batch_t *instance)
|
---|
| 178 | {
|
---|
| 179 | assert(instance);
|
---|
| 180 | batch_call_out(instance);
|
---|
| 181 | batch_dispose(instance);
|
---|
[be7950e8] | 182 | }
|
---|
[41b96b4] | 183 | /**
|
---|
| 184 | * @}
|
---|
| 185 | */
|
---|