Skip to content

Commit

Permalink
chore: minimum work for at least get usb to work
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex4386 committed Jun 4, 2024
1 parent e3b83f3 commit 6f353a5
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 108 deletions.
1 change: 1 addition & 0 deletions src/scenes/mtp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "main.h"
#include <demo_app_icons.h>
#include "usb.h"
#include "usb_desc.h"

#define THIS_SCENE MTP

Expand Down
1 change: 1 addition & 0 deletions src/scenes/mtp/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <gui/view.h>
#include <gui/modules/submenu.h>
#include <gui/modules/popup.h>
#include <gui/scene_manager.h>
#include <furi_hal.h>

typedef struct AppMTP {
Expand Down
39 changes: 23 additions & 16 deletions src/scenes/mtp/usb.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "main.h"
#include "usb.h"
#include "usb_desc.h"
#include <furi.h>
#include <furi_hal.h>
#include <furi_hal_usb.h>

// Define MTP specific request constants
#define MTP_REQ_GET_DEVICE_STATUS 0x67
Expand All @@ -21,6 +23,10 @@ typedef enum {
int32_t usb_mtp_worker(void* ctx) {
AppMTP* mtp = ctx;
usbd_device* dev = mtp->dev;

// temporary!!!
UNUSED(dev);

while(true) {
furi_thread_flags_wait(EventAll, FuriFlagWaitAny, FuriWaitForever);
if(furi_thread_flags_get() & EventExit) break;
Expand All @@ -39,6 +45,20 @@ int32_t usb_mtp_worker(void* ctx) {
return 0;
}

usbd_respond usb_mtp_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
UNUSED(dev);
UNUSED(callback);
if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) !=
(USB_REQ_INTERFACE | USB_REQ_CLASS)) {
if(global_mtp != NULL) {
global_mtp->usb_connected = false;
}
return usbd_fail;
}
switch(req->bRequest) {};
return usbd_fail;
}

void usb_mtp_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) {
UNUSED(intf);
AppMTP* mtp = ctx;
Expand All @@ -55,20 +75,7 @@ void usb_mtp_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) {
furi_thread_start(mtp->worker_thread);
}

usbd_respond usb_mtp_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
UNUSED(callback);
if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) !=
(USB_REQ_INTERFACE | USB_REQ_CLASS)) {
if(global_mtp != NULL) {
global_mtp->usb_connected = false;
}
return usbd_fail;
}
switch(req->bRequest) {};
return usbd_fail;
}

