2011-01-12 05:03:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/usbhost/usbhost_skeleton.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>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#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>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2011-01-12 05:03:57 +01:00
|
|
|
#include <nuttx/arch.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2011-01-12 05:03:57 +01:00
|
|
|
#include <nuttx/wqueue.h>
|
|
|
|
|
|
|
|
#include <nuttx/usb/usb.h>
|
|
|
|
#include <nuttx/usb/usbhost.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_SCHED_WORKQUEUE
|
2011-01-18 20:58:49 +01:00
|
|
|
# warning "Worker thread support is required (CONFIG_SCHED_WORKQUEUE)"
|
2011-01-12 05:03:57 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Driver support ***********************************************************/
|
2020-03-30 22:20:53 +02:00
|
|
|
|
2024-01-19 03:00:44 +01:00
|
|
|
/* The format used to construct device file path, defined for consistent use
|
|
|
|
* from all places.
|
2011-01-12 05:03:57 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
#define DEV_FORMAT "/dev/skel%c"
|
|
|
|
#define DEV_NAMELEN 12
|
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
|
|
|
|
|
|
|
#define USBHOST_IFFOUND 0x01
|
|
|
|
#define USBHOST_BINFOUND 0x02
|
|
|
|
#define USBHOST_BOUTFOUND 0x04
|
|
|
|
#define USBHOST_ALLFOUND 0x07
|
|
|
|
|
|
|
|
#define USBHOST_MAX_CREFS 0x7fff
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-01-19 03:00:44 +01:00
|
|
|
/* This is the internal, private state of the USB host class driver. */
|
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 to the class driver */
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2024-01-19 03:00:44 +01:00
|
|
|
char devchar; /* char in /dev/skel[n] format name */
|
|
|
|
volatile bool disconnected; /* TRUE: Device has been disconnected */
|
|
|
|
uint8_t ifno; /* Interface number */
|
|
|
|
int16_t crefs; /* Reference count on the driver instance */
|
|
|
|
mutex_t lock; /* Used for mutual exclusive access */
|
|
|
|
struct work_s work; /* For interacting with the worker thread */
|
|
|
|
FAR uint8_t *tbuffer; /* The allocated transfer buffer */
|
|
|
|
size_t tbuflen; /* Size of the allocated transfer buffer */
|
|
|
|
usbhost_ep_t epin; /* IN endpoint */
|
|
|
|
usbhost_ep_t epout; /* OUT endpoint */
|
2011-01-12 05:03:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Memory allocation services */
|
|
|
|
|
|
|
|
static inline FAR struct usbhost_state_s *usbhost_allocclass(void);
|
2015-01-11 15:05:09 +01:00
|
|
|
static inline void usbhost_freeclass(FAR struct usbhost_state_s *usbclass);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
/* Device name management */
|
|
|
|
|
|
|
|
static int usbhost_allocdevno(FAR struct usbhost_state_s *priv);
|
|
|
|
static void usbhost_freedevno(FAR struct usbhost_state_s *priv);
|
2015-01-11 15:05:09 +01:00
|
|
|
static inline void usbhost_mkdevname(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR char *devname);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
/* Worker thread actions */
|
|
|
|
|
|
|
|
static void usbhost_destroy(FAR void *arg);
|
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,
|
2015-01-11 15:05:09 +01:00
|
|
|
FAR const uint8_t *configdesc,
|
2015-04-21 17:54:11 +02:00
|
|
|
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);
|
|
|
|
static void usbhost_putle32(uint8_t *dest, uint32_t val);
|
|
|
|
|
|
|
|
/* Transfer descriptor memory management */
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
static inline int usbhost_talloc(FAR struct usbhost_state_s *priv);
|
|
|
|
static inline int usbhost_tfree(FAR struct usbhost_state_s *priv);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
/* struct usbhost_registry_s methods */
|
2014-04-13 22:32:20 +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 */
|
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
static int usbhost_connect(FAR struct usbhost_class_s *usbclass,
|
2015-04-21 17:54:11 +02:00
|
|
|
FAR const uint8_t *configdesc, int desclen);
|
2015-01-11 15:05:09 +01:00
|
|
|
static int usbhost_disconnected(FAR struct usbhost_class_s *usbclass);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2021-04-04 11:58:11 +02:00
|
|
|
/* Driver methods --
|
|
|
|
* depend upon the type of NuttX driver interface exported
|
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-01-19 03:00:44 +01:00
|
|
|
/* This structure provides the registry entry ID information that will be
|
|
|
|
* used to associate the USB host class driver to a connected USB device.
|
2011-01-12 05:03:57 +01:00
|
|
|
*/
|
|
|
|
|
2019-06-20 14:49:56 +02:00
|
|
|
static const struct usbhost_id_s g_id =
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
|
|
|
0, /* base -- Must be one of the USB_CLASS_* definitions in usb.h */
|
|
|
|
0, /* subclass -- depends on the device */
|
|
|
|
0, /* proto -- depends on the device */
|
|
|
|
0, /* vid */
|
|
|
|
0 /* pid */
|
|
|
|
};
|
|
|
|
|
2024-01-19 03:00:44 +01:00
|
|
|
/* This is the USB host class' registry entry */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
static struct usbhost_registry_s g_skeleton =
|
|
|
|
{
|
|
|
|
NULL, /* flink */
|
|
|
|
usbhost_create, /* create */
|
|
|
|
1, /* nids */
|
|
|
|
&g_id /* id[] */
|
|
|
|
};
|
|
|
|
|
2024-01-19 03:00:44 +01:00
|
|
|
/* The bitmap depicting allocated device names in "/dev/skel[a-z]". */
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
static uint32_t g_devinuse;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_allocclass
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is really part of the logic that implements the create() method
|
|
|
|
* of struct usbhost_registry_s. This function allocates memory for one
|
|
|
|
* new class instance.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
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
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
/* Free the class instance (perhaps calling sched_kmm_free() in case we are
|
2011-04-06 18:40:47 +02:00
|
|
|
* executing from an interrupt handler.
|
2011-01-12 05:03:57 +01:00
|
|
|
*/
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Freeing: %p\n", usbclass);
|
2015-01-11 15:05:09 +01: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-11 15:05:09 +01: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-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_destroy
|
|
|
|
*
|
|
|
|
* Description:
|
2015-01-11 15:05:09 +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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_destroy(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv = (FAR struct usbhost_state_s *)arg;
|
2015-04-21 20:17:49 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
|
|
|
FAR struct usbhost_driver_s *drvr;
|
2015-04-21 17:54:11 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL);
|
|
|
|
hport = priv->usbclass.hport;
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
DEBUGASSERT(hport->drvr);
|
|
|
|
drvr = hport->drvr;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("crefs: %d\n", priv->crefs);
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* Unregister the driver */
|
|
|
|
|
|
|
|
/* Release the device name used by this connection */
|
|
|
|
|
|
|
|
usbhost_freedevno(priv);
|
|
|
|
|
|
|
|
/* Free the endpoints */
|
|
|
|
|
|
|
|
/* Free any transfer buffers */
|
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
/* Free the function address assigned to this device */
|
|
|
|
|
2015-04-24 00:42:53 +02:00
|
|
|
usbhost_devaddr_destroy(hport, hport->funcaddr);
|
|
|
|
hport->funcaddr = 0;
|
2015-04-21 20:17:49 +02:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* Destroy the semaphores */
|
|
|
|
|
|
|
|
/* Disconnect the USB host device */
|
|
|
|
|
2015-05-02 18:44:18 +02:00
|
|
|
DRVR_DISCONNECT(drvr, hport);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
2015-01-11 15:05:09 +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 17:54:11 +02:00
|
|
|
FAR const uint8_t *configdesc, int desclen)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2015-04-21 17:54:11 +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;
|
|
|
|
FAR struct usbhost_epdesc_s bindesc;
|
|
|
|
FAR struct usbhost_epdesc_s boutdesc;
|
|
|
|
int remaining;
|
|
|
|
uint8_t found = 0;
|
|
|
|
int ret;
|
|
|
|
|
2015-04-21 17:54:11 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport &&
|
|
|
|
configdesc != NULL && desclen >= sizeof(struct usb_cfgdesc_s));
|
|
|
|
hport = priv->usbclass.hport;
|
2014-04-13 22:32:20 +02:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* 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:
|
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
FAR struct usb_ifdesc_s *ifdesc =
|
|
|
|
(FAR struct usb_ifdesc_s *)configdesc;
|
2014-04-13 22:32:20 +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
|
|
|
/* Save the interface number and mark ONLY the interface found */
|
|
|
|
|
|
|
|
priv->ifno = ifdesc->ifno;
|
|
|
|
found = USBHOST_IFFOUND;
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* Endpoint descriptor. Here, we expect two bulk endpoints, an IN
|
|
|
|
* and an OUT.
|
|
|
|
*/
|
|
|
|
|
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-17 14:52:33 +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 a bulk 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_BULK)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
|
|
|
/* Yes.. it is a bulk endpoint. IN or OUT? */
|
|
|
|
|
|
|
|
if (USB_ISEPOUT(epdesc->addr))
|
|
|
|
{
|
2011-01-14 18:06:30 +01:00
|
|
|
/* It is an OUT bulk endpoint. There should be only one
|
|
|
|
* bulk OUT endpoint.
|
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
if ((found & USBHOST_BOUTFOUND) != 0)
|
|
|
|
{
|
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;
|
|
|
|
}
|
2020-03-30 22:20:53 +02:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
found |= USBHOST_BOUTFOUND;
|
|
|
|
|
|
|
|
/* Save the bulk OUT endpoint information */
|
|
|
|
|
2015-04-21 17:54:11 +02:00
|
|
|
boutdesc.hport = hport;
|
2020-03-30 22:20:53 +02:00
|
|
|
boutdesc.addr = epdesc->addr &
|
|
|
|
USB_EP_ADDR_NUMBER_MASK;
|
2011-01-12 05:03:57 +01:00
|
|
|
boutdesc.in = false;
|
2011-01-14 19:38:46 +01:00
|
|
|
boutdesc.xfrtype = USB_EP_ATTR_XFER_BULK;
|
2011-01-16 15:02:42 +01:00
|
|
|
boutdesc.interval = epdesc->interval;
|
2020-03-30 22:20:53 +02:00
|
|
|
boutdesc.mxpacketsize =
|
|
|
|
usbhost_getle16(epdesc->mxpacketsize);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Bulk OUT EP addr:%d mxpacketsize:%d\n",
|
2011-01-12 05:03:57 +01:00
|
|
|
boutdesc.addr, boutdesc.mxpacketsize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-14 18:06:30 +01:00
|
|
|
/* It is an IN bulk endpoint. There should be only one
|
|
|
|
* bulk IN endpoint.
|
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
if ((found & USBHOST_BINFOUND) != 0)
|
|
|
|
{
|
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;
|
|
|
|
}
|
2015-04-21 17:54:11 +02:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
found |= USBHOST_BINFOUND;
|
|
|
|
|
|
|
|
/* Save the bulk IN endpoint information */
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2015-04-21 17:54:11 +02:00
|
|
|
bindesc.hport = hport;
|
2020-03-30 22:20:53 +02:00
|
|
|
bindesc.addr = epdesc->addr &
|
|
|
|
USB_EP_ADDR_NUMBER_MASK;
|
2011-01-12 05:03:57 +01:00
|
|
|
bindesc.in = 1;
|
2011-01-14 19:38:46 +01:00
|
|
|
bindesc.xfrtype = USB_EP_ATTR_XFER_BULK;
|
2011-01-16 15:02:42 +01:00
|
|
|
bindesc.interval = epdesc->interval;
|
2020-03-30 22:20:53 +02:00
|
|
|
bindesc.mxpacketsize =
|
|
|
|
usbhost_getle16(epdesc->mxpacketsize);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Bulk IN EP addr:%d mxpacketsize:%d\n",
|
2011-01-12 05:03:57 +01:00
|
|
|
bindesc.addr, bindesc.mxpacketsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Other descriptors are just ignored for now */
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-01-17 14:52:33 +01:00
|
|
|
/* If we found everything we need with this interface, then break out
|
|
|
|
* of the loop early.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (found == USBHOST_ALLFOUND)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/* Increment the address of the next descriptor */
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
configdesc += desc->len;
|
|
|
|
remaining -= desc->len;
|
|
|
|
}
|
|
|
|
|
2011-01-17 14:52:33 +01:00
|
|
|
/* Sanity checking... did we find all of things that we need? */
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
if (found != USBHOST_ALLFOUND)
|
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
uerr("ERROR: Found IF:%s BIN:%s BOUT:%s\n",
|
|
|
|
(found & USBHOST_IFFOUND) != 0 ? "YES" : "NO",
|
|
|
|
(found & USBHOST_BINFOUND) != 0 ? "YES" : "NO",
|
|
|
|
(found & USBHOST_BOUTFOUND) != 0 ? "YES" : "NO");
|
2011-01-12 05:03:57 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are good... Allocate the endpoints */
|
|
|
|
|
2015-04-21 17:54:11 +02:00
|
|
|
ret = DRVR_EPALLOC(hport->drvr, &boutdesc, &priv->epout);
|
2015-05-05 23:48:48 +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 Bulk OUT endpoint\n");
|
2011-01-12 05:03:57 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-04-21 17:54:11 +02:00
|
|
|
ret = DRVR_EPALLOC(hport->drvr, &bindesc, &priv->epin);
|
2015-05-05 23:48:48 +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 Bulk IN endpoint\n");
|
2020-01-02 17:49:34 +01:00
|
|
|
DRVR_EPFREE(hport->drvr, priv->epout);
|
2011-01-12 05:03:57 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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-19 22:17:37 +01:00
|
|
|
int ret = OK;
|
2011-01-12 05:03:57 +01:00
|
|
|
|
|
|
|
/* Set aside a transfer buffer for exclusive use by the class driver */
|
|
|
|
|
|
|
|
/* Increment the reference count. This will prevent usbhost_destroy() from
|
|
|
|
* being called asynchronously if the device is removed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->crefs++;
|
|
|
|
DEBUGASSERT(priv->crefs == 2);
|
|
|
|
|
|
|
|
/* Configure the device */
|
|
|
|
|
|
|
|
/* Register the driver */
|
|
|
|
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret >= 0)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
|
|
|
char devname[DEV_NAMELEN];
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Register block driver\n");
|
2011-01-12 05:03:57 +01:00
|
|
|
usbhost_mkdevname(priv, devname);
|
2020-03-30 22:20:53 +02:00
|
|
|
#if 0 /* Finish me */
|
|
|
|
ret = register_blockdriver(devname, &g_bops, 0, priv);
|
|
|
|
#endif
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we successfully initialized. We now have to be concerned
|
|
|
|
* about asynchronous modification of crefs because the block
|
2020-02-23 09:50:23 +01:00
|
|
|
* driver has been registered.
|
2011-01-12 05:03:57 +01:00
|
|
|
*/
|
|
|
|
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret >= 0)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-03-30 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-11-03 11:16:29 +01:00
|
|
|
return ret;
|
2020-03-30 22:20:53 +02:00
|
|
|
}
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
DEBUGASSERT(priv->crefs >= 2);
|
|
|
|
|
|
|
|
/* Handle a corner case where (1) open() has been called so the
|
|
|
|
* reference count is > 2, but the device has been disconnected.
|
|
|
|
* In this case, the class instance needs to persist until close()
|
|
|
|
* is called.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->crefs <= 2 && priv->disconnected)
|
|
|
|
{
|
|
|
|
/* We don't have to give the semaphore because it will be
|
|
|
|
* destroyed when usb_destroy is called.
|
|
|
|
*/
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
ret = -ENODEV;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Ready for normal operation as a block device driver */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Successfully initialized\n");
|
2011-01-12 05:03:57 +01:00
|
|
|
priv->crefs--;
|
|
|
|
}
|
2022-09-06 08:18:45 +02:00
|
|
|
|
|
|
|
nxmutex_unlock(&priv->lock);
|
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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-01-16 20:08:16 +01:00
|
|
|
* Name: usbhost_talloc
|
2011-01-12 05:03:57 +01:00
|
|
|
*
|
|
|
|
* 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:
|
2018-09-14 14:55:45 +02: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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
static inline int usbhost_talloc(FAR struct usbhost_state_s *priv)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2015-04-21 17:54:11 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL &&
|
|
|
|
priv->tbuffer == NULL);
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
|
|
|
|
return DRVR_ALLOC(hport->drvr, &priv->tbuffer, &priv->tbuflen);
|
2011-01-12 05:03:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-01-16 20:08:16 +01:00
|
|
|
* Name: usbhost_tfree
|
2011-01-12 05:03:57 +01:00
|
|
|
*
|
|
|
|
* 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:
|
2018-09-14 14:55:45 +02: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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
static inline int usbhost_tfree(FAR struct usbhost_state_s *priv)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2015-04-21 17:54:11 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2011-01-12 05:03:57 +01:00
|
|
|
int result = OK;
|
2015-04-21 17:54:11 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
if (priv->tbuffer)
|
2011-01-12 05:03:57 +01:00
|
|
|
{
|
2015-04-21 17:54:11 +02:00
|
|
|
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
|
|
|
}
|
2015-04-21 17:54:11 +02: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 17:54:11 +02:00
|
|
|
* hport - The hub hat 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 17:54:11 +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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
static FAR 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
|
|
|
{
|
|
|
|
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 17:54:11 +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
|
|
|
|
2021-04-04 11:58:11 +02:00
|
|
|
/* The initial reference count is 1...
|
|
|
|
* One reference is held by the driver
|
|
|
|
*/
|
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
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Initialize mutex
|
2021-04-04 11:58:11 +02:00
|
|
|
* (this works okay in the interrupt context)
|
|
|
|
*/
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&priv->lock);
|
2011-01-12 05:03:57 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Return the instance of the USB class driver */
|
2014-04-13 22:32:20 +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);
|
|
|
|
}
|
2020-03-30 22:20:53 +02:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* struct usbhost_class_s methods
|
|
|
|
****************************************************************************/
|
2020-03-30 22:20:53 +02:00
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_connect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements the connect() method of struct
|
|
|
|
* usbhost_class_s. This method is a callback into the class
|
|
|
|
* implementation. It is used to provide the device's configuration
|
|
|
|
* descriptor to the class so that the class may initialize properly
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2015-01-11 15:05:09 +01:00
|
|
|
* usbclass - The USB host class entry previously obtained from a call to
|
|
|
|
* create().
|
|
|
|
* 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
|
|
|
*
|
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 17:54:11 +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;
|
|
|
|
|
2014-04-13 22:32:20 +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 17:54:11 +02:00
|
|
|
ret = usbhost_cfgdesc(priv, configdesc, desclen);
|
2015-05-05 23:48:48 +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-05-05 23:48:48 +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
|
|
|
}
|
|
|
|
}
|
2014-04-13 22:32:20 +02: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
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* Set an indication to any users of the device that the device is no
|
|
|
|
* longer available.
|
2011-01-12 05:03:57 +01:00
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2011-01-12 05:03:57 +01:00
|
|
|
priv->disconnected = true;
|
|
|
|
|
|
|
|
/* Now check the number of references on the class instance. If it is one,
|
|
|
|
* then we can free the class instance now. Otherwise, we will have to
|
|
|
|
* wait until the holders of the references free them by closing the
|
|
|
|
* block driver.
|
|
|
|
*/
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
uinfo("crefs: %d\n", priv->crefs);
|
2011-01-12 05:03:57 +01:00
|
|
|
if (priv->crefs == 1)
|
|
|
|
{
|
|
|
|
/* Destroy the class instance. If we are executing from an interrupt
|
|
|
|
* handler, then defer the destruction to the worker thread.
|
|
|
|
* Otherwise, destroy the instance now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (up_interrupt_context())
|
|
|
|
{
|
|
|
|
/* Destroy the instance on the worker thread. */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
uinfo("Queuing destruction: worker %p->%p\n",
|
|
|
|
priv->work.worker, usbhost_destroy);
|
|
|
|
|
2011-01-12 05:03:57 +01:00
|
|
|
DEBUGASSERT(priv->work.worker == NULL);
|
2020-01-02 17:49:34 +01:00
|
|
|
work_queue(HPWORK, &priv->work, usbhost_destroy, priv, 0);
|
2020-03-30 22:20:53 +02:00
|
|
|
}
|
2011-01-12 05:03:57 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Do the work now */
|
|
|
|
|
|
|
|
usbhost_destroy(priv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2011-01-12 05:03:57 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_skelinit
|
|
|
|
*
|
|
|
|
* Description:
|
2011-01-14 18:06:30 +01:00
|
|
|
* Initialize the USB class driver. This function should be called
|
2011-01-12 05:03:57 +01:00
|
|
|
* be platform-specific code in order to initialize and register support
|
2011-01-14 18:06:30 +01:00
|
|
|
* for the USB host 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int usbhost_skelinit(void)
|
|
|
|
{
|
|
|
|
/* Perform any one-time initialization of the class implementation */
|
|
|
|
|
|
|
|
/* Advertise our availability to support (certain) devices */
|
|
|
|
|
|
|
|
return usbhost_registerclass(&g_skeleton);
|
|
|
|
}
|