--- NetworkManager-0.5.2/src/nm-device-802-11-wireless.c	2006-02-21 11:39:33.000000000 +0100
+++ /root/Desktop/nm-device-802-11-wireless.c	2006-03-03 17:13:29.000000000 +0100
@@ -1798,18 +1798,22 @@
 			int			orig_rate = 0;
 			struct iwreq	wrq;
 
+			/* We only unlock the frequency if the card is in adhoc mode, in case it is
+			 * a costly operation for the driver.
+			 *
+			 * We only set the mode and unlock the frequency if the card is in adhoc mode,
+			 * in case doing so is a costly operation for the driver or the driver prefers
+			 * IW_MODE_AUTO.
+ 			 */
 			orig_mode = nm_device_802_11_wireless_get_mode (self);
 			if (orig_mode == IW_MODE_ADHOC)
 			{
 				orig_freq = nm_device_802_11_wireless_get_frequency (self);
 				orig_rate = nm_device_802_11_wireless_get_bitrate (self);
+				nm_device_802_11_wireless_set_mode (self, IW_MODE_INFRA);
+				nm_device_802_11_wireless_set_frequency (self, 0);
 			}
 
-			/* Must be in infrastructure mode during scan, otherwise we don't get a full
-			 * list of scan results.  Scanning doesn't work well in Ad-Hoc mode :( 
-			 */
-			nm_device_802_11_wireless_set_mode (self, IW_MODE_INFRA);
-			nm_device_802_11_wireless_set_frequency (self, 0);
 
 			wrq.u.data.pointer = NULL;
 			wrq.u.data.flags = 0;
@@ -1839,10 +1843,10 @@
 					nm_warning ("get_scan_results() on device %s returned an error.", iface);
 			}
 
-			nm_device_802_11_wireless_set_mode (self, orig_mode);
 			/* Only set frequency if ad-hoc mode */
 			if (orig_mode == IW_MODE_ADHOC)
 			{
+				nm_device_802_11_wireless_set_mode (self, orig_mode);
 				nm_device_802_11_wireless_set_frequency (self, orig_freq);
 				nm_device_802_11_wireless_set_bitrate (self, orig_rate);
 			}
@@ -2282,13 +2286,29 @@
 	const char *		iface = nm_device_get_iface (NM_DEVICE (self));
 	gboolean			success = FALSE;
 	int				tries = 0;
+	const char *		wpa_driver;
+	const char *		kernel_driver;
+
 
 	if (!(ctrl = wpa_ctrl_open (WPA_SUPPLICANT_GLOBAL_SOCKET, NM_RUN_DIR)))
 		goto exit;
 
+	kernel_driver = nm_device_get_driver (NM_DEVICE (self));
+
+	/*
+	 * We want to work with the generic "wext" wpa_supplicant driver, but some kernel drivers
+	 * are just utter junk.  For those losers, we use a specific wpa_supplicant driver.
+	 */
+	if (!strcmp (kernel_driver, "ndiswrapper"))
+		wpa_driver = "ndiswrapper";
+	else if (!strcmp (kernel_driver, "ath_pci"))
+		wpa_driver = "madwifi";
+	else
+		wpa_driver = "wext";
+
 	/* wpa_cli -g/var/run/wpa_supplicant-global interface_add eth1 "" wext /var/run/wpa_supplicant */
 	if (!nm_utils_supplicant_request_with_check (ctrl, "OK", __func__, NULL,
-			"INTERFACE_ADD %s\t\twext\t" WPA_SUPPLICANT_CONTROL_SOCKET "\t", iface))
+			"INTERFACE_ADD %s\t\t%s\t" WPA_SUPPLICANT_CONTROL_SOCKET "\t", iface, wpa_driver))
 		goto exit;
 	wpa_ctrl_close (ctrl);
 
@@ -2324,7 +2344,8 @@
 	struct wpa_ctrl *	ctrl;
 	gboolean			user_created;
 	char *			hex_essid;
-	char *			ap_scan = "AP_SCAN 1";
+	const char *		ap_scan;
+	const char *		kernel_driver;
 
 	g_return_val_if_fail (self != NULL, FALSE);
 	g_return_val_if_fail (req != NULL, FALSE);
@@ -2335,14 +2356,25 @@
 	ctrl = self->priv->sup_ctrl;
 	g_assert (ctrl);
 
-	/* Ad-Hoc and non-broadcasting networks need AP_SCAN 2 */
 	user_created = nm_ap_get_user_created (ap);
-	if (!nm_ap_get_broadcast (ap) || user_created)
+	kernel_driver = nm_device_get_driver (NM_DEVICE (self));
+
+	/*
+	 * We want to use "AP_SCAN 1", which tells wpa_supplicant to perform scanning.  This seems
+	 * to work better with some drivers.  But we want "AP_SCAN 2", telling wpa_supplicant that
+	 * we will do our own scanning, if
+	 *
+	 *	- The driver is orinoco_cs.  It chokes on "AP_SCAN 1".
+	 *	- The AP is non-broadcast or Ad-Hoc.  Unless the driver is madwifi.
+	 */
+	if (!strcmp (kernel_driver, "orinoco_cs"))
+		ap_scan = "AP_SCAN 2";
+	else if ((!nm_ap_get_broadcast (ap) || user_created) && strcmp (kernel_driver, "ath_pci"))
 		ap_scan = "AP_SCAN 2";
+	else
+		ap_scan = "AP_SCAN 1";
 
-	/* Tell wpa_supplicant that we'll do the scanning */
-	if (!nm_utils_supplicant_request_with_check (ctrl, "OK", __func__, NULL,
-			ap_scan))
+	if (!nm_utils_supplicant_request_with_check (ctrl, "OK", __func__, NULL, ap_scan))
 		goto out;
 
 	/* Standard network setup info */



