Open FFBoard
Open source force feedback firmware
dfu_rt_device.c
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Sylvain Munaut <tnt@246tNt.com>
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 (CFG_TUD_ENABLED && CFG_TUD_DFU_RUNTIME)
30
31#include "device/usbd.h"
32#include "device/usbd_pvt.h"
33
34#include "dfu_rt_device.h"
35
36//--------------------------------------------------------------------+
37// MACRO CONSTANT TYPEDEF
38//--------------------------------------------------------------------+
39
40// Level where CFG_TUSB_DEBUG must be at least for this driver is logged
41#ifndef CFG_TUD_DFU_RUNTIME_LOG_LEVEL
42 #define CFG_TUD_DFU_RUNTIME_LOG_LEVEL CFG_TUD_LOG_LEVEL
43#endif
44
45#define TU_LOG_DRV(...) TU_LOG(CFG_TUD_DFU_RUNTIME_LOG_LEVEL, __VA_ARGS__)
46
47//--------------------------------------------------------------------+
48// INTERNAL OBJECT & FUNCTION DECLARATION
49//--------------------------------------------------------------------+
50
51//--------------------------------------------------------------------+
52// USBD Driver API
53//--------------------------------------------------------------------+
54void dfu_rtd_init(void) {
55}
56
57bool dfu_rtd_deinit(void) {
58 return true;
59}
60
61void dfu_rtd_reset(uint8_t rhport)
62{
63 (void) rhport;
64}
65
66uint16_t dfu_rtd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
67{
68 (void) rhport;
69 (void) max_len;
70
71 // Ensure this is DFU Runtime
72 TU_VERIFY((itf_desc->bInterfaceSubClass == TUD_DFU_APP_SUBCLASS) &&
73 (itf_desc->bInterfaceProtocol == DFU_PROTOCOL_RT), 0);
74
75 uint8_t const * p_desc = tu_desc_next( itf_desc );
76 uint16_t drv_len = sizeof(tusb_desc_interface_t);
77
78 if ( TUSB_DESC_FUNCTIONAL == tu_desc_type(p_desc) )
79 {
80 drv_len += tu_desc_len(p_desc);
81 p_desc = tu_desc_next(p_desc);
82 }
83
84 return drv_len;
85}
86
87// Invoked when a control transfer occurred on an interface of this class
88// Driver response accordingly to the request and the transfer stage (setup/data/ack)
89// return false to stall control endpoint (e.g unsupported request)
90bool dfu_rtd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
91{
92 // nothing to do with DATA or ACK stage
93 if ( stage != CONTROL_STAGE_SETUP ) return true;
94
95 TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE);
96
97 // dfu-util will try to claim the interface with SET_INTERFACE request before sending DFU request
100 {
102 return true;
103 }
104
105 // Handle class request only from here
107
108 switch (request->bRequest)
109 {
111 {
112 TU_LOG_DRV(" DFU RT Request: DETACH\r\n");
114 tud_dfu_runtime_reboot_to_dfu_cb();
115 }
116 break;
117
119 {
120 TU_LOG_DRV(" DFU RT Request: GETSTATUS\r\n");
122 // Status = OK, Poll timeout is ignored during RT, State = APP_IDLE, IString = 0
123 TU_VERIFY(tu_memset_s(&resp, sizeof(resp), 0x00, sizeof(resp))==0);
124 tud_control_xfer(rhport, request, &resp, sizeof(dfu_status_response_t));
125 }
126 break;
127
128 default:
129 {
130 TU_LOG_DRV(" DFU RT Unexpected Request: %d\r\n", request->bRequest);
131 return false; // stall unsupported request
132 }
133 }
134
135 return true;
136}
137
138#endif
@ DFU_REQUEST_GETSTATUS
Definition: dfu.h:58
@ DFU_REQUEST_DETACH
Definition: dfu.h:55
void dfu_rtd_init(void)
Definition: dfu_rt_device.c:54
uint16_t dfu_rtd_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len)
Definition: dfu_rt_device.c:66
bool dfu_rtd_deinit(void)
Definition: dfu_rt_device.c:57
bool dfu_rtd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const *request)
Definition: dfu_rt_device.c:90
void dfu_rtd_reset(uint8_t rhport)
Definition: dfu_rt_device.c:61
AUDIO Channel Cluster Descriptor (4.1)
Definition: audio.h:647
struct TU_ATTR_PACKED::@16::TU_ATTR_PACKED bmRequestType_bit
uint8_t bInterfaceSubClass
Subclass code (assigned by the USB-IF). These codes are qualified by the value of the bInterfaceCla...
Definition: tusb_types.h:350
uint8_t bRequest
Request type audio_cs_req_t.
Definition: audio.h:831
uint8_t bInterfaceProtocol
Protocol code (assigned by the USB). These codes are qualified by the value of the bInterfaceClass ...
Definition: tusb_types.h:351
static TU_ATTR_ALWAYS_INLINE int tu_memset_s(void *dest, size_t destsz, int ch, size_t count)
Definition: tusb_common.h:104
@ TUSB_REQ_SET_INTERFACE
Definition: tusb_types.h:133
static TU_ATTR_ALWAYS_INLINE uint8_t tu_desc_len(void const *desc)
Definition: tusb_types.h:542
@ TUSB_REQ_RCPT_INTERFACE
Definition: tusb_types.h:152
@ CONTROL_STAGE_SETUP
Definition: tusb_types.h:268
static TU_ATTR_ALWAYS_INLINE uint8_t tu_desc_type(void const *desc)
Definition: tusb_types.h:537
@ TUSB_REQ_TYPE_STANDARD
Definition: tusb_types.h:144
@ TUSB_REQ_TYPE_CLASS
Definition: tusb_types.h:145
struct TU_ATTR_PACKED tusb_desc_interface_t
USB Interface Descriptor.
static TU_ATTR_ALWAYS_INLINE uint8_t const * tu_desc_next(void const *desc)
Definition: tusb_types.h:531
@ TUSB_DESC_FUNCTIONAL
Definition: tusb_types.h:108
bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const *request, void *buffer, uint16_t len)
Definition: usbd_control.c:111
bool tud_control_status(uint8_t rhport, tusb_control_request_t const *request)
Definition: usbd_control.c:81
CFG_TUH_MEM_ALIGN tusb_control_request_t request
Definition: usbh.c:259
volatile uint8_t stage
Definition: usbh.c:265