From d629327ae42999d1900d460338999c63c9328f25 Mon Sep 17 00:00:00 2001 From: Robert Fisk Date: Tue, 20 Jun 2017 13:03:44 +1200 Subject: [PATCH] Implemented optional build for mass storage, keyboard, and mouse (Upstream). Also implemented read-only mass storage mode. Todo: - Test. Everything!!! - Report write protect flag in SCSI "mode parameter header" (whatever that is) --- Upstream/Inc/options.h | 24 +++ .../Class/HID/Src/usbd_hid.c | 29 ++- .../Class/MSC/Src/usbd_msc.c | 6 + .../Class/MSC/Src/usbd_msc_bot.c | 6 + .../Class/MSC/Src/usbd_msc_data.c | 6 + .../Class/MSC/Src/usbd_msc_scsi.c | 17 +- .../Class/MSC/Src/usbd_msc_storage_template.c | 188 ------------------ Upstream/Src/upstream_hid.c | 24 ++- Upstream/Src/upstream_msc.c | 9 +- Upstream/Src/upstream_statemachine.c | 10 +- 10 files changed, 115 insertions(+), 204 deletions(-) create mode 100644 Upstream/Inc/options.h delete mode 100755 Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_storage_template.c diff --git a/Upstream/Inc/options.h b/Upstream/Inc/options.h new file mode 100644 index 0000000..325fb7e --- /dev/null +++ b/Upstream/Inc/options.h @@ -0,0 +1,24 @@ +/* + * options.h + * + * Created on: Jun 20, 2017 + * Author: Robert Fisk + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#ifndef INC_OPTIONS_H_ +#define INC_OPTIONS_H_ + + +#define ENABLE_MASS_STORAGE +#define ENABLE_KEYBOARD +#define ENABLE_MOUSE + +#define MASS_STORAGE_WRITES_PERMITTED + + + +#endif /* INC_OPTIONS_H_ */ diff --git a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c index 9f49a6c..29de652 100644 --- a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c +++ b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c @@ -52,8 +52,11 @@ #include "usbd_hid.h" #include "usbd_ctlreq.h" #include "upstream_hid.h" +#include "options.h" +#if defined (ENABLE_KEYBOARD) || defined (ENABLE_MOUSE) + static uint8_t USBD_HID_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx); @@ -76,9 +79,12 @@ static uint8_t USBD_HID_SendReport (uint8_t *report, static uint8_t USBD_HID_EP0RxReady(USBD_HandleTypeDef *pdev); - -#define USBD_PID_MOUSE 0x0002 -#define USBD_PID_KEYBOARD 0x0003 +#ifdef ENABLE_MOUSE + #define USBD_PID_MOUSE 0x0002 +#endif +#ifdef ENABLE_KEYBOARD + #define USBD_PID_KEYBOARD 0x0003 +#endif USBD_ClassTypeDef USBD_HID = @@ -188,6 +194,7 @@ __ALIGN_BEGIN static uint8_t USBD_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_ 0x00, }; +#ifdef ENABLE_MOUSE __ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END = { 0x05, 0x01, // Usage Page (Generic Desktop Ctrls) @@ -230,8 +237,9 @@ __ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] _ 0xC0, // End Collection // 74 bytes }; +#endif - +#ifdef ENABLE_KEYBOARD __ALIGN_BEGIN static uint8_t HID_KEYBOARD_ReportDesc[HID_KEYBOARD_REPORT_DESC_SIZE] __ALIGN_END = { 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x06, // USAGE (Keyboard) @@ -266,7 +274,7 @@ __ALIGN_BEGIN static uint8_t HID_KEYBOARD_ReportDesc[HID_KEYBOARD_REPORT_DESC_SI 0x81, 0x00, // INPUT (Data,Ary,Abs) 0xc0 // END_COLLECTION }; - +#endif @@ -293,6 +301,7 @@ uint8_t OutReportSize; * @param cfgidx: Configuration index * @retval status */ +#ifdef ENABLE_MOUSE void USBD_HID_PreinitMouse(void) { ActiveReportDescriptor = HID_MOUSE_ReportDesc; @@ -306,8 +315,10 @@ void USBD_HID_PreinitMouse(void) USBD_HID_CfgDesc[USB_HID_CFGDESC__EPIN_SIZE_OFFSET] = HID_MOUSE_INPUT_DATA_LEN; USBD_HID_Desc[USB_HID_DESC__HID_REPORT_DESC_SIZE_OFFSET] = HID_MOUSE_REPORT_DESC_SIZE; } +#endif +#ifdef ENABLE_KEYBOARD void USBD_HID_PreinitKeyboard(void) { ActiveReportDescriptor = HID_KEYBOARD_ReportDesc; @@ -321,7 +332,7 @@ void USBD_HID_PreinitKeyboard(void) USBD_HID_CfgDesc[USB_HID_CFGDESC__EPIN_SIZE_OFFSET] = HID_KEYBOARD_INPUT_DATA_LEN; USBD_HID_Desc[USB_HID_DESC__HID_REPORT_DESC_SIZE_OFFSET] = HID_KEYBOARD_REPORT_DESC_SIZE; } - +#endif static uint8_t USBD_HID_Init (USBD_HandleTypeDef *pdev, @@ -598,18 +609,24 @@ static uint8_t *USBD_HID_GetDeviceQualifierDesc (uint16_t *length) //Upstream_HID will send it after the next IN interrupt transfer static uint8_t USBD_HID_EP0RxReady(USBD_HandleTypeDef *pdev) { + UNUSED(pdev); + if ((OutReportPacket == NULL) || (OutReportSize == 0)) { while(1); } +#ifdef ENABLE_KEYBOARD Upstream_HID_RequestSendControlReport(OutReportPacket, OutReportSize); Upstream_ReleasePacket(OutReportPacket); OutReportPacket = NULL; +#endif return USBD_OK; } +#endif //#if defined (ENABLE_KEYBOARD) || defined (ENABLE_MOUSE) + /** * @} */ diff --git a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc.c b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc.c index 09efdb9..291504d 100755 --- a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc.c +++ b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc.c @@ -46,6 +46,10 @@ #include #include #include "usbd_msc.h" +#include "options.h" + + +#ifdef ENABLE_MASS_STORAGE /** @addtogroup STM32_USB_DEVICE_LIBRARY * @{ @@ -605,6 +609,8 @@ uint8_t *USBD_MSC_GetDeviceQualifierDescriptor (uint16_t *length) return USBD_MSC_DeviceQualifierDesc; } +#endif //#ifdef ENABLE_MASS_STORAGE + /** * @} diff --git a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_bot.c b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_bot.c index 5d6fb94..a0a16e3 100755 --- a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_bot.c +++ b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_bot.c @@ -32,6 +32,10 @@ #include "usbd_msc.h" #include "usbd_msc_scsi.h" #include "usbd_ioreq.h" +#include "options.h" + + +#ifdef ENABLE_MASS_STORAGE /** @addtogroup STM32_USB_DEVICE_LIBRARY * @{ @@ -423,6 +427,8 @@ void MSC_BOT_CplClrFeature (USBD_HandleTypeDef *pdev, uint8_t epnum) } } + +#endif //#ifdef ENABLE_MASS_STORAGE /** * @} */ diff --git a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_data.c b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_data.c index f087f7e..826c69c 100755 --- a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_data.c +++ b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_data.c @@ -27,8 +27,11 @@ /* Includes ------------------------------------------------------------------*/ #include "usbd_msc_data.h" +#include "options.h" +#ifdef ENABLE_MASS_STORAGE + /** @addtogroup STM32_USB_DEVICE_LIBRARY * @{ */ @@ -100,6 +103,9 @@ const uint8_t MSC_Mode_Sense10_data[] = { 0x00, 0x00 }; + +#endif //ifdef ENABLE_MASS_STORAGE + /** * @} */ diff --git a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_scsi.c b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_scsi.c index c809832..228aa83 100755 --- a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_scsi.c +++ b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_scsi.c @@ -36,8 +36,11 @@ #include "usbd_msc.h" #include "usbd_msc_data.h" #include "usbd_descriptors.h" +#include "options.h" +#ifdef ENABLE_MASS_STORAGE + /** @addtogroup STM32_USB_DEVICE_LIBRARY * @{ */ @@ -192,7 +195,15 @@ void SCSI_ProcessCmd(USBD_HandleTypeDef *pdev, return; case SCSI_WRITE10: - SCSI_Write10(); +#ifdef MASS_STORAGE_WRITES_PERMITTED + SCSI_Write10(); +#else + SCSI_SenseCode(pdev, + lun, + DATA_PROTECT, + WRITE_PROTECTED); + SCSI_ProcessCmd_callback(-1); +#endif return; case SCSI_VERIFY10: @@ -670,6 +681,7 @@ void SCSI_Read10ReplyCallback(UpstreamPacketTypeDef* upstreamPacket, * @param params: Command parameters * @retval status */ +#ifdef MASS_STORAGE_WRITES_PERMITTED static void SCSI_Write10(void) { uint32_t dataLength; @@ -804,7 +816,7 @@ void SCSI_Write10FreePacketCallback(UpstreamPacketTypeDef* freePacket) MIN(SCSI_ProcessCmd_hmsc->csw.dDataResidue, MSC_MEDIA_PACKET)); SCSI_ProcessCmd_callback(0); //Report eventual success! } - +#endif /** * @brief SCSI_Verify10 @@ -858,6 +870,7 @@ static int8_t SCSI_CheckAddressRange (uint32_t blk_offset , uint16_t blk_nbr) return 0; } +#endif //#ifdef ENABLE_MASS_STORAGE /** * @} */ diff --git a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_storage_template.c b/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_storage_template.c deleted file mode 100755 index 19f3d8d..0000000 --- a/Upstream/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_storage_template.c +++ /dev/null @@ -1,188 +0,0 @@ -/** - ****************************************************************************** - * @file usbd_msc_storage_template.c - * @author MCD Application Team - * @version V2.3.0 - * @date 04-November-2014 - * @brief Memory management layer - ****************************************************************************** - * @attention - * - *

