2010-12-14 17:35:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/usbhost/usbhost_storage.c
|
|
|
|
*
|
2020-03-21 17:36:26 +01: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.
|
2010-12-14 17:35:10 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2021-07-21 09:27:36 +02:00
|
|
|
#include <inttypes.h>
|
2010-12-16 02:29:51 +01:00
|
|
|
#include <stdio.h>
|
2010-12-15 04:30:20 +01:00
|
|
|
#include <stdlib.h>
|
2010-12-16 02:29:51 +01:00
|
|
|
#include <string.h>
|
2012-08-29 00:28:49 +02:00
|
|
|
#include <unistd.h>
|
2010-12-15 04:12:09 +01:00
|
|
|
#include <assert.h>
|
2010-12-16 02:29:51 +01:00
|
|
|
#include <errno.h>
|
2010-12-15 04:12:09 +01:00
|
|
|
#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>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2010-12-15 19:39:19 +01:00
|
|
|
#include <nuttx/arch.h>
|
2010-12-15 23:28:35 +01:00
|
|
|
#include <nuttx/wqueue.h>
|
2010-12-18 02:53:05 +01:00
|
|
|
#include <nuttx/scsi.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2020-02-01 08:17:32 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2010-12-16 02:29:51 +01:00
|
|
|
|
2010-12-14 17:35:10 +01:00
|
|
|
#include <nuttx/usb/usb.h>
|
|
|
|
#include <nuttx/usb/usbhost.h>
|
2011-01-12 03:12:41 +01:00
|
|
|
#include <nuttx/usb/storage.h>
|
2015-04-21 20:17:49 +02:00
|
|
|
#include <nuttx/usb/usbhost_devaddr.h>
|
2010-12-14 17:35:10 +01:00
|
|
|
|
2011-01-16 15:02:42 +01:00
|
|
|
/* Don't compile if prerequisites are not met */
|
|
|
|
|
|
|
|
#if defined(CONFIG_USBHOST) && !defined(CONFIG_USBHOST_BULK_DISABLE) && \
|
2019-02-11 19:09:26 +01:00
|
|
|
!defined(CONFIG_DISABLE_MOUNTPOINT)
|
2010-12-16 01:49:02 +01:00
|
|
|
|
2010-12-15 04:12:09 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
#ifndef CONFIG_SCHED_WORKQUEUE
|
2011-01-18 20:58:49 +01:00
|
|
|
# warning "Worker thread support is required (CONFIG_SCHED_WORKQUEUE)"
|
2010-12-15 23:28:35 +01:00
|
|
|
#endif
|
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
/* If the create() method is called by the USB host device driver from an
|
2014-09-01 01:26:36 +02:00
|
|
|
* interrupt handler, then it will be unable to call kmm_malloc() in order to
|
2010-12-15 19:39:19 +01:00
|
|
|
* allocate a new class instance. If the create() method is called from the
|
|
|
|
* interrupt level, then class instances must be pre-allocated.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBHOST_NPREALLOC
|
|
|
|
# define CONFIG_USBHOST_NPREALLOC 0
|
|
|
|
#endif
|
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
#if CONFIG_USBHOST_NPREALLOC > 26
|
|
|
|
# error "Currently limited to 26 devices /dev/sda-z"
|
|
|
|
#endif
|
|
|
|
|
2010-12-16 02:29:51 +01:00
|
|
|
/* Driver support ***********************************************************/
|
2020-03-21 20:54:03 +01:00
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
/* This format is used to construct the /dev/sd[n] device driver path. It
|
|
|
|
* defined here so that it will be used consistently in all places.
|
|
|
|
*/
|
|
|
|
|
2010-12-18 04:24:13 +01:00
|
|
|
#define DEV_FORMAT "/dev/sd%c"
|
|
|
|
#define DEV_NAMELEN 10
|
2010-12-15 23:28:35 +01:00
|
|
|
|
2010-12-21 12:21:07 +01:00
|
|
|
/* Used in usbhost_connect() */
|
2010-12-16 02:29:51 +01:00
|
|
|
|
2010-12-18 04:24:13 +01:00
|
|
|
#define USBHOST_IFFOUND 0x01
|
|
|
|
#define USBHOST_BINFOUND 0x02
|
|
|
|
#define USBHOST_BOUTFOUND 0x04
|
|
|
|
#define USBHOST_ALLFOUND 0x07
|
|
|
|
|
2013-09-08 21:42:56 +02:00
|
|
|
#define USBHOST_RETRY_USEC (50*1000) /* Retry each 50 milliseconds */
|
|
|
|
#define USBHOST_MAX_RETRIES 100 /* Give up after 5 seconds */
|
|
|
|
#define USBHOST_MAX_CREFS INT16_MAX /* Max cref count before signed overflow */
|
2010-12-16 02:29:51 +01:00
|
|
|
|
2010-12-15 04:12:09 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
/* This structure contains the internal, private state of the USB host mass
|
2010-12-15 04:12:09 +01:00
|
|
|
* storage class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct usbhost_state_s
|
|
|
|
{
|
|
|
|
/* This is the externally visible portion of the state */
|
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
struct usbhost_class_s usbclass;
|
2010-12-15 04:30:20 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
/* The remainder of the fields are provide to the mass storage class */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
char sdchar; /* Character identifying the /dev/sd[n] device */
|
|
|
|
volatile bool disconnected; /* TRUE: Device has been disconnected */
|
2011-01-17 14:52:33 +01:00
|
|
|
uint8_t ifno; /* Interface number */
|
2011-01-09 03:15:30 +01:00
|
|
|
int16_t crefs; /* Reference count on the driver instance */
|
|
|
|
uint16_t blocksize; /* Block size of USB mass storage device */
|
|
|
|
uint32_t nblocks; /* Number of blocks on the USB mass storage device */
|
|
|
|
sem_t exclsem; /* Used to maintain mutual exclusive access */
|
|
|
|
struct work_s work; /* For interacting with the worker thread */
|
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 */
|
2011-01-11 02:41:06 +01:00
|
|
|
usbhost_ep_t bulkin; /* Bulk IN endpoint */
|
|
|
|
usbhost_ep_t bulkout; /* Bulk OUT endpoint */
|
2010-12-15 04:12:09 +01:00
|
|
|
};
|
|
|
|
|
2013-08-12 01:11:32 +02:00
|
|
|
/* This is how struct usbhost_state_s looks to the free list logic */
|
|
|
|
|
|
|
|
struct usbhost_freestate_s
|
|
|
|
{
|
|
|
|
FAR struct usbhost_freestate_s *flink;
|
|
|
|
};
|
|
|
|
|
2010-12-14 17:35:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-12-16 02:29:51 +01:00
|
|
|
/* Semaphores */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
static int usbhost_takesem(FAR sem_t *sem);
|
|
|
|
static void usbhost_forcetake(FAR sem_t *sem);
|
2017-10-03 23:35:24 +02:00
|
|
|
#define usbhost_givesem(s) nxsem_post(s);
|
2010-12-16 02:29:51 +01:00
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
/* Memory allocation services */
|
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
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);
|
2010-12-15 23:28:35 +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);
|
2010-12-15 23:28:35 +01:00
|
|
|
|
2011-01-01 17:56:59 +01:00
|
|
|
/* CBW/CSW debug helpers */
|
|
|
|
|
2016-06-11 19:50:18 +02:00
|
|
|
#if defined(CONFIG_DEBUG_USB) && defined(CONFIG_DEBUG_INFO)
|
2012-01-25 21:17:59 +01:00
|
|
|
static void usbhost_dumpcbw(FAR struct usbmsc_cbw_s *cbw);
|
|
|
|
static void usbhost_dumpcsw(FAR struct usbmsc_csw_s *csw);
|
2011-01-01 17:56:59 +01:00
|
|
|
#else
|
|
|
|
# define usbhost_dumpcbw(cbw);
|
|
|
|
# define usbhost_dumpcsw(csw);
|
|
|
|
#endif
|
|
|
|
|
2010-12-18 02:53:05 +01:00
|
|
|
/* CBW helpers */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline void usbhost_requestsensecbw(FAR struct usbmsc_cbw_s *cbw);
|
|
|
|
static inline void usbhost_testunitreadycbw(FAR struct usbmsc_cbw_s *cbw);
|
|
|
|
static inline void usbhost_readcapacitycbw(FAR struct usbmsc_cbw_s *cbw);
|
|
|
|
static inline void usbhost_inquirycbw (FAR struct usbmsc_cbw_s *cbw);
|
2021-02-17 08:58:11 +01:00
|
|
|
static inline void usbhost_readcbw (blkcnt_t startsector, uint16_t blocksize,
|
2010-12-18 02:53:05 +01:00
|
|
|
unsigned int nsectors,
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_cbw_s *cbw);
|
2021-02-17 08:58:11 +01:00
|
|
|
static inline void usbhost_writecbw(blkcnt_t startsector, uint16_t blocksize,
|
2010-12-18 02:53:05 +01:00
|
|
|
unsigned int nsectors,
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_cbw_s *cbw);
|
2020-03-21 20:54:03 +01:00
|
|
|
|
2010-12-18 04:24:13 +01:00
|
|
|
/* Command helpers */
|
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
static inline int usbhost_maxlunreq(FAR struct usbhost_state_s *priv);
|
|
|
|
static inline int usbhost_testunitready(FAR struct usbhost_state_s *priv);
|
|
|
|
static inline int usbhost_requestsense(FAR struct usbhost_state_s *priv);
|
|
|
|
static inline int usbhost_readcapacity(FAR struct usbhost_state_s *priv);
|
|
|
|
static inline int usbhost_inquiry(FAR struct usbhost_state_s *priv);
|
2010-12-18 02:53:05 +01:00
|
|
|
|
2010-12-15 23:28:35 +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-11 02:41:06 +01:00
|
|
|
static inline int usbhost_cfgdesc(FAR struct usbhost_state_s *priv,
|
2020-03-21 20:54:03 +01:00
|
|
|
FAR const uint8_t *configdesc,
|
|
|
|
int desclen);
|
2011-01-11 02:41:06 +01:00
|
|
|
static inline int usbhost_initvolume(FAR struct usbhost_state_s *priv);
|
2010-12-15 23:28:35 +01:00
|
|
|
|
2010-12-17 03:19:04 +01:00
|
|
|
/* (Little Endian) Data helpers */
|
2010-12-15 23:28:35 +01:00
|
|
|
|
2010-12-17 03:19:04 +01:00
|
|
|
static inline uint16_t usbhost_getle16(const uint8_t *val);
|
2010-12-18 18:35:54 +01:00
|
|
|
static inline uint16_t usbhost_getbe16(const uint8_t *val);
|
2010-12-17 03:19:04 +01:00
|
|
|
static inline void usbhost_putle16(uint8_t *dest, uint16_t val);
|
2010-12-18 02:53:05 +01:00
|
|
|
static inline void usbhost_putbe16(uint8_t *dest, uint16_t val);
|
2011-01-01 17:56:59 +01:00
|
|
|
static inline uint32_t usbhost_getle32(const uint8_t *val);
|
2010-12-18 18:35:54 +01:00
|
|
|
static inline uint32_t usbhost_getbe32(const uint8_t *val);
|
2010-12-17 03:19:04 +01:00
|
|
|
static void usbhost_putle32(uint8_t *dest, uint32_t val);
|
2010-12-18 02:53:05 +01:00
|
|
|
static void usbhost_putbe32(uint8_t *dest, uint32_t val);
|
2010-12-17 03:19:04 +01:00
|
|
|
|
|
|
|
/* 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);
|
2020-03-21 20:54:03 +01:00
|
|
|
static FAR struct usbmsc_cbw_s *
|
|
|
|
usbhost_cbwalloc(FAR struct usbhost_state_s *priv);
|
2010-12-15 23:28:35 +01:00
|
|
|
|
2010-12-15 04:12:09 +01:00
|
|
|
/* struct usbhost_registry_s methods */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2015-04-21 00:50:32 +02:00
|
|
|
static struct usbhost_class_s *
|
2015-04-21 15:26:28 +02:00
|
|
|
usbhost_create(FAR struct usbhost_hubport_s *hport,
|
2015-04-21 00:50:32 +02:00
|
|
|
FAR const struct usbhost_id_s *id);
|
2010-12-15 04:12:09 +01:00
|
|
|
|
2010-12-15 04:30:20 +01:00
|
|
|
/* struct usbhost_class_s methods */
|
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
static int usbhost_connect(FAR struct usbhost_class_s *usbclass,
|
2015-04-21 00:21:13 +02:00
|
|
|
FAR const uint8_t *configdesc, int desclen);
|
2015-01-11 15:05:09 +01:00
|
|
|
static int usbhost_disconnected(FAR struct usbhost_class_s *usbclass);
|
2010-12-15 04:30:20 +01:00
|
|
|
|
2010-12-15 04:12:09 +01:00
|
|
|
/* struct block_operations methods */
|
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
static int usbhost_open(FAR struct inode *inode);
|
|
|
|
static int usbhost_close(FAR struct inode *inode);
|
2020-03-21 20:54:03 +01:00
|
|
|
static ssize_t usbhost_read(FAR struct inode *inode,
|
2021-02-17 08:58:11 +01:00
|
|
|
FAR unsigned char *buffer, blkcnt_t startsector,
|
2020-03-21 20:54:03 +01:00
|
|
|
unsigned int nsectors);
|
2010-12-15 04:12:09 +01:00
|
|
|
static ssize_t usbhost_write(FAR struct inode *inode,
|
2020-03-21 20:54:03 +01:00
|
|
|
FAR const unsigned char *buffer,
|
2021-02-17 08:58:11 +01:00
|
|
|
blkcnt_t startsector, unsigned int nsectors);
|
2010-12-15 23:28:35 +01:00
|
|
|
static int usbhost_geometry(FAR struct inode *inode,
|
|
|
|
FAR struct geometry *geometry);
|
|
|
|
static int usbhost_ioctl(FAR struct inode *inode, int cmd,
|
|
|
|
unsigned long arg);
|
2010-12-14 17:35:10 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-08 21:12:36 +02:00
|
|
|
/* This structure provides the registry entry ID information that will be
|
2010-12-15 19:39:19 +01:00
|
|
|
* used to associate the USB host mass storage class to a connected USB
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 19:11:11 +02:00
|
|
|
static const struct usbhost_id_s g_id =
|
2010-12-14 17:35:10 +01:00
|
|
|
{
|
2010-12-15 04:30:20 +01:00
|
|
|
USB_CLASS_MASS_STORAGE, /* base */
|
2020-03-21 20:54:03 +01:00
|
|
|
USBMSC_SUBCLASS_SCSI, /* subclass */
|
|
|
|
USBMSC_PROTO_BULKONLY, /* proto */
|
2010-12-15 04:30:20 +01:00
|
|
|
0, /* vid */
|
|
|
|
0 /* pid */
|
2010-12-15 04:12:09 +01:00
|
|
|
};
|
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
/* This is the USB host storage class's registry entry */
|
|
|
|
|
2010-12-15 04:12:09 +01:00
|
|
|
static struct usbhost_registry_s g_storage =
|
|
|
|
{
|
2010-12-15 04:30:20 +01:00
|
|
|
NULL, /* flink */
|
|
|
|
usbhost_create, /* create */
|
|
|
|
1, /* nids */
|
|
|
|
&g_id /* id[] */
|
2010-12-15 04:12:09 +01:00
|
|
|
};
|
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
/* Block driver operations. This is the interface exposed to NuttX by the
|
|
|
|
* class that permits it to behave like a block driver.
|
|
|
|
*/
|
|
|
|
|
2010-12-15 04:12:09 +01:00
|
|
|
static const struct block_operations g_bops =
|
|
|
|
{
|
2010-12-15 04:30:20 +01:00
|
|
|
usbhost_open, /* open */
|
|
|
|
usbhost_close, /* close */
|
|
|
|
usbhost_read, /* read */
|
|
|
|
usbhost_write, /* write */
|
|
|
|
usbhost_geometry, /* geometry */
|
|
|
|
usbhost_ioctl /* ioctl */
|
2010-12-14 17:35:10 +01:00
|
|
|
};
|
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
/* This is an array of pre-allocated USB host storage class instances */
|
|
|
|
|
|
|
|
#if CONFIG_USBHOST_NPREALLOC > 0
|
|
|
|
static struct usbhost_state_s g_prealloc[CONFIG_USBHOST_NPREALLOC];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* This is a list of free, pre-allocated USB host storage class instances */
|
|
|
|
|
|
|
|
#if CONFIG_USBHOST_NPREALLOC > 0
|
2013-08-12 01:11:32 +02:00
|
|
|
static FAR struct usbhost_freestate_s *g_freelist;
|
2010-12-15 19:39:19 +01:00
|
|
|
#endif
|
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
/* This is a bitmap that is used to allocate device names /dev/sda-z. */
|
|
|
|
|
|
|
|
static uint32_t g_devinuse;
|
2010-12-14 17:35:10 +01:00
|
|
|
|
2010-12-15 04:12:09 +01:00
|
|
|
/****************************************************************************
|
2010-12-15 23:28:35 +01:00
|
|
|
* Private Functions
|
2010-12-15 04:12:09 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2010-12-16 02:29:51 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_takesem
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is just a wrapper to handle the annoying behavior of semaphore
|
|
|
|
* waits that return due to the receipt of a signal.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
static int usbhost_takesem(FAR sem_t *sem)
|
2010-12-16 02:29:51 +01:00
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
return nxsem_wait_uninterruptible(sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_forcetake
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is just another wrapper but this one continues even if the thread
|
|
|
|
* is canceled. This must be done in certain conditions where were must
|
|
|
|
* continue in order to clean-up resources.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_forcetake(FAR sem_t *sem)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = nxsem_wait_uninterruptible(sem);
|
|
|
|
|
|
|
|
/* The only expected error would -ECANCELED meaning that the
|
|
|
|
* parent thread has been canceled. We have to continue and
|
|
|
|
* terminate the poll in this case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(ret == OK || ret == -ECANCELED);
|
|
|
|
}
|
|
|
|
while (ret < 0);
|
2010-12-16 02:29:51 +01:00
|
|
|
}
|
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_allocclass
|
|
|
|
*
|
|
|
|
* Description:
|
2010-12-15 23:28:35 +01:00
|
|
|
* This is really part of the logic that implements the create() method
|
2010-12-15 19:39:19 +01:00
|
|
|
* 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:
|
2010-12-15 19:39:19 +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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
#if CONFIG_USBHOST_NPREALLOC > 0
|
2011-01-09 03:15:30 +01:00
|
|
|
static inline FAR struct usbhost_state_s *usbhost_allocclass(void)
|
2010-12-15 19:39:19 +01:00
|
|
|
{
|
2013-08-12 01:11:32 +02:00
|
|
|
FAR struct usbhost_freestate_s *entry;
|
2010-12-15 23:28:35 +01:00
|
|
|
irqstate_t flags;
|
2010-12-15 19:39:19 +01:00
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
/* We may be executing from an interrupt handler so we need to take one of
|
|
|
|
* our pre-allocated class instances from the free list.
|
2010-12-15 19:39:19 +01:00
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2013-08-12 01:11:32 +02:00
|
|
|
entry = g_freelist;
|
|
|
|
if (entry)
|
2010-12-15 19:39:19 +01:00
|
|
|
{
|
2013-08-12 01:11:32 +02:00
|
|
|
g_freelist = entry->flink;
|
2010-12-15 19:39:19 +01:00
|
|
|
}
|
2013-08-12 01:11:32 +02:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2016-06-20 19:59:15 +02:00
|
|
|
uinfo("Allocated: %p\n", entry);
|
2013-08-12 01:11:32 +02:00
|
|
|
return (FAR struct usbhost_state_s *)entry;
|
2010-12-15 23:28:35 +01:00
|
|
|
}
|
2010-12-15 19:39:19 +01:00
|
|
|
#else
|
2011-01-09 03:15:30 +01:00
|
|
|
static inline FAR struct usbhost_state_s *usbhost_allocclass(void)
|
2010-12-15 23:28:35 +01:00
|
|
|
{
|
2011-01-09 03:15:30 +01:00
|
|
|
FAR struct usbhost_state_s *priv;
|
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
/* We are not executing from an interrupt handler so we can just call
|
2014-09-01 01:26:36 +02:00
|
|
|
* kmm_malloc() to get memory for the class instance.
|
2010-12-15 19:39:19 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(!up_interrupt_context());
|
2020-03-21 20:54:03 +01: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-09 03:15:30 +01:00
|
|
|
return priv;
|
2010-12-15 23:28:35 +01:00
|
|
|
}
|
2010-12-15 19:39:19 +01:00
|
|
|
#endif
|
2010-12-15 23:28:35 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
2010-12-15 23:28:35 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2010-12-15 23:28:35 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_USBHOST_NPREALLOC > 0
|
2015-01-11 15:05:09 +01:00
|
|
|
static inline void usbhost_freeclass(FAR struct usbhost_state_s *usbclass)
|
2010-12-15 23:28:35 +01:00
|
|
|
{
|
2020-03-21 20:54:03 +01:00
|
|
|
FAR struct usbhost_freestate_s *entry =
|
|
|
|
(FAR struct usbhost_freestate_s *)usbclass;
|
2010-12-15 23:28:35 +01:00
|
|
|
irqstate_t flags;
|
2013-08-12 01:11:32 +02:00
|
|
|
DEBUGASSERT(entry != NULL);
|
2010-12-15 23:28:35 +01:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
uinfo("Freeing: %p\n", entry);
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
/* Just put the pre-allocated class structure back on the freelist */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2013-08-12 01:11:32 +02:00
|
|
|
entry->flink = g_freelist;
|
|
|
|
g_freelist = entry;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2010-12-15 23:28:35 +01:00
|
|
|
}
|
|
|
|
#else
|
2015-01-11 15:05:09 +01:00
|
|
|
static inline void usbhost_freeclass(FAR struct usbhost_state_s *usbclass)
|
2010-12-15 23:28:35 +01:00
|
|
|
{
|
2015-01-11 15:05:09 +01:00
|
|
|
DEBUGASSERT(usbclass != NULL);
|
2010-12-15 23:28:35 +01:00
|
|
|
|
2020-04-07 16:31:47 +02:00
|
|
|
/* Free the class instance (calling kmm_free() in case we are executing
|
2010-12-15 23:28:35 +01:00
|
|
|
* from an interrupt handler.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Freeing: %p\n", usbclass);
|
2020-04-07 16:31:47 +02:00
|
|
|
kmm_free(usbclass);
|
2010-12-15 23:28:35 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: Device name management
|
|
|
|
*
|
|
|
|
* Description:
|
2020-03-21 20:54:03 +01:00
|
|
|
* Some tiny functions to coordinate management of mass storage device
|
|
|
|
* names.
|
2010-12-15 23:28:35 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
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();
|
2010-12-15 23:28:35 +01:00
|
|
|
for (devno = 0; devno < 26; devno++)
|
|
|
|
{
|
|
|
|
uint32_t bitno = 1 << devno;
|
|
|
|
if ((g_devinuse & bitno) == 0)
|
|
|
|
{
|
|
|
|
g_devinuse |= bitno;
|
|
|
|
priv->sdchar = 'a' + devno;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2010-12-15 23:28:35 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2010-12-15 23:28:35 +01:00
|
|
|
return -EMFILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usbhost_freedevno(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
2020-08-09 19:12:17 +02:00
|
|
|
int devno = priv->sdchar - 'a';
|
2010-12-15 23:28:35 +01:00
|
|
|
|
|
|
|
if (devno >= 0 && devno < 26)
|
|
|
|
{
|
2016-02-14 14:32:58 +01:00
|
|
|
irqstate_t flags = enter_critical_section();
|
2010-12-15 23:28:35 +01:00
|
|
|
g_devinuse &= ~(1 << devno);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2010-12-15 23:28:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
static inline void usbhost_mkdevname(FAR struct usbhost_state_s *priv,
|
|
|
|
FAR char *devname)
|
2010-12-15 23:28:35 +01:00
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->sdchar);
|
2010-12-15 19:39:19 +01:00
|
|
|
}
|
|
|
|
|
2011-01-01 17:56:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: CBW/CSW debug helpers
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The following functions are helper functions used to dump CBWs and CSWs.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* cbw/csw - A reference to the CBW/CSW to dump.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2011-01-01 17:56:59 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-11 19:50:18 +02:00
|
|
|
#if defined(CONFIG_DEBUG_USB) && defined(CONFIG_DEBUG_INFO)
|
2012-01-25 21:17:59 +01:00
|
|
|
static void usbhost_dumpcbw(FAR struct usbmsc_cbw_s *cbw)
|
2011-01-01 17:56:59 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("CBW:\n");
|
|
|
|
uinfo(" signature: %08x\n", usbhost_getle32(cbw->signature));
|
|
|
|
uinfo(" tag: %08x\n", usbhost_getle32(cbw->tag));
|
|
|
|
uinfo(" datlen: %08x\n", usbhost_getle32(cbw->datlen));
|
|
|
|
uinfo(" flags: %02x\n", cbw->flags);
|
|
|
|
uinfo(" lun: %02x\n", cbw->lun);
|
|
|
|
uinfo(" cdblen: %02x\n", cbw->cdblen);
|
2011-01-01 17:56:59 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("CDB:\n");
|
2011-01-01 17:56:59 +01:00
|
|
|
for (i = 0; i < cbw->cdblen; i += 8)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo(" %02x %02x %02x %02x %02x %02x %02x %02x\n",
|
2020-03-21 20:54:03 +01:00
|
|
|
cbw->cdb[i], cbw->cdb[i + 1], cbw->cdb[i + 2],
|
|
|
|
cbw->cdb[i + 3], cbw->cdb[i + 4], cbw->cdb[i + 5],
|
|
|
|
cbw->cdb[i + 6], cbw->cdb[i + 7]);
|
2011-01-01 17:56:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static void usbhost_dumpcsw(FAR struct usbmsc_csw_s *csw)
|
2011-01-01 17:56:59 +01:00
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("CSW:\n");
|
|
|
|
uinfo(" signature: %08x\n", usbhost_getle32(csw->signature));
|
|
|
|
uinfo(" tag: %08x\n", usbhost_getle32(csw->tag));
|
|
|
|
uinfo(" residue: %08x\n", usbhost_getle32(csw->residue));
|
|
|
|
uinfo(" status: %02x\n", csw->status);
|
2011-01-01 17:56:59 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-12-18 02:53:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: CBW helpers
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The following functions are helper functions used to format CBWs.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2011-01-01 17:56:59 +01:00
|
|
|
* cbw - A reference to allocated and initialized CBW to be built.
|
2010-12-18 02:53:05 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2010-12-18 02:53:05 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline void usbhost_requestsensecbw(FAR struct usbmsc_cbw_s *cbw)
|
2010-12-18 02:53:05 +01:00
|
|
|
{
|
|
|
|
FAR struct scsicmd_requestsense_s *reqsense;
|
|
|
|
|
|
|
|
/* Format the CBW */
|
|
|
|
|
|
|
|
usbhost_putle32(cbw->datlen, SCSIRESP_FIXEDSENSEDATA_SIZEOF);
|
2012-01-25 21:17:59 +01:00
|
|
|
cbw->flags = USBMSC_CBWFLAG_IN;
|
2010-12-18 02:53:05 +01:00
|
|
|
cbw->cdblen = SCSICMD_REQUESTSENSE_SIZEOF;
|
|
|
|
|
|
|
|
/* Format the CDB */
|
|
|
|
|
|
|
|
reqsense = (FAR struct scsicmd_requestsense_s *)cbw->cdb;
|
|
|
|
reqsense->opcode = SCSI_CMD_REQUESTSENSE;
|
|
|
|
reqsense->alloclen = SCSIRESP_FIXEDSENSEDATA_SIZEOF;
|
2011-01-01 17:56:59 +01:00
|
|
|
|
|
|
|
usbhost_dumpcbw(cbw);
|
2010-12-18 02:53:05 +01:00
|
|
|
}
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline void usbhost_testunitreadycbw(FAR struct usbmsc_cbw_s *cbw)
|
2010-12-18 02:53:05 +01:00
|
|
|
{
|
|
|
|
/* Format the CBW */
|
|
|
|
|
|
|
|
cbw->cdblen = SCSICMD_TESTUNITREADY_SIZEOF;
|
|
|
|
|
|
|
|
/* Format the CDB */
|
|
|
|
|
|
|
|
cbw->cdb[0] = SCSI_CMD_TESTUNITREADY;
|
2011-01-01 17:56:59 +01:00
|
|
|
|
|
|
|
usbhost_dumpcbw(cbw);
|
2010-12-18 02:53:05 +01:00
|
|
|
}
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline void usbhost_readcapacitycbw(FAR struct usbmsc_cbw_s *cbw)
|
2010-12-18 02:53:05 +01:00
|
|
|
{
|
|
|
|
FAR struct scsicmd_readcapacity10_s *rcap10;
|
|
|
|
|
|
|
|
/* Format the CBW */
|
|
|
|
|
|
|
|
usbhost_putle32(cbw->datlen, SCSIRESP_READCAPACITY10_SIZEOF);
|
2012-01-25 21:17:59 +01:00
|
|
|
cbw->flags = USBMSC_CBWFLAG_IN;
|
2010-12-18 02:53:05 +01:00
|
|
|
cbw->cdblen = SCSICMD_READCAPACITY10_SIZEOF;
|
|
|
|
|
|
|
|
/* Format the CDB */
|
|
|
|
|
|
|
|
rcap10 = (FAR struct scsicmd_readcapacity10_s *)cbw->cdb;
|
|
|
|
rcap10->opcode = SCSI_CMD_READCAPACITY10;
|
2011-01-01 17:56:59 +01:00
|
|
|
|
|
|
|
usbhost_dumpcbw(cbw);
|
2010-12-18 02:53:05 +01:00
|
|
|
}
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline void usbhost_inquirycbw (FAR struct usbmsc_cbw_s *cbw)
|
2010-12-18 02:53:05 +01:00
|
|
|
{
|
|
|
|
FAR struct scscicmd_inquiry_s *inq;
|
|
|
|
|
|
|
|
/* Format the CBW */
|
|
|
|
|
2011-01-01 17:56:59 +01:00
|
|
|
usbhost_putle32(cbw->datlen, SCSIRESP_INQUIRY_SIZEOF);
|
2012-01-25 21:17:59 +01:00
|
|
|
cbw->flags = USBMSC_CBWFLAG_IN;
|
2011-01-01 17:56:59 +01:00
|
|
|
cbw->cdblen = SCSICMD_INQUIRY_SIZEOF;
|
2010-12-18 02:53:05 +01:00
|
|
|
|
|
|
|
/* Format the CDB */
|
|
|
|
|
|
|
|
inq = (FAR struct scscicmd_inquiry_s *)cbw->cdb;
|
|
|
|
inq->opcode = SCSI_CMD_INQUIRY;
|
|
|
|
usbhost_putbe16(inq->alloclen, SCSIRESP_INQUIRY_SIZEOF);
|
2011-01-01 17:56:59 +01:00
|
|
|
|
|
|
|
usbhost_dumpcbw(cbw);
|
2010-12-18 02:53:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2021-02-17 08:58:11 +01:00
|
|
|
usbhost_readcbw (blkcnt_t startsector, uint16_t blocksize,
|
2012-01-25 21:17:59 +01:00
|
|
|
unsigned int nsectors, FAR struct usbmsc_cbw_s *cbw)
|
2010-12-18 02:53:05 +01:00
|
|
|
{
|
|
|
|
FAR struct scsicmd_read10_s *rd10;
|
|
|
|
|
|
|
|
/* Format the CBW */
|
|
|
|
|
|
|
|
usbhost_putle32(cbw->datlen, blocksize * nsectors);
|
2012-01-25 21:17:59 +01:00
|
|
|
cbw->flags = USBMSC_CBWFLAG_IN;
|
2010-12-18 02:53:05 +01:00
|
|
|
cbw->cdblen = SCSICMD_READ10_SIZEOF;
|
|
|
|
|
|
|
|
/* Format the CDB */
|
|
|
|
|
|
|
|
rd10 = (FAR struct scsicmd_read10_s *)cbw->cdb;
|
|
|
|
rd10->opcode = SCSI_CMD_READ10;
|
|
|
|
usbhost_putbe32(rd10->lba, startsector);
|
|
|
|
usbhost_putbe16(rd10->xfrlen, nsectors);
|
2011-01-01 17:56:59 +01:00
|
|
|
|
|
|
|
usbhost_dumpcbw(cbw);
|
2010-12-18 02:53:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2021-02-17 08:58:11 +01:00
|
|
|
usbhost_writecbw(blkcnt_t startsector, uint16_t blocksize,
|
2012-01-25 21:17:59 +01:00
|
|
|
unsigned int nsectors, FAR struct usbmsc_cbw_s *cbw)
|
2010-12-18 02:53:05 +01:00
|
|
|
{
|
|
|
|
FAR struct scsicmd_write10_s *wr10;
|
|
|
|
|
|
|
|
/* Format the CBW */
|
|
|
|
|
|
|
|
usbhost_putle32(cbw->datlen, blocksize * nsectors);
|
|
|
|
cbw->cdblen = SCSICMD_WRITE10_SIZEOF;
|
|
|
|
|
|
|
|
/* Format the CDB */
|
|
|
|
|
|
|
|
wr10 = (FAR struct scsicmd_write10_s *)cbw->cdb;
|
|
|
|
wr10->opcode = SCSI_CMD_WRITE10;
|
|
|
|
usbhost_putbe32(wr10->lba, startsector);
|
|
|
|
usbhost_putbe16(wr10->xfrlen, nsectors);
|
2011-01-01 17:56:59 +01:00
|
|
|
|
|
|
|
usbhost_dumpcbw(cbw);
|
2010-12-18 02:53:05 +01:00
|
|
|
}
|
|
|
|
|
2010-12-18 04:24:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: Command helpers
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The following functions are helper functions used to send commands.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to the class instance.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2010-12-18 04:24:13 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
static inline int usbhost_maxlunreq(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
2011-01-16 20:08:16 +01:00
|
|
|
FAR struct usb_ctrlreq_s *req = (FAR struct usb_ctrlreq_s *)priv->tbuffer;
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2011-01-16 20:08:16 +01:00
|
|
|
DEBUGASSERT(priv && priv->tbuffer);
|
2011-07-01 15:46:34 +02:00
|
|
|
int ret;
|
2010-12-18 18:35:54 +01:00
|
|
|
|
2011-01-09 03:42:02 +01:00
|
|
|
/* Request maximum logical unit number. NOTE: On an IN transaction, The
|
|
|
|
* req and buffer pointers passed to DRVR_CTRLIN may refer to the same
|
|
|
|
* allocated memory.
|
|
|
|
*/
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Request maximum logical unit number\n");
|
2015-04-20 20:06:47 +02:00
|
|
|
|
2011-01-09 03:42:02 +01:00
|
|
|
memset(req, 0, sizeof(struct usb_ctrlreq_s));
|
2020-03-21 20:54:03 +01:00
|
|
|
req->type = USB_DIR_IN | USB_REQ_TYPE_CLASS |
|
|
|
|
USB_REQ_RECIPIENT_INTERFACE;
|
2012-01-25 21:17:59 +01:00
|
|
|
req->req = USBMSC_REQ_GETMAXLUN;
|
2011-01-09 03:42:02 +01:00
|
|
|
usbhost_putle16(req->len, 1);
|
2011-07-01 15:46:34 +02:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv->usbclass.hport);
|
|
|
|
hport = priv->usbclass.hport;
|
2015-04-20 20:06:47 +02:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
ret = DRVR_CTRLIN(hport->drvr, hport->ep0, req, priv->tbuffer);
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret < 0)
|
2011-07-01 15:46:34 +02:00
|
|
|
{
|
|
|
|
/* Devices that do not support multiple LUNs may stall this command.
|
|
|
|
* On a failure, a single LUN is assumed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
*(priv->tbuffer) = 0;
|
|
|
|
}
|
2013-09-08 21:42:56 +02:00
|
|
|
|
2011-07-01 15:46:34 +02:00
|
|
|
return OK;
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int usbhost_testunitready(FAR struct usbhost_state_s *priv)
|
2010-12-18 04:24:13 +01:00
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_cbw_s *cbw;
|
2015-05-05 21:14:22 +02:00
|
|
|
ssize_t nbytes;
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv->usbclass.hport);
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
|
2010-12-21 16:57:40 +01:00
|
|
|
/* Initialize a CBW (re-using the allocated transfer buffer) */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2010-12-18 04:24:13 +01:00
|
|
|
cbw = usbhost_cbwalloc(priv);
|
2011-01-09 03:15:30 +01:00
|
|
|
if (!cbw)
|
2010-12-18 04:24:13 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to create CBW\n");
|
2011-01-09 03:15:30 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
/* Construct and send the CBW */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
usbhost_testunitreadycbw(cbw);
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkout,
|
2015-10-10 18:41:00 +02:00
|
|
|
(FAR uint8_t *)cbw, USBMSC_CBW_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2011-01-09 03:15:30 +01:00
|
|
|
{
|
|
|
|
/* Receive the CSW */
|
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkin,
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->tbuffer, USBMSC_CSW_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2010-12-18 04:24:13 +01:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbhost_dumpcsw((FAR struct usbmsc_csw_s *)priv->tbuffer);
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
|
|
|
}
|
2012-08-29 00:28:49 +02:00
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
return nbytes < 0 ? (int)nbytes : OK;
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int usbhost_requestsense(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_cbw_s *cbw;
|
2015-05-05 21:14:22 +02:00
|
|
|
ssize_t nbytes;
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv->usbclass.hport);
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
|
2010-12-21 16:57:40 +01:00
|
|
|
/* Initialize a CBW (re-using the allocated transfer buffer) */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
cbw = usbhost_cbwalloc(priv);
|
2011-01-09 03:15:30 +01:00
|
|
|
if (!cbw)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to create CBW\n");
|
2011-01-09 03:15:30 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Construct and send the CBW */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
usbhost_requestsensecbw(cbw);
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkout,
|
2015-10-10 18:41:00 +02:00
|
|
|
(FAR uint8_t *)cbw, USBMSC_CBW_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2011-01-09 03:15:30 +01:00
|
|
|
{
|
|
|
|
/* Receive the sense data response */
|
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkin,
|
2011-01-16 20:08:16 +01:00
|
|
|
priv->tbuffer, SCSIRESP_FIXEDSENSEDATA_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2011-01-09 03:15:30 +01:00
|
|
|
/* Receive the CSW */
|
2010-12-18 18:35:54 +01:00
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkin,
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->tbuffer, USBMSC_CSW_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbhost_dumpcsw((FAR struct usbmsc_csw_s *)priv->tbuffer);
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
return nbytes < 0 ? (int)nbytes : OK;
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int usbhost_readcapacity(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_cbw_s *cbw;
|
2010-12-18 18:35:54 +01:00
|
|
|
FAR struct scsiresp_readcapacity10_s *resp;
|
2015-05-05 21:14:22 +02:00
|
|
|
ssize_t nbytes;
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv->usbclass.hport);
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
|
2010-12-21 16:57:40 +01:00
|
|
|
/* Initialize a CBW (re-using the allocated transfer buffer) */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
cbw = usbhost_cbwalloc(priv);
|
2011-01-09 03:15:30 +01:00
|
|
|
if (!cbw)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to create CBW\n");
|
2011-01-09 03:15:30 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Construct and send the CBW */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
usbhost_readcapacitycbw(cbw);
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkout,
|
2015-10-10 18:41:00 +02:00
|
|
|
(FAR uint8_t *)cbw, USBMSC_CBW_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2011-01-09 03:15:30 +01:00
|
|
|
{
|
|
|
|
/* Receive the read capacity CBW IN response */
|
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkin,
|
2011-01-16 20:08:16 +01:00
|
|
|
priv->tbuffer, SCSIRESP_READCAPACITY10_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2011-01-09 03:15:30 +01:00
|
|
|
/* Save the capacity information */
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
resp = (FAR struct scsiresp_readcapacity10_s *)
|
|
|
|
priv->tbuffer;
|
2011-07-01 15:46:34 +02:00
|
|
|
priv->nblocks = usbhost_getbe32(resp->lba) + 1;
|
2011-01-09 03:15:30 +01:00
|
|
|
priv->blocksize = usbhost_getbe32(resp->blklen);
|
|
|
|
|
|
|
|
/* Receive the CSW */
|
2010-12-18 18:35:54 +01:00
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkin,
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->tbuffer, USBMSC_CSW_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbhost_dumpcsw((FAR struct usbmsc_csw_s *)priv->tbuffer);
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
return nbytes < 0 ? (int)nbytes : OK;
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int usbhost_inquiry(FAR struct usbhost_state_s *priv)
|
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_cbw_s *cbw;
|
2015-05-05 21:14:22 +02:00
|
|
|
ssize_t nbytes;
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv->usbclass.hport);
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
|
2010-12-21 16:57:40 +01:00
|
|
|
/* Initialize a CBW (re-using the allocated transfer buffer) */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
cbw = usbhost_cbwalloc(priv);
|
2011-01-09 03:15:30 +01:00
|
|
|
if (!cbw)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to create CBW\n");
|
2011-01-09 03:15:30 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Construct and send the CBW */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
usbhost_inquirycbw(cbw);
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkout,
|
2015-10-10 18:41:00 +02:00
|
|
|
(FAR uint8_t *)cbw, USBMSC_CBW_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2011-01-09 03:15:30 +01:00
|
|
|
{
|
|
|
|
/* Receive the CBW IN response */
|
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkin,
|
2011-01-16 20:08:16 +01:00
|
|
|
priv->tbuffer, SCSIRESP_INQUIRY_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2013-08-12 01:11:32 +02:00
|
|
|
#if 0
|
|
|
|
FAR struct scsiresp_inquiry_s *resp;
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2013-08-12 01:11:32 +02:00
|
|
|
/* TODO: If USB debug is enabled, dump the response data here */
|
2015-05-05 21:14:22 +02:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
resp = (FAR struct scsiresp_inquiry_s *)priv->tbuffer;
|
2013-08-12 01:11:32 +02:00
|
|
|
#endif
|
2011-01-09 03:15:30 +01:00
|
|
|
/* Receive the CSW */
|
2010-12-18 18:35:54 +01:00
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkin,
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->tbuffer, USBMSC_CSW_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbhost_dumpcsw((FAR struct usbmsc_csw_s *)priv->tbuffer);
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
2010-12-18 04:24:13 +01:00
|
|
|
}
|
|
|
|
}
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
return nbytes < 0 ? (int)nbytes : OK;
|
2010-12-18 04:24:13 +01:00
|
|
|
}
|
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_destroy
|
|
|
|
*
|
|
|
|
* Description:
|
2020-03-21 20:54:03 +01:00
|
|
|
* The USB mass storage device has been disconnected and the reference
|
|
|
|
* count on the USB host class instance has gone to 1.. Time to destroy
|
|
|
|
* the USB host class instance.
|
2010-12-15 23:28:35 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2010-12-17 03:19:04 +01:00
|
|
|
* arg - A reference to the class instance to be destroyed.
|
2010-12-15 23:28:35 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2010-12-15 23:28:35 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_destroy(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv = (FAR struct usbhost_state_s *)arg;
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2010-12-15 23:28:35 +01:00
|
|
|
char devname[DEV_NAMELEN];
|
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL);
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("crefs: %d\n", priv->crefs);
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
/* Unregister the block driver */
|
|
|
|
|
|
|
|
usbhost_mkdevname(priv, devname);
|
2020-01-02 17:49:34 +01:00
|
|
|
unregister_blockdriver(devname);
|
2010-12-15 23:28:35 +01:00
|
|
|
|
|
|
|
/* Release the device name used by this connection */
|
|
|
|
|
|
|
|
usbhost_freedevno(priv);
|
|
|
|
|
2011-01-11 02:41:06 +01:00
|
|
|
/* Free the bulk endpoints */
|
|
|
|
|
|
|
|
if (priv->bulkout)
|
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
DRVR_EPFREE(hport->drvr, priv->bulkout);
|
2011-01-11 02:41:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->bulkin)
|
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
DRVR_EPFREE(hport->drvr, priv->bulkin);
|
2011-01-11 02:41:06 +01:00
|
|
|
}
|
|
|
|
|
2010-12-18 04:24:13 +01:00
|
|
|
/* Free any transfer buffers */
|
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
usbhost_tfree(priv);
|
2010-12-18 04:24:13 +01:00
|
|
|
|
|
|
|
/* Destroy the semaphores */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&priv->exclsem);
|
2010-12-18 04:24:13 +01:00
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
/* Disconnect the USB host device */
|
|
|
|
|
2015-05-02 18:44:18 +02:00
|
|
|
DRVR_DISCONNECT(hport->drvr, hport);
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2015-04-26 18:05:15 +02:00
|
|
|
/* Free the function address assigned to this device */
|
|
|
|
|
|
|
|
usbhost_devaddr_destroy(hport, hport->funcaddr);
|
|
|
|
hport->funcaddr = 0;
|
|
|
|
|
|
|
|
/* And free the class instance. */
|
2010-12-15 23:28:35 +01:00
|
|
|
|
|
|
|
usbhost_freeclass(priv);
|
|
|
|
}
|
|
|
|
|
2011-01-11 02:41:06 +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.
|
2015-01-11 15:05:09 +01:00
|
|
|
* configdesc - A pointer to a uint8_t buffer container the configuration
|
|
|
|
* descriptor.
|
2011-01-11 02:41:06 +01:00
|
|
|
* desclen - The length in bytes of the configuration descriptor.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-03-21 20:54:03 +01:00
|
|
|
* On success, zero (OK) is returned. On a failure, a negated errno value
|
|
|
|
* is returned indicating the nature of the failure
|
2011-01-11 02:41:06 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function will *not* be called from an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int usbhost_cfgdesc(FAR struct usbhost_state_s *priv,
|
2015-04-21 00:21:13 +02:00
|
|
|
FAR const uint8_t *configdesc, int desclen)
|
2011-01-11 02:41:06 +01:00
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2011-01-11 02:41:06 +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 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport &&
|
2015-04-21 00:21:13 +02:00
|
|
|
configdesc != NULL && desclen >= sizeof(struct usb_cfgdesc_s));
|
2015-04-21 15:26:28 +02:00
|
|
|
hport = priv->usbclass.hport;
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2013-08-12 01:11:32 +02:00
|
|
|
/* Keep the compiler from complaining about uninitialized variables */
|
|
|
|
|
|
|
|
memset(&bindesc, 0, sizeof(struct usbhost_epdesc_s));
|
|
|
|
memset(&boutdesc, 0, sizeof(struct usbhost_epdesc_s));
|
|
|
|
|
2011-01-11 02:41:06 +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-11 02:41:06 +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-21 20:54:03 +01:00
|
|
|
FAR struct usb_ifdesc_s *ifdesc =
|
|
|
|
(FAR struct usb_ifdesc_s *)configdesc;
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Interface descriptor\n");
|
2011-01-11 02:41:06 +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-11 02:41:06 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-01-14 18:06:30 +01:00
|
|
|
/* Endpoint descriptor. We expect two bulk endpoints, an IN and an
|
|
|
|
* OUT.
|
|
|
|
*/
|
|
|
|
|
2011-01-11 02:41:06 +01:00
|
|
|
case USB_DESC_TYPE_ENDPOINT:
|
|
|
|
{
|
2020-03-21 20:54:03 +01: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-11 02:41:06 +01:00
|
|
|
DEBUGASSERT(remaining >= USB_SIZEOF_EPDESC);
|
|
|
|
|
|
|
|
/* Check for a bulk endpoint. We only support the bulk-only
|
|
|
|
* protocol so I suppose anything else should really be an error.
|
|
|
|
*/
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
if ((epdesc->attr & USB_EP_ATTR_XFERTYPE_MASK) ==
|
|
|
|
USB_EP_ATTR_XFER_BULK)
|
2011-01-11 02:41:06 +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-11 02:41:06 +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-11 02:41:06 +01:00
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-03-21 20:54:03 +01:00
|
|
|
|
2011-01-11 02:41:06 +01:00
|
|
|
found |= USBHOST_BOUTFOUND;
|
|
|
|
|
|
|
|
/* Save the bulk OUT endpoint information */
|
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
boutdesc.hport = hport;
|
2020-03-21 20:54:03 +01:00
|
|
|
boutdesc.addr = epdesc->addr &
|
|
|
|
USB_EP_ADDR_NUMBER_MASK;
|
2011-01-11 02:41:06 +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-21 20:54:03 +01:00
|
|
|
boutdesc.mxpacketsize =
|
|
|
|
usbhost_getle16(epdesc->mxpacketsize);
|
2015-04-21 15:26:28 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Bulk OUT EP addr:%d mxpacketsize:%d\n",
|
2011-01-11 02:41:06 +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-11 02:41:06 +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-11 02:41:06 +01:00
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-03-21 20:54:03 +01:00
|
|
|
|
2011-01-11 02:41:06 +01:00
|
|
|
found |= USBHOST_BINFOUND;
|
|
|
|
|
|
|
|
/* Save the bulk IN endpoint information */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
bindesc.hport = hport;
|
2020-03-21 20:54:03 +01:00
|
|
|
bindesc.addr = epdesc->addr &
|
|
|
|
USB_EP_ADDR_NUMBER_MASK;
|
2011-01-11 02:41:06 +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-21 20:54:03 +01: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-11 02:41:06 +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-11 02:41:06 +01:00
|
|
|
/* Increment the address of the next descriptor */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2011-01-11 02:41:06 +01:00
|
|
|
configdesc += desc->len;
|
|
|
|
remaining -= desc->len;
|
|
|
|
}
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
/* Sanity checking... did we find all of things that we need? Hmmm..
|
|
|
|
* I wonder.. can we work read-only or write-only if only one bulk
|
|
|
|
* endpoint found?
|
2011-01-11 02:41:06 +01:00
|
|
|
*/
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2011-01-11 02:41:06 +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-11 02:41:06 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are good... Allocate the endpoints */
|
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
ret = DRVR_EPALLOC(hport->drvr, &boutdesc, &priv->bulkout);
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret < 0)
|
2011-01-11 02:41:06 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to allocate Bulk OUT endpoint\n");
|
2011-01-11 02:41:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
ret = DRVR_EPALLOC(hport->drvr, &bindesc, &priv->bulkin);
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret < 0)
|
2011-01-11 02:41:06 +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->bulkout);
|
2011-01-11 02:41:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
uinfo("Endpoints allocated\n");
|
2011-01-11 02:41:06 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2010-12-16 02:29:51 +01:00
|
|
|
/****************************************************************************
|
2010-12-21 12:21:07 +01:00
|
|
|
* Name: usbhost_initvolume
|
2010-12-16 02:29:51 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2010-12-21 12:21:07 +01:00
|
|
|
* The USB mass storage device has been successfully connected. This
|
|
|
|
* completes the initialization operations. It is first called after the
|
|
|
|
* configuration descriptor has been received.
|
2010-12-16 18:11:05 +01:00
|
|
|
*
|
2011-01-12 03:12:41 +01:00
|
|
|
* This function is called from the connect() method. This function always
|
|
|
|
* executes on the thread of the caller of connect().
|
2010-12-16 02:29:51 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2011-01-11 02:41:06 +01:00
|
|
|
* priv - A reference to the class instance.
|
2010-12-16 02:29:51 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2010-12-16 02:29:51 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-01-11 02:41:06 +01:00
|
|
|
static inline int usbhost_initvolume(FAR struct usbhost_state_s *priv)
|
2010-12-16 02:29:51 +01:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_csw_s *csw;
|
2010-12-18 18:35:54 +01:00
|
|
|
unsigned int retries;
|
2011-01-11 02:41:06 +01:00
|
|
|
int ret = OK;
|
2010-12-16 18:11:05 +01:00
|
|
|
|
2010-12-16 04:45:09 +01:00
|
|
|
DEBUGASSERT(priv != NULL);
|
2010-12-16 02:29:51 +01:00
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
/* Set aside a transfer buffer for exclusive
|
|
|
|
* use by the mass storage driver
|
|
|
|
*/
|
2010-12-21 16:57:40 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
ret = usbhost_talloc(priv);
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret < 0)
|
2010-12-21 16:57:40 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to allocate transfer buffer\n");
|
2011-01-11 02:41:06 +01:00
|
|
|
return ret;
|
2010-12-21 16:57:40 +01:00
|
|
|
}
|
|
|
|
|
2011-01-09 03:15:30 +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);
|
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
/* Request the maximum logical unit number */
|
2010-12-18 04:24:13 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Get max LUN\n");
|
2011-01-11 02:41:06 +01:00
|
|
|
ret = usbhost_maxlunreq(priv);
|
2010-12-18 04:24:13 +01:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
for (retries = 0; retries < USBHOST_MAX_RETRIES; retries++)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Test unit ready, retries=%d\n", retries);
|
2010-12-18 04:24:13 +01:00
|
|
|
|
2012-08-29 00:28:49 +02:00
|
|
|
/* Wait just a bit */
|
|
|
|
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(USBHOST_RETRY_USEC);
|
2012-08-29 00:28:49 +02:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
/* Send TESTUNITREADY to see if the unit is ready. The most likely
|
|
|
|
* error error that can occur here is a a stall which simply means
|
|
|
|
* that the the device is not yet able to respond.
|
2013-09-08 21:42:56 +02:00
|
|
|
*/
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2011-01-11 02:41:06 +01:00
|
|
|
ret = usbhost_testunitready(priv);
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
|
|
|
/* Is the unit is ready */
|
2010-12-16 02:29:51 +01:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
csw = (FAR struct usbmsc_csw_s *)priv->tbuffer;
|
2010-12-18 18:35:54 +01:00
|
|
|
if (csw->status == 0)
|
|
|
|
{
|
|
|
|
/* Yes... break out of the loop */
|
2010-12-16 02:29:51 +01:00
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-12-18 04:24:13 +01:00
|
|
|
|
2011-01-04 04:04:06 +01:00
|
|
|
/* No.. Request mode sense information. The REQUEST SENSE command
|
|
|
|
* is sent only "to clear interlocked unit attention conditions."
|
|
|
|
* The returned status is ignored here.
|
|
|
|
*/
|
2010-12-16 02:29:51 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Request sense\n");
|
2011-01-11 02:41:06 +01:00
|
|
|
ret = usbhost_requestsense(priv);
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
2013-09-09 18:00:16 +02:00
|
|
|
|
|
|
|
/* It is acceptable for a mass storage device to respond to the
|
|
|
|
* Test Unit Ready and Request Sense commands with a stall if it is
|
|
|
|
* unable to respond. But other failures mean that something is
|
|
|
|
* wrong and a device reset is in order. The transfer functions will
|
|
|
|
* return -EPERM if the transfer failed due to a stall.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret < 0 && ret != -EPERM)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: DRVR_TRANSFER returned: %d\n", ret);
|
2013-09-09 18:00:16 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
2010-12-18 04:24:13 +01:00
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
/* Did the unit become ready? Did an error occur? Or did we time out? */
|
2010-12-18 04:24:13 +01:00
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
if (retries >= USBHOST_MAX_RETRIES)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Timeout!\n");
|
2011-01-11 02:41:06 +01:00
|
|
|
ret = -ETIMEDOUT;
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
2010-12-16 04:45:09 +01:00
|
|
|
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
|
|
|
/* Get the capacity of the volume */
|
2010-12-18 04:24:13 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Read capacity\n");
|
2011-01-11 02:41:06 +01:00
|
|
|
ret = usbhost_readcapacity(priv);
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
|
|
|
/* Check the CSW for errors */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
csw = (FAR struct usbmsc_csw_s *)priv->tbuffer;
|
2010-12-18 18:35:54 +01:00
|
|
|
if (csw->status != 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: CSW status error: %d\n", csw->status);
|
2011-01-11 02:41:06 +01:00
|
|
|
ret = -ENODEV;
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-16 04:45:09 +01:00
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
/* Get information about the volume */
|
2010-12-18 04:24:13 +01:00
|
|
|
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
|
|
|
/* Inquiry */
|
2010-12-18 04:24:13 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Inquiry\n");
|
2011-01-11 02:41:06 +01:00
|
|
|
ret = usbhost_inquiry(priv);
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
|
|
|
/* Check the CSW for errors */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
csw = (FAR struct usbmsc_csw_s *)priv->tbuffer;
|
2010-12-18 18:35:54 +01:00
|
|
|
if (csw->status != 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: CSW status error: %d\n", csw->status);
|
2011-01-11 02:41:06 +01:00
|
|
|
ret = -ENODEV;
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-18 04:24:13 +01:00
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
/* Register the block driver */
|
2010-12-16 04:45:09 +01:00
|
|
|
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
|
|
|
char devname[DEV_NAMELEN];
|
2010-12-16 18:11:05 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Register block driver\n");
|
2010-12-18 18:35:54 +01:00
|
|
|
usbhost_mkdevname(priv, devname);
|
2011-01-11 02:41:06 +01:00
|
|
|
ret = register_blockdriver(devname, &g_bops, 0, priv);
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
2010-12-16 02:29:51 +01:00
|
|
|
|
2013-09-10 19:38:40 +02:00
|
|
|
/* Decrement the reference count. We incremented the reference count
|
|
|
|
* above so that usbhost_destroy() could not be called. 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-09 03:15:30 +01:00
|
|
|
*/
|
2010-12-16 04:45:09 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
usbhost_forcetake(&priv->exclsem);
|
2013-09-10 19:38:40 +02:00
|
|
|
DEBUGASSERT(priv->crefs >= 2);
|
2011-05-13 05:33:03 +02:00
|
|
|
|
2013-09-10 19:38:40 +02:00
|
|
|
/* Decrement the reference count */
|
2011-05-13 05:33:03 +02:00
|
|
|
|
2013-09-10 19:38:40 +02:00
|
|
|
priv->crefs--;
|
2011-05-13 05:33:03 +02:00
|
|
|
|
2013-09-10 19:38:40 +02:00
|
|
|
/* Check if we successfully initialized. If so, handle a corner case
|
|
|
|
* where (1) open() has been called so the reference count was > 2, but
|
|
|
|
* the device has been disconnected. In this case, the class instance
|
|
|
|
* needs to persist until close()
|
|
|
|
* is called.
|
|
|
|
*/
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret >= 0 && priv->crefs <= 1 && priv->disconnected)
|
2013-09-10 19:38:40 +02:00
|
|
|
{
|
|
|
|
/* The will cause the enumeration logic to disconnect the class
|
|
|
|
* driver.
|
2011-05-13 05:33:03 +02:00
|
|
|
*/
|
|
|
|
|
2013-09-10 19:38:40 +02:00
|
|
|
ret = -ENODEV;
|
2010-12-16 04:45:09 +01:00
|
|
|
}
|
2020-08-06 13:30:01 +02:00
|
|
|
# ifdef CONFIG_USBHOST_MSC_NOTIFIER
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Signal the connect */
|
|
|
|
|
|
|
|
usbhost_msc_notifier_signal(WORK_USB_MSC_CONNECT, priv->sdchar);
|
|
|
|
}
|
|
|
|
# endif
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2013-09-10 19:38:40 +02:00
|
|
|
/* Release the semaphore... there is a race condition here.
|
|
|
|
* Decrementing the reference count and releasing the semaphore
|
|
|
|
* allows usbhost_destroy() to execute (on the worker thread);
|
|
|
|
* the class driver instance could get destroyed before we are
|
|
|
|
* ready to handle it!
|
|
|
|
*/
|
|
|
|
|
|
|
|
usbhost_givesem(&priv->exclsem);
|
2020-08-06 13:30:01 +02:00
|
|
|
|
2011-01-11 02:41:06 +01:00
|
|
|
return ret;
|
2010-12-16 02:29:51 +01:00
|
|
|
}
|
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
/****************************************************************************
|
2010-12-17 03:19:04 +01:00
|
|
|
* Name: usbhost_getle16
|
2010-12-15 23:28:35 +01:00
|
|
|
*
|
|
|
|
* 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:
|
2010-12-21 12:21:07 +01:00
|
|
|
* A uint16_t representing the whole 16-bit integer value
|
2010-12-15 23:28:35 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-12-17 03:19:04 +01:00
|
|
|
static inline uint16_t usbhost_getle16(const uint8_t *val)
|
2010-12-15 23:28:35 +01:00
|
|
|
{
|
2010-12-16 02:29:51 +01:00
|
|
|
return (uint16_t)val[1] << 8 | (uint16_t)val[0];
|
2010-12-15 23:28:35 +01:00
|
|
|
}
|
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_getbe16
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get a (possibly unaligned) 16-bit big endian value.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* val - A pointer to the first byte of the big endian value.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2010-12-21 12:21:07 +01:00
|
|
|
* A uint16_t representing the whole 16-bit integer value
|
2010-12-18 18:35:54 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline uint16_t usbhost_getbe16(const uint8_t *val)
|
|
|
|
{
|
|
|
|
return (uint16_t)val[0] << 8 | (uint16_t)val[1];
|
|
|
|
}
|
|
|
|
|
2010-12-17 03:19:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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:
|
2010-12-17 03:19:04 +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;
|
|
|
|
}
|
|
|
|
|
2010-12-18 02:53:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_putbe16
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Put a (possibly unaligned) 16-bit big endian value.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dest - A pointer to the first byte to save the big endian value.
|
|
|
|
* val - The 16-bit value to be saved.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2010-12-18 02:53:05 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_putbe16(uint8_t *dest, uint16_t val)
|
|
|
|
{
|
|
|
|
dest[0] = val >> 8; /* Big endian means MS byte first in byte stream */
|
|
|
|
dest[1] = val & 0xff;
|
|
|
|
}
|
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
/****************************************************************************
|
2011-01-12 03:12:41 +01:00
|
|
|
* Name: usbhost_getle32
|
2010-12-18 18:35:54 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2011-01-01 17:56:59 +01:00
|
|
|
* 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-01 17:56:59 +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-01 17:56:59 +01:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
return (uint32_t)usbhost_getle16(&val[2]) << 16 |
|
|
|
|
(uint32_t)usbhost_getle16(val);
|
2011-01-01 17:56:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_getbe32
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get a (possibly unaligned) 32-bit big endian value.
|
2010-12-18 18:35:54 +01:00
|
|
|
*
|
|
|
|
* 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:
|
2010-12-18 18:35:54 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline uint32_t usbhost_getbe32(const uint8_t *val)
|
|
|
|
{
|
|
|
|
/* Big endian means MS halfword first in byte stream */
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
return (uint32_t)usbhost_getbe16(val) << 16 |
|
|
|
|
(uint32_t)usbhost_getbe16(&val[2]);
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
|
|
|
|
2010-12-17 03:19:04 +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:
|
2010-12-17 03:19:04 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_putle32(uint8_t *dest, uint32_t val)
|
|
|
|
{
|
2010-12-18 18:35:54 +01:00
|
|
|
/* Little endian means LS halfword first in byte stream */
|
2010-12-17 03:19:04 +01:00
|
|
|
|
|
|
|
usbhost_putle16(dest, (uint16_t)(val & 0xffff));
|
2020-03-21 20:54:03 +01:00
|
|
|
usbhost_putle16(dest + 2, (uint16_t)(val >> 16));
|
2010-12-17 03:19:04 +01:00
|
|
|
}
|
|
|
|
|
2010-12-18 02:53:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_putbe32
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Put a (possibly unaligned) 32-bit big 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:
|
2010-12-18 02:53:05 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_putbe32(uint8_t *dest, uint32_t val)
|
|
|
|
{
|
2010-12-18 18:35:54 +01:00
|
|
|
/* Big endian means MS halfword first in byte stream */
|
2010-12-18 02:53:05 +01:00
|
|
|
|
|
|
|
usbhost_putbe16(dest, (uint16_t)(val >> 16));
|
2020-03-21 20:54:03 +01:00
|
|
|
usbhost_putbe16(dest + 2, (uint16_t)(val & 0xffff));
|
2010-12-18 02:53:05 +01:00
|
|
|
}
|
|
|
|
|
2010-12-17 03:19:04 +01:00
|
|
|
/****************************************************************************
|
2011-01-16 20:08:16 +01:00
|
|
|
* Name: usbhost_talloc
|
2010-12-17 03:19:04 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2011-01-16 20:08:16 +01:00
|
|
|
* Allocate transfer buffer memory.
|
2010-12-17 03:19:04 +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
|
2010-12-17 03:19:04 +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)
|
2010-12-17 03:19:04 +01:00
|
|
|
{
|
2015-04-21 15:26:28 +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);
|
2010-12-17 03:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-01-16 20:08:16 +01:00
|
|
|
* Name: usbhost_tfree
|
2010-12-17 03:19:04 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2011-01-16 20:08:16 +01:00
|
|
|
* Free transfer buffer memory.
|
2010-12-17 03:19:04 +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
|
2010-12-17 03:19:04 +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)
|
2010-12-17 03:19:04 +01:00
|
|
|
{
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2011-01-09 03:15:30 +01:00
|
|
|
int result = OK;
|
2015-04-21 15:26:28 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL);
|
2010-12-17 03:19:04 +01:00
|
|
|
|
2011-01-16 20:08:16 +01:00
|
|
|
if (priv->tbuffer)
|
2011-01-09 03:15:30 +01:00
|
|
|
{
|
2015-04-21 15:26:28 +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-09 03:15:30 +01:00
|
|
|
}
|
2013-09-09 18:00:16 +02:00
|
|
|
|
2010-12-17 03:19:04 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_cbwalloc
|
|
|
|
*
|
|
|
|
* Description:
|
2010-12-21 16:57:40 +01:00
|
|
|
* Initialize a CBW (re-using the allocated transfer buffer). Upon
|
2020-03-21 20:54:03 +01:00
|
|
|
* successful return, the CBW is cleared and has the CBW signature in
|
|
|
|
* place.
|
2010-12-17 03:19:04 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to the class instance.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2010-12-17 03:19:04 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
static FAR struct usbmsc_cbw_s *
|
|
|
|
usbhost_cbwalloc(FAR struct usbhost_state_s *priv)
|
2010-12-17 03:19:04 +01:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_cbw_s *cbw = NULL;
|
2010-12-17 03:19:04 +01:00
|
|
|
|
2015-04-27 20:26:40 +02:00
|
|
|
DEBUGASSERT(priv->tbuffer && priv->tbuflen >= sizeof(struct usbmsc_cbw_s));
|
2010-12-17 03:19:04 +01:00
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Initialize the CBW structure */
|
2010-12-17 03:19:04 +01:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
cbw = (FAR struct usbmsc_cbw_s *)priv->tbuffer;
|
|
|
|
memset(cbw, 0, sizeof(struct usbmsc_cbw_s));
|
|
|
|
usbhost_putle32(cbw->signature, USBMSC_CBW_SIGNATURE);
|
2010-12-17 03:19:04 +01:00
|
|
|
return cbw;
|
|
|
|
}
|
|
|
|
|
2010-12-14 17:35:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_create
|
|
|
|
*
|
|
|
|
* Description:
|
2020-03-21 20:54:03 +01: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.
|
2010-12-14 17:35:10 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2015-04-21 15:26:28 +02:00
|
|
|
* hport - The hub port that manages the new class instance.
|
2010-12-14 20:02:49 +01:00
|
|
|
* id - In the case where the device supports multiple base classes,
|
|
|
|
* subclasses, or protocols, this specifies which to configure for.
|
2010-12-14 17:35:10 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2010-12-14 17:35:10 +01:00
|
|
|
* On success, this function will return a non-NULL instance of struct
|
|
|
|
* usbhost_class_s that can be used by the USB host driver to communicate
|
|
|
|
* with the USB host class. NULL is returned on failure; this function
|
2015-04-21 15:26:28 +02:00
|
|
|
* will fail only if the hport input parameter is NULL or if there are
|
2010-12-14 17:35:10 +01:00
|
|
|
* insufficient resources to create another USB host class instance.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
static FAR struct usbhost_class_s *
|
2020-03-21 20:54:03 +01:00
|
|
|
usbhost_create(FAR struct usbhost_hubport_s *hport,
|
|
|
|
FAR const struct usbhost_id_s *id)
|
2010-12-14 17:35:10 +01:00
|
|
|
{
|
2010-12-15 19:39:19 +01:00
|
|
|
FAR struct usbhost_state_s *priv;
|
2010-12-15 04:30:20 +01:00
|
|
|
|
|
|
|
/* Allocate a USB host mass storage class instance */
|
|
|
|
|
2010-12-16 02:29:51 +01:00
|
|
|
priv = usbhost_allocclass();
|
2010-12-15 04:30:20 +01:00
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
/* Initialize the allocated storage class instance */
|
|
|
|
|
2010-12-16 02:29:51 +01:00
|
|
|
memset(priv, 0, sizeof(struct usbhost_state_s));
|
2010-12-15 04:30:20 +01:00
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
/* Assign a device number to this class instance */
|
|
|
|
|
|
|
|
if (usbhost_allocdevno(priv) == OK)
|
|
|
|
{
|
2020-03-21 20:54:03 +01:00
|
|
|
/* Initialize class method function pointers */
|
2010-12-16 18:11:05 +01:00
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
priv->usbclass.hport = hport;
|
2015-01-11 15:05:09 +01:00
|
|
|
priv->usbclass.connect = usbhost_connect;
|
|
|
|
priv->usbclass.disconnected = usbhost_disconnected;
|
2010-12-16 18:11:05 +01:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
/* The initial reference count is 1... One reference is held by
|
|
|
|
* the driver.
|
|
|
|
*/
|
2010-12-16 18:11:05 +01:00
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
priv->crefs = 1;
|
2010-12-15 23:28:35 +01:00
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
/* Initialize semaphores
|
|
|
|
* (this works okay in the interrupt context)
|
|
|
|
*/
|
2010-12-16 18:11:05 +01:00
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&priv->exclsem, 0, 1);
|
2010-12-16 18:11:05 +01:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
/* NOTE: We do not yet know the geometry of the USB mass storage
|
|
|
|
* device.
|
|
|
|
*/
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
/* Return the instance of the USB mass storage class */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
return &priv->usbclass;
|
2010-12-15 23:28:35 +01:00
|
|
|
}
|
2010-12-15 04:30:20 +01:00
|
|
|
}
|
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
/* An error occurred. Free the allocation and return NULL on all failures */
|
2010-12-15 04:30:20 +01:00
|
|
|
|
2010-12-15 23:28:35 +01:00
|
|
|
if (priv)
|
|
|
|
{
|
2010-12-16 02:29:51 +01:00
|
|
|
usbhost_freeclass(priv);
|
2010-12-15 23:28:35 +01:00
|
|
|
}
|
2015-05-03 16:51:44 +02:00
|
|
|
|
2010-12-14 17:35:10 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-15 04:30:20 +01:00
|
|
|
/****************************************************************************
|
2010-12-21 12:21:07 +01:00
|
|
|
* Name: usbhost_connect
|
2010-12-15 04:30:20 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2010-12-21 12:21:07 +01:00
|
|
|
* This function implements the connect() method of struct
|
2010-12-15 04:30:20 +01:00
|
|
|
* 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.
|
2010-12-15 04:30:20 +01:00
|
|
|
* desclen - The length in bytes of the configuration descriptor.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-03-21 20:54:03 +01:00
|
|
|
* On success, zero (OK) is returned. On a failure, a negated errno value
|
|
|
|
* is returned indicating the nature of the failure
|
2010-12-15 04:30:20 +01:00
|
|
|
*
|
2020-03-21 20:54:03 +01: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-13 05:33:03 +02:00
|
|
|
* CLASS_DISCONNECTED to free up the class driver resources.
|
|
|
|
*
|
2010-12-21 12:21:07 +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
|
2010-12-21 12:21:07 +01:00
|
|
|
*
|
2010-12-15 04:30:20 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
static int usbhost_connect(FAR struct usbhost_class_s *usbclass,
|
2015-04-21 00:21:13 +02:00
|
|
|
FAR const uint8_t *configdesc, int desclen)
|
2010-12-15 04:30:20 +01:00
|
|
|
{
|
2015-01-11 15:05:09 +01:00
|
|
|
FAR struct usbhost_state_s *priv = (FAR struct usbhost_state_s *)usbclass;
|
2011-01-11 02:41:06 +01:00
|
|
|
int ret;
|
2010-12-15 23:28:35 +01:00
|
|
|
|
2012-08-25 00:16:09 +02:00
|
|
|
DEBUGASSERT(priv != NULL &&
|
2010-12-16 04:45:09 +01:00
|
|
|
configdesc != NULL &&
|
2010-12-18 18:35:54 +01:00
|
|
|
desclen >= sizeof(struct usb_cfgdesc_s));
|
2010-12-15 23:28:35 +01:00
|
|
|
|
2011-01-11 02:41:06 +01:00
|
|
|
/* Parse the configuration descriptor to get the bulk I/O endpoints */
|
|
|
|
|
2015-04-21 00:21:13 +02:00
|
|
|
ret = usbhost_cfgdesc(priv, configdesc, desclen);
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret < 0)
|
2010-12-15 23:28:35 +01:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
uerr("ERROR: usbhost_cfgdesc() failed: %d\n", ret);
|
2010-12-15 23:28:35 +01:00
|
|
|
}
|
2011-01-11 02:41:06 +01:00
|
|
|
else
|
2010-12-15 23:28:35 +01:00
|
|
|
{
|
2011-01-11 02:41:06 +01:00
|
|
|
/* Now configure the LUNs and register the block driver(s) */
|
2010-12-15 23:28:35 +01:00
|
|
|
|
2011-01-11 02:41:06 +01:00
|
|
|
ret = usbhost_initvolume(priv);
|
2015-05-05 23:48:48 +02:00
|
|
|
if (ret < 0)
|
2010-12-15 23:28:35 +01:00
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
uerr("ERROR: usbhost_initvolume() failed: %d\n", ret);
|
2010-12-15 23:28:35 +01:00
|
|
|
}
|
|
|
|
}
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2011-01-11 02:41:06 +01:00
|
|
|
return ret;
|
2010-12-15 04:30:20 +01:00
|
|
|
}
|
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
2010-12-15 19:39:19 +01:00
|
|
|
* create().
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2010-12-15 19:39:19 +01:00
|
|
|
* On success, zero (OK) is returned. On a failure, a negated errno value
|
|
|
|
* is returned indicating the nature of the failure
|
|
|
|
*
|
2010-12-21 12:21:07 +01:00
|
|
|
* Assumptions:
|
|
|
|
* This function may be called from an interrupt handler.
|
|
|
|
*
|
2010-12-15 19:39:19 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-11 15:05:09 +01:00
|
|
|
static int usbhost_disconnected(struct usbhost_class_s *usbclass)
|
2010-12-15 19:39:19 +01:00
|
|
|
{
|
2015-01-11 15:05:09 +01:00
|
|
|
FAR struct usbhost_state_s *priv = (FAR struct usbhost_state_s *)usbclass;
|
2010-12-15 23:28:35 +01:00
|
|
|
irqstate_t flags;
|
|
|
|
|
2015-04-26 18:05:15 +02:00
|
|
|
DEBUGASSERT(priv != NULL);
|
2010-12-15 19:39:19 +01:00
|
|
|
|
2020-08-06 13:30:01 +02:00
|
|
|
# ifdef CONFIG_USBHOST_MSC_NOTIFIER
|
|
|
|
/* Signal the disconnect */
|
|
|
|
|
|
|
|
usbhost_msc_notifier_signal(WORK_USB_MSC_DISCONNECT, priv->sdchar);
|
|
|
|
# endif
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
/* Set an indication to any users of the mass storage device that the
|
|
|
|
* device is no longer available.
|
2010-12-15 19:39:19 +01:00
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2011-01-09 03:15:30 +01:00
|
|
|
priv->disconnected = true;
|
2010-12-15 19:39:19 +01:00
|
|
|
|
|
|
|
/* 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);
|
2010-12-15 23:28:35 +01:00
|
|
|
if (priv->crefs == 1)
|
|
|
|
{
|
2011-01-11 02:41:06 +01:00
|
|
|
/* 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. */
|
2010-12-15 23:28:35 +01:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
uinfo("Queuing destruction: worker %p->%p\n",
|
|
|
|
priv->work.worker, usbhost_destroy);
|
2011-01-11 02:41:06 +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-21 20:54:03 +01:00
|
|
|
}
|
2011-01-11 02:41:06 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Do the work now */
|
|
|
|
|
|
|
|
usbhost_destroy(priv);
|
|
|
|
}
|
2010-12-15 23:28:35 +01:00
|
|
|
}
|
2011-01-11 02:41:06 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2010-12-15 23:28:35 +01:00
|
|
|
return OK;
|
2010-12-15 19:39:19 +01:00
|
|
|
}
|
|
|
|
|
2010-12-15 04:12:09 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_open
|
|
|
|
*
|
|
|
|
* Description: Open the block device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_open(FAR struct inode *inode)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv;
|
2011-01-09 03:15:30 +01:00
|
|
|
irqstate_t flags;
|
2010-12-15 19:39:19 +01:00
|
|
|
int ret;
|
2010-12-15 04:12:09 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entry\n");
|
2010-12-15 04:12:09 +01:00
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
priv = (FAR struct usbhost_state_s *)inode->i_private;
|
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
/* Make sure that we have exclusive access to the private data structure */
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS);
|
2020-03-30 22:20:53 +02:00
|
|
|
ret = usbhost_takesem(&priv->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
/* Check if the mass storage device is still connected. We need to
|
|
|
|
* disable interrupts momentarily to assure that there are no asynchronous
|
|
|
|
* disconnect events.
|
2011-01-09 03:15:30 +01:00
|
|
|
*/
|
2010-12-15 04:12:09 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2011-01-09 03:15:30 +01:00
|
|
|
if (priv->disconnected)
|
2010-12-15 19:39:19 +01:00
|
|
|
{
|
2020-03-21 20:54:03 +01:00
|
|
|
/* No... the block 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.
|
2010-12-15 19:39:19 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -ENODEV;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, just increment the reference count on the driver */
|
|
|
|
|
|
|
|
priv->crefs++;
|
|
|
|
ret = OK;
|
|
|
|
}
|
2020-03-21 20:54:03 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2010-12-15 19:39:19 +01:00
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
usbhost_givesem(&priv->exclsem);
|
2010-12-15 19:39:19 +01:00
|
|
|
return ret;
|
2010-12-15 04:12:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_close
|
|
|
|
*
|
|
|
|
* Description: close the block device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_close(FAR struct inode *inode)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv;
|
2011-01-09 03:15:30 +01:00
|
|
|
irqstate_t flags;
|
2010-12-15 04:12:09 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entry\n");
|
2010-12-15 04:12:09 +01:00
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
priv = (FAR struct usbhost_state_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Decrement the reference count on the block driver */
|
|
|
|
|
2010-12-21 02:10:29 +01:00
|
|
|
DEBUGASSERT(priv->crefs > 1);
|
2020-03-30 22:20:53 +02:00
|
|
|
|
|
|
|
usbhost_forcetake(&priv->exclsem);
|
2010-12-15 04:12:09 +01:00
|
|
|
priv->crefs--;
|
2010-12-15 19:39:19 +01:00
|
|
|
|
2010-12-15 23:28:35 +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 block driver.
|
|
|
|
*/
|
|
|
|
|
2010-12-16 18:11:05 +01:00
|
|
|
usbhost_givesem(&priv->exclsem);
|
2010-12-15 23:28:35 +01:00
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
/* We need to disable interrupts momentarily to assure that there are
|
|
|
|
* no asynchronous disconnect events.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
/* Check if the USB mass storage device is still connected. If the
|
|
|
|
* storage device is not connected and the reference count just
|
|
|
|
* decremented to one, then unregister the block driver and free
|
|
|
|
* the class instance.
|
|
|
|
*/
|
2010-12-15 23:28:35 +01:00
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
if (priv->crefs <= 1 && priv->disconnected)
|
2010-12-15 23:28:35 +01:00
|
|
|
{
|
|
|
|
/* Destroy the class instance */
|
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
DEBUGASSERT(priv->crefs == 1);
|
2010-12-15 23:28:35 +01:00
|
|
|
usbhost_destroy(priv);
|
|
|
|
}
|
2011-01-09 03:15:30 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2010-12-15 04:12:09 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_read
|
|
|
|
*
|
|
|
|
* Description:
|
2020-02-23 09:50:23 +01:00
|
|
|
* Read the specified number of sectors from the read-ahead buffer or from
|
2010-12-15 04:12:09 +01:00
|
|
|
* the physical device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t usbhost_read(FAR struct inode *inode, unsigned char *buffer,
|
2021-02-17 08:58:11 +01:00
|
|
|
blkcnt_t startsector, unsigned int nsectors)
|
2010-12-15 04:12:09 +01:00
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv;
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2015-05-05 21:14:22 +02:00
|
|
|
ssize_t nbytes = 0;
|
2020-03-30 22:20:53 +02:00
|
|
|
int ret;
|
2010-12-15 04:12:09 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
priv = (FAR struct usbhost_state_s *)inode->i_private;
|
2015-04-21 15:26:28 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(priv->usbclass.hport);
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
|
2021-07-21 09:27:36 +02:00
|
|
|
uinfo("startsector: %" PRIuOFF " nsectors: %u "
|
|
|
|
"sectorsize: %" PRIu16 "\n", startsector, nsectors, priv->blocksize);
|
2010-12-15 04:12:09 +01:00
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
/* Check if the mass storage device is still connected */
|
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
if (priv->disconnected)
|
2010-12-15 19:39:19 +01:00
|
|
|
{
|
2015-01-11 15:05:09 +01:00
|
|
|
/* No... the block driver is no longer bound to the class. That means
|
|
|
|
* that the USB storage device is no longer connected. Refuse any
|
|
|
|
* attempt to read from the device.
|
2010-12-15 19:39:19 +01:00
|
|
|
*/
|
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = -ENODEV;
|
2010-12-15 19:39:19 +01:00
|
|
|
}
|
|
|
|
else if (nsectors > 0)
|
2010-12-15 04:12:09 +01:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_cbw_s *cbw;
|
2010-12-18 18:35:54 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
ret = usbhost_takesem(&priv->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2010-12-18 18:35:54 +01:00
|
|
|
|
|
|
|
/* Assume allocation failure */
|
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = -ENOMEM;
|
2010-12-18 18:35:54 +01:00
|
|
|
|
2010-12-21 16:57:40 +01:00
|
|
|
/* Initialize a CBW (re-using the allocated transfer buffer) */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
cbw = usbhost_cbwalloc(priv);
|
|
|
|
if (cbw)
|
|
|
|
{
|
2012-08-25 00:16:09 +02:00
|
|
|
/* Loop in the event that EAGAIN is returned (mean that the
|
|
|
|
* transaction was NAKed and we should try again.
|
|
|
|
*/
|
2010-12-18 18:35:54 +01:00
|
|
|
|
2012-08-25 00:16:09 +02:00
|
|
|
do
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2012-08-25 00:16:09 +02:00
|
|
|
/* Assume some device failure */
|
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = -ENODEV;
|
2010-12-21 02:10:29 +01:00
|
|
|
|
2012-08-25 00:16:09 +02:00
|
|
|
/* Construct and send the CBW */
|
|
|
|
|
|
|
|
usbhost_readcbw(startsector, priv->blocksize, nsectors, cbw);
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkout,
|
2015-10-10 18:41:00 +02:00
|
|
|
(FAR uint8_t *)cbw, USBMSC_CBW_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2012-08-25 00:16:09 +02:00
|
|
|
/* Receive the user data */
|
2010-12-18 18:35:54 +01:00
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkin,
|
2012-08-25 00:16:09 +02:00
|
|
|
buffer, priv->blocksize * nsectors);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2012-08-25 00:16:09 +02:00
|
|
|
/* Receive the CSW */
|
2010-12-18 18:35:54 +01:00
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkin,
|
2020-03-21 20:54:03 +01:00
|
|
|
priv->tbuffer,
|
|
|
|
USBMSC_CSW_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2012-08-25 00:16:09 +02:00
|
|
|
FAR struct usbmsc_csw_s *csw;
|
|
|
|
|
|
|
|
/* Check the CSW status */
|
|
|
|
|
|
|
|
csw = (FAR struct usbmsc_csw_s *)priv->tbuffer;
|
2015-05-05 23:48:48 +02:00
|
|
|
if (csw->status != 0)
|
2012-08-25 00:16:09 +02:00
|
|
|
{
|
2020-03-21 20:54:03 +01:00
|
|
|
uerr("ERROR: CSW status error: %d\n",
|
|
|
|
csw->status);
|
2015-05-05 23:48:48 +02:00
|
|
|
nbytes = -ENODEV;
|
2012-08-25 00:16:09 +02:00
|
|
|
}
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-10 18:41:00 +02:00
|
|
|
}
|
|
|
|
while (nbytes == -EAGAIN);
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
|
|
|
|
2010-12-16 18:11:05 +01:00
|
|
|
usbhost_givesem(&priv->exclsem);
|
2010-12-15 04:12:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* On success, return the number of blocks read */
|
|
|
|
|
2015-05-05 23:48:48 +02:00
|
|
|
return nbytes < 0 ? (int)nbytes : nsectors;
|
2010-12-15 04:12:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write the specified number of sectors to the write buffer or to the
|
|
|
|
* physical device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
static ssize_t usbhost_write(FAR struct inode *inode,
|
|
|
|
FAR const unsigned char *buffer,
|
2021-02-17 08:58:11 +01:00
|
|
|
blkcnt_t startsector, unsigned int nsectors)
|
2010-12-15 04:12:09 +01:00
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv;
|
2015-04-21 15:26:28 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2015-05-05 21:14:22 +02:00
|
|
|
ssize_t nbytes;
|
2020-03-30 22:20:53 +02:00
|
|
|
int ret;
|
2010-12-15 04:12:09 +01:00
|
|
|
|
2021-07-21 09:27:36 +02:00
|
|
|
uinfo("sector: %" PRIuOFF " nsectors: %u\n", startsector, nsectors);
|
2015-04-21 15:26:28 +02:00
|
|
|
|
2010-12-15 04:12:09 +01:00
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
priv = (FAR struct usbhost_state_s *)inode->i_private;
|
|
|
|
|
2015-04-21 15:26:28 +02:00
|
|
|
DEBUGASSERT(priv->usbclass.hport);
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
/* Check if the mass storage device is still connected */
|
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
if (priv->disconnected)
|
2010-12-15 19:39:19 +01:00
|
|
|
{
|
2015-01-11 15:05:09 +01:00
|
|
|
/* No... the block driver is no longer bound to the class. That means
|
|
|
|
* that the USB storage device is no longer connected. Refuse any
|
|
|
|
* attempt to write to the device.
|
2010-12-15 19:39:19 +01:00
|
|
|
*/
|
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = -ENODEV;
|
2010-12-15 19:39:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_cbw_s *cbw;
|
2010-12-18 18:35:54 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
ret = usbhost_takesem(&priv->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2010-12-18 18:35:54 +01:00
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
/* Assume allocation failure */
|
2010-12-18 18:35:54 +01:00
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = -ENOMEM;
|
2010-12-18 18:35:54 +01:00
|
|
|
|
2010-12-21 16:57:40 +01:00
|
|
|
/* Initialize a CBW (re-using the allocated transfer buffer) */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
cbw = usbhost_cbwalloc(priv);
|
|
|
|
if (cbw)
|
|
|
|
{
|
|
|
|
/* Assume some device failure */
|
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = -ENODEV;
|
2010-12-18 18:35:54 +01:00
|
|
|
|
|
|
|
/* Construct and send the CBW */
|
2012-08-25 00:16:09 +02:00
|
|
|
|
2010-12-18 18:35:54 +01:00
|
|
|
usbhost_writecbw(startsector, priv->blocksize, nsectors, cbw);
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkout,
|
2015-10-10 18:41:00 +02:00
|
|
|
(FAR uint8_t *)cbw, USBMSC_CBW_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
|
|
|
/* Send the user data */
|
2010-12-21 02:10:29 +01:00
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkout,
|
2020-03-21 20:54:03 +01:00
|
|
|
(FAR uint8_t *)buffer,
|
|
|
|
priv->blocksize * nsectors);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
|
|
|
/* Receive the CSW */
|
|
|
|
|
2015-05-05 21:14:22 +02:00
|
|
|
nbytes = DRVR_TRANSFER(hport->drvr, priv->bulkin,
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->tbuffer, USBMSC_CSW_SIZEOF);
|
2015-05-05 21:14:22 +02:00
|
|
|
if (nbytes >= 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_csw_s *csw;
|
2010-12-18 18:35:54 +01:00
|
|
|
|
|
|
|
/* Check the CSW status */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
csw = (FAR struct usbmsc_csw_s *)priv->tbuffer;
|
2015-05-05 23:48:48 +02:00
|
|
|
if (csw->status != 0)
|
2010-12-18 18:35:54 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: CSW status error: %d\n", csw->status);
|
2015-05-05 23:48:48 +02:00
|
|
|
nbytes = -ENODEV;
|
2010-12-18 18:35:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-16 18:11:05 +01:00
|
|
|
usbhost_givesem(&priv->exclsem);
|
2010-12-15 19:39:19 +01:00
|
|
|
}
|
2010-12-15 04:12:09 +01:00
|
|
|
|
|
|
|
/* On success, return the number of blocks written */
|
|
|
|
|
2015-05-05 23:48:48 +02:00
|
|
|
return nbytes < 0 ? (int)nbytes : nsectors;
|
2010-12-15 04:12:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_geometry
|
|
|
|
*
|
|
|
|
* Description: Return device geometry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-21 20:54:03 +01:00
|
|
|
static int usbhost_geometry(FAR struct inode *inode,
|
|
|
|
FAR struct geometry *geometry)
|
2010-12-15 04:12:09 +01:00
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv;
|
|
|
|
int ret = -EINVAL;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entry\n");
|
2010-12-15 04:12:09 +01:00
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
/* Check if the mass storage device is still connected */
|
|
|
|
|
2010-12-17 03:19:04 +01:00
|
|
|
priv = (FAR struct usbhost_state_s *)inode->i_private;
|
2011-01-09 03:15:30 +01:00
|
|
|
if (priv->disconnected)
|
2010-12-15 19:39:19 +01:00
|
|
|
{
|
2015-01-11 15:05:09 +01:00
|
|
|
/* No... the block driver is no longer bound to the class. That means
|
|
|
|
* that the USB storage device is no longer connected. Refuse to
|
|
|
|
* return any geometry info.
|
2010-12-15 19:39:19 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -ENODEV;
|
|
|
|
}
|
|
|
|
else if (geometry)
|
2010-12-15 04:12:09 +01:00
|
|
|
{
|
|
|
|
/* Return the geometry of the USB mass storage device */
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
ret = usbhost_takesem(&priv->exclsem);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
geometry->geo_available = true;
|
|
|
|
geometry->geo_mediachanged = false;
|
|
|
|
geometry->geo_writeenabled = true;
|
|
|
|
geometry->geo_nsectors = priv->nblocks;
|
|
|
|
geometry->geo_sectorsize = priv->blocksize;
|
|
|
|
usbhost_givesem(&priv->exclsem);
|
|
|
|
|
2021-07-21 09:27:36 +02:00
|
|
|
uinfo("nsectors: %" PRIdOFF " sectorsize: %" PRIi16 "\n",
|
|
|
|
geometry->geo_nsectors, geometry->geo_sectorsize);
|
2020-03-30 22:20:53 +02:00
|
|
|
}
|
2010-12-15 04:12:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_ioctl
|
|
|
|
*
|
|
|
|
* Description: Return device geometry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_ioctl(FAR struct inode *inode, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_state_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entry\n");
|
2010-12-15 04:12:09 +01:00
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
priv = (FAR struct usbhost_state_s *)inode->i_private;
|
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
/* Check if the mass storage device is still connected */
|
2010-12-15 04:12:09 +01:00
|
|
|
|
2011-01-09 03:15:30 +01:00
|
|
|
if (priv->disconnected)
|
2010-12-15 04:12:09 +01:00
|
|
|
{
|
2015-01-11 15:05:09 +01:00
|
|
|
/* No... the block driver is no longer bound to the class. That means
|
|
|
|
* that the USB storage device is no longer connected. Refuse to
|
|
|
|
* process any ioctl commands.
|
2010-12-15 19:39:19 +01:00
|
|
|
*/
|
2010-12-15 04:12:09 +01:00
|
|
|
|
2010-12-15 19:39:19 +01:00
|
|
|
ret = -ENODEV;
|
2010-12-15 04:12:09 +01:00
|
|
|
}
|
2010-12-15 19:39:19 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Process the IOCTL by command */
|
2010-12-15 04:12:09 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
ret = usbhost_takesem(&priv->exclsem);
|
|
|
|
if (ret >= 0)
|
2010-12-15 19:39:19 +01:00
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* Add support for ioctl commands here */
|
2010-12-15 19:39:19 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
2020-03-21 20:54:03 +01:00
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
usbhost_givesem(&priv->exclsem);
|
|
|
|
}
|
2010-12-15 19:39:19 +01:00
|
|
|
}
|
2020-03-21 20:54:03 +01:00
|
|
|
|
2010-12-15 04:12:09 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-14 17:35:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-05-06 22:11:29 +02:00
|
|
|
* Name: usbhost_msc_initialize
|
2010-12-14 17:35:10 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the USB host storage class. This function should be called
|
|
|
|
* be platform-specific code in order to initialize and register support
|
|
|
|
* for the USB host storage class.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2010-12-14 17:35:10 +01:00
|
|
|
* On success this function will return zero (OK); A negated errno value
|
|
|
|
* will be returned on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-05-06 22:11:29 +02:00
|
|
|
int usbhost_msc_initialize(void)
|
2010-12-14 17:35:10 +01:00
|
|
|
{
|
2010-12-15 19:39:19 +01:00
|
|
|
/* If we have been configured to use pre-allocated storage class instances,
|
|
|
|
* then place all of the pre-allocated USB host storage class instances
|
|
|
|
* into a free list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_USBHOST_NPREALLOC > 0
|
2013-08-12 01:11:32 +02:00
|
|
|
FAR struct usbhost_freestate_s *entry;
|
2010-12-15 19:39:19 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
g_freelist = NULL;
|
|
|
|
for (i = 0; i < CONFIG_USBHOST_NPREALLOC; i++)
|
|
|
|
{
|
2013-08-12 01:11:32 +02:00
|
|
|
entry = (FAR struct usbhost_freestate_s *)&g_prealloc[i];
|
|
|
|
entry->flink = g_freelist;
|
|
|
|
g_freelist = entry;
|
2010-12-15 19:39:19 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Advertise our availability to support (certain) mass storage devices */
|
2010-12-14 17:35:10 +01:00
|
|
|
|
|
|
|
return usbhost_registerclass(&g_storage);
|
|
|
|
}
|
|
|
|
|
2020-08-06 13:30:01 +02:00
|
|
|
# ifdef CONFIG_USBHOST_MSC_NOTIFIER
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_msc_notifier_setup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set up to perform a callback to the worker function when a mass storage
|
|
|
|
* device is attached.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* worker - The worker function to execute on the low priority work queue
|
|
|
|
* when the event occurs.
|
|
|
|
* event - Currently only USBHOST_MSC_DISCONNECT and USBHOST_MSC_CONNECT
|
|
|
|
* sdchar - sdchar of the connected or disconnected block device
|
|
|
|
* arg - A user-defined argument that will be available to the worker
|
|
|
|
* function when it runs.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* > 0 - The notification is in place. The returned value is a key that
|
|
|
|
* may be used later in a call to
|
|
|
|
* usbmsc_attach_notifier_teardown().
|
|
|
|
* == 0 - Not used.
|
|
|
|
* < 0 - An unexpected error occurred and no notification will occur. The
|
|
|
|
* returned value is a negated errno value that indicates the
|
|
|
|
* nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int usbhost_msc_notifier_setup(worker_t worker, uint8_t event, char sdchar,
|
|
|
|
FAR void *arg)
|
|
|
|
{
|
|
|
|
struct work_notifier_s info;
|
|
|
|
|
|
|
|
DEBUGASSERT(worker != NULL);
|
|
|
|
|
|
|
|
info.evtype = event;
|
|
|
|
info.qid = LPWORK;
|
|
|
|
info.qualifier = (FAR void *)(uintptr_t)sdchar;
|
|
|
|
info.arg = arg;
|
|
|
|
info.worker = worker;
|
|
|
|
|
|
|
|
return work_notifier_setup(&info);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_msc_notifier_teardown
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Eliminate an USB MSC notification previously setup by
|
|
|
|
* usbhost_msc_notifier_setup().
|
|
|
|
* This function should only be called if the notification should be
|
|
|
|
* aborted prior to the notification. The notification will automatically
|
|
|
|
* be torn down after the notification.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* key - The key value returned from a previous call to
|
|
|
|
* usbhost_msc_notifier_setup().
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int usbhost_msc_notifier_teardown(int key)
|
|
|
|
{
|
|
|
|
/* This is just a simple wrapper around work_notifier_teardown(). */
|
|
|
|
|
|
|
|
return work_notifier_teardown(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_msc_notifier_signal
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* An USB mass storage device has been connected or disconnected.
|
|
|
|
* Signal all threads.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* event - Currently only USBHOST_MSC_DISCONNECT and USBHOST_MSC_CONNECT
|
|
|
|
* sdchar - sdchar of the connected or disconnected block device
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void usbhost_msc_notifier_signal(uint8_t event, char sdchar)
|
|
|
|
{
|
|
|
|
work_notifier_signal(event, (FAR void *)(uintptr_t)sdchar);
|
|
|
|
}
|
|
|
|
|
|
|
|
# endif /* CONFIG_USBHOST_MSC_NOTIFIER */
|
|
|
|
|
2020-01-31 19:07:39 +01:00
|
|
|
#endif /* CONFIG_USBHOST && !CONFIG_USBHOST_BULK_DISABLE && !CONFIG_DISABLE_MOUNTPOINT */
|