source: mainline/uspace/lib/usbhost/include/usb/host/usb_transfer_batch.h@ 46577995

Last change on this file since 46577995 was 46577995, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

After this commit, HelenOS is free of code that mixes error codes with non-error
values on the assumption that error codes are negative.

  • 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
29/** @addtogroup libusbhost
30 * @{
31 */
32/** @file
33 * USB transfer transaction structures.
34 */
35
36#ifndef LIBUSBHOST_HOST_USB_TRANSFER_BATCH_H
37#define LIBUSBHOST_HOST_USB_TRANSFER_BATCH_H
38
39#include <usb/host/endpoint.h>
40#include <usb/usb.h>
41
42#include <assert.h>
43#include <stddef.h>
44#include <stdint.h>
45#include <usbhc_iface.h>
46
47#define USB_SETUP_PACKET_SIZE 8
48
49/** Structure stores additional data needed for communication with EP */
50typedef struct usb_transfer_batch {
51 /** Endpoint used for communication */
52 endpoint_t *ep;
53 /** Function called on completion (IN version) */
54 usbhc_iface_transfer_in_callback_t callback_in;
55 /** Function called on completion (OUT version) */
56 usbhc_iface_transfer_out_callback_t callback_out;
57 /** Argument to pass to the completion function */
58 void *arg;
59 /** Place for data to send/receive */
60 char *buffer;
61 /** Size of memory pointed to by buffer member */
62 size_t buffer_size;
63 /** Place to store SETUP data needed by control transfers */
64 char setup_buffer[USB_SETUP_PACKET_SIZE];
65 /** Used portion of setup_buffer member
66 *
67 * SETUP buffer must be 8 bytes for control transfers and is left
68 * unused for all other transfers. Thus, this field is either 0 or 8.
69 */
70 size_t setup_size;
71
72 /** Actually used portion of the buffer
73 * This member is never accessed by functions provided in this header,
74 * with the exception of usb_transfer_batch_finish. For external use.
75 */
76 size_t transfered_size;
77 /** Indicates success/failure of the communication
78 * This member is never accessed by functions provided in this header,
79 * with the exception of usb_transfer_batch_finish. For external use.
80 */
81 errno_t error;
82} usb_transfer_batch_t;
83
84/** Printf formatting string for dumping usb_transfer_batch_t. */
85#define USB_TRANSFER_BATCH_FMT "[%d:%d %s %s-%s %zuB/%zu]"
86
87/** Printf arguments for dumping usb_transfer_batch_t.
88 * @param batch USB transfer batch to be dumped.
89 */
90#define USB_TRANSFER_BATCH_ARGS(batch) \
91 (batch).ep->address, (batch).ep->endpoint, \
92 usb_str_speed((batch).ep->speed), \
93 usb_str_transfer_type_short((batch).ep->transfer_type), \
94 usb_str_direction((batch).ep->direction), \
95 (batch).buffer_size, (batch).ep->max_packet_size
96
97
98usb_transfer_batch_t * usb_transfer_batch_create(
99 endpoint_t *ep,
100 char *buffer,
101 size_t buffer_size,
102 uint64_t setup_buffer,
103 usbhc_iface_transfer_in_callback_t func_in,
104 usbhc_iface_transfer_out_callback_t func_out,
105 void *arg
106);
107void usb_transfer_batch_destroy(const usb_transfer_batch_t *instance);
108
109void usb_transfer_batch_finish_error(const usb_transfer_batch_t *instance,
110 const void* data, size_t size, errno_t error);
111
112/** Finish batch using stored error value and transferred size.
113 *
114 * @param[in] instance Batch structure to use.
115 * @param[in] data Data to copy to the output buffer.
116 */
117static inline void usb_transfer_batch_finish(
118 const usb_transfer_batch_t *instance, const void* data)
119{
120 assert(instance);
121 usb_transfer_batch_finish_error(
122 instance, data, instance->transfered_size, instance->error);
123}
124
125/** Determine batch direction based on the callbacks present
126 * @param[in] instance Batch structure to use, non-null.
127 * @return USB_DIRECTION_IN, or USB_DIRECTION_OUT.
128 */
129static inline usb_direction_t usb_transfer_batch_direction(
130 const usb_transfer_batch_t *instance)
131{
132 assert(instance);
133 if (instance->callback_in) {
134 assert(instance->callback_out == NULL);
135 assert(instance->ep == NULL
136 || instance->ep->transfer_type == USB_TRANSFER_CONTROL
137 || instance->ep->direction == USB_DIRECTION_IN);
138 return USB_DIRECTION_IN;
139 }
140 if (instance->callback_out) {
141 assert(instance->callback_in == NULL);
142 assert(instance->ep == NULL
143 || instance->ep->transfer_type == USB_TRANSFER_CONTROL
144 || instance->ep->direction == USB_DIRECTION_OUT);
145 return USB_DIRECTION_OUT;
146 }
147 assert(false);
148}
149
150#endif
151
152/**
153 * @}
154 */
Note: See TracBrowser for help on using the repository browser.