1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef HDF_HOST_RNDIS_RAWAPI_H
17 #define HDF_HOST_RNDIS_RAWAPI_H
18 
19 #include <linux/types.h>
20 #include <servmgr_hdi.h>
21 #include <hdf_remote_service.h>
22 #include <hdf_sbuf.h>
23 
24 #include "data_fifo.h"
25 #include "hdf_device_desc.h"
26 #include "usb_raw_api.h"
27 #include "rndis_host.h"
28 
29 /* MS-Windows uses this strange size, but RNDIS spec says 1024 minimum */
30 #define    CONTROL_BUFFER_SIZE        1025
31 
32 /* RNDIS defines an (absurdly huge) 10 second control timeout,
33  * but ActiveSync seems to use a more usual 5 second timeout
34  * (which matches the USB 2.0 spec).
35  */
36 #define    RNDIS_CONTROL_TIMEOUT_MS    (5 * 1000)
37 
38 /* default filter used with RNDIS devices */
39 #define RNDIS_DEFAULT_FILTER ( \
40     RNDIS_PACKET_TYPE_DIRECTED | \
41     RNDIS_PACKET_TYPE_BROADCAST | \
42     RNDIS_PACKET_TYPE_ALL_MULTICAST | \
43     RNDIS_PACKET_TYPE_PROMISCUOUS)
44 
45 /* Flags to require specific physical medium type for generic_rndis_bind() */
46 #define FLAG_RNDIS_PHYM_NOT_WIRELESS    0x0001
47 #define FLAG_RNDIS_PHYM_WIRELESS        0x0002
48 /* Flags for driver_info::data */
49 #define RNDIS_DRIVER_DATA_POLL_STATUS    1    /* poll status before control */
50 
51 /*
52  * CONTROL uses CDC "encapsulated commands" with funky notifications.
53  *  - control-out:  SEND_ENCAPSULATED
54  *  - interrupt-in:  RESPONSE_AVAILABLE
55  *  - control-in:  GET_ENCAPSULATED
56  *
57  * We'll try to ignore the RESPONSE_AVAILABLE notifications.
58  *
59  * REVISIT some RNDIS implementations seem to have curious issues still
60  * to be resolved.
61  */
62 struct RndisMsgHdr {
63     __le32    msgType;            /* RNDIS_MSG_* */
64     __le32    msgLen;
65     /* followed by data that varies between messages */
66     __le32    requestId;
67     __le32    status;
68     /* ... and more */
69 } __attribute__ ((packed));
70 
71 struct RndisDataHdr {
72     __le32    msgType;         /* RNDIS_MSG_PACKET */
73     __le32    msgLen;          /* RndisDataHdr + dataLen + pad */
74     __le32    dataOffset;      /* 36 -- right after header */
75     __le32    dataLen;         /* ... real packet size */
76 
77     __le32    oobDataOffset;      /* zero */
78     __le32    oobDataLen;         /* zero */
79     __le32    numOob;              /* zero */
80     __le32    packetDataOffset;   /* zero */
81 
82     __le32    packetDataLen;      /* zero */
83     __le32    vcHandle;            /* zero */
84     __le32    reserved;             /* zero */
85 } __attribute__ ((packed));
86 
87 struct RndisInit {                /* OUT */
88     /* header and: */
89     __le32    msgType;             /* RNDIS_MSG_INIT */
90     __le32    msgLen;              /* 24 */
91     __le32    requestId;
92     __le32    majorVersion;        /* of rndis (1.0) */
93     __le32    minorVersion;
94     __le32    maxTransferSize;
95 } __attribute__ ((packed));
96 
97 struct RndisInitC {               /* IN */
98     /* header and: */
99     __le32    msgType;                 /* RNDIS_MSG_INIT_C */
100     __le32    msgLen;
101     __le32    requestId;
102     __le32    status;
103     __le32    majorVersion;            /* of rndis (1.0) */
104     __le32    minorVersion;
105     __le32    deviceFlags;
106     __le32    medium;                   /* zero == 802.3 */
107     __le32    maxPacketsPerMessage;
108     __le32    maxTransferSize;
109     __le32    packetAlignment;         /* max 7; (1<<n) bytes */
110     __le32    afListOffset;           /* zero */
111     __le32    afListSize;             /* zero */
112 } __attribute__ ((packed));
113 
114 struct RndisHalt {                    /* OUT (no reply) */
115     /* header and: */
116     __le32    msgType;                 /* RNDIS_MSG_HALT */
117     __le32    msgLen;
118     __le32    requestId;
119 } __attribute__ ((packed));
120 
121 struct RndisQuery {                /* OUT */
122     /* header and: */
123     __le32    msgType;             /* RNDIS_MSG_QUERY */
124     __le32    msgLen;
125     __le32    requestId;
126     __le32    oid;
127     __le32    len;
128     __le32    offset;
129     __le32    handle;               /* zero */
130 } __attribute__ ((packed));
131 
132 struct RndisQueryParam {
133     void      *buf;
134     uint32_t  oid;
135     uint32_t  in_len;
136 } __attribute__ ((packed));
137 
138 struct RndisQueryC {              /* IN */
139     /* header and: */
140     __le32    msgType;              /* RNDIS_MSG_QUERY_C */
141     __le32    msgLen;
142     __le32    requestId;
143     __le32    status;
144     __le32    len;
145     __le32    offset;
146 } __attribute__ ((packed));
147 
148 struct RndisSet {                /* OUT */
149     /* header and: */
150     __le32    msgType;            /* RNDIS_MSG_SET */
151     __le32    msgLen;
152     __le32    requestId;
153     __le32    oid;
154     __le32    len;
155     __le32    offset;
156     __le32    handle;               /* zero */
157 } __attribute__ ((packed));
158 
159 struct RndisSetC {               /* IN */
160     /* header and: */
161     __le32    msgType;             /* RNDIS_MSG_SET_C */
162     __le32    msgLen;
163     __le32    requestId;
164     __le32    status;
165 } __attribute__ ((packed));
166 
167 struct RndisReset {            /* IN */
168     /* header and: */
169     __le32    msgType;          /* RNDIS_MSG_RESET */
170     __le32    msgLen;
171     __le32    reserved;
172 } __attribute__ ((packed));
173 
174 struct RndisResetC {        /* OUT */
175     /* header and: */
176     __le32    msgType;          /* RNDIS_MSG_RESET_C */
177     __le32    msgLen;
178     __le32    status;
179     __le32    addressingLost;
180 } __attribute__ ((packed));
181 
182 struct RndisIndicate {        /* IN (unrequested) */
183     /* header and: */
184     __le32    msgType;         /* RNDIS_MSG_INDICATE */
185     __le32    msgLen;
186     __le32    status;
187     __le32    length;
188     __le32    offset;
189     __le32    diagStatus;
190     __le32    errorOffset;
191     __le32    message;
192 } __attribute__ ((packed));
193 
194 struct RndisKeepalive {    /* OUT (optionally IN) */
195     /* header and: */
196     __le32    msgType;            /* RNDIS_MSG_KEEPALIVE */
197     __le32    msgLen;
198     __le32    requestId;
199 } __attribute__ ((packed));
200 
201 struct RndisKeepaliveC {      /* IN (optionally OUT) */
202     /* header and: */
203     __le32    msgType;           /* RNDIS_MSG_KEEPALIVE_C */
204     __le32    msgLen;
205     __le32    requestId;
206     __le32    status;
207 } __attribute__ ((packed));
208 
209 #endif /* HDF_USB_SERIAL_RAWAPI_H */
210