Skip to content

Commit 6f353a5

Browse files
committed
chore: minimum work for at least get usb to work
1 parent e3b83f3 commit 6f353a5

File tree

6 files changed

+131
-108
lines changed

6 files changed

+131
-108
lines changed

src/scenes/mtp/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "main.h"
55
#include <demo_app_icons.h>
66
#include "usb.h"
7+
#include "usb_desc.h"
78

89
#define THIS_SCENE MTP
910

src/scenes/mtp/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <gui/view.h>
33
#include <gui/modules/submenu.h>
44
#include <gui/modules/popup.h>
5+
#include <gui/scene_manager.h>
56
#include <furi_hal.h>
67

78
typedef struct AppMTP {

src/scenes/mtp/usb.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "main.h"
22
#include "usb.h"
3+
#include "usb_desc.h"
34
#include <furi.h>
45
#include <furi_hal.h>
6+
#include <furi_hal_usb.h>
57

68
// Define MTP specific request constants
79
#define MTP_REQ_GET_DEVICE_STATUS 0x67
@@ -21,6 +23,10 @@ typedef enum {
2123
int32_t usb_mtp_worker(void* ctx) {
2224
AppMTP* mtp = ctx;
2325
usbd_device* dev = mtp->dev;
26+
27+
// temporary!!!
28+
UNUSED(dev);
29+
2430
while(true) {
2531
furi_thread_flags_wait(EventAll, FuriFlagWaitAny, FuriWaitForever);
2632
if(furi_thread_flags_get() & EventExit) break;
@@ -39,6 +45,20 @@ int32_t usb_mtp_worker(void* ctx) {
3945
return 0;
4046
}
4147

48+
usbd_respond usb_mtp_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
49+
UNUSED(dev);
50+
UNUSED(callback);
51+
if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) !=
52+
(USB_REQ_INTERFACE | USB_REQ_CLASS)) {
53+
if(global_mtp != NULL) {
54+
global_mtp->usb_connected = false;
55+
}
56+
return usbd_fail;
57+
}
58+
switch(req->bRequest) {};
59+
return usbd_fail;
60+
}
61+
4262
void usb_mtp_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) {
4363
UNUSED(intf);
4464
AppMTP* mtp = ctx;
@@ -55,20 +75,7 @@ void usb_mtp_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) {
5575
furi_thread_start(mtp->worker_thread);
5676
}
5777

