2017-11-28 19:10:06 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/input/nunchuck.c
|
|
|
|
*
|
2021-03-13 10:55:07 +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
|
2017-11-28 19:10:06 +01:00
|
|
|
*
|
2021-03-13 10:55:07 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-11-28 19:10:06 +01:00
|
|
|
*
|
2021-03-13 10:55:07 +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.
|
2017-11-28 19:10:06 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This file provides a driver for a Nintendo Wii Nunchuck joystick device.
|
|
|
|
* The nunchuck joystick provides X/Y positional data as integer values.
|
2021-03-13 11:55:37 +01:00
|
|
|
* The analog positional data may also be accompanied by discrete button
|
|
|
|
* data.
|
2017-11-28 19:10:06 +01:00
|
|
|
*
|
|
|
|
* The nunchuck joystick driver exports a standard character driver
|
|
|
|
* interface. By convention, the nunchuck joystick is registered as an input
|
|
|
|
* device at /dev/nunchuckN where N uniquely identifies the driver instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <poll.h>
|
2021-06-08 20:00:55 +02:00
|
|
|
#include <assert.h>
|
2017-11-28 19:10:06 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2017-11-28 19:10:06 +01:00
|
|
|
#include <nuttx/signal.h>
|
|
|
|
#include <nuttx/random.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
|
|
|
#include <nuttx/input/nunchuck.h>
|
|
|
|
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure provides the state of one nunchuck joystick driver */
|
|
|
|
|
|
|
|
struct nunchuck_dev_s
|
|
|
|
{
|
|
|
|
FAR struct i2c_master_s *i2c_dev; /* I2C interface connected to Nunchuck */
|
|
|
|
nunchuck_buttonset_t nck_sample; /* Last sampled button states */
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t nck_lock; /* Supports exclusive access to the device */
|
2017-11-28 19:10:06 +01:00
|
|
|
|
|
|
|
/* The following is a singly linked list of open references to the
|
|
|
|
* joystick device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FAR struct nunchuck_open_s *nck_open;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This structure describes the state of one open joystick driver instance */
|
|
|
|
|
|
|
|
struct nunchuck_open_s
|
|
|
|
{
|
|
|
|
/* Supports a singly linked list */
|
|
|
|
|
|
|
|
FAR struct nunchuck_open_s *nck_flink;
|
|
|
|
|
|
|
|
/* The following will be true if we are closing */
|
|
|
|
|
|
|
|
volatile bool nck_closing;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
|
|
|
static int nunchuck_open(FAR struct file *filep);
|
|
|
|
static int nunchuck_close(FAR struct file *filep);
|
|
|
|
static ssize_t nunchuck_read(FAR struct file *filep, FAR char *buffer,
|
2022-04-12 12:16:13 +02:00
|
|
|
size_t buflen);
|
2017-11-28 19:10:06 +01:00
|
|
|
static int nunchuck_ioctl(FAR struct file *filep, int cmd,
|
2022-04-12 12:16:13 +02:00
|
|
|
unsigned long arg);
|
2021-03-13 11:55:37 +01:00
|
|
|
|
2017-11-28 19:10:06 +01:00
|
|
|
/* I2C Helpers */
|
2021-03-13 11:55:37 +01:00
|
|
|
|
2017-11-28 19:10:06 +01:00
|
|
|
static int nunchuck_i2c_read(FAR struct nunchuck_dev_s *priv,
|
2022-04-12 12:16:13 +02:00
|
|
|
FAR uint8_t *regval, int len);
|
2017-11-28 19:10:06 +01:00
|
|
|
static int nunchuck_i2c_write(FAR struct nunchuck_dev_s *priv,
|
2022-04-12 12:16:13 +02:00
|
|
|
uint8_t const *data, int len);
|
2017-11-28 19:10:06 +01:00
|
|
|
static int nunchuck_sample(FAR struct nunchuck_dev_s *priv,
|
|
|
|
FAR struct nunchuck_sample_s *buffer);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations nunchuck_fops =
|
|
|
|
{
|
|
|
|
nunchuck_open, /* open */
|
|
|
|
nunchuck_close, /* close */
|
|
|
|
nunchuck_read, /* read */
|
|
|
|
NULL, /* write */
|
|
|
|
NULL, /* seek */
|
2019-05-22 02:57:54 +02:00
|
|
|
nunchuck_ioctl, /* ioctl */
|
|
|
|
NULL /* poll */
|
2017-11-28 19:10:06 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, NULL /* unlink */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nunchuck_i2c_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nunchuck_i2c_read(FAR struct nunchuck_dev_s *priv,
|
|
|
|
FAR uint8_t *regval, int len)
|
|
|
|
{
|
|
|
|
struct i2c_config_s config;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
/* Set up the I2C configuration */
|
|
|
|
|
|
|
|
config.frequency = NUNCHUCK_I2C_FREQ;
|
|
|
|
config.address = NUNCHUCK_ADDR;
|
|
|
|
config.addrlen = 7;
|
|
|
|
|
|
|
|
/* Read "len" bytes from regaddr */
|
|
|
|
|
|
|
|
ret = i2c_read(priv->i2c_dev, &config, regval, len);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ierr ("i2c_read failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nunchuck_i2c_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nunchuck_i2c_write(FAR struct nunchuck_dev_s *priv,
|
|
|
|
uint8_t const *data, int len)
|
|
|
|
{
|
|
|
|
struct i2c_config_s config;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Set up the I2C configuration */
|
|
|
|
|
|
|
|
config.frequency = NUNCHUCK_I2C_FREQ;
|
|
|
|
config.address = NUNCHUCK_ADDR;
|
|
|
|
config.addrlen = 7;
|
|
|
|
|
|
|
|
/* Write the data */
|
|
|
|
|
|
|
|
ret = i2c_write(priv->i2c_dev, &config, data, len);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ierr("ERROR: i2c_write failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nunchuck_sample(FAR struct nunchuck_dev_s *priv,
|
|
|
|
FAR struct nunchuck_sample_s *buffer)
|
|
|
|
{
|
|
|
|
uint8_t cmd[2];
|
|
|
|
uint8_t data[6];
|
|
|
|
static bool initialized = false;
|
|
|
|
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
/* Start device */
|
|
|
|
|
|
|
|
cmd[0] = 0x40;
|
|
|
|
cmd[1] = 0x00;
|
|
|
|
nunchuck_i2c_write(priv, cmd, 2);
|
|
|
|
|
|
|
|
/* Delay 20ms */
|
|
|
|
|
2021-03-13 11:55:37 +01:00
|
|
|
nxsig_usleep(20 * 1000);
|
2017-11-28 19:10:06 +01:00
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare to read */
|
|
|
|
|
|
|
|
cmd[0] = 0x00;
|
|
|
|
nunchuck_i2c_write(priv, cmd, 1);
|
|
|
|
|
|
|
|
/* Wait */
|
|
|
|
|
|
|
|
nxsig_usleep(1000);
|
|
|
|
|
|
|
|
/* Read data */
|
|
|
|
|
|
|
|
nunchuck_i2c_read(priv, &data[0], 1);
|
|
|
|
|
|
|
|
/* Wait */
|
|
|
|
|
|
|
|
nxsig_usleep(1000);
|
|
|
|
|
|
|
|
/* Wait */
|
|
|
|
|
|
|
|
nxsig_usleep(1000);
|
|
|
|
|
|
|
|
nunchuck_i2c_read(priv, &data[1], 1);
|
|
|
|
|
|
|
|
/* Wait */
|
|
|
|
|
|
|
|
nxsig_usleep(1000);
|
|
|
|
|
|
|
|
nunchuck_i2c_read(priv, &data[2], 1);
|
|
|
|
|
|
|
|
/* Wait */
|
|
|
|
|
|
|
|
nxsig_usleep(1000);
|
|
|
|
|
|
|
|
nunchuck_i2c_read(priv, &data[3], 1);
|
|
|
|
|
|
|
|
/* Wait */
|
|
|
|
|
|
|
|
nxsig_usleep(1000);
|
|
|
|
|
|
|
|
nunchuck_i2c_read(priv, &data[4], 1);
|
|
|
|
|
|
|
|
/* Wait */
|
|
|
|
|
|
|
|
nxsig_usleep(1000);
|
|
|
|
|
|
|
|
nunchuck_i2c_read(priv, &data[5], 1);
|
|
|
|
|
|
|
|
/* Save the sample */
|
|
|
|
|
|
|
|
buffer->js_x = (uint16_t) data[0];
|
|
|
|
buffer->js_y = (uint16_t) data[1];
|
|
|
|
buffer->acc_x = (uint16_t) data[2];
|
|
|
|
buffer->acc_y = (uint16_t) data[3];
|
|
|
|
buffer->acc_z = (uint16_t) data[4];
|
2021-03-13 11:55:37 +01:00
|
|
|
buffer->nck_buttons = (uint8_t) ((data[5] + 1) & 0x03);
|
2017-11-28 19:10:06 +01:00
|
|
|
|
|
|
|
iinfo("X: %03d | Y: %03d | AX: %03d AY: %03d AZ: %03d | B: %d\n",
|
2021-03-13 11:55:37 +01:00
|
|
|
data[0], data[1], data[2], data[3],
|
|
|
|
data[4], ((data[5] + 1) & 0x03));
|
2017-11-28 19:10:06 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nunchuck_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nunchuck_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct nunchuck_dev_s *priv;
|
|
|
|
FAR struct nunchuck_open_s *opriv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(filep && filep->f_inode);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
DEBUGASSERT(inode->i_private);
|
|
|
|
priv = (FAR struct nunchuck_dev_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2017-11-28 19:10:06 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ierr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2017-11-28 19:10:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a new open structure */
|
|
|
|
|
2021-03-13 11:55:37 +01:00
|
|
|
opriv = (FAR struct nunchuck_open_s *)
|
|
|
|
kmm_zalloc(sizeof(struct nunchuck_open_s));
|
2017-11-28 19:10:06 +01:00
|
|
|
if (!opriv)
|
|
|
|
{
|
2020-02-23 09:50:23 +01:00
|
|
|
ierr("ERROR: Failed to allocate open structure\n");
|
2017-11-28 19:10:06 +01:00
|
|
|
ret = -ENOMEM;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2017-11-28 19:10:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Attach the open structure to the device */
|
|
|
|
|
|
|
|
opriv->nck_flink = priv->nck_open;
|
|
|
|
priv->nck_open = opriv;
|
|
|
|
|
|
|
|
/* Attach the open structure to the file structure */
|
|
|
|
|
|
|
|
filep->f_priv = (FAR void *)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->lock);
|
2017-11-28 19:10:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nunchuck_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nunchuck_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct nunchuck_dev_s *priv;
|
|
|
|
FAR struct nunchuck_open_s *opriv;
|
|
|
|
FAR struct nunchuck_open_s *curr;
|
|
|
|
FAR struct nunchuck_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 nunchuck_dev_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 joystick driver at the same time. The rule: don't do
|
|
|
|
* that! It is feeble because we do not really enforce stale pointer
|
|
|
|
* detection anyway.
|
|
|
|
*/
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
closing = opriv->nck_closing;
|
|
|
|
opriv->nck_closing = true;
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
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->lock);
|
2017-11-28 19:10:06 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ierr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2017-11-28 19:10:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the open structure in the list of open structures for the device */
|
|
|
|
|
|
|
|
for (prev = NULL, curr = priv->nck_open;
|
|
|
|
curr && curr != opriv;
|
|
|
|
prev = curr, curr = curr->nck_flink);
|
|
|
|
|
|
|
|
DEBUGASSERT(curr);
|
|
|
|
if (!curr)
|
|
|
|
{
|
|
|
|
ierr("ERROR: Failed to find open entry\n");
|
|
|
|
ret = -ENOENT;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2017-11-28 19:10:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the structure from the device */
|
|
|
|
|
|
|
|
if (prev)
|
|
|
|
{
|
|
|
|
prev->nck_flink = opriv->nck_flink;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->nck_open = opriv->nck_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->lock);
|
2017-11-28 19:10:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nunchuck_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t nunchuck_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct nunchuck_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(filep && filep->f_inode);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
DEBUGASSERT(inode->i_private);
|
|
|
|
priv = (FAR struct nunchuck_dev_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(struct nunchuck_sample_s))
|
|
|
|
{
|
|
|
|
ierr("ERROR: buffer too small: %lu\n", (unsigned long)len);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2017-11-28 19:10:06 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ierr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2017-11-28 19:10:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read and return the current state of the joystick buttons */
|
|
|
|
|
|
|
|
ret = nunchuck_sample(priv, (FAR struct nunchuck_sample_s *)buffer);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
ret = sizeof(struct nunchuck_sample_s);
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2017-11-28 19:10:06 +01:00
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nunchuck_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nunchuck_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct nunchuck_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(filep && filep->f_priv && filep->f_inode);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
DEBUGASSERT(inode->i_private);
|
|
|
|
priv = (FAR struct nunchuck_dev_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2017-11-28 19:10:06 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ierr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2017-11-28 19:10:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the ioctl command */
|
|
|
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* Command: NUNCHUCKIOC_SUPPORTED
|
2021-03-13 11:55:37 +01:00
|
|
|
* Description: Report the set of button events supported by the
|
|
|
|
* hardware;
|
|
|
|
* Argument: A pointer to writeable integer value in which to
|
|
|
|
* return the set of supported buttons.
|
|
|
|
* Return: Zero (OK) on success. Minus one will be returned
|
|
|
|
* on failure with the errno value set appropriately.
|
2017-11-28 19:10:06 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
case NUNCHUCKIOC_SUPPORTED:
|
|
|
|
{
|
|
|
|
FAR int *supported = (FAR int *)((uintptr_t)arg);
|
|
|
|
|
|
|
|
if (supported)
|
|
|
|
{
|
|
|
|
*supported = (NUNCHUCK_BUTTON_Z_BIT | NUNCHUCK_BUTTON_C_BIT);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ierr("ERROR: Unrecognized command: %ld\n", cmd);
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2017-11-28 19:10:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nunchuck_register
|
|
|
|
*
|
|
|
|
* Description:
|
2017-11-28 20:40:07 +01:00
|
|
|
* Register the Nunchuck character driver as the specified device.
|
2017-11-28 19:10:06 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2017-11-28 20:40:07 +01:00
|
|
|
* devname - The name of the Nunchuck joystick device to be registered.
|
|
|
|
* This should be a string of the form "/dev/nunchuckN" where N is the
|
2017-11-28 19:10:06 +01:00
|
|
|
* minor device number.
|
|
|
|
* i2c - An instance of the platform-specific I2C connected to Nunchuck.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-11-28 19:10:06 +01:00
|
|
|
* Zero (OK) is returned on success. Otherwise a negated errno value is
|
|
|
|
* returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nunchuck_register(FAR const char *devname, FAR struct i2c_master_s *i2c)
|
|
|
|
{
|
|
|
|
FAR struct nunchuck_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
iinfo("Enter\n");
|
|
|
|
|
|
|
|
DEBUGASSERT(devname && i2c);
|
|
|
|
|
|
|
|
/* Allocate a new nunchuck driver instance */
|
|
|
|
|
|
|
|
priv = (FAR struct nunchuck_dev_s *)
|
|
|
|
kmm_zalloc(sizeof(struct nunchuck_dev_s));
|
|
|
|
|
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
ierr("ERROR: Failed to allocate device structure\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the i2c device */
|
|
|
|
|
|
|
|
priv->i2c_dev = i2c;
|
|
|
|
|
|
|
|
/* Initialize the new nunchuck driver instance */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&priv->nck_lock);
|
2017-11-28 19:10:06 +01:00
|
|
|
|
|
|
|
/* And register the nunchuck driver */
|
|
|
|
|
|
|
|
ret = register_driver(devname, &nunchuck_fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ierr("ERROR: register_driver failed: %d\n", ret);
|
|
|
|
goto errout_with_priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_priv:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&priv->nck_lock);
|
2017-11-28 19:10:06 +01:00
|
|
|
kmm_free(priv);
|
|
|
|
return ret;
|
|
|
|
}
|