[81dce9f] | 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 | */
|
---|
[160b75e] | 28 | /** @addtogroup libusbhost
|
---|
[81dce9f] | 29 | * @{
|
---|
| 30 | */
|
---|
| 31 | /** @file
|
---|
[c92c13f] | 32 | * USB transfer transaction structures (implementation).
|
---|
[81dce9f] | 33 | */
|
---|
| 34 |
|
---|
[8d2e251] | 35 | #include <usb/host/usb_transfer_batch.h>
|
---|
[81dce9f] | 36 | #include <usb/debug.h>
|
---|
[549ff23] | 37 |
|
---|
[8d2e251] | 38 | #include <assert.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <macros.h>
|
---|
| 41 | #include <mem.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
| 43 | #include <usbhc_iface.h>
|
---|
[81dce9f] | 44 |
|
---|
[549ff23] | 45 | /** Allocate and initialize usb_transfer_batch structure.
|
---|
| 46 | * @param ep endpoint used by the transfer batch.
|
---|
| 47 | * @param buffer data to send/recieve.
|
---|
| 48 | * @param buffer_size Size of data buffer.
|
---|
| 49 | * @param setup_buffer Data to send in SETUP stage of control transfer.
|
---|
| 50 | * @param func_in callback on IN transfer completion.
|
---|
| 51 | * @param func_out callback on OUT transfer completion.
|
---|
[cbd568b] | 52 | * @param fun DDF function (passed to callback function).
|
---|
[549ff23] | 53 | * @param arg Argument to pass to the callback function.
|
---|
| 54 | * @param private_data driver specific per batch data.
|
---|
| 55 | * @param private_data_dtor Function to properly destroy private_data.
|
---|
| 56 | * @return Pointer to valid usb_transfer_batch_t structure, NULL on failure.
|
---|
| 57 | */
|
---|
| 58 | usb_transfer_batch_t * usb_transfer_batch_create(
|
---|
[2cc6e97] | 59 | endpoint_t *ep,
|
---|
[81dce9f] | 60 | char *buffer,
|
---|
| 61 | size_t buffer_size,
|
---|
[a837544] | 62 | uint64_t setup_buffer,
|
---|
[81dce9f] | 63 | usbhc_iface_transfer_in_callback_t func_in,
|
---|
| 64 | usbhc_iface_transfer_out_callback_t func_out,
|
---|
[3c4663e] | 65 | void *arg
|
---|
[81dce9f] | 66 | )
|
---|
| 67 | {
|
---|
[549ff23] | 68 | if (func_in == NULL && func_out == NULL)
|
---|
| 69 | return NULL;
|
---|
| 70 | if (func_in != NULL && func_out != NULL)
|
---|
| 71 | return NULL;
|
---|
| 72 |
|
---|
[70fb822] | 73 | usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
|
---|
| 74 | if (instance) {
|
---|
| 75 | instance->ep = ep;
|
---|
| 76 | instance->callback_in = func_in;
|
---|
| 77 | instance->callback_out = func_out;
|
---|
| 78 | instance->arg = arg;
|
---|
| 79 | instance->buffer = buffer;
|
---|
| 80 | instance->buffer_size = buffer_size;
|
---|
[a00ac07] | 81 | instance->setup_size = 0;
|
---|
[70fb822] | 82 | instance->transfered_size = 0;
|
---|
| 83 | instance->error = EOK;
|
---|
[a00ac07] | 84 | if (ep && ep->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
[a837544] | 85 | memcpy(instance->setup_buffer, &setup_buffer,
|
---|
[a00ac07] | 86 | USB_SETUP_PACKET_SIZE);
|
---|
| 87 | instance->setup_size = USB_SETUP_PACKET_SIZE;
|
---|
| 88 | }
|
---|
[70fb822] | 89 | if (instance->ep)
|
---|
| 90 | endpoint_use(instance->ep);
|
---|
| 91 | }
|
---|
| 92 | return instance;
|
---|
[2cc6e97] | 93 | }
|
---|
[a76b01b4] | 94 |
|
---|
[2cc6e97] | 95 | /** Correctly dispose all used data structures.
|
---|
| 96 | *
|
---|
| 97 | * @param[in] instance Batch structure to use.
|
---|
| 98 | */
|
---|
[549ff23] | 99 | void usb_transfer_batch_destroy(const usb_transfer_batch_t *instance)
|
---|
[2cc6e97] | 100 | {
|
---|
[96e2d01] | 101 | if (!instance)
|
---|
| 102 | return;
|
---|
[c4fb5ecd] | 103 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.\n",
|
---|
| 104 | instance, USB_TRANSFER_BATCH_ARGS(*instance));
|
---|
[70fb822] | 105 | if (instance->ep) {
|
---|
| 106 | endpoint_release(instance->ep);
|
---|
| 107 | }
|
---|
[2cc6e97] | 108 | free(instance);
|
---|
| 109 | }
|
---|
[a76b01b4] | 110 |
|
---|
[549ff23] | 111 | /** Prepare data and call the right callback.
|
---|
| 112 | *
|
---|
| 113 | * @param[in] instance Batch structure to use.
|
---|
| 114 | * @param[in] data Data to copy to the output buffer.
|
---|
| 115 | * @param[in] size Size of @p data.
|
---|
[f167f55b] | 116 | * @param[in] error Error value to use.
|
---|
[549ff23] | 117 | */
|
---|
[f167f55b] | 118 | void usb_transfer_batch_finish_error(const usb_transfer_batch_t *instance,
|
---|
| 119 | const void *data, size_t size, int error)
|
---|
[549ff23] | 120 | {
|
---|
| 121 | assert(instance);
|
---|
| 122 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " finishing.\n",
|
---|
| 123 | instance, USB_TRANSFER_BATCH_ARGS(*instance));
|
---|
| 124 |
|
---|
| 125 | /* NOTE: Only one of these pointers should be set. */
|
---|
| 126 | if (instance->callback_out) {
|
---|
[8f68913f] | 127 | instance->callback_out(error, instance->arg);
|
---|
[549ff23] | 128 | }
|
---|
| 129 |
|
---|
| 130 | if (instance->callback_in) {
|
---|
| 131 | /* We care about the data and there are some to copy */
|
---|
[2582d8f] | 132 | const size_t safe_size = min(size, instance->buffer_size);
|
---|
[549ff23] | 133 | if (data) {
|
---|
[2582d8f] | 134 | memcpy(instance->buffer, data, safe_size);
|
---|
[549ff23] | 135 | }
|
---|
[8f68913f] | 136 | instance->callback_in(error, safe_size, instance->arg);
|
---|
[549ff23] | 137 | }
|
---|
| 138 | }
|
---|
[81dce9f] | 139 | /**
|
---|
| 140 | * @}
|
---|
| 141 | */
|
---|