hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Copyright (c) 2015 South Silicon Valley Microelectronics Inc.
 * Copyright (c) 2015 iComm Corporation
 *
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
#include <ssv6200.h>
#include <linux/types.h>
#include <linux/nl80211.h>
#include <net/mac80211.h>
#include <linux/nl80211.h>
#include <linux/etherdevice.h>
#include <linux/delay.h>
#include <linux/version.h>
#include <linux/time.h>
#include <linux/kthread.h>
#include <net/mac80211.h>
#include <ssv6200.h>
#include "p2p.h"
#include "dev.h"
#include "lib.h"
#ifdef CONFIG_P2P_NOA
#define P2P_IE_VENDOR_TYPE 0x506f9a09
#define P2P_NOA_DETECT_INTERVAL (5 * HZ)
#ifndef MAC2STR
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
#define COMPACT_MACSTR "%02x%02x%02x%02x%02x%02x"
#endif
void ssv6xxx_send_noa_cmd(struct ssv_softc *sc, struct ssv6xxx_p2p_noa_param *p2p_noa_param);
static inline u32 WPA_GET_BE32(const u8 *a)
{
 return (a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3];
}
static inline u16 WPA_GET_LE16(const u8 *a)
{
 return (a[1] << 8) | a[0];
}
static inline u32 WPA_GET_LE32(const u8 *a)
{
 return (a[3] << 24) |(a[2] << 16) |(a[1] << 8) | a[0];
}
#define IEEE80211_HDRLEN 24
enum p2p_attr_id {
 P2P_ATTR_STATUS = 0,
 P2P_ATTR_MINOR_REASON_CODE = 1,
 P2P_ATTR_CAPABILITY = 2,
 P2P_ATTR_DEVICE_ID = 3,
 P2P_ATTR_GROUP_OWNER_INTENT = 4,
 P2P_ATTR_CONFIGURATION_TIMEOUT = 5,
 P2P_ATTR_LISTEN_CHANNEL = 6,
 P2P_ATTR_GROUP_BSSID = 7,
 P2P_ATTR_EXT_LISTEN_TIMING = 8,
 P2P_ATTR_INTENDED_INTERFACE_ADDR = 9,
 P2P_ATTR_MANAGEABILITY = 10,
 P2P_ATTR_CHANNEL_LIST = 11,
 P2P_ATTR_NOTICE_OF_ABSENCE = 12,
 P2P_ATTR_DEVICE_INFO = 13,
 P2P_ATTR_GROUP_INFO = 14,
 P2P_ATTR_GROUP_ID = 15,
 P2P_ATTR_INTERFACE = 16,
 P2P_ATTR_OPERATING_CHANNEL = 17,
 P2P_ATTR_INVITATION_FLAGS = 18,
 P2P_ATTR_OOB_GO_NEG_CHANNEL = 19,
 P2P_ATTR_VENDOR_SPECIFIC = 221
};
struct ssv6xxx_p2p_noa_attribute {
    u8 index;
    u16 ctwindows_oppps;
    struct ssv6xxx_p2p_noa_param noa_param;
};
extern void _ssv6xxx_hexdump(const char *title, const u8 *buf,
                             size_t len);
