[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
|
---|
| 33 | * Initialization of endpoint pipes.
|
---|
| 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>
|
---|
[b7d8fd9] | 40 | #include <usbhc_iface.h>
|
---|
[93ef8f6] | 41 | #include <errno.h>
|
---|
| 42 | #include <assert.h>
|
---|
| 43 |
|
---|
[206f71a] | 44 | #define CTRL_PIPE_MIN_PACKET_SIZE 8
|
---|
| 45 | #define DEV_DESCR_MAX_PACKET_SIZE_OFFSET 7
|
---|
| 46 |
|
---|
[93ef8f6] | 47 |
|
---|
| 48 | #define NESTING(parentname, childname) \
|
---|
| 49 | { \
|
---|
| 50 | .child = USB_DESCTYPE_##childname, \
|
---|
| 51 | .parent = USB_DESCTYPE_##parentname, \
|
---|
| 52 | }
|
---|
| 53 | #define LAST_NESTING { -1, -1 }
|
---|
| 54 |
|
---|
| 55 | /** Nesting pairs of standard descriptors. */
|
---|
| 56 | static usb_dp_descriptor_nesting_t descriptor_nesting[] = {
|
---|
| 57 | NESTING(CONFIGURATION, INTERFACE),
|
---|
| 58 | NESTING(INTERFACE, ENDPOINT),
|
---|
| 59 | NESTING(INTERFACE, HUB),
|
---|
| 60 | NESTING(INTERFACE, HID),
|
---|
| 61 | NESTING(HID, HID_REPORT),
|
---|
| 62 | LAST_NESTING
|
---|
| 63 | };
|
---|
| 64 |
|
---|
| 65 | /** Tells whether given descriptor is of endpoint type.
|
---|
| 66 | *
|
---|
| 67 | * @param descriptor Descriptor in question.
|
---|
| 68 | * @return Whether the given descriptor is endpoint descriptor.
|
---|
| 69 | */
|
---|
[8a121b1] | 70 | static inline bool is_endpoint_descriptor(const uint8_t *descriptor)
|
---|
[93ef8f6] | 71 | {
|
---|
| 72 | return descriptor[1] == USB_DESCTYPE_ENDPOINT;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /** Tells whether found endpoint corresponds to endpoint described by user.
|
---|
| 76 | *
|
---|
| 77 | * @param wanted Endpoint description as entered by driver author.
|
---|
| 78 | * @param found Endpoint description obtained from endpoint descriptor.
|
---|
| 79 | * @return Whether the @p found descriptor fits the @p wanted descriptor.
|
---|
| 80 | */
|
---|
| 81 | static bool endpoint_fits_description(const usb_endpoint_description_t *wanted,
|
---|
[8a121b1] | 82 | const usb_endpoint_description_t *found)
|
---|
[93ef8f6] | 83 | {
|
---|
| 84 | #define _SAME(fieldname) ((wanted->fieldname) == (found->fieldname))
|
---|
| 85 |
|
---|
| 86 | if (!_SAME(direction)) {
|
---|
| 87 | return false;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | if (!_SAME(transfer_type)) {
|
---|
| 91 | return false;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | if ((wanted->interface_class >= 0) && !_SAME(interface_class)) {
|
---|
| 95 | return false;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | if ((wanted->interface_subclass >= 0) && !_SAME(interface_subclass)) {
|
---|
| 99 | return false;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | if ((wanted->interface_protocol >= 0) && !_SAME(interface_protocol)) {
|
---|
| 103 | return false;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | #undef _SAME
|
---|
| 107 |
|
---|
| 108 | return true;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /** Find endpoint mapping for a found endpoint.
|
---|
| 112 | *
|
---|
| 113 | * @param mapping Endpoint mapping list.
|
---|
| 114 | * @param mapping_count Number of endpoint mappings in @p mapping.
|
---|
| 115 | * @param found_endpoint Description of found endpoint.
|
---|
[18cb870] | 116 | * @param interface_number Number of currently processed interface.
|
---|
[93ef8f6] | 117 | * @return Endpoint mapping corresponding to @p found_endpoint.
|
---|
| 118 | * @retval NULL No corresponding endpoint found.
|
---|
| 119 | */
|
---|
| 120 | static usb_endpoint_mapping_t *find_endpoint_mapping(
|
---|
| 121 | usb_endpoint_mapping_t *mapping, size_t mapping_count,
|
---|
[8a121b1] | 122 | const usb_endpoint_description_t *found_endpoint,
|
---|
[159b91f4] | 123 | int interface_number, int interface_setting)
|
---|
[93ef8f6] | 124 | {
|
---|
| 125 | while (mapping_count > 0) {
|
---|
[18cb870] | 126 | bool interface_number_fits = (mapping->interface_no < 0)
|
---|
| 127 | || (mapping->interface_no == interface_number);
|
---|
| 128 |
|
---|
[159b91f4] | 129 | bool interface_setting_fits = (mapping->interface_setting < 0)
|
---|
| 130 | || (mapping->interface_setting == interface_setting);
|
---|
| 131 |
|
---|
[18cb870] | 132 | bool endpoint_descriptions_fits = endpoint_fits_description(
|
---|
| 133 | mapping->description, found_endpoint);
|
---|
| 134 |
|
---|
[159b91f4] | 135 | if (interface_number_fits
|
---|
| 136 | && interface_setting_fits
|
---|
| 137 | && endpoint_descriptions_fits) {
|
---|
[93ef8f6] | 138 | return mapping;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | mapping++;
|
---|
| 142 | mapping_count--;
|
---|
| 143 | }
|
---|
| 144 | return NULL;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /** Process endpoint descriptor.
|
---|
| 148 | *
|
---|
| 149 | * @param mapping Endpoint mapping list.
|
---|
| 150 | * @param mapping_count Number of endpoint mappings in @p mapping.
|
---|
| 151 | * @param interface Interface descriptor under which belongs the @p endpoint.
|
---|
| 152 | * @param endpoint Endpoint descriptor.
|
---|
| 153 | * @param wire Connection backing the endpoint pipes.
|
---|
| 154 | * @return Error code.
|
---|
| 155 | */
|
---|
| 156 | static int process_endpoint(
|
---|
| 157 | usb_endpoint_mapping_t *mapping, size_t mapping_count,
|
---|
| 158 | usb_standard_interface_descriptor_t *interface,
|
---|
| 159 | usb_standard_endpoint_descriptor_t *endpoint,
|
---|
| 160 | usb_device_connection_t *wire)
|
---|
| 161 | {
|
---|
| 162 |
|
---|
| 163 | /*
|
---|
| 164 | * Get endpoint characteristics.
|
---|
| 165 | */
|
---|
| 166 |
|
---|
| 167 | /* Actual endpoint number is in bits 0..3 */
|
---|
[8a121b1] | 168 | const usb_endpoint_t ep_no = endpoint->endpoint_address & 0x0F;
|
---|
| 169 |
|
---|
| 170 | const usb_endpoint_description_t description = {
|
---|
| 171 | /* Endpoint direction is set by bit 7 */
|
---|
| 172 | .direction = (endpoint->endpoint_address & 128)
|
---|
| 173 | ? USB_DIRECTION_IN : USB_DIRECTION_OUT,
|
---|
| 174 | /* Transfer type is in bits 0..2 and
|
---|
| 175 | * the enum values corresponds 1:1 */
|
---|
| 176 | .transfer_type = endpoint->attributes & 3,
|
---|
| 177 |
|
---|
| 178 | /* Get interface characteristics. */
|
---|
| 179 | .interface_class = interface->interface_class,
|
---|
| 180 | .interface_subclass = interface->interface_subclass,
|
---|
| 181 | .interface_protocol = interface->interface_protocol,
|
---|
| 182 | };
|
---|
[93ef8f6] | 183 |
|
---|
| 184 | /*
|
---|
| 185 | * Find the most fitting mapping and initialize the pipe.
|
---|
| 186 | */
|
---|
| 187 | usb_endpoint_mapping_t *ep_mapping = find_endpoint_mapping(mapping,
|
---|
[159b91f4] | 188 | mapping_count, &description,
|
---|
| 189 | interface->interface_number, interface->alternate_setting);
|
---|
[93ef8f6] | 190 | if (ep_mapping == NULL) {
|
---|
| 191 | return ENOENT;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | if (ep_mapping->present) {
|
---|
| 195 | return EEXISTS;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[b77931d] | 198 | int rc = usb_pipe_initialize(&ep_mapping->pipe, wire,
|
---|
[25971d2] | 199 | ep_no, description.transfer_type, endpoint->max_packet_size,
|
---|
| 200 | description.direction);
|
---|
[93ef8f6] | 201 | if (rc != EOK) {
|
---|
| 202 | return rc;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | ep_mapping->present = true;
|
---|
| 206 | ep_mapping->descriptor = endpoint;
|
---|
[9d4579e] | 207 | ep_mapping->interface = interface;
|
---|
[93ef8f6] | 208 |
|
---|
| 209 | return EOK;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | /** Process whole USB interface.
|
---|
| 213 | *
|
---|
| 214 | * @param mapping Endpoint mapping list.
|
---|
| 215 | * @param mapping_count Number of endpoint mappings in @p mapping.
|
---|
| 216 | * @param parser Descriptor parser.
|
---|
| 217 | * @param parser_data Descriptor parser data.
|
---|
| 218 | * @param interface_descriptor Interface descriptor.
|
---|
| 219 | * @return Error code.
|
---|
| 220 | */
|
---|
| 221 | static int process_interface(
|
---|
| 222 | usb_endpoint_mapping_t *mapping, size_t mapping_count,
|
---|
[7c95d6f5] | 223 | const usb_dp_parser_t *parser, const usb_dp_parser_data_t *parser_data,
|
---|
[8a121b1] | 224 | const uint8_t *interface_descriptor)
|
---|
[93ef8f6] | 225 | {
|
---|
[8a121b1] | 226 | const uint8_t *descriptor = usb_dp_get_nested_descriptor(parser,
|
---|
[93ef8f6] | 227 | parser_data, interface_descriptor);
|
---|
| 228 |
|
---|
| 229 | if (descriptor == NULL) {
|
---|
| 230 | return ENOENT;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | do {
|
---|
| 234 | if (is_endpoint_descriptor(descriptor)) {
|
---|
| 235 | (void) process_endpoint(mapping, mapping_count,
|
---|
| 236 | (usb_standard_interface_descriptor_t *)
|
---|
| 237 | interface_descriptor,
|
---|
| 238 | (usb_standard_endpoint_descriptor_t *)
|
---|
| 239 | descriptor,
|
---|
| 240 | (usb_device_connection_t *) parser_data->arg);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | descriptor = usb_dp_get_sibling_descriptor(parser, parser_data,
|
---|
| 244 | interface_descriptor, descriptor);
|
---|
| 245 | } while (descriptor != NULL);
|
---|
| 246 |
|
---|
| 247 | return EOK;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /** Initialize endpoint pipes from configuration descriptor.
|
---|
| 251 | *
|
---|
| 252 | * The mapping array is expected to conform to following rules:
|
---|
[b77931d] | 253 | * - @c pipe must be uninitialized pipe
|
---|
[93ef8f6] | 254 | * - @c description must point to prepared endpoint description
|
---|
| 255 | * - @c descriptor does not need to be initialized (will be overwritten)
|
---|
[9d4579e] | 256 | * - @c interface does not need to be initialized (will be overwritten)
|
---|
[93ef8f6] | 257 | * - @c present does not need to be initialized (will be overwritten)
|
---|
| 258 | *
|
---|
| 259 | * After processing the configuration descriptor, the mapping is updated
|
---|
| 260 | * in the following fashion:
|
---|
| 261 | * - @c present will be set to @c true when the endpoint was found in the
|
---|
| 262 | * configuration
|
---|
| 263 | * - @c descriptor will point inside the configuration descriptor to endpoint
|
---|
[1110ebd] | 264 | * corresponding to given description (or NULL for not found descriptor)
|
---|
[9d4579e] | 265 | * - @c interface will point inside the configuration descriptor to interface
|
---|
| 266 | * descriptor the endpoint @c descriptor belongs to (or NULL for not found
|
---|
| 267 | * descriptor)
|
---|
[93ef8f6] | 268 | * - @c pipe will be initialized when found, otherwise left untouched
|
---|
| 269 | * - @c description will be untouched under all circumstances
|
---|
| 270 | *
|
---|
| 271 | * @param mapping Endpoint mapping list.
|
---|
| 272 | * @param mapping_count Number of endpoint mappings in @p mapping.
|
---|
| 273 | * @param configuration_descriptor Full configuration descriptor (is expected
|
---|
| 274 | * to be in USB endianness: i.e. as-is after being retrieved from
|
---|
| 275 | * the device).
|
---|
| 276 | * @param configuration_descriptor_size Size of @p configuration_descriptor
|
---|
| 277 | * in bytes.
|
---|
| 278 | * @param connection Connection backing the endpoint pipes.
|
---|
| 279 | * @return Error code.
|
---|
| 280 | */
|
---|
[3954a63b] | 281 | int usb_pipe_initialize_from_configuration(
|
---|
[93ef8f6] | 282 | usb_endpoint_mapping_t *mapping, size_t mapping_count,
|
---|
[7c95d6f5] | 283 | const uint8_t *config_descriptor, size_t config_descriptor_size,
|
---|
[93ef8f6] | 284 | usb_device_connection_t *connection)
|
---|
| 285 | {
|
---|
| 286 | assert(connection);
|
---|
| 287 |
|
---|
[7c95d6f5] | 288 | if (config_descriptor == NULL) {
|
---|
[93ef8f6] | 289 | return EBADMEM;
|
---|
| 290 | }
|
---|
[7c95d6f5] | 291 | if (config_descriptor_size
|
---|
[93ef8f6] | 292 | < sizeof(usb_standard_configuration_descriptor_t)) {
|
---|
| 293 | return ERANGE;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[b77931d] | 296 | /* Go through the mapping and set all endpoints to not present. */
|
---|
| 297 | for (size_t i = 0; i < mapping_count; i++) {
|
---|
[93ef8f6] | 298 | mapping[i].present = false;
|
---|
| 299 | mapping[i].descriptor = NULL;
|
---|
[9d4579e] | 300 | mapping[i].interface = NULL;
|
---|
[93ef8f6] | 301 | }
|
---|
| 302 |
|
---|
[b77931d] | 303 | /* Prepare the descriptor parser. */
|
---|
[7c95d6f5] | 304 | const usb_dp_parser_t dp_parser = {
|
---|
[93ef8f6] | 305 | .nesting = descriptor_nesting
|
---|
| 306 | };
|
---|
[7c95d6f5] | 307 | const usb_dp_parser_data_t dp_data = {
|
---|
| 308 | .data = config_descriptor,
|
---|
| 309 | .size = config_descriptor_size,
|
---|
[93ef8f6] | 310 | .arg = connection
|
---|
| 311 | };
|
---|
| 312 |
|
---|
| 313 | /*
|
---|
| 314 | * Iterate through all interfaces.
|
---|
| 315 | */
|
---|
[8a121b1] | 316 | const uint8_t *interface = usb_dp_get_nested_descriptor(&dp_parser,
|
---|
[7c95d6f5] | 317 | &dp_data, config_descriptor);
|
---|
[93ef8f6] | 318 | if (interface == NULL) {
|
---|
| 319 | return ENOENT;
|
---|
| 320 | }
|
---|
| 321 | do {
|
---|
| 322 | (void) process_interface(mapping, mapping_count,
|
---|
[8a121b1] | 323 | &dp_parser, &dp_data, interface);
|
---|
[93ef8f6] | 324 | interface = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
|
---|
[7c95d6f5] | 325 | config_descriptor, interface);
|
---|
[93ef8f6] | 326 | } while (interface != NULL);
|
---|
| 327 |
|
---|
| 328 | return EOK;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
[dc04868] | 331 | /** Initialize USB endpoint pipe.
|
---|
| 332 | *
|
---|
| 333 | * @param pipe Endpoint pipe to be initialized.
|
---|
| 334 | * @param connection Connection to the USB device backing this pipe (the wire).
|
---|
| 335 | * @param endpoint_no Endpoint number (in USB 1.1 in range 0 to 15).
|
---|
| 336 | * @param transfer_type Transfer type (e.g. interrupt or bulk).
|
---|
| 337 | * @param max_packet_size Maximum packet size in bytes.
|
---|
| 338 | * @param direction Endpoint direction (in/out).
|
---|
| 339 | * @return Error code.
|
---|
| 340 | */
|
---|
[3954a63b] | 341 | int usb_pipe_initialize(usb_pipe_t *pipe,
|
---|
[dc04868] | 342 | usb_device_connection_t *connection, usb_endpoint_t endpoint_no,
|
---|
| 343 | usb_transfer_type_t transfer_type, size_t max_packet_size,
|
---|
| 344 | usb_direction_t direction)
|
---|
| 345 | {
|
---|
| 346 | assert(pipe);
|
---|
| 347 | assert(connection);
|
---|
| 348 |
|
---|
[d48fcc0] | 349 | fibril_mutex_initialize(&pipe->guard);
|
---|
[dc04868] | 350 | pipe->wire = connection;
|
---|
[79ae36dd] | 351 | pipe->hc_sess = NULL;
|
---|
| 352 | fibril_mutex_initialize(&pipe->hc_sess_mutex);
|
---|
[dc04868] | 353 | pipe->endpoint_no = endpoint_no;
|
---|
| 354 | pipe->transfer_type = transfer_type;
|
---|
| 355 | pipe->max_packet_size = max_packet_size;
|
---|
| 356 | pipe->direction = direction;
|
---|
[a546687] | 357 | pipe->refcount = 0;
|
---|
[2c2cbcf] | 358 | pipe->refcount_soft = 0;
|
---|
[fa0f53b] | 359 | pipe->auto_reset_halt = false;
|
---|
[dc04868] | 360 |
|
---|
| 361 | return EOK;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 |
|
---|
| 365 | /** Initialize USB endpoint pipe as the default zero control pipe.
|
---|
| 366 | *
|
---|
| 367 | * @param pipe Endpoint pipe to be initialized.
|
---|
| 368 | * @param connection Connection to the USB device backing this pipe (the wire).
|
---|
| 369 | * @return Error code.
|
---|
| 370 | */
|
---|
[3954a63b] | 371 | int usb_pipe_initialize_default_control(usb_pipe_t *pipe,
|
---|
[dc04868] | 372 | usb_device_connection_t *connection)
|
---|
| 373 | {
|
---|
| 374 | assert(pipe);
|
---|
| 375 | assert(connection);
|
---|
| 376 |
|
---|
[3954a63b] | 377 | int rc = usb_pipe_initialize(pipe, connection,
|
---|
[206f71a] | 378 | 0, USB_TRANSFER_CONTROL, CTRL_PIPE_MIN_PACKET_SIZE,
|
---|
| 379 | USB_DIRECTION_BOTH);
|
---|
| 380 |
|
---|
[fa0f53b] | 381 | pipe->auto_reset_halt = true;
|
---|
| 382 |
|
---|
[206f71a] | 383 | return rc;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | /** Probe default control pipe for max packet size.
|
---|
| 387 | *
|
---|
| 388 | * The function tries to get the correct value of max packet size several
|
---|
| 389 | * time before giving up.
|
---|
| 390 | *
|
---|
| 391 | * The session on the pipe shall not be started.
|
---|
| 392 | *
|
---|
| 393 | * @param pipe Default control pipe.
|
---|
| 394 | * @return Error code.
|
---|
| 395 | */
|
---|
[3954a63b] | 396 | int usb_pipe_probe_default_control(usb_pipe_t *pipe)
|
---|
[206f71a] | 397 | {
|
---|
| 398 | assert(pipe);
|
---|
| 399 | assert(DEV_DESCR_MAX_PACKET_SIZE_OFFSET < CTRL_PIPE_MIN_PACKET_SIZE);
|
---|
| 400 |
|
---|
| 401 | if ((pipe->direction != USB_DIRECTION_BOTH) ||
|
---|
| 402 | (pipe->transfer_type != USB_TRANSFER_CONTROL) ||
|
---|
| 403 | (pipe->endpoint_no != 0)) {
|
---|
| 404 | return EINVAL;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | #define TRY_LOOP(attempt_var) \
|
---|
| 408 | for (attempt_var = 0; attempt_var < 3; attempt_var++)
|
---|
| 409 |
|
---|
| 410 | size_t failed_attempts;
|
---|
| 411 | int rc;
|
---|
| 412 |
|
---|
[2c2cbcf] | 413 | usb_pipe_start_long_transfer(pipe);
|
---|
[206f71a] | 414 |
|
---|
| 415 | uint8_t dev_descr_start[CTRL_PIPE_MIN_PACKET_SIZE];
|
---|
| 416 | size_t transferred_size;
|
---|
| 417 | TRY_LOOP(failed_attempts) {
|
---|
| 418 | rc = usb_request_get_descriptor(pipe, USB_REQUEST_TYPE_STANDARD,
|
---|
| 419 | USB_REQUEST_RECIPIENT_DEVICE, USB_DESCTYPE_DEVICE,
|
---|
| 420 | 0, 0, dev_descr_start, CTRL_PIPE_MIN_PACKET_SIZE,
|
---|
| 421 | &transferred_size);
|
---|
| 422 | if (rc == EOK) {
|
---|
| 423 | if (transferred_size != CTRL_PIPE_MIN_PACKET_SIZE) {
|
---|
| 424 | rc = ELIMIT;
|
---|
| 425 | continue;
|
---|
| 426 | }
|
---|
| 427 | break;
|
---|
| 428 | }
|
---|
| 429 | }
|
---|
[4ede178] | 430 | usb_pipe_end_long_transfer(pipe);
|
---|
[206f71a] | 431 | if (rc != EOK) {
|
---|
[bf4cc3e] | 432 | return rc;
|
---|
| 433 | }
|
---|
[dc04868] | 434 |
|
---|
[206f71a] | 435 | pipe->max_packet_size
|
---|
| 436 | = dev_descr_start[DEV_DESCR_MAX_PACKET_SIZE_OFFSET];
|
---|
| 437 |
|
---|
| 438 | return EOK;
|
---|
[dc04868] | 439 | }
|
---|
| 440 |
|
---|
[b7d8fd9] | 441 | /** Register endpoint with the host controller.
|
---|
| 442 | *
|
---|
| 443 | * @param pipe Pipe to be registered.
|
---|
| 444 | * @param interval Polling interval.
|
---|
| 445 | * @param hc_connection Connection to the host controller (must be opened).
|
---|
| 446 | * @return Error code.
|
---|
| 447 | */
|
---|
[27736cf] | 448 | int usb_pipe_register(usb_pipe_t *pipe, unsigned interval,
|
---|
[1998bcd] | 449 | usb_hc_connection_t *hc_connection)
|
---|
[b7d8fd9] | 450 | {
|
---|
| 451 | assert(pipe);
|
---|
| 452 | assert(hc_connection);
|
---|
[27736cf] | 453 |
|
---|
[79ae36dd] | 454 | if (!usb_hc_connection_is_opened(hc_connection))
|
---|
[b7d8fd9] | 455 | return EBADF;
|
---|
[27736cf] | 456 |
|
---|
[365e29e2] | 457 | const usb_target_t target =
|
---|
| 458 | {{ .address = pipe->wire->address, .endpoint = pipe->endpoint_no }};
|
---|
[27736cf] | 459 | #define _PACK2(high, low) (((high & 0xffff) << 16) | (low & 0xffff))
|
---|
| 460 |
|
---|
[79ae36dd] | 461 | async_exch_t *exch = async_exchange_begin(hc_connection->hc_sess);
|
---|
| 462 | int rc = async_req_4_0(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
|
---|
[365e29e2] | 463 | IPC_M_USBHC_REGISTER_ENDPOINT, target.packed,
|
---|
[27736cf] | 464 | _PACK2(pipe->transfer_type, pipe->direction),
|
---|
[1998bcd] | 465 | _PACK2(pipe->max_packet_size, interval));
|
---|
[79ae36dd] | 466 | async_exchange_end(exch);
|
---|
[27736cf] | 467 |
|
---|
[1998bcd] | 468 | #undef _PACK2
|
---|
[79ae36dd] | 469 | return rc;
|
---|
[b7d8fd9] | 470 | }
|
---|
| 471 |
|
---|
| 472 | /** Revert endpoint registration with the host controller.
|
---|
| 473 | *
|
---|
| 474 | * @param pipe Pipe to be unregistered.
|
---|
| 475 | * @param hc_connection Connection to the host controller (must be opened).
|
---|
| 476 | * @return Error code.
|
---|
| 477 | */
|
---|
[3954a63b] | 478 | int usb_pipe_unregister(usb_pipe_t *pipe,
|
---|
[b7d8fd9] | 479 | usb_hc_connection_t *hc_connection)
|
---|
| 480 | {
|
---|
| 481 | assert(pipe);
|
---|
[1526c174] | 482 | assert(pipe->wire);
|
---|
[b7d8fd9] | 483 | assert(hc_connection);
|
---|
[79ae36dd] | 484 |
|
---|
| 485 | if (!usb_hc_connection_is_opened(hc_connection))
|
---|
[b7d8fd9] | 486 | return EBADF;
|
---|
[79ae36dd] | 487 |
|
---|
| 488 | async_exch_t *exch = async_exchange_begin(hc_connection->hc_sess);
|
---|
| 489 | int rc = async_req_4_0(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
|
---|
| 490 | IPC_M_USBHC_UNREGISTER_ENDPOINT,
|
---|
[b7d8fd9] | 491 | pipe->wire->address, pipe->endpoint_no, pipe->direction);
|
---|
[79ae36dd] | 492 | async_exchange_end(exch);
|
---|
| 493 |
|
---|
| 494 | return rc;
|
---|
[b7d8fd9] | 495 | }
|
---|
| 496 |
|
---|
[93ef8f6] | 497 | /**
|
---|
| 498 | * @}
|
---|
| 499 | */
|
---|