Skip to content

Commit 0b99ded

Browse files
committed
USB: Device_next: Add USB MTP class support
Implement a basic version of USB MTP (Media transfer protocol) class which support the necessary MTP commands to handle Dir/file transfer between a device and host. Signed-off-by: Mohamed ElShahawi <[email protected]>
1 parent b8541c5 commit 0b99ded

File tree

11 files changed

+2071
-0
lines changed

11 files changed

+2071
-0
lines changed

dts/bindings/fs/zephyr,fstab-common.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ properties:
4343
4444
This causes the FS_MOUNT_FLAG_USE_DISK_ACCESS option to be set in
4545
the mount descriptor generated for the file system.
46+
47+
mtp-enabled:
48+
type: boolean
49+
description: |
50+
Provide File system access over USB MTP Protocol

include/zephyr/usb/usb_ch9.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ struct usb_association_descriptor {
264264
#define USB_BCC_AUDIO 0x01
265265
#define USB_BCC_CDC_CONTROL 0x02
266266
#define USB_BCC_HID 0x03
267+
#define USB_BCC_IMAGE 0x06
267268
#define USB_BCC_MASS_STORAGE 0x08
268269
#define USB_BCC_CDC_DATA 0x0A
269270
#define USB_BCC_VIDEO 0x0E

samples/subsys/usb/mtp/prj.conf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CONFIG_USB_DEVICE_STACK_NEXT=y
2+
CONFIG_USBD_MTP_CLASS=y
3+
4+
CONFIG_SAMPLE_USBD_PRODUCT="USBD MTP sample"
5+
CONFIG_SAMPLE_USBD_PID=0x0009
6+
7+
CONFIG_LOG=y
8+
CONFIG_USBD_LOG_LEVEL_WRN=y
9+
CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y
10+
CONFIG_USBD_MTP_LOG_LEVEL_ERR=y
11+
12+
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n
13+
14+
CONFIG_FLASH=y
15+
CONFIG_FLASH_MAP=y
16+
17+
CONFIG_FILE_SYSTEM=y
18+
CONFIG_FILE_SYSTEM_LITTLEFS=y
19+
20+
CONFIG_USBD_THREAD_STACK_SIZE=4096

subsys/usb/device_next/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,10 @@ zephyr_linker_sources_ifdef(
104104
SECTIONS class/usbd_dfu.ld
105105
)
106106

107+
zephyr_library_sources_ifdef(
108+
CONFIG_USBD_MTP_CLASS
109+
class/usbd_mtp.c
110+
class/usbd_mtp_class.c
111+
)
112+
107113
zephyr_linker_sources(DATA_SECTIONS usbd_data.ld)

subsys/usb/device_next/class/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ rsource "Kconfig.hid"
1313
rsource "Kconfig.midi2"
1414
rsource "Kconfig.dfu"
1515
rsource "Kconfig.uvc"
16+
rsource "Kconfig.mtp"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright (c) 2025 Mohamed ElShahawi ([email protected])
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
config USBD_MTP_CLASS
6+
bool "USB MTP Class"
7+
help
8+
USB Media Transfer Protocol Device Class support.
9+
Allow file transfer independent of storage file system.
10+
11+
if USBD_MTP_CLASS
12+
13+
config USBD_MTP_MAX_HANDLES
14+
int "Max number of Object handles per Storage"
15+
default 10
16+
help
17+
Maximum number of objects to be handled by MTP, including
18+
stored, removed and added objects.
19+
20+
config RECYCLE_OBJECT_HANDLES
21+
bool "Recycle Object Handles"
22+
default n
23+
help
24+
Recycle object handles when the maximum number of objects is reached.
25+
If disabled, new objects will not be added when the maximum number of
26+
objects is reached. Although this option provide better usability,
27+
it is against the MTP specification.
28+
29+
module = USBD_MTP
30+
module-str = usbd mtp
31+
default-count = 1
32+
source "subsys/logging/Kconfig.template.log_config"
33+
34+
rsource "Kconfig.template.instances_count"
35+
endif

0 commit comments

Comments
 (0)