xref: /openbmc/qemu/hw/usb/dev-mtp.c (revision ac1d8878)
1 /*
2  * Media Transfer Protocol implementation, backed by host filesystem.
3  *
4  * Copyright Red Hat, Inc 2014
5  *
6  * Author:
7  *   Gerd Hoffmann <kraxel@redhat.com>
8  *
9  * This code is licensed under the GPL v2 or later.
10  */
11 
12 #include <wchar.h>
13 #include <dirent.h>
14 #include <unistd.h>
15 
16 #include <sys/stat.h>
17 #include <sys/statvfs.h>
18 #ifdef __linux__
19 #include <sys/inotify.h>
20 #include "qemu/main-loop.h"
21 #endif
22 
23 #include "qemu-common.h"
24 #include "qemu/iov.h"
25 #include "trace.h"
26 #include "hw/usb.h"
27 #include "hw/usb/desc.h"
28 
29 /* ----------------------------------------------------------------------- */
30 
31 enum mtp_container_type {
32     TYPE_COMMAND  = 1,
33     TYPE_DATA     = 2,
34     TYPE_RESPONSE = 3,
35     TYPE_EVENT    = 4,
36 };
37 
38 enum mtp_code {
39     /* command codes */
40     CMD_GET_DEVICE_INFO            = 0x1001,
41     CMD_OPEN_SESSION               = 0x1002,
42     CMD_CLOSE_SESSION              = 0x1003,
43     CMD_GET_STORAGE_IDS            = 0x1004,
44     CMD_GET_STORAGE_INFO           = 0x1005,
45     CMD_GET_NUM_OBJECTS            = 0x1006,
46     CMD_GET_OBJECT_HANDLES         = 0x1007,
47     CMD_GET_OBJECT_INFO            = 0x1008,
48     CMD_GET_OBJECT                 = 0x1009,
49     CMD_GET_PARTIAL_OBJECT         = 0x101b,
50 
51     /* response codes */
52     RES_OK                         = 0x2001,
53     RES_GENERAL_ERROR              = 0x2002,
54     RES_SESSION_NOT_OPEN           = 0x2003,
55     RES_INVALID_TRANSACTION_ID     = 0x2004,
56     RES_OPERATION_NOT_SUPPORTED    = 0x2005,
57     RES_PARAMETER_NOT_SUPPORTED    = 0x2006,
58     RES_INCOMPLETE_TRANSFER        = 0x2007,
59     RES_INVALID_STORAGE_ID         = 0x2008,
60     RES_INVALID_OBJECT_HANDLE      = 0x2009,
61     RES_SPEC_BY_FORMAT_UNSUPPORTED = 0x2014,
62     RES_INVALID_PARENT_OBJECT      = 0x201a,
63     RES_INVALID_PARAMETER          = 0x201d,
64     RES_SESSION_ALREADY_OPEN       = 0x201e,
65 
66     /* format codes */
67     FMT_UNDEFINED_OBJECT           = 0x3000,
68     FMT_ASSOCIATION                = 0x3001,
69 
70     /* event codes */
71     EVT_OBJ_ADDED                  = 0x4002,
72     EVT_OBJ_REMOVED                = 0x4003,
73     EVT_OBJ_INFO_CHANGED           = 0x4007,
74 };
75 
76 typedef struct {
77     uint32_t length;
78     uint16_t type;
79     uint16_t code;
80     uint32_t trans;
81 } QEMU_PACKED mtp_container;
82 
83 /* ----------------------------------------------------------------------- */
84 
85 typedef struct MTPState MTPState;
86 typedef struct MTPControl MTPControl;
87 typedef struct MTPData MTPData;
88 typedef struct MTPObject MTPObject;
89 
90 enum {
91     EP_DATA_IN = 1,
92     EP_DATA_OUT,
93     EP_EVENT,
94 };
95 
96 #ifdef __linux__
97 typedef struct MTPMonEntry MTPMonEntry;
98 
99 struct MTPMonEntry {
100     uint32_t event;
101     uint32_t handle;
102 
103     QTAILQ_ENTRY(MTPMonEntry) next;
104 };
105 #endif
106 
107 struct MTPControl {
108     uint16_t     code;
109     uint32_t     trans;
110     int          argc;
111     uint32_t     argv[5];
112 };
113 
114 struct MTPData {
115     uint16_t     code;
116     uint32_t     trans;
117     uint32_t     offset;
118     uint32_t     length;
119     uint32_t     alloc;
120     uint8_t      *data;
121     bool         first;
122     int          fd;
123 };
124 
125 struct MTPObject {
126     uint32_t     handle;
127     uint16_t     format;
128     char         *name;
129     char         *path;
130     struct stat  stat;
131 #ifdef __linux__
132     /* inotify watch cookie */
133     int          watchfd;
134 #endif
135     MTPObject    *parent;
136     uint32_t     nchildren;
137     QLIST_HEAD(, MTPObject) children;
138     QLIST_ENTRY(MTPObject) list;
139     bool         have_children;
140     QTAILQ_ENTRY(MTPObject) next;
141 };
142 
143 struct MTPState {
144     USBDevice    dev;
145     char         *root;
146     char         *desc;
147     uint32_t     flags;
148 
149     MTPData      *data_in;
150     MTPData      *data_out;
151     MTPControl   *result;
152     uint32_t     session;
153     uint32_t     next_handle;
154 
155     QTAILQ_HEAD(, MTPObject) objects;
156 #ifdef __linux__
157     /* inotify descriptor */
158     int          inotifyfd;
159     QTAILQ_HEAD(events, MTPMonEntry) events;
160 #endif
161 };
162 
163 #define TYPE_USB_MTP "usb-mtp"
164 #define USB_MTP(obj) OBJECT_CHECK(MTPState, (obj), TYPE_USB_MTP)
165 
166 #define QEMU_STORAGE_ID 0x00010001
167 
168 #define MTP_FLAG_WRITABLE 0
169 
170 #define FLAG_SET(_mtp, _flag)  ((_mtp)->flags & (1 << (_flag)))
171 
172 /* ----------------------------------------------------------------------- */
173 
174 #define MTP_MANUFACTURER  "QEMU"
175 #define MTP_PRODUCT       "QEMU filesharing"
176 
177 enum {
178     STR_MANUFACTURER = 1,
179     STR_PRODUCT,
180     STR_SERIALNUMBER,
181     STR_MTP,
182     STR_CONFIG_FULL,
183     STR_CONFIG_HIGH,
184     STR_CONFIG_SUPER,
185 };
186 
187 static const USBDescStrings desc_strings = {
188     [STR_MANUFACTURER] = MTP_MANUFACTURER,
189     [STR_PRODUCT]      = MTP_PRODUCT,
190     [STR_SERIALNUMBER] = "34617",
191     [STR_MTP]          = "MTP",
192     [STR_CONFIG_FULL]  = "Full speed config (usb 1.1)",
193     [STR_CONFIG_HIGH]  = "High speed config (usb 2.0)",
194     [STR_CONFIG_SUPER] = "Super speed config (usb 3.0)",
195 };
196 
197 static const USBDescIface desc_iface_full = {
198     .bInterfaceNumber              = 0,
199     .bNumEndpoints                 = 3,
200     .bInterfaceClass               = USB_CLASS_STILL_IMAGE,
201     .bInterfaceSubClass            = 0x01,
202     .bInterfaceProtocol            = 0x01,
203     .iInterface                    = STR_MTP,
204     .eps = (USBDescEndpoint[]) {
205         {
206             .bEndpointAddress      = USB_DIR_IN | EP_DATA_IN,
207             .bmAttributes          = USB_ENDPOINT_XFER_BULK,
208             .wMaxPacketSize        = 64,
209         },{
210             .bEndpointAddress      = USB_DIR_OUT | EP_DATA_OUT,
211             .bmAttributes          = USB_ENDPOINT_XFER_BULK,
212             .wMaxPacketSize        = 64,
213         },{
214             .bEndpointAddress      = USB_DIR_IN | EP_EVENT,
215             .bmAttributes          = USB_ENDPOINT_XFER_INT,
216             .wMaxPacketSize        = 64,
217             .bInterval             = 0x0a,
218         },
219     }
220 };
221 
222 static const USBDescDevice desc_device_full = {
223     .bcdUSB                        = 0x0200,
224     .bMaxPacketSize0               = 8,
225     .bNumConfigurations            = 1,
226     .confs = (USBDescConfig[]) {
227         {
228             .bNumInterfaces        = 1,
229             .bConfigurationValue   = 1,
230             .iConfiguration        = STR_CONFIG_FULL,
231             .bmAttributes          = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
232             .bMaxPower             = 2,
233             .nif = 1,
234             .ifs = &desc_iface_full,
235         },
236     },
237 };
238 
239 static const USBDescIface desc_iface_high = {
240     .bInterfaceNumber              = 0,
241     .bNumEndpoints                 = 3,
242     .bInterfaceClass               = USB_CLASS_STILL_IMAGE,
243     .bInterfaceSubClass            = 0x01,
244     .bInterfaceProtocol            = 0x01,
245     .iInterface                    = STR_MTP,
246     .eps = (USBDescEndpoint[]) {
247         {
248             .bEndpointAddress      = USB_DIR_IN | EP_DATA_IN,
249             .bmAttributes          = USB_ENDPOINT_XFER_BULK,
250             .wMaxPacketSize        = 512,
251         },{
252             .bEndpointAddress      = USB_DIR_OUT | EP_DATA_OUT,
253             .bmAttributes          = USB_ENDPOINT_XFER_BULK,
254             .wMaxPacketSize        = 512,
255         },{
256             .bEndpointAddress      = USB_DIR_IN | EP_EVENT,
257             .bmAttributes          = USB_ENDPOINT_XFER_INT,
258             .wMaxPacketSize        = 64,
259             .bInterval             = 0x0a,
260         },
261     }
262 };
263 
264 static const USBDescDevice desc_device_high = {
265     .bcdUSB                        = 0x0200,
266     .bMaxPacketSize0               = 64,
267     .bNumConfigurations            = 1,
268     .confs = (USBDescConfig[]) {
269         {
270             .bNumInterfaces        = 1,
271             .bConfigurationValue   = 1,
272             .iConfiguration        = STR_CONFIG_HIGH,
273             .bmAttributes          = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
274             .bMaxPower             = 2,
275             .nif = 1,
276             .ifs = &desc_iface_high,
277         },
278     },
279 };
280 
281 static const USBDescMSOS desc_msos = {
282     .CompatibleID = "MTP",
283     .SelectiveSuspendEnabled = true,
284 };
285 
286 static const USBDesc desc = {
287     .id = {
288         .idVendor          = 0x46f4, /* CRC16() of "QEMU" */
289         .idProduct         = 0x0004,
290         .bcdDevice         = 0,
291         .iManufacturer     = STR_MANUFACTURER,
292         .iProduct          = STR_PRODUCT,
293         .iSerialNumber     = STR_SERIALNUMBER,
294     },
295     .full  = &desc_device_full,
296     .high  = &desc_device_high,
297     .str   = desc_strings,
298     .msos  = &desc_msos,
299 };
300 
301 /* ----------------------------------------------------------------------- */
302 
303 static MTPObject *usb_mtp_object_alloc(MTPState *s, uint32_t handle,
304                                        MTPObject *parent, char *name)
305 {
306     MTPObject *o = g_new0(MTPObject, 1);
307 
308     if (name[0] == '.') {
309         goto ignore;
310     }
311 
312     o->handle = handle;
313     o->parent = parent;
314     o->name = g_strdup(name);
315     if (parent == NULL) {
316         o->path = g_strdup(name);
317     } else {
318         o->path = g_strdup_printf("%s/%s", parent->path, name);
319     }
320 
321     if (lstat(o->path, &o->stat) != 0) {
322         goto ignore;
323     }
324     if (S_ISREG(o->stat.st_mode)) {
325         o->format = FMT_UNDEFINED_OBJECT;
326     } else if (S_ISDIR(o->stat.st_mode)) {
327         o->format = FMT_ASSOCIATION;
328     } else {
329         goto ignore;
330     }
331 
332     if (access(o->path, R_OK) != 0) {
333         goto ignore;
334     }
335 
336     trace_usb_mtp_object_alloc(s->dev.addr, o->handle, o->path);
337 
338     QTAILQ_INSERT_TAIL(&s->objects, o, next);
339     return o;
340 
341 ignore:
342     g_free(o->name);
343     g_free(o->path);
344     g_free(o);
345     return NULL;
346 }
347 
348 static void usb_mtp_object_free(MTPState *s, MTPObject *o)
349 {
350     MTPObject *iter;
351 
352     if (!o) {
353         return;
354     }
355 
356     trace_usb_mtp_object_free(s->dev.addr, o->handle, o->path);
357 
358     QTAILQ_REMOVE(&s->objects, o, next);
359     if (o->parent) {
360         QLIST_REMOVE(o, list);
361         o->parent->nchildren--;
362     }
363 
364     while (!QLIST_EMPTY(&o->children)) {
365         iter = QLIST_FIRST(&o->children);
366         usb_mtp_object_free(s, iter);
367     }
368     g_free(o->name);
369     g_free(o->path);
370     g_free(o);
371 }
372 
373 static MTPObject *usb_mtp_object_lookup(MTPState *s, uint32_t handle)
374 {
375     MTPObject *o;
376 
377     QTAILQ_FOREACH(o, &s->objects, next) {
378         if (o->handle == handle) {
379             return o;
380         }
381     }
382     return NULL;
383 }
384 
385 static MTPObject *usb_mtp_add_child(MTPState *s, MTPObject *o,
386                                     char *name)
387 {
388     MTPObject *child =
389         usb_mtp_object_alloc(s, s->next_handle++, o, name);
390 
391     if (child) {
392         trace_usb_mtp_add_child(s->dev.addr, child->handle, child->path);
393         QLIST_INSERT_HEAD(&o->children, child, list);
394         o->nchildren++;
395 
396         if (child->format == FMT_ASSOCIATION) {
397             QLIST_INIT(&child->children);
398         }
399     }
400 
401     return child;
402 }
403 
404 #ifdef __linux__
405 static MTPObject *usb_mtp_object_lookup_name(MTPObject *parent,
406                                              char *name, int len)
407 {
408     MTPObject *iter;
409 
410     QLIST_FOREACH(iter, &parent->children, list) {
411         if (strncmp(iter->name, name, len) == 0) {
412             return iter;
413         }
414     }
415 
416     return NULL;
417 }
418 
419 static MTPObject *usb_mtp_object_lookup_wd(MTPState *s, int wd)
420 {
421     MTPObject *iter;
422 
423     QTAILQ_FOREACH(iter, &s->objects, next) {
424         if (iter->watchfd == wd) {
425             return iter;
426         }
427     }
428 
429     return NULL;
430 }
431 
432 static void inotify_watchfn(void *arg)
433 {
434     MTPState *s = arg;
435     ssize_t bytes;
436     /* From the man page: atleast one event can be read */
437     int len = sizeof(struct inotify_event) + NAME_MAX + 1;
438     int pos;
439     char buf[len];
440 
441     for (;;) {
442         bytes = read(s->inotifyfd, buf, len);
443         pos = 0;
444 
445         if (bytes <= 0) {
446             /* Better luck next time */
447             return;
448         }
449 
450         /*
451          * TODO: Ignore initiator initiated events.
452          * For now we are good because the store is RO
453          */
454         while (bytes > 0) {
455             char *p = buf + pos;
456             struct inotify_event *event = (struct inotify_event *)p;
457             int watchfd = 0;
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;
462             MTPObject *o;
463 
464             pos = pos + sizeof(struct inotify_event) + event->len;
465             bytes = bytes - pos;
466 
467             if (!parent) {
468                 continue;
469             }
470 
471             switch (mask) {
472             case IN_CREATE:
473                 if (usb_mtp_object_lookup_name
474                     (parent, event->name, event->len)) {
475                     /* Duplicate create event */
476                     continue;
477                 }
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);
482                 if (!o) {
483                     g_free(entry);
484                     continue;
485                 }
486                 o->watchfd = watchfd;
487                 trace_usb_mtp_inotify_event(s->dev.addr, event->name,
488                                             event->mask, "Obj Added");
489                 break;
490 
491             case IN_DELETE:
492                 /*
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
496                  * watchpoint
497                  */
498                 o = usb_mtp_object_lookup_name(parent, event->name, event->len);
499                 if (!o) {
500                     continue;
501                 }
502                 entry = g_new0(MTPMonEntry, 1);
503                 entry->handle = o->handle;
504                 entry->event = EVT_OBJ_REMOVED;
505                 usb_mtp_object_free(s, o);
506                 trace_usb_mtp_inotify_event(s->dev.addr, o->path,
507                                       event->mask, "Obj Deleted");
508                 break;
509 
510             case IN_MODIFY:
511                 o = usb_mtp_object_lookup_name(parent, event->name, event->len);
512                 if (!o) {
513                     continue;
514                 }
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");
520                 break;
521 
522             case IN_IGNORED:
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");
526                 break;
527 
528             default:
529                 fprintf(stderr, "usb-mtp: failed to parse inotify event\n");
530                 continue;
531             }
532 
533             if (entry) {
534                 QTAILQ_INSERT_HEAD(&s->events, entry, next);
535             }
536         }
537     }
538 }
539 
540 static int usb_mtp_inotify_init(MTPState *s)
541 {
542     int fd;
543 
544     fd = inotify_init1(IN_NONBLOCK);
545     if (fd == -1) {
546         return 1;
547     }
548 
549     QTAILQ_INIT(&s->events);
550     s->inotifyfd = fd;
551 
552     qemu_set_fd_handler(fd, inotify_watchfn, NULL, s);
553 
554     return 0;
555 }
556 
557 static void usb_mtp_inotify_cleanup(MTPState *s)
558 {
559     MTPMonEntry *e;
560 
561     if (!s->inotifyfd) {
562         return;
563     }
564 
565     qemu_set_fd_handler(s->inotifyfd, NULL, NULL, s);
566     close(s->inotifyfd);
567 
568     QTAILQ_FOREACH(e, &s->events, next) {
569         QTAILQ_REMOVE(&s->events, e, next);
570         g_free(e);
571     }
572 }
573 
574 static int usb_mtp_add_watch(int inotifyfd, char *path)
575 {
576     uint32_t mask = IN_CREATE | IN_DELETE | IN_MODIFY |
577         IN_ISDIR;
578 
579     return inotify_add_watch(inotifyfd, path, mask);
580 }
581 #endif
582 
583 static void usb_mtp_object_readdir(MTPState *s, MTPObject *o)
584 {
585     struct dirent *entry;
586     DIR *dir;
587 
588     if (o->have_children) {
589         return;
590     }
591     o->have_children = true;
592 
593     dir = opendir(o->path);
594     if (!dir) {
595         return;
596     }
597 #ifdef __linux__
598     int watchfd = usb_mtp_add_watch(s->inotifyfd, o->path);
599     if (watchfd == -1) {
600         fprintf(stderr, "usb-mtp: failed to add watch for %s\n", o->path);
601     } else {
602         trace_usb_mtp_inotify_event(s->dev.addr, o->path,
603                                     0, "Watch Added");
604         o->watchfd = watchfd;
605     }
606 #endif
607     while ((entry = readdir(dir)) != NULL) {
608         usb_mtp_add_child(s, o, entry->d_name);
609     }
610     closedir(dir);
611 }
612 
613 /* ----------------------------------------------------------------------- */
614 
615 static MTPData *usb_mtp_data_alloc(MTPControl *c)
616 {
617     MTPData *data = g_new0(MTPData, 1);
618 
619     data->code  = c->code;
620     data->trans = c->trans;
621     data->fd    = -1;
622     data->first = true;
623     return data;
624 }
625 
626 static void usb_mtp_data_free(MTPData *data)
627 {
628     if (data == NULL) {
629         return;
630     }
631     if (data->fd != -1) {
632         close(data->fd);
633     }
634     g_free(data->data);
635     g_free(data);
636 }
637 
638 static void usb_mtp_realloc(MTPData *data, uint32_t bytes)
639 {
640     if (data->length + bytes <= data->alloc) {
641         return;
642     }
643     data->alloc = (data->length + bytes + 0xff) & ~0xff;
644     data->data  = g_realloc(data->data, data->alloc);
645 }
646 
647 static void usb_mtp_add_u8(MTPData *data, uint8_t val)
648 {
649     usb_mtp_realloc(data, 1);
650     data->data[data->length++] = val;
651 }
652 
653 static void usb_mtp_add_u16(MTPData *data, uint16_t val)
654 {
655     usb_mtp_realloc(data, 2);
656     data->data[data->length++] = (val >> 0) & 0xff;
657     data->data[data->length++] = (val >> 8) & 0xff;
658 }
659 
660 static void usb_mtp_add_u32(MTPData *data, uint32_t val)
661 {
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;
667 }
668 
669 static void usb_mtp_add_u64(MTPData *data, uint64_t val)
670 {
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;
680 }
681 
682 static void usb_mtp_add_u16_array(MTPData *data, uint32_t len,
683                                   const uint16_t *vals)
684 {
685     int i;
686 
687     usb_mtp_add_u32(data, len);
688     for (i = 0; i < len; i++) {
689         usb_mtp_add_u16(data, vals[i]);
690     }
691 }
692 
693 static void usb_mtp_add_u32_array(MTPData *data, uint32_t len,
694                                   const uint32_t *vals)
695 {
696     int i;
697 
698     usb_mtp_add_u32(data, len);
699     for (i = 0; i < len; i++) {
700         usb_mtp_add_u32(data, vals[i]);
701     }
702 }
703 
704 static void usb_mtp_add_wstr(MTPData *data, const wchar_t *str)
705 {
706     uint32_t len = wcslen(str);
707     int i;
708 
709     if (len > 0) {
710         len++; /* include terminating L'\0' */
711     }
712 
713     usb_mtp_add_u8(data, len);
714     for (i = 0; i < len; i++) {
715         usb_mtp_add_u16(data, str[i]);
716     }
717 }
718 
719 static void usb_mtp_add_str(MTPData *data, const char *str)
720 {
721     uint32_t len = strlen(str)+1;
722     wchar_t wstr[len];
723     size_t ret;
724 
725     ret = mbstowcs(wstr, str, len);
726     if (ret == -1) {
727         usb_mtp_add_wstr(data, L"Oops");
728     } else {
729         usb_mtp_add_wstr(data, wstr);
730     }
731 }
732 
733 static void usb_mtp_add_time(MTPData *data, time_t time)
734 {
735     char buf[16];
736     struct tm tm;
737 
738     gmtime_r(&time, &tm);
739     strftime(buf, sizeof(buf), "%Y%m%dT%H%M%S", &tm);
740     usb_mtp_add_str(data, buf);
741 }
742 
743 /* ----------------------------------------------------------------------- */
744 
745 static void usb_mtp_queue_result(MTPState *s, uint16_t code, uint32_t trans,
746                                  int argc, uint32_t arg0, uint32_t arg1)
747 {
748     MTPControl *c = g_new0(MTPControl, 1);
749 
750     c->code  = code;
751     c->trans = trans;
752     c->argc  = argc;
753     if (argc > 0) {
754         c->argv[0] = arg0;
755     }
756     if (argc > 1) {
757         c->argv[1] = arg1;
758     }
759 
760     assert(s->result == NULL);
761     s->result = c;
762 }
763 
764 /* ----------------------------------------------------------------------- */
765 
766 static MTPData *usb_mtp_get_device_info(MTPState *s, MTPControl *c)
767 {
768     static const uint16_t ops[] = {
769         CMD_GET_DEVICE_INFO,
770         CMD_OPEN_SESSION,
771         CMD_CLOSE_SESSION,
772         CMD_GET_STORAGE_IDS,
773         CMD_GET_STORAGE_INFO,
774         CMD_GET_NUM_OBJECTS,
775         CMD_GET_OBJECT_HANDLES,
776         CMD_GET_OBJECT_INFO,
777         CMD_GET_OBJECT,
778         CMD_GET_PARTIAL_OBJECT,
779     };
780     static const uint16_t fmt[] = {
781         FMT_UNDEFINED_OBJECT,
782         FMT_ASSOCIATION,
783     };
784     MTPData *d = usb_mtp_data_alloc(c);
785 
786     trace_usb_mtp_op_get_device_info(s->dev.addr);
787 
788     usb_mtp_add_u16(d, 100);
789     usb_mtp_add_u32(d, 0xffffffff);
790     usb_mtp_add_u16(d, 0x0101);
791     usb_mtp_add_wstr(d, L"");
792     usb_mtp_add_u16(d, 0x0000);
793 
794     usb_mtp_add_u16_array(d, ARRAY_SIZE(ops), ops);
795     usb_mtp_add_u16_array(d, 0, NULL);
796     usb_mtp_add_u16_array(d, 0, NULL);
797     usb_mtp_add_u16_array(d, 0, NULL);
798     usb_mtp_add_u16_array(d, ARRAY_SIZE(fmt), fmt);
799 
800     usb_mtp_add_wstr(d, L"" MTP_MANUFACTURER);
801     usb_mtp_add_wstr(d, L"" MTP_PRODUCT);
802     usb_mtp_add_wstr(d, L"0.1");
803     usb_mtp_add_wstr(d, L"0123456789abcdef0123456789abcdef");
804 
805     return d;
806 }
807 
808 static MTPData *usb_mtp_get_storage_ids(MTPState *s, MTPControl *c)
809 {
810     static const uint32_t ids[] = {
811         QEMU_STORAGE_ID,
812     };
813     MTPData *d = usb_mtp_data_alloc(c);
814 
815     trace_usb_mtp_op_get_storage_ids(s->dev.addr);
816 
817     usb_mtp_add_u32_array(d, ARRAY_SIZE(ids), ids);
818 
819     return d;
820 }
821 
822 static MTPData *usb_mtp_get_storage_info(MTPState *s, MTPControl *c)
823 {
824     MTPData *d = usb_mtp_data_alloc(c);
825     struct statvfs buf;
826     int rc;
827 
828     trace_usb_mtp_op_get_storage_info(s->dev.addr);
829 
830     if (FLAG_SET(s, MTP_FLAG_WRITABLE)) {
831         usb_mtp_add_u16(d, 0x0003);
832         usb_mtp_add_u16(d, 0x0002);
833         usb_mtp_add_u16(d, 0x0000);
834     } else {
835         usb_mtp_add_u16(d, 0x0001);
836         usb_mtp_add_u16(d, 0x0002);
837         usb_mtp_add_u16(d, 0x0001);
838     }
839 
840     rc = statvfs(s->root, &buf);
841     if (rc == 0) {
842         usb_mtp_add_u64(d, (uint64_t)buf.f_frsize * buf.f_blocks);
843         usb_mtp_add_u64(d, (uint64_t)buf.f_bavail * buf.f_blocks);
844         usb_mtp_add_u32(d, buf.f_ffree);
845     } else {
846         usb_mtp_add_u64(d, 0xffffffff);
847         usb_mtp_add_u64(d, 0xffffffff);
848         usb_mtp_add_u32(d, 0xffffffff);
849     }
850 
851     usb_mtp_add_str(d, s->desc);
852     usb_mtp_add_wstr(d, L"123456789abcdef");
853     return d;
854 }
855 
856 static MTPData *usb_mtp_get_object_handles(MTPState *s, MTPControl *c,
857                                            MTPObject *o)
858 {
859     MTPData *d = usb_mtp_data_alloc(c);
860     uint32_t i = 0, handles[o->nchildren];
861     MTPObject *iter;
862 
863     trace_usb_mtp_op_get_object_handles(s->dev.addr, o->handle, o->path);
864 
865     QLIST_FOREACH(iter, &o->children, list) {
866         handles[i++] = iter->handle;
867     }
868     assert(i == o->nchildren);
869     usb_mtp_add_u32_array(d, o->nchildren, handles);
870 
871     return d;
872 }
873 
874 static MTPData *usb_mtp_get_object_info(MTPState *s, MTPControl *c,
875                                         MTPObject *o)
876 {
877     MTPData *d = usb_mtp_data_alloc(c);
878 
879     trace_usb_mtp_op_get_object_info(s->dev.addr, o->handle, o->path);
880 
881     usb_mtp_add_u32(d, QEMU_STORAGE_ID);
882     usb_mtp_add_u16(d, o->format);
883     usb_mtp_add_u16(d, 0);
884     usb_mtp_add_u32(d, o->stat.st_size);
885 
886     usb_mtp_add_u16(d, 0);
887     usb_mtp_add_u32(d, 0);
888     usb_mtp_add_u32(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 
894     if (o->parent) {
895         usb_mtp_add_u32(d, o->parent->handle);
896     } else {
897         usb_mtp_add_u32(d, 0);
898     }
899     if (o->format == FMT_ASSOCIATION) {
900         usb_mtp_add_u16(d, 0x0001);
901         usb_mtp_add_u32(d, 0x00000001);
902         usb_mtp_add_u32(d, 0);
903     } else {
904         usb_mtp_add_u16(d, 0);
905         usb_mtp_add_u32(d, 0);
906         usb_mtp_add_u32(d, 0);
907     }
908 
909     usb_mtp_add_str(d, o->name);
910     usb_mtp_add_time(d, o->stat.st_ctime);
911     usb_mtp_add_time(d, o->stat.st_mtime);
912     usb_mtp_add_wstr(d, L"");
913 
914     return d;
915 }
916 
917 static MTPData *usb_mtp_get_object(MTPState *s, MTPControl *c,
918                                    MTPObject *o)
919 {
920     MTPData *d = usb_mtp_data_alloc(c);
921 
922     trace_usb_mtp_op_get_object(s->dev.addr, o->handle, o->path);
923 
924     d->fd = open(o->path, O_RDONLY);
925     if (d->fd == -1) {
926         usb_mtp_data_free(d);
927         return NULL;
928     }
929     d->length = o->stat.st_size;
930     d->alloc  = 512;
931     d->data   = g_malloc(d->alloc);
932     return d;
933 }
934 
935 static MTPData *usb_mtp_get_partial_object(MTPState *s, MTPControl *c,
936                                            MTPObject *o)
937 {
938     MTPData *d = usb_mtp_data_alloc(c);
939     off_t offset;
940 
941     trace_usb_mtp_op_get_partial_object(s->dev.addr, o->handle, o->path,
942                                         c->argv[1], c->argv[2]);
943 
944     d->fd = open(o->path, O_RDONLY);
945     if (d->fd == -1) {
946         usb_mtp_data_free(d);
947         return NULL;
948     }
949 
950     offset = c->argv[1];
951     if (offset > o->stat.st_size) {
952         offset = o->stat.st_size;
953     }
954     if (lseek(d->fd, offset, SEEK_SET) < 0) {
955         usb_mtp_data_free(d);
956         return NULL;
957     }
958 
959     d->length = c->argv[2];
960     if (d->length > o->stat.st_size - offset) {
961         d->length = o->stat.st_size - offset;
962     }
963 
964     return d;
965 }
966 
967 static void usb_mtp_command(MTPState *s, MTPControl *c)
968 {
969     MTPData *data_in = NULL;
970     MTPObject *o;
971     uint32_t nres = 0, res0 = 0;
972 
973     /* sanity checks */
974     if (c->code >= CMD_CLOSE_SESSION && s->session == 0) {
975         usb_mtp_queue_result(s, RES_SESSION_NOT_OPEN,
976                              c->trans, 0, 0, 0);
977         return;
978     }
979 
980     /* process commands */
981     switch (c->code) {
982     case CMD_GET_DEVICE_INFO:
983         data_in = usb_mtp_get_device_info(s, c);
984         break;
985     case CMD_OPEN_SESSION:
986         if (s->session) {
987             usb_mtp_queue_result(s, RES_SESSION_ALREADY_OPEN,
988                                  c->trans, 1, s->session, 0);
989             return;
990         }
991         if (c->argv[0] == 0) {
992             usb_mtp_queue_result(s, RES_INVALID_PARAMETER,
993                                  c->trans, 0, 0, 0);
994             return;
995         }
996         trace_usb_mtp_op_open_session(s->dev.addr);
997         s->session = c->argv[0];
998         usb_mtp_object_alloc(s, s->next_handle++, NULL, s->root);
999 #ifdef __linux__
1000         if (usb_mtp_inotify_init(s)) {
1001             fprintf(stderr, "usb-mtp: file monitoring init failed\n");
1002         }
1003 #endif
1004         break;
1005     case CMD_CLOSE_SESSION:
1006         trace_usb_mtp_op_close_session(s->dev.addr);
1007         s->session = 0;
1008         s->next_handle = 0;
1009 #ifdef __linux__
1010         usb_mtp_inotify_cleanup(s);
1011 #endif
1012         usb_mtp_object_free(s, QTAILQ_FIRST(&s->objects));
1013         assert(QTAILQ_EMPTY(&s->objects));
1014         break;
1015     case CMD_GET_STORAGE_IDS:
1016         data_in = usb_mtp_get_storage_ids(s, c);
1017         break;
1018     case CMD_GET_STORAGE_INFO:
1019         if (c->argv[0] != QEMU_STORAGE_ID &&
1020             c->argv[0] != 0xffffffff) {
1021             usb_mtp_queue_result(s, RES_INVALID_STORAGE_ID,
1022                                  c->trans, 0, 0, 0);
1023             return;
1024         }
1025         data_in = usb_mtp_get_storage_info(s, c);
1026         break;
1027     case CMD_GET_NUM_OBJECTS:
1028     case CMD_GET_OBJECT_HANDLES:
1029         if (c->argv[0] != QEMU_STORAGE_ID &&
1030             c->argv[0] != 0xffffffff) {
1031             usb_mtp_queue_result(s, RES_INVALID_STORAGE_ID,
1032                                  c->trans, 0, 0, 0);
1033             return;
1034         }
1035         if (c->argv[1] != 0x00000000) {
1036             usb_mtp_queue_result(s, RES_SPEC_BY_FORMAT_UNSUPPORTED,
1037                                  c->trans, 0, 0, 0);
1038             return;
1039         }
1040         if (c->argv[2] == 0x00000000 ||
1041             c->argv[2] == 0xffffffff) {
1042             o = QTAILQ_FIRST(&s->objects);
1043         } else {
1044             o = usb_mtp_object_lookup(s, c->argv[2]);
1045         }
1046         if (o == NULL) {
1047             usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE,
1048                                  c->trans, 0, 0, 0);
1049             return;
1050         }
1051         if (o->format != FMT_ASSOCIATION) {
1052             usb_mtp_queue_result(s, RES_INVALID_PARENT_OBJECT,
1053                                  c->trans, 0, 0, 0);
1054             return;
1055         }
1056         usb_mtp_object_readdir(s, o);
1057         if (c->code == CMD_GET_NUM_OBJECTS) {
1058             trace_usb_mtp_op_get_num_objects(s->dev.addr, o->handle, o->path);
1059             nres = 1;
1060             res0 = o->nchildren;
1061         } else {
1062             data_in = usb_mtp_get_object_handles(s, c, o);
1063         }
1064         break;
1065     case CMD_GET_OBJECT_INFO:
1066         o = usb_mtp_object_lookup(s, c->argv[0]);
1067         if (o == NULL) {
1068             usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE,
1069                                  c->trans, 0, 0, 0);
1070             return;
1071         }
1072         data_in = usb_mtp_get_object_info(s, c, o);
1073         break;
1074     case CMD_GET_OBJECT:
1075         o = usb_mtp_object_lookup(s, c->argv[0]);
1076         if (o == NULL) {
1077             usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE,
1078                                  c->trans, 0, 0, 0);
1079             return;
1080         }
1081         if (o->format == FMT_ASSOCIATION) {
1082             usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE,
1083                                  c->trans, 0, 0, 0);
1084             return;
1085         }
1086         data_in = usb_mtp_get_object(s, c, o);
1087         if (data_in == NULL) {
1088             usb_mtp_queue_result(s, RES_GENERAL_ERROR,
1089                                  c->trans, 0, 0, 0);
1090             return;
1091         }
1092         break;
1093     case CMD_GET_PARTIAL_OBJECT:
1094         o = usb_mtp_object_lookup(s, c->argv[0]);
1095         if (o == NULL) {
1096             usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE,
1097                                  c->trans, 0, 0, 0);
1098             return;
1099         }
1100         if (o->format == FMT_ASSOCIATION) {
1101             usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE,
1102                                  c->trans, 0, 0, 0);
1103             return;
1104         }
1105         data_in = usb_mtp_get_partial_object(s, c, o);
1106         if (data_in == NULL) {
1107             usb_mtp_queue_result(s, RES_GENERAL_ERROR,
1108                                  c->trans, 0, 0, 0);
1109             return;
1110         }
1111         nres = 1;
1112         res0 = data_in->length;
1113         break;
1114     default:
1115         trace_usb_mtp_op_unknown(s->dev.addr, c->code);
1116         usb_mtp_queue_result(s, RES_OPERATION_NOT_SUPPORTED,
1117                              c->trans, 0, 0, 0);
1118         return;
1119     }
1120 
1121     /* return results on success */
1122     if (data_in) {
1123         assert(s->data_in == NULL);
1124         s->data_in = data_in;
1125     }
1126     usb_mtp_queue_result(s, RES_OK, c->trans, nres, res0, 0);
1127 }
1128 
1129 /* ----------------------------------------------------------------------- */
1130 
1131 static void usb_mtp_handle_reset(USBDevice *dev)
1132 {
1133     MTPState *s = USB_MTP(dev);
1134 
1135     trace_usb_mtp_reset(s->dev.addr);
1136 
1137 #ifdef __linux__
1138     usb_mtp_inotify_cleanup(s);
1139 #endif
1140     usb_mtp_object_free(s, QTAILQ_FIRST(&s->objects));
1141     s->session = 0;
1142     usb_mtp_data_free(s->data_in);
1143     s->data_in = NULL;
1144     usb_mtp_data_free(s->data_out);
1145     s->data_out = NULL;
1146     g_free(s->result);
1147     s->result = NULL;
1148 }
1149 
1150 static void usb_mtp_handle_control(USBDevice *dev, USBPacket *p,
1151                                    int request, int value, int index,
1152                                    int length, uint8_t *data)
1153 {
1154     int ret;
1155 
1156     ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
1157     if (ret >= 0) {
1158         return;
1159     }
1160 
1161     trace_usb_mtp_stall(dev->addr, "unknown control request");
1162     p->status = USB_RET_STALL;
1163 }
1164 
1165 static void usb_mtp_cancel_packet(USBDevice *dev, USBPacket *p)
1166 {
1167     /* we don't use async packets, so this should never be called */
1168     fprintf(stderr, "%s\n", __func__);
1169 }
1170 
1171 static void usb_mtp_handle_data(USBDevice *dev, USBPacket *p)
1172 {
1173     MTPState *s = USB_MTP(dev);
1174     MTPControl cmd;
1175     mtp_container container;
1176     uint32_t params[5];
1177     int i, rc;
1178 
1179     switch (p->ep->nr) {
1180     case EP_DATA_IN:
1181         if (s->data_out != NULL) {
1182             /* guest bug */
1183             trace_usb_mtp_stall(s->dev.addr, "awaiting data-out");
1184             p->status = USB_RET_STALL;
1185             return;
1186         }
1187         if (p->iov.size < sizeof(container)) {
1188             trace_usb_mtp_stall(s->dev.addr, "packet too small");
1189             p->status = USB_RET_STALL;
1190             return;
1191         }
1192         if (s->data_in !=  NULL) {
1193             MTPData *d = s->data_in;
1194             int dlen = d->length - d->offset;
1195             if (d->first) {
1196                 trace_usb_mtp_data_in(s->dev.addr, d->trans, d->length);
1197                 container.length = cpu_to_le32(d->length + sizeof(container));
1198                 container.type   = cpu_to_le16(TYPE_DATA);
1199                 container.code   = cpu_to_le16(d->code);
1200                 container.trans  = cpu_to_le32(d->trans);
1201                 usb_packet_copy(p, &container, sizeof(container));
1202                 d->first = false;
1203                 if (dlen > p->iov.size - sizeof(container)) {
1204                     dlen = p->iov.size - sizeof(container);
1205                 }
1206             } else {
1207                 if (dlen > p->iov.size) {
1208                     dlen = p->iov.size;
1209                 }
1210             }
1211             if (d->fd == -1) {
1212                 usb_packet_copy(p, d->data + d->offset, dlen);
1213             } else {
1214                 if (d->alloc < p->iov.size) {
1215                     d->alloc = p->iov.size;
1216                     d->data = g_realloc(d->data, d->alloc);
1217                 }
1218                 rc = read(d->fd, d->data, dlen);
1219                 if (rc != dlen) {
1220                     memset(d->data, 0, dlen);
1221                     s->result->code = RES_INCOMPLETE_TRANSFER;
1222                 }
1223                 usb_packet_copy(p, d->data, dlen);
1224             }
1225             d->offset += dlen;
1226             if (d->offset == d->length) {
1227                 usb_mtp_data_free(s->data_in);
1228                 s->data_in = NULL;
1229             }
1230         } else if (s->result != NULL) {
1231             MTPControl *r = s->result;
1232             int length = sizeof(container) + r->argc * sizeof(uint32_t);
1233             if (r->code == RES_OK) {
1234                 trace_usb_mtp_success(s->dev.addr, r->trans,
1235                                       (r->argc > 0) ? r->argv[0] : 0,
1236                                       (r->argc > 1) ? r->argv[1] : 0);
1237             } else {
1238                 trace_usb_mtp_error(s->dev.addr, r->code, r->trans,
1239                                     (r->argc > 0) ? r->argv[0] : 0,
1240                                     (r->argc > 1) ? r->argv[1] : 0);
1241             }
1242             container.length = cpu_to_le32(length);
1243             container.type   = cpu_to_le16(TYPE_RESPONSE);
1244             container.code   = cpu_to_le16(r->code);
1245             container.trans  = cpu_to_le32(r->trans);
1246             for (i = 0; i < r->argc; i++) {
1247                 params[i] = cpu_to_le32(r->argv[i]);
1248             }
1249             usb_packet_copy(p, &container, sizeof(container));
1250             usb_packet_copy(p, &params, length - sizeof(container));
1251             g_free(s->result);
1252             s->result = NULL;
1253         }
1254         break;
1255     case EP_DATA_OUT:
1256         if (p->iov.size < sizeof(container)) {
1257             trace_usb_mtp_stall(s->dev.addr, "packet too small");
1258             p->status = USB_RET_STALL;
1259             return;
1260         }
1261         usb_packet_copy(p, &container, sizeof(container));
1262         switch (le16_to_cpu(container.type)) {
1263         case TYPE_COMMAND:
1264             if (s->data_in || s->data_out || s->result) {
1265                 trace_usb_mtp_stall(s->dev.addr, "transaction inflight");
1266                 p->status = USB_RET_STALL;
1267                 return;
1268             }
1269             cmd.code = le16_to_cpu(container.code);
1270             cmd.argc = (le32_to_cpu(container.length) - sizeof(container))
1271                 / sizeof(uint32_t);
1272             cmd.trans = le32_to_cpu(container.trans);
1273             if (cmd.argc > ARRAY_SIZE(cmd.argv)) {
1274                 cmd.argc = ARRAY_SIZE(cmd.argv);
1275             }
1276             if (p->iov.size < sizeof(container) + cmd.argc * sizeof(uint32_t)) {
1277                 trace_usb_mtp_stall(s->dev.addr, "packet too small");
1278                 p->status = USB_RET_STALL;
1279                 return;
1280             }
1281             usb_packet_copy(p, &params, cmd.argc * sizeof(uint32_t));
1282             for (i = 0; i < cmd.argc; i++) {
1283                 cmd.argv[i] = le32_to_cpu(params[i]);
1284             }
1285             trace_usb_mtp_command(s->dev.addr, cmd.code, cmd.trans,
1286                                   (cmd.argc > 0) ? cmd.argv[0] : 0,
1287                                   (cmd.argc > 1) ? cmd.argv[1] : 0,
1288                                   (cmd.argc > 2) ? cmd.argv[2] : 0,
1289                                   (cmd.argc > 3) ? cmd.argv[3] : 0,
1290                                   (cmd.argc > 4) ? cmd.argv[4] : 0);
1291             usb_mtp_command(s, &cmd);
1292             break;
1293         default:
1294             /* not needed as long as the mtp device is read-only */
1295             p->status = USB_RET_STALL;
1296             return;
1297         }
1298         break;
1299     case EP_EVENT:
1300 #ifdef __linux__
1301         if (!QTAILQ_EMPTY(&s->events)) {
1302             struct MTPMonEntry *e = QTAILQ_LAST(&s->events, events);
1303             uint32_t handle;
1304             int len = sizeof(container) + sizeof(uint32_t);
1305 
1306             if (p->iov.size < len) {
1307                 trace_usb_mtp_stall(s->dev.addr,
1308                                     "packet too small to send event");
1309                 p->status = USB_RET_STALL;
1310                 return;
1311             }
1312 
1313             QTAILQ_REMOVE(&s->events, e, next);
1314             container.length = cpu_to_le32(len);
1315             container.type = cpu_to_le32(TYPE_EVENT);
1316             container.code = cpu_to_le16(e->event);
1317             container.trans = 0; /* no trans specific events */
1318             handle = cpu_to_le32(e->handle);
1319             usb_packet_copy(p, &container, sizeof(container));
1320             usb_packet_copy(p, &handle, sizeof(uint32_t));
1321             g_free(e);
1322             return;
1323         }
1324 #endif
1325         p->status = USB_RET_NAK;
1326         return;
1327     default:
1328         trace_usb_mtp_stall(s->dev.addr, "invalid endpoint");
1329         p->status = USB_RET_STALL;
1330         return;
1331     }
1332 
1333     if (p->actual_length == 0) {
1334         trace_usb_mtp_nak(s->dev.addr, p->ep->nr);
1335         p->status = USB_RET_NAK;
1336         return;
1337     } else {
1338         trace_usb_mtp_xfer(s->dev.addr, p->ep->nr, p->actual_length,
1339                            p->iov.size);
1340         return;
1341     }
1342 }
1343 
1344 static void usb_mtp_realize(USBDevice *dev, Error **errp)
1345 {
1346     MTPState *s = USB_MTP(dev);
1347 
1348     usb_desc_create_serial(dev);
1349     usb_desc_init(dev);
1350     QTAILQ_INIT(&s->objects);
1351     if (s->desc == NULL) {
1352         if (s->root == NULL) {
1353             error_setg(errp, "usb-mtp: x-root property must be configured");
1354             return;
1355         }
1356         s->desc = strrchr(s->root, '/');
1357         if (s->desc && s->desc[0]) {
1358             s->desc = g_strdup(s->desc + 1);
1359         } else {
1360             s->desc = g_strdup("none");
1361         }
1362     }
1363 }
1364 
1365 static const VMStateDescription vmstate_usb_mtp = {
1366     .name = "usb-mtp",
1367     .unmigratable = 1,
1368     .version_id = 1,
1369     .minimum_version_id = 1,
1370     .fields = (VMStateField[]) {
1371         VMSTATE_USB_DEVICE(dev, MTPState),
1372         VMSTATE_END_OF_LIST()
1373     }
1374 };
1375 
1376 static Property mtp_properties[] = {
1377     DEFINE_PROP_STRING("x-root", MTPState, root),
1378     DEFINE_PROP_STRING("desc", MTPState, desc),
1379     DEFINE_PROP_END_OF_LIST(),
1380 };
1381 
1382 static void usb_mtp_class_initfn(ObjectClass *klass, void *data)
1383 {
1384     DeviceClass *dc = DEVICE_CLASS(klass);
1385     USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
1386 
1387     uc->realize        = usb_mtp_realize;
1388     uc->product_desc   = "QEMU USB MTP";
1389     uc->usb_desc       = &desc;
1390     uc->cancel_packet  = usb_mtp_cancel_packet;
1391     uc->handle_attach  = usb_desc_attach;
1392     uc->handle_reset   = usb_mtp_handle_reset;
1393     uc->handle_control = usb_mtp_handle_control;
1394     uc->handle_data    = usb_mtp_handle_data;
1395     dc->fw_name = "mtp";
1396     dc->vmsd = &vmstate_usb_mtp;
1397     dc->props = mtp_properties;
1398 }
1399 
1400 static TypeInfo mtp_info = {
1401     .name          = TYPE_USB_MTP,
1402     .parent        = TYPE_USB_DEVICE,
1403     .instance_size = sizeof(MTPState),
1404     .class_init    = usb_mtp_class_initfn,
1405 };
1406 
1407 static void usb_mtp_register_types(void)
1408 {
1409     type_register_static(&mtp_info);
1410 }
1411 
1412 type_init(usb_mtp_register_types)
1413