From e51376870f894e1567200aa39d8a9a99b59c7a0d Mon Sep 17 00:00:00 2001 From: liuhongchao Date: Tue, 29 Oct 2024 10:45:01 +0800 Subject: [PATCH 1/3] nuttx: Support for the mouse ioctl interface --- drivers/input/mouse_upper.c | 34 +++++++++++++++++++++++++++++++++- include/nuttx/fs/ioctl.h | 6 ++++++ include/nuttx/input/mouse.h | 27 +++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/drivers/input/mouse_upper.c b/drivers/input/mouse_upper.c index 1a51698457a..62d625e26ab 100644 --- a/drivers/input/mouse_upper.c +++ b/drivers/input/mouse_upper.c @@ -68,6 +68,8 @@ static int mouse_open(FAR struct file *filep); static int mouse_close(FAR struct file *filep); static ssize_t mouse_read(FAR struct file *filep, FAR char *buffer, size_t buflen); +static int mouse_ioctl(FAR struct file *filep, int cmd, + unsigned long arg); static int mouse_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup); @@ -82,7 +84,7 @@ static const struct file_operations g_mouse_fops = mouse_read, /* read */ NULL, /* write */ NULL, /* seek */ - NULL, /* ioctl */ + mouse_ioctl, /* ioctl */ NULL, /* mmap */ NULL, /* truncate */ mouse_poll /* poll */ @@ -217,6 +219,36 @@ static ssize_t mouse_read(FAR struct file *filep, FAR char *buffer, return ret; } +/**************************************************************************** + * Name: mouse_ioctl + ****************************************************************************/ + +static int mouse_ioctl(FAR struct file *filep, int cmd, unsigned long arg) +{ + FAR struct inode *inode = filep->f_inode; + FAR struct mouse_upperhalf_s *upper = inode->i_private; + FAR struct mouse_lowerhalf_s *lower = upper->lower; + int ret; + + ret = nxmutex_lock(&upper->lock); + if (ret < 0) + { + return ret; + } + + if (lower->control) + { + ret = lower->control(lower, cmd, arg); + } + else + { + ret = -ENOTTY; + } + + nxmutex_unlock(&upper->lock); + return ret; +} + /**************************************************************************** * Name: mouse_poll ****************************************************************************/ diff --git a/include/nuttx/fs/ioctl.h b/include/nuttx/fs/ioctl.h index e823e751246..b6e42de6d58 100644 --- a/include/nuttx/fs/ioctl.h +++ b/include/nuttx/fs/ioctl.h @@ -107,6 +107,7 @@ #define _PINCTRLBASE (0x4000) /* Pinctrl driver ioctl commands */ #define _PCIBASE (0x4100) /* Pci ioctl commands */ #define _I3CBASE (0x4200) /* I3C driver ioctl commands */ +#define _MSEIOCBASE (0x4300) /* Mouse ioctl commands */ #define _WLIOCBASE (0x8b00) /* Wireless modules ioctl network commands */ /* boardctl() commands share the same number space */ @@ -369,6 +370,11 @@ #define _TSIOCVALID(c) (_IOC_TYPE(c)==_TSIOCBASE) #define _TSIOC(nr) _IOC(_TSIOCBASE,nr) +/* NuttX mouse ioctl definitions (see nuttx/input/mouse.h) ******************/ + +#define _MSEIOCVALID(c) (_IOC_TYPE(c)==_MSEIOCBASE) +#define _MSEIOC(nr) _IOC(_MSEIOCBASE,nr) + /* NuttX sensor ioctl definitions (see nuttx/sensor/ioctl.h) ****************/ #define _SNIOCVALID(c) (_IOC_TYPE(c)==_SNIOCBASE) diff --git a/include/nuttx/input/mouse.h b/include/nuttx/input/mouse.h index 2f7f3f0ae0d..81c60667c6f 100644 --- a/include/nuttx/input/mouse.h +++ b/include/nuttx/input/mouse.h @@ -37,6 +37,7 @@ ****************************************************************************/ #include +#include /**************************************************************************** * Pre-processor Definitions @@ -50,6 +51,13 @@ #define MOUSE_BUTTON_2 (1 << 1) /* True: Right mouse button pressed */ #define MOUSE_BUTTON_3 (1 << 2) /* True: Middle mouse button pressed */ +/* IOCTL Commands ***********************************************************/ + +/* Common mouse IOCTL commands */ + +#define MSE_FIRST 0x0001 /* First common command */ +#define MSE_NCMDS 1 /* One common commands */ + /**************************************************************************** * Public Types ****************************************************************************/ @@ -75,6 +83,25 @@ struct mouse_report_s struct mouse_lowerhalf_s { FAR void *priv; /* Save the upper half pointer */ + + /************************************************************************** + * Name: control + * + * Description: + * Users can use this interface to implement custom IOCTL. + * + * Arguments: + * lower - The instance of lower half of mouse device. + * cmd - User defined specific command. + * arg - Argument of the specific command. + * + * Return Value: + * Zero(OK) on success; a negated errno value on failure. + * -ENOTTY - The command is not supported. + **************************************************************************/ + + CODE int (*control)(FAR struct mouse_lowerhalf_s *lower, + int cmd, unsigned long arg); }; /**************************************************************************** From eb7d47606994d6460d3d71f83b462f5f4495b352 Mon Sep 17 00:00:00 2001 From: liuhongchao Date: Thu, 31 Oct 2024 16:24:49 +0800 Subject: [PATCH 2/3] nuttx: Support for rpmsgdev custom ioctl --- drivers/misc/rpmsgdev.c | 11 ++++++--- include/nuttx/fs/ioctl.h | 6 ++--- include/nuttx/input/mouse.h | 45 +++++++++++++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/drivers/misc/rpmsgdev.c b/drivers/misc/rpmsgdev.c index 91e5420ed50..2b9eefdb4e3 100644 --- a/drivers/misc/rpmsgdev.c +++ b/drivers/misc/rpmsgdev.c @@ -45,6 +45,7 @@ #include #include #include +#include #include "rpmsgdev.h" @@ -103,7 +104,7 @@ static ssize_t rpmsgdev_write(FAR struct file *filep, FAR const char *buffer, size_t buflen); static off_t rpmsgdev_seek(FAR struct file *filep, off_t offset, int whence); -static ssize_t rpmsgdev_ioctl_arglen(int cmd); +static ssize_t rpmsgdev_ioctl_arglen(int cmd, unsigned long arg); static int rpmsgdev_ioctl(FAR struct file *filep, int cmd, unsigned long arg); static int rpmsgdev_poll(FAR struct file *filep, FAR struct pollfd *fds, @@ -591,6 +592,7 @@ static off_t rpmsgdev_seek(FAR struct file *filep, off_t offset, int whence) * * Parameters: * cmd - the ioctl command + * arg - the ioctl arguments * * Returned Values: * 0 - ioctl command not support @@ -598,7 +600,7 @@ static off_t rpmsgdev_seek(FAR struct file *filep, off_t offset, int whence) * ****************************************************************************/ -static ssize_t rpmsgdev_ioctl_arglen(int cmd) +static ssize_t rpmsgdev_ioctl_arglen(int cmd, unsigned long arg) { switch (cmd) { @@ -624,6 +626,9 @@ static ssize_t rpmsgdev_ioctl_arglen(int cmd) case BATIOC_GET_PROTOCOL: case BATIOC_OPERATE: return sizeof(struct batio_operate_msg_s); + case MSIOC_VENDOR: + return sizeof(struct mouse_vendor_cmd_s) + + ((FAR struct mouse_vendor_cmd_s *)(uintptr_t)arg)->len; default: return -ENOTTY; } @@ -663,7 +668,7 @@ static int rpmsgdev_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Call our internal routine to perform the ioctl */ - arglen = rpmsgdev_ioctl_arglen(cmd); + arglen = rpmsgdev_ioctl_arglen(cmd, arg); if (arglen < 0) { return arglen; diff --git a/include/nuttx/fs/ioctl.h b/include/nuttx/fs/ioctl.h index b6e42de6d58..0f9e80920ed 100644 --- a/include/nuttx/fs/ioctl.h +++ b/include/nuttx/fs/ioctl.h @@ -107,7 +107,7 @@ #define _PINCTRLBASE (0x4000) /* Pinctrl driver ioctl commands */ #define _PCIBASE (0x4100) /* Pci ioctl commands */ #define _I3CBASE (0x4200) /* I3C driver ioctl commands */ -#define _MSEIOCBASE (0x4300) /* Mouse ioctl commands */ +#define _MSIOCBASE (0x4300) /* Mouse ioctl commands */ #define _WLIOCBASE (0x8b00) /* Wireless modules ioctl network commands */ /* boardctl() commands share the same number space */ @@ -372,8 +372,8 @@ /* NuttX mouse ioctl definitions (see nuttx/input/mouse.h) ******************/ -#define _MSEIOCVALID(c) (_IOC_TYPE(c)==_MSEIOCBASE) -#define _MSEIOC(nr) _IOC(_MSEIOCBASE,nr) +#define _MSIOCVALID(c) (_IOC_TYPE(c)==_MSIOCBASE) +#define _MSIOC(nr) _IOC(_MSIOCBASE,nr) /* NuttX sensor ioctl definitions (see nuttx/sensor/ioctl.h) ****************/ diff --git a/include/nuttx/input/mouse.h b/include/nuttx/input/mouse.h index 81c60667c6f..62426d2461e 100644 --- a/include/nuttx/input/mouse.h +++ b/include/nuttx/input/mouse.h @@ -55,8 +55,49 @@ /* Common mouse IOCTL commands */ -#define MSE_FIRST 0x0001 /* First common command */ -#define MSE_NCMDS 1 /* One common commands */ +#define MSIOC_VENDOR _MSIOC(0x0001) /* Vendor-specific commands */ + +#define MSC_FIRST 0x0001 /* First common command */ +#define MSC_NCMDS 1 /* One common commands */ + +/* Vendor-specific command structure + * + * This structure is used to pass vendor-specific commands to the mouse + * driver. The vendor-specific command is identified by the 'cmd' field + * and the length of the data is specified by the 'len' field. The + * data follows the structure in a contiguous block of memory. + * + * The vendor-specific command is defined by the vendor and is not + * standardized. The data format and meaning is defined by the vendor. + * + * The usage is as follows : + * + * struct mse_vendor_data_s + * { + * uint16_t cmd; + * uint16_t len; + * uint16_t data; + * ... ... ... + * }; + * + * struct mse_vendor_data_s cmd_data; + * cmd_data.cmd = VENDOR_CMD_ID; + * cmd_data.data = 12; + * + * struct mouse_vendor_cmd_s *ioctl; + * ioctl = malloc(sizeof(*ioctl) + sizeof(struct mse_vendor_data_s)); + * ioctl->len = sizeof(struct mse_vendor_data_s); + * memcpy(ioctl->data, &cmd_data, sizeof(struct mse_vendor_data_s)); + * + * ioctl(file, MSIOC_VENDOR, ioctl); + * + */ + +struct mouse_vendor_cmd_s +{ + size_t len; + char data[1]; +}; /**************************************************************************** * Public Types From 09f4df2bc22e978987cc6cb11cb467e19cd66d12 Mon Sep 17 00:00:00 2001 From: liuhongchao Date: Tue, 5 Nov 2024 10:55:46 +0800 Subject: [PATCH 3/3] nuxxt: add open and close callback functions for the mouse. --- drivers/input/mouse_upper.c | 12 ++++++++++++ include/nuttx/input/mouse.h | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/drivers/input/mouse_upper.c b/drivers/input/mouse_upper.c index 62d625e26ab..069b47a969b 100644 --- a/drivers/input/mouse_upper.c +++ b/drivers/input/mouse_upper.c @@ -103,6 +103,7 @@ static int mouse_open(FAR struct file *filep) FAR struct mouse_openpriv_s *openpriv; FAR struct inode *inode = filep->f_inode; FAR struct mouse_upperhalf_s *upper = inode->i_private; + FAR struct mouse_lowerhalf_s *lower = upper->lower; int ret; ret = nxmutex_lock(&upper->lock); @@ -131,6 +132,11 @@ static int mouse_open(FAR struct file *filep) nxmutex_init(&openpriv->lock); list_add_tail(&upper->head, &openpriv->node); + if (lower->open && list_is_singular(&openpriv->node)) + { + ret = lower->open(lower); + } + /* Save the buffer node pointer so that it can be used directly * in the read operation. */ @@ -149,6 +155,7 @@ static int mouse_close(FAR struct file *filep) FAR struct mouse_openpriv_s *openpriv = filep->f_priv; FAR struct inode *inode = filep->f_inode; FAR struct mouse_upperhalf_s *upper = inode->i_private; + FAR struct mouse_lowerhalf_s *lower = upper->lower; int ret; ret = nxmutex_lock(&upper->lock); @@ -157,6 +164,11 @@ static int mouse_close(FAR struct file *filep) return ret; } + if (lower->close && list_is_singular(&openpriv->node)) + { + ret = lower->close(lower); + } + list_delete(&openpriv->node); circbuf_uninit(&openpriv->circbuf); nxsem_destroy(&openpriv->waitsem); diff --git a/include/nuttx/input/mouse.h b/include/nuttx/input/mouse.h index 62426d2461e..19970cce8f1 100644 --- a/include/nuttx/input/mouse.h +++ b/include/nuttx/input/mouse.h @@ -143,6 +143,45 @@ struct mouse_lowerhalf_s CODE int (*control)(FAR struct mouse_lowerhalf_s *lower, int cmd, unsigned long arg); + + /************************************************************************** + * Name: open + * + * Description: + * This function pointer is used to open a connection to the mouse driver + * instance. It initializes the mouse and prepares it for subsequent + * interactions with the user. This function typically sets up the state + * of the driver and allocates any necessary resources. + * + * Input Parameters: + * lower - A pointer to the instance of the lower half mouse driver. + * filep - A pointer to the file structure representing the user. + * + * Returned Value: + * It returns zero (OK) on success; a negative errno value on failure. + **************************************************************************/ + + CODE int (*open)(FAR struct mouse_lowerhalf_s *lower); + + /************************************************************************** + * Name: close + * + * Description: + * This function pointer is used to close the connection to the mouse + * driver instance. It performs any necessary cleanup operations, such as + * releasing resources and resetting the state of the mouse driver, + * before ending theinteraction with the user. + * + * Input Parameters: + * lower - A pointer to the instance of the lower half mouse driver. + * filep - A pointer to the file structure representing the user closing + * the mouse connection. + * + * Returned Value: + * Returns zero (OK) on success; a negative errno value on failure. + **************************************************************************/ + + CODE int (*close)(FAR struct mouse_lowerhalf_s *lower); }; /****************************************************************************