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 "qemu/osdep.h" 13 #include "qapi/error.h" 14 #include <wchar.h> 15 #include <dirent.h> 16 17 #include <sys/statvfs.h> 18 #ifdef CONFIG_INOTIFY1 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_DELETE_OBJECT = 0x100b, 50 CMD_SEND_OBJECT_INFO = 0x100c, 51 CMD_SEND_OBJECT = 0x100d, 52 CMD_GET_PARTIAL_OBJECT = 0x101b, 53 CMD_GET_OBJECT_PROPS_SUPPORTED = 0x9801, 54 CMD_GET_OBJECT_PROP_DESC = 0x9802, 55 CMD_GET_OBJECT_PROP_VALUE = 0x9803, 56 57 /* response codes */ 58 RES_OK = 0x2001, 59 RES_GENERAL_ERROR = 0x2002, 60 RES_SESSION_NOT_OPEN = 0x2003, 61 RES_INVALID_TRANSACTION_ID = 0x2004, 62 RES_OPERATION_NOT_SUPPORTED = 0x2005, 63 RES_PARAMETER_NOT_SUPPORTED = 0x2006, 64 RES_INCOMPLETE_TRANSFER = 0x2007, 65 RES_INVALID_STORAGE_ID = 0x2008, 66 RES_INVALID_OBJECT_HANDLE = 0x2009, 67 RES_INVALID_OBJECT_FORMAT_CODE = 0x200b, 68 RES_STORE_FULL = 0x200c, 69 RES_STORE_READ_ONLY = 0x200e, 70 RES_PARTIAL_DELETE = 0x2012, 71 RES_STORE_NOT_AVAILABLE = 0x2013, 72 RES_SPEC_BY_FORMAT_UNSUPPORTED = 0x2014, 73 RES_INVALID_OBJECTINFO = 0x2015, 74 RES_DESTINATION_UNSUPPORTED = 0x2020, 75 RES_INVALID_PARENT_OBJECT = 0x201a, 76 RES_INVALID_PARAMETER = 0x201d, 77 RES_SESSION_ALREADY_OPEN = 0x201e, 78 RES_INVALID_OBJECT_PROP_CODE = 0xA801, 79 80 /* format codes */ 81 FMT_UNDEFINED_OBJECT = 0x3000, 82 FMT_ASSOCIATION = 0x3001, 83 84 /* event codes */ 85 EVT_OBJ_ADDED = 0x4002, 86 EVT_OBJ_REMOVED = 0x4003, 87 EVT_OBJ_INFO_CHANGED = 0x4007, 88 89 /* object properties */ 90 PROP_STORAGE_ID = 0xDC01, 91 PROP_OBJECT_FORMAT = 0xDC02, 92 PROP_OBJECT_COMPRESSED_SIZE = 0xDC04, 93 PROP_PARENT_OBJECT = 0xDC0B, 94 PROP_PERSISTENT_UNIQUE_OBJECT_IDENTIFIER = 0xDC41, 95 PROP_NAME = 0xDC44, 96 }; 97 98 enum mtp_data_type { 99 DATA_TYPE_UINT16 = 0x0004, 100 DATA_TYPE_UINT32 = 0x0006, 101 DATA_TYPE_UINT64 = 0x0008, 102 DATA_TYPE_UINT128 = 0x000a, 103 DATA_TYPE_STRING = 0xffff, 104 }; 105 106 typedef struct { 107 uint32_t length; 108 uint16_t type; 109 uint16_t code; 110 uint32_t trans; 111 } QEMU_PACKED mtp_container; 112 113 /* ----------------------------------------------------------------------- */ 114 115 typedef struct MTPState MTPState; 116 typedef struct MTPControl MTPControl; 117 typedef struct MTPData MTPData; 118 typedef struct MTPObject MTPObject; 119 120 enum { 121 EP_DATA_IN = 1, 122 EP_DATA_OUT, 123 EP_EVENT, 124 }; 125 126 #ifdef CONFIG_INOTIFY1 127 typedef struct MTPMonEntry MTPMonEntry; 128 129 struct MTPMonEntry { 130 uint32_t event; 131 uint32_t handle; 132 133 QTAILQ_ENTRY(MTPMonEntry) next; 134 }; 135 #endif 136 137 struct MTPControl { 138 uint16_t code; 139 uint32_t trans; 140 int argc; 141 uint32_t argv[5]; 142 }; 143 144 struct MTPData { 145 uint16_t code; 146 uint32_t trans; 147 uint64_t offset; 148 uint64_t length; 149 uint32_t alloc; 150 uint8_t *data; 151 bool first; 152 int fd; 153 }; 154 155 struct MTPObject { 156 uint32_t handle; 157 uint16_t format; 158 char *name; 159 char *path; 160 struct stat stat; 161 #ifdef CONFIG_INOTIFY1 162 /* inotify watch cookie */ 163 int watchfd; 164 #endif 165 MTPObject *parent; 166 uint32_t nchildren; 167 QLIST_HEAD(, MTPObject) children; 168 QLIST_ENTRY(MTPObject) list; 169 bool have_children; 170 QTAILQ_ENTRY(MTPObject) next; 171 }; 172 173 struct MTPState { 174 USBDevice dev; 175 char *root; 176 char *desc; 177 uint32_t flags; 178 179 MTPData *data_in; 180 MTPData *data_out; 181 MTPControl *result; 182 uint32_t session; 183 uint32_t next_handle; 184 bool readonly; 185 186 QTAILQ_HEAD(, MTPObject) objects; 187 #ifdef CONFIG_INOTIFY1 188 /* inotify descriptor */ 189 int inotifyfd; 190 QTAILQ_HEAD(events, MTPMonEntry) events; 191 #endif 192 /* Responder is expecting a write operation */ 193 bool write_pending; 194 struct { 195 uint32_t parent_handle; 196 uint16_t format; 197 uint32_t size; 198 char *filename; 199 } dataset; 200 }; 201 202 /* 203 * ObjectInfo dataset received from initiator 204 * Fields we don't care about are ignored 205 */ 206 typedef struct { 207 uint32_t storage_id; /*unused*/ 208 uint16_t format; 209 uint16_t protection_status; /*unused*/ 210 uint32_t size; 211 uint16_t thumb_format; /*unused*/ 212 uint32_t thumb_comp_sz; /*unused*/ 213 uint32_t thumb_pix_width; /*unused*/ 214 uint32_t thumb_pix_height; /*unused*/ 215 uint32_t image_pix_width; /*unused*/ 216 uint32_t image_pix_height; /*unused*/ 217 uint32_t image_bit_depth; /*unused*/ 218 uint32_t parent; /*unused*/ 219 uint16_t assoc_type; 220 uint32_t assoc_desc; 221 uint32_t seq_no; /*unused*/ 222 uint8_t length; /*part of filename field*/ 223 uint16_t filename[0]; 224 char date_created[0]; /*unused*/ 225 char date_modified[0]; /*unused*/ 226 char keywords[0]; /*unused*/ 227 /* string and other data follows */ 228 } QEMU_PACKED ObjectInfo; 229 230 #define TYPE_USB_MTP "usb-mtp" 231 #define USB_MTP(obj) OBJECT_CHECK(MTPState, (obj), TYPE_USB_MTP) 232 233 #define QEMU_STORAGE_ID 0x00010001 234 235 #define MTP_FLAG_WRITABLE 0 236 237 #define FLAG_SET(_mtp, _flag) ((_mtp)->flags & (1 << (_flag))) 238 239 /* ----------------------------------------------------------------------- */ 240 241 #define MTP_MANUFACTURER "QEMU" 242 #define MTP_PRODUCT "QEMU filesharing" 243 244 enum { 245 STR_MANUFACTURER = 1, 246 STR_PRODUCT, 247 STR_SERIALNUMBER, 248 STR_MTP, 249 STR_CONFIG_FULL, 250 STR_CONFIG_HIGH, 251 STR_CONFIG_SUPER, 252 }; 253 254 static const USBDescStrings desc_strings = { 255 [STR_MANUFACTURER] = MTP_MANUFACTURER, 256 [STR_PRODUCT] = MTP_PRODUCT, 257 [STR_SERIALNUMBER] = "34617", 258 [STR_MTP] = "MTP", 259 [STR_CONFIG_FULL] = "Full speed config (usb 1.1)", 260 [STR_CONFIG_HIGH] = "High speed config (usb 2.0)", 261 [STR_CONFIG_SUPER] = "Super speed config (usb 3.0)", 262 }; 263 264 static const USBDescIface desc_iface_full = { 265 .bInterfaceNumber = 0, 266 .bNumEndpoints = 3, 267 .bInterfaceClass = USB_CLASS_STILL_IMAGE, 268 .bInterfaceSubClass = 0x01, 269 .bInterfaceProtocol = 0x01, 270 .iInterface = STR_MTP, 271 .eps = (USBDescEndpoint[]) { 272 { 273 .bEndpointAddress = USB_DIR_IN | EP_DATA_IN, 274 .bmAttributes = USB_ENDPOINT_XFER_BULK, 275 .wMaxPacketSize = 64, 276 },{ 277 .bEndpointAddress = USB_DIR_OUT | EP_DATA_OUT, 278 .bmAttributes = USB_ENDPOINT_XFER_BULK, 279 .wMaxPacketSize = 64, 280 },{ 281 .bEndpointAddress = USB_DIR_IN | EP_EVENT, 282 .bmAttributes = USB_ENDPOINT_XFER_INT, 283 .wMaxPacketSize = 64, 284 .bInterval = 0x0a, 285 }, 286 } 287 }; 288 289 static const USBDescDevice desc_device_full = { 290 .bcdUSB = 0x0200, 291 .bMaxPacketSize0 = 8, 292 .bNumConfigurations = 1, 293 .confs = (USBDescConfig[]) { 294 { 295 .bNumInterfaces = 1, 296 .bConfigurationValue = 1, 297 .iConfiguration = STR_CONFIG_FULL, 298 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP, 299 .bMaxPower = 2, 300 .nif = 1, 301 .ifs = &desc_iface_full, 302 }, 303 }, 304 }; 305 306 static const USBDescIface desc_iface_high = { 307 .bInterfaceNumber = 0, 308 .bNumEndpoints = 3, 309 .bInterfaceClass = USB_CLASS_STILL_IMAGE, 310 .bInterfaceSubClass = 0x01, 311 .bInterfaceProtocol = 0x01, 312 .iInterface = STR_MTP, 313 .eps = (USBDescEndpoint[]) { 314 { 315 .bEndpointAddress = USB_DIR_IN | EP_DATA_IN, 316 .bmAttributes = USB_ENDPOINT_XFER_BULK, 317 .wMaxPacketSize = 512, 318 },{ 319 .bEndpointAddress = USB_DIR_OUT | EP_DATA_OUT, 320 .bmAttributes = USB_ENDPOINT_XFER_BULK, 321 .wMaxPacketSize = 512, 322 },{ 323 .bEndpointAddress = USB_DIR_IN | EP_EVENT, 324 .bmAttributes = USB_ENDPOINT_XFER_INT, 325 .wMaxPacketSize = 64, 326 .bInterval = 0x0a, 327 }, 328 } 329 }; 330 331 static const USBDescDevice desc_device_high = { 332 .bcdUSB = 0x0200, 333 .bMaxPacketSize0 = 64, 334 .bNumConfigurations = 1, 335 .confs = (USBDescConfig[]) { 336 { 337 .bNumInterfaces = 1, 338 .bConfigurationValue = 1, 339 .iConfiguration = STR_CONFIG_HIGH, 340 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP, 341 .bMaxPower = 2, 342 .nif = 1, 343 .ifs = &desc_iface_high, 344 }, 345 }, 346 }; 347 348 static const USBDescMSOS desc_msos = { 349 .CompatibleID = "MTP", 350 .SelectiveSuspendEnabled = true, 351 }; 352 353 static const USBDesc desc = { 354 .id = { 355 .idVendor = 0x46f4, /* CRC16() of "QEMU" */ 356 .idProduct = 0x0004, 357 .bcdDevice = 0, 358 .iManufacturer = STR_MANUFACTURER, 359 .iProduct = STR_PRODUCT, 360 .iSerialNumber = STR_SERIALNUMBER, 361 }, 362 .full = &desc_device_full, 363 .high = &desc_device_high, 364 .str = desc_strings, 365 .msos = &desc_msos, 366 }; 367 368 /* ----------------------------------------------------------------------- */ 369 370 static MTPObject *usb_mtp_object_alloc(MTPState *s, uint32_t handle, 371 MTPObject *parent, char *name) 372 { 373 MTPObject *o = g_new0(MTPObject, 1); 374 375 if (name[0] == '.') { 376 goto ignore; 377 } 378 379 o->handle = handle; 380 o->parent = parent; 381 o->name = g_strdup(name); 382 if (parent == NULL) { 383 o->path = g_strdup(name); 384 } else { 385 o->path = g_strdup_printf("%s/%s", parent->path, name); 386 } 387 388 if (lstat(o->path, &o->stat) != 0) { 389 goto ignore; 390 } 391 if (S_ISREG(o->stat.st_mode)) { 392 o->format = FMT_UNDEFINED_OBJECT; 393 } else if (S_ISDIR(o->stat.st_mode)) { 394 o->format = FMT_ASSOCIATION; 395 } else { 396 goto ignore; 397 } 398 399 if (access(o->path, R_OK) != 0) { 400 goto ignore; 401 } 402 403 trace_usb_mtp_object_alloc(s->dev.addr, o->handle, o->path); 404 405 QTAILQ_INSERT_TAIL(&s->objects, o, next); 406 return o; 407 408 ignore: 409 g_free(o->name); 410 g_free(o->path); 411 g_free(o); 412 return NULL; 413 } 414 415 static void usb_mtp_object_free(MTPState *s, MTPObject *o) 416 { 417 MTPObject *iter; 418 419 if (!o) { 420 return; 421 } 422 423 trace_usb_mtp_object_free(s->dev.addr, o->handle, o->path); 424 425 QTAILQ_REMOVE(&s->objects, o, next); 426 if (o->parent) { 427 QLIST_REMOVE(o, list); 428 o->parent->nchildren--; 429 } 430 431 while (!QLIST_EMPTY(&o->children)) { 432 iter = QLIST_FIRST(&o->children); 433 usb_mtp_object_free(s, iter); 434 } 435 g_free(o->name); 436 g_free(o->path); 437 g_free(o); 438 } 439 440 static MTPObject *usb_mtp_object_lookup(MTPState *s, uint32_t handle) 441 { 442 MTPObject *o; 443 444 QTAILQ_FOREACH(o, &s->objects, next) { 445 if (o->handle == handle) { 446 return o; 447 } 448 } 449 return NULL; 450 } 451 452 static MTPObject *usb_mtp_add_child(MTPState *s, MTPObject *o, 453 char *name) 454 { 455 MTPObject *child = 456 usb_mtp_object_alloc(s, s->next_handle++, o, name); 457 458 if (child) { 459 trace_usb_mtp_add_child(s->dev.addr, child->handle, child->path); 460 QLIST_INSERT_HEAD(&o->children, child, list); 461 o->nchildren++; 462 463 if (child->format == FMT_ASSOCIATION) { 464 QLIST_INIT(&child->children); 465 } 466 } 467 468 return child; 469 } 470 471 static MTPObject *usb_mtp_object_lookup_name(MTPObject *parent, 472 char *name, int len) 473 { 474 MTPObject *iter; 475 476 QLIST_FOREACH(iter, &parent->children, list) { 477 if (strncmp(iter->name, name, len) == 0) { 478 return iter; 479 } 480 } 481 482 return NULL; 483 } 484 485 #ifdef CONFIG_INOTIFY1 486 static MTPObject *usb_mtp_object_lookup_wd(MTPState *s, int wd) 487 { 488 MTPObject *iter; 489 490 QTAILQ_FOREACH(iter, &s->objects, next) { 491 if (iter->watchfd == wd) { 492 return iter; 493 } 494 } 495 496 return NULL; 497 } 498 499 static void inotify_watchfn(void *arg) 500 { 501 MTPState *s = arg; 502 ssize_t bytes; 503 /* From the man page: atleast one event can be read */ 504 int pos; 505 char buf[sizeof(struct inotify_event) + NAME_MAX + 1]; 506 507 for (;;) { 508 bytes = read(s->inotifyfd, buf, sizeof(buf)); 509 pos = 0; 510 511 if (bytes <= 0) { 512 /* Better luck next time */ 513 return; 514 } 515 516 /* 517 * TODO: Ignore initiator initiated events. 518 * For now we are good because the store is RO 519 */ 520 while (bytes > 0) { 521 char *p = buf + pos; 522 struct inotify_event *event = (struct inotify_event *)p; 523 int watchfd = 0; 524 uint32_t mask = event->mask & (IN_CREATE | IN_DELETE | 525 IN_MODIFY | IN_IGNORED); 526 MTPObject *parent = usb_mtp_object_lookup_wd(s, event->wd); 527 MTPMonEntry *entry = NULL; 528 MTPObject *o; 529 530 pos = pos + sizeof(struct inotify_event) + event->len; 531 bytes = bytes - pos; 532 533 if (!parent) { 534 continue; 535 } 536 537 switch (mask) { 538 case IN_CREATE: 539 if (usb_mtp_object_lookup_name 540 (parent, event->name, event->len)) { 541 /* Duplicate create event */ 542 continue; 543 } 544 entry = g_new0(MTPMonEntry, 1); 545 entry->handle = s->next_handle; 546 entry->event = EVT_OBJ_ADDED; 547 o = usb_mtp_add_child(s, parent, event->name); 548 if (!o) { 549 g_free(entry); 550 continue; 551 } 552 o->watchfd = watchfd; 553 trace_usb_mtp_inotify_event(s->dev.addr, event->name, 554 event->mask, "Obj Added"); 555 break; 556 557 case IN_DELETE: 558 /* 559 * The kernel issues a IN_IGNORED event 560 * when a dir containing a watchpoint is 561 * deleted, so we don't have to delete the 562 * watchpoint 563 */ 564 o = usb_mtp_object_lookup_name(parent, event->name, event->len); 565 if (!o) { 566 continue; 567 } 568 entry = g_new0(MTPMonEntry, 1); 569 entry->handle = o->handle; 570 entry->event = EVT_OBJ_REMOVED; 571 trace_usb_mtp_inotify_event(s->dev.addr, o->path, 572 event->mask, "Obj Deleted"); 573 usb_mtp_object_free(s, o); 574 break; 575 576 case IN_MODIFY: 577 o = usb_mtp_object_lookup_name(parent, event->name, event->len); 578 if (!o) { 579 continue; 580 } 581 entry = g_new0(MTPMonEntry, 1); 582 entry->handle = o->handle; 583 entry->event = EVT_OBJ_INFO_CHANGED; 584 trace_usb_mtp_inotify_event(s->dev.addr, o->path, 585 event->mask, "Obj Modified"); 586 break; 587 588 case IN_IGNORED: 589 trace_usb_mtp_inotify_event(s->dev.addr, parent->path, 590 event->mask, "Obj parent dir ignored"); 591 break; 592 593 default: 594 fprintf(stderr, "usb-mtp: failed to parse inotify event\n"); 595 continue; 596 } 597 598 if (entry) { 599 QTAILQ_INSERT_HEAD(&s->events, entry, next); 600 } 601 } 602 } 603 } 604 605 static int usb_mtp_inotify_init(MTPState *s) 606 { 607 int fd; 608 609 fd = inotify_init1(IN_NONBLOCK); 610 if (fd == -1) { 611 return 1; 612 } 613 614 QTAILQ_INIT(&s->events); 615 s->inotifyfd = fd; 616 617 qemu_set_fd_handler(fd, inotify_watchfn, NULL, s); 618 619 return 0; 620 } 621 622 static void usb_mtp_inotify_cleanup(MTPState *s) 623 { 624 MTPMonEntry *e, *p; 625 626 if (!s->inotifyfd) { 627 return; 628 } 629 630 qemu_set_fd_handler(s->inotifyfd, NULL, NULL, s); 631 close(s->inotifyfd); 632 633 QTAILQ_FOREACH_SAFE(e, &s->events, next, p) { 634 QTAILQ_REMOVE(&s->events, e, next); 635 g_free(e); 636 } 637 } 638 639 static int usb_mtp_add_watch(int inotifyfd, char *path) 640 { 641 uint32_t mask = IN_CREATE | IN_DELETE | IN_MODIFY | 642 IN_ISDIR; 643 644 return inotify_add_watch(inotifyfd, path, mask); 645 } 646 #endif 647 648 static void usb_mtp_object_readdir(MTPState *s, MTPObject *o) 649 { 650 struct dirent *entry; 651 DIR *dir; 652 653 if (o->have_children) { 654 return; 655 } 656 o->have_children = true; 657 658 dir = opendir(o->path); 659 if (!dir) { 660 return; 661 } 662 #ifdef CONFIG_INOTIFY1 663 int watchfd = usb_mtp_add_watch(s->inotifyfd, o->path); 664 if (watchfd == -1) { 665 fprintf(stderr, "usb-mtp: failed to add watch for %s\n", o->path); 666 } else { 667 trace_usb_mtp_inotify_event(s->dev.addr, o->path, 668 0, "Watch Added"); 669 o->watchfd = watchfd; 670 } 671 #endif 672 while ((entry = readdir(dir)) != NULL) { 673 usb_mtp_add_child(s, o, entry->d_name); 674 } 675 closedir(dir); 676 } 677 678 /* ----------------------------------------------------------------------- */ 679 680 static MTPData *usb_mtp_data_alloc(MTPControl *c) 681 { 682 MTPData *data = g_new0(MTPData, 1); 683 684 data->code = c->code; 685 data->trans = c->trans; 686 data->fd = -1; 687 data->first = true; 688 return data; 689 } 690 691 static void usb_mtp_data_free(MTPData *data) 692 { 693 if (data == NULL) { 694 return; 695 } 696 if (data->fd != -1) { 697 close(data->fd); 698 } 699 g_free(data->data); 700 g_free(data); 701 } 702 703 static void usb_mtp_realloc(MTPData *data, uint32_t bytes) 704 { 705 if (data->length + bytes <= data->alloc) { 706 return; 707 } 708 data->alloc = (data->length + bytes + 0xff) & ~0xff; 709 data->data = g_realloc(data->data, data->alloc); 710 } 711 712 static void usb_mtp_add_u8(MTPData *data, uint8_t val) 713 { 714 usb_mtp_realloc(data, 1); 715 data->data[data->length++] = val; 716 } 717 718 static void usb_mtp_add_u16(MTPData *data, uint16_t val) 719 { 720 usb_mtp_realloc(data, 2); 721 data->data[data->length++] = (val >> 0) & 0xff; 722 data->data[data->length++] = (val >> 8) & 0xff; 723 } 724 725 static void usb_mtp_add_u32(MTPData *data, uint32_t val) 726 { 727 usb_mtp_realloc(data, 4); 728 data->data[data->length++] = (val >> 0) & 0xff; 729 data->data[data->length++] = (val >> 8) & 0xff; 730 data->data[data->length++] = (val >> 16) & 0xff; 731 data->data[data->length++] = (val >> 24) & 0xff; 732 } 733 734 static void usb_mtp_add_u64(MTPData *data, uint64_t val) 735 { 736 usb_mtp_realloc(data, 8); 737 data->data[data->length++] = (val >> 0) & 0xff; 738 data->data[data->length++] = (val >> 8) & 0xff; 739 data->data[data->length++] = (val >> 16) & 0xff; 740 data->data[data->length++] = (val >> 24) & 0xff; 741 data->data[data->length++] = (val >> 32) & 0xff; 742 data->data[data->length++] = (val >> 40) & 0xff; 743 data->data[data->length++] = (val >> 48) & 0xff; 744 data->data[data->length++] = (val >> 56) & 0xff; 745 } 746 747 static void usb_mtp_add_u16_array(MTPData *data, uint32_t len, 748 const uint16_t *vals) 749 { 750 int i; 751 752 usb_mtp_add_u32(data, len); 753 for (i = 0; i < len; i++) { 754 usb_mtp_add_u16(data, vals[i]); 755 } 756 } 757 758 static void usb_mtp_add_u32_array(MTPData *data, uint32_t len, 759 const uint32_t *vals) 760 { 761 int i; 762 763 usb_mtp_add_u32(data, len); 764 for (i = 0; i < len; i++) { 765 usb_mtp_add_u32(data, vals[i]); 766 } 767 } 768 769 static void usb_mtp_add_wstr(MTPData *data, const wchar_t *str) 770 { 771 uint32_t len = wcslen(str); 772 int i; 773 774 if (len > 0) { 775 len++; /* include terminating L'\0' */ 776 } 777 778 usb_mtp_add_u8(data, len); 779 for (i = 0; i < len; i++) { 780 usb_mtp_add_u16(data, str[i]); 781 } 782 } 783 784 static void usb_mtp_add_str(MTPData *data, const char *str) 785 { 786 uint32_t len = strlen(str)+1; 787 wchar_t *wstr = g_new(wchar_t, len); 788 size_t ret; 789 790 ret = mbstowcs(wstr, str, len); 791 if (ret == -1) { 792 usb_mtp_add_wstr(data, L"Oops"); 793 } else { 794 usb_mtp_add_wstr(data, wstr); 795 } 796 797 g_free(wstr); 798 } 799 800 static void usb_mtp_add_time(MTPData *data, time_t time) 801 { 802 char buf[16]; 803 struct tm tm; 804 805 gmtime_r(&time, &tm); 806 strftime(buf, sizeof(buf), "%Y%m%dT%H%M%S", &tm); 807 usb_mtp_add_str(data, buf); 808 } 809 810 /* ----------------------------------------------------------------------- */ 811 812 static void usb_mtp_queue_result(MTPState *s, uint16_t code, uint32_t trans, 813 int argc, uint32_t arg0, uint32_t arg1, 814 uint32_t arg2) 815 { 816 MTPControl *c = g_new0(MTPControl, 1); 817 818 c->code = code; 819 c->trans = trans; 820 c->argc = argc; 821 if (argc > 0) { 822 c->argv[0] = arg0; 823 } 824 if (argc > 1) { 825 c->argv[1] = arg1; 826 } 827 if (argc > 2) { 828 c->argv[2] = arg2; 829 } 830 831 assert(s->result == NULL); 832 s->result = c; 833 } 834 835 /* ----------------------------------------------------------------------- */ 836 837 static MTPData *usb_mtp_get_device_info(MTPState *s, MTPControl *c) 838 { 839 static const uint16_t ops[] = { 840 CMD_GET_DEVICE_INFO, 841 CMD_OPEN_SESSION, 842 CMD_CLOSE_SESSION, 843 CMD_GET_STORAGE_IDS, 844 CMD_GET_STORAGE_INFO, 845 CMD_GET_NUM_OBJECTS, 846 CMD_GET_OBJECT_HANDLES, 847 CMD_GET_OBJECT_INFO, 848 CMD_DELETE_OBJECT, 849 CMD_SEND_OBJECT_INFO, 850 CMD_SEND_OBJECT, 851 CMD_GET_OBJECT, 852 CMD_GET_PARTIAL_OBJECT, 853 CMD_GET_OBJECT_PROPS_SUPPORTED, 854 CMD_GET_OBJECT_PROP_DESC, 855 CMD_GET_OBJECT_PROP_VALUE, 856 }; 857 static const uint16_t fmt[] = { 858 FMT_UNDEFINED_OBJECT, 859 FMT_ASSOCIATION, 860 }; 861 MTPData *d = usb_mtp_data_alloc(c); 862 863 trace_usb_mtp_op_get_device_info(s->dev.addr); 864 865 usb_mtp_add_u16(d, 100); 866 usb_mtp_add_u32(d, 0x00000006); 867 usb_mtp_add_u16(d, 0x0064); 868 usb_mtp_add_wstr(d, L""); 869 usb_mtp_add_u16(d, 0x0000); 870 871 usb_mtp_add_u16_array(d, ARRAY_SIZE(ops), ops); 872 usb_mtp_add_u16_array(d, 0, NULL); 873 usb_mtp_add_u16_array(d, 0, NULL); 874 usb_mtp_add_u16_array(d, 0, NULL); 875 usb_mtp_add_u16_array(d, ARRAY_SIZE(fmt), fmt); 876 877 usb_mtp_add_wstr(d, L"" MTP_MANUFACTURER); 878 usb_mtp_add_wstr(d, L"" MTP_PRODUCT); 879 usb_mtp_add_wstr(d, L"0.1"); 880 usb_mtp_add_wstr(d, L"0123456789abcdef0123456789abcdef"); 881 882 return d; 883 } 884 885 static MTPData *usb_mtp_get_storage_ids(MTPState *s, MTPControl *c) 886 { 887 static const uint32_t ids[] = { 888 QEMU_STORAGE_ID, 889 }; 890 MTPData *d = usb_mtp_data_alloc(c); 891 892 trace_usb_mtp_op_get_storage_ids(s->dev.addr); 893 894 usb_mtp_add_u32_array(d, ARRAY_SIZE(ids), ids); 895 896 return d; 897 } 898 899 static MTPData *usb_mtp_get_storage_info(MTPState *s, MTPControl *c) 900 { 901 MTPData *d = usb_mtp_data_alloc(c); 902 struct statvfs buf; 903 int rc; 904 905 trace_usb_mtp_op_get_storage_info(s->dev.addr); 906 907 if (FLAG_SET(s, MTP_FLAG_WRITABLE)) { 908 usb_mtp_add_u16(d, 0x0003); 909 usb_mtp_add_u16(d, 0x0002); 910 usb_mtp_add_u16(d, 0x0000); 911 } else { 912 usb_mtp_add_u16(d, 0x0001); 913 usb_mtp_add_u16(d, 0x0002); 914 usb_mtp_add_u16(d, 0x0001); 915 } 916 917 rc = statvfs(s->root, &buf); 918 if (rc == 0) { 919 usb_mtp_add_u64(d, (uint64_t)buf.f_frsize * buf.f_blocks); 920 usb_mtp_add_u64(d, (uint64_t)buf.f_bavail * buf.f_blocks); 921 usb_mtp_add_u32(d, buf.f_ffree); 922 } else { 923 usb_mtp_add_u64(d, 0xffffffff); 924 usb_mtp_add_u64(d, 0xffffffff); 925 usb_mtp_add_u32(d, 0xffffffff); 926 } 927 928 usb_mtp_add_str(d, s->desc); 929 usb_mtp_add_wstr(d, L"123456789abcdef"); 930 return d; 931 } 932 933 static MTPData *usb_mtp_get_object_handles(MTPState *s, MTPControl *c, 934 MTPObject *o) 935 { 936 MTPData *d = usb_mtp_data_alloc(c); 937 uint32_t i = 0, handles[o->nchildren]; 938 MTPObject *iter; 939 940 trace_usb_mtp_op_get_object_handles(s->dev.addr, o->handle, o->path); 941 942 QLIST_FOREACH(iter, &o->children, list) { 943 handles[i++] = iter->handle; 944 } 945 assert(i == o->nchildren); 946 usb_mtp_add_u32_array(d, o->nchildren, handles); 947 948 return d; 949 } 950 951 static MTPData *usb_mtp_get_object_info(MTPState *s, MTPControl *c, 952 MTPObject *o) 953 { 954 MTPData *d = usb_mtp_data_alloc(c); 955 956 trace_usb_mtp_op_get_object_info(s->dev.addr, o->handle, o->path); 957 958 usb_mtp_add_u32(d, QEMU_STORAGE_ID); 959 usb_mtp_add_u16(d, o->format); 960 usb_mtp_add_u16(d, 0); 961 962 if (o->stat.st_size > 0xFFFFFFFF) { 963 usb_mtp_add_u32(d, 0xFFFFFFFF); 964 } else { 965 usb_mtp_add_u32(d, o->stat.st_size); 966 } 967 968 usb_mtp_add_u16(d, 0); 969 usb_mtp_add_u32(d, 0); 970 usb_mtp_add_u32(d, 0); 971 usb_mtp_add_u32(d, 0); 972 usb_mtp_add_u32(d, 0); 973 usb_mtp_add_u32(d, 0); 974 usb_mtp_add_u32(d, 0); 975 976 if (o->parent) { 977 usb_mtp_add_u32(d, o->parent->handle); 978 } else { 979 usb_mtp_add_u32(d, 0); 980 } 981 if (o->format == FMT_ASSOCIATION) { 982 usb_mtp_add_u16(d, 0x0001); 983 usb_mtp_add_u32(d, 0x00000001); 984 usb_mtp_add_u32(d, 0); 985 } else { 986 usb_mtp_add_u16(d, 0); 987 usb_mtp_add_u32(d, 0); 988 usb_mtp_add_u32(d, 0); 989 } 990 991 usb_mtp_add_str(d, o->name); 992 usb_mtp_add_time(d, o->stat.st_ctime); 993 usb_mtp_add_time(d, o->stat.st_mtime); 994 usb_mtp_add_wstr(d, L""); 995 996 return d; 997 } 998 999 static MTPData *usb_mtp_get_object(MTPState *s, MTPControl *c, 1000 MTPObject *o) 1001 { 1002 MTPData *d = usb_mtp_data_alloc(c); 1003 1004 trace_usb_mtp_op_get_object(s->dev.addr, o->handle, o->path); 1005 1006 d->fd = open(o->path, O_RDONLY); 1007 if (d->fd == -1) { 1008 usb_mtp_data_free(d); 1009 return NULL; 1010 } 1011 d->length = o->stat.st_size; 1012 d->alloc = 512; 1013 d->data = g_malloc(d->alloc); 1014 return d; 1015 } 1016 1017 static MTPData *usb_mtp_get_partial_object(MTPState *s, MTPControl *c, 1018 MTPObject *o) 1019 { 1020 MTPData *d = usb_mtp_data_alloc(c); 1021 off_t offset; 1022 1023 trace_usb_mtp_op_get_partial_object(s->dev.addr, o->handle, o->path, 1024 c->argv[1], c->argv[2]); 1025 1026 d->fd = open(o->path, O_RDONLY); 1027 if (d->fd == -1) { 1028 usb_mtp_data_free(d); 1029 return NULL; 1030 } 1031 1032 offset = c->argv[1]; 1033 if (offset > o->stat.st_size) { 1034 offset = o->stat.st_size; 1035 } 1036 if (lseek(d->fd, offset, SEEK_SET) < 0) { 1037 usb_mtp_data_free(d); 1038 return NULL; 1039 } 1040 1041 d->length = c->argv[2]; 1042 if (d->length > o->stat.st_size - offset) { 1043 d->length = o->stat.st_size - offset; 1044 } 1045 1046 return d; 1047 } 1048 1049 static MTPData *usb_mtp_get_object_props_supported(MTPState *s, MTPControl *c) 1050 { 1051 static const uint16_t props[] = { 1052 PROP_STORAGE_ID, 1053 PROP_OBJECT_FORMAT, 1054 PROP_OBJECT_COMPRESSED_SIZE, 1055 PROP_PARENT_OBJECT, 1056 PROP_PERSISTENT_UNIQUE_OBJECT_IDENTIFIER, 1057 PROP_NAME, 1058 }; 1059 MTPData *d = usb_mtp_data_alloc(c); 1060 usb_mtp_add_u16_array(d, ARRAY_SIZE(props), props); 1061 1062 return d; 1063 } 1064 1065 static MTPData *usb_mtp_get_object_prop_desc(MTPState *s, MTPControl *c) 1066 { 1067 MTPData *d = usb_mtp_data_alloc(c); 1068 switch (c->argv[0]) { 1069 case PROP_STORAGE_ID: 1070 usb_mtp_add_u16(d, PROP_STORAGE_ID); 1071 usb_mtp_add_u16(d, DATA_TYPE_UINT32); 1072 usb_mtp_add_u8(d, 0x00); 1073 usb_mtp_add_u32(d, 0x00000000); 1074 usb_mtp_add_u32(d, 0x00000000); 1075 usb_mtp_add_u8(d, 0x00); 1076 break; 1077 case PROP_OBJECT_FORMAT: 1078 usb_mtp_add_u16(d, PROP_OBJECT_FORMAT); 1079 usb_mtp_add_u16(d, DATA_TYPE_UINT16); 1080 usb_mtp_add_u8(d, 0x00); 1081 usb_mtp_add_u16(d, 0x0000); 1082 usb_mtp_add_u32(d, 0x00000000); 1083 usb_mtp_add_u8(d, 0x00); 1084 break; 1085 case PROP_OBJECT_COMPRESSED_SIZE: 1086 usb_mtp_add_u16(d, PROP_OBJECT_COMPRESSED_SIZE); 1087 usb_mtp_add_u16(d, DATA_TYPE_UINT64); 1088 usb_mtp_add_u8(d, 0x00); 1089 usb_mtp_add_u64(d, 0x0000000000000000); 1090 usb_mtp_add_u32(d, 0x00000000); 1091 usb_mtp_add_u8(d, 0x00); 1092 break; 1093 case PROP_PARENT_OBJECT: 1094 usb_mtp_add_u16(d, PROP_PARENT_OBJECT); 1095 usb_mtp_add_u16(d, DATA_TYPE_UINT32); 1096 usb_mtp_add_u8(d, 0x00); 1097 usb_mtp_add_u32(d, 0x00000000); 1098 usb_mtp_add_u32(d, 0x00000000); 1099 usb_mtp_add_u8(d, 0x00); 1100 break; 1101 case PROP_PERSISTENT_UNIQUE_OBJECT_IDENTIFIER: 1102 usb_mtp_add_u16(d, PROP_PERSISTENT_UNIQUE_OBJECT_IDENTIFIER); 1103 usb_mtp_add_u16(d, DATA_TYPE_UINT128); 1104 usb_mtp_add_u8(d, 0x00); 1105 usb_mtp_add_u64(d, 0x0000000000000000); 1106 usb_mtp_add_u64(d, 0x0000000000000000); 1107 usb_mtp_add_u32(d, 0x00000000); 1108 usb_mtp_add_u8(d, 0x00); 1109 break; 1110 case PROP_NAME: 1111 usb_mtp_add_u16(d, PROP_NAME); 1112 usb_mtp_add_u16(d, DATA_TYPE_STRING); 1113 usb_mtp_add_u8(d, 0x00); 1114 usb_mtp_add_u8(d, 0x00); 1115 usb_mtp_add_u32(d, 0x00000000); 1116 usb_mtp_add_u8(d, 0x00); 1117 break; 1118 default: 1119 usb_mtp_data_free(d); 1120 return NULL; 1121 } 1122 1123 return d; 1124 } 1125 1126 static MTPData *usb_mtp_get_object_prop_value(MTPState *s, MTPControl *c, 1127 MTPObject *o) 1128 { 1129 MTPData *d = usb_mtp_data_alloc(c); 1130 switch (c->argv[1]) { 1131 case PROP_STORAGE_ID: 1132 usb_mtp_add_u32(d, QEMU_STORAGE_ID); 1133 break; 1134 case PROP_OBJECT_FORMAT: 1135 usb_mtp_add_u16(d, o->format); 1136 break; 1137 case PROP_OBJECT_COMPRESSED_SIZE: 1138 usb_mtp_add_u64(d, o->stat.st_size); 1139 break; 1140 case PROP_PARENT_OBJECT: 1141 if (o->parent == NULL) { 1142 usb_mtp_add_u32(d, 0x00000000); 1143 } else { 1144 usb_mtp_add_u32(d, o->parent->handle); 1145 } 1146 break; 1147 case PROP_PERSISTENT_UNIQUE_OBJECT_IDENTIFIER: 1148 /* Should be persistent between sessions, 1149 * but using our objedt ID is "good enough" 1150 * for now */ 1151 usb_mtp_add_u64(d, 0x0000000000000000); 1152 usb_mtp_add_u64(d, o->handle); 1153 break; 1154 case PROP_NAME: 1155 usb_mtp_add_str(d, o->name); 1156 break; 1157 default: 1158 usb_mtp_data_free(d); 1159 return NULL; 1160 } 1161 1162 return d; 1163 } 1164 1165 /* Return correct return code for a delete event */ 1166 enum { 1167 ALL_DELETE, 1168 PARTIAL_DELETE, 1169 READ_ONLY, 1170 }; 1171 1172 /* Assumes that children, if any, have been already freed */ 1173 static void usb_mtp_object_free_one(MTPState *s, MTPObject *o) 1174 { 1175 #ifndef CONFIG_INOTIFY1 1176 assert(o->nchildren == 0); 1177 QTAILQ_REMOVE(&s->objects, o, next); 1178 g_free(o->name); 1179 g_free(o->path); 1180 g_free(o); 1181 #endif 1182 } 1183 1184 static int usb_mtp_deletefn(MTPState *s, MTPObject *o, uint32_t trans) 1185 { 1186 MTPObject *iter, *iter2; 1187 bool partial_delete = false; 1188 bool success = false; 1189 1190 /* 1191 * TODO: Add support for Protection Status 1192 */ 1193 1194 QLIST_FOREACH(iter, &o->children, list) { 1195 if (iter->format == FMT_ASSOCIATION) { 1196 QLIST_FOREACH(iter2, &iter->children, list) { 1197 usb_mtp_deletefn(s, iter2, trans); 1198 } 1199 } 1200 } 1201 1202 if (o->format == FMT_UNDEFINED_OBJECT) { 1203 if (remove(o->path)) { 1204 partial_delete = true; 1205 } else { 1206 usb_mtp_object_free_one(s, o); 1207 success = true; 1208 } 1209 } 1210 1211 if (o->format == FMT_ASSOCIATION) { 1212 if (rmdir(o->path)) { 1213 partial_delete = true; 1214 } else { 1215 usb_mtp_object_free_one(s, o); 1216 success = true; 1217 } 1218 } 1219 1220 if (success && partial_delete) { 1221 return PARTIAL_DELETE; 1222 } 1223 if (!success && partial_delete) { 1224 return READ_ONLY; 1225 } 1226 return ALL_DELETE; 1227 } 1228 1229 static void usb_mtp_object_delete(MTPState *s, uint32_t handle, 1230 uint32_t format_code, uint32_t trans) 1231 { 1232 MTPObject *o; 1233 int ret; 1234 1235 /* Return error if store is read-only */ 1236 if (!FLAG_SET(s, MTP_FLAG_WRITABLE)) { 1237 usb_mtp_queue_result(s, RES_STORE_READ_ONLY, 1238 trans, 0, 0, 0, 0); 1239 return; 1240 } 1241 1242 if (format_code != 0) { 1243 usb_mtp_queue_result(s, RES_SPEC_BY_FORMAT_UNSUPPORTED, 1244 trans, 0, 0, 0, 0); 1245 return; 1246 } 1247 1248 if (handle == 0xFFFFFFF) { 1249 o = QTAILQ_FIRST(&s->objects); 1250 } else { 1251 o = usb_mtp_object_lookup(s, handle); 1252 } 1253 if (o == NULL) { 1254 usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, 1255 trans, 0, 0, 0, 0); 1256 return; 1257 } 1258 1259 ret = usb_mtp_deletefn(s, o, trans); 1260 if (ret == PARTIAL_DELETE) { 1261 usb_mtp_queue_result(s, RES_PARTIAL_DELETE, 1262 trans, 0, 0, 0, 0); 1263 return; 1264 } else if (ret == READ_ONLY) { 1265 usb_mtp_queue_result(s, RES_STORE_READ_ONLY, trans, 1266 0, 0, 0, 0); 1267 return; 1268 } else { 1269 usb_mtp_queue_result(s, RES_OK, trans, 1270 0, 0, 0, 0); 1271 return; 1272 } 1273 } 1274 1275 static void usb_mtp_command(MTPState *s, MTPControl *c) 1276 { 1277 MTPData *data_in = NULL; 1278 MTPObject *o = NULL; 1279 uint32_t nres = 0, res0 = 0; 1280 1281 /* sanity checks */ 1282 if (c->code >= CMD_CLOSE_SESSION && s->session == 0) { 1283 usb_mtp_queue_result(s, RES_SESSION_NOT_OPEN, 1284 c->trans, 0, 0, 0, 0); 1285 return; 1286 } 1287 1288 /* process commands */ 1289 switch (c->code) { 1290 case CMD_GET_DEVICE_INFO: 1291 data_in = usb_mtp_get_device_info(s, c); 1292 break; 1293 case CMD_OPEN_SESSION: 1294 if (s->session) { 1295 usb_mtp_queue_result(s, RES_SESSION_ALREADY_OPEN, 1296 c->trans, 1, s->session, 0, 0); 1297 return; 1298 } 1299 if (c->argv[0] == 0) { 1300 usb_mtp_queue_result(s, RES_INVALID_PARAMETER, 1301 c->trans, 0, 0, 0, 0); 1302 return; 1303 } 1304 trace_usb_mtp_op_open_session(s->dev.addr); 1305 s->session = c->argv[0]; 1306 usb_mtp_object_alloc(s, s->next_handle++, NULL, s->root); 1307 #ifdef CONFIG_INOTIFY1 1308 if (usb_mtp_inotify_init(s)) { 1309 fprintf(stderr, "usb-mtp: file monitoring init failed\n"); 1310 } 1311 #endif 1312 break; 1313 case CMD_CLOSE_SESSION: 1314 trace_usb_mtp_op_close_session(s->dev.addr); 1315 s->session = 0; 1316 s->next_handle = 0; 1317 #ifdef CONFIG_INOTIFY1 1318 usb_mtp_inotify_cleanup(s); 1319 #endif 1320 usb_mtp_object_free(s, QTAILQ_FIRST(&s->objects)); 1321 assert(QTAILQ_EMPTY(&s->objects)); 1322 break; 1323 case CMD_GET_STORAGE_IDS: 1324 data_in = usb_mtp_get_storage_ids(s, c); 1325 break; 1326 case CMD_GET_STORAGE_INFO: 1327 if (c->argv[0] != QEMU_STORAGE_ID && 1328 c->argv[0] != 0xffffffff) { 1329 usb_mtp_queue_result(s, RES_INVALID_STORAGE_ID, 1330 c->trans, 0, 0, 0, 0); 1331 return; 1332 } 1333 data_in = usb_mtp_get_storage_info(s, c); 1334 break; 1335 case CMD_GET_NUM_OBJECTS: 1336 case CMD_GET_OBJECT_HANDLES: 1337 if (c->argv[0] != QEMU_STORAGE_ID && 1338 c->argv[0] != 0xffffffff) { 1339 usb_mtp_queue_result(s, RES_INVALID_STORAGE_ID, 1340 c->trans, 0, 0, 0, 0); 1341 return; 1342 } 1343 if (c->argv[1] != 0x00000000) { 1344 usb_mtp_queue_result(s, RES_SPEC_BY_FORMAT_UNSUPPORTED, 1345 c->trans, 0, 0, 0, 0); 1346 return; 1347 } 1348 if (c->argv[2] == 0x00000000 || 1349 c->argv[2] == 0xffffffff) { 1350 o = QTAILQ_FIRST(&s->objects); 1351 } else { 1352 o = usb_mtp_object_lookup(s, c->argv[2]); 1353 } 1354 if (o == NULL) { 1355 usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, 1356 c->trans, 0, 0, 0, 0); 1357 return; 1358 } 1359 if (o->format != FMT_ASSOCIATION) { 1360 usb_mtp_queue_result(s, RES_INVALID_PARENT_OBJECT, 1361 c->trans, 0, 0, 0, 0); 1362 return; 1363 } 1364 usb_mtp_object_readdir(s, o); 1365 if (c->code == CMD_GET_NUM_OBJECTS) { 1366 trace_usb_mtp_op_get_num_objects(s->dev.addr, o->handle, o->path); 1367 nres = 1; 1368 res0 = o->nchildren; 1369 } else { 1370 data_in = usb_mtp_get_object_handles(s, c, o); 1371 } 1372 break; 1373 case CMD_GET_OBJECT_INFO: 1374 o = usb_mtp_object_lookup(s, c->argv[0]); 1375 if (o == NULL) { 1376 usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, 1377 c->trans, 0, 0, 0, 0); 1378 return; 1379 } 1380 data_in = usb_mtp_get_object_info(s, c, o); 1381 break; 1382 case CMD_GET_OBJECT: 1383 o = usb_mtp_object_lookup(s, c->argv[0]); 1384 if (o == NULL) { 1385 usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, 1386 c->trans, 0, 0, 0, 0); 1387 return; 1388 } 1389 if (o->format == FMT_ASSOCIATION) { 1390 usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, 1391 c->trans, 0, 0, 0, 0); 1392 return; 1393 } 1394 data_in = usb_mtp_get_object(s, c, o); 1395 if (data_in == NULL) { 1396 usb_mtp_queue_result(s, RES_GENERAL_ERROR, 1397 c->trans, 0, 0, 0, 0); 1398 return; 1399 } 1400 break; 1401 case CMD_DELETE_OBJECT: 1402 usb_mtp_object_delete(s, c->argv[0], c->argv[1], c->trans); 1403 return; 1404 case CMD_GET_PARTIAL_OBJECT: 1405 o = usb_mtp_object_lookup(s, c->argv[0]); 1406 if (o == NULL) { 1407 usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, 1408 c->trans, 0, 0, 0, 0); 1409 return; 1410 } 1411 if (o->format == FMT_ASSOCIATION) { 1412 usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, 1413 c->trans, 0, 0, 0, 0); 1414 return; 1415 } 1416 data_in = usb_mtp_get_partial_object(s, c, o); 1417 if (data_in == NULL) { 1418 usb_mtp_queue_result(s, RES_GENERAL_ERROR, 1419 c->trans, 0, 0, 0, 0); 1420 return; 1421 } 1422 nres = 1; 1423 res0 = data_in->length; 1424 break; 1425 case CMD_SEND_OBJECT_INFO: 1426 /* Return error if store is read-only */ 1427 if (!FLAG_SET(s, MTP_FLAG_WRITABLE)) { 1428 usb_mtp_queue_result(s, RES_STORE_READ_ONLY, 1429 c->trans, 0, 0, 0, 0); 1430 } else if (c->argv[0] && (c->argv[0] != QEMU_STORAGE_ID)) { 1431 /* First parameter points to storage id or is 0 */ 1432 usb_mtp_queue_result(s, RES_STORE_NOT_AVAILABLE, c->trans, 1433 0, 0, 0, 0); 1434 } else if (c->argv[1] && !c->argv[0]) { 1435 /* If second parameter is specified, first must also be specified */ 1436 usb_mtp_queue_result(s, RES_DESTINATION_UNSUPPORTED, c->trans, 1437 0, 0, 0, 0); 1438 } else { 1439 uint32_t handle = c->argv[1]; 1440 if (handle == 0xFFFFFFFF || handle == 0) { 1441 /* root object */ 1442 o = QTAILQ_FIRST(&s->objects); 1443 } else { 1444 o = usb_mtp_object_lookup(s, handle); 1445 } 1446 if (o == NULL) { 1447 usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, c->trans, 1448 0, 0, 0, 0); 1449 } else if (o->format != FMT_ASSOCIATION) { 1450 usb_mtp_queue_result(s, RES_INVALID_PARENT_OBJECT, c->trans, 1451 0, 0, 0, 0); 1452 } 1453 } 1454 if (o) { 1455 s->dataset.parent_handle = o->handle; 1456 } 1457 s->data_out = usb_mtp_data_alloc(c); 1458 return; 1459 case CMD_SEND_OBJECT: 1460 if (!FLAG_SET(s, MTP_FLAG_WRITABLE)) { 1461 usb_mtp_queue_result(s, RES_STORE_READ_ONLY, 1462 c->trans, 0, 0, 0, 0); 1463 return; 1464 } 1465 if (!s->write_pending) { 1466 usb_mtp_queue_result(s, RES_INVALID_OBJECTINFO, 1467 c->trans, 0, 0, 0, 0); 1468 return; 1469 } 1470 s->data_out = usb_mtp_data_alloc(c); 1471 return; 1472 case CMD_GET_OBJECT_PROPS_SUPPORTED: 1473 if (c->argv[0] != FMT_UNDEFINED_OBJECT && 1474 c->argv[0] != FMT_ASSOCIATION) { 1475 usb_mtp_queue_result(s, RES_INVALID_OBJECT_FORMAT_CODE, 1476 c->trans, 0, 0, 0, 0); 1477 return; 1478 } 1479 data_in = usb_mtp_get_object_props_supported(s, c); 1480 break; 1481 case CMD_GET_OBJECT_PROP_DESC: 1482 if (c->argv[1] != FMT_UNDEFINED_OBJECT && 1483 c->argv[1] != FMT_ASSOCIATION) { 1484 usb_mtp_queue_result(s, RES_INVALID_OBJECT_FORMAT_CODE, 1485 c->trans, 0, 0, 0, 0); 1486 return; 1487 } 1488 data_in = usb_mtp_get_object_prop_desc(s, c); 1489 if (data_in == NULL) { 1490 usb_mtp_queue_result(s, RES_INVALID_OBJECT_PROP_CODE, 1491 c->trans, 0, 0, 0, 0); 1492 return; 1493 } 1494 break; 1495 case CMD_GET_OBJECT_PROP_VALUE: 1496 o = usb_mtp_object_lookup(s, c->argv[0]); 1497 if (o == NULL) { 1498 usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, 1499 c->trans, 0, 0, 0, 0); 1500 return; 1501 } 1502 data_in = usb_mtp_get_object_prop_value(s, c, o); 1503 if (data_in == NULL) { 1504 usb_mtp_queue_result(s, RES_INVALID_OBJECT_PROP_CODE, 1505 c->trans, 0, 0, 0, 0); 1506 return; 1507 } 1508 break; 1509 default: 1510 trace_usb_mtp_op_unknown(s->dev.addr, c->code); 1511 usb_mtp_queue_result(s, RES_OPERATION_NOT_SUPPORTED, 1512 c->trans, 0, 0, 0, 0); 1513 return; 1514 } 1515 1516 /* return results on success */ 1517 if (data_in) { 1518 assert(s->data_in == NULL); 1519 s->data_in = data_in; 1520 } 1521 usb_mtp_queue_result(s, RES_OK, c->trans, nres, res0, 0, 0); 1522 } 1523 1524 /* ----------------------------------------------------------------------- */ 1525 1526 static void usb_mtp_handle_reset(USBDevice *dev) 1527 { 1528 MTPState *s = USB_MTP(dev); 1529 1530 trace_usb_mtp_reset(s->dev.addr); 1531 1532 #ifdef CONFIG_INOTIFY1 1533 usb_mtp_inotify_cleanup(s); 1534 #endif 1535 usb_mtp_object_free(s, QTAILQ_FIRST(&s->objects)); 1536 s->session = 0; 1537 usb_mtp_data_free(s->data_in); 1538 s->data_in = NULL; 1539 usb_mtp_data_free(s->data_out); 1540 s->data_out = NULL; 1541 g_free(s->result); 1542 s->result = NULL; 1543 } 1544 1545 static void usb_mtp_handle_control(USBDevice *dev, USBPacket *p, 1546 int request, int value, int index, 1547 int length, uint8_t *data) 1548 { 1549 int ret; 1550 1551 ret = usb_desc_handle_control(dev, p, request, value, index, length, data); 1552 if (ret >= 0) { 1553 return; 1554 } 1555 1556 trace_usb_mtp_stall(dev->addr, "unknown control request"); 1557 p->status = USB_RET_STALL; 1558 } 1559 1560 static void usb_mtp_cancel_packet(USBDevice *dev, USBPacket *p) 1561 { 1562 /* we don't use async packets, so this should never be called */ 1563 fprintf(stderr, "%s\n", __func__); 1564 } 1565 1566 static void utf16_to_str(uint8_t len, uint16_t *arr, char *name) 1567 { 1568 int count; 1569 wchar_t *wstr = g_new0(wchar_t, len); 1570 1571 for (count = 0; count < len; count++) { 1572 wstr[count] = (wchar_t)arr[count]; 1573 } 1574 1575 wcstombs(name, wstr, len); 1576 g_free(wstr); 1577 } 1578 1579 static void usb_mtp_write_data(MTPState *s) 1580 { 1581 MTPData *d = s->data_out; 1582 MTPObject *parent = 1583 usb_mtp_object_lookup(s, s->dataset.parent_handle); 1584 char *path = NULL; 1585 int rc = -1; 1586 mode_t mask = 0644; 1587 1588 assert(d != NULL); 1589 1590 if (parent == NULL || !s->write_pending) { 1591 usb_mtp_queue_result(s, RES_INVALID_OBJECTINFO, d->trans, 1592 0, 0, 0, 0); 1593 return; 1594 } 1595 1596 if (s->dataset.filename) { 1597 path = g_strdup_printf("%s/%s", parent->path, s->dataset.filename); 1598 if (s->dataset.format == FMT_ASSOCIATION) { 1599 d->fd = mkdir(path, mask); 1600 goto free; 1601 } 1602 if (s->dataset.size < d->length) { 1603 usb_mtp_queue_result(s, RES_STORE_FULL, d->trans, 1604 0, 0, 0, 0); 1605 goto done; 1606 } 1607 d->fd = open(path, O_CREAT | O_WRONLY, mask); 1608 if (d->fd == -1) { 1609 usb_mtp_queue_result(s, RES_STORE_FULL, d->trans, 1610 0, 0, 0, 0); 1611 goto done; 1612 } 1613 1614 /* 1615 * Return success if initiator sent 0 sized data 1616 */ 1617 if (!s->dataset.size) { 1618 goto success; 1619 } 1620 1621 rc = write(d->fd, d->data, s->dataset.size); 1622 if (rc == -1) { 1623 usb_mtp_queue_result(s, RES_STORE_FULL, d->trans, 1624 0, 0, 0, 0); 1625 goto done; 1626 } 1627 if (rc != s->dataset.size) { 1628 usb_mtp_queue_result(s, RES_INCOMPLETE_TRANSFER, d->trans, 1629 0, 0, 0, 0); 1630 goto done; 1631 } 1632 } 1633 1634 success: 1635 usb_mtp_queue_result(s, RES_OK, d->trans, 1636 0, 0, 0, 0); 1637 1638 done: 1639 /* 1640 * The write dataset is kept around and freed only 1641 * on success or if another write request comes in 1642 */ 1643 if (d->fd != -1) { 1644 close(d->fd); 1645 } 1646 free: 1647 g_free(s->dataset.filename); 1648 g_free(path); 1649 s->write_pending = false; 1650 } 1651 1652 static void usb_mtp_write_metadata(MTPState *s) 1653 { 1654 MTPData *d = s->data_out; 1655 ObjectInfo *dataset = (ObjectInfo *)d->data; 1656 char *filename = g_new0(char, dataset->length); 1657 MTPObject *o; 1658 MTPObject *p = usb_mtp_object_lookup(s, s->dataset.parent_handle); 1659 uint32_t next_handle = s->next_handle; 1660 1661 assert(!s->write_pending); 1662 assert(p != NULL); 1663 1664 utf16_to_str(dataset->length, dataset->filename, filename); 1665 1666 o = usb_mtp_object_lookup_name(p, filename, dataset->length); 1667 if (o != NULL) { 1668 next_handle = o->handle; 1669 } 1670 1671 s->dataset.filename = filename; 1672 s->dataset.format = dataset->format; 1673 s->dataset.size = dataset->size; 1674 s->dataset.filename = filename; 1675 s->write_pending = true; 1676 1677 if (s->dataset.format == FMT_ASSOCIATION) { 1678 usb_mtp_write_data(s); 1679 /* next_handle will be allocated to the newly created dir */ 1680 if (d->fd == -1) { 1681 usb_mtp_queue_result(s, RES_STORE_FULL, d->trans, 1682 0, 0, 0, 0); 1683 return; 1684 } 1685 d->fd = -1; 1686 } 1687 1688 usb_mtp_queue_result(s, RES_OK, d->trans, 3, QEMU_STORAGE_ID, 1689 s->dataset.parent_handle, next_handle); 1690 } 1691 1692 static void usb_mtp_get_data(MTPState *s, mtp_container *container, 1693 USBPacket *p) 1694 { 1695 MTPData *d = s->data_out; 1696 uint64_t dlen; 1697 uint32_t data_len = p->iov.size; 1698 1699 if (d->first) { 1700 /* Total length of incoming data */ 1701 d->length = cpu_to_le32(container->length) - sizeof(mtp_container); 1702 /* Length of data in this packet */ 1703 data_len -= sizeof(mtp_container); 1704 usb_mtp_realloc(d, d->length); 1705 d->offset = 0; 1706 d->first = false; 1707 } 1708 1709 if (d->length - d->offset > data_len) { 1710 dlen = data_len; 1711 } else { 1712 dlen = d->length - d->offset; 1713 } 1714 1715 switch (d->code) { 1716 case CMD_SEND_OBJECT_INFO: 1717 usb_packet_copy(p, d->data + d->offset, dlen); 1718 d->offset += dlen; 1719 if (d->offset == d->length) { 1720 /* The operation might have already failed */ 1721 if (!s->result) { 1722 usb_mtp_write_metadata(s); 1723 } 1724 usb_mtp_data_free(s->data_out); 1725 s->data_out = NULL; 1726 return; 1727 } 1728 break; 1729 case CMD_SEND_OBJECT: 1730 usb_packet_copy(p, d->data + d->offset, dlen); 1731 d->offset += dlen; 1732 if (d->offset == d->length) { 1733 usb_mtp_write_data(s); 1734 usb_mtp_data_free(s->data_out); 1735 s->data_out = NULL; 1736 return; 1737 } 1738 break; 1739 default: 1740 p->status = USB_RET_STALL; 1741 return; 1742 } 1743 } 1744 1745 static void usb_mtp_handle_data(USBDevice *dev, USBPacket *p) 1746 { 1747 MTPState *s = USB_MTP(dev); 1748 MTPControl cmd; 1749 mtp_container container; 1750 uint32_t params[5]; 1751 uint16_t container_type; 1752 int i, rc; 1753 1754 switch (p->ep->nr) { 1755 case EP_DATA_IN: 1756 if (s->data_out != NULL) { 1757 /* guest bug */ 1758 trace_usb_mtp_stall(s->dev.addr, "awaiting data-out"); 1759 p->status = USB_RET_STALL; 1760 return; 1761 } 1762 if (p->iov.size < sizeof(container)) { 1763 trace_usb_mtp_stall(s->dev.addr, "packet too small"); 1764 p->status = USB_RET_STALL; 1765 return; 1766 } 1767 if (s->data_in != NULL) { 1768 MTPData *d = s->data_in; 1769 uint64_t dlen = d->length - d->offset; 1770 if (d->first) { 1771 trace_usb_mtp_data_in(s->dev.addr, d->trans, d->length); 1772 if (d->length + sizeof(container) > 0xFFFFFFFF) { 1773 container.length = cpu_to_le32(0xFFFFFFFF); 1774 } else { 1775 container.length = 1776 cpu_to_le32(d->length + sizeof(container)); 1777 } 1778 container.type = cpu_to_le16(TYPE_DATA); 1779 container.code = cpu_to_le16(d->code); 1780 container.trans = cpu_to_le32(d->trans); 1781 usb_packet_copy(p, &container, sizeof(container)); 1782 d->first = false; 1783 if (dlen > p->iov.size - sizeof(container)) { 1784 dlen = p->iov.size - sizeof(container); 1785 } 1786 } else { 1787 if (dlen > p->iov.size) { 1788 dlen = p->iov.size; 1789 } 1790 } 1791 if (d->fd == -1) { 1792 usb_packet_copy(p, d->data + d->offset, dlen); 1793 } else { 1794 if (d->alloc < p->iov.size) { 1795 d->alloc = p->iov.size; 1796 d->data = g_realloc(d->data, d->alloc); 1797 } 1798 rc = read(d->fd, d->data, dlen); 1799 if (rc != dlen) { 1800 memset(d->data, 0, dlen); 1801 s->result->code = RES_INCOMPLETE_TRANSFER; 1802 } 1803 usb_packet_copy(p, d->data, dlen); 1804 } 1805 d->offset += dlen; 1806 if (d->offset == d->length) { 1807 usb_mtp_data_free(s->data_in); 1808 s->data_in = NULL; 1809 } 1810 } else if (s->result != NULL) { 1811 MTPControl *r = s->result; 1812 int length = sizeof(container) + r->argc * sizeof(uint32_t); 1813 if (r->code == RES_OK) { 1814 trace_usb_mtp_success(s->dev.addr, r->trans, 1815 (r->argc > 0) ? r->argv[0] : 0, 1816 (r->argc > 1) ? r->argv[1] : 0); 1817 } else { 1818 trace_usb_mtp_error(s->dev.addr, r->code, r->trans, 1819 (r->argc > 0) ? r->argv[0] : 0, 1820 (r->argc > 1) ? r->argv[1] : 0); 1821 } 1822 container.length = cpu_to_le32(length); 1823 container.type = cpu_to_le16(TYPE_RESPONSE); 1824 container.code = cpu_to_le16(r->code); 1825 container.trans = cpu_to_le32(r->trans); 1826 for (i = 0; i < r->argc; i++) { 1827 params[i] = cpu_to_le32(r->argv[i]); 1828 } 1829 usb_packet_copy(p, &container, sizeof(container)); 1830 usb_packet_copy(p, ¶ms, length - sizeof(container)); 1831 g_free(s->result); 1832 s->result = NULL; 1833 } 1834 break; 1835 case EP_DATA_OUT: 1836 if (p->iov.size < sizeof(container)) { 1837 trace_usb_mtp_stall(s->dev.addr, "packet too small"); 1838 p->status = USB_RET_STALL; 1839 return; 1840 } 1841 if ((s->data_out != NULL) && !s->data_out->first) { 1842 container_type = TYPE_DATA; 1843 } else { 1844 usb_packet_copy(p, &container, sizeof(container)); 1845 container_type = le16_to_cpu(container.type); 1846 } 1847 switch (container_type) { 1848 case TYPE_COMMAND: 1849 if (s->data_in || s->data_out || s->result) { 1850 trace_usb_mtp_stall(s->dev.addr, "transaction inflight"); 1851 p->status = USB_RET_STALL; 1852 return; 1853 } 1854 cmd.code = le16_to_cpu(container.code); 1855 cmd.argc = (le32_to_cpu(container.length) - sizeof(container)) 1856 / sizeof(uint32_t); 1857 cmd.trans = le32_to_cpu(container.trans); 1858 if (cmd.argc > ARRAY_SIZE(cmd.argv)) { 1859 cmd.argc = ARRAY_SIZE(cmd.argv); 1860 } 1861 if (p->iov.size < sizeof(container) + cmd.argc * sizeof(uint32_t)) { 1862 trace_usb_mtp_stall(s->dev.addr, "packet too small"); 1863 p->status = USB_RET_STALL; 1864 return; 1865 } 1866 usb_packet_copy(p, ¶ms, cmd.argc * sizeof(uint32_t)); 1867 for (i = 0; i < cmd.argc; i++) { 1868 cmd.argv[i] = le32_to_cpu(params[i]); 1869 } 1870 trace_usb_mtp_command(s->dev.addr, cmd.code, cmd.trans, 1871 (cmd.argc > 0) ? cmd.argv[0] : 0, 1872 (cmd.argc > 1) ? cmd.argv[1] : 0, 1873 (cmd.argc > 2) ? cmd.argv[2] : 0, 1874 (cmd.argc > 3) ? cmd.argv[3] : 0, 1875 (cmd.argc > 4) ? cmd.argv[4] : 0); 1876 usb_mtp_command(s, &cmd); 1877 break; 1878 case TYPE_DATA: 1879 /* One of the previous transfers has already errored but the 1880 * responder is still sending data associated with it 1881 */ 1882 if (s->result != NULL) { 1883 return; 1884 } 1885 usb_mtp_get_data(s, &container, p); 1886 break; 1887 default: 1888 /* not needed as long as the mtp device is read-only */ 1889 p->status = USB_RET_STALL; 1890 return; 1891 } 1892 break; 1893 case EP_EVENT: 1894 #ifdef CONFIG_INOTIFY1 1895 if (!QTAILQ_EMPTY(&s->events)) { 1896 struct MTPMonEntry *e = QTAILQ_LAST(&s->events, events); 1897 uint32_t handle; 1898 int len = sizeof(container) + sizeof(uint32_t); 1899 1900 if (p->iov.size < len) { 1901 trace_usb_mtp_stall(s->dev.addr, 1902 "packet too small to send event"); 1903 p->status = USB_RET_STALL; 1904 return; 1905 } 1906 1907 QTAILQ_REMOVE(&s->events, e, next); 1908 container.length = cpu_to_le32(len); 1909 container.type = cpu_to_le32(TYPE_EVENT); 1910 container.code = cpu_to_le16(e->event); 1911 container.trans = 0; /* no trans specific events */ 1912 handle = cpu_to_le32(e->handle); 1913 usb_packet_copy(p, &container, sizeof(container)); 1914 usb_packet_copy(p, &handle, sizeof(uint32_t)); 1915 g_free(e); 1916 return; 1917 } 1918 #endif 1919 p->status = USB_RET_NAK; 1920 return; 1921 default: 1922 trace_usb_mtp_stall(s->dev.addr, "invalid endpoint"); 1923 p->status = USB_RET_STALL; 1924 return; 1925 } 1926 1927 if (p->actual_length == 0) { 1928 trace_usb_mtp_nak(s->dev.addr, p->ep->nr); 1929 p->status = USB_RET_NAK; 1930 return; 1931 } else { 1932 trace_usb_mtp_xfer(s->dev.addr, p->ep->nr, p->actual_length, 1933 p->iov.size); 1934 return; 1935 } 1936 } 1937 1938 static void usb_mtp_realize(USBDevice *dev, Error **errp) 1939 { 1940 MTPState *s = USB_MTP(dev); 1941 1942 usb_desc_create_serial(dev); 1943 usb_desc_init(dev); 1944 QTAILQ_INIT(&s->objects); 1945 if (s->desc == NULL) { 1946 if (s->root == NULL) { 1947 error_setg(errp, "usb-mtp: x-root property must be configured"); 1948 return; 1949 } 1950 s->desc = strrchr(s->root, '/'); 1951 if (s->desc && s->desc[0]) { 1952 s->desc = g_strdup(s->desc + 1); 1953 } else { 1954 s->desc = g_strdup("none"); 1955 } 1956 } 1957 /* Mark store as RW */ 1958 if (!s->readonly) { 1959 s->flags |= (1 << MTP_FLAG_WRITABLE); 1960 } 1961 1962 } 1963 1964 static const VMStateDescription vmstate_usb_mtp = { 1965 .name = "usb-mtp", 1966 .unmigratable = 1, 1967 .version_id = 1, 1968 .minimum_version_id = 1, 1969 .fields = (VMStateField[]) { 1970 VMSTATE_USB_DEVICE(dev, MTPState), 1971 VMSTATE_END_OF_LIST() 1972 } 1973 }; 1974 1975 static Property mtp_properties[] = { 1976 DEFINE_PROP_STRING("x-root", MTPState, root), 1977 DEFINE_PROP_STRING("desc", MTPState, desc), 1978 DEFINE_PROP_BOOL("readonly", MTPState, readonly, true), 1979 DEFINE_PROP_END_OF_LIST(), 1980 }; 1981 1982 static void usb_mtp_class_initfn(ObjectClass *klass, void *data) 1983 { 1984 DeviceClass *dc = DEVICE_CLASS(klass); 1985 USBDeviceClass *uc = USB_DEVICE_CLASS(klass); 1986 1987 uc->realize = usb_mtp_realize; 1988 uc->product_desc = "QEMU USB MTP"; 1989 uc->usb_desc = &desc; 1990 uc->cancel_packet = usb_mtp_cancel_packet; 1991 uc->handle_attach = usb_desc_attach; 1992 uc->handle_reset = usb_mtp_handle_reset; 1993 uc->handle_control = usb_mtp_handle_control; 1994 uc->handle_data = usb_mtp_handle_data; 1995 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1996 dc->desc = "USB Media Transfer Protocol device"; 1997 dc->fw_name = "mtp"; 1998 dc->vmsd = &vmstate_usb_mtp; 1999 dc->props = mtp_properties; 2000 } 2001 2002 static TypeInfo mtp_info = { 2003 .name = TYPE_USB_MTP, 2004 .parent = TYPE_USB_DEVICE, 2005 .instance_size = sizeof(MTPState), 2006 .class_init = usb_mtp_class_initfn, 2007 }; 2008 2009 static void usb_mtp_register_types(void) 2010 { 2011 type_register_static(&mtp_info); 2012 } 2013 2014 type_init(usb_mtp_register_types) 2015