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 
17 #ifndef MTP_DESCRIPTORS_H
18 #define MTP_DESCRIPTORS_H
19 
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/functionfs.h>
22 #include <sys/endian.h>
23 
24 namespace android {
25 
26 #ifdef MTP_FUZZER
27 constexpr char FFS_MTP_EP0[] = "/data/local/tmp/usb-ffs/mtp/ep0";
28 constexpr char FFS_MTP_EP_IN[] = "/data/local/tmp/usb-ffs/mtp/ep1";
29 constexpr char FFS_MTP_EP_OUT[] = "/data/local/tmp/usb-ffs/mtp/ep2";
30 constexpr char FFS_MTP_EP_INTR[] = "/data/local/tmp/usb-ffs/mtp/ep3";
31 
32 constexpr char FFS_PTP_EP0[] = "/data/local/tmp/usb-ffs/ptp/ep0";
33 constexpr char FFS_PTP_EP_IN[] = "/data/local/tmp/usb-ffs/ptp/ep1";
34 constexpr char FFS_PTP_EP_OUT[] = "/data/local/tmp/usb-ffs/ptp/ep2";
35 constexpr char FFS_PTP_EP_INTR[] = "/data/local/tmp/usb-ffs/ptp/ep3";
36 #else
37 constexpr char FFS_MTP_EP0[] = "/dev/usb-ffs/mtp/ep0";
38 constexpr char FFS_MTP_EP_IN[] = "/dev/usb-ffs/mtp/ep1";
39 constexpr char FFS_MTP_EP_OUT[] = "/dev/usb-ffs/mtp/ep2";
40 constexpr char FFS_MTP_EP_INTR[] = "/dev/usb-ffs/mtp/ep3";
41 
42 constexpr char FFS_PTP_EP0[] = "/dev/usb-ffs/ptp/ep0";
43 constexpr char FFS_PTP_EP_IN[] = "/dev/usb-ffs/ptp/ep1";
44 constexpr char FFS_PTP_EP_OUT[] = "/dev/usb-ffs/ptp/ep2";
45 constexpr char FFS_PTP_EP_INTR[] = "/dev/usb-ffs/ptp/ep3";
46 #endif
47 
48 constexpr int MAX_PACKET_SIZE_FS = 64;
49 constexpr int MAX_PACKET_SIZE_HS = 512;
50 constexpr int MAX_PACKET_SIZE_SS = 1024;
51 constexpr int MAX_PACKET_SIZE_EV = 28;
52 
53 struct func_desc {
54     struct usb_interface_descriptor intf;
55     struct usb_endpoint_descriptor_no_audio sink;
56     struct usb_endpoint_descriptor_no_audio source;
57     struct usb_endpoint_descriptor_no_audio intr;
58 } __attribute__((packed));
59 
60 struct ss_func_desc {
61     struct usb_interface_descriptor intf;
62     struct usb_endpoint_descriptor_no_audio sink;
63     struct usb_ss_ep_comp_descriptor sink_comp;
64     struct usb_endpoint_descriptor_no_audio source;
65     struct usb_ss_ep_comp_descriptor source_comp;
66     struct usb_endpoint_descriptor_no_audio intr;
67     struct usb_ss_ep_comp_descriptor intr_comp;
68 } __attribute__((packed));
69 
70 struct desc_v1 {
71     struct usb_functionfs_descs_head_v1 {
72         __le32 magic;
73         __le32 length;
74         __le32 fs_count;
75         __le32 hs_count;
76     } __attribute__((packed)) header;
77     struct func_desc fs_descs, hs_descs;
78 } __attribute__((packed));
79 
80 struct desc_v2 {
81     struct usb_functionfs_descs_head_v2 header;
82     // The rest of the structure depends on the flags in the header.
83     __le32 fs_count;
84     __le32 hs_count;
85     __le32 ss_count;
86     __le32 os_count;
87     struct func_desc fs_descs, hs_descs;
88     struct ss_func_desc ss_descs;
89     struct usb_os_desc_header os_header;
90     struct usb_ext_compat_desc os_desc;
91 } __attribute__((packed));
92 
93 // OS descriptor contents should not be changed. See b/64790536.
94 static_assert(sizeof(struct desc_v2) == sizeof(usb_functionfs_descs_head_v2) +
95         16 + 2 * sizeof(struct func_desc) + sizeof(struct ss_func_desc) +
96         sizeof(usb_os_desc_header) + sizeof(usb_ext_compat_desc),
97         "Size of mtp descriptor is incorrect!");
98 
99 #define STR_INTERFACE "MTP"
100 struct functionfs_lang {
101     __le16 code;
102     char str1[sizeof(STR_INTERFACE)];
103 } __attribute__((packed));
104 
105 struct functionfs_strings {
106     struct usb_functionfs_strings_head header;
107     struct functionfs_lang lang0;
108 } __attribute__((packed));
109 
110 extern const struct desc_v2 mtp_desc_v2;
111 extern const struct desc_v2 ptp_desc_v2;
112 extern const struct desc_v1 mtp_desc_v1;
113 extern const struct desc_v1 ptp_desc_v1;
114 extern const struct functionfs_strings mtp_strings;
115 
116 bool writeDescriptors(int fd, bool ptp);
117 
118 }; // namespace android
119 
120 #endif // MTP_DESCRIPTORS_H
121