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 (implementation).
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <usb/host/usb_transfer_batch.h>
|
---|
36 | #include <usb/host/endpoint.h>
|
---|
37 | #include <usb/host/bus.h>
|
---|
38 | #include <usb/debug.h>
|
---|
39 |
|
---|
40 | #include <assert.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <str_error.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | /** Create a batch on given endpoint.
|
---|
47 | */
|
---|
48 | usb_transfer_batch_t *usb_transfer_batch_create(endpoint_t *ep)
|
---|
49 | {
|
---|
50 | assert(ep);
|
---|
51 | assert(ep->bus);
|
---|
52 |
|
---|
53 | usb_transfer_batch_t *batch;
|
---|
54 | if (ep->bus->ops.create_batch)
|
---|
55 | batch = ep->bus->ops.create_batch(ep->bus, ep);
|
---|
56 | else
|
---|
57 | batch = malloc(sizeof(usb_transfer_batch_t));
|
---|
58 |
|
---|
59 | return batch;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /** Initialize given batch structure.
|
---|
63 | */
|
---|
64 | void usb_transfer_batch_init(usb_transfer_batch_t *batch, endpoint_t *ep)
|
---|
65 | {
|
---|
66 | memset(batch, 0, sizeof(*batch));
|
---|
67 | batch->ep = ep;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** Resolve resetting toggle.
|
---|
71 | *
|
---|
72 | * @param[in] batch Batch structure to use.
|
---|
73 | */
|
---|
74 | int usb_transfer_batch_reset_toggle(usb_transfer_batch_t *batch)
|
---|
75 | {
|
---|
76 | assert(batch);
|
---|
77 |
|
---|
78 | if (batch->error != EOK || batch->toggle_reset_mode == RESET_NONE)
|
---|
79 | return EOK;
|
---|
80 |
|
---|
81 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " resets %s",
|
---|
82 | batch, USB_TRANSFER_BATCH_ARGS(*batch),
|
---|
83 | batch->toggle_reset_mode == RESET_ALL ? "all EPs toggle" : "EP toggle");
|
---|
84 |
|
---|
85 | return bus_reset_toggle(batch->ep->bus, batch->target, batch->toggle_reset_mode);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /** Destroy the batch.
|
---|
89 | *
|
---|
90 | * @param[in] batch Batch structure to use.
|
---|
91 | */
|
---|
92 | void usb_transfer_batch_destroy(usb_transfer_batch_t *batch)
|
---|
93 | {
|
---|
94 | assert(batch);
|
---|
95 | assert(batch->ep);
|
---|
96 | assert(batch->ep->bus);
|
---|
97 |
|
---|
98 | bus_t *bus = batch->ep->bus;
|
---|
99 | if (bus->ops.destroy_batch) {
|
---|
100 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " destroying.\n",
|
---|
101 | batch, USB_TRANSFER_BATCH_ARGS(*batch));
|
---|
102 | bus->ops.destroy_batch(batch);
|
---|
103 | }
|
---|
104 | else {
|
---|
105 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.\n",
|
---|
106 | batch, USB_TRANSFER_BATCH_ARGS(*batch));
|
---|
107 | free(batch);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | /** Finish a transfer batch: call handler, destroy batch, release endpoint.
|
---|
112 | *
|
---|
113 | * Call only after the batch have been scheduled && completed!
|
---|
114 | *
|
---|
115 | * @param[in] batch Batch structure to use.
|
---|
116 | */
|
---|
117 | void usb_transfer_batch_finish(usb_transfer_batch_t *batch)
|
---|
118 | {
|
---|
119 | assert(batch);
|
---|
120 | assert(batch->ep);
|
---|
121 |
|
---|
122 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " finishing.\n",
|
---|
123 | batch, USB_TRANSFER_BATCH_ARGS(*batch));
|
---|
124 |
|
---|
125 | if (batch->on_complete) {
|
---|
126 | const int err = batch->on_complete(batch->on_complete_data, batch->error, batch->transfered_size);
|
---|
127 | if (err)
|
---|
128 | usb_log_warning("batch %p failed to complete: %s",
|
---|
129 | batch, str_error(err));
|
---|
130 | }
|
---|
131 |
|
---|
132 | usb_transfer_batch_destroy(batch);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** Finish a transfer batch as an aborted one.
|
---|
136 | *
|
---|
137 | * @param[in] batch Batch structure to use.
|
---|
138 | */
|
---|
139 | void usb_transfer_batch_abort(usb_transfer_batch_t *batch)
|
---|
140 | {
|
---|
141 | assert(batch);
|
---|
142 | assert(batch->ep);
|
---|
143 |
|
---|
144 | batch->error = EAGAIN;
|
---|
145 | usb_transfer_batch_finish(batch);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * @}
|
---|
150 | */
|
---|