• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 #include "powertop.h"
34 
activate_usb_autosuspend(void)35 void activate_usb_autosuspend(void)
36 {
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;
53 		fprintf(file, "0\n");
54 		fclose(file);
55 		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);
61 	}
62 
63 	closedir(dir);
64 }
65 
suggest_usb_autosuspend(void)66 void 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 
75 
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 		}
92 		if (strtoll(line, NULL,10)<0)
93 			need_hint = 1;
94 
95 		fclose(file);
96 
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 
113 	}
114 
115 	closedir(dir);
116 
117 	if (need_hint) {
118 		add_suggestion(_("Suggestion: Enable USB autosuspend by pressing the U key or adding \n"
119 				 "usbcore.autosuspend=1 to the kernel command line in the grub config"
120 				 ),
121 				45, 'U', _(" U - Enable USB suspend "), activate_usb_autosuspend);
122 	}
123 }
124 
125 
126