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.
17 #include <sys/statvfs.h>
19 #include "qemu-common.h"
23 #include "hw/usb/desc.h"
25 /* ----------------------------------------------------------------------- */
27 enum mtp_container_type
{
36 CMD_GET_DEVICE_INFO
= 0x1001,
37 CMD_OPEN_SESSION
= 0x1002,
38 CMD_CLOSE_SESSION
= 0x1003,
39 CMD_GET_STORAGE_IDS
= 0x1004,
40 CMD_GET_STORAGE_INFO
= 0x1005,
41 CMD_GET_NUM_OBJECTS
= 0x1006,
42 CMD_GET_OBJECT_HANDLES
= 0x1007,
43 CMD_GET_OBJECT_INFO
= 0x1008,
44 CMD_GET_OBJECT
= 0x1009,
45 CMD_GET_PARTIAL_OBJECT
= 0x101b,
49 RES_GENERAL_ERROR
= 0x2002,
50 RES_SESSION_NOT_OPEN
= 0x2003,
51 RES_INVALID_TRANSACTION_ID
= 0x2004,
52 RES_OPERATION_NOT_SUPPORTED
= 0x2005,
53 RES_PARAMETER_NOT_SUPPORTED
= 0x2006,
54 RES_INCOMPLETE_TRANSFER
= 0x2007,
55 RES_INVALID_STORAGE_ID
= 0x2008,
56 RES_INVALID_OBJECT_HANDLE
= 0x2009,
57 RES_SPEC_BY_FORMAT_UNSUPPORTED
= 0x2014,
58 RES_INVALID_PARENT_OBJECT
= 0x201a,
59 RES_INVALID_PARAMETER
= 0x201d,
60 RES_SESSION_ALREADY_OPEN
= 0x201e,
63 FMT_UNDEFINED_OBJECT
= 0x3000,
64 FMT_ASSOCIATION
= 0x3001,
72 } QEMU_PACKED mtp_container
;
74 /* ----------------------------------------------------------------------- */
76 typedef struct MTPState MTPState
;
77 typedef struct MTPControl MTPControl
;
78 typedef struct MTPData MTPData
;
79 typedef struct MTPObject MTPObject
;
112 MTPObject
**children
;
115 QTAILQ_ENTRY(MTPObject
) next
;
128 uint32_t next_handle
;
130 QTAILQ_HEAD(, MTPObject
) objects
;
133 #define QEMU_STORAGE_ID 0x00010001
135 #define MTP_FLAG_WRITABLE 0
137 #define FLAG_SET(_mtp, _flag) ((_mtp)->flags & (1 << (_flag)))
139 /* ----------------------------------------------------------------------- */
141 #define MTP_MANUFACTURER "QEMU"
142 #define MTP_PRODUCT "QEMU filesharing"
145 STR_MANUFACTURER
= 1,
154 static const USBDescStrings desc_strings
= {
155 [STR_MANUFACTURER
] = MTP_MANUFACTURER
,
156 [STR_PRODUCT
] = MTP_PRODUCT
,
157 [STR_SERIALNUMBER
] = "34617",
159 [STR_CONFIG_FULL
] = "Full speed config (usb 1.1)",
160 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
161 [STR_CONFIG_SUPER
] = "Super speed config (usb 3.0)",
164 static const USBDescIface desc_iface_full
= {
165 .bInterfaceNumber
= 0,
167 .bInterfaceClass
= USB_CLASS_STILL_IMAGE
,
168 .bInterfaceSubClass
= 0x01,
169 .bInterfaceProtocol
= 0x01,
170 .iInterface
= STR_MTP
,
171 .eps
= (USBDescEndpoint
[]) {
173 .bEndpointAddress
= USB_DIR_IN
| EP_DATA_IN
,
174 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
175 .wMaxPacketSize
= 64,
177 .bEndpointAddress
= USB_DIR_OUT
| EP_DATA_OUT
,
178 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
179 .wMaxPacketSize
= 64,
181 .bEndpointAddress
= USB_DIR_IN
| EP_EVENT
,
182 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
189 static const USBDescDevice desc_device_full
= {
191 .bMaxPacketSize0
= 8,
192 .bNumConfigurations
= 1,
193 .confs
= (USBDescConfig
[]) {
196 .bConfigurationValue
= 1,
197 .iConfiguration
= STR_CONFIG_FULL
,
198 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
201 .ifs
= &desc_iface_full
,
206 static const USBDescIface desc_iface_high
= {
207 .bInterfaceNumber
= 0,
209 .bInterfaceClass
= USB_CLASS_STILL_IMAGE
,
210 .bInterfaceSubClass
= 0x01,
211 .bInterfaceProtocol
= 0x01,
212 .iInterface
= STR_MTP
,
213 .eps
= (USBDescEndpoint
[]) {
215 .bEndpointAddress
= USB_DIR_IN
| EP_DATA_IN
,
216 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
217 .wMaxPacketSize
= 512,
219 .bEndpointAddress
= USB_DIR_OUT
| EP_DATA_OUT
,
220 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
221 .wMaxPacketSize
= 512,
223 .bEndpointAddress
= USB_DIR_IN
| EP_EVENT
,
224 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
231 static const USBDescDevice desc_device_high
= {
233 .bMaxPacketSize0
= 64,
234 .bNumConfigurations
= 1,
235 .confs
= (USBDescConfig
[]) {
238 .bConfigurationValue
= 1,
239 .iConfiguration
= STR_CONFIG_HIGH
,
240 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
243 .ifs
= &desc_iface_high
,
248 static const USBDescMSOS desc_msos
= {
249 .CompatibleID
= "MTP",
250 .SelectiveSuspendEnabled
= true,
253 static const USBDesc desc
= {
255 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
258 .iManufacturer
= STR_MANUFACTURER
,
259 .iProduct
= STR_PRODUCT
,
260 .iSerialNumber
= STR_SERIALNUMBER
,
262 .full
= &desc_device_full
,
263 .high
= &desc_device_high
,
268 /* ----------------------------------------------------------------------- */
270 static MTPObject
*usb_mtp_object_alloc(MTPState
*s
, uint32_t handle
,
271 MTPObject
*parent
, char *name
)
273 MTPObject
*o
= g_new0(MTPObject
, 1);
275 if (name
[0] == '.') {
281 o
->name
= g_strdup(name
);
282 if (parent
== NULL
) {
283 o
->path
= g_strdup(name
);
285 o
->path
= g_strdup_printf("%s/%s", parent
->path
, name
);
288 if (lstat(o
->path
, &o
->stat
) != 0) {
291 if (S_ISREG(o
->stat
.st_mode
)) {
292 o
->format
= FMT_UNDEFINED_OBJECT
;
293 } else if (S_ISDIR(o
->stat
.st_mode
)) {
294 o
->format
= FMT_ASSOCIATION
;
299 if (access(o
->path
, R_OK
) != 0) {
303 trace_usb_mtp_object_alloc(s
->dev
.addr
, o
->handle
, o
->path
);
305 QTAILQ_INSERT_TAIL(&s
->objects
, o
, next
);
315 static void usb_mtp_object_free(MTPState
*s
, MTPObject
*o
)
319 trace_usb_mtp_object_free(s
->dev
.addr
, o
->handle
, o
->path
);
321 QTAILQ_REMOVE(&s
->objects
, o
, next
);
322 for (i
= 0; i
< o
->nchildren
; i
++) {
323 usb_mtp_object_free(s
, o
->children
[i
]);
331 static MTPObject
*usb_mtp_object_lookup(MTPState
*s
, uint32_t handle
)
335 QTAILQ_FOREACH(o
, &s
->objects
, next
) {
336 if (o
->handle
== handle
) {
343 static void usb_mtp_object_readdir(MTPState
*s
, MTPObject
*o
)
345 struct dirent
*entry
;
348 if (o
->have_children
) {
351 o
->have_children
= true;
353 dir
= opendir(o
->path
);
357 while ((entry
= readdir(dir
)) != NULL
) {
358 if ((o
->nchildren
% 32) == 0) {
359 o
->children
= g_realloc(o
->children
,
360 (o
->nchildren
+ 32) * sizeof(MTPObject
*));
362 o
->children
[o
->nchildren
] =
363 usb_mtp_object_alloc(s
, s
->next_handle
++, o
, entry
->d_name
);
364 if (o
->children
[o
->nchildren
] != NULL
) {
371 /* ----------------------------------------------------------------------- */
373 static MTPData
*usb_mtp_data_alloc(MTPControl
*c
)
375 MTPData
*data
= g_new0(MTPData
, 1);
377 data
->code
= c
->code
;
378 data
->trans
= c
->trans
;
384 static void usb_mtp_data_free(MTPData
*data
)
389 if (data
->fd
!= -1) {
396 static void usb_mtp_realloc(MTPData
*data
, uint32_t bytes
)
398 if (data
->length
+ bytes
<= data
->alloc
) {
401 data
->alloc
= (data
->length
+ bytes
+ 0xff) & ~0xff;
402 data
->data
= g_realloc(data
->data
, data
->alloc
);
405 static void usb_mtp_add_u8(MTPData
*data
, uint8_t val
)
407 usb_mtp_realloc(data
, 1);
408 data
->data
[data
->length
++] = val
;
411 static void usb_mtp_add_u16(MTPData
*data
, uint16_t val
)
413 usb_mtp_realloc(data
, 2);
414 data
->data
[data
->length
++] = (val
>> 0) & 0xff;
415 data
->data
[data
->length
++] = (val
>> 8) & 0xff;
418 static void usb_mtp_add_u32(MTPData
*data
, uint32_t val
)
420 usb_mtp_realloc(data
, 4);
421 data
->data
[data
->length
++] = (val
>> 0) & 0xff;
422 data
->data
[data
->length
++] = (val
>> 8) & 0xff;
423 data
->data
[data
->length
++] = (val
>> 16) & 0xff;
424 data
->data
[data
->length
++] = (val
>> 24) & 0xff;
427 static void usb_mtp_add_u64(MTPData
*data
, uint64_t val
)
429 usb_mtp_realloc(data
, 8);
430 data
->data
[data
->length
++] = (val
>> 0) & 0xff;
431 data
->data
[data
->length
++] = (val
>> 8) & 0xff;
432 data
->data
[data
->length
++] = (val
>> 16) & 0xff;
433 data
->data
[data
->length
++] = (val
>> 24) & 0xff;
434 data
->data
[data
->length
++] = (val
>> 32) & 0xff;
435 data
->data
[data
->length
++] = (val
>> 40) & 0xff;
436 data
->data
[data
->length
++] = (val
>> 48) & 0xff;
437 data
->data
[data
->length
++] = (val
>> 56) & 0xff;
440 static void usb_mtp_add_u16_array(MTPData
*data
, uint32_t len
,
441 const uint16_t *vals
)
445 usb_mtp_add_u32(data
, len
);
446 for (i
= 0; i
< len
; i
++) {
447 usb_mtp_add_u16(data
, vals
[i
]);
451 static void usb_mtp_add_u32_array(MTPData
*data
, uint32_t len
,
452 const uint32_t *vals
)
456 usb_mtp_add_u32(data
, len
);
457 for (i
= 0; i
< len
; i
++) {
458 usb_mtp_add_u32(data
, vals
[i
]);
462 static void usb_mtp_add_wstr(MTPData
*data
, const wchar_t *str
)
464 uint32_t len
= wcslen(str
);
468 len
++; /* include terminating L'\0' */
471 usb_mtp_add_u8(data
, len
);
472 for (i
= 0; i
< len
; i
++) {
473 usb_mtp_add_u16(data
, str
[i
]);
477 static void usb_mtp_add_str(MTPData
*data
, const char *str
)
479 uint32_t len
= strlen(str
)+1;
483 ret
= mbstowcs(wstr
, str
, len
);
485 usb_mtp_add_wstr(data
, L
"Oops");
487 usb_mtp_add_wstr(data
, wstr
);
491 static void usb_mtp_add_time(MTPData
*data
, time_t time
)
496 gmtime_r(&time
, &tm
);
497 strftime(buf
, sizeof(buf
), "%Y%m%dT%H%M%S", &tm
);
498 usb_mtp_add_str(data
, buf
);
501 /* ----------------------------------------------------------------------- */
503 static void usb_mtp_queue_result(MTPState
*s
, uint16_t code
, uint32_t trans
,
504 int argc
, uint32_t arg0
, uint32_t arg1
)
506 MTPControl
*c
= g_new0(MTPControl
, 1);
518 assert(s
->result
== NULL
);
522 /* ----------------------------------------------------------------------- */
524 static MTPData
*usb_mtp_get_device_info(MTPState
*s
, MTPControl
*c
)
526 static const uint16_t ops
[] = {
531 CMD_GET_STORAGE_INFO
,
533 CMD_GET_OBJECT_HANDLES
,
536 CMD_GET_PARTIAL_OBJECT
,
538 static const uint16_t fmt
[] = {
539 FMT_UNDEFINED_OBJECT
,
542 MTPData
*d
= usb_mtp_data_alloc(c
);
544 trace_usb_mtp_op_get_device_info(s
->dev
.addr
);
546 usb_mtp_add_u16(d
, 100);
547 usb_mtp_add_u32(d
, 0xffffffff);
548 usb_mtp_add_u16(d
, 0x0101);
549 usb_mtp_add_wstr(d
, L
"");
550 usb_mtp_add_u16(d
, 0x0000);
552 usb_mtp_add_u16_array(d
, ARRAY_SIZE(ops
), ops
);
553 usb_mtp_add_u16_array(d
, 0, NULL
);
554 usb_mtp_add_u16_array(d
, 0, NULL
);
555 usb_mtp_add_u16_array(d
, 0, NULL
);
556 usb_mtp_add_u16_array(d
, ARRAY_SIZE(fmt
), fmt
);
558 usb_mtp_add_wstr(d
, L
"" MTP_MANUFACTURER
);
559 usb_mtp_add_wstr(d
, L
"" MTP_PRODUCT
);
560 usb_mtp_add_wstr(d
, L
"0.1");
561 usb_mtp_add_wstr(d
, L
"0123456789abcdef0123456789abcdef");
566 static MTPData
*usb_mtp_get_storage_ids(MTPState
*s
, MTPControl
*c
)
568 static const uint32_t ids
[] = {
571 MTPData
*d
= usb_mtp_data_alloc(c
);
573 trace_usb_mtp_op_get_storage_ids(s
->dev
.addr
);
575 usb_mtp_add_u32_array(d
, ARRAY_SIZE(ids
), ids
);
580 static MTPData
*usb_mtp_get_storage_info(MTPState
*s
, MTPControl
*c
)
582 MTPData
*d
= usb_mtp_data_alloc(c
);
586 trace_usb_mtp_op_get_storage_info(s
->dev
.addr
);
588 if (FLAG_SET(s
, MTP_FLAG_WRITABLE
)) {
589 usb_mtp_add_u16(d
, 0x0003);
590 usb_mtp_add_u16(d
, 0x0002);
591 usb_mtp_add_u16(d
, 0x0000);
593 usb_mtp_add_u16(d
, 0x0001);
594 usb_mtp_add_u16(d
, 0x0002);
595 usb_mtp_add_u16(d
, 0x0001);
598 rc
= statvfs(s
->root
, &buf
);
600 usb_mtp_add_u64(d
, (uint64_t)buf
.f_frsize
* buf
.f_blocks
);
601 usb_mtp_add_u64(d
, (uint64_t)buf
.f_bavail
* buf
.f_blocks
);
602 usb_mtp_add_u32(d
, buf
.f_ffree
);
604 usb_mtp_add_u64(d
, 0xffffffff);
605 usb_mtp_add_u64(d
, 0xffffffff);
606 usb_mtp_add_u32(d
, 0xffffffff);
609 usb_mtp_add_str(d
, s
->desc
);
610 usb_mtp_add_wstr(d
, L
"123456789abcdef");
614 static MTPData
*usb_mtp_get_object_handles(MTPState
*s
, MTPControl
*c
,
617 MTPData
*d
= usb_mtp_data_alloc(c
);
618 uint32_t i
, handles
[o
->nchildren
];
620 trace_usb_mtp_op_get_object_handles(s
->dev
.addr
, o
->handle
, o
->path
);
622 for (i
= 0; i
< o
->nchildren
; i
++) {
623 handles
[i
] = o
->children
[i
]->handle
;
625 usb_mtp_add_u32_array(d
, o
->nchildren
, handles
);
630 static MTPData
*usb_mtp_get_object_info(MTPState
*s
, MTPControl
*c
,
633 MTPData
*d
= usb_mtp_data_alloc(c
);
635 trace_usb_mtp_op_get_object_info(s
->dev
.addr
, o
->handle
, o
->path
);
637 usb_mtp_add_u32(d
, QEMU_STORAGE_ID
);
638 usb_mtp_add_u16(d
, o
->format
);
639 usb_mtp_add_u16(d
, 0);
640 usb_mtp_add_u32(d
, o
->stat
.st_size
);
642 usb_mtp_add_u16(d
, 0);
643 usb_mtp_add_u32(d
, 0);
644 usb_mtp_add_u32(d
, 0);
645 usb_mtp_add_u32(d
, 0);
646 usb_mtp_add_u32(d
, 0);
647 usb_mtp_add_u32(d
, 0);
648 usb_mtp_add_u32(d
, 0);
651 usb_mtp_add_u32(d
, o
->parent
->handle
);
653 usb_mtp_add_u32(d
, 0);
655 if (o
->format
== FMT_ASSOCIATION
) {
656 usb_mtp_add_u16(d
, 0x0001);
657 usb_mtp_add_u32(d
, 0x00000001);
658 usb_mtp_add_u32(d
, 0);
660 usb_mtp_add_u16(d
, 0);
661 usb_mtp_add_u32(d
, 0);
662 usb_mtp_add_u32(d
, 0);
665 usb_mtp_add_str(d
, o
->name
);
666 usb_mtp_add_time(d
, o
->stat
.st_ctime
);
667 usb_mtp_add_time(d
, o
->stat
.st_mtime
);
668 usb_mtp_add_wstr(d
, L
"");
673 static MTPData
*usb_mtp_get_object(MTPState
*s
, MTPControl
*c
,
676 MTPData
*d
= usb_mtp_data_alloc(c
);
678 trace_usb_mtp_op_get_object(s
->dev
.addr
, o
->handle
, o
->path
);
680 d
->fd
= open(o
->path
, O_RDONLY
);
682 usb_mtp_data_free(d
);
685 d
->length
= o
->stat
.st_size
;
687 d
->data
= g_malloc(d
->alloc
);
691 static MTPData
*usb_mtp_get_partial_object(MTPState
*s
, MTPControl
*c
,
694 MTPData
*d
= usb_mtp_data_alloc(c
);
697 trace_usb_mtp_op_get_partial_object(s
->dev
.addr
, o
->handle
, o
->path
,
698 c
->argv
[1], c
->argv
[2]);
700 d
->fd
= open(o
->path
, O_RDONLY
);
702 usb_mtp_data_free(d
);
707 if (offset
> o
->stat
.st_size
) {
708 offset
= o
->stat
.st_size
;
710 if (lseek(d
->fd
, offset
, SEEK_SET
) < 0) {
711 usb_mtp_data_free(d
);
715 d
->length
= c
->argv
[2];
716 if (d
->length
> o
->stat
.st_size
- offset
) {
717 d
->length
= o
->stat
.st_size
- offset
;
723 static void usb_mtp_command(MTPState
*s
, MTPControl
*c
)
725 MTPData
*data_in
= NULL
;
727 uint32_t nres
= 0, res0
= 0;
730 if (c
->code
>= CMD_CLOSE_SESSION
&& s
->session
== 0) {
731 usb_mtp_queue_result(s
, RES_SESSION_NOT_OPEN
,
736 /* process commands */
738 case CMD_GET_DEVICE_INFO
:
739 data_in
= usb_mtp_get_device_info(s
, c
);
741 case CMD_OPEN_SESSION
:
743 usb_mtp_queue_result(s
, RES_SESSION_ALREADY_OPEN
,
744 c
->trans
, 1, s
->session
, 0);
747 if (c
->argv
[0] == 0) {
748 usb_mtp_queue_result(s
, RES_INVALID_PARAMETER
,
752 trace_usb_mtp_op_open_session(s
->dev
.addr
);
753 s
->session
= c
->argv
[0];
754 usb_mtp_object_alloc(s
, s
->next_handle
++, NULL
, s
->root
);
756 case CMD_CLOSE_SESSION
:
757 trace_usb_mtp_op_close_session(s
->dev
.addr
);
760 usb_mtp_object_free(s
, QTAILQ_FIRST(&s
->objects
));
761 assert(QTAILQ_EMPTY(&s
->objects
));
763 case CMD_GET_STORAGE_IDS
:
764 data_in
= usb_mtp_get_storage_ids(s
, c
);
766 case CMD_GET_STORAGE_INFO
:
767 if (c
->argv
[0] != QEMU_STORAGE_ID
&&
768 c
->argv
[0] != 0xffffffff) {
769 usb_mtp_queue_result(s
, RES_INVALID_STORAGE_ID
,
773 data_in
= usb_mtp_get_storage_info(s
, c
);
775 case CMD_GET_NUM_OBJECTS
:
776 case CMD_GET_OBJECT_HANDLES
:
777 if (c
->argv
[0] != QEMU_STORAGE_ID
&&
778 c
->argv
[0] != 0xffffffff) {
779 usb_mtp_queue_result(s
, RES_INVALID_STORAGE_ID
,
783 if (c
->argv
[1] != 0x00000000) {
784 usb_mtp_queue_result(s
, RES_SPEC_BY_FORMAT_UNSUPPORTED
,
788 if (c
->argv
[2] == 0x00000000 ||
789 c
->argv
[2] == 0xffffffff) {
790 o
= QTAILQ_FIRST(&s
->objects
);
792 o
= usb_mtp_object_lookup(s
, c
->argv
[2]);
795 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
799 if (o
->format
!= FMT_ASSOCIATION
) {
800 usb_mtp_queue_result(s
, RES_INVALID_PARENT_OBJECT
,
804 usb_mtp_object_readdir(s
, o
);
805 if (c
->code
== CMD_GET_NUM_OBJECTS
) {
806 trace_usb_mtp_op_get_num_objects(s
->dev
.addr
, o
->handle
, o
->path
);
810 data_in
= usb_mtp_get_object_handles(s
, c
, o
);
813 case CMD_GET_OBJECT_INFO
:
814 o
= usb_mtp_object_lookup(s
, c
->argv
[0]);
816 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
820 data_in
= usb_mtp_get_object_info(s
, c
, o
);
823 o
= usb_mtp_object_lookup(s
, c
->argv
[0]);
825 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
829 if (o
->format
== FMT_ASSOCIATION
) {
830 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
834 data_in
= usb_mtp_get_object(s
, c
, o
);
835 if (data_in
== NULL
) {
836 usb_mtp_queue_result(s
, RES_GENERAL_ERROR
,
841 case CMD_GET_PARTIAL_OBJECT
:
842 o
= usb_mtp_object_lookup(s
, c
->argv
[0]);
844 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
848 if (o
->format
== FMT_ASSOCIATION
) {
849 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
853 data_in
= usb_mtp_get_partial_object(s
, c
, o
);
854 if (data_in
== NULL
) {
855 usb_mtp_queue_result(s
, RES_GENERAL_ERROR
,
860 res0
= data_in
->length
;
863 trace_usb_mtp_op_unknown(s
->dev
.addr
, c
->code
);
864 usb_mtp_queue_result(s
, RES_OPERATION_NOT_SUPPORTED
,
869 /* return results on success */
871 assert(s
->data_in
== NULL
);
872 s
->data_in
= data_in
;
874 usb_mtp_queue_result(s
, RES_OK
, c
->trans
, nres
, res0
, 0);
877 /* ----------------------------------------------------------------------- */
879 static void usb_mtp_handle_reset(USBDevice
*dev
)
881 MTPState
*s
= DO_UPCAST(MTPState
, dev
, dev
);
883 trace_usb_mtp_reset(s
->dev
.addr
);
886 usb_mtp_data_free(s
->data_in
);
888 usb_mtp_data_free(s
->data_out
);
894 static void usb_mtp_handle_control(USBDevice
*dev
, USBPacket
*p
,
895 int request
, int value
, int index
,
896 int length
, uint8_t *data
)
900 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
905 trace_usb_mtp_stall(dev
->addr
, "unknown control request");
906 p
->status
= USB_RET_STALL
;
909 static void usb_mtp_cancel_packet(USBDevice
*dev
, USBPacket
*p
)
911 /* we don't use async packets, so this should never be called */
912 fprintf(stderr
, "%s\n", __func__
);
915 static void usb_mtp_handle_data(USBDevice
*dev
, USBPacket
*p
)
917 MTPState
*s
= DO_UPCAST(MTPState
, dev
, dev
);
919 mtp_container container
;
925 if (s
->data_out
!= NULL
) {
927 trace_usb_mtp_stall(s
->dev
.addr
, "awaiting data-out");
928 p
->status
= USB_RET_STALL
;
931 if (p
->iov
.size
< sizeof(container
)) {
932 trace_usb_mtp_stall(s
->dev
.addr
, "packet too small");
933 p
->status
= USB_RET_STALL
;
936 if (s
->data_in
!= NULL
) {
937 MTPData
*d
= s
->data_in
;
938 int dlen
= d
->length
- d
->offset
;
940 trace_usb_mtp_data_in(s
->dev
.addr
, d
->trans
, d
->length
);
941 container
.length
= cpu_to_le32(d
->length
+ sizeof(container
));
942 container
.type
= cpu_to_le16(TYPE_DATA
);
943 container
.code
= cpu_to_le16(d
->code
);
944 container
.trans
= cpu_to_le32(d
->trans
);
945 usb_packet_copy(p
, &container
, sizeof(container
));
947 if (dlen
> p
->iov
.size
- sizeof(container
)) {
948 dlen
= p
->iov
.size
- sizeof(container
);
951 if (dlen
> p
->iov
.size
) {
956 usb_packet_copy(p
, d
->data
+ d
->offset
, dlen
);
958 if (d
->alloc
< p
->iov
.size
) {
959 d
->alloc
= p
->iov
.size
;
960 d
->data
= g_realloc(d
->data
, d
->alloc
);
962 rc
= read(d
->fd
, d
->data
, dlen
);
964 memset(d
->data
, 0, dlen
);
965 s
->result
->code
= RES_INCOMPLETE_TRANSFER
;
967 usb_packet_copy(p
, d
->data
, dlen
);
970 if (d
->offset
== d
->length
) {
971 usb_mtp_data_free(s
->data_in
);
974 } else if (s
->result
!= NULL
) {
975 MTPControl
*r
= s
->result
;
976 int length
= sizeof(container
) + r
->argc
* sizeof(uint32_t);
977 if (r
->code
== RES_OK
) {
978 trace_usb_mtp_success(s
->dev
.addr
, r
->trans
,
979 (r
->argc
> 0) ? r
->argv
[0] : 0,
980 (r
->argc
> 1) ? r
->argv
[1] : 0);
982 trace_usb_mtp_error(s
->dev
.addr
, r
->code
, r
->trans
,
983 (r
->argc
> 0) ? r
->argv
[0] : 0,
984 (r
->argc
> 1) ? r
->argv
[1] : 0);
986 container
.length
= cpu_to_le32(length
);
987 container
.type
= cpu_to_le16(TYPE_RESPONSE
);
988 container
.code
= cpu_to_le16(r
->code
);
989 container
.trans
= cpu_to_le32(r
->trans
);
990 for (i
= 0; i
< r
->argc
; i
++) {
991 params
[i
] = cpu_to_le32(r
->argv
[i
]);
993 usb_packet_copy(p
, &container
, sizeof(container
));
994 usb_packet_copy(p
, ¶ms
, length
- sizeof(container
));
1000 if (p
->iov
.size
< sizeof(container
)) {
1001 trace_usb_mtp_stall(s
->dev
.addr
, "packet too small");
1002 p
->status
= USB_RET_STALL
;
1005 usb_packet_copy(p
, &container
, sizeof(container
));
1006 switch (le16_to_cpu(container
.type
)) {
1008 if (s
->data_in
|| s
->data_out
|| s
->result
) {
1009 trace_usb_mtp_stall(s
->dev
.addr
, "transaction inflight");
1010 p
->status
= USB_RET_STALL
;
1013 cmd
.code
= le16_to_cpu(container
.code
);
1014 cmd
.argc
= (le32_to_cpu(container
.length
) - sizeof(container
))
1016 cmd
.trans
= le32_to_cpu(container
.trans
);
1017 if (cmd
.argc
> ARRAY_SIZE(cmd
.argv
)) {
1018 cmd
.argc
= ARRAY_SIZE(cmd
.argv
);
1020 if (p
->iov
.size
< sizeof(container
) + cmd
.argc
* sizeof(uint32_t)) {
1021 trace_usb_mtp_stall(s
->dev
.addr
, "packet too small");
1022 p
->status
= USB_RET_STALL
;
1025 usb_packet_copy(p
, ¶ms
, cmd
.argc
* sizeof(uint32_t));
1026 for (i
= 0; i
< cmd
.argc
; i
++) {
1027 cmd
.argv
[i
] = le32_to_cpu(params
[i
]);
1029 trace_usb_mtp_command(s
->dev
.addr
, cmd
.code
, cmd
.trans
,
1030 (cmd
.argc
> 0) ? cmd
.argv
[0] : 0,
1031 (cmd
.argc
> 1) ? cmd
.argv
[1] : 0,
1032 (cmd
.argc
> 2) ? cmd
.argv
[2] : 0,
1033 (cmd
.argc
> 3) ? cmd
.argv
[3] : 0,
1034 (cmd
.argc
> 4) ? cmd
.argv
[4] : 0);
1035 usb_mtp_command(s
, &cmd
);
1038 /* not needed as long as the mtp device is read-only */
1039 p
->status
= USB_RET_STALL
;
1044 p
->status
= USB_RET_NAK
;
1047 trace_usb_mtp_stall(s
->dev
.addr
, "invalid endpoint");
1048 p
->status
= USB_RET_STALL
;
1052 if (p
->actual_length
== 0) {
1053 trace_usb_mtp_nak(s
->dev
.addr
, p
->ep
->nr
);
1054 p
->status
= USB_RET_NAK
;
1057 trace_usb_mtp_xfer(s
->dev
.addr
, p
->ep
->nr
, p
->actual_length
,
1063 static int usb_mtp_initfn(USBDevice
*dev
)
1065 MTPState
*s
= DO_UPCAST(MTPState
, dev
, dev
);
1067 usb_desc_create_serial(dev
);
1069 QTAILQ_INIT(&s
->objects
);
1070 if (s
->desc
== NULL
) {
1071 s
->desc
= strrchr(s
->root
, '/');
1072 if (s
->desc
&& s
->desc
[0]) {
1073 s
->desc
= g_strdup(s
->desc
+ 1);
1075 s
->desc
= g_strdup("none");
1081 static const VMStateDescription vmstate_usb_mtp
= {
1085 .minimum_version_id
= 1,
1086 .fields
= (VMStateField
[]) {
1087 VMSTATE_USB_DEVICE(dev
, MTPState
),
1088 VMSTATE_END_OF_LIST()
1092 static Property mtp_properties
[] = {
1093 DEFINE_PROP_STRING("x-root", MTPState
, root
),
1094 DEFINE_PROP_STRING("desc", MTPState
, desc
),
1095 DEFINE_PROP_END_OF_LIST(),
1098 static void usb_mtp_class_initfn(ObjectClass
*klass
, void *data
)
1100 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1101 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1103 uc
->init
= usb_mtp_initfn
;
1104 uc
->product_desc
= "QEMU USB MTP";
1105 uc
->usb_desc
= &desc
;
1106 uc
->cancel_packet
= usb_mtp_cancel_packet
;
1107 uc
->handle_attach
= usb_desc_attach
;
1108 uc
->handle_reset
= usb_mtp_handle_reset
;
1109 uc
->handle_control
= usb_mtp_handle_control
;
1110 uc
->handle_data
= usb_mtp_handle_data
;
1111 dc
->fw_name
= "mtp";
1112 dc
->vmsd
= &vmstate_usb_mtp
;
1113 dc
->props
= mtp_properties
;
1116 static TypeInfo mtp_info
= {
1118 .parent
= TYPE_USB_DEVICE
,
1119 .instance_size
= sizeof(MTPState
),
1120 .class_init
= usb_mtp_class_initfn
,
1123 static void usb_mtp_register_types(void)
1125 type_register_static(&mtp_info
);
1128 type_init(usb_mtp_register_types
)