17a192d395ce5d7e5945afa684146d5b0abc53480mbligh/*
27a192d395ce5d7e5945afa684146d5b0abc53480mbligh * Copyright 2007, Intel Corporation
37a192d395ce5d7e5945afa684146d5b0abc53480mbligh *
47a192d395ce5d7e5945afa684146d5b0abc53480mbligh * This file is part of PowerTOP
57a192d395ce5d7e5945afa684146d5b0abc53480mbligh *
67a192d395ce5d7e5945afa684146d5b0abc53480mbligh * This program file is free software; you can redistribute it and/or modify it
77a192d395ce5d7e5945afa684146d5b0abc53480mbligh * under the terms of the GNU General Public License as published by the
87a192d395ce5d7e5945afa684146d5b0abc53480mbligh * Free Software Foundation; version 2 of the License.
97a192d395ce5d7e5945afa684146d5b0abc53480mbligh *
107a192d395ce5d7e5945afa684146d5b0abc53480mbligh * This program is distributed in the hope that it will be useful, but WITHOUT
117a192d395ce5d7e5945afa684146d5b0abc53480mbligh * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
127a192d395ce5d7e5945afa684146d5b0abc53480mbligh * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
137a192d395ce5d7e5945afa684146d5b0abc53480mbligh * for more details.
147a192d395ce5d7e5945afa684146d5b0abc53480mbligh *
157a192d395ce5d7e5945afa684146d5b0abc53480mbligh * You should have received a copy of the GNU General Public License
167a192d395ce5d7e5945afa684146d5b0abc53480mbligh * along with this program in a file named COPYING; if not, write to the
177a192d395ce5d7e5945afa684146d5b0abc53480mbligh * Free Software Foundation, Inc.,
187a192d395ce5d7e5945afa684146d5b0abc53480mbligh * 51 Franklin Street, Fifth Floor,
197a192d395ce5d7e5945afa684146d5b0abc53480mbligh * Boston, MA 02110-1301 USA
207a192d395ce5d7e5945afa684146d5b0abc53480mbligh *
217a192d395ce5d7e5945afa684146d5b0abc53480mbligh * Authors:
227a192d395ce5d7e5945afa684146d5b0abc53480mbligh * 	Arjan van de Ven <arjan@linux.intel.com>
237a192d395ce5d7e5945afa684146d5b0abc53480mbligh */
247a192d395ce5d7e5945afa684146d5b0abc53480mbligh
257a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <unistd.h>
267a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <stdio.h>
277a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <stdlib.h>
287a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <string.h>
297a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <stdint.h>
307a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <sys/types.h>
317a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <dirent.h>
327a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <linux/types.h>
337a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <net/if.h>
347a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <linux/sockios.h>
357a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <sys/ioctl.h>
367a192d395ce5d7e5945afa684146d5b0abc53480mbligh
377a192d395ce5d7e5945afa684146d5b0abc53480mbligh/* work around a bug in debian -- it exposes kernel internal types to userspace */
387a192d395ce5d7e5945afa684146d5b0abc53480mbligh#define u64 __u64
397a192d395ce5d7e5945afa684146d5b0abc53480mbligh#define u32 __u32
407a192d395ce5d7e5945afa684146d5b0abc53480mbligh#define u16 __u16
417a192d395ce5d7e5945afa684146d5b0abc53480mbligh#define u8 __u8
427a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include <linux/ethtool.h>
437a192d395ce5d7e5945afa684146d5b0abc53480mbligh#undef u64
447a192d395ce5d7e5945afa684146d5b0abc53480mbligh#undef u32
457a192d395ce5d7e5945afa684146d5b0abc53480mbligh#undef u16
467a192d395ce5d7e5945afa684146d5b0abc53480mbligh#undef u8
477a192d395ce5d7e5945afa684146d5b0abc53480mbligh
487a192d395ce5d7e5945afa684146d5b0abc53480mbligh
497a192d395ce5d7e5945afa684146d5b0abc53480mbligh
507a192d395ce5d7e5945afa684146d5b0abc53480mbligh#include "powertop.h"
517a192d395ce5d7e5945afa684146d5b0abc53480mbligh
527a192d395ce5d7e5945afa684146d5b0abc53480mbligh
537a192d395ce5d7e5945afa684146d5b0abc53480mblighstatic char wireless_nic[32];
547a192d395ce5d7e5945afa684146d5b0abc53480mblighstatic char rfkill_path[PATH_MAX];
557a192d395ce5d7e5945afa684146d5b0abc53480mblighstatic char powersave_path[PATH_MAX];
567a192d395ce5d7e5945afa684146d5b0abc53480mbligh
577a192d395ce5d7e5945afa684146d5b0abc53480mblighstatic int rfkill_enabled(void)
587a192d395ce5d7e5945afa684146d5b0abc53480mbligh{
597a192d395ce5d7e5945afa684146d5b0abc53480mbligh	FILE *file;
607a192d395ce5d7e5945afa684146d5b0abc53480mbligh	char val;
617a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (strlen(rfkill_path)<2)
627a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 0;
637a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (access(rfkill_path, W_OK))
647a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 0;
657a192d395ce5d7e5945afa684146d5b0abc53480mbligh
667a192d395ce5d7e5945afa684146d5b0abc53480mbligh	file = fopen(rfkill_path, "r");
677a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (!file)
687a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 0;
697a192d395ce5d7e5945afa684146d5b0abc53480mbligh	val = fgetc(file);
707a192d395ce5d7e5945afa684146d5b0abc53480mbligh	fclose(file);
717a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (val != '0') /* already rfkill'd */
727a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 1;
737a192d395ce5d7e5945afa684146d5b0abc53480mbligh	return 0;
747a192d395ce5d7e5945afa684146d5b0abc53480mbligh}
757a192d395ce5d7e5945afa684146d5b0abc53480mbligh
767a192d395ce5d7e5945afa684146d5b0abc53480mblighint check_unused_wiresless_up(void)
777a192d395ce5d7e5945afa684146d5b0abc53480mbligh{
787a192d395ce5d7e5945afa684146d5b0abc53480mbligh	FILE *file;
797a192d395ce5d7e5945afa684146d5b0abc53480mbligh	char val;
807a192d395ce5d7e5945afa684146d5b0abc53480mbligh	char line[1024];
817a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (strlen(rfkill_path)<2)
827a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 0;
837a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (access(rfkill_path, W_OK))
847a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 0;
857a192d395ce5d7e5945afa684146d5b0abc53480mbligh
867a192d395ce5d7e5945afa684146d5b0abc53480mbligh	file = fopen(rfkill_path, "r");
877a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (!file)
887a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 0;
897a192d395ce5d7e5945afa684146d5b0abc53480mbligh	val = fgetc(file);
907a192d395ce5d7e5945afa684146d5b0abc53480mbligh	fclose(file);
917a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (val != '0') /* already rfkill'd */
927a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return -1;
937a192d395ce5d7e5945afa684146d5b0abc53480mbligh
947a192d395ce5d7e5945afa684146d5b0abc53480mbligh	sprintf(line,"iwconfig %s 2> /dev/null", wireless_nic);
957a192d395ce5d7e5945afa684146d5b0abc53480mbligh	file = popen(line, "r");
967a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (!file)
977a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 0;
987a192d395ce5d7e5945afa684146d5b0abc53480mbligh	while (!feof(file)) {
997a192d395ce5d7e5945afa684146d5b0abc53480mbligh		memset(line, 0, 1024);
1007a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (fgets(line, 1023, file) == 0)
1017a192d395ce5d7e5945afa684146d5b0abc53480mbligh			break;
1027a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (strstr(line, "Mode:Managed") && strstr(line,"Access Point: Not-Associated")) {
1037a192d395ce5d7e5945afa684146d5b0abc53480mbligh			pclose(file);
1047a192d395ce5d7e5945afa684146d5b0abc53480mbligh			return 1;
1057a192d395ce5d7e5945afa684146d5b0abc53480mbligh		}
1067a192d395ce5d7e5945afa684146d5b0abc53480mbligh	}
1077a192d395ce5d7e5945afa684146d5b0abc53480mbligh	pclose(file);
1087a192d395ce5d7e5945afa684146d5b0abc53480mbligh	return 0;
1097a192d395ce5d7e5945afa684146d5b0abc53480mbligh}
1107a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1117a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1127a192d395ce5d7e5945afa684146d5b0abc53480mblighstatic int need_wireless_suggest(char *iface)
1137a192d395ce5d7e5945afa684146d5b0abc53480mbligh{
1147a192d395ce5d7e5945afa684146d5b0abc53480mbligh	FILE *file;
1157a192d395ce5d7e5945afa684146d5b0abc53480mbligh	char line[1024];
1167a192d395ce5d7e5945afa684146d5b0abc53480mbligh	int ret = 0;
1177a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1187a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (rfkill_enabled())
1197a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 0;
1207a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1217a192d395ce5d7e5945afa684146d5b0abc53480mbligh	sprintf(line, "/sbin/iwpriv %s get_power 2> /dev/null", iface);
1227a192d395ce5d7e5945afa684146d5b0abc53480mbligh	file = popen(line, "r");
1237a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (!file)
1247a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 0;
1257a192d395ce5d7e5945afa684146d5b0abc53480mbligh	while (!feof(file)) {
1267a192d395ce5d7e5945afa684146d5b0abc53480mbligh		memset(line, 0, 1024);
1277a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (fgets(line, 1023, file)==NULL)
1287a192d395ce5d7e5945afa684146d5b0abc53480mbligh			break;
1297a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (strstr(line, "Power save level: 6 (AC)")) {
1307a192d395ce5d7e5945afa684146d5b0abc53480mbligh			ret = 1;
1317a192d395ce5d7e5945afa684146d5b0abc53480mbligh			break;
1327a192d395ce5d7e5945afa684146d5b0abc53480mbligh		}
1337a192d395ce5d7e5945afa684146d5b0abc53480mbligh	}
1347a192d395ce5d7e5945afa684146d5b0abc53480mbligh	pclose(file);
1357a192d395ce5d7e5945afa684146d5b0abc53480mbligh	return ret;
1367a192d395ce5d7e5945afa684146d5b0abc53480mbligh}
1377a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1387a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1397a192d395ce5d7e5945afa684146d5b0abc53480mblighstatic int need_wireless_suggest_new(void)
1407a192d395ce5d7e5945afa684146d5b0abc53480mbligh{
1417a192d395ce5d7e5945afa684146d5b0abc53480mbligh	FILE *file;
1427a192d395ce5d7e5945afa684146d5b0abc53480mbligh	char val;
1437a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (strlen(powersave_path)<2)
1447a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 0;
1457a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (access(powersave_path, W_OK))
1467a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 0;
1477a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1487a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (rfkill_enabled())
1497a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 0;
1507a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1517a192d395ce5d7e5945afa684146d5b0abc53480mbligh	file = fopen(powersave_path, "r");
1527a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (!file)
1537a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 0;
1547a192d395ce5d7e5945afa684146d5b0abc53480mbligh	val = fgetc(file);
1557a192d395ce5d7e5945afa684146d5b0abc53480mbligh	fclose(file);
1567a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (val <= '5' && val >= '0') /* already in powersave */
1577a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return 0;
1587a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1597a192d395ce5d7e5945afa684146d5b0abc53480mbligh	return 1;
1607a192d395ce5d7e5945afa684146d5b0abc53480mbligh}
1617a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1627a192d395ce5d7e5945afa684146d5b0abc53480mblighvoid find_4965(void)
1637a192d395ce5d7e5945afa684146d5b0abc53480mbligh{
1647a192d395ce5d7e5945afa684146d5b0abc53480mbligh	static int tried_4965 = 0;
1657a192d395ce5d7e5945afa684146d5b0abc53480mbligh	DIR *dir;
1667a192d395ce5d7e5945afa684146d5b0abc53480mbligh	struct dirent *dirent;
1677a192d395ce5d7e5945afa684146d5b0abc53480mbligh	char pathname[PATH_MAX];
1687a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1697a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (tried_4965++)
1707a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return;
1717a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1727a192d395ce5d7e5945afa684146d5b0abc53480mbligh	dir = opendir("/sys/bus/pci/drivers/iwl4965");
1737a192d395ce5d7e5945afa684146d5b0abc53480mbligh	while (dir && (dirent = readdir(dir))) {
1747a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (dirent->d_name[0]=='.')
1757a192d395ce5d7e5945afa684146d5b0abc53480mbligh			continue;
1767a192d395ce5d7e5945afa684146d5b0abc53480mbligh		sprintf(pathname, "/sys/bus/pci/drivers/iwl4965/%s/power_level", dirent->d_name);
1777a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (!access(pathname, W_OK))
1787a192d395ce5d7e5945afa684146d5b0abc53480mbligh			strcpy(powersave_path, pathname);
1797a192d395ce5d7e5945afa684146d5b0abc53480mbligh	}
1807a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (dir)
1817a192d395ce5d7e5945afa684146d5b0abc53480mbligh		closedir(dir);
1827a192d395ce5d7e5945afa684146d5b0abc53480mbligh	dir = opendir("/sys/bus/pci/drivers/iwl3945");
1837a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (!dir)
1847a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return;
1857a192d395ce5d7e5945afa684146d5b0abc53480mbligh	while ((dirent = readdir(dir))) {
1867a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (dirent->d_name[0]=='.')
1877a192d395ce5d7e5945afa684146d5b0abc53480mbligh			continue;
1887a192d395ce5d7e5945afa684146d5b0abc53480mbligh		sprintf(pathname, "/sys/bus/pci/drivers/iwl3945/%s/power_level", dirent->d_name);
1897a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (!access(pathname, W_OK))
1907a192d395ce5d7e5945afa684146d5b0abc53480mbligh			strcpy(powersave_path, pathname);
1917a192d395ce5d7e5945afa684146d5b0abc53480mbligh	}
1927a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1937a192d395ce5d7e5945afa684146d5b0abc53480mbligh	closedir(dir);
1947a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1957a192d395ce5d7e5945afa684146d5b0abc53480mbligh}
1967a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1977a192d395ce5d7e5945afa684146d5b0abc53480mbligh
1987a192d395ce5d7e5945afa684146d5b0abc53480mblighvoid find_wireless_nic(void)
1997a192d395ce5d7e5945afa684146d5b0abc53480mbligh{
2007a192d395ce5d7e5945afa684146d5b0abc53480mbligh	static int found = 0;
2017a192d395ce5d7e5945afa684146d5b0abc53480mbligh	FILE *file;
2027a192d395ce5d7e5945afa684146d5b0abc53480mbligh	int sock;
2037a192d395ce5d7e5945afa684146d5b0abc53480mbligh	struct ifreq ifr;
2047a192d395ce5d7e5945afa684146d5b0abc53480mbligh	struct ethtool_value ethtool;
2057a192d395ce5d7e5945afa684146d5b0abc53480mbligh	struct ethtool_drvinfo driver;
2067a192d395ce5d7e5945afa684146d5b0abc53480mbligh	int ifaceup = 0;
2077a192d395ce5d7e5945afa684146d5b0abc53480mbligh	int ret;
2087a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2097a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (found++)
2107a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return;
2117a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2127a192d395ce5d7e5945afa684146d5b0abc53480mbligh	wireless_nic[0] = 0;
2137a192d395ce5d7e5945afa684146d5b0abc53480mbligh	rfkill_path[0] = 0;
2147a192d395ce5d7e5945afa684146d5b0abc53480mbligh	powersave_path[0] = 0;
2157a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2167a192d395ce5d7e5945afa684146d5b0abc53480mbligh	strcpy(wireless_nic, "wlan0");
2177a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2187a192d395ce5d7e5945afa684146d5b0abc53480mbligh	file = popen("/sbin/iwpriv -a 2> /dev/null", "r");
2197a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (!file)
2207a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return;
2217a192d395ce5d7e5945afa684146d5b0abc53480mbligh	while (!feof(file)) {
2227a192d395ce5d7e5945afa684146d5b0abc53480mbligh		char line[1024];
2237a192d395ce5d7e5945afa684146d5b0abc53480mbligh		memset(line, 0, 1024);
2247a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (fgets(line, 1023, file)==NULL)
2257a192d395ce5d7e5945afa684146d5b0abc53480mbligh			break;
2267a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (strstr(line, "get_power:Power save level")) {
2277a192d395ce5d7e5945afa684146d5b0abc53480mbligh			char *c;
2287a192d395ce5d7e5945afa684146d5b0abc53480mbligh			c = strchr(line, ' ');
2297a192d395ce5d7e5945afa684146d5b0abc53480mbligh			if (c) *c = 0;
2307a192d395ce5d7e5945afa684146d5b0abc53480mbligh			strcpy(wireless_nic, line);
2317a192d395ce5d7e5945afa684146d5b0abc53480mbligh		}
2327a192d395ce5d7e5945afa684146d5b0abc53480mbligh		if (strstr(line, "wlan0:"))
2337a192d395ce5d7e5945afa684146d5b0abc53480mbligh			strcpy(wireless_nic, "wlan0");
2347a192d395ce5d7e5945afa684146d5b0abc53480mbligh	}
2357a192d395ce5d7e5945afa684146d5b0abc53480mbligh	pclose(file);
2367a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2377a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2387a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (strlen(wireless_nic)==0)
2397a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return;
2407a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2417a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2427a192d395ce5d7e5945afa684146d5b0abc53480mbligh	memset(&ifr, 0, sizeof(struct ifreq));
2437a192d395ce5d7e5945afa684146d5b0abc53480mbligh	memset(&ethtool, 0, sizeof(struct ethtool_value));
2447a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2457a192d395ce5d7e5945afa684146d5b0abc53480mbligh	sock = socket(AF_INET, SOCK_DGRAM, 0);
2467a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (sock<0)
2477a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return;
2487a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2497a192d395ce5d7e5945afa684146d5b0abc53480mbligh	strcpy(ifr.ifr_name, wireless_nic);
2507a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2517a192d395ce5d7e5945afa684146d5b0abc53480mbligh	/* Check if the interface is up */
2527a192d395ce5d7e5945afa684146d5b0abc53480mbligh	ret = ioctl(sock, SIOCGIFFLAGS, &ifr);
2537a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (ret<0) {
2547a192d395ce5d7e5945afa684146d5b0abc53480mbligh		close(sock);
2557a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return;
2567a192d395ce5d7e5945afa684146d5b0abc53480mbligh	}
2577a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2587a192d395ce5d7e5945afa684146d5b0abc53480mbligh	ifaceup = 0;
2597a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (ifr.ifr_flags & (IFF_UP | IFF_RUNNING))
2607a192d395ce5d7e5945afa684146d5b0abc53480mbligh		ifaceup = 1;
2617a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2627a192d395ce5d7e5945afa684146d5b0abc53480mbligh	memset(&driver, 0, sizeof(driver));
2637a192d395ce5d7e5945afa684146d5b0abc53480mbligh	driver.cmd = ETHTOOL_GDRVINFO;
2647a192d395ce5d7e5945afa684146d5b0abc53480mbligh        ifr.ifr_data = (void*) &driver;
2657a192d395ce5d7e5945afa684146d5b0abc53480mbligh        ret = ioctl(sock, SIOCETHTOOL, &ifr);
2667a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2677a192d395ce5d7e5945afa684146d5b0abc53480mbligh	sprintf(rfkill_path,"/sys/bus/pci/devices/%s/rfkill/rfkill0/state", driver.bus_info);
2687a192d395ce5d7e5945afa684146d5b0abc53480mbligh	sprintf(powersave_path,"/sys/bus/pci/devices/%s/power_level", driver.bus_info);
2697a192d395ce5d7e5945afa684146d5b0abc53480mbligh	close(sock);
2707a192d395ce5d7e5945afa684146d5b0abc53480mbligh}
2717a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2727a192d395ce5d7e5945afa684146d5b0abc53480mblighvoid activate_wireless_suggestion(void)
2737a192d395ce5d7e5945afa684146d5b0abc53480mbligh{
2747a192d395ce5d7e5945afa684146d5b0abc53480mbligh	char line[1024];
2757a192d395ce5d7e5945afa684146d5b0abc53480mbligh	sprintf(line, "/sbin/iwpriv %s set_power 5 2> /dev/null", wireless_nic);
2767a192d395ce5d7e5945afa684146d5b0abc53480mbligh	system(line);
2777a192d395ce5d7e5945afa684146d5b0abc53480mbligh}
2787a192d395ce5d7e5945afa684146d5b0abc53480mblighvoid activate_wireless_suggestion_new(void)
2797a192d395ce5d7e5945afa684146d5b0abc53480mbligh{
2807a192d395ce5d7e5945afa684146d5b0abc53480mbligh	FILE *file;
2817a192d395ce5d7e5945afa684146d5b0abc53480mbligh	file = fopen(powersave_path, "w");
2827a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (!file)
2837a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return;
2847a192d395ce5d7e5945afa684146d5b0abc53480mbligh	fprintf(file,"1\n");
2857a192d395ce5d7e5945afa684146d5b0abc53480mbligh	fclose(file);
2867a192d395ce5d7e5945afa684146d5b0abc53480mbligh}
2877a192d395ce5d7e5945afa684146d5b0abc53480mbligh
2887a192d395ce5d7e5945afa684146d5b0abc53480mblighvoid activate_rfkill_suggestion(void)
2897a192d395ce5d7e5945afa684146d5b0abc53480mbligh{
2907a192d395ce5d7e5945afa684146d5b0abc53480mbligh	FILE *file;
2917a192d395ce5d7e5945afa684146d5b0abc53480mbligh	file = fopen(rfkill_path, "w");
2927a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (!file)
2937a192d395ce5d7e5945afa684146d5b0abc53480mbligh		return;
2947a192d395ce5d7e5945afa684146d5b0abc53480mbligh	fprintf(file,"1\n");
2957a192d395ce5d7e5945afa684146d5b0abc53480mbligh	fclose(file);
2967a192d395ce5d7e5945afa684146d5b0abc53480mbligh}
2977a192d395ce5d7e5945afa684146d5b0abc53480mblighvoid suggest_wireless_powersave(void)
2987a192d395ce5d7e5945afa684146d5b0abc53480mbligh{
2997a192d395ce5d7e5945afa684146d5b0abc53480mbligh	char sug[1024];
3007a192d395ce5d7e5945afa684146d5b0abc53480mbligh	int ret;
3017a192d395ce5d7e5945afa684146d5b0abc53480mbligh
3027a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (strlen(wireless_nic)==0)
3037a192d395ce5d7e5945afa684146d5b0abc53480mbligh		find_wireless_nic();
3047a192d395ce5d7e5945afa684146d5b0abc53480mbligh	find_4965();
3057a192d395ce5d7e5945afa684146d5b0abc53480mbligh	ret = check_unused_wiresless_up();
3067a192d395ce5d7e5945afa684146d5b0abc53480mbligh
3077a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (ret >= 0 && need_wireless_suggest(wireless_nic)) {
3087a192d395ce5d7e5945afa684146d5b0abc53480mbligh		sprintf(sug, _("Suggestion: Enable wireless power saving mode by executing the following command:\n "
3097a192d395ce5d7e5945afa684146d5b0abc53480mbligh			       " iwpriv %s set_power 5 \n"
3107a192d395ce5d7e5945afa684146d5b0abc53480mbligh			       "This will sacrifice network performance slightly to save power."), wireless_nic);
3117a192d395ce5d7e5945afa684146d5b0abc53480mbligh		add_suggestion(sug, 20, 'W', _(" W - Enable wireless power saving "), activate_wireless_suggestion);
3127a192d395ce5d7e5945afa684146d5b0abc53480mbligh	}
3137a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (ret >= 0 && need_wireless_suggest_new()) {
3147a192d395ce5d7e5945afa684146d5b0abc53480mbligh		sprintf(sug, _("Suggestion: Enable wireless power saving mode by executing the following command:\n "
3157a192d395ce5d7e5945afa684146d5b0abc53480mbligh			       " echo 5 > %s \n"
3167a192d395ce5d7e5945afa684146d5b0abc53480mbligh			       "This will sacrifice network performance slightly to save power."), powersave_path);
3177a192d395ce5d7e5945afa684146d5b0abc53480mbligh		add_suggestion(sug, 20, 'W', _(" W - Enable wireless power saving "), activate_wireless_suggestion_new);
3187a192d395ce5d7e5945afa684146d5b0abc53480mbligh	}
3197a192d395ce5d7e5945afa684146d5b0abc53480mbligh	if (ret>0) {
3207a192d395ce5d7e5945afa684146d5b0abc53480mbligh		sprintf(sug, _("Suggestion: Disable the unused WIFI radio by executing the following command:\n "
3217a192d395ce5d7e5945afa684146d5b0abc53480mbligh			       " echo 1 > %s \n"), rfkill_path);
3227a192d395ce5d7e5945afa684146d5b0abc53480mbligh		add_suggestion(sug, 60, 'I', _(" I - disable WIFI Radio "), activate_rfkill_suggestion);
3237a192d395ce5d7e5945afa684146d5b0abc53480mbligh
3247a192d395ce5d7e5945afa684146d5b0abc53480mbligh	}
3257a192d395ce5d7e5945afa684146d5b0abc53480mbligh}
326