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