2014-09-20 04:23:18 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include "apm.h"
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "sonypi.h"
|
|
|
|
|
|
|
|
signed int spicfd = -1;
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
int sonypi_supported(void)
|
|
|
|
{
|
|
|
|
spicfd = open("/dev/sonypi", O_RDWR);
|
|
|
|
if (spicfd == -1)
|
2014-09-20 04:23:18 +00:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
inline int sonypi_ioctl(int ioctlno, void *param)
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
if (ioctl(spicfd, ioctlno, param) < 0)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read battery info from sonypi device and shove it into an apm_info
|
|
|
|
* struct. */
|
2014-10-05 15:30:02 +00:00
|
|
|
int sonypi_read(apm_info *info)
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
__u8 batflags;
|
|
|
|
__u16 cap, rem;
|
|
|
|
int havebatt = 0;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
info->using_minutes = info->battery_flags = 0;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
if (!sonypi_ioctl(SONYPI_IOCGBATFLAGS, &batflags))
|
2014-09-20 04:23:18 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
info->ac_line_status = (batflags & SONYPI_BFLAGS_AC) != 0;
|
|
|
|
if (batflags & SONYPI_BFLAGS_B1) {
|
2014-10-05 15:30:02 +00:00
|
|
|
if (!sonypi_ioctl(SONYPI_IOCGBAT1CAP, &cap))
|
2014-09-20 04:23:18 +00:00
|
|
|
return 1;
|
2014-10-05 15:30:02 +00:00
|
|
|
if (!sonypi_ioctl(SONYPI_IOCGBAT1REM, &rem))
|
2014-09-20 04:23:18 +00:00
|
|
|
return 1;
|
|
|
|
havebatt = 1;
|
2014-10-05 15:30:02 +00:00
|
|
|
} else if (batflags & SONYPI_BFLAGS_B2) {
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Not quite right, if there is a second battery I should
|
|
|
|
* probably merge the two somehow.. */
|
2014-10-05 15:30:02 +00:00
|
|
|
if (!sonypi_ioctl(SONYPI_IOCGBAT2CAP, &cap))
|
2014-09-20 04:23:18 +00:00
|
|
|
return 1;
|
2014-10-05 15:30:02 +00:00
|
|
|
if (!sonypi_ioctl(SONYPI_IOCGBAT2REM, &rem))
|
2014-09-20 04:23:18 +00:00
|
|
|
return 1;
|
|
|
|
havebatt = 1;
|
2014-10-05 15:30:02 +00:00
|
|
|
} else {
|
2014-09-20 04:23:18 +00:00
|
|
|
info->battery_percentage = 0;
|
|
|
|
info->battery_status = BATTERY_STATUS_ABSENT;
|
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
if (havebatt) {
|
|
|
|
info->battery_percentage = 100 * rem / cap;
|
|
|
|
/* Guess at whether the battery is charging. */
|
|
|
|
if (info->battery_percentage < 99 && info->ac_line_status == 1) {
|
|
|
|
info->battery_flags = info->battery_flags | BATTERY_FLAGS_CHARGING;
|
|
|
|
info->battery_status = BATTERY_STATUS_CHARGING;
|
|
|
|
}
|
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Sadly, there is no way to estimate this. */
|
|
|
|
info->battery_time = 0;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|