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
, u32 did
, u32 data
)
75 struct p80211msg_dot11req_mibset msg
;
76 struct p80211item_uint32
*mibitem
=
77 (struct p80211item_uint32
*)&msg
.mibattribute
.data
;
79 msg
.msgcode
= DIDmsg_dot11req_mibset
;
83 return p80211req_dorequest(wlandev
, (u8
*)&msg
);
86 static int prism2_domibset_pstr32(struct wlandevice
*wlandev
,
87 u32 did
, u8 len
, const u8
*data
)
89 struct p80211msg_dot11req_mibset msg
;
90 struct p80211item_pstr32
*mibitem
=
91 (struct p80211item_pstr32
*)&msg
.mibattribute
.data
;
93 msg
.msgcode
= DIDmsg_dot11req_mibset
;
95 mibitem
->data
.len
= len
;
96 memcpy(mibitem
->data
.data
, data
, len
);
98 return p80211req_dorequest(wlandev
, (u8
*)&msg
);
101 /* The interface functions, called by the cfg80211 layer */
102 static int prism2_change_virtual_intf(struct wiphy
*wiphy
,
103 struct net_device
*dev
,
104 enum nl80211_iftype type
,
105 struct vif_params
*params
)
107 struct wlandevice
*wlandev
= dev
->ml_priv
;
113 case NL80211_IFTYPE_ADHOC
:
114 if (wlandev
->macmode
== WLAN_MACMODE_IBSS_STA
)
116 wlandev
->macmode
= WLAN_MACMODE_IBSS_STA
;
119 case NL80211_IFTYPE_STATION
:
120 if (wlandev
->macmode
== WLAN_MACMODE_ESS_STA
)
122 wlandev
->macmode
= WLAN_MACMODE_ESS_STA
;
126 netdev_warn(dev
, "Operation mode: %d not support\n", type
);
130 /* Set Operation mode to the PORT TYPE RID */
131 result
= prism2_domibset_uint32(wlandev
,
132 DIDmib_p2_p2Static_p2CnfPortType
,
138 dev
->ieee80211_ptr
->iftype
= type
;
144 static int prism2_add_key(struct wiphy
*wiphy
, struct net_device
*dev
,
145 u8 key_index
, bool pairwise
, const u8
*mac_addr
,
146 struct key_params
*params
)
148 struct wlandevice
*wlandev
= dev
->ml_priv
;
154 if (key_index
>= NUM_WEPKEYS
)
157 switch (params
->cipher
) {
158 case WLAN_CIPHER_SUITE_WEP40
:
159 case WLAN_CIPHER_SUITE_WEP104
:
160 result
= prism2_domibset_uint32(wlandev
,
161 DIDmib_dot11smt_dot11PrivacyTable_dot11WEPDefaultKeyID
,
166 /* send key to driver */
167 did
= DIDmib_dot11smt_dot11WEPDefaultKeysTable_key(key_index
+ 1);
169 result
= prism2_domibset_pstr32(wlandev
, did
,
170 params
->key_len
, params
->key
);
176 pr_debug("Unsupported cipher suite\n");
187 static int prism2_get_key(struct wiphy
*wiphy
, struct net_device
*dev
,
188 u8 key_index
, bool pairwise
,
189 const u8
*mac_addr
, void *cookie
,
190 void (*callback
)(void *cookie
, struct key_params
*))
192 struct wlandevice
*wlandev
= dev
->ml_priv
;
193 struct key_params params
;
196 if (key_index
>= NUM_WEPKEYS
)
199 len
= wlandev
->wep_keylens
[key_index
];
200 memset(¶ms
, 0, sizeof(params
));
203 params
.cipher
= WLAN_CIPHER_SUITE_WEP104
;
205 params
.cipher
= WLAN_CIPHER_SUITE_WEP104
;
208 params
.key_len
= len
;
209 params
.key
= wlandev
->wep_keys
[key_index
];
212 callback(cookie
, ¶ms
);
217 static int prism2_del_key(struct wiphy
*wiphy
, struct net_device
*dev
,
218 u8 key_index
, bool pairwise
, const u8
*mac_addr
)
220 struct wlandevice
*wlandev
= dev
->ml_priv
;
225 /* There is no direct way in the hardware (AFAIK) of removing
226 * a key, so we will cheat by setting the key to a bogus value
229 if (key_index
>= NUM_WEPKEYS
)
232 /* send key to driver */
233 did
= DIDmib_dot11smt_dot11WEPDefaultKeysTable_key(key_index
+ 1);
234 result
= prism2_domibset_pstr32(wlandev
, did
, 13, "0000000000000");
242 static int prism2_set_default_key(struct wiphy
*wiphy
, struct net_device
*dev
,
243 u8 key_index
, bool unicast
, bool multicast
)
245 struct wlandevice
*wlandev
= dev
->ml_priv
;
250 result
= prism2_domibset_uint32(wlandev
,
251 DIDmib_dot11smt_dot11PrivacyTable_dot11WEPDefaultKeyID
,
260 static int prism2_get_station(struct wiphy
*wiphy
, struct net_device
*dev
,
261 const u8
*mac
, struct station_info
*sinfo
)
263 struct wlandevice
*wlandev
= dev
->ml_priv
;
264 struct p80211msg_lnxreq_commsquality quality
;
267 memset(sinfo
, 0, sizeof(*sinfo
));
269 if (!wlandev
|| (wlandev
->msdstate
!= WLAN_MSD_RUNNING
))
272 /* build request message */
273 quality
.msgcode
= DIDmsg_lnxreq_commsquality
;
274 quality
.dbm
.data
= P80211ENUM_truth_true
;
275 quality
.dbm
.status
= P80211ENUM_msgitem_status_data_ok
;
277 /* send message to nsd */
278 if (!wlandev
->mlmerequest
)
281 result
= wlandev
->mlmerequest(wlandev
, (struct p80211msg
*)&quality
);
284 sinfo
->txrate
.legacy
= quality
.txrate
.data
;
285 sinfo
->filled
|= BIT(NL80211_STA_INFO_TX_BITRATE
);
286 sinfo
->signal
= quality
.level
.data
;
287 sinfo
->filled
|= BIT(NL80211_STA_INFO_SIGNAL
);
293 static int prism2_scan(struct wiphy
*wiphy
,
294 struct cfg80211_scan_request
*request
)
296 struct net_device
*dev
;
297 struct prism2_wiphy_private
*priv
= wiphy_priv(wiphy
);
298 struct wlandevice
*wlandev
;
299 struct p80211msg_dot11req_scan msg1
;
300 struct p80211msg_dot11req_scan_results msg2
;
301 struct cfg80211_bss
*bss
;
302 struct cfg80211_scan_info info
= {};
314 dev
= request
->wdev
->netdev
;
315 wlandev
= dev
->ml_priv
;
317 if (priv
->scan_request
&& priv
->scan_request
!= request
)
320 if (wlandev
->macmode
== WLAN_MACMODE_ESS_AP
) {
321 netdev_err(dev
, "Can't scan in AP mode\n");
325 priv
->scan_request
= request
;
327 memset(&msg1
, 0x00, sizeof(msg1
));
328 msg1
.msgcode
= DIDmsg_dot11req_scan
;
329 msg1
.bsstype
.data
= P80211ENUM_bsstype_any
;
331 memset(&msg1
.bssid
.data
.data
, 0xFF, sizeof(msg1
.bssid
.data
.data
));
332 msg1
.bssid
.data
.len
= 6;
334 if (request
->n_ssids
> 0) {
335 msg1
.scantype
.data
= P80211ENUM_scantype_active
;
336 msg1
.ssid
.data
.len
= request
->ssids
->ssid_len
;
337 memcpy(msg1
.ssid
.data
.data
,
338 request
->ssids
->ssid
, request
->ssids
->ssid_len
);
340 msg1
.scantype
.data
= 0;
342 msg1
.probedelay
.data
= 0;
345 (i
< request
->n_channels
) && i
< ARRAY_SIZE(prism2_channels
);
347 msg1
.channellist
.data
.data
[i
] =
348 ieee80211_frequency_to_channel(
349 request
->channels
[i
]->center_freq
);
350 msg1
.channellist
.data
.len
= request
->n_channels
;
352 msg1
.maxchanneltime
.data
= 250;
353 msg1
.minchanneltime
.data
= 200;
355 result
= p80211req_dorequest(wlandev
, (u8
*)&msg1
);
357 err
= prism2_result2err(msg1
.resultcode
.data
);
360 /* Now retrieve scan results */
361 numbss
= msg1
.numbss
.data
;
363 for (i
= 0; i
< numbss
; i
++) {
366 memset(&msg2
, 0, sizeof(msg2
));
367 msg2
.msgcode
= DIDmsg_dot11req_scan_results
;
368 msg2
.bssindex
.data
= i
;
370 result
= p80211req_dorequest(wlandev
, (u8
*)&msg2
);
372 (msg2
.resultcode
.data
!= P80211ENUM_resultcode_success
)) {
376 ie_buf
[0] = WLAN_EID_SSID
;
377 ie_buf
[1] = msg2
.ssid
.data
.len
;
378 ie_len
= ie_buf
[1] + 2;
379 memcpy(&ie_buf
[2], &msg2
.ssid
.data
.data
, msg2
.ssid
.data
.len
);
380 freq
= ieee80211_channel_to_frequency(msg2
.dschannel
.data
,
382 bss
= cfg80211_inform_bss(wiphy
,
383 ieee80211_get_channel(wiphy
, freq
),
384 CFG80211_BSS_FTYPE_UNKNOWN
,
385 (const u8
*)&msg2
.bssid
.data
.data
,
386 msg2
.timestamp
.data
, msg2
.capinfo
.data
,
387 msg2
.beaconperiod
.data
,
390 (msg2
.signal
.data
- 65536) * 100, /* Conversion to signed type */
399 cfg80211_put_bss(wiphy
, bss
);
403 err
= prism2_result2err(msg2
.resultcode
.data
);
406 info
.aborted
= !!(err
);
407 cfg80211_scan_done(request
, &info
);
408 priv
->scan_request
= NULL
;
412 static int prism2_set_wiphy_params(struct wiphy
*wiphy
, u32 changed
)
414 struct prism2_wiphy_private
*priv
= wiphy_priv(wiphy
);
415 struct wlandevice
*wlandev
= priv
->wlandev
;
420 if (changed
& WIPHY_PARAM_RTS_THRESHOLD
) {
421 if (wiphy
->rts_threshold
== -1)
424 data
= wiphy
->rts_threshold
;
426 result
= prism2_domibset_uint32(wlandev
,
427 DIDmib_dot11mac_dot11OperationTable_dot11RTSThreshold
,
435 if (changed
& WIPHY_PARAM_FRAG_THRESHOLD
) {
436 if (wiphy
->frag_threshold
== -1)
439 data
= wiphy
->frag_threshold
;
441 result
= prism2_domibset_uint32(wlandev
,
442 DIDmib_dot11mac_dot11OperationTable_dot11FragmentationThreshold
,
454 static int prism2_connect(struct wiphy
*wiphy
, struct net_device
*dev
,
455 struct cfg80211_connect_params
*sme
)
457 struct wlandevice
*wlandev
= dev
->ml_priv
;
458 struct ieee80211_channel
*channel
= sme
->channel
;
459 struct p80211msg_lnxreq_autojoin msg_join
;
461 int length
= sme
->ssid_len
;
463 int is_wep
= (sme
->crypto
.cipher_group
== WLAN_CIPHER_SUITE_WEP40
) ||
464 (sme
->crypto
.cipher_group
== WLAN_CIPHER_SUITE_WEP104
);
468 /* Set the channel */
470 chan
= ieee80211_frequency_to_channel(channel
->center_freq
);
471 result
= prism2_domibset_uint32(wlandev
,
472 DIDmib_dot11phy_dot11PhyDSSSTable_dot11CurrentChannel
,
478 /* Set the authorization */
479 if ((sme
->auth_type
== NL80211_AUTHTYPE_OPEN_SYSTEM
) ||
480 ((sme
->auth_type
== NL80211_AUTHTYPE_AUTOMATIC
) && !is_wep
))
481 msg_join
.authtype
.data
= P80211ENUM_authalg_opensystem
;
482 else if ((sme
->auth_type
== NL80211_AUTHTYPE_SHARED_KEY
) ||
483 ((sme
->auth_type
== NL80211_AUTHTYPE_AUTOMATIC
) && is_wep
))
484 msg_join
.authtype
.data
= P80211ENUM_authalg_sharedkey
;
487 "Unhandled authorisation type for connect (%d)\n",
490 /* Set the encryption - we only support wep */
493 if (sme
->key_idx
>= NUM_WEPKEYS
) {
498 result
= prism2_domibset_uint32(wlandev
,
499 DIDmib_dot11smt_dot11PrivacyTable_dot11WEPDefaultKeyID
,
504 /* send key to driver */
505 did
= DIDmib_dot11smt_dot11WEPDefaultKeysTable_key(
507 result
= prism2_domibset_pstr32(wlandev
,
514 /* Assume we should set privacy invoked and exclude unencrypted
515 * We could possible use sme->privacy here, but the assumption
516 * seems reasonable anyways
518 result
= prism2_domibset_uint32(wlandev
,
519 DIDmib_dot11smt_dot11PrivacyTable_dot11PrivacyInvoked
,
520 P80211ENUM_truth_true
);
524 result
= prism2_domibset_uint32(wlandev
,
525 DIDmib_dot11smt_dot11PrivacyTable_dot11ExcludeUnencrypted
,
526 P80211ENUM_truth_true
);
531 /* Assume we should unset privacy invoked
532 * and exclude unencrypted
534 result
= prism2_domibset_uint32(wlandev
,
535 DIDmib_dot11smt_dot11PrivacyTable_dot11PrivacyInvoked
,
536 P80211ENUM_truth_false
);
540 result
= prism2_domibset_uint32(wlandev
,
541 DIDmib_dot11smt_dot11PrivacyTable_dot11ExcludeUnencrypted
,
542 P80211ENUM_truth_false
);
547 /* Now do the actual join. Note there is no way that I can
548 * see to request a specific bssid
550 msg_join
.msgcode
= DIDmsg_lnxreq_autojoin
;
552 memcpy(msg_join
.ssid
.data
.data
, sme
->ssid
, length
);
553 msg_join
.ssid
.data
.len
= length
;
555 result
= p80211req_dorequest(wlandev
, (u8
*)&msg_join
);
564 static int prism2_disconnect(struct wiphy
*wiphy
, struct net_device
*dev
,
567 struct wlandevice
*wlandev
= dev
->ml_priv
;
568 struct p80211msg_lnxreq_autojoin msg_join
;
572 /* Do a join, with a bogus ssid. Thats the only way I can think of */
573 msg_join
.msgcode
= DIDmsg_lnxreq_autojoin
;
575 memcpy(msg_join
.ssid
.data
.data
, "---", 3);
576 msg_join
.ssid
.data
.len
= 3;
578 result
= p80211req_dorequest(wlandev
, (u8
*)&msg_join
);
586 static int prism2_join_ibss(struct wiphy
*wiphy
, struct net_device
*dev
,
587 struct cfg80211_ibss_params
*params
)
592 static int prism2_leave_ibss(struct wiphy
*wiphy
, struct net_device
*dev
)
597 static int prism2_set_tx_power(struct wiphy
*wiphy
, struct wireless_dev
*wdev
,
598 enum nl80211_tx_power_setting type
, int mbm
)
600 struct prism2_wiphy_private
*priv
= wiphy_priv(wiphy
);
601 struct wlandevice
*wlandev
= priv
->wlandev
;
606 if (type
== NL80211_TX_POWER_AUTOMATIC
)
609 data
= MBM_TO_DBM(mbm
);
611 result
= prism2_domibset_uint32(wlandev
,
612 DIDmib_dot11phy_dot11PhyTxPowerTable_dot11CurrentTxPowerLevel
,
624 static int prism2_get_tx_power(struct wiphy
*wiphy
, struct wireless_dev
*wdev
,
627 struct prism2_wiphy_private
*priv
= wiphy_priv(wiphy
);
628 struct wlandevice
*wlandev
= priv
->wlandev
;
629 struct p80211msg_dot11req_mibget msg
;
630 struct p80211item_uint32
*mibitem
;
634 mibitem
= (struct p80211item_uint32
*)&msg
.mibattribute
.data
;
635 msg
.msgcode
= DIDmsg_dot11req_mibget
;
637 DIDmib_dot11phy_dot11PhyTxPowerTable_dot11CurrentTxPowerLevel
;
639 result
= p80211req_dorequest(wlandev
, (u8
*)&msg
);
646 *dbm
= mibitem
->data
;
652 /* Interface callback functions, passing data back up to the cfg80211 layer */
653 void prism2_connect_result(struct wlandevice
*wlandev
, u8 failed
)
655 u16 status
= failed
?
656 WLAN_STATUS_UNSPECIFIED_FAILURE
: WLAN_STATUS_SUCCESS
;
658 cfg80211_connect_result(wlandev
->netdev
, wlandev
->bssid
,
659 NULL
, 0, NULL
, 0, status
, GFP_KERNEL
);
662 void prism2_disconnected(struct wlandevice
*wlandev
)
664 cfg80211_disconnected(wlandev
->netdev
, 0, NULL
,
665 0, false, GFP_KERNEL
);
668 void prism2_roamed(struct wlandevice
*wlandev
)
670 struct cfg80211_roam_info roam_info
= {
671 .bssid
= wlandev
->bssid
,
674 cfg80211_roamed(wlandev
->netdev
, &roam_info
, GFP_KERNEL
);
677 /* Structures for declaring wiphy interface */
678 static const struct cfg80211_ops prism2_usb_cfg_ops
= {
679 .change_virtual_intf
= prism2_change_virtual_intf
,
680 .add_key
= prism2_add_key
,
681 .get_key
= prism2_get_key
,
682 .del_key
= prism2_del_key
,
683 .set_default_key
= prism2_set_default_key
,
684 .get_station
= prism2_get_station
,
686 .set_wiphy_params
= prism2_set_wiphy_params
,
687 .connect
= prism2_connect
,
688 .disconnect
= prism2_disconnect
,
689 .join_ibss
= prism2_join_ibss
,
690 .leave_ibss
= prism2_leave_ibss
,
691 .set_tx_power
= prism2_set_tx_power
,
692 .get_tx_power
= prism2_get_tx_power
,
695 /* Functions to create/free wiphy interface */
696 static struct wiphy
*wlan_create_wiphy(struct device
*dev
, struct wlandevice
*wlandev
)
699 struct prism2_wiphy_private
*priv
;
701 wiphy
= wiphy_new(&prism2_usb_cfg_ops
, sizeof(*priv
));
705 priv
= wiphy_priv(wiphy
);
706 priv
->wlandev
= wlandev
;
707 memcpy(priv
->channels
, prism2_channels
, sizeof(prism2_channels
));
708 memcpy(priv
->rates
, prism2_rates
, sizeof(prism2_rates
));
709 priv
->band
.channels
= priv
->channels
;
710 priv
->band
.n_channels
= ARRAY_SIZE(prism2_channels
);
711 priv
->band
.bitrates
= priv
->rates
;
712 priv
->band
.n_bitrates
= ARRAY_SIZE(prism2_rates
);
713 priv
->band
.band
= NL80211_BAND_2GHZ
;
714 priv
->band
.ht_cap
.ht_supported
= false;
715 wiphy
->bands
[NL80211_BAND_2GHZ
] = &priv
->band
;
717 set_wiphy_dev(wiphy
, dev
);
718 wiphy
->privid
= prism2_wiphy_privid
;
719 wiphy
->max_scan_ssids
= 1;
720 wiphy
->interface_modes
= BIT(NL80211_IFTYPE_STATION
)
721 | BIT(NL80211_IFTYPE_ADHOC
);
722 wiphy
->signal_type
= CFG80211_SIGNAL_TYPE_MBM
;
723 wiphy
->n_cipher_suites
= PRISM2_NUM_CIPHER_SUITES
;
724 wiphy
->cipher_suites
= prism2_cipher_suites
;
726 if (wiphy_register(wiphy
) < 0) {
734 static void wlan_free_wiphy(struct wiphy
*wiphy
)
736 wiphy_unregister(wiphy
);