| [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 <assert.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| [64fea02] | 37 | #include <stdlib.h>
|
|---|
| [2b61945] | 38 | #include <str_error.h>
|
|---|
| [64fea02] | 39 | #include <usb/debug.h>
|
|---|
| 40 |
|
|---|
| 41 | #include "endpoint.h"
|
|---|
| 42 | #include "bus.h"
|
|---|
| [5fd9c30] | 43 |
|
|---|
| [64fea02] | 44 | #include "usb_transfer_batch.h"
|
|---|
| [5fd9c30] | 45 |
|
|---|
| 46 | /** Create a batch on given endpoint.
|
|---|
| [549ff23] | 47 | */
|
|---|
| [5fd9c30] | 48 | usb_transfer_batch_t *usb_transfer_batch_create(endpoint_t *ep)
|
|---|
| [81dce9f] | 49 | {
|
|---|
| [5fd9c30] | 50 | assert(ep);
|
|---|
| 51 |
|
|---|
| [6832245] | 52 | bus_t *bus = endpoint_get_bus(ep);
|
|---|
| 53 | const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, batch_create);
|
|---|
| [5fd9c30] | 54 |
|
|---|
| [6832245] | 55 | if (!ops) {
|
|---|
| 56 | usb_transfer_batch_t *batch = calloc(1, sizeof(usb_transfer_batch_t));
|
|---|
| 57 | usb_transfer_batch_init(batch, ep);
|
|---|
| 58 | return batch;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | return ops->batch_create(ep);
|
|---|
| [2cc6e97] | 62 | }
|
|---|
| [a76b01b4] | 63 |
|
|---|
| [5fd9c30] | 64 | /** Initialize given batch structure.
|
|---|
| 65 | */
|
|---|
| 66 | void usb_transfer_batch_init(usb_transfer_batch_t *batch, endpoint_t *ep)
|
|---|
| 67 | {
|
|---|
| [6832245] | 68 | assert(ep);
|
|---|
| 69 | endpoint_add_ref(ep);
|
|---|
| [5fd9c30] | 70 | batch->ep = ep;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /** Destroy the batch.
|
|---|
| [549ff23] | 74 | *
|
|---|
| [5fd9c30] | 75 | * @param[in] batch Batch structure to use.
|
|---|
| [549ff23] | 76 | */
|
|---|
| [5fd9c30] | 77 | void usb_transfer_batch_destroy(usb_transfer_batch_t *batch)
|
|---|
| [549ff23] | 78 | {
|
|---|
| [5fd9c30] | 79 | assert(batch);
|
|---|
| 80 | assert(batch->ep);
|
|---|
| [549ff23] | 81 |
|
|---|
| [6832245] | 82 | bus_t *bus = endpoint_get_bus(batch->ep);
|
|---|
| 83 | const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, batch_destroy);
|
|---|
| 84 |
|
|---|
| 85 | endpoint_del_ref(batch->ep);
|
|---|
| 86 |
|
|---|
| 87 | if (ops) {
|
|---|
| [2b61945] | 88 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " destroying.\n",
|
|---|
| 89 | batch, USB_TRANSFER_BATCH_ARGS(*batch));
|
|---|
| [6832245] | 90 | ops->batch_destroy(batch);
|
|---|
| [2b61945] | 91 | }
|
|---|
| 92 | else {
|
|---|
| 93 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.\n",
|
|---|
| 94 | batch, USB_TRANSFER_BATCH_ARGS(*batch));
|
|---|
| [5fd9c30] | 95 | free(batch);
|
|---|
| [2b61945] | 96 | }
|
|---|
| [5fd9c30] | 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /** Finish a transfer batch: call handler, destroy batch, release endpoint.
|
|---|
| 100 | *
|
|---|
| 101 | * Call only after the batch have been scheduled && completed!
|
|---|
| 102 | *
|
|---|
| 103 | * @param[in] batch Batch structure to use.
|
|---|
| 104 | */
|
|---|
| 105 | void usb_transfer_batch_finish(usb_transfer_batch_t *batch)
|
|---|
| 106 | {
|
|---|
| [17873ac7] | 107 | assert(batch);
|
|---|
| 108 | assert(batch->ep);
|
|---|
| 109 |
|
|---|
| 110 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " finishing.\n",
|
|---|
| 111 | batch, USB_TRANSFER_BATCH_ARGS(*batch));
|
|---|
| 112 |
|
|---|
| 113 | if (batch->on_complete) {
|
|---|
| [f9d0a86] | 114 | const int err = batch->on_complete(batch->on_complete_data, batch->error, batch->transfered_size);
|
|---|
| [17873ac7] | 115 | if (err)
|
|---|
| 116 | usb_log_warning("batch %p failed to complete: %s",
|
|---|
| 117 | batch, str_error(err));
|
|---|
| 118 | }
|
|---|
| [5fd9c30] | 119 |
|
|---|
| 120 | usb_transfer_batch_destroy(batch);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| [17873ac7] | 123 | /** Finish a transfer batch as an aborted one.
|
|---|
| 124 | *
|
|---|
| 125 | * @param[in] batch Batch structure to use.
|
|---|
| 126 | */
|
|---|
| 127 | void usb_transfer_batch_abort(usb_transfer_batch_t *batch)
|
|---|
| 128 | {
|
|---|
| 129 | assert(batch);
|
|---|
| 130 | assert(batch->ep);
|
|---|
| 131 |
|
|---|
| 132 | batch->error = EAGAIN;
|
|---|
| 133 | usb_transfer_batch_finish(batch);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| [81dce9f] | 136 | /**
|
|---|
| 137 | * @}
|
|---|
| 138 | */
|
|---|