1 /*!
2     \file    usbh_pipe.c
3     \brief   USB host mode pipe operation driver
4 
5     \version 2020-08-04, V1.1.0, firmware for GD32VF103
6 */
7 
8 /*
9     Copyright (c) 2020, GigaDevice Semiconductor Inc.
10 
11     Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13 
14     1. Redistributions of source code must retain the above copyright notice, this
15        list of conditions and the following disclaimer.
16     2. Redistributions in binary form must reproduce the above copyright notice,
17        this list of conditions and the following disclaimer in the documentation
18        and/or other materials provided with the distribution.
19     3. Neither the name of the copyright holder nor the names of its contributors
20        may be used to endorse or promote products derived from this software without
21        specific prior written permission.
22 
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34 
35 #include "usbh_pipe.h"
36 
37 /* local function prototypes ('static') */
38 static uint16_t usbh_freepipe_get (usb_core_driver *pudev);
39 
40 /*!
41     \brief      create a pipe
42     \param[in]  pudev: pointer to usb core instance
43     \param[in]  udev: USB device
44     \param[in]  pp_num: pipe number
45     \param[in]  ep_type: endpoint type
46     \param[in]  ep_mpl: endpoint max packet length
47     \param[out] none
48     \retval     operation status
49 */
usbh_pipe_create(usb_core_driver * pudev,usb_dev_prop * udev,uint8_t pp_num,uint8_t ep_type,uint16_t ep_mpl)50 uint8_t usbh_pipe_create (usb_core_driver *pudev,
51                           usb_dev_prop *udev,
52                           uint8_t  pp_num,
53                           uint8_t  ep_type,
54                           uint16_t ep_mpl)
55 {
56     usb_pipe *pp = &pudev->host.pipe[pp_num];
57 
58     pp->dev_addr = udev->addr;
59     pp->dev_speed = udev->speed;
60     pp->ep.type = ep_type;
61     pp->ep.mps = ep_mpl;
62     pp->ping = (uint8_t)(udev->speed == PORT_SPEED_HIGH);
63 
64     usb_pipe_init (pudev, pp_num);
65 
66     return HC_OK;
67 }
68 
69 /*!
70     \brief      modify a pipe
71     \param[in]  pudev: pointer to usb core instance
72     \param[in]  pp_num: pipe number
73     \param[in]  dev_addr: device address
74     \param[in]  dev_speed: device speed
75     \param[in]  ep_mpl: endpoint max packet length
76     \param[out] none
77     \retval     operation status
78 */
usbh_pipe_update(usb_core_driver * pudev,uint8_t pp_num,uint8_t dev_addr,uint32_t dev_speed,uint16_t ep_mpl)79 uint8_t usbh_pipe_update (usb_core_driver *pudev,
80                           uint8_t  pp_num,
81                           uint8_t  dev_addr,
82                           uint32_t dev_speed,
83                           uint16_t ep_mpl)
84 {
85     usb_pipe *pp = &pudev->host.pipe[pp_num];
86 
87     if ((pp->dev_addr != dev_addr) && (dev_addr)) {
88         pp->dev_addr = dev_addr;
89     }
90 
91     if ((pp->dev_speed != dev_speed) && (dev_speed)) {
92         pp->dev_speed = dev_speed;
93     }
94 
95     if ((pp->ep.mps != ep_mpl) && (ep_mpl)) {
96         pp->ep.mps = ep_mpl;
97     }
98 
99     usb_pipe_init (pudev, pp_num);
100 
101     return HC_OK;
102 }
103 
104 /*!
105     \brief      allocate a new pipe
106     \param[in]  pudev: pointer to usb core instance
107     \param[in]  ep_addr: endpoint address
108     \param[out] none
109     \retval     operation status
110 */
usbh_pipe_allocate(usb_core_driver * pudev,uint8_t ep_addr)111 uint8_t usbh_pipe_allocate (usb_core_driver *pudev, uint8_t ep_addr)
112 {
113     uint16_t pp_num = usbh_freepipe_get (pudev);
114 
115     if (HC_ERROR != pp_num) {
116         pudev->host.pipe[pp_num].in_used = 1U;
117         pudev->host.pipe[pp_num].ep.dir = EP_DIR(ep_addr);
118         pudev->host.pipe[pp_num].ep.num = EP_ID(ep_addr);
119     }
120 
121     return (uint8_t)pp_num;
122 }
123 
124 /*!
125     \brief      free a pipe
126     \param[in]  pudev: pointer to usb core instance
127     \param[in]  pp_num: pipe number
128     \param[out] none
129     \retval     operation status
130 */
usbh_pipe_free(usb_core_driver * pudev,uint8_t pp_num)131 uint8_t usbh_pipe_free (usb_core_driver *pudev, uint8_t pp_num)
132 {
133     if (pp_num < HC_MAX) {
134         pudev->host.pipe[pp_num].in_used = 0U;
135     }
136 
137     return USBH_OK;
138 }
139 
140 /*!
141     \brief      delete all USB host pipe
142     \param[in]  pudev: pointer to usb core instance
143     \param[out] none
144     \retval     operation status
145 */
usbh_pipe_delete(usb_core_driver * pudev)146 uint8_t usbh_pipe_delete (usb_core_driver *pudev)
147 {
148     uint8_t pp_num = 0U;
149 
150     for (pp_num = 2U; pp_num < HC_MAX; pp_num++) {
151         pudev->host.pipe[pp_num] = (usb_pipe) {0};
152     }
153 
154     return USBH_OK;
155 }
156 
157 /*!
158     \brief      get a free pipe number for allocation
159     \param[in]  pudev: pointer to usb core instance
160     \param[out] none
161     \retval     operation status
162 */
usbh_freepipe_get(usb_core_driver * pudev)163 static uint16_t usbh_freepipe_get (usb_core_driver *pudev)
164 {
165     uint8_t pp_num = 0U;
166 
167     for (pp_num = 0U; pp_num < HC_MAX; pp_num++) {
168         if (0U == pudev->host.pipe[pp_num].in_used) {
169             return (uint16_t)pp_num;
170         }
171     }
172 
173     return HC_ERROR;
174 }
175