source: mainline/uspace/lib/usbdev/include/usb/dev/driver.h@ 0f4bff8

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

libusbdev: Add and use few new wrappers.

  • Property mode set to 100644
File size: 6.6 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 libusbdev
30 * @{
31 */
32/** @file
33 * USB device driver framework.
34 */
35
36#ifndef LIBUSBDEV_DRIVER_H_
37#define LIBUSBDEV_DRIVER_H_
38
39#include <usb/hc.h>
40#include <usb/dev/usb_device_connection.h>
41#include <usb/dev/pipes.h>
42#include <usb_iface.h>
43
44/** Descriptors for USB device. */
45typedef struct {
46 /** Standard device descriptor. */
47 usb_standard_device_descriptor_t device;
48 /** Full configuration descriptor of current configuration. */
49 const uint8_t *configuration;
50 size_t configuration_size;
51} usb_device_descriptors_t;
52
53/** Wrapper for data related to alternate interface setting.
54 * The pointers will typically point inside configuration descriptor and
55 * thus you shall not deallocate them.
56 */
57typedef struct {
58 /** Interface descriptor. */
59 const usb_standard_interface_descriptor_t *interface;
60 /** Pointer to start of descriptor tree bound with this interface. */
61 const uint8_t *nested_descriptors;
62 /** Size of data pointed by nested_descriptors in bytes. */
63 size_t nested_descriptors_size;
64} usb_alternate_interface_descriptors_t;
65
66/** Alternate interface settings. */
67typedef struct {
68 /** Array of alternate interfaces descriptions. */
69 usb_alternate_interface_descriptors_t *alternatives;
70 /** Size of @c alternatives array. */
71 size_t alternative_count;
72 /** Index of currently selected one. */
73 size_t current;
74} usb_alternate_interfaces_t;
75
76/** USB device structure. */
77typedef struct {
78 /** Connection to USB hc, used by wire and arbitrary requests. */
79 usb_hc_connection_t hc_conn;
80 /** Connection backing the pipes.
81 * Typically, you will not need to use this attribute at all.
82 */
83 usb_device_connection_t wire;
84 /** The default control pipe. */
85 usb_pipe_t ctrl_pipe;
86 /** Other endpoint pipes.
87 * This is an array of other endpoint pipes in the same order as
88 * in usb_driver_t.
89 */
90 usb_endpoint_mapping_t *pipes;
91 /** Number of other endpoint pipes. */
92 size_t pipes_count;
93 /** Current interface.
94 * Usually, drivers operate on single interface only.
95 * This item contains the value of the interface or -1 for any.
96 */
97 int interface_no;
98
99 /** Alternative interfaces. */
100 usb_alternate_interfaces_t alternate_interfaces;
101
102 /** Some useful descriptors. */
103 usb_device_descriptors_t descriptors;
104
105 /** Generic DDF device backing this one. DO NOT TOUCH! */
106 ddf_dev_t *ddf_dev;
107 /** Custom driver data.
108 * Do not use the entry in generic device, that is already used
109 * by the framework.
110 */
111 void *driver_data;
112
113 usb_dev_session_t *bus_session;
114} usb_device_t;
115
116/** USB driver ops. */
117typedef struct {
118 /** Callback when a new device was added to the system. */
119 int (*device_add)(usb_device_t *);
120 /** Callback when a device is about to be removed from the system. */
121 int (*device_rem)(usb_device_t *);
122 /** Callback when a device was removed from the system. */
123 int (*device_gone)(usb_device_t *);
124} usb_driver_ops_t;
125
126/** USB driver structure. */
127typedef struct {
128 /** Driver name.
129 * This name is copied to the generic driver name and must be exactly
130 * the same as the directory name where the driver executable resides.
131 */
132 const char *name;
133 /** Expected endpoints description.
134 * This description shall exclude default control endpoint (pipe zero)
135 * and must be NULL terminated.
136 * When only control endpoint is expected, you may set NULL directly
137 * without creating one item array containing NULL.
138 *
139 * When the driver expect single interrupt in endpoint,
140 * the initialization may look like this:
141\code
142static usb_endpoint_description_t poll_endpoint_description = {
143 .transfer_type = USB_TRANSFER_INTERRUPT,
144 .direction = USB_DIRECTION_IN,
145 .interface_class = USB_CLASS_HUB,
146 .interface_subclass = 0,
147 .interface_protocol = 0,
148 .flags = 0
149};
150
151static usb_endpoint_description_t *hub_endpoints[] = {
152 &poll_endpoint_description,
153 NULL
154};
155
156static usb_driver_t hub_driver = {
157 .endpoints = hub_endpoints,
158 ...
159};
160\endcode
161 */
162 const usb_endpoint_description_t **endpoints;
163 /** Driver ops. */
164 const usb_driver_ops_t *ops;
165} usb_driver_t;
166
167int usb_driver_main(const usb_driver_t *);
168
169int usb_device_init(usb_device_t *, ddf_dev_t *,
170 const usb_endpoint_description_t **, const char **);
171void usb_device_deinit(usb_device_t *);
172
173async_exch_t * usb_device_bus_exchange_begin(usb_device_t *);
174void usb_device_bus_exchange_end(async_exch_t *);
175
176int usb_device_select_interface(usb_device_t *, uint8_t,
177 const usb_endpoint_description_t **);
178
179int usb_device_retrieve_descriptors(usb_pipe_t *, usb_device_descriptors_t *);
180void usb_device_release_descriptors(usb_device_descriptors_t *);
181
182int usb_device_create_pipes(usb_device_connection_t *,
183 const usb_endpoint_description_t **, const uint8_t *, size_t, int, int,
184 usb_endpoint_mapping_t **, size_t *);
185void usb_device_destroy_pipes(usb_endpoint_mapping_t *, size_t);
186usb_pipe_t *usb_device_get_default_pipe(usb_device_t *);
187usb_pipe_t *usb_device_get_pipe(usb_device_t *, usb_endpoint_t, usb_direction_t);
188
189void * usb_device_data_alloc(usb_device_t *, size_t);
190void * usb_device_data_get(usb_device_t *);
191
192size_t usb_interface_count_alternates(const uint8_t *, size_t, uint8_t);
193int usb_alternate_interfaces_init(usb_alternate_interfaces_t *,
194 const uint8_t *, size_t, int);
195void usb_alternate_interfaces_deinit(usb_alternate_interfaces_t *);
196#endif
197/**
198 * @}
199 */
Note: See TracBrowser for help on using the repository browser.