| 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 |
|
|---|
| 29 | /** @addtogroup libusbhost
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * USB transfer transaction structures.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #ifndef LIBUSBHOST_HOST_USB_TRANSFER_BATCH_H
|
|---|
| 37 | #define LIBUSBHOST_HOST_USB_TRANSFER_BATCH_H
|
|---|
| 38 |
|
|---|
| 39 | #include <adt/list.h>
|
|---|
| 40 |
|
|---|
| 41 | #include <usbhc_iface.h>
|
|---|
| 42 | #include <usb/usb.h>
|
|---|
| 43 | #include <usb/host/endpoint.h>
|
|---|
| 44 |
|
|---|
| 45 | #define USB_SETUP_PACKET_SIZE 8
|
|---|
| 46 |
|
|---|
| 47 | /** Structure stores additional data needed for communication with EP */
|
|---|
| 48 | typedef struct usb_transfer_batch {
|
|---|
| 49 | /** Endpoint used for communication */
|
|---|
| 50 | endpoint_t *ep;
|
|---|
| 51 | /** Function called on completion (IN version) */
|
|---|
| 52 | usbhc_iface_transfer_in_callback_t callback_in;
|
|---|
| 53 | /** Function called on completion (OUT version) */
|
|---|
| 54 | usbhc_iface_transfer_out_callback_t callback_out;
|
|---|
| 55 | /** Argument to pass to the completion function */
|
|---|
| 56 | void *arg;
|
|---|
| 57 | /** Place for data to send/receive */
|
|---|
| 58 | char *buffer;
|
|---|
| 59 | /** Size of memory pointed to by buffer member */
|
|---|
| 60 | size_t buffer_size;
|
|---|
| 61 | /** Place to store SETUP data needed by control transfers */
|
|---|
| 62 | char setup_buffer[USB_SETUP_PACKET_SIZE];
|
|---|
| 63 | /** Used portion of setup_buffer member
|
|---|
| 64 | *
|
|---|
| 65 | * SETUP buffer must be 8 bytes for control transfers and is left
|
|---|
| 66 | * unused for all other transfers. Thus, this field is either 0 or 8.
|
|---|
| 67 | */
|
|---|
| 68 | size_t setup_size;
|
|---|
| 69 |
|
|---|
| 70 | /** Actually used portion of the buffer
|
|---|
| 71 | * This member is never accessed by functions provided in this header,
|
|---|
| 72 | * with the exception of usb_transfer_batch_finish. For external use.
|
|---|
| 73 | */
|
|---|
| 74 | size_t transfered_size;
|
|---|
| 75 | /** Indicates success/failure of the communication
|
|---|
| 76 | * This member is never accessed by functions provided in this header,
|
|---|
| 77 | * with the exception of usb_transfer_batch_finish. For external use.
|
|---|
| 78 | */
|
|---|
| 79 | int error;
|
|---|
| 80 | } usb_transfer_batch_t;
|
|---|
| 81 |
|
|---|
| 82 | /** Printf formatting string for dumping usb_transfer_batch_t. */
|
|---|
| 83 | #define USB_TRANSFER_BATCH_FMT "[%d:%d %s %s-%s %zuB/%zu]"
|
|---|
| 84 |
|
|---|
| 85 | /** Printf arguments for dumping usb_transfer_batch_t.
|
|---|
| 86 | * @param batch USB transfer batch to be dumped.
|
|---|
| 87 | */
|
|---|
| 88 | #define USB_TRANSFER_BATCH_ARGS(batch) \
|
|---|
| 89 | (batch).ep->address, (batch).ep->endpoint, \
|
|---|
| 90 | usb_str_speed((batch).ep->speed), \
|
|---|
| 91 | usb_str_transfer_type_short((batch).ep->transfer_type), \
|
|---|
| 92 | usb_str_direction((batch).ep->direction), \
|
|---|
| 93 | (batch).buffer_size, (batch).ep->max_packet_size
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 | usb_transfer_batch_t * usb_transfer_batch_create(
|
|---|
| 97 | endpoint_t *ep,
|
|---|
| 98 | char *buffer,
|
|---|
| 99 | size_t buffer_size,
|
|---|
| 100 | uint64_t setup_buffer,
|
|---|
| 101 | usbhc_iface_transfer_in_callback_t func_in,
|
|---|
| 102 | usbhc_iface_transfer_out_callback_t func_out,
|
|---|
| 103 | void *arg
|
|---|
| 104 | );
|
|---|
| 105 | void usb_transfer_batch_destroy(const usb_transfer_batch_t *instance);
|
|---|
| 106 |
|
|---|
| 107 | void usb_transfer_batch_finish_error(const usb_transfer_batch_t *instance,
|
|---|
| 108 | const void* data, size_t size, int error);
|
|---|
| 109 |
|
|---|
| 110 | /** Finish batch using stored error value and transferred size.
|
|---|
| 111 | *
|
|---|
| 112 | * @param[in] instance Batch structure to use.
|
|---|
| 113 | * @param[in] data Data to copy to the output buffer.
|
|---|
| 114 | */
|
|---|
| 115 | static inline void usb_transfer_batch_finish(
|
|---|
| 116 | const usb_transfer_batch_t *instance, const void* data)
|
|---|
| 117 | {
|
|---|
| 118 | assert(instance);
|
|---|
| 119 | usb_transfer_batch_finish_error(
|
|---|
| 120 | instance, data, instance->transfered_size, instance->error);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /** Determine batch direction based on the callbacks present
|
|---|
| 124 | * @param[in] instance Batch structure to use, non-null.
|
|---|
| 125 | * @return USB_DIRECTION_IN, or USB_DIRECTION_OUT.
|
|---|
| 126 | */
|
|---|
| 127 | static inline usb_direction_t usb_transfer_batch_direction(
|
|---|
| 128 | const usb_transfer_batch_t *instance)
|
|---|
| 129 | {
|
|---|
| 130 | assert(instance);
|
|---|
| 131 | if (instance->callback_in) {
|
|---|
| 132 | assert(instance->callback_out == NULL);
|
|---|
| 133 | assert(instance->ep == NULL
|
|---|
| 134 | || instance->ep->transfer_type == USB_TRANSFER_CONTROL
|
|---|
| 135 | || instance->ep->direction == USB_DIRECTION_IN);
|
|---|
| 136 | return USB_DIRECTION_IN;
|
|---|
| 137 | }
|
|---|
| 138 | if (instance->callback_out) {
|
|---|
| 139 | assert(instance->callback_in == NULL);
|
|---|
| 140 | assert(instance->ep == NULL
|
|---|
| 141 | || instance->ep->transfer_type == USB_TRANSFER_CONTROL
|
|---|
| 142 | || instance->ep->direction == USB_DIRECTION_OUT);
|
|---|
| 143 | return USB_DIRECTION_OUT;
|
|---|
| 144 | }
|
|---|
| 145 | assert(false);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | #endif
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 151 | * @}
|
|---|
| 152 | */
|
|---|