| 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 libusbhost
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * USB transfer transaction structures.
|
|---|
| 33 | */
|
|---|
| 34 | #ifndef LIBUSBHOST_HOST_USB_TRANSFER_BATCH_H
|
|---|
| 35 | #define LIBUSBHOST_HOST_USB_TRANSFER_BATCH_H
|
|---|
| 36 |
|
|---|
| 37 | #include <adt/list.h>
|
|---|
| 38 |
|
|---|
| 39 | #include <usbhc_iface.h>
|
|---|
| 40 | #include <usb/usb.h>
|
|---|
| 41 | #include <usb/host/endpoint.h>
|
|---|
| 42 |
|
|---|
| 43 | #define USB_SETUP_PACKET_SIZE 8
|
|---|
| 44 |
|
|---|
| 45 | typedef struct usb_transfer_batch usb_transfer_batch_t;
|
|---|
| 46 | /** Structure stores additional data needed for communication with EP */
|
|---|
| 47 | struct usb_transfer_batch {
|
|---|
| 48 | /** Endpoint used for communication */
|
|---|
| 49 | endpoint_t *ep;
|
|---|
| 50 | /** Function called on completion (IN version) */
|
|---|
| 51 | usbhc_iface_transfer_in_callback_t callback_in;
|
|---|
| 52 | /** Function called on completion (OUT version) */
|
|---|
| 53 | usbhc_iface_transfer_out_callback_t callback_out;
|
|---|
| 54 | /** Argument to pass to the completion function */
|
|---|
| 55 | void *arg;
|
|---|
| 56 | /** Place for data to send/receive */
|
|---|
| 57 | char *buffer;
|
|---|
| 58 | /** Size of memory pointed to by buffer member */
|
|---|
| 59 | size_t buffer_size;
|
|---|
| 60 | /** Place to store SETUP data needed by control transfers */
|
|---|
| 61 | char setup_buffer[USB_SETUP_PACKET_SIZE];
|
|---|
| 62 | /** Used portion of setup_buffer member
|
|---|
| 63 | *
|
|---|
| 64 | * SETUP buffer must be 8 bytes for control transfers and is left
|
|---|
| 65 | * unused for all other transfers. Thus, this field is either 0 or 8.
|
|---|
| 66 | */
|
|---|
| 67 | size_t setup_size;
|
|---|
| 68 | /** Actually used portion of the buffer */
|
|---|
| 69 | size_t transfered_size;
|
|---|
| 70 | /** Indicates success/failure of the communication */
|
|---|
| 71 | int error;
|
|---|
| 72 | /** Host controller function, passed to callback function */
|
|---|
| 73 | ddf_fun_t *fun;
|
|---|
| 74 |
|
|---|
| 75 | /** Driver specific data */
|
|---|
| 76 | void *private_data;
|
|---|
| 77 | /** Callback to properly remove driver data during destruction */
|
|---|
| 78 | void (*private_data_dtor)(void *p_data);
|
|---|
| 79 | };
|
|---|
| 80 |
|
|---|
| 81 | /** Printf formatting string for dumping usb_transfer_batch_t. */
|
|---|
| 82 | #define USB_TRANSFER_BATCH_FMT "[%d:%d %s %s-%s %zuB/%zu]"
|
|---|
| 83 |
|
|---|
| 84 | /** Printf arguments for dumping usb_transfer_batch_t.
|
|---|
| 85 | * @param batch USB transfer batch to be dumped.
|
|---|
| 86 | */
|
|---|
| 87 | #define USB_TRANSFER_BATCH_ARGS(batch) \
|
|---|
| 88 | (batch).ep->address, (batch).ep->endpoint, \
|
|---|
| 89 | usb_str_speed((batch).ep->speed), \
|
|---|
| 90 | usb_str_transfer_type_short((batch).ep->transfer_type), \
|
|---|
| 91 | usb_str_direction((batch).ep->direction), \
|
|---|
| 92 | (batch).buffer_size, (batch).ep->max_packet_size
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | usb_transfer_batch_t * usb_transfer_batch_get(
|
|---|
| 96 | endpoint_t *ep,
|
|---|
| 97 | char *buffer,
|
|---|
| 98 | size_t buffer_size,
|
|---|
| 99 | uint64_t setup_buffer,
|
|---|
| 100 | usbhc_iface_transfer_in_callback_t func_in,
|
|---|
| 101 | usbhc_iface_transfer_out_callback_t func_out,
|
|---|
| 102 | void *arg,
|
|---|
| 103 | ddf_fun_t *fun,
|
|---|
| 104 | void *private_data,
|
|---|
| 105 | void (*private_data_dtor)(void *p_data)
|
|---|
| 106 | );
|
|---|
| 107 |
|
|---|
| 108 | void usb_transfer_batch_finish(usb_transfer_batch_t *instance,
|
|---|
| 109 | const void* data, size_t size);
|
|---|
| 110 | void usb_transfer_batch_call_in(usb_transfer_batch_t *instance);
|
|---|
| 111 | void usb_transfer_batch_call_out(usb_transfer_batch_t *instance);
|
|---|
| 112 | void usb_transfer_batch_dispose(usb_transfer_batch_t *instance);
|
|---|
| 113 |
|
|---|
| 114 | /** Helper function, calls callback and correctly destroys batch structure.
|
|---|
| 115 | *
|
|---|
| 116 | * @param[in] instance Batch structure to use.
|
|---|
| 117 | */
|
|---|
| 118 | static inline void usb_transfer_batch_call_in_and_dispose(
|
|---|
| 119 | usb_transfer_batch_t *instance)
|
|---|
| 120 | {
|
|---|
| 121 | assert(instance);
|
|---|
| 122 | usb_transfer_batch_call_in(instance);
|
|---|
| 123 | usb_transfer_batch_dispose(instance);
|
|---|
| 124 | }
|
|---|
| 125 | /*----------------------------------------------------------------------------*/
|
|---|
| 126 | /** Helper function calls callback and correctly destroys batch structure.
|
|---|
| 127 | *
|
|---|
| 128 | * @param[in] instance Batch structure to use.
|
|---|
| 129 | */
|
|---|
| 130 | static inline void usb_transfer_batch_call_out_and_dispose(
|
|---|
| 131 | usb_transfer_batch_t *instance)
|
|---|
| 132 | {
|
|---|
| 133 | assert(instance);
|
|---|
| 134 | usb_transfer_batch_call_out(instance);
|
|---|
| 135 | usb_transfer_batch_dispose(instance);
|
|---|
| 136 | }
|
|---|
| 137 | /*----------------------------------------------------------------------------*/
|
|---|
| 138 | /** Helper function, sets error value and finishes transfer.
|
|---|
| 139 | *
|
|---|
| 140 | * @param[in] instance Batch structure to use.
|
|---|
| 141 | * @param[in] data Data to copy to the output buffer.
|
|---|
| 142 | * @param[in] size Size of @p data.
|
|---|
| 143 | * @param[in] error Set batch status to this error value.
|
|---|
| 144 | */
|
|---|
| 145 | static inline void usb_transfer_batch_finish_error(
|
|---|
| 146 | usb_transfer_batch_t *instance, const void* data, size_t size, int error)
|
|---|
| 147 | {
|
|---|
| 148 | assert(instance);
|
|---|
| 149 | instance->error = error;
|
|---|
| 150 | usb_transfer_batch_finish(instance, data, size);
|
|---|
| 151 | }
|
|---|
| 152 | /*----------------------------------------------------------------------------*/
|
|---|
| 153 | /** Helper function, determines batch direction absed on the present callbacks
|
|---|
| 154 | * @param[in] instance Batch structure to use.
|
|---|
| 155 | * @return USB_DIRECTION_IN, or USB_DIRECTION_OUT.
|
|---|
| 156 | */
|
|---|
| 157 | static inline usb_direction_t usb_transfer_batch_direction(
|
|---|
| 158 | const usb_transfer_batch_t *instance)
|
|---|
| 159 | {
|
|---|
| 160 | assert(instance);
|
|---|
| 161 | if (instance->callback_in) {
|
|---|
| 162 | assert(instance->callback_out == NULL);
|
|---|
| 163 | assert(instance->ep == NULL
|
|---|
| 164 | || instance->ep->transfer_type == USB_TRANSFER_CONTROL
|
|---|
| 165 | || instance->ep->direction == USB_DIRECTION_IN);
|
|---|
| 166 | return USB_DIRECTION_IN;
|
|---|
| 167 | }
|
|---|
| 168 | if (instance->callback_out) {
|
|---|
| 169 | assert(instance->callback_in == NULL);
|
|---|
| 170 | assert(instance->ep == NULL
|
|---|
| 171 | || instance->ep->transfer_type == USB_TRANSFER_CONTROL
|
|---|
| 172 | || instance->ep->direction == USB_DIRECTION_OUT);
|
|---|
| 173 | return USB_DIRECTION_OUT;
|
|---|
| 174 | }
|
|---|
| 175 | assert(false);
|
|---|
| 176 | }
|
|---|
| 177 | #endif
|
|---|
| 178 | /**
|
|---|
| 179 | * @}
|
|---|
| 180 | */
|
|---|