[93ef8f6] | 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 |
|
---|
[160b75e] | 29 | /** @addtogroup libusbdev
|
---|
[93ef8f6] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[3538b0e] | 33 | * Non trivial initialization of endpoint pipes.
|
---|
[93ef8f6] | 34 | *
|
---|
| 35 | */
|
---|
[7d521e24] | 36 | #include <usb/dev/pipes.h>
|
---|
| 37 | #include <usb/dev/dp.h>
|
---|
| 38 | #include <usb/dev/request.h>
|
---|
[c01987c] | 39 | #include <usb/usb.h>
|
---|
| 40 | #include <usb/descriptor.h>
|
---|
| 41 |
|
---|
[93ef8f6] | 42 | #include <assert.h>
|
---|
[c01987c] | 43 | #include <errno.h>
|
---|
[93ef8f6] | 44 |
|
---|
[206f71a] | 45 | #define DEV_DESCR_MAX_PACKET_SIZE_OFFSET 7
|
---|
| 46 |
|
---|
[93ef8f6] | 47 | #define NESTING(parentname, childname) \
|
---|
| 48 | { \
|
---|
| 49 | .child = USB_DESCTYPE_##childname, \
|
---|
| 50 | .parent = USB_DESCTYPE_##parentname, \
|
---|
| 51 | }
|
---|
| 52 | #define LAST_NESTING { -1, -1 }
|
---|
| 53 |
|
---|
| 54 | /** Nesting pairs of standard descriptors. */
|
---|
[3ddbd38] | 55 | static const usb_dp_descriptor_nesting_t descriptor_nesting[] = {
|
---|
[93ef8f6] | 56 | NESTING(CONFIGURATION, INTERFACE),
|
---|
| 57 | NESTING(INTERFACE, ENDPOINT),
|
---|
| 58 | NESTING(INTERFACE, HUB),
|
---|
| 59 | NESTING(INTERFACE, HID),
|
---|
| 60 | NESTING(HID, HID_REPORT),
|
---|
| 61 | LAST_NESTING
|
---|
| 62 | };
|
---|
| 63 |
|
---|
| 64 | /** Tells whether given descriptor is of endpoint type.
|
---|
| 65 | *
|
---|
| 66 | * @param descriptor Descriptor in question.
|
---|
| 67 | * @return Whether the given descriptor is endpoint descriptor.
|
---|
| 68 | */
|
---|
[8a121b1] | 69 | static inline bool is_endpoint_descriptor(const uint8_t *descriptor)
|
---|
[93ef8f6] | 70 | {
|
---|
| 71 | return descriptor[1] == USB_DESCTYPE_ENDPOINT;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /** Tells whether found endpoint corresponds to endpoint described by user.
|
---|
| 75 | *
|
---|
| 76 | * @param wanted Endpoint description as entered by driver author.
|
---|
| 77 | * @param found Endpoint description obtained from endpoint descriptor.
|
---|
| 78 | * @return Whether the @p found descriptor fits the @p wanted descriptor.
|
---|
| 79 | */
|
---|
| 80 | static bool endpoint_fits_description(const usb_endpoint_description_t *wanted,
|
---|
[8a121b1] | 81 | const usb_endpoint_description_t *found)
|
---|
[93ef8f6] | 82 | {
|
---|
| 83 | #define _SAME(fieldname) ((wanted->fieldname) == (found->fieldname))
|
---|
| 84 |
|
---|
| 85 | if (!_SAME(direction)) {
|
---|
| 86 | return false;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | if (!_SAME(transfer_type)) {
|
---|
| 90 | return false;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | if ((wanted->interface_class >= 0) && !_SAME(interface_class)) {
|
---|
| 94 | return false;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | if ((wanted->interface_subclass >= 0) && !_SAME(interface_subclass)) {
|
---|
| 98 | return false;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | if ((wanted->interface_protocol >= 0) && !_SAME(interface_protocol)) {
|
---|
| 102 | return false;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | #undef _SAME
|
---|
| 106 |
|
---|
| 107 | return true;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /** Find endpoint mapping for a found endpoint.
|
---|
| 111 | *
|
---|
| 112 | * @param mapping Endpoint mapping list.
|
---|
| 113 | * @param mapping_count Number of endpoint mappings in @p mapping.
|
---|
| 114 | * @param found_endpoint Description of found endpoint.
|
---|
[18cb870] | 115 | * @param interface_number Number of currently processed interface.
|
---|
[93ef8f6] | 116 | * @return Endpoint mapping corresponding to @p found_endpoint.
|
---|
| 117 | * @retval NULL No corresponding endpoint found.
|
---|
| 118 | */
|
---|
| 119 | static usb_endpoint_mapping_t *find_endpoint_mapping(
|
---|
| 120 | usb_endpoint_mapping_t *mapping, size_t mapping_count,
|
---|
[8a121b1] | 121 | const usb_endpoint_description_t *found_endpoint,
|
---|
[159b91f4] | 122 | int interface_number, int interface_setting)
|
---|
[93ef8f6] | 123 | {
|
---|
| 124 | while (mapping_count > 0) {
|
---|
[18cb870] | 125 | bool interface_number_fits = (mapping->interface_no < 0)
|
---|
| 126 | || (mapping->interface_no == interface_number);
|
---|
| 127 |
|
---|
[159b91f4] | 128 | bool interface_setting_fits = (mapping->interface_setting < 0)
|
---|
| 129 | || (mapping->interface_setting == interface_setting);
|
---|
| 130 |
|
---|
[18cb870] | 131 | bool endpoint_descriptions_fits = endpoint_fits_description(
|
---|
| 132 | mapping->description, found_endpoint);
|
---|
| 133 |
|
---|
[159b91f4] | 134 | if (interface_number_fits
|
---|
| 135 | && interface_setting_fits
|
---|
| 136 | && endpoint_descriptions_fits) {
|
---|
[93ef8f6] | 137 | return mapping;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | mapping++;
|
---|
| 141 | mapping_count--;
|
---|
| 142 | }
|
---|
| 143 | return NULL;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /** Process endpoint descriptor.
|
---|
| 147 | *
|
---|
| 148 | * @param mapping Endpoint mapping list.
|
---|
| 149 | * @param mapping_count Number of endpoint mappings in @p mapping.
|
---|
| 150 | * @param interface Interface descriptor under which belongs the @p endpoint.
|
---|
| 151 | * @param endpoint Endpoint descriptor.
|
---|
| 152 | * @return Error code.
|
---|
| 153 | */
|
---|
| 154 | static int process_endpoint(
|
---|
| 155 | usb_endpoint_mapping_t *mapping, size_t mapping_count,
|
---|
| 156 | usb_standard_interface_descriptor_t *interface,
|
---|
[48f0f73a] | 157 | usb_standard_endpoint_descriptor_t *endpoint_desc,
|
---|
[d93f5afb] | 158 | usb_dev_session_t *bus_session)
|
---|
[93ef8f6] | 159 | {
|
---|
| 160 |
|
---|
| 161 | /*
|
---|
| 162 | * Get endpoint characteristics.
|
---|
| 163 | */
|
---|
| 164 |
|
---|
| 165 | /* Actual endpoint number is in bits 0..3 */
|
---|
[48f0f73a] | 166 | const usb_endpoint_t ep_no = endpoint_desc->endpoint_address & 0x0F;
|
---|
[8a121b1] | 167 |
|
---|
| 168 | const usb_endpoint_description_t description = {
|
---|
| 169 | /* Endpoint direction is set by bit 7 */
|
---|
[48f0f73a] | 170 | .direction = (endpoint_desc->endpoint_address & 128)
|
---|
[8a121b1] | 171 | ? USB_DIRECTION_IN : USB_DIRECTION_OUT,
|
---|
| 172 | /* Transfer type is in bits 0..2 and
|
---|
| 173 | * the enum values corresponds 1:1 */
|
---|
[48f0f73a] | 174 | .transfer_type = endpoint_desc->attributes & 3,
|
---|
[8a121b1] | 175 |
|
---|
| 176 | /* Get interface characteristics. */
|
---|
| 177 | .interface_class = interface->interface_class,
|
---|
| 178 | .interface_subclass = interface->interface_subclass,
|
---|
| 179 | .interface_protocol = interface->interface_protocol,
|
---|
| 180 | };
|
---|
[93ef8f6] | 181 |
|
---|
| 182 | /*
|
---|
| 183 | * Find the most fitting mapping and initialize the pipe.
|
---|
| 184 | */
|
---|
| 185 | usb_endpoint_mapping_t *ep_mapping = find_endpoint_mapping(mapping,
|
---|
[159b91f4] | 186 | mapping_count, &description,
|
---|
| 187 | interface->interface_number, interface->alternate_setting);
|
---|
[93ef8f6] | 188 | if (ep_mapping == NULL) {
|
---|
| 189 | return ENOENT;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | if (ep_mapping->present) {
|
---|
| 193 | return EEXISTS;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[d93f5afb] | 196 | int rc = usb_pipe_initialize(&ep_mapping->pipe,
|
---|
[48f0f73a] | 197 | ep_no, description.transfer_type,
|
---|
| 198 | uint16_usb2host(endpoint_desc->max_packet_size),
|
---|
[8582076] | 199 | description.direction, bus_session);
|
---|
[93ef8f6] | 200 | if (rc != EOK) {
|
---|
| 201 | return rc;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | ep_mapping->present = true;
|
---|
[48f0f73a] | 205 | ep_mapping->descriptor = endpoint_desc;
|
---|
[9d4579e] | 206 | ep_mapping->interface = interface;
|
---|
[93ef8f6] | 207 |
|
---|
| 208 | return EOK;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /** Process whole USB interface.
|
---|
| 212 | *
|
---|
| 213 | * @param mapping Endpoint mapping list.
|
---|
| 214 | * @param mapping_count Number of endpoint mappings in @p mapping.
|
---|
| 215 | * @param parser Descriptor parser.
|
---|
| 216 | * @param parser_data Descriptor parser data.
|
---|
| 217 | * @param interface_descriptor Interface descriptor.
|
---|
| 218 | * @return Error code.
|
---|
| 219 | */
|
---|
| 220 | static int process_interface(
|
---|
| 221 | usb_endpoint_mapping_t *mapping, size_t mapping_count,
|
---|
[7c95d6f5] | 222 | const usb_dp_parser_t *parser, const usb_dp_parser_data_t *parser_data,
|
---|
[8582076] | 223 | const uint8_t *interface_descriptor, usb_dev_session_t *bus_session)
|
---|
[93ef8f6] | 224 | {
|
---|
[8a121b1] | 225 | const uint8_t *descriptor = usb_dp_get_nested_descriptor(parser,
|
---|
[93ef8f6] | 226 | parser_data, interface_descriptor);
|
---|
| 227 |
|
---|
| 228 | if (descriptor == NULL) {
|
---|
| 229 | return ENOENT;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | do {
|
---|
| 233 | if (is_endpoint_descriptor(descriptor)) {
|
---|
| 234 | (void) process_endpoint(mapping, mapping_count,
|
---|
| 235 | (usb_standard_interface_descriptor_t *)
|
---|
| 236 | interface_descriptor,
|
---|
| 237 | (usb_standard_endpoint_descriptor_t *)
|
---|
| 238 | descriptor,
|
---|
[8582076] | 239 | bus_session);
|
---|
[93ef8f6] | 240 | }
|
---|
| 241 |
|
---|
| 242 | descriptor = usb_dp_get_sibling_descriptor(parser, parser_data,
|
---|
| 243 | interface_descriptor, descriptor);
|
---|
| 244 | } while (descriptor != NULL);
|
---|
| 245 |
|
---|
| 246 | return EOK;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | /** Initialize endpoint pipes from configuration descriptor.
|
---|
| 250 | *
|
---|
| 251 | * The mapping array is expected to conform to following rules:
|
---|
[b77931d] | 252 | * - @c pipe must be uninitialized pipe
|
---|
[93ef8f6] | 253 | * - @c description must point to prepared endpoint description
|
---|
| 254 | * - @c descriptor does not need to be initialized (will be overwritten)
|
---|
[9d4579e] | 255 | * - @c interface does not need to be initialized (will be overwritten)
|
---|
[93ef8f6] | 256 | * - @c present does not need to be initialized (will be overwritten)
|
---|
| 257 | *
|
---|
| 258 | * After processing the configuration descriptor, the mapping is updated
|
---|
| 259 | * in the following fashion:
|
---|
| 260 | * - @c present will be set to @c true when the endpoint was found in the
|
---|
| 261 | * configuration
|
---|
| 262 | * - @c descriptor will point inside the configuration descriptor to endpoint
|
---|
[1110ebd] | 263 | * corresponding to given description (or NULL for not found descriptor)
|
---|
[9d4579e] | 264 | * - @c interface will point inside the configuration descriptor to interface
|
---|
| 265 | * descriptor the endpoint @c descriptor belongs to (or NULL for not found
|
---|
| 266 | * descriptor)
|
---|
[93ef8f6] | 267 | * - @c pipe will be initialized when found, otherwise left untouched
|
---|
| 268 | * - @c description will be untouched under all circumstances
|
---|
| 269 | *
|
---|
| 270 | * @param mapping Endpoint mapping list.
|
---|
| 271 | * @param mapping_count Number of endpoint mappings in @p mapping.
|
---|
| 272 | * @param configuration_descriptor Full configuration descriptor (is expected
|
---|
| 273 | * to be in USB endianness: i.e. as-is after being retrieved from
|
---|
| 274 | * the device).
|
---|
| 275 | * @param configuration_descriptor_size Size of @p configuration_descriptor
|
---|
| 276 | * in bytes.
|
---|
| 277 | * @param connection Connection backing the endpoint pipes.
|
---|
| 278 | * @return Error code.
|
---|
| 279 | */
|
---|
[3954a63b] | 280 | int usb_pipe_initialize_from_configuration(
|
---|
[93ef8f6] | 281 | usb_endpoint_mapping_t *mapping, size_t mapping_count,
|
---|
[7c95d6f5] | 282 | const uint8_t *config_descriptor, size_t config_descriptor_size,
|
---|
[d93f5afb] | 283 | usb_dev_session_t *bus_session)
|
---|
[93ef8f6] | 284 | {
|
---|
| 285 |
|
---|
[7c95d6f5] | 286 | if (config_descriptor == NULL) {
|
---|
[93ef8f6] | 287 | return EBADMEM;
|
---|
| 288 | }
|
---|
[7c95d6f5] | 289 | if (config_descriptor_size
|
---|
[93ef8f6] | 290 | < sizeof(usb_standard_configuration_descriptor_t)) {
|
---|
| 291 | return ERANGE;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[b77931d] | 294 | /* Go through the mapping and set all endpoints to not present. */
|
---|
| 295 | for (size_t i = 0; i < mapping_count; i++) {
|
---|
[93ef8f6] | 296 | mapping[i].present = false;
|
---|
| 297 | mapping[i].descriptor = NULL;
|
---|
[9d4579e] | 298 | mapping[i].interface = NULL;
|
---|
[93ef8f6] | 299 | }
|
---|
| 300 |
|
---|
[b77931d] | 301 | /* Prepare the descriptor parser. */
|
---|
[7c95d6f5] | 302 | const usb_dp_parser_t dp_parser = {
|
---|
[93ef8f6] | 303 | .nesting = descriptor_nesting
|
---|
| 304 | };
|
---|
[7c95d6f5] | 305 | const usb_dp_parser_data_t dp_data = {
|
---|
| 306 | .data = config_descriptor,
|
---|
| 307 | .size = config_descriptor_size,
|
---|
[93ef8f6] | 308 | };
|
---|
| 309 |
|
---|
| 310 | /*
|
---|
| 311 | * Iterate through all interfaces.
|
---|
| 312 | */
|
---|
[8a121b1] | 313 | const uint8_t *interface = usb_dp_get_nested_descriptor(&dp_parser,
|
---|
[7c95d6f5] | 314 | &dp_data, config_descriptor);
|
---|
[93ef8f6] | 315 | if (interface == NULL) {
|
---|
| 316 | return ENOENT;
|
---|
| 317 | }
|
---|
| 318 | do {
|
---|
| 319 | (void) process_interface(mapping, mapping_count,
|
---|
[8582076] | 320 | &dp_parser, &dp_data, interface, bus_session);
|
---|
[93ef8f6] | 321 | interface = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
|
---|
[7c95d6f5] | 322 | config_descriptor, interface);
|
---|
[93ef8f6] | 323 | } while (interface != NULL);
|
---|
| 324 |
|
---|
| 325 | return EOK;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[206f71a] | 328 | /** Probe default control pipe for max packet size.
|
---|
| 329 | *
|
---|
| 330 | * The function tries to get the correct value of max packet size several
|
---|
| 331 | * time before giving up.
|
---|
| 332 | *
|
---|
| 333 | * The session on the pipe shall not be started.
|
---|
| 334 | *
|
---|
| 335 | * @param pipe Default control pipe.
|
---|
| 336 | * @return Error code.
|
---|
| 337 | */
|
---|
[3954a63b] | 338 | int usb_pipe_probe_default_control(usb_pipe_t *pipe)
|
---|
[206f71a] | 339 | {
|
---|
| 340 | assert(pipe);
|
---|
| 341 | assert(DEV_DESCR_MAX_PACKET_SIZE_OFFSET < CTRL_PIPE_MIN_PACKET_SIZE);
|
---|
| 342 |
|
---|
| 343 | if ((pipe->direction != USB_DIRECTION_BOTH) ||
|
---|
| 344 | (pipe->transfer_type != USB_TRANSFER_CONTROL) ||
|
---|
| 345 | (pipe->endpoint_no != 0)) {
|
---|
| 346 | return EINVAL;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | uint8_t dev_descr_start[CTRL_PIPE_MIN_PACKET_SIZE];
|
---|
| 350 | size_t transferred_size;
|
---|
[02fc5c4] | 351 | int rc;
|
---|
| 352 | for (size_t attempt_var = 0; attempt_var < 3; ++attempt_var) {
|
---|
[206f71a] | 353 | rc = usb_request_get_descriptor(pipe, USB_REQUEST_TYPE_STANDARD,
|
---|
| 354 | USB_REQUEST_RECIPIENT_DEVICE, USB_DESCTYPE_DEVICE,
|
---|
| 355 | 0, 0, dev_descr_start, CTRL_PIPE_MIN_PACKET_SIZE,
|
---|
| 356 | &transferred_size);
|
---|
| 357 | if (rc == EOK) {
|
---|
| 358 | if (transferred_size != CTRL_PIPE_MIN_PACKET_SIZE) {
|
---|
| 359 | rc = ELIMIT;
|
---|
| 360 | continue;
|
---|
| 361 | }
|
---|
| 362 | break;
|
---|
| 363 | }
|
---|
| 364 | }
|
---|
| 365 | if (rc != EOK) {
|
---|
[bf4cc3e] | 366 | return rc;
|
---|
| 367 | }
|
---|
[dc04868] | 368 |
|
---|
[206f71a] | 369 | pipe->max_packet_size
|
---|
| 370 | = dev_descr_start[DEV_DESCR_MAX_PACKET_SIZE_OFFSET];
|
---|
| 371 |
|
---|
| 372 | return EOK;
|
---|
[dc04868] | 373 | }
|
---|
| 374 |
|
---|
[93ef8f6] | 375 | /**
|
---|
| 376 | * @}
|
---|
| 377 | */
|
---|