blob: 67bcb9343ca6f9f050ffe4d2f875e9e51afc38fe [file] [log] [blame]
Jouni Malinen8ca21f02009-03-03 19:23:27 +02001/*
2 * Copyright (c) 2008-2009 Atheros Communications Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include "ath9k.h"
18
19struct ath9k_vif_iter_data {
20 int count;
21 u8 *addr;
22};
23
24static void ath9k_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
25{
26 struct ath9k_vif_iter_data *iter_data = data;
27 u8 *nbuf;
28
29 nbuf = krealloc(iter_data->addr, (iter_data->count + 1) * ETH_ALEN,
30 GFP_ATOMIC);
31 if (nbuf == NULL)
32 return;
33
34 memcpy(nbuf + iter_data->count * ETH_ALEN, mac, ETH_ALEN);
35 iter_data->addr = nbuf;
36 iter_data->count++;
37}
38
39void ath9k_set_bssid_mask(struct ieee80211_hw *hw)
40{
Jouni Malinenbce048d2009-03-03 19:23:28 +020041 struct ath_wiphy *aphy = hw->priv;
42 struct ath_softc *sc = aphy->sc;
Jouni Malinen8ca21f02009-03-03 19:23:27 +020043 struct ath9k_vif_iter_data iter_data;
44 int i, j;
45 u8 mask[ETH_ALEN];
46
47 /*
48 * Add primary MAC address even if it is not in active use since it
49 * will be configured to the hardware as the starting point and the
50 * BSSID mask will need to be changed if another address is active.
51 */
52 iter_data.addr = kmalloc(ETH_ALEN, GFP_ATOMIC);
53 if (iter_data.addr) {
54 memcpy(iter_data.addr, sc->sc_ah->macaddr, ETH_ALEN);
55 iter_data.count = 1;
56 } else
57 iter_data.count = 0;
58
59 /* Get list of all active MAC addresses */
Jouni Malinenc52f33d2009-03-03 19:23:29 +020060 spin_lock_bh(&sc->wiphy_lock);
61 ieee80211_iterate_active_interfaces_atomic(sc->hw, ath9k_vif_iter,
Jouni Malinen8ca21f02009-03-03 19:23:27 +020062 &iter_data);
Jouni Malinenc52f33d2009-03-03 19:23:29 +020063 for (i = 0; i < sc->num_sec_wiphy; i++) {
64 if (sc->sec_wiphy[i] == NULL)
65 continue;
66 ieee80211_iterate_active_interfaces_atomic(
67 sc->sec_wiphy[i]->hw, ath9k_vif_iter, &iter_data);
68 }
69 spin_unlock_bh(&sc->wiphy_lock);
Jouni Malinen8ca21f02009-03-03 19:23:27 +020070
71 /* Generate an address mask to cover all active addresses */
72 memset(mask, 0, ETH_ALEN);
73 for (i = 0; i < iter_data.count; i++) {
74 u8 *a1 = iter_data.addr + i * ETH_ALEN;
75 for (j = i + 1; j < iter_data.count; j++) {
76 u8 *a2 = iter_data.addr + j * ETH_ALEN;
77 mask[0] |= a1[0] ^ a2[0];
78 mask[1] |= a1[1] ^ a2[1];
79 mask[2] |= a1[2] ^ a2[2];
80 mask[3] |= a1[3] ^ a2[3];
81 mask[4] |= a1[4] ^ a2[4];
82 mask[5] |= a1[5] ^ a2[5];
83 }
84 }
85
86 kfree(iter_data.addr);
87
88 /* Invert the mask and configure hardware */
89 sc->bssidmask[0] = ~mask[0];
90 sc->bssidmask[1] = ~mask[1];
91 sc->bssidmask[2] = ~mask[2];
92 sc->bssidmask[3] = ~mask[3];
93 sc->bssidmask[4] = ~mask[4];
94 sc->bssidmask[5] = ~mask[5];
95
96 ath9k_hw_setbssidmask(sc);
97}
Jouni Malinenc52f33d2009-03-03 19:23:29 +020098
99int ath9k_wiphy_add(struct ath_softc *sc)
100{
101 int i, error;
102 struct ath_wiphy *aphy;
103 struct ieee80211_hw *hw;
104 u8 addr[ETH_ALEN];
105
106 hw = ieee80211_alloc_hw(sizeof(struct ath_wiphy), &ath9k_ops);
107 if (hw == NULL)
108 return -ENOMEM;
109
110 spin_lock_bh(&sc->wiphy_lock);
111 for (i = 0; i < sc->num_sec_wiphy; i++) {
112 if (sc->sec_wiphy[i] == NULL)
113 break;
114 }
115
116 if (i == sc->num_sec_wiphy) {
117 /* No empty slot available; increase array length */
118 struct ath_wiphy **n;
119 n = krealloc(sc->sec_wiphy,
120 (sc->num_sec_wiphy + 1) *
121 sizeof(struct ath_wiphy *),
122 GFP_ATOMIC);
123 if (n == NULL) {
124 spin_unlock_bh(&sc->wiphy_lock);
125 ieee80211_free_hw(hw);
126 return -ENOMEM;
127 }
128 n[i] = NULL;
129 sc->sec_wiphy = n;
130 sc->num_sec_wiphy++;
131 }
132
133 SET_IEEE80211_DEV(hw, sc->dev);
134
135 aphy = hw->priv;
136 aphy->sc = sc;
137 aphy->hw = hw;
138 sc->sec_wiphy[i] = aphy;
139 spin_unlock_bh(&sc->wiphy_lock);
140
141 memcpy(addr, sc->sc_ah->macaddr, ETH_ALEN);
142 addr[0] |= 0x02; /* Locally managed address */
143 /*
144 * XOR virtual wiphy index into the least significant bits to generate
145 * a different MAC address for each virtual wiphy.
146 */
147 addr[5] ^= i & 0xff;
148 addr[4] ^= (i & 0xff00) >> 8;
149 addr[3] ^= (i & 0xff0000) >> 16;
150
151 SET_IEEE80211_PERM_ADDR(hw, addr);
152
153 ath_set_hw_capab(sc, hw);
154
155 error = ieee80211_register_hw(hw);
156
157 return error;
158}
159
160int ath9k_wiphy_del(struct ath_wiphy *aphy)
161{
162 struct ath_softc *sc = aphy->sc;
163 int i;
164
165 spin_lock_bh(&sc->wiphy_lock);
166 for (i = 0; i < sc->num_sec_wiphy; i++) {
167 if (aphy == sc->sec_wiphy[i]) {
168 sc->sec_wiphy[i] = NULL;
169 spin_unlock_bh(&sc->wiphy_lock);
170 ieee80211_unregister_hw(aphy->hw);
171 ieee80211_free_hw(aphy->hw);
172 return 0;
173 }
174 }
175 spin_unlock_bh(&sc->wiphy_lock);
176 return -ENOENT;
177}