/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** USBX Component                                                        */
/**                                                                       */
/**   Video Class                                                         */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

#include "ux_api.h"
#include "ux_host_class_video.h"
#include "ux_host_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_class_video_transfer_request_callback      PORTABLE C      */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function receives a completion call back on an isoch transfer  */
/*    request.                                                            */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    transfer_request                      Pointer to transfer request   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    (ux_host_class_video_transfer_completion_function)                  */
/*                                          Transfer request completion   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Video Class                                                         */
/*                                                                        */
/**************************************************************************/
VOID  _ux_host_class_video_transfer_request_callback(UX_TRANSFER *transfer_request)
{

UX_HOST_CLASS_VIDEO *video;
UX_ENDPOINT         *endpoint;
ULONG               transfer_index;


    /* Get the pointer to the video instance.  */
    video =  (UX_HOST_CLASS_VIDEO *) transfer_request -> ux_transfer_request_class_instance;

    /* Do a sanity check on the transfer request, if NULL something is wrong.  */
    if (video == UX_NULL)
        return;

    /* Check endpoint status.  */
    endpoint = video -> ux_host_class_video_isochronous_endpoint;
    if (endpoint -> ux_endpoint_transfer_request.ux_transfer_request_completion_code != UX_TRANSFER_STATUS_PENDING)
        return;

    /* The caller's transfer request needs to be updated.  */
    transfer_index = video -> ux_host_class_video_transfer_request_end_index;
    transfer_index++;
    if (transfer_index == UX_HOST_CLASS_VIDEO_TRANSFER_REQUEST_COUNT)
        transfer_index = 0;

    /* Update the transfer index.  */
    video -> ux_host_class_video_transfer_request_end_index = transfer_index;

    /* Call the completion routine.  */
    if (video -> ux_host_class_video_transfer_completion_function)
        video -> ux_host_class_video_transfer_completion_function(transfer_request);

    /* Return to caller.  */
    return;
}
