2015-11-01 21:41:40 +01:00
|
|
|
/****************************************************************************
|
2015-12-15 15:23:53 +01:00
|
|
|
* drivers/leds/userled_upper.c
|
2015-11-01 21:41:40 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* 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
|
2015-11-01 21:41:40 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-11-01 21:41:40 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* 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.
|
2015-11-01 21:41:40 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This file provides a driver for a LED input devices.
|
|
|
|
*
|
|
|
|
* The LEDs driver exports a standard character driver interface. By
|
|
|
|
* convention, the LED driver is registered as an input device at
|
|
|
|
* /dev/btnN where N uniquely identifies the driver instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2015-11-01 21:41:40 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
#include <nuttx/irq.h>
|
2015-11-01 21:41:40 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2015-11-01 21:41:40 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2015-12-15 15:23:53 +01:00
|
|
|
#include <nuttx/leds/userled.h>
|
2015-11-01 21:41:40 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure provides the state of one LED driver */
|
|
|
|
|
|
|
|
struct userled_upperhalf_s
|
|
|
|
{
|
|
|
|
/* Saved binding to the lower half LED driver */
|
|
|
|
|
|
|
|
FAR const struct userled_lowerhalf_s *lu_lower;
|
|
|
|
|
|
|
|
userled_set_t lu_supported; /* The set of supported LEDs */
|
|
|
|
userled_set_t lu_ledset; /* Current state of LEDs */
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t lu_lock; /* Supports exclusive access to the device */
|
2015-11-01 21:41:40 +01:00
|
|
|
|
|
|
|
/* The following is a singly linked list of open references to the
|
|
|
|
* LED device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FAR struct userled_open_s *lu_open;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This structure describes the state of one open LED driver instance */
|
|
|
|
|
|
|
|
struct userled_open_s
|
|
|
|
{
|
|
|
|
/* Supports a singly linked list */
|
|
|
|
|
|
|
|
FAR struct userled_open_s *bo_flink;
|
|
|
|
|
|
|
|
/* The following will be true if we are closing */
|
|
|
|
|
|
|
|
volatile bool bo_closing;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
|
|
|
static int userled_open(FAR struct file *filep);
|
|
|
|
static int userled_close(FAR struct file *filep);
|
|
|
|
static ssize_t userled_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int userled_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations userled_fops =
|
|
|
|
{
|
2019-05-22 02:57:54 +02:00
|
|
|
userled_open, /* open */
|
|
|
|
userled_close, /* close */
|
|
|
|
NULL, /* read */
|
|
|
|
userled_write, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
userled_ioctl, /* ioctl */
|
2015-11-01 21:41:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: userled_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int userled_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct userled_upperhalf_s *priv;
|
|
|
|
FAR struct userled_open_s *opriv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(filep && filep->f_inode);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
DEBUGASSERT(inode->i_private);
|
|
|
|
priv = (FAR struct userled_upperhalf_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lu_lock);
|
2015-11-01 21:41:40 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
lederr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2015-11-01 21:41:40 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a new open structure */
|
|
|
|
|
2020-11-22 07:15:09 +01:00
|
|
|
opriv = (FAR struct userled_open_s *)
|
|
|
|
kmm_zalloc(sizeof(struct userled_open_s));
|
2015-11-01 21:41:40 +01:00
|
|
|
if (!opriv)
|
|
|
|
{
|
2020-02-23 09:50:23 +01:00
|
|
|
lederr("ERROR: Failed to allocate open structure\n");
|
2015-11-01 21:41:40 +01:00
|
|
|
ret = -ENOMEM;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2015-11-01 21:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Attach the open structure to the device */
|
|
|
|
|
|
|
|
opriv->bo_flink = priv->lu_open;
|
|
|
|
priv->lu_open = opriv;
|
|
|
|
|
|
|
|
/* Attach the open structure to the file structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
filep->f_priv = opriv;
|
2015-11-01 21:41:40 +01:00
|
|
|
ret = OK;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
nxmutex_unlock(&priv->lu_lock);
|
2015-11-01 21:41:40 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: userled_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int userled_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct userled_upperhalf_s *priv;
|
|
|
|
FAR struct userled_open_s *opriv;
|
|
|
|
FAR struct userled_open_s *curr;
|
|
|
|
FAR struct userled_open_s *prev;
|
|
|
|
irqstate_t flags;
|
|
|
|
bool closing;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(filep && filep->f_priv && filep->f_inode);
|
|
|
|
opriv = filep->f_priv;
|
|
|
|
inode = filep->f_inode;
|
|
|
|
DEBUGASSERT(inode->i_private);
|
|
|
|
priv = (FAR struct userled_upperhalf_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Handle an improbable race conditions with the following atomic test
|
|
|
|
* and set.
|
|
|
|
*
|
|
|
|
* This is actually a pretty feeble attempt to handle this. The
|
|
|
|
* improbable race condition occurs if two different threads try to
|
|
|
|
* close the LED driver at the same time. The rule: don't do
|
|
|
|
* that! It is feeble because we do not really enforce stale pointer
|
|
|
|
* detection anyway.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2015-11-01 21:41:40 +01:00
|
|
|
closing = opriv->bo_closing;
|
|
|
|
opriv->bo_closing = true;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2015-11-01 21:41:40 +01:00
|
|
|
|
|
|
|
if (closing)
|
|
|
|
{
|
|
|
|
/* Another thread is doing the close */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lu_lock);
|
2015-11-01 21:41:40 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
lederr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2015-11-01 21:41:40 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the open structure in the list of open structures for the device */
|
|
|
|
|
|
|
|
for (prev = NULL, curr = priv->lu_open;
|
|
|
|
curr && curr != opriv;
|
|
|
|
prev = curr, curr = curr->bo_flink);
|
|
|
|
|
|
|
|
DEBUGASSERT(curr);
|
|
|
|
if (!curr)
|
|
|
|
{
|
2018-03-12 00:43:04 +01:00
|
|
|
lederr("ERROR: Failed to find open entry\n");
|
2015-11-01 21:41:40 +01:00
|
|
|
ret = -ENOENT;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2015-11-01 21:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the structure from the device */
|
|
|
|
|
|
|
|
if (prev)
|
|
|
|
{
|
|
|
|
prev->bo_flink = opriv->bo_flink;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->lu_open = opriv->bo_flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And free the open structure */
|
|
|
|
|
|
|
|
kmm_free(opriv);
|
|
|
|
ret = OK;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lu_lock);
|
2015-11-01 21:41:40 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: userled_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t userled_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct userled_upperhalf_s *priv;
|
|
|
|
FAR const struct userled_lowerhalf_s *lower;
|
|
|
|
userled_set_t ledset;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(filep && filep->f_inode);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
DEBUGASSERT(inode->i_private);
|
|
|
|
priv = (FAR struct userled_upperhalf_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Make sure that the buffer is sufficiently large to hold at least one
|
|
|
|
* complete sample.
|
|
|
|
*
|
|
|
|
* REVISIT: Should also check buffer alignment.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (len < sizeof(userled_set_t))
|
|
|
|
{
|
2018-03-12 00:43:04 +01:00
|
|
|
lederr("ERROR: buffer too small: %lu\n", (unsigned long)len);
|
2015-11-01 21:41:40 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the LED set to write.
|
|
|
|
* REVISIT: if sizeof(userled_set_t) > 1, then we will have to address
|
|
|
|
* some buffer alignment issues.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(buffer != NULL);
|
|
|
|
ledset = *(userled_set_t *)buffer;
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lu_lock);
|
2015-11-01 21:41:40 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
lederr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2015-11-01 21:41:40 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read and return the current state of the LEDs */
|
|
|
|
|
|
|
|
lower = priv->lu_lower;
|
2021-10-04 15:07:34 +02:00
|
|
|
DEBUGASSERT(lower && lower->ll_setall);
|
|
|
|
lower->ll_setall(lower, ledset);
|
2015-11-01 21:41:40 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lu_lock);
|
2015-11-01 21:41:40 +01:00
|
|
|
return (ssize_t)sizeof(userled_set_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: userled_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int userled_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct userled_upperhalf_s *priv;
|
|
|
|
FAR const struct userled_lowerhalf_s *lower;
|
|
|
|
int ret;
|
|
|
|
|
2020-11-22 07:15:09 +01:00
|
|
|
DEBUGASSERT(filep != NULL && filep->f_priv != NULL &&
|
|
|
|
filep->f_inode != NULL);
|
2015-11-01 21:41:40 +01:00
|
|
|
inode = filep->f_inode;
|
|
|
|
DEBUGASSERT(inode->i_private);
|
|
|
|
priv = (FAR struct userled_upperhalf_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lu_lock);
|
2015-11-01 21:41:40 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
lederr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2015-11-01 21:41:40 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the ioctl command */
|
|
|
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* Command: ULEDIOC_SUPPORTED
|
|
|
|
* Description: Report the set of LEDs supported by the hardware;
|
|
|
|
* Argument: A pointer to writeable userled_set_t value in which to
|
|
|
|
* return the set of supported LEDs.
|
2020-11-22 07:15:09 +01:00
|
|
|
* Return: Zero (OK) on success. Minus one will be returned on
|
|
|
|
* failure with the errno value set appropriately.
|
2015-11-01 21:41:40 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
case ULEDIOC_SUPPORTED:
|
|
|
|
{
|
|
|
|
FAR userled_set_t *supported = (FAR userled_set_t *)((uintptr_t)arg);
|
|
|
|
|
|
|
|
/* Verify that a non-NULL pointer was provided */
|
|
|
|
|
|
|
|
if (supported)
|
|
|
|
{
|
|
|
|
*supported = priv->lu_supported;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Command: ULEDIOC_SETLED
|
|
|
|
* Description: Set the state of one LED.
|
|
|
|
* Argument: A read-only pointer to an instance of struct userled_s
|
2020-11-22 07:15:09 +01:00
|
|
|
* Return: Zero (OK) on success. Minus one will be returned on
|
|
|
|
* failure with the errno value set appropriately.
|
2015-11-01 21:41:40 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
case ULEDIOC_SETLED:
|
|
|
|
{
|
2020-11-22 07:15:09 +01:00
|
|
|
FAR struct userled_s *userled = (FAR struct userled_s *)
|
|
|
|
((uintptr_t)arg);
|
2015-11-01 21:41:40 +01:00
|
|
|
int led;
|
|
|
|
bool ledon;
|
|
|
|
|
|
|
|
/* Verify that a non-NULL pointer was provided */
|
|
|
|
|
|
|
|
if (userled)
|
|
|
|
{
|
|
|
|
led = userled->ul_led;
|
|
|
|
ledon = userled->ul_on;
|
|
|
|
|
|
|
|
/* Check that a valid LED is being set */
|
|
|
|
|
2016-05-26 18:01:15 +02:00
|
|
|
if ((size_t)led < 8 * sizeof(userled_set_t) &&
|
2015-11-01 21:41:40 +01:00
|
|
|
(priv->lu_supported & (1 << led)) != 0)
|
|
|
|
{
|
|
|
|
/* Update the LED state */
|
|
|
|
|
|
|
|
if (ledon)
|
|
|
|
{
|
|
|
|
priv->lu_ledset |= (1 << led);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->lu_ledset &= ~(1 << led);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the LED state */
|
|
|
|
|
|
|
|
lower = priv->lu_lower;
|
2021-10-04 15:07:34 +02:00
|
|
|
DEBUGASSERT(lower != NULL && lower->ll_setled != NULL);
|
|
|
|
lower->ll_setled(lower, led, ledon);
|
2015-11-01 21:41:40 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Command: ULEDIOC_SETALL
|
|
|
|
* Description: Set the state of all LEDs.
|
|
|
|
* Argument: A value of type userled_set_t cast to unsigned long
|
2020-11-22 07:15:09 +01:00
|
|
|
* Return: Zero (OK) on success. Minus one will be returned on
|
|
|
|
* failure with the errno value set appropriately.
|
2015-11-01 21:41:40 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
case ULEDIOC_SETALL:
|
|
|
|
{
|
|
|
|
userled_set_t ledset = (userled_set_t)((uintptr_t)arg);
|
|
|
|
|
|
|
|
/* Verify that a valid LED set was provided */
|
|
|
|
|
2019-04-15 15:54:11 +02:00
|
|
|
if ((ledset & ~priv->lu_supported) == 0)
|
2015-11-01 21:41:40 +01:00
|
|
|
{
|
|
|
|
/* Update the LED state */
|
|
|
|
|
|
|
|
priv->lu_ledset = ledset;
|
|
|
|
|
|
|
|
/* Set the new LED state */
|
|
|
|
|
|
|
|
lower = priv->lu_lower;
|
2021-10-04 15:07:34 +02:00
|
|
|
DEBUGASSERT(lower != NULL && lower->ll_setall != NULL);
|
|
|
|
lower->ll_setall(lower, ledset);
|
2015-11-01 21:41:40 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Command: ULEDIOC_GETALL
|
2021-10-18 10:03:33 +02:00
|
|
|
* Description: Get the state of all LEDs.
|
2020-11-22 07:15:09 +01:00
|
|
|
* Argument: A write-able pointer to a userled_set_t memory location
|
|
|
|
* in which to return the LED state.
|
|
|
|
* Return: Zero (OK) on success. Minus one will be returned on
|
|
|
|
* failure with the errno value set appropriately.
|
2015-11-01 21:41:40 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
case ULEDIOC_GETALL:
|
|
|
|
{
|
|
|
|
FAR userled_set_t *ledset = (FAR userled_set_t *)((uintptr_t)arg);
|
|
|
|
|
|
|
|
/* Verify that a non-NULL pointer was provided */
|
|
|
|
|
|
|
|
if (ledset)
|
|
|
|
{
|
2021-10-18 10:03:33 +02:00
|
|
|
#ifdef CONFIG_USERLED_LOWER_READSTATE
|
|
|
|
lower = priv->lu_lower;
|
|
|
|
DEBUGASSERT(lower != NULL && lower->ll_getall != NULL);
|
|
|
|
lower->ll_getall(lower, ledset);
|
|
|
|
#else
|
2015-11-01 21:41:40 +01:00
|
|
|
*ledset = priv->lu_ledset;
|
2021-10-18 10:03:33 +02:00
|
|
|
#endif
|
2015-11-01 21:41:40 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-11-22 07:15:45 +01:00
|
|
|
lederr("ERROR: Unrecognized command: %d\n", cmd);
|
2015-11-01 21:41:40 +01:00
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lu_lock);
|
2015-11-01 21:41:40 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: userled_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Bind the lower half LED driver to an instance of the upper half
|
|
|
|
* LED driver and register the composite character driver as the
|
|
|
|
* specified device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* devname - The name of the LED device to be registered.
|
2017-05-11 21:35:56 +02:00
|
|
|
* This should be a string of the form "/dev/ledN" where N is the
|
2015-11-01 21:41:40 +01:00
|
|
|
* minor device number.
|
|
|
|
* lower - An instance of the platform-specific LED lower half driver.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2015-11-01 21:41:40 +01:00
|
|
|
* Zero (OK) is returned on success. Otherwise a negated errno value is
|
|
|
|
* returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int userled_register(FAR const char *devname,
|
|
|
|
FAR const struct userled_lowerhalf_s *lower)
|
|
|
|
{
|
|
|
|
FAR struct userled_upperhalf_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(devname && lower);
|
|
|
|
|
|
|
|
/* Allocate a new LED driver instance */
|
|
|
|
|
|
|
|
priv = (FAR struct userled_upperhalf_s *)
|
|
|
|
kmm_zalloc(sizeof(struct userled_upperhalf_s));
|
|
|
|
|
|
|
|
if (!priv)
|
|
|
|
{
|
2018-03-12 00:43:04 +01:00
|
|
|
lederr("ERROR: Failed to allocate device structure\n");
|
2015-11-01 21:41:40 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the new LED driver instance */
|
|
|
|
|
|
|
|
priv->lu_lower = lower;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&priv->lu_lock);
|
2015-11-01 21:41:40 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(lower && lower->ll_supported);
|
|
|
|
priv->lu_supported = lower->ll_supported(lower);
|
|
|
|
|
2021-10-04 15:07:34 +02:00
|
|
|
DEBUGASSERT(lower && lower->ll_setall);
|
2015-11-01 21:41:40 +01:00
|
|
|
priv->lu_ledset = 0;
|
2021-10-04 15:07:34 +02:00
|
|
|
lower->ll_setall(lower, priv->lu_ledset);
|
2015-11-01 21:41:40 +01:00
|
|
|
|
|
|
|
/* And register the LED driver */
|
|
|
|
|
|
|
|
ret = register_driver(devname, &userled_fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-03-12 00:43:04 +01:00
|
|
|
lederr("ERROR: register_driver failed: %d\n", ret);
|
2015-11-01 21:41:40 +01:00
|
|
|
goto errout_with_priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_priv:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&priv->lu_lock);
|
2015-11-01 21:41:40 +01:00
|
|
|
kmm_free(priv);
|
|
|
|
return ret;
|
|
|
|
}
|