Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / staging / wlan-ng / cfg80211.c
blob759e475e303c06c440cb85fb6e640441eb20f19f
1 // SPDX-License-Identifier: GPL-2.0
2 /* cfg80211 Interface for prism2_usb module */
3 #include "hfa384x.h"
4 #include "prism2mgmt.h"
6 /* Prism2 channel/frequency/bitrate declarations */
7 static const struct ieee80211_channel prism2_channels[] = {
8 { .center_freq = 2412 },
9 { .center_freq = 2417 },
10 { .center_freq = 2422 },
11 { .center_freq = 2427 },
12 { .center_freq = 2432 },
13 { .center_freq = 2437 },
14 { .center_freq = 2442 },
15 { .center_freq = 2447 },
16 { .center_freq = 2452 },
17 { .center_freq = 2457 },
18 { .center_freq = 2462 },
19 { .center_freq = 2467 },
20 { .center_freq = 2472 },
21 { .center_freq = 2484 },
24 static const struct ieee80211_rate prism2_rates[] = {
25 { .bitrate = 10 },
26 { .bitrate = 20 },
27 { .bitrate = 55 },
28 { .bitrate = 110 }
31 #define PRISM2_NUM_CIPHER_SUITES 2
32 static const u32 prism2_cipher_suites[PRISM2_NUM_CIPHER_SUITES] = {
33 WLAN_CIPHER_SUITE_WEP40,
34 WLAN_CIPHER_SUITE_WEP104
37 /* prism2 device private data */
38 struct prism2_wiphy_private {
39 struct wlandevice *wlandev;
41 struct ieee80211_supported_band band;
42 struct ieee80211_channel channels[ARRAY_SIZE(prism2_channels)];
43 struct ieee80211_rate rates[ARRAY_SIZE(prism2_rates)];
45 struct cfg80211_scan_request *scan_request;
48 static const void * const prism2_wiphy_privid = &prism2_wiphy_privid;
50 /* Helper Functions */
51 static int prism2_result2err(int prism2_result)
53 int err = 0;
55 switch (prism2_result) {
56 case P80211ENUM_resultcode_invalid_parameters:
57 err = -EINVAL;
58 break;
59 case P80211ENUM_resultcode_implementation_failure:
60 err = -EIO;
61 break;
62 case P80211ENUM_resultcode_not_supported:
63 err = -EOPNOTSUPP;
64 break;
65 default:
66 err = 0;
67 break;
70 return err;
73 static int prism2_domibset_uint32(struct wlandevice *wlandev,
74 u32 did, u32 data)
76 struct p80211msg_dot11req_mibset msg;
77 struct p80211item_uint32 *mibitem =
78 (struct p80211item_uint32 *)&msg.mibattribute.data;
80 msg.msgcode = DIDMSG_DOT11REQ_MIBSET;
81 mibitem->did = did;
82 mibitem->data = data;
84 return p80211req_dorequest(wlandev, (u8 *)&msg);
87 static int prism2_domibset_pstr32(struct wlandevice *wlandev,
88 u32 did, u8 len, const u8 *data)
90 struct p80211msg_dot11req_mibset msg;
91 struct p80211item_pstr32 *mibitem =
92 (struct p80211item_pstr32 *)&msg.mibattribute.data;
94 msg.msgcode = DIDMSG_DOT11REQ_MIBSET;
95 mibitem->did = did;
96 mibitem->data.len = len;
97 memcpy(mibitem->data.data, data, len);
99 return p80211req_dorequest(wlandev, (u8 *)&msg);
102 /* The interface functions, called by the cfg80211 layer */
103 static int prism2_change_virtual_intf(struct wiphy *wiphy,
104 struct net_device *dev,
105 enum nl80211_iftype type,
106 struct vif_params *params)
108 struct wlandevice *wlandev = dev->ml_priv;
109 u32 data;
110 int result;
111 int err = 0;
113 switch (type) {
114 case NL80211_IFTYPE_ADHOC:
115 if (wlandev->macmode == WLAN_MACMODE_IBSS_STA)
116 goto exit;
117 wlandev->macmode = WLAN_MACMODE_IBSS_STA;
118 data = 0;
119 break;
120 case NL80211_IFTYPE_STATION:
121 if (wlandev->macmode == WLAN_MACMODE_ESS_STA)
122 goto exit;
123 wlandev->macmode = WLAN_MACMODE_ESS_STA;
124 data = 1;
125 break;
126 default:
127 netdev_warn(dev, "Operation mode: %d not support\n", type);
128 return -EOPNOTSUPP;
131 /* Set Operation mode to the PORT TYPE RID */
132 result = prism2_domibset_uint32(wlandev,
133 DIDMIB_P2_STATIC_CNFPORTTYPE,
134 data);
136 if (result)
137 err = -EFAULT;
139 dev->ieee80211_ptr->iftype = type;
141 exit:
142 return err;
145 static int prism2_add_key(struct wiphy *wiphy, struct net_device *dev,
146 u8 key_index, bool pairwise, const u8 *mac_addr,
147 struct key_params *params)
149 struct wlandevice *wlandev = dev->ml_priv;
150 u32 did;
152 if (key_index >= NUM_WEPKEYS)
153 return -EINVAL;
155 if (params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
156 params->cipher != WLAN_CIPHER_SUITE_WEP104) {
157 pr_debug("Unsupported cipher suite\n");
158 return -EFAULT;
161 if (prism2_domibset_uint32(wlandev,
162 DIDMIB_DOT11SMT_PRIVACYTABLE_WEPDEFAULTKEYID,
163 key_index))
164 return -EFAULT;
166 /* send key to driver */
167 did = didmib_dot11smt_wepdefaultkeystable_key(key_index + 1);
169 if (prism2_domibset_pstr32(wlandev, did, params->key_len, params->key))
170 return -EFAULT;
171 return 0;
174 static int prism2_get_key(struct wiphy *wiphy, struct net_device *dev,
175 u8 key_index, bool pairwise,
176 const u8 *mac_addr, void *cookie,
177 void (*callback)(void *cookie, struct key_params*))
179 struct wlandevice *wlandev = dev->ml_priv;
180 struct key_params params;
181 int len;
183 if (key_index >= NUM_WEPKEYS)
184 return -EINVAL;
186 len = wlandev->wep_keylens[key_index];
187 memset(&params, 0, sizeof(params));
189 if (len == 13)
190 params.cipher = WLAN_CIPHER_SUITE_WEP104;
191 else if (len == 5)
192 params.cipher = WLAN_CIPHER_SUITE_WEP104;
193 else
194 return -ENOENT;
195 params.key_len = len;
196 params.key = wlandev->wep_keys[key_index];
197 params.seq_len = 0;
199 callback(cookie, &params);
201 return 0;
204 static int prism2_del_key(struct wiphy *wiphy, struct net_device *dev,
205 u8 key_index, bool pairwise, const u8 *mac_addr)
207 struct wlandevice *wlandev = dev->ml_priv;
208 u32 did;
209 int err = 0;
210 int result = 0;
212 /* There is no direct way in the hardware (AFAIK) of removing
213 * a key, so we will cheat by setting the key to a bogus value
216 if (key_index >= NUM_WEPKEYS)
217 return -EINVAL;
219 /* send key to driver */
220 did = didmib_dot11smt_wepdefaultkeystable_key(key_index + 1);
221 result = prism2_domibset_pstr32(wlandev, did, 13, "0000000000000");
223 if (result)
224 err = -EFAULT;
226 return err;
229 static int prism2_set_default_key(struct wiphy *wiphy, struct net_device *dev,
230 u8 key_index, bool unicast, bool multicast)
232 struct wlandevice *wlandev = dev->ml_priv;
234 return prism2_domibset_uint32(wlandev,
235 DIDMIB_DOT11SMT_PRIVACYTABLE_WEPDEFAULTKEYID,
236 key_index);
239 static int prism2_get_station(struct wiphy *wiphy, struct net_device *dev,
240 const u8 *mac, struct station_info *sinfo)
242 struct wlandevice *wlandev = dev->ml_priv;
243 struct p80211msg_lnxreq_commsquality quality;
244 int result;
246 memset(sinfo, 0, sizeof(*sinfo));
248 if (!wlandev || (wlandev->msdstate != WLAN_MSD_RUNNING))
249 return -EOPNOTSUPP;
251 /* build request message */
252 quality.msgcode = DIDMSG_LNXREQ_COMMSQUALITY;
253 quality.dbm.data = P80211ENUM_truth_true;
254 quality.dbm.status = P80211ENUM_msgitem_status_data_ok;
256 /* send message to nsd */
257 if (!wlandev->mlmerequest)
258 return -EOPNOTSUPP;
260 result = wlandev->mlmerequest(wlandev, (struct p80211msg *)&quality);
262 if (result == 0) {
263 sinfo->txrate.legacy = quality.txrate.data;
264 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
265 sinfo->signal = quality.level.data;
266 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
269 return result;
272 static int prism2_scan(struct wiphy *wiphy,
273 struct cfg80211_scan_request *request)
275 struct net_device *dev;
276 struct prism2_wiphy_private *priv = wiphy_priv(wiphy);
277 struct wlandevice *wlandev;
278 struct p80211msg_dot11req_scan msg1;
279 struct p80211msg_dot11req_scan_results msg2;
280 struct cfg80211_bss *bss;
281 struct cfg80211_scan_info info = {};
283 int result;
284 int err = 0;
285 int numbss = 0;
286 int i = 0;
287 u8 ie_buf[46];
288 int ie_len;
290 if (!request)
291 return -EINVAL;
293 dev = request->wdev->netdev;
294 wlandev = dev->ml_priv;
296 if (priv->scan_request && priv->scan_request != request)
297 return -EBUSY;
299 if (wlandev->macmode == WLAN_MACMODE_ESS_AP) {
300 netdev_err(dev, "Can't scan in AP mode\n");
301 return -EOPNOTSUPP;
304 priv->scan_request = request;
306 memset(&msg1, 0x00, sizeof(msg1));
307 msg1.msgcode = DIDMSG_DOT11REQ_SCAN;
308 msg1.bsstype.data = P80211ENUM_bsstype_any;
310 memset(&msg1.bssid.data.data, 0xFF, sizeof(msg1.bssid.data.data));
311 msg1.bssid.data.len = 6;
313 if (request->n_ssids > 0) {
314 msg1.scantype.data = P80211ENUM_scantype_active;
315 msg1.ssid.data.len = request->ssids->ssid_len;
316 memcpy(msg1.ssid.data.data,
317 request->ssids->ssid, request->ssids->ssid_len);
318 } else {
319 msg1.scantype.data = 0;
321 msg1.probedelay.data = 0;
323 for (i = 0;
324 (i < request->n_channels) && i < ARRAY_SIZE(prism2_channels);
325 i++)
326 msg1.channellist.data.data[i] =
327 ieee80211_frequency_to_channel(
328 request->channels[i]->center_freq);
329 msg1.channellist.data.len = request->n_channels;
331 msg1.maxchanneltime.data = 250;
332 msg1.minchanneltime.data = 200;
334 result = p80211req_dorequest(wlandev, (u8 *)&msg1);
335 if (result) {
336 err = prism2_result2err(msg1.resultcode.data);
337 goto exit;
339 /* Now retrieve scan results */
340 numbss = msg1.numbss.data;
342 for (i = 0; i < numbss; i++) {
343 int freq;
345 memset(&msg2, 0, sizeof(msg2));
346 msg2.msgcode = DIDMSG_DOT11REQ_SCAN_RESULTS;
347 msg2.bssindex.data = i;
349 result = p80211req_dorequest(wlandev, (u8 *)&msg2);
350 if ((result != 0) ||
351 (msg2.resultcode.data != P80211ENUM_resultcode_success)) {
352 break;
355 ie_buf[0] = WLAN_EID_SSID;
356 ie_buf[1] = msg2.ssid.data.len;
357 ie_len = ie_buf[1] + 2;
358 memcpy(&ie_buf[2], &msg2.ssid.data.data, msg2.ssid.data.len);
359 freq = ieee80211_channel_to_frequency(msg2.dschannel.data,
360 NL80211_BAND_2GHZ);
361 bss = cfg80211_inform_bss(wiphy,
362 ieee80211_get_channel(wiphy, freq),
363 CFG80211_BSS_FTYPE_UNKNOWN,
364 (const u8 *)&msg2.bssid.data.data,
365 msg2.timestamp.data, msg2.capinfo.data,
366 msg2.beaconperiod.data,
367 ie_buf,
368 ie_len,
369 (msg2.signal.data - 65536) * 100, /* Conversion to signed type */
370 GFP_KERNEL);
372 if (!bss) {
373 err = -ENOMEM;
374 goto exit;
377 cfg80211_put_bss(wiphy, bss);
380 if (result)
381 err = prism2_result2err(msg2.resultcode.data);
383 exit:
384 info.aborted = !!(err);
385 cfg80211_scan_done(request, &info);
386 priv->scan_request = NULL;
387 return err;
390 static int prism2_set_wiphy_params(struct wiphy *wiphy, u32 changed)
392 struct prism2_wiphy_private *priv = wiphy_priv(wiphy);
393 struct wlandevice *wlandev = priv->wlandev;
394 u32 data;
395 int result;
396 int err = 0;
398 if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
399 if (wiphy->rts_threshold == -1)
400 data = 2347;
401 else
402 data = wiphy->rts_threshold;
404 result = prism2_domibset_uint32(wlandev,
405 DIDMIB_DOT11MAC_OPERATIONTABLE_RTSTHRESHOLD,
406 data);
407 if (result) {
408 err = -EFAULT;
409 goto exit;
413 if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
414 if (wiphy->frag_threshold == -1)
415 data = 2346;
416 else
417 data = wiphy->frag_threshold;
419 result = prism2_domibset_uint32(wlandev,
420 DIDMIB_DOT11MAC_OPERATIONTABLE_FRAGMENTATIONTHRESHOLD,
421 data);
422 if (result) {
423 err = -EFAULT;
424 goto exit;
428 exit:
429 return err;
432 static int prism2_connect(struct wiphy *wiphy, struct net_device *dev,
433 struct cfg80211_connect_params *sme)
435 struct wlandevice *wlandev = dev->ml_priv;
436 struct ieee80211_channel *channel = sme->channel;
437 struct p80211msg_lnxreq_autojoin msg_join;
438 u32 did;
439 int length = sme->ssid_len;
440 int chan = -1;
441 int is_wep = (sme->crypto.cipher_group == WLAN_CIPHER_SUITE_WEP40) ||
442 (sme->crypto.cipher_group == WLAN_CIPHER_SUITE_WEP104);
443 int result;
444 int err = 0;
446 /* Set the channel */
447 if (channel) {
448 chan = ieee80211_frequency_to_channel(channel->center_freq);
449 result = prism2_domibset_uint32(wlandev,
450 DIDMIB_DOT11PHY_DSSSTABLE_CURRENTCHANNEL,
451 chan);
452 if (result)
453 goto exit;
456 /* Set the authorization */
457 if ((sme->auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM) ||
458 ((sme->auth_type == NL80211_AUTHTYPE_AUTOMATIC) && !is_wep))
459 msg_join.authtype.data = P80211ENUM_authalg_opensystem;
460 else if ((sme->auth_type == NL80211_AUTHTYPE_SHARED_KEY) ||
461 ((sme->auth_type == NL80211_AUTHTYPE_AUTOMATIC) && is_wep))
462 msg_join.authtype.data = P80211ENUM_authalg_sharedkey;
463 else
464 netdev_warn(dev,
465 "Unhandled authorisation type for connect (%d)\n",
466 sme->auth_type);
468 /* Set the encryption - we only support wep */
469 if (is_wep) {
470 if (sme->key) {
471 if (sme->key_idx >= NUM_WEPKEYS)
472 return -EINVAL;
474 result = prism2_domibset_uint32(wlandev,
475 DIDMIB_DOT11SMT_PRIVACYTABLE_WEPDEFAULTKEYID,
476 sme->key_idx);
477 if (result)
478 goto exit;
480 /* send key to driver */
481 did = didmib_dot11smt_wepdefaultkeystable_key(
482 sme->key_idx + 1);
483 result = prism2_domibset_pstr32(wlandev,
484 did, sme->key_len,
485 (u8 *)sme->key);
486 if (result)
487 goto exit;
490 /* Assume we should set privacy invoked and exclude unencrypted
491 * We could possible use sme->privacy here, but the assumption
492 * seems reasonable anyways
494 result = prism2_domibset_uint32(wlandev,
495 DIDMIB_DOT11SMT_PRIVACYTABLE_PRIVACYINVOKED,
496 P80211ENUM_truth_true);
497 if (result)
498 goto exit;
500 result = prism2_domibset_uint32(wlandev,
501 DIDMIB_DOT11SMT_PRIVACYTABLE_EXCLUDEUNENCRYPTED,
502 P80211ENUM_truth_true);
503 if (result)
504 goto exit;
506 } else {
507 /* Assume we should unset privacy invoked
508 * and exclude unencrypted
510 result = prism2_domibset_uint32(wlandev,
511 DIDMIB_DOT11SMT_PRIVACYTABLE_PRIVACYINVOKED,
512 P80211ENUM_truth_false);
513 if (result)
514 goto exit;
516 result = prism2_domibset_uint32(wlandev,
517 DIDMIB_DOT11SMT_PRIVACYTABLE_EXCLUDEUNENCRYPTED,
518 P80211ENUM_truth_false);
519 if (result)
520 goto exit;
523 /* Now do the actual join. Note there is no way that I can
524 * see to request a specific bssid
526 msg_join.msgcode = DIDMSG_LNXREQ_AUTOJOIN;
528 memcpy(msg_join.ssid.data.data, sme->ssid, length);
529 msg_join.ssid.data.len = length;
531 result = p80211req_dorequest(wlandev, (u8 *)&msg_join);
533 exit:
534 if (result)
535 err = -EFAULT;
537 return err;
540 static int prism2_disconnect(struct wiphy *wiphy, struct net_device *dev,
541 u16 reason_code)
543 struct wlandevice *wlandev = dev->ml_priv;
544 struct p80211msg_lnxreq_autojoin msg_join;
545 int result;
546 int err = 0;
548 /* Do a join, with a bogus ssid. Thats the only way I can think of */
549 msg_join.msgcode = DIDMSG_LNXREQ_AUTOJOIN;
551 memcpy(msg_join.ssid.data.data, "---", 3);
552 msg_join.ssid.data.len = 3;
554 result = p80211req_dorequest(wlandev, (u8 *)&msg_join);
556 if (result)
557 err = -EFAULT;
559 return err;
562 static int prism2_join_ibss(struct wiphy *wiphy, struct net_device *dev,
563 struct cfg80211_ibss_params *params)
565 return -EOPNOTSUPP;
568 static int prism2_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
570 return -EOPNOTSUPP;
573 static int prism2_set_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev,
574 enum nl80211_tx_power_setting type, int mbm)
576 struct prism2_wiphy_private *priv = wiphy_priv(wiphy);
577 struct wlandevice *wlandev = priv->wlandev;
578 u32 data;
579 int result;
580 int err = 0;
582 if (type == NL80211_TX_POWER_AUTOMATIC)
583 data = 30;
584 else
585 data = MBM_TO_DBM(mbm);
587 result = prism2_domibset_uint32(wlandev,
588 DIDMIB_DOT11PHY_TXPOWERTABLE_CURRENTTXPOWERLEVEL,
589 data);
591 if (result) {
592 err = -EFAULT;
593 goto exit;
596 exit:
597 return err;
600 static int prism2_get_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev,
601 int *dbm)
603 struct prism2_wiphy_private *priv = wiphy_priv(wiphy);
604 struct wlandevice *wlandev = priv->wlandev;
605 struct p80211msg_dot11req_mibget msg;
606 struct p80211item_uint32 *mibitem;
607 int result;
608 int err = 0;
610 mibitem = (struct p80211item_uint32 *)&msg.mibattribute.data;
611 msg.msgcode = DIDMSG_DOT11REQ_MIBGET;
612 mibitem->did = DIDMIB_DOT11PHY_TXPOWERTABLE_CURRENTTXPOWERLEVEL;
614 result = p80211req_dorequest(wlandev, (u8 *)&msg);
616 if (result) {
617 err = -EFAULT;
618 goto exit;
621 *dbm = mibitem->data;
623 exit:
624 return err;
627 /* Interface callback functions, passing data back up to the cfg80211 layer */
628 void prism2_connect_result(struct wlandevice *wlandev, u8 failed)
630 u16 status = failed ?
631 WLAN_STATUS_UNSPECIFIED_FAILURE : WLAN_STATUS_SUCCESS;
633 cfg80211_connect_result(wlandev->netdev, wlandev->bssid,
634 NULL, 0, NULL, 0, status, GFP_KERNEL);
637 void prism2_disconnected(struct wlandevice *wlandev)
639 cfg80211_disconnected(wlandev->netdev, 0, NULL,
640 0, false, GFP_KERNEL);
643 void prism2_roamed(struct wlandevice *wlandev)
645 struct cfg80211_roam_info roam_info = {
646 .bssid = wlandev->bssid,
649 cfg80211_roamed(wlandev->netdev, &roam_info, GFP_KERNEL);
652 /* Structures for declaring wiphy interface */
653 static const struct cfg80211_ops prism2_usb_cfg_ops = {
654 .change_virtual_intf = prism2_change_virtual_intf,
655 .add_key = prism2_add_key,
656 .get_key = prism2_get_key,
657 .del_key = prism2_del_key,
658 .set_default_key = prism2_set_default_key,
659 .get_station = prism2_get_station,
660 .scan = prism2_scan,
661 .set_wiphy_params = prism2_set_wiphy_params,
662 .connect = prism2_connect,
663 .disconnect = prism2_disconnect,
664 .join_ibss = prism2_join_ibss,
665 .leave_ibss = prism2_leave_ibss,
666 .set_tx_power = prism2_set_tx_power,
667 .get_tx_power = prism2_get_tx_power,
670 /* Functions to create/free wiphy interface */
671 static struct wiphy *wlan_create_wiphy(struct device *dev,
672 struct wlandevice *wlandev)
674 struct wiphy *wiphy;
675 struct prism2_wiphy_private *priv;
677 wiphy = wiphy_new(&prism2_usb_cfg_ops, sizeof(*priv));
678 if (!wiphy)
679 return NULL;
681 priv = wiphy_priv(wiphy);
682 priv->wlandev = wlandev;
683 memcpy(priv->channels, prism2_channels, sizeof(prism2_channels));
684 memcpy(priv->rates, prism2_rates, sizeof(prism2_rates));
685 priv->band.channels = priv->channels;
686 priv->band.n_channels = ARRAY_SIZE(prism2_channels);
687 priv->band.bitrates = priv->rates;
688 priv->band.n_bitrates = ARRAY_SIZE(prism2_rates);
689 priv->band.band = NL80211_BAND_2GHZ;
690 priv->band.ht_cap.ht_supported = false;
691 wiphy->bands[NL80211_BAND_2GHZ] = &priv->band;
693 set_wiphy_dev(wiphy, dev);
694 wiphy->privid = prism2_wiphy_privid;
695 wiphy->max_scan_ssids = 1;
696 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
697 | BIT(NL80211_IFTYPE_ADHOC);
698 wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
699 wiphy->n_cipher_suites = PRISM2_NUM_CIPHER_SUITES;
700 wiphy->cipher_suites = prism2_cipher_suites;
702 if (wiphy_register(wiphy) < 0) {
703 wiphy_free(wiphy);
704 return NULL;
707 return wiphy;
710 static void wlan_free_wiphy(struct wiphy *wiphy)
712 wiphy_unregister(wiphy);
713 wiphy_free(wiphy);