summaryrefslogtreecommitdiff
path: root/common/usbx_host_controllers/src/ux_hcd_ohci_request_bulk_transfer.c
blob: 02e20ba32bfc3ce37bd653bdf7ad1140d6d1a406 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/***************************************************************************
 * 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                                                        */
/**                                                                       */
/**   OHCI Controller Driver                                              */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

#include "ux_api.h"
#include "ux_hcd_ohci.h"
#include "ux_host_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_hcd_ohci_request_bulk_transfer                  PORTABLE C      */
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*     This function performs a bulk transfer request. A bulk transfer    */
/*     can be larger than the size of the OHCI buffer so it may be        */
/*     required to chain multiple tds to accommodate this transfer        */
/*     request. A bulk transfer is non blocking, so we return before the  */
/*     transfer request is completed.                                     */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    hcd_ohci                              Pointer to OHCI controller    */
/*    transfer_request                      Pointer to transfer request   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_hcd_ohci_register_read            Read OHCI register            */
/*    _ux_hcd_ohci_register_write           Write OHCI register           */
/*    _ux_hcd_ohci_regular_td_obtain        Get regular TD                */
/*    _ux_utility_physical_address          Get physical address          */
/*    _ux_utility_virtual_address           Get virtual address           */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    OHCI Controller Driver                                              */
/*                                                                        */
/**************************************************************************/
UINT  _ux_hcd_ohci_request_bulk_transfer(UX_HCD_OHCI *hcd_ohci, UX_TRANSFER *transfer_request)
{

UX_ENDPOINT     *endpoint;
UX_OHCI_TD      *data_td;
UX_OHCI_TD      *start_data_td;
UX_OHCI_TD      *next_data_td;
UX_OHCI_TD      *previous_td;
UX_OHCI_TD      *tail_td;
UX_OHCI_ED      *ed;
ULONG           transfer_request_payload_length;
ULONG           bulk_packet_payload_length;
UCHAR *         data_pointer;
ULONG           ohci_register;
ULONG           zlp_flag;


    /* Get the pointer to the Endpoint.  */
    endpoint =  (UX_ENDPOINT *) transfer_request -> ux_transfer_request_endpoint;

    /* Now get the physical ED attached to this endpoint.  */
    ed =  endpoint -> ux_endpoint_ed;

    /* Use the TD pointer by ed -> tail for the first TD of this transfer
        and chain from this one on.  */
    data_td =  _ux_utility_virtual_address(ed -> ux_ohci_ed_tail_td);
    previous_td =  data_td;

    /* Reset the first obtained data TD in case there is a TD shortage while building the list of tds.  */
    start_data_td =  0;

    /* It may take more than one TD if the transfer_request length is more than the
       maximum length for a OHCI TD (this is irrelevant of the MaxPacketSize value in
       the endpoint descriptor). OHCI data payload has a maximum size of 4K.  */
    transfer_request_payload_length =  transfer_request -> ux_transfer_request_requested_length;
    data_pointer =  transfer_request -> ux_transfer_request_data_pointer;

    /* Check for ZLP condition.  */
    if (transfer_request_payload_length == 0)

        /* We have a zlp condition.  */
        zlp_flag = UX_TRUE;
    else

        /* We do not have a zlp.  */
        zlp_flag = UX_FALSE;

    /* Build all necessary TDs.  */
    while ((transfer_request_payload_length != 0) || zlp_flag == UX_TRUE)
    {

        /* Reset ZLP now.  */
        zlp_flag = UX_FALSE;

        /* Check if we are exceeding the max payload. */
        if (transfer_request_payload_length > UX_OHCI_MAX_PAYLOAD)

            bulk_packet_payload_length =  UX_OHCI_MAX_PAYLOAD;
        else

            bulk_packet_payload_length =  transfer_request_payload_length;

        /* IN transfer ? */
        if ((transfer_request -> ux_transfer_request_type&UX_REQUEST_DIRECTION) == UX_REQUEST_IN)

            data_td -> ux_ohci_td_dw0 =  UX_OHCI_TD_IN | UX_OHCI_TD_DEFAULT_DW0;
        else

            data_td -> ux_ohci_td_dw0 =  UX_OHCI_TD_OUT | UX_OHCI_TD_DEFAULT_DW0;

        /* Store the beginning of the buffer address in the TD.  */
        data_td -> ux_ohci_td_cbp =  _ux_utility_physical_address(data_pointer);

        /* Store the end buffer address in the TD.  */
        data_td -> ux_ohci_td_be =  data_td -> ux_ohci_td_cbp + bulk_packet_payload_length - 1;

        /* Update the length of the transfer for this TD.  */
        data_td -> ux_ohci_td_length =  bulk_packet_payload_length;

        /* Attach the endpoint and transfer request to the TD.  */
        data_td -> ux_ohci_td_transfer_request =  transfer_request;
        data_td -> ux_ohci_td_ed =  ed;

        /* Adjust the data payload length and the data payload pointer.  */
        transfer_request_payload_length -=  bulk_packet_payload_length;
        data_pointer +=  bulk_packet_payload_length;

        /* Check if there will be another transaction.  */
        if (transfer_request_payload_length != 0)
        {

            /* Get a new TD to hook this payload.  */
            data_td =  _ux_hcd_ohci_regular_td_obtain(hcd_ohci);

            if (data_td == UX_NULL)
            {

                /* If there was already a TD chain in progress, free it.  */
                if (start_data_td != UX_NULL)
                {

                    data_td =  start_data_td;
                    while(data_td)
                    {

                        next_data_td =  _ux_utility_virtual_address(data_td -> ux_ohci_td_next_td);
                        data_td -> ux_ohci_td_status =  UX_UNUSED;
                        data_td =  next_data_td;
                    }
                }

                return(UX_NO_TD_AVAILABLE);
            }

            /* the first obtained TD in the chain has to be remembered.  */
            if (start_data_td == UX_NULL)
                start_data_td =  data_td;

            /* Attach this new TD to the previous one.  */
            previous_td -> ux_ohci_td_next_td =  _ux_utility_physical_address(data_td);
            previous_td -> ux_ohci_td_next_td_transfer_request =  data_td;
            previous_td =  data_td;
        }
    }

    /* At this stage, the Head and Tail in the ED are still the same and the OHCI controller
       will skip this ED until we have hooked the new tail TD.  */
    tail_td =  _ux_hcd_ohci_regular_td_obtain(hcd_ohci);
    if (tail_td == UX_NULL)
    {

        /* If there was already a TD chain in progress, free it.  */
        if (start_data_td != UX_NULL)
        {

            data_td =  start_data_td;
            while(data_td)
            {

                next_data_td =  _ux_utility_virtual_address(data_td -> ux_ohci_td_next_td);
                data_td -> ux_ohci_td_status =  UX_UNUSED;
                data_td =  next_data_td;
            }
        }

        return(UX_NO_TD_AVAILABLE);
    }

    /* Attach the tail TD to the last data TD.  */
    data_td -> ux_ohci_td_next_td =  _ux_utility_physical_address(tail_td);

    /* Store the new tail TD.  */
    ed -> ux_ohci_ed_tail_td =  _ux_utility_physical_address(tail_td);

    /* Now, we must tell the OHCI controller that there is something in the
       bulk queue.  */
    ohci_register =  _ux_hcd_ohci_register_read(hcd_ohci, OHCI_HC_COMMAND_STATUS);
    ohci_register |=  OHCI_HC_CS_BLF;
    _ux_hcd_ohci_register_write(hcd_ohci, OHCI_HC_COMMAND_STATUS, ohci_register);

    /* Return successful completion.  */
    return(UX_SUCCESS);
}