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

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

lilbusbhost: Do not use usb_transfer_batch_t.error field in finish_error variant

  • Property mode set to 100644
File size: 5.4 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 <macros.h>
36
37#include <usb/usb.h>
38#include <usb/debug.h>
39
40#include <usb/host/usb_transfer_batch.h>
41#include <usb/host/hcd.h>
42
43/** Allocate and initialize usb_transfer_batch structure.
44 * @param ep endpoint used by the transfer batch.
45 * @param buffer data to send/recieve.
46 * @param buffer_size Size of data buffer.
47 * @param setup_buffer Data to send in SETUP stage of control transfer.
48 * @param func_in callback on IN transfer completion.
49 * @param func_out callback on OUT transfer completion.
50 * @param arg Argument to pass to the callback function.
51 * @param private_data driver specific per batch data.
52 * @param private_data_dtor Function to properly destroy private_data.
53 * @return Pointer to valid usb_transfer_batch_t structure, NULL on failure.
54 */
55usb_transfer_batch_t * usb_transfer_batch_create(
56 endpoint_t *ep,
57 char *buffer,
58 size_t buffer_size,
59 uint64_t setup_buffer,
60 usbhc_iface_transfer_in_callback_t func_in,
61 usbhc_iface_transfer_out_callback_t func_out,
62 void *arg,
63 ddf_fun_t *fun,
64 void *private_data,
65 void (*private_data_dtor)(void *)
66 )
67{
68 if (func_in == NULL && func_out == NULL)
69 return NULL;
70 if (func_in != NULL && func_out != NULL)
71 return NULL;
72
73 usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
74 if (instance) {
75 instance->ep = ep;
76 instance->callback_in = func_in;
77 instance->callback_out = func_out;
78 instance->arg = arg;
79 instance->buffer = buffer;
80 instance->buffer_size = buffer_size;
81 instance->setup_size = 0;
82 instance->fun = fun;
83 instance->private_data = private_data;
84 instance->private_data_dtor = private_data_dtor;
85 instance->transfered_size = 0;
86 instance->error = EOK;
87 if (ep && ep->transfer_type == USB_TRANSFER_CONTROL) {
88 memcpy(instance->setup_buffer, &setup_buffer,
89 USB_SETUP_PACKET_SIZE);
90 instance->setup_size = USB_SETUP_PACKET_SIZE;
91 }
92 if (instance->ep)
93 endpoint_use(instance->ep);
94 }
95 return instance;
96}
97/*----------------------------------------------------------------------------*/
98/** Correctly dispose all used data structures.
99 *
100 * @param[in] instance Batch structure to use.
101 */
102void usb_transfer_batch_destroy(const usb_transfer_batch_t *instance)
103{
104 if (!instance)
105 return;
106 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.\n",
107 instance, USB_TRANSFER_BATCH_ARGS(*instance));
108 if (instance->ep) {
109 endpoint_release(instance->ep);
110 }
111 if (instance->private_data) {
112 assert(instance->private_data_dtor);
113 instance->private_data_dtor(instance->private_data);
114 }
115 free(instance);
116}
117/*----------------------------------------------------------------------------*/
118/** Prepare data and call the right callback.
119 *
120 * @param[in] instance Batch structure to use.
121 * @param[in] data Data to copy to the output buffer.
122 * @param[in] size Size of @p data.
123 * @param[in] error Error value to use.
124 */
125void usb_transfer_batch_finish_error(const usb_transfer_batch_t *instance,
126 const void *data, size_t size, int error)
127{
128 assert(instance);
129 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " finishing.\n",
130 instance, USB_TRANSFER_BATCH_ARGS(*instance));
131
132 /* NOTE: Only one of these pointers should be set. */
133 if (instance->callback_out) {
134 /* Check for commands that reset toggle bit */
135 if (instance->ep->transfer_type == USB_TRANSFER_CONTROL
136 && error == EOK) {
137 const usb_target_t target =
138 {{ instance->ep->address, instance->ep->endpoint }};
139 reset_ep_if_need(fun_to_hcd(instance->fun), target,
140 instance->setup_buffer);
141 }
142 instance->callback_out(instance->fun, error, instance->arg);
143 }
144
145 if (instance->callback_in) {
146 /* We care about the data and there are some to copy */
147 if (data) {
148 const size_t minsize = min(size, instance->buffer_size);
149 memcpy(instance->buffer, data, minsize);
150 }
151 instance->callback_in(instance->fun, error,
152 instance->transfered_size, instance->arg);
153 }
154}
155/**
156 * @}
157 */
Note: See TracBrowser for help on using the repository browser.