123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- #include <linux/version.h>
- #include <linux/init.h>
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/kobject.h>
- #include <linux/errno.h>
- #include <linux/sysfs.h>
- #include <linux/syscalls.h>
- #include "defines.h"
- #include "sfsattrib.h"
- #include "kfirmware.h"
- /////////////////////////////////////////////////////////////////////////////
- int g_hw = -1, g_sw = -1;
- TIVA_ADC g_tadc;
- unsigned long long g_nUpTime = 0;
- void *g_pFwBuffer = NULL;
- size_t g_nCbFwData = 0;
- static bool g_bFwWriteLock = false;
- DEFINE_MUTEX(g_mutex);
- /////////////////////////////////////////////////////////////////////////////
- static ssize_t version_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
- {
- if(g_hw < 0 || g_sw < 0)
- return -ENODATA;
- return sprintf(buf, "%d %d", g_hw, g_sw);
- }
- /////////////////////////////////////////////////////////////////////////////
- static ssize_t Uptime_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
- {
- return sprintf(buf, "%llu", g_nUpTime);
- }
- /////////////////////////////////////////////////////////////////////////////
- static ssize_t UVers_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
- {
- int nVal = g_tadc.UVers + 40;
- return sprintf(buf, "%d.%02d", nVal / 100, nVal % 100);
- }
- /////////////////////////////////////////////////////////////////////////////
- static ssize_t UBatV3_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
- {
- int nVal = g_tadc.UBatV3;
- return sprintf(buf, "%d.%02d", nVal / 100, nVal % 100);
- }
- /////////////////////////////////////////////////////////////////////////////
- static ssize_t TempBoard_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
- {
- int nVal = g_tadc.Temp;
- return sprintf(buf, "%d.%d0", nVal / 10, nVal % 10);
- }
- /////////////////////////////////////////////////////////////////////////////
- static ssize_t UV5Vsys_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
- {
- int nVal = g_tadc.UV5Vsys;
- return sprintf(buf, "%d.%02d", nVal / 100, nVal % 100);
- }
- /////////////////////////////////////////////////////////////////////////////
- static ssize_t UV3V6Bat_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
- {
- int nVal = g_tadc.UV3V6Bat;
- return sprintf(buf, "%d.%02d", nVal / 100, nVal % 100);
- }
- /////////////////////////////////////////////////////////////////////////////
- static ssize_t TempTIVA_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
- {
- long nVal = 14750 - 18750 * g_tadc.TempTIVA / 4096;
- return sprintf(buf, "%ld.%02ld", nVal / 100, nVal % 100);
- }
- /////////////////////////////////////////////////////////////////////////////
- static ssize_t AdcBin_read(struct file *pf, struct kobject *kobj, struct bin_attribute *attr, char *buf, loff_t off, size_t len)
- {
- if(off >= sizeof(g_tadc))
- return 0;
- if((len + off) > sizeof(g_tadc))
- len = sizeof(g_tadc) - off;
- mutex_lock(&g_mutex);
- memcpy(buf, ((const unsigned char*)&g_tadc) + off, len);
- mutex_unlock(&g_mutex);
- // KALERT("%s buf: %p, off: %lld, len: %zu\n", __FUNCTION__, buf, off, len);
- return (ssize_t)len;
- }
- /////////////////////////////////////////////////////////////////////////////
- static ssize_t firmware_read(struct file *pf, struct kobject *kobj, struct bin_attribute *attr, char *buf, loff_t off, size_t len)
- {
- return -EACCES;
- /* if(off >= g_nCbFwData)
- return 0;
- if((off + len) > g_nCbFwData)
- len = g_nCbFwData - off;
- mutex_lock(&g_mutex);
- memcpy(buf, ((char*)g_pFwBuffer) + off, len);
- mutex_unlock(&g_mutex);
- KALERT("%s buf: %p, off: %lld, len: %zu\n", __FUNCTION__, buf, off, len);
- return len;*/
- }
- static ssize_t firmware_write(struct file *pf, struct kobject *kobj, struct bin_attribute *attr, char *buf, loff_t off, size_t len)
- {
- int nRet;
- size_t nCbUbound;
- static KFW_DROP_CTX dctx;
- if(SfAttIsFirmwareLocked())
- return -EBUSY;
- else if(len == 0)
- return 0;
- else if(off < 0)
- return -EFAULT;
- else if(off >= _FIRMWARE_BUFFER_SIZE)
- return -ENOMEM;
- else if((off + len) > _FIRMWARE_BUFFER_SIZE)
- len = _FIRMWARE_BUFFER_SIZE - off;
-
- if(off == 0) // first block of data
- {
- memset(&dctx, 0, sizeof(dctx));
- mutex_lock(&g_mutex);
- g_nCbFwData = 0;
- memset(g_pFwBuffer, 0xFF, _FIRMWARE_BUFFER_SIZE);
- mutex_unlock(&g_mutex);
- }
- nCbUbound = off + len;
- mutex_lock(&g_mutex);
- memcpy(((char*)g_pFwBuffer) + off, buf, len);
- if(g_nCbFwData < nCbUbound)
- g_nCbFwData = nCbUbound;
- mutex_unlock(&g_mutex);
-
- if((nRet = KfwOnDataDropped(g_pFwBuffer, g_nCbFwData, &dctx)) < 0)
- {
- mutex_lock(&g_mutex);
- g_nCbFwData = 0;
- mutex_unlock(&g_mutex);
- memset(&dctx, 0, sizeof(dctx));
- SfAttLockFirmware(false);
- return nRet;
- }
- else if(nRet > 0)
- {
- mutex_lock(&g_mutex);
- g_nCbFwData = 0;
- mutex_unlock(&g_mutex);
- memset(&dctx, 0, sizeof(dctx));
- }
- // KALERT("%s buf: %p, off: %lld, len: %zu\n", __FUNCTION__, buf, off, len);
- return len;
- }
- /////////////////////////////////////////////////////////////////////////////
- bool SfAttIsFirmwareLocked(void)
- {
- bool bRet;
- mutex_lock(&g_mutex);
- bRet = g_bFwWriteLock;
- mutex_unlock(&g_mutex);
- return bRet;
- }
- /////////////////////////////////////////////////////////////////////////////
- void SfAttLockFirmware(bool bLock)
- {
- mutex_lock(&g_mutex);
- g_bFwWriteLock = bLock;
- mutex_unlock(&g_mutex);
- }
- /////////////////////////////////////////////////////////////////////////////
- struct kobj_attribute g_tivaVersionAtt = __ATTR_RO(version);
- struct kobj_attribute g_tivaUVersAtt = __ATTR_RO(UVers);
- struct kobj_attribute g_tivaUBatV3Att = __ATTR_RO(UBatV3);
- struct kobj_attribute g_tivaTempAtt = __ATTR_RO(TempBoard);
- struct kobj_attribute g_tivaUV5VsysAtt = __ATTR_RO(UV5Vsys);
- struct kobj_attribute g_tivaUV3V6BatAtt = __ATTR_RO(UV3V6Bat);
- struct kobj_attribute g_tivaTempTIVAAtt = __ATTR_RO(TempTIVA);
- struct bin_attribute g_tivaAdcBinAtt = __BIN_ATTR_RO(AdcBin, sizeof(TIVA_ADC));
- struct kobj_attribute g_tivaUptimeAtt = __ATTR_RO(Uptime);
- struct bin_attribute g_tivaFirmwareAtt = __BIN_ATTR_RW(firmware, 0);
|