Files
mayhem-firmware/firmware/application/usb_serial_cdc.c
gullradriel 95d246a020 Update hackrf submodule (#3174)
* updated submodule

* usb_serial: populate usb_device before usb_run. Upstream now leaves the global usb_device zero-initialized and expects hackrf_usb.c to memcpy a per-board const into it. We don't link hackrf_usb.c, so do the memcpy here. Also flip control_in to enable_zlp=true to match the new endpoint API.

* baseband: derive IS_* board defines from BOARD. Upstream moved platform IS_* macros from platform_detect.h to cmake. Set them per BOARD here since we don't include platform-detect.cmake. Drop the duplicate clkin.c entry.

* sd_over_usb: drop duplicate driver objects, fix setup callback. Upstream now defines i2c0/i2c1, spi_bus_ssp0/1, sgpio_config and rf_path in the common driver files, so stop redefining them here and patch the runtime fields in pin_setup() instead. Update si5351c_reset_pll() call to the two-arg form. Replace the no-op transceiver_usb_setup_complete stub with one that delegates to usb_setup_complete.

* sd_over_usb: populate usb_device before usb_run. Same fix as the application side: memcpy a const template into usb_device before usb_device_init/usb_run, otherwise the descriptor pointer is NULL. Also pass enable_zlp=true on control_in to match the new endpoint API.

* fix unicode in comment

* remove lz4 bufs reservation in M0
2026-05-13 14:53:54 +02:00

195 lines
5.9 KiB
C

/*
* Copyright (C) 2023 Bernd Herzog
*
* This file is part of PortaPack.
*
* This program 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 2, or (at your option)
* any later version.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "usb_serial_cdc.h"
#include "usb_serial_descriptor.h"
#include "usb_serial_endpoints.h"
#include "usb_serial_event.hpp"
#include <string.h>
/* Defined in hackrf/firmware/hackrf_usb/usb_device.c. After upstream commit
* 85dfacf6 ("Universalize: firmware/hackrf_usb"), the global `usb_device` is
* left zero-initialized and is populated at runtime from per-board
* `usb_device_*` constants inside hackrf_usb.c. Mayhem does not link
* hackrf_usb.c, so we must populate `usb_device` ourselves before usb_run().
* The descriptor fields are const-qualified, so we follow the upstream
* pattern of memcpy from a const template. */
extern usb_configuration_t* usb_configurations[];
static const usb_device_t usb_device_mayhem = {
.descriptor = usb_descriptor_device,
.descriptor_strings = usb_descriptor_strings,
.qualifier_descriptor = usb_descriptor_device_qualifier,
.configurations = &usb_configurations,
.configuration = 0,
.wcid_string_descriptor = wcid_string_descriptor,
.wcid_feature_descriptor = wcid_feature_descriptor,
};
uint32_t EVT_MASK_USB = EVENT_MASK(8);
extern void usb0_isr(void);
static Thread* thread_usb_event = NULL;
CH_IRQ_HANDLER(USB0_IRQHandler) {
CH_IRQ_PROLOGUE();
const uint32_t status = USB0_USBSTS_D & USB0_USBINTR_D;
usb0_isr();
if (status & USB0_USBSTS_D_UI) {
#ifdef PRALINE
if (thread_usb_event) {
chSysLockFromIsr();
chEvtSignalI(thread_usb_event, EVT_MASK_USB);
chSysUnlockFromIsr();
}
#else
chSysLockFromIsr();
chEvtSignalI(thread_usb_event, EVT_MASK_USB);
chSysUnlockFromIsr();
#endif
}
if (status & USB0_USBSTS_D_SLI) {
on_channel_closed();
}
CH_IRQ_EPILOGUE();
}
uint32_t __ldrex(volatile uint32_t* addr) {
__disable_irq();
return *addr;
}
uint32_t __strex(uint32_t val, volatile uint32_t* addr) {
(void)val;
(void)addr;
*addr = val;
__enable_irq();
return 0;
}
void nvic_enable_irq(uint8_t irqn) {
NVIC_ISER(irqn / 32) = (1 << (irqn % 32));
thread_usb_event = chThdSelf();
}
void usb_configuration_changed(usb_device_t* const device) {
(void)device;
usb_endpoint_init(&usb_endpoint_int_in, false);
usb_endpoint_init(&usb_endpoint_bulk_in, false);
usb_endpoint_init(&usb_endpoint_bulk_out, false);
}
void setup_usb_serial_controller(void) {
memcpy(&usb_device, &usb_device_mayhem, sizeof(usb_device_mayhem));
usb_set_configuration_changed_cb(usb_configuration_changed);
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_int_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, false);
/* Control IN needs ZLP for descriptor reads whose length is a multiple of
* the max packet size (otherwise the host hangs waiting for the transfer
* to terminate). Matches upstream hackrf_usb.c after commit db73ecbf. */
usb_endpoint_init(&usb_endpoint_control_in, true);
usb_run(&usb_device);
}
const usb_request_handlers_t usb_request_handlers = {
.standard = usb_standard_request,
.class = usb_class_request,
.vendor = 0,
.reserved = 0};
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;
volatile uint8_t request = endpoint->setup.request;
if (request == 0x21) // GET LINE CODING REQUEST
return usb_get_line_coding_request(endpoint, stage);
if (request == 0x22) // SET CONTROL LINE STATE REQUEST
return usb_set_control_line_state_request(endpoint, stage);
if (request == 0x20) // SET LINE CODING REQUEST
return usb_set_line_coding_request(endpoint, stage);
return USB_REQUEST_STATUS_OK;
return status;
}
usb_request_status_t usb_get_line_coding_request(usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage) {
if (stage == USB_TRANSFER_STAGE_SETUP) {
usb_transfer_schedule_block(
endpoint->in,
&endpoint->buffer,
0,
NULL,
NULL);
} else if (stage == USB_TRANSFER_STAGE_DATA) {
usb_transfer_schedule_ack(endpoint->out);
}
return USB_REQUEST_STATUS_OK;
}
usb_request_status_t usb_set_control_line_state_request(usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage) {
if (stage == USB_TRANSFER_STAGE_SETUP) {
on_channel_opened();
usb_transfer_schedule_ack(endpoint->in);
}
return USB_REQUEST_STATUS_OK;
}
usb_request_status_t usb_set_line_coding_request(usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage) {
if (stage == USB_TRANSFER_STAGE_SETUP) {
usb_transfer_schedule_block(
endpoint->out,
&endpoint->buffer,
32,
NULL,
NULL);
} else if (stage == USB_TRANSFER_STAGE_DATA) {
usb_transfer_schedule_ack(endpoint->in);
}
return USB_REQUEST_STATUS_OK;
}