2 * Media Transfer Protocol implementation, backed by host filesystem.
4 * Copyright Red Hat, Inc 2014
7 * Gerd Hoffmann <kraxel@redhat.com>
9 * This code is licensed under the GPL v2 or later.
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
17 #include <sys/statvfs.h>
18 #ifdef CONFIG_INOTIFY1
19 #include <sys/inotify.h>
20 #include "qemu/main-loop.h"
23 #include "qemu-common.h"
28 #include "qemu/units.h"
30 /* ----------------------------------------------------------------------- */
32 enum mtp_container_type
{
39 /* MTP write stage, for internal use only */
40 enum mtp_write_status
{
48 CMD_GET_DEVICE_INFO
= 0x1001,
49 CMD_OPEN_SESSION
= 0x1002,
50 CMD_CLOSE_SESSION
= 0x1003,
51 CMD_GET_STORAGE_IDS
= 0x1004,
52 CMD_GET_STORAGE_INFO
= 0x1005,
53 CMD_GET_NUM_OBJECTS
= 0x1006,
54 CMD_GET_OBJECT_HANDLES
= 0x1007,
55 CMD_GET_OBJECT_INFO
= 0x1008,
56 CMD_GET_OBJECT
= 0x1009,
57 CMD_DELETE_OBJECT
= 0x100b,
58 CMD_SEND_OBJECT_INFO
= 0x100c,
59 CMD_SEND_OBJECT
= 0x100d,
60 CMD_GET_PARTIAL_OBJECT
= 0x101b,
61 CMD_GET_OBJECT_PROPS_SUPPORTED
= 0x9801,
62 CMD_GET_OBJECT_PROP_DESC
= 0x9802,
63 CMD_GET_OBJECT_PROP_VALUE
= 0x9803,
67 RES_GENERAL_ERROR
= 0x2002,
68 RES_SESSION_NOT_OPEN
= 0x2003,
69 RES_INVALID_TRANSACTION_ID
= 0x2004,
70 RES_OPERATION_NOT_SUPPORTED
= 0x2005,
71 RES_PARAMETER_NOT_SUPPORTED
= 0x2006,
72 RES_INCOMPLETE_TRANSFER
= 0x2007,
73 RES_INVALID_STORAGE_ID
= 0x2008,
74 RES_INVALID_OBJECT_HANDLE
= 0x2009,
75 RES_INVALID_OBJECT_FORMAT_CODE
= 0x200b,
76 RES_STORE_FULL
= 0x200c,
77 RES_STORE_READ_ONLY
= 0x200e,
78 RES_PARTIAL_DELETE
= 0x2012,
79 RES_STORE_NOT_AVAILABLE
= 0x2013,
80 RES_SPEC_BY_FORMAT_UNSUPPORTED
= 0x2014,
81 RES_INVALID_OBJECTINFO
= 0x2015,
82 RES_DESTINATION_UNSUPPORTED
= 0x2020,
83 RES_INVALID_PARENT_OBJECT
= 0x201a,
84 RES_INVALID_PARAMETER
= 0x201d,
85 RES_SESSION_ALREADY_OPEN
= 0x201e,
86 RES_INVALID_OBJECT_PROP_CODE
= 0xA801,
89 FMT_UNDEFINED_OBJECT
= 0x3000,
90 FMT_ASSOCIATION
= 0x3001,
93 EVT_CANCEL_TRANSACTION
= 0x4001,
94 EVT_OBJ_ADDED
= 0x4002,
95 EVT_OBJ_REMOVED
= 0x4003,
96 EVT_OBJ_INFO_CHANGED
= 0x4007,
98 /* object properties */
99 PROP_STORAGE_ID
= 0xDC01,
100 PROP_OBJECT_FORMAT
= 0xDC02,
101 PROP_OBJECT_COMPRESSED_SIZE
= 0xDC04,
102 PROP_PARENT_OBJECT
= 0xDC0B,
103 PROP_PERSISTENT_UNIQUE_OBJECT_IDENTIFIER
= 0xDC41,
108 DATA_TYPE_UINT16
= 0x0004,
109 DATA_TYPE_UINT32
= 0x0006,
110 DATA_TYPE_UINT64
= 0x0008,
111 DATA_TYPE_UINT128
= 0x000a,
112 DATA_TYPE_STRING
= 0xffff,
120 } QEMU_PACKED mtp_container
;
122 /* ----------------------------------------------------------------------- */
124 typedef struct MTPState MTPState
;
125 typedef struct MTPControl MTPControl
;
126 typedef struct MTPData MTPData
;
127 typedef struct MTPObject MTPObject
;
135 #ifdef CONFIG_INOTIFY1
136 typedef struct MTPMonEntry MTPMonEntry
;
142 QTAILQ_ENTRY(MTPMonEntry
) next
;
161 /* Used for >4G file sizes */
164 uint8_t write_status
;
165 /* Internal pointer per every MTP_WRITE_BUF_SZ */
166 uint64_t data_offset
;
175 #ifdef CONFIG_INOTIFY1
176 /* inotify watch cookie */
181 QLIST_HEAD(, MTPObject
) children
;
182 QLIST_ENTRY(MTPObject
) list
;
184 QTAILQ_ENTRY(MTPObject
) next
;
197 uint32_t next_handle
;
200 QTAILQ_HEAD(, MTPObject
) objects
;
201 #ifdef CONFIG_INOTIFY1
202 /* inotify descriptor */
204 QTAILQ_HEAD(, MTPMonEntry
) events
;
206 /* Responder is expecting a write operation */
209 uint32_t parent_handle
;
217 * ObjectInfo dataset received from initiator
218 * Fields we don't care about are ignored
221 uint32_t storage_id
; /*unused*/
223 uint16_t protection_status
; /*unused*/
225 uint16_t thumb_format
; /*unused*/
226 uint32_t thumb_comp_sz
; /*unused*/
227 uint32_t thumb_pix_width
; /*unused*/
228 uint32_t thumb_pix_height
; /*unused*/
229 uint32_t image_pix_width
; /*unused*/
230 uint32_t image_pix_height
; /*unused*/
231 uint32_t image_bit_depth
; /*unused*/
232 uint32_t parent
; /*unused*/
235 uint32_t seq_no
; /*unused*/
236 uint8_t length
; /*part of filename field*/
237 uint16_t filename
[0];
238 char date_created
[0]; /*unused*/
239 char date_modified
[0]; /*unused*/
240 char keywords
[0]; /*unused*/
241 /* string and other data follows */
242 } QEMU_PACKED ObjectInfo
;
244 #define TYPE_USB_MTP "usb-mtp"
245 #define USB_MTP(obj) OBJECT_CHECK(MTPState, (obj), TYPE_USB_MTP)
247 #define QEMU_STORAGE_ID 0x00010001
249 #define MTP_FLAG_WRITABLE 0
251 #define FLAG_SET(_mtp, _flag) ((_mtp)->flags & (1 << (_flag)))
253 /* ----------------------------------------------------------------------- */
255 #define MTP_MANUFACTURER "QEMU"
256 #define MTP_PRODUCT "QEMU filesharing"
257 #define MTP_WRITE_BUF_SZ (512 * KiB)
260 STR_MANUFACTURER
= 1,
269 static const USBDescStrings desc_strings
= {
270 [STR_MANUFACTURER
] = MTP_MANUFACTURER
,
271 [STR_PRODUCT
] = MTP_PRODUCT
,
272 [STR_SERIALNUMBER
] = "34617",
274 [STR_CONFIG_FULL
] = "Full speed config (usb 1.1)",
275 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
276 [STR_CONFIG_SUPER
] = "Super speed config (usb 3.0)",
279 static const USBDescIface desc_iface_full
= {
280 .bInterfaceNumber
= 0,
282 .bInterfaceClass
= USB_CLASS_STILL_IMAGE
,
283 .bInterfaceSubClass
= 0x01,
284 .bInterfaceProtocol
= 0x01,
285 .iInterface
= STR_MTP
,
286 .eps
= (USBDescEndpoint
[]) {
288 .bEndpointAddress
= USB_DIR_IN
| EP_DATA_IN
,
289 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
290 .wMaxPacketSize
= 64,
292 .bEndpointAddress
= USB_DIR_OUT
| EP_DATA_OUT
,
293 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
294 .wMaxPacketSize
= 64,
296 .bEndpointAddress
= USB_DIR_IN
| EP_EVENT
,
297 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
298 .wMaxPacketSize
= 64,
304 static const USBDescDevice desc_device_full
= {
306 .bMaxPacketSize0
= 8,
307 .bNumConfigurations
= 1,
308 .confs
= (USBDescConfig
[]) {
311 .bConfigurationValue
= 1,
312 .iConfiguration
= STR_CONFIG_FULL
,
313 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
316 .ifs
= &desc_iface_full
,
321 static const USBDescIface desc_iface_high
= {
322 .bInterfaceNumber
= 0,
324 .bInterfaceClass
= USB_CLASS_STILL_IMAGE
,
325 .bInterfaceSubClass
= 0x01,
326 .bInterfaceProtocol
= 0x01,
327 .iInterface
= STR_MTP
,
328 .eps
= (USBDescEndpoint
[]) {
330 .bEndpointAddress
= USB_DIR_IN
| EP_DATA_IN
,
331 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
332 .wMaxPacketSize
= 512,
334 .bEndpointAddress
= USB_DIR_OUT
| EP_DATA_OUT
,
335 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
336 .wMaxPacketSize
= 512,
338 .bEndpointAddress
= USB_DIR_IN
| EP_EVENT
,
339 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
340 .wMaxPacketSize
= 64,
346 static const USBDescDevice desc_device_high
= {
348 .bMaxPacketSize0
= 64,
349 .bNumConfigurations
= 1,
350 .confs
= (USBDescConfig
[]) {
353 .bConfigurationValue
= 1,
354 .iConfiguration
= STR_CONFIG_HIGH
,
355 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
358 .ifs
= &desc_iface_high
,
363 static const USBDescMSOS desc_msos
= {
364 .CompatibleID
= "MTP",
365 .SelectiveSuspendEnabled
= true,
368 static const USBDesc desc
= {
370 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
373 .iManufacturer
= STR_MANUFACTURER
,
374 .iProduct
= STR_PRODUCT
,
375 .iSerialNumber
= STR_SERIALNUMBER
,
377 .full
= &desc_device_full
,
378 .high
= &desc_device_high
,
383 /* ----------------------------------------------------------------------- */
385 static MTPObject
*usb_mtp_object_alloc(MTPState
*s
, uint32_t handle
,
386 MTPObject
*parent
, char *name
)
388 MTPObject
*o
= g_new0(MTPObject
, 1);
390 if (name
[0] == '.') {
396 o
->name
= g_strdup(name
);
397 if (parent
== NULL
) {
398 o
->path
= g_strdup(name
);
400 o
->path
= g_strdup_printf("%s/%s", parent
->path
, name
);
403 if (lstat(o
->path
, &o
->stat
) != 0) {
406 if (S_ISREG(o
->stat
.st_mode
)) {
407 o
->format
= FMT_UNDEFINED_OBJECT
;
408 } else if (S_ISDIR(o
->stat
.st_mode
)) {
409 o
->format
= FMT_ASSOCIATION
;
414 if (access(o
->path
, R_OK
) != 0) {
418 trace_usb_mtp_object_alloc(s
->dev
.addr
, o
->handle
, o
->path
);
420 QTAILQ_INSERT_TAIL(&s
->objects
, o
, next
);
430 static void usb_mtp_object_free(MTPState
*s
, MTPObject
*o
)
438 trace_usb_mtp_object_free(s
->dev
.addr
, o
->handle
, o
->path
);
440 QTAILQ_REMOVE(&s
->objects
, o
, next
);
442 QLIST_REMOVE(o
, list
);
443 o
->parent
->nchildren
--;
446 while (!QLIST_EMPTY(&o
->children
)) {
447 iter
= QLIST_FIRST(&o
->children
);
448 usb_mtp_object_free(s
, iter
);
455 static MTPObject
*usb_mtp_object_lookup(MTPState
*s
, uint32_t handle
)
459 QTAILQ_FOREACH(o
, &s
->objects
, next
) {
460 if (o
->handle
== handle
) {
467 static MTPObject
*usb_mtp_add_child(MTPState
*s
, MTPObject
*o
,
471 usb_mtp_object_alloc(s
, s
->next_handle
++, o
, name
);
474 trace_usb_mtp_add_child(s
->dev
.addr
, child
->handle
, child
->path
);
475 QLIST_INSERT_HEAD(&o
->children
, child
, list
);
478 if (child
->format
== FMT_ASSOCIATION
) {
479 QLIST_INIT(&child
->children
);
486 static MTPObject
*usb_mtp_object_lookup_name(MTPObject
*parent
,
491 QLIST_FOREACH(iter
, &parent
->children
, list
) {
492 if (strncmp(iter
->name
, name
, len
) == 0) {
500 #ifdef CONFIG_INOTIFY1
501 static MTPObject
*usb_mtp_object_lookup_wd(MTPState
*s
, int wd
)
505 QTAILQ_FOREACH(iter
, &s
->objects
, next
) {
506 if (iter
->watchfd
== wd
) {
514 static void inotify_watchfn(void *arg
)
518 /* From the man page: atleast one event can be read */
520 char buf
[sizeof(struct inotify_event
) + NAME_MAX
+ 1];
523 bytes
= read(s
->inotifyfd
, buf
, sizeof(buf
));
527 /* Better luck next time */
532 * TODO: Ignore initiator initiated events.
533 * For now we are good because the store is RO
537 struct inotify_event
*event
= (struct inotify_event
*)p
;
539 uint32_t mask
= event
->mask
& (IN_CREATE
| IN_DELETE
|
540 IN_MODIFY
| IN_IGNORED
);
541 MTPObject
*parent
= usb_mtp_object_lookup_wd(s
, event
->wd
);
542 MTPMonEntry
*entry
= NULL
;
545 pos
= pos
+ sizeof(struct inotify_event
) + event
->len
;
554 if (usb_mtp_object_lookup_name
555 (parent
, event
->name
, event
->len
)) {
556 /* Duplicate create event */
559 entry
= g_new0(MTPMonEntry
, 1);
560 entry
->handle
= s
->next_handle
;
561 entry
->event
= EVT_OBJ_ADDED
;
562 o
= usb_mtp_add_child(s
, parent
, event
->name
);
567 o
->watchfd
= watchfd
;
568 trace_usb_mtp_inotify_event(s
->dev
.addr
, event
->name
,
569 event
->mask
, "Obj Added");
574 * The kernel issues a IN_IGNORED event
575 * when a dir containing a watchpoint is
576 * deleted, so we don't have to delete the
579 o
= usb_mtp_object_lookup_name(parent
, event
->name
, event
->len
);
583 entry
= g_new0(MTPMonEntry
, 1);
584 entry
->handle
= o
->handle
;
585 entry
->event
= EVT_OBJ_REMOVED
;
586 trace_usb_mtp_inotify_event(s
->dev
.addr
, o
->path
,
587 event
->mask
, "Obj Deleted");
588 usb_mtp_object_free(s
, o
);
592 o
= usb_mtp_object_lookup_name(parent
, event
->name
, event
->len
);
596 entry
= g_new0(MTPMonEntry
, 1);
597 entry
->handle
= o
->handle
;
598 entry
->event
= EVT_OBJ_INFO_CHANGED
;
599 trace_usb_mtp_inotify_event(s
->dev
.addr
, o
->path
,
600 event
->mask
, "Obj Modified");
604 trace_usb_mtp_inotify_event(s
->dev
.addr
, parent
->path
,
605 event
->mask
, "Obj parent dir ignored");
609 fprintf(stderr
, "usb-mtp: failed to parse inotify event\n");
614 QTAILQ_INSERT_HEAD(&s
->events
, entry
, next
);
620 static int usb_mtp_inotify_init(MTPState
*s
)
624 fd
= inotify_init1(IN_NONBLOCK
);
629 QTAILQ_INIT(&s
->events
);
632 qemu_set_fd_handler(fd
, inotify_watchfn
, NULL
, s
);
637 static void usb_mtp_inotify_cleanup(MTPState
*s
)
645 qemu_set_fd_handler(s
->inotifyfd
, NULL
, NULL
, s
);
648 QTAILQ_FOREACH_SAFE(e
, &s
->events
, next
, p
) {
649 QTAILQ_REMOVE(&s
->events
, e
, next
);
654 static int usb_mtp_add_watch(int inotifyfd
, char *path
)
656 uint32_t mask
= IN_CREATE
| IN_DELETE
| IN_MODIFY
|
659 return inotify_add_watch(inotifyfd
, path
, mask
);
663 static void usb_mtp_object_readdir(MTPState
*s
, MTPObject
*o
)
665 struct dirent
*entry
;
669 if (o
->have_children
) {
672 o
->have_children
= true;
674 fd
= open(o
->path
, O_DIRECTORY
| O_CLOEXEC
| O_NOFOLLOW
);
683 #ifdef CONFIG_INOTIFY1
684 int watchfd
= usb_mtp_add_watch(s
->inotifyfd
, o
->path
);
686 fprintf(stderr
, "usb-mtp: failed to add watch for %s\n", o
->path
);
688 trace_usb_mtp_inotify_event(s
->dev
.addr
, o
->path
,
690 o
->watchfd
= watchfd
;
693 while ((entry
= readdir(dir
)) != NULL
) {
694 usb_mtp_add_child(s
, o
, entry
->d_name
);
699 /* ----------------------------------------------------------------------- */
701 static MTPData
*usb_mtp_data_alloc(MTPControl
*c
)
703 MTPData
*data
= g_new0(MTPData
, 1);
705 data
->code
= c
->code
;
706 data
->trans
= c
->trans
;
712 static void usb_mtp_data_free(MTPData
*data
)
717 if (data
->fd
!= -1) {
724 static void usb_mtp_realloc(MTPData
*data
, uint32_t bytes
)
726 if (data
->length
+ bytes
<= data
->alloc
) {
729 data
->alloc
= (data
->length
+ bytes
+ 0xff) & ~0xff;
730 data
->data
= g_realloc(data
->data
, data
->alloc
);
733 static void usb_mtp_add_u8(MTPData
*data
, uint8_t val
)
735 usb_mtp_realloc(data
, 1);
736 data
->data
[data
->length
++] = val
;
739 static void usb_mtp_add_u16(MTPData
*data
, uint16_t val
)
741 usb_mtp_realloc(data
, 2);
742 data
->data
[data
->length
++] = (val
>> 0) & 0xff;
743 data
->data
[data
->length
++] = (val
>> 8) & 0xff;
746 static void usb_mtp_add_u32(MTPData
*data
, uint32_t val
)
748 usb_mtp_realloc(data
, 4);
749 data
->data
[data
->length
++] = (val
>> 0) & 0xff;
750 data
->data
[data
->length
++] = (val
>> 8) & 0xff;
751 data
->data
[data
->length
++] = (val
>> 16) & 0xff;
752 data
->data
[data
->length
++] = (val
>> 24) & 0xff;
755 static void usb_mtp_add_u64(MTPData
*data
, uint64_t val
)
757 usb_mtp_realloc(data
, 8);
758 data
->data
[data
->length
++] = (val
>> 0) & 0xff;
759 data
->data
[data
->length
++] = (val
>> 8) & 0xff;
760 data
->data
[data
->length
++] = (val
>> 16) & 0xff;
761 data
->data
[data
->length
++] = (val
>> 24) & 0xff;
762 data
->data
[data
->length
++] = (val
>> 32) & 0xff;
763 data
->data
[data
->length
++] = (val
>> 40) & 0xff;
764 data
->data
[data
->length
++] = (val
>> 48) & 0xff;
765 data
->data
[data
->length
++] = (val
>> 56) & 0xff;
768 static void usb_mtp_add_u16_array(MTPData
*data
, uint32_t len
,
769 const uint16_t *vals
)
773 usb_mtp_add_u32(data
, len
);
774 for (i
= 0; i
< len
; i
++) {
775 usb_mtp_add_u16(data
, vals
[i
]);
779 static void usb_mtp_add_u32_array(MTPData
*data
, uint32_t len
,
780 const uint32_t *vals
)
784 usb_mtp_add_u32(data
, len
);
785 for (i
= 0; i
< len
; i
++) {
786 usb_mtp_add_u32(data
, vals
[i
]);
790 static void usb_mtp_add_wstr(MTPData
*data
, const wchar_t *str
)
792 uint32_t len
= wcslen(str
);
796 len
++; /* include terminating L'\0' */
799 usb_mtp_add_u8(data
, len
);
800 for (i
= 0; i
< len
; i
++) {
801 usb_mtp_add_u16(data
, str
[i
]);
805 static void usb_mtp_add_str(MTPData
*data
, const char *str
)
807 uint32_t len
= strlen(str
)+1;
808 wchar_t *wstr
= g_new(wchar_t, len
);
811 ret
= mbstowcs(wstr
, str
, len
);
813 usb_mtp_add_wstr(data
, L
"Oops");
815 usb_mtp_add_wstr(data
, wstr
);
821 static void usb_mtp_add_time(MTPData
*data
, time_t time
)
826 gmtime_r(&time
, &tm
);
827 strftime(buf
, sizeof(buf
), "%Y%m%dT%H%M%S", &tm
);
828 usb_mtp_add_str(data
, buf
);
831 /* ----------------------------------------------------------------------- */
833 static void usb_mtp_queue_result(MTPState
*s
, uint16_t code
, uint32_t trans
,
834 int argc
, uint32_t arg0
, uint32_t arg1
,
837 MTPControl
*c
= g_new0(MTPControl
, 1);
852 assert(s
->result
== NULL
);
856 /* ----------------------------------------------------------------------- */
858 static MTPData
*usb_mtp_get_device_info(MTPState
*s
, MTPControl
*c
)
860 static const uint16_t ops
[] = {
865 CMD_GET_STORAGE_INFO
,
867 CMD_GET_OBJECT_HANDLES
,
870 CMD_SEND_OBJECT_INFO
,
873 CMD_GET_PARTIAL_OBJECT
,
874 CMD_GET_OBJECT_PROPS_SUPPORTED
,
875 CMD_GET_OBJECT_PROP_DESC
,
876 CMD_GET_OBJECT_PROP_VALUE
,
878 static const uint16_t fmt
[] = {
879 FMT_UNDEFINED_OBJECT
,
882 MTPData
*d
= usb_mtp_data_alloc(c
);
884 trace_usb_mtp_op_get_device_info(s
->dev
.addr
);
886 usb_mtp_add_u16(d
, 100);
887 usb_mtp_add_u32(d
, 0x00000006);
888 usb_mtp_add_u16(d
, 0x0064);
889 usb_mtp_add_wstr(d
, L
"");
890 usb_mtp_add_u16(d
, 0x0000);
892 usb_mtp_add_u16_array(d
, ARRAY_SIZE(ops
), ops
);
893 usb_mtp_add_u16_array(d
, 0, NULL
);
894 usb_mtp_add_u16_array(d
, 0, NULL
);
895 usb_mtp_add_u16_array(d
, 0, NULL
);
896 usb_mtp_add_u16_array(d
, ARRAY_SIZE(fmt
), fmt
);
898 usb_mtp_add_wstr(d
, L
"" MTP_MANUFACTURER
);
899 usb_mtp_add_wstr(d
, L
"" MTP_PRODUCT
);
900 usb_mtp_add_wstr(d
, L
"0.1");
901 usb_mtp_add_wstr(d
, L
"0123456789abcdef0123456789abcdef");
906 static MTPData
*usb_mtp_get_storage_ids(MTPState
*s
, MTPControl
*c
)
908 static const uint32_t ids
[] = {
911 MTPData
*d
= usb_mtp_data_alloc(c
);
913 trace_usb_mtp_op_get_storage_ids(s
->dev
.addr
);
915 usb_mtp_add_u32_array(d
, ARRAY_SIZE(ids
), ids
);
920 static MTPData
*usb_mtp_get_storage_info(MTPState
*s
, MTPControl
*c
)
922 MTPData
*d
= usb_mtp_data_alloc(c
);
926 trace_usb_mtp_op_get_storage_info(s
->dev
.addr
);
928 if (FLAG_SET(s
, MTP_FLAG_WRITABLE
)) {
929 usb_mtp_add_u16(d
, 0x0003);
930 usb_mtp_add_u16(d
, 0x0002);
931 usb_mtp_add_u16(d
, 0x0000);
933 usb_mtp_add_u16(d
, 0x0001);
934 usb_mtp_add_u16(d
, 0x0002);
935 usb_mtp_add_u16(d
, 0x0001);
938 rc
= statvfs(s
->root
, &buf
);
940 usb_mtp_add_u64(d
, (uint64_t)buf
.f_frsize
* buf
.f_blocks
);
941 usb_mtp_add_u64(d
, (uint64_t)buf
.f_bavail
* buf
.f_blocks
);
942 usb_mtp_add_u32(d
, buf
.f_ffree
);
944 usb_mtp_add_u64(d
, 0xffffffff);
945 usb_mtp_add_u64(d
, 0xffffffff);
946 usb_mtp_add_u32(d
, 0xffffffff);
949 usb_mtp_add_str(d
, s
->desc
);
950 usb_mtp_add_wstr(d
, L
"123456789abcdef");
954 static MTPData
*usb_mtp_get_object_handles(MTPState
*s
, MTPControl
*c
,
957 MTPData
*d
= usb_mtp_data_alloc(c
);
958 uint32_t i
= 0, handles
[o
->nchildren
];
961 trace_usb_mtp_op_get_object_handles(s
->dev
.addr
, o
->handle
, o
->path
);
963 QLIST_FOREACH(iter
, &o
->children
, list
) {
964 handles
[i
++] = iter
->handle
;
966 assert(i
== o
->nchildren
);
967 usb_mtp_add_u32_array(d
, o
->nchildren
, handles
);
972 static MTPData
*usb_mtp_get_object_info(MTPState
*s
, MTPControl
*c
,
975 MTPData
*d
= usb_mtp_data_alloc(c
);
977 trace_usb_mtp_op_get_object_info(s
->dev
.addr
, o
->handle
, o
->path
);
979 usb_mtp_add_u32(d
, QEMU_STORAGE_ID
);
980 usb_mtp_add_u16(d
, o
->format
);
981 usb_mtp_add_u16(d
, 0);
983 if (o
->stat
.st_size
> 0xFFFFFFFF) {
984 usb_mtp_add_u32(d
, 0xFFFFFFFF);
986 usb_mtp_add_u32(d
, o
->stat
.st_size
);
989 usb_mtp_add_u16(d
, 0);
990 usb_mtp_add_u32(d
, 0);
991 usb_mtp_add_u32(d
, 0);
992 usb_mtp_add_u32(d
, 0);
993 usb_mtp_add_u32(d
, 0);
994 usb_mtp_add_u32(d
, 0);
995 usb_mtp_add_u32(d
, 0);
998 usb_mtp_add_u32(d
, o
->parent
->handle
);
1000 usb_mtp_add_u32(d
, 0);
1002 if (o
->format
== FMT_ASSOCIATION
) {
1003 usb_mtp_add_u16(d
, 0x0001);
1004 usb_mtp_add_u32(d
, 0x00000001);
1005 usb_mtp_add_u32(d
, 0);
1007 usb_mtp_add_u16(d
, 0);
1008 usb_mtp_add_u32(d
, 0);
1009 usb_mtp_add_u32(d
, 0);
1012 usb_mtp_add_str(d
, o
->name
);
1013 usb_mtp_add_time(d
, o
->stat
.st_ctime
);
1014 usb_mtp_add_time(d
, o
->stat
.st_mtime
);
1015 usb_mtp_add_wstr(d
, L
"");
1020 static MTPData
*usb_mtp_get_object(MTPState
*s
, MTPControl
*c
,
1023 MTPData
*d
= usb_mtp_data_alloc(c
);
1025 trace_usb_mtp_op_get_object(s
->dev
.addr
, o
->handle
, o
->path
);
1027 d
->fd
= open(o
->path
, O_RDONLY
| O_CLOEXEC
| O_NOFOLLOW
);
1029 usb_mtp_data_free(d
);
1032 d
->length
= o
->stat
.st_size
;
1034 d
->data
= g_malloc(d
->alloc
);
1038 static MTPData
*usb_mtp_get_partial_object(MTPState
*s
, MTPControl
*c
,
1047 trace_usb_mtp_op_get_partial_object(s
->dev
.addr
, o
->handle
, o
->path
,
1048 c
->argv
[1], c
->argv
[2]);
1050 d
= usb_mtp_data_alloc(c
);
1051 d
->fd
= open(o
->path
, O_RDONLY
| O_CLOEXEC
| O_NOFOLLOW
);
1053 usb_mtp_data_free(d
);
1057 offset
= c
->argv
[1];
1058 if (offset
> o
->stat
.st_size
) {
1059 offset
= o
->stat
.st_size
;
1061 if (lseek(d
->fd
, offset
, SEEK_SET
) < 0) {
1062 usb_mtp_data_free(d
);
1066 d
->length
= c
->argv
[2];
1067 if (d
->length
> o
->stat
.st_size
- offset
) {
1068 d
->length
= o
->stat
.st_size
- offset
;
1074 static MTPData
*usb_mtp_get_object_props_supported(MTPState
*s
, MTPControl
*c
)
1076 static const uint16_t props
[] = {
1079 PROP_OBJECT_COMPRESSED_SIZE
,
1081 PROP_PERSISTENT_UNIQUE_OBJECT_IDENTIFIER
,
1084 MTPData
*d
= usb_mtp_data_alloc(c
);
1085 usb_mtp_add_u16_array(d
, ARRAY_SIZE(props
), props
);
1090 static MTPData
*usb_mtp_get_object_prop_desc(MTPState
*s
, MTPControl
*c
)
1092 MTPData
*d
= usb_mtp_data_alloc(c
);
1093 switch (c
->argv
[0]) {
1094 case PROP_STORAGE_ID
:
1095 usb_mtp_add_u16(d
, PROP_STORAGE_ID
);
1096 usb_mtp_add_u16(d
, DATA_TYPE_UINT32
);
1097 usb_mtp_add_u8(d
, 0x00);
1098 usb_mtp_add_u32(d
, 0x00000000);
1099 usb_mtp_add_u32(d
, 0x00000000);
1100 usb_mtp_add_u8(d
, 0x00);
1102 case PROP_OBJECT_FORMAT
:
1103 usb_mtp_add_u16(d
, PROP_OBJECT_FORMAT
);
1104 usb_mtp_add_u16(d
, DATA_TYPE_UINT16
);
1105 usb_mtp_add_u8(d
, 0x00);
1106 usb_mtp_add_u16(d
, 0x0000);
1107 usb_mtp_add_u32(d
, 0x00000000);
1108 usb_mtp_add_u8(d
, 0x00);
1110 case PROP_OBJECT_COMPRESSED_SIZE
:
1111 usb_mtp_add_u16(d
, PROP_OBJECT_COMPRESSED_SIZE
);
1112 usb_mtp_add_u16(d
, DATA_TYPE_UINT64
);
1113 usb_mtp_add_u8(d
, 0x00);
1114 usb_mtp_add_u64(d
, 0x0000000000000000);
1115 usb_mtp_add_u32(d
, 0x00000000);
1116 usb_mtp_add_u8(d
, 0x00);
1118 case PROP_PARENT_OBJECT
:
1119 usb_mtp_add_u16(d
, PROP_PARENT_OBJECT
);
1120 usb_mtp_add_u16(d
, DATA_TYPE_UINT32
);
1121 usb_mtp_add_u8(d
, 0x00);
1122 usb_mtp_add_u32(d
, 0x00000000);
1123 usb_mtp_add_u32(d
, 0x00000000);
1124 usb_mtp_add_u8(d
, 0x00);
1126 case PROP_PERSISTENT_UNIQUE_OBJECT_IDENTIFIER
:
1127 usb_mtp_add_u16(d
, PROP_PERSISTENT_UNIQUE_OBJECT_IDENTIFIER
);
1128 usb_mtp_add_u16(d
, DATA_TYPE_UINT128
);
1129 usb_mtp_add_u8(d
, 0x00);
1130 usb_mtp_add_u64(d
, 0x0000000000000000);
1131 usb_mtp_add_u64(d
, 0x0000000000000000);
1132 usb_mtp_add_u32(d
, 0x00000000);
1133 usb_mtp_add_u8(d
, 0x00);
1136 usb_mtp_add_u16(d
, PROP_NAME
);
1137 usb_mtp_add_u16(d
, DATA_TYPE_STRING
);
1138 usb_mtp_add_u8(d
, 0x00);
1139 usb_mtp_add_u8(d
, 0x00);
1140 usb_mtp_add_u32(d
, 0x00000000);
1141 usb_mtp_add_u8(d
, 0x00);
1144 usb_mtp_data_free(d
);
1151 static MTPData
*usb_mtp_get_object_prop_value(MTPState
*s
, MTPControl
*c
,
1154 MTPData
*d
= usb_mtp_data_alloc(c
);
1155 switch (c
->argv
[1]) {
1156 case PROP_STORAGE_ID
:
1157 usb_mtp_add_u32(d
, QEMU_STORAGE_ID
);
1159 case PROP_OBJECT_FORMAT
:
1160 usb_mtp_add_u16(d
, o
->format
);
1162 case PROP_OBJECT_COMPRESSED_SIZE
:
1163 usb_mtp_add_u64(d
, o
->stat
.st_size
);
1165 case PROP_PARENT_OBJECT
:
1166 if (o
->parent
== NULL
) {
1167 usb_mtp_add_u32(d
, 0x00000000);
1169 usb_mtp_add_u32(d
, o
->parent
->handle
);
1172 case PROP_PERSISTENT_UNIQUE_OBJECT_IDENTIFIER
:
1173 /* Should be persistent between sessions,
1174 * but using our objedt ID is "good enough"
1176 usb_mtp_add_u64(d
, 0x0000000000000000);
1177 usb_mtp_add_u64(d
, o
->handle
);
1180 usb_mtp_add_str(d
, o
->name
);
1183 usb_mtp_data_free(d
);
1190 /* Return correct return code for a delete event */
1197 /* Assumes that children, if any, have been already freed */
1198 static void usb_mtp_object_free_one(MTPState
*s
, MTPObject
*o
)
1200 #ifndef CONFIG_INOTIFY1
1201 assert(o
->nchildren
== 0);
1202 QTAILQ_REMOVE(&s
->objects
, o
, next
);
1209 static int usb_mtp_deletefn(MTPState
*s
, MTPObject
*o
, uint32_t trans
)
1211 MTPObject
*iter
, *iter2
;
1212 bool partial_delete
= false;
1213 bool success
= false;
1216 * TODO: Add support for Protection Status
1219 QLIST_FOREACH(iter
, &o
->children
, list
) {
1220 if (iter
->format
== FMT_ASSOCIATION
) {
1221 QLIST_FOREACH(iter2
, &iter
->children
, list
) {
1222 usb_mtp_deletefn(s
, iter2
, trans
);
1227 if (o
->format
== FMT_UNDEFINED_OBJECT
) {
1228 if (remove(o
->path
)) {
1229 partial_delete
= true;
1231 usb_mtp_object_free_one(s
, o
);
1236 if (o
->format
== FMT_ASSOCIATION
) {
1237 if (rmdir(o
->path
)) {
1238 partial_delete
= true;
1240 usb_mtp_object_free_one(s
, o
);
1245 if (success
&& partial_delete
) {
1246 return PARTIAL_DELETE
;
1248 if (!success
&& partial_delete
) {
1254 static void usb_mtp_object_delete(MTPState
*s
, uint32_t handle
,
1255 uint32_t format_code
, uint32_t trans
)
1260 /* Return error if store is read-only */
1261 if (!FLAG_SET(s
, MTP_FLAG_WRITABLE
)) {
1262 usb_mtp_queue_result(s
, RES_STORE_READ_ONLY
,
1267 if (format_code
!= 0) {
1268 usb_mtp_queue_result(s
, RES_SPEC_BY_FORMAT_UNSUPPORTED
,
1273 if (handle
== 0xFFFFFFF) {
1274 o
= QTAILQ_FIRST(&s
->objects
);
1276 o
= usb_mtp_object_lookup(s
, handle
);
1279 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
1284 ret
= usb_mtp_deletefn(s
, o
, trans
);
1285 if (ret
== PARTIAL_DELETE
) {
1286 usb_mtp_queue_result(s
, RES_PARTIAL_DELETE
,
1289 } else if (ret
== READ_ONLY
) {
1290 usb_mtp_queue_result(s
, RES_STORE_READ_ONLY
, trans
,
1294 usb_mtp_queue_result(s
, RES_OK
, trans
,
1300 static void usb_mtp_command(MTPState
*s
, MTPControl
*c
)
1302 MTPData
*data_in
= NULL
;
1303 MTPObject
*o
= NULL
;
1304 uint32_t nres
= 0, res0
= 0;
1307 if (c
->code
>= CMD_CLOSE_SESSION
&& s
->session
== 0) {
1308 usb_mtp_queue_result(s
, RES_SESSION_NOT_OPEN
,
1309 c
->trans
, 0, 0, 0, 0);
1313 /* process commands */
1315 case CMD_GET_DEVICE_INFO
:
1316 data_in
= usb_mtp_get_device_info(s
, c
);
1318 case CMD_OPEN_SESSION
:
1320 usb_mtp_queue_result(s
, RES_SESSION_ALREADY_OPEN
,
1321 c
->trans
, 1, s
->session
, 0, 0);
1324 if (c
->argv
[0] == 0) {
1325 usb_mtp_queue_result(s
, RES_INVALID_PARAMETER
,
1326 c
->trans
, 0, 0, 0, 0);
1329 trace_usb_mtp_op_open_session(s
->dev
.addr
);
1330 s
->session
= c
->argv
[0];
1331 usb_mtp_object_alloc(s
, s
->next_handle
++, NULL
, s
->root
);
1332 #ifdef CONFIG_INOTIFY1
1333 if (usb_mtp_inotify_init(s
)) {
1334 fprintf(stderr
, "usb-mtp: file monitoring init failed\n");
1338 case CMD_CLOSE_SESSION
:
1339 trace_usb_mtp_op_close_session(s
->dev
.addr
);
1342 #ifdef CONFIG_INOTIFY1
1343 usb_mtp_inotify_cleanup(s
);
1345 usb_mtp_object_free(s
, QTAILQ_FIRST(&s
->objects
));
1346 assert(QTAILQ_EMPTY(&s
->objects
));
1348 case CMD_GET_STORAGE_IDS
:
1349 data_in
= usb_mtp_get_storage_ids(s
, c
);
1351 case CMD_GET_STORAGE_INFO
:
1352 if (c
->argv
[0] != QEMU_STORAGE_ID
&&
1353 c
->argv
[0] != 0xffffffff) {
1354 usb_mtp_queue_result(s
, RES_INVALID_STORAGE_ID
,
1355 c
->trans
, 0, 0, 0, 0);
1358 data_in
= usb_mtp_get_storage_info(s
, c
);
1360 case CMD_GET_NUM_OBJECTS
:
1361 case CMD_GET_OBJECT_HANDLES
:
1362 if (c
->argv
[0] != QEMU_STORAGE_ID
&&
1363 c
->argv
[0] != 0xffffffff) {
1364 usb_mtp_queue_result(s
, RES_INVALID_STORAGE_ID
,
1365 c
->trans
, 0, 0, 0, 0);
1368 if (c
->argv
[1] != 0x00000000) {
1369 usb_mtp_queue_result(s
, RES_SPEC_BY_FORMAT_UNSUPPORTED
,
1370 c
->trans
, 0, 0, 0, 0);
1373 if (c
->argv
[2] == 0x00000000 ||
1374 c
->argv
[2] == 0xffffffff) {
1375 o
= QTAILQ_FIRST(&s
->objects
);
1377 o
= usb_mtp_object_lookup(s
, c
->argv
[2]);
1380 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
1381 c
->trans
, 0, 0, 0, 0);
1384 if (o
->format
!= FMT_ASSOCIATION
) {
1385 usb_mtp_queue_result(s
, RES_INVALID_PARENT_OBJECT
,
1386 c
->trans
, 0, 0, 0, 0);
1389 usb_mtp_object_readdir(s
, o
);
1390 if (c
->code
== CMD_GET_NUM_OBJECTS
) {
1391 trace_usb_mtp_op_get_num_objects(s
->dev
.addr
, o
->handle
, o
->path
);
1393 res0
= o
->nchildren
;
1395 data_in
= usb_mtp_get_object_handles(s
, c
, o
);
1398 case CMD_GET_OBJECT_INFO
:
1399 o
= usb_mtp_object_lookup(s
, c
->argv
[0]);
1401 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
1402 c
->trans
, 0, 0, 0, 0);
1405 data_in
= usb_mtp_get_object_info(s
, c
, o
);
1407 case CMD_GET_OBJECT
:
1408 o
= usb_mtp_object_lookup(s
, c
->argv
[0]);
1410 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
1411 c
->trans
, 0, 0, 0, 0);
1414 if (o
->format
== FMT_ASSOCIATION
) {
1415 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
1416 c
->trans
, 0, 0, 0, 0);
1419 data_in
= usb_mtp_get_object(s
, c
, o
);
1420 if (data_in
== NULL
) {
1421 usb_mtp_queue_result(s
, RES_GENERAL_ERROR
,
1422 c
->trans
, 0, 0, 0, 0);
1426 case CMD_DELETE_OBJECT
:
1427 usb_mtp_object_delete(s
, c
->argv
[0], c
->argv
[1], c
->trans
);
1429 case CMD_GET_PARTIAL_OBJECT
:
1430 o
= usb_mtp_object_lookup(s
, c
->argv
[0]);
1432 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
1433 c
->trans
, 0, 0, 0, 0);
1436 if (o
->format
== FMT_ASSOCIATION
) {
1437 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
1438 c
->trans
, 0, 0, 0, 0);
1441 data_in
= usb_mtp_get_partial_object(s
, c
, o
);
1442 if (data_in
== NULL
) {
1443 usb_mtp_queue_result(s
, RES_GENERAL_ERROR
,
1444 c
->trans
, 0, 0, 0, 0);
1448 res0
= data_in
->length
;
1450 case CMD_SEND_OBJECT_INFO
:
1451 /* Return error if store is read-only */
1452 if (!FLAG_SET(s
, MTP_FLAG_WRITABLE
)) {
1453 usb_mtp_queue_result(s
, RES_STORE_READ_ONLY
,
1454 c
->trans
, 0, 0, 0, 0);
1455 } else if (c
->argv
[0] && (c
->argv
[0] != QEMU_STORAGE_ID
)) {
1456 /* First parameter points to storage id or is 0 */
1457 usb_mtp_queue_result(s
, RES_STORE_NOT_AVAILABLE
, c
->trans
,
1459 } else if (c
->argv
[1] && !c
->argv
[0]) {
1460 /* If second parameter is specified, first must also be specified */
1461 usb_mtp_queue_result(s
, RES_DESTINATION_UNSUPPORTED
, c
->trans
,
1464 uint32_t handle
= c
->argv
[1];
1465 if (handle
== 0xFFFFFFFF || handle
== 0) {
1467 o
= QTAILQ_FIRST(&s
->objects
);
1469 o
= usb_mtp_object_lookup(s
, handle
);
1472 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
, c
->trans
,
1474 } else if (o
->format
!= FMT_ASSOCIATION
) {
1475 usb_mtp_queue_result(s
, RES_INVALID_PARENT_OBJECT
, c
->trans
,
1480 s
->dataset
.parent_handle
= o
->handle
;
1482 s
->data_out
= usb_mtp_data_alloc(c
);
1484 case CMD_SEND_OBJECT
:
1485 if (!FLAG_SET(s
, MTP_FLAG_WRITABLE
)) {
1486 usb_mtp_queue_result(s
, RES_STORE_READ_ONLY
,
1487 c
->trans
, 0, 0, 0, 0);
1490 if (!s
->write_pending
) {
1491 usb_mtp_queue_result(s
, RES_INVALID_OBJECTINFO
,
1492 c
->trans
, 0, 0, 0, 0);
1495 s
->data_out
= usb_mtp_data_alloc(c
);
1497 case CMD_GET_OBJECT_PROPS_SUPPORTED
:
1498 if (c
->argv
[0] != FMT_UNDEFINED_OBJECT
&&
1499 c
->argv
[0] != FMT_ASSOCIATION
) {
1500 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_FORMAT_CODE
,
1501 c
->trans
, 0, 0, 0, 0);
1504 data_in
= usb_mtp_get_object_props_supported(s
, c
);
1506 case CMD_GET_OBJECT_PROP_DESC
:
1507 if (c
->argv
[1] != FMT_UNDEFINED_OBJECT
&&
1508 c
->argv
[1] != FMT_ASSOCIATION
) {
1509 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_FORMAT_CODE
,
1510 c
->trans
, 0, 0, 0, 0);
1513 data_in
= usb_mtp_get_object_prop_desc(s
, c
);
1514 if (data_in
== NULL
) {
1515 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_PROP_CODE
,
1516 c
->trans
, 0, 0, 0, 0);
1520 case CMD_GET_OBJECT_PROP_VALUE
:
1521 o
= usb_mtp_object_lookup(s
, c
->argv
[0]);
1523 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
1524 c
->trans
, 0, 0, 0, 0);
1527 data_in
= usb_mtp_get_object_prop_value(s
, c
, o
);
1528 if (data_in
== NULL
) {
1529 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_PROP_CODE
,
1530 c
->trans
, 0, 0, 0, 0);
1535 trace_usb_mtp_op_unknown(s
->dev
.addr
, c
->code
);
1536 usb_mtp_queue_result(s
, RES_OPERATION_NOT_SUPPORTED
,
1537 c
->trans
, 0, 0, 0, 0);
1541 /* return results on success */
1543 assert(s
->data_in
== NULL
);
1544 s
->data_in
= data_in
;
1546 usb_mtp_queue_result(s
, RES_OK
, c
->trans
, nres
, res0
, 0, 0);
1549 /* ----------------------------------------------------------------------- */
1551 static void usb_mtp_handle_reset(USBDevice
*dev
)
1553 MTPState
*s
= USB_MTP(dev
);
1555 trace_usb_mtp_reset(s
->dev
.addr
);
1557 #ifdef CONFIG_INOTIFY1
1558 usb_mtp_inotify_cleanup(s
);
1560 usb_mtp_object_free(s
, QTAILQ_FIRST(&s
->objects
));
1562 usb_mtp_data_free(s
->data_in
);
1564 usb_mtp_data_free(s
->data_out
);
1570 static void usb_mtp_handle_control(USBDevice
*dev
, USBPacket
*p
,
1571 int request
, int value
, int index
,
1572 int length
, uint8_t *data
)
1575 MTPState
*s
= USB_MTP(dev
);
1576 uint16_t *event
= (uint16_t *)data
;
1579 case ClassInterfaceOutRequest
| 0x64:
1580 if (*event
== EVT_CANCEL_TRANSACTION
) {
1583 usb_mtp_data_free(s
->data_in
);
1585 if (s
->write_pending
) {
1586 g_free(s
->dataset
.filename
);
1587 s
->write_pending
= false;
1588 s
->dataset
.size
= 0;
1590 usb_mtp_data_free(s
->data_out
);
1593 p
->status
= USB_RET_STALL
;
1597 ret
= usb_desc_handle_control(dev
, p
, request
,
1598 value
, index
, length
, data
);
1604 trace_usb_mtp_stall(dev
->addr
, "unknown control request");
1607 static void usb_mtp_cancel_packet(USBDevice
*dev
, USBPacket
*p
)
1609 /* we don't use async packets, so this should never be called */
1610 fprintf(stderr
, "%s\n", __func__
);
1613 static char *utf16_to_str(uint8_t len
, uint16_t *arr
)
1615 wchar_t *wstr
= g_new0(wchar_t, len
+ 1);
1619 for (count
= 0; count
< len
; count
++) {
1620 /* FIXME: not working for surrogate pairs */
1621 wstr
[count
] = (wchar_t)arr
[count
];
1625 dlen
= wcstombs(NULL
, wstr
, 0) + 1;
1626 dest
= g_malloc(dlen
);
1627 wcstombs(dest
, wstr
, dlen
);
1632 /* Wrapper around write, returns 0 on failure */
1633 static uint64_t write_retry(int fd
, void *buf
, uint64_t size
, off_t offset
)
1637 if (lseek(fd
, offset
, SEEK_SET
) < 0) {
1641 ret
= qemu_write_full(fd
, buf
, size
);
1647 static void usb_mtp_update_object(MTPObject
*parent
, char *name
)
1650 usb_mtp_object_lookup_name(parent
, name
, strlen(name
));
1653 lstat(o
->path
, &o
->stat
);
1657 static void usb_mtp_write_data(MTPState
*s
)
1659 MTPData
*d
= s
->data_out
;
1661 usb_mtp_object_lookup(s
, s
->dataset
.parent_handle
);
1668 switch (d
->write_status
) {
1670 if (!parent
|| !s
->write_pending
) {
1671 usb_mtp_queue_result(s
, RES_INVALID_OBJECTINFO
, d
->trans
,
1676 if (s
->dataset
.filename
) {
1677 path
= g_strdup_printf("%s/%s", parent
->path
, s
->dataset
.filename
);
1678 if (s
->dataset
.format
== FMT_ASSOCIATION
) {
1679 d
->fd
= mkdir(path
, mask
);
1682 d
->fd
= open(path
, O_CREAT
| O_WRONLY
|
1683 O_CLOEXEC
| O_NOFOLLOW
, mask
);
1685 usb_mtp_queue_result(s
, RES_STORE_FULL
, d
->trans
,
1690 /* Return success if initiator sent 0 sized data */
1691 if (!s
->dataset
.size
) {
1694 if (d
->length
!= MTP_WRITE_BUF_SZ
&& !d
->pending
) {
1695 d
->write_status
= WRITE_END
;
1699 case WRITE_CONTINUE
:
1701 rc
= write_retry(d
->fd
, d
->data
, d
->data_offset
,
1702 d
->offset
- d
->data_offset
);
1703 if (rc
!= d
->data_offset
) {
1704 usb_mtp_queue_result(s
, RES_STORE_FULL
, d
->trans
,
1708 if (d
->write_status
!= WRITE_END
) {
1711 /* Only for < 4G file sizes */
1712 if (s
->dataset
.size
!= 0xFFFFFFFF && d
->offset
!= s
->dataset
.size
) {
1713 usb_mtp_queue_result(s
, RES_INCOMPLETE_TRANSFER
, d
->trans
,
1717 usb_mtp_update_object(parent
, s
->dataset
.filename
);
1722 usb_mtp_queue_result(s
, RES_OK
, d
->trans
,
1727 * The write dataset is kept around and freed only
1728 * on success or if another write request comes in
1734 g_free(s
->dataset
.filename
);
1735 s
->dataset
.size
= 0;
1737 s
->write_pending
= false;
1740 static void usb_mtp_write_metadata(MTPState
*s
, uint64_t dlen
)
1742 MTPData
*d
= s
->data_out
;
1743 ObjectInfo
*dataset
= (ObjectInfo
*)d
->data
;
1746 MTPObject
*p
= usb_mtp_object_lookup(s
, s
->dataset
.parent_handle
);
1747 uint32_t next_handle
= s
->next_handle
;
1749 assert(!s
->write_pending
);
1752 filename
= utf16_to_str(MIN(dataset
->length
,
1753 dlen
- offsetof(ObjectInfo
, filename
)),
1756 if (strchr(filename
, '/')) {
1757 usb_mtp_queue_result(s
, RES_PARAMETER_NOT_SUPPORTED
, d
->trans
,
1763 o
= usb_mtp_object_lookup_name(p
, filename
, dataset
->length
);
1765 next_handle
= o
->handle
;
1768 s
->dataset
.filename
= filename
;
1769 s
->dataset
.format
= dataset
->format
;
1770 s
->dataset
.size
= dataset
->size
;
1771 s
->write_pending
= true;
1773 if (s
->dataset
.format
== FMT_ASSOCIATION
) {
1774 usb_mtp_write_data(s
);
1775 /* next_handle will be allocated to the newly created dir */
1777 usb_mtp_queue_result(s
, RES_STORE_FULL
, d
->trans
,
1784 usb_mtp_queue_result(s
, RES_OK
, d
->trans
, 3, QEMU_STORAGE_ID
,
1785 s
->dataset
.parent_handle
, next_handle
);
1788 static void usb_mtp_get_data(MTPState
*s
, mtp_container
*container
,
1791 MTPData
*d
= s
->data_out
;
1793 uint32_t data_len
= p
->iov
.size
;
1797 usb_mtp_queue_result(s
, RES_INVALID_OBJECTINFO
, 0,
1802 /* Total length of incoming data */
1803 total_len
= cpu_to_le32(container
->length
) - sizeof(mtp_container
);
1804 /* Length of data in this packet */
1805 data_len
-= sizeof(mtp_container
);
1806 if (total_len
< MTP_WRITE_BUF_SZ
) {
1807 usb_mtp_realloc(d
, total_len
);
1808 d
->length
+= total_len
;
1810 usb_mtp_realloc(d
, MTP_WRITE_BUF_SZ
- sizeof(mtp_container
));
1811 d
->length
+= MTP_WRITE_BUF_SZ
- sizeof(mtp_container
);
1817 d
->write_status
= WRITE_START
;
1821 memset(d
->data
, 0, d
->length
);
1822 if (d
->length
!= MTP_WRITE_BUF_SZ
) {
1823 usb_mtp_realloc(d
, MTP_WRITE_BUF_SZ
- d
->length
);
1824 d
->length
+= (MTP_WRITE_BUF_SZ
- d
->length
);
1827 d
->write_status
= WRITE_CONTINUE
;
1831 if (d
->length
- d
->data_offset
> data_len
) {
1834 dlen
= d
->length
- d
->data_offset
;
1838 case CMD_SEND_OBJECT_INFO
:
1839 usb_packet_copy(p
, d
->data
+ d
->data_offset
, dlen
);
1841 d
->data_offset
+= dlen
;
1842 if (d
->data_offset
== d
->length
) {
1843 /* The operation might have already failed */
1845 usb_mtp_write_metadata(s
, dlen
);
1847 usb_mtp_data_free(s
->data_out
);
1852 case CMD_SEND_OBJECT
:
1853 usb_packet_copy(p
, d
->data
+ d
->data_offset
, dlen
);
1855 d
->data_offset
+= dlen
;
1856 if ((p
->iov
.size
% 64) || !p
->iov
.size
) {
1857 assert((s
->dataset
.size
== 0xFFFFFFFF) ||
1858 (s
->dataset
.size
== d
->offset
));
1860 if (d
->length
== MTP_WRITE_BUF_SZ
) {
1861 d
->write_status
= WRITE_END
;
1863 d
->write_status
= WRITE_START
;
1865 usb_mtp_write_data(s
);
1866 usb_mtp_data_free(s
->data_out
);
1870 if (d
->data_offset
== d
->length
) {
1872 usb_mtp_write_data(s
);
1876 p
->status
= USB_RET_STALL
;
1881 static void usb_mtp_handle_data(USBDevice
*dev
, USBPacket
*p
)
1883 MTPState
*s
= USB_MTP(dev
);
1885 mtp_container container
;
1887 uint16_t container_type
;
1890 switch (p
->ep
->nr
) {
1892 if (s
->data_out
!= NULL
) {
1894 trace_usb_mtp_stall(s
->dev
.addr
, "awaiting data-out");
1895 p
->status
= USB_RET_STALL
;
1898 if (p
->iov
.size
< sizeof(container
)) {
1899 trace_usb_mtp_stall(s
->dev
.addr
, "packet too small");
1900 p
->status
= USB_RET_STALL
;
1903 if (s
->data_in
!= NULL
) {
1904 MTPData
*d
= s
->data_in
;
1905 uint64_t dlen
= d
->length
- d
->offset
;
1907 trace_usb_mtp_data_in(s
->dev
.addr
, d
->trans
, d
->length
);
1908 if (d
->length
+ sizeof(container
) > 0xFFFFFFFF) {
1909 container
.length
= cpu_to_le32(0xFFFFFFFF);
1912 cpu_to_le32(d
->length
+ sizeof(container
));
1914 container
.type
= cpu_to_le16(TYPE_DATA
);
1915 container
.code
= cpu_to_le16(d
->code
);
1916 container
.trans
= cpu_to_le32(d
->trans
);
1917 usb_packet_copy(p
, &container
, sizeof(container
));
1919 if (dlen
> p
->iov
.size
- sizeof(container
)) {
1920 dlen
= p
->iov
.size
- sizeof(container
);
1923 if (dlen
> p
->iov
.size
) {
1928 usb_packet_copy(p
, d
->data
+ d
->offset
, dlen
);
1930 if (d
->alloc
< p
->iov
.size
) {
1931 d
->alloc
= p
->iov
.size
;
1932 d
->data
= g_realloc(d
->data
, d
->alloc
);
1934 rc
= read(d
->fd
, d
->data
, dlen
);
1936 memset(d
->data
, 0, dlen
);
1937 s
->result
->code
= RES_INCOMPLETE_TRANSFER
;
1939 usb_packet_copy(p
, d
->data
, dlen
);
1942 if (d
->offset
== d
->length
) {
1943 usb_mtp_data_free(s
->data_in
);
1946 } else if (s
->result
!= NULL
) {
1947 MTPControl
*r
= s
->result
;
1948 int length
= sizeof(container
) + r
->argc
* sizeof(uint32_t);
1949 if (r
->code
== RES_OK
) {
1950 trace_usb_mtp_success(s
->dev
.addr
, r
->trans
,
1951 (r
->argc
> 0) ? r
->argv
[0] : 0,
1952 (r
->argc
> 1) ? r
->argv
[1] : 0);
1954 trace_usb_mtp_error(s
->dev
.addr
, r
->code
, r
->trans
,
1955 (r
->argc
> 0) ? r
->argv
[0] : 0,
1956 (r
->argc
> 1) ? r
->argv
[1] : 0);
1958 container
.length
= cpu_to_le32(length
);
1959 container
.type
= cpu_to_le16(TYPE_RESPONSE
);
1960 container
.code
= cpu_to_le16(r
->code
);
1961 container
.trans
= cpu_to_le32(r
->trans
);
1962 for (i
= 0; i
< r
->argc
; i
++) {
1963 params
[i
] = cpu_to_le32(r
->argv
[i
]);
1965 usb_packet_copy(p
, &container
, sizeof(container
));
1966 usb_packet_copy(p
, ¶ms
, length
- sizeof(container
));
1972 if (p
->iov
.size
< sizeof(container
)) {
1973 trace_usb_mtp_stall(s
->dev
.addr
, "packet too small");
1974 p
->status
= USB_RET_STALL
;
1977 if ((s
->data_out
!= NULL
) && !s
->data_out
->first
) {
1978 container_type
= TYPE_DATA
;
1980 usb_packet_copy(p
, &container
, sizeof(container
));
1981 container_type
= le16_to_cpu(container
.type
);
1983 switch (container_type
) {
1985 if (s
->data_in
|| s
->data_out
|| s
->result
) {
1986 trace_usb_mtp_stall(s
->dev
.addr
, "transaction inflight");
1987 p
->status
= USB_RET_STALL
;
1990 cmd
.code
= le16_to_cpu(container
.code
);
1991 cmd
.argc
= (le32_to_cpu(container
.length
) - sizeof(container
))
1993 cmd
.trans
= le32_to_cpu(container
.trans
);
1994 if (cmd
.argc
> ARRAY_SIZE(cmd
.argv
)) {
1995 cmd
.argc
= ARRAY_SIZE(cmd
.argv
);
1997 if (p
->iov
.size
< sizeof(container
) + cmd
.argc
* sizeof(uint32_t)) {
1998 trace_usb_mtp_stall(s
->dev
.addr
, "packet too small");
1999 p
->status
= USB_RET_STALL
;
2002 usb_packet_copy(p
, ¶ms
, cmd
.argc
* sizeof(uint32_t));
2003 for (i
= 0; i
< cmd
.argc
; i
++) {
2004 cmd
.argv
[i
] = le32_to_cpu(params
[i
]);
2006 trace_usb_mtp_command(s
->dev
.addr
, cmd
.code
, cmd
.trans
,
2007 (cmd
.argc
> 0) ? cmd
.argv
[0] : 0,
2008 (cmd
.argc
> 1) ? cmd
.argv
[1] : 0,
2009 (cmd
.argc
> 2) ? cmd
.argv
[2] : 0,
2010 (cmd
.argc
> 3) ? cmd
.argv
[3] : 0,
2011 (cmd
.argc
> 4) ? cmd
.argv
[4] : 0);
2012 usb_mtp_command(s
, &cmd
);
2015 /* One of the previous transfers has already errored but the
2016 * responder is still sending data associated with it
2018 if (s
->result
!= NULL
) {
2021 usb_mtp_get_data(s
, &container
, p
);
2024 /* not needed as long as the mtp device is read-only */
2025 p
->status
= USB_RET_STALL
;
2030 #ifdef CONFIG_INOTIFY1
2031 if (!QTAILQ_EMPTY(&s
->events
)) {
2032 struct MTPMonEntry
*e
= QTAILQ_LAST(&s
->events
);
2034 int len
= sizeof(container
) + sizeof(uint32_t);
2036 if (p
->iov
.size
< len
) {
2037 trace_usb_mtp_stall(s
->dev
.addr
,
2038 "packet too small to send event");
2039 p
->status
= USB_RET_STALL
;
2043 QTAILQ_REMOVE(&s
->events
, e
, next
);
2044 container
.length
= cpu_to_le32(len
);
2045 container
.type
= cpu_to_le32(TYPE_EVENT
);
2046 container
.code
= cpu_to_le16(e
->event
);
2047 container
.trans
= 0; /* no trans specific events */
2048 handle
= cpu_to_le32(e
->handle
);
2049 usb_packet_copy(p
, &container
, sizeof(container
));
2050 usb_packet_copy(p
, &handle
, sizeof(uint32_t));
2055 p
->status
= USB_RET_NAK
;
2058 trace_usb_mtp_stall(s
->dev
.addr
, "invalid endpoint");
2059 p
->status
= USB_RET_STALL
;
2063 if (p
->actual_length
== 0) {
2064 trace_usb_mtp_nak(s
->dev
.addr
, p
->ep
->nr
);
2065 p
->status
= USB_RET_NAK
;
2068 trace_usb_mtp_xfer(s
->dev
.addr
, p
->ep
->nr
, p
->actual_length
,
2074 static void usb_mtp_realize(USBDevice
*dev
, Error
**errp
)
2076 MTPState
*s
= USB_MTP(dev
);
2078 usb_desc_create_serial(dev
);
2080 QTAILQ_INIT(&s
->objects
);
2081 if (s
->desc
== NULL
) {
2082 if (s
->root
== NULL
) {
2083 error_setg(errp
, "usb-mtp: rootdir property must be configured");
2086 s
->desc
= strrchr(s
->root
, '/');
2087 if (s
->desc
&& s
->desc
[0]) {
2088 s
->desc
= g_strdup(s
->desc
+ 1);
2090 s
->desc
= g_strdup("none");
2093 /* Mark store as RW */
2095 s
->flags
|= (1 << MTP_FLAG_WRITABLE
);
2100 static const VMStateDescription vmstate_usb_mtp
= {
2104 .minimum_version_id
= 1,
2105 .fields
= (VMStateField
[]) {
2106 VMSTATE_USB_DEVICE(dev
, MTPState
),
2107 VMSTATE_END_OF_LIST()
2111 static Property mtp_properties
[] = {
2112 DEFINE_PROP_STRING("rootdir", MTPState
, root
),
2113 DEFINE_PROP_STRING("desc", MTPState
, desc
),
2114 DEFINE_PROP_BOOL("readonly", MTPState
, readonly
, true),
2115 DEFINE_PROP_END_OF_LIST(),
2118 static void usb_mtp_class_initfn(ObjectClass
*klass
, void *data
)
2120 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2121 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
2123 uc
->realize
= usb_mtp_realize
;
2124 uc
->product_desc
= "QEMU USB MTP";
2125 uc
->usb_desc
= &desc
;
2126 uc
->cancel_packet
= usb_mtp_cancel_packet
;
2127 uc
->handle_attach
= usb_desc_attach
;
2128 uc
->handle_reset
= usb_mtp_handle_reset
;
2129 uc
->handle_control
= usb_mtp_handle_control
;
2130 uc
->handle_data
= usb_mtp_handle_data
;
2131 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2132 dc
->desc
= "USB Media Transfer Protocol device";
2133 dc
->fw_name
= "mtp";
2134 dc
->vmsd
= &vmstate_usb_mtp
;
2135 dc
->props
= mtp_properties
;
2138 static TypeInfo mtp_info
= {
2139 .name
= TYPE_USB_MTP
,
2140 .parent
= TYPE_USB_DEVICE
,
2141 .instance_size
= sizeof(MTPState
),
2142 .class_init
= usb_mtp_class_initfn
,
2145 static void usb_mtp_register_types(void)
2147 type_register_static(&mtp_info
);
2150 type_init(usb_mtp_register_types
)