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