blob: 9204576ac6021b1816a161afe67e1687f19d958c [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2020, Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * This file is part of the TinyUSB stack.
25 */
26
27#include "tusb_option.h"
28
29#if TUSB_OPT_HOST_ENABLED
30
31#include "tusb.h"
32#include "usbh_classdriver.h"
33
34enum
35{
36 STAGE_SETUP,
37 STAGE_DATA,
38 STAGE_ACK
39};
40
41typedef struct
42{
43 tusb_control_request_t request TU_ATTR_ALIGNED(4);
44
45 uint8_t stage;
46 uint8_t* buffer;
47 tuh_control_complete_cb_t complete_cb;
48} usbh_control_xfer_t;
49
50static usbh_control_xfer_t _ctrl_xfer;
51
52//CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN
53//static uint8_t _tuh_ctrl_buf[CFG_TUH_ENUMERATION_BUFSIZE];
54
55//--------------------------------------------------------------------+
56// MACRO TYPEDEF CONSTANT ENUM DECLARATION
57//--------------------------------------------------------------------+
58
59bool tuh_control_xfer (uint8_t dev_addr, tusb_control_request_t const* request, void* buffer, tuh_control_complete_cb_t complete_cb)
60{
61 // TODO need to claim the endpoint first
62 const uint8_t rhport = usbh_get_rhport(dev_addr);
63
64 _ctrl_xfer.request = (*request);
65 _ctrl_xfer.buffer = buffer;
66 _ctrl_xfer.stage = STAGE_SETUP;
67 _ctrl_xfer.complete_cb = complete_cb;
68
69 TU_LOG2("Control Setup (addr = %u): ", dev_addr);
70 TU_LOG2_VAR(request);
71 TU_LOG2("\r\n");
72
73 // Send setup packet
74 TU_ASSERT( hcd_setup_send(rhport, dev_addr, (uint8_t const*) &_ctrl_xfer.request) );
75
76 return true;
77}
78
79static void _xfer_complete(uint8_t dev_addr, xfer_result_t result)
80{
81 TU_LOG2("\r\n");
82 if (_ctrl_xfer.complete_cb) _ctrl_xfer.complete_cb(dev_addr, &_ctrl_xfer.request, result);
83}
84
85bool usbh_control_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
86{
87 (void) ep_addr;
88 (void) xferred_bytes;
89
90 const uint8_t rhport = usbh_get_rhport(dev_addr);
91
92 tusb_control_request_t const * request = &_ctrl_xfer.request;
93
94 if (XFER_RESULT_SUCCESS != result)
95 {
96 TU_LOG2("Control failed: result = %d\r\n", result);
97
98 // terminate transfer if any stage failed
99 _xfer_complete(dev_addr, result);
100 }else
101 {
102 switch(_ctrl_xfer.stage)
103 {
104 case STAGE_SETUP:
105 _ctrl_xfer.stage = STAGE_DATA;
106 if (request->wLength)
107 {
108 // DATA stage: initial data toggle is always 1
109 hcd_edpt_xfer(rhport, dev_addr, tu_edpt_addr(0, request->bmRequestType_bit.direction), _ctrl_xfer.buffer, request->wLength);
110 return true;
111 }
112 __attribute__((fallthrough));
113
114 case STAGE_DATA:
115 _ctrl_xfer.stage = STAGE_ACK;
116
117 if (request->wLength)
118 {
119 TU_LOG2("Control data (addr = %u):\r\n", dev_addr);
120 TU_LOG2_MEM(_ctrl_xfer.buffer, request->wLength, 2);
121 }
122
123 // ACK stage: toggle is always 1
124 hcd_edpt_xfer(rhport, dev_addr, tu_edpt_addr(0, 1-request->bmRequestType_bit.direction), NULL, 0);
125 break;
126
127 case STAGE_ACK:
128 _xfer_complete(dev_addr, result);
129 break;
130
131 default: return false;
132 }
133 }
134
135 return true;
136}
137
138#endif