smart_battery.c 15 KB

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