mayhem-firmware/firmware/baseband/sd_over_usb/sd_over_usb.c

223 lines
4.7 KiB
C
Raw Normal View History

2023-03-29 15:05:11 +00:00
#include "sd_over_usb.h"
2023-03-31 17:18:39 +00:00
#include "scsi.h"
2023-03-29 15:05:11 +00:00
//extern usb_request_handlers_t usb_request_handlers;
2023-03-31 17:18:39 +00:00
#define HALT_UNTIL_DEBUGGING() \
while (!((*(volatile uint32_t *)0xE000EDF0) & (1 << 0))) {} \
__asm__ __volatile__("bkpt 1")
2023-03-29 15:05:11 +00:00
2023-03-31 17:18:39 +00:00
// uint8_t usb_buffer[USB_TRANSFER_SIZE];
2023-03-29 15:05:11 +00:00
2023-03-31 17:18:39 +00:00
bool scsi_running = false;
usb_request_status_t report_max_lun(
usb_endpoint_t* const endpoint,
const usb_transfer_stage_t stage)
{
if (stage == USB_TRANSFER_STAGE_SETUP) {
endpoint->buffer[0] = 0;
usb_transfer_schedule_block(
endpoint->in,
&endpoint->buffer,
1,
NULL,
NULL);
usb_transfer_schedule_ack(endpoint->out);
/* start loop here?*/
scsi_running = true;
// Host to Device. receive inquiry LUN
return USB_REQUEST_STATUS_OK;
}
else if (stage == USB_TRANSFER_STAGE_DATA) {
return USB_REQUEST_STATUS_OK;
}
else if (stage == USB_TRANSFER_STAGE_STATUS) {
// response here?
return USB_REQUEST_STATUS_OK;
}
}
usb_request_status_t report_magic_scsi(
2023-03-29 15:05:11 +00:00
usb_endpoint_t* const endpoint,
const usb_transfer_stage_t stage)
{
2023-03-31 17:18:39 +00:00
// return USB_REQUEST_STATUS_OK;
// if (endpoint->address != 0x02) {
// HALT_UNTIL_DEBUGGING();
// return USB_REQUEST_STATUS_OK;
// }
if (stage == USB_TRANSFER_STAGE_SETUP) {
return USB_REQUEST_STATUS_OK;
// usb_bulk_buffer[0] = 0x55;
// usb_bulk_buffer[1] = 0x55;
// usb_bulk_buffer[2] = 0x55;
// usb_bulk_buffer[3] = 0x55;
// usb_bulk_buffer[4] = 0;
// usb_transfer_schedule_block(
// &usb_endpoint_bulk_out,
// &usb_bulk_buffer[0x0000],
// 5,
// NULL,
// NULL);
// usb_transfer_schedule_ack(endpoint->out);
}
else if (stage == USB_TRANSFER_STAGE_DATA) {
HALT_UNTIL_DEBUGGING();
}
else if (stage == USB_TRANSFER_STAGE_STATUS) {
HALT_UNTIL_DEBUGGING();
}
return USB_REQUEST_STATUS_OK;
2023-03-29 15:05:11 +00:00
(void)(endpoint);
(void)(stage);
return USB_REQUEST_STATUS_OK;
}
usb_request_status_t usb_vendor_request(
usb_endpoint_t* const endpoint,
const usb_transfer_stage_t stage)
{
usb_request_status_t status = USB_REQUEST_STATUS_STALL;
2023-04-01 15:21:57 +00:00
// volatile uint_fast8_t address = endpoint->address;
2023-03-31 17:18:39 +00:00
volatile uint8_t request = endpoint->setup.request;
2023-04-01 15:21:57 +00:00
// volatile uint32_t b = 0;
2023-03-31 17:18:39 +00:00
if (request == 25) { // unknown code
return report_magic_scsi(endpoint, stage);
2023-03-29 15:05:11 +00:00
}
2023-04-01 15:21:57 +00:00
// b = request + (address << 16);
// HALT_UNTIL_DEBUGGING();
2023-03-31 17:18:39 +00:00
2023-03-29 15:05:11 +00:00
return status;
}
2023-03-31 17:18:39 +00:00
usb_request_status_t usb_class_request(
usb_endpoint_t* const endpoint,
const usb_transfer_stage_t stage)
{
usb_request_status_t status = USB_REQUEST_STATUS_STALL;
2023-03-29 15:05:11 +00:00
2023-03-31 17:18:39 +00:00
volatile uint8_t request = endpoint->setup.request;
2023-04-01 15:21:57 +00:00
// volatile uint32_t b = 0;
2023-03-29 15:05:11 +00:00
2023-03-31 17:18:39 +00:00
if (request == 0xFE) {
return report_max_lun(endpoint, stage);
}
2023-03-30 09:46:36 +00:00
2023-04-01 15:21:57 +00:00
// b = request + (address << 16);
// HALT_UNTIL_DEBUGGING();
2023-03-29 15:05:11 +00:00
2023-03-31 17:18:39 +00:00
// if (address == 0x80) {
2023-03-30 09:46:36 +00:00
2023-03-31 17:18:39 +00:00
// HALT_UNTIL_DEBUGGING();
// return USB_REQUEST_STATUS_OK;
// }
2023-03-30 09:46:36 +00:00
2023-03-31 17:18:39 +00:00
// else if (address == 0x00) {
// if (request == 0xFE) {
// return USB_REQUEST_STATUS_OK; // ???????????????????
// }
// b = request + (address << 16);
// HALT_UNTIL_DEBUGGING();
// }
2023-03-30 09:46:36 +00:00
2023-03-31 17:18:39 +00:00
// else if (address == 0x02) {
// if (request == 0xFE) {
// return USB_REQUEST_STATUS_OK; // ???????????????????
// }
// //return USB_REQUEST_STATUS_OK;
// }
2023-03-30 09:46:36 +00:00
2023-03-31 17:18:39 +00:00
// HALT_UNTIL_DEBUGGING();
2023-04-01 15:21:57 +00:00
return status;
2023-03-31 17:18:39 +00:00
}
2023-03-30 09:46:36 +00:00
2023-03-31 17:18:39 +00:00
const usb_request_handlers_t usb_request_handlers = {
.standard = usb_standard_request,
.class = usb_class_request,
.vendor = usb_vendor_request,
.reserved = 0
};
2023-03-30 09:46:36 +00:00
2023-03-31 17:18:39 +00:00
void usb_configuration_changed(usb_device_t* const device)
{
usb_endpoint_init(&usb_endpoint_bulk_in);
usb_endpoint_init(&usb_endpoint_bulk_out);
}
2023-03-30 09:46:36 +00:00
2023-03-31 17:18:39 +00:00
void start_usb(void) {
2023-03-30 09:46:36 +00:00
detect_hardware_platform();
pin_setup();
2023-04-01 15:21:57 +00:00
cpu_clock_init(); // required
2023-03-30 09:46:36 +00:00
usb_set_configuration_changed_cb(usb_configuration_changed);
2023-03-29 15:05:11 +00:00
usb_peripheral_reset();
usb_device_init(0, &usb_device);
usb_queue_init(&usb_endpoint_control_out_queue);
usb_queue_init(&usb_endpoint_control_in_queue);
usb_queue_init(&usb_endpoint_bulk_out_queue);
usb_queue_init(&usb_endpoint_bulk_in_queue);
usb_endpoint_init(&usb_endpoint_control_out);
usb_endpoint_init(&usb_endpoint_control_in);
nvic_set_priority(NVIC_USB0_IRQ, 255);
usb_run(&usb_device);
}
void stop_usb(void) {
usb_peripheral_reset();
}
void irq_usb(void) {
usb0_isr();
}
2023-03-31 17:18:39 +00:00
volatile bool transfer_complete = false;
void scsi_bulk_transfer_complete(void* user_data, unsigned int bytes_transferred)
{
transfer_complete = true;
}
void usb_transfer(void) {
if (scsi_running) {
transfer_complete = false;
usb_transfer_schedule_block(
&usb_endpoint_bulk_out,
&usb_bulk_buffer[0],
USB_TRANSFER_SIZE,
scsi_bulk_transfer_complete,
NULL);
while (!transfer_complete);
msd_cbw_t *msd_cbw_data = (msd_cbw_t *) &usb_bulk_buffer[0];
if (msd_cbw_data->signature == MSD_CBW_SIGNATURE){
scsi_command(msd_cbw_data);
}
}
}