Skip to content

Commit 096b6ec

Browse files
authored
Merge pull request hathach#1964 from hathach/add-tuh-set-interface
add tuh_set_interface
2 parents cbcf5d8 + ccf17e3 commit 096b6ec

File tree

7 files changed

+56
-19
lines changed

7 files changed

+56
-19
lines changed

examples/make.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ GCC_CFLAGS += \
150150
-Wnull-dereference \
151151
-Wuninitialized \
152152
-Wunused \
153+
-Wreturn-type \
153154
-Wredundant-decls
154155

155156
# conversion is too strict for most mcu driver, may be disable sign/int/arith-conversion

src/class/cdc/cdc_host.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static inline uint8_t get_idx_by_ep_addr(uint8_t daddr, uint8_t ep_addr)
9797
}
9898
}
9999

100-
return TU_INDEX_INVALID_8;
100+
return TUSB_INDEX_INVALID_8;
101101
}
102102

103103

@@ -124,7 +124,7 @@ uint8_t tuh_cdc_itf_get_index(uint8_t daddr, uint8_t itf_num)
124124
if (p_cdc->daddr == daddr && p_cdc->bInterfaceNumber == itf_num) return i;
125125
}
126126

127-
return TU_INDEX_INVALID_8;
127+
return TUSB_INDEX_INVALID_8;
128128
}
129129

130130
bool tuh_cdc_itf_get_info(uint8_t idx, tuh_cdc_itf_info_t* info)
@@ -533,7 +533,7 @@ static void process_cdc_config(tuh_xfer_t* xfer)
533533
uintptr_t const state = xfer->user_data;
534534
uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
535535
uint8_t const idx = tuh_cdc_itf_get_index(xfer->daddr, itf_num);
536-
TU_ASSERT(idx != TU_INDEX_INVALID_8, );
536+
TU_ASSERT(idx != TUSB_INDEX_INVALID_8, );
537537

538538
switch(state)
539539
{

src/class/cdc/cdc_host.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ typedef struct
8080
} tuh_cdc_itf_info_t;
8181

8282
// Get Interface index from device address + interface number
83-
// return TUSB_INDEX_INVALID (0xFF) if not found
83+
// return TUSB_INDEX_INVALID_8 (0xFF) if not found
8484
uint8_t tuh_cdc_itf_get_index(uint8_t daddr, uint8_t itf_num);
8585

8686
// Get Interface information

src/common/tusb_common.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,6 @@
7575

7676
#include "tusb_timeout.h" // TODO remove
7777

78-
enum
79-
{
80-
TU_INDEX_INVALID_8 = 0xFFu
81-
};
82-
83-
8478
//--------------------------------------------------------------------+
8579
// Optional API implemented by application if needed
8680
// TODO move to a more ovious place/file

src/common/tusb_types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@ enum
273273
CONTROL_STAGE_ACK
274274
};
275275

276+
enum
277+
{
278+
TUSB_INDEX_INVALID_8 = 0xFFu
279+
};
280+
276281
//--------------------------------------------------------------------+
277282
// USB Descriptors
278283
//--------------------------------------------------------------------+

src/host/usbh.c

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ enum { CONFIG_NUM = 1 }; // default to use configuration 1
195195
// sum of end device + hub
196196
#define TOTAL_DEVICES (CFG_TUH_DEVICE_MAX + CFG_TUH_HUB)
197197

198-
static uint8_t _usbh_controller = TU_INDEX_INVALID_8;
198+
static uint8_t _usbh_controller = TUSB_INDEX_INVALID_8;
199199

200200
// Device with address = 0 for enumeration
201201
static usbh_dev0_t _dev0;
@@ -303,13 +303,13 @@ tusb_speed_t tuh_speed_get (uint8_t dev_addr)
303303
static void clear_device(usbh_device_t* dev)
304304
{
305305
tu_memclr(dev, sizeof(usbh_device_t));
306-
memset(dev->itf2drv, TU_INDEX_INVALID_8, sizeof(dev->itf2drv)); // invalid mapping
307-
memset(dev->ep2drv , TU_INDEX_INVALID_8, sizeof(dev->ep2drv )); // invalid mapping
306+
memset(dev->itf2drv, TUSB_INDEX_INVALID_8, sizeof(dev->itf2drv)); // invalid mapping
307+
memset(dev->ep2drv , TUSB_INDEX_INVALID_8, sizeof(dev->ep2drv )); // invalid mapping
308308
}
309309

