blob: b63db40a5e2e40512e34f19b0415e78adc1c873c [file] [log] [blame]
Arjan van der Ven9823a122007-07-28 03:49:35 +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
33#include "powertop.h"
34
Arjan van der Vene3c78822008-03-27 04:57:39 +000035void activate_usb_autosuspend(void)
Arjan van der Ven9823a122007-07-28 03:49:35 +000036{
37 DIR *dir;
38 struct dirent *dirent;
39 FILE *file;
40 char filename[PATH_MAX];
41
42 dir = opendir("/sys/bus/usb/devices");
43 if (!dir)
44 return;
45
46 while ((dirent = readdir(dir))) {
47 if (dirent->d_name[0]=='.')
48 continue;
49 sprintf(filename, "/sys/bus/usb/devices/%s/power/autosuspend", dirent->d_name);
50 file = fopen(filename, "w");
51 if (!file)
52 continue;
Arjan van der Ven87ac5712008-03-27 03:22:48 +000053 fprintf(file, "0\n");
Arjan van der Ven9823a122007-07-28 03:49:35 +000054 fclose(file);
Arjan van der Veneeb2e172008-03-27 04:01:21 +000055 sprintf(filename, "/sys/bus/usb/devices/%s/power/level", dirent->d_name);
56 file = fopen(filename, "w");
57 if (!file)
58 continue;
59 fprintf(file, "auto\n");
60 fclose(file);
Arjan van der Ven9823a122007-07-28 03:49:35 +000061 }
62
63 closedir(dir);
Arjan van der Ven9823a122007-07-28 03:49:35 +000064}
65
66void suggest_usb_autosuspend(void)
67{
68 DIR *dir;
69 struct dirent *dirent;
70 FILE *file;
71 char filename[PATH_MAX];
72 char line[1024];
73 int need_hint = 0;
74
Arjan van der Ven9823a122007-07-28 03:49:35 +000075
76 dir = opendir("/sys/bus/usb/devices");
77 if (!dir)
78 return;
79
80 while ((dirent = readdir(dir))) {
81 if (dirent->d_name[0]=='.')
82 continue;
83 sprintf(filename, "/sys/bus/usb/devices/%s/power/autosuspend", dirent->d_name);
84 file = fopen(filename, "r");
85 if (!file)
86 continue;
87 memset(line, 0, 1024);
88 if (fgets(line, 1023,file)==NULL) {
89 fclose(file);
90 continue;
91 }
Arjan van der Ven87ac5712008-03-27 03:22:48 +000092 if (strtoll(line, NULL,10)<0)
Arjan van der Ven9823a122007-07-28 03:49:35 +000093 need_hint = 1;
94
95 fclose(file);
Arjan van der Veneeb2e172008-03-27 04:01:21 +000096
97
98 sprintf(filename, "/sys/bus/usb/devices/%s/power/level", dirent->d_name);
99 file = fopen(filename, "r");
100 if (!file)
101 continue;
102 memset(line, 0, 1024);
103 if (fgets(line, 1023,file)==NULL) {
104 fclose(file);
105 continue;
106 }
107 if (strstr(line, "on"))
108 need_hint = 1;
109
110 fclose(file);
111
112
Arjan van der Ven9823a122007-07-28 03:49:35 +0000113 }
114
115 closedir(dir);
116
117 if (need_hint) {
118 add_suggestion(_("Suggestion: Enable USB autosuspend by pressing the U key or adding \n"
Arjan van der Ven493d8b32007-07-28 15:18:16 +0000119 "usbcore.autosuspend=1 to the kernel command line in the grub config"
Arjan van der Ven9823a122007-07-28 03:49:35 +0000120 ),
121 45, 'U', _(" U - Enable USB suspend "), activate_usb_autosuspend);
122 }
123}
Arjan van der Vena332b9e2008-03-27 03:40:55 +0000124
125