MtpDescriptors.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MTP_DESCRIPTORS_H
  17. #define MTP_DESCRIPTORS_H
  18. #include <linux/usb/ch9.h>
  19. #include <linux/usb/functionfs.h>
  20. #include <sys/endian.h>
  21. namespace android {
  22. constexpr char FFS_MTP_EP0[] = "/dev/usb-ffs/mtp/ep0";
  23. constexpr char FFS_MTP_EP_IN[] = "/dev/usb-ffs/mtp/ep1";
  24. constexpr char FFS_MTP_EP_OUT[] = "/dev/usb-ffs/mtp/ep2";
  25. constexpr char FFS_MTP_EP_INTR[] = "/dev/usb-ffs/mtp/ep3";
  26. constexpr char FFS_PTP_EP0[] = "/dev/usb-ffs/ptp/ep0";
  27. constexpr char FFS_PTP_EP_IN[] = "/dev/usb-ffs/ptp/ep1";
  28. constexpr char FFS_PTP_EP_OUT[] = "/dev/usb-ffs/ptp/ep2";
  29. constexpr char FFS_PTP_EP_INTR[] = "/dev/usb-ffs/ptp/ep3";
  30. constexpr int MAX_PACKET_SIZE_FS = 64;
  31. constexpr int MAX_PACKET_SIZE_HS = 512;
  32. constexpr int MAX_PACKET_SIZE_SS = 1024;
  33. constexpr int MAX_PACKET_SIZE_EV = 28;
  34. struct func_desc {
  35. struct usb_interface_descriptor intf;
  36. struct usb_endpoint_descriptor_no_audio sink;
  37. struct usb_endpoint_descriptor_no_audio source;
  38. struct usb_endpoint_descriptor_no_audio intr;
  39. } __attribute__((packed));
  40. struct ss_func_desc {
  41. struct usb_interface_descriptor intf;
  42. struct usb_endpoint_descriptor_no_audio sink;
  43. struct usb_ss_ep_comp_descriptor sink_comp;
  44. struct usb_endpoint_descriptor_no_audio source;
  45. struct usb_ss_ep_comp_descriptor source_comp;
  46. struct usb_endpoint_descriptor_no_audio intr;
  47. struct usb_ss_ep_comp_descriptor intr_comp;
  48. } __attribute__((packed));
  49. struct desc_v1 {
  50. struct usb_functionfs_descs_head_v1 {
  51. __le32 magic;
  52. __le32 length;
  53. __le32 fs_count;
  54. __le32 hs_count;
  55. } __attribute__((packed)) header;
  56. struct func_desc fs_descs, hs_descs;
  57. } __attribute__((packed));
  58. struct desc_v2 {
  59. struct usb_functionfs_descs_head_v2 header;
  60. // The rest of the structure depends on the flags in the header.
  61. __le32 fs_count;
  62. __le32 hs_count;
  63. __le32 ss_count;
  64. __le32 os_count;
  65. struct func_desc fs_descs, hs_descs;
  66. struct ss_func_desc ss_descs;
  67. struct usb_os_desc_header os_header;
  68. struct usb_ext_compat_desc os_desc;
  69. } __attribute__((packed));
  70. // OS descriptor contents should not be changed. See b/64790536.
  71. static_assert(sizeof(struct desc_v2) == sizeof(usb_functionfs_descs_head_v2) +
  72. 16 + 2 * sizeof(struct func_desc) + sizeof(struct ss_func_desc) +
  73. sizeof(usb_os_desc_header) + sizeof(usb_ext_compat_desc),
  74. "Size of mtp descriptor is incorrect!");
  75. #define STR_INTERFACE "MTP"
  76. struct functionfs_lang {
  77. __le16 code;
  78. char str1[sizeof(STR_INTERFACE)];
  79. } __attribute__((packed));
  80. struct functionfs_strings {
  81. struct usb_functionfs_strings_head header;
  82. struct functionfs_lang lang0;
  83. } __attribute__((packed));
  84. extern const struct desc_v2 mtp_desc_v2;
  85. extern const struct desc_v2 ptp_desc_v2;
  86. extern const struct desc_v1 mtp_desc_v1;
  87. extern const struct desc_v1 ptp_desc_v1;
  88. extern const struct functionfs_strings mtp_strings;
  89. bool writeDescriptors(int fd, bool ptp);
  90. }; // namespace android
  91. #endif // MTP_DESCRIPTORS_H