hqsys_misc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (C) 2015 MediaTek Inc.
  3. * Copyright (C) 2021 XiaoMi, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include "hqsys_misc.h"
  15. MISC_INFO(MISC_EMMC_SIZE, emmc_size);
  16. MISC_INFO(MISC_RAM_SIZE, ram_size);
  17. MISC_INFO(MISC_BOOT_MODE, boot_mode);
  18. MISC_INFO(MISC_OTP_SN, otp_sn);
  19. extern unsigned int msdc_get_capacity(int get_emmc_total);
  20. extern char *get_emmc_name(void);
  21. unsigned int round_kbytes_to_readable_mbytes(unsigned int k)
  22. {
  23. unsigned int r_size_m = 0;
  24. unsigned int in_mega = k/1024;
  25. if (in_mega > 64*1024) { //if memory is larger than 64G
  26. r_size_m = 128*1024; // It should be 128G
  27. } else if (in_mega > 32*1024) { //larger than 32G
  28. r_size_m = 64*1024; //should be 64G
  29. } else if (in_mega > 16*1024) {
  30. r_size_m = 32*1024;
  31. } else if (in_mega > 8*1024) {
  32. r_size_m = 16*1024;
  33. } else if (in_mega > 6*1024) {
  34. r_size_m = 8*1024;
  35. } else if (in_mega > 4*1024) {
  36. r_size_m = 6*1024; //RAM may be 6G
  37. } else if (in_mega > 3*1024) {
  38. r_size_m = 4*1024;
  39. } else if (in_mega > 2*1024) {
  40. r_size_m = 3*1024; //RAM may be 3G
  41. } else if (in_mega > 1024) {
  42. r_size_m = 2*1024;
  43. } else if (in_mega > 512) {
  44. r_size_m = 1024;
  45. } else if (in_mega > 256) {
  46. r_size_m = 512;
  47. } else if (in_mega > 128) {
  48. r_size_m = 256;
  49. } else {
  50. k = 0;
  51. }
  52. return r_size_m;
  53. }
  54. ssize_t hq_emmcinfo(char *buf)
  55. {
  56. ssize_t count = -1;
  57. struct file *pfile = NULL;
  58. mm_segment_t old_fs;
  59. loff_t pos;
  60. ssize_t ret = 0;
  61. unsigned long long Size_buf = 0;
  62. char buf_size[qcom_emmc_len];
  63. memset(buf_size, 0, sizeof(buf_size));
  64. pfile = filp_open(qcom_emmc, O_RDONLY, 0);
  65. if (IS_ERR(pfile)) {
  66. goto ERR_0;
  67. }
  68. old_fs = get_fs();
  69. set_fs(KERNEL_DS);
  70. pos = 0;
  71. ret = vfs_read(pfile, buf_size, qcom_emmc_len, &pos);
  72. if (ret <= 0) {
  73. goto ERR_1;
  74. }
  75. Size_buf = simple_strtoull(buf_size, NULL, 0);
  76. Size_buf >>= 1; //Switch to KB
  77. count = sprintf(buf, "%dGB", round_kbytes_to_readable_mbytes((unsigned int)Size_buf)/1024);
  78. ERR_1:
  79. filp_close(pfile, NULL);
  80. set_fs(old_fs);
  81. return count;
  82. ERR_0:
  83. return count;
  84. }
  85. static struct attribute *hq_misc_attrs[] = {
  86. &misc_info_emmc_size.attr,
  87. &misc_info_ram_size.attr,
  88. &misc_info_boot_mode.attr,
  89. &misc_info_otp_sn.attr,
  90. NULL
  91. };
  92. #if 0
  93. extern int hq_read_sn_from_otp(char *sn);
  94. extern int hq_write_sn_to_otp(char *sn, unsigned int len);
  95. #endif
  96. #define SN_LEN (12) //for B6H Nikeh
  97. static ssize_t hq_misc_show(struct kobject *kobj, struct attribute *a, char *buf)
  98. {
  99. ssize_t count = 0;
  100. struct misc_info *mi = container_of(a, struct misc_info, attr);
  101. switch (mi->m_id) {
  102. case MISC_RAM_SIZE:
  103. {
  104. #define K(x) ((x) << (PAGE_SHIFT - 10))
  105. struct sysinfo i;
  106. si_meminfo(&i);
  107. //count=sprintf(buf,"%u",(unsigned int)K(i.totalram));
  108. if (round_kbytes_to_readable_mbytes(K(i.totalram)) >= 1024) {
  109. count = sprintf(buf, "%dGB", round_kbytes_to_readable_mbytes(K(i.totalram))/1024);
  110. } else{
  111. count = sprintf(buf, "%dMB", round_kbytes_to_readable_mbytes(K(i.totalram)));
  112. }
  113. }
  114. break;
  115. case MISC_EMMC_SIZE:
  116. //count = sprintf(buf,"%dGB",round_kbytes_to_readable_mbytes(msdc_get_capacity(1)/2)/1024);
  117. count = hq_emmcinfo(buf);
  118. break;
  119. case MISC_OTP_SN:
  120. #if 0 //#ifdef CONFIG_MTK_EMMC_SUPPORT_OTP
  121. {
  122. char temp[SN_LEN+1] = {0};
  123. int result = 0;
  124. int i = 0;
  125. result = hq_read_sn_from_otp(temp);
  126. if (0 == result) {
  127. //#if 0
  128. //check if alpha and num
  129. for (i = 0; i < SN_LEN; i++) {
  130. if (!isalnum(temp[i])) {
  131. count = sprintf(buf, "Not Valid SN\n");
  132. goto r_error;
  133. }
  134. }
  135. //#endif
  136. count = sprintf(buf, "%s", temp);
  137. } else{
  138. count = sprintf(buf, "Read SN in OTP error %d\n", result);
  139. }
  140. }
  141. #else
  142. count = sprintf(buf, "SN in OTP not enabled\n");
  143. #endif
  144. break;
  145. default:
  146. count = sprintf(buf, "Not support");
  147. break;
  148. }
  149. //r_error:
  150. return count;
  151. }
  152. static ssize_t hq_misc_store(struct kobject *kobj, struct attribute *a, const char *buf, size_t count)
  153. {
  154. struct misc_info *mi = container_of(a, struct misc_info, attr);
  155. switch (mi->m_id) {
  156. #if 0 //#ifdef CONFIG_MTK_EMMC_SUPPORT_OTP
  157. case MISC_OTP_SN:
  158. {
  159. char temp[SN_LEN+1] = {0};
  160. int result = 0;
  161. int i = 0;
  162. if (0 != strncmp(buf, "SN:=", 4)) {
  163. printk("[%s] invalid write sn command\n", __func__);
  164. break;
  165. }
  166. for (i = 0; i < SN_LEN; i++) {
  167. temp[i] = buf[i+4];
  168. if (('\n' == buf[i+4]) || ('\r' == buf[i+4])) {
  169. temp[i] = 0;
  170. break;
  171. }
  172. }
  173. result = hq_write_sn_to_otp(temp, strlen(temp));
  174. if (0 != result)
  175. printk("[%s] called write error %d\n", __func__, result);
  176. }
  177. break;
  178. #endif
  179. default:
  180. break;
  181. }
  182. return count;
  183. }
  184. /* hq_misc object */
  185. static struct kobject hq_misc_kobj;
  186. static const struct sysfs_ops hq_misc_sysfs_ops = {
  187. .show = hq_misc_show,
  188. .store = hq_misc_store,
  189. };
  190. /* hq_misc type */
  191. static struct kobj_type hq_misc_ktype = {
  192. .sysfs_ops = &hq_misc_sysfs_ops,
  193. .default_attrs = hq_misc_attrs
  194. };
  195. static int __init create_misc(void)
  196. {
  197. int ret;
  198. /* add kobject */
  199. ret = register_kboj_under_hqsysfs(&hq_misc_kobj, &hq_misc_ktype, HUAQIN_MISC_NAME);
  200. if (ret < 0) {
  201. pr_err("%s fail to add hq_misc_kobj\n", __func__);
  202. return ret;
  203. }
  204. return 0;
  205. }
  206. static int __init hq_misc_sys_init(void)
  207. {
  208. /* create sysfs entry at /sys/class/hq_misc/interface/misc */
  209. create_misc();
  210. return 0;
  211. }
  212. late_initcall(hq_misc_sys_init);
  213. MODULE_AUTHOR("KaKa Ni <nigang@hq_misc.com>");
  214. MODULE_DESCRIPTION("Huaqin Hardware Info Driver");
  215. MODULE_LICENSE("GPL");