2011-01-12 05:03:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/usbhost/usbhost_hidkbd.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.
|
2011-01-12 05:03:57 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdbool.h>
|
2011-01-12 05:03:57 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-01-16 20:08:16 +01:00
|
|
|
#include <unistd.h>
|
2011-01-12 05:03:57 +01:00
|
|
|
#include <string.h>
|
2011-01-14 18:06:30 +01:00
|
|
|
#include <poll.h>
|
2015-04-25 19:17:57 +02:00
|
|
|
#include <signal.h>
|
2011-01-14 18:06:30 +01:00
|
|
|
#include <time.h>
|
2011-01-19 05:17:45 +01:00
|
|
|
#include <fcntl.h>
|
2011-01-12 05:03:57 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
#include <nuttx/irq.h>
|
2011-04-06 18:40:47 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2015-04-26 01:05:30 +02:00
|
|
|
#include <nuttx/kthread.h>
|
2011-01-12 05:03:57 +01:00
|
|
|
#include <nuttx/arch.h>
|
2011-01-17 14:52:33 +01:00
|
|
|
#include <nuttx/wqueue.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2016-11-03 18:00:47 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2011-01-12 05:03:57 +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>
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2012-12-26 19:54:59 +01:00
|
|
|
#ifdef CONFIG_HIDKBD_ENCODED
|
|
|
|
# include <nuttx/streams.h>
|
|
|
|
# include <nuttx/input/kbd_codec.h>
|
|
|
|
#endif
|
|
|
|
|
2011-01-16 15:02:42 +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)
|
2011-01-16 15:02:42 +01:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2019-05-22 22:14:15 +02:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* Configuration ************************************************************/
|
2019-05-22 02:57:54 +02:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* This determines how often the USB keyboard will be polled in units of
|
|
|
|
* of microseconds. The default is 100MS.
|
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
#ifndef CONFIG_HIDKBD_POLLUSEC
|
|
|
|
# define CONFIG_HIDKBD_POLLUSEC (100*1000)
|
2011-01-12 05:03:57 +01:00
|
|
|
#endif
|
|
|
|
|
2011-01-17 14:52:33 +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
|
2011-01-18 20:58:49 +01:00
|
|
|
# warning "Worker thread support is required (CONFIG_SCHED_WORKQUEUE)"
|
2011-01-17 14:52:33 +01:00
|
|
|
#endif
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Provide some default values for other configuration settings */
|
2011-01-14 18:06:30 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
#ifndef CONFIG_HIDKBD_DEFPRIO
|
|
|
|
# define CONFIG_HIDKBD_DEFPRIO 50
|
|
|
|
#endif
|
2011-01-18 03:17:49 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
#ifndef CONFIG_HIDKBD_STACKSIZE
|
|
|
|
# define CONFIG_HIDKBD_STACKSIZE 1024
|
2011-01-14 18:06:30 +01:00
|
|
|
#endif
|
|
|
|
|
2011-01-19 21:02:23 +01:00
|
|
|
#ifndef CONFIG_HIDKBD_BUFSIZE
|
|
|
|
# define CONFIG_HIDKBD_BUFSIZE 64
|
2011-01-18 03:17:49 +01:00
|
|
|
#endif
|
|
|
|
|
2011-01-19 21:02:23 +01:00
|
|
|
#ifndef CONFIG_HIDKBD_NPOLLWAITERS
|
|
|
|
# define CONFIG_HIDKBD_NPOLLWAITERS 2
|
2011-01-19 05:17:45 +01:00
|
|
|
#endif
|
|
|
|
|
2011-01-18 20:58:49 +01:00
|
|
|
/* The default is to support scancode mapping for the standard 104 key
|
2011-01-19 21:02:23 +01:00
|
|
|
* keyboard. Setting CONFIG_HIDKBD_RAWSCANCODES will disable all scancode
|
|
|
|
* mapping; Setting CONFIG_HIDKBD_ALLSCANCODES will enable mapping of all
|
2013-08-12 01:11:32 +02:00
|
|
|
* scancodes;
|
2011-01-18 03:17:49 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-19 21:02:23 +01:00
|
|
|
#ifndef CONFIG_HIDKBD_RAWSCANCODES
|
|
|
|
# ifdef CONFIG_HIDKBD_ALLSCANCODES
|
2011-01-18 20:58:49 +01:00
|
|
|
# define USBHID_NUMSCANCODES (USBHID_KBDUSE_MAX+1)
|
|
|
|
# else
|
|
|
|
# define USBHID_NUMSCANCODES 104
|
|
|
|
# endif
|
2011-01-18 03:17:49 +01:00
|
|
|
#endif
|
|
|
|
|
2020-02-22 19:31:14 +01:00
|
|
|
/* We can't support encoding of special characters of unless the Keyboard
|
2012-12-26 22:37:50 +01:00
|
|
|
* CODEC is enabled.
|
|
|
|
*/
|
|
|
|
|
2021-08-01 09:27:08 +02:00
|
|
|
#ifndef CONFIG_LIBC_KBDCODEC
|
2012-12-26 22:37:50 +01:00
|
|
|
# undef CONFIG_HIDKBD_ENCODED
|
|
|
|
#endif
|
|
|
|
|
2012-12-26 19:54:59 +01:00
|
|
|
/* If we are using raw scancodes, then we cannot support encoding of
|
|
|
|
* special characters either.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_HIDKBD_RAWSCANCODES
|
|
|
|
# undef CONFIG_HIDKBD_ENCODED
|
|
|
|
#endif
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* Driver support ***********************************************************/
|
2019-05-22 02:57:54 +02:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* This format is used to construct the /dev/kbd[n] device driver path. It
|
2011-01-12 05:03:57 +01:00
|
|
|
* defined here so that it will be used consistently in all places.
|
|
|
|
*/
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
#define DEV_FORMAT "/dev/kbd%c"
|
|
|
|
#define DEV_NAMELEN 11
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* Used in usbhost_cfgdesc() */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
#define USBHOST_IFFOUND 0x01 /* Required I/F descriptor found */
|
|
|
|
#define USBHOST_EPINFOUND 0x02 /* Required interrupt IN EP descriptor found */
|
|
|
|
#define USBHOST_EPOUTFOUND 0x04 /* Optional interrupt OUT EP descriptor found */
|
|
|
|
#define USBHOST_RQDFOUND (USBHOST_IFFOUND|USBHOST_EPINFOUND)
|
2011-01-17 14:52:33 +01:00
|
|
|
#define USBHOST_ALLFOUND (USBHOST_RQDFOUND|USBHOST_EPOUTFOUND)
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
#define USBHOST_MAX_CREFS 0x7fff
|
|
|
|
|
2012-12-26 19:54:59 +01:00
|
|
|
/* Debug ********************************************************************/
|
2019-05-22 02:57:54 +02:00
|
|
|
|
2012-12-26 19:54:59 +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
|
2012-12-26 19:54:59 +01:00
|
|
|
#endif
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* This structure contains the internal, private state of the USB host
|
|
|
|
* keyboard storage class.
|
2011-01-12 05:03:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* The remainder of the fields are provide o the keyboard class driver */
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
char devchar; /* Character identifying the /dev/kbd[n] device */
|
2011-01-12 05:03:57 +01:00
|
|
|
volatile bool disconnected; /* TRUE: Device has been disconnected */
|
2011-01-16 20:08:16 +01:00
|
|
|
volatile bool polling; /* TRUE: Poll thread is running */
|
2011-01-19 05:17:45 +01:00
|
|
|
volatile bool open; /* TRUE: The keyboard device is open */
|
|
|
|
volatile bool waiting; /* TRUE: waiting for keyboard data */
|
2011-01-17 14:52:33 +01:00
|
|
|
uint8_t ifno; /* Interface number */
|
2011-01-12 05:03:57 +01:00
|
|
|
int16_t crefs; /* Reference count on the driver instance */
|
|
|
|
sem_t exclsem; /* Used to maintain mutual exclusive access */
|
2011-01-19 05:17:45 +01:00
|
|
|
sem_t waitsem; /* Used to wait for keyboard data */
|
2011-01-16 20:08:16 +01:00
|
|
|
FAR uint8_t *tbuffer; /* The allocated transfer buffer */
|
|
|
|
size_t tbuflen; /* Size of the allocated transfer buffer */
|
|
|
|
pid_t pollpid; /* PID of the poll task */
|
2011-01-17 14:52:33 +01:00
|
|
|
struct work_s work; /* For cornercase error handling by the worker thread */
|
2011-01-14 18:06:30 +01:00
|
|
|
|
|
|
|
/* Endpoints:
|
|
|
|
* EP0 (Control):
|
|
|
|
* - Receiving and responding to requests for USB control and class data.
|
|
|
|
* - IN data when polled by the HID class driver (Get_Report)
|
|
|
|
* - OUT data from the host.
|
|
|
|
* EP Interrupt IN:
|
|
|
|
* - Receiving asynchronous (unrequested) IN data from the device.
|
2015-01-11 15:05:09 +01:00
|
|
|
* EP Interrupt OUT (optional):
|
2011-01-14 18:06:30 +01:00
|
|
|
* - Transmitting low latency OUT data to the device.
|
|
|
|
* - If not present, EP0 used.
|
|
|
|
*/
|
|
|
|
|
|
|
|
usbhost_ep_t epin; /* Interrupt IN endpoint */
|
|
|
|
usbhost_ep_t epout; /* Optional interrupt OUT endpoint */
|
2011-01-18 03:17:49 +01:00
|
|
|
|
2011-01-19 05:17:45 +01:00
|
|
|
/* 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'.
|
|
|
|
*/
|
|
|
|
|
2011-01-19 21:02:23 +01:00
|
|
|
struct pollfd *fds[CONFIG_HIDKBD_NPOLLWAITERS];
|
2011-01-19 05:17:45 +01:00
|
|
|
|
2011-01-18 03:17:49 +01:00
|
|
|
/* Buffer used to collect and buffer incoming keyboard characters */
|
|
|
|
|
2011-01-19 05:17:45 +01:00
|
|
|
volatile uint16_t headndx; /* Buffer head index */
|
|
|
|
volatile uint16_t tailndx; /* Buffer tail index */
|
2011-01-19 21:02:23 +01:00
|
|
|
uint8_t kbdbuffer[CONFIG_HIDKBD_BUFSIZE];
|
2011-01-12 05:03:57 +01:00
|
|
|
};
|
|
|
|
|
2012-12-26 19:54:59 +01:00
|
|
|
/* This type is used for encoding special characters */
|
|
|
|
|
|
|
|
#ifdef CONFIG_HIDKBD_ENCODED
|
|
|
|
struct usbhost_outstream_s
|
|
|
|
{
|
|
|
|
struct lib_outstream_s stream;
|
|
|
|
FAR struct usbhost_state_s *priv;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Semaphores */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
static int usbhost_takesem(FAR sem_t *sem);
|
|
|
|
static void usbhost_forcetake(FAR sem_t *sem);
|
2017-10-03 23:35:24 +02:00
|
|
|
#define usbhost_givesem(s) nxsem_post(s);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-19 05:17:45 +01:00
|
|
|
/* Polling support */
|
|
|
|
|
|
|
|
static void usbhost_pollnotify(FAR struct usbhost_state_s *dev);
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* 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);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
/* Device name management */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
static int usbhost_allocdevno(FAR struct usbhost_state_s *priv);
|
2011-01-12 05:03:57 +01:00
|
|
|
static void usbhost_freedevno(FAR struct usbhost_state_s *priv);
|
2020-03-30 22:20:53 +02:00
|
|
|
static inline void usbhost_mkdevname(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR char *devname);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Keyboard polling thread */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-17 14:52:33 +01:00
|
|
|
static void usbhost_destroy(FAR void *arg);
|
2020-03-30 22:20:53 +02:00
|
|
|
static void usbhost_putbuffer(FAR struct usbhost_state_s *priv,
|
|
|
|
uint8_t keycode);
|
2012-12-26 19:54:59 +01:00
|
|
|
#ifdef CONFIG_HIDKBD_ENCODED
|
|
|
|
static void usbhost_putstream(FAR struct lib_outstream_s *this, int ch);
|
|
|
|
#endif
|
2020-03-30 22:20:53 +02:00
|
|
|
static inline uint8_t usbhost_mapscancode(uint8_t scancode,
|
|
|
|
uint8_t modifier);
|
2012-12-26 19:54:59 +01:00
|
|
|
#ifdef CONFIG_HIDKBD_ENCODED
|
|
|
|
static inline void usbhost_encodescancode(FAR struct usbhost_state_s *priv,
|
2020-03-30 22:20:53 +02:00
|
|
|
uint8_t scancode, uint8_t modifier);
|
2012-12-26 19:54:59 +01:00
|
|
|
#endif
|
2020-03-30 22:20:53 +02:00
|
|
|
static int usbhost_kbdpoll(int argc, char *argv[]);
|
2011-01-14 18:06:30 +01:00
|
|
|
|
|
|
|
/* Helpers for usbhost_connect() */
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
static inline int usbhost_cfgdesc(FAR struct usbhost_state_s *priv,
|
2020-03-30 22:20:53 +02:00
|
|
|
FAR const uint8_t *configdesc, int desclen);
|
2011-01-12 05:03:57 +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);
|
|
|
|
static inline void usbhost_putle16(uint8_t *dest, uint16_t val);
|
|
|
|
static inline uint32_t usbhost_getle32(const uint8_t *val);
|
2012-08-20 18:06:39 +02:00
|
|
|
#if 0 /* Not used */
|
2011-01-12 05:03:57 +01:00
|
|
|
static void usbhost_putle32(uint8_t *dest, uint32_t val);
|
2012-08-20 18:06:39 +02:00
|
|
|
#endif
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
/* 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 */
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
static struct usbhost_class_s *usbhost_create(
|
|
|
|
FAR struct usbhost_hubport_s *hport,
|
|
|
|
FAR const struct usbhost_id_s *id);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
/* struct usbhost_class_s methods */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
static int usbhost_connect(FAR struct usbhost_class_s *usbclass,
|
|
|
|
FAR const uint8_t *configdesc, int desclen);
|
|
|
|
static int usbhost_disconnected(FAR struct usbhost_class_s *usbclass);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* Driver methods. We export the keyboard as a standard character driver */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
static int usbhost_open(FAR struct file *filep);
|
|
|
|
static int usbhost_close(FAR struct file *filep);
|
2011-01-16 20:08:16 +01:00
|
|
|
static ssize_t usbhost_read(FAR struct file *filep,
|
2020-03-30 22:20:53 +02:00
|
|
|
FAR char *buffer, size_t len);
|
2011-01-16 20:08:16 +01:00
|
|
|
static ssize_t usbhost_write(FAR struct file *filep,
|
2020-03-30 22:20:53 +02:00
|
|
|
FAR const char *buffer, size_t len);
|
|
|
|
static int usbhost_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-09 20:47:11 +01:00
|
|
|
/* This structure provides the registry entry ID information that will be
|
2011-01-16 20:08:16 +01:00
|
|
|
* used to associate the USB host keyboard class driver to a connected USB
|
2011-01-12 05:03:57 +01:00
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
|
2018-10-25 14:42:21 +02:00
|
|
|
static const struct usbhost_id_s g_hidkbd_id =
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
|
|
|
USB_CLASS_HID, /* base */
|
|
|
|
USBHID_SUBCLASS_BOOTIF, /* subclass */
|
|
|
|
USBHID_PROTOCOL_KEYBOARD, /* proto */
|
2019-05-22 22:14:15 +02:00
|
|
|
0, /* vid */
|
|
|
|
0 /* pid */
|
2011-01-12 05:03:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This is the USB host storage class's registry entry */
|
|
|
|
|
2013-09-20 23:23:00 +02:00
|
|
|
static struct usbhost_registry_s g_hidkbd =
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2019-05-22 02:57:54 +02:00
|
|
|
NULL, /* flink */
|
|
|
|
usbhost_create, /* create */
|
|
|
|
1, /* nids */
|
|
|
|
&g_hidkbd_id /* id[] */
|
2011-01-12 05:03:57 +01:00
|
|
|
};
|
|
|
|
|
2013-09-20 23:23:00 +02:00
|
|
|
static const struct file_operations g_hidkbd_fops =
|
2011-01-14 18:06:30 +01:00
|
|
|
{
|
2019-05-22 02:57:54 +02:00
|
|
|
usbhost_open, /* open */
|
|
|
|
usbhost_close, /* close */
|
|
|
|
usbhost_read, /* read */
|
|
|
|
usbhost_write, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
NULL, /* ioctl */
|
|
|
|
usbhost_poll /* poll */
|
2011-01-14 18:06:30 +01:00
|
|
|
};
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* This is a bitmap that is used to allocate device names /dev/kbda-z. */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
static uint32_t g_devinuse;
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* The following are used to managed the class creation operation */
|
|
|
|
|
|
|
|
static sem_t g_exclsem; /* For mutually exclusive thread creation */
|
|
|
|
static sem_t g_syncsem; /* Thread data passing interlock */
|
|
|
|
static struct usbhost_state_s *g_priv; /* Data passed to thread */
|
|
|
|
|
2011-01-18 03:17:49 +01:00
|
|
|
/* The following tables map keyboard scan codes to printable ASIC
|
|
|
|
* characters. There is no support here for function keys or cursor
|
|
|
|
* controls.
|
|
|
|
*/
|
|
|
|
|
2011-01-19 21:02:23 +01:00
|
|
|
#ifndef CONFIG_HIDKBD_RAWSCANCODES
|
2012-12-26 19:54:59 +01:00
|
|
|
#ifdef CONFIG_HIDKBD_ENCODED
|
|
|
|
|
|
|
|
/* The first and last scancode values with encode-able values */
|
|
|
|
|
2012-12-26 22:37:50 +01:00
|
|
|
#define FIRST_ENCODING USBHID_KBDUSE_ENTER /* 0x28 Keyboard Return (ENTER) */
|
|
|
|
#ifndef CONFIG_HIDKBD_ALLSCANCODES
|
|
|
|
# define LAST_ENCODING USBHID_KBDUSE_POWER /* 0x66 Keyboard Power */
|
2012-12-26 19:54:59 +01:00
|
|
|
#else
|
2012-12-26 22:37:50 +01:00
|
|
|
# define LAST_ENCODING USBHID_KBDUSE_KPDHEXADECIMAL /* 0xdd Keypad Hexadecimal */
|
2012-12-26 19:54:59 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define USBHID_NUMENCODINGS (LAST_ENCODING - FIRST_ENCODING + 1)
|
|
|
|
|
|
|
|
static const uint8_t encoding[USBHID_NUMENCODINGS] =
|
|
|
|
{
|
|
|
|
/* 0x28-0x2f: Enter,escape,del,back-tab,space,_,+,{ */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
KEYCODE_ENTER, 0,
|
|
|
|
KEYCODE_FWDDEL, KEYCODE_BACKDEL,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
2012-12-29 00:40:54 +01:00
|
|
|
/* 0x30-0x37: },|,Non-US tilde,:,",grave tilde,<,> */
|
2012-12-26 19:54:59 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0x38-0x3f: /,CapsLock,F1,F2,F3,F4,F5,F6 */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
0, KEYCODE_CAPSLOCK,
|
|
|
|
KEYCODE_F1, KEYCODE_F2,
|
|
|
|
KEYCODE_F3, KEYCODE_F4,
|
|
|
|
KEYCODE_F5, KEYCODE_F6,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0x40-0x47: F7,F8,F9,F10,F11,F12,PrtScn,ScrollLock */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
KEYCODE_F7, KEYCODE_F8,
|
|
|
|
KEYCODE_F9, KEYCODE_F10,
|
|
|
|
KEYCODE_F11, KEYCODE_F12,
|
|
|
|
KEYCODE_PRTSCRN, KEYCODE_SCROLLLOCK,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
/* 0x48-0x4f: Pause,Insert,Home,PageUp,
|
|
|
|
* DeleteForward,End,PageDown,RightArrow
|
|
|
|
*/
|
2012-12-26 19:54:59 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
KEYCODE_PAUSE, KEYCODE_INSERT,
|
|
|
|
KEYCODE_HOME, KEYCODE_PAGEUP,
|
|
|
|
KEYCODE_FWDDEL, KEYCODE_END,
|
|
|
|
KEYCODE_PAGEDOWN, KEYCODE_RIGHT,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0x50-0x57: LeftArrow,DownArrow,UpArrow,Num Lock,/,*,-,+ */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
KEYCODE_LEFT, KEYCODE_DOWN,
|
|
|
|
KEYCODE_UP, KEYCODE_NUMLOCK,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0x58-0x5f: Enter,1-7 */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
KEYCODE_ENTER, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0x60-0x66: 8-9,0,.,Non-US \,Application,Power */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
KEYCODE_POWER,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_HIDKBD_ALLSCANCODES
|
|
|
|
|
|
|
|
0, /* 0x67 = */
|
|
|
|
|
|
|
|
/* 0x68-0x6f: F13,F14,F15,F16,F17,F18,F19,F20 */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
KEYCODE_F13, KEYCODE_F14,
|
|
|
|
KEYCODE_F15, KEYCODE_F16,
|
|
|
|
KEYCODE_F17, KEYCODE_F18,
|
|
|
|
KEYCODE_F19, KEYCODE_F20,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0x70-0x77: F21,F22,F23,F24,Execute,Help,Menu,Select */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
KEYCODE_F21, KEYCODE_F22,
|
|
|
|
KEYCODE_F23, KEYCODE_F24,
|
|
|
|
KEYCODE_EXECUTE, KEYCODE_HELP,
|
|
|
|
KEYCODE_MENU, KEYCODE_SELECT,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0x78-0x7f: Stop,Again,Undo,Cut,Copy,Paste,Find,Mute */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
KEYCODE_STOP, KEYCODE_AGAIN,
|
|
|
|
KEYCODE_UNDO, KEYCODE_CUT,
|
|
|
|
KEYCODE_COPY, KEYCODE_PASTE,
|
|
|
|
KEYCODE_FIND, KEYCODE_MUTE,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
/* 0x80-0x87: VolUp,VolDown,LCapsLock,lNumLock,LScrollLock,,,
|
|
|
|
* =,International1
|
|
|
|
*/
|
2012-12-26 19:54:59 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
KEYCODE_VOLUP, KEYCODE_VOLDOWN,
|
|
|
|
KEYCODE_LCAPSLOCK, KEYCODE_LNUMLOCK,
|
|
|
|
KEYCODE_LSCROLLLOCK, 0,
|
|
|
|
0, 0,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0x88-0x8f: International 2-9 */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0x90-0x97: LAN 1-8 */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
KEYCODE_LANG1, KEYCODE_LANG2,
|
|
|
|
KEYCODE_LANG3, KEYCODE_LANG4,
|
|
|
|
KEYCODE_LANG5, KEYCODE_LANG6,
|
|
|
|
KEYCODE_LANG7, KEYCODE_LANG8,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0x98-0x9f: LAN 9,Erase,SysReq,Cancel,Clear,Prior,Return,Separator */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
0, 0,
|
|
|
|
KEYCODE_SYSREQ, KEYCODE_CANCEL,
|
|
|
|
KEYCODE_CLEAR, 0,
|
|
|
|
KEYCODE_ENTER, 0,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0xa0-0xa7: Out,Oper,Clear,CrSel,Excel,(reserved) */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0xa8-0xaf: (reserved) */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0xb0-0xb7: 00,000,ThouSeparator,DecSeparator,CurrencyUnit,SubUnit,(,) */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0xb8-0xbf: {,},tab,backspace,A-D */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
0, 0,
|
|
|
|
0, KEYCODE_BACKDEL,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0xc0-0xc7: E-F,XOR,^,%,<,>,& */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0xc8-0xcf: &&,|,||,:,#, ,@,! */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0xd0-0xd7: Memory Store,Recall,Clear,Add,Subtract,Muliply,Divide,+/- */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
KEYCODE_MEMSTORE, KEYCODE_MEMRECALL,
|
|
|
|
KEYCODE_MEMCLEAR, KEYCODE_MEMADD,
|
|
|
|
KEYCODE_MEMSUB, KEYCODE_MEMMUL,
|
|
|
|
KEYCODE_MEMDIV, KEYCODE_NEGATE,
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* 0xd8-0xdd: Clear,ClearEntry,Binary,Octal,Decimal,Hexadecimal */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
KEYCODE_CLEAR, KEYCODE_CLEARENTRY,
|
|
|
|
KEYCODE_BINARY, KEYCODE_OCTAL,
|
|
|
|
KEYCODE_DECIMAL, KEYCODE_HEXADECIMAL
|
2012-12-26 19:54:59 +01:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-01-18 03:17:49 +01:00
|
|
|
static const uint8_t ucmap[USBHID_NUMSCANCODES] =
|
|
|
|
{
|
2011-01-19 05:17:45 +01:00
|
|
|
0, 0, 0, 0, 'A', 'B', 'C', 'D', /* 0x00-0x07: Reserved, errors, A-D */
|
|
|
|
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', /* 0x08-0x0f: E-L */
|
|
|
|
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', /* 0x10-0x17: M-T */
|
|
|
|
'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@', /* 0x18-0x1f: U-Z,!,@ */
|
|
|
|
'#', '$', '%', '^', '&', '*', '(', ')', /* 0x20-0x27: #,$,%,^,&,*,(,) */
|
|
|
|
'\n', '\033', '\177', 0, ' ', '_', '+', '{', /* 0x28-0x2f: Enter,escape,del,back-tab,space,_,+,{ */
|
2012-12-29 00:40:54 +01:00
|
|
|
'}', '|', 0, ':', '"', '~', '<', '>', /* 0x30-0x37: },|,Non-US tilde,:,",grave tilde,<,> */
|
2011-01-19 05:17:45 +01:00
|
|
|
'?', 0, 0, 0, 0, 0, 0, 0, /* 0x38-0x3f: /,CapsLock,F1,F2,F3,F4,F5,F6 */
|
2012-12-26 19:54:59 +01:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0x40-0x47: F7,F8,F9,F10,F11,F12,PrtScn,ScrollLock */
|
2011-01-19 05:17:45 +01:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0x48-0x4f: Pause,Insert,Home,PageUp,DeleteForward,End,PageDown,RightArrow */
|
|
|
|
0, 0, 0, 0, '/', '*', '-', '+', /* 0x50-0x57: LeftArrow,DownArrow,UpArrow,Num Lock,/,*,-,+ */
|
2012-12-27 15:01:59 +01:00
|
|
|
'\n', '1', '2', '3', '4', '5', '6', '7', /* 0x58-0x5f: Enter,1-7 */
|
2011-01-19 05:17:45 +01:00
|
|
|
'8', '9', '0', '.', 0, 0, 0, '=', /* 0x60-0x67: 8-9,0,.,Non-US \,Application,Power,= */
|
2011-01-19 21:02:23 +01:00
|
|
|
#ifdef CONFIG_HIDKBD_ALLSCANCODES
|
2011-01-19 05:17:45 +01:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0x68-0x6f: F13,F14,F15,F16,F17,F18,F19,F20 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0x70-0x77: F21,F22,F23,F24,Execute,Help,Menu,Select */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0x78-0x7f: Stop,Again,Undo,Cut,Copy,Paste,Find,Mute */
|
|
|
|
0, 0, 0, 0, 0, ',', 0, 0, /* 0x80-0x87: VolUp,VolDown,LCapsLock,lNumLock,LScrollLock,,,=,International1 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0x88-0x8f: International 2-9 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0x90-0x97: LAN 1-8 */
|
2012-12-26 19:54:59 +01:00
|
|
|
0, 0, 0, 0, 0, 0, '\n', 0, /* 0x98-0x9f: LAN 9,Erase,SysReq,Cancel,Clear,Prior,Return,Separator */
|
2011-01-19 05:17:45 +01:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0-0xa7: Out,Oper,Clear,CrSel,Excel,(reserved) */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0xa8-0xaf: (reserved) */
|
|
|
|
0, 0, 0, 0, 0, 0, '(', ')', /* 0xb0-0xb7: 00,000,ThouSeparator,DecSeparator,CurrencyUnit,SubUnit,(,) */
|
|
|
|
'{', '}', '\t', \177, 'A', 'B', 'C', 'D', /* 0xb8-0xbf: {,},tab,backspace,A-D */
|
|
|
|
'F', 'F', 0, '^', '%', '<', '>', '&', /* 0xc0-0xc7: E-F,XOR,^,%,<,>,& */
|
|
|
|
0, '|', 0, ':', '%', ' ', '@', '!', /* 0xc8-0xcf: &&,|,||,:,#, ,@,! */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0-0xd7: Memory Store,Recall,Clear,Add,Subtract,Muliply,Divide,+/- */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0xd8-0xdf: Clear,ClearEntry,Binary,Octal,Decimal,Hexadecimal */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0-0xe7: Left Ctrl,Shift,Alt,GUI, Right Ctrl,Shift,Alt,GUI */
|
2011-01-18 03:17:49 +01:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t lcmap[USBHID_NUMSCANCODES] =
|
|
|
|
{
|
2011-01-19 05:17:45 +01:00
|
|
|
0, 0, 0, 0, 'a', 'b', 'c', 'd', /* 0x00-0x07: Reserved, errors, a-d */
|
|
|
|
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', /* 0x08-0x0f: e-l */
|
|
|
|
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', /* 0x10-0x17: m-t */
|
|
|
|
'u', 'v', 'w', 'x', 'y', 'z', '1', '2', /* 0x18-0x1f: u-z,1-2 */
|
|
|
|
'3', '4', '5', '6', '7', '8', '9', '0', /* 0x20-0x27: 3-9,0 */
|
|
|
|
'\n', '\033', '\177', '\t', ' ', '-', '=', '[', /* 0x28-0x2f: Enter,escape,del,tab,space,-,=,[ */
|
2012-12-29 00:40:54 +01:00
|
|
|
']', '\\', '\234', ';', '\'', '`', ',', '.', /* 0x30-0x37: ],\,Non-US pound,;,',grave accent,,,. */
|
2011-01-19 05:17:45 +01:00
|
|
|
'/', 0, 0, 0, 0, 0, 0, 0, /* 0x38-0x3f: /,CapsLock,F1,F2,F3,F4,F5,F6 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0x40-0x47: F7,F8,F9,F10,F11,F12,PrtScn,ScrollLock */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0x48-0x4f: Pause,Insert,Home,PageUp,DeleteForward,End,PageDown,RightArrow */
|
|
|
|
0, 0, 0, 0, '/', '*', '-', '+', /* 0x50-0x57: LeftArrow,DownArrow,UpArrow,Num Lock,/,*,-,+ */
|
2012-12-27 15:01:59 +01:00
|
|
|
'\n', '1', '2', '3', '4', '5', '6', '7', /* 0x58-0x5f: Enter,1-7 */
|
2011-01-19 05:17:45 +01:00
|
|
|
'8', '9', '0', '.', 0, 0, 0, '=', /* 0x60-0x67: 8-9,0,.,Non-US \,Application,Power,= */
|
2011-01-19 21:02:23 +01:00
|
|
|
#ifdef CONFIG_HIDKBD_ALLSCANCODES
|
2011-01-19 05:17:45 +01:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0x68-0x6f: F13,F14,F15,F16,F17,F18,F19,F20 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0x70-0x77: F21,F22,F23,F24,Execute,Help,Menu,Select */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0x78-0x7f: Stop,Again,Undo,Cut,Copy,Paste,Find,Mute */
|
|
|
|
0, 0, 0, 0, 0, ',', 0, 0, /* 0x80-0x87: VolUp,VolDown,LCapsLock,lNumLock,LScrollLock,,,=,International1 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0x88-0x8f: International 2-9 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0x90-0x97: LAN 1-8 */
|
2012-12-26 19:54:59 +01:00
|
|
|
0, 0, 0, 0, 0, 0, '\n', 0, /* 0x98-0x9f: LAN 9,Erase,SysReq,Cancel,Clear,Prior,Return,Separator */
|
2011-01-19 05:17:45 +01:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0-0xa7: Out,Oper,Clear,CrSel,Excel,(reserved) */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0xa8-0xaf: (reserved) */
|
|
|
|
0, 0, 0, 0, 0, 0, '(', ')', /* 0xb0-0xb7: 00,000,ThouSeparator,DecSeparator,CurrencyUnit,SubUnit,(,) */
|
|
|
|
'{', '}', '\t', '\177', 'A', 'B', 'C', 'D', /* 0xb8-0xbf: {,},tab,backspace,A-D */
|
|
|
|
'F', 'F', 0, '^', '%', '<', '>', '&', /* 0xc0-0xc7: E-F,XOR,^,%,<,>,& */
|
|
|
|
0, '|', 0, ':', '%', ' ', '@', '!', /* 0xc8-0xcf: &&,|,||,:,#, ,@,! */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0-0xd7: Memory Store,Recall,Clear,Add,Subtract,Muliply,Divide,+/- */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0xd8-0xdf: Clear,ClearEntry,Binary,Octal,Decimal,Hexadecimal */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0-0xe7: Left Ctrl,Shift,Alt,GUI, Right Ctrl,Shift,Alt,GUI */
|
2011-01-18 03:17:49 +01:00
|
|
|
#endif
|
|
|
|
};
|
2011-01-19 21:02:23 +01:00
|
|
|
#endif /* CONFIG_HIDKBD_RAWSCANCODES */
|
2011-01-18 03:17:49 +01:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_takesem
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is just a wrapper to handle the annoying behavior of semaphore
|
|
|
|
* waits that return due to the receipt of a signal.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
static int usbhost_takesem(FAR sem_t *sem)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
return nxsem_wait_uninterruptible(sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_forcetake
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is just another wrapper but this one continues even if the thread
|
|
|
|
* is canceled. This must be done in certain conditions where were must
|
|
|
|
* continue in order to clean-up resources.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_forcetake(FAR sem_t *sem)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = nxsem_wait_uninterruptible(sem);
|
|
|
|
|
|
|
|
/* The only expected error would -ECANCELED meaning that the
|
|
|
|
* parent thread has been canceled. We have to continue and
|
|
|
|
* terminate the poll in this case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(ret == OK || ret == -ECANCELED);
|
|
|
|
}
|
|
|
|
while (ret < 0);
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
|
2011-01-19 05:17:45 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_pollnotify
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_pollnotify(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-01-19 21:02:23 +01:00
|
|
|
for (i = 0; i < CONFIG_HIDKBD_NPOLLWAITERS; i++)
|
2011-01-19 05:17:45 +01:00
|
|
|
{
|
|
|
|
struct pollfd *fds = priv->fds[i];
|
|
|
|
if (fds)
|
|
|
|
{
|
|
|
|
fds->revents |= (fds->events & POLLIN);
|
|
|
|
if (fds->revents != 0)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Report events: %02x\n", fds->revents);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(fds->sem);
|
2011-01-19 05:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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:
|
2011-01-12 05:03:57 +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);
|
2011-01-12 05:03:57 +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.
|
2011-01-12 05:03:57 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-01-12 05:03:57 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
static inline void usbhost_freeclass(FAR struct usbhost_state_s *usbclass)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2015-01-11 15:05:09 +01:00
|
|
|
DEBUGASSERT(usbclass != NULL);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-04-06 18:40:47 +02:00
|
|
|
/* Free the class instance. */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Freeing: %p\n", usbclass);
|
2020-04-07 16:31:47 +02:00
|
|
|
kmm_free(usbclass);
|
2011-01-12 05:03:57 +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();
|
2011-01-12 05:03:57 +01:00
|
|
|
for (devno = 0; devno < 26; devno++)
|
|
|
|
{
|
|
|
|
uint32_t bitno = 1 << devno;
|
|
|
|
if ((g_devinuse & bitno) == 0)
|
|
|
|
{
|
|
|
|
g_devinuse |= bitno;
|
2011-01-16 20:08:16 +01:00
|
|
|
priv->devchar = 'a' + devno;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2011-01-12 05:03:57 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2011-01-12 05:03:57 +01:00
|
|
|
return -EMFILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usbhost_freedevno(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
2011-01-16 20:08:16 +01:00
|
|
|
int devno = 'a' - priv->devchar;
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
if (devno >= 0 && devno < 26)
|
|
|
|
{
|
2016-02-14 14:32:58 +01:00
|
|
|
irqstate_t flags = enter_critical_section();
|
2011-01-12 05:03:57 +01:00
|
|
|
g_devinuse &= ~(1 << devno);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
static inline void usbhost_mkdevname(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR char *devname)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->devchar);
|
2011-01-14 18:06:30 +01:00
|
|
|
}
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_destroy
|
|
|
|
*
|
|
|
|
* Description:
|
2014-02-09 20:47:11 +01:00
|
|
|
* The USB device has been disconnected and the reference count on the USB
|
2011-01-12 05:03:57 +01:00
|
|
|
* 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:
|
2011-01-12 05:03:57 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-01-17 14:52:33 +01:00
|
|
|
static void usbhost_destroy(FAR void *arg)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2011-01-17 14:52:33 +01:00
|
|
|
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;
|
2011-01-14 18:06:30 +01:00
|
|
|
char devname[DEV_NAMELEN];
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL);
|
|
|
|
hport = priv->usbclass.hport;
|
2015-04-20 20:06:47 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("crefs: %d\n", priv->crefs);
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* Unregister the driver */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Unregister driver\n");
|
2011-01-14 18:06:30 +01:00
|
|
|
usbhost_mkdevname(priv, devname);
|
2020-01-02 17:49:34 +01:00
|
|
|
unregister_driver(devname);
|
2011-01-14 18:06:30 +01:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* Release the device name used by this connection */
|
|
|
|
|
|
|
|
usbhost_freedevno(priv);
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* Free the interrupt endpoints */
|
|
|
|
|
|
|
|
if (priv->epin)
|
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
DRVR_EPFREE(hport->drvr, priv->epin);
|
2011-01-14 18:06:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->epout)
|
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
DRVR_EPFREE(hport->drvr, priv->epout);
|
2011-01-14 18:06:30 +01:00
|
|
|
}
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
/* Free any transfer buffers */
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
usbhost_tdfree(priv);
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* Destroy the semaphores */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&priv->exclsem);
|
|
|
|
nxsem_destroy(&priv->waitsem);
|
2011-01-14 18:06:30 +01:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* Disconnect the USB host device */
|
|
|
|
|
2015-05-02 18:44:18 +02:00
|
|
|
DRVR_DISCONNECT(hport->drvr, hport);
|
2011-01-12 05:03:57 +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. */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
usbhost_freeclass(priv);
|
|
|
|
}
|
|
|
|
|
2012-12-26 19:54:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_putbuffer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add one character to the user buffer.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - Driver internal state
|
|
|
|
* keycode - The value to add to the user buffer
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2012-12-26 19:54:59 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_putbuffer(FAR struct usbhost_state_s *priv,
|
|
|
|
uint8_t keycode)
|
|
|
|
{
|
|
|
|
register unsigned int head;
|
|
|
|
register unsigned int tail;
|
|
|
|
|
|
|
|
/* Copy the next keyboard character into the user buffer. */
|
|
|
|
|
|
|
|
head = priv->headndx;
|
|
|
|
priv->kbdbuffer[head] = keycode;
|
|
|
|
|
|
|
|
/* Increment the head index */
|
|
|
|
|
|
|
|
if (++head >= CONFIG_HIDKBD_BUFSIZE)
|
|
|
|
{
|
|
|
|
head = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the buffer is full, then increment the tail index to make space. Is
|
|
|
|
* it better to lose old keystrokes or new?
|
|
|
|
*/
|
|
|
|
|
|
|
|
tail = priv->tailndx;
|
|
|
|
if (tail == head)
|
|
|
|
{
|
|
|
|
if (++tail >= CONFIG_HIDKBD_BUFSIZE)
|
|
|
|
{
|
|
|
|
tail = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the updated tail index */
|
|
|
|
|
|
|
|
priv->tailndx = tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the updated head index */
|
|
|
|
|
|
|
|
priv->headndx = head;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_putstream
|
|
|
|
*
|
|
|
|
* Description:
|
2014-02-09 20:47:11 +01:00
|
|
|
* A wrapper for usbhost_putc that is compatible with the lib_outstream_s
|
|
|
|
* putc methods.
|
2012-12-26 19:54:59 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* stream - The struct lib_outstream_s reference
|
|
|
|
* ch - The character to add to the user buffer
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2012-12-26 19:54:59 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_HIDKBD_ENCODED
|
|
|
|
static void usbhost_putstream(FAR struct lib_outstream_s *stream, int ch)
|
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
FAR struct usbhost_outstream_s *privstream =
|
|
|
|
(FAR struct usbhost_outstream_s *)stream;
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(privstream && privstream->priv);
|
2012-12-26 22:37:50 +01:00
|
|
|
usbhost_putbuffer(privstream->priv, (uint8_t)ch);
|
2012-12-26 19:54:59 +01:00
|
|
|
stream->nput++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/****************************************************************************
|
2011-01-18 03:17:49 +01:00
|
|
|
* Name: usbhost_mapscancode
|
2011-01-16 20:08:16 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2011-01-18 03:17:49 +01:00
|
|
|
* Map a keyboard scancode to a printable ASCII character. There is no
|
|
|
|
* support here for function keys or cursor controls in this version of
|
|
|
|
* the driver.
|
2011-01-16 20:08:16 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2011-01-18 03:17:49 +01:00
|
|
|
* scancode - Scan code to be mapped.
|
|
|
|
* modifier - Ctrl,Alt,Shift,GUI modifier bits
|
2011-01-16 20:08:16 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-01-16 20:08:16 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-01-18 20:58:49 +01:00
|
|
|
static inline uint8_t usbhost_mapscancode(uint8_t scancode, uint8_t modifier)
|
2011-01-16 20:08:16 +01:00
|
|
|
{
|
2011-01-19 21:02:23 +01:00
|
|
|
#ifndef CONFIG_HIDKBD_RAWSCANCODES
|
2011-01-18 03:17:49 +01:00
|
|
|
/* Range check */
|
|
|
|
|
|
|
|
if (scancode >= USBHID_NUMSCANCODES)
|
2011-01-16 20:08:16 +01:00
|
|
|
{
|
2011-01-18 03:17:49 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is either shift key pressed? */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
if ((modifier & (USBHID_MODIFER_LSHIFT | USBHID_MODIFER_RSHIFT)) != 0)
|
2011-01-18 03:17:49 +01:00
|
|
|
{
|
|
|
|
return ucmap[scancode];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return lcmap[scancode];
|
2011-01-16 20:08:16 +01:00
|
|
|
}
|
2011-01-18 20:58:49 +01:00
|
|
|
#else
|
|
|
|
return scancode;
|
|
|
|
#endif
|
2011-01-16 20:08:16 +01:00
|
|
|
}
|
|
|
|
|
2012-12-26 19:54:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_encodescancode
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if the key has a special function encoding and, if it does, add
|
|
|
|
* the encoded value to the user buffer.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - Driver internal state
|
|
|
|
* scancode - Scan code to be mapped.
|
2014-02-09 20:47:11 +01:00
|
|
|
* modifier - Ctrl, Alt, Shift, GUI modifier bits
|
2012-12-26 19:54:59 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2012-12-26 19:54:59 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_HIDKBD_ENCODED
|
|
|
|
static inline void usbhost_encodescancode(FAR struct usbhost_state_s *priv,
|
|
|
|
uint8_t scancode, uint8_t modifier)
|
|
|
|
{
|
|
|
|
uint8_t encoded;
|
|
|
|
|
|
|
|
/* Check if the raw scancode is in a valid range */
|
|
|
|
|
|
|
|
if (scancode >= FIRST_ENCODING && scancode <= LAST_ENCODING)
|
|
|
|
{
|
|
|
|
/* Yes the value is within range */
|
|
|
|
|
2012-12-26 22:37:50 +01:00
|
|
|
encoded = encoding[scancode - FIRST_ENCODING];
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo(" scancode: %02x modifier: %02x encoded: %d\n",
|
2012-12-26 19:54:59 +01:00
|
|
|
scancode, modifier, encoded);
|
|
|
|
|
|
|
|
if (encoded)
|
|
|
|
{
|
|
|
|
struct usbhost_outstream_s usbstream;
|
|
|
|
|
|
|
|
/* And it does correspond to a special function key */
|
|
|
|
|
2012-12-26 22:37:50 +01:00
|
|
|
usbstream.stream.put = usbhost_putstream;
|
|
|
|
usbstream.stream.nput = 0;
|
|
|
|
usbstream.priv = priv;
|
2012-12-26 19:54:59 +01:00
|
|
|
|
|
|
|
/* Add the special function value to the user buffer */
|
|
|
|
|
2012-12-27 15:01:59 +01:00
|
|
|
kbd_specpress((enum kbd_keycode_e)encoded,
|
|
|
|
(FAR struct lib_outstream_s *)&usbstream);
|
2012-12-26 19:54:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_kbdpoll
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Periodically check for new keyboard data.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - A reference to the class instance to be destroyed.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-01-16 20:08:16 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_kbdpoll(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv;
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
|
|
|
FAR struct usb_ctrlreq_s *ctrlreq;
|
2015-04-25 19:17:57 +02:00
|
|
|
irqstate_t flags;
|
2011-01-20 01:46:56 +01:00
|
|
|
#ifndef CONFIG_HIDKBD_NODEBOUNCE
|
2020-03-30 22:20:53 +02:00
|
|
|
uint8_t lastkey[6] =
|
|
|
|
{
|
|
|
|
0, 0, 0, 0, 0, 0
|
|
|
|
};
|
2011-01-20 01:46:56 +01:00
|
|
|
#endif
|
2020-03-30 22:20:53 +02:00
|
|
|
|
2016-06-11 19:50:18 +02:00
|
|
|
#if defined(CONFIG_DEBUG_USB) && defined(CONFIG_DEBUG_INFO)
|
2015-04-21 15:26:28 +02:00
|
|
|
unsigned int npolls = 0;
|
2011-01-16 20:08:16 +01:00
|
|
|
#endif
|
2015-04-21 15:26:28 +02:00
|
|
|
unsigned int nerrors = 0;
|
2015-04-25 19:17:57 +02:00
|
|
|
useconds_t delay;
|
2015-04-21 15:26:28 +02:00
|
|
|
bool empty = true;
|
|
|
|
bool newstate;
|
|
|
|
int ret;
|
2011-01-16 20:08:16 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Started\n");
|
2011-01-16 20:08:16 +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
|
|
|
|
* decrement when this thread exits.
|
2011-01-16 20:08:16 +01:00
|
|
|
*/
|
|
|
|
|
2012-12-26 19:54:59 +01:00
|
|
|
priv = g_priv;
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport);
|
|
|
|
hport = priv->usbclass.hport;
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2012-12-26 19:54:59 +01:00
|
|
|
priv->polling = true;
|
|
|
|
usbhost_givesem(&g_syncsem);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_sleep(1);
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Loop here until the device is disconnected */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entering poll loop\n");
|
2012-12-26 21:04:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
while (!priv->disconnected)
|
|
|
|
{
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
ret = usbhost_takesem(&priv->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2011-01-16 20:08:16 +01:00
|
|
|
|
2011-01-18 03:17:49 +01:00
|
|
|
/* Format the HID report request:
|
2011-01-16 20:08:16 +01:00
|
|
|
*
|
2011-01-19 05:17:45 +01:00
|
|
|
* bmRequestType 10100001
|
|
|
|
* bRequest GET_REPORT (0x01)
|
|
|
|
* wValue Report Type and Report Index
|
2011-01-16 20:08:16 +01:00
|
|
|
* wIndex Interface Number
|
|
|
|
* wLength Descriptor Length
|
|
|
|
* Data Descriptor Data
|
|
|
|
*/
|
|
|
|
|
|
|
|
ctrlreq = (struct usb_ctrlreq_s *)priv->tbuffer;
|
2020-03-30 22:20:53 +02:00
|
|
|
ctrlreq->type = USB_REQ_DIR_IN | USB_REQ_TYPE_CLASS |
|
|
|
|
USB_REQ_RECIPIENT_INTERFACE;
|
2011-01-18 03:17:49 +01:00
|
|
|
ctrlreq->req = USBHID_REQUEST_GETREPORT;
|
|
|
|
|
|
|
|
usbhost_putle16(ctrlreq->value, (USBHID_REPORTTYPE_INPUT << 8));
|
2011-01-17 14:52:33 +01:00
|
|
|
usbhost_putle16(ctrlreq->index, priv->ifno);
|
2011-01-18 03:17:49 +01:00
|
|
|
usbhost_putle16(ctrlreq->len, sizeof(struct usbhid_kbdreport_s));
|
2011-01-16 20:08:16 +01:00
|
|
|
|
2011-01-18 03:17:49 +01:00
|
|
|
/* Send HID report request */
|
2011-01-16 20:08:16 +01:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
ret = DRVR_CTRLIN(hport->drvr, hport->ep0, ctrlreq, priv->tbuffer);
|
2011-01-16 20:08:16 +01:00
|
|
|
usbhost_givesem(&priv->exclsem);
|
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Check for errors -- Bail if an excessive number of consecutive
|
|
|
|
* errors are encountered.
|
2011-01-18 03:17:49 +01:00
|
|
|
*/
|
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
if (ret < 0)
|
2011-01-16 20:08:16 +01:00
|
|
|
{
|
|
|
|
nerrors++;
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: GETREPORT/INPUT, DRVR_CTRLIN returned: %d/%d\n",
|
2011-01-16 20:08:16 +01:00
|
|
|
ret, nerrors);
|
|
|
|
|
|
|
|
if (nerrors > 200)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
uerr(" Too many errors... aborting: %d\n", nerrors);
|
2011-01-16 20:08:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-01-18 03:17:49 +01:00
|
|
|
|
|
|
|
/* The report was received correctly. But ignore the keystrokes if no
|
|
|
|
* task has opened the driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (priv->open)
|
2011-01-16 20:08:16 +01:00
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
struct usbhid_kbdreport_s *rpt =
|
|
|
|
(struct usbhid_kbdreport_s *)priv->tbuffer;
|
|
|
|
uint8_t keycode;
|
|
|
|
int i;
|
2011-01-18 03:17:49 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Success, reset the error counter */
|
|
|
|
|
|
|
|
nerrors = 0;
|
|
|
|
|
2011-01-18 03:17:49 +01:00
|
|
|
/* Add the newly received keystrokes to our internal buffer */
|
2011-01-16 20:08:16 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
ret = usbhost_takesem(&priv->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-18 03:17:49 +01:00
|
|
|
for (i = 0; i < 6; i++)
|
|
|
|
{
|
2011-01-20 01:46:56 +01:00
|
|
|
/* Is this key pressed? But not pressed last time?
|
|
|
|
* HID spec: "The order of keycodes in array fields has no
|
|
|
|
* significance. Order determination is done by the host
|
|
|
|
* software comparing the contents of the previous report to
|
|
|
|
* the current report. If two or more keys are reported in
|
|
|
|
* one report, their order is indeterminate. Keyboards may
|
|
|
|
* buffer events that would have otherwise resulted in
|
|
|
|
* multiple event in a single report.
|
|
|
|
*
|
|
|
|
* "'Repeat Rate' and 'Delay Before First Repeat' are
|
|
|
|
* implemented by the host and not in the keyboard (this
|
|
|
|
* means the BIOS in legacy mode). The host may use the
|
|
|
|
* device report rate and the number of reports to determine
|
|
|
|
* how long a key is being held down. Alternatively, the host
|
|
|
|
* may use its own clock or the idle request for the timing
|
|
|
|
* of these features."
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (rpt->key[i] != USBHID_KBDUSE_NONE
|
|
|
|
#ifndef CONFIG_HIDKBD_NODEBOUNCE
|
2019-10-23 16:39:09 +02:00
|
|
|
&& rpt->key[i] != lastkey[0]
|
|
|
|
&& rpt->key[i] != lastkey[1]
|
|
|
|
&& rpt->key[i] != lastkey[2]
|
|
|
|
&& rpt->key[i] != lastkey[3]
|
|
|
|
&& rpt->key[i] != lastkey[4]
|
|
|
|
&& rpt->key[i] != lastkey[5]
|
2011-01-20 01:46:56 +01:00
|
|
|
#endif
|
|
|
|
)
|
2011-01-18 03:17:49 +01:00
|
|
|
{
|
|
|
|
/* Yes.. Add it to the buffer. */
|
|
|
|
|
|
|
|
/* Map the keyboard scancode to a printable ASCII
|
|
|
|
* character. There is no support here for function keys
|
|
|
|
* or cursor controls in this version of the driver.
|
|
|
|
*/
|
|
|
|
|
2012-12-26 19:54:59 +01:00
|
|
|
keycode = usbhost_mapscancode(rpt->key[i], rpt->modifier);
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Key %d: %02x keycode:%c modifier: %02x\n",
|
2020-03-30 22:20:53 +02:00
|
|
|
i, rpt->key[i], keycode ? keycode : ' ',
|
|
|
|
rpt->modifier);
|
2011-01-18 03:17:49 +01:00
|
|
|
|
|
|
|
/* Zero at this point means that the key does not map to a
|
|
|
|
* printable character.
|
|
|
|
*/
|
|
|
|
|
2012-12-26 19:54:59 +01:00
|
|
|
if (keycode != 0)
|
2011-01-18 03:17:49 +01:00
|
|
|
{
|
|
|
|
/* Handle control characters. Zero after this means
|
|
|
|
* a valid, NUL character.
|
|
|
|
*/
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
if ((rpt->modifier & (USBHID_MODIFER_LCTRL |
|
|
|
|
USBHID_MODIFER_RCTRL)) != 0)
|
2011-01-18 03:17:49 +01:00
|
|
|
{
|
2012-12-26 19:54:59 +01:00
|
|
|
keycode &= 0x1f;
|
2011-01-18 03:17:49 +01:00
|
|
|
}
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2011-01-18 03:17:49 +01:00
|
|
|
/* Copy the next keyboard character into the user
|
|
|
|
* buffer.
|
|
|
|
*/
|
|
|
|
|
2012-12-26 19:54:59 +01:00
|
|
|
usbhost_putbuffer(priv, keycode);
|
|
|
|
}
|
2011-01-18 03:17:49 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
/* The zero might, however, map to a special keyboard
|
|
|
|
* action (such as a cursor movement or function key).
|
|
|
|
* Attempt to encode the special key.
|
2012-12-26 19:54:59 +01:00
|
|
|
*/
|
2011-01-18 03:17:49 +01:00
|
|
|
|
2012-12-26 19:54:59 +01:00
|
|
|
#ifdef CONFIG_HIDKBD_ENCODED
|
|
|
|
else
|
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
usbhost_encodescancode(priv, rpt->key[i],
|
|
|
|
rpt->modifier);
|
2011-01-18 03:17:49 +01:00
|
|
|
}
|
2012-12-26 19:54:59 +01:00
|
|
|
#endif
|
2011-01-18 03:17:49 +01:00
|
|
|
}
|
2012-12-26 19:54:59 +01:00
|
|
|
|
2011-01-20 01:46:56 +01:00
|
|
|
/* Save the scancode (or lack thereof) for key debouncing on
|
|
|
|
* next keyboard report.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_HIDKBD_NODEBOUNCE
|
|
|
|
lastkey[i] = rpt->key[i];
|
|
|
|
#endif
|
2011-01-18 03:17:49 +01:00
|
|
|
}
|
|
|
|
|
2012-12-26 21:04:57 +01:00
|
|
|
/* Is there data available? */
|
2011-01-19 05:17:45 +01:00
|
|
|
|
2012-12-26 19:54:59 +01:00
|
|
|
newstate = (priv->headndx == priv->tailndx);
|
2012-12-26 21:04:57 +01:00
|
|
|
if (!newstate)
|
2011-01-19 05:17:45 +01:00
|
|
|
{
|
2012-12-26 21:04:57 +01:00
|
|
|
/* Did we just transition from no data available to data
|
|
|
|
* available? If so, wake up any threads waiting for the
|
|
|
|
* POLLIN event.
|
|
|
|
*/
|
2011-01-19 05:17:45 +01:00
|
|
|
|
2012-12-26 21:04:57 +01:00
|
|
|
if (empty)
|
|
|
|
{
|
|
|
|
usbhost_pollnotify(priv);
|
|
|
|
}
|
2020-09-26 14:25:03 +02:00
|
|
|
|
|
|
|
/* Yes.. Is there a thread waiting for keyboard data now? */
|
|
|
|
|
|
|
|
if (priv->waiting)
|
|
|
|
{
|
|
|
|
/* Yes.. wake it up */
|
|
|
|
|
|
|
|
usbhost_givesem(&priv->waitsem);
|
|
|
|
priv->waiting = false;
|
|
|
|
}
|
2011-01-19 05:17:45 +01:00
|
|
|
}
|
|
|
|
|
2012-12-26 19:54:59 +01:00
|
|
|
empty = newstate;
|
2011-01-18 03:17:49 +01:00
|
|
|
usbhost_givesem(&priv->exclsem);
|
2011-01-16 20:08:16 +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)
|
2011-01-16 20:08:16 +01:00
|
|
|
npolls++;
|
2011-01-19 05:17:45 +01:00
|
|
|
if ((npolls & 31) == 0)
|
2011-01-16 20:08:16 +01:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
uinfo("Still polling: %d\n", npolls);
|
2011-01-16 20:08:16 +01:00
|
|
|
}
|
|
|
|
#endif
|
2020-03-30 22:20:53 +02:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Wait for the required amount (or until a signal is received). We
|
|
|
|
* will wake up when either the delay elapses or we are signalled that
|
|
|
|
* the device has been disconnected.
|
2015-04-25 19:17:57 +02:00
|
|
|
*
|
|
|
|
* If we are getting errors, then sleep longer. In the event that
|
|
|
|
* the keyboard is connected via a hub, there may be a significant
|
|
|
|
* amount of time after the keyboard is removed before we are stopped.
|
2011-01-16 20:08:16 +01:00
|
|
|
*/
|
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
if (nerrors > 1)
|
|
|
|
{
|
|
|
|
delay = nerrors * CONFIG_HIDKBD_POLLUSEC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delay = CONFIG_HIDKBD_POLLUSEC;
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(delay);
|
2011-01-16 20:08:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We get here when the driver is removed.. or when too many errors have
|
|
|
|
* been encountered.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
usbhost_forcetake(&priv->exclsem);
|
2011-01-16 20:08:16 +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,
|
2011-01-16 20:08:16 +01:00
|
|
|
* we can destroy it now. Otherwise, we have to wait until the all
|
|
|
|
* of the file descriptors are closed.
|
|
|
|
*/
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2016-06-12 16:31:22 +02:00
|
|
|
uinfo("Keyboard removed, polling halted\n");
|
2015-04-25 19:17:57 +02:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2011-01-16 20:08:16 +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 keyboard 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)
|
2011-01-16 20:08:16 +01:00
|
|
|
{
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Unregister the driver and destroy the instance (while we hold
|
|
|
|
* the semaphore!)
|
|
|
|
*/
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2011-01-16 20:08:16 +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
|
|
|
|
*/
|
2011-01-16 20:08:16 +01:00
|
|
|
|
|
|
|
usbhost_givesem(&priv->exclsem);
|
|
|
|
}
|
2012-12-26 19:54:59 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2011-01-16 20:08:16 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
2014-02-09 20:47:11 +01:00
|
|
|
* configdesc - A pointer to a uint8_t buffer container the configuration
|
|
|
|
* descriptor.
|
2011-01-12 05:03:57 +01:00
|
|
|
* 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
|
2011-01-12 05:03:57 +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)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2011-01-12 05:03:57 +01:00
|
|
|
FAR struct usb_cfgdesc_s *cfgdesc;
|
|
|
|
FAR struct usb_desc_s *desc;
|
2011-01-14 18:06:30 +01:00
|
|
|
FAR struct usbhost_epdesc_s epindesc;
|
|
|
|
FAR struct usbhost_epdesc_s epoutdesc;
|
2011-01-12 05:03:57 +01:00
|
|
|
int remaining;
|
|
|
|
uint8_t found = 0;
|
2011-01-17 14:52:33 +01:00
|
|
|
bool done = false;
|
2011-01-12 05:03:57 +01:00
|
|
|
int ret;
|
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL &&
|
|
|
|
configdesc != NULL && desclen >= sizeof(struct usb_cfgdesc_s));
|
|
|
|
hport = priv->usbclass.hport;
|
2013-08-12 01:11:32 +02:00
|
|
|
|
|
|
|
/* Keep the compiler from complaining about uninitialized variables */
|
|
|
|
|
|
|
|
memset(&epindesc, 0, sizeof(struct usbhost_epdesc_s));
|
|
|
|
memset(&epoutdesc, 0, sizeof(struct usbhost_epdesc_s));
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* 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
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
remaining = (int)usbhost_getle16(cfgdesc->totallen);
|
|
|
|
|
|
|
|
/* Skip to the next entry descriptor */
|
|
|
|
|
|
|
|
configdesc += cfgdesc->len;
|
|
|
|
remaining -= cfgdesc->len;
|
|
|
|
|
2014-02-09 20:47:11 +01:00
|
|
|
/* Loop where there are more descriptors to examine */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-17 14:52:33 +01:00
|
|
|
while (remaining >= sizeof(struct usb_desc_s) && !done)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
|
|
|
/* 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:
|
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
FAR struct usb_ifdesc_s *ifdesc =
|
|
|
|
(FAR struct usb_ifdesc_s *)configdesc;
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Interface descriptor\n");
|
2011-01-12 05:03:57 +01:00
|
|
|
DEBUGASSERT(remaining >= USB_SIZEOF_IFDESC);
|
2011-01-17 14:52:33 +01:00
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
/* Did we already find what we needed
|
|
|
|
* from a preceding interface?
|
|
|
|
*/
|
2011-01-17 14:52:33 +01:00
|
|
|
|
|
|
|
if ((found & USBHOST_RQDFOUND) == USBHOST_RQDFOUND)
|
|
|
|
{
|
|
|
|
/* Yes.. then break out of the loop and use the preceding
|
|
|
|
* interface.
|
|
|
|
*/
|
|
|
|
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
else
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2011-01-17 14:52:33 +01:00
|
|
|
/* Otherwise, save the interface number and discard any
|
|
|
|
* endpoints previously found
|
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-17 14:52:33 +01:00
|
|
|
priv->ifno = ifdesc->ifno;
|
|
|
|
found = USBHOST_IFFOUND;
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* HID descriptor */
|
|
|
|
|
|
|
|
case USBHID_DESCTYPE_HID:
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("HID descriptor\n");
|
2011-01-14 18:06:30 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Endpoint descriptor. We expect one or two interrupt endpoints,
|
|
|
|
* a required IN endpoint and an optional OUT endpoint.
|
|
|
|
*/
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
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;
|
2011-01-14 18:06:30 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Endpoint descriptor\n");
|
2011-01-12 05:03:57 +01:00
|
|
|
DEBUGASSERT(remaining >= USB_SIZEOF_EPDESC);
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* Check for an interrupt endpoint. */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
if ((epdesc->attr & USB_EP_ATTR_XFERTYPE_MASK) ==
|
|
|
|
USB_EP_ATTR_XFER_INT)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2011-01-14 18:06:30 +01:00
|
|
|
/* Yes.. it is a interrupt endpoint. IN or OUT? */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
if (USB_ISEPOUT(epdesc->addr))
|
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
/* It is an interrupt OUT endpoint. There not be more
|
|
|
|
* than one interrupt OUT endpoint.
|
2011-01-14 18:06:30 +01:00
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
if ((found & USBHOST_EPOUTFOUND) != 0)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2014-02-09 20:47:11 +01:00
|
|
|
/* Oops.. more than one endpoint. We don't know what
|
|
|
|
* to do with this.
|
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-02-09 20:47:11 +01:00
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
found |= USBHOST_EPOUTFOUND;
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* Save the interrupt OUT endpoint information */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
epoutdesc.hport = hport;
|
2020-03-30 22:20:53 +02:00
|
|
|
epoutdesc.addr = epdesc->addr &
|
|
|
|
USB_EP_ADDR_NUMBER_MASK;
|
2011-01-14 18:06:30 +01:00
|
|
|
epoutdesc.in = false;
|
2011-01-14 19:38:46 +01:00
|
|
|
epoutdesc.xfrtype = USB_EP_ATTR_XFER_INT;
|
2011-01-16 15:02:42 +01:00
|
|
|
epoutdesc.interval = epdesc->interval;
|
2020-03-30 22:20:53 +02:00
|
|
|
epoutdesc.mxpacketsize =
|
|
|
|
usbhost_getle16(epdesc->mxpacketsize);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Interrupt OUT EP addr:%d mxpacketsize:%d\n",
|
2011-01-14 18:06:30 +01:00
|
|
|
epoutdesc.addr, epoutdesc.mxpacketsize);
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-14 18:06:30 +01:00
|
|
|
/* It is an interrupt IN endpoint. There should be only
|
|
|
|
* one interrupt IN endpoint.
|
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
if ((found & USBHOST_EPINFOUND) != 0)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2014-02-09 20:47:11 +01:00
|
|
|
/* Oops.. more than one endpoint. We don't know what
|
2011-01-14 18:06:30 +01:00
|
|
|
* to do with this.
|
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-02-09 20:47:11 +01:00
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
found |= USBHOST_EPINFOUND;
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* Save the interrupt IN endpoint information */
|
2013-08-12 01:11:32 +02:00
|
|
|
|
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;
|
2011-01-14 18:06:30 +01:00
|
|
|
epindesc.in = 1;
|
2011-01-14 19:38:46 +01:00
|
|
|
epindesc.xfrtype = USB_EP_ATTR_XFER_INT;
|
2011-01-16 15:02:42 +01:00
|
|
|
epindesc.interval = epdesc->interval;
|
2020-03-30 22:20:53 +02:00
|
|
|
epindesc.mxpacketsize =
|
|
|
|
usbhost_getle16(epdesc->mxpacketsize);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Interrupt IN EP addr:%d mxpacketsize:%d\n",
|
2011-01-14 18:06:30 +01:00
|
|
|
epindesc.addr, epindesc.mxpacketsize);
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Other descriptors are just ignored for now */
|
|
|
|
|
|
|
|
default:
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Other descriptor: %d\n", desc->type);
|
2011-01-12 05:03:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-01-17 14:52:33 +01:00
|
|
|
/* 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
|
|
|
|
*/
|
2011-01-17 14:52:33 +01:00
|
|
|
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* Increment the address of the next descriptor */
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
configdesc += desc->len;
|
|
|
|
remaining -= desc->len;
|
|
|
|
}
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* Sanity checking... did we find all of things that we need? */
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
if ((found & USBHOST_RQDFOUND) != USBHOST_RQDFOUND)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
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");
|
2011-01-12 05:03:57 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* We are good... Allocate the endpoints. First, the required interrupt
|
|
|
|
* IN endpoint.
|
|
|
|
*/
|
2011-01-12 05:03:57 +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)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to allocate interrupt IN endpoint\n");
|
2011-01-12 05:03:57 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* Then the optional interrupt OUT endpoint */
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
uinfo("Found EPOOUT:%s\n",
|
|
|
|
(found & USBHOST_EPOUTFOUND) != 0 ? "YES" : "NO");
|
2011-01-14 18:06:30 +01:00
|
|
|
|
|
|
|
if ((found & USBHOST_EPOUTFOUND) != 0)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
ret = DRVR_EPALLOC(hport->drvr, &epoutdesc, &priv->epout);
|
2015-04-25 19:17:57 +02:00
|
|
|
if (ret < 0)
|
2011-01-14 18:06:30 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to allocate interrupt OUT endpoint\n");
|
2020-01-02 17:49:34 +01:00
|
|
|
DRVR_EPFREE(hport->drvr, priv->epin);
|
2011-01-14 18:06:30 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
uinfo("Endpoints allocated\n");
|
2011-01-12 05:03:57 +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:
|
2011-01-12 05:03:57 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int usbhost_devinit(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
2011-01-16 20:08:16 +01:00
|
|
|
char devname[DEV_NAMELEN];
|
2011-01-12 05:03:57 +01:00
|
|
|
int ret;
|
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
/* Set aside a transfer buffer for exclusive
|
|
|
|
* use by the keyboard class driver
|
|
|
|
*/
|
2011-01-14 18:06:30 +01:00
|
|
|
|
|
|
|
ret = usbhost_tdalloc(priv);
|
2015-04-25 19:17:57 +02:00
|
|
|
if (ret < 0)
|
2011-01-14 18:06:30 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to allocate transfer buffer\n");
|
2011-01-14 18:06:30 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
/* 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_HIDKBD_STACKSIZE).
|
2011-01-16 20:08:16 +01:00
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Start poll task\n");
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
/* The inputs to a task started by kthread_create() are very awkard for
|
|
|
|
* this purpose. They are really designed for command line tasks
|
|
|
|
* (argc/argv). So the following is kludge pass binary data when the
|
|
|
|
* keyboard poll task is started.
|
2011-01-16 20:08:16 +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).
|
2011-01-12 05:03:57 +01:00
|
|
|
*/
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
ret = usbhost_takesem(&g_exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
usbhost_tdfree(priv);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
g_priv = priv;
|
|
|
|
|
2017-10-16 19:38:00 +02:00
|
|
|
priv->pollpid = kthread_create("kbdpoll", CONFIG_HIDKBD_DEFPRIO,
|
|
|
|
CONFIG_HIDKBD_STACKSIZE,
|
|
|
|
(main_t)usbhost_kbdpoll,
|
|
|
|
(FAR char * const *)NULL);
|
2018-01-31 23:11:54 +01:00
|
|
|
if (priv->pollpid < 0)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2020-04-09 14:46:24 +02:00
|
|
|
/* Failed to started the poll thread...
|
|
|
|
* probably due to memory resources
|
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
usbhost_givesem(&g_exclsem);
|
2018-01-31 23:11:54 +01:00
|
|
|
ret = (int)priv->pollpid;
|
2011-01-16 20:08:16 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Now wait for the poll task to get properly initialized */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
ret = usbhost_takesem(&g_syncsem);
|
2011-01-16 20:08:16 +01:00
|
|
|
usbhost_givesem(&g_exclsem);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Register the driver */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Register driver\n");
|
2011-01-16 20:08:16 +01:00
|
|
|
usbhost_mkdevname(priv, devname);
|
2013-09-20 23:23:00 +02:00
|
|
|
ret = register_driver(devname, &g_hidkbd_fops, 0666, priv);
|
2011-01-16 20:08:16 +01:00
|
|
|
|
|
|
|
/* We now have to be concerned about asynchronous modification of crefs
|
2014-02-09 20:47:11 +01:00
|
|
|
* because the driver has been registered.
|
2011-01-16 20:08:16 +01:00
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
errout:
|
2020-03-30 22:20:53 +02:00
|
|
|
usbhost_forcetake(&priv->exclsem);
|
2011-01-16 20:08:16 +01:00
|
|
|
priv->crefs--;
|
|
|
|
usbhost_givesem(&priv->exclsem);
|
2020-03-30 22:20:53 +02:00
|
|
|
|
2011-01-12 05:03:57 +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:
|
2011-01-12 05:03:57 +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_putle16
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Put a (possibly unaligned) 16-bit little endian value.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dest - A pointer to the first byte to save the little endian value.
|
|
|
|
* val - The 16-bit value to be saved.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-01-12 05:03:57 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_putle16(uint8_t *dest, uint16_t val)
|
|
|
|
{
|
|
|
|
dest[0] = val & 0xff; /* Little endian means LS byte first in byte stream */
|
|
|
|
dest[1] = val >> 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_getle32
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get a (possibly unaligned) 32-bit little endian value.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dest - A pointer to the first byte to save the big endian value.
|
|
|
|
* val - The 32-bit value to be saved.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-01-12 05:03:57 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline uint32_t usbhost_getle32(const uint8_t *val)
|
|
|
|
{
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Little endian means LS halfword first in byte stream */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
return (uint32_t)usbhost_getle16(&val[2]) << 16 |
|
|
|
|
(uint32_t)usbhost_getle16(val);
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_putle32
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Put a (possibly unaligned) 32-bit little endian value.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dest - A pointer to the first byte to save the little endian value.
|
|
|
|
* val - The 32-bit value to be saved.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-01-12 05:03:57 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-08-20 18:06:39 +02:00
|
|
|
#if 0 /* Not used */
|
2011-01-12 05:03:57 +01:00
|
|
|
static void usbhost_putle32(uint8_t *dest, uint32_t val)
|
|
|
|
{
|
|
|
|
/* Little endian means LS halfword first in byte stream */
|
|
|
|
|
|
|
|
usbhost_putle16(dest, (uint16_t)(val & 0xffff));
|
2020-03-30 22:20:53 +02:00
|
|
|
usbhost_putle16(dest + 2, (uint16_t)(val >> 16));
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
2012-08-20 18:06:39 +02:00
|
|
|
#endif
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_tdalloc
|
|
|
|
*
|
|
|
|
* Description:
|
2011-01-16 20:08:16 +01:00
|
|
|
* Allocate transfer buffer memory.
|
2011-01-12 05:03:57 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to the class instance.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-02-09 20:47:11 +01:00
|
|
|
* On success, zero (OK) is returned. On failure, an negated errno value
|
2011-01-12 05:03:57 +01:00
|
|
|
* is returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
static int usbhost_tdalloc(FAR struct usbhost_state_s *priv)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2015-04-20 20:06:47 +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-20 20:06:47 +02:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
return DRVR_ALLOC(hport->drvr, &priv->tbuffer, &priv->tbuflen);
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_tdfree
|
|
|
|
*
|
|
|
|
* Description:
|
2011-01-16 20:08:16 +01:00
|
|
|
* Free transfer buffer memory.
|
2011-01-12 05:03:57 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to the class instance.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-02-09 20:47:11 +01:00
|
|
|
* On success, zero (OK) is returned. On failure, an negated errno value
|
2011-01-12 05:03:57 +01:00
|
|
|
* is returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
static int usbhost_tdfree(FAR struct usbhost_state_s *priv)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2011-01-12 05:03:57 +01:00
|
|
|
int result = OK;
|
2015-04-20 20:06:47 +02:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
if (priv->tbuffer)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv->usbclass.hport);
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
result = DRVR_FREE(hport->drvr, priv->tbuffer);
|
2011-01-16 20:08:16 +01:00
|
|
|
priv->tbuffer = NULL;
|
|
|
|
priv->tbuflen = 0;
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
2014-02-09 20:47:11 +01:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
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.
|
2011-01-12 05:03:57 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2015-04-21 15:26:28 +02:00
|
|
|
* hport - The hub port that manages the new class instance.
|
2011-01-12 05:03:57 +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:
|
2011-01-12 05:03:57 +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
|
2011-01-12 05:03:57 +01:00
|
|
|
* insufficient resources to create another USB host class instance.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-04-20 20:06:47 +02:00
|
|
|
static FAR struct usbhost_class_s *
|
2015-04-21 15:26:28 +02:00
|
|
|
usbhost_create(FAR struct usbhost_hubport_s *hport,
|
2015-04-20 20:06:47 +02:00
|
|
|
FAR const struct usbhost_id_s *id)
|
2011-01-12 05:03:57 +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-03-30 22:20:53 +02:00
|
|
|
/* Initialize class method function pointers */
|
2011-01-12 05:03:57 +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;
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2014-02-09 20:47:11 +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_kbdpoll() task.
|
2014-02-09 20:47:11 +01:00
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
priv->crefs = 1;
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-19 05:17:45 +01:00
|
|
|
/* Initialize semaphores */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&priv->exclsem, 0, 1);
|
|
|
|
nxsem_init(&priv->waitsem, 0, 0);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2016-11-03 18:00:47 +01:00
|
|
|
/* The waitsem semaphore is used for signaling and, hence, should
|
|
|
|
* not have priority inheritance enabled.
|
|
|
|
*/
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
nxsem_set_protocol(&priv->waitsem, SEM_PRIO_NONE);
|
2016-11-03 18:00:47 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Return the instance of the USB keyboard class driver */
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
return &priv->usbclass;
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* An error occurred. Free the allocation and return NULL on all failures */
|
|
|
|
|
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
usbhost_freeclass(priv);
|
|
|
|
}
|
2014-02-09 20:47:11 +01:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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:
|
2020-03-30 22:20:53 +02:00
|
|
|
* usbclass - The USB host class entry previously obtained from a call
|
|
|
|
* to create().
|
2014-02-09 20:47:11 +01:00
|
|
|
* configdesc - A pointer to a uint8_t buffer container the configuration
|
2020-03-30 22:20:53 +02:00
|
|
|
* descriptor.
|
|
|
|
* desclen - The length in bytes of the configuration descriptor.
|
2011-01-12 05:03:57 +01:00
|
|
|
*
|
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
|
2011-01-12 05:03:57 +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
|
2011-05-12 14:58:56 +02:00
|
|
|
* CLASS_DISCONNECTED to free up the class driver resources.
|
|
|
|
*
|
2011-01-12 05:03:57 +01:00
|
|
|
* Assumptions:
|
2011-01-17 14:52:33 +01:00
|
|
|
* - 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
|
2011-01-12 05:03:57 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
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)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2015-01-11 15:05:09 +01:00
|
|
|
FAR struct usbhost_state_s *priv = (FAR struct usbhost_state_s *)usbclass;
|
2011-01-12 05:03:57 +01:00
|
|
|
int ret;
|
|
|
|
|
2013-08-12 01:11:32 +02:00
|
|
|
DEBUGASSERT(priv != NULL &&
|
2011-01-12 05:03:57 +01:00
|
|
|
configdesc != NULL &&
|
|
|
|
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)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
uerr("ERROR: usbhost_cfgdesc() failed: %d\n", ret);
|
2011-01-12 05:03:57 +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)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
uerr("ERROR: usbhost_devinit() failed: %d\n", ret);
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
}
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2011-01-17 14:52:33 +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 kbdpoll task was started successfully.
|
|
|
|
* In this case, the disconnection will have to be handled on the worker
|
|
|
|
* task.
|
2014-02-09 20:47:11 +01:00
|
|
|
* - Failure occurred after the kbdpoll task was started successfully. In
|
|
|
|
* this case, the disconnection can be performed on the kbdpoll thread.
|
2011-01-17 14:52:33 +01:00
|
|
|
*/
|
2011-01-16 20:08:16 +01:00
|
|
|
|
2011-01-12 05:03:57 +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
|
2011-01-12 05:03:57 +01:00
|
|
|
* create().
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-01-12 05:03:57 +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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
static int usbhost_disconnected(struct usbhost_class_s *usbclass)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2015-01-11 15:05:09 +01:00
|
|
|
FAR struct usbhost_state_s *priv = (FAR struct usbhost_state_s *)usbclass;
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2015-04-26 18:05:15 +02:00
|
|
|
DEBUGASSERT(priv != NULL);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Set an indication to any users of the keyboard device that the device
|
2011-01-12 05:03:57 +01:00
|
|
|
* is no longer available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->disconnected = true;
|
2016-06-20 19:59:15 +02:00
|
|
|
uinfo("Disconnected\n");
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-19 05:17:45 +01:00
|
|
|
/* Is there a thread waiting for keyboard data that will never come? */
|
|
|
|
|
|
|
|
if (priv->waiting)
|
|
|
|
{
|
|
|
|
/* Yes.. wake it up */
|
|
|
|
|
|
|
|
usbhost_givesem(&priv->waitsem);
|
|
|
|
priv->waiting = false;
|
|
|
|
}
|
|
|
|
|
2011-01-17 14:52:33 +01:00
|
|
|
/* Possibilities:
|
|
|
|
*
|
|
|
|
* - Failure occurred before the kbdpoll task was started successfully.
|
|
|
|
* In this case, the disconnection will have to be handled on the worker
|
|
|
|
* task.
|
2014-02-09 20:47:11 +01:00
|
|
|
* - Failure occurred after the kbdpoll task was started successfully. In
|
|
|
|
* this case, the disconnection can be performed on the kbdpoll thread.
|
2011-01-12 05:03:57 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-17 14:52:33 +01:00
|
|
|
if (priv->polling)
|
|
|
|
{
|
2013-08-12 01:11:32 +02:00
|
|
|
/* The polling task is still alive. Signal the keyboard polling task.
|
2011-01-17 14:52:33 +01:00
|
|
|
* 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);
|
2011-01-17 14:52:33 +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);
|
2011-01-17 14:52:33 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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");
|
2011-01-16 20:08:16 +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 */
|
|
|
|
|
2011-01-19 05:17:45 +01:00
|
|
|
DEBUGASSERT(priv && priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS);
|
2020-03-30 22:20:53 +02:00
|
|
|
ret = usbhost_takesem(&priv->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2011-01-16 20:08:16 +01:00
|
|
|
|
|
|
|
/* Check if the keyboard 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.
|
2011-01-16 20:08:16 +01:00
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2011-01-16 20:08:16 +01:00
|
|
|
if (priv->disconnected)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2011-01-16 20:08:16 +01:00
|
|
|
/* 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.
|
2011-01-12 05:03:57 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
ret = -ENODEV;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, just increment the reference count on the driver */
|
|
|
|
|
|
|
|
priv->crefs++;
|
2011-01-18 03:17:49 +01:00
|
|
|
priv->open = true;
|
|
|
|
ret = OK;
|
2011-01-16 20:08:16 +01:00
|
|
|
}
|
2020-03-30 22:20:53 +02:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2011-01-16 20:08:16 +01:00
|
|
|
|
|
|
|
usbhost_givesem(&priv->exclsem);
|
|
|
|
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;
|
2011-01-16 20:08:16 +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;
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entry\n");
|
2011-01-16 20:08:16 +01:00
|
|
|
DEBUGASSERT(filep && filep->f_inode);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
priv = inode->i_private;
|
2011-01-14 18:06:30 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Decrement the reference count on the driver */
|
2011-01-14 18:06:30 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
DEBUGASSERT(priv->crefs >= 1);
|
2020-03-30 22:20:53 +02:00
|
|
|
ret = usbhost_takesem(&priv->exclsem);
|
|
|
|
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();
|
2011-01-16 20:08:16 +01:00
|
|
|
priv->crefs--;
|
2011-01-14 18:06:30 +01:00
|
|
|
|
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.
|
2011-01-16 20:08:16 +01:00
|
|
|
*/
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
if (priv->disconnected)
|
2011-01-18 03:17:49 +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
|
|
|
*/
|
2011-01-14 18:06:30 +01:00
|
|
|
|
2020-03-30 22:20:53 +02: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 */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
priv->open = false;
|
|
|
|
priv->headndx = 0;
|
|
|
|
priv->tailndx = 0;
|
2011-01-16 20:08:16 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
/* Check if the USB keyboard device is still connected. */
|
2011-01-16 20:08:16 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
if (priv->crefs == 0)
|
|
|
|
{
|
|
|
|
/* The polling thread is no longer running */
|
2011-01-16 20:08:16 +01:00
|
|
|
|
2015-04-25 19:17:57 +02:00
|
|
|
DEBUGASSERT(!priv->polling);
|
2011-01-16 20:08:16 +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.
|
|
|
|
*/
|
2011-01-12 05:03:57 +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:47:11 +01:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsig_kill(priv->pollpid, SIGALRM);
|
2015-04-25 19:17:57 +02:00
|
|
|
}
|
|
|
|
}
|
2011-01-18 03:17:49 +01:00
|
|
|
}
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2015-04-25 23:57:57 +02:00
|
|
|
usbhost_givesem(&priv->exclsem);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2011-01-12 05:03:57 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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)
|
2011-01-14 18:06:30 +01:00
|
|
|
{
|
2011-01-16 20:08:16 +01:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct usbhost_state_s *priv;
|
2011-01-19 00:28:02 +01:00
|
|
|
size_t nbytes;
|
2011-01-18 03:17:49 +01:00
|
|
|
unsigned int tail;
|
|
|
|
int ret;
|
2011-01-16 20:08:16 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entry\n");
|
2011-01-16 20:08:16 +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);
|
2020-03-30 22:20:53 +02:00
|
|
|
ret = usbhost_takesem(&priv->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2011-01-16 20:08:16 +01:00
|
|
|
|
|
|
|
/* Check if the keyboard 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
|
2020-03-30 22:20:53 +02:00
|
|
|
* the USB keyboard is no longer connected. Refuse any further
|
|
|
|
* attempts to access the driver.
|
2011-01-16 20:08:16 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -ENODEV;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-19 05:17:45 +01:00
|
|
|
/* Is there keyboard data now? */
|
|
|
|
|
|
|
|
while (priv->tailndx == priv->headndx)
|
|
|
|
{
|
|
|
|
/* No.. were we open non-blocking? */
|
|
|
|
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
/* Yes.. then return a failure */
|
|
|
|
|
|
|
|
ret = -EAGAIN;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for data to be available */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Waiting...\n");
|
2012-12-26 21:04:57 +01:00
|
|
|
|
2011-01-19 05:17:45 +01:00
|
|
|
priv->waiting = true;
|
|
|
|
usbhost_givesem(&priv->exclsem);
|
2020-03-30 22:20:53 +02:00
|
|
|
ret = usbhost_takesem(&priv->waitsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = usbhost_takesem(&priv->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2011-01-19 05:17:45 +01:00
|
|
|
|
|
|
|
/* Did the keyboard become disconnected while we were waiting */
|
|
|
|
|
|
|
|
if (priv->disconnected)
|
|
|
|
{
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Read data from our internal buffer of received characters */
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2011-01-19 00:28:02 +01:00
|
|
|
for (tail = priv->tailndx, nbytes = 0;
|
|
|
|
tail != priv->headndx && nbytes < len;
|
|
|
|
nbytes++)
|
2011-01-18 03:17:49 +01:00
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
/* Copy the next keyboard character into the user buffer */
|
2011-01-19 00:28:02 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
*buffer++ = priv->kbdbuffer[tail];
|
2011-01-19 00:28:02 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
/* Handle wrap-around of the tail index */
|
2011-01-18 03:17:49 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
if (++tail >= CONFIG_HIDKBD_BUFSIZE)
|
|
|
|
{
|
|
|
|
tail = 0;
|
|
|
|
}
|
2011-01-18 03:17:49 +01:00
|
|
|
}
|
2013-09-21 20:19:12 +02:00
|
|
|
|
2011-01-19 00:28:02 +01:00
|
|
|
ret = nbytes;
|
2011-01-18 03:17:49 +01:00
|
|
|
|
2014-02-09 20:47:11 +01:00
|
|
|
/* Update the tail index (perhaps marking the buffer empty) */
|
2011-01-18 03:17:49 +01:00
|
|
|
|
|
|
|
priv->tailndx = tail;
|
2011-01-16 20:08:16 +01:00
|
|
|
}
|
|
|
|
|
2011-01-19 05:17:45 +01:00
|
|
|
errout:
|
2011-01-16 20:08:16 +01:00
|
|
|
usbhost_givesem(&priv->exclsem);
|
2011-01-19 00:28:02 +01:00
|
|
|
return (ssize_t)ret;
|
2011-01-14 18:06:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver write method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-09 20:47:11 +01:00
|
|
|
static ssize_t usbhost_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t len)
|
2011-01-14 18:06:30 +01:00
|
|
|
{
|
2011-01-16 20:08:16 +01:00
|
|
|
/* We won't try to write to the keyboard */
|
|
|
|
|
|
|
|
return -ENOSYS;
|
2011-01-14 18:06:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_poll
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver poll method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
static int usbhost_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2011-01-14 18:06:30 +01:00
|
|
|
bool setup)
|
|
|
|
{
|
2011-01-19 05:17:45 +01:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct usbhost_state_s *priv;
|
2020-03-30 22:20:53 +02:00
|
|
|
int ret;
|
2011-01-19 05:17:45 +01:00
|
|
|
int i;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entry\n");
|
2011-01-19 05:17:45 +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);
|
2020-03-30 22:20:53 +02:00
|
|
|
ret = usbhost_takesem(&priv->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2011-01-19 05:17:45 +01:00
|
|
|
|
|
|
|
/* Check if the keyboard is still connected. We need to disable interrupts
|
|
|
|
* momentarily to assure that there are no asynchronous disconnect events.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->disconnected)
|
2011-01-14 18:06:30 +01:00
|
|
|
{
|
2011-01-19 05:17:45 +01:00
|
|
|
/* No... the driver is no longer bound to the class. That means that
|
2020-03-30 22:20:53 +02:00
|
|
|
* the USB keyboard is no longer connected. Refuse any further
|
|
|
|
* attempts to access the driver.
|
2011-01-19 05:17:45 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -ENODEV;
|
|
|
|
}
|
|
|
|
else if (setup)
|
|
|
|
{
|
2014-02-09 20:47:11 +01:00
|
|
|
/* This is a request to set up the poll. Find an available slot for
|
2011-01-19 05:17:45 +01:00
|
|
|
* the poll structure reference
|
|
|
|
*/
|
|
|
|
|
2011-01-19 21:02:23 +01:00
|
|
|
for (i = 0; i < CONFIG_HIDKBD_NPOLLWAITERS; i++)
|
2011-01-19 05:17:45 +01:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-19 21:02:23 +01:00
|
|
|
if (i >= CONFIG_HIDKBD_NPOLLWAITERS)
|
2011-01-14 18:06:30 +01:00
|
|
|
{
|
2011-01-19 05:17:45 +01:00
|
|
|
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 keyboard data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->headndx != priv->tailndx)
|
|
|
|
{
|
|
|
|
usbhost_pollnotify(priv);
|
2011-01-14 18:06:30 +01:00
|
|
|
}
|
|
|
|
}
|
2011-01-19 05:17:45 +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:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2011-01-19 05:17:45 +01:00
|
|
|
return ret;
|
2011-01-14 18:06:30 +01:00
|
|
|
}
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-01-14 18:06:30 +01:00
|
|
|
* Name: usbhost_kbdinit
|
2011-01-12 05:03:57 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2011-01-14 18:06:30 +01:00
|
|
|
* Initialize the USB storage HID keyboard class driver. This function
|
|
|
|
* should be called be platform-specific code in order to initialize and
|
|
|
|
* register support for the USB host HID keyboard class device.
|
2011-01-12 05:03:57 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-01-12 05:03:57 +01:00
|
|
|
* On success this function will return zero (OK); A negated errno value
|
|
|
|
* will be returned on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
int usbhost_kbdinit(void)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
|
|
|
/* Perform any one-time initialization of the class implementation */
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&g_exclsem, 0, 1);
|
|
|
|
nxsem_init(&g_syncsem, 0, 0);
|
2011-01-16 20:08:16 +01:00
|
|
|
|
2016-11-03 18:00:47 +01:00
|
|
|
/* The g_syncsem semaphore is used for signaling and, hence, should not
|
|
|
|
* have priority inheritance enabled.
|
|
|
|
*/
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
nxsem_set_protocol(&g_syncsem, SEM_PRIO_NONE);
|
2016-11-03 18:00:47 +01:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* Advertise our availability to support (certain) devices */
|
|
|
|
|
2013-09-20 23:23:00 +02:00
|
|
|
return usbhost_registerclass(&g_hidkbd);
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
|
2019-02-11 19:09:26 +01:00
|
|
|
#endif /* CONFIG_USBHOST)&& !CONFIG_USBHOST_INT_DISABLE */
|