smart_battery.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/fs.h>
  5. #include <linux/cdev.h>
  6. #include <linux/device.h>
  7. #include <linux/slab.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/power_supply.h>
  10. #include <linux/mutex.h>
  11. #include "smart_battery.h"
  12. #define DEVICE_NAME "smart_battery"
  13. #define CLASS_NAME "battery_acpi_class"
  14. #define BATTERY_IOC_MAGIC 'B'
  15. #define BATTERY_IOC_GET_CAPACITY _IOR(BATTERY_IOC_MAGIC, 1, int)
  16. #define BATTERY_IOC_GET_VOLTAGE _IOR(BATTERY_IOC_MAGIC, 2, int)
  17. #define BATTERY_IOC_GET_STATUS _IOR(BATTERY_IOC_MAGIC, 3, int)
  18. #define BATTERY_IOC_GET_ENERGY_NOW _IOR(BATTERY_IOC_MAGIC, 4, int)
  19. #define BATTERY_IOC_GET_ENERGY_FULL _IOR(BATTERY_IOC_MAGIC, 5, int)
  20. #define BATTERY_IOC_GET_POWER _IOR(BATTERY_IOC_MAGIC, 6, int)
  21. #define BATTERY_IOC_GET_TECHNOLOGY _IOR(BATTERY_IOC_MAGIC, 7, int)
  22. #define BATTERY_IOC_GET_SERIAL _IOR(BATTERY_IOC_MAGIC, 8, char[16])
  23. #define BATTERY_IOC_GET_PRESENT _IOR(BATTERY_IOC_MAGIC, 9, int)
  24. struct battery_acpi_device {
  25. struct cdev cdev;
  26. struct device *device;
  27. struct class *class;
  28. struct mutex lock;
  29. char battery_name[16];
  30. /* 电池数据 */
  31. int present;
  32. int capacity;
  33. int voltage;
  34. int status;
  35. int energy_now;
  36. int energy_full;
  37. int energy_full_design;
  38. int power_now;
  39. int technology;
  40. char serial[16];
  41. unsigned int read_count;
  42. unsigned int ioctl_count;
  43. };
  44. static struct battery_acpi_device *g_battery_dev = NULL;
  45. static const char* find_battery_name(void)
  46. {
  47. static char name[16];
  48. struct power_supply *psy;
  49. int i;
  50. const char *battery_names[] = {"BAT4", "BAT0", "BAT1", "BAT2", "BAT3", "BATT"};
  51. for (i = 0; i < ARRAY_SIZE(battery_names); i++) {
  52. psy = power_supply_get_by_name(battery_names[i]);
  53. if (psy) {
  54. if (psy->desc->type == POWER_SUPPLY_TYPE_BATTERY) {
  55. strcpy(name, battery_names[i]);
  56. power_supply_put(psy);
  57. pr_info("Found battery: %s\n", name);
  58. return name;
  59. }
  60. power_supply_put(psy);
  61. }
  62. }
  63. return NULL;
  64. }
  65. static int read_psy_property(const char *bat_name, enum power_supply_property prop, int *value)
  66. {
  67. struct power_supply *psy;
  68. union power_supply_propval val;
  69. int ret;
  70. psy = power_supply_get_by_name(bat_name);
  71. if (!psy)
  72. return -ENODEV;
  73. ret = power_supply_get_property(psy, prop, &val);
  74. if (ret == 0)
  75. *value = val.intval;
  76. power_supply_put(psy);
  77. return ret;
  78. }
  79. static int read_psy_property_string(const char *bat_name, enum power_supply_property prop, char *buf, size_t buf_size)
  80. {
  81. struct power_supply *psy;
  82. union power_supply_propval val;
  83. int ret;
  84. psy = power_supply_get_by_name(bat_name);
  85. if (!psy)
  86. return -ENODEV;
  87. ret = power_supply_get_property(psy, prop, &val);
  88. if (ret == 0 && val.strval) {
  89. strncpy(buf, val.strval, buf_size - 1);
  90. buf[buf_size - 1] = '\0';
  91. } else {
  92. buf[0] = '\0';
  93. }
  94. power_supply_put(psy);
  95. return ret;
  96. }
  97. static int charge_to_energy(int charge_uh, int voltage_uv)
  98. {
  99. /* energy (µWh) = charge (µAh) * voltage (µV) / 1000000 */
  100. if (charge_uh <= 0 || voltage_uv <= 0)
  101. return 0;
  102. return ((long long)charge_uh * voltage_uv) / 1000000;
  103. }
  104. static void update_battery_data(struct battery_acpi_device *dev)
  105. {
  106. const char *bat_name;
  107. int ret;
  108. int charge_now = 0, charge_full = 0;
  109. int energy_now_direct = 0, energy_full_direct = 0;
  110. int voltage = 0;
  111. mutex_lock(&dev->lock);
  112. bat_name = find_battery_name();
  113. if (!bat_name) {
  114. dev->present = 0;
  115. mutex_unlock(&dev->lock);
  116. return;
  117. }
  118. strcpy(dev->battery_name, bat_name);
  119. ret = read_psy_property(bat_name, POWER_SUPPLY_PROP_PRESENT, &dev->present);
  120. if (ret < 0)
  121. dev->present = 1;
  122. if (!dev->present) {
  123. mutex_unlock(&dev->lock);
  124. return;
  125. }
  126. read_psy_property(bat_name, POWER_SUPPLY_PROP_CAPACITY, &dev->capacity);
  127. read_psy_property(bat_name, POWER_SUPPLY_PROP_VOLTAGE_NOW, &dev->voltage);
  128. voltage = dev->voltage;
  129. read_psy_property(bat_name, POWER_SUPPLY_PROP_STATUS, &dev->status);
  130. ret = read_psy_property(bat_name, POWER_SUPPLY_PROP_ENERGY_NOW, &energy_now_direct);
  131. if (ret == 0 && energy_now_direct > 0) {
  132. dev->energy_now = energy_now_direct;
  133. pr_debug("Using ENERGY_NOW: %d µWh\n", dev->energy_now);
  134. } else {
  135. ret = read_psy_property(bat_name, POWER_SUPPLY_PROP_CHARGE_NOW, &charge_now);
  136. if (ret == 0 && charge_now > 0 && voltage > 0) {
  137. dev->energy_now = charge_to_energy(charge_now, voltage);
  138. pr_debug("Calculated from CHARGE_NOW: %d µAh * %d µV = %d µWh\n",
  139. charge_now, voltage, dev->energy_now);
  140. } else {
  141. dev->energy_now = -1;
  142. }
  143. }
  144. ret = read_psy_property(bat_name, POWER_SUPPLY_PROP_ENERGY_FULL, &energy_full_direct);
  145. if (ret == 0 && energy_full_direct > 0) {
  146. dev->energy_full = energy_full_direct;
  147. pr_debug("Using ENERGY_FULL: %d µWh\n", dev->energy_full);
  148. } else {
  149. ret = read_psy_property(bat_name, POWER_SUPPLY_PROP_CHARGE_FULL, &charge_full);
  150. if (ret == 0 && charge_full > 0 && voltage > 0) {
  151. dev->energy_full = charge_to_energy(charge_full, voltage);
  152. pr_debug("Calculated from CHARGE_FULL: %d µAh * %d µV = %d µWh\n",
  153. charge_full, voltage, dev->energy_full);
  154. } else {
  155. ret = read_psy_property(bat_name, POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, &charge_full);
  156. if (ret == 0 && charge_full > 0 && voltage > 0) {
  157. dev->energy_full = charge_to_energy(charge_full, voltage);
  158. } else {
  159. dev->energy_full = -1;
  160. }
  161. }
  162. }
  163. ret = read_psy_property(bat_name, POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, &dev->energy_full_design);
  164. if (ret < 0 || dev->energy_full_design <= 0) {
  165. dev->energy_full_design = dev->energy_full;
  166. }
  167. ret = read_psy_property(bat_name, POWER_SUPPLY_PROP_POWER_NOW, &dev->power_now);
  168. if (ret < 0) {
  169. int current_now;
  170. ret = read_psy_property(bat_name, POWER_SUPPLY_PROP_CURRENT_NOW, &current_now);
  171. if (ret == 0 && voltage > 0) {
  172. /* power (µW) = current (µA) * voltage (µV) / 1000000 */
  173. dev->power_now = ((long long)current_now * voltage) / 1000000;
  174. } else {
  175. dev->power_now = 0;
  176. }
  177. }
  178. read_psy_property(bat_name, POWER_SUPPLY_PROP_TECHNOLOGY, &dev->technology);
  179. read_psy_property_string(bat_name, POWER_SUPPLY_PROP_SERIAL_NUMBER, dev->serial, sizeof(dev->serial));
  180. if (dev->serial[0] == '\0')
  181. strcpy(dev->serial, "N/A");
  182. mutex_unlock(&dev->lock);
  183. pr_debug("Battery %s: capacity=%d%%, voltage=%d µV, energy_now=%d µWh, energy_full=%d µWh\n",
  184. dev->battery_name, dev->capacity, dev->voltage, dev->energy_now, dev->energy_full);
  185. }
  186. static const char* get_status_string(int status)
  187. {
  188. switch (status) {
  189. case POWER_SUPPLY_STATUS_CHARGING:
  190. return "Charging";
  191. case POWER_SUPPLY_STATUS_DISCHARGING:
  192. return "Discharging";
  193. case POWER_SUPPLY_STATUS_FULL:
  194. return "Full";
  195. case POWER_SUPPLY_STATUS_NOT_CHARGING:
  196. return "Not charging";
  197. default:
  198. return "Unknown";
  199. }
  200. }
  201. static const char* get_technology_string(int tech)
  202. {
  203. switch (tech) {
  204. case POWER_SUPPLY_TECHNOLOGY_LION:
  205. return "Lithium-ion";
  206. case POWER_SUPPLY_TECHNOLOGY_LIPO:
  207. return "Lithium-polymer";
  208. case POWER_SUPPLY_TECHNOLOGY_NiMH:
  209. return "NiMH";
  210. default:
  211. return "Unknown";
  212. }
  213. }
  214. static int battery_open(struct inode *inode, struct file *file)
  215. {
  216. struct battery_acpi_device *dev = container_of(inode->i_cdev,
  217. struct battery_acpi_device,
  218. cdev);
  219. file->private_data = dev;
  220. update_battery_data(dev);
  221. return 0;
  222. }
  223. static int battery_release(struct inode *inode, struct file *file)
  224. {
  225. return 0;
  226. }
  227. int get_battery_info(struct smart_battery_info *info)
  228. {
  229. if(info == NULL)
  230. return -1;
  231. if(g_battery_dev == NULL)
  232. return -1;
  233. update_battery_data(g_battery_dev);
  234. if(g_battery_dev->present == 0)
  235. {
  236. info->status = 0;
  237. return 0;
  238. }
  239. if(g_battery_dev->status == POWER_SUPPLY_STATUS_CHARGING)
  240. {
  241. info->status = 1;
  242. }
  243. else
  244. {
  245. info->status = 2;
  246. }
  247. info->capacity = g_battery_dev->capacity;
  248. return 0;
  249. }
  250. static ssize_t battery_read(struct file *file, char __user *buf,
  251. size_t count, loff_t *f_pos)
  252. {
  253. struct battery_acpi_device *dev = file->private_data;
  254. char info[512];
  255. int len;
  256. if (*f_pos > 0)
  257. return 0;
  258. update_battery_data(dev);
  259. dev->read_count++;
  260. if (!dev->present) {
  261. len = snprintf(info, sizeof(info),
  262. "=== Battery Information ===\n"
  263. " Battery: NOT PRESENT\n"
  264. " Read Count: %u\n",
  265. dev->read_count);
  266. } else {
  267. len = snprintf(info, sizeof(info),
  268. "=== Battery Information ===\n"
  269. " Battery Name: %s\n"
  270. " Present: Yes\n"
  271. " Capacity: %d%%\n"
  272. " Voltage: %d.%03d V\n"
  273. " Status: %s\n"
  274. " Energy Now: %d.%02d Wh\n"
  275. " Energy Full: %d.%02d Wh\n"
  276. " Energy Full Design:%d.%02d Wh\n"
  277. " Power Now: %d.%02d W\n"
  278. " Technology: %s\n"
  279. " Serial Number: %s\n"
  280. " Read Count: %u\n",
  281. dev->battery_name,
  282. dev->capacity,
  283. dev->voltage / 1000000,
  284. (dev->voltage % 1000000) / 1000,
  285. get_status_string(dev->status),
  286. dev->energy_now * (dev->voltage / 1000000) / 1000000,
  287. ((dev->energy_now * (dev->voltage / 1000000)) % 1000000) / 10000,
  288. dev->energy_full * (dev->voltage / 1000000) / 1000000,
  289. ((dev->energy_full * (dev->voltage / 1000000)) % 1000000) / 10000,
  290. dev->energy_full_design * (dev->voltage / 1000000) / 1000000,
  291. ((dev->energy_full_design * (dev->voltage / 1000000)) % 1000000) / 10000,
  292. dev->power_now / 1000000,
  293. (dev->power_now % 1000000) / 10000,
  294. get_technology_string(dev->technology),
  295. dev->serial,
  296. dev->read_count);
  297. }
  298. if (len > count)
  299. len = count;
  300. if (copy_to_user(buf, info, len))
  301. return -EFAULT;
  302. *f_pos += len;
  303. return len;
  304. }
  305. static long battery_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  306. {
  307. struct battery_acpi_device *dev = file->private_data;
  308. void __user *argp = (void __user *)arg;
  309. update_battery_data(dev);
  310. dev->ioctl_count++;
  311. switch (cmd) {
  312. case BATTERY_IOC_GET_PRESENT:
  313. if (copy_to_user(argp, &dev->present, sizeof(dev->present)))
  314. return -EFAULT;
  315. break;
  316. case BATTERY_IOC_GET_CAPACITY:
  317. if (copy_to_user(argp, &dev->capacity, sizeof(dev->capacity)))
  318. return -EFAULT;
  319. break;
  320. case BATTERY_IOC_GET_VOLTAGE:
  321. if (copy_to_user(argp, &dev->voltage, sizeof(dev->voltage)))
  322. return -EFAULT;
  323. break;
  324. case BATTERY_IOC_GET_STATUS:
  325. if (copy_to_user(argp, &dev->status, sizeof(dev->status)))
  326. return -EFAULT;
  327. break;
  328. case BATTERY_IOC_GET_ENERGY_NOW:
  329. if (copy_to_user(argp, &dev->energy_now, sizeof(dev->energy_now)))
  330. return -EFAULT;
  331. break;
  332. case BATTERY_IOC_GET_ENERGY_FULL:
  333. if (copy_to_user(argp, &dev->energy_full, sizeof(dev->energy_full)))
  334. return -EFAULT;
  335. break;
  336. case BATTERY_IOC_GET_POWER:
  337. if (copy_to_user(argp, &dev->power_now, sizeof(dev->power_now)))
  338. return -EFAULT;
  339. break;
  340. case BATTERY_IOC_GET_TECHNOLOGY:
  341. if (copy_to_user(argp, &dev->technology, sizeof(dev->technology)))
  342. return -EFAULT;
  343. break;
  344. case BATTERY_IOC_GET_SERIAL:
  345. if (copy_to_user(argp, dev->serial, sizeof(dev->serial)))
  346. return -EFAULT;
  347. break;
  348. default:
  349. return -ENOTTY;
  350. }
  351. return 0;
  352. }
  353. static const struct file_operations battery_fops = {
  354. .owner = THIS_MODULE,
  355. .open = battery_open,
  356. .release = battery_release,
  357. .read = battery_read,
  358. .unlocked_ioctl = battery_ioctl,
  359. };
  360. int battery_acpi_driver_init(void)
  361. {
  362. dev_t dev_num;
  363. int ret;
  364. struct battery_acpi_device *dev;
  365. pr_info("Initializing Battery Character Device Driver v2.2\n");
  366. ret = alloc_chrdev_region(&dev_num, 0, 1, DEVICE_NAME);
  367. if (ret < 0) {
  368. pr_err("Failed to allocate device number\n");
  369. return ret;
  370. }
  371. dev = kzalloc(sizeof(struct battery_acpi_device), GFP_KERNEL);
  372. if (!dev) {
  373. ret = -ENOMEM;
  374. goto err_alloc;
  375. }
  376. mutex_init(&dev->lock);
  377. dev->class = class_create(THIS_MODULE, CLASS_NAME);
  378. if (IS_ERR(dev->class)) {
  379. ret = PTR_ERR(dev->class);
  380. goto err_class;
  381. }
  382. dev->device = device_create(dev->class, NULL, dev_num, dev, DEVICE_NAME);
  383. if (IS_ERR(dev->device)) {
  384. ret = PTR_ERR(dev->device);
  385. goto err_device;
  386. }
  387. cdev_init(&dev->cdev, &battery_fops);
  388. dev->cdev.owner = THIS_MODULE;
  389. ret = cdev_add(&dev->cdev, dev_num, 1);
  390. if (ret < 0) {
  391. goto err_cdev;
  392. }
  393. g_battery_dev = dev;
  394. update_battery_data(dev);
  395. pr_info("Battery driver initialized: /dev/%s\n", DEVICE_NAME);
  396. return 0;
  397. err_cdev:
  398. device_destroy(dev->class, dev_num);
  399. err_device:
  400. class_destroy(dev->class);
  401. err_class:
  402. mutex_destroy(&dev->lock);
  403. kfree(dev);
  404. err_alloc:
  405. unregister_chrdev_region(dev_num, 1);
  406. return ret;
  407. }
  408. void battery_acpi_driver_exit(void)
  409. {
  410. dev_t dev_num = g_battery_dev->cdev.dev;
  411. cdev_del(&g_battery_dev->cdev);
  412. device_destroy(g_battery_dev->class, dev_num);
  413. class_destroy(g_battery_dev->class);
  414. mutex_destroy(&g_battery_dev->lock);
  415. kfree(g_battery_dev);
  416. unregister_chrdev_region(dev_num, 1);
  417. pr_info("Battery driver exited\n");
  418. }