2011-01-12 05:03:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/usbhost/usbhost_hidkbd.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
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>
|
2011-01-12 05:03:57 +01:00
|
|
|
#include <semaphore.h>
|
2011-01-14 18:06:30 +01:00
|
|
|
#include <time.h>
|
2011-01-12 05:03:57 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/fs.h>
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
|
|
|
|
#include <nuttx/usb/usb.h>
|
|
|
|
#include <nuttx/usb/usbhost.h>
|
|
|
|
#include <nuttx/usb/hid.h>
|
|
|
|
|
2011-01-16 15:02:42 +01:00
|
|
|
/* Don't compile if prerequisites are not met */
|
|
|
|
|
|
|
|
#if defined(CONFIG_USBHOST) && !defined(CONFIG_USBHOST_INT_DISABLE) && CONFIG_NFILE_DESCRIPTORS > 0
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
/* Configuration ************************************************************/
|
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-16 20:08:16 +01:00
|
|
|
/* Signals must not be disabled as they are needed by usleep */
|
2011-01-14 18:06:30 +01:00
|
|
|
|
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
|
|
|
|
#ifndef CONFIG_HIDKBD_STACKSIZE
|
|
|
|
# define CONFIG_HIDKBD_STACKSIZE 1024
|
2011-01-14 18:06:30 +01:00
|
|
|
#endif
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* Driver support ***********************************************************/
|
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-12 05:03:57 +01:00
|
|
|
|
|
|
|
#define USBHOST_MAX_CREFS 0x7fff
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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 */
|
|
|
|
|
|
|
|
struct usbhost_class_s class;
|
|
|
|
|
|
|
|
/* This is an instance of the USB host driver bound to this class instance */
|
|
|
|
|
|
|
|
struct usbhost_driver_s *drvr;
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* The remainder of the fields are provide o the keyboard class driver */
|
2011-01-12 05:03:57 +01: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-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-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-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.
|
|
|
|
* EP Interrrupt OUT (optional):
|
|
|
|
* - 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-12 05:03:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Semaphores */
|
|
|
|
|
|
|
|
static void usbhost_takesem(sem_t *sem);
|
|
|
|
#define usbhost_givesem(s) sem_post(s);
|
|
|
|
|
|
|
|
/* Memory allocation services */
|
|
|
|
|
|
|
|
static inline FAR struct usbhost_state_s *usbhost_allocclass(void);
|
|
|
|
static inline void usbhost_freeclass(FAR struct usbhost_state_s *class);
|
|
|
|
|
|
|
|
/* Device name management */
|
|
|
|
|
|
|
|
static int usbhost_allocdevno(FAR struct usbhost_state_s *priv);
|
|
|
|
static void usbhost_freedevno(FAR struct usbhost_state_s *priv);
|
|
|
|
static inline void usbhost_mkdevname(FAR struct usbhost_state_s *priv, char *devname);
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Keyboard polling thread */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
static void usbhost_destroy(FAR struct usbhost_state_s *priv);
|
|
|
|
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,
|
|
|
|
FAR const uint8_t *configdesc, int desclen,
|
|
|
|
uint8_t funcaddr);
|
|
|
|
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);
|
|
|
|
static void usbhost_putle32(uint8_t *dest, uint32_t val);
|
|
|
|
|
|
|
|
/* Transfer descriptor memory management */
|
|
|
|
|
|
|
|
static inline int usbhost_tdalloc(FAR struct usbhost_state_s *priv);
|
|
|
|
static inline int usbhost_tdfree(FAR struct usbhost_state_s *priv);
|
|
|
|
|
|
|
|
/* struct usbhost_registry_s methods */
|
|
|
|
|
|
|
|
static struct usbhost_class_s *usbhost_create(FAR struct usbhost_driver_s *drvr,
|
|
|
|
FAR const struct usbhost_id_s *id);
|
|
|
|
|
|
|
|
/* struct usbhost_class_s methods */
|
|
|
|
|
|
|
|
static int usbhost_connect(FAR struct usbhost_class_s *class,
|
|
|
|
FAR const uint8_t *configdesc, int desclen,
|
|
|
|
uint8_t funcaddr);
|
|
|
|
static int usbhost_disconnected(FAR struct usbhost_class_s *class);
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* Driver methods. We export the keyboard as a standard character driver */
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
static int usbhost_open(FAR struct file *filep);
|
|
|
|
static int usbhost_close(FAR struct file *filep);
|
|
|
|
static ssize_t usbhost_read(FAR struct file *filep,
|
2011-01-14 18:06:30 +01:00
|
|
|
FAR char *buffer, size_t len);
|
2011-01-16 20:08:16 +01:00
|
|
|
static ssize_t usbhost_write(FAR struct file *filep,
|
2011-01-14 18:06:30 +01:00
|
|
|
FAR const char *buffer, size_t len);
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
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);
|
|
|
|
#endif
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure provides the registry entry ID informatino 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const const struct usbhost_id_s g_id =
|
|
|
|
{
|
|
|
|
USB_CLASS_HID, /* base */
|
|
|
|
USBHID_SUBCLASS_BOOTIF, /* subclass */
|
|
|
|
USBHID_PROTOCOL_KEYBOARD, /* proto */
|
|
|
|
0, /* vid */
|
|
|
|
0 /* pid */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This is the USB host storage class's registry entry */
|
|
|
|
|
|
|
|
static struct usbhost_registry_s g_skeleton =
|
|
|
|
{
|
|
|
|
NULL, /* flink */
|
|
|
|
usbhost_create, /* create */
|
|
|
|
1, /* nids */
|
|
|
|
&g_id /* id[] */
|
|
|
|
};
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
static const struct file_operations usbhost_fops =
|
|
|
|
{
|
2011-01-16 20:08:16 +01:00
|
|
|
usbhost_open, /* open */
|
|
|
|
usbhost_close, /* close */
|
2011-01-14 18:06:30 +01:00
|
|
|
usbhost_read, /* read */
|
|
|
|
usbhost_write, /* write */
|
|
|
|
0, /* seek */
|
|
|
|
0 /* ioctl */
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
, usbhost_poll /* poll */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
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-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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_takesem(sem_t *sem)
|
|
|
|
{
|
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
|
|
|
while (sem_wait(sem) != 0)
|
|
|
|
{
|
|
|
|
/* The only case that an error should occr here is if the wait was
|
|
|
|
* awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ASSERT(errno == EINTR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* 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());
|
|
|
|
priv = (FAR struct usbhost_state_s *)malloc(sizeof(struct usbhost_state_s));
|
|
|
|
uvdbg("Allocated: %p\n", priv);;
|
|
|
|
return priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_freeclass
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a class instance previously allocated by usbhost_allocclass().
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* class - A reference to the class instance to be freed.
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void usbhost_freeclass(FAR struct usbhost_state_s *class)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(class != NULL);
|
|
|
|
|
|
|
|
/* Free the class instance (calling sched_free() in case we are executing
|
|
|
|
* from an interrupt handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
uvdbg("Freeing: %p\n", class);;
|
|
|
|
free(class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
flags = irqsave();
|
|
|
|
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;
|
2011-01-12 05:03:57 +01:00
|
|
|
irqrestore(flags);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
irqrestore(flags);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
irqstate_t flags = irqsave();
|
|
|
|
g_devinuse &= ~(1 << devno);
|
|
|
|
irqrestore(flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void usbhost_mkdevname(FAR struct usbhost_state_s *priv, char *devname)
|
|
|
|
{
|
2011-01-16 20:08:16 +01:00
|
|
|
(void)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:
|
|
|
|
* The USB device has been disconnected and the refernce count on the USB
|
|
|
|
* host class instance has gone to 1.. Time to destroy the USB host class
|
|
|
|
* instance.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - A reference to the class instance to be destroyed.
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
static void usbhost_destroy(FAR struct usbhost_state_s *priv)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2011-01-14 18:06:30 +01:00
|
|
|
char devname[DEV_NAMELEN];
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
uvdbg("crefs: %d\n", priv->crefs);
|
|
|
|
|
|
|
|
/* Unregister the driver */
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
uvdbg("Unregister driver\n");
|
|
|
|
usbhost_mkdevname(priv, devname);
|
|
|
|
(void)unregister_driver(devname);
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
DRVR_EPFREE(priv->drvr, priv->epin);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->epout)
|
|
|
|
{
|
|
|
|
DRVR_EPFREE(priv->drvr, priv->epout);
|
|
|
|
}
|
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 */
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
sem_destroy(&priv->exclsem);
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* Disconnect the USB host device */
|
|
|
|
|
|
|
|
DRVR_DISCONNECT(priv->drvr);
|
|
|
|
|
|
|
|
/* And free the class instance. Hmmm.. this may execute on the worker
|
|
|
|
* thread and the work structure is part of what is getting freed. That
|
|
|
|
* should be okay because once the work contained is removed from the
|
|
|
|
* queue, it should not longer be accessed by the worker thread.
|
|
|
|
*/
|
|
|
|
|
|
|
|
usbhost_freeclass(priv);
|
|
|
|
}
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_dumprpt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Dump the interesting context of the keyboard report that we just
|
|
|
|
* received.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - A reference to the class instance to be destroyed.
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_DEBUG_USB) && defined(CONFIG_DEBUG_VERBOSE)
|
|
|
|
static inline void usbhost_dumprpt(uint8_t *buffer)
|
|
|
|
{
|
|
|
|
struct usbhid_kbdreport_s *rpt = (struct usbhid_kbdreport_s *)buffer;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 6; i++)
|
|
|
|
{
|
|
|
|
if (rpt->key[i])
|
|
|
|
{
|
|
|
|
uvdbg("Key %d: %08x modifier: %08x\n", rpt->key[i], rpt->modifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define usbhost_dumprpt(buffer)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_kbdpoll
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Periodically check for new keyboard data.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - A reference to the class instance to be destroyed.
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_kbdpoll(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv;
|
|
|
|
FAR struct usb_ctrlreq_s *ctrlreq;
|
|
|
|
#ifdef CONFIG_DEBUG_USB
|
|
|
|
static unsigned int npolls = 0;
|
|
|
|
#endif
|
|
|
|
static unsigned int nerrors;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
uvdbg("Started\n");
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*
|
|
|
|
* NOTE: that the reference count is incremented here. Therefore, we know
|
|
|
|
* that the driver data structure will remain stable while this thread is
|
|
|
|
* running.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv = g_priv;
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
|
|
|
|
priv->polling = true;
|
|
|
|
priv->crefs++;
|
|
|
|
usbhost_givesem(&g_syncsem);
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
/* Loop here until the device is disconnected */
|
|
|
|
|
|
|
|
uvdbg("Entering poll loop\n");
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
usbhost_takesem(&priv->exclsem);
|
|
|
|
|
|
|
|
/* Format the hid report request:
|
|
|
|
*
|
|
|
|
* bmRequestType 10000001
|
|
|
|
* bRequest GET_DESCRIPTOR (0x06)
|
|
|
|
* wValue Descriptor Type and Descriptor Index
|
|
|
|
* wIndex Interface Number
|
|
|
|
* wLength Descriptor Length
|
|
|
|
* Data Descriptor Data
|
|
|
|
*/
|
|
|
|
|
|
|
|
ctrlreq = (struct usb_ctrlreq_s *)priv->tbuffer;
|
|
|
|
ctrlreq->type = USB_REQ_DIR_IN|USB_REQ_RECIPIENT_INTERFACE;
|
|
|
|
ctrlreq->req = USB_REQ_GETDESCRIPTOR;
|
|
|
|
usbhost_putle16(ctrlreq->value, (USBHID_DESCTYPE_REPORT << 8));
|
|
|
|
usbhost_putle16(ctrlreq->index, 0);
|
|
|
|
usbhost_putle16(ctrlreq->len, 8);
|
|
|
|
|
|
|
|
/* Send the report */
|
|
|
|
|
|
|
|
ret = DRVR_CTRLIN(priv->drvr, ctrlreq, priv->tbuffer);
|
|
|
|
usbhost_givesem(&priv->exclsem);
|
|
|
|
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
nerrors++;
|
|
|
|
udbg("ERROR: GETDESCRIPTOR/REPORT, DRVR_CTRLIN returned: %d/%d\n",
|
|
|
|
ret, nerrors);
|
|
|
|
|
|
|
|
if (nerrors > 200)
|
|
|
|
{
|
|
|
|
udbg("Too man errors... aborting: %d\n", nerrors);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If debug is enabled, then dump the interesting poarts of the
|
|
|
|
* report that we just received.
|
|
|
|
*/
|
|
|
|
|
|
|
|
usbhost_dumprpt(priv->tbuffer);
|
|
|
|
|
|
|
|
/* Add the newly recevied keystrokes to our internal buffer */
|
|
|
|
#warning "Missing logic"
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If USB debug is on, then provide some periodic indication that
|
|
|
|
* polling is still happening.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_USB
|
|
|
|
npolls++;
|
|
|
|
if (!(npolls & ~31) == 0)
|
|
|
|
{
|
|
|
|
udbg("Still polling: %d\n", npolls);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
usleep(CONFIG_HIDKBD_POLLUSEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
usbhost_takesem(&priv->exclsem);
|
|
|
|
|
|
|
|
/* Indicate that we are no longer running and decrement the reference
|
|
|
|
* count help by this thread. If there are no other users of the class,
|
|
|
|
* we can destroy it now. Otherwise, we have to wait until the all
|
|
|
|
* of the file descriptors are closed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
udbg("Keyboard removed, polling halted\n");
|
|
|
|
priv->polling = false;
|
|
|
|
if (--priv->crefs < 2)
|
|
|
|
{
|
|
|
|
/* Destroy the instance (while we hold the semaphore!) */
|
|
|
|
|
|
|
|
usbhost_destroy(priv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No, we will destroy the driver instance when it is finally closed */
|
|
|
|
|
|
|
|
usbhost_givesem(&priv->exclsem);
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
* configdesc - A pointer to a uint8_t buffer container the configuration descripor.
|
|
|
|
* desclen - The length in bytes of the configuration descriptor.
|
|
|
|
* funcaddr - The USB address of the function containing the endpoint that EP0
|
|
|
|
* controls
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* On success, zero (OK) is returned. On a failure, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function will *not* be called from an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int usbhost_cfgdesc(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR const uint8_t *configdesc, int desclen,
|
|
|
|
uint8_t funcaddr)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL &&
|
|
|
|
configdesc != NULL &&
|
|
|
|
desclen >= sizeof(struct usb_cfgdesc_s));
|
|
|
|
|
|
|
|
/* Verify that we were passed a configuration descriptor */
|
|
|
|
|
|
|
|
cfgdesc = (FAR struct usb_cfgdesc_s *)configdesc;
|
|
|
|
if (cfgdesc->type != USB_DESC_TYPE_CONFIG)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the total length of the configuration descriptor (little endian).
|
|
|
|
* It might be a good check to get the number of interfaces here too.
|
|
|
|
*/
|
|
|
|
|
|
|
|
remaining = (int)usbhost_getle16(cfgdesc->totallen);
|
|
|
|
|
|
|
|
/* Skip to the next entry descriptor */
|
|
|
|
|
|
|
|
configdesc += cfgdesc->len;
|
|
|
|
remaining -= cfgdesc->len;
|
|
|
|
|
|
|
|
/* Loop where there are more dscriptors to examine */
|
|
|
|
|
|
|
|
while (remaining >= sizeof(struct usb_desc_s))
|
|
|
|
{
|
|
|
|
/* 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:
|
|
|
|
{
|
2011-01-14 18:06:30 +01:00
|
|
|
uvdbg("Interface descriptor\n");
|
2011-01-12 05:03:57 +01:00
|
|
|
DEBUGASSERT(remaining >= USB_SIZEOF_IFDESC);
|
|
|
|
if ((found & USBHOST_IFFOUND) != 0)
|
|
|
|
{
|
|
|
|
/* Oops.. more than one interface. We don't know what to do with this. */
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
found |= USBHOST_IFFOUND;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* HID descriptor */
|
|
|
|
|
|
|
|
case USBHID_DESCTYPE_HID:
|
|
|
|
uvdbg("HID descriptor\n");
|
|
|
|
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:
|
|
|
|
{
|
|
|
|
FAR struct usb_epdesc_s *epdesc = (FAR struct usb_epdesc_s *)configdesc;
|
2011-01-14 18:06:30 +01:00
|
|
|
|
|
|
|
uvdbg("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
|
|
|
|
2011-01-14 18:06:30 +01: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))
|
|
|
|
{
|
2011-01-14 18:06:30 +01:00
|
|
|
/* It is an interrupt OUT endpoint. There not be more than one
|
|
|
|
* interrupt OUT endpoint.
|
|
|
|
*/
|
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
|
|
|
{
|
2011-01-14 18:06:30 +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;
|
|
|
|
}
|
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
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
epoutdesc.addr = epdesc->addr & USB_EP_ADDR_NUMBER_MASK;
|
|
|
|
epoutdesc.in = false;
|
|
|
|
epoutdesc.funcaddr = funcaddr;
|
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;
|
2011-01-14 18:06:30 +01:00
|
|
|
epoutdesc.mxpacketsize = usbhost_getle16(epdesc->mxpacketsize);
|
|
|
|
uvdbg("Interrupt OUT EP addr:%d mxpacketsize:%d\n",
|
|
|
|
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
|
|
|
{
|
2011-01-14 18:06:30 +01:00
|
|
|
/* Oops.. more than one endpint. We don't know what
|
|
|
|
* to do with this.
|
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
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 */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-14 19:38:46 +01:00
|
|
|
epindesc.addr = epdesc->addr & USB_EP_ADDR_NUMBER_MASK;
|
2011-01-14 18:06:30 +01:00
|
|
|
epindesc.in = 1;
|
|
|
|
epindesc.funcaddr = funcaddr;
|
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;
|
2011-01-14 18:06:30 +01:00
|
|
|
epindesc.mxpacketsize = usbhost_getle16(epdesc->mxpacketsize);
|
|
|
|
uvdbg("Interrupt IN EP addr:%d mxpacketsize:%d\n",
|
|
|
|
epindesc.addr, epindesc.mxpacketsize);
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Other descriptors are just ignored for now */
|
|
|
|
|
|
|
|
default:
|
2011-01-14 18:06:30 +01:00
|
|
|
uvdbg("Other descriptor: %d\n", desc->type);
|
2011-01-12 05:03:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the address of the next descriptor */
|
|
|
|
|
|
|
|
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? */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
if ((found & USBHOST_RQDFOUND) != USBHOST_RQDFOUND)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2011-01-14 18:06:30 +01:00
|
|
|
ulldbg("ERROR: Found IF:%s EPIN:%s\n",
|
2011-01-12 05:03:57 +01:00
|
|
|
(found & USBHOST_IFFOUND) != 0 ? "YES" : "NO",
|
2011-01-14 18:06:30 +01:00
|
|
|
(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
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
ret = DRVR_EPALLOC(priv->drvr, &epindesc, &priv->epin);
|
2011-01-12 05:03:57 +01:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2011-01-14 18:06:30 +01:00
|
|
|
udbg("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 */
|
|
|
|
|
|
|
|
ullvdbg("Found EPOOUT:%s\n",
|
|
|
|
(found & USBHOST_EPOUTFOUND) != 0 ? "YES" : "NO");
|
|
|
|
|
|
|
|
if ((found & USBHOST_EPOUTFOUND) != 0)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2011-01-14 18:06:30 +01:00
|
|
|
ret = DRVR_EPALLOC(priv->drvr, &epoutdesc, &priv->epout);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
udbg("ERROR: Failed to allocate interrupt OUT endpoint\n");
|
|
|
|
(void)DRVR_EPFREE(priv->drvr, priv->epin);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ullvdbg("Endpoints allocated\n");
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* 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;
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* Set aside a transfer buffer for exclusive use by the keyboard class driver */
|
|
|
|
|
|
|
|
ret = usbhost_tdalloc(priv);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
udbg("ERROR: Failed to allocate transfer buffer\n");
|
|
|
|
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);
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Start a worker task to poll the USB device. It would be nice to used the
|
|
|
|
* 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-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
uvdbg("user_start: Start poll task\n");
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* The inputs to a task started by task_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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
usbhost_takesem(&g_exclsem);
|
|
|
|
g_priv = priv;
|
|
|
|
|
|
|
|
#ifndef CONFIG_CUSTOM_STACK
|
|
|
|
priv->pollpid = task_create("usbhost", CONFIG_HIDKBD_DEFPRIO,
|
|
|
|
CONFIG_HIDKBD_STACKSIZE,
|
|
|
|
(main_t)usbhost_kbdpoll, (const char **)NULL);
|
|
|
|
#else
|
|
|
|
priv->pollpid = task_create("usbhost", CONFIG_HIDKBD_DEFPRIO,
|
|
|
|
(main_t)hidkbd_waiter, (const char **)NULL);
|
|
|
|
#endif
|
|
|
|
if (priv->pollpid == ERROR)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2011-01-16 20:08:16 +01: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);
|
|
|
|
ret = -ENOMEM;
|
|
|
|
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
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
usbhost_takesem(&g_syncsem);
|
|
|
|
usbhost_givesem(&g_exclsem);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Register the driver */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
uvdbg("Register driver\n");
|
|
|
|
usbhost_mkdevname(priv, devname);
|
|
|
|
ret = register_driver(devname, &usbhost_fops, 0666, NULL);
|
|
|
|
|
|
|
|
/* We now have to be concerned about asynchronous modification of crefs
|
|
|
|
* because the driver has been registerd.
|
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
errout:
|
|
|
|
usbhost_takesem(&priv->exclsem);
|
|
|
|
priv->crefs--;
|
|
|
|
usbhost_givesem(&priv->exclsem);
|
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.
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline uint32_t usbhost_getle32(const uint8_t *val)
|
|
|
|
{
|
|
|
|
/* Little endian means LS halfword first in byte stream */
|
|
|
|
|
|
|
|
return (uint32_t)usbhost_getle16(&val[2]) << 16 | (uint32_t)usbhost_getle16(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
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));
|
|
|
|
usbhost_putle16(dest+2, (uint16_t)(val >> 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* On sucess, zero (OK) is returned. On failure, an negated errno value
|
|
|
|
* is returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int usbhost_tdalloc(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
2011-01-16 20:08:16 +01:00
|
|
|
DEBUGASSERT(priv && priv->tbuffer == NULL);
|
|
|
|
return DRVR_ALLOC(priv->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.
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* On sucess, zero (OK) is returned. On failure, an negated errno value
|
|
|
|
* is returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int usbhost_tdfree(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
|
|
|
int result = OK;
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
if (priv->tbuffer)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
|
|
|
DEBUGASSERT(priv->drvr);
|
2011-01-16 20:08:16 +01:00
|
|
|
result = DRVR_FREE(priv->drvr, priv->tbuffer);
|
|
|
|
priv->tbuffer = NULL;
|
|
|
|
priv->tbuflen = 0;
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* struct usbhost_registry_s methods
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_create
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* drvr - An instance of struct usbhost_driver_s that the class
|
|
|
|
* implementation will "bind" to its state structure and will
|
|
|
|
* subsequently use to communicate with the USB host driver.
|
|
|
|
* id - In the case where the device supports multiple base classes,
|
|
|
|
* subclasses, or protocols, this specifies which to configure for.
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* 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
|
|
|
|
* will fail only if the drvr input parameter is NULL or if there are
|
|
|
|
* insufficient resources to create another USB host class instance.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct usbhost_class_s *usbhost_create(FAR struct usbhost_driver_s *drvr,
|
|
|
|
FAR const struct usbhost_id_s *id)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
/* Initialize class method function pointers */
|
|
|
|
|
|
|
|
priv->class.connect = usbhost_connect;
|
|
|
|
priv->class.disconnected = usbhost_disconnected;
|
|
|
|
|
|
|
|
/* The initial reference count is 1... One reference is held by the driver */
|
|
|
|
|
|
|
|
priv->crefs = 1;
|
|
|
|
|
|
|
|
/* Initialize semphores (this works okay in the interrupt context) */
|
|
|
|
|
|
|
|
sem_init(&priv->exclsem, 0, 1);
|
|
|
|
|
|
|
|
/* Bind the driver to the storage class instance */
|
|
|
|
|
|
|
|
priv->drvr = drvr;
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Return the instance of the USB keyboard class driver */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
return &priv->class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* An error occurred. Free the allocation and return NULL on all failures */
|
|
|
|
|
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
usbhost_freeclass(priv);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* struct usbhost_class_s methods
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
|
|
* 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:
|
|
|
|
* class - The USB host class entry previously obtained from a call to create().
|
|
|
|
* configdesc - A pointer to a uint8_t buffer container the configuration descripor.
|
|
|
|
* desclen - The length in bytes of the configuration descriptor.
|
|
|
|
* funcaddr - The USB address of the function containing the endpoint that EP0
|
|
|
|
* controls
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* On success, zero (OK) is returned. On a failure, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function will *not* be called from an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_connect(FAR struct usbhost_class_s *class,
|
|
|
|
FAR const uint8_t *configdesc, int desclen,
|
|
|
|
uint8_t funcaddr)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv = (FAR struct usbhost_state_s *)class;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL &&
|
|
|
|
configdesc != NULL &&
|
|
|
|
desclen >= sizeof(struct usb_cfgdesc_s));
|
|
|
|
|
|
|
|
/* Parse the configuration descriptor to get the endpoints */
|
|
|
|
|
|
|
|
ret = usbhost_cfgdesc(priv, configdesc, desclen, funcaddr);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
udbg("usbhost_cfgdesc() failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Now configure the device and register the NuttX driver */
|
|
|
|
|
|
|
|
ret = usbhost_devinit(priv);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
udbg("usbhost_devinit() failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Disconnect on any errors detected during initialization. */
|
|
|
|
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
priv->disconnected = true;
|
|
|
|
|
|
|
|
/* Is the polling task still running? If so, then ask it politely to
|
|
|
|
* stop and release its reference count.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (priv->polling)
|
|
|
|
{
|
|
|
|
(void)kill(priv->pollpid, SIGALRM);
|
|
|
|
usleep(500*1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The following operations when crefs == 1 are safe because we know
|
|
|
|
* that there is no outstanding open references to the driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->crefs <= 1)
|
|
|
|
{
|
|
|
|
/* Destroy the class instance */
|
|
|
|
|
|
|
|
usbhost_destroy(priv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
* class - The USB host class entry previously obtained from a call to
|
|
|
|
* create().
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_disconnected(struct usbhost_class_s *class)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv = (FAR struct usbhost_state_s *)class;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
|
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;
|
2011-01-16 20:08:16 +01:00
|
|
|
ullvdbg("Disconnected\n");
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Signal the keyboard polling task. When that task wakes up, it will
|
|
|
|
* decrement the reference count and, perhaps, destroy the class instance.
|
2011-01-12 05:03:57 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
(void)kill(priv->pollpid, SIGALRM);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Character driver methods
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
uvdbg("Entry\n");
|
|
|
|
DEBUGASSERT(filep && filep->f_inode);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
priv = inode->i_private;
|
|
|
|
|
|
|
|
/* Make sure that we have exclusive access to the private data structure */
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS);
|
|
|
|
usbhost_takesem(&priv->exclsem);
|
|
|
|
|
|
|
|
/* Check if the keyboard device is still connected. We need to disable
|
|
|
|
* interrupts momentarily to assure that there are no asynchronous disconnect
|
|
|
|
* events.
|
|
|
|
*/
|
|
|
|
|
|
|
|
flags = irqsave();
|
|
|
|
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++;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
irqrestore(flags);
|
|
|
|
|
|
|
|
usbhost_givesem(&priv->exclsem);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver close method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct usbhost_state_s *priv;
|
|
|
|
irqstate_t flags;
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
uvdbg("Entry\n");
|
|
|
|
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
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
DEBUGASSERT(priv->crefs > 1);
|
|
|
|
usbhost_takesem(&priv->exclsem);
|
|
|
|
priv->crefs--;
|
2011-01-14 18:06:30 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Release the semaphore. The following operations when crefs == 1 are
|
|
|
|
* safe because we know that there is no outstanding open references to
|
|
|
|
* the driver.
|
|
|
|
*/
|
2011-01-14 18:06:30 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
usbhost_givesem(&priv->exclsem);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* We need to disable interrupts momentarily to assure that there are
|
|
|
|
* no asynchronous disconnect events.
|
|
|
|
*/
|
|
|
|
|
|
|
|
flags = irqsave();
|
|
|
|
|
|
|
|
/* Check if the USB keyboard device is still connected. If the device is
|
|
|
|
* not connected and the reference count just decremented to one, then
|
|
|
|
* unregister then free the driver class instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->crefs <= 1 && priv->disconnected)
|
|
|
|
{
|
|
|
|
/* Destroy the class instance */
|
|
|
|
|
|
|
|
usbhost_destroy(priv);
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
irqrestore(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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-01-16 20:08:16 +01: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;
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
uvdbg("Entry\n");
|
|
|
|
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);
|
|
|
|
usbhost_takesem(&priv->exclsem);
|
|
|
|
|
|
|
|
/* Check if the keyboard is still connected. We need to disable interrupts
|
|
|
|
* momentarily to assure that there are no asynchronous disconnect events.
|
|
|
|
*/
|
|
|
|
|
|
|
|
flags = irqsave();
|
|
|
|
if (priv->disconnected)
|
|
|
|
{
|
|
|
|
/* No... the driver is no longer bound to the class. That means that
|
|
|
|
* the USB keybaord is no longer connected. Refuse any further attempts
|
|
|
|
* to access the driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -ENODEV;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Read data from our internal buffer of received characters */
|
|
|
|
#warning "Missing logic"
|
|
|
|
}
|
|
|
|
irqrestore(flags);
|
|
|
|
|
|
|
|
usbhost_givesem(&priv->exclsem);
|
2011-01-14 18:06:30 +01:00
|
|
|
return 0; /* Return EOF for now */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver write method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-01-16 20:08:16 +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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
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)
|
|
|
|
{
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
fds->revents |= (fds->events & (POLLIN|POLLOUT));
|
|
|
|
if (fds->revents != 0)
|
|
|
|
{
|
|
|
|
sem_post(fds->sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* Returned Values:
|
|
|
|
* 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 */
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
sem_init(&g_exclsem, 0, 1);
|
|
|
|
sem_init(&g_syncsem, 0, 0);
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* Advertise our availability to support (certain) devices */
|
|
|
|
|
|
|
|
return usbhost_registerclass(&g_skeleton);
|
|
|
|
}
|
|
|
|
|
2011-01-16 15:02:42 +01:00
|
|
|
#endif /* CONFIG_USBHOST)&& !CONFIG_USBHOST_INT_DISABLE && CONFIG_NFILE_DESCRIPTORS */
|
|
|
|
|
|
|
|
|