aboutsummaryrefslogtreecommitdiff
path: root/src/tuning/tuningusb.cpp
blob: 3b7b2b16559637d00651a9402ad4385884fdb0ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright 2010, Intel Corporation
 *
 * This file is part of PowerTOP
 *
 * This program file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program in a file named COPYING; if not, write to the
 * Free Software Foundation, Inc,
 * 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 * or just google for it.
 *
 * Authors:
 *	Arjan van de Ven <arjan@linux.intel.com>
 */

#include "tuning.h"
#include "tunable.h"
#include "unistd.h"
#include "tuningusb.h"
#include <string.h>
#include <dirent.h>
#include <utility>
#include <iostream>
#include <fstream>
#include <limits.h>

#include "../lib.h"

usb_tunable::usb_tunable(const char *path, const char *name) : tunable("", 0.9, _("Good"), _("Bad"), _("Unknown"))
{
	ifstream file;
	char filename[PATH_MAX];
	char vendor[2048];
	char product[2048];
	string str1, str2;
	snprintf(usb_path, sizeof(usb_path), "%s/power/control", path);

	vendor[0] = 0;
	product[0] = 0;

	str1 = read_sysfs_string("%s/idVendor", path);
	str2 = read_sysfs_string("%s/idProduct", path);

	snprintf(desc, sizeof(desc), _("Autosuspend for unknown USB device %s (%s:%s)"), name, str1.c_str(), str2.c_str());

	snprintf(filename, sizeof(filename), "%s/manufacturer", path);
	file.open(filename, ios::in);
	if (file) {
		file.getline(vendor, 2047);
		if (strstr(vendor, "Linux "))
			vendor[0] = 0;
		file.close();
	};
	snprintf(filename, sizeof(filename), "%s/product", path);
	file.open(filename, ios::in);
	if (file) {
		file.getline(product, 2040);
		file.close();
	};
	if (strlen(vendor) && strlen(product))
		snprintf(desc, sizeof(desc), _("Autosuspend for USB device %s [%s]"), product, vendor);
	else if (strlen(product))
		snprintf(desc, sizeof(desc), _("Autosuspend for USB device %s [%s]"), product, name);
	else if (strlen(vendor))
		snprintf(desc, sizeof(desc), _("Autosuspend for USB device %s [%s]"), vendor, name);

	snprintf(toggle_good, sizeof(toggle_good), "echo 'auto' > '%s';", usb_path);
	snprintf(toggle_bad, sizeof(toggle_bad), "echo 'on' > '%s';", usb_path);
}

int usb_tunable::good_bad(void)
{
	string content;

	content = read_sysfs_string(usb_path);

	if (strcmp(content.c_str(), "auto") == 0)
		return TUNE_GOOD;

	return TUNE_BAD;
}

void usb_tunable::toggle(void)
{
	int good;
	good = good_bad();

	if (good == TUNE_GOOD) {
		write_sysfs(usb_path, "on");
		return;
	}

	write_sysfs(usb_path, "auto");
}

const char *usb_tunable::toggle_script(void)
{
	int good;
	good = good_bad();

	if (good == TUNE_GOOD) {
		return toggle_bad;
	}

	return toggle_good;
}

static void add_usb_callback(const char *d_name)
{
	class usb_tunable *usb;
	char filename[PATH_MAX];
	DIR *dir;

	snprintf(filename, sizeof(filename), "/sys/bus/usb/devices/%s/power/control", d_name);
	if (access(filename, R_OK) != 0)
		return;

	snprintf(filename, sizeof(filename), "/sys/bus/usb/devices/%s/power/active_duration", d_name);
	if (access(filename, R_OK)!=0)
		return;

	/* every interface of this device should support autosuspend */
	snprintf(filename, sizeof(filename), "/sys/bus/usb/devices/%s", d_name);
	if ((dir = opendir(filename))) {
		struct dirent *entry;
		while ((entry = readdir(dir))) {
			/* dirname: <busnum>-<devnum>...:<config num>-<interface num> */
			if (!isdigit(entry->d_name[0]))
				continue;
			snprintf(filename, sizeof(filename), "/sys/bus/usb/devices/%s/%s/supports_autosuspend", d_name, entry->d_name);
			if (access(filename, R_OK) == 0 && read_sysfs(filename) == 0)
				break;
		}
		closedir(dir);
		if (entry)
			return;
	}

	snprintf(filename, sizeof(filename), "/sys/bus/usb/devices/%s", d_name);
	usb = new class usb_tunable(filename, d_name);
	all_tunables.push_back(usb);
}

void add_usb_tunables(void)
{
	process_directory("/sys/bus/usb/devices/", add_usb_callback);
}