© COPYRIGHT 2014 STMicroelectronics

- * - * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); - * You may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.st.com/software_license_agreement_liberty_v2 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ****************************************************************************** - */ - - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_msc_storage_template.h" - - -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -/* Private function prototypes -----------------------------------------------*/ -/* Extern function prototypes ------------------------------------------------*/ -/* Private functions ---------------------------------------------------------*/ - -#define STORAGE_LUN_NBR 1 -#define STORAGE_BLK_NBR 0x10000 -#define STORAGE_BLK_SIZ 0x200 - -int8_t STORAGE_Init (uint8_t lun); - -int8_t STORAGE_GetCapacity (uint8_t lun, - uint32_t *block_num, - uint16_t *block_size); - -int8_t STORAGE_IsReady (uint8_t lun); - -int8_t STORAGE_IsWriteProtected (uint8_t lun); - -int8_t STORAGE_Read (uint8_t lun, - uint8_t *buf, - uint32_t blk_addr, - uint16_t blk_len); - -int8_t STORAGE_Write (uint8_t lun, - uint8_t *buf, - uint32_t blk_addr, - uint16_t blk_len); - -int8_t STORAGE_GetMaxLun (void); - -/* USB Mass storage Standard Inquiry Data */ -int8_t STORAGE_Inquirydata[] = {//36 - - /* LUN 0 */ - 0x00, - 0x80, - 0x02, - 0x02, - (STANDARD_INQUIRY_DATA_LEN - 5), - 0x00, - 0x00, - 0x00, - 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */ - 'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product : 16 Bytes */ - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - '0', '.', '0' ,'1', /* Version : 4 Bytes */ -}; - -USBD_StorageTypeDef USBD_MSC_Template_fops = -{ - STORAGE_Init, - STORAGE_GetCapacity, - STORAGE_IsReady, - STORAGE_IsWriteProtected, - STORAGE_Read, - STORAGE_Write, - STORAGE_GetMaxLun, - STORAGE_Inquirydata, - -}; -/******************************************************************************* -* Function Name : Read_Memory -* Description : Handle the Read operation from the microSD card. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -int8_t STORAGE_Init (uint8_t lun) -{ - return (0); -} - -/******************************************************************************* -* Function Name : Read_Memory -* Description : Handle the Read operation from the STORAGE card. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint16_t *block_size) -{ - *block_num = STORAGE_BLK_NBR; - *block_size = STORAGE_BLK_SIZ; - return (0); -} - -/******************************************************************************* -* Function Name : Read_Memory -* Description : Handle the Read operation from the STORAGE card. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -int8_t STORAGE_IsReady (uint8_t lun) -{ - return (0); -} - -/******************************************************************************* -* Function Name : Read_Memory -* Description : Handle the Read operation from the STORAGE card. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -int8_t STORAGE_IsWriteProtected (uint8_t lun) -{ - return 0; -} - -/******************************************************************************* -* Function Name : Read_Memory -* Description : Handle the Read operation from the STORAGE card. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -int8_t STORAGE_Read (uint8_t lun, - uint8_t *buf, - uint32_t blk_addr, - uint16_t blk_len) -{ - return 0; -} -/******************************************************************************* -* Function Name : Write_Memory -* Description : Handle the Write operation to the STORAGE card. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -int8_t STORAGE_Write (uint8_t lun, - uint8_t *buf, - uint32_t blk_addr, - uint16_t blk_len) -{ - return (0); -} -/******************************************************************************* -* Function Name : Write_Memory -* Description : Handle the Write operation to the STORAGE card. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -int8_t STORAGE_GetMaxLun (void) -{ - return (STORAGE_LUN_NBR - 1); -} - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - diff --git a/Upstream/Src/upstream_hid.c b/Upstream/Src/upstream_hid.c index 0784cae..0dabf11 100644 --- a/Upstream/Src/upstream_hid.c +++ b/Upstream/Src/upstream_hid.c @@ -13,14 +13,19 @@ #include "upstream_hid.h" #include "upstream_interface_def.h" +#include "options.h" +#if defined (ENABLE_KEYBOARD) || defined (ENABLE_MOUSE) + UpstreamPacketTypeDef* UpstreamHidPacket = NULL; UpstreamHidGetReportCallback GetReportCallback = NULL; +#ifdef ENABLE_KEYBOARD KeyboardOutStateTypeDef KeyboardOutDataState = KEYBOARD_OUT_STATE_IDLE; uint8_t KeyboardOutData[HID_KEYBOARD_OUTPUT_DATA_LEN]; +#endif uint8_t GetReportLoopIsRunning = 0; @@ -41,7 +46,10 @@ void Upstream_HID_DeInit(void) } GetReportCallback = NULL; GetReportLoopIsRunning = 0; + +#ifdef ENABLE_KEYBOARD KeyboardOutDataState = KEYBOARD_OUT_STATE_IDLE; +#endif } @@ -102,7 +110,6 @@ static void Upstream_HID_ReceiveInterruptReport(void) { Upstream_ReleasePacket(freePacket); } - } @@ -135,6 +142,7 @@ static void Upstream_HID_ReceiveInterruptReportCallback(UpstreamPacketTypeDef* r } else { +#ifdef ENABLE_MOUSE if (activeClass == COMMAND_CLASS_HID_MOUSE) { if (receivedPacket->Length16 != (UPSTREAM_PACKET_HEADER_LEN_16 + ((HID_MOUSE_INPUT_DATA_LEN + 1) / 2))) @@ -160,8 +168,10 @@ static void Upstream_HID_ReceiveInterruptReportCallback(UpstreamPacketTypeDef* r //Other mouse sanity checks & stuff go here... } - - else if (activeClass == COMMAND_CLASS_HID_KEYBOARD) + else +#endif +#ifdef ENABLE_KEYBOARD + if (activeClass == COMMAND_CLASS_HID_KEYBOARD) { if (receivedPacket->Length16 != (UPSTREAM_PACKET_HEADER_LEN_16 + ((HID_KEYBOARD_INPUT_DATA_LEN + 1) / 2))) { @@ -194,6 +204,8 @@ static void Upstream_HID_ReceiveInterruptReportCallback(UpstreamPacketTypeDef* r //Other HID classes go here... else +#endif + { UPSTREAM_STATEMACHINE_FREAKOUT; return; @@ -217,12 +229,14 @@ static void Upstream_HID_ReceiveInterruptReportCallback(UpstreamPacketTypeDef* r if (GetReportLoopIsRunning) { +#ifdef ENABLE_KEYBOARD //Check if we need to send OUT data to the keyboard before requesting next Interrupt IN data if (KeyboardOutDataState == KEYBOARD_OUT_STATE_DATA_READY) { Upstream_HID_SendControlReport(); } else +#endif { Upstream_HID_ReceiveInterruptReport(); //Otherwise poll downstream again } @@ -231,6 +245,7 @@ static void Upstream_HID_ReceiveInterruptReportCallback(UpstreamPacketTypeDef* r +#ifdef ENABLE_KEYBOARD void Upstream_HID_RequestSendControlReport(UpstreamPacketTypeDef* packetToSend, uint8_t dataLength) { InterfaceCommandClassTypeDef activeClass; @@ -307,6 +322,7 @@ static void Upstream_HID_SendControlReportCallback(UpstreamPacketTypeDef* receiv KeyboardOutDataState = KEYBOARD_OUT_STATE_IDLE; Upstream_HID_ReceiveInterruptReport(); } +#endif - +#endif //#if defined (ENABLE_KEYBOARD) || defined (ENABLE_MOUSE) diff --git a/Upstream/Src/upstream_msc.c b/Upstream/Src/upstream_msc.c index 55a4d8c..b0473c6 100644 --- a/Upstream/Src/upstream_msc.c +++ b/Upstream/Src/upstream_msc.c @@ -15,6 +15,10 @@ #include "upstream_spi.h" #include "upstream_statemachine.h" #include "stm32f4xx_hal.h" +#include "options.h" + + +#ifdef ENABLE_MASS_STORAGE //Stuff we need to save for our callbacks to use: @@ -275,7 +279,7 @@ void Upstream_MSC_GetStreamDataPacketCallback(UpstreamPacketTypeDef* replyPacket } - +#ifdef MASS_STORAGE_WRITES_PERMITTED HAL_StatusTypeDef Upstream_MSC_BeginWrite(UpstreamMSCCallbackTypeDef callback, uint64_t writeBlockStart, uint32_t writeBlockCount) @@ -365,4 +369,7 @@ HAL_StatusTypeDef Upstream_MSC_PutStreamDataPacket(UpstreamPacketTypeDef* packet packetToSend->Command = COMMAND_MSC_WRITE; return Upstream_TransmitPacket(packetToSend); } +#endif + +#endif //#ifdef ENABLE_MASS_STORAGE diff --git a/Upstream/Src/upstream_statemachine.c b/Upstream/Src/upstream_statemachine.c index 8261d82..a92c304 100644 --- a/Upstream/Src/upstream_statemachine.c +++ b/Upstream/Src/upstream_statemachine.c @@ -17,6 +17,7 @@ #include "usbd_core.h" #include "usbd_msc.h" #include "usbd_hid.h" +#include "options.h" UpstreamStateTypeDef UpstreamState = STATE_TEST_INTERFACE; @@ -181,23 +182,26 @@ void Upstream_StateMachine_NotifyDeviceReplyCallback(UpstreamPacketTypeDef* repl switch (replyPacket->Data[0]) { +#ifdef ENABLE_MASS_STORAGE case COMMAND_CLASS_MASS_STORAGE: newActiveClass = COMMAND_CLASS_MASS_STORAGE; newClassPointer = &USBD_MSC; break; - +#endif +#ifdef ENABLE_MOUSE case COMMAND_CLASS_HID_MOUSE: newActiveClass = COMMAND_CLASS_HID_MOUSE; newClassPointer = &USBD_HID; USBD_HID_PreinitMouse(); break; - +#endif +#ifdef ENABLE_KEYBOARD case COMMAND_CLASS_HID_KEYBOARD: newActiveClass = COMMAND_CLASS_HID_KEYBOARD; newClassPointer = &USBD_HID; USBD_HID_PreinitKeyboard(); break; - +#endif //Add other supported classes here... }