310310
bool tuh_inited(void)
311311
{
312-
return _usbh_controller != TU_INDEX_INVALID_8;
312+
return _usbh_controller != TUSB_INDEX_INVALID_8;
313313
}
314314

315315
bool tuh_init(uint8_t controller_id)
@@ -1033,6 +1033,38 @@ bool tuh_configuration_set(uint8_t daddr, uint8_t config_num,
10331033
return tuh_control_xfer(&xfer);
10341034
}
10351035

1036+
bool tuh_interface_set(uint8_t daddr, uint8_t itf_num, uint8_t itf_alt,
1037+
tuh_xfer_cb_t complete_cb, uintptr_t user_data)
1038+
{
1039+
TU_LOG_USBH("Set Interface %u Alternate %u\r\n", itf_num, itf_alt);
1040+
1041+
tusb_control_request_t const request =
1042+
{
1043+
.bmRequestType_bit =
1044+
{
1045+
.recipient = TUSB_REQ_RCPT_DEVICE,
1046+
.type = TUSB_REQ_TYPE_STANDARD,
1047+
.direction = TUSB_DIR_OUT
1048+
},
1049+
.bRequest = TUSB_REQ_SET_INTERFACE,
1050+
.wValue = tu_htole16(itf_alt),
1051+
.wIndex = tu_htole16(itf_num),
1052+
.wLength = 0
1053+
};
1054+
1055+
tuh_xfer_t xfer =
1056+
{
1057+
.daddr = daddr,
1058+
.ep_addr = 0,
1059+
.setup = &request,
1060+
.buffer = NULL,
1061+
.complete_cb = complete_cb,
1062+
.user_data = user_data
1063+
};
1064+
1065+
return tuh_control_xfer(&xfer);
1066+
}
1067+
10361068
//--------------------------------------------------------------------+
10371069
// Descriptor Sync
10381070
//--------------------------------------------------------------------+
@@ -1358,11 +1390,11 @@ static void process_enumeration(tuh_xfer_t* xfer)
13581390

13591391
dev->configured = 1;
13601392

1361-
// Start the Set Configuration process for interfaces (itf = TU_INDEX_INVALID_8)
1393+
// Start the Set Configuration process for interfaces (itf = TUSB_INDEX_INVALID_8)
13621394
// Since driver can perform control transfer within its set_config, this is done asynchronously.
13631395
// The process continue with next interface when class driver complete its sequence with usbh_driver_set_config_complete()
1364-
// TODO use separated API instead of using TU_INDEX_INVALID_8
1365-
usbh_driver_set_config_complete(daddr, TU_INDEX_INVALID_8);
1396+
// TODO use separated API instead of using TUSB_INDEX_INVALID_8
1397+
usbh_driver_set_config_complete(daddr, TUSB_INDEX_INVALID_8);
13661398
}
13671399
break;
13681400

@@ -1562,7 +1594,7 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur
15621594
uint8_t const itf_num = desc_itf->bInterfaceNumber+i;
15631595

15641596
// Interface number must not be used already
1565-
TU_ASSERT( TU_INDEX_INVALID_8 == dev->itf2drv[itf_num] );
1597+
TU_ASSERT( TUSB_INDEX_INVALID_8 == dev->itf2drv[itf_num] );
15661598
dev->itf2drv[itf_num] = drv_id;
15671599
}
15681600

@@ -1596,7 +1628,7 @@ void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num)
15961628
// IAD binding interface such as CDCs should return itf_num + 1 when complete
15971629
// with usbh_driver_set_config_complete()
15981630
uint8_t const drv_id = dev->itf2drv[itf_num];
1599-
if (drv_id != TU_INDEX_INVALID_8)
1631+
if (drv_id != TUSB_INDEX_INVALID_8)
16001632
{
16011633
usbh_class_driver_t const * driver = &usbh_class_drivers[drv_id];
16021634
TU_LOG_USBH("%s set config: itf = %u\r\n", driver->name, itf_num);

src/host/usbh.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ bool tuh_edpt_open(uint8_t dev_addr, tusb_desc_endpoint_t const * desc_ep);
171171
bool tuh_configuration_set(uint8_t daddr, uint8_t config_num,
172172
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
173173

174+
// Set Interface (control transfer)
175+
// true on success, false if there is on-going control transfer or incorrect parameters
176+
bool tuh_interface_set(uint8_t daddr, uint8_t itf_num, uint8_t itf_alt,
177+
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
178+
174179
//--------------------------------------------------------------------+
175180
// Descriptors Asynchronous (non-blocking)
176181
//--------------------------------------------------------------------+

0 commit comments

Comments
 (0)