Skip to content

Commit c164410

Browse files
committed
Use USB MaxPacketSize
Use USB MaxPacketSize to save memory for full speed devices
1 parent 5bb4761 commit c164410

File tree

4 files changed

+15
-29
lines changed

4 files changed

+15
-29
lines changed

samples/subsys/usb/mtp/app.overlay

Lines changed: 0 additions & 17 deletions
This file was deleted.

subsys/usb/device_next/class/usbd_mtp.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,13 @@ static int usbd_mtp_init(struct usbd_class_data *c_data)
313313

314314
device_version = "1.0";
315315

316-
LOG_DBG("Desc data: Manufacturer: %s, Product: %s, SN: %s",
316+
mtp_ctx->max_packet_size = usbd_desc->bMaxPacketSize0;
317+
318+
LOG_DBG("Desc data: Manufacturer: %s, Product: %s, SN: %s, PktSize: %u",
317319
manufacturer,
318320
model,
319-
d_nd->bLength > 0 ? serial_number : "NULL");
321+
d_nd->bLength > 0 ? serial_number : "NULL",
322+
mtp_ctx->max_packet_size);
320323

321324
mtp_init(mtp_ctx, manufacturer, model, device_version, serial_number);
322325

subsys/usb/device_next/class/usbd_mtp_class.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ LOG_MODULE_REGISTER(usb_mtp_impl, CONFIG_USBD_MTP_LOG_LEVEL);
113113

114114
/* MACROS */
115115
#define MAX_FILES CONFIG_USBD_MTP_MAX_HANDLES
116-
#define MAX_PACKET_SIZE 512 /* Get it in a usb complaint way */
117116

118117
#define INTERNAL_STORAGE_ID(id) (0x00010000 + id)
119118
#define REMOVABLE_STORAGE_ID(id) (0x00020000 + id)
@@ -844,12 +843,12 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT_INFO)
844843
static int continue_get_object(struct mtp_context* ctx, struct net_buf *buf)
845844
{
846845
int len = 0;
847-
int total_chunks = (ctx->filestate.total_size / MAX_PACKET_SIZE);
846+
int total_chunks = (ctx->filestate.total_size / ctx->max_packet_size);
848847

849848
memset(ctx->filebuf, 0x00, 512);
850849

851850
if (ctx->filestate.transferred < ctx->filestate.total_size) {
852-
len = MIN(MAX_PACKET_SIZE,
851+
len = MIN(ctx->max_packet_size,
853852
(ctx->filestate.total_size - ctx->filestate.transferred));
854853

855854
int read = fs_read(&ctx->filestate.file, ctx->filebuf, len);
@@ -920,7 +919,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT)
920919
}
921920

922921
uint32_t filesize = storage[storage_id].filelist[object_id].size;
923-
uint32_t available_buf_len = MAX_PACKET_SIZE - sizeof(struct mtp_header);
922+
uint32_t available_buf_len = ctx->max_packet_size - sizeof(struct mtp_header);
924923

925924
LOG_DBG("Sending file: %s size: %u", path, storage[storage_id].filelist[object_id].size);
926925

subsys/usb/device_next/class/usbd_mtp_class.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313

1414
struct mtp_context {
1515
bool session_opened;
16-
uint8_t filebuf[512]; /* TODO: should match USB Packet size */
16+
uint16_t max_packet_size;
17+
uint8_t filebuf[512]; /* Use USB HS MPS */
18+
uint16_t cmd;
19+
20+
bool more_data_needed;
21+
int (*extra_data_fn)(struct mtp_context* ctx, struct net_buf *buf_in);
22+
int (*pending_fn)(struct mtp_context* ctx, struct net_buf *buf);
1723
struct {
1824
struct fs_file_t file;
1925
char filepath[MAX_PATH_LEN];
@@ -23,11 +29,6 @@ struct mtp_context {
2329
uint32_t storage_id;
2430
} filestate;
2531

26-
uint16_t cmd;
27-
uint16_t cmd_result;
28-
bool more_data_needed;
29-
int (*extra_data_fn)(struct mtp_context* ctx, struct net_buf *buf_in);
30-
int (*pending_fn)(struct mtp_context* ctx, struct net_buf *buf);
3132
};
3233

3334

0 commit comments

Comments
 (0)