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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since df2e5514 was 33b8d024, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Remove const qualifier from the argument of free() and realloc(),
as well as in numerous other variables that hold ownership of memory.

By convention, a pointer that holds ownership is _never_ qualified by const.
This is reflected in the standard type signature of free() and realloc().
Allowing const pointers to hold ownership may seem superficially convenient,
but is actually quite confusing to experienced C programmers.

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