2 * Copyright 2010, Intel Corporation
4 * This file is part of PowerTOP
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.
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
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 * or just google for it.
23 * Arjan van de Ven <arjan@linux.intel.com>
30 #include <sys/types.h>
33 #include "../parameters/parameters.h"
38 usbdevice::usbdevice(const char *_name, const char *path, const char *devid): device()
45 strcpy(sysfs_path, path);
46 register_sysfs_path(sysfs_path);
48 strcpy(devname, devid);
49 sprintf(humanname, _("USB device: %s"), pretty_print(devid, vendor, 4096));
55 index = get_param_index(devname);
56 r_index = get_result_index(name);
61 /* root ports and hubs should count as 0 power ... their activity is derived */
62 sprintf(filename, "%s/bDeviceClass", path);
63 file.open(filename, ios::in);
75 sprintf(filename, "%s/manufacturer", path);
76 file.open(filename, ios::in);
78 file.getline(vendor, 2047);
79 if (strstr(vendor, "Linux "))
83 sprintf(filename, "%s/product", path);
84 file.open(filename, ios::in);
86 file.getline(product, 2040);
89 if (strlen(vendor) && strlen(product))
90 sprintf(humanname, _("USB device: %s (%s)"), product, vendor);
91 else if (strlen(product))
92 sprintf(humanname, _("USB device: %s"), product);
93 else if (strlen(vendor))
94 sprintf(humanname, _("USB device: %s"), vendor);
99 void usbdevice::start_measurement(void)
106 connected_before = 0;
109 sprintf(fullpath, "%s/power/active_duration", sysfs_path);
110 file.open(fullpath, ios::in);
112 file >> active_before;
116 sprintf(fullpath, "%s/power/connected_duration", sysfs_path);
117 file.open(fullpath, ios::in);
119 file >> connected_before;
124 void usbdevice::end_measurement(void)
129 sprintf(fullpath, "%s/power/active_duration", sysfs_path);
130 file.open(fullpath, ios::in);
132 file >> active_after;
136 sprintf(fullpath, "%s/power/connected_duration", sysfs_path);
137 file.open(fullpath, ios::in);
139 file >> connected_after;
142 report_utilization(name, utilization());
146 double usbdevice::utilization(void) /* percentage */
149 d = 100.0 * (active_after - active_before) / (0.01 + connected_after - connected_before);
157 const char * usbdevice::device_name(void)
162 const char * usbdevice::human_name(void)
168 double usbdevice::power_usage(struct result_bundle *result, struct parameter_bundle *bundle)
174 if (rootport || !cached_valid)
179 factor = get_parameter_value(index, bundle);
180 util = get_result_value(r_index, result);
182 power += util * factor / 100.0;
187 static void create_all_usb_devices_callback(const char *d_name)
191 class usbdevice *usb;
192 char device_name[4096];
193 char vendorid[64], devid[64];
194 char devid_name[4096];
196 sprintf(filename, "/sys/bus/usb/devices/%s", d_name);
197 sprintf(device_name, "%s/power/active_duration", filename);
198 if (access(device_name, R_OK) != 0)
201 sprintf(device_name, "%s/idVendor", filename);
202 file.open(device_name, ios::in);
204 file.getline(vendorid, 64);
206 sprintf(device_name, "%s/idProduct", filename);
207 file.open(device_name, ios::in);
209 file.getline(devid, 64);
212 sprintf(devid_name, "usb-device-%s-%s", vendorid, devid);
213 sprintf(device_name, "usb-device-%s-%s-%s", d_name, vendorid, devid);
214 if (result_device_exists(device_name))
217 usb = new class usbdevice(device_name, filename, devid_name);
218 all_devices.push_back(usb);
219 register_parameter(devid_name, 0.1);
222 void create_all_usb_devices(void)
224 process_directory("/sys/bus/usb/devices/", create_all_usb_devices_callback);