bool p2p_find_noa(const u8 *ies, struct ssv6xxx_p2p_noa_attribute *noa_attr)
{
    const u8 *end, *pos, *ie;
    u32 len;
 len = ie[1] - 4;
 pos = ie + 6;
 end = pos+len;
 while (pos < end) {
  u16 attr_len;
  if (pos + 2 >= end) {
   return false;
  }
  attr_len = WPA_GET_LE16(pos + 1);
  if (pos + 3 + attr_len > end) {
   return false;
  }
        if(pos[0] != P2P_ATTR_NOTICE_OF_ABSENCE){
            pos += 3 + attr_len;
            continue;
        }
        if(attr_len<15){
            printk("*********************NOA descriptor does not exist len[%d]\n", attr_len);
            break;
        }
        if(attr_len>15)
            printk("More than one NOA descriptor\n");
        noa_attr->index = pos[3];
        noa_attr->ctwindows_oppps = pos[4];
        noa_attr->noa_param.count = pos[5];
        noa_attr->noa_param.duration = WPA_GET_LE32(&pos[6]);
        noa_attr->noa_param.interval = WPA_GET_LE32(&pos[10]);
        noa_attr->noa_param.start_time = WPA_GET_LE32(&pos[14]);
        return true;
 }
 return false;
}
bool p2p_get_attribute_noa(const u8 *ies, u32 oui_type, struct ssv6xxx_p2p_noa_attribute *noa_attr)
{
 const u8 *end, *pos, *ie;
    u32 len;
 pos = ies;
 end = ies + ies_len;
 ie = NULL;
 while (pos + 1 < end) {
  if (pos + 2 + pos[1] > end)
   return false;
  if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
      WPA_GET_BE32(&pos[2]) == oui_type) {
   ie = pos;
            if(p2p_find_noa(ie, 0, noa_attr) == true)
                return true;
  }
  pos += 2 + pos[1];
 }
 return false;
}
void ssv6xxx_process_noa_event(struct ssv_softc *sc, struct sk_buff *skb)
{
    struct cfg_host_event *host_event;
    struct ssv62xx_noa_evt *noa_evt;
    host_event = (struct cfg_host_event *)skb->data;
    noa_evt= (struct ssv62xx_noa_evt *)&host_event->dat[0];
    switch(noa_evt->evt_id){
        case SSV6XXX_NOA_START:
            sc->p2p_noa.active_noa_vif |= (1<<noa_evt->vif);
            printk("SSV6XXX_NOA_START===>[%08x]\n", sc->p2p_noa.active_noa_vif);
            break;
        case SSV6XXX_NOA_STOP:
            sc->p2p_noa.active_noa_vif &= ~(1<<noa_evt->vif);
            printk("SSV6XXX_NOA_STOP===>[%08x]\n", sc->p2p_noa.active_noa_vif);
            break;
        default:
            printk("--------->NOA wrong command<---------\n");
            break;
    }
}
void ssv6xxx_noa_reset(struct ssv_softc *sc){
    unsigned long flags;
    printk("Reset NOA param...\n");
    spin_lock_irqsave(&sc->p2p_noa.p2p_config_lock, flags);
    memset(&sc->p2p_noa.noa_detect, 0, sizeof(struct ssv_p2p_noa_detect)*SSV_NUM_VIF);
    sc->p2p_noa.active_noa_vif = 0;
    sc->p2p_noa.monitor_noa_vif = 0;
    spin_unlock_irqrestore(&sc->p2p_noa.p2p_config_lock, flags);
}
void ssv6xxx_noa_host_stop_noa(struct ssv_softc *sc, u8 vif_id){
    struct ssv6xxx_p2p_noa_attribute noa_attr;
    if(sc->p2p_noa.noa_detect[vif_id].p2p_noa_index>=0){
        sc->p2p_noa.noa_detect[vif_id].p2p_noa_index = -1;
        sc->p2p_noa.active_noa_vif &= ~(1<<vif_id);
        memset(&sc->p2p_noa.noa_detect[vif_id].noa_param_cmd, 0, sizeof(struct ssv6xxx_p2p_noa_param));
        printk("->remove NOA operating vif[%d]\n", vif_id);
        noa_attr.noa_param.enable = 0;
        noa_attr.noa_param.vif_id = vif_id;
        ssv6xxx_send_noa_cmd(sc, &noa_attr.noa_param);
    }
}
void ssv6xxx_noa_detect(struct ssv_softc *sc, struct ieee80211_hdr * hdr, u32 len){
    int i;
    unsigned long flags;
    struct ieee80211_mgmt * mgmt =(struct ieee80211_mgmt *)hdr;
    struct ssv6xxx_p2p_noa_attribute noa_attr;
    spin_lock_irqsave(&sc->p2p_noa.p2p_config_lock, flags);
    if(sc->p2p_noa.monitor_noa_vif == 0)
        goto out;
    for(i=0;i<SSV_NUM_VIF;i++){
        if(sc->p2p_noa.noa_detect[i].noa_addr == NULL)
            continue;
        if(memcmp(mgmt->bssid, sc->p2p_noa.noa_detect[i].noa_addr, 6) != 0)
            continue;
        if(sc->p2p_noa.active_noa_vif &&
            ((sc->p2p_noa.active_noa_vif & 1<<i) == 0))
            continue;
  sc->p2p_noa.noa_detect[i].last_rx = jiffies;
        if(p2p_get_attribute_noa((const u8*)mgmt->u.beacon.variable,
                len - (IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)),
                P2P_IE_VENDOR_TYPE,
                &noa_attr)== false){
            continue;
        }
        if(sc->p2p_noa.noa_detect[i].p2p_noa_index == noa_attr.index){
            goto out;
        }
        printk(MACSTR"->set NOA element\n", MAC2STR(mgmt->bssid));
        sc->p2p_noa.active_noa_vif |= (1<<i);
        sc->p2p_noa.noa_detect[i].p2p_noa_index = noa_attr.index;
        memcpy(&sc->p2p_noa.noa_detect[i].noa_param_cmd, &noa_attr.noa_param, sizeof(struct ssv6xxx_p2p_noa_param));
        noa_attr.noa_param.enable = 1;
        noa_attr.noa_param.vif_id = i;
        memcpy(noa_attr.noa_param.addr, hdr->addr2, 6);
        ssv6xxx_send_noa_cmd(sc, &noa_attr.noa_param);
    }
