Changeset 944f8fdd in mainline for uspace/lib/usbhost/src/hcd.c


Ignore:
Timestamp:
2018-01-19T17:38:22Z (6 years ago)
Author:
Ondřej Hlavatý <aearsis@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2833bb4
Parents:
861b5d6
git-author:
Ondřej Hlavatý <aearsis@…> (2018-01-19 17:06:40)
git-committer:
Ondřej Hlavatý <aearsis@…> (2018-01-19 17:38:22)
Message:

libusbhost: move utility functions to new header utility.h

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usbhost/src/hcd.c

    r861b5d6 r944f8fdd  
    370370}
    371371
    372 /** Get max packet size for the control endpoint 0.
    373  *
    374  * For LS, HS, and SS devices this value is fixed. For FS devices we must fetch
    375  * the first 8B of the device descriptor to determine it.
    376  *
    377  * @return Max packet size for EP 0
    378  */
    379 int hcd_get_ep0_max_packet_size(uint16_t *mps, bus_t *bus, device_t *dev)
    380 {
    381         assert(mps);
    382 
    383         static const uint16_t mps_fixed [] = {
    384                 [USB_SPEED_LOW] = 8,
    385                 [USB_SPEED_HIGH] = 64,
    386                 [USB_SPEED_SUPER] = 512,
    387         };
    388 
    389         if (dev->speed < ARRAY_SIZE(mps_fixed) && mps_fixed[dev->speed] != 0) {
    390                 *mps = mps_fixed[dev->speed];
    391                 return EOK;
    392         }
    393 
    394         const usb_target_t control_ep = {{
    395                 .address = dev->address,
    396                 .endpoint = 0,
    397         }};
    398 
    399         usb_standard_device_descriptor_t desc = { 0 };
    400         const usb_device_request_setup_packet_t get_device_desc_8 =
    401             GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
    402 
    403         usb_log_debug("Requesting first 8B of device descriptor to determine MPS.");
    404         ssize_t got = bus_device_send_batch_sync(dev, control_ep, USB_DIRECTION_IN,
    405             (char *) &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
    406             "read first 8 bytes of dev descriptor");
    407 
    408         if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
    409                 const int err = got < 0 ? got : EOVERFLOW;
    410                 usb_log_error("Failed to get 8B of dev descr: %s.", str_error(err));
    411                 return err;
    412         }
    413 
    414         if (desc.descriptor_type != USB_DESCTYPE_DEVICE) {
    415                 usb_log_error("The device responded with wrong device descriptor.");
    416                 return EIO;
    417         }
    418 
    419         uint16_t version = uint16_usb2host(desc.usb_spec_version);
    420         if (version < 0x0300) {
    421                 /* USB 2 and below have MPS raw in the field */
    422                 *mps = desc.max_packet_size;
    423         } else {
    424                 /* USB 3 have MPS as an 2-based exponent */
    425                 *mps = (1 << desc.max_packet_size);
    426         }
    427         return EOK;
    428 }
    429 
    430 /** Check setup packet data for signs of toggle reset.
    431  *
    432  * @param[in] requst Setup requst data.
    433  *
    434  * @retval -1 No endpoints need reset.
    435  * @retval 0 All endpoints need reset.
    436  * @retval >0 Specified endpoint needs reset.
    437  *
    438  */
    439 toggle_reset_mode_t hcd_get_request_toggle_reset_mode(
    440     const usb_device_request_setup_packet_t *request)
    441 {
    442         assert(request);
    443         switch (request->request)
    444         {
    445         /* Clear Feature ENPOINT_STALL */
    446         case USB_DEVREQ_CLEAR_FEATURE: /*resets only cleared ep */
    447                 /* 0x2 ( HOST to device | STANDART | TO ENPOINT) */
    448                 if ((request->request_type == 0x2) &&
    449                     (request->value == USB_FEATURE_ENDPOINT_HALT))
    450                         return RESET_EP;
    451                 break;
    452         case USB_DEVREQ_SET_CONFIGURATION:
    453         case USB_DEVREQ_SET_INTERFACE:
    454                 /* Recipient must be device, this resets all endpoints,
    455                  * In fact there should be no endpoints but EP 0 registered
    456                  * as different interfaces use different endpoints,
    457                  * unless you're changing configuration or alternative
    458                  * interface of an already setup device. */
    459                 if (!(request->request_type & SETUP_REQUEST_TYPE_DEVICE_TO_HOST))
    460                         return RESET_ALL;
    461                 break;
    462         default:
    463                 break;
    464         }
    465 
    466         return RESET_NONE;
    467 }
    468 
    469372
    470373/**
Note: See TracChangeset for help on using the changeset viewer.