Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  battery.c - ACPI Battery Driver (Revision: 2.0)
0004  *
0005  *  Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
0006  *  Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
0007  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
0008  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
0009  */
0010 
0011 #define pr_fmt(fmt) "ACPI: battery: " fmt
0012 
0013 #include <linux/async.h>
0014 #include <linux/delay.h>
0015 #include <linux/dmi.h>
0016 #include <linux/jiffies.h>
0017 #include <linux/kernel.h>
0018 #include <linux/list.h>
0019 #include <linux/module.h>
0020 #include <linux/mutex.h>
0021 #include <linux/slab.h>
0022 #include <linux/suspend.h>
0023 #include <linux/types.h>
0024 
0025 #include <asm/unaligned.h>
0026 
0027 #include <linux/acpi.h>
0028 #include <linux/power_supply.h>
0029 
0030 #include <acpi/battery.h>
0031 
0032 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
0033 #define ACPI_BATTERY_CAPACITY_VALID(capacity) \
0034     ((capacity) != 0 && (capacity) != ACPI_BATTERY_VALUE_UNKNOWN)
0035 
0036 #define ACPI_BATTERY_DEVICE_NAME    "Battery"
0037 
0038 /* Battery power unit: 0 means mW, 1 means mA */
0039 #define ACPI_BATTERY_POWER_UNIT_MA  1
0040 
0041 #define ACPI_BATTERY_STATE_DISCHARGING  0x1
0042 #define ACPI_BATTERY_STATE_CHARGING 0x2
0043 #define ACPI_BATTERY_STATE_CRITICAL 0x4
0044 
0045 MODULE_AUTHOR("Paul Diefenbaugh");
0046 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
0047 MODULE_DESCRIPTION("ACPI Battery Driver");
0048 MODULE_LICENSE("GPL");
0049 
0050 static async_cookie_t async_cookie;
0051 static bool battery_driver_registered;
0052 static int battery_bix_broken_package;
0053 static int battery_notification_delay_ms;
0054 static int battery_ac_is_broken;
0055 static unsigned int cache_time = 1000;
0056 module_param(cache_time, uint, 0644);
0057 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
0058 
0059 static const struct acpi_device_id battery_device_ids[] = {
0060     {"PNP0C0A", 0},
0061 
0062     /* Microsoft Surface Go 3 */
0063     {"MSHW0146", 0},
0064 
0065     {"", 0},
0066 };
0067 
0068 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
0069 
0070 enum {
0071     ACPI_BATTERY_ALARM_PRESENT,
0072     ACPI_BATTERY_XINFO_PRESENT,
0073     ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
0074     /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
0075      * switches between mWh and mAh depending on whether the system
0076      * is running on battery or not.  When mAh is the unit, most
0077      * reported values are incorrect and need to be adjusted by
0078      * 10000/design_voltage.  Verified on x201, t410, t410s, and x220.
0079      * Pre-2010 and 2012 models appear to always report in mWh and
0080      * are thus unaffected (tested with t42, t61, t500, x200, x300,
0081      * and x230).  Also, in mid-2012 Lenovo issued a BIOS update for
0082      *  the 2011 models that fixes the issue (tested on x220 with a
0083      * post-1.29 BIOS), but as of Nov. 2012, no such update is
0084      * available for the 2010 models.
0085      */
0086     ACPI_BATTERY_QUIRK_THINKPAD_MAH,
0087     /* for batteries reporting current capacity with design capacity
0088      * on a full charge, but showing degradation in full charge cap.
0089      */
0090     ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE,
0091 };
0092 
0093 struct acpi_battery {
0094     struct mutex lock;
0095     struct mutex sysfs_lock;
0096     struct power_supply *bat;
0097     struct power_supply_desc bat_desc;
0098     struct acpi_device *device;
0099     struct notifier_block pm_nb;
0100     struct list_head list;
0101     unsigned long update_time;
0102     int revision;
0103     int rate_now;
0104     int capacity_now;
0105     int voltage_now;
0106     int design_capacity;
0107     int full_charge_capacity;
0108     int technology;
0109     int design_voltage;
0110     int design_capacity_warning;
0111     int design_capacity_low;
0112     int cycle_count;
0113     int measurement_accuracy;
0114     int max_sampling_time;
0115     int min_sampling_time;
0116     int max_averaging_interval;
0117     int min_averaging_interval;
0118     int capacity_granularity_1;
0119     int capacity_granularity_2;
0120     int alarm;
0121     char model_number[32];
0122     char serial_number[32];
0123     char type[32];
0124     char oem_info[32];
0125     int state;
0126     int power_unit;
0127     unsigned long flags;
0128 };
0129 
0130 #define to_acpi_battery(x) power_supply_get_drvdata(x)
0131 
0132 static inline int acpi_battery_present(struct acpi_battery *battery)
0133 {
0134     return battery->device->status.battery_present;
0135 }
0136 
0137 static int acpi_battery_technology(struct acpi_battery *battery)
0138 {
0139     if (!strcasecmp("NiCd", battery->type))
0140         return POWER_SUPPLY_TECHNOLOGY_NiCd;
0141     if (!strcasecmp("NiMH", battery->type))
0142         return POWER_SUPPLY_TECHNOLOGY_NiMH;
0143     if (!strcasecmp("LION", battery->type))
0144         return POWER_SUPPLY_TECHNOLOGY_LION;
0145     if (!strncasecmp("LI-ION", battery->type, 6))
0146         return POWER_SUPPLY_TECHNOLOGY_LION;
0147     if (!strcasecmp("LiP", battery->type))
0148         return POWER_SUPPLY_TECHNOLOGY_LIPO;
0149     return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
0150 }
0151 
0152 static int acpi_battery_get_state(struct acpi_battery *battery);
0153 
0154 static int acpi_battery_is_charged(struct acpi_battery *battery)
0155 {
0156     /* charging, discharging or critical low */
0157     if (battery->state != 0)
0158         return 0;
0159 
0160     /* battery not reporting charge */
0161     if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
0162         battery->capacity_now == 0)
0163         return 0;
0164 
0165     /* good batteries update full_charge as the batteries degrade */
0166     if (battery->full_charge_capacity == battery->capacity_now)
0167         return 1;
0168 
0169     /* fallback to using design values for broken batteries */
0170     if (battery->design_capacity <= battery->capacity_now)
0171         return 1;
0172 
0173     /* we don't do any sort of metric based on percentages */
0174     return 0;
0175 }
0176 
0177 static bool acpi_battery_is_degraded(struct acpi_battery *battery)
0178 {
0179     return ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
0180         ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity) &&
0181         battery->full_charge_capacity < battery->design_capacity;
0182 }
0183 
0184 static int acpi_battery_handle_discharging(struct acpi_battery *battery)
0185 {
0186     /*
0187      * Some devices wrongly report discharging if the battery's charge level
0188      * was above the device's start charging threshold atm the AC adapter
0189      * was plugged in and the device thus did not start a new charge cycle.
0190      */
0191     if ((battery_ac_is_broken || power_supply_is_system_supplied()) &&
0192         battery->rate_now == 0)
0193         return POWER_SUPPLY_STATUS_NOT_CHARGING;
0194 
0195     return POWER_SUPPLY_STATUS_DISCHARGING;
0196 }
0197 
0198 static int acpi_battery_get_property(struct power_supply *psy,
0199                      enum power_supply_property psp,
0200                      union power_supply_propval *val)
0201 {
0202     int full_capacity = ACPI_BATTERY_VALUE_UNKNOWN, ret = 0;
0203     struct acpi_battery *battery = to_acpi_battery(psy);
0204 
0205     if (acpi_battery_present(battery)) {
0206         /* run battery update only if it is present */
0207         acpi_battery_get_state(battery);
0208     } else if (psp != POWER_SUPPLY_PROP_PRESENT)
0209         return -ENODEV;
0210     switch (psp) {
0211     case POWER_SUPPLY_PROP_STATUS:
0212         if (battery->state & ACPI_BATTERY_STATE_DISCHARGING)
0213             val->intval = acpi_battery_handle_discharging(battery);
0214         else if (battery->state & ACPI_BATTERY_STATE_CHARGING)
0215             val->intval = POWER_SUPPLY_STATUS_CHARGING;
0216         else if (acpi_battery_is_charged(battery))
0217             val->intval = POWER_SUPPLY_STATUS_FULL;
0218         else
0219             val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
0220         break;
0221     case POWER_SUPPLY_PROP_PRESENT:
0222         val->intval = acpi_battery_present(battery);
0223         break;
0224     case POWER_SUPPLY_PROP_TECHNOLOGY:
0225         val->intval = acpi_battery_technology(battery);
0226         break;
0227     case POWER_SUPPLY_PROP_CYCLE_COUNT:
0228         val->intval = battery->cycle_count;
0229         break;
0230     case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
0231         if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
0232             ret = -ENODEV;
0233         else
0234             val->intval = battery->design_voltage * 1000;
0235         break;
0236     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0237         if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
0238             ret = -ENODEV;
0239         else
0240             val->intval = battery->voltage_now * 1000;
0241         break;
0242     case POWER_SUPPLY_PROP_CURRENT_NOW:
0243     case POWER_SUPPLY_PROP_POWER_NOW:
0244         if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
0245             ret = -ENODEV;
0246         else
0247             val->intval = battery->rate_now * 1000;
0248         break;
0249     case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
0250     case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
0251         if (!ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
0252             ret = -ENODEV;
0253         else
0254             val->intval = battery->design_capacity * 1000;
0255         break;
0256     case POWER_SUPPLY_PROP_CHARGE_FULL:
0257     case POWER_SUPPLY_PROP_ENERGY_FULL:
0258         if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
0259             ret = -ENODEV;
0260         else
0261             val->intval = battery->full_charge_capacity * 1000;
0262         break;
0263     case POWER_SUPPLY_PROP_CHARGE_NOW:
0264     case POWER_SUPPLY_PROP_ENERGY_NOW:
0265         if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
0266             ret = -ENODEV;
0267         else
0268             val->intval = battery->capacity_now * 1000;
0269         break;
0270     case POWER_SUPPLY_PROP_CAPACITY:
0271         if (ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
0272             full_capacity = battery->full_charge_capacity;
0273         else if (ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
0274             full_capacity = battery->design_capacity;
0275 
0276         if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
0277             full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
0278             ret = -ENODEV;
0279         else
0280             val->intval = battery->capacity_now * 100/
0281                     full_capacity;
0282         break;
0283     case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
0284         if (battery->state & ACPI_BATTERY_STATE_CRITICAL)
0285             val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
0286         else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
0287             (battery->capacity_now <= battery->alarm))
0288             val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
0289         else if (acpi_battery_is_charged(battery))
0290             val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
0291         else
0292             val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
0293         break;
0294     case POWER_SUPPLY_PROP_MODEL_NAME:
0295         val->strval = battery->model_number;
0296         break;
0297     case POWER_SUPPLY_PROP_MANUFACTURER:
0298         val->strval = battery->oem_info;
0299         break;
0300     case POWER_SUPPLY_PROP_SERIAL_NUMBER:
0301         val->strval = battery->serial_number;
0302         break;
0303     default:
0304         ret = -EINVAL;
0305     }
0306     return ret;
0307 }
0308 
0309 static enum power_supply_property charge_battery_props[] = {
0310     POWER_SUPPLY_PROP_STATUS,
0311     POWER_SUPPLY_PROP_PRESENT,
0312     POWER_SUPPLY_PROP_TECHNOLOGY,
0313     POWER_SUPPLY_PROP_CYCLE_COUNT,
0314     POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
0315     POWER_SUPPLY_PROP_VOLTAGE_NOW,
0316     POWER_SUPPLY_PROP_CURRENT_NOW,
0317     POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
0318     POWER_SUPPLY_PROP_CHARGE_FULL,
0319     POWER_SUPPLY_PROP_CHARGE_NOW,
0320     POWER_SUPPLY_PROP_CAPACITY,
0321     POWER_SUPPLY_PROP_CAPACITY_LEVEL,
0322     POWER_SUPPLY_PROP_MODEL_NAME,
0323     POWER_SUPPLY_PROP_MANUFACTURER,
0324     POWER_SUPPLY_PROP_SERIAL_NUMBER,
0325 };
0326 
0327 static enum power_supply_property charge_battery_full_cap_broken_props[] = {
0328     POWER_SUPPLY_PROP_STATUS,
0329     POWER_SUPPLY_PROP_PRESENT,
0330     POWER_SUPPLY_PROP_TECHNOLOGY,
0331     POWER_SUPPLY_PROP_CYCLE_COUNT,
0332     POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
0333     POWER_SUPPLY_PROP_VOLTAGE_NOW,
0334     POWER_SUPPLY_PROP_CURRENT_NOW,
0335     POWER_SUPPLY_PROP_CHARGE_NOW,
0336     POWER_SUPPLY_PROP_MODEL_NAME,
0337     POWER_SUPPLY_PROP_MANUFACTURER,
0338     POWER_SUPPLY_PROP_SERIAL_NUMBER,
0339 };
0340 
0341 static enum power_supply_property energy_battery_props[] = {
0342     POWER_SUPPLY_PROP_STATUS,
0343     POWER_SUPPLY_PROP_PRESENT,
0344     POWER_SUPPLY_PROP_TECHNOLOGY,
0345     POWER_SUPPLY_PROP_CYCLE_COUNT,
0346     POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
0347     POWER_SUPPLY_PROP_VOLTAGE_NOW,
0348     POWER_SUPPLY_PROP_POWER_NOW,
0349     POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
0350     POWER_SUPPLY_PROP_ENERGY_FULL,
0351     POWER_SUPPLY_PROP_ENERGY_NOW,
0352     POWER_SUPPLY_PROP_CAPACITY,
0353     POWER_SUPPLY_PROP_CAPACITY_LEVEL,
0354     POWER_SUPPLY_PROP_MODEL_NAME,
0355     POWER_SUPPLY_PROP_MANUFACTURER,
0356     POWER_SUPPLY_PROP_SERIAL_NUMBER,
0357 };
0358 
0359 static enum power_supply_property energy_battery_full_cap_broken_props[] = {
0360     POWER_SUPPLY_PROP_STATUS,
0361     POWER_SUPPLY_PROP_PRESENT,
0362     POWER_SUPPLY_PROP_TECHNOLOGY,
0363     POWER_SUPPLY_PROP_CYCLE_COUNT,
0364     POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
0365     POWER_SUPPLY_PROP_VOLTAGE_NOW,
0366     POWER_SUPPLY_PROP_POWER_NOW,
0367     POWER_SUPPLY_PROP_ENERGY_NOW,
0368     POWER_SUPPLY_PROP_MODEL_NAME,
0369     POWER_SUPPLY_PROP_MANUFACTURER,
0370     POWER_SUPPLY_PROP_SERIAL_NUMBER,
0371 };
0372 
0373 /* Battery Management */
0374 struct acpi_offsets {
0375     size_t offset;      /* offset inside struct acpi_sbs_battery */
0376     u8 mode;        /* int or string? */
0377 };
0378 
0379 static const struct acpi_offsets state_offsets[] = {
0380     {offsetof(struct acpi_battery, state), 0},
0381     {offsetof(struct acpi_battery, rate_now), 0},
0382     {offsetof(struct acpi_battery, capacity_now), 0},
0383     {offsetof(struct acpi_battery, voltage_now), 0},
0384 };
0385 
0386 static const struct acpi_offsets info_offsets[] = {
0387     {offsetof(struct acpi_battery, power_unit), 0},
0388     {offsetof(struct acpi_battery, design_capacity), 0},
0389     {offsetof(struct acpi_battery, full_charge_capacity), 0},
0390     {offsetof(struct acpi_battery, technology), 0},
0391     {offsetof(struct acpi_battery, design_voltage), 0},
0392     {offsetof(struct acpi_battery, design_capacity_warning), 0},
0393     {offsetof(struct acpi_battery, design_capacity_low), 0},
0394     {offsetof(struct acpi_battery, capacity_granularity_1), 0},
0395     {offsetof(struct acpi_battery, capacity_granularity_2), 0},
0396     {offsetof(struct acpi_battery, model_number), 1},
0397     {offsetof(struct acpi_battery, serial_number), 1},
0398     {offsetof(struct acpi_battery, type), 1},
0399     {offsetof(struct acpi_battery, oem_info), 1},
0400 };
0401 
0402 static const struct acpi_offsets extended_info_offsets[] = {
0403     {offsetof(struct acpi_battery, revision), 0},
0404     {offsetof(struct acpi_battery, power_unit), 0},
0405     {offsetof(struct acpi_battery, design_capacity), 0},
0406     {offsetof(struct acpi_battery, full_charge_capacity), 0},
0407     {offsetof(struct acpi_battery, technology), 0},
0408     {offsetof(struct acpi_battery, design_voltage), 0},
0409     {offsetof(struct acpi_battery, design_capacity_warning), 0},
0410     {offsetof(struct acpi_battery, design_capacity_low), 0},
0411     {offsetof(struct acpi_battery, cycle_count), 0},
0412     {offsetof(struct acpi_battery, measurement_accuracy), 0},
0413     {offsetof(struct acpi_battery, max_sampling_time), 0},
0414     {offsetof(struct acpi_battery, min_sampling_time), 0},
0415     {offsetof(struct acpi_battery, max_averaging_interval), 0},
0416     {offsetof(struct acpi_battery, min_averaging_interval), 0},
0417     {offsetof(struct acpi_battery, capacity_granularity_1), 0},
0418     {offsetof(struct acpi_battery, capacity_granularity_2), 0},
0419     {offsetof(struct acpi_battery, model_number), 1},
0420     {offsetof(struct acpi_battery, serial_number), 1},
0421     {offsetof(struct acpi_battery, type), 1},
0422     {offsetof(struct acpi_battery, oem_info), 1},
0423 };
0424 
0425 static int extract_package(struct acpi_battery *battery,
0426                union acpi_object *package,
0427                const struct acpi_offsets *offsets, int num)
0428 {
0429     int i;
0430     union acpi_object *element;
0431 
0432     if (package->type != ACPI_TYPE_PACKAGE)
0433         return -EFAULT;
0434     for (i = 0; i < num; ++i) {
0435         if (package->package.count <= i)
0436             return -EFAULT;
0437         element = &package->package.elements[i];
0438         if (offsets[i].mode) {
0439             u8 *ptr = (u8 *)battery + offsets[i].offset;
0440 
0441             if (element->type == ACPI_TYPE_STRING ||
0442                 element->type == ACPI_TYPE_BUFFER)
0443                 strncpy(ptr, element->string.pointer, 32);
0444             else if (element->type == ACPI_TYPE_INTEGER) {
0445                 strncpy(ptr, (u8 *)&element->integer.value,
0446                     sizeof(u64));
0447                 ptr[sizeof(u64)] = 0;
0448             } else
0449                 *ptr = 0; /* don't have value */
0450         } else {
0451             int *x = (int *)((u8 *)battery + offsets[i].offset);
0452             *x = (element->type == ACPI_TYPE_INTEGER) ?
0453                 element->integer.value : -1;
0454         }
0455     }
0456     return 0;
0457 }
0458 
0459 static int acpi_battery_get_status(struct acpi_battery *battery)
0460 {
0461     if (acpi_bus_get_status(battery->device)) {
0462         acpi_handle_info(battery->device->handle,
0463                  "_STA evaluation failed\n");
0464         return -ENODEV;
0465     }
0466     return 0;
0467 }
0468 
0469 
0470 static int extract_battery_info(const int use_bix,
0471              struct acpi_battery *battery,
0472              const struct acpi_buffer *buffer)
0473 {
0474     int result = -EFAULT;
0475 
0476     if (use_bix && battery_bix_broken_package)
0477         result = extract_package(battery, buffer->pointer,
0478                 extended_info_offsets + 1,
0479                 ARRAY_SIZE(extended_info_offsets) - 1);
0480     else if (use_bix)
0481         result = extract_package(battery, buffer->pointer,
0482                 extended_info_offsets,
0483                 ARRAY_SIZE(extended_info_offsets));
0484     else
0485         result = extract_package(battery, buffer->pointer,
0486                 info_offsets, ARRAY_SIZE(info_offsets));
0487     if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
0488         battery->full_charge_capacity = battery->design_capacity;
0489     if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
0490         battery->power_unit && battery->design_voltage) {
0491         battery->design_capacity = battery->design_capacity *
0492             10000 / battery->design_voltage;
0493         battery->full_charge_capacity = battery->full_charge_capacity *
0494             10000 / battery->design_voltage;
0495         battery->design_capacity_warning =
0496             battery->design_capacity_warning *
0497             10000 / battery->design_voltage;
0498         /* Curiously, design_capacity_low, unlike the rest of them,
0499          *  is correct.
0500          */
0501         /* capacity_granularity_* equal 1 on the systems tested, so
0502          * it's impossible to tell if they would need an adjustment
0503          * or not if their values were higher.
0504          */
0505     }
0506     if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
0507         battery->capacity_now > battery->full_charge_capacity)
0508         battery->capacity_now = battery->full_charge_capacity;
0509 
0510     return result;
0511 }
0512 
0513 static int acpi_battery_get_info(struct acpi_battery *battery)
0514 {
0515     const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
0516     int use_bix;
0517     int result = -ENODEV;
0518 
0519     if (!acpi_battery_present(battery))
0520         return 0;
0521 
0522 
0523     for (use_bix = xinfo ? 1 : 0; use_bix >= 0; use_bix--) {
0524         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0525         acpi_status status = AE_ERROR;
0526 
0527         mutex_lock(&battery->lock);
0528         status = acpi_evaluate_object(battery->device->handle,
0529                           use_bix ? "_BIX":"_BIF",
0530                           NULL, &buffer);
0531         mutex_unlock(&battery->lock);
0532 
0533         if (ACPI_FAILURE(status)) {
0534             acpi_handle_info(battery->device->handle,
0535                      "%s evaluation failed: %s\n",
0536                      use_bix ? "_BIX":"_BIF",
0537                      acpi_format_exception(status));
0538         } else {
0539             result = extract_battery_info(use_bix,
0540                               battery,
0541                               &buffer);
0542 
0543             kfree(buffer.pointer);
0544             break;
0545         }
0546     }
0547 
0548     if (!result && !use_bix && xinfo)
0549         pr_warn(FW_BUG "The _BIX method is broken, using _BIF.\n");
0550 
0551     return result;
0552 }
0553 
0554 static int acpi_battery_get_state(struct acpi_battery *battery)
0555 {
0556     int result = 0;
0557     acpi_status status = 0;
0558     struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0559 
0560     if (!acpi_battery_present(battery))
0561         return 0;
0562 
0563     if (battery->update_time &&
0564         time_before(jiffies, battery->update_time +
0565             msecs_to_jiffies(cache_time)))
0566         return 0;
0567 
0568     mutex_lock(&battery->lock);
0569     status = acpi_evaluate_object(battery->device->handle, "_BST",
0570                       NULL, &buffer);
0571     mutex_unlock(&battery->lock);
0572 
0573     if (ACPI_FAILURE(status)) {
0574         acpi_handle_info(battery->device->handle,
0575                  "_BST evaluation failed: %s",
0576                  acpi_format_exception(status));
0577         return -ENODEV;
0578     }
0579 
0580     result = extract_package(battery, buffer.pointer,
0581                  state_offsets, ARRAY_SIZE(state_offsets));
0582     battery->update_time = jiffies;
0583     kfree(buffer.pointer);
0584 
0585     /* For buggy DSDTs that report negative 16-bit values for either
0586      * charging or discharging current and/or report 0 as 65536
0587      * due to bad math.
0588      */
0589     if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
0590         battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
0591         (s16)(battery->rate_now) < 0) {
0592         battery->rate_now = abs((s16)battery->rate_now);
0593         pr_warn_once(FW_BUG "(dis)charge rate invalid.\n");
0594     }
0595 
0596     if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
0597         && battery->capacity_now >= 0 && battery->capacity_now <= 100)
0598         battery->capacity_now = (battery->capacity_now *
0599                 battery->full_charge_capacity) / 100;
0600     if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
0601         battery->power_unit && battery->design_voltage) {
0602         battery->capacity_now = battery->capacity_now *
0603             10000 / battery->design_voltage;
0604     }
0605     if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
0606         battery->capacity_now > battery->full_charge_capacity)
0607         battery->capacity_now = battery->full_charge_capacity;
0608 
0609     return result;
0610 }
0611 
0612 static int acpi_battery_set_alarm(struct acpi_battery *battery)
0613 {
0614     acpi_status status = 0;
0615 
0616     if (!acpi_battery_present(battery) ||
0617         !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
0618         return -ENODEV;
0619 
0620     mutex_lock(&battery->lock);
0621     status = acpi_execute_simple_method(battery->device->handle, "_BTP",
0622                         battery->alarm);
0623     mutex_unlock(&battery->lock);
0624 
0625     if (ACPI_FAILURE(status))
0626         return -ENODEV;
0627 
0628     acpi_handle_debug(battery->device->handle, "Alarm set to %d\n",
0629               battery->alarm);
0630 
0631     return 0;
0632 }
0633 
0634 static int acpi_battery_init_alarm(struct acpi_battery *battery)
0635 {
0636     /* See if alarms are supported, and if so, set default */
0637     if (!acpi_has_method(battery->device->handle, "_BTP")) {
0638         clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
0639         return 0;
0640     }
0641     set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
0642     if (!battery->alarm)
0643         battery->alarm = battery->design_capacity_warning;
0644     return acpi_battery_set_alarm(battery);
0645 }
0646 
0647 static ssize_t acpi_battery_alarm_show(struct device *dev,
0648                     struct device_attribute *attr,
0649                     char *buf)
0650 {
0651     struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
0652 
0653     return sprintf(buf, "%d\n", battery->alarm * 1000);
0654 }
0655 
0656 static ssize_t acpi_battery_alarm_store(struct device *dev,
0657                     struct device_attribute *attr,
0658                     const char *buf, size_t count)
0659 {
0660     unsigned long x;
0661     struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
0662 
0663     if (sscanf(buf, "%lu\n", &x) == 1)
0664         battery->alarm = x/1000;
0665     if (acpi_battery_present(battery))
0666         acpi_battery_set_alarm(battery);
0667     return count;
0668 }
0669 
0670 static const struct device_attribute alarm_attr = {
0671     .attr = {.name = "alarm", .mode = 0644},
0672     .show = acpi_battery_alarm_show,
0673     .store = acpi_battery_alarm_store,
0674 };
0675 
0676 /*
0677  * The Battery Hooking API
0678  *
0679  * This API is used inside other drivers that need to expose
0680  * platform-specific behaviour within the generic driver in a
0681  * generic way.
0682  *
0683  */
0684 
0685 static LIST_HEAD(acpi_battery_list);
0686 static LIST_HEAD(battery_hook_list);
0687 static DEFINE_MUTEX(hook_mutex);
0688 
0689 static void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock)
0690 {
0691     struct acpi_battery *battery;
0692     /*
0693      * In order to remove a hook, we first need to
0694      * de-register all the batteries that are registered.
0695      */
0696     if (lock)
0697         mutex_lock(&hook_mutex);
0698     list_for_each_entry(battery, &acpi_battery_list, list) {
0699         hook->remove_battery(battery->bat);
0700     }
0701     list_del(&hook->list);
0702     if (lock)
0703         mutex_unlock(&hook_mutex);
0704     pr_info("extension unregistered: %s\n", hook->name);
0705 }
0706 
0707 void battery_hook_unregister(struct acpi_battery_hook *hook)
0708 {
0709     __battery_hook_unregister(hook, 1);
0710 }
0711 EXPORT_SYMBOL_GPL(battery_hook_unregister);
0712 
0713 void battery_hook_register(struct acpi_battery_hook *hook)
0714 {
0715     struct acpi_battery *battery;
0716 
0717     mutex_lock(&hook_mutex);
0718     INIT_LIST_HEAD(&hook->list);
0719     list_add(&hook->list, &battery_hook_list);
0720     /*
0721      * Now that the driver is registered, we need
0722      * to notify the hook that a battery is available
0723      * for each battery, so that the driver may add
0724      * its attributes.
0725      */
0726     list_for_each_entry(battery, &acpi_battery_list, list) {
0727         if (hook->add_battery(battery->bat)) {
0728             /*
0729              * If a add-battery returns non-zero,
0730              * the registration of the extension has failed,
0731              * and we will not add it to the list of loaded
0732              * hooks.
0733              */
0734             pr_err("extension failed to load: %s", hook->name);
0735             __battery_hook_unregister(hook, 0);
0736             goto end;
0737         }
0738     }
0739     pr_info("new extension: %s\n", hook->name);
0740 end:
0741     mutex_unlock(&hook_mutex);
0742 }
0743 EXPORT_SYMBOL_GPL(battery_hook_register);
0744 
0745 /*
0746  * This function gets called right after the battery sysfs
0747  * attributes have been added, so that the drivers that
0748  * define custom sysfs attributes can add their own.
0749  */
0750 static void battery_hook_add_battery(struct acpi_battery *battery)
0751 {
0752     struct acpi_battery_hook *hook_node, *tmp;
0753 
0754     mutex_lock(&hook_mutex);
0755     INIT_LIST_HEAD(&battery->list);
0756     list_add(&battery->list, &acpi_battery_list);
0757     /*
0758      * Since we added a new battery to the list, we need to
0759      * iterate over the hooks and call add_battery for each
0760      * hook that was registered. This usually happens
0761      * when a battery gets hotplugged or initialized
0762      * during the battery module initialization.
0763      */
0764     list_for_each_entry_safe(hook_node, tmp, &battery_hook_list, list) {
0765         if (hook_node->add_battery(battery->bat)) {
0766             /*
0767              * The notification of the extensions has failed, to
0768              * prevent further errors we will unload the extension.
0769              */
0770             pr_err("error in extension, unloading: %s",
0771                     hook_node->name);
0772             __battery_hook_unregister(hook_node, 0);
0773         }
0774     }
0775     mutex_unlock(&hook_mutex);
0776 }
0777 
0778 static void battery_hook_remove_battery(struct acpi_battery *battery)
0779 {
0780     struct acpi_battery_hook *hook;
0781 
0782     mutex_lock(&hook_mutex);
0783     /*
0784      * Before removing the hook, we need to remove all
0785      * custom attributes from the battery.
0786      */
0787     list_for_each_entry(hook, &battery_hook_list, list) {
0788         hook->remove_battery(battery->bat);
0789     }
0790     /* Then, just remove the battery from the list */
0791     list_del(&battery->list);
0792     mutex_unlock(&hook_mutex);
0793 }
0794 
0795 static void __exit battery_hook_exit(void)
0796 {
0797     struct acpi_battery_hook *hook;
0798     struct acpi_battery_hook *ptr;
0799     /*
0800      * At this point, the acpi_bus_unregister_driver()
0801      * has called remove for all batteries. We just
0802      * need to remove the hooks.
0803      */
0804     list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) {
0805         __battery_hook_unregister(hook, 1);
0806     }
0807     mutex_destroy(&hook_mutex);
0808 }
0809 
0810 static int sysfs_add_battery(struct acpi_battery *battery)
0811 {
0812     struct power_supply_config psy_cfg = { .drv_data = battery, };
0813     bool full_cap_broken = false;
0814 
0815     if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
0816         !ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
0817         full_cap_broken = true;
0818 
0819     if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
0820         if (full_cap_broken) {
0821             battery->bat_desc.properties =
0822                 charge_battery_full_cap_broken_props;
0823             battery->bat_desc.num_properties =
0824                 ARRAY_SIZE(charge_battery_full_cap_broken_props);
0825         } else {
0826             battery->bat_desc.properties = charge_battery_props;
0827             battery->bat_desc.num_properties =
0828                 ARRAY_SIZE(charge_battery_props);
0829         }
0830     } else {
0831         if (full_cap_broken) {
0832             battery->bat_desc.properties =
0833                 energy_battery_full_cap_broken_props;
0834             battery->bat_desc.num_properties =
0835                 ARRAY_SIZE(energy_battery_full_cap_broken_props);
0836         } else {
0837             battery->bat_desc.properties = energy_battery_props;
0838             battery->bat_desc.num_properties =
0839                 ARRAY_SIZE(energy_battery_props);
0840         }
0841     }
0842 
0843     battery->bat_desc.name = acpi_device_bid(battery->device);
0844     battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
0845     battery->bat_desc.get_property = acpi_battery_get_property;
0846 
0847     battery->bat = power_supply_register_no_ws(&battery->device->dev,
0848                 &battery->bat_desc, &psy_cfg);
0849 
0850     if (IS_ERR(battery->bat)) {
0851         int result = PTR_ERR(battery->bat);
0852 
0853         battery->bat = NULL;
0854         return result;
0855     }
0856     battery_hook_add_battery(battery);
0857     return device_create_file(&battery->bat->dev, &alarm_attr);
0858 }
0859 
0860 static void sysfs_remove_battery(struct acpi_battery *battery)
0861 {
0862     mutex_lock(&battery->sysfs_lock);
0863     if (!battery->bat) {
0864         mutex_unlock(&battery->sysfs_lock);
0865         return;
0866     }
0867     battery_hook_remove_battery(battery);
0868     device_remove_file(&battery->bat->dev, &alarm_attr);
0869     power_supply_unregister(battery->bat);
0870     battery->bat = NULL;
0871     mutex_unlock(&battery->sysfs_lock);
0872 }
0873 
0874 static void find_battery(const struct dmi_header *dm, void *private)
0875 {
0876     struct acpi_battery *battery = (struct acpi_battery *)private;
0877     /* Note: the hardcoded offsets below have been extracted from
0878      * the source code of dmidecode.
0879      */
0880     if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
0881         const u8 *dmi_data = (const u8 *)(dm + 1);
0882         int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
0883 
0884         if (dm->length >= 18)
0885             dmi_capacity *= dmi_data[17];
0886         if (battery->design_capacity * battery->design_voltage / 1000
0887             != dmi_capacity &&
0888             battery->design_capacity * 10 == dmi_capacity)
0889             set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
0890                 &battery->flags);
0891     }
0892 }
0893 
0894 /*
0895  * According to the ACPI spec, some kinds of primary batteries can
0896  * report percentage battery remaining capacity directly to OS.
0897  * In this case, it reports the Last Full Charged Capacity == 100
0898  * and BatteryPresentRate == 0xFFFFFFFF.
0899  *
0900  * Now we found some battery reports percentage remaining capacity
0901  * even if it's rechargeable.
0902  * https://bugzilla.kernel.org/show_bug.cgi?id=15979
0903  *
0904  * Handle this correctly so that they won't break userspace.
0905  */
0906 static void acpi_battery_quirks(struct acpi_battery *battery)
0907 {
0908     if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
0909         return;
0910 
0911     if (battery->full_charge_capacity == 100 &&
0912         battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
0913         battery->capacity_now >= 0 && battery->capacity_now <= 100) {
0914         set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
0915         battery->full_charge_capacity = battery->design_capacity;
0916         battery->capacity_now = (battery->capacity_now *
0917                 battery->full_charge_capacity) / 100;
0918     }
0919 
0920     if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
0921         return;
0922 
0923     if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
0924         const char *s;
0925 
0926         s = dmi_get_system_info(DMI_PRODUCT_VERSION);
0927         if (s && !strncasecmp(s, "ThinkPad", 8)) {
0928             dmi_walk(find_battery, battery);
0929             if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
0930                      &battery->flags) &&
0931                 battery->design_voltage) {
0932                 battery->design_capacity =
0933                     battery->design_capacity *
0934                     10000 / battery->design_voltage;
0935                 battery->full_charge_capacity =
0936                     battery->full_charge_capacity *
0937                     10000 / battery->design_voltage;
0938                 battery->design_capacity_warning =
0939                     battery->design_capacity_warning *
0940                     10000 / battery->design_voltage;
0941                 battery->capacity_now = battery->capacity_now *
0942                     10000 / battery->design_voltage;
0943             }
0944         }
0945     }
0946 
0947     if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags))
0948         return;
0949 
0950     if (acpi_battery_is_degraded(battery) &&
0951         battery->capacity_now > battery->full_charge_capacity) {
0952         set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags);
0953         battery->capacity_now = battery->full_charge_capacity;
0954     }
0955 }
0956 
0957 static int acpi_battery_update(struct acpi_battery *battery, bool resume)
0958 {
0959     int result = acpi_battery_get_status(battery);
0960 
0961     if (result)
0962         return result;
0963 
0964     if (!acpi_battery_present(battery)) {
0965         sysfs_remove_battery(battery);
0966         battery->update_time = 0;
0967         return 0;
0968     }
0969 
0970     if (resume)
0971         return 0;
0972 
0973     if (!battery->update_time) {
0974         result = acpi_battery_get_info(battery);
0975         if (result)
0976             return result;
0977         acpi_battery_init_alarm(battery);
0978     }
0979 
0980     result = acpi_battery_get_state(battery);
0981     if (result)
0982         return result;
0983     acpi_battery_quirks(battery);
0984 
0985     if (!battery->bat) {
0986         result = sysfs_add_battery(battery);
0987         if (result)
0988             return result;
0989     }
0990 
0991     /*
0992      * Wakeup the system if battery is critical low
0993      * or lower than the alarm level
0994      */
0995     if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) ||
0996         (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
0997          (battery->capacity_now <= battery->alarm)))
0998         acpi_pm_wakeup_event(&battery->device->dev);
0999 
1000     return result;
1001 }
1002 
1003 static void acpi_battery_refresh(struct acpi_battery *battery)
1004 {
1005     int power_unit;
1006 
1007     if (!battery->bat)
1008         return;
1009 
1010     power_unit = battery->power_unit;
1011 
1012     acpi_battery_get_info(battery);
1013 
1014     if (power_unit == battery->power_unit)
1015         return;
1016 
1017     /* The battery has changed its reporting units. */
1018     sysfs_remove_battery(battery);
1019     sysfs_add_battery(battery);
1020 }
1021 
1022 /* Driver Interface */
1023 static void acpi_battery_notify(struct acpi_device *device, u32 event)
1024 {
1025     struct acpi_battery *battery = acpi_driver_data(device);
1026     struct power_supply *old;
1027 
1028     if (!battery)
1029         return;
1030     old = battery->bat;
1031     /*
1032      * On Acer Aspire V5-573G notifications are sometimes triggered too
1033      * early. For example, when AC is unplugged and notification is
1034      * triggered, battery state is still reported as "Full", and changes to
1035      * "Discharging" only after short delay, without any notification.
1036      */
1037     if (battery_notification_delay_ms > 0)
1038         msleep(battery_notification_delay_ms);
1039     if (event == ACPI_BATTERY_NOTIFY_INFO)
1040         acpi_battery_refresh(battery);
1041     acpi_battery_update(battery, false);
1042     acpi_bus_generate_netlink_event(device->pnp.device_class,
1043                     dev_name(&device->dev), event,
1044                     acpi_battery_present(battery));
1045     acpi_notifier_call_chain(device, event, acpi_battery_present(battery));
1046     /* acpi_battery_update could remove power_supply object */
1047     if (old && battery->bat)
1048         power_supply_changed(battery->bat);
1049 }
1050 
1051 static int battery_notify(struct notifier_block *nb,
1052                    unsigned long mode, void *_unused)
1053 {
1054     struct acpi_battery *battery = container_of(nb, struct acpi_battery,
1055                             pm_nb);
1056     int result;
1057 
1058     switch (mode) {
1059     case PM_POST_HIBERNATION:
1060     case PM_POST_SUSPEND:
1061         if (!acpi_battery_present(battery))
1062             return 0;
1063 
1064         if (battery->bat) {
1065             acpi_battery_refresh(battery);
1066         } else {
1067             result = acpi_battery_get_info(battery);
1068             if (result)
1069                 return result;
1070 
1071             result = sysfs_add_battery(battery);
1072             if (result)
1073                 return result;
1074         }
1075 
1076         acpi_battery_init_alarm(battery);
1077         acpi_battery_get_state(battery);
1078         break;
1079     }
1080 
1081     return 0;
1082 }
1083 
1084 static int __init
1085 battery_bix_broken_package_quirk(const struct dmi_system_id *d)
1086 {
1087     battery_bix_broken_package = 1;
1088     return 0;
1089 }
1090 
1091 static int __init
1092 battery_notification_delay_quirk(const struct dmi_system_id *d)
1093 {
1094     battery_notification_delay_ms = 1000;
1095     return 0;
1096 }
1097 
1098 static int __init
1099 battery_ac_is_broken_quirk(const struct dmi_system_id *d)
1100 {
1101     battery_ac_is_broken = 1;
1102     return 0;
1103 }
1104 
1105 static const struct dmi_system_id bat_dmi_table[] __initconst = {
1106     {
1107         /* NEC LZ750/LS */
1108         .callback = battery_bix_broken_package_quirk,
1109         .matches = {
1110             DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
1111             DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
1112         },
1113     },
1114     {
1115         /* Acer Aspire V5-573G */
1116         .callback = battery_notification_delay_quirk,
1117         .matches = {
1118             DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1119             DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"),
1120         },
1121     },
1122     {
1123         /* Point of View mobii wintab p800w */
1124         .callback = battery_ac_is_broken_quirk,
1125         .matches = {
1126             DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1127             DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1128             DMI_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
1129             /* Above matches are too generic, add bios-date match */
1130             DMI_MATCH(DMI_BIOS_DATE, "08/22/2014"),
1131         },
1132     },
1133     {
1134         /* Microsoft Surface Go 3 */
1135         .callback = battery_notification_delay_quirk,
1136         .matches = {
1137             DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
1138             DMI_MATCH(DMI_PRODUCT_NAME, "Surface Go 3"),
1139         },
1140     },
1141     {},
1142 };
1143 
1144 /*
1145  * Some machines'(E,G Lenovo Z480) ECs are not stable
1146  * during boot up and this causes battery driver fails to be
1147  * probed due to failure of getting battery information
1148  * from EC sometimes. After several retries, the operation
1149  * may work. So add retry code here and 20ms sleep between
1150  * every retries.
1151  */
1152 static int acpi_battery_update_retry(struct acpi_battery *battery)
1153 {
1154     int retry, ret;
1155 
1156     for (retry = 5; retry; retry--) {
1157         ret = acpi_battery_update(battery, false);
1158         if (!ret)
1159             break;
1160 
1161         msleep(20);
1162     }
1163     return ret;
1164 }
1165 
1166 static int acpi_battery_add(struct acpi_device *device)
1167 {
1168     int result = 0;
1169     struct acpi_battery *battery = NULL;
1170 
1171     if (!device)
1172         return -EINVAL;
1173 
1174     if (device->dep_unmet)
1175         return -EPROBE_DEFER;
1176 
1177     battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
1178     if (!battery)
1179         return -ENOMEM;
1180     battery->device = device;
1181     strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1182     strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
1183     device->driver_data = battery;
1184     mutex_init(&battery->lock);
1185     mutex_init(&battery->sysfs_lock);
1186     if (acpi_has_method(battery->device->handle, "_BIX"))
1187         set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
1188 
1189     result = acpi_battery_update_retry(battery);
1190     if (result)
1191         goto fail;
1192 
1193     pr_info("Slot [%s] (battery %s)\n", acpi_device_bid(device),
1194         device->status.battery_present ? "present" : "absent");
1195 
1196     battery->pm_nb.notifier_call = battery_notify;
1197     register_pm_notifier(&battery->pm_nb);
1198 
1199     device_init_wakeup(&device->dev, 1);
1200 
1201     return result;
1202 
1203 fail:
1204     sysfs_remove_battery(battery);
1205     mutex_destroy(&battery->lock);
1206     mutex_destroy(&battery->sysfs_lock);
1207     kfree(battery);
1208     return result;
1209 }
1210 
1211 static int acpi_battery_remove(struct acpi_device *device)
1212 {
1213     struct acpi_battery *battery = NULL;
1214 
1215     if (!device || !acpi_driver_data(device))
1216         return -EINVAL;
1217     device_init_wakeup(&device->dev, 0);
1218     battery = acpi_driver_data(device);
1219     unregister_pm_notifier(&battery->pm_nb);
1220     sysfs_remove_battery(battery);
1221     mutex_destroy(&battery->lock);
1222     mutex_destroy(&battery->sysfs_lock);
1223     kfree(battery);
1224     return 0;
1225 }
1226 
1227 #ifdef CONFIG_PM_SLEEP
1228 /* this is needed to learn about changes made in suspended state */
1229 static int acpi_battery_resume(struct device *dev)
1230 {
1231     struct acpi_battery *battery;
1232 
1233     if (!dev)
1234         return -EINVAL;
1235 
1236     battery = acpi_driver_data(to_acpi_device(dev));
1237     if (!battery)
1238         return -EINVAL;
1239 
1240     battery->update_time = 0;
1241     acpi_battery_update(battery, true);
1242     return 0;
1243 }
1244 #else
1245 #define acpi_battery_resume NULL
1246 #endif
1247 
1248 static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1249 
1250 static struct acpi_driver acpi_battery_driver = {
1251     .name = "battery",
1252     .class = ACPI_BATTERY_CLASS,
1253     .ids = battery_device_ids,
1254     .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1255     .ops = {
1256         .add = acpi_battery_add,
1257         .remove = acpi_battery_remove,
1258         .notify = acpi_battery_notify,
1259         },
1260     .drv.pm = &acpi_battery_pm,
1261 };
1262 
1263 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1264 {
1265     int result;
1266 
1267     if (acpi_quirk_skip_acpi_ac_and_battery())
1268         return;
1269 
1270     dmi_check_system(bat_dmi_table);
1271 
1272     result = acpi_bus_register_driver(&acpi_battery_driver);
1273     battery_driver_registered = (result == 0);
1274 }
1275 
1276 static int __init acpi_battery_init(void)
1277 {
1278     if (acpi_disabled)
1279         return -ENODEV;
1280 
1281     async_cookie = async_schedule(acpi_battery_init_async, NULL);
1282     return 0;
1283 }
1284 
1285 static void __exit acpi_battery_exit(void)
1286 {
1287     async_synchronize_cookie(async_cookie + 1);
1288     if (battery_driver_registered) {
1289         acpi_bus_unregister_driver(&acpi_battery_driver);
1290         battery_hook_exit();
1291     }
1292 }
1293 
1294 module_init(acpi_battery_init);
1295 module_exit(acpi_battery_exit);