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 "qapi/error.h"
21 #include "qemu/main-loop.h"
24 #include "qemu-common.h"
28 #include "hw/usb/desc.h"
30 /* ----------------------------------------------------------------------- */
32 enum mtp_container_type
{
41 CMD_GET_DEVICE_INFO
= 0x1001,
42 CMD_OPEN_SESSION
= 0x1002,
43 CMD_CLOSE_SESSION
= 0x1003,
44 CMD_GET_STORAGE_IDS
= 0x1004,
45 CMD_GET_STORAGE_INFO
= 0x1005,
46 CMD_GET_NUM_OBJECTS
= 0x1006,
47 CMD_GET_OBJECT_HANDLES
= 0x1007,
48 CMD_GET_OBJECT_INFO
= 0x1008,
49 CMD_GET_OBJECT
= 0x1009,
50 CMD_GET_PARTIAL_OBJECT
= 0x101b,
54 RES_GENERAL_ERROR
= 0x2002,
55 RES_SESSION_NOT_OPEN
= 0x2003,
56 RES_INVALID_TRANSACTION_ID
= 0x2004,
57 RES_OPERATION_NOT_SUPPORTED
= 0x2005,
58 RES_PARAMETER_NOT_SUPPORTED
= 0x2006,
59 RES_INCOMPLETE_TRANSFER
= 0x2007,
60 RES_INVALID_STORAGE_ID
= 0x2008,
61 RES_INVALID_OBJECT_HANDLE
= 0x2009,
62 RES_SPEC_BY_FORMAT_UNSUPPORTED
= 0x2014,
63 RES_INVALID_PARENT_OBJECT
= 0x201a,
64 RES_INVALID_PARAMETER
= 0x201d,
65 RES_SESSION_ALREADY_OPEN
= 0x201e,
68 FMT_UNDEFINED_OBJECT
= 0x3000,
69 FMT_ASSOCIATION
= 0x3001,
72 EVT_OBJ_ADDED
= 0x4002,
73 EVT_OBJ_REMOVED
= 0x4003,
74 EVT_OBJ_INFO_CHANGED
= 0x4007,
82 } QEMU_PACKED mtp_container
;
84 /* ----------------------------------------------------------------------- */
86 typedef struct MTPState MTPState
;
87 typedef struct MTPControl MTPControl
;
88 typedef struct MTPData MTPData
;
89 typedef struct MTPObject MTPObject
;
97 #ifdef CONFIG_INOTIFY1
98 typedef struct MTPMonEntry MTPMonEntry
;
104 QTAILQ_ENTRY(MTPMonEntry
) next
;
132 #ifdef CONFIG_INOTIFY1
133 /* inotify watch cookie */
138 QLIST_HEAD(, MTPObject
) children
;
139 QLIST_ENTRY(MTPObject
) list
;
141 QTAILQ_ENTRY(MTPObject
) next
;
154 uint32_t next_handle
;
156 QTAILQ_HEAD(, MTPObject
) objects
;
157 #ifdef CONFIG_INOTIFY1
158 /* inotify descriptor */
160 QTAILQ_HEAD(events
, MTPMonEntry
) events
;
164 #define TYPE_USB_MTP "usb-mtp"
165 #define USB_MTP(obj) OBJECT_CHECK(MTPState, (obj), TYPE_USB_MTP)
167 #define QEMU_STORAGE_ID 0x00010001
169 #define MTP_FLAG_WRITABLE 0
171 #define FLAG_SET(_mtp, _flag) ((_mtp)->flags & (1 << (_flag)))
173 /* ----------------------------------------------------------------------- */
175 #define MTP_MANUFACTURER "QEMU"
176 #define MTP_PRODUCT "QEMU filesharing"
179 STR_MANUFACTURER
= 1,
188 static const USBDescStrings desc_strings
= {
189 [STR_MANUFACTURER
] = MTP_MANUFACTURER
,
190 [STR_PRODUCT
] = MTP_PRODUCT
,
191 [STR_SERIALNUMBER
] = "34617",
193 [STR_CONFIG_FULL
] = "Full speed config (usb 1.1)",
194 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
195 [STR_CONFIG_SUPER
] = "Super speed config (usb 3.0)",
198 static const USBDescIface desc_iface_full
= {
199 .bInterfaceNumber
= 0,
201 .bInterfaceClass
= USB_CLASS_STILL_IMAGE
,
202 .bInterfaceSubClass
= 0x01,
203 .bInterfaceProtocol
= 0x01,
204 .iInterface
= STR_MTP
,
205 .eps
= (USBDescEndpoint
[]) {
207 .bEndpointAddress
= USB_DIR_IN
| EP_DATA_IN
,
208 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
209 .wMaxPacketSize
= 64,
211 .bEndpointAddress
= USB_DIR_OUT
| EP_DATA_OUT
,
212 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
213 .wMaxPacketSize
= 64,
215 .bEndpointAddress
= USB_DIR_IN
| EP_EVENT
,
216 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
217 .wMaxPacketSize
= 64,
223 static const USBDescDevice desc_device_full
= {
225 .bMaxPacketSize0
= 8,
226 .bNumConfigurations
= 1,
227 .confs
= (USBDescConfig
[]) {
230 .bConfigurationValue
= 1,
231 .iConfiguration
= STR_CONFIG_FULL
,
232 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
235 .ifs
= &desc_iface_full
,
240 static const USBDescIface desc_iface_high
= {
241 .bInterfaceNumber
= 0,
243 .bInterfaceClass
= USB_CLASS_STILL_IMAGE
,
244 .bInterfaceSubClass
= 0x01,
245 .bInterfaceProtocol
= 0x01,
246 .iInterface
= STR_MTP
,
247 .eps
= (USBDescEndpoint
[]) {
249 .bEndpointAddress
= USB_DIR_IN
| EP_DATA_IN
,
250 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
251 .wMaxPacketSize
= 512,
253 .bEndpointAddress
= USB_DIR_OUT
| EP_DATA_OUT
,
254 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
255 .wMaxPacketSize
= 512,
257 .bEndpointAddress
= USB_DIR_IN
| EP_EVENT
,
258 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
259 .wMaxPacketSize
= 64,
265 static const USBDescDevice desc_device_high
= {
267 .bMaxPacketSize0
= 64,
268 .bNumConfigurations
= 1,
269 .confs
= (USBDescConfig
[]) {
272 .bConfigurationValue
= 1,
273 .iConfiguration
= STR_CONFIG_HIGH
,
274 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
277 .ifs
= &desc_iface_high
,
282 static const USBDescMSOS desc_msos
= {
283 .CompatibleID
= "MTP",
284 .SelectiveSuspendEnabled
= true,
287 static const USBDesc desc
= {
289 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
292 .iManufacturer
= STR_MANUFACTURER
,
293 .iProduct
= STR_PRODUCT
,
294 .iSerialNumber
= STR_SERIALNUMBER
,
296 .full
= &desc_device_full
,
297 .high
= &desc_device_high
,
302 /* ----------------------------------------------------------------------- */
304 static MTPObject
*usb_mtp_object_alloc(MTPState
*s
, uint32_t handle
,
305 MTPObject
*parent
, char *name
)
307 MTPObject
*o
= g_new0(MTPObject
, 1);
309 if (name
[0] == '.') {
315 o
->name
= g_strdup(name
);
316 if (parent
== NULL
) {
317 o
->path
= g_strdup(name
);
319 o
->path
= g_strdup_printf("%s/%s", parent
->path
, name
);
322 if (lstat(o
->path
, &o
->stat
) != 0) {
325 if (S_ISREG(o
->stat
.st_mode
)) {
326 o
->format
= FMT_UNDEFINED_OBJECT
;
327 } else if (S_ISDIR(o
->stat
.st_mode
)) {
328 o
->format
= FMT_ASSOCIATION
;
333 if (access(o
->path
, R_OK
) != 0) {
337 trace_usb_mtp_object_alloc(s
->dev
.addr
, o
->handle
, o
->path
);
339 QTAILQ_INSERT_TAIL(&s
->objects
, o
, next
);
349 static void usb_mtp_object_free(MTPState
*s
, MTPObject
*o
)
357 trace_usb_mtp_object_free(s
->dev
.addr
, o
->handle
, o
->path
);
359 QTAILQ_REMOVE(&s
->objects
, o
, next
);
361 QLIST_REMOVE(o
, list
);
362 o
->parent
->nchildren
--;
365 while (!QLIST_EMPTY(&o
->children
)) {
366 iter
= QLIST_FIRST(&o
->children
);
367 usb_mtp_object_free(s
, iter
);
374 static MTPObject
*usb_mtp_object_lookup(MTPState
*s
, uint32_t handle
)
378 QTAILQ_FOREACH(o
, &s
->objects
, next
) {
379 if (o
->handle
== handle
) {
386 static MTPObject
*usb_mtp_add_child(MTPState
*s
, MTPObject
*o
,
390 usb_mtp_object_alloc(s
, s
->next_handle
++, o
, name
);
393 trace_usb_mtp_add_child(s
->dev
.addr
, child
->handle
, child
->path
);
394 QLIST_INSERT_HEAD(&o
->children
, child
, list
);
397 if (child
->format
== FMT_ASSOCIATION
) {
398 QLIST_INIT(&child
->children
);
405 #ifdef CONFIG_INOTIFY1
406 static MTPObject
*usb_mtp_object_lookup_name(MTPObject
*parent
,
411 QLIST_FOREACH(iter
, &parent
->children
, list
) {
412 if (strncmp(iter
->name
, name
, len
) == 0) {
420 static MTPObject
*usb_mtp_object_lookup_wd(MTPState
*s
, int wd
)
424 QTAILQ_FOREACH(iter
, &s
->objects
, next
) {
425 if (iter
->watchfd
== wd
) {
433 static void inotify_watchfn(void *arg
)
437 /* From the man page: atleast one event can be read */
439 char buf
[sizeof(struct inotify_event
) + NAME_MAX
+ 1];
442 bytes
= read(s
->inotifyfd
, buf
, sizeof(buf
));
446 /* Better luck next time */
451 * TODO: Ignore initiator initiated events.
452 * For now we are good because the store is RO
456 struct inotify_event
*event
= (struct inotify_event
*)p
;
458 uint32_t mask
= event
->mask
& (IN_CREATE
| IN_DELETE
|
459 IN_MODIFY
| IN_IGNORED
);
460 MTPObject
*parent
= usb_mtp_object_lookup_wd(s
, event
->wd
);
461 MTPMonEntry
*entry
= NULL
;
464 pos
= pos
+ sizeof(struct inotify_event
) + event
->len
;
473 if (usb_mtp_object_lookup_name
474 (parent
, event
->name
, event
->len
)) {
475 /* Duplicate create event */
478 entry
= g_new0(MTPMonEntry
, 1);
479 entry
->handle
= s
->next_handle
;
480 entry
->event
= EVT_OBJ_ADDED
;
481 o
= usb_mtp_add_child(s
, parent
, event
->name
);
486 o
->watchfd
= watchfd
;
487 trace_usb_mtp_inotify_event(s
->dev
.addr
, event
->name
,
488 event
->mask
, "Obj Added");
493 * The kernel issues a IN_IGNORED event
494 * when a dir containing a watchpoint is
495 * deleted, so we don't have to delete the
498 o
= usb_mtp_object_lookup_name(parent
, event
->name
, event
->len
);
502 entry
= g_new0(MTPMonEntry
, 1);
503 entry
->handle
= o
->handle
;
504 entry
->event
= EVT_OBJ_REMOVED
;
505 trace_usb_mtp_inotify_event(s
->dev
.addr
, o
->path
,
506 event
->mask
, "Obj Deleted");
507 usb_mtp_object_free(s
, o
);
511 o
= usb_mtp_object_lookup_name(parent
, event
->name
, event
->len
);
515 entry
= g_new0(MTPMonEntry
, 1);
516 entry
->handle
= o
->handle
;
517 entry
->event
= EVT_OBJ_INFO_CHANGED
;
518 trace_usb_mtp_inotify_event(s
->dev
.addr
, o
->path
,
519 event
->mask
, "Obj Modified");
523 o
= usb_mtp_object_lookup_name(parent
, event
->name
, event
->len
);
524 trace_usb_mtp_inotify_event(s
->dev
.addr
, o
->path
,
525 event
->mask
, "Obj ignored");
529 fprintf(stderr
, "usb-mtp: failed to parse inotify event\n");
534 QTAILQ_INSERT_HEAD(&s
->events
, entry
, next
);
540 static int usb_mtp_inotify_init(MTPState
*s
)
544 fd
= inotify_init1(IN_NONBLOCK
);
549 QTAILQ_INIT(&s
->events
);
552 qemu_set_fd_handler(fd
, inotify_watchfn
, NULL
, s
);
557 static void usb_mtp_inotify_cleanup(MTPState
*s
)
565 qemu_set_fd_handler(s
->inotifyfd
, NULL
, NULL
, s
);
568 QTAILQ_FOREACH_SAFE(e
, &s
->events
, next
, p
) {
569 QTAILQ_REMOVE(&s
->events
, e
, next
);
574 static int usb_mtp_add_watch(int inotifyfd
, char *path
)
576 uint32_t mask
= IN_CREATE
| IN_DELETE
| IN_MODIFY
|
579 return inotify_add_watch(inotifyfd
, path
, mask
);
583 static void usb_mtp_object_readdir(MTPState
*s
, MTPObject
*o
)
585 struct dirent
*entry
;
588 if (o
->have_children
) {
591 o
->have_children
= true;
593 dir
= opendir(o
->path
);
597 #ifdef CONFIG_INOTIFY1
598 int watchfd
= usb_mtp_add_watch(s
->inotifyfd
, o
->path
);
600 fprintf(stderr
, "usb-mtp: failed to add watch for %s\n", o
->path
);
602 trace_usb_mtp_inotify_event(s
->dev
.addr
, o
->path
,
604 o
->watchfd
= watchfd
;
607 while ((entry
= readdir(dir
)) != NULL
) {
608 usb_mtp_add_child(s
, o
, entry
->d_name
);
613 /* ----------------------------------------------------------------------- */
615 static MTPData
*usb_mtp_data_alloc(MTPControl
*c
)
617 MTPData
*data
= g_new0(MTPData
, 1);
619 data
->code
= c
->code
;
620 data
->trans
= c
->trans
;
626 static void usb_mtp_data_free(MTPData
*data
)
631 if (data
->fd
!= -1) {
638 static void usb_mtp_realloc(MTPData
*data
, uint32_t bytes
)
640 if (data
->length
+ bytes
<= data
->alloc
) {
643 data
->alloc
= (data
->length
+ bytes
+ 0xff) & ~0xff;
644 data
->data
= g_realloc(data
->data
, data
->alloc
);
647 static void usb_mtp_add_u8(MTPData
*data
, uint8_t val
)
649 usb_mtp_realloc(data
, 1);
650 data
->data
[data
->length
++] = val
;
653 static void usb_mtp_add_u16(MTPData
*data
, uint16_t val
)
655 usb_mtp_realloc(data
, 2);
656 data
->data
[data
->length
++] = (val
>> 0) & 0xff;
657 data
->data
[data
->length
++] = (val
>> 8) & 0xff;
660 static void usb_mtp_add_u32(MTPData
*data
, uint32_t val
)
662 usb_mtp_realloc(data
, 4);
663 data
->data
[data
->length
++] = (val
>> 0) & 0xff;
664 data
->data
[data
->length
++] = (val
>> 8) & 0xff;
665 data
->data
[data
->length
++] = (val
>> 16) & 0xff;
666 data
->data
[data
->length
++] = (val
>> 24) & 0xff;
669 static void usb_mtp_add_u64(MTPData
*data
, uint64_t val
)
671 usb_mtp_realloc(data
, 8);
672 data
->data
[data
->length
++] = (val
>> 0) & 0xff;
673 data
->data
[data
->length
++] = (val
>> 8) & 0xff;
674 data
->data
[data
->length
++] = (val
>> 16) & 0xff;
675 data
->data
[data
->length
++] = (val
>> 24) & 0xff;
676 data
->data
[data
->length
++] = (val
>> 32) & 0xff;
677 data
->data
[data
->length
++] = (val
>> 40) & 0xff;
678 data
->data
[data
->length
++] = (val
>> 48) & 0xff;
679 data
->data
[data
->length
++] = (val
>> 56) & 0xff;
682 static void usb_mtp_add_u16_array(MTPData
*data
, uint32_t len
,
683 const uint16_t *vals
)
687 usb_mtp_add_u32(data
, len
);
688 for (i
= 0; i
< len
; i
++) {
689 usb_mtp_add_u16(data
, vals
[i
]);
693 static void usb_mtp_add_u32_array(MTPData
*data
, uint32_t len
,
694 const uint32_t *vals
)
698 usb_mtp_add_u32(data
, len
);
699 for (i
= 0; i
< len
; i
++) {
700 usb_mtp_add_u32(data
, vals
[i
]);
704 static void usb_mtp_add_wstr(MTPData
*data
, const wchar_t *str
)
706 uint32_t len
= wcslen(str
);
710 len
++; /* include terminating L'\0' */
713 usb_mtp_add_u8(data
, len
);
714 for (i
= 0; i
< len
; i
++) {
715 usb_mtp_add_u16(data
, str
[i
]);
719 static void usb_mtp_add_str(MTPData
*data
, const char *str
)
721 uint32_t len
= strlen(str
)+1;
722 wchar_t *wstr
= g_new(wchar_t, len
);
725 ret
= mbstowcs(wstr
, str
, len
);
727 usb_mtp_add_wstr(data
, L
"Oops");
729 usb_mtp_add_wstr(data
, wstr
);
735 static void usb_mtp_add_time(MTPData
*data
, time_t time
)
740 gmtime_r(&time
, &tm
);
741 strftime(buf
, sizeof(buf
), "%Y%m%dT%H%M%S", &tm
);
742 usb_mtp_add_str(data
, buf
);
745 /* ----------------------------------------------------------------------- */
747 static void usb_mtp_queue_result(MTPState
*s
, uint16_t code
, uint32_t trans
,
748 int argc
, uint32_t arg0
, uint32_t arg1
)
750 MTPControl
*c
= g_new0(MTPControl
, 1);
762 assert(s
->result
== NULL
);
766 /* ----------------------------------------------------------------------- */
768 static MTPData
*usb_mtp_get_device_info(MTPState
*s
, MTPControl
*c
)
770 static const uint16_t ops
[] = {
775 CMD_GET_STORAGE_INFO
,
777 CMD_GET_OBJECT_HANDLES
,
780 CMD_GET_PARTIAL_OBJECT
,
782 static const uint16_t fmt
[] = {
783 FMT_UNDEFINED_OBJECT
,
786 MTPData
*d
= usb_mtp_data_alloc(c
);
788 trace_usb_mtp_op_get_device_info(s
->dev
.addr
);
790 usb_mtp_add_u16(d
, 100);
791 usb_mtp_add_u32(d
, 0x00000006);
792 usb_mtp_add_u16(d
, 0x0064);
793 usb_mtp_add_wstr(d
, L
"");
794 usb_mtp_add_u16(d
, 0x0000);
796 usb_mtp_add_u16_array(d
, ARRAY_SIZE(ops
), ops
);
797 usb_mtp_add_u16_array(d
, 0, NULL
);
798 usb_mtp_add_u16_array(d
, 0, NULL
);
799 usb_mtp_add_u16_array(d
, 0, NULL
);
800 usb_mtp_add_u16_array(d
, ARRAY_SIZE(fmt
), fmt
);
802 usb_mtp_add_wstr(d
, L
"" MTP_MANUFACTURER
);
803 usb_mtp_add_wstr(d
, L
"" MTP_PRODUCT
);
804 usb_mtp_add_wstr(d
, L
"0.1");
805 usb_mtp_add_wstr(d
, L
"0123456789abcdef0123456789abcdef");
810 static MTPData
*usb_mtp_get_storage_ids(MTPState
*s
, MTPControl
*c
)
812 static const uint32_t ids
[] = {
815 MTPData
*d
= usb_mtp_data_alloc(c
);
817 trace_usb_mtp_op_get_storage_ids(s
->dev
.addr
);
819 usb_mtp_add_u32_array(d
, ARRAY_SIZE(ids
), ids
);
824 static MTPData
*usb_mtp_get_storage_info(MTPState
*s
, MTPControl
*c
)
826 MTPData
*d
= usb_mtp_data_alloc(c
);
830 trace_usb_mtp_op_get_storage_info(s
->dev
.addr
);
832 if (FLAG_SET(s
, MTP_FLAG_WRITABLE
)) {
833 usb_mtp_add_u16(d
, 0x0003);
834 usb_mtp_add_u16(d
, 0x0002);
835 usb_mtp_add_u16(d
, 0x0000);
837 usb_mtp_add_u16(d
, 0x0001);
838 usb_mtp_add_u16(d
, 0x0002);
839 usb_mtp_add_u16(d
, 0x0001);
842 rc
= statvfs(s
->root
, &buf
);
844 usb_mtp_add_u64(d
, (uint64_t)buf
.f_frsize
* buf
.f_blocks
);
845 usb_mtp_add_u64(d
, (uint64_t)buf
.f_bavail
* buf
.f_blocks
);
846 usb_mtp_add_u32(d
, buf
.f_ffree
);
848 usb_mtp_add_u64(d
, 0xffffffff);
849 usb_mtp_add_u64(d
, 0xffffffff);
850 usb_mtp_add_u32(d
, 0xffffffff);
853 usb_mtp_add_str(d
, s
->desc
);
854 usb_mtp_add_wstr(d
, L
"123456789abcdef");
858 static MTPData
*usb_mtp_get_object_handles(MTPState
*s
, MTPControl
*c
,
861 MTPData
*d
= usb_mtp_data_alloc(c
);
862 uint32_t i
= 0, handles
[o
->nchildren
];
865 trace_usb_mtp_op_get_object_handles(s
->dev
.addr
, o
->handle
, o
->path
);
867 QLIST_FOREACH(iter
, &o
->children
, list
) {
868 handles
[i
++] = iter
->handle
;
870 assert(i
== o
->nchildren
);
871 usb_mtp_add_u32_array(d
, o
->nchildren
, handles
);
876 static MTPData
*usb_mtp_get_object_info(MTPState
*s
, MTPControl
*c
,
879 MTPData
*d
= usb_mtp_data_alloc(c
);
881 trace_usb_mtp_op_get_object_info(s
->dev
.addr
, o
->handle
, o
->path
);
883 usb_mtp_add_u32(d
, QEMU_STORAGE_ID
);
884 usb_mtp_add_u16(d
, o
->format
);
885 usb_mtp_add_u16(d
, 0);
886 usb_mtp_add_u32(d
, o
->stat
.st_size
);
888 usb_mtp_add_u16(d
, 0);
889 usb_mtp_add_u32(d
, 0);
890 usb_mtp_add_u32(d
, 0);
891 usb_mtp_add_u32(d
, 0);
892 usb_mtp_add_u32(d
, 0);
893 usb_mtp_add_u32(d
, 0);
894 usb_mtp_add_u32(d
, 0);
897 usb_mtp_add_u32(d
, o
->parent
->handle
);
899 usb_mtp_add_u32(d
, 0);
901 if (o
->format
== FMT_ASSOCIATION
) {
902 usb_mtp_add_u16(d
, 0x0001);
903 usb_mtp_add_u32(d
, 0x00000001);
904 usb_mtp_add_u32(d
, 0);
906 usb_mtp_add_u16(d
, 0);
907 usb_mtp_add_u32(d
, 0);
908 usb_mtp_add_u32(d
, 0);
911 usb_mtp_add_str(d
, o
->name
);
912 usb_mtp_add_time(d
, o
->stat
.st_ctime
);
913 usb_mtp_add_time(d
, o
->stat
.st_mtime
);
914 usb_mtp_add_wstr(d
, L
"");
919 static MTPData
*usb_mtp_get_object(MTPState
*s
, MTPControl
*c
,
922 MTPData
*d
= usb_mtp_data_alloc(c
);
924 trace_usb_mtp_op_get_object(s
->dev
.addr
, o
->handle
, o
->path
);
926 d
->fd
= open(o
->path
, O_RDONLY
);
928 usb_mtp_data_free(d
);
931 d
->length
= o
->stat
.st_size
;
933 d
->data
= g_malloc(d
->alloc
);
937 static MTPData
*usb_mtp_get_partial_object(MTPState
*s
, MTPControl
*c
,
940 MTPData
*d
= usb_mtp_data_alloc(c
);
943 trace_usb_mtp_op_get_partial_object(s
->dev
.addr
, o
->handle
, o
->path
,
944 c
->argv
[1], c
->argv
[2]);
946 d
->fd
= open(o
->path
, O_RDONLY
);
948 usb_mtp_data_free(d
);
953 if (offset
> o
->stat
.st_size
) {
954 offset
= o
->stat
.st_size
;
956 if (lseek(d
->fd
, offset
, SEEK_SET
) < 0) {
957 usb_mtp_data_free(d
);
961 d
->length
= c
->argv
[2];
962 if (d
->length
> o
->stat
.st_size
- offset
) {
963 d
->length
= o
->stat
.st_size
- offset
;
969 static void usb_mtp_command(MTPState
*s
, MTPControl
*c
)
971 MTPData
*data_in
= NULL
;
973 uint32_t nres
= 0, res0
= 0;
976 if (c
->code
>= CMD_CLOSE_SESSION
&& s
->session
== 0) {
977 usb_mtp_queue_result(s
, RES_SESSION_NOT_OPEN
,
982 /* process commands */
984 case CMD_GET_DEVICE_INFO
:
985 data_in
= usb_mtp_get_device_info(s
, c
);
987 case CMD_OPEN_SESSION
:
989 usb_mtp_queue_result(s
, RES_SESSION_ALREADY_OPEN
,
990 c
->trans
, 1, s
->session
, 0);
993 if (c
->argv
[0] == 0) {
994 usb_mtp_queue_result(s
, RES_INVALID_PARAMETER
,
998 trace_usb_mtp_op_open_session(s
->dev
.addr
);
999 s
->session
= c
->argv
[0];
1000 usb_mtp_object_alloc(s
, s
->next_handle
++, NULL
, s
->root
);
1001 #ifdef CONFIG_INOTIFY1
1002 if (usb_mtp_inotify_init(s
)) {
1003 fprintf(stderr
, "usb-mtp: file monitoring init failed\n");
1007 case CMD_CLOSE_SESSION
:
1008 trace_usb_mtp_op_close_session(s
->dev
.addr
);
1011 #ifdef CONFIG_INOTIFY1
1012 usb_mtp_inotify_cleanup(s
);
1014 usb_mtp_object_free(s
, QTAILQ_FIRST(&s
->objects
));
1015 assert(QTAILQ_EMPTY(&s
->objects
));
1017 case CMD_GET_STORAGE_IDS
:
1018 data_in
= usb_mtp_get_storage_ids(s
, c
);
1020 case CMD_GET_STORAGE_INFO
:
1021 if (c
->argv
[0] != QEMU_STORAGE_ID
&&
1022 c
->argv
[0] != 0xffffffff) {
1023 usb_mtp_queue_result(s
, RES_INVALID_STORAGE_ID
,
1027 data_in
= usb_mtp_get_storage_info(s
, c
);
1029 case CMD_GET_NUM_OBJECTS
:
1030 case CMD_GET_OBJECT_HANDLES
:
1031 if (c
->argv
[0] != QEMU_STORAGE_ID
&&
1032 c
->argv
[0] != 0xffffffff) {
1033 usb_mtp_queue_result(s
, RES_INVALID_STORAGE_ID
,
1037 if (c
->argv
[1] != 0x00000000) {
1038 usb_mtp_queue_result(s
, RES_SPEC_BY_FORMAT_UNSUPPORTED
,
1042 if (c
->argv
[2] == 0x00000000 ||
1043 c
->argv
[2] == 0xffffffff) {
1044 o
= QTAILQ_FIRST(&s
->objects
);
1046 o
= usb_mtp_object_lookup(s
, c
->argv
[2]);
1049 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
1053 if (o
->format
!= FMT_ASSOCIATION
) {
1054 usb_mtp_queue_result(s
, RES_INVALID_PARENT_OBJECT
,
1058 usb_mtp_object_readdir(s
, o
);
1059 if (c
->code
== CMD_GET_NUM_OBJECTS
) {
1060 trace_usb_mtp_op_get_num_objects(s
->dev
.addr
, o
->handle
, o
->path
);
1062 res0
= o
->nchildren
;
1064 data_in
= usb_mtp_get_object_handles(s
, c
, o
);
1067 case CMD_GET_OBJECT_INFO
:
1068 o
= usb_mtp_object_lookup(s
, c
->argv
[0]);
1070 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
1074 data_in
= usb_mtp_get_object_info(s
, c
, o
);
1076 case CMD_GET_OBJECT
:
1077 o
= usb_mtp_object_lookup(s
, c
->argv
[0]);
1079 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
1083 if (o
->format
== FMT_ASSOCIATION
) {
1084 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
1088 data_in
= usb_mtp_get_object(s
, c
, o
);
1089 if (data_in
== NULL
) {
1090 usb_mtp_queue_result(s
, RES_GENERAL_ERROR
,
1095 case CMD_GET_PARTIAL_OBJECT
:
1096 o
= usb_mtp_object_lookup(s
, c
->argv
[0]);
1098 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
1102 if (o
->format
== FMT_ASSOCIATION
) {
1103 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
1107 data_in
= usb_mtp_get_partial_object(s
, c
, o
);
1108 if (data_in
== NULL
) {
1109 usb_mtp_queue_result(s
, RES_GENERAL_ERROR
,
1114 res0
= data_in
->length
;
1117 trace_usb_mtp_op_unknown(s
->dev
.addr
, c
->code
);
1118 usb_mtp_queue_result(s
, RES_OPERATION_NOT_SUPPORTED
,
1123 /* return results on success */
1125 assert(s
->data_in
== NULL
);
1126 s
->data_in
= data_in
;
1128 usb_mtp_queue_result(s
, RES_OK
, c
->trans
, nres
, res0
, 0);
1131 /* ----------------------------------------------------------------------- */
1133 static void usb_mtp_handle_reset(USBDevice
*dev
)
1135 MTPState
*s
= USB_MTP(dev
);
1137 trace_usb_mtp_reset(s
->dev
.addr
);
1139 #ifdef CONFIG_INOTIFY1
1140 usb_mtp_inotify_cleanup(s
);
1142 usb_mtp_object_free(s
, QTAILQ_FIRST(&s
->objects
));
1144 usb_mtp_data_free(s
->data_in
);
1146 usb_mtp_data_free(s
->data_out
);
1152 static void usb_mtp_handle_control(USBDevice
*dev
, USBPacket
*p
,
1153 int request
, int value
, int index
,
1154 int length
, uint8_t *data
)
1158 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
1163 trace_usb_mtp_stall(dev
->addr
, "unknown control request");
1164 p
->status
= USB_RET_STALL
;
1167 static void usb_mtp_cancel_packet(USBDevice
*dev
, USBPacket
*p
)
1169 /* we don't use async packets, so this should never be called */
1170 fprintf(stderr
, "%s\n", __func__
);
1173 static void usb_mtp_handle_data(USBDevice
*dev
, USBPacket
*p
)
1175 MTPState
*s
= USB_MTP(dev
);
1177 mtp_container container
;
1181 switch (p
->ep
->nr
) {
1183 if (s
->data_out
!= NULL
) {
1185 trace_usb_mtp_stall(s
->dev
.addr
, "awaiting data-out");
1186 p
->status
= USB_RET_STALL
;
1189 if (p
->iov
.size
< sizeof(container
)) {
1190 trace_usb_mtp_stall(s
->dev
.addr
, "packet too small");
1191 p
->status
= USB_RET_STALL
;
1194 if (s
->data_in
!= NULL
) {
1195 MTPData
*d
= s
->data_in
;
1196 int dlen
= d
->length
- d
->offset
;
1198 trace_usb_mtp_data_in(s
->dev
.addr
, d
->trans
, d
->length
);
1199 container
.length
= cpu_to_le32(d
->length
+ sizeof(container
));
1200 container
.type
= cpu_to_le16(TYPE_DATA
);
1201 container
.code
= cpu_to_le16(d
->code
);
1202 container
.trans
= cpu_to_le32(d
->trans
);
1203 usb_packet_copy(p
, &container
, sizeof(container
));
1205 if (dlen
> p
->iov
.size
- sizeof(container
)) {
1206 dlen
= p
->iov
.size
- sizeof(container
);
1209 if (dlen
> p
->iov
.size
) {
1214 usb_packet_copy(p
, d
->data
+ d
->offset
, dlen
);
1216 if (d
->alloc
< p
->iov
.size
) {
1217 d
->alloc
= p
->iov
.size
;
1218 d
->data
= g_realloc(d
->data
, d
->alloc
);
1220 rc
= read(d
->fd
, d
->data
, dlen
);
1222 memset(d
->data
, 0, dlen
);
1223 s
->result
->code
= RES_INCOMPLETE_TRANSFER
;
1225 usb_packet_copy(p
, d
->data
, dlen
);
1228 if (d
->offset
== d
->length
) {
1229 usb_mtp_data_free(s
->data_in
);
1232 } else if (s
->result
!= NULL
) {
1233 MTPControl
*r
= s
->result
;
1234 int length
= sizeof(container
) + r
->argc
* sizeof(uint32_t);
1235 if (r
->code
== RES_OK
) {
1236 trace_usb_mtp_success(s
->dev
.addr
, r
->trans
,
1237 (r
->argc
> 0) ? r
->argv
[0] : 0,
1238 (r
->argc
> 1) ? r
->argv
[1] : 0);
1240 trace_usb_mtp_error(s
->dev
.addr
, r
->code
, r
->trans
,
1241 (r
->argc
> 0) ? r
->argv
[0] : 0,
1242 (r
->argc
> 1) ? r
->argv
[1] : 0);
1244 container
.length
= cpu_to_le32(length
);
1245 container
.type
= cpu_to_le16(TYPE_RESPONSE
);
1246 container
.code
= cpu_to_le16(r
->code
);
1247 container
.trans
= cpu_to_le32(r
->trans
);
1248 for (i
= 0; i
< r
->argc
; i
++) {
1249 params
[i
] = cpu_to_le32(r
->argv
[i
]);
1251 usb_packet_copy(p
, &container
, sizeof(container
));
1252 usb_packet_copy(p
, ¶ms
, length
- sizeof(container
));
1258 if (p
->iov
.size
< sizeof(container
)) {
1259 trace_usb_mtp_stall(s
->dev
.addr
, "packet too small");
1260 p
->status
= USB_RET_STALL
;
1263 usb_packet_copy(p
, &container
, sizeof(container
));
1264 switch (le16_to_cpu(container
.type
)) {
1266 if (s
->data_in
|| s
->data_out
|| s
->result
) {
1267 trace_usb_mtp_stall(s
->dev
.addr
, "transaction inflight");
1268 p
->status
= USB_RET_STALL
;
1271 cmd
.code
= le16_to_cpu(container
.code
);
1272 cmd
.argc
= (le32_to_cpu(container
.length
) - sizeof(container
))
1274 cmd
.trans
= le32_to_cpu(container
.trans
);
1275 if (cmd
.argc
> ARRAY_SIZE(cmd
.argv
)) {
1276 cmd
.argc
= ARRAY_SIZE(cmd
.argv
);
1278 if (p
->iov
.size
< sizeof(container
) + cmd
.argc
* sizeof(uint32_t)) {
1279 trace_usb_mtp_stall(s
->dev
.addr
, "packet too small");
1280 p
->status
= USB_RET_STALL
;
1283 usb_packet_copy(p
, ¶ms
, cmd
.argc
* sizeof(uint32_t));
1284 for (i
= 0; i
< cmd
.argc
; i
++) {
1285 cmd
.argv
[i
] = le32_to_cpu(params
[i
]);
1287 trace_usb_mtp_command(s
->dev
.addr
, cmd
.code
, cmd
.trans
,
1288 (cmd
.argc
> 0) ? cmd
.argv
[0] : 0,
1289 (cmd
.argc
> 1) ? cmd
.argv
[1] : 0,
1290 (cmd
.argc
> 2) ? cmd
.argv
[2] : 0,
1291 (cmd
.argc
> 3) ? cmd
.argv
[3] : 0,
1292 (cmd
.argc
> 4) ? cmd
.argv
[4] : 0);
1293 usb_mtp_command(s
, &cmd
);
1296 /* not needed as long as the mtp device is read-only */
1297 p
->status
= USB_RET_STALL
;
1302 #ifdef CONFIG_INOTIFY1
1303 if (!QTAILQ_EMPTY(&s
->events
)) {
1304 struct MTPMonEntry
*e
= QTAILQ_LAST(&s
->events
, events
);
1306 int len
= sizeof(container
) + sizeof(uint32_t);
1308 if (p
->iov
.size
< len
) {
1309 trace_usb_mtp_stall(s
->dev
.addr
,
1310 "packet too small to send event");
1311 p
->status
= USB_RET_STALL
;
1315 QTAILQ_REMOVE(&s
->events
, e
, next
);
1316 container
.length
= cpu_to_le32(len
);
1317 container
.type
= cpu_to_le32(TYPE_EVENT
);
1318 container
.code
= cpu_to_le16(e
->event
);
1319 container
.trans
= 0; /* no trans specific events */
1320 handle
= cpu_to_le32(e
->handle
);
1321 usb_packet_copy(p
, &container
, sizeof(container
));
1322 usb_packet_copy(p
, &handle
, sizeof(uint32_t));
1327 p
->status
= USB_RET_NAK
;
1330 trace_usb_mtp_stall(s
->dev
.addr
, "invalid endpoint");
1331 p
->status
= USB_RET_STALL
;
1335 if (p
->actual_length
== 0) {
1336 trace_usb_mtp_nak(s
->dev
.addr
, p
->ep
->nr
);
1337 p
->status
= USB_RET_NAK
;
1340 trace_usb_mtp_xfer(s
->dev
.addr
, p
->ep
->nr
, p
->actual_length
,
1346 static void usb_mtp_realize(USBDevice
*dev
, Error
**errp
)
1348 MTPState
*s
= USB_MTP(dev
);
1350 usb_desc_create_serial(dev
);
1352 QTAILQ_INIT(&s
->objects
);
1353 if (s
->desc
== NULL
) {
1354 if (s
->root
== NULL
) {
1355 error_setg(errp
, "usb-mtp: x-root property must be configured");
1358 s
->desc
= strrchr(s
->root
, '/');
1359 if (s
->desc
&& s
->desc
[0]) {
1360 s
->desc
= g_strdup(s
->desc
+ 1);
1362 s
->desc
= g_strdup("none");
1367 static const VMStateDescription vmstate_usb_mtp
= {
1371 .minimum_version_id
= 1,
1372 .fields
= (VMStateField
[]) {
1373 VMSTATE_USB_DEVICE(dev
, MTPState
),
1374 VMSTATE_END_OF_LIST()
1378 static Property mtp_properties
[] = {
1379 DEFINE_PROP_STRING("x-root", MTPState
, root
),
1380 DEFINE_PROP_STRING("desc", MTPState
, desc
),
1381 DEFINE_PROP_END_OF_LIST(),
1384 static void usb_mtp_class_initfn(ObjectClass
*klass
, void *data
)
1386 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1387 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1389 uc
->realize
= usb_mtp_realize
;
1390 uc
->product_desc
= "QEMU USB MTP";
1391 uc
->usb_desc
= &desc
;
1392 uc
->cancel_packet
= usb_mtp_cancel_packet
;
1393 uc
->handle_attach
= usb_desc_attach
;
1394 uc
->handle_reset
= usb_mtp_handle_reset
;
1395 uc
->handle_control
= usb_mtp_handle_control
;
1396 uc
->handle_data
= usb_mtp_handle_data
;
1397 dc
->fw_name
= "mtp";
1398 dc
->vmsd
= &vmstate_usb_mtp
;
1399 dc
->props
= mtp_properties
;
1402 static TypeInfo mtp_info
= {
1403 .name
= TYPE_USB_MTP
,
1404 .parent
= TYPE_USB_DEVICE
,
1405 .instance_size
= sizeof(MTPState
),
1406 .class_init
= usb_mtp_class_initfn
,
1409 static void usb_mtp_register_types(void)
1411 type_register_static(&mtp_info
);
1414 type_init(usb_mtp_register_types
)