blob: 9b98a7176724394e5fab3b06d0d580ba7e6b6492 [file] [log] [blame]
Jerry Zhang69b74502017-10-02 16:26:37 -07001/*
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
24namespace android {
25
Akshata Kadam027fb912022-06-09 15:58:37 +053026#ifdef MTP_FUZZER
27constexpr char FFS_MTP_EP0[] = "/data/local/tmp/usb-ffs/mtp/ep0";
28constexpr char FFS_MTP_EP_IN[] = "/data/local/tmp/usb-ffs/mtp/ep1";
29constexpr char FFS_MTP_EP_OUT[] = "/data/local/tmp/usb-ffs/mtp/ep2";
30constexpr char FFS_MTP_EP_INTR[] = "/data/local/tmp/usb-ffs/mtp/ep3";
31
32constexpr char FFS_PTP_EP0[] = "/data/local/tmp/usb-ffs/ptp/ep0";
33constexpr char FFS_PTP_EP_IN[] = "/data/local/tmp/usb-ffs/ptp/ep1";
34constexpr char FFS_PTP_EP_OUT[] = "/data/local/tmp/usb-ffs/ptp/ep2";
35constexpr char FFS_PTP_EP_INTR[] = "/data/local/tmp/usb-ffs/ptp/ep3";
36#else
Jerry Zhang63dac452017-12-06 15:19:36 -080037constexpr char FFS_MTP_EP0[] = "/dev/usb-ffs/mtp/ep0";
38constexpr char FFS_MTP_EP_IN[] = "/dev/usb-ffs/mtp/ep1";
39constexpr char FFS_MTP_EP_OUT[] = "/dev/usb-ffs/mtp/ep2";
40constexpr char FFS_MTP_EP_INTR[] = "/dev/usb-ffs/mtp/ep3";
41
42constexpr char FFS_PTP_EP0[] = "/dev/usb-ffs/ptp/ep0";
43constexpr char FFS_PTP_EP_IN[] = "/dev/usb-ffs/ptp/ep1";
44constexpr char FFS_PTP_EP_OUT[] = "/dev/usb-ffs/ptp/ep2";
45constexpr char FFS_PTP_EP_INTR[] = "/dev/usb-ffs/ptp/ep3";
Akshata Kadam027fb912022-06-09 15:58:37 +053046#endif
Jerry Zhang63dac452017-12-06 15:19:36 -080047
Jerry Zhang69b74502017-10-02 16:26:37 -070048constexpr int MAX_PACKET_SIZE_FS = 64;
49constexpr int MAX_PACKET_SIZE_HS = 512;
50constexpr int MAX_PACKET_SIZE_SS = 1024;
51constexpr int MAX_PACKET_SIZE_EV = 28;
52
53struct 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
60struct 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
70struct 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
80struct 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.
94static_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"
100struct functionfs_lang {
101 __le16 code;
102 char str1[sizeof(STR_INTERFACE)];
103} __attribute__((packed));
104
105struct functionfs_strings {
106 struct usb_functionfs_strings_head header;
107 struct functionfs_lang lang0;
108} __attribute__((packed));
109
110extern const struct desc_v2 mtp_desc_v2;
111extern const struct desc_v2 ptp_desc_v2;
112extern const struct desc_v1 mtp_desc_v1;
113extern const struct desc_v1 ptp_desc_v1;
114extern const struct functionfs_strings mtp_strings;
115
Jerry Zhang63dac452017-12-06 15:19:36 -0800116bool writeDescriptors(int fd, bool ptp);
117
Jerry Zhang69b74502017-10-02 16:26:37 -0700118}; // namespace android
119
120#endif // MTP_DESCRIPTORS_H