summaryrefslogtreecommitdiff
path: root/common/usbx_host_controllers/src/ux_hcd_ehci_interrupt_handler.c
blob: 3caef3cffef6b7f3b7cf49fd67bac29555482232 (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
/***************************************************************************
 * 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                                                        */
/**                                                                       */
/**   EHCI Controller Driver                                              */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

#include "ux_api.h"
#include "ux_hcd_ehci.h"
#include "ux_host_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_hcd_ehci_interrupt_handler                      PORTABLE C      */
/*                                                           6.1.10       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is the interrupt handler for the EHCI interrupts.     */
/*    Normally an interrupt occurs from the controller when there is      */
/*    either a EOF signal and there has been transfers within the frame   */
/*    or when there is a change on one of the downstream ports, a         */
/*    doorbell signal or an unrecoverable error.                          */
/*                                                                        */
/*    All we need to do in the ISR is scan the controllers to find out    */
/*    which one has issued a IRQ. If there is work to do for this         */
/*    controller we need to wake up the corresponding thread to take care */
/*    of the job.                                                         */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_hcd_ehci_controller_disable       Disable controller            */
/*    _ux_hcd_ehci_register_read            Read EHCI register            */
/*    _ux_hcd_ehci_register_write           Write EHCI register           */
/*    _ux_host_semaphore_put                Put semaphore                 */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    EHCI Controller Driver                                              */
/*                                                                        */
/**************************************************************************/
VOID  _ux_hcd_ehci_interrupt_handler(VOID)
{

UINT            hcd_index;
UX_HCD          *hcd;
UX_HCD_EHCI     *hcd_ehci;
ULONG           ehci_register;
ULONG           ehci_register_port_status;
ULONG           root_hub_thread_wakeup = 0;
ULONG           port_index;


    /* We need to parse the controller driver table to find all controllers that registered
       as EHCI.  */
    for (hcd_index = 0; hcd_index < _ux_system_host -> ux_system_host_registered_hcd; hcd_index++)
    {

        /* Check type of controller.  */
        if (_ux_system_host -> ux_system_host_hcd_array[hcd_index].ux_hcd_controller_type == UX_EHCI_CONTROLLER)
        {

            /* Get the pointers to the generic HCD and EHCI specific areas.  */
            hcd =  &_ux_system_host -> ux_system_host_hcd_array[hcd_index];
            hcd_ehci =  (UX_HCD_EHCI *) hcd -> ux_hcd_controller_hardware;

            /* Check if the controller is operational, if not, skip it.  */
            if (hcd -> ux_hcd_status == UX_HCD_STATUS_OPERATIONAL)
            {

                /* For debugging purposes, increment the interrupt count.  */
                hcd_ehci -> ux_hcd_ehci_interrupt_count++;

                /* We get the current interrupt status for this controller.  */
                ehci_register =  _ux_hcd_ehci_register_read(hcd_ehci, EHCI_HCOR_USB_STATUS);

                /* We  acknowledge the interrupts for this controller so that it
                   can continue to work.  */
                _ux_hcd_ehci_register_write(hcd_ehci, EHCI_HCOR_USB_STATUS, ehci_register);

                /* Examine the source of interrupts.  */
                if ((ehci_register & EHCI_HC_STS_USB_INT) || (ehci_register & EHCI_HC_STS_USB_ERR_INT))
                {

                    /* We have some transactions done in the past frame/micro-frame.
                       The controller thread needs to wake up and process them.  */
                    hcd -> ux_hcd_thread_signal++;
                    _ux_host_semaphore_put(&_ux_system_host -> ux_system_host_hcd_semaphore);
                }

                if (ehci_register & EHCI_HC_STS_HSE)
                {

                    /* The controller has issued a Host System Error which is fatal.
                       The controller will be reset now, and we wake up the HCD thread.  */
                    _ux_hcd_ehci_controller_disable(hcd_ehci);
                    hcd -> ux_hcd_thread_signal++;
                    hcd -> ux_hcd_status =  UX_HCD_STATUS_DEAD;
                    hcd -> ux_hcd_thread_signal++;
                    _ux_host_semaphore_put(&_ux_system_host -> ux_system_host_hcd_semaphore);

                    /* Error trap. */
                    _ux_system_error_handler(UX_SYSTEM_LEVEL_INTERRUPT, UX_SYSTEM_CONTEXT_HCD, UX_CONTROLLER_DEAD);

                }

                if (ehci_register & EHCI_HC_STS_PCD)
                {

                    /* The controller has issued a Root hub status change signal. Scan all ports.  */
                    for (port_index = 0; port_index < hcd_ehci -> ux_hcd_ehci_nb_root_hubs; port_index++)
                    {

                        /* Read the port status.  */
                        ehci_register_port_status =  _ux_hcd_ehci_register_read(hcd_ehci, EHCI_HCOR_PORT_SC + port_index);

                        /* Check for Connect Status Change signal.  */
                        if (ehci_register_port_status &  EHCI_HC_PS_CSC)
                        {
                            /* Something happened on this port. Signal it to the root hub thread.  */
                            hcd -> ux_hcd_root_hub_signal[port_index]++;

                            /* Memorize wake up signal.  */
                            root_hub_thread_wakeup ++;

                        }

                    }

                    /* We only wake up the root hub thread if there has been device insertion/extraction.  */
                    if (root_hub_thread_wakeup != 0)

                        /* The controller has issued a Root hub status change signal.
                           We need to resume the thread in charge of the USB topology.  */
                        _ux_host_semaphore_put(&_ux_system_host -> ux_system_host_enum_semaphore);
                }

                if (ehci_register & EHCI_HC_STS_IAA)
                {

                    /* The controller has issued a Door Bell status change signal.
                       We need to resume the thread who raised the doorbell.  */
                    _ux_host_semaphore_put(&hcd_ehci -> ux_hcd_ehci_doorbell_semaphore);
                }
            }
        }
    }
}