2016-01-30 14:37:43 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/modem/u-blox.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Vladimir Komendantskiy. All rights reserved.
|
|
|
|
* Author: Vladimir Komendantskiy <vladimir@moixaenergy.com>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <poll.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2021-05-14 04:45:57 +02:00
|
|
|
#include <debug.h>
|
2016-01-30 14:37:43 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
|
|
|
#include <nuttx/irq.h>
|
2023-08-08 00:11:20 +02:00
|
|
|
#include <nuttx/lib/lib.h>
|
2016-01-30 14:37:43 +01:00
|
|
|
#include <nuttx/modem/u-blox.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2016-09-16 17:39:51 +02:00
|
|
|
|
2016-01-30 14:37:43 +01:00
|
|
|
/* Debug ********************************************************************/
|
2020-06-06 19:21:44 +02:00
|
|
|
|
2016-01-30 14:37:43 +01:00
|
|
|
/* Non-standard debug that may be enabled just for testing the modem driver */
|
|
|
|
|
|
|
|
#ifdef CONFIG_MODEM_U_BLOX_DEBUG
|
2016-06-16 20:33:32 +02:00
|
|
|
# define m_err _err
|
|
|
|
# define m_info _info
|
2016-01-30 14:37:43 +01:00
|
|
|
#else
|
2016-06-11 23:50:49 +02:00
|
|
|
# define m_err(x...)
|
2016-06-11 19:59:51 +02:00
|
|
|
# define m_info(x...)
|
2016-01-30 14:37:43 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Type Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The type of upper half driver state. */
|
|
|
|
|
|
|
|
struct ubxmdm_upper
|
|
|
|
{
|
2023-08-08 00:11:20 +02:00
|
|
|
FAR char *path; /* Registration path */
|
2016-01-30 14:37:43 +01:00
|
|
|
|
|
|
|
/* The contained lower-half driver. */
|
|
|
|
|
2023-08-08 00:11:20 +02:00
|
|
|
FAR struct ubxmdm_lower *lower;
|
2016-01-30 14:37:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-08-08 00:11:20 +02:00
|
|
|
static ssize_t ubxmdm_read(FAR struct file *filep,
|
|
|
|
FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t ubxmdm_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer,
|
2016-01-30 14:37:43 +01:00
|
|
|
size_t buflen);
|
2023-08-08 00:11:20 +02:00
|
|
|
static int ubxmdm_ioctl(FAR struct file *filep,
|
2016-01-30 14:37:43 +01:00
|
|
|
int cmd,
|
|
|
|
unsigned long arg);
|
|
|
|
|
2023-08-08 00:11:20 +02:00
|
|
|
static int ubxmdm_poll(FAR struct file *filep,
|
|
|
|
FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
2016-01-30 14:37:43 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-22 16:43:07 +02:00
|
|
|
static const struct file_operations g_ubxmdm_fops =
|
2016-01-30 14:37:43 +01:00
|
|
|
{
|
2022-01-10 15:51:17 +01:00
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
2016-01-30 14:37:43 +01:00
|
|
|
ubxmdm_read, /* read */
|
|
|
|
ubxmdm_write, /* write */
|
2022-01-10 15:51:17 +01:00
|
|
|
NULL, /* seek */
|
2016-01-30 14:37:43 +01:00
|
|
|
ubxmdm_ioctl, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2019-05-22 02:57:54 +02:00
|
|
|
ubxmdm_poll /* poll */
|
2016-01-30 14:37:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-08-08 00:11:20 +02:00
|
|
|
static ssize_t ubxmdm_read(FAR struct file *filep,
|
|
|
|
FAR char *buffer,
|
2016-01-30 14:37:43 +01:00
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
return 0; /* Return EOF */
|
|
|
|
}
|
|
|
|
|
2023-08-08 00:11:20 +02:00
|
|
|
static ssize_t ubxmdm_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer,
|
2016-01-30 14:37:43 +01:00
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
return len; /* Say that everything was written */
|
|
|
|
}
|
|
|
|
|
2023-08-08 00:11:20 +02:00
|
|
|
static int ubxmdm_ioctl(FAR struct file *filep,
|
2016-01-30 14:37:43 +01:00
|
|
|
int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
2023-08-08 00:11:20 +02:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct ubxmdm_upper *upper;
|
|
|
|
FAR struct ubxmdm_lower *lower;
|
|
|
|
FAR struct ubxmdm_status *status;
|
|
|
|
int ret;
|
2016-01-30 14:37:43 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
m_info("cmd: %d arg: %ld\n", cmd, arg);
|
2017-05-15 15:20:32 +02:00
|
|
|
upper = inode->i_private;
|
|
|
|
DEBUGASSERT(upper != NULL);
|
|
|
|
lower = upper->lower;
|
|
|
|
DEBUGASSERT(lower != NULL);
|
2016-01-30 14:37:43 +01:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* cmd: UBXMDM_IOC_START
|
|
|
|
* Description:
|
|
|
|
* arg: Ignored
|
|
|
|
*/
|
|
|
|
|
|
|
|
case MODEM_IOC_POWERON:
|
|
|
|
if (lower->ops->poweron)
|
|
|
|
{
|
|
|
|
ret = lower->ops->poweron(lower);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* cmd: UBXMDM_IOC_STOP
|
|
|
|
* Description:
|
|
|
|
* arg: Ignored
|
|
|
|
*/
|
|
|
|
|
|
|
|
case MODEM_IOC_POWEROFF:
|
|
|
|
if (lower->ops->poweroff)
|
|
|
|
{
|
|
|
|
ret = lower->ops->poweroff(lower);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* cmd: UBXMDM_IOC_RESET
|
|
|
|
* Description:
|
|
|
|
* arg: Ignored
|
|
|
|
*/
|
|
|
|
|
|
|
|
case MODEM_IOC_RESET:
|
|
|
|
if (lower->ops->reset)
|
|
|
|
{
|
|
|
|
ret = lower->ops->reset(lower);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* cmd: UBXMDM_IOC_GETSTATUS
|
|
|
|
* Description:
|
|
|
|
* arg: Writeable pointer to struct ubxmdm_status.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case MODEM_IOC_GETSTATUS:
|
|
|
|
if (lower->ops->getstatus)
|
|
|
|
{
|
2023-08-08 00:11:20 +02:00
|
|
|
status = (FAR struct ubxmdm_status *)((uintptr_t)arg);
|
2016-01-30 14:37:43 +01:00
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
ret = lower->ops->getstatus(lower, status);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Unrecognized IOCTL commands are forwarded to the lower-half IOCTL
|
|
|
|
* handler, if defined.
|
|
|
|
*/
|
|
|
|
|
|
|
|
default:
|
2016-06-11 19:59:51 +02:00
|
|
|
m_info("Forwarding unrecognized cmd: %d arg: %ld\n", cmd, arg);
|
2016-01-30 14:37:43 +01:00
|
|
|
|
|
|
|
if (lower->ops->ioctl)
|
|
|
|
{
|
|
|
|
ret = lower->ops->ioctl(lower, cmd, arg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-06 19:21:44 +02:00
|
|
|
ret = -ENOTTY;
|
2016-01-30 14:37:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-08-08 00:11:20 +02:00
|
|
|
static int ubxmdm_poll(FAR struct file *filep,
|
|
|
|
FAR struct pollfd *fds,
|
2016-01-30 14:37:43 +01:00
|
|
|
bool setup)
|
|
|
|
{
|
|
|
|
if (setup)
|
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(&fds, 1, POLLIN | POLLOUT);
|
2016-01-30 14:37:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-08-08 00:11:20 +02:00
|
|
|
FAR void *ubxmdm_register(FAR const char *path,
|
|
|
|
FAR struct ubxmdm_lower *lower)
|
2016-01-30 14:37:43 +01:00
|
|
|
{
|
|
|
|
FAR struct ubxmdm_upper *upper;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(path && lower);
|
|
|
|
|
2020-06-06 19:21:44 +02:00
|
|
|
upper = (FAR struct ubxmdm_upper *)
|
2016-01-30 14:37:43 +01:00
|
|
|
kmm_zalloc(sizeof(struct ubxmdm_upper));
|
|
|
|
if (!upper)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
m_err("ERROR: Upper half allocation failed\n");
|
2016-01-30 14:37:43 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
upper->lower = lower;
|
|
|
|
upper->path = strdup(path);
|
|
|
|
if (!upper->path)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
m_err("ERROR: Path allocation failed\n");
|
2016-01-30 14:37:43 +01:00
|
|
|
goto errout_with_upper;
|
|
|
|
}
|
|
|
|
|
2023-04-22 16:43:07 +02:00
|
|
|
ret = register_driver(path, &g_ubxmdm_fops, 0666, upper);
|
2016-01-30 14:37:43 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
m_err("ERROR: register_driver failed: %d\n", ret);
|
2016-01-30 14:37:43 +01:00
|
|
|
goto errout_with_path;
|
|
|
|
}
|
|
|
|
|
2023-08-08 00:11:20 +02:00
|
|
|
return (FAR void *)upper;
|
2016-01-30 14:37:43 +01:00
|
|
|
|
|
|
|
errout_with_path:
|
2023-08-08 00:11:20 +02:00
|
|
|
lib_free(upper->path);
|
2016-01-30 14:37:43 +01:00
|
|
|
|
|
|
|
errout_with_upper:
|
|
|
|
kmm_free(upper);
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ubxmdm_unregister(FAR void *handle)
|
|
|
|
{
|
|
|
|
FAR struct ubxmdm_upper *upper;
|
|
|
|
FAR struct ubxmdm_lower *lower;
|
|
|
|
|
2020-06-06 19:21:44 +02:00
|
|
|
upper = (FAR struct ubxmdm_upper *) handle;
|
2017-05-15 15:20:32 +02:00
|
|
|
DEBUGASSERT(upper != NULL);
|
2016-01-30 14:37:43 +01:00
|
|
|
lower = upper->lower;
|
2017-05-15 15:20:32 +02:00
|
|
|
DEBUGASSERT(lower != NULL);
|
2016-01-30 14:37:43 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
m_info("Unregistering: %s\n", upper->path);
|
2016-01-30 14:37:43 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(lower->ops->poweroff);
|
2020-01-02 17:49:34 +01:00
|
|
|
lower->ops->poweroff(lower);
|
2016-01-30 14:37:43 +01:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
unregister_driver(upper->path);
|
2016-01-30 14:37:43 +01:00
|
|
|
|
2023-08-08 00:11:20 +02:00
|
|
|
lib_free(upper->path);
|
2016-01-30 14:37:43 +01:00
|
|
|
kmm_free(upper);
|
|
|
|
}
|