source: mainline/uspace/lib/usbvirt/include/usbvirt/device.h@ d1974966

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

libusbvirt: More const.

  • Property mode set to 100644
File size: 7.4 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
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 libusbvirt
30 * @{
31 */
32/** @file
33 * Virtual USB device.
34 */
35
36#ifndef LIBUSBVIRT_DEVICE_H_
37#define LIBUSBVIRT_DEVICE_H_
38
39#include <usb/usb.h>
40#include <usb/dev/request.h>
41#include <async.h>
42
43/** Maximum number of endpoints supported by virtual USB. */
44#define USBVIRT_ENDPOINT_MAX 16
45
46typedef struct usbvirt_device usbvirt_device_t;
47
48/** Callback for data to device (OUT transaction).
49 *
50 * @param dev Virtual device to which the transaction belongs.
51 * @param endpoint Target endpoint number.
52 * @param transfer_type Transfer type.
53 * @param buffer Data buffer.
54 * @param buffer_size Size of the buffer in bytes.
55 * @return Error code.
56 */
57typedef int (*usbvirt_on_data_to_device_t)(usbvirt_device_t *dev,
58 usb_endpoint_t endpoint, usb_transfer_type_t transfer_type,
59 const void *buffer, size_t buffer_size);
60
61/** Callback for data from device (IN transaction).
62 *
63 * @param dev Virtual device to which the transaction belongs.
64 * @param endpoint Target endpoint number.
65 * @param transfer_type Transfer type.
66 * @param buffer Data buffer to write answer to.
67 * @param buffer_size Size of the buffer in bytes.
68 * @param act_buffer_size Write here how many bytes were actually written.
69 * @return Error code.
70 */
71typedef int (*usbvirt_on_data_from_device_t)(usbvirt_device_t *dev,
72 usb_endpoint_t endpoint, usb_transfer_type_t transfer_type,
73 void *buffer, size_t buffer_size, size_t *act_buffer_size);
74
75/** Callback for control transfer on endpoint zero.
76 *
77 * Notice that size of the data buffer is expected to be read from the
78 * setup packet.
79 *
80 * @param dev Virtual device to which the transaction belongs.
81 * @param setup_packet Standard setup packet.
82 * @param data Data (might be NULL).
83 * @param act_data_size Size of returned data in bytes.
84 * @return Error code.
85 */
86typedef int (*usbvirt_on_control_t)(usbvirt_device_t *dev,
87 const usb_device_request_setup_packet_t *setup_packet,
88 uint8_t *data, size_t *act_data_size);
89
90/** Callback for control request on a virtual USB device.
91 *
92 * See usbvirt_control_reply_helper() for simple way of answering
93 * control read requests.
94 */
95typedef struct {
96 /** Request direction (in or out). */
97 usb_direction_t req_direction;
98 /** Request recipient (device, interface or endpoint). */
99 usb_request_recipient_t req_recipient;
100 /** Request type (standard, class or vendor). */
101 usb_request_type_t req_type;
102 /** Actual request code. */
103 uint8_t request;
104 /** Request handler name for debugging purposes. */
105 const char *name;
106 /** Callback to be executed on matching request. */
107 usbvirt_on_control_t callback;
108} usbvirt_control_request_handler_t;
109
110/** Extra configuration data for GET_CONFIGURATION request. */
111typedef struct {
112 /** Actual data. */
113 const uint8_t *data;
114 /** Data length. */
115 size_t length;
116} usbvirt_device_configuration_extras_t;
117
118/** Single device configuration. */
119typedef struct {
120 /** Standard configuration descriptor. */
121 usb_standard_configuration_descriptor_t *descriptor;
122 /** Array of extra data. */
123 const usbvirt_device_configuration_extras_t *extra;
124 /** Length of @c extra array. */
125 size_t extra_count;
126} usbvirt_device_configuration_t;
127
128/** Standard USB descriptors for virtual device. */
129typedef struct {
130 /** Standard device descriptor.
131 * There is always only one such descriptor for the device.
132 */
133 const usb_standard_device_descriptor_t *device;
134
135 /** Configurations. */
136 usbvirt_device_configuration_t *configuration;
137 /** Number of configurations. */
138 size_t configuration_count;
139} usbvirt_descriptors_t;
140
141/** Possible states of virtual USB device.
142 * Notice that these are not 1:1 mappings to those in USB specification.
143 */
144typedef enum {
145 /** Default state, device listens at default address. */
146 USBVIRT_STATE_DEFAULT,
147 /** Device has non-default address assigned. */
148 USBVIRT_STATE_ADDRESS,
149 /** Device is configured. */
150 USBVIRT_STATE_CONFIGURED
151} usbvirt_device_state_t;
152
153/** Ops structure for virtual USB device. */
154typedef struct {
155 /** Callbacks for data to device.
156 * Index zero is ignored.
157 */
158 usbvirt_on_data_to_device_t data_out[USBVIRT_ENDPOINT_MAX];
159 /** Callbacks for data from device.
160 * Index zero is ignored.
161 */
162 usbvirt_on_data_from_device_t data_in[USBVIRT_ENDPOINT_MAX];
163 /** Array of control handlers.
164 * Last handler is expected to have the @c callback field set to NULL
165 */
166 usbvirt_control_request_handler_t *control;
167 /** Callback when device changes state.
168 *
169 * The value of @c state attribute of @p dev device is not
170 * defined during call of this function.
171 *
172 * @param dev The virtual USB device.
173 * @param old_state Old device state.
174 * @param new_state New device state.
175 */
176 void (*state_changed)(usbvirt_device_t *dev,
177 usbvirt_device_state_t old_state, usbvirt_device_state_t new_state);
178} usbvirt_device_ops_t;
179
180/** Virtual USB device. */
181struct usbvirt_device {
182 /** Name for debugging purposes. */
183 const char *name;
184 /** Custom device data. */
185 void *device_data;
186 /** Device ops. */
187 usbvirt_device_ops_t *ops;
188 /** Device descriptors. */
189 usbvirt_descriptors_t *descriptors;
190 /** Current device address.
191 * You shall treat this field as read only in your code.
192 */
193 usb_address_t address;
194 /** Current device state.
195 * You shall treat this field as read only in your code.
196 */
197 usbvirt_device_state_t state;
198 /** Session to the host controller.
199 * You shall treat this field as read only in your code.
200 */
201 async_sess_t *vhc_sess;
202};
203
204int usbvirt_device_plug(usbvirt_device_t *, const char *);
205void usbvirt_device_unplug(usbvirt_device_t *);
206
207void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *,
208 uint8_t *, size_t *, const void *, size_t);
209
210int usbvirt_control_write(usbvirt_device_t *, const void *, size_t, void *, size_t);
211int usbvirt_control_read(usbvirt_device_t *, const void *, size_t, void *, size_t, size_t *);
212int usbvirt_data_out(usbvirt_device_t *, usb_transfer_type_t, usb_endpoint_t,
213 const void *, size_t);
214int usbvirt_data_in(usbvirt_device_t *, usb_transfer_type_t, usb_endpoint_t,
215 void *, size_t, size_t *);
216
217#endif
218
219/**
220 * @}
221 */
Note: See TracBrowser for help on using the repository browser.