source: mainline/uspace/lib/usbhost/src/usb_transfer_batch.c@ 5dfb70c9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5dfb70c9 was 6832245, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usbhost bus: refactor the bus ops

This way, method names better represent the entity it is working with.
Their semantics was shifted a bit.

Regarding the tree of structures:

bus ← device ← endpoint ← batch

Previously, devices were kept in DDF function nodes, and endpoints had
pointer to the bus and device. Now, devices have pointer to bus,
endpoints don't.

Pointer to hcd_t in bus is WIP, and will be removed.

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