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 TYPE_USB_MTP "usb-mtp"
134 #define USB_MTP(obj) OBJECT_CHECK(MTPState, (obj), TYPE_USB_MTP)
136 #define QEMU_STORAGE_ID 0x00010001
138 #define MTP_FLAG_WRITABLE 0
140 #define FLAG_SET(_mtp, _flag) ((_mtp)->flags & (1 << (_flag)))
142 /* ----------------------------------------------------------------------- */
144 #define MTP_MANUFACTURER "QEMU"
145 #define MTP_PRODUCT "QEMU filesharing"
148 STR_MANUFACTURER
= 1,
157 static const USBDescStrings desc_strings
= {
158 [STR_MANUFACTURER
] = MTP_MANUFACTURER
,
159 [STR_PRODUCT
] = MTP_PRODUCT
,
160 [STR_SERIALNUMBER
] = "34617",
162 [STR_CONFIG_FULL
] = "Full speed config (usb 1.1)",
163 [STR_CONFIG_HIGH
] = "High speed config (usb 2.0)",
164 [STR_CONFIG_SUPER
] = "Super speed config (usb 3.0)",
167 static const USBDescIface desc_iface_full
= {
168 .bInterfaceNumber
= 0,
170 .bInterfaceClass
= USB_CLASS_STILL_IMAGE
,
171 .bInterfaceSubClass
= 0x01,
172 .bInterfaceProtocol
= 0x01,
173 .iInterface
= STR_MTP
,
174 .eps
= (USBDescEndpoint
[]) {
176 .bEndpointAddress
= USB_DIR_IN
| EP_DATA_IN
,
177 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
178 .wMaxPacketSize
= 64,
180 .bEndpointAddress
= USB_DIR_OUT
| EP_DATA_OUT
,
181 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
182 .wMaxPacketSize
= 64,
184 .bEndpointAddress
= USB_DIR_IN
| EP_EVENT
,
185 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
192 static const USBDescDevice desc_device_full
= {
194 .bMaxPacketSize0
= 8,
195 .bNumConfigurations
= 1,
196 .confs
= (USBDescConfig
[]) {
199 .bConfigurationValue
= 1,
200 .iConfiguration
= STR_CONFIG_FULL
,
201 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
204 .ifs
= &desc_iface_full
,
209 static const USBDescIface desc_iface_high
= {
210 .bInterfaceNumber
= 0,
212 .bInterfaceClass
= USB_CLASS_STILL_IMAGE
,
213 .bInterfaceSubClass
= 0x01,
214 .bInterfaceProtocol
= 0x01,
215 .iInterface
= STR_MTP
,
216 .eps
= (USBDescEndpoint
[]) {
218 .bEndpointAddress
= USB_DIR_IN
| EP_DATA_IN
,
219 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
220 .wMaxPacketSize
= 512,
222 .bEndpointAddress
= USB_DIR_OUT
| EP_DATA_OUT
,
223 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
224 .wMaxPacketSize
= 512,
226 .bEndpointAddress
= USB_DIR_IN
| EP_EVENT
,
227 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
234 static const USBDescDevice desc_device_high
= {
236 .bMaxPacketSize0
= 64,
237 .bNumConfigurations
= 1,
238 .confs
= (USBDescConfig
[]) {
241 .bConfigurationValue
= 1,
242 .iConfiguration
= STR_CONFIG_HIGH
,
243 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
246 .ifs
= &desc_iface_high
,
251 static const USBDescMSOS desc_msos
= {
252 .CompatibleID
= "MTP",
253 .SelectiveSuspendEnabled
= true,
256 static const USBDesc desc
= {
258 .idVendor
= 0x46f4, /* CRC16() of "QEMU" */
261 .iManufacturer
= STR_MANUFACTURER
,
262 .iProduct
= STR_PRODUCT
,
263 .iSerialNumber
= STR_SERIALNUMBER
,
265 .full
= &desc_device_full
,
266 .high
= &desc_device_high
,
271 /* ----------------------------------------------------------------------- */
273 static MTPObject
*usb_mtp_object_alloc(MTPState
*s
, uint32_t handle
,
274 MTPObject
*parent
, char *name
)
276 MTPObject
*o
= g_new0(MTPObject
, 1);
278 if (name
[0] == '.') {
284 o
->name
= g_strdup(name
);
285 if (parent
== NULL
) {
286 o
->path
= g_strdup(name
);
288 o
->path
= g_strdup_printf("%s/%s", parent
->path
, name
);
291 if (lstat(o
->path
, &o
->stat
) != 0) {
294 if (S_ISREG(o
->stat
.st_mode
)) {
295 o
->format
= FMT_UNDEFINED_OBJECT
;
296 } else if (S_ISDIR(o
->stat
.st_mode
)) {
297 o
->format
= FMT_ASSOCIATION
;
302 if (access(o
->path
, R_OK
) != 0) {
306 trace_usb_mtp_object_alloc(s
->dev
.addr
, o
->handle
, o
->path
);
308 QTAILQ_INSERT_TAIL(&s
->objects
, o
, next
);
318 static void usb_mtp_object_free(MTPState
*s
, MTPObject
*o
)
322 trace_usb_mtp_object_free(s
->dev
.addr
, o
->handle
, o
->path
);
324 QTAILQ_REMOVE(&s
->objects
, o
, next
);
325 for (i
= 0; i
< o
->nchildren
; i
++) {
326 usb_mtp_object_free(s
, o
->children
[i
]);
334 static MTPObject
*usb_mtp_object_lookup(MTPState
*s
, uint32_t handle
)
338 QTAILQ_FOREACH(o
, &s
->objects
, next
) {
339 if (o
->handle
== handle
) {
346 static void usb_mtp_object_readdir(MTPState
*s
, MTPObject
*o
)
348 struct dirent
*entry
;
351 if (o
->have_children
) {
354 o
->have_children
= true;
356 dir
= opendir(o
->path
);
360 while ((entry
= readdir(dir
)) != NULL
) {
361 if ((o
->nchildren
% 32) == 0) {
362 o
->children
= g_realloc(o
->children
,
363 (o
->nchildren
+ 32) * sizeof(MTPObject
*));
365 o
->children
[o
->nchildren
] =
366 usb_mtp_object_alloc(s
, s
->next_handle
++, o
, entry
->d_name
);
367 if (o
->children
[o
->nchildren
] != NULL
) {
374 /* ----------------------------------------------------------------------- */
376 static MTPData
*usb_mtp_data_alloc(MTPControl
*c
)
378 MTPData
*data
= g_new0(MTPData
, 1);
380 data
->code
= c
->code
;
381 data
->trans
= c
->trans
;
387 static void usb_mtp_data_free(MTPData
*data
)
392 if (data
->fd
!= -1) {
399 static void usb_mtp_realloc(MTPData
*data
, uint32_t bytes
)
401 if (data
->length
+ bytes
<= data
->alloc
) {
404 data
->alloc
= (data
->length
+ bytes
+ 0xff) & ~0xff;
405 data
->data
= g_realloc(data
->data
, data
->alloc
);
408 static void usb_mtp_add_u8(MTPData
*data
, uint8_t val
)
410 usb_mtp_realloc(data
, 1);
411 data
->data
[data
->length
++] = val
;
414 static void usb_mtp_add_u16(MTPData
*data
, uint16_t val
)
416 usb_mtp_realloc(data
, 2);
417 data
->data
[data
->length
++] = (val
>> 0) & 0xff;
418 data
->data
[data
->length
++] = (val
>> 8) & 0xff;
421 static void usb_mtp_add_u32(MTPData
*data
, uint32_t val
)
423 usb_mtp_realloc(data
, 4);
424 data
->data
[data
->length
++] = (val
>> 0) & 0xff;
425 data
->data
[data
->length
++] = (val
>> 8) & 0xff;
426 data
->data
[data
->length
++] = (val
>> 16) & 0xff;
427 data
->data
[data
->length
++] = (val
>> 24) & 0xff;
430 static void usb_mtp_add_u64(MTPData
*data
, uint64_t val
)
432 usb_mtp_realloc(data
, 8);
433 data
->data
[data
->length
++] = (val
>> 0) & 0xff;
434 data
->data
[data
->length
++] = (val
>> 8) & 0xff;
435 data
->data
[data
->length
++] = (val
>> 16) & 0xff;
436 data
->data
[data
->length
++] = (val
>> 24) & 0xff;
437 data
->data
[data
->length
++] = (val
>> 32) & 0xff;
438 data
->data
[data
->length
++] = (val
>> 40) & 0xff;
439 data
->data
[data
->length
++] = (val
>> 48) & 0xff;
440 data
->data
[data
->length
++] = (val
>> 56) & 0xff;
443 static void usb_mtp_add_u16_array(MTPData
*data
, uint32_t len
,
444 const uint16_t *vals
)
448 usb_mtp_add_u32(data
, len
);
449 for (i
= 0; i
< len
; i
++) {
450 usb_mtp_add_u16(data
, vals
[i
]);
454 static void usb_mtp_add_u32_array(MTPData
*data
, uint32_t len
,
455 const uint32_t *vals
)
459 usb_mtp_add_u32(data
, len
);
460 for (i
= 0; i
< len
; i
++) {
461 usb_mtp_add_u32(data
, vals
[i
]);
465 static void usb_mtp_add_wstr(MTPData
*data
, const wchar_t *str
)
467 uint32_t len
= wcslen(str
);
471 len
++; /* include terminating L'\0' */
474 usb_mtp_add_u8(data
, len
);
475 for (i
= 0; i
< len
; i
++) {
476 usb_mtp_add_u16(data
, str
[i
]);
480 static void usb_mtp_add_str(MTPData
*data
, const char *str
)
482 uint32_t len
= strlen(str
)+1;
486 ret
= mbstowcs(wstr
, str
, len
);
488 usb_mtp_add_wstr(data
, L
"Oops");
490 usb_mtp_add_wstr(data
, wstr
);
494 static void usb_mtp_add_time(MTPData
*data
, time_t time
)
499 gmtime_r(&time
, &tm
);
500 strftime(buf
, sizeof(buf
), "%Y%m%dT%H%M%S", &tm
);
501 usb_mtp_add_str(data
, buf
);
504 /* ----------------------------------------------------------------------- */
506 static void usb_mtp_queue_result(MTPState
*s
, uint16_t code
, uint32_t trans
,
507 int argc
, uint32_t arg0
, uint32_t arg1
)
509 MTPControl
*c
= g_new0(MTPControl
, 1);
521 assert(s
->result
== NULL
);
525 /* ----------------------------------------------------------------------- */
527 static MTPData
*usb_mtp_get_device_info(MTPState
*s
, MTPControl
*c
)
529 static const uint16_t ops
[] = {
534 CMD_GET_STORAGE_INFO
,
536 CMD_GET_OBJECT_HANDLES
,
539 CMD_GET_PARTIAL_OBJECT
,
541 static const uint16_t fmt
[] = {
542 FMT_UNDEFINED_OBJECT
,
545 MTPData
*d
= usb_mtp_data_alloc(c
);
547 trace_usb_mtp_op_get_device_info(s
->dev
.addr
);
549 usb_mtp_add_u16(d
, 100);
550 usb_mtp_add_u32(d
, 0xffffffff);
551 usb_mtp_add_u16(d
, 0x0101);
552 usb_mtp_add_wstr(d
, L
"");
553 usb_mtp_add_u16(d
, 0x0000);
555 usb_mtp_add_u16_array(d
, ARRAY_SIZE(ops
), ops
);
556 usb_mtp_add_u16_array(d
, 0, NULL
);
557 usb_mtp_add_u16_array(d
, 0, NULL
);
558 usb_mtp_add_u16_array(d
, 0, NULL
);
559 usb_mtp_add_u16_array(d
, ARRAY_SIZE(fmt
), fmt
);
561 usb_mtp_add_wstr(d
, L
"" MTP_MANUFACTURER
);
562 usb_mtp_add_wstr(d
, L
"" MTP_PRODUCT
);
563 usb_mtp_add_wstr(d
, L
"0.1");
564 usb_mtp_add_wstr(d
, L
"0123456789abcdef0123456789abcdef");
569 static MTPData
*usb_mtp_get_storage_ids(MTPState
*s
, MTPControl
*c
)
571 static const uint32_t ids
[] = {
574 MTPData
*d
= usb_mtp_data_alloc(c
);
576 trace_usb_mtp_op_get_storage_ids(s
->dev
.addr
);
578 usb_mtp_add_u32_array(d
, ARRAY_SIZE(ids
), ids
);
583 static MTPData
*usb_mtp_get_storage_info(MTPState
*s
, MTPControl
*c
)
585 MTPData
*d
= usb_mtp_data_alloc(c
);
589 trace_usb_mtp_op_get_storage_info(s
->dev
.addr
);
591 if (FLAG_SET(s
, MTP_FLAG_WRITABLE
)) {
592 usb_mtp_add_u16(d
, 0x0003);
593 usb_mtp_add_u16(d
, 0x0002);
594 usb_mtp_add_u16(d
, 0x0000);
596 usb_mtp_add_u16(d
, 0x0001);
597 usb_mtp_add_u16(d
, 0x0002);
598 usb_mtp_add_u16(d
, 0x0001);
601 rc
= statvfs(s
->root
, &buf
);
603 usb_mtp_add_u64(d
, (uint64_t)buf
.f_frsize
* buf
.f_blocks
);
604 usb_mtp_add_u64(d
, (uint64_t)buf
.f_bavail
* buf
.f_blocks
);
605 usb_mtp_add_u32(d
, buf
.f_ffree
);
607 usb_mtp_add_u64(d
, 0xffffffff);
608 usb_mtp_add_u64(d
, 0xffffffff);
609 usb_mtp_add_u32(d
, 0xffffffff);
612 usb_mtp_add_str(d
, s
->desc
);
613 usb_mtp_add_wstr(d
, L
"123456789abcdef");
617 static MTPData
*usb_mtp_get_object_handles(MTPState
*s
, MTPControl
*c
,
620 MTPData
*d
= usb_mtp_data_alloc(c
);
621 uint32_t i
, handles
[o
->nchildren
];
623 trace_usb_mtp_op_get_object_handles(s
->dev
.addr
, o
->handle
, o
->path
);
625 for (i
= 0; i
< o
->nchildren
; i
++) {
626 handles
[i
] = o
->children
[i
]->handle
;
628 usb_mtp_add_u32_array(d
, o
->nchildren
, handles
);
633 static MTPData
*usb_mtp_get_object_info(MTPState
*s
, MTPControl
*c
,
636 MTPData
*d
= usb_mtp_data_alloc(c
);
638 trace_usb_mtp_op_get_object_info(s
->dev
.addr
, o
->handle
, o
->path
);
640 usb_mtp_add_u32(d
, QEMU_STORAGE_ID
);
641 usb_mtp_add_u16(d
, o
->format
);
642 usb_mtp_add_u16(d
, 0);
643 usb_mtp_add_u32(d
, o
->stat
.st_size
);
645 usb_mtp_add_u16(d
, 0);
646 usb_mtp_add_u32(d
, 0);
647 usb_mtp_add_u32(d
, 0);
648 usb_mtp_add_u32(d
, 0);
649 usb_mtp_add_u32(d
, 0);
650 usb_mtp_add_u32(d
, 0);
651 usb_mtp_add_u32(d
, 0);
654 usb_mtp_add_u32(d
, o
->parent
->handle
);
656 usb_mtp_add_u32(d
, 0);
658 if (o
->format
== FMT_ASSOCIATION
) {
659 usb_mtp_add_u16(d
, 0x0001);
660 usb_mtp_add_u32(d
, 0x00000001);
661 usb_mtp_add_u32(d
, 0);
663 usb_mtp_add_u16(d
, 0);
664 usb_mtp_add_u32(d
, 0);
665 usb_mtp_add_u32(d
, 0);
668 usb_mtp_add_str(d
, o
->name
);
669 usb_mtp_add_time(d
, o
->stat
.st_ctime
);
670 usb_mtp_add_time(d
, o
->stat
.st_mtime
);
671 usb_mtp_add_wstr(d
, L
"");
676 static MTPData
*usb_mtp_get_object(MTPState
*s
, MTPControl
*c
,
679 MTPData
*d
= usb_mtp_data_alloc(c
);
681 trace_usb_mtp_op_get_object(s
->dev
.addr
, o
->handle
, o
->path
);
683 d
->fd
= open(o
->path
, O_RDONLY
);
685 usb_mtp_data_free(d
);
688 d
->length
= o
->stat
.st_size
;
690 d
->data
= g_malloc(d
->alloc
);
694 static MTPData
*usb_mtp_get_partial_object(MTPState
*s
, MTPControl
*c
,
697 MTPData
*d
= usb_mtp_data_alloc(c
);
700 trace_usb_mtp_op_get_partial_object(s
->dev
.addr
, o
->handle
, o
->path
,
701 c
->argv
[1], c
->argv
[2]);
703 d
->fd
= open(o
->path
, O_RDONLY
);
705 usb_mtp_data_free(d
);
710 if (offset
> o
->stat
.st_size
) {
711 offset
= o
->stat
.st_size
;
713 if (lseek(d
->fd
, offset
, SEEK_SET
) < 0) {
714 usb_mtp_data_free(d
);
718 d
->length
= c
->argv
[2];
719 if (d
->length
> o
->stat
.st_size
- offset
) {
720 d
->length
= o
->stat
.st_size
- offset
;
726 static void usb_mtp_command(MTPState
*s
, MTPControl
*c
)
728 MTPData
*data_in
= NULL
;
730 uint32_t nres
= 0, res0
= 0;
733 if (c
->code
>= CMD_CLOSE_SESSION
&& s
->session
== 0) {
734 usb_mtp_queue_result(s
, RES_SESSION_NOT_OPEN
,
739 /* process commands */
741 case CMD_GET_DEVICE_INFO
:
742 data_in
= usb_mtp_get_device_info(s
, c
);
744 case CMD_OPEN_SESSION
:
746 usb_mtp_queue_result(s
, RES_SESSION_ALREADY_OPEN
,
747 c
->trans
, 1, s
->session
, 0);
750 if (c
->argv
[0] == 0) {
751 usb_mtp_queue_result(s
, RES_INVALID_PARAMETER
,
755 trace_usb_mtp_op_open_session(s
->dev
.addr
);
756 s
->session
= c
->argv
[0];
757 usb_mtp_object_alloc(s
, s
->next_handle
++, NULL
, s
->root
);
759 case CMD_CLOSE_SESSION
:
760 trace_usb_mtp_op_close_session(s
->dev
.addr
);
763 usb_mtp_object_free(s
, QTAILQ_FIRST(&s
->objects
));
764 assert(QTAILQ_EMPTY(&s
->objects
));
766 case CMD_GET_STORAGE_IDS
:
767 data_in
= usb_mtp_get_storage_ids(s
, c
);
769 case CMD_GET_STORAGE_INFO
:
770 if (c
->argv
[0] != QEMU_STORAGE_ID
&&
771 c
->argv
[0] != 0xffffffff) {
772 usb_mtp_queue_result(s
, RES_INVALID_STORAGE_ID
,
776 data_in
= usb_mtp_get_storage_info(s
, c
);
778 case CMD_GET_NUM_OBJECTS
:
779 case CMD_GET_OBJECT_HANDLES
:
780 if (c
->argv
[0] != QEMU_STORAGE_ID
&&
781 c
->argv
[0] != 0xffffffff) {
782 usb_mtp_queue_result(s
, RES_INVALID_STORAGE_ID
,
786 if (c
->argv
[1] != 0x00000000) {
787 usb_mtp_queue_result(s
, RES_SPEC_BY_FORMAT_UNSUPPORTED
,
791 if (c
->argv
[2] == 0x00000000 ||
792 c
->argv
[2] == 0xffffffff) {
793 o
= QTAILQ_FIRST(&s
->objects
);
795 o
= usb_mtp_object_lookup(s
, c
->argv
[2]);
798 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
802 if (o
->format
!= FMT_ASSOCIATION
) {
803 usb_mtp_queue_result(s
, RES_INVALID_PARENT_OBJECT
,
807 usb_mtp_object_readdir(s
, o
);
808 if (c
->code
== CMD_GET_NUM_OBJECTS
) {
809 trace_usb_mtp_op_get_num_objects(s
->dev
.addr
, o
->handle
, o
->path
);
813 data_in
= usb_mtp_get_object_handles(s
, c
, o
);
816 case CMD_GET_OBJECT_INFO
:
817 o
= usb_mtp_object_lookup(s
, c
->argv
[0]);
819 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
823 data_in
= usb_mtp_get_object_info(s
, c
, o
);
826 o
= usb_mtp_object_lookup(s
, c
->argv
[0]);
828 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
832 if (o
->format
== FMT_ASSOCIATION
) {
833 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
837 data_in
= usb_mtp_get_object(s
, c
, o
);
838 if (data_in
== NULL
) {
839 usb_mtp_queue_result(s
, RES_GENERAL_ERROR
,
844 case CMD_GET_PARTIAL_OBJECT
:
845 o
= usb_mtp_object_lookup(s
, c
->argv
[0]);
847 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
851 if (o
->format
== FMT_ASSOCIATION
) {
852 usb_mtp_queue_result(s
, RES_INVALID_OBJECT_HANDLE
,
856 data_in
= usb_mtp_get_partial_object(s
, c
, o
);
857 if (data_in
== NULL
) {
858 usb_mtp_queue_result(s
, RES_GENERAL_ERROR
,
863 res0
= data_in
->length
;
866 trace_usb_mtp_op_unknown(s
->dev
.addr
, c
->code
);
867 usb_mtp_queue_result(s
, RES_OPERATION_NOT_SUPPORTED
,
872 /* return results on success */
874 assert(s
->data_in
== NULL
);
875 s
->data_in
= data_in
;
877 usb_mtp_queue_result(s
, RES_OK
, c
->trans
, nres
, res0
, 0);
880 /* ----------------------------------------------------------------------- */
882 static void usb_mtp_handle_reset(USBDevice
*dev
)
884 MTPState
*s
= USB_MTP(dev
);
886 trace_usb_mtp_reset(s
->dev
.addr
);
889 usb_mtp_data_free(s
->data_in
);
891 usb_mtp_data_free(s
->data_out
);
897 static void usb_mtp_handle_control(USBDevice
*dev
, USBPacket
*p
,
898 int request
, int value
, int index
,
899 int length
, uint8_t *data
)
903 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
908 trace_usb_mtp_stall(dev
->addr
, "unknown control request");
909 p
->status
= USB_RET_STALL
;
912 static void usb_mtp_cancel_packet(USBDevice
*dev
, USBPacket
*p
)
914 /* we don't use async packets, so this should never be called */
915 fprintf(stderr
, "%s\n", __func__
);
918 static void usb_mtp_handle_data(USBDevice
*dev
, USBPacket
*p
)
920 MTPState
*s
= USB_MTP(dev
);
922 mtp_container container
;
928 if (s
->data_out
!= NULL
) {
930 trace_usb_mtp_stall(s
->dev
.addr
, "awaiting data-out");
931 p
->status
= USB_RET_STALL
;
934 if (p
->iov
.size
< sizeof(container
)) {
935 trace_usb_mtp_stall(s
->dev
.addr
, "packet too small");
936 p
->status
= USB_RET_STALL
;
939 if (s
->data_in
!= NULL
) {
940 MTPData
*d
= s
->data_in
;
941 int dlen
= d
->length
- d
->offset
;
943 trace_usb_mtp_data_in(s
->dev
.addr
, d
->trans
, d
->length
);
944 container
.length
= cpu_to_le32(d
->length
+ sizeof(container
));
945 container
.type
= cpu_to_le16(TYPE_DATA
);
946 container
.code
= cpu_to_le16(d
->code
);
947 container
.trans
= cpu_to_le32(d
->trans
);
948 usb_packet_copy(p
, &container
, sizeof(container
));
950 if (dlen
> p
->iov
.size
- sizeof(container
)) {
951 dlen
= p
->iov
.size
- sizeof(container
);
954 if (dlen
> p
->iov
.size
) {
959 usb_packet_copy(p
, d
->data
+ d
->offset
, dlen
);
961 if (d
->alloc
< p
->iov
.size
) {
962 d
->alloc
= p
->iov
.size
;
963 d
->data
= g_realloc(d
->data
, d
->alloc
);
965 rc
= read(d
->fd
, d
->data
, dlen
);
967 memset(d
->data
, 0, dlen
);
968 s
->result
->code
= RES_INCOMPLETE_TRANSFER
;
970 usb_packet_copy(p
, d
->data
, dlen
);
973 if (d
->offset
== d
->length
) {
974 usb_mtp_data_free(s
->data_in
);
977 } else if (s
->result
!= NULL
) {
978 MTPControl
*r
= s
->result
;
979 int length
= sizeof(container
) + r
->argc
* sizeof(uint32_t);
980 if (r
->code
== RES_OK
) {
981 trace_usb_mtp_success(s
->dev
.addr
, r
->trans
,
982 (r
->argc
> 0) ? r
->argv
[0] : 0,
983 (r
->argc
> 1) ? r
->argv
[1] : 0);
985 trace_usb_mtp_error(s
->dev
.addr
, r
->code
, r
->trans
,
986 (r
->argc
> 0) ? r
->argv
[0] : 0,
987 (r
->argc
> 1) ? r
->argv
[1] : 0);
989 container
.length
= cpu_to_le32(length
);
990 container
.type
= cpu_to_le16(TYPE_RESPONSE
);
991 container
.code
= cpu_to_le16(r
->code
);
992 container
.trans
= cpu_to_le32(r
->trans
);
993 for (i
= 0; i
< r
->argc
; i
++) {
994 params
[i
] = cpu_to_le32(r
->argv
[i
]);
996 usb_packet_copy(p
, &container
, sizeof(container
));
997 usb_packet_copy(p
, ¶ms
, length
- sizeof(container
));
1003 if (p
->iov
.size
< sizeof(container
)) {
1004 trace_usb_mtp_stall(s
->dev
.addr
, "packet too small");
1005 p
->status
= USB_RET_STALL
;
1008 usb_packet_copy(p
, &container
, sizeof(container
));
1009 switch (le16_to_cpu(container
.type
)) {
1011 if (s
->data_in
|| s
->data_out
|| s
->result
) {
1012 trace_usb_mtp_stall(s
->dev
.addr
, "transaction inflight");
1013 p
->status
= USB_RET_STALL
;
1016 cmd
.code
= le16_to_cpu(container
.code
);
1017 cmd
.argc
= (le32_to_cpu(container
.length
) - sizeof(container
))
1019 cmd
.trans
= le32_to_cpu(container
.trans
);
1020 if (cmd
.argc
> ARRAY_SIZE(cmd
.argv
)) {
1021 cmd
.argc
= ARRAY_SIZE(cmd
.argv
);
1023 if (p
->iov
.size
< sizeof(container
) + cmd
.argc
* sizeof(uint32_t)) {
1024 trace_usb_mtp_stall(s
->dev
.addr
, "packet too small");
1025 p
->status
= USB_RET_STALL
;
1028 usb_packet_copy(p
, ¶ms
, cmd
.argc
* sizeof(uint32_t));
1029 for (i
= 0; i
< cmd
.argc
; i
++) {
1030 cmd
.argv
[i
] = le32_to_cpu(params
[i
]);
1032 trace_usb_mtp_command(s
->dev
.addr
, cmd
.code
, cmd
.trans
,
1033 (cmd
.argc
> 0) ? cmd
.argv
[0] : 0,
1034 (cmd
.argc
> 1) ? cmd
.argv
[1] : 0,
1035 (cmd
.argc
> 2) ? cmd
.argv
[2] : 0,
1036 (cmd
.argc
> 3) ? cmd
.argv
[3] : 0,
1037 (cmd
.argc
> 4) ? cmd
.argv
[4] : 0);
1038 usb_mtp_command(s
, &cmd
);
1041 /* not needed as long as the mtp device is read-only */
1042 p
->status
= USB_RET_STALL
;
1047 p
->status
= USB_RET_NAK
;
1050 trace_usb_mtp_stall(s
->dev
.addr
, "invalid endpoint");
1051 p
->status
= USB_RET_STALL
;
1055 if (p
->actual_length
== 0) {
1056 trace_usb_mtp_nak(s
->dev
.addr
, p
->ep
->nr
);
1057 p
->status
= USB_RET_NAK
;
1060 trace_usb_mtp_xfer(s
->dev
.addr
, p
->ep
->nr
, p
->actual_length
,
1066 static void usb_mtp_realize(USBDevice
*dev
, Error
**errp
)
1068 MTPState
*s
= USB_MTP(dev
);
1070 usb_desc_create_serial(dev
);
1072 QTAILQ_INIT(&s
->objects
);
1073 if (s
->desc
== NULL
) {
1074 if (s
->root
== NULL
) {
1075 error_setg(errp
, "usb-mtp: x-root property must be configured");
1078 s
->desc
= strrchr(s
->root
, '/');
1079 if (s
->desc
&& s
->desc
[0]) {
1080 s
->desc
= g_strdup(s
->desc
+ 1);
1082 s
->desc
= g_strdup("none");
1087 static const VMStateDescription vmstate_usb_mtp
= {
1091 .minimum_version_id
= 1,
1092 .fields
= (VMStateField
[]) {
1093 VMSTATE_USB_DEVICE(dev
, MTPState
),
1094 VMSTATE_END_OF_LIST()
1098 static Property mtp_properties
[] = {
1099 DEFINE_PROP_STRING("x-root", MTPState
, root
),
1100 DEFINE_PROP_STRING("desc", MTPState
, desc
),
1101 DEFINE_PROP_END_OF_LIST(),
1104 static void usb_mtp_class_initfn(ObjectClass
*klass
, void *data
)
1106 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1107 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
1109 uc
->realize
= usb_mtp_realize
;
1110 uc
->product_desc
= "QEMU USB MTP";
1111 uc
->usb_desc
= &desc
;
1112 uc
->cancel_packet
= usb_mtp_cancel_packet
;
1113 uc
->handle_attach
= usb_desc_attach
;
1114 uc
->handle_reset
= usb_mtp_handle_reset
;
1115 uc
->handle_control
= usb_mtp_handle_control
;
1116 uc
->handle_data
= usb_mtp_handle_data
;
1117 dc
->fw_name
= "mtp";
1118 dc
->vmsd
= &vmstate_usb_mtp
;
1119 dc
->props
= mtp_properties
;
1122 static TypeInfo mtp_info
= {
1123 .name
= TYPE_USB_MTP
,
1124 .parent
= TYPE_USB_DEVICE
,
1125 .instance_size
= sizeof(MTPState
),
1126 .class_init
= usb_mtp_class_initfn
,
1129 static void usb_mtp_register_types(void)
1131 type_register_static(&mtp_info
);
1134 type_init(usb_mtp_register_types
)