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

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

typo: transferred is spelled with two r

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