| 
									
										
										
										
											2020-03-22 17:19:57 +07:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  |  * The MIT License (MIT) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2019 Ha Thach (tinyusb.org) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
					
						
							|  |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
					
						
							|  |  |  |  * in the Software without restriction, including without limitation the rights | 
					
						
							|  |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
					
						
							|  |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
					
						
							|  |  |  |  * furnished to do so, subject to the following conditions: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The above copyright notice and this permission notice shall be included in | 
					
						
							|  |  |  |  * all copies or substantial portions of the Software. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
					
						
							|  |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
					
						
							|  |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
					
						
							|  |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
					
						
							|  |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
					
						
							|  |  |  |  * THE SOFTWARE. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of the TinyUSB stack. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "tusb_option.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if TUSB_OPT_DEVICE_ENABLED
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "tusb.h"
 | 
					
						
							|  |  |  | #include "device/usbd_pvt.h"
 | 
					
						
							|  |  |  | #include "dcd.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   EDPT_CTRL_OUT = 0x00, | 
					
						
							|  |  |  |   EDPT_CTRL_IN  = 0x80 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   tusb_control_request_t request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   uint8_t* buffer; | 
					
						
							|  |  |  |   uint16_t data_len; | 
					
						
							|  |  |  |   uint16_t total_xferred; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bool (*complete_cb) (uint8_t, tusb_control_request_t const *); | 
					
						
							|  |  |  | } usbd_control_xfer_t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static usbd_control_xfer_t _ctrl_xfer; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 16:43:44 +07:00
										 |  |  | CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN | 
					
						
							|  |  |  | static uint8_t _usbd_ctrl_buf[CFG_TUD_ENDPOINT0_SIZE]; | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // Application API
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-26 22:02:41 +07:00
										 |  |  | // Queue ZLP status transaction
 | 
					
						
							| 
									
										
										
										
											2019-11-04 11:21:57 +07:00
										 |  |  | static inline bool _status_stage_xact(uint8_t rhport, tusb_control_request_t const * request) | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-07 16:43:44 +07:00
										 |  |  |   // Opposite to endpoint in Data Phase
 | 
					
						
							|  |  |  |   uint8_t const ep_addr = request->bmRequestType_bit.direction ? EDPT_CTRL_OUT : EDPT_CTRL_IN; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-26 14:51:44 +07:00
										 |  |  |   TU_LOG2("  Queue EP %02X with zlp Status\r\n", ep_addr); | 
					
						
							| 
									
										
										
										
											2020-02-07 16:43:44 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  |   // status direction is reversed to one in the setup packet
 | 
					
						
							| 
									
										
										
										
											2020-02-07 16:43:44 +07:00
										 |  |  |   // Note: Status must always be DATA1
 | 
					
						
							|  |  |  |   return dcd_edpt_xfer(rhport, ep_addr, NULL, 0); | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-28 11:46:36 +07:00
										 |  |  | // Status phase
 | 
					
						
							| 
									
										
										
										
											2019-11-04 11:21:57 +07:00
										 |  |  | bool tud_control_status(uint8_t rhport, tusb_control_request_t const * request) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   _ctrl_xfer.request       = (*request); | 
					
						
							|  |  |  |   _ctrl_xfer.buffer        = NULL; | 
					
						
							|  |  |  |   _ctrl_xfer.total_xferred = 0; | 
					
						
							|  |  |  |   _ctrl_xfer.data_len      = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return _status_stage_xact(rhport, request); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-26 22:02:41 +07:00
										 |  |  | // Queue an transaction in Data Stage
 | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  | // Each transaction has up to Endpoint0's max packet size.
 | 
					
						
							|  |  |  | // This function can also transfer an zero-length packet
 | 
					
						
							|  |  |  | static bool _data_stage_xact(uint8_t rhport) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   uint16_t const xact_len = tu_min16(_ctrl_xfer.data_len - _ctrl_xfer.total_xferred, CFG_TUD_ENDPOINT0_SIZE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   uint8_t ep_addr = EDPT_CTRL_OUT; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ( _ctrl_xfer.request.bmRequestType_bit.direction == TUSB_DIR_IN ) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     ep_addr = EDPT_CTRL_IN; | 
					
						
							|  |  |  |     if ( xact_len ) memcpy(_usbd_ctrl_buf, _ctrl_xfer.buffer, xact_len); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-26 14:51:44 +07:00
										 |  |  |   TU_LOG2("  Queue EP %02X with %u bytes\r\n", ep_addr, xact_len); | 
					
						
							| 
									
										
										
										
											2020-03-08 14:20:28 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  |   return dcd_edpt_xfer(rhport, ep_addr, xact_len ? _usbd_ctrl_buf : NULL, xact_len); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:30:40 -04:00
										 |  |  | // Transmit data to/from the control endpoint.
 | 
					
						
							|  |  |  | // If the request's wLength is zero, a status packet is sent instead.
 | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  | bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const * request, void* buffer, uint16_t len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   _ctrl_xfer.request       = (*request); | 
					
						
							|  |  |  |   _ctrl_xfer.buffer        = (uint8_t*) buffer; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:30:40 -04:00
										 |  |  |   _ctrl_xfer.total_xferred = 0U; | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  |   _ctrl_xfer.data_len      = tu_min16(len, request->wLength); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:30:40 -04:00
										 |  |  |    | 
					
						
							|  |  |  |   if (request->wLength > 0U) | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:30:40 -04:00
										 |  |  |     if(_ctrl_xfer.data_len > 0U) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       TU_ASSERT(buffer); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-26 14:51:44 +07:00
										 |  |  | //    TU_LOG2("  Control total data length is %u bytes\r\n", _ctrl_xfer.data_len);
 | 
					
						
							| 
									
										
										
										
											2020-03-08 14:20:28 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  |     // Data stage
 | 
					
						
							|  |  |  |     TU_ASSERT( _data_stage_xact(rhport) ); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:30:40 -04:00
										 |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  |   { | 
					
						
							|  |  |  |     // Status stage
 | 
					
						
							| 
									
										
										
										
											2019-11-04 11:21:57 +07:00
										 |  |  |     TU_ASSERT( _status_stage_xact(rhport, request) ); | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // USBD API
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-28 13:39:29 +07:00
										 |  |  | void usbd_control_reset(void) | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  | { | 
					
						
							|  |  |  |   tu_varclr(&_ctrl_xfer); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TODO may find a better way
 | 
					
						
							|  |  |  | void usbd_control_set_complete_callback( bool (*fp) (uint8_t, tusb_control_request_t const * ) ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   _ctrl_xfer.complete_cb = fp; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-28 13:39:29 +07:00
										 |  |  | // useful for dcd_set_address where DCD is responsible for status response
 | 
					
						
							|  |  |  | void usbd_control_set_request(tusb_control_request_t const *request) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   _ctrl_xfer.request       = (*request); | 
					
						
							|  |  |  |   _ctrl_xfer.buffer        = NULL; | 
					
						
							|  |  |  |   _ctrl_xfer.total_xferred = 0; | 
					
						
							|  |  |  |   _ctrl_xfer.data_len      = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 12:12:52 +07:00
										 |  |  | // callback when a transaction complete on
 | 
					
						
							|  |  |  | // - DATA stage of control endpoint or
 | 
					
						
							|  |  |  | // - Status stage
 | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  | bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (void) result; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Endpoint Address is opposite to direction bit, this is Status Stage complete event
 | 
					
						
							|  |  |  |   if ( tu_edpt_dir(ep_addr) != _ctrl_xfer.request.bmRequestType_bit.direction ) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     TU_ASSERT(0 == xferred_bytes); | 
					
						
							| 
									
										
										
										
											2019-11-28 11:46:36 +07:00
										 |  |  |     if (dcd_edpt0_status_complete) dcd_edpt0_status_complete(rhport, &_ctrl_xfer.request); | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ( _ctrl_xfer.request.bmRequestType_bit.direction == TUSB_DIR_OUT ) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     TU_VERIFY(_ctrl_xfer.buffer); | 
					
						
							|  |  |  |     memcpy(_ctrl_xfer.buffer, _usbd_ctrl_buf, xferred_bytes); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   _ctrl_xfer.total_xferred += xferred_bytes; | 
					
						
							|  |  |  |   _ctrl_xfer.buffer += xferred_bytes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Data Stage is complete when all request's length are transferred or
 | 
					
						
							|  |  |  |   // a short packet is sent including zero-length packet.
 | 
					
						
							| 
									
										
										
										
											2020-04-26 22:02:41 +07:00
										 |  |  |   if ( (_ctrl_xfer.request.wLength == _ctrl_xfer.total_xferred) || (xferred_bytes < CFG_TUD_ENDPOINT0_SIZE) ) | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  |   { | 
					
						
							|  |  |  |     // DATA stage is complete
 | 
					
						
							|  |  |  |     bool is_ok = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // invoke complete callback if set
 | 
					
						
							|  |  |  |     // callback can still stall control in status phase e.g out data does not make sense
 | 
					
						
							|  |  |  |     if ( _ctrl_xfer.complete_cb ) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-04-26 22:02:41 +07:00
										 |  |  |       #if CFG_TUSB_DEBUG >= 2
 | 
					
						
							|  |  |  |       extern void usbd_driver_print_control_complete_name(bool (*control_complete) (uint8_t, tusb_control_request_t const *)); | 
					
						
							|  |  |  |       usbd_driver_print_control_complete_name(_ctrl_xfer.complete_cb); | 
					
						
							|  |  |  |       #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  |       is_ok = _ctrl_xfer.complete_cb(rhport, &_ctrl_xfer.request); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ( is_ok ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       // Send status
 | 
					
						
							| 
									
										
										
										
											2019-11-04 11:21:57 +07:00
										 |  |  |       TU_ASSERT( _status_stage_xact(rhport, &_ctrl_xfer.request) ); | 
					
						
							| 
									
										
										
										
											2019-11-01 10:16:59 +07:00
										 |  |  |     }else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       // Stall both IN and OUT control endpoint
 | 
					
						
							|  |  |  |       dcd_edpt_stall(rhport, EDPT_CTRL_OUT); | 
					
						
							|  |  |  |       dcd_edpt_stall(rhport, EDPT_CTRL_IN); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     // More data to transfer
 | 
					
						
							|  |  |  |     TU_ASSERT( _data_stage_xact(rhport) ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 |