mirror of
https://gitee.com/Vancouver2017/luban-lite-t3e-pro.git
synced 2025-12-14 10:28:54 +00:00
V1.0.5
This commit is contained in:
@@ -48,7 +48,7 @@
|
||||
#define USB_HID_CONFIG_DESC_SIZ (9 + 9 + 9 + 7 + 7)
|
||||
|
||||
/*!< custom hid report descriptor size */
|
||||
#define HID_CUSTOM_REPORT_DESC_SIZE 38
|
||||
#define HID_CUSTOM_REPORT_DESC_SIZE 63
|
||||
|
||||
/*!< global descriptor */
|
||||
static const uint8_t hid_descriptor[] = {
|
||||
@@ -88,7 +88,7 @@ static const uint8_t hid_descriptor[] = {
|
||||
0x03, /* bmAttributes: Interrupt endpoint */
|
||||
WBVAL(HIDRAW_OUT_EP_SIZE), /* wMaxPacketSize: 4 Byte max */
|
||||
HIDRAW_OUT_EP_INTERVAL, /* bInterval: Polling Interval */
|
||||
/* 73 */
|
||||
/* 41 */
|
||||
/*
|
||||
* string0 descriptor
|
||||
*/
|
||||
@@ -163,29 +163,39 @@ static const uint8_t hid_descriptor[] = {
|
||||
0x00
|
||||
};
|
||||
|
||||
/*!< custom hid report descriptor */
|
||||
static const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = {
|
||||
/* USER CODE BEGIN 0 */
|
||||
0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */
|
||||
0x09, 0x01, /* USAGE (Vendor Usage 1) */
|
||||
0xa1, 0x01, /* COLLECTION (Application) */
|
||||
0x85, 0x02, /* REPORT ID (0x02) */
|
||||
0x09, 0x01, /* USAGE (Vendor Usage 1) */
|
||||
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
|
||||
0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
|
||||
0x95, 0x40 - 1, /* REPORT_COUNT (63) */
|
||||
0x75, 0x08, /* REPORT_SIZE (8) */
|
||||
0x81, 0x02, /* INPUT (Data,Var,Abs) */
|
||||
/* <___________________________________________________> */
|
||||
0x85, 0x01, /* REPORT ID (0x01) */
|
||||
0x09, 0x01, /* USAGE (Vendor Usage 1) */
|
||||
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
|
||||
0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
|
||||
0x95, 0x40 - 1, /* REPORT_COUNT (63) */
|
||||
0x75, 0x08, /* REPORT_SIZE (8) */
|
||||
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
|
||||
/* USER CODE END 0 */
|
||||
0xC0 /* END_COLLECTION */
|
||||
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
|
||||
0x09, 0x06, // USAGE (Keyboard)
|
||||
0xa1, 0x01, // COLLECTION (Application)
|
||||
0x05, 0x07, // USAGE_PAGE (Keyboard)
|
||||
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
|
||||
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
|
||||
0x15, 0x00, // LOGICAL_MINIMUM (0)
|
||||
0x25, 0x01, // LOGICAL_MAXIMUM (1)
|
||||
0x75, 0x01, // REPORT_SIZE (1)
|
||||
0x95, 0x08, // REPORT_COUNT (8)
|
||||
0x81, 0x02, // INPUT (Data,Var,Abs)
|
||||
0x95, 0x01, // REPORT_COUNT (1)
|
||||
0x75, 0x08, // REPORT_SIZE (8)
|
||||
0x81, 0x03, // INPUT (Cnst,Var,Abs)
|
||||
0x95, 0x05, // REPORT_COUNT (5)
|
||||
0x75, 0x01, // REPORT_SIZE (1)
|
||||
0x05, 0x08, // USAGE_PAGE (LEDs)
|
||||
0x19, 0x01, // USAGE_MINIMUM (Num Lock)
|
||||
0x29, 0x05, // USAGE_MAXIMUM (Kana)
|
||||
0x91, 0x02, // OUTPUT (Data,Var,Abs)
|
||||
0x95, 0x01, // REPORT_COUNT (1)
|
||||
0x75, 0x03, // REPORT_SIZE (3)
|
||||
0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
|
||||
0x95, 0x06, // REPORT_COUNT (6)
|
||||
0x75, 0x08, // REPORT_SIZE (8)
|
||||
0x15, 0x00, // LOGICAL_MINIMUM (0)
|
||||
0x25, 0xFF, // LOGICAL_MAXIMUM (255)
|
||||
0x05, 0x07, // USAGE_PAGE (Keyboard)
|
||||
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
|
||||
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
|
||||
0x81, 0x00, // INPUT (Data,Ary,Abs)
|
||||
0xc0 // END_COLLECTION
|
||||
};
|
||||
|
||||
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[HIDRAW_OUT_EP_SIZE];
|
||||
@@ -217,7 +227,6 @@ struct hid_vbuf {
|
||||
unsigned int buf_len[HID_RECV_BUF_SIZE / HID_VBUF_PLANE_SIZE];
|
||||
struct vb_queue queue;
|
||||
struct list_head active_list;
|
||||
aicos_mutex_t active_lock; /* lock of active buf list */
|
||||
unsigned int sequence;
|
||||
unsigned int streaming;
|
||||
};
|
||||
@@ -268,7 +277,7 @@ static void show_speed(u64 start, u64 end, int bytes)
|
||||
static void notify_file_decode(void)
|
||||
{
|
||||
printf("\nThe follow command can decode the image file to FB: \n");
|
||||
printf("pic_test -a 0x%lx -z %d\n", (long)g_hid_img_buf, g_hid_img_size);
|
||||
printf("pic_test -a 0x%lx -z %d\n", (long)g_hid_img_buf, (int)g_hid_img_size);
|
||||
if (!g_hid_only_recv)
|
||||
rt_sem_release(g_hid_sem);
|
||||
}
|
||||
@@ -390,7 +399,7 @@ static void dump_to_buf(uint8_t *data, uint32_t len)
|
||||
parse_file_type(data);
|
||||
|
||||
#ifdef HID_RECV_VIDEO_FILE
|
||||
if (!g_hid_vb.active_lock) {
|
||||
if (!g_hid_vb.vbuf.num_planes) {
|
||||
pr_err("Must init HID vb first!\n");
|
||||
return;
|
||||
}
|
||||
@@ -398,10 +407,8 @@ static void dump_to_buf(uint8_t *data, uint32_t len)
|
||||
if (g_hid_need_wr_file) {
|
||||
/* Need switch to the next buf */
|
||||
if (g_hid_img_size) { /* Be sure it's not the first pkt */
|
||||
aicos_mutex_take(g_hid_vb.active_lock, AICOS_WAIT_FOREVER);
|
||||
hid_buf_done();
|
||||
hid_buf_update();
|
||||
aicos_mutex_give(g_hid_vb.active_lock);
|
||||
}
|
||||
max_len = HID_VBUF_PLANE_SIZE;
|
||||
}
|
||||
@@ -454,7 +461,6 @@ static void dump_to_buf(uint8_t *data, uint32_t len)
|
||||
|
||||
static void usbd_hid_custom_in_callback(uint8_t ep, uint32_t nbytes)
|
||||
{
|
||||
usbd_ep_start_write(HIDRAW_IN_EP, send_buffer, nbytes);
|
||||
custom_state = HID_STATE_IDLE;
|
||||
}
|
||||
|
||||
@@ -536,9 +542,7 @@ int decode_pic(uint8_t* pic, int len, u32 offset_x, u32 offset_y,
|
||||
#ifdef HID_RECV_VIDEO_FILE
|
||||
static void hid_vb_buf_queue(struct vb_buffer *vb)
|
||||
{
|
||||
aicos_mutex_take(g_hid_vb.active_lock, AICOS_WAIT_FOREVER);
|
||||
list_add_tail(&vb->active_entry, &g_hid_vb.active_list);
|
||||
aicos_mutex_give(g_hid_vb.active_lock);
|
||||
vb->hw_using = 0;
|
||||
}
|
||||
|
||||
@@ -549,13 +553,11 @@ static int hid_vb_start_streaming(struct vb_queue *q)
|
||||
pr_debug("Start streaming\n");
|
||||
|
||||
g_hid_vb.sequence = 0;
|
||||
aicos_mutex_take(g_hid_vb.active_lock, AICOS_WAIT_FOREVER);
|
||||
|
||||
/* Prepare active_buffers for HID recv*/
|
||||
vb = list_first_entry(&g_hid_vb.active_list, struct vb_buffer, active_entry);
|
||||
hid_buf_reload(vb);
|
||||
|
||||
aicos_mutex_give(g_hid_vb.active_lock);
|
||||
g_hid_vb.streaming = 1;
|
||||
return 0;
|
||||
}
|
||||
@@ -564,12 +566,10 @@ static void hid_vb_reclaim_all_buffers(enum vb_buffer_state state)
|
||||
{
|
||||
struct vb_buffer *vb, *node;
|
||||
|
||||
aicos_mutex_take(g_hid_vb.active_lock, AICOS_WAIT_FOREVER);
|
||||
list_for_each_entry_safe(vb, node, &g_hid_vb.active_list, active_entry) {
|
||||
vin_vb_buffer_done(vb, state);
|
||||
list_del(&vb->active_entry);
|
||||
}
|
||||
aicos_mutex_give(g_hid_vb.active_lock);
|
||||
}
|
||||
|
||||
static void hid_vb_stop_streaming(struct vb_queue *q)
|
||||
@@ -577,9 +577,7 @@ static void hid_vb_stop_streaming(struct vb_queue *q)
|
||||
pr_debug("Stopping capture\n");
|
||||
|
||||
/* Release all active buffers */
|
||||
aicos_mutex_take(g_hid_vb.active_lock, AICOS_WAIT_FOREVER);
|
||||
hid_vb_reclaim_all_buffers(VB_BUF_STATE_ERROR);
|
||||
aicos_mutex_give(g_hid_vb.active_lock);
|
||||
g_hid_vb.streaming = 0;
|
||||
}
|
||||
|
||||
@@ -594,11 +592,10 @@ static int hid_vb_init(void)
|
||||
struct vin_video_buf *vbuf = &g_hid_vb.vbuf;
|
||||
int i, ret = 0;
|
||||
|
||||
g_hid_vb.active_lock = aicos_mutex_create();
|
||||
INIT_LIST_HEAD(&g_hid_vb.active_list);
|
||||
vin_vb_init(&g_hid_vb.queue, &hid_vb_ops);
|
||||
|
||||
vbuf->num_planes = HID_VBUF_PLANE_NUM;
|
||||
vbuf->num_planes = HID_VBUF_PLANE_NUM;
|
||||
vbuf->planes[0].len = HID_VBUF_PLANE_SIZE;
|
||||
ret = vin_vb_req_buf(&g_hid_vb.queue,
|
||||
(char *)g_hid_buf_poll, HID_RECV_BUF_SIZE, vbuf);
|
||||
@@ -670,7 +667,7 @@ static void hid_dec_thread(void *arg)
|
||||
/* 2. Write the buffer to a file */
|
||||
if (fd < 0) { /* Need create a new file */
|
||||
pr_info("Create a new file: %s\n", HID_FILE_FOR_SAVE);
|
||||
fd = open(HID_FILE_FOR_SAVE, O_CREAT|O_WRONLY|O_BINARY);
|
||||
fd = open(HID_FILE_FOR_SAVE, O_CREAT|O_WRONLY|O_BINARY|O_TRUNC);
|
||||
if (fd < 0) {
|
||||
pr_err("Failed to open(%s)\n", HID_FILE_FOR_SAVE);
|
||||
return;
|
||||
@@ -695,11 +692,11 @@ static void hid_dec_thread(void *arg)
|
||||
/* 3. Release/Queue the buffer */
|
||||
vin_vb_q_buf(&g_hid_vb.queue, index);
|
||||
#else
|
||||
printf("\nWaiting for %d/%d file ...\n", cnt + 1, max);
|
||||
printf("\nWaiting for %d/%d file ...\n", (int)cnt + 1, (int)max);
|
||||
if (rt_sem_take(g_hid_sem, RT_WAITING_FOREVER) != RT_EOK)
|
||||
break;
|
||||
|
||||
printf("Try to decode and display the file ...\n");
|
||||
printf("Try to decode and display the picture ...\n");
|
||||
if (!g_hid_need_wr_file) {
|
||||
if (cnt % 5 == 0) {
|
||||
layer_id = AICFB_LAYER_TYPE_VIDEO;
|
||||
@@ -715,7 +712,7 @@ static void hid_dec_thread(void *arg)
|
||||
width = 0;
|
||||
height = 0;
|
||||
}
|
||||
printf("Layer %d: Offset (%d, %d), Size %d x %d\n", layer_id,
|
||||
printf("Layer %d: Offset (%d, %d), Size %d x %d\n", (int)layer_id,
|
||||
offset_x, offset_y, width, height);
|
||||
decode_pic(g_hid_img_buf, g_hid_img_size, offset_x, offset_y,
|
||||
width, height, layer_id);
|
||||
@@ -778,6 +775,25 @@ int cmd_test_usbd_hid_custom(int argc, char **argv)
|
||||
}
|
||||
MSH_CMD_EXPORT_ALIAS(cmd_test_usbd_hid_custom, test_usbd_hid_custom, Receive and decode a file);
|
||||
|
||||
static int test_usbd_hid_keyboard(int argc, char **argv)
|
||||
{
|
||||
uint8_t buf[8] = {0x00, 0x00, HID_KBD_USAGE_A, 0x00, 0x01, 0x03, 0x05, 0x07};
|
||||
|
||||
memcpy(send_buffer, buf, 8);
|
||||
int ret = usbd_ep_start_write(HIDRAW_IN_EP, send_buffer, 8);
|
||||
if (ret < 0) {
|
||||
pr_err("Failed to write IN_EP, return %d\n", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
custom_state = HID_STATE_BUSY;
|
||||
while (custom_state == HID_STATE_BUSY) {
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
MSH_CMD_EXPORT_ALIAS(test_usbd_hid_keyboard, test_usbd_hid_keyboard, test USB device HID keyboard);
|
||||
|
||||
#endif // end of #if defined(RT_USING_FINSH)
|
||||
|
||||
#endif // end of #if defined(KERNEL_RTTHREAD)
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
/*
|
||||
* Copyright (c) 2022, sakumisu
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "usbd_core.h"
|
||||
#include "usbd_hid.h"
|
||||
|
||||
@@ -204,9 +210,15 @@ void usbd_event_handler(uint8_t event)
|
||||
|
||||
/*!< hid state ! Data can be sent only when state is idle */
|
||||
static volatile uint8_t hid_state = HID_STATE_IDLE;
|
||||
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[64];
|
||||
|
||||
void usbd_hid_int_callback(uint8_t ep, uint32_t nbytes)
|
||||
{
|
||||
if (hid_state == HID_STATE_BUSY) {
|
||||
memset(write_buffer, 0, 8);
|
||||
usbd_ep_start_write(HID_INT_EP, write_buffer, 8);
|
||||
}
|
||||
|
||||
hid_state = HID_STATE_IDLE;
|
||||
}
|
||||
|
||||
@@ -226,8 +238,6 @@ void hid_keyboard_init(void)
|
||||
usbd_initialize();
|
||||
}
|
||||
|
||||
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[64];
|
||||
|
||||
void hid_keyboard_test(void)
|
||||
{
|
||||
const uint8_t sendbuffer[8] = { 0x00, 0x00, HID_KBD_USAGE_A, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
@@ -210,11 +210,14 @@ int usbd_msc_sector_read(uint32_t sector, uint8_t *buffer, uint32_t length)
|
||||
int ret = -1;
|
||||
struct usbd_storage_p *usbd_storage = get_usbd_storage();
|
||||
#if defined(KERNEL_RTTHREAD)
|
||||
ret = rt_device_read(usbd_storage->dev, sector, buffer, 1);
|
||||
ret = rt_device_read(usbd_storage->dev,
|
||||
sector, buffer,
|
||||
length / usbd_storage->block_size);
|
||||
if (ret == length / usbd_storage->block_size)
|
||||
return 0;
|
||||
#else
|
||||
ret = disk_read(usbd_storage->pdrv, buffer, sector, 1);
|
||||
ret = disk_read(usbd_storage->pdrv, buffer, sector,
|
||||
length / usbd_storage->block_size);
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
@@ -224,11 +227,14 @@ int usbd_msc_sector_write(uint32_t sector, uint8_t *buffer, uint32_t length)
|
||||
int ret = -1;
|
||||
struct usbd_storage_p *usbd_storage = get_usbd_storage();
|
||||
#if defined(KERNEL_RTTHREAD)
|
||||
ret = rt_device_write(usbd_storage->dev, sector, buffer, 1);
|
||||
ret = rt_device_write(usbd_storage->dev,
|
||||
sector, buffer,
|
||||
length / usbd_storage->block_size);
|
||||
if (ret == length / usbd_storage->block_size)
|
||||
return 0;
|
||||
#else
|
||||
ret = disk_write(usbd_storage->pdrv, buffer, sector, 1);
|
||||
ret = disk_write(usbd_storage->pdrv, buffer, sector,
|
||||
length / usbd_storage->block_size);
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
@@ -355,7 +361,8 @@ static void usbd_msc_detection_thread(void *parameter)
|
||||
int usbd_msc_detection(void)
|
||||
{
|
||||
usbd_msc_tid = rt_thread_create("usbd_msc_detection", usbd_msc_detection_thread, RT_NULL,
|
||||
1536, RT_THREAD_PRIORITY_MAX - 2, 20);
|
||||
1024 + CONFIG_USBDEV_MSC_MAX_BUFSIZE,
|
||||
RT_THREAD_PRIORITY_MAX - 2, 20);
|
||||
if (usbd_msc_tid != RT_NULL)
|
||||
rt_thread_startup(usbd_msc_tid);
|
||||
else
|
||||
|
||||
256
packages/third-party/cherryusb/demo/mtp_template.c
vendored
256
packages/third-party/cherryusb/demo/mtp_template.c
vendored
@@ -1,3 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2023, Artinchip Technology Co., Ltd
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include "usbd_core.h"
|
||||
#include "usbd_mtp.h"
|
||||
|
||||
@@ -38,20 +43,55 @@ __ALIGN_BEGIN const uint8_t WINUSB_WCIDDescriptor[40] __ALIGN_END = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* bReserved_6 */
|
||||
};
|
||||
|
||||
__ALIGN_BEGIN const uint8_t WINUSB_IF0_WCIDProperties[142] __ALIGN_END = {
|
||||
///////////////////////////////////////
|
||||
/// WCID property descriptor
|
||||
///////////////////////////////////////
|
||||
0x8e, 0x00, 0x00, 0x00, /* dwLength */
|
||||
0x00, 0x01, /* bcdVersion */
|
||||
0x05, 0x00, /* wIndex */
|
||||
0x01, 0x00, /* wCount */
|
||||
|
||||
///////////////////////////////////////
|
||||
/// registry propter descriptor
|
||||
///////////////////////////////////////
|
||||
0x84, 0x00, 0x00, 0x00, /* dwSize */
|
||||
0x01, 0x00, 0x00, 0x00, /* dwPropertyDataType */
|
||||
0x28, 0x00, /* wPropertyNameLength */
|
||||
/* DeviceInterfaceGUID */
|
||||
'D', 0x00, 'e', 0x00, 'v', 0x00, 'i', 0x00, /* wcName_20 */
|
||||
'c', 0x00, 'e', 0x00, 'I', 0x00, 'n', 0x00, /* wcName_20 */
|
||||
't', 0x00, 'e', 0x00, 'r', 0x00, 'f', 0x00, /* wcName_20 */
|
||||
'a', 0x00, 'c', 0x00, 'e', 0x00, 'G', 0x00, /* wcName_20 */
|
||||
'U', 0x00, 'I', 0x00, 'D', 0x00, 0x00, 0x00, /* wcName_20 */
|
||||
0x4e, 0x00, 0x00, 0x00, /* dwPropertyDataLength */
|
||||
|
||||
/* {1D4B2365-4749-48EA-B38A-7C6FDDDD7E26} */
|
||||
'{', 0x00, '1', 0x00, 'D', 0x00, '4', 0x00, /* wcData_39 */
|
||||
'B', 0x00, '2', 0x00, '3', 0x00, '6', 0x00, /* wcData_39 */
|
||||
'5', 0x00, '-', 0x00, '4', 0x00, '7', 0x00, /* wcData_39 */
|
||||
'4', 0x00, '9', 0x00, '-', 0x00, '4', 0x00, /* wcData_39 */
|
||||
'8', 0x00, 'E', 0x00, 'A', 0x00, '-', 0x00, /* wcData_39 */
|
||||
'B', 0x00, '3', 0x00, '8', 0x00, 'A', 0x00, /* wcData_39 */
|
||||
'-', 0x00, '7', 0x00, 'C', 0x00, '6', 0x00, /* wcData_39 */
|
||||
'F', 0x00, 'D', 0x00, 'D', 0x00, 'D', 0x00, /* wcData_39 */
|
||||
'D', 0x00, '7', 0x00, 'E', 0x00, '2', 0x00, /* wcData_39 */
|
||||
'6', 0x00, '}', 0x00, 0x00, 0x00, /* wcData_39 */
|
||||
|
||||
};
|
||||
|
||||
|
||||
struct usb_msosv1_descriptor msosv1_desc = {
|
||||
.string = (uint8_t *)WCID_StringDescriptor_MSOS,
|
||||
.string_len = 18,
|
||||
.vendor_code = WCID_VENDOR_CODE,
|
||||
.compat_id = (uint8_t *)WINUSB_WCIDDescriptor,
|
||||
.compat_id_len = sizeof(WINUSB_WCIDDescriptor),
|
||||
.comp_id_property = NULL,
|
||||
.comp_id_property_len = 0,
|
||||
.comp_id_property = (const uint8_t **)&WINUSB_IF0_WCIDProperties,
|
||||
};
|
||||
|
||||
/*!< endpoint address */
|
||||
#define CDC_IN_EP 0x81
|
||||
#define CDC_OUT_EP 0x02
|
||||
#define CDC_INT_EP 0x83
|
||||
#define MTP_IN_EP 0x81
|
||||
#define MTP_OUT_EP 0x02
|
||||
#define MTP_INT_EP 0x83
|
||||
|
||||
#define USBD_VID 0x33C3
|
||||
#define USBD_PID 0xFFFF
|
||||
@@ -70,7 +110,7 @@ struct usb_msosv1_descriptor msosv1_desc = {
|
||||
const uint8_t mtp_descriptor[] = {
|
||||
USB_DEVICE_DESCRIPTOR_INIT(USB_2_1, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0201, 0x01),
|
||||
USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
|
||||
MTP_DESCRIPTOR_INIT(0x00, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, MTP_MAX_MPS, 0x02),
|
||||
MTP_DESCRIPTOR_INIT(0x00, MTP_OUT_EP, MTP_IN_EP, MTP_INT_EP, MTP_MAX_MPS, 0x02),
|
||||
///////////////////////////////////////
|
||||
/// string0 descriptor
|
||||
///////////////////////////////////////
|
||||
@@ -145,7 +185,7 @@ const uint8_t mtp_descriptor[] = {
|
||||
0x00
|
||||
};
|
||||
|
||||
const uint8_t bos_descriptor[] = {
|
||||
uint8_t bos_descriptor[] = {
|
||||
0x05, 0x0f, 0x16, 0x00, 0x02,
|
||||
0x07, 0x10, 0x02, 0x06, 0x00, 0x00, 0x00,
|
||||
0x0a, 0x10, 0x03, 0x00, 0x0f, 0x00, 0x01, 0x01, 0xf4, 0x01
|
||||
@@ -176,6 +216,21 @@ void usbd_event_handler(uint8_t event)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <string.h>
|
||||
#include "ff.h" /* Declarations of FatFs API */
|
||||
/* ELM FatFs provide a DIR struct */
|
||||
#define HAVE_DIR_STRUCTURE
|
||||
#include <dfs_fs.h>
|
||||
#include <dfs_file.h>
|
||||
#include <dfs_elm.h>
|
||||
#include "diskio.h" /* Declarations of device I/O functions */
|
||||
#if defined(KERNEL_RTTHREAD)
|
||||
#include "rtthread.h"
|
||||
#else
|
||||
#include "dfs_bare.h"
|
||||
#endif
|
||||
struct usbd_interface intf0;
|
||||
|
||||
struct usb_bos_descriptor bos_desc = {
|
||||
@@ -183,11 +238,190 @@ struct usb_bos_descriptor bos_desc = {
|
||||
.string_len = 22
|
||||
};
|
||||
|
||||
struct usbd_mtp_fs_p {
|
||||
struct dfs_filesystem *fs;
|
||||
struct statfs stat; /* 1. block size 2. total data blocks in file system 3. free blocks in file system*/
|
||||
char fs_path[10];
|
||||
char fs_type[5];
|
||||
};
|
||||
|
||||
struct usbd_mtp_fs_p usbd_mtp_fs;
|
||||
struct usbd_mtp_fs_p *get_usbd_mtp_fs(void)
|
||||
{
|
||||
return &usbd_mtp_fs;
|
||||
}
|
||||
|
||||
int usbd_mtp_get_cap(uint64_t *max_capability, uint64_t *free_space)
|
||||
{
|
||||
int ret;
|
||||
struct usbd_mtp_fs_p *usbd_mtp_fs = get_usbd_mtp_fs();
|
||||
|
||||
ret = dfs_statfs(usbd_mtp_fs->fs_path, &usbd_mtp_fs->stat);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
*max_capability = (uint64_t)usbd_mtp_fs->stat.f_bsize * (uint64_t)usbd_mtp_fs->stat.f_blocks;
|
||||
*free_space = (uint64_t)usbd_mtp_fs->stat.f_bsize * (uint64_t)usbd_mtp_fs->stat.f_bfree;
|
||||
|
||||
USB_LOG_DBG("max_capability:%lld free_space:%lld\n", *max_capability, *free_space);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include "stdio.h"
|
||||
#include "fcntl.h"
|
||||
#include "dirent.h"
|
||||
#include "unistd.h"
|
||||
/* directory operation */
|
||||
int usbd_mtp_make_dir(const char *path)
|
||||
{
|
||||
return mkdir(path, 0);
|
||||
}
|
||||
|
||||
int usbd_mtp_remove_dir(const char *path)
|
||||
{
|
||||
return rmdir(path);
|
||||
}
|
||||
|
||||
void *usbd_mtp_open_dir(const char *path)
|
||||
{
|
||||
return opendir(path);
|
||||
}
|
||||
|
||||
int usbd_mtp_close_dir(void *d)
|
||||
{
|
||||
DIR *dir = (DIR *)d;
|
||||
return closedir(dir);
|
||||
}
|
||||
|
||||
int usbd_mtp_get_file_info(void *d,
|
||||
char *file_name,
|
||||
uint8_t *file_name_len,
|
||||
uint32_t *protection_status)
|
||||
{
|
||||
DIR *dir = (DIR *)d;
|
||||
struct dirent *dirent;
|
||||
|
||||
dirent = readdir(dir);
|
||||
if (dirent == NULL)
|
||||
return 0;
|
||||
|
||||
*file_name_len = dirent->d_namlen;
|
||||
strcpy(file_name, dirent->d_name);
|
||||
*protection_status = 0;
|
||||
|
||||
return 1; /* if still read the file, please return 1 */
|
||||
}
|
||||
|
||||
/* directory operation */
|
||||
int usbd_mtp_creat_file(const char *path)
|
||||
{
|
||||
return creat(path, O_RDWR);
|
||||
}
|
||||
|
||||
int usbd_mtp_unlink_file(const char *pathname)
|
||||
{
|
||||
return unlink(pathname);
|
||||
}
|
||||
|
||||
int usbd_mtp_open_file_rdonly(const char *file)
|
||||
{
|
||||
return open(file, O_RDONLY);
|
||||
}
|
||||
|
||||
int usbd_mtp_open_file_wronly(const char *file)
|
||||
{
|
||||
return open(file, O_WRONLY);
|
||||
}
|
||||
|
||||
int usbd_mtp_close_file(int fd)
|
||||
{
|
||||
return close(fd);
|
||||
}
|
||||
|
||||
int usbd_mtp_read_file(int fd, void *buf, size_t len)
|
||||
{
|
||||
return read(fd, (char *)buf, len);
|
||||
}
|
||||
|
||||
int usbd_mtp_write_file(int fd, const void *buf, size_t len)
|
||||
{
|
||||
return write(fd, buf, len);
|
||||
}
|
||||
|
||||
uint32_t usbd_mtp_get_file_size(const char *file)
|
||||
{
|
||||
struct stat s;
|
||||
if(stat(file, &s) < 0)
|
||||
return -1;
|
||||
|
||||
return s.st_size;
|
||||
}
|
||||
|
||||
int usbd_mtp_get_fullpath(char *buf, const char *directory, const char *filename)
|
||||
{
|
||||
char *fullpath;
|
||||
fullpath = dfs_normalize_path(directory, filename);
|
||||
strcpy(buf, fullpath);
|
||||
rt_free(fullpath);
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern struct dfs_filesystem filesystem_table[];
|
||||
|
||||
int usbd_mtp_fs_init(char *path)
|
||||
{
|
||||
struct dfs_filesystem *iter;
|
||||
struct usbd_mtp_fs_p *usbd_mtp_fs = get_usbd_mtp_fs();
|
||||
|
||||
for (iter = &filesystem_table[0];
|
||||
iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++) {
|
||||
if ((iter != NULL) && (iter->path != NULL)) {
|
||||
if (strcmp(iter->path, path) == 0)
|
||||
goto __init;
|
||||
}
|
||||
}
|
||||
USB_LOG_WRN("Invalid file system!\n");
|
||||
return -1;
|
||||
__init:
|
||||
usbd_mtp_fs->fs = dfs_filesystem_lookup(path);
|
||||
strcpy(usbd_mtp_fs->fs_type, iter->ops->name);
|
||||
strcpy(usbd_mtp_fs->fs_path, iter->path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mtp_init(void)
|
||||
{
|
||||
|
||||
usbd_mtp_fs_init(ROOT_PATH);
|
||||
usbd_desc_register(mtp_descriptor);
|
||||
usbd_msosv1_desc_register(&msosv1_desc);
|
||||
usbd_bos_desc_register(&bos_desc);
|
||||
usbd_add_interface(usbd_mtp_init_intf(&intf0, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP));
|
||||
usbd_add_interface(usbd_mtp_init_intf(&intf0, MTP_OUT_EP, MTP_IN_EP, MTP_INT_EP));
|
||||
usbd_initialize();
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(KERNEL_RTTHREAD)
|
||||
rt_thread_t usbd_mtp_tid;
|
||||
|
||||
static void usbd_mtp_detection_thread(void *parameter)
|
||||
{
|
||||
// waiting to mount the correspongding file system
|
||||
rt_thread_mdelay(800);
|
||||
mtp_init();
|
||||
|
||||
}
|
||||
|
||||
int usbd_mtp_detection(void)
|
||||
{
|
||||
usbd_mtp_tid = rt_thread_create("usbd_mtp_detection", usbd_mtp_detection_thread, RT_NULL,
|
||||
1536, RT_THREAD_PRIORITY_MAX - 2, 20);
|
||||
if (usbd_mtp_tid != RT_NULL)
|
||||
rt_thread_startup(usbd_mtp_tid);
|
||||
else
|
||||
printf("create usbd_mtp_detection thread err!\n");
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
INIT_APP_EXPORT(usbd_mtp_detection);
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2022, Artinchip Technology Co., Ltd
|
||||
* Copyright (c) 2022, ArtInChip Technology Co., Ltd
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
*/
|
||||
|
||||
#include <rtconfig.h>
|
||||
@@ -11,6 +12,44 @@
|
||||
struct usbh_msc *active_msc_class;
|
||||
struct dfs_partition part0;
|
||||
|
||||
#define USING_AIC_GET_PART
|
||||
|
||||
#ifdef USING_AIC_GET_PART
|
||||
#include <disk_part.h>
|
||||
#include <dfs_fs.h>
|
||||
static unsigned long usb_msc_read(struct blk_desc *blk_dev, u64 start, u64 blkcnt,
|
||||
const void *buffer)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = usbh_msc_scsi_read10(active_msc_class, start, buffer, blkcnt);
|
||||
if (err == RT_EOK)
|
||||
return blkcnt;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int aic_get_part(struct dfs_partition *part)
|
||||
{
|
||||
struct blk_desc dev_desc = {0};
|
||||
struct disk_blk_ops ops = {0};
|
||||
struct aic_partition *parts = NULL;
|
||||
ops.blk_read = usb_msc_read;
|
||||
dev_desc.blksz = active_msc_class->blocksize;
|
||||
dev_desc.lba_count = active_msc_class->blocknum;
|
||||
|
||||
aic_disk_part_set_ops(&ops);
|
||||
parts = aic_disk_get_gpt_parts(&dev_desc);
|
||||
|
||||
if (parts) {
|
||||
part->type = 0;
|
||||
part->offset = parts->start / dev_desc.blksz;
|
||||
part->size = parts->size / dev_desc.blksz;
|
||||
aic_part_free(parts);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef KERNEL_RTTHREAD
|
||||
#include <dfs_fs.h>
|
||||
|
||||
@@ -103,32 +142,34 @@ int udisk_init(void)
|
||||
|
||||
/* get the first sector to read partition table */
|
||||
sector = (rt_uint8_t *)rt_malloc(512);
|
||||
if (sector == RT_NULL)
|
||||
{
|
||||
if (sector == RT_NULL) {
|
||||
pr_err("allocate partition sector buffer failed!");
|
||||
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
ret = usbh_msc_scsi_read10(active_msc_class, 0, sector, 1);
|
||||
if (ret != RT_EOK)
|
||||
{
|
||||
if (ret != RT_EOK) {
|
||||
rt_kprintf("usb mass_storage read failed\n");
|
||||
goto free_res;;
|
||||
goto free_res;
|
||||
}
|
||||
|
||||
memset(&part0, 0, sizeof(part0));
|
||||
|
||||
for (i=0; i<16; i++) {
|
||||
/* Get the first partition */
|
||||
/* Get the first partition (MBR)*/
|
||||
ret = dfs_filesystem_get_partition(&part0, sector, i);
|
||||
if (ret == RT_EOK) {
|
||||
pr_info("Found partition %d: type = %d, offet=0x%x, size=0x%x\n",
|
||||
pr_info("Found partition %d: type = %d, offet=0x%lx, size=0x%x\n",
|
||||
i, part0.type, part0.offset, part0.size);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USING_AIC_GET_PART
|
||||
aic_get_part(&part0);
|
||||
#endif
|
||||
|
||||
udisk_dev.type = RT_Device_Class_Block;
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
udisk_dev.ops = &udisk_device_ops;
|
||||
@@ -150,12 +191,9 @@ int udisk_init(void)
|
||||
#else
|
||||
int ret = 0;
|
||||
ret = dfs_mount(udisk_dev.parent.name, "/", "elm", 0, 0);
|
||||
if (ret == 0)
|
||||
{
|
||||
if (ret == 0) {
|
||||
printf("udisk mount successfully\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
printf("udisk mount failed, ret = %d\n", ret);
|
||||
}
|
||||
#endif
|
||||
@@ -275,16 +313,14 @@ int udisk_init(void)
|
||||
|
||||
/* get the first sector to read partition table */
|
||||
sector = (uint8_t *)aicos_malloc(0, 512);
|
||||
if (sector == NULL)
|
||||
{
|
||||
if (sector == NULL) {
|
||||
pr_err("allocate partition sector buffer failed!");
|
||||
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
ret = usbh_msc_scsi_read10(active_msc_class, 0, sector, 1);
|
||||
if (ret != EOK)
|
||||
{
|
||||
if (ret != EOK) {
|
||||
pr_err("usb mass_storage read failed\n");
|
||||
goto free_res;;
|
||||
}
|
||||
@@ -301,6 +337,10 @@ int udisk_init(void)
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USING_AIC_GET_PART
|
||||
aic_get_part(&part0);
|
||||
#endif
|
||||
|
||||
if (dfs_mount("udisk", "/udisk", "elm", 0, DEVICE_TYPE_USB_DISK) < 0) {
|
||||
pr_err("Failed to mount udisk with FatFS\n");
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user