* drivers/efuse/efuse.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/fs/fs.h>
#include <nuttx/irq.h>
#include <nuttx/lib/lib.h>
#include <nuttx/mutex.h>
#include <nuttx/efuse/efuse.h>
#ifdef CONFIG_EFUSE
* Pre-processor Definitions
****************************************************************************/
* Private Type Definitions
****************************************************************************/
struct efuse_upperhalf_s
{
mutex_t lock;
FAR char *path;
FAR struct efuse_lowerhalf_s *lower;
};
* Private Function Prototypes
****************************************************************************/
static ssize_t efuse_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static ssize_t efuse_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
static int efuse_ioctl(FAR struct file *filep, int cmd,
unsigned long arg);
* Private Data
****************************************************************************/
static const struct file_operations g_efuseops =
{
NULL,
NULL,
efuse_read,
efuse_write,
NULL,
efuse_ioctl,
};
* Private Functions
****************************************************************************/
* Name: efuse_read
*
* Description:
* A dummy read method. This is provided only to satisfy the VFS layer.
*
****************************************************************************/
static ssize_t efuse_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
return 0;
}
* Name: efuse_write
*
* Description:
* A dummy write method. This is provided only to satisfy the VFS layer.
*
****************************************************************************/
static ssize_t efuse_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen)
{
return 0;
}
* Name: efuse_ioctl
*
* Description:
* The standard ioctl method. This is where ALL of the efuse timer
* work is done.
*
****************************************************************************/
static int efuse_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
FAR struct inode *inode = filep->f_inode;
FAR struct efuse_upperhalf_s *upper;
FAR struct efuse_lowerhalf_s *lower;
int ret;
minfo("cmd: %d arg: %lu\n", cmd, arg);
upper = inode->i_private;
DEBUGASSERT(upper != NULL);
lower = upper->lower;
DEBUGASSERT(lower != NULL);
ret = nxmutex_lock(&upper->lock);
if (ret < 0)
{
return ret;
}
switch (cmd)
{
* Description: Read a field
* Argument: A pointer to a struct efuse_param.
* Where the field is an array.
* Each item in the array is a pointer to an efuse_desc_t
* variable.
* An efuse_desc_t variable contains an offset to a field
* or subfield and its size in bits.
* The size param is a redundancy, and it is the sum
* of all subfields sizes from efuse_desc_t in bits.
* The data is a pointer to a pre-allocated space
* where the driver will load the data read from efuse.
*/
case EFUSEIOC_READ_FIELD:
{
FAR struct efuse_param_s *param =
(FAR struct efuse_param_s *)((uintptr_t)arg);
DEBUGASSERT(lower->ops->read_field);
ret = lower->ops->read_field(lower,
param->field,
param->data,
param->size);
}
break;
* Description: Write a field
* Argument: A pointer to a struct efuse_param.
* Where the field is an array.
* Each item in the array is a pointer to an efuse_desc_t
* variable.
* An efuse_desc_t variable contains an offset to a field
* or subfield and its size in bits.
* The size param is a redundancy, and it is the sum
* of all subfields sizes from efuse_desc_t in bits.
* The data is a pointer to a pre-allocated space
* where the user wrote the value that he wants
* to write in a field or subfield.
*/
case EFUSEIOC_WRITE_FIELD:
{
FAR struct efuse_param_s *param =
(FAR struct efuse_param_s *)((uintptr_t)arg);
DEBUGASSERT(lower->ops->write_field);
ret = lower->ops->write_field(lower,
param->field,
param->data,
param->size);
}
break;
default:
{
minfo("Forwarding unrecognized cmd: %d arg: %lu\n", cmd, arg);
* driver are forwarded to the lower half driver through this
* method.
*/
if (lower->ops->ioctl)
{
ret = lower->ops->ioctl(lower, cmd, arg);
}
else
{
ret = -ENOTTY;
}
}
break;
}
nxmutex_unlock(&upper->lock);
return ret;
}
* Public Functions
****************************************************************************/
* Name: efuse_register
*
* Description:
* This function binds an instance of a "lower half" efuse driver with
* the "upper half" efuse device and registers that device so that can
* be used by application code.
*
* Input Parameters:
* dev path - The full path to the driver to be registered in the NuttX
* pseudo-filesystem. The recommended convention is to name all
* efuse drivers as "/dev/efuse".
* lower - A pointer to an instance of lower half efuse driver. This
* instance is bound to the efuse driver and must persists as long as
* the driver persists.
*
* Returned Value:
* On success, a non-NULL handle is returned to the caller. In the event
* of any failure, a NULL value is returned.
*
****************************************************************************/
FAR void *efuse_register(FAR const char *path,
FAR struct efuse_lowerhalf_s *lower)
{
FAR struct efuse_upperhalf_s *upper;
int ret;
DEBUGASSERT(path && lower);
minfo("Entry: path=%s\n", path);
upper = (FAR struct efuse_upperhalf_s *)
kmm_zalloc(sizeof(struct efuse_upperhalf_s));
if (!upper)
{
merr("Upper half allocation failed\n");
goto errout;
}
* by kmm_zalloc()).
*/
nxmutex_init(&upper->lock);
upper->lower = lower;
upper->path = strdup(path);
if (!upper->path)
{
merr("Path allocation failed\n");
goto errout_with_upper;
}
ret = register_driver(path, &g_efuseops, 0666, upper);
if (ret < 0)
{
merr("register_driver failed: %d\n", ret);
goto errout_with_path;
}
return (FAR void *)upper;
errout_with_path:
lib_free(upper->path);
errout_with_upper:
nxmutex_destroy(&upper->lock);
kmm_free(upper);
errout:
return NULL;
}
* Name: efuse_unregister
*
* Description:
* This function can be called to disable and unregister the efuse
* device driver.
*
* Input Parameters:
* handle - This is the handle that was returned by efuse_register()
*
* Returned Value:
* None
*
****************************************************************************/
void efuse_unregister(FAR void *handle)
{
FAR struct efuse_upperhalf_s *upper;
FAR struct efuse_lowerhalf_s *lower;
upper = (FAR struct efuse_upperhalf_s *)handle;
DEBUGASSERT(upper != NULL);
lower = upper->lower;
DEBUGASSERT(lower != NULL);
minfo("Unregistering: %s\n", upper->path);
unregister_driver(upper->path);
lib_free(upper->path);
nxmutex_destroy(&upper->lock);
kmm_free(upper);
}
#endif