out:
    spin_unlock_irqrestore(&sc->p2p_noa.p2p_config_lock, flags);
}
void ssv6xxx_noa_hdl_bss_change(struct ssv_softc *sc, enum ssv6xxx_noa_conf conf, u8 vif_idx){
    unsigned long flags;
    if(sc->vif_info[vif_idx].vif->type != NL80211_IFTYPE_STATION ||
        sc->vif_info[vif_idx].vif->p2p != true)
        return;
    spin_lock_irqsave(&sc->p2p_noa.p2p_config_lock, flags);
    printk("====>[NOA]ssv6xxx_noa_hdl_bss_change conf[%d] vif_idx[%d]\n", conf, vif_idx);
    switch(conf)
    {
        case MONITOR_NOA_CONF_ADD:
            memset(&sc->p2p_noa.noa_detect[vif_idx], 0, sizeof(struct ssv_p2p_noa_detect));
            sc->p2p_noa.noa_detect[vif_idx].noa_addr = sc->vif_info[vif_idx].vif->bss_conf.bssid;
            sc->p2p_noa.noa_detect[vif_idx].p2p_noa_index = -1;
            sc->p2p_noa.noa_detect[vif_idx].last_rx = jiffies;
            sc->p2p_noa.monitor_noa_vif |= 1<< vif_idx;
            break;
        case MONITOR_NOA_CONF_REMOVE:
            sc->p2p_noa.monitor_noa_vif &= ~(1<< vif_idx);
            sc->p2p_noa.noa_detect[vif_idx].noa_addr = NULL;
            ssv6xxx_noa_host_stop_noa(sc, vif_idx);
            break;
        default:
            break;
    }
    spin_unlock_irqrestore(&sc->p2p_noa.p2p_config_lock, flags);
}
void ssv6xxx_send_noa_cmd(struct ssv_softc *sc, struct ssv6xxx_p2p_noa_param *p2p_noa_param)
{
    struct sk_buff *skb;
    struct cfg_host_cmd *host_cmd;
    int retry_cnt = 5;
    skb = ssv_skb_alloc(HOST_CMD_HDR_LEN + sizeof(struct ssv6xxx_p2p_noa_param));
    skb->data_len = HOST_CMD_HDR_LEN + sizeof(struct ssv6xxx_p2p_noa_param);
    skb->len = skb->data_len;
    host_cmd = (struct cfg_host_cmd *)skb->data;
    host_cmd->c_type = HOST_CMD;
    host_cmd->h_cmd = (u8)SSV6XXX_HOST_CMD_SET_NOA;
    host_cmd->len = skb->data_len;
    memcpy(host_cmd->dat32, p2p_noa_param, sizeof(struct ssv6xxx_p2p_noa_param));
    printk("Noa cmd NOA Parameter:\nEnable=%d\nInterval=%d\nDuration=%d\nStart_time=0x%08x\nCount=%d\nAddr=[%02x:%02x:%02x:%02x:%02x:%02x]vif[%d]\n\n",
                        p2p_noa_param->enable,
                        p2p_noa_param->interval,
                        p2p_noa_param->duration,
                        p2p_noa_param->start_time,
                        p2p_noa_param->count,
                        p2p_noa_param->addr[0],
                        p2p_noa_param->addr[1],
                        p2p_noa_param->addr[2],
                        p2p_noa_param->addr[3],
                        p2p_noa_param->addr[4],
                        p2p_noa_param->addr[5],
                        p2p_noa_param->vif_id);
    while((HCI_SEND_CMD(sc->sh, skb)!=0)&&(retry_cnt)){
        printk(KERN_INFO "NOA cmd retry=%d!!\n",retry_cnt);
        retry_cnt--;
    }
    ssv_skb_free(skb);
}
#if 0
void ssv6200_cmd_work(struct work_struct *work)
{
    struct ssv_softc *sc =
            container_of(work, struct ssv_softc, cmd_work);
    struct sk_buff *skb;
    bool cmd_mode;
    int retry_cnt = 5;
    do{
        if(sc->cmd_info.state == SSC_CMD_STATE_IDLE){
            struct cfg_host_cmd *host_cmd;
            skb = skb_peek(&sc->cmd_info.cmd_que);
            if(skb == NULL)
                break;
            host_cmd = (struct cfg_host_cmd *)skb->data;
            while((HCI_SEND_CMD(sc->sh, skb)!=0)&&(retry_cnt)){
                    printk(KERN_INFO "cmd retry=%d!!\n",retry_cnt);
                    retry_cnt--;
            }
            if(retry_cnt)
                sc->cmd_info.state = SSC_CMD_STATE_WAIT_RSP;
        }else{
            skb = skb_dequeue(&sc->cmd_info.evt_que);
            if(skb == NULL)
                break;
        }
    }while(1);
}
#endif
#endif