blob: b209e348686db75878fe74773efcb361b3ac7f16 [file] [log] [blame]
Arjan van der Ven8ca6c1f2007-05-21 22:25:44 +00001/*
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 <sys/types.h>
33#include <sys/socket.h>
34#include <sys/ioctl.h>
35#include <errno.h>
36
37#include "powertop.h"
38
39
40/* structure definitions copied from include/net/bluetooth/hci.h from the 2.6.20 kernel */
41#define HCIGETDEVINFO _IOR('H', 211, int)
42#define BTPROTO_HCI 1
43
44#define __u16 uint16_t
45#define __u8 uint8_t
46#define __u32 uint32_t
47
48typedef struct {
49 __u8 b[6];
50} __attribute__((packed)) bdaddr_t;
51
52struct hci_dev_stats {
53 __u32 err_rx;
54 __u32 err_tx;
55 __u32 cmd_tx;
56 __u32 evt_rx;
57 __u32 acl_tx;
58 __u32 acl_rx;
59 __u32 sco_tx;
60 __u32 sco_rx;
61 __u32 byte_rx;
62 __u32 byte_tx;
63};
64
65
66struct hci_dev_info {
67 __u16 dev_id;
68 char name[8];
69
70 bdaddr_t bdaddr;
71
72 __u32 flags;
73 __u8 type;
74
75 __u8 features[8];
76
77 __u32 pkt_type;
78 __u32 link_policy;
79 __u32 link_mode;
80
81 __u16 acl_mtu;
82 __u16 acl_pkts;
83 __u16 sco_mtu;
84 __u16 sco_pkts;
85
86 struct hci_dev_stats stat;
87};
88
89static int previous_bytes = -1;
90
Arjan van der Venb43056b2007-05-24 20:47:42 +000091void turn_bluetooth_off(void)
92{
Arjan van der Venc2da47b2008-03-27 18:20:59 +000093 system("/usr/sbin/hciconfig hci0 down &> /dev/null");
Arjan van der Ven00e3e712007-06-12 22:20:47 +000094 system("/sbin/rmmod hci_usb &> /dev/null");
Arjan van der Venb43056b2007-05-24 20:47:42 +000095}
96
Arjan van der Ven8ca6c1f2007-05-21 22:25:44 +000097void suggest_bluetooth_off(void)
98{
99 struct hci_dev_info devinfo;
Arjan van der Ven3ffc3642007-09-29 19:00:23 +0000100 FILE *file;
Arjan van der Ven8ca6c1f2007-05-21 22:25:44 +0000101 int fd;
102 int ret;
103 int thisbytes = 0;
104
Arjan van der Vend8e3c272007-05-22 18:47:49 +0000105 /* first check if /sys/modules/bluetooth exists, if not, don't probe bluetooth because
106 it would trigger an autoload */
107
108 if (access("/sys/module/bluetooth",F_OK))
109 return;
110
Arjan van der Ven8ca6c1f2007-05-21 22:25:44 +0000111 fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
112 if (fd < 0)
113 return;
114
115 memset(&devinfo, 0, sizeof(devinfo));
116 strcpy(devinfo.name, "hci0");
117 ret = ioctl(fd, HCIGETDEVINFO, (void *) &devinfo);
118 if (ret < 0)
119 goto out;
120
Arjan van der Vend8e3c272007-05-22 18:47:49 +0000121 if ( (devinfo.flags & 1) == 0 &&
122 access("/sys/module/hci_usb",F_OK)) /* interface down already */
Arjan van der Ven8ca6c1f2007-05-21 22:25:44 +0000123 goto out;
124
125 thisbytes += devinfo.stat.byte_rx;
126 thisbytes += devinfo.stat.byte_tx;
127
Arjan van der Ven3ffc3642007-09-29 19:00:23 +0000128 if (thisbytes != previous_bytes)
129 goto out;
130
131 /* now, also check for active connections */
132 file = popen("/usr/bin/hcitool con 2> /dev/null", "r");
133 if (file) {
134 char line[2048];
135 /* first line is standard header */
136 fgets(line,2048,file);
137 memset(line, 0, 2048);
138 fgets(line, 2047, file);
139 pclose(file);
140 if (strlen(line)>0)
141 goto out;
142 }
143
144 add_suggestion( _("Suggestion: Disable the unused bluetooth interface with the following command:\n"
Arjan van der Ven1e722612007-05-22 18:34:37 +0000145 " hciconfig hci0 down ; rmmod hci_usb\n"
Arjan van der Ven3037a452007-05-24 21:26:27 +0000146 "Bluetooth is a radio and consumes quite some power, and keeps USB busy as well.\n"), 40, 'B' , _(" B - Turn Bluetooth off "), turn_bluetooth_off);
Arjan van der Ven8ca6c1f2007-05-21 22:25:44 +0000147out:
Arjan van der Ven3ffc3642007-09-29 19:00:23 +0000148 previous_bytes = thisbytes;
Arjan van der Ven8ca6c1f2007-05-21 22:25:44 +0000149 close(fd);
150 return;
151}