void usb_deinit(usbd_device* dev) {
void usb_mtp_deinit(usbd_device* dev) {
usbd_reg_control(dev, NULL);

AppMTP* mtp = global_mtp;
Expand All @@ -86,11 +93,11 @@ void usb_deinit(usbd_device* dev) {
mtp->worker_thread = NULL;
}

void usb_wakeup(usbd_device* dev) {
void usb_mtp_wakeup(usbd_device* dev) {
UNUSED(dev);
}

void usb_suspend(usbd_device* dev) {
void usb_mtp_suspend(usbd_device* dev) {
AppMTP* mtp = global_mtp;
if(!mtp || mtp->dev != dev) return;
furi_thread_flags_set(furi_thread_get_id(mtp->worker_thread), EventReset);
Expand Down
97 changes: 5 additions & 92 deletions src/scenes/mtp/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/* === START furi_hal_usb_i.h === */
// https://github.com/flipperdevices/flipperzero-firmware/blob/03196fa11007c0f1e002cbb0b82102d8492456b5/targets/f7/furi_hal/furi_hal_usb_i.h#L5
#define USB_EP0_SIZE 8
#define USB_EP0_SIZE 64

enum UsbDevDescStr {
UsbDevLang = 0,
Expand All @@ -15,99 +15,12 @@ enum UsbDevDescStr {
};
/* === END furi_hal_usb_i.h === */

void usb_mtp_init(void);
void usb_mtp_deinit(void);
void usb_mtp_wakeup(void);
void usb_mtp_suspend(void);

#define USB_EP0_SIZE 64
#define USB_MTP_RX_EP 0x01
#define USB_MTP_TX_EP 0x81
#define USB_MTP_RX_EP_SIZE 64
#define USB_MTP_TX_EP_SIZE 64

static const struct usb_string_descriptor dev_manuf_desc = USB_STRING_DESC("Flipper Devices Inc.");
static const struct usb_string_descriptor dev_prod_desc = USB_STRING_DESC("MTP Device");

struct MtpDescriptor {
struct usb_config_descriptor config;
struct usb_interface_descriptor intf;
struct usb_endpoint_descriptor ep_rx;
struct usb_endpoint_descriptor ep_tx;
} __attribute__((packed));

static const struct usb_device_descriptor usb_mtp_dev_descr = {
.bLength = sizeof(struct usb_device_descriptor),
.bDescriptorType = USB_DTYPE_DEVICE,
.bcdUSB = VERSION_BCD(2, 0, 0),
.bDeviceClass = USB_CLASS_STILL_IMAGE, // MTP falls under Still Image class
.bDeviceSubClass = 1, // Subclass for MTP
.bDeviceProtocol = 1, // Protocol for MTP
.bMaxPacketSize0 = USB_EP0_SIZE,
.idVendor = 0x0483, // STMicroelectronics
.idProduct = 0x5741, // Custom Product ID
.bcdDevice = VERSION_BCD(1, 0, 0),
.iManufacturer = UsbDevManuf, // UsbDevManuf
.iProduct = UsbDevProduct, // UsbDevProduct
.iSerialNumber = UsbDevSerial, // UsbDevSerial
.bNumConfigurations = 1,
};

static const struct MtpDescriptor usb_mtp_cfg_descr = {
.config =
{
.bLength = sizeof(struct usb_config_descriptor),
.bDescriptorType = USB_DTYPE_CONFIGURATION,
.wTotalLength = sizeof(struct MtpDescriptor),
.bNumInterfaces = 1,
.bConfigurationValue = 1,
.iConfiguration = NO_DESCRIPTOR,
.bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED,
.bMaxPower = USB_CFG_POWER_MA(100),
},
.intf =
{
.bLength = sizeof(struct usb_interface_descriptor),
.bDescriptorType = USB_DTYPE_INTERFACE,
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 2,
.bInterfaceClass = USB_CLASS_STILL_IMAGE,
.bInterfaceSubClass = 1, // Subclass for MTP
.bInterfaceProtocol = 1, // Protocol for MTP
.iInterface = NO_DESCRIPTOR,
},
.ep_rx =
{
.bLength = sizeof(struct usb_endpoint_descriptor),
.bDescriptorType = USB_DTYPE_ENDPOINT,
.bEndpointAddress = USB_MTP_RX_EP,
.bmAttributes = USB_EPTYPE_BULK,
.wMaxPacketSize = USB_MTP_RX_EP_SIZE,
.bInterval = 0,
},
.ep_tx =
{
.bLength = sizeof(struct usb_endpoint_descriptor),
.bDescriptorType = USB_DTYPE_ENDPOINT,
.bEndpointAddress = USB_MTP_TX_EP,
.bmAttributes = USB_EPTYPE_BULK,
.wMaxPacketSize = USB_MTP_TX_EP_SIZE,
.bInterval = 0,
},
};

FuriHalUsbInterface usb_mtp_interface = {
.init = usb_mtp_init,
.deinit = usb_mtp_deinit,
.wakeup = usb_mtp_wakeup,
.suspend = usb_mtp_suspend,

.dev_descr = (struct usb_device_descriptor*)&usb_mtp_dev_descr,

.str_manuf_descr = (void*)&dev_manuf_desc,
.str_prod_descr = (void*)&dev_prod_desc,
.str_serial_descr = NULL,

.cfg_descr = (void*)&usb_mtp_cfg_descr,
};
void usb_mtp_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx);
void usb_mtp_deinit(usbd_device* dev);
void usb_mtp_wakeup(usbd_device* dev);
void usb_mtp_suspend(usbd_device* dev);
84 changes: 84 additions & 0 deletions src/scenes/mtp/usb_desc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <furi.h>
#include <furi_hal.h>
#include <furi_hal_usb.h>
#include "usb.h"
#include "usb_desc.h"

const struct usb_string_descriptor dev_manuf_desc = USB_STRING_DESC("Flipper Devices Inc.");
const struct usb_string_descriptor dev_prod_desc = USB_STRING_DESC("MTP Device");

const struct usb_device_descriptor usb_mtp_dev_descr = {
.bLength = sizeof(struct usb_device_descriptor),
.bDescriptorType = USB_DTYPE_DEVICE,
.bcdUSB = VERSION_BCD(2, 0, 0),
.bDeviceClass = USB_CLASS_STILL_IMAGE, // MTP falls under Still Image class
.bDeviceSubClass = 1, // Subclass for MTP
.bDeviceProtocol = 1, // Protocol for MTP
.bMaxPacketSize0 = USB_EP0_SIZE,
.idVendor = 0x0483, // STMicroelectronics
.idProduct = 0x5741, // Custom Product ID
.bcdDevice = VERSION_BCD(1, 0, 0),
.iManufacturer = UsbDevManuf, // UsbDevManuf
.iProduct = UsbDevProduct, // UsbDevProduct
.iSerialNumber = UsbDevSerial, // UsbDevSerial
.bNumConfigurations = 1,
};

const struct MtpDescriptor usb_mtp_cfg_descr = {
.config =
{
.bLength = sizeof(struct usb_config_descriptor),
.bDescriptorType = USB_DTYPE_CONFIGURATION,
.wTotalLength = sizeof(struct MtpDescriptor),
.bNumInterfaces = 1,
.bConfigurationValue = 1,
.iConfiguration = NO_DESCRIPTOR,
.bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED,
.bMaxPower = USB_CFG_POWER_MA(100),
},
.intf =
{
.bLength = sizeof(struct usb_interface_descriptor),
.bDescriptorType = USB_DTYPE_INTERFACE,
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 2,
.bInterfaceClass = USB_CLASS_STILL_IMAGE,
.bInterfaceSubClass = 1, // Subclass for MTP
.bInterfaceProtocol = 1, // Protocol for MTP
.iInterface = NO_DESCRIPTOR,
},
.ep_rx =
{
.bLength = sizeof(struct usb_endpoint_descriptor),
.bDescriptorType = USB_DTYPE_ENDPOINT,
.bEndpointAddress = USB_MTP_RX_EP,
.bmAttributes = USB_EPTYPE_BULK,
.wMaxPacketSize = USB_MTP_RX_EP_SIZE,
.bInterval = 0,
},
.ep_tx =
{
.bLength = sizeof(struct usb_endpoint_descriptor),
.bDescriptorType = USB_DTYPE_ENDPOINT,
.bEndpointAddress = USB_MTP_TX_EP,
.bmAttributes = USB_EPTYPE_BULK,
.wMaxPacketSize = USB_MTP_TX_EP_SIZE,
.bInterval = 0,
},
};

FuriHalUsbInterface usb_mtp_interface = {
.init = usb_mtp_init,
.deinit = usb_mtp_deinit,
.wakeup = usb_mtp_wakeup,
.suspend = usb_mtp_suspend,

.dev_descr = (struct usb_device_descriptor*)&usb_mtp_dev_descr,

.str_manuf_descr = (void*)&dev_manuf_desc,
.str_prod_descr = (void*)&dev_prod_desc,
.str_serial_descr = NULL,

.cfg_descr = (void*)&usb_mtp_cfg_descr,
};
17 changes: 17 additions & 0 deletions src/scenes/mtp/usb_desc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once
#include <furi.h>
#include <furi_hal.h>
#include <furi_hal_usb.h>

struct MtpDescriptor {
struct usb_config_descriptor config;
struct usb_interface_descriptor intf;
struct usb_endpoint_descriptor ep_rx;
struct usb_endpoint_descriptor ep_tx;
} __attribute__((packed));

extern const struct usb_string_descriptor dev_manuf_desc;
extern const struct usb_string_descriptor dev_prod_desc;
extern const struct usb_device_descriptor usb_mtp_dev_descr;
extern const struct MtpDescriptor usb_mtp_cfg_descr;
extern FuriHalUsbInterface usb_mtp_interface;

0 comments on commit 6f353a5

Please sign in to comment.