@@ -133,6 +133,18 @@ LOG_MODULE_REGISTER(usb_mtp_impl, CONFIG_USBD_MTP_LOG_LEVEL);
133
133
IF_ENABLED(DT_PROP(node_id, mtp_enabled), \
134
134
({.mountpoint = DT_PROP(node_id, mount_point), .files_count = 1, .read_only = DT_PROP(node_id, read_only)},))
135
135
136
+ /* Used by MTP commands which reply to host over multiple packets
137
+ * like GetObject (Sending file to host) or a command sending its result
138
+ * in a separate packet
139
+ */
140
+ #define DEV_TO_HOST_SET_PENDING_PACKET (ctx , fn ) ctx->pending_fn = fn
141
+
142
+ /* Used by MTP commands which receive data from host over multiple packets
143
+ * like SendObject (Receiving file from host) or a command receiving all required
144
+ * info over multiple packets like SendObjectInfo
145
+ */
146
+ #define HOST_TO_DEV_SET_CONT_DATA_HANDLER (ctx , fn ) ctx->extra_data_fn = fn
147
+
136
148
/* Types */
137
149
enum mtp_container_type {
138
150
MTP_CONTAINER_UNDEFINED = 0x00 ,
@@ -422,6 +434,19 @@ const char *mtp_code_to_string(uint16_t code)
422
434
return str ;
423
435
}
424
436
#endif
437
+ int mtp_get_pending_packet (struct mtp_context * ctx , struct net_buf * buf )
438
+ {
439
+ if (ctx -> pending_fn ) {
440
+ return ctx -> pending_fn (ctx , buf );
441
+ } else {
442
+ return - EINVAL ;
443
+ }
444
+ }
445
+
446
+ bool mtp_packet_pending (struct mtp_context * ctx )
447
+ {
448
+ return (ctx -> pending_fn != NULL );
449
+ }
425
450
426
451
static void net_buf_add_utf16le (struct net_buf * buf , const char * str )
427
452
{
@@ -443,20 +468,6 @@ static void net_buf_pull_utf16le(struct net_buf *buf, char *strbuf, size_t len)
443
468
}
444
469
}
445
470
446
- int mtp_get_pending_packet (struct mtp_context * ctx , struct net_buf * buf )
447
- {
448
- if (ctx -> pending_fn ) {
449
- return ctx -> pending_fn (ctx , buf );
450
- } else {
451
- return - EINVAL ;
452
- }
453
- }
454
-
455
- bool mtp_packet_pending (struct mtp_context * ctx )
456
- {
457
- return (ctx -> pending_fn != NULL );
458
- }
459
-
460
471
static void data_header_push (struct net_buf * buf , struct mtp_container * mtp_command , uint32_t data_len )
461
472
{
462
473
/* DATA Block Header */
@@ -627,7 +638,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_DEVICE_INFO)
627
638
/* Add the Packet Header */
628
639
data_header_push (buf , mtp_command , buf -> len );
629
640
630
- ctx -> pending_fn = mtp_send_confirmation ;
641
+ DEV_TO_HOST_SET_PENDING_PACKET ( ctx , mtp_send_confirmation ) ;
631
642
}
632
643
633
644
MTP_CMD_HANDLER (MTP_OP_OPEN_SESSION )
@@ -712,7 +723,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_STORAGE_INFO)
712
723
/* Add the Packet Header */
713
724
data_header_push (buf , mtp_command , buf -> len );
714
725
715
- ctx -> pending_fn = mtp_send_confirmation ;
726
+ DEV_TO_HOST_SET_PENDING_PACKET ( ctx , mtp_send_confirmation ) ;
716
727
}
717
728
718
729
MTP_CMD_HANDLER (MTP_OP_GET_STORAGE_IDS )
@@ -727,7 +738,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_STORAGE_IDS)
727
738
/* Add the Packet Header */
728
739
data_header_push (buf , mtp_command , buf -> len );
729
740
730
- ctx -> pending_fn = mtp_send_confirmation ;
741
+ DEV_TO_HOST_SET_PENDING_PACKET ( ctx , mtp_send_confirmation ) ;
731
742
}
732
743
733
744
MTP_CMD_HANDLER (MTP_OP_GET_OBJECT_HANDLES )
@@ -755,7 +766,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT_HANDLES)
755
766
/* Add the Packet Header */
756
767
data_header_push (buf , mtp_command , buf -> len );
757
768
758
- ctx -> pending_fn = mtp_send_confirmation ;
769
+ DEV_TO_HOST_SET_PENDING_PACKET ( ctx , mtp_send_confirmation ) ;
759
770
}
760
771
761
772
MTP_CMD_HANDLER (MTP_OP_GET_OBJECT_INFO )
@@ -827,7 +838,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT_INFO)
827
838
/* Add the Packet Header */
828
839
data_header_push (buf , mtp_command , buf -> len );
829
840
830
- ctx -> pending_fn = mtp_send_confirmation ;
841
+ DEV_TO_HOST_SET_PENDING_PACKET ( ctx , mtp_send_confirmation ) ;
831
842
}
832
843
833
844
static int continue_get_object (struct mtp_context * ctx , struct net_buf * buf )
@@ -858,10 +869,10 @@ static int continue_get_object(struct mtp_context* ctx, struct net_buf *buf)
858
869
LOG_DBG ("Done (%u), CONFIRMING" , read );
859
870
fs_close (& ctx -> filestate .file );
860
871
memset (& ctx -> filestate , 0x00 , sizeof (ctx -> filestate ));
861
- ctx -> pending_fn = mtp_send_confirmation ;
872
+ DEV_TO_HOST_SET_PENDING_PACKET ( ctx , mtp_send_confirmation ) ;
862
873
} else {
863
874
LOG_DBG ("Continue (%u) Next" , read );
864
- ctx -> pending_fn = continue_get_object ;
875
+ DEV_TO_HOST_SET_PENDING_PACKET ( ctx , continue_get_object ) ;
865
876
}
866
877
} else {
867
878
LOG_ERR ("Shouldn't happen" );
@@ -927,7 +938,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT)
927
938
ctx -> cmd = MTP_OP_GET_OBJECT ;
928
939
ctx -> filestate .total_size = filesize ;
929
940
ctx -> filestate .transferred = read ;
930
- ctx -> pending_fn = continue_get_object ;
941
+ DEV_TO_HOST_SET_PENDING_PACKET ( ctx , continue_get_object ) ;
931
942
} else {
932
943
int read = fs_read (& ctx -> filestate .file , ctx -> filebuf , filesize );
933
944
@@ -938,7 +949,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT)
938
949
939
950
fs_close (& ctx -> filestate .file );
940
951
memset (& ctx -> filestate , 0x00 , sizeof (ctx -> filestate ));
941
- ctx -> pending_fn = mtp_send_confirmation ;
952
+ DEV_TO_HOST_SET_PENDING_PACKET ( ctx , mtp_send_confirmation ) ;
942
953
}
943
954
}
944
955
@@ -1118,12 +1129,12 @@ static int continue_send_object(struct mtp_context* ctx, struct net_buf *buf_in)
1118
1129
LOG_DBG ("Sending Confirmation after reciving data (Total len: %u)" ,
1119
1130
ctx -> filestate .transferred );
1120
1131
1121
- ctx -> extra_data_fn = NULL ;
1132
+ HOST_TO_DEV_SET_CONT_DATA_HANDLER ( ctx , NULL ) ;
1122
1133
memset (& ctx -> filestate , 0x00 , sizeof (ctx -> filestate ));
1123
- ctx -> pending_fn = mtp_send_confirmation ;
1134
+ DEV_TO_HOST_SET_PENDING_PACKET ( ctx , mtp_send_confirmation ) ;
1124
1135
return 1 ;
1125
1136
} else {
1126
- ctx -> extra_data_fn = continue_send_object ;
1137
+ HOST_TO_DEV_SET_CONT_DATA_HANDLER ( ctx , continue_send_object ) ;
1127
1138
return 0 ;
1128
1139
}
1129
1140
}
@@ -1149,10 +1160,10 @@ MTP_CMD_HANDLER(MTP_OP_SEND_OBJECT)
1149
1160
ctx -> filestate .transferred );
1150
1161
1151
1162
memset (& ctx -> filestate , 0x00 , sizeof (ctx -> filestate ));
1152
- ctx -> extra_data_fn = NULL ;
1163
+ HOST_TO_DEV_SET_CONT_DATA_HANDLER ( ctx , NULL ) ;
1153
1164
mtp_send_confirmation (ctx , buf );
1154
1165
} else {
1155
- ctx -> extra_data_fn = continue_send_object ;
1166
+ HOST_TO_DEV_SET_CONT_DATA_HANDLER ( ctx , continue_send_object ) ;
1156
1167
}
1157
1168
}
1158
1169
}
@@ -1205,7 +1216,7 @@ MTP_CMD_HANDLER(MTP_OP_GET_OBJECT_REFERENCES)
1205
1216
1206
1217
data_header_push (buf , mtp_command , buf -> len );
1207
1218
1208
- ctx -> pending_fn = mtp_send_confirmation ;
1219
+ DEV_TO_HOST_SET_PENDING_PACKET ( ctx , mtp_send_confirmation ) ;
1209
1220
}
1210
1221
1211
1222
/*
@@ -1240,6 +1251,7 @@ int mtp_commands_handler(struct mtp_context* ctx, struct net_buf *buf_in, struct
1240
1251
}
1241
1252
}
1242
1253
1254
+ ctx -> cmd = mtp_command -> hdr .code ;
1243
1255
LOG_DBG (GREEN "[%s]" RESET , mtp_code_to_string (mtp_command -> hdr .code ));
1244
1256
1245
1257
switch (mtp_command -> hdr .code ) {
@@ -1330,8 +1342,8 @@ int mtp_control_handler(struct mtp_context* ctx, uint8_t request, struct net_buf
1330
1342
memset (ctx -> filebuf , 0x00 , sizeof (ctx -> filebuf ));
1331
1343
memset (& ctx -> filestate , 0x00 , sizeof (ctx -> filestate ));
1332
1344
}
1333
- ctx -> pending_fn = NULL ;
1334
- ctx -> extra_data_fn = NULL ;
1345
+ DEV_TO_HOST_SET_PENDING_PACKET ( ctx , NULL ) ;
1346
+ HOST_TO_DEV_SET_CONT_DATA_HANDLER ( ctx , NULL ) ;
1335
1347
1336
1348
canceled = true;
1337
1349
break ;
@@ -1367,7 +1379,7 @@ static int mtp_send_confirmation(struct mtp_context* ctx, struct net_buf *buf)
1367
1379
LOG_ERR ("Failed to send MTP confirmation" );
1368
1380
}
1369
1381
1370
- ctx -> pending_fn = NULL ;
1382
+ DEV_TO_HOST_SET_PENDING_PACKET ( ctx , NULL ) ;
1371
1383
return 0 ;
1372
1384
}
1373
1385
0 commit comments