1 // SPDX-License-Identifier: GPL-2.0
2 /* cfg80211 Interface for prism2_usb module */
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[] = {
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)
55 switch (prism2_result) {
56 case P80211ENUM_resultcode_invalid_parameters:
59 case P80211ENUM_resultcode_implementation_failure:
62 case P80211ENUM_resultcode_not_supported:
73 static int prism2_domibset_uint32(struct wlandevice *wlandev,
76 struct p80211msg_dot11req_mibset msg;
77 struct p80211item_uint32 *mibitem =
78 (struct p80211item_uint32 *)&msg.mibattribute.data;
80 msg.msgcode = DIDMSG_DOT11REQ_MIBSET;
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;
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;
114 case NL80211_IFTYPE_ADHOC:
115 if (wlandev->macmode == WLAN_MACMODE_IBSS_STA)
117 wlandev->macmode = WLAN_MACMODE_IBSS_STA;
120 case NL80211_IFTYPE_STATION:
121 if (wlandev->macmode == WLAN_MACMODE_ESS_STA)
123 wlandev->macmode = WLAN_MACMODE_ESS_STA;
127 netdev_warn(dev, "Operation mode: %d not support\n", type);
131 /* Set Operation mode to the PORT TYPE RID */
132 result = prism2_domibset_uint32(wlandev,
133 DIDMIB_P2_STATIC_CNFPORTTYPE,
139 dev->ieee80211_ptr->iftype = type;
145 static int prism2_add_key(struct wiphy *wiphy, struct net_device *dev,
146 int link_id, u8 key_index, bool pairwise,
147 const u8 *mac_addr, struct key_params *params)
149 struct wlandevice *wlandev = dev->ml_priv;
152 if (key_index >= NUM_WEPKEYS)
155 if (params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
156 params->cipher != WLAN_CIPHER_SUITE_WEP104) {
157 pr_debug("Unsupported cipher suite\n");
161 if (prism2_domibset_uint32(wlandev,
162 DIDMIB_DOT11SMT_PRIVACYTABLE_WEPDEFAULTKEYID,
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))
174 static int prism2_get_key(struct wiphy *wiphy, struct net_device *dev,
175 int link_id, 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;
183 if (key_index >= NUM_WEPKEYS)
186 len = wlandev->wep_keylens[key_index];
187 memset(¶ms, 0, sizeof(params));
190 params.cipher = WLAN_CIPHER_SUITE_WEP104;
192 params.cipher = WLAN_CIPHER_SUITE_WEP104;
195 params.key_len = len;
196 params.key = wlandev->wep_keys[key_index];
199 callback(cookie, ¶ms);
204 static int prism2_del_key(struct wiphy *wiphy, struct net_device *dev,
205 int link_id, u8 key_index, bool pairwise,
208 struct wlandevice *wlandev = dev->ml_priv;
213 /* There is no direct way in the hardware (AFAIK) of removing
214 * a key, so we will cheat by setting the key to a bogus value
217 if (key_index >= NUM_WEPKEYS)
220 /* send key to driver */
221 did = didmib_dot11smt_wepdefaultkeystable_key(key_index + 1);
222 result = prism2_domibset_pstr32(wlandev, did, 13, "0000000000000");
230 static int prism2_set_default_key(struct wiphy *wiphy, struct net_device *dev,
231 int link_id, u8 key_index, bool unicast,
234 struct wlandevice *wlandev = dev->ml_priv;
236 return prism2_domibset_uint32(wlandev,
237 DIDMIB_DOT11SMT_PRIVACYTABLE_WEPDEFAULTKEYID,
241 static int prism2_get_station(struct wiphy *wiphy, struct net_device *dev,
242 const u8 *mac, struct station_info *sinfo)
244 struct wlandevice *wlandev = dev->ml_priv;
245 struct p80211msg_lnxreq_commsquality quality;
248 memset(sinfo, 0, sizeof(*sinfo));
250 if (!wlandev || (wlandev->msdstate != WLAN_MSD_RUNNING))
253 /* build request message */
254 quality.msgcode = DIDMSG_LNXREQ_COMMSQUALITY;
255 quality.dbm.data = P80211ENUM_truth_true;
256 quality.dbm.status = P80211ENUM_msgitem_status_data_ok;
258 /* send message to nsd */
259 if (!wlandev->mlmerequest)
262 result = wlandev->mlmerequest(wlandev, (struct p80211msg *)&quality);
265 sinfo->txrate.legacy = quality.txrate.data;
266 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
267 sinfo->signal = quality.level.data;
268 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
274 static int prism2_scan(struct wiphy *wiphy,
275 struct cfg80211_scan_request *request)
277 struct net_device *dev;
278 struct prism2_wiphy_private *priv = wiphy_priv(wiphy);
279 struct wlandevice *wlandev;
280 struct p80211msg_dot11req_scan msg1;
281 struct p80211msg_dot11req_scan_results *msg2;
282 struct cfg80211_bss *bss;
283 struct cfg80211_scan_info info = {};
295 dev = request->wdev->netdev;
296 wlandev = dev->ml_priv;
298 if (priv->scan_request && priv->scan_request != request)
301 if (wlandev->macmode == WLAN_MACMODE_ESS_AP) {
302 netdev_err(dev, "Can't scan in AP mode\n");
306 msg2 = kzalloc(sizeof(*msg2), GFP_KERNEL);
310 priv->scan_request = request;
312 memset(&msg1, 0x00, sizeof(msg1));
313 msg1.msgcode = DIDMSG_DOT11REQ_SCAN;
314 msg1.bsstype.data = P80211ENUM_bsstype_any;
316 memset(&msg1.bssid.data.data, 0xFF, sizeof(msg1.bssid.data.data));
317 msg1.bssid.data.len = 6;
319 if (request->n_ssids > 0) {
320 msg1.scantype.data = P80211ENUM_scantype_active;
321 msg1.ssid.data.len = request->ssids->ssid_len;
322 memcpy(msg1.ssid.data.data,
323 request->ssids->ssid, request->ssids->ssid_len);
325 msg1.scantype.data = 0;
327 msg1.probedelay.data = 0;
330 (i < request->n_channels) && i < ARRAY_SIZE(prism2_channels);
332 msg1.channellist.data.data[i] =
333 ieee80211_frequency_to_channel(request->channels[i]->center_freq);
334 msg1.channellist.data.len = request->n_channels;
336 msg1.maxchanneltime.data = 250;
337 msg1.minchanneltime.data = 200;
339 result = p80211req_dorequest(wlandev, (u8 *)&msg1);
341 err = prism2_result2err(msg1.resultcode.data);
344 /* Now retrieve scan results */
345 numbss = msg1.numbss.data;
347 for (i = 0; i < numbss; i++) {
350 msg2->msgcode = DIDMSG_DOT11REQ_SCAN_RESULTS;
351 msg2->bssindex.data = i;
353 result = p80211req_dorequest(wlandev, (u8 *)&msg2);
355 (msg2->resultcode.data != P80211ENUM_resultcode_success)) {
359 ie_buf[0] = WLAN_EID_SSID;
360 ie_buf[1] = msg2->ssid.data.len;
361 ie_len = ie_buf[1] + 2;
362 memcpy(&ie_buf[2], &msg2->ssid.data.data, msg2->ssid.data.len);
363 freq = ieee80211_channel_to_frequency(msg2->dschannel.data,
365 bss = cfg80211_inform_bss(wiphy,
366 ieee80211_get_channel(wiphy, freq),
367 CFG80211_BSS_FTYPE_UNKNOWN,
368 (const u8 *)&msg2->bssid.data.data,
369 msg2->timestamp.data, msg2->capinfo.data,
370 msg2->beaconperiod.data,
373 (msg2->signal.data - 65536) * 100, /* Conversion to signed type */
381 cfg80211_put_bss(wiphy, bss);
385 err = prism2_result2err(msg2->resultcode.data);
388 info.aborted = !!(err);
389 cfg80211_scan_done(request, &info);
390 priv->scan_request = NULL;
395 static int prism2_set_wiphy_params(struct wiphy *wiphy, u32 changed)
397 struct prism2_wiphy_private *priv = wiphy_priv(wiphy);
398 struct wlandevice *wlandev = priv->wlandev;
403 if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
404 if (wiphy->rts_threshold == -1)
407 data = wiphy->rts_threshold;
409 result = prism2_domibset_uint32(wlandev,
410 DIDMIB_DOT11MAC_OPERATIONTABLE_RTSTHRESHOLD,
418 if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
419 if (wiphy->frag_threshold == -1)
422 data = wiphy->frag_threshold;
424 result = prism2_domibset_uint32(wlandev,
425 DIDMIB_DOT11MAC_OPERATIONTABLE_FRAGMENTATIONTHRESHOLD,
437 static int prism2_connect(struct wiphy *wiphy, struct net_device *dev,
438 struct cfg80211_connect_params *sme)
440 struct wlandevice *wlandev = dev->ml_priv;
441 struct ieee80211_channel *channel = sme->channel;
442 struct p80211msg_lnxreq_autojoin msg_join;
444 int length = sme->ssid_len;
446 int is_wep = (sme->crypto.cipher_group == WLAN_CIPHER_SUITE_WEP40) ||
447 (sme->crypto.cipher_group == WLAN_CIPHER_SUITE_WEP104);
451 /* Set the channel */
453 chan = ieee80211_frequency_to_channel(channel->center_freq);
454 result = prism2_domibset_uint32(wlandev,
455 DIDMIB_DOT11PHY_DSSSTABLE_CURRENTCHANNEL,
461 /* Set the authorization */
462 if ((sme->auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM) ||
463 ((sme->auth_type == NL80211_AUTHTYPE_AUTOMATIC) && !is_wep))
464 msg_join.authtype.data = P80211ENUM_authalg_opensystem;
465 else if ((sme->auth_type == NL80211_AUTHTYPE_SHARED_KEY) ||
466 ((sme->auth_type == NL80211_AUTHTYPE_AUTOMATIC) && is_wep))
467 msg_join.authtype.data = P80211ENUM_authalg_sharedkey;
470 "Unhandled authorisation type for connect (%d)\n",
473 /* Set the encryption - we only support wep */
476 if (sme->key_idx >= NUM_WEPKEYS)
479 result = prism2_domibset_uint32(wlandev,
480 DIDMIB_DOT11SMT_PRIVACYTABLE_WEPDEFAULTKEYID,
485 /* send key to driver */
486 did = didmib_dot11smt_wepdefaultkeystable_key(sme->key_idx + 1);
487 result = prism2_domibset_pstr32(wlandev,
494 /* Assume we should set privacy invoked and exclude unencrypted
495 * We could possible use sme->privacy here, but the assumption
496 * seems reasonable anyways
498 result = prism2_domibset_uint32(wlandev,
499 DIDMIB_DOT11SMT_PRIVACYTABLE_PRIVACYINVOKED,
500 P80211ENUM_truth_true);
504 result = prism2_domibset_uint32(wlandev,
505 DIDMIB_DOT11SMT_PRIVACYTABLE_EXCLUDEUNENCRYPTED,
506 P80211ENUM_truth_true);
511 /* Assume we should unset privacy invoked
512 * and exclude unencrypted
514 result = prism2_domibset_uint32(wlandev,
515 DIDMIB_DOT11SMT_PRIVACYTABLE_PRIVACYINVOKED,
516 P80211ENUM_truth_false);
520 result = prism2_domibset_uint32(wlandev,
521 DIDMIB_DOT11SMT_PRIVACYTABLE_EXCLUDEUNENCRYPTED,
522 P80211ENUM_truth_false);
527 /* Now do the actual join. Note there is no way that I can
528 * see to request a specific bssid
530 msg_join.msgcode = DIDMSG_LNXREQ_AUTOJOIN;
532 memcpy(msg_join.ssid.data.data, sme->ssid, length);
533 msg_join.ssid.data.len = length;
535 result = p80211req_dorequest(wlandev, (u8 *)&msg_join);
544 static int prism2_disconnect(struct wiphy *wiphy, struct net_device *dev,
547 struct wlandevice *wlandev = dev->ml_priv;
548 struct p80211msg_lnxreq_autojoin msg_join;
552 /* Do a join, with a bogus ssid. Thats the only way I can think of */
553 msg_join.msgcode = DIDMSG_LNXREQ_AUTOJOIN;
555 memcpy(msg_join.ssid.data.data, "---", 3);
556 msg_join.ssid.data.len = 3;
558 result = p80211req_dorequest(wlandev, (u8 *)&msg_join);
566 static int prism2_join_ibss(struct wiphy *wiphy, struct net_device *dev,
567 struct cfg80211_ibss_params *params)
572 static int prism2_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
577 static int prism2_set_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev,
578 enum nl80211_tx_power_setting type, int mbm)
580 struct prism2_wiphy_private *priv = wiphy_priv(wiphy);
581 struct wlandevice *wlandev = priv->wlandev;
586 if (type == NL80211_TX_POWER_AUTOMATIC)
589 data = MBM_TO_DBM(mbm);
591 result = prism2_domibset_uint32(wlandev,
592 DIDMIB_DOT11PHY_TXPOWERTABLE_CURRENTTXPOWERLEVEL,
604 static int prism2_get_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev,
607 struct prism2_wiphy_private *priv = wiphy_priv(wiphy);
608 struct wlandevice *wlandev = priv->wlandev;
609 struct p80211msg_dot11req_mibget msg;
610 struct p80211item_uint32 *mibitem;
614 mibitem = (struct p80211item_uint32 *)&msg.mibattribute.data;
615 msg.msgcode = DIDMSG_DOT11REQ_MIBGET;
616 mibitem->did = DIDMIB_DOT11PHY_TXPOWERTABLE_CURRENTTXPOWERLEVEL;
618 result = p80211req_dorequest(wlandev, (u8 *)&msg);
625 *dbm = mibitem->data;
631 /* Interface callback functions, passing data back up to the cfg80211 layer */
632 void prism2_connect_result(struct wlandevice *wlandev, u8 failed)
634 u16 status = failed ?
635 WLAN_STATUS_UNSPECIFIED_FAILURE : WLAN_STATUS_SUCCESS;
637 cfg80211_connect_result(wlandev->netdev, wlandev->bssid,
638 NULL, 0, NULL, 0, status, GFP_KERNEL);
641 void prism2_disconnected(struct wlandevice *wlandev)
643 cfg80211_disconnected(wlandev->netdev, 0, NULL,
644 0, false, GFP_KERNEL);
647 void prism2_roamed(struct wlandevice *wlandev)
649 struct cfg80211_roam_info roam_info = {
650 .links[0].bssid = wlandev->bssid,
653 cfg80211_roamed(wlandev->netdev, &roam_info, GFP_KERNEL);
656 /* Structures for declaring wiphy interface */
657 static const struct cfg80211_ops prism2_usb_cfg_ops = {
658 .change_virtual_intf = prism2_change_virtual_intf,
659 .add_key = prism2_add_key,
660 .get_key = prism2_get_key,
661 .del_key = prism2_del_key,
662 .set_default_key = prism2_set_default_key,
663 .get_station = prism2_get_station,
665 .set_wiphy_params = prism2_set_wiphy_params,
666 .connect = prism2_connect,
667 .disconnect = prism2_disconnect,
668 .join_ibss = prism2_join_ibss,
669 .leave_ibss = prism2_leave_ibss,
670 .set_tx_power = prism2_set_tx_power,
671 .get_tx_power = prism2_get_tx_power,
674 /* Functions to create/free wiphy interface */
675 static struct wiphy *wlan_create_wiphy(struct device *dev,
676 struct wlandevice *wlandev)
679 struct prism2_wiphy_private *priv;
681 wiphy = wiphy_new(&prism2_usb_cfg_ops, sizeof(*priv));
685 priv = wiphy_priv(wiphy);
686 priv->wlandev = wlandev;
687 memcpy(priv->channels, prism2_channels, sizeof(prism2_channels));
688 memcpy(priv->rates, prism2_rates, sizeof(prism2_rates));
689 priv->band.channels = priv->channels;
690 priv->band.n_channels = ARRAY_SIZE(prism2_channels);
691 priv->band.bitrates = priv->rates;
692 priv->band.n_bitrates = ARRAY_SIZE(prism2_rates);
693 priv->band.band = NL80211_BAND_2GHZ;
694 priv->band.ht_cap.ht_supported = false;
695 wiphy->bands[NL80211_BAND_2GHZ] = &priv->band;
697 set_wiphy_dev(wiphy, dev);
698 wiphy->privid = prism2_wiphy_privid;
699 wiphy->max_scan_ssids = 1;
700 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
701 | BIT(NL80211_IFTYPE_ADHOC);
702 wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
703 wiphy->n_cipher_suites = PRISM2_NUM_CIPHER_SUITES;
704 wiphy->cipher_suites = prism2_cipher_suites;
706 if (wiphy_register(wiphy) < 0) {
714 static void wlan_free_wiphy(struct wiphy *wiphy)
716 wiphy_unregister(wiphy);