Open FFBoard
Open source force feedback firmware
vendor_host.c
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 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 (CFG_TUH_ENABLED && CFG_TUH_VENDOR)
30
31//--------------------------------------------------------------------+
32// INCLUDE
33//--------------------------------------------------------------------+
34#include "host/usbh.h"
35#include "vendor_host.h"
36
37//--------------------------------------------------------------------+
38// MACRO CONSTANT TYPEDEF
39//--------------------------------------------------------------------+
40
41//--------------------------------------------------------------------+
42// INTERNAL OBJECT & FUNCTION DECLARATION
43//--------------------------------------------------------------------+
44custom_interface_info_t custom_interface[CFG_TUH_DEVICE_MAX];
45
46static tusb_error_t cush_validate_paras(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void * p_buffer, uint16_t length)
47{
48 if ( !tusbh_custom_is_mounted(dev_addr, vendor_id, product_id) )
49 {
50 return TUSB_ERROR_DEVICE_NOT_READY;
51 }
52
53 TU_ASSERT( p_buffer != NULL && length != 0, TUSB_ERROR_INVALID_PARA);
54
55 return TUSB_ERROR_NONE;
56}
57//--------------------------------------------------------------------+
58// APPLICATION API (need to check parameters)
59//--------------------------------------------------------------------+
60tusb_error_t tusbh_custom_read(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void * p_buffer, uint16_t length)
61{
62 TU_ASSERT_ERR( cush_validate_paras(dev_addr, vendor_id, product_id, p_buffer, length) );
63
64 if ( !hcd_pipe_is_idle(custom_interface[dev_addr-1].pipe_in) )
65 {
66 return TUSB_ERROR_INTERFACE_IS_BUSY;
67 }
68
69 (void) usbh_edpt_xfer( custom_interface[dev_addr-1].pipe_in, p_buffer, length);
70
71 return TUSB_ERROR_NONE;
72}
73
74tusb_error_t tusbh_custom_write(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void const * p_data, uint16_t length)
75{
76 TU_ASSERT_ERR( cush_validate_paras(dev_addr, vendor_id, product_id, p_data, length) );
77
78 if ( !hcd_pipe_is_idle(custom_interface[dev_addr-1].pipe_out) )
79 {
80 return TUSB_ERROR_INTERFACE_IS_BUSY;
81 }
82
83 (void) usbh_edpt_xfer( custom_interface[dev_addr-1].pipe_out, p_data, length);
84
85 return TUSB_ERROR_NONE;
86}
87
88//--------------------------------------------------------------------+
89// USBH-CLASS API
90//--------------------------------------------------------------------+
91void cush_init(void)
92{
93 tu_memclr(&custom_interface, sizeof(custom_interface_info_t) * CFG_TUH_DEVICE_MAX);
94}
95
96tusb_error_t cush_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length)
97{
98 // FIXME quick hack to test lpc1k custom class with 2 bulk endpoints
99 uint8_t const *p_desc = (uint8_t const *) p_interface_desc;
100 p_desc = tu_desc_next(p_desc);
101
102 //------------- Bulk Endpoints Descriptor -------------//
103 for(uint32_t i=0; i<2; i++)
104 {
105 tusb_desc_endpoint_t const *p_endpoint = (tusb_desc_endpoint_t const *) p_desc;
106 TU_ASSERT(TUSB_DESC_ENDPOINT == p_endpoint->bDescriptorType, TUSB_ERROR_INVALID_PARA);
107
108 pipe_handle_t * p_pipe_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_IN_MASK ) ?
109 &custom_interface[dev_addr-1].pipe_in : &custom_interface[dev_addr-1].pipe_out;
110 *p_pipe_hdl = usbh_edpt_open(dev_addr, p_endpoint, TUSB_CLASS_VENDOR_SPECIFIC);
111 TU_ASSERT ( pipehandle_is_valid(*p_pipe_hdl), TUSB_ERROR_HCD_OPEN_PIPE_FAILED );
112
113 p_desc = tu_desc_next(p_desc);
114 }
115
116 (*p_length) = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t);
117 return TUSB_ERROR_NONE;
118}
119
120void cush_isr(pipe_handle_t pipe_hdl, xfer_result_t event)
121{
122
123}
124
125void cush_close(uint8_t dev_addr)
126{
127 tusb_error_t err1, err2;
128 custom_interface_info_t * p_interface = &custom_interface[dev_addr-1];
129
130 // TODO re-consider to check pipe valid before calling pipe_close
131 if( pipehandle_is_valid( p_interface->pipe_in ) )
132 {
133 err1 = hcd_pipe_close( p_interface->pipe_in );
134 }
135
136 if ( pipehandle_is_valid( p_interface->pipe_out ) )
137 {
138 err2 = hcd_pipe_close( p_interface->pipe_out );
139 }
140
141 tu_memclr(p_interface, sizeof(custom_interface_info_t));
142
143 TU_ASSERT(err1 == TUSB_ERROR_NONE && err2 == TUSB_ERROR_NONE, (void) 0 );
144}
145
146#endif
uint8_t dev_addr
Definition: dcd_pic32mz.c:81
AUDIO Channel Cluster Descriptor (4.1)
Definition: audio.h:647
uint8_t bDescriptorType
Descriptor Type. Value: TUSB_DESC_CS_INTERFACE.
Definition: audio.h:657
uint8_t bEndpointAddress
Definition: video.h:306
@ TUSB_DIR_IN_MASK
Definition: tusb_types.h:69
@ TUSB_CLASS_VENDOR_SPECIFIC
Definition: tusb_types.h:181
xfer_result_t
Definition: tusb_types.h:236
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_ENDPOINT
Definition: tusb_types.h:97
static TU_ATTR_ALWAYS_INLINE bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t *buffer, uint16_t total_bytes)
Definition: usbh_pvt.h:87
tusb_error_t tusbh_custom_write(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void const *p_data, uint16_t length)
Definition: vendor_host.c:74
void cush_init(void)
Definition: vendor_host.c:91
void cush_isr(pipe_handle_t pipe_hdl, xfer_result_t event)
Definition: vendor_host.c:120
static tusb_error_t cush_validate_paras(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void *p_buffer, uint16_t length)
Definition: vendor_host.c:46
tusb_error_t cush_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length)
Definition: vendor_host.c:96
custom_interface_info_t custom_interface[CFG_TUH_DEVICE_MAX]
Definition: vendor_host.c:44
tusb_error_t tusbh_custom_read(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void *p_buffer, uint16_t length)
Definition: vendor_host.c:60
void cush_close(uint8_t dev_addr)
Definition: vendor_host.c:125
static bool tusbh_custom_is_mounted(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id)
Definition: vendor_host.h:44