[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 |
|
---|
[1102eca] | 46 | /**
|
---|
| 47 | * Create a batch on a given endpoint.
|
---|
| 48 | *
|
---|
| 49 | * If the bus callback is not defined, it just creates a default batch.
|
---|
[549ff23] | 50 | */
|
---|
[5fd9c30] | 51 | usb_transfer_batch_t *usb_transfer_batch_create(endpoint_t *ep)
|
---|
[81dce9f] | 52 | {
|
---|
[5fd9c30] | 53 | assert(ep);
|
---|
| 54 |
|
---|
[6832245] | 55 | bus_t *bus = endpoint_get_bus(ep);
|
---|
| 56 | const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, batch_create);
|
---|
[5fd9c30] | 57 |
|
---|
[6832245] | 58 | if (!ops) {
|
---|
| 59 | usb_transfer_batch_t *batch = calloc(1, sizeof(usb_transfer_batch_t));
|
---|
[1102eca] | 60 | if (!batch)
|
---|
| 61 | return NULL;
|
---|
[6832245] | 62 | usb_transfer_batch_init(batch, ep);
|
---|
| 63 | return batch;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | return ops->batch_create(ep);
|
---|
[2cc6e97] | 67 | }
|
---|
[a76b01b4] | 68 |
|
---|
[1102eca] | 69 | /**
|
---|
| 70 | * Initialize given batch structure.
|
---|
[5fd9c30] | 71 | */
|
---|
| 72 | void usb_transfer_batch_init(usb_transfer_batch_t *batch, endpoint_t *ep)
|
---|
| 73 | {
|
---|
[6832245] | 74 | assert(ep);
|
---|
[1102eca] | 75 | /* Batch reference */
|
---|
[6832245] | 76 | endpoint_add_ref(ep);
|
---|
[5fd9c30] | 77 | batch->ep = ep;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[1102eca] | 80 | /**
|
---|
| 81 | * Destroy the batch. If there's no bus callback, just free it.
|
---|
[549ff23] | 82 | */
|
---|
[5fd9c30] | 83 | void usb_transfer_batch_destroy(usb_transfer_batch_t *batch)
|
---|
[549ff23] | 84 | {
|
---|
[5fd9c30] | 85 | assert(batch);
|
---|
| 86 | assert(batch->ep);
|
---|
[549ff23] | 87 |
|
---|
[6832245] | 88 | bus_t *bus = endpoint_get_bus(batch->ep);
|
---|
| 89 | const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, batch_destroy);
|
---|
| 90 |
|
---|
[1102eca] | 91 | /* Batch reference */
|
---|
[6832245] | 92 | endpoint_del_ref(batch->ep);
|
---|
| 93 |
|
---|
| 94 | if (ops) {
|
---|
[a1732929] | 95 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " destroying.",
|
---|
[2b61945] | 96 | batch, USB_TRANSFER_BATCH_ARGS(*batch));
|
---|
[6832245] | 97 | ops->batch_destroy(batch);
|
---|
[2b61945] | 98 | }
|
---|
| 99 | else {
|
---|
[a1732929] | 100 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.",
|
---|
[2b61945] | 101 | batch, USB_TRANSFER_BATCH_ARGS(*batch));
|
---|
[5fd9c30] | 102 | free(batch);
|
---|
[2b61945] | 103 | }
|
---|
[5fd9c30] | 104 | }
|
---|
| 105 |
|
---|
[1102eca] | 106 | /**
|
---|
| 107 | * Finish a transfer batch: call handler, destroy batch, release endpoint.
|
---|
[5fd9c30] | 108 | *
|
---|
| 109 | * Call only after the batch have been scheduled && completed!
|
---|
| 110 | */
|
---|
| 111 | void usb_transfer_batch_finish(usb_transfer_batch_t *batch)
|
---|
| 112 | {
|
---|
[17873ac7] | 113 | assert(batch);
|
---|
| 114 | assert(batch->ep);
|
---|
| 115 |
|
---|
[a1732929] | 116 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " finishing.",
|
---|
[17873ac7] | 117 | batch, USB_TRANSFER_BATCH_ARGS(*batch));
|
---|
| 118 |
|
---|
| 119 | if (batch->on_complete) {
|
---|
[db51a6a6] | 120 | const int err = batch->on_complete(batch->on_complete_data, batch->error, batch->transferred_size);
|
---|
[17873ac7] | 121 | if (err)
|
---|
| 122 | usb_log_warning("batch %p failed to complete: %s",
|
---|
| 123 | batch, str_error(err));
|
---|
| 124 | }
|
---|
[5fd9c30] | 125 |
|
---|
| 126 | usb_transfer_batch_destroy(batch);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[81dce9f] | 129 | /**
|
---|
| 130 | * @}
|
---|
| 131 | */
|
---|