58-
usbd_respond usb_mtp_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
59-
UNUSED(callback);
60-
if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) !=
61-
(USB_REQ_INTERFACE | USB_REQ_CLASS)) {
62-
if(global_mtp != NULL) {
63-
global_mtp->usb_connected = false;
64-
}
65-
return usbd_fail;
66-
}
67-
switch(req->bRequest) {};
68-
return usbd_fail;
69-
}
70-
71-
void usb_deinit(usbd_device* dev) {
78+
void usb_mtp_deinit(usbd_device* dev) {
7279
usbd_reg_control(dev, NULL);
7380

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

89-
void usb_wakeup(usbd_device* dev) {
96+
void usb_mtp_wakeup(usbd_device* dev) {
9097
UNUSED(dev);
9198
}
9299

93-
void usb_suspend(usbd_device* dev) {
100+
void usb_mtp_suspend(usbd_device* dev) {
94101
AppMTP* mtp = global_mtp;
95102
if(!mtp || mtp->dev != dev) return;
96103
furi_thread_flags_set(furi_thread_get_id(mtp->worker_thread), EventReset);

src/scenes/mtp/usb.h

Lines changed: 5 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

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

1010
enum UsbDevDescStr {
1111
UsbDevLang = 0,
@@ -15,99 +15,12 @@ enum UsbDevDescStr {
1515
};
1616
/* === END furi_hal_usb_i.h === */
1717

18-
void usb_mtp_init(void);
19-
void usb_mtp_deinit(void);
20-
void usb_mtp_wakeup(void);
21-
void usb_mtp_suspend(void);
22-
23-
#define USB_EP0_SIZE 64
2418
#define USB_MTP_RX_EP 0x01
2519
#define USB_MTP_TX_EP 0x81
2620
#define USB_MTP_RX_EP_SIZE 64
2721
#define USB_MTP_TX_EP_SIZE 64
2822

29-
static const struct usb_string_descriptor dev_manuf_desc = USB_STRING_DESC("Flipper Devices Inc.");
30-
static const struct usb_string_descriptor dev_prod_desc = USB_STRING_DESC("MTP Device");
31-
32-
struct MtpDescriptor {
33-
struct usb_config_descriptor config;
34-
struct usb_interface_descriptor intf;
35-
struct usb_endpoint_descriptor ep_rx;
36-
struct usb_endpoint_descriptor ep_tx;
37-
} __attribute__((packed));
38-
39-
static const struct usb_device_descriptor usb_mtp_dev_descr = {
40-
.bLength = sizeof(struct usb_device_descriptor),
41-
.bDescriptorType = USB_DTYPE_DEVICE,
42-
.bcdUSB = VERSION_BCD(2, 0, 0),
43-
.bDeviceClass = USB_CLASS_STILL_IMAGE, // MTP falls under Still Image class
44-
.bDeviceSubClass = 1, // Subclass for MTP
45-
.bDeviceProtocol = 1, // Protocol for MTP
46-
.bMaxPacketSize0 = USB_EP0_SIZE,
47-
.idVendor = 0x0483, // STMicroelectronics
48-
.idProduct = 0x5741, // Custom Product ID
49-
.bcdDevice = VERSION_BCD(1, 0, 0),
50-
.iManufacturer = UsbDevManuf, // UsbDevManuf
51-
.iProduct = UsbDevProduct, // UsbDevProduct
52-
.iSerialNumber = UsbDevSerial, // UsbDevSerial
53-
.bNumConfigurations = 1,
54-
};
55-
56-
static const struct MtpDescriptor usb_mtp_cfg_descr = {
57-
.config =
58-
{
59-
.bLength = sizeof(struct usb_config_descriptor),
60-
.bDescriptorType = USB_DTYPE_CONFIGURATION,
61-
.wTotalLength = sizeof(struct MtpDescriptor),
62-
.bNumInterfaces = 1,
63-
.bConfigurationValue = 1,
64-
.iConfiguration = NO_DESCRIPTOR,
65-
.bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED,
66-
.bMaxPower = USB_CFG_POWER_MA(100),
67-
},
68-
.intf =
69-
{
70-
.bLength = sizeof(struct usb_interface_descriptor),
71-
.bDescriptorType = USB_DTYPE_INTERFACE,
72-
.bInterfaceNumber = 0,
73-
.bAlternateSetting = 0,
74-
.bNumEndpoints = 2,
75-
.bInterfaceClass = USB_CLASS_STILL_IMAGE,
76-
.bInterfaceSubClass = 1, // Subclass for MTP
77-
.bInterfaceProtocol = 1, // Protocol for MTP
78-
.iInterface = NO_DESCRIPTOR,
79-
},
80-
.ep_rx =
81-
{
82-
.bLength = sizeof(struct usb_endpoint_descriptor),
83-
.bDescriptorType = USB_DTYPE_ENDPOINT,
84-
.bEndpointAddress = USB_MTP_RX_EP,
85-
.bmAttributes = USB_EPTYPE_BULK,
86-
.wMaxPacketSize = USB_MTP_RX_EP_SIZE,
87-
.bInterval = 0,
88-
},
89-
.ep_tx =
90-
{
91-
.bLength = sizeof(struct usb_endpoint_descriptor),
92-
.bDescriptorType = USB_DTYPE_ENDPOINT,
93-
.bEndpointAddress = USB_MTP_TX_EP,
94-
.bmAttributes = USB_EPTYPE_BULK,
95-
.wMaxPacketSize = USB_MTP_TX_EP_SIZE,
96-
.bInterval = 0,
97-
},
98-
};
99-
100-
FuriHalUsbInterface usb_mtp_interface = {
101-
.init = usb_mtp_init,
102-
.deinit = usb_mtp_deinit,
103-
.wakeup = usb_mtp_wakeup,
104-
.suspend = usb_mtp_suspend,
105-
106-
.dev_descr = (struct usb_device_descriptor*)&usb_mtp_dev_descr,
107-
108-
.str_manuf_descr = (void*)&dev_manuf_desc,
109-
.str_prod_descr = (void*)&dev_prod_desc,
110-
.str_serial_descr = NULL,
111-
112-
.cfg_descr = (void*)&usb_mtp_cfg_descr,
113-
};
23+
void usb_mtp_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx);
24+
void usb_mtp_deinit(usbd_device* dev);
25+
void usb_mtp_wakeup(usbd_device* dev);
26+
void usb_mtp_suspend(usbd_device* dev);

src/scenes/mtp/usb_desc.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <furi.h>
2+
#include <furi_hal.h>
3+
#include <furi_hal_usb.h>
4+
#include "usb.h"
5+
#include "usb_desc.h"
6+
7+
const struct usb_string_descriptor dev_manuf_desc = USB_STRING_DESC("Flipper Devices Inc.");
8+
const struct usb_string_descriptor dev_prod_desc = USB_STRING_DESC("MTP Device");
9+
10+
const struct usb_device_descriptor usb_mtp_dev_descr = {
11+
.bLength = sizeof(struct usb_device_descriptor),
12+
.bDescriptorType = USB_DTYPE_DEVICE,
13+
.bcdUSB = VERSION_BCD(2, 0, 0),
14+
.bDeviceClass = USB_CLASS_STILL_IMAGE, // MTP falls under Still Image class
15+
.bDeviceSubClass = 1, // Subclass for MTP
16+
.bDeviceProtocol = 1, // Protocol for MTP
17+
.bMaxPacketSize0 = USB_EP0_SIZE,
18+
.idVendor = 0x0483, // STMicroelectronics
19+
.idProduct = 0x5741, // Custom Product ID
20+
.bcdDevice = VERSION_BCD(1, 0, 0),
21+
.iManufacturer = UsbDevManuf, // UsbDevManuf
22+
.iProduct = UsbDevProduct, // UsbDevProduct
23+
.iSerialNumber = UsbDevSerial, // UsbDevSerial
24+
.bNumConfigurations = 1,
25+
};
26+
27+
const struct MtpDescriptor usb_mtp_cfg_descr = {
28+
.config =
29+
{
30+
.bLength = sizeof(struct usb_config_descriptor),
31+
.bDescriptorType = USB_DTYPE_CONFIGURATION,
32+
.wTotalLength = sizeof(struct MtpDescriptor),
33+
.bNumInterfaces = 1,
34+
.bConfigurationValue = 1,
35+
.iConfiguration = NO_DESCRIPTOR,
36+
.bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED,
37+
.bMaxPower = USB_CFG_POWER_MA(100),
38+
},
39+
.intf =
40+
{
41+
.bLength = sizeof(struct usb_interface_descriptor),
42+
.bDescriptorType = USB_DTYPE_INTERFACE,
43+
.bInterfaceNumber = 0,
44+
.bAlternateSetting = 0,
45+
.bNumEndpoints = 2,
46+
.bInterfaceClass = USB_CLASS_STILL_IMAGE,
47+
.bInterfaceSubClass = 1, // Subclass for MTP
48+
.bInterfaceProtocol = 1, // Protocol for MTP
49+
.iInterface = NO_DESCRIPTOR,
50+
},
51+
.ep_rx =
52+
{
53+
.bLength = sizeof(struct usb_endpoint_descriptor),
54+
.bDescriptorType = USB_DTYPE_ENDPOINT,
55+
.bEndpointAddress = USB_MTP_RX_EP,
56+
.bmAttributes = USB_EPTYPE_BULK,
57+
.wMaxPacketSize = USB_MTP_RX_EP_SIZE,
58+
.bInterval = 0,
59+
},
60+
.ep_tx =
61+
{
62+
.bLength = sizeof(struct usb_endpoint_descriptor),
63+
.bDescriptorType = USB_DTYPE_ENDPOINT,
64+
.bEndpointAddress = USB_MTP_TX_EP,
65+
.bmAttributes = USB_EPTYPE_BULK,
66+
.wMaxPacketSize = USB_MTP_TX_EP_SIZE,
67+
.bInterval = 0,
68+
},
69+
};
70+
71+
FuriHalUsbInterface usb_mtp_interface = {
72+
.init = usb_mtp_init,
73+
.deinit = usb_mtp_deinit,
74+
.wakeup = usb_mtp_wakeup,
75+
.suspend = usb_mtp_suspend,
76+
77+
.dev_descr = (struct usb_device_descriptor*)&usb_mtp_dev_descr,
78+
79+
.str_manuf_descr = (void*)&dev_manuf_desc,
80+
.str_prod_descr = (void*)&dev_prod_desc,
81+
.str_serial_descr = NULL,
82+
83+
.cfg_descr = (void*)&usb_mtp_cfg_descr,
84+
};

src/scenes/mtp/usb_desc.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
#include <furi.h>
3+
#include <furi_hal.h>
4+
#include <furi_hal_usb.h>
5+
6+
struct MtpDescriptor {
7+
struct usb_config_descriptor config;
8+
struct usb_interface_descriptor intf;
9+
struct usb_endpoint_descriptor ep_rx;
10+
struct usb_endpoint_descriptor ep_tx;
11+
} __attribute__((packed));
12+
13+
extern const struct usb_string_descriptor dev_manuf_desc;
14+
extern const struct usb_string_descriptor dev_prod_desc;
15+
extern const struct usb_device_descriptor usb_mtp_dev_descr;
16+
extern const struct MtpDescriptor usb_mtp_cfg_descr;
17+
extern FuriHalUsbInterface usb_mtp_interface;

0 commit comments

Comments
 (0)