2020-05-05 16:22:16 +01:00
/*
2020-07-03 10:31:22 +01:00
* usb . h
2020-05-05 16:22:16 +01:00
*
2020-07-03 10:31:22 +01:00
* Heavily based in usb_comms from libnx .
*
2020-07-06 01:10:07 +01:00
* Copyright ( c ) 2018 - 2020 , Switchbrew and libnx contributors .
2023-04-08 12:42:22 +01:00
* Copyright ( c ) 2020 - 2023 , DarkMatterCore < pabloacurielz @ gmail . com > .
2020-07-03 10:31:22 +01:00
*
* This file is part of nxdumptool ( https : //github.com/DarkMatterCore/nxdumptool).
*
2021-03-25 19:26:58 +00:00
* nxdumptool is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
2020-05-05 16:22:16 +01:00
*
2021-03-25 19:26:58 +00:00
* nxdumptool is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
2020-05-05 16:22:16 +01:00
*
* You should have received a copy of the GNU General Public License
2021-03-25 19:26:58 +00:00
* along with this program . If not , see < https : //www.gnu.org/licenses/>.
2020-05-05 16:22:16 +01:00
*/
# pragma once
2021-03-24 17:25:19 +00:00
# ifndef __USB_H__
# define __USB_H__
2021-03-23 14:06:52 +00:00
# ifdef __cplusplus
extern " C " {
# endif
2020-07-06 01:10:07 +01:00
# define USB_TRANSFER_BUFFER_SIZE 0x800000 /* 8 MiB. */
2020-05-05 16:22:16 +01:00
2021-06-25 22:01:15 +01:00
/// Used to indicate the USB speed selected by the host device.
typedef enum {
UsbHostSpeed_None = 0 ,
UsbHostSpeed_FullSpeed = 1 , ///< USB 1.x.
UsbHostSpeed_HighSpeed = 2 , ///< USB 2.0.
2023-07-17 00:03:05 +01:00
UsbHostSpeed_SuperSpeed = 3 , ///< USB 3.0.
UsbHostSpeed_Count = 4 ///< Total values supported by this enum.
2021-06-25 22:01:15 +01:00
} UsbHostSpeed ;
2020-05-06 15:04:10 +01:00
/// Initializes the USB interface, input and output endpoints and allocates an internal transfer buffer.
2020-05-05 16:22:16 +01:00
bool usbInitialize ( void ) ;
/// Closes the USB interface, input and output endpoints and frees the transfer buffer.
void usbExit ( void ) ;
2021-02-12 20:35:23 +00:00
/// Returns a pointer to a dynamically allocated, page aligned memory buffer that's suitable for USB transfers.
2020-05-07 12:08:54 +01:00
void * usbAllocatePageAlignedBuffer ( size_t size ) ;
2020-05-05 16:22:16 +01:00
2021-05-18 13:32:43 +01:00
/// Used to check if the console has been connected to a USB host device and if a valid USB session has been established.
2021-06-25 22:01:15 +01:00
/// Returns a value from the UsbHostSpeed enum.
u8 usbIsReady ( void ) ;
2020-05-05 16:22:16 +01:00
2023-05-24 20:05:34 +01:00
/// Sends file properties to the host device before starting a file data transfer. If needed, it must be called before usbSendFileData().
/// 'file_size' may be zero if an empty file shall be created, in which case no file data transfer will be necessary.
/// Calling this function before finishing an ongoing file data transfer will result in an error.
2020-10-26 06:39:33 +00:00
/// Under NSP transfer mode, this function must be called right before transferring data from each NSP file entry to the host device, which should in turn write it all to the same output file.
2023-05-24 20:05:34 +01:00
bool usbSendFileProperties ( u64 file_size , const char * filename ) ;
/// Sends NSP properties to the host device and enables NSP transfer mode. If needed, it must be called before usbSendFileData().
/// Both 'nsp_size' and 'nsp_header_size' must be greater than zero. 'nsp_size' must also be greater than 'nsp_header_size'.
/// Calling this function after NSP transfer mode has already been enabled will result in an error.
2020-10-26 06:39:33 +00:00
/// The host device should immediately write 'nsp_header_size' padding at the start of the output file and start listening for further usbSendFileProperties() calls, or a usbSendNspHeader() call.
2023-05-24 20:05:34 +01:00
bool usbSendNspProperties ( u64 nsp_size , const char * filename , u32 nsp_header_size ) ;
2020-05-05 16:22:16 +01:00
2023-05-24 20:05:34 +01:00
/// Performs a file data transfer. Must be continuously called after usbSendFileProperties() / usbSendNspProperties() until all file data has been transferred.
2020-05-05 16:22:16 +01:00
/// Data chunk size must not exceed USB_TRANSFER_BUFFER_SIZE.
2020-10-26 06:39:33 +00:00
/// If the last file data chunk is aligned to the endpoint max packet size, the host device should expect a Zero Length Termination (ZLT) packet.
2023-05-24 20:05:34 +01:00
/// Calling this function if there's no remaining data to transfer will result in an error.
2020-05-07 12:08:54 +01:00
bool usbSendFileData ( void * data , u64 data_size ) ;
2020-05-05 16:22:16 +01:00
2021-02-15 21:07:47 +00:00
/// Used to gracefully cancel an ongoing file transfer. The current USB session is kept alive.
void usbCancelFileTransfer ( void ) ;
2020-10-26 06:39:33 +00:00
/// Sends NSP header data to the host device, making it rewind the NSP file pointer to write this data, essentially finishing the NSP transfer process.
2023-05-24 20:05:34 +01:00
/// Must be called after the data from all NSP file entries has been transferred using both usbSendNspProperties() and usbSendFileData() calls.
2021-02-13 21:49:05 +00:00
/// If the NSP header size is aligned to the endpoint max packet size, the host device should expect a Zero Length Termination (ZLT) packet.
2020-10-26 06:39:33 +00:00
bool usbSendNspHeader ( void * nsp_header , u32 nsp_header_size ) ;
2023-11-11 20:39:41 +00:00
/// Informs the host device that an extracted filesystem dump (e.g. HFS, PFS, RomFS) is about to begin.
bool usbStartExtractedFsDump ( u64 extracted_fs_size , const char * extracted_fs_root_path ) ;
/// Informs the host device that a previously started filesystem dump (via usbStartExtractedFsDump()) has finished.
/// This is only issued after all extracted file entries have been successfully transferred to the host device.
void usbEndExtractedFsDump ( void ) ;
2021-03-23 14:06:52 +00:00
# ifdef __cplusplus
}
2021-03-24 17:25:19 +00:00
# endif
# endif /* __USB_H__ */