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

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

libusbvirt: Make request creation macros available in header.

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