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
RevLine 
[bc9a629]1/*
[6cb58e6]2 * Copyright (c) 2011 Vojtech Horky
[bc9a629]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
[bd8c753d]29/** @addtogroup libusbvirt
[bc9a629]30 * @{
31 */
32/** @file
[a943106]33 * Virtual USB device.
[bc9a629]34 */
[79ae36dd]35
[b8100da]36#ifndef LIBUSBVIRT_DEVICE_H_
37#define LIBUSBVIRT_DEVICE_H_
[bc9a629]38
[4b4c797]39#include <usb/usb.h>
[7d521e24]40#include <usb/dev/request.h>
[79ae36dd]41#include <async.h>
[bc9a629]42
[2a6e2358]43
[a943106]44/** Maximum number of endpoints supported by virtual USB. */
[6cb58e6]45#define USBVIRT_ENDPOINT_MAX 16
[266d0871]46
[ca07cd3]47typedef struct usbvirt_device usbvirt_device_t;
[bc9a629]48
[a943106]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,
[d1974966]60 const void *buffer, size_t buffer_size);
[a943106]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);
[bc9a629]75
[a943106]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
[2a6e2358]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
[dc06caa]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 */
[7feeb84]132typedef struct {
[c9399c0]133 /* Request type. See usb/request.h */
134 uint8_t request_type;
[a943106]135 /** Actual request code. */
[7feeb84]136 uint8_t request;
[a943106]137 /** Request handler name for debugging purposes. */
[d5e7668]138 const char *name;
[a943106]139 /** Callback to be executed on matching request. */
[6cb58e6]140 usbvirt_on_control_t callback;
141} usbvirt_control_request_handler_t;
[bc9a629]142
[2c381250]143/** Extra configuration data for GET_CONFIGURATION request. */
144typedef struct {
145 /** Actual data. */
[97663ee]146 const uint8_t *data;
[2c381250]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. */
[97663ee]156 const usbvirt_device_configuration_extras_t *extra;
[2c381250]157 /** Length of @c extra array. */
158 size_t extra_count;
159} usbvirt_device_configuration_t;
160
[a943106]161/** Standard USB descriptors for virtual device. */
[2c381250]162typedef struct {
163 /** Standard device descriptor.
164 * There is always only one such descriptor for the device.
165 */
[97663ee]166 const usb_standard_device_descriptor_t *device;
[6cb58e6]167
[2c381250]168 /** Configurations. */
169 usbvirt_device_configuration_t *configuration;
170 /** Number of configurations. */
171 size_t configuration_count;
172} usbvirt_descriptors_t;
173
[6cb58e6]174/** Possible states of virtual USB device.
175 * Notice that these are not 1:1 mappings to those in USB specification.
[7a7bfeb3]176 */
[aab02fb]177typedef enum {
[6cb58e6]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;
[aab02fb]185
[a943106]186/** Ops structure for virtual USB device. */
[6cb58e6]187typedef struct {
[a943106]188 /** Callbacks for data to device.
189 * Index zero is ignored.
190 */
[6cb58e6]191 usbvirt_on_data_to_device_t data_out[USBVIRT_ENDPOINT_MAX];
[a943106]192 /** Callbacks for data from device.
193 * Index zero is ignored.
194 */
[6cb58e6]195 usbvirt_on_data_from_device_t data_in[USBVIRT_ENDPOINT_MAX];
[a943106]196 /** Array of control handlers.
197 * Last handler is expected to have the @c callback field set to NULL
198 */
[6cb58e6]199 usbvirt_control_request_handler_t *control;
[a943106]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 */
[6cb58e6]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;
[1840e0d]212
[a943106]213/** Virtual USB device. */
[6cb58e6]214struct usbvirt_device {
[a943106]215 /** Name for debugging purposes. */
[ca07cd3]216 const char *name;
[a943106]217 /** Custom device data. */
[6cb58e6]218 void *device_data;
[a943106]219 /** Device ops. */
[6cb58e6]220 usbvirt_device_ops_t *ops;
[a943106]221 /** Device descriptors. */
[2c381250]222 usbvirt_descriptors_t *descriptors;
[a943106]223 /** Current device address.
224 * You shall treat this field as read only in your code.
225 */
[47e3a8e]226 usb_address_t address;
[a943106]227 /** Current device state.
228 * You shall treat this field as read only in your code.
229 */
[6cb58e6]230 usbvirt_device_state_t state;
[79ae36dd]231 /** Session to the host controller.
[beee81a]232 * You shall treat this field as read only in your code.
233 */
[79ae36dd]234 async_sess_t *vhc_sess;
[ca07cd3]235};
[bc9a629]236
[6cb58e6]237int usbvirt_device_plug(usbvirt_device_t *, const char *);
[beee81a]238void usbvirt_device_unplug(usbvirt_device_t *);
[6cb58e6]239
240void usbvirt_control_reply_helper(const usb_device_request_setup_packet_t *,
[97663ee]241 uint8_t *, size_t *, const void *, size_t);
[6cb58e6]242
[d1974966]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 *);
[6cb58e6]245int usbvirt_data_out(usbvirt_device_t *, usb_transfer_type_t, usb_endpoint_t,
[d1974966]246 const void *, size_t);
[6cb58e6]247int usbvirt_data_in(usbvirt_device_t *, usb_transfer_type_t, usb_endpoint_t,
248 void *, size_t, size_t *);
249
[bc9a629]250#endif
[79ae36dd]251
[bc9a629]252/**
253 * @}
254 */
Note: See TracBrowser for help on using the repository browser.