Skip to content

Commit 4ba8b1d

Browse files
andrewleechclaude
andcommitted
usb-device-mtp: Fix vendor extension and device identification.
Adds proper Microsoft MTP vendor extension ID and extension string to ensure the device is correctly identified as MTP rather than PTP. Also enhances device and storage identification with more complete manufacturer, model, version and descriptive information. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent c3f7633 commit 4ba8b1d

File tree

1 file changed

+53
-15
lines changed
  • micropython/usb/usb-device-mtp/usb/device

1 file changed

+53
-15
lines changed

micropython/usb/usb-device-mtp/usb/device/mtp.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ def _process_data_phase(self, data):
461461

462462
def _cmd_get_device_info(self):
463463
"""Handle GetDeviceInfo command."""
464+
print("[MTP] Generating device info response")
465+
464466
# Prepare the device info dataset
465467
data = bytearray(512) # Pre-allocate buffer
466468
offset = 0
@@ -470,15 +472,22 @@ def _cmd_get_device_info(self):
470472
offset += 2
471473

472474
# MTP vendor extension ID
473-
struct.pack_into("<I", data, offset, 0x00000000) # No vendor extension
475+
# Use Microsoft's extension ID to better identify as a true MTP device
476+
struct.pack_into("<I", data, offset, 0x00000006) # Microsoft MTP Extension
474477
offset += 4
475478

476479
# MTP version
477480
struct.pack_into("<H", data, offset, 100) # Version 1.00
478481
offset += 2
479482

480-
# MTP extensions (empty string)
481-
struct.pack_into("<H", data, offset, 0) # No extension string
483+
# MTP extensions description string
484+
ext_string = "microsoft.com: 1.0" # Standard Microsoft extension string
485+
struct.pack_into("<H", data, offset, len(ext_string) + 1) # String length including null terminator
486+
offset += 2
487+
for c in ext_string:
488+
struct.pack_into("<H", data, offset, ord(c))
489+
offset += 2
490+
struct.pack_into("<H", data, offset, 0) # Null terminator
482491
offset += 2
483492

484493
# Functional mode
@@ -530,12 +539,18 @@ def _cmd_get_device_info(self):
530539
struct.pack_into("<H", data, offset, fmt)
531540
offset += 2
532541

533-
# Manufacturer (empty string)
534-
struct.pack_into("<H", data, offset, 0)
542+
# Manufacturer
543+
manufacturer = "MicroPython"
544+
struct.pack_into("<H", data, offset, len(manufacturer) + 1)
545+
offset += 2
546+
for c in manufacturer:
547+
struct.pack_into("<H", data, offset, ord(c))
548+
offset += 2
549+
struct.pack_into("<H", data, offset, 0) # Null terminator
535550
offset += 2
536551

537-
# Model (MicroPython)
538-
model = "MicroPython"
552+
# Model
553+
model = "MicroPython MTP Device"
539554
struct.pack_into("<H", data, offset, len(model) + 1)
540555
offset += 2
541556
for c in model:
@@ -544,12 +559,24 @@ def _cmd_get_device_info(self):
544559
struct.pack_into("<H", data, offset, 0) # Null terminator
545560
offset += 2
546561

547-
# Device version (empty string)
548-
struct.pack_into("<H", data, offset, 0)
562+
# Device version
563+
version = "1.0"
564+
struct.pack_into("<H", data, offset, len(version) + 1)
565+
offset += 2
566+
for c in version:
567+
struct.pack_into("<H", data, offset, ord(c))
568+
offset += 2
569+
struct.pack_into("<H", data, offset, 0) # Null terminator
549570
offset += 2
550571

551-
# Serial number (empty string)
552-
struct.pack_into("<H", data, offset, 0)
572+
# Serial number
573+
serial = "MP12345" # Generic serial number
574+
struct.pack_into("<H", data, offset, len(serial) + 1)
575+
offset += 2
576+
for c in serial:
577+
struct.pack_into("<H", data, offset, ord(c))
578+
offset += 2
579+
struct.pack_into("<H", data, offset, 0) # Null terminator
553580
offset += 2
554581

555582
# Send the device info
@@ -603,7 +630,10 @@ def _cmd_get_storage_ids(self):
603630

604631
def _cmd_get_storage_info(self, params):
605632
"""Handle GetStorageInfo command."""
633+
print("[MTP] Generating storage info for storage ID: 0x{:08x}".format(params[0] if params else 0))
634+
606635
if not params or params[0] != self._storage_id:
636+
print("[MTP] Invalid storage ID requested: 0x{:08x}".format(params[0] if params else 0))
607637
self._send_response(_MTP_RESPONSE_INVALID_STORAGE_ID)
608638
return
609639

@@ -612,8 +642,10 @@ def _cmd_get_storage_info(self, params):
612642
fs_stat = os.statvfs(self._storage_path)
613643
free_bytes = fs_stat[0] * fs_stat[4] # f_bsize * f_bavail
614644
total_bytes = fs_stat[0] * fs_stat[2] # f_bsize * f_blocks
615-
except:
645+
print("[MTP] Storage stats: total={} bytes, free={} bytes".format(total_bytes, free_bytes))
646+
except Exception as e:
616647
# If we can't get stats, just return reasonable defaults
648+
print("[MTP] Error getting storage stats: {}".format(str(e)))
617649
free_bytes = 1024 * 1024 # 1MB
618650
total_bytes = 4 * 1024 * 1024 # 4MB
619651

@@ -645,12 +677,18 @@ def _cmd_get_storage_info(self, params):
645677
struct.pack_into("<I", data, offset, 0xFFFFFFFF)
646678
offset += 4
647679

648-
# Storage description (empty)
649-
struct.pack_into("<H", data, offset, 0)
680+
# Storage description
681+
desc = "MicroPython Flash Storage"
682+
struct.pack_into("<H", data, offset, len(desc) + 1)
683+
offset += 2
684+
for c in desc:
685+
struct.pack_into("<H", data, offset, ord(c))
686+
offset += 2
687+
struct.pack_into("<H", data, offset, 0) # Null terminator
650688
offset += 2
651689

652690
# Volume identifier (root)
653-
volume_id = "MicroPython"
691+
volume_id = "MicroPython Storage"
654692
struct.pack_into("<H", data, offset, len(volume_id) + 1)
655693
offset += 2
656694
for c in volume_id:

0 commit comments

Comments
 (0)