1746
1746
***************************************************************************
1747
* Read current USB device data.
1750
* @st_pwr_usb Structure where stats will be saved.
1751
* @usb_device File name for current USB device.
1754
* @st_pwr_usb Structure with statistics.
1755
***************************************************************************
1757
void read_usb_stats(struct stats_pwr_usb *st_pwr_usb, char *usb_device)
1761
char filename[MAX_PF_NAME];
1763
/* Get USB device bus number */
1764
sscanf(usb_device, "%u", &st_pwr_usb->bus_nr);
1766
/* Read USB device vendor ID */
1767
snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1768
SYSFS_USBDEV, usb_device, SYSFS_IDVENDOR);
1769
if ((fp = fopen(filename, "r")) != NULL) {
1771
&st_pwr_usb->vendor_id);
1775
/* Read USB device product ID */
1776
snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1777
SYSFS_USBDEV, usb_device, SYSFS_IDPRODUCT);
1778
if ((fp = fopen(filename, "r")) != NULL) {
1780
&st_pwr_usb->product_id);
1784
/* Read USB device max power consumption */
1785
snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1786
SYSFS_USBDEV, usb_device, SYSFS_BMAXPOWER);
1787
if ((fp = fopen(filename, "r")) != NULL) {
1789
&st_pwr_usb->bmaxpower);
1793
/* Read USB device manufacturer */
1794
snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1795
SYSFS_USBDEV, usb_device, SYSFS_MANUFACTURER);
1796
if ((fp = fopen(filename, "r")) != NULL) {
1797
fgets(st_pwr_usb->manufacturer, MAX_MANUF_LEN - 1, fp);
1799
if ((l = strlen(st_pwr_usb->manufacturer)) > 0) {
1800
/* Remove trailing CR */
1801
st_pwr_usb->manufacturer[l - 1] = '\0';
1805
/* Read USB device product */
1806
snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1807
SYSFS_USBDEV, usb_device, SYSFS_PRODUCT);
1808
if ((fp = fopen(filename, "r")) != NULL) {
1809
fgets(st_pwr_usb->product, MAX_PROD_LEN - 1, fp);
1811
if ((l = strlen(st_pwr_usb->product)) > 0) {
1812
/* Remove trailing CR */
1813
st_pwr_usb->product[l - 1] = '\0';
1819
***************************************************************************
1820
* Read USB devices statistics.
1823
* @st_pwr_usb Structure where stats will be saved.
1824
* @nbr Total number of USB devices.
1827
* @st_pwr_usb Structure with statistics.
1828
***************************************************************************
1830
void read_bus_usb_dev(struct stats_pwr_usb *st_pwr_usb, int nbr)
1834
struct stats_pwr_usb *st_pwr_usb_j;
1837
/* Open relevant /sys directory */
1838
if ((dir = opendir(SYSFS_USBDEV)) == NULL)
1841
/* Get current file entry */
1842
while ((drd = readdir(dir)) != NULL) {
1844
if (isdigit(drd->d_name[0]) && !strchr(drd->d_name, ':')) {
1846
/* Read current USB device data */
1847
st_pwr_usb_j = st_pwr_usb + j;
1848
read_usb_stats(st_pwr_usb_j, drd->d_name);
1856
/* Close directory */
1861
***************************************************************************
1747
1862
* Read machine uptime, independently of the number of processors.
2247
***************************************************************************
2248
* Count number of USB devices in /sys/bus/usb/devices.
2251
* Number of USB devices plugged into the system.
2252
* Don't count USB root hubs.
2253
* Return -1 if directory doesn't exist in sysfs.
2254
***************************************************************************
2256
int get_usb_nr(void)
2262
/* Open relevant /sys directory */
2263
if ((dir = opendir(SYSFS_USBDEV)) == NULL)
2266
/* Get current file entry */
2267
while ((drd = readdir(dir)) != NULL) {
2269
if (isdigit(drd->d_name[0]) && !strchr(drd->d_name, ':')) {
2274
/* Close directory */