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