Arjan van der Ven | c29ff6b | 2007-06-16 17:00:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2007, Intel Corporation |
| 3 | * |
| 4 | * This file is part of PowerTOP |
| 5 | * |
| 6 | * This program file is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | * for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program in a file named COPYING; if not, write to the |
| 17 | * Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, |
| 19 | * Boston, MA 02110-1301 USA |
| 20 | * |
| 21 | * Authors: |
| 22 | * Arjan van de Ven <arjan@linux.intel.com> |
| 23 | */ |
| 24 | |
| 25 | #include <unistd.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <stdint.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <dirent.h> |
| 32 | #include <linux/types.h> |
| 33 | #include <net/if.h> |
| 34 | #include <linux/sockios.h> |
| 35 | #include <sys/ioctl.h> |
Anurag Singh | c5781f8 | 2011-10-10 10:44:44 -0700 | [diff] [blame] | 36 | #include <sys/socket.h> |
Arjan van der Ven | c29ff6b | 2007-06-16 17:00:46 +0000 | [diff] [blame] | 37 | |
| 38 | /* work around a bug in debian -- it exposes kernel internal types to userspace */ |
| 39 | #define u64 __u64 |
| 40 | #define u32 __u32 |
| 41 | #define u16 __u16 |
| 42 | #define u8 __u8 |
| 43 | #include <linux/ethtool.h> |
| 44 | #undef u64 |
| 45 | #undef u32 |
| 46 | #undef u16 |
| 47 | #undef u8 |
| 48 | |
| 49 | |
| 50 | |
| 51 | #include "powertop.h" |
| 52 | |
| 53 | void activate_WOL_suggestion(void) |
| 54 | { |
| 55 | int sock; |
| 56 | struct ifreq ifr; |
| 57 | struct ethtool_wolinfo wol; |
| 58 | int ret; |
| 59 | |
| 60 | memset(&ifr, 0, sizeof(struct ifreq)); |
| 61 | |
| 62 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 63 | if (sock<0) |
| 64 | return; |
| 65 | |
| 66 | strcpy(ifr.ifr_name, "eth0"); |
| 67 | |
| 68 | /* Check if the interface is up */ |
| 69 | ret = ioctl(sock, SIOCGIFFLAGS, &ifr); |
Arjan van der Ven | 34b4494 | 2007-07-19 17:14:55 +0000 | [diff] [blame] | 70 | if (ret<0) { |
| 71 | close(sock); |
Arjan van der Ven | c29ff6b | 2007-06-16 17:00:46 +0000 | [diff] [blame] | 72 | return; |
Arjan van der Ven | 34b4494 | 2007-07-19 17:14:55 +0000 | [diff] [blame] | 73 | } |
Arjan van der Ven | c29ff6b | 2007-06-16 17:00:46 +0000 | [diff] [blame] | 74 | |
| 75 | if (ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) { |
| 76 | close(sock); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | memset(&wol, 0, sizeof(wol)); |
| 81 | |
| 82 | wol.cmd = ETHTOOL_GWOL; |
| 83 | ifr.ifr_data = (caddr_t)&wol; |
| 84 | ioctl(sock, SIOCETHTOOL, &ifr); |
| 85 | wol.cmd = ETHTOOL_SWOL; |
| 86 | wol.wolopts = 0; |
| 87 | ioctl(sock, SIOCETHTOOL, &ifr); |
| 88 | |
| 89 | close(sock); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | |
| 94 | void suggest_WOL_off(void) |
| 95 | { |
Arjan van der Ven | c29ff6b | 2007-06-16 17:00:46 +0000 | [diff] [blame] | 96 | int sock; |
| 97 | struct ifreq ifr; |
| 98 | struct ethtool_wolinfo wol; |
| 99 | int ret; |
| 100 | |
| 101 | memset(&ifr, 0, sizeof(struct ifreq)); |
| 102 | |
| 103 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 104 | if (sock<0) |
| 105 | return; |
| 106 | |
| 107 | strcpy(ifr.ifr_name, "eth0"); |
| 108 | |
| 109 | /* Check if the interface is up */ |
| 110 | ret = ioctl(sock, SIOCGIFFLAGS, &ifr); |
Arjan van der Ven | 34b4494 | 2007-07-19 17:14:55 +0000 | [diff] [blame] | 111 | if (ret<0) { |
| 112 | close(sock); |
Arjan van der Ven | c29ff6b | 2007-06-16 17:00:46 +0000 | [diff] [blame] | 113 | return; |
Arjan van der Ven | 34b4494 | 2007-07-19 17:14:55 +0000 | [diff] [blame] | 114 | } |
Arjan van der Ven | c29ff6b | 2007-06-16 17:00:46 +0000 | [diff] [blame] | 115 | |
| 116 | if (ifr.ifr_flags & (IFF_UP | IFF_RUNNING)) { |
| 117 | close(sock); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | memset(&wol, 0, sizeof(wol)); |
| 122 | |
| 123 | wol.cmd = ETHTOOL_GWOL; |
| 124 | ifr.ifr_data = (caddr_t)&wol; |
| 125 | ioctl(sock, SIOCETHTOOL, &ifr); |
| 126 | |
| 127 | if (wol.wolopts) { |
| 128 | add_suggestion(_( |
| 129 | "Disable Ethernet Wake-On-Lan with the following command:\n" |
| 130 | " ethtool -s eth0 wol d \n" |
| 131 | "Wake-on-Lan keeps the phy active, this costs power."), 5, |
| 132 | 'W', _(" W - disable Wake-On-Lan "), activate_WOL_suggestion); |
| 133 | |
| 134 | |
| 135 | } |
| 136 | |
| 137 | close(sock); |
| 138 | } |
| 139 | |