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