sfsattrib.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include <linux/version.h>
  2. #include <linux/init.h>
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/kobject.h>
  6. #include <linux/errno.h>
  7. #include <linux/sysfs.h>
  8. #include <linux/syscalls.h>
  9. #include "defines.h"
  10. #include "sfsattrib.h"
  11. #include "kfirmware.h"
  12. /////////////////////////////////////////////////////////////////////////////
  13. int g_hw = -1, g_sw = -1;
  14. TIVA_ADC g_tadc;
  15. unsigned long long g_nUpTime = 0;
  16. void *g_pFwBuffer = NULL;
  17. size_t g_nCbFwData = 0;
  18. static bool g_bFwWriteLock = false;
  19. DEFINE_MUTEX(g_mutex);
  20. /////////////////////////////////////////////////////////////////////////////
  21. static ssize_t version_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  22. {
  23. if(g_hw < 0 || g_sw < 0)
  24. return -ENODATA;
  25. return sprintf(buf, "%d %d", g_hw, g_sw);
  26. }
  27. /////////////////////////////////////////////////////////////////////////////
  28. static ssize_t Uptime_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  29. {
  30. return sprintf(buf, "%llu", g_nUpTime);
  31. }
  32. /////////////////////////////////////////////////////////////////////////////
  33. static ssize_t UVers_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  34. {
  35. int nVal = g_tadc.UVers + 40;
  36. return sprintf(buf, "%d.%02d", nVal / 100, nVal % 100);
  37. }
  38. /////////////////////////////////////////////////////////////////////////////
  39. static ssize_t UBatV3_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  40. {
  41. int nVal = g_tadc.UBatV3;
  42. return sprintf(buf, "%d.%02d", nVal / 100, nVal % 100);
  43. }
  44. /////////////////////////////////////////////////////////////////////////////
  45. static ssize_t TempBoard_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  46. {
  47. int nVal = g_tadc.Temp;
  48. return sprintf(buf, "%d.%d0", nVal / 10, nVal % 10);
  49. }
  50. /////////////////////////////////////////////////////////////////////////////
  51. static ssize_t UV5Vsys_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  52. {
  53. int nVal = g_tadc.UV5Vsys;
  54. return sprintf(buf, "%d.%02d", nVal / 100, nVal % 100);
  55. }
  56. /////////////////////////////////////////////////////////////////////////////
  57. static ssize_t UV3V6Bat_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  58. {
  59. int nVal = g_tadc.UV3V6Bat;
  60. return sprintf(buf, "%d.%02d", nVal / 100, nVal % 100);
  61. }
  62. /////////////////////////////////////////////////////////////////////////////
  63. static ssize_t TempTIVA_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  64. {
  65. long nVal = 14750 - 18750 * g_tadc.TempTIVA / 4096;
  66. return sprintf(buf, "%ld.%02ld", nVal / 100, nVal % 100);
  67. }
  68. /////////////////////////////////////////////////////////////////////////////
  69. static ssize_t AdcBin_read(struct file *pf, struct kobject *kobj, struct bin_attribute *attr, char *buf, loff_t off, size_t len)
  70. {
  71. if(off >= sizeof(g_tadc))
  72. return 0;
  73. if((len + off) > sizeof(g_tadc))
  74. len = sizeof(g_tadc) - off;
  75. mutex_lock(&g_mutex);
  76. memcpy(buf, ((const unsigned char*)&g_tadc) + off, len);
  77. mutex_unlock(&g_mutex);
  78. // KALERT("%s buf: %p, off: %lld, len: %zu\n", __FUNCTION__, buf, off, len);
  79. return (ssize_t)len;
  80. }
  81. /////////////////////////////////////////////////////////////////////////////
  82. static ssize_t firmware_read(struct file *pf, struct kobject *kobj, struct bin_attribute *attr, char *buf, loff_t off, size_t len)
  83. {
  84. return -EACCES;
  85. /* if(off >= g_nCbFwData)
  86. return 0;
  87. if((off + len) > g_nCbFwData)
  88. len = g_nCbFwData - off;
  89. mutex_lock(&g_mutex);
  90. memcpy(buf, ((char*)g_pFwBuffer) + off, len);
  91. mutex_unlock(&g_mutex);
  92. KALERT("%s buf: %p, off: %lld, len: %zu\n", __FUNCTION__, buf, off, len);
  93. return len;*/
  94. }
  95. static ssize_t firmware_write(struct file *pf, struct kobject *kobj, struct bin_attribute *attr, char *buf, loff_t off, size_t len)
  96. {
  97. int nRet;
  98. size_t nCbUbound;
  99. static KFW_DROP_CTX dctx;
  100. if(SfAttIsFirmwareLocked())
  101. return -EBUSY;
  102. else if(len == 0)
  103. return 0;
  104. else if(off < 0)
  105. return -EFAULT;
  106. else if(off >= _FIRMWARE_BUFFER_SIZE)
  107. return -ENOMEM;
  108. else if((off + len) > _FIRMWARE_BUFFER_SIZE)
  109. len = _FIRMWARE_BUFFER_SIZE - off;
  110. if(off == 0) // first block of data
  111. {
  112. memset(&dctx, 0, sizeof(dctx));
  113. mutex_lock(&g_mutex);
  114. g_nCbFwData = 0;
  115. memset(g_pFwBuffer, 0xFF, _FIRMWARE_BUFFER_SIZE);
  116. mutex_unlock(&g_mutex);
  117. }
  118. nCbUbound = off + len;
  119. mutex_lock(&g_mutex);
  120. memcpy(((char*)g_pFwBuffer) + off, buf, len);
  121. if(g_nCbFwData < nCbUbound)
  122. g_nCbFwData = nCbUbound;
  123. mutex_unlock(&g_mutex);
  124. if((nRet = KfwOnDataDropped(g_pFwBuffer, g_nCbFwData, &dctx)) < 0)
  125. {
  126. mutex_lock(&g_mutex);
  127. g_nCbFwData = 0;
  128. mutex_unlock(&g_mutex);
  129. memset(&dctx, 0, sizeof(dctx));
  130. SfAttLockFirmware(false);
  131. return nRet;
  132. }
  133. else if(nRet > 0)
  134. {
  135. mutex_lock(&g_mutex);
  136. g_nCbFwData = 0;
  137. mutex_unlock(&g_mutex);
  138. memset(&dctx, 0, sizeof(dctx));
  139. }
  140. // KALERT("%s buf: %p, off: %lld, len: %zu\n", __FUNCTION__, buf, off, len);
  141. return len;
  142. }
  143. /////////////////////////////////////////////////////////////////////////////
  144. bool SfAttIsFirmwareLocked(void)
  145. {
  146. bool bRet;
  147. mutex_lock(&g_mutex);
  148. bRet = g_bFwWriteLock;
  149. mutex_unlock(&g_mutex);
  150. return bRet;
  151. }
  152. /////////////////////////////////////////////////////////////////////////////
  153. void SfAttLockFirmware(bool bLock)
  154. {
  155. mutex_lock(&g_mutex);
  156. g_bFwWriteLock = bLock;
  157. mutex_unlock(&g_mutex);
  158. }
  159. /////////////////////////////////////////////////////////////////////////////
  160. struct kobj_attribute g_tivaVersionAtt = __ATTR_RO(version);
  161. struct kobj_attribute g_tivaUVersAtt = __ATTR_RO(UVers);
  162. struct kobj_attribute g_tivaUBatV3Att = __ATTR_RO(UBatV3);
  163. struct kobj_attribute g_tivaTempAtt = __ATTR_RO(TempBoard);
  164. struct kobj_attribute g_tivaUV5VsysAtt = __ATTR_RO(UV5Vsys);
  165. struct kobj_attribute g_tivaUV3V6BatAtt = __ATTR_RO(UV3V6Bat);
  166. struct kobj_attribute g_tivaTempTIVAAtt = __ATTR_RO(TempTIVA);
  167. struct bin_attribute g_tivaAdcBinAtt = __BIN_ATTR_RO(AdcBin, sizeof(TIVA_ADC));
  168. struct kobj_attribute g_tivaUptimeAtt = __ATTR_RO(Uptime);
  169. struct bin_attribute g_tivaFirmwareAtt = __BIN_ATTR_RW(firmware, 0);