2014-02-09 20:49:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/usbhost/usbhost_hidmouse.c
|
|
|
|
*
|
2020-03-30 22:20:53 +02: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
|
|
|
|
*
|
|
|
|
* 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.
|
2014-02-09 20:49:31 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <poll.h>
|
2015-04-25 19:17:57 +02:00
|
|
|
#include <signal.h>
|
2014-02-09 20:49:31 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fixedmath.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
#include <nuttx/irq.h>
|
2014-02-09 20:49:31 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2015-04-26 01:05:30 +02:00
|
|
|
#include <nuttx/kthread.h>
|
2014-02-09 20:49:31 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/wqueue.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2016-11-03 18:00:47 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
#include <nuttx/usb/usb.h>
|
|
|
|
#include <nuttx/usb/usbhost.h>
|
|
|
|
#include <nuttx/usb/hid.h>
|
2015-04-21 20:17:49 +02:00
|
|
|
#include <nuttx/usb/usbhost_devaddr.h>
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2014-02-10 17:14:22 +01:00
|
|
|
#ifdef CONFIG_HIDMOUSE_TSCIF
|
|
|
|
# include <nuttx/input/touchscreen.h>
|
|
|
|
#else
|
|
|
|
# include <nuttx/input/mouse.h>
|
|
|
|
#endif
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Don't compile if prerequisites are not met */
|
|
|
|
|
2019-02-11 19:09:26 +01:00
|
|
|
#if defined(CONFIG_USBHOST) && !defined(CONFIG_USBHOST_INT_DISABLE)
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2019-05-22 22:14:15 +02:00
|
|
|
|
2014-02-09 20:49:31 +01:00
|
|
|
/* Configuration ************************************************************/
|
2019-05-22 02:57:54 +02:00
|
|
|
|
2014-02-09 20:49:31 +01:00
|
|
|
/* Worker thread is needed, unfortunately, to handle some cornercase failure
|
|
|
|
* conditions. This is kind of wasteful and begs for a re-design.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_SCHED_WORKQUEUE
|
|
|
|
# warning "Worker thread support is required (CONFIG_SCHED_WORKQUEUE)"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Provide some default values for other configuration settings */
|
|
|
|
|
|
|
|
#ifndef CONFIG_HIDMOUSE_XMAX
|
|
|
|
# define CONFIG_HIDMOUSE_XMAX 320
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define HIDMOUSE_XMAX_B16 (CONFIG_HIDMOUSE_XMAX << 16)
|
|
|
|
|
|
|
|
#ifndef CONFIG_HIDMOUSE_YMAX
|
|
|
|
# define CONFIG_HIDMOUSE_YMAX 240
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define HIDMOUSE_YMAX_B16 (CONFIG_HIDMOUSE_YMAX << 16)
|
|
|
|
|
|
|
|
#ifndef CONFIG_HIDMOUSE_XSCALE
|
|
|
|
# define CONFIG_HIDMOUSE_XSCALE 0x00010000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_HIDMOUSE_YSCALE
|
|
|
|
# define CONFIG_HIDMOUSE_YSCALE 0x00010000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_HIDMOUSE_XTHRESH
|
|
|
|
# define CONFIG_HIDMOUSE_XTHRESH 12
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define HIDMOUSE_XTHRESH_B16 (CONFIG_HIDMOUSE_XTHRESH << 16)
|
|
|
|
|
|
|
|
#ifndef CONFIG_HIDMOUSE_YTHRESH
|
|
|
|
# define CONFIG_HIDMOUSE_YTHRESH 12
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define HIDMOUSE_YTHRESH_B16 (CONFIG_HIDMOUSE_YTHRESH << 16)
|
|
|
|
|
2014-02-10 18:53:37 +01:00
|
|
|
#ifdef CONFIG_HIDMOUSE_TSCIF
|
2021-04-08 11:02:31 +02:00
|
|
|
# undef CONFIG_INPUT_MOUSE_WHEEL
|
2014-02-10 18:53:37 +01:00
|
|
|
#endif
|
|
|
|
|
2021-04-08 11:02:31 +02:00
|
|
|
#ifdef CONFIG_INPUT_MOUSE_WHEEL
|
2014-02-10 18:53:37 +01:00
|
|
|
|
|
|
|
# ifndef CONFIG_HIDMOUSE_WMAX
|
|
|
|
# define CONFIG_HIDMOUSE_WMAX 100
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# define HIDMOUSE_WMAX_B16 (CONFIG_HIDMOUSE_WMAX << 16)
|
|
|
|
|
|
|
|
# ifndef CONFIG_HIDMOUSE_WSCALE
|
|
|
|
# define CONFIG_HIDMOUSE_WSCALE 0x00010000
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifndef CONFIG_HIDMOUSE_WTHRESH
|
|
|
|
# define CONFIG_HIDMOUSE_WTHRESH 1
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# define HIDMOUSE_WTHRESH_B16 (CONFIG_HIDMOUSE_WTHRESH << 16)
|
|
|
|
|
2021-04-08 11:02:31 +02:00
|
|
|
#endif /* CONFIG_INPUT_MOUSE_WHEEL */
|
2014-02-10 18:53:37 +01:00
|
|
|
|
2014-02-09 20:49:31 +01:00
|
|
|
#ifndef CONFIG_HIDMOUSE_DEFPRIO
|
|
|
|
# define CONFIG_HIDMOUSE_DEFPRIO 50
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_HIDMOUSE_STACKSIZE
|
|
|
|
# define CONFIG_HIDMOUSE_STACKSIZE 1024
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_HIDMOUSE_NPOLLWAITERS
|
|
|
|
# define CONFIG_HIDMOUSE_NPOLLWAITERS 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* This is a value for the threshold that guarantees a big difference on the
|
|
|
|
* first left button (but can't overflow).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define INVALID_POSITION_B16 (0x7fffffff)
|
|
|
|
|
|
|
|
/* Driver support ***********************************************************/
|
2019-05-22 02:57:54 +02:00
|
|
|
|
2014-02-09 20:49:31 +01:00
|
|
|
/* This format is used to construct the /dev/mouse[n] device driver path. It
|
|
|
|
* defined here so that it will be used consistently in all places.
|
|
|
|
*/
|
|
|
|
|
2014-02-10 17:14:22 +01:00
|
|
|
#ifdef CONFIG_HIDMOUSE_TSCIF
|
|
|
|
# define DEV_FORMAT "/dev/input%d"
|
2022-03-05 08:49:25 +01:00
|
|
|
# define DEV_NAMELEN 14
|
2014-02-10 17:14:22 +01:00
|
|
|
#else
|
|
|
|
# define DEV_FORMAT "/dev/mouse%d"
|
2022-03-05 08:49:25 +01:00
|
|
|
# define DEV_NAMELEN 14
|
2014-02-10 17:14:22 +01:00
|
|
|
#endif
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Used in usbhost_cfgdesc() */
|
|
|
|
|
|
|
|
#define USBHOST_IFFOUND 0x01 /* Required I/F descriptor found */
|
|
|
|
#define USBHOST_EPINFOUND 0x02 /* Required interrupt IN EP descriptor found */
|
|
|
|
#define USBHOST_ALLFOUND (USBHOST_IFFOUND|USBHOST_EPINFOUND)
|
|
|
|
|
|
|
|
#define USBHOST_MAX_CREFS 0x7fff
|
|
|
|
|
|
|
|
/* Debug ********************************************************************/
|
2019-05-22 02:57:54 +02:00
|
|
|
|
2014-02-09 20:49:31 +01:00
|
|
|
/* Both CONFIG_DEBUG_INPUT and CONFIG_DEBUG_USB could apply to this file.
|
|
|
|
* We assume here that CONFIG_DEBUG_INPUT might be enabled separately, but
|
|
|
|
* CONFIG_DEBUG_USB implies both.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DEBUG_INPUT
|
2016-06-11 23:50:49 +02:00
|
|
|
# undef ierr
|
|
|
|
# define ierr uerr
|
2016-06-11 19:59:51 +02:00
|
|
|
# undef iinfo
|
|
|
|
# define iinfo uinfo
|
2014-02-09 20:49:31 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-10 17:14:22 +01:00
|
|
|
#ifdef CONFIG_HIDMOUSE_TSCIF
|
2014-02-09 20:49:31 +01:00
|
|
|
/* This describes the state of one event */
|
|
|
|
|
|
|
|
enum mouse_button_e
|
|
|
|
{
|
|
|
|
BUTTON_NONE = 0, /* No button pressed */
|
|
|
|
BUTTON_PRESSED, /* Left buffer pressed */
|
|
|
|
BUTTON_MOVE, /* Same button press, possibly different position */
|
|
|
|
BUTTON_RELEASED /* Left button released */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This structure describes the results of one mouse sample. Since this
|
|
|
|
* emulates a touchscreen, only the mouse left button is used.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct mouse_sample_s
|
|
|
|
{
|
|
|
|
uint8_t id; /* Sampled touch point ID */
|
|
|
|
uint8_t event; /* Button state (see enum mouse_button_e) */
|
|
|
|
bool valid; /* True: x,y contain valid, sampled data */
|
|
|
|
uint16_t x; /* Measured X position */
|
|
|
|
uint16_t y; /* Measured Y position */
|
|
|
|
};
|
|
|
|
|
2014-02-10 17:14:22 +01:00
|
|
|
#else
|
|
|
|
/* This structure summarizes the mouse report */
|
|
|
|
|
|
|
|
struct mouse_sample_s
|
|
|
|
{
|
|
|
|
uint8_t buttons; /* Button state (see MOUSE_BUTTON_* definitions) */
|
2014-02-10 18:53:37 +01:00
|
|
|
uint16_t x; /* Accumulated X position */
|
|
|
|
uint16_t y; /* Accumulated Y position */
|
2021-04-08 11:02:31 +02:00
|
|
|
#ifdef CONFIG_INPUT_MOUSE_WHEEL
|
2014-02-10 18:53:37 +01:00
|
|
|
uint16_t wheel; /* Reported wheel position */
|
|
|
|
#endif
|
2014-02-10 17:14:22 +01:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2014-02-09 20:49:31 +01:00
|
|
|
/* This structure contains the internal, private state of the USB host
|
|
|
|
* mouse storage class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct usbhost_state_s
|
|
|
|
{
|
|
|
|
/* This is the externally visible portion of the state */
|
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
struct usbhost_class_s usbclass;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
/* This is an instance of the USB host driver
|
|
|
|
* bound to this class instance
|
|
|
|
*/
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
struct usbhost_driver_s *drvr;
|
|
|
|
|
|
|
|
/* The remainder of the fields are provide o the mouse class driver */
|
|
|
|
|
|
|
|
volatile bool disconnected; /* TRUE: Device has been disconnected */
|
|
|
|
volatile bool polling; /* TRUE: Poll thread is running */
|
|
|
|
volatile bool open; /* TRUE: The mouse device is open */
|
|
|
|
volatile bool valid; /* TRUE: New sample data is available */
|
|
|
|
uint8_t devno; /* Minor number in the /dev/mouse[n] device */
|
|
|
|
uint8_t nwaiters; /* Number of threads waiting for mouse data */
|
2014-02-10 17:14:22 +01:00
|
|
|
#ifdef CONFIG_HIDMOUSE_TSCIF
|
2014-02-09 20:49:31 +01:00
|
|
|
uint8_t id; /* Current "touch" point ID */
|
2014-02-10 17:14:22 +01:00
|
|
|
#else
|
|
|
|
uint8_t buttons; /* Current state of the mouse buttons */
|
|
|
|
#endif
|
2014-02-09 20:49:31 +01:00
|
|
|
int16_t crefs; /* Reference count on the driver instance */
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t lock; /* Used to maintain mutual exclusive access */
|
2014-02-09 20:49:31 +01:00
|
|
|
sem_t waitsem; /* Used to wait for mouse data */
|
|
|
|
FAR uint8_t *tbuffer; /* The allocated transfer buffer */
|
2014-02-10 00:40:23 +01:00
|
|
|
b16_t xaccum; /* Current integrated X position */
|
|
|
|
b16_t yaccum; /* Current integrated Y position */
|
2014-02-09 20:49:31 +01:00
|
|
|
b16_t xlast; /* Last reported X position */
|
|
|
|
b16_t ylast; /* Last reported Y position */
|
2021-04-08 11:02:31 +02:00
|
|
|
#ifdef CONFIG_INPUT_MOUSE_WHEEL
|
2014-02-10 18:53:37 +01:00
|
|
|
b16_t waccum; /* Current integrated while position */
|
|
|
|
b16_t wlast; /* Last reported wheel position */
|
|
|
|
#endif
|
2014-02-09 20:49:31 +01:00
|
|
|
size_t tbuflen; /* Size of the allocated transfer buffer */
|
|
|
|
pid_t pollpid; /* PID of the poll task */
|
|
|
|
struct work_s work; /* For cornercase error handling by the worker thread */
|
2014-02-09 23:50:09 +01:00
|
|
|
struct mouse_sample_s sample; /* Last sampled mouse data */
|
2014-02-09 20:49:31 +01:00
|
|
|
usbhost_ep_t epin; /* Interrupt IN endpoint */
|
|
|
|
|
|
|
|
/* The following is a list if poll structures of threads waiting for
|
|
|
|
* driver events. The 'struct pollfd' reference for each open is also
|
|
|
|
* retained in the f_priv field of the 'struct file'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct pollfd *fds[CONFIG_HIDMOUSE_NPOLLWAITERS];
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Memory allocation services */
|
|
|
|
|
|
|
|
static inline FAR struct usbhost_state_s *usbhost_allocclass(void);
|
2015-01-11 15:05:09 +01:00
|
|
|
static inline void usbhost_freeclass(FAR struct usbhost_state_s *usbclass);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Device name management */
|
|
|
|
|
|
|
|
static int usbhost_allocdevno(FAR struct usbhost_state_s *priv);
|
|
|
|
static void usbhost_freedevno(FAR struct usbhost_state_s *priv);
|
2014-02-10 18:53:37 +01:00
|
|
|
static inline void usbhost_mkdevname(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR char *devname);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Mouse polling thread */
|
|
|
|
|
|
|
|
static void usbhost_destroy(FAR void *arg);
|
|
|
|
static void usbhost_notify(FAR struct usbhost_state_s *priv);
|
2014-02-10 17:14:22 +01:00
|
|
|
static void usbhost_position(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR struct usbhid_mousereport_s *rpt);
|
|
|
|
#ifdef CONFIG_HIDMOUSE_TSCIF
|
|
|
|
static bool usbhost_touchscreen(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR struct usbhid_mousereport_s *rpt);
|
|
|
|
#endif
|
2014-02-10 18:53:37 +01:00
|
|
|
static bool usbhost_threshold(FAR struct usbhost_state_s *priv);
|
2014-02-09 20:49:31 +01:00
|
|
|
static int usbhost_mouse_poll(int argc, char *argv[]);
|
|
|
|
static int usbhost_sample(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR struct mouse_sample_s *sample);
|
|
|
|
static int usbhost_waitsample(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR struct mouse_sample_s *sample);
|
|
|
|
|
|
|
|
/* Helpers for usbhost_connect() */
|
|
|
|
|
|
|
|
static inline int usbhost_cfgdesc(FAR struct usbhost_state_s *priv,
|
2014-02-10 18:53:37 +01:00
|
|
|
FAR const uint8_t *configdesc,
|
2015-04-21 00:21:13 +02:00
|
|
|
int desclen);
|
2014-02-09 20:49:31 +01:00
|
|
|
static inline int usbhost_devinit(FAR struct usbhost_state_s *priv);
|
|
|
|
|
|
|
|
/* (Little Endian) Data helpers */
|
|
|
|
|
|
|
|
static inline uint16_t usbhost_getle16(const uint8_t *val);
|
|
|
|
|
|
|
|
/* Transfer descriptor memory management */
|
|
|
|
|
|
|
|
static inline int usbhost_tdalloc(FAR struct usbhost_state_s *priv);
|
|
|
|
static inline int usbhost_tdfree(FAR struct usbhost_state_s *priv);
|
|
|
|
|
|
|
|
/* struct usbhost_registry_s methods */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static FAR struct usbhost_class_s *
|
|
|
|
usbhost_create(FAR struct usbhost_hubport_s *hport,
|
|
|
|
FAR const struct usbhost_id_s *id);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* struct usbhost_class_s methods */
|
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
static int usbhost_connect(FAR struct usbhost_class_s *usbclass,
|
2015-04-21 00:21:13 +02:00
|
|
|
FAR const uint8_t *configdesc, int desclen);
|
2015-01-11 15:05:09 +01:00
|
|
|
static int usbhost_disconnected(FAR struct usbhost_class_s *usbclass);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Driver methods. We export the mouse as a standard character driver */
|
|
|
|
|
|
|
|
static int usbhost_open(FAR struct file *filep);
|
|
|
|
static int usbhost_close(FAR struct file *filep);
|
|
|
|
static ssize_t usbhost_read(FAR struct file *filep,
|
|
|
|
FAR char *buffer, size_t len);
|
|
|
|
static ssize_t usbhost_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer, size_t len);
|
|
|
|
static int usbhost_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure provides the registry entry ID information that will be
|
|
|
|
* used to associate the USB host mouse class driver to a connected USB
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
|
2019-06-20 14:49:56 +02:00
|
|
|
static const struct usbhost_id_s g_hidmouse_id =
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2022-01-10 15:51:17 +01:00
|
|
|
USB_CLASS_HID, /* base */
|
2014-02-09 20:49:31 +01:00
|
|
|
USBHID_SUBCLASS_BOOTIF, /* subclass */
|
2022-01-10 15:51:17 +01:00
|
|
|
USBHID_PROTOCOL_MOUSE, /* proto */
|
|
|
|
0, /* vid */
|
|
|
|
0 /* pid */
|
2014-02-09 20:49:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This is the USB host storage class's registry entry */
|
|
|
|
|
|
|
|
static struct usbhost_registry_s g_hidmouse =
|
|
|
|
{
|
2022-01-10 15:51:17 +01:00
|
|
|
NULL, /* flink */
|
|
|
|
usbhost_create, /* create */
|
|
|
|
1, /* nids */
|
|
|
|
&g_hidmouse_id /* id[] */
|
2014-02-09 20:49:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct file_operations g_hidmouse_fops =
|
|
|
|
{
|
2022-01-10 15:51:17 +01:00
|
|
|
usbhost_open, /* open */
|
|
|
|
usbhost_close, /* close */
|
|
|
|
usbhost_read, /* read */
|
|
|
|
usbhost_write, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
NULL, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2022-01-10 15:51:17 +01:00
|
|
|
usbhost_poll /* poll */
|
2014-02-09 20:49:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This is a bitmap that is used to allocate device names /dev/mouse0-31. */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static uint32_t g_devinuse;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* The following are used to managed the class creation operation */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static mutex_t g_lock = NXMUTEX_INITIALIZER;
|
|
|
|
static sem_t g_syncsem = SEM_INITIALIZER(0);
|
2022-09-06 08:18:45 +02:00
|
|
|
static FAR struct usbhost_state_s *g_priv;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_allocclass
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is really part of the logic that implements the create() method
|
|
|
|
* of struct usbhost_registry_s. This function allocates memory for one
|
|
|
|
* new class instance.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-02-09 20:49:31 +01:00
|
|
|
* On success, this function will return a non-NULL instance of struct
|
|
|
|
* usbhost_class_s. NULL is returned on failure; this function will
|
|
|
|
* will fail only if there are insufficient resources to create another
|
|
|
|
* USB host class instance.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline FAR struct usbhost_state_s *usbhost_allocclass(void)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(!up_interrupt_context());
|
2020-03-30 22:20:53 +02:00
|
|
|
|
|
|
|
priv = (FAR struct usbhost_state_s *)
|
|
|
|
kmm_malloc(sizeof(struct usbhost_state_s));
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Allocated: %p\n", priv);
|
2014-02-09 20:49:31 +01:00
|
|
|
return priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_freeclass
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a class instance previously allocated by usbhost_allocclass().
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2015-01-11 15:05:09 +01:00
|
|
|
* usbclass - A reference to the class instance to be freed.
|
2014-02-09 20:49:31 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-02-09 20:49:31 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
static inline void usbhost_freeclass(FAR struct usbhost_state_s *usbclass)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2015-01-11 15:05:09 +01:00
|
|
|
DEBUGASSERT(usbclass != NULL);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Free the class instance. */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Freeing: %p\n", usbclass);
|
2020-04-07 16:31:47 +02:00
|
|
|
kmm_free(usbclass);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: Device name management
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Some tiny functions to coordinate management of device names.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_allocdevno(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
int devno;
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2014-02-09 20:49:31 +01:00
|
|
|
for (devno = 0; devno < 26; devno++)
|
|
|
|
{
|
|
|
|
uint32_t bitno = 1 << devno;
|
|
|
|
if ((g_devinuse & bitno) == 0)
|
|
|
|
{
|
|
|
|
g_devinuse |= bitno;
|
|
|
|
priv->devno = devno;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2014-02-09 20:49:31 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2014-02-09 20:49:31 +01:00
|
|
|
return -EMFILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usbhost_freedevno(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
|
|
|
int devno = priv->devno;
|
|
|
|
|
|
|
|
if (devno >= 0 && devno < 26)
|
|
|
|
{
|
2016-02-14 14:32:58 +01:00
|
|
|
irqstate_t flags = enter_critical_section();
|
2014-02-09 20:49:31 +01:00
|
|
|
g_devinuse &= ~(1 << devno);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 18:53:37 +01:00
|
|
|
static inline void usbhost_mkdevname(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR char *devname)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->devno);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_destroy
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The USB device has been disconnected and the reference count on the USB
|
|
|
|
* host class instance has gone to 1.. Time to destroy the USB host class
|
|
|
|
* instance.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - A reference to the class instance to be destroyed.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-02-09 20:49:31 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_destroy(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv = (FAR struct usbhost_state_s *)arg;
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2014-02-09 20:49:31 +01:00
|
|
|
char devname[DEV_NAMELEN];
|
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL);
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("crefs: %d\n", priv->crefs);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
hport = priv->usbclass.hport;
|
2015-04-21 00:21:13 +02:00
|
|
|
|
2014-02-09 20:49:31 +01:00
|
|
|
/* Unregister the driver */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Unregister driver\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
usbhost_mkdevname(priv, devname);
|
2020-01-02 17:49:34 +01:00
|
|
|
unregister_driver(devname);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Release the device name used by this connection */
|
|
|
|
|
|
|
|
usbhost_freedevno(priv);
|
|
|
|
|
|
|
|
/* Free the interrupt endpoints */
|
|
|
|
|
|
|
|
if (priv->epin)
|
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
DRVR_EPFREE(hport->drvr, priv->epin);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free any transfer buffers */
|
|
|
|
|
|
|
|
usbhost_tdfree(priv);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Destroy the mutex & semaphores */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&priv->lock);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&priv->waitsem);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Disconnect the USB host device */
|
|
|
|
|
2015-05-02 18:44:18 +02:00
|
|
|
DRVR_DISCONNECT(hport->drvr, hport);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-26 18:05:15 +02:00
|
|
|
/* Free the function address assigned to this device */
|
|
|
|
|
|
|
|
usbhost_devaddr_destroy(hport, hport->funcaddr);
|
|
|
|
hport->funcaddr = 0;
|
|
|
|
|
|
|
|
/* And free the class instance. */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
usbhost_freeclass(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_notify
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Wake any threads waiting for mouse data
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2014-02-09 20:49:31 +01:00
|
|
|
* priv - A reference to the mouse state structure.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_notify(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
|
|
|
/* If there are threads waiting for read data, then signal one of them
|
|
|
|
* that the read data is available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->nwaiters > 0)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->waitsem);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
/* If there are threads waiting on poll() for mouse data to become
|
|
|
|
* available, then wake them up now. NOTE: we wake up all waiting
|
|
|
|
* threads because we do not know that they are going to do. If they
|
|
|
|
* all try to read the data, then some make end up blocking after all.
|
2014-02-09 20:49:31 +01:00
|
|
|
*/
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(priv->fds, CONFIG_HIDMOUSE_NPOLLWAITERS, POLLIN);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 17:14:22 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_position
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Integrate the current mouse displacement to get the updated mouse
|
|
|
|
* position.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to the mouse state structure.
|
|
|
|
* rpt - The new mouse report data.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_position(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR struct usbhid_mousereport_s *rpt)
|
|
|
|
{
|
2014-02-10 18:53:37 +01:00
|
|
|
int32_t disp;
|
|
|
|
b16_t pos;
|
2014-02-10 17:14:22 +01:00
|
|
|
|
|
|
|
/* The following logic performs an constant integration of the mouse X/Y
|
|
|
|
* displacement data in order to keep the X/Y positional data current.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Sign extend the mouse X position. We do this manually because some
|
|
|
|
* architectures do not support signed character types and some compilers
|
|
|
|
* may be configured to treat all characters as unsigned.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_HIDMOUSE_SWAPXY
|
2014-02-10 18:53:37 +01:00
|
|
|
disp = rpt->ydisp;
|
2014-02-10 17:14:22 +01:00
|
|
|
if ((rpt->ydisp & 0x80) != 0)
|
|
|
|
{
|
2014-02-10 18:53:37 +01:00
|
|
|
disp |= 0xffffff00;
|
2014-02-10 17:14:22 +01:00
|
|
|
}
|
|
|
|
#else
|
2014-02-10 18:53:37 +01:00
|
|
|
disp = rpt->xdisp;
|
2014-02-10 17:14:22 +01:00
|
|
|
if ((rpt->xdisp & 0x80) != 0)
|
|
|
|
{
|
2014-02-10 18:53:37 +01:00
|
|
|
disp |= 0xffffff00;
|
2014-02-10 17:14:22 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Scale the X displacement and determine the new X position */
|
|
|
|
|
2014-02-10 18:53:37 +01:00
|
|
|
pos = priv->xaccum + CONFIG_HIDMOUSE_XSCALE * disp;
|
2014-02-10 17:14:22 +01:00
|
|
|
|
|
|
|
/* Make sure that the scaled X position does not become negative or exceed
|
|
|
|
* the maximum.
|
|
|
|
*/
|
|
|
|
|
2014-02-10 18:53:37 +01:00
|
|
|
if (pos > HIDMOUSE_XMAX_B16)
|
2014-02-10 17:14:22 +01:00
|
|
|
{
|
2014-02-10 18:53:37 +01:00
|
|
|
pos = HIDMOUSE_XMAX_B16;
|
2014-02-10 17:14:22 +01:00
|
|
|
}
|
2014-02-10 18:53:37 +01:00
|
|
|
else if (pos < 0)
|
2014-02-10 17:14:22 +01:00
|
|
|
{
|
2014-02-10 18:53:37 +01:00
|
|
|
pos = 0;
|
2014-02-10 17:14:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the updated X position */
|
|
|
|
|
2014-02-10 18:53:37 +01:00
|
|
|
priv->xaccum = pos;
|
2014-02-10 17:14:22 +01:00
|
|
|
|
|
|
|
/* Do the same for the Y position */
|
|
|
|
|
|
|
|
#ifdef CONFIG_HIDMOUSE_SWAPXY
|
2014-02-10 18:53:37 +01:00
|
|
|
disp = rpt->xdisp;
|
2014-02-10 17:14:22 +01:00
|
|
|
if ((rpt->xdisp & 0x80) != 0)
|
|
|
|
{
|
2014-02-10 18:53:37 +01:00
|
|
|
disp |= 0xffffff00;
|
2014-02-10 17:14:22 +01:00
|
|
|
}
|
|
|
|
#else
|
2014-02-10 18:53:37 +01:00
|
|
|
disp = rpt->ydisp;
|
2014-02-10 17:14:22 +01:00
|
|
|
if ((rpt->ydisp & 0x80) != 0)
|
|
|
|
{
|
2014-02-10 18:53:37 +01:00
|
|
|
disp |= 0xffffff00;
|
2014-02-10 17:14:22 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-10 18:53:37 +01:00
|
|
|
pos = priv->yaccum + CONFIG_HIDMOUSE_YSCALE * disp;
|
2014-02-10 17:14:22 +01:00
|
|
|
|
2014-02-10 18:53:37 +01:00
|
|
|
if (pos > HIDMOUSE_YMAX_B16)
|
2014-02-10 17:14:22 +01:00
|
|
|
{
|
2014-02-10 18:53:37 +01:00
|
|
|
pos = HIDMOUSE_YMAX_B16;
|
2014-02-10 17:14:22 +01:00
|
|
|
}
|
2014-02-10 18:53:37 +01:00
|
|
|
else if (pos < 0)
|
|
|
|
{
|
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->yaccum = pos;
|
|
|
|
|
2021-04-08 11:02:31 +02:00
|
|
|
#ifdef CONFIG_INPUT_MOUSE_WHEEL
|
2014-02-10 18:53:37 +01:00
|
|
|
/* Do the same for the wheel position */
|
|
|
|
|
|
|
|
disp = rpt->wdisp;
|
|
|
|
if ((rpt->wdisp & 0x80) != 0)
|
2014-02-10 17:14:22 +01:00
|
|
|
{
|
2014-02-10 18:53:37 +01:00
|
|
|
disp |= 0xffffff00;
|
2014-02-10 17:14:22 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 18:53:37 +01:00
|
|
|
pos = priv->waccum + CONFIG_HIDMOUSE_WSCALE * disp;
|
|
|
|
|
|
|
|
if (pos > HIDMOUSE_WMAX_B16)
|
|
|
|
{
|
|
|
|
pos = HIDMOUSE_WMAX_B16;
|
|
|
|
}
|
|
|
|
else if (pos < 0)
|
|
|
|
{
|
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->waccum = pos;
|
|
|
|
#endif
|
2014-02-10 17:14:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_touchscreen
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Execute the (emulated) touchscreen press/drag/release state machine.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to the mouse state structure.
|
|
|
|
* rpt - The new mouse report data.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* False if the mouse data should not be reported.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_HIDMOUSE_TSCIF
|
|
|
|
static bool usbhost_touchscreen(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR struct usbhid_mousereport_s *rpt)
|
|
|
|
{
|
|
|
|
/* Check if the left button is pressed */
|
|
|
|
|
|
|
|
if ((rpt->buttons & USBHID_MOUSEIN_BUTTON1) == 0)
|
|
|
|
{
|
|
|
|
/* The left button is not pressed.. reset thresholding variables. */
|
|
|
|
|
|
|
|
priv->xlast = INVALID_POSITION_B16;
|
|
|
|
priv->ylast = INVALID_POSITION_B16;
|
|
|
|
|
|
|
|
/* Ignore the report if the button was not pressed last time
|
|
|
|
* (BUTTON_NONE == button released and already reported;
|
|
|
|
* BUTTON_RELEASED == button released, but not yet reported)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->sample.event == BUTTON_NONE ||
|
|
|
|
priv->sample.event == BUTTON_RELEASED)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The left button has just been released. NOTE: We know from a
|
|
|
|
* previous test, that this is a button release condition. This will
|
|
|
|
* be changed to BUTTON_NONE after the button release has been
|
|
|
|
* reported.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->sample.event = BUTTON_RELEASED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It is a left button press event. If the last button release event has
|
|
|
|
* not been processed yet, then we have to ignore the button press event
|
|
|
|
* (or else it will look like a drag event)
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (priv->sample.event == BUTTON_RELEASED)
|
|
|
|
{
|
|
|
|
/* If we have not yet processed the button release event, then we
|
|
|
|
* cannot handle this button press event. We will have to discard the
|
|
|
|
* data and wait for the next sample.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle left-button down events */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If this is the first left button press report, then report that
|
|
|
|
* event. If event == BUTTON_PRESSED, it will be set to set to
|
|
|
|
* BUTTON_MOVE after the button press is first sampled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->sample.event != BUTTON_MOVE)
|
|
|
|
{
|
|
|
|
/* First button press */
|
|
|
|
|
|
|
|
priv->sample.event = BUTTON_PRESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, perform a thresholding operation so that the results
|
|
|
|
* will be more stable. If the difference from the last sample is
|
|
|
|
* small, then ignore the event.
|
|
|
|
*/
|
|
|
|
|
2014-02-10 18:53:37 +01:00
|
|
|
else if (!usbhost_xythreshold(priv))
|
2014-02-10 17:14:22 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We get here:
|
|
|
|
*
|
|
|
|
* (1) When the left button is just release,
|
|
|
|
* (2) When the left button is first pressed, or
|
|
|
|
* (3) When the left button is held and some significant 'dragging'
|
|
|
|
* has occurred.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_threshold
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if the current mouse position differs from the previous mouse
|
|
|
|
* position by a threshold amount.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to the mouse state structure.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* True if the mouse position is significantly different from the last
|
|
|
|
* reported mouse position.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-10 18:53:37 +01:00
|
|
|
static bool usbhost_threshold(FAR struct usbhost_state_s *priv)
|
2014-02-10 17:14:22 +01:00
|
|
|
{
|
|
|
|
#if CONFIG_HIDMOUSE_XTHRESH > 0 && CONFIG_HIDMOUSE_YTHRESH > 0
|
2014-02-10 18:53:37 +01:00
|
|
|
b16_t pos;
|
|
|
|
b16_t diff;
|
2014-02-10 17:14:22 +01:00
|
|
|
|
|
|
|
/* Get the difference in the X position from the last report */
|
|
|
|
|
2014-02-10 18:53:37 +01:00
|
|
|
pos = priv->xaccum;
|
|
|
|
if (pos > priv->xlast)
|
2014-02-10 17:14:22 +01:00
|
|
|
{
|
2014-02-10 18:53:37 +01:00
|
|
|
diff = pos - priv->xlast;
|
2014-02-10 17:14:22 +01:00
|
|
|
}
|
|
|
|
else
|
2020-01-03 15:34:44 +01:00
|
|
|
{
|
2014-02-10 18:53:37 +01:00
|
|
|
diff = priv->xlast - pos;
|
2020-01-03 15:34:44 +01:00
|
|
|
}
|
2014-02-10 17:14:22 +01:00
|
|
|
|
|
|
|
/* Check if the X difference exceeds the report threshold */
|
|
|
|
|
2014-02-10 18:53:37 +01:00
|
|
|
if (diff >= HIDMOUSE_XTHRESH_B16)
|
2014-02-10 17:14:22 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Little or no change in the X direction, check the Y direction. */
|
|
|
|
|
2014-02-10 18:53:37 +01:00
|
|
|
pos = priv->yaccum;
|
|
|
|
if (pos > priv->ylast)
|
2014-02-10 17:14:22 +01:00
|
|
|
{
|
2014-02-10 18:53:37 +01:00
|
|
|
diff = pos - priv->ylast;
|
2014-02-10 17:14:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-10 18:53:37 +01:00
|
|
|
diff = priv->ylast - pos;
|
2014-02-10 17:14:22 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 18:53:37 +01:00
|
|
|
if (diff >= HIDMOUSE_YTHRESH_B16)
|
2014-02-10 17:14:22 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-08 11:02:31 +02:00
|
|
|
#ifdef CONFIG_INPUT_MOUSE_WHEEL
|
2014-02-10 18:53:37 +01:00
|
|
|
/* Get the difference in the wheel position from the last report */
|
|
|
|
|
|
|
|
pos = priv->waccum;
|
|
|
|
if (pos > priv->wlast)
|
|
|
|
{
|
|
|
|
diff = pos - priv->wlast;
|
|
|
|
}
|
|
|
|
else
|
2020-01-03 15:34:44 +01:00
|
|
|
{
|
2014-02-10 18:53:37 +01:00
|
|
|
diff = priv->wlast - pos;
|
2020-01-03 15:34:44 +01:00
|
|
|
}
|
2014-02-10 18:53:37 +01:00
|
|
|
|
|
|
|
/* Check if the X difference exceeds the report threshold */
|
|
|
|
|
|
|
|
if (diff >= HIDMOUSE_WTHRESH_B16)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-10 17:14:22 +01:00
|
|
|
/* Little or no change in either direction... don't report anything. */
|
|
|
|
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
/* No thresholding */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-09 20:49:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_mouse_poll
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Periodically check for new mouse data.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - A reference to the class instance to be destroyed.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-02-09 20:49:31 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_mouse_poll(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv;
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2014-02-10 17:14:22 +01:00
|
|
|
FAR struct usbhid_mousereport_s *rpt;
|
2015-04-25 19:17:57 +02:00
|
|
|
irqstate_t flags;
|
2014-02-10 17:14:22 +01:00
|
|
|
#ifndef CONFIG_HIDMOUSE_TSCIF
|
|
|
|
uint8_t buttons;
|
|
|
|
#endif
|
2016-06-11 19:50:18 +02:00
|
|
|
#if defined(CONFIG_DEBUG_USB) && defined(CONFIG_DEBUG_INFO)
|
2014-02-10 17:14:22 +01:00
|
|
|
unsigned int npolls = 0;
|
2014-02-09 20:49:31 +01:00
|
|
|
#endif
|
2014-02-10 17:14:22 +01:00
|
|
|
unsigned int nerrors = 0;
|
2015-05-05 21:14:22 +02:00
|
|
|
ssize_t nbytes;
|
2015-07-22 23:46:43 +02:00
|
|
|
int ret = OK;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Started\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Synchronize with the start-up logic. Get the private instance, re-start
|
|
|
|
* the start-up logic, and wait a bit to make sure that all of the class
|
|
|
|
* creation logic has a chance to run to completion.
|
|
|
|
*
|
2020-03-30 22:20:53 +02:00
|
|
|
* NOTE: that the reference count is *not* incremented here. When the
|
|
|
|
* driver structure was created, it was created with a reference count of
|
|
|
|
* one. This thread is responsible for that count. The count will be
|
|
|
|
* decremented when this thread exits.
|
2014-02-09 20:49:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
priv = g_priv;
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL);
|
|
|
|
hport = priv->usbclass.hport;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
priv->polling = true;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxsem_post(&g_syncsem);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_sleep(1);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Loop here until the device is disconnected */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entering poll loop\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
while (!priv->disconnected)
|
|
|
|
{
|
2014-02-09 23:50:09 +01:00
|
|
|
/* Read the next mouse report. We will stall here until the mouse
|
|
|
|
* sends data.
|
2014-02-09 20:49:31 +01:00
|
|
|
*/
|
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->epin,
|
|
|
|
priv->tbuffer, priv->tbuflen);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Check for errors -- Bail if an excessive number of consecutive
|
|
|
|
* errors are encountered.
|
2014-02-09 20:49:31 +01:00
|
|
|
*/
|
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes < 0)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2014-04-17 19:47:00 +02:00
|
|
|
/* If DRVR_TRANSFER() returns EAGAIN, that simply means that
|
|
|
|
* the devices was not ready and has NAK'ed the transfer. That
|
2014-04-17 22:02:22 +02:00
|
|
|
* should not be treated as an error (unless it persists for a
|
|
|
|
* long time).
|
2014-04-17 19:47:00 +02:00
|
|
|
*/
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: DRVR_TRANSFER returned: %d/%u\n",
|
2015-07-22 23:46:43 +02:00
|
|
|
(int)nbytes, nerrors);
|
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes != -EAGAIN)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2014-04-17 22:02:22 +02:00
|
|
|
if (++nerrors > 200)
|
2014-04-17 19:47:00 +02:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
uerr(" Too many errors... aborting: %d\n", nerrors);
|
2015-05-05 21:14:22 +02:00
|
|
|
ret = (int)nbytes;
|
2014-04-17 19:47:00 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
/* The report was received correctly. */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
else
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Success, reset the error counter */
|
|
|
|
|
|
|
|
nerrors = 0;
|
|
|
|
|
|
|
|
/* Ignore the mouse data if no task has opened the driver. */
|
|
|
|
|
|
|
|
if (priv->open)
|
|
|
|
{
|
|
|
|
/* Get exclusive access to the mouse state data */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-03-30 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Break out and disconnect if the thread is canceled. */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Get the HID mouse report */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
rpt = (FAR struct usbhid_mousereport_s *)priv->tbuffer;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Get the updated mouse position */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
usbhost_position(priv, rpt);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2014-02-10 17:14:22 +01:00
|
|
|
#ifdef CONFIG_HIDMOUSE_TSCIF
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Execute the touchscreen state machine */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
if (usbhost_touchscreen(priv, rpt))
|
2014-02-10 17:14:22 +01:00
|
|
|
#else
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Check if any buttons have changed. If so, then report the
|
|
|
|
* new mouse data.
|
2014-02-10 17:14:22 +01:00
|
|
|
*
|
2015-04-25 19:17:57 +02:00
|
|
|
* If not, then perform a thresholding operation so that the
|
|
|
|
* results will be more stable. If the difference from the
|
|
|
|
* last sample is small, then ignore the event.
|
2014-02-09 20:49:31 +01:00
|
|
|
*/
|
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
buttons = rpt->buttons & USBHID_MOUSEIN_BUTTON_MASK;
|
|
|
|
if (buttons != priv->buttons || usbhost_threshold(priv))
|
|
|
|
#endif
|
|
|
|
{
|
2017-11-03 16:21:19 +01:00
|
|
|
/* We get here when either there is a meaningful button
|
2015-04-25 19:17:57 +02:00
|
|
|
* change and/or a significant movement of the mouse. We
|
|
|
|
* are going to report the mouse event.
|
|
|
|
*
|
|
|
|
* Snap to the new x/y position for subsequent
|
|
|
|
* thresholding
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->xlast = priv->xaccum;
|
|
|
|
priv->ylast = priv->yaccum;
|
2021-04-08 11:02:31 +02:00
|
|
|
#ifdef CONFIG_INPUT_MOUSE_WHEEL
|
2015-04-25 19:17:57 +02:00
|
|
|
priv->wlast = priv->waccum;
|
2014-02-10 18:53:37 +01:00
|
|
|
#endif
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Update the sample X/Y positions */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
priv->sample.x = b16toi(priv->xaccum);
|
|
|
|
priv->sample.y = b16toi(priv->yaccum);
|
2021-04-08 11:02:31 +02:00
|
|
|
#ifdef CONFIG_INPUT_MOUSE_WHEEL
|
2015-04-25 19:17:57 +02:00
|
|
|
priv->sample.wheel = b16toi(priv->waccum);
|
2014-02-10 18:53:37 +01:00
|
|
|
#endif
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2014-02-10 17:14:22 +01:00
|
|
|
#ifdef CONFIG_HIDMOUSE_TSCIF
|
2015-04-25 19:17:57 +02:00
|
|
|
/* The X/Y positional data is now valid */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
priv->sample.valid = true;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
/* Indicate the availability of new sample data
|
|
|
|
* for this ID
|
|
|
|
*/
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
priv->sample.id = priv->id;
|
2014-02-10 17:14:22 +01:00
|
|
|
#else
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Report and remember the new button state */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
priv->sample.buttons = buttons;
|
|
|
|
priv->buttons = buttons;
|
2014-02-10 17:14:22 +01:00
|
|
|
#endif
|
2015-04-25 19:17:57 +02:00
|
|
|
priv->valid = true;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Notify any waiters that new HIDMOUSE data is available */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
usbhost_notify(priv);
|
|
|
|
}
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
/* Release our lock on the state structure */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2020-03-30 22:20:53 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* If USB debug is on, then provide some periodic indication that
|
|
|
|
* polling is still happening.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 19:50:18 +02:00
|
|
|
#if defined(CONFIG_DEBUG_USB) && defined(CONFIG_DEBUG_INFO)
|
2014-02-09 20:49:31 +01:00
|
|
|
npolls++;
|
|
|
|
if ((npolls & 31) == 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
uinfo("Still polling: %d\n", npolls);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
/* We get here when the driver is removed, when too many errors have
|
|
|
|
* been encountered, or the parent thread has been canceled.
|
2014-02-09 20:49:31 +01:00
|
|
|
*
|
|
|
|
* Make sure that we have exclusive access to the private data structure.
|
|
|
|
* There may now be other tasks with the character driver open and actively
|
|
|
|
* trying to interact with the class driver.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&priv->lock);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Indicate that we are no longer running and decrement the reference
|
2015-04-25 19:17:57 +02:00
|
|
|
* count held by this thread. If there are no other users of the class,
|
2014-02-09 20:49:31 +01:00
|
|
|
* we can destroy it now. Otherwise, we have to wait until the all
|
|
|
|
* of the file descriptors are closed.
|
|
|
|
*/
|
|
|
|
|
2016-06-12 16:31:22 +02:00
|
|
|
uinfo("Mouse removed, polling halted\n");
|
2015-04-25 19:17:57 +02:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2014-02-09 20:49:31 +01:00
|
|
|
priv->polling = false;
|
2015-04-25 19:17:57 +02:00
|
|
|
|
|
|
|
/* Decrement the reference count held by this thread. */
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->crefs > 0);
|
|
|
|
priv->crefs--;
|
|
|
|
|
|
|
|
/* There are two possibilities:
|
|
|
|
* 1) The reference count is greater than zero. This means that there
|
|
|
|
* are still open references to the mouse driver. In this case
|
|
|
|
* we need to wait until usbhost_close() is called and all of the
|
|
|
|
* open driver references are decremented. Then usbhost_destroy() can
|
|
|
|
* be called from usbhost_close().
|
|
|
|
* 2) The reference count is now zero. This means that there are no
|
|
|
|
* further open references and we can call usbhost_destroy() now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->crefs < 1)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Unregister the driver and destroy the instance (while we hold
|
|
|
|
* the semaphore!)
|
|
|
|
*/
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
usbhost_destroy(priv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-25 19:17:57 +02:00
|
|
|
/* No, we will destroy the driver instance when it is final open
|
|
|
|
* reference is closed
|
|
|
|
*/
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2015-07-22 23:46:43 +02:00
|
|
|
return ret;
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_sample
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if mouse data is available
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_sample(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR struct mouse_sample_s *sample)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret = -EAGAIN;
|
|
|
|
|
|
|
|
/* Interrupts me be disabled when this is called to (1) prevent posting
|
|
|
|
* of semaphores from interrupt handlers, and (2) to prevent sampled data
|
|
|
|
* from changing until it has been reported.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Is there new mouse data available? */
|
|
|
|
|
|
|
|
if (priv->valid)
|
|
|
|
{
|
|
|
|
/* Return a copy of the sampled data. */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
memcpy(sample, &priv->sample, sizeof(struct mouse_sample_s));
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2014-02-10 17:14:22 +01:00
|
|
|
#ifdef CONFIG_HIDMOUSE_TSCIF
|
2014-02-09 20:49:31 +01:00
|
|
|
/* Now manage state transitions */
|
|
|
|
|
|
|
|
if (sample->event == BUTTON_RELEASED)
|
|
|
|
{
|
|
|
|
/* Next.. No button press. Increment the ID so that next event ID
|
|
|
|
* will be unique. X/Y positions are no longer valid.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->sample.event = BUTTON_NONE;
|
|
|
|
priv->id++;
|
2014-02-10 17:14:22 +01:00
|
|
|
priv->sample.valid = false;
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
else if (sample->event == BUTTON_PRESSED)
|
2020-01-03 15:34:44 +01:00
|
|
|
{
|
2014-02-09 20:49:31 +01:00
|
|
|
/* First report -- next report will be a movement */
|
|
|
|
|
2020-01-03 15:34:44 +01:00
|
|
|
priv->sample.event = BUTTON_MOVE;
|
|
|
|
}
|
|
|
|
|
2014-02-10 17:14:22 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* The sample has been reported and is no longer valid */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
priv->valid = false;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2014-02-09 20:49:31 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_waitsample
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Wait for the next valid mouse sample
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - HID mouse state instance
|
|
|
|
* sample - The location to regurn the sample data
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_waitsample(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR struct mouse_sample_s *sample)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Interrupts me be disabled when this is called to (1) prevent posting
|
|
|
|
* of semaphores from interrupt handlers, and (2) to prevent sampled data
|
|
|
|
* from changing until it has been reported.
|
|
|
|
*
|
|
|
|
* In addition, we will also disable pre-emption to prevent other threads
|
|
|
|
* from getting control while we muck with the semaphores.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Now release the semaphore that manages mutually exclusive access to
|
|
|
|
* the device structure. This may cause other tasks to become ready to
|
|
|
|
* run, but they cannot run yet because pre-emption is disabled.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Try to get the a sample... if we cannot, then wait on the semaphore
|
|
|
|
* that is posted when new sample data is available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (usbhost_sample(priv, sample) < 0)
|
|
|
|
{
|
|
|
|
/* Wait for a change in the HIDMOUSE state */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Waiting..\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
priv->nwaiters++;
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->waitsem);
|
2014-02-09 20:49:31 +01:00
|
|
|
priv->nwaiters--;
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ierr("ERROR: nxsem_wait: %d\n", ret);
|
2014-02-09 20:49:31 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Did the mouse become disconnected while we were waiting */
|
|
|
|
|
|
|
|
if (priv->disconnected)
|
|
|
|
{
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Sampled\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
/* Re-acquire the semaphore that manages mutually exclusive access to
|
2020-03-30 22:20:53 +02:00
|
|
|
* the device structure. We may have to wait here. But we have our
|
|
|
|
* sample. Interrupts and pre-emption will be re-enabled while we wait.
|
2014-02-09 20:49:31 +01:00
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
errout:
|
|
|
|
/* Then re-enable interrupts. We might get interrupt here and there
|
|
|
|
* could be a new sample. But no new threads will run because we still
|
|
|
|
* have pre-emption disabled.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Restore pre-emption. We might get suspended here but that is okay
|
|
|
|
* because we already have our sample. Note: this means that if there
|
|
|
|
* were two threads reading from the HIDMOUSE for some reason, the data
|
|
|
|
* might be read out of order.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_cfgdesc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements the connect() method of struct
|
|
|
|
* usbhost_class_s. This method is a callback into the class
|
|
|
|
* implementation. It is used to provide the device's configuration
|
|
|
|
* descriptor to the class so that the class may initialize properly
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - The USB host class instance.
|
|
|
|
* configdesc - A pointer to a uint8_t buffer container the configuration
|
|
|
|
* descriptor.
|
|
|
|
* desclen - The length in bytes of the configuration descriptor.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-03-30 22:20:53 +02:00
|
|
|
* On success, zero (OK) is returned. On a failure, a negated errno value
|
|
|
|
* is returned indicating the nature of the failure
|
2014-02-09 20:49:31 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function will *not* be called from an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int usbhost_cfgdesc(FAR struct usbhost_state_s *priv,
|
2015-04-21 00:21:13 +02:00
|
|
|
FAR const uint8_t *configdesc, int desclen)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2014-02-09 20:49:31 +01:00
|
|
|
FAR struct usb_cfgdesc_s *cfgdesc;
|
|
|
|
FAR struct usb_desc_s *desc;
|
|
|
|
FAR struct usbhost_epdesc_s epindesc;
|
|
|
|
int remaining;
|
|
|
|
uint8_t found = 0;
|
|
|
|
bool done = false;
|
|
|
|
int ret;
|
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL &&
|
2015-04-21 00:21:13 +02:00
|
|
|
configdesc != NULL && desclen >= sizeof(struct usb_cfgdesc_s));
|
2015-04-21 15:26:28 +02:00
|
|
|
hport = priv->usbclass.hport;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Keep the compiler from complaining about uninitialized variables */
|
|
|
|
|
|
|
|
memset(&epindesc, 0, sizeof(struct usbhost_epdesc_s));
|
|
|
|
|
|
|
|
/* Verify that we were passed a configuration descriptor */
|
|
|
|
|
|
|
|
cfgdesc = (FAR struct usb_cfgdesc_s *)configdesc;
|
|
|
|
if (cfgdesc->type != USB_DESC_TYPE_CONFIG)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the total length of the configuration descriptor (little endian).
|
|
|
|
* It might be a good check to get the number of interfaces here too.
|
2015-10-10 18:41:00 +02:00
|
|
|
*/
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
remaining = (int)usbhost_getle16(cfgdesc->totallen);
|
|
|
|
|
|
|
|
/* Skip to the next entry descriptor */
|
|
|
|
|
|
|
|
configdesc += cfgdesc->len;
|
|
|
|
remaining -= cfgdesc->len;
|
|
|
|
|
|
|
|
/* Loop where there are more descriptors to examine */
|
|
|
|
|
|
|
|
while (remaining >= sizeof(struct usb_desc_s) && !done)
|
|
|
|
{
|
|
|
|
/* What is the next descriptor? */
|
|
|
|
|
|
|
|
desc = (FAR struct usb_desc_s *)configdesc;
|
|
|
|
switch (desc->type)
|
|
|
|
{
|
|
|
|
/* Interface descriptor. We really should get the number of endpoints
|
|
|
|
* from this descriptor too.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case USB_DESC_TYPE_INTERFACE:
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Interface descriptor\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
DEBUGASSERT(remaining >= USB_SIZEOF_IFDESC);
|
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
/* Did we already find what we needed from
|
|
|
|
* a preceding interface?
|
|
|
|
*/
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
if ((found & USBHOST_ALLFOUND) == USBHOST_ALLFOUND)
|
|
|
|
{
|
|
|
|
/* Yes.. then break out of the loop and use the preceding
|
|
|
|
* interface.
|
|
|
|
*/
|
|
|
|
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-09 23:50:09 +01:00
|
|
|
/* Otherwise, discard any endpoints previously found */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2014-02-09 23:50:09 +01:00
|
|
|
found = USBHOST_IFFOUND;
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* HID descriptor */
|
|
|
|
|
|
|
|
case USBHID_DESCTYPE_HID:
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("HID descriptor\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Endpoint descriptor. We expect one or two interrupt endpoints,
|
|
|
|
* a required IN endpoint and an optional OUT endpoint.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case USB_DESC_TYPE_ENDPOINT:
|
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
FAR struct usb_epdesc_s *epdesc =
|
|
|
|
(FAR struct usb_epdesc_s *)configdesc;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Endpoint descriptor\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
DEBUGASSERT(remaining >= USB_SIZEOF_EPDESC);
|
|
|
|
|
|
|
|
/* Check for an interrupt endpoint. */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
if ((epdesc->attr & USB_EP_ATTR_XFERTYPE_MASK) ==
|
|
|
|
USB_EP_ATTR_XFER_INT)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
|
|
|
/* Yes.. it is a interrupt endpoint. IN or OUT? */
|
|
|
|
|
|
|
|
if (USB_ISEPIN(epdesc->addr))
|
|
|
|
{
|
|
|
|
/* It is an interrupt IN endpoint. There should be only
|
|
|
|
* one interrupt IN endpoint.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((found & USBHOST_EPINFOUND) != 0)
|
|
|
|
{
|
|
|
|
/* Oops.. more than one endpoint. We don't know what
|
|
|
|
* to do with this.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
found |= USBHOST_EPINFOUND;
|
|
|
|
|
|
|
|
/* Save the interrupt IN endpoint information */
|
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
epindesc.hport = hport;
|
2020-03-30 22:20:53 +02:00
|
|
|
epindesc.addr = epdesc->addr &
|
|
|
|
USB_EP_ADDR_NUMBER_MASK;
|
2014-02-09 23:50:09 +01:00
|
|
|
epindesc.in = true;
|
2014-02-09 20:49:31 +01:00
|
|
|
epindesc.xfrtype = USB_EP_ATTR_XFER_INT;
|
|
|
|
epindesc.interval = epdesc->interval;
|
2020-03-30 22:20:53 +02:00
|
|
|
epindesc.mxpacketsize =
|
|
|
|
usbhost_getle16(epdesc->mxpacketsize);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Interrupt IN EP addr:%d mxpacketsize:%d\n",
|
2014-02-09 20:49:31 +01:00
|
|
|
epindesc.addr, epindesc.mxpacketsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Other descriptors are just ignored for now */
|
|
|
|
|
|
|
|
default:
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Other descriptor: %d\n", desc->type);
|
2014-02-09 20:49:31 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* What we found everything that we are going to find? */
|
|
|
|
|
|
|
|
if (found == USBHOST_ALLFOUND)
|
|
|
|
{
|
2020-04-09 14:46:24 +02:00
|
|
|
/* Yes.. then break out of the loop
|
|
|
|
* and use the preceding interface
|
|
|
|
*/
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the address of the next descriptor */
|
|
|
|
|
|
|
|
configdesc += desc->len;
|
|
|
|
remaining -= desc->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanity checking... did we find all of things that we need? */
|
|
|
|
|
|
|
|
if ((found & USBHOST_ALLFOUND) != USBHOST_ALLFOUND)
|
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
uerr("ERROR: Found IF:%s EPIN:%s\n",
|
|
|
|
(found & USBHOST_IFFOUND) != 0 ? "YES" : "NO",
|
|
|
|
(found & USBHOST_EPINFOUND) != 0 ? "YES" : "NO");
|
2014-02-09 20:49:31 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-02-09 23:50:09 +01:00
|
|
|
/* We are good... Allocate the interrupt IN endpoint. */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
ret = DRVR_EPALLOC(hport->drvr, &epindesc, &priv->epin);
|
2015-04-25 19:17:57 +02:00
|
|
|
if (ret < 0)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to allocate interrupt IN endpoint\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
uinfo("Endpoint allocated\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_devinit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The USB device has been successfully connected. This completes the
|
|
|
|
* initialization operations. It is first called after the
|
|
|
|
* configuration descriptor has been received.
|
|
|
|
*
|
|
|
|
* This function is called from the connect() method. This function always
|
|
|
|
* executes on the thread of the caller of connect().
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to the class instance.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-02-09 20:49:31 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int usbhost_devinit(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
|
|
|
char devname[DEV_NAMELEN];
|
|
|
|
int ret;
|
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
/* Set aside a transfer buffer for exclusive
|
|
|
|
* use by the mouse class driver
|
|
|
|
*/
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
ret = usbhost_tdalloc(priv);
|
2015-04-25 19:17:57 +02:00
|
|
|
if (ret < 0)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to allocate transfer buffer\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the reference count. This will prevent usbhost_destroy() from
|
|
|
|
* being called asynchronously if the device is removed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->crefs++;
|
|
|
|
DEBUGASSERT(priv->crefs == 2);
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
/* Start a worker task to poll the USB device. It would be nice to use
|
|
|
|
* the NuttX worker thread to do this, but this task needs to wait for
|
|
|
|
* events and activities on the worker thread should not involve
|
|
|
|
* significant waiting. Having a dedicated thread is more efficient in
|
|
|
|
* this sense, but requires more memory resources, primarily for the
|
|
|
|
* dedicated stack (CONFIG_HIDMOUSE_STACKSIZE).
|
2014-02-09 20:49:31 +01:00
|
|
|
*/
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Start poll task\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
/* The inputs to a task started by kthread_create() are very awkward for
|
|
|
|
* this purpose. They are really designed for command line tasks
|
|
|
|
* (argc/argv). So the following is kludge pass binary data when the
|
|
|
|
* mouse poll task is started.
|
2014-02-09 20:49:31 +01:00
|
|
|
*
|
2020-03-30 22:20:53 +02:00
|
|
|
* First, make sure we have exclusive access to g_priv (what is the
|
|
|
|
* likelihood of this being used? About zero, but we protect it anyway).
|
2014-02-09 20:49:31 +01:00
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_lock);
|
2020-03-30 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
usbhost_tdfree(priv);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2014-02-09 20:49:31 +01:00
|
|
|
g_priv = priv;
|
|
|
|
|
2022-03-21 23:47:09 +01:00
|
|
|
ret = kthread_create("mouse", CONFIG_HIDMOUSE_DEFPRIO,
|
2022-10-15 19:48:35 +02:00
|
|
|
CONFIG_HIDMOUSE_STACKSIZE, usbhost_mouse_poll, NULL);
|
2022-03-21 23:47:09 +01:00
|
|
|
if (ret < 0)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2020-04-09 14:46:24 +02:00
|
|
|
/* Failed to started the poll thread...
|
|
|
|
* probably due to memory resources
|
|
|
|
*/
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_lock);
|
2014-02-09 20:49:31 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2022-03-21 23:47:09 +01:00
|
|
|
priv->pollpid = (pid_t)ret;
|
|
|
|
|
2014-02-09 20:49:31 +01:00
|
|
|
/* Now wait for the poll task to get properly initialized */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxsem_wait_uninterruptible(&g_syncsem);
|
|
|
|
nxmutex_unlock(&g_lock);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2014-02-09 20:49:31 +01:00
|
|
|
/* Register the driver */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Register driver\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
usbhost_mkdevname(priv, devname);
|
|
|
|
ret = register_driver(devname, &g_hidmouse_fops, 0666, priv);
|
|
|
|
|
|
|
|
/* We now have to be concerned about asynchronous modification of crefs
|
|
|
|
* because the driver has been registered.
|
|
|
|
*/
|
|
|
|
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&priv->lock);
|
2014-02-09 20:49:31 +01:00
|
|
|
priv->crefs--;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2020-03-30 22:20:53 +02:00
|
|
|
|
2014-02-09 20:49:31 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_getle16
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get a (possibly unaligned) 16-bit little endian value.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* val - A pointer to the first byte of the little endian value.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-02-09 20:49:31 +01:00
|
|
|
* A uint16_t representing the whole 16-bit integer value
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline uint16_t usbhost_getle16(const uint8_t *val)
|
|
|
|
{
|
|
|
|
return (uint16_t)val[1] << 8 | (uint16_t)val[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_tdalloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate transfer buffer memory.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to the class instance.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-02-09 20:49:31 +01:00
|
|
|
* On success, zero (OK) is returned. On failure, an negated errno value
|
|
|
|
* is returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int usbhost_tdalloc(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2015-04-21 00:21:13 +02:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL &&
|
|
|
|
priv->tbuffer == NULL);
|
|
|
|
hport = priv->usbclass.hport;
|
2015-04-21 00:21:13 +02:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
return DRVR_ALLOC(hport->drvr, &priv->tbuffer, &priv->tbuflen);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_tdfree
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free transfer buffer memory.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to the class instance.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-02-09 20:49:31 +01:00
|
|
|
* On success, zero (OK) is returned. On failure, an negated errno value
|
|
|
|
* is returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int usbhost_tdfree(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2014-02-09 20:49:31 +01:00
|
|
|
int result = OK;
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
if (priv->tbuffer)
|
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv->usbclass.hport);
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
result = DRVR_FREE(hport->drvr, priv->tbuffer);
|
2014-02-09 20:49:31 +01:00
|
|
|
priv->tbuffer = NULL;
|
|
|
|
priv->tbuflen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* struct usbhost_registry_s methods
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_create
|
|
|
|
*
|
|
|
|
* Description:
|
2020-03-30 22:20:53 +02:00
|
|
|
* This function implements the create() method of struct
|
|
|
|
* usbhost_registry_s. The create() method is a callback into the class
|
|
|
|
* implementation. It is used to (1) create a new instance of the USB
|
|
|
|
* host class state and to (2) bind a USB host driver "session" to the
|
|
|
|
* class instance. Use of this create() method will support environments
|
|
|
|
* where there may be multiple USB ports and multiple USB devices
|
|
|
|
* simultaneously connected.
|
2014-02-09 20:49:31 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2015-04-21 15:26:28 +02:00
|
|
|
* hport - The hub port that manages the new class instance.
|
2014-02-09 20:49:31 +01:00
|
|
|
* id - In the case where the device supports multiple base classes,
|
|
|
|
* subclasses, or protocols, this specifies which to configure for.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-02-09 20:49:31 +01:00
|
|
|
* On success, this function will return a non-NULL instance of struct
|
|
|
|
* usbhost_class_s that can be used by the USB host driver to communicate
|
|
|
|
* with the USB host class. NULL is returned on failure; this function
|
2015-04-21 15:26:28 +02:00
|
|
|
* will fail only if the hport input parameter is NULL or if there are
|
2014-02-09 20:49:31 +01:00
|
|
|
* insufficient resources to create another USB host class instance.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct usbhost_class_s *
|
2022-09-06 08:18:45 +02:00
|
|
|
usbhost_create(FAR struct usbhost_hubport_s *hport,
|
|
|
|
FAR const struct usbhost_id_s *id)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv;
|
|
|
|
|
|
|
|
/* Allocate a USB host class instance */
|
|
|
|
|
|
|
|
priv = usbhost_allocclass();
|
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
/* Initialize the allocated storage class instance */
|
|
|
|
|
|
|
|
memset(priv, 0, sizeof(struct usbhost_state_s));
|
|
|
|
|
|
|
|
/* Assign a device number to this class instance */
|
|
|
|
|
|
|
|
if (usbhost_allocdevno(priv) == OK)
|
|
|
|
{
|
2020-01-03 15:34:44 +01:00
|
|
|
/* Initialize class method function pointers */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
priv->usbclass.hport = hport;
|
2015-01-11 15:05:09 +01:00
|
|
|
priv->usbclass.connect = usbhost_connect;
|
|
|
|
priv->usbclass.disconnected = usbhost_disconnected;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* The initial reference count is 1... One reference is held by
|
2015-04-26 01:05:30 +02:00
|
|
|
* the driver's usbhost_mouse_poll() task.
|
2014-02-09 20:49:31 +01:00
|
|
|
*/
|
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
priv->crefs = 1;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Initialize mutex & semaphores */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&priv->lock);
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&priv->waitsem, 0, 0);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Return the instance of the USB mouse class driver */
|
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
return &priv->usbclass;
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* An error occurred. Free the allocation and return NULL on all failures */
|
|
|
|
|
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
usbhost_freeclass(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* struct usbhost_class_s methods
|
|
|
|
****************************************************************************/
|
2020-01-03 15:34:44 +01:00
|
|
|
|
2014-02-09 20:49:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_connect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements the connect() method of struct
|
|
|
|
* usbhost_class_s. This method is a callback into the class
|
|
|
|
* implementation. It is used to provide the device's configuration
|
|
|
|
* descriptor to the class so that the class may initialize properly
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2015-01-11 15:05:09 +01:00
|
|
|
* usbclass - The USB host class entry previously obtained from a call to
|
|
|
|
* create().
|
2014-02-09 20:49:31 +01:00
|
|
|
* configdesc - A pointer to a uint8_t buffer container the configuration
|
|
|
|
* descriptor.
|
|
|
|
* desclen - The length in bytes of the configuration descriptor.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-03-30 22:20:53 +02:00
|
|
|
* On success, zero (OK) is returned. On a failure, a negated errno value
|
|
|
|
* is returned indicating the nature of the failure
|
2014-02-09 20:49:31 +01:00
|
|
|
*
|
2020-03-30 22:20:53 +02:00
|
|
|
* NOTE that the class instance remains valid upon return with a failure.
|
|
|
|
* It is the responsibility of the higher level enumeration logic to call
|
2014-02-09 20:49:31 +01:00
|
|
|
* CLASS_DISCONNECTED to free up the class driver resources.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* - This function will *not* be called from an interrupt handler.
|
|
|
|
* - If this function returns an error, the USB host controller driver
|
|
|
|
* must call to DISCONNECTED method to recover from the error
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
static int usbhost_connect(FAR struct usbhost_class_s *usbclass,
|
2015-04-21 00:21:13 +02:00
|
|
|
FAR const uint8_t *configdesc, int desclen)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2015-01-11 15:05:09 +01:00
|
|
|
FAR struct usbhost_state_s *priv = (FAR struct usbhost_state_s *)usbclass;
|
2014-02-09 20:49:31 +01:00
|
|
|
int ret;
|
|
|
|
|
2015-04-21 00:21:13 +02:00
|
|
|
DEBUGASSERT(priv != NULL && configdesc != NULL &&
|
2014-02-09 20:49:31 +01:00
|
|
|
desclen >= sizeof(struct usb_cfgdesc_s));
|
|
|
|
|
|
|
|
/* Parse the configuration descriptor to get the endpoints */
|
|
|
|
|
2015-04-21 00:21:13 +02:00
|
|
|
ret = usbhost_cfgdesc(priv, configdesc, desclen);
|
2015-04-25 19:17:57 +02:00
|
|
|
if (ret < 0)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
uerr("ERROR: usbhost_cfgdesc() failed: %d\n", ret);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Now configure the device and register the NuttX driver */
|
|
|
|
|
|
|
|
ret = usbhost_devinit(priv);
|
2015-04-25 19:17:57 +02:00
|
|
|
if (ret < 0)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
uerr("ERROR: usbhost_devinit() failed: %d\n", ret);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ERROR handling: Do nothing. If we return and error during connection,
|
|
|
|
* the driver is required to call the DISCONNECT method. Possibilities:
|
|
|
|
*
|
|
|
|
* - Failure occurred before the mouse poll task was started successfully.
|
|
|
|
* In this case, the disconnection will have to be handled on the worker
|
|
|
|
* task.
|
2020-03-30 22:20:53 +02:00
|
|
|
* - Failure occurred after the mouse poll task was started successfully.
|
|
|
|
* In this case, the disconnection can be performed on the mouse poll
|
|
|
|
* thread.
|
2014-02-09 20:49:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_disconnected
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements the disconnected() method of struct
|
|
|
|
* usbhost_class_s. This method is a callback into the class
|
|
|
|
* implementation. It is used to inform the class that the USB device has
|
|
|
|
* been disconnected.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2015-01-11 15:05:09 +01:00
|
|
|
* usbclass - The USB host class entry previously obtained from a call to
|
2014-02-09 20:49:31 +01:00
|
|
|
* create().
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-02-09 20:49:31 +01:00
|
|
|
* On success, zero (OK) is returned. On a failure, a negated errno value
|
|
|
|
* is returned indicating the nature of the failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function may be called from an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static int usbhost_disconnected(FAR struct usbhost_class_s *usbclass)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2015-01-11 15:05:09 +01:00
|
|
|
FAR struct usbhost_state_s *priv = (FAR struct usbhost_state_s *)usbclass;
|
2014-02-09 23:50:09 +01:00
|
|
|
int i;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-26 18:05:15 +02:00
|
|
|
DEBUGASSERT(priv != NULL);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Set an indication to any users of the mouse device that the device
|
|
|
|
* is no longer available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->disconnected = true;
|
2016-06-20 19:59:15 +02:00
|
|
|
uinfo("Disconnected\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2014-02-09 23:50:09 +01:00
|
|
|
/* Are there a thread(s) waiting for mouse data that will never come? */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2014-02-09 23:50:09 +01:00
|
|
|
for (i = 0; i < priv->nwaiters; i++)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2014-02-09 23:50:09 +01:00
|
|
|
/* Yes.. wake them up */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxsem_post(&priv->waitsem);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Possibilities:
|
|
|
|
*
|
|
|
|
* - Failure occurred before the mouse poll task was started successfully.
|
|
|
|
* In this case, the disconnection will have to be handled on the worker
|
|
|
|
* task.
|
2020-03-30 22:20:53 +02:00
|
|
|
* - Failure occurred after the mouse poll task was started successfully.
|
|
|
|
* In this case, the disconnection can be performed on the mouse poll
|
|
|
|
* thread.
|
2014-02-09 20:49:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->polling)
|
|
|
|
{
|
|
|
|
/* The polling task is still alive. Signal the mouse polling task.
|
|
|
|
* When that task wakes up, it will decrement the reference count and,
|
|
|
|
* perhaps, destroy the class instance. Then it will exit.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsig_kill(priv->pollpid, SIGALRM);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* In the case where the failure occurs before the polling task was
|
|
|
|
* started. Now what? We are probably executing from an interrupt
|
|
|
|
* handler here. We will use the worker thread. This is kind of
|
|
|
|
* wasteful and begs for a re-design.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->work.worker == NULL);
|
2020-01-02 17:49:34 +01:00
|
|
|
work_queue(HPWORK, &priv->work, usbhost_destroy, priv, 0);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Character driver methods
|
|
|
|
****************************************************************************/
|
2020-01-03 15:34:44 +01:00
|
|
|
|
2014-02-09 20:49:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver open method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct usbhost_state_s *priv;
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entry\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
DEBUGASSERT(filep && filep->f_inode);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
priv = inode->i_private;
|
|
|
|
|
|
|
|
/* Make sure that we have exclusive access to the private data structure */
|
|
|
|
|
|
|
|
DEBUGASSERT(priv && priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS);
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-03-30 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Check if the mouse device is still connected. We need to disable
|
2020-03-30 22:20:53 +02:00
|
|
|
* interrupts momentarily to assure that there are no asynchronous
|
|
|
|
* disconnect events.
|
2014-02-09 20:49:31 +01:00
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2014-02-09 20:49:31 +01:00
|
|
|
if (priv->disconnected)
|
|
|
|
{
|
|
|
|
/* No... the driver is no longer bound to the class. That means that
|
|
|
|
* the USB storage device is no longer connected. Refuse any further
|
|
|
|
* attempts to open the driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -ENODEV;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Was the driver previously open? We need to perform special
|
|
|
|
* initialization on the first time that the driver is opened.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!priv->open)
|
|
|
|
{
|
|
|
|
/* Set the thresholding values so that the first button press
|
|
|
|
* will be reported.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->xlast = INVALID_POSITION_B16;
|
|
|
|
priv->ylast = INVALID_POSITION_B16;
|
2021-04-08 11:02:31 +02:00
|
|
|
#ifdef CONFIG_INPUT_MOUSE_WHEEL
|
2014-02-10 18:53:37 +01:00
|
|
|
priv->wlast = INVALID_POSITION_B16;
|
|
|
|
#endif
|
2014-02-09 20:49:31 +01:00
|
|
|
/* Set the reported position to the center of the range */
|
|
|
|
|
2014-02-10 00:40:23 +01:00
|
|
|
priv->xaccum = (HIDMOUSE_XMAX_B16 >> 1);
|
|
|
|
priv->yaccum = (HIDMOUSE_YMAX_B16 >> 1);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, just increment the reference count on the driver */
|
|
|
|
|
|
|
|
priv->crefs++;
|
|
|
|
priv->open = true;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2014-02-09 20:49:31 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver close method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_close(FAR struct file *filep)
|
|
|
|
{
|
2015-04-25 19:17:57 +02:00
|
|
|
FAR struct inode *inode;
|
2014-02-09 20:49:31 +01:00
|
|
|
FAR struct usbhost_state_s *priv;
|
2015-04-25 19:17:57 +02:00
|
|
|
irqstate_t flags;
|
2020-03-30 22:20:53 +02:00
|
|
|
int ret;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entry\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
DEBUGASSERT(filep && filep->f_inode);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
priv = inode->i_private;
|
|
|
|
|
|
|
|
/* Decrement the reference count on the driver */
|
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
DEBUGASSERT(priv->crefs >= 1);
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-03-30 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-04-25 19:17:57 +02:00
|
|
|
|
|
|
|
/* We need to disable interrupts momentarily to assure that there are no
|
|
|
|
* asynchronous poll or disconnect events.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2014-02-09 20:49:31 +01:00
|
|
|
priv->crefs--;
|
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Check if the USB mouse device is still connected. If the device is
|
|
|
|
* no longer connected, then unregister the driver and free the driver
|
|
|
|
* class instance.
|
2014-02-09 20:49:31 +01:00
|
|
|
*/
|
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
if (priv->disconnected)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
2015-04-25 19:17:57 +02:00
|
|
|
/* If the reference count is one or less then there are two
|
|
|
|
* possibilities:
|
|
|
|
*
|
|
|
|
* 1) It might be zero meaning that the polling thread has already
|
|
|
|
* exited and decremented its count.
|
2020-03-30 22:20:53 +02:00
|
|
|
* 2) If might be one meaning either that (a) the polling thread is
|
|
|
|
* still running and still holds a count, or (b) the polling thread
|
|
|
|
* has exited, but there is still an outstanding open reference.
|
2015-04-25 19:17:57 +02:00
|
|
|
*/
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2020-01-03 15:34:44 +01:00
|
|
|
if (priv->crefs == 0 || (priv->crefs == 1 && priv->polling))
|
2015-04-25 19:17:57 +02:00
|
|
|
{
|
|
|
|
/* Yes.. In either case, then the driver is no longer open */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 23:57:57 +02:00
|
|
|
priv->open = false;
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Check if the USB keyboard device is still connected. */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
if (priv->crefs == 0)
|
|
|
|
{
|
|
|
|
/* The polling thread is no longer running */
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
DEBUGASSERT(!priv->polling);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
/* If the device is no longer connected, unregister the driver
|
|
|
|
* and free the driver class instance.
|
|
|
|
*/
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
usbhost_destroy(priv);
|
2015-04-25 23:57:57 +02:00
|
|
|
|
|
|
|
/* Skip giving the semaphore... it is no longer valid */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2015-04-25 23:57:57 +02:00
|
|
|
return OK;
|
2015-04-25 19:17:57 +02:00
|
|
|
}
|
|
|
|
else /* if (priv->crefs == 1) */
|
|
|
|
{
|
|
|
|
/* The polling thread is still running. Signal it so that it
|
|
|
|
* will wake up and call usbhost_destroy(). The particular
|
|
|
|
* signal that we use does not matter in this case.
|
|
|
|
*/
|
2014-02-09 20:49:31 +01:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsig_kill(priv->pollpid, SIGALRM);
|
2015-04-25 19:17:57 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2014-02-09 20:49:31 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver read method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
2014-02-09 20:49:31 +01:00
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct usbhost_state_s *priv;
|
2014-02-10 17:14:22 +01:00
|
|
|
#ifdef CONFIG_HIDMOUSE_TSCIF
|
2014-02-09 20:49:31 +01:00
|
|
|
FAR struct touch_sample_s *report;
|
2014-02-10 17:14:22 +01:00
|
|
|
#else
|
|
|
|
FAR struct mouse_report_s *report;
|
|
|
|
#endif
|
2014-02-09 20:49:31 +01:00
|
|
|
struct mouse_sample_s sample;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entry\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
DEBUGASSERT(filep && filep->f_inode && buffer);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
priv = inode->i_private;
|
|
|
|
|
|
|
|
/* Make sure that we have exclusive access to the private data structure */
|
|
|
|
|
|
|
|
DEBUGASSERT(priv && priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS);
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-03-30 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Check if the mouse is still connected. We need to disable interrupts
|
|
|
|
* momentarily to assure that there are no asynchronous disconnect events.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->disconnected)
|
|
|
|
{
|
|
|
|
/* No... the driver is no longer bound to the class. That means that
|
|
|
|
* the USB mouse is no longer connected. Refuse any further attempts
|
|
|
|
* to access the driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to read sample data. */
|
|
|
|
|
|
|
|
ret = usbhost_sample(priv, &sample);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Sample data is not available now. We would ave to wait to get
|
|
|
|
* receive sample data. If the user has specified the O_NONBLOCK
|
|
|
|
* option, then just return an error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
/* Yes.. then return a failure */
|
|
|
|
|
|
|
|
ret = -EAGAIN;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for sample data */
|
|
|
|
|
|
|
|
ret = usbhost_waitsample(priv, &sample);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* We might have been awakened by a signal */
|
|
|
|
|
2016-06-12 16:31:22 +02:00
|
|
|
ierr("ERROR: usbhost_waitsample: %d\n", ret);
|
2014-02-09 20:49:31 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We now have sampled HIDMOUSE data that we can report to the caller. */
|
|
|
|
|
2014-02-10 17:14:22 +01:00
|
|
|
#ifdef CONFIG_HIDMOUSE_TSCIF
|
2014-02-09 20:49:31 +01:00
|
|
|
report = (FAR struct touch_sample_s *)buffer;
|
|
|
|
memset(report, 0, SIZEOF_TOUCH_SAMPLE_S(1));
|
|
|
|
|
|
|
|
report->npoints = 1;
|
|
|
|
report->point[0].id = sample.id;
|
|
|
|
report->point[0].x = sample.x;
|
|
|
|
report->point[0].y = sample.y;
|
|
|
|
|
|
|
|
/* Report the appropriate flags */
|
|
|
|
|
|
|
|
if (sample.event == BUTTON_RELEASED)
|
|
|
|
{
|
|
|
|
/* But was released. Is the positional data valid? This is
|
|
|
|
* important to know because the release will be sent to the
|
|
|
|
* window based on its last positional data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (sample.valid)
|
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID |
|
|
|
|
TOUCH_POS_VALID;
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (sample.event == BUTTON_PRESSED)
|
|
|
|
{
|
|
|
|
/* First event */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
report->point[0].flags = TOUCH_DOWN | TOUCH_ID_VALID |
|
|
|
|
TOUCH_POS_VALID;
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
else /* if (sample->event == BUTTON_MOVE) */
|
|
|
|
{
|
|
|
|
/* Movement of the same event */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
report->point[0].flags = TOUCH_MOVE | TOUCH_ID_VALID |
|
|
|
|
TOUCH_POS_VALID;
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo(" id: %d\n", report->point[0].id);
|
|
|
|
iinfo(" flags: %02x\n", report->point[0].flags);
|
|
|
|
iinfo(" x: %d\n", report->point[0].x);
|
|
|
|
iinfo(" y: %d\n", report->point[0].y);
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
ret = SIZEOF_TOUCH_SAMPLE_S(1);
|
2014-02-10 17:14:22 +01:00
|
|
|
#else
|
|
|
|
report = (FAR struct mouse_report_s *)buffer;
|
|
|
|
memset(report, 0, sizeof(struct mouse_report_s));
|
|
|
|
|
|
|
|
report->buttons = sample.buttons;
|
|
|
|
report->x = sample.x;
|
|
|
|
report->y = sample.y;
|
2021-04-08 11:02:31 +02:00
|
|
|
#ifdef CONFIG_INPUT_MOUSE_WHEEL
|
2014-02-10 18:53:37 +01:00
|
|
|
report->wheel = sample.wheel;
|
|
|
|
#endif
|
2014-02-10 17:14:22 +01:00
|
|
|
|
|
|
|
ret = sizeof(struct mouse_report_s);
|
|
|
|
#endif
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Returning: %d\n", ret);
|
2014-02-09 20:49:31 +01:00
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver write method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t usbhost_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
/* We won't try to write to the mouse */
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_poll
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver poll method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct usbhost_state_s *priv;
|
2020-03-30 22:20:53 +02:00
|
|
|
int ret;
|
2014-02-09 20:49:31 +01:00
|
|
|
int i;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entry\n");
|
2014-02-09 20:49:31 +01:00
|
|
|
DEBUGASSERT(filep && filep->f_inode && fds);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
priv = inode->i_private;
|
|
|
|
|
|
|
|
/* Make sure that we have exclusive access to the private data structure */
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-03-30 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2014-02-09 20:49:31 +01:00
|
|
|
|
|
|
|
/* Check if the mouse is still connected. We need to disable interrupts
|
|
|
|
* momentarily to assure that there are no asynchronous disconnect events.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->disconnected)
|
|
|
|
{
|
|
|
|
/* No... the driver is no longer bound to the class. That means that
|
|
|
|
* the USB mouse is no longer connected. Refuse any further attempts
|
|
|
|
* to access the driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -ENODEV;
|
|
|
|
}
|
|
|
|
else if (setup)
|
|
|
|
{
|
|
|
|
/* This is a request to set up the poll. Find an available slot for
|
|
|
|
* the poll structure reference
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_HIDMOUSE_NPOLLWAITERS; i++)
|
|
|
|
{
|
|
|
|
/* Find an available slot */
|
|
|
|
|
|
|
|
if (!priv->fds[i])
|
|
|
|
{
|
|
|
|
/* Bind the poll structure and this slot */
|
|
|
|
|
|
|
|
priv->fds[i] = fds;
|
|
|
|
fds->priv = &priv->fds[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= CONFIG_HIDMOUSE_NPOLLWAITERS)
|
|
|
|
{
|
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should we immediately notify on any of the requested events? Notify
|
|
|
|
* the POLLIN event if there is buffered mouse data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->valid)
|
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(priv->fds, CONFIG_HIDMOUSE_NPOLLWAITERS, POLLIN);
|
2014-02-09 20:49:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll. */
|
|
|
|
|
|
|
|
struct pollfd **slot = (struct pollfd **)fds->priv;
|
|
|
|
DEBUGASSERT(slot);
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2014-02-09 20:49:31 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_mouse_init
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the USB storage HID mouse class driver. This function
|
|
|
|
* should be called be platform-specific code in order to initialize and
|
|
|
|
* register support for the USB host HID mouse class device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-02-09 20:49:31 +01:00
|
|
|
* On success this function will return zero (OK); A negated errno value
|
|
|
|
* will be returned on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int usbhost_mouse_init(void)
|
|
|
|
{
|
|
|
|
/* Advertise our availability to support (certain) mouse devices */
|
|
|
|
|
|
|
|
return usbhost_registerclass(&g_hidmouse);
|
|
|
|
}
|
|
|
|
|
2019-02-11 19:09:26 +01:00
|
|
|
#endif /* CONFIG_USBHOST)&& !CONFIG_USBHOST_INT_DISABLE */
|