source: mainline/uspace/lib/usbhost/src/batch.c@ b991d37

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b991d37 was 96e2d01, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

libusbhost: make batch call interface public

add pointer checks

  • Property mode set to 100644
File size: 5.2 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#include <errno.h>
35#include <str_error.h>
36
37#include <usb/usb.h>
38#include <usb/debug.h>
39#include <usb/host/batch.h>
40#include <usb/host/hcd.h>
41
42void usb_transfer_batch_init(
43 usb_transfer_batch_t *instance,
44 endpoint_t *ep,
45 char *buffer,
46 char *data_buffer,
47 size_t buffer_size,
48 char *setup_buffer,
49 size_t setup_size,
50 usbhc_iface_transfer_in_callback_t func_in,
51 usbhc_iface_transfer_out_callback_t func_out,
52 void *arg,
53 ddf_fun_t *fun,
54 void *private_data,
55 void (*private_data_dtor)(void *p_data)
56 )
57{
58 assert(instance);
59 link_initialize(&instance->link);
60 instance->ep = ep;
61 instance->callback_in = func_in;
62 instance->callback_out = func_out;
63 instance->arg = arg;
64 instance->buffer = buffer;
65 instance->data_buffer = data_buffer;
66 instance->buffer_size = buffer_size;
67 instance->setup_buffer = setup_buffer;
68 instance->setup_size = setup_size;
69 instance->fun = fun;
70 instance->private_data = private_data;
71 instance->private_data_dtor = private_data_dtor;
72 instance->transfered_size = 0;
73 instance->next_step = NULL;
74 instance->error = EOK;
75 if (instance->ep)
76 endpoint_use(instance->ep);
77}
78/*----------------------------------------------------------------------------*/
79/** Mark batch as finished and continue with next step.
80 *
81 * @param[in] instance Batch structure to use.
82 *
83 */
84void usb_transfer_batch_finish(usb_transfer_batch_t *instance)
85{
86 assert(instance);
87 if (instance->ep)
88 endpoint_release(instance->ep);
89 if (instance->next_step)
90 instance->next_step(instance);
91}
92/*----------------------------------------------------------------------------*/
93/** Prepare data, get error status and call callback in.
94 *
95 * @param[in] instance Batch structure to use.
96 * Copies data from transport buffer, and calls callback with appropriate
97 * parameters.
98 */
99void usb_transfer_batch_call_in(usb_transfer_batch_t *instance)
100{
101 assert(instance);
102 assert(instance->callback_in);
103
104 /* We are data in, we need data */
105 if (instance->data_buffer && (instance->buffer != instance->data_buffer))
106 memcpy(instance->buffer,
107 instance->data_buffer, instance->buffer_size);
108
109 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " completed (%zuB): %s.\n",
110 instance, USB_TRANSFER_BATCH_ARGS(*instance),
111 instance->transfered_size, str_error(instance->error));
112
113 instance->callback_in(instance->fun, instance->error,
114 instance->transfered_size, instance->arg);
115}
116/*----------------------------------------------------------------------------*/
117/** Get error status and call callback out.
118 *
119 * @param[in] instance Batch structure to use.
120 */
121void usb_transfer_batch_call_out(usb_transfer_batch_t *instance)
122{
123 assert(instance);
124 assert(instance->callback_out);
125
126 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " completed: %s.\n",
127 instance, USB_TRANSFER_BATCH_ARGS(*instance),
128 str_error(instance->error));
129
130 if (instance->ep->transfer_type == USB_TRANSFER_CONTROL
131 && instance->error == EOK) {
132 usb_target_t target =
133 {instance->ep->address, instance->ep->endpoint};
134 reset_ep_if_need(
135 fun_to_hcd(instance->fun), target, instance->setup_buffer);
136 }
137
138 instance->callback_out(instance->fun,
139 instance->error, instance->arg);
140}
141/*----------------------------------------------------------------------------*/
142/** Correctly dispose all used data structures.
143 *
144 * @param[in] instance Batch structure to use.
145 */
146void usb_transfer_batch_dispose(usb_transfer_batch_t *instance)
147{
148 if (!instance)
149 return;
150 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.\n",
151 instance, USB_TRANSFER_BATCH_ARGS(*instance));
152 if (instance->private_data) {
153 assert(instance->private_data_dtor);
154 instance->private_data_dtor(instance->private_data);
155 }
156 free(instance);
157}
158/**
159 * @}
160 */
Note: See TracBrowser for help on using the repository browser.