2011-12-19 15:24:09 -04:00
|
|
|
/****************************************************************************
|
2012-01-26 10:24:15 -04:00
|
|
|
* drivers/usbdev/usbmsc.c
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
2012-01-24 17:51:26 -04:00
|
|
|
* Copyright (C) 2008-2012 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Mass storage class device. Bulk-only with SCSI subclass.
|
|
|
|
*
|
|
|
|
* References:
|
|
|
|
* "Universal Serial Bus Mass Storage Class, Specification Overview,"
|
|
|
|
* Revision 1.2, USB Implementer's Forum, June 23, 2003.
|
|
|
|
*
|
|
|
|
* "Universal Serial Bus Mass Storage Class, Bulk-Only Transport,"
|
|
|
|
* Revision 1.0, USB Implementer's Forum, September 31, 1999.
|
|
|
|
*
|
|
|
|
* "SCSI Primary Commands - 3 (SPC-3)," American National Standard
|
|
|
|
* for Information Technology, May 4, 2005
|
|
|
|
*
|
|
|
|
* "SCSI Primary Commands - 4 (SPC-4)," American National Standard
|
|
|
|
* for Information Technology, July 19, 2008
|
|
|
|
*
|
|
|
|
* "SCSI Block Commands -2 (SBC-2)," American National Standard
|
|
|
|
* for Information Technology, November 13, 2004
|
|
|
|
*
|
|
|
|
* "SCSI Multimedia Commands - 3 (MMC-3)," American National Standard
|
|
|
|
* for Information Technology, November 12, 2001
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <queue.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/fs.h>
|
|
|
|
#include <nuttx/usb/usb.h>
|
|
|
|
#include <nuttx/usb/storage.h>
|
|
|
|
#include <nuttx/usb/usbdev.h>
|
|
|
|
#include <nuttx/usb/usbdev_trace.h>
|
|
|
|
|
2012-01-26 10:24:15 -04:00
|
|
|
#include "usbmsc.h"
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-01-26 13:42:44 -04:00
|
|
|
#ifdef CONFIG_USBMSC_COMPOSITE
|
2012-01-26 19:14:27 -04:00
|
|
|
# include <nuttx/usb/composite.h>
|
2012-01-26 13:42:44 -04:00
|
|
|
# include "composite.h"
|
|
|
|
#endif
|
|
|
|
|
2011-12-19 15:24:09 -04:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The internal version of the class driver */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
struct usbmsc_driver_s
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
struct usbdevclass_driver_s drvr;
|
2012-01-25 16:17:59 -04:00
|
|
|
FAR struct usbmsc_dev_s *dev;
|
2011-12-19 15:24:09 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This is what is allocated */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
struct usbmsc_alloc_s
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
struct usbmsc_dev_s dev;
|
|
|
|
struct usbmsc_driver_s drvr;
|
2011-12-19 15:24:09 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Class Driver Support *****************************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
static void usbmsc_ep0incomplete(FAR struct usbdev_ep_s *ep,
|
2011-12-19 15:24:09 -04:00
|
|
|
FAR struct usbdev_req_s *req);
|
2012-01-25 16:17:59 -04:00
|
|
|
static struct usbdev_req_s *usbmsc_allocreq(FAR struct usbdev_ep_s *ep,
|
2011-12-19 15:24:09 -04:00
|
|
|
uint16_t len);
|
2012-01-25 16:17:59 -04:00
|
|
|
static void usbmsc_freereq(FAR struct usbdev_ep_s *ep,
|
2011-12-19 15:24:09 -04:00
|
|
|
FAR struct usbdev_req_s *req);
|
|
|
|
|
|
|
|
/* Class Driver Operations (most at interrupt level) ************************/
|
|
|
|
|
2012-01-26 13:42:44 -04:00
|
|
|
static int usbmsc_bind(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev);
|
|
|
|
static void usbmsc_unbind(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev);
|
|
|
|
static int usbmsc_setup(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev,
|
2011-12-19 15:24:09 -04:00
|
|
|
FAR const struct usb_ctrlreq_s *ctrl);
|
2012-01-26 13:42:44 -04:00
|
|
|
static void usbmsc_disconnect(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev);
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Initialization/Uninitialization ******************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
static void usbmsc_lununinitialize(struct usbmsc_lun_s *lun);
|
2012-01-26 19:14:27 -04:00
|
|
|
#ifndef CONFIG_USBMSC_COMPOSITE
|
|
|
|
static int usbmsc_exportluns(FAR void *handle);
|
|
|
|
#endif
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Driver operations ********************************************************/
|
|
|
|
|
|
|
|
static struct usbdevclass_driverops_s g_driverops =
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_bind, /* bind */
|
|
|
|
usbmsc_unbind, /* unbind */
|
|
|
|
usbmsc_setup, /* setup */
|
|
|
|
usbmsc_disconnect, /* disconnect */
|
|
|
|
NULL, /* suspend */
|
|
|
|
NULL /* resume */
|
2011-12-19 15:24:09 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Class Driver Support
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_ep0incomplete
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle completion of EP0 control operations
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
static void usbmsc_ep0incomplete(FAR struct usbdev_ep_s *ep,
|
|
|
|
FAR struct usbdev_req_s *req)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
if (req->result || req->xfrd != req->len)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_REQRESULT),
|
2011-12-19 15:24:09 -04:00
|
|
|
(uint16_t)-req->result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_allocreq
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a request instance along with its buffer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
static struct usbdev_req_s *usbmsc_allocreq(FAR struct usbdev_ep_s *ep,
|
|
|
|
uint16_t len)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
FAR struct usbdev_req_s *req;
|
|
|
|
|
|
|
|
req = EP_ALLOCREQ(ep);
|
|
|
|
if (req != NULL)
|
|
|
|
{
|
|
|
|
req->len = len;
|
|
|
|
req->buf = EP_ALLOCBUFFER(ep, len);
|
|
|
|
if (!req->buf)
|
|
|
|
{
|
|
|
|
EP_FREEREQ(ep, req);
|
|
|
|
req = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_freereq
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a request instance along with its buffer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
static void usbmsc_freereq(FAR struct usbdev_ep_s *ep, struct usbdev_req_s *req)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
if (ep != NULL && req != NULL)
|
|
|
|
{
|
|
|
|
if (req->buf != NULL)
|
|
|
|
{
|
|
|
|
EP_FREEBUFFER(ep, req->buf);
|
|
|
|
}
|
|
|
|
EP_FREEREQ(ep, req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Class Driver Interfaces
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_bind
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked when the driver is bound to a USB device driver
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-26 13:42:44 -04:00
|
|
|
static int usbmsc_bind(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-26 13:42:44 -04:00
|
|
|
FAR struct usbmsc_dev_s *priv = ((FAR struct usbmsc_driver_s*)driver)->dev;
|
2012-01-25 16:17:59 -04:00
|
|
|
FAR struct usbmsc_req_s *reqcontainer;
|
2011-12-19 15:24:09 -04:00
|
|
|
irqstate_t flags;
|
|
|
|
int ret = OK;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSBIND, 0);
|
|
|
|
|
|
|
|
/* Bind the structures */
|
|
|
|
|
|
|
|
priv->usbdev = dev;
|
2012-01-26 13:42:44 -04:00
|
|
|
|
|
|
|
/* Save the reference to our private data structure in EP0 so that it
|
|
|
|
* can be recovered in ep0 completion events (Unless we are part of
|
|
|
|
* a composite device and, in that case, the composite device owns
|
|
|
|
* EP0).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBMSC_COMPOSITE
|
2011-12-19 15:24:09 -04:00
|
|
|
dev->ep0->priv = priv;
|
2012-01-26 13:42:44 -04:00
|
|
|
#endif
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* The configured EP0 size should match the reported EP0 size. We could
|
|
|
|
* easily adapt to the reported EP0 size, but then we could not use the
|
|
|
|
* const, canned descriptors.
|
|
|
|
*/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
DEBUGASSERT(CONFIG_USBMSC_EP0MAXPACKET == dev->ep0->maxpacket);
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Preallocate control request */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->ctrlreq = usbmsc_allocreq(dev->ep0, USBMSC_MXDESCLEN);
|
2011-12-19 15:24:09 -04:00
|
|
|
if (priv->ctrlreq == NULL)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_ALLOCCTRLREQ), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->ctrlreq->callback = usbmsc_ep0incomplete;
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Pre-allocate all endpoints... the endpoints will not be functional
|
2012-01-25 16:17:59 -04:00
|
|
|
* until the SET CONFIGURATION request is processed in usbmsc_setconfig.
|
2011-12-19 15:24:09 -04:00
|
|
|
* This is done here because there may be calls to kmalloc and the SET
|
|
|
|
* CONFIGURATION processing probably occurrs within interrupt handling
|
|
|
|
* logic where kmalloc calls will fail.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Pre-allocate the IN bulk endpoint */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->epbulkin = DEV_ALLOCEP(dev, USBMSC_EPINBULK_ADDR, true, USB_EP_ATTR_XFER_BULK);
|
2011-12-19 15:24:09 -04:00
|
|
|
if (!priv->epbulkin)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EPBULKINALLOCFAIL), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
ret = -ENODEV;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
priv->epbulkin->priv = priv;
|
|
|
|
|
|
|
|
/* Pre-allocate the OUT bulk endpoint */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->epbulkout = DEV_ALLOCEP(dev, USBMSC_EPOUTBULK_ADDR, false, USB_EP_ATTR_XFER_BULK);
|
2011-12-19 15:24:09 -04:00
|
|
|
if (!priv->epbulkout)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EPBULKOUTALLOCFAIL), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
ret = -ENODEV;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
priv->epbulkout->priv = priv;
|
|
|
|
|
|
|
|
/* Pre-allocate read requests */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
for (i = 0; i < CONFIG_USBMSC_NRDREQS; i++)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
reqcontainer = &priv->rdreqs[i];
|
2012-01-25 16:17:59 -04:00
|
|
|
reqcontainer->req = usbmsc_allocreq(priv->epbulkout, CONFIG_USBMSC_BULKOUTREQLEN);
|
2011-12-19 15:24:09 -04:00
|
|
|
if (reqcontainer->req == NULL)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_RDALLOCREQ),
|
2011-12-19 15:24:09 -04:00
|
|
|
(uint16_t)-ret);
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
reqcontainer->req->priv = reqcontainer;
|
2012-01-25 16:17:59 -04:00
|
|
|
reqcontainer->req->callback = usbmsc_rdcomplete;
|
2011-12-19 15:24:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Pre-allocate write request containers and put in a free list */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
for (i = 0; i < CONFIG_USBMSC_NWRREQS; i++)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
reqcontainer = &priv->wrreqs[i];
|
2012-01-25 16:17:59 -04:00
|
|
|
reqcontainer->req = usbmsc_allocreq(priv->epbulkin, CONFIG_USBMSC_BULKINREQLEN);
|
2011-12-19 15:24:09 -04:00
|
|
|
if (reqcontainer->req == NULL)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRALLOCREQ),
|
2011-12-19 15:24:09 -04:00
|
|
|
(uint16_t)-ret);
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
reqcontainer->req->priv = reqcontainer;
|
2012-01-25 16:17:59 -04:00
|
|
|
reqcontainer->req->callback = usbmsc_wrcomplete;
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
flags = irqsave();
|
|
|
|
sq_addlast((sq_entry_t*)reqcontainer, &priv->wrreqlist);
|
|
|
|
irqrestore(flags);
|
|
|
|
}
|
|
|
|
|
2012-01-25 15:27:20 -04:00
|
|
|
/* Report if we are selfpowered (unless we are part of a composite device) */
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
#ifndef CONFIG_USBMSC_COMPOSITE
|
2011-12-19 15:24:09 -04:00
|
|
|
#ifdef CONFIG_USBDEV_SELFPOWERED
|
|
|
|
DEV_SETSELFPOWERED(dev);
|
|
|
|
#endif
|
|
|
|
|
2012-01-25 15:27:20 -04:00
|
|
|
/* And pull-up the data line for the soft connect function (unless we are
|
|
|
|
* part of a composite device)
|
|
|
|
*/
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
DEV_CONNECT(dev);
|
2012-01-25 15:27:20 -04:00
|
|
|
#endif
|
2011-12-19 15:24:09 -04:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
2012-01-26 13:42:44 -04:00
|
|
|
usbmsc_unbind(driver, dev);
|
2011-12-19 15:24:09 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_unbind
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked when the driver is unbound from a USB device driver
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-26 13:42:44 -04:00
|
|
|
static void usbmsc_unbind(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
FAR struct usbmsc_dev_s *priv;
|
|
|
|
FAR struct usbmsc_req_s *reqcontainer;
|
2011-12-19 15:24:09 -04:00
|
|
|
irqstate_t flags;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSUNBIND, 0);
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
2012-01-26 13:42:44 -04:00
|
|
|
if (!driver || !dev || !dev->ep0)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_UNBINDINVALIDARGS), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract reference to private data */
|
|
|
|
|
2012-01-26 13:42:44 -04:00
|
|
|
priv = ((FAR struct usbmsc_driver_s*)driver)->dev;
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!priv)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EP0NOTBOUND1), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* The worker thread should have already been stopped by the
|
|
|
|
* driver un-initialize logic.
|
|
|
|
*/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
DEBUGASSERT(priv->thstate == USBMSC_STATE_TERMINATED);
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Make sure that we are not already unbound */
|
|
|
|
|
|
|
|
if (priv != NULL)
|
|
|
|
{
|
|
|
|
/* Make sure that the endpoints have been unconfigured. If
|
|
|
|
* we were terminated gracefully, then the configuration should
|
2012-01-25 16:17:59 -04:00
|
|
|
* already have been reset. If not, then calling usbmsc_resetconfig
|
2011-12-19 15:24:09 -04:00
|
|
|
* should cause the endpoints to immediately terminate all
|
|
|
|
* transfers and return the requests to us (with result == -ESHUTDOWN)
|
|
|
|
*/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_resetconfig(priv);
|
2011-12-19 15:24:09 -04:00
|
|
|
up_mdelay(50);
|
|
|
|
|
|
|
|
/* Free the pre-allocated control request */
|
|
|
|
|
|
|
|
if (priv->ctrlreq != NULL)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_freereq(dev->ep0, priv->ctrlreq);
|
2011-12-19 15:24:09 -04:00
|
|
|
priv->ctrlreq = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free pre-allocated read requests (which should all have
|
|
|
|
* been returned to the free list at this time -- we don't check)
|
|
|
|
*/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
for (i = 0; i < CONFIG_USBMSC_NRDREQS; i++)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
reqcontainer = &priv->rdreqs[i];
|
|
|
|
if (reqcontainer->req)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_freereq(priv->epbulkout, reqcontainer->req);
|
2011-12-19 15:24:09 -04:00
|
|
|
reqcontainer->req = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the bulk OUT endpoint */
|
|
|
|
|
|
|
|
if (priv->epbulkout)
|
|
|
|
{
|
|
|
|
DEV_FREEEP(dev, priv->epbulkout);
|
|
|
|
priv->epbulkout = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free write requests that are not in use (which should be all
|
|
|
|
* of them
|
|
|
|
*/
|
|
|
|
|
|
|
|
flags = irqsave();
|
|
|
|
while (!sq_empty(&priv->wrreqlist))
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
reqcontainer = (struct usbmsc_req_s *)sq_remfirst(&priv->wrreqlist);
|
2011-12-19 15:24:09 -04:00
|
|
|
if (reqcontainer->req != NULL)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_freereq(priv->epbulkin, reqcontainer->req);
|
2011-12-19 15:24:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the bulk IN endpoint */
|
|
|
|
|
|
|
|
if (priv->epbulkin)
|
|
|
|
{
|
|
|
|
DEV_FREEEP(dev, priv->epbulkin);
|
|
|
|
priv->epbulkin = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
irqrestore(flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_setup
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked for ep0 control requests. This function probably executes
|
|
|
|
* in the context of an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-26 13:42:44 -04:00
|
|
|
static int usbmsc_setup(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev,
|
2012-01-25 16:17:59 -04:00
|
|
|
FAR const struct usb_ctrlreq_s *ctrl)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
FAR struct usbmsc_dev_s *priv;
|
2011-12-19 15:24:09 -04:00
|
|
|
FAR struct usbdev_req_s *ctrlreq;
|
|
|
|
uint16_t value;
|
|
|
|
uint16_t index;
|
|
|
|
uint16_t len;
|
|
|
|
int ret = -EOPNOTSUPP;
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
2012-01-26 13:42:44 -04:00
|
|
|
if (!driver || !dev || !dev->ep0 || !ctrl)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_SETUPINVALIDARGS), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract reference to private data */
|
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSSETUP, ctrl->req);
|
2012-01-26 13:42:44 -04:00
|
|
|
priv = ((FAR struct usbmsc_driver_s *)driver)->dev;
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!priv || !priv->ctrlreq)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EP0NOTBOUND2), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ctrlreq = priv->ctrlreq;
|
|
|
|
|
|
|
|
/* Extract the little-endian 16-bit values to host order */
|
|
|
|
|
|
|
|
value = GETUINT16(ctrl->value);
|
|
|
|
index = GETUINT16(ctrl->index);
|
|
|
|
len = GETUINT16(ctrl->len);
|
|
|
|
|
|
|
|
uvdbg("type=%02x req=%02x value=%04x index=%04x len=%04x\n",
|
|
|
|
ctrl->type, ctrl->req, value, index, len);
|
|
|
|
|
|
|
|
if ((ctrl->type & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_STANDARD)
|
|
|
|
{
|
|
|
|
/**********************************************************************
|
|
|
|
* Standard Requests
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
switch (ctrl->req)
|
|
|
|
{
|
|
|
|
case USB_REQ_GETDESCRIPTOR:
|
|
|
|
{
|
|
|
|
/* The value field specifies the descriptor type in the MS byte and the
|
|
|
|
* descriptor index in the LS byte (order is little endian)
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (ctrl->value[1])
|
|
|
|
{
|
2012-01-25 15:27:20 -04:00
|
|
|
/* If the mass storage device is used in as part of a composite
|
|
|
|
* device, then the device descriptor is is provided by logic
|
|
|
|
* in the composite device implementation.
|
|
|
|
*/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
#ifndef CONFIG_USBMSC_COMPOSITE
|
2011-12-19 15:24:09 -04:00
|
|
|
case USB_DESC_TYPE_DEVICE:
|
|
|
|
{
|
|
|
|
ret = USB_SIZEOF_DEVDESC;
|
2012-01-25 16:17:59 -04:00
|
|
|
memcpy(ctrlreq->buf, usbmsc_getdevdesc(), ret);
|
2011-12-19 15:24:09 -04:00
|
|
|
}
|
|
|
|
break;
|
2012-01-25 15:27:20 -04:00
|
|
|
#endif
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-01-25 15:27:20 -04:00
|
|
|
/* If the mass storage device is used in as part of a composite device,
|
|
|
|
* then the device qualifier descriptor is provided by logic in the
|
|
|
|
* composite device implementation.
|
|
|
|
*/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
#if !defined(CONFIG_USBMSC_COMPOSITE) && defined(CONFIG_USBDEV_DUALSPEED)
|
2011-12-19 15:24:09 -04:00
|
|
|
case USB_DESC_TYPE_DEVICEQUALIFIER:
|
|
|
|
{
|
|
|
|
ret = USB_SIZEOF_QUALDESC;
|
2012-01-25 16:17:59 -04:00
|
|
|
memcpy(ctrlreq->buf, usbmsc_getqualdesc(), ret);
|
2011-12-19 15:24:09 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_DESC_TYPE_OTHERSPEEDCONFIG:
|
2012-01-25 15:27:20 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If the mass storage device is used in as part of a composite device,
|
|
|
|
* then the configuration descriptor is provided by logic in the
|
|
|
|
* composite device implementation.
|
|
|
|
*/
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
#ifndef CONFIG_USBMSC_COMPOSITE
|
2011-12-19 15:24:09 -04:00
|
|
|
case USB_DESC_TYPE_CONFIG:
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
2012-01-25 16:17:59 -04:00
|
|
|
ret = usbmsc_mkcfgdesc(ctrlreq->buf, dev->speed, ctrl->value[1]);
|
2011-12-19 15:24:09 -04:00
|
|
|
#else
|
2012-01-25 16:17:59 -04:00
|
|
|
ret = usbmsc_mkcfgdesc(ctrlreq->buf);
|
2011-12-19 15:24:09 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
2012-01-25 15:27:20 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If the mass storage device is used in as part of a composite device,
|
|
|
|
* then the language string descriptor is provided by logic in the
|
|
|
|
* composite device implementation.
|
|
|
|
*/
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
#ifndef CONFIG_USBMSC_COMPOSITE
|
2011-12-19 15:24:09 -04:00
|
|
|
case USB_DESC_TYPE_STRING:
|
|
|
|
{
|
|
|
|
/* index == language code. */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
ret = usbmsc_mkstrdesc(ctrl->value[0], (struct usb_strdesc_s *)ctrlreq->buf);
|
2011-12-19 15:24:09 -04:00
|
|
|
}
|
|
|
|
break;
|
2012-01-25 15:27:20 -04:00
|
|
|
#endif
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_GETUNKNOWNDESC), value);
|
2011-12-19 15:24:09 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REQ_SETCONFIGURATION:
|
|
|
|
{
|
|
|
|
if (ctrl->type == 0)
|
|
|
|
{
|
|
|
|
/* Signal the worker thread to instantiate the new configuration */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->theventset |= USBMSC_EVENT_CFGCHANGE;
|
2011-12-19 15:24:09 -04:00
|
|
|
priv->thvalue = value;
|
|
|
|
pthread_cond_signal(&priv->cond);
|
|
|
|
|
|
|
|
/* Return here... the response will be provided later by the
|
|
|
|
* worker thread.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-01-25 15:27:20 -04:00
|
|
|
/* If the mass storage device is used in as part of a composite device,
|
|
|
|
* then the overall composite class configuration is managed by logic
|
|
|
|
* in the composite device implementation.
|
|
|
|
*/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
#ifndef CONFIG_USBMSC_COMPOSITE
|
2011-12-19 15:24:09 -04:00
|
|
|
case USB_REQ_GETCONFIGURATION:
|
|
|
|
{
|
|
|
|
if (ctrl->type == USB_DIR_IN)
|
|
|
|
{
|
|
|
|
ctrlreq->buf[0] = priv->config;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2012-01-25 15:27:20 -04:00
|
|
|
#endif
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
case USB_REQ_SETINTERFACE:
|
|
|
|
{
|
|
|
|
if (ctrl->type == USB_REQ_RECIPIENT_INTERFACE)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
if (priv->config == USBMSC_CONFIGID &&
|
|
|
|
index == USBMSC_INTERFACEID &&
|
|
|
|
value == USBMSC_ALTINTERFACEID)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
/* Signal to instantiate the interface change */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->theventset |= USBMSC_EVENT_IFCHANGE;
|
2011-12-19 15:24:09 -04:00
|
|
|
pthread_cond_signal(&priv->cond);
|
|
|
|
|
|
|
|
/* Return here... the response will be provided later by the
|
|
|
|
* worker thread.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REQ_GETINTERFACE:
|
|
|
|
{
|
|
|
|
if (ctrl->type == (USB_DIR_IN|USB_REQ_RECIPIENT_INTERFACE) &&
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->config == USBMSC_CONFIGIDNONE)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
if (index != USBMSC_INTERFACEID)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
ret = -EDOM;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
ctrlreq->buf[0] = USBMSC_ALTINTERFACEID;
|
2011-12-19 15:24:09 -04:00
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_UNSUPPORTEDSTDREQ), ctrl->req);
|
2011-12-19 15:24:09 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/**********************************************************************
|
|
|
|
* Bulk-Only Mass Storage Class Requests
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
/* Verify that we are configured */
|
|
|
|
|
|
|
|
if (!priv->config)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_NOTCONFIGURED), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ctrl->req)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
case USBMSC_REQ_MSRESET: /* Reset mass storage device and interface */
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
if (ctrl->type == USBMSC_TYPE_SETUPOUT && value == 0 && len == 0)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
/* Only one interface is supported */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
if (index != USBMSC_INTERFACEID)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_MSRESETNDX), index);
|
2011-12-19 15:24:09 -04:00
|
|
|
ret = -EDOM;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Signal to stop the current operation and reinitialize state */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->theventset |= USBMSC_EVENT_RESET;
|
2011-12-19 15:24:09 -04:00
|
|
|
pthread_cond_signal(&priv->cond);
|
|
|
|
|
|
|
|
/* Return here... the response will be provided later by the
|
|
|
|
* worker thread.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
case USBMSC_REQ_GETMAXLUN: /* Return number LUNs supported */
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
if (ctrl->type == USBMSC_TYPE_SETUPIN && value == 0 && len == 1)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
/* Only one interface is supported */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
if (index != USBMSC_INTERFACEID)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_GETMAXLUNNDX), index);
|
2011-12-19 15:24:09 -04:00
|
|
|
ret = -EDOM;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctrlreq->buf[0] = priv->nluns - 1;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_BADREQUEST), index);
|
2011-12-19 15:24:09 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Respond to the setup command if data was returned. On an error return
|
|
|
|
* value (ret < 0), the USB driver will stall EP0.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2012-01-26 13:42:44 -04:00
|
|
|
/* Configure the response */
|
|
|
|
|
2012-01-25 15:27:20 -04:00
|
|
|
ctrlreq->len = MIN(len, ret);
|
2011-12-19 15:24:09 -04:00
|
|
|
ctrlreq->flags = USBDEV_REQFLAGS_NULLPKT;
|
2012-01-26 13:42:44 -04:00
|
|
|
|
|
|
|
/* Send the response -- either directly to the USB controller or
|
|
|
|
* indirectly in the case where this class is a member of a composite
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBMSC_COMPOSITE
|
|
|
|
ret = EP_SUBMIT(dev->ep0, ctrlreq);
|
|
|
|
#else
|
|
|
|
ret = composite_ep0submit(driver, dev, ctrlreq);
|
|
|
|
#endif
|
2011-12-19 15:24:09 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EPRESPQ), (uint16_t)-ret);
|
2011-12-19 15:24:09 -04:00
|
|
|
#if 0 /* Not necessary */
|
|
|
|
ctrlreq->result = OK;
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_ep0incomplete(dev->ep0, ctrlreq);
|
2011-12-19 15:24:09 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_disconnect
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked after all transfers have been stopped, when the host is
|
|
|
|
* disconnected. This function is probably called from the context of an
|
|
|
|
* interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-26 13:42:44 -04:00
|
|
|
static void usbmsc_disconnect(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
struct usbmsc_dev_s *priv;
|
2011-12-19 15:24:09 -04:00
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSDISCONNECT, 0);
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
2012-01-26 13:42:44 -04:00
|
|
|
if (!driver || !dev || !dev->ep0)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_DISCONNECTINVALIDARGS), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract reference to private data */
|
|
|
|
|
2012-01-26 13:42:44 -04:00
|
|
|
priv = ((FAR struct usbmsc_driver_s *)driver)->dev;
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!priv)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EP0NOTBOUND3), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Reset the configuration */
|
|
|
|
|
|
|
|
flags = irqsave();
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_resetconfig(priv);
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Signal the worker thread */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->theventset |= USBMSC_EVENT_DISCONNECT;
|
2011-12-19 15:24:09 -04:00
|
|
|
pthread_cond_signal(&priv->cond);
|
|
|
|
irqrestore(flags);
|
|
|
|
|
|
|
|
/* Perform the soft connect function so that we will we can be
|
2012-01-25 15:27:20 -04:00
|
|
|
* re-enumerated (unless we are part of a composite device)
|
2011-12-19 15:24:09 -04:00
|
|
|
*/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
#ifndef CONFIG_USBMSC_COMPOSITE
|
2012-01-25 15:27:20 -04:00
|
|
|
DEV_CONNECT(dev);
|
|
|
|
#endif
|
2011-12-19 15:24:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Initialization/Un-Initialization
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_lununinitialize
|
2011-12-19 15:24:09 -04:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
static void usbmsc_lununinitialize(struct usbmsc_lun_s *lun)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
/* Has a block driver has been bound to the LUN? */
|
|
|
|
|
|
|
|
if (lun->inode)
|
|
|
|
{
|
|
|
|
/* Close the block driver */
|
|
|
|
|
|
|
|
(void)close_blockdriver(lun->inode);
|
|
|
|
}
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
memset(lun, 0, sizeof(struct usbmsc_lun_s *));
|
2011-12-19 15:24:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
|
|
* Internal Interfaces
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_setconfig
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the device configuration by allocating and configuring endpoints and
|
|
|
|
* by allocating and queuing read and write requests.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
int usbmsc_setconfig(FAR struct usbmsc_dev_s *priv, uint8_t config)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
FAR struct usbmsc_req_s *privreq;
|
2011-12-19 15:24:09 -04:00
|
|
|
FAR struct usbdev_req_s *req;
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
|
|
|
FAR const struct usb_epdesc_s *epdesc;
|
|
|
|
bool hispeed = (priv->usbdev->speed == USB_SPEED_HIGH);
|
|
|
|
uint16_t bulkmxpacket;
|
|
|
|
#endif
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
#if CONFIG_DEBUG
|
|
|
|
if (priv == NULL)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_SETCONFIGINVALIDARGS), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (config == priv->config)
|
|
|
|
{
|
|
|
|
/* Already configured -- Do nothing */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_ALREADYCONFIGURED), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Discard the previous configuration data */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_resetconfig(priv);
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Was this a request to simply discard the current configuration? */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
if (config == USBMSC_CONFIGIDNONE)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CONFIGNONE), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We only accept one configuration */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
if (config != USBMSC_CONFIGID)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CONFIGIDBAD), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure the IN bulk endpoint */
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
2012-01-25 16:17:59 -04:00
|
|
|
bulkmxpacket = USBMSC_BULKMAXPACKET(hispeed);
|
|
|
|
epdesc = USBMSC_EPBULKINDESC(hispeed);
|
2011-12-19 15:24:09 -04:00
|
|
|
ret = EP_CONFIGURE(priv->epbulkin, epdesc, false);
|
|
|
|
#else
|
|
|
|
ret = EP_CONFIGURE(priv->epbulkin,
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_getepdesc(USBMSC_EPFSBULKIN), false);
|
2011-12-19 15:24:09 -04:00
|
|
|
#endif
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EPBULKINCONFIGFAIL), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->epbulkin->priv = priv;
|
|
|
|
|
|
|
|
/* Configure the OUT bulk endpoint */
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
2012-01-25 16:17:59 -04:00
|
|
|
epdesc = USBMSC_EPBULKOUTDESC(hispeed);
|
2011-12-19 15:24:09 -04:00
|
|
|
ret = EP_CONFIGURE(priv->epbulkout, epdesc, true);
|
|
|
|
#else
|
|
|
|
ret = EP_CONFIGURE(priv->epbulkout,
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_getepdesc(USBMSC_EPFSBULKOUT), true);
|
2011-12-19 15:24:09 -04:00
|
|
|
#endif
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EPBULKOUTCONFIGFAIL), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->epbulkout->priv = priv;
|
|
|
|
|
|
|
|
/* Queue read requests in the bulk OUT endpoint */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
for (i = 0; i < CONFIG_USBMSC_NRDREQS; i++)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
privreq = &priv->rdreqs[i];
|
|
|
|
req = privreq->req;
|
2012-01-25 16:17:59 -04:00
|
|
|
req->len = CONFIG_USBMSC_BULKOUTREQLEN;
|
2011-12-19 15:24:09 -04:00
|
|
|
req->priv = privreq;
|
2012-01-25 16:17:59 -04:00
|
|
|
req->callback = usbmsc_rdcomplete;
|
2011-12-19 15:24:09 -04:00
|
|
|
ret = EP_SUBMIT(priv->epbulkout, req);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_RDSUBMIT), (uint16_t)-ret);
|
2011-12-19 15:24:09 -04:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->config = config;
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_resetconfig(priv);
|
2011-12-19 15:24:09 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_resetconfig
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Mark the device as not configured and disable all endpoints.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
void usbmsc_resetconfig(FAR struct usbmsc_dev_s *priv)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
/* Are we configured? */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
if (priv->config != USBMSC_CONFIGIDNONE)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
/* Yes.. but not anymore */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->config = USBMSC_CONFIGIDNONE;
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Disable endpoints. This should force completion of all pending
|
|
|
|
* transfers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
EP_DISABLE(priv->epbulkin);
|
|
|
|
EP_DISABLE(priv->epbulkout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_wrcomplete
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle completion of write request. This function probably executes
|
|
|
|
* in the context of an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
void usbmsc_wrcomplete(FAR struct usbdev_ep_s *ep, FAR struct usbdev_req_s *req)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
FAR struct usbmsc_dev_s *priv;
|
|
|
|
FAR struct usbmsc_req_s *privreq;
|
2011-12-19 15:24:09 -04:00
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!ep || !ep->priv || !req || !req->priv)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRCOMPLETEINVALIDARGS), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract references to private data */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
priv = (FAR struct usbmsc_dev_s*)ep->priv;
|
|
|
|
privreq = (FAR struct usbmsc_req_s *)req->priv;
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Return the write request to the free list */
|
|
|
|
|
|
|
|
flags = irqsave();
|
|
|
|
sq_addlast((sq_entry_t*)privreq, &priv->wrreqlist);
|
|
|
|
irqrestore(flags);
|
|
|
|
|
|
|
|
/* Process the received data unless this is some unusual condition */
|
|
|
|
|
|
|
|
switch (req->result)
|
|
|
|
{
|
|
|
|
case OK: /* Normal completion */
|
|
|
|
usbtrace(TRACE_CLASSWRCOMPLETE, req->xfrd);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case -ESHUTDOWN: /* Disconnection */
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRSHUTDOWN), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* Some other error occurred */
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRUNEXPECTED),
|
2011-12-19 15:24:09 -04:00
|
|
|
(uint16_t)-req->result);
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inform the worker thread that a write request has been returned */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->theventset |= USBMSC_EVENT_WRCOMPLETE;
|
2011-12-19 15:24:09 -04:00
|
|
|
pthread_cond_signal(&priv->cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_rdcomplete
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle completion of read request on the bulk OUT endpoint. This
|
|
|
|
* is handled like the receipt of serial data on the "UART"
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
void usbmsc_rdcomplete(FAR struct usbdev_ep_s *ep, FAR struct usbdev_req_s *req)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
FAR struct usbmsc_dev_s *priv;
|
|
|
|
FAR struct usbmsc_req_s *privreq;
|
2011-12-19 15:24:09 -04:00
|
|
|
irqstate_t flags;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!ep || !ep->priv || !req || !req->priv)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_RDCOMPLETEINVALIDARGS), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract references to private data */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
priv = (FAR struct usbmsc_dev_s*)ep->priv;
|
|
|
|
privreq = (FAR struct usbmsc_req_s *)req->priv;
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Process the received data unless this is some unusual condition */
|
|
|
|
|
|
|
|
switch (req->result)
|
|
|
|
{
|
|
|
|
case 0: /* Normal completion */
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLASSRDCOMPLETE, req->xfrd);
|
|
|
|
|
|
|
|
/* Add the filled read request from the rdreqlist */
|
|
|
|
|
|
|
|
flags = irqsave();
|
|
|
|
sq_addlast((sq_entry_t*)privreq, &priv->rdreqlist);
|
|
|
|
irqrestore(flags);
|
|
|
|
|
|
|
|
/* Signal the worker thread that there is received data to be processed */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->theventset |= USBMSC_EVENT_RDCOMPLETE;
|
2011-12-19 15:24:09 -04:00
|
|
|
pthread_cond_signal(&priv->cond);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case -ESHUTDOWN: /* Disconnection */
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_RDSHUTDOWN), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Drop the read request... it will be cleaned up later */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* Some other error occurred */
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_RDUNEXPECTED),
|
2011-12-19 15:24:09 -04:00
|
|
|
(uint16_t)-req->result);
|
|
|
|
|
|
|
|
/* Return the read request to the bulk out endpoint for re-filling */
|
|
|
|
|
|
|
|
req = privreq->req;
|
|
|
|
req->priv = privreq;
|
2012-01-25 16:17:59 -04:00
|
|
|
req->callback = usbmsc_rdcomplete;
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
ret = EP_SUBMIT(priv->epbulkout, req);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_RDCOMPLETERDSUBMIT),
|
2011-12-19 15:24:09 -04:00
|
|
|
(uint16_t)-ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_deferredresponse
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Some EP0 setup request cannot be responded to immediately becuase they
|
|
|
|
* require some asynchronous action from the SCSI worker thread. This
|
|
|
|
* function is provided for the SCSI thread to make that deferred response.
|
|
|
|
* The specific requests that require this deferred response are:
|
|
|
|
*
|
|
|
|
* 1. USB_REQ_SETCONFIGURATION,
|
|
|
|
* 2. USB_REQ_SETINTERFACE, or
|
2012-01-25 16:17:59 -04:00
|
|
|
* 3. USBMSC_REQ_MSRESET
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* In all cases, the success reponse is a zero-length packet; the failure
|
|
|
|
* response is an EP0 stall.
|
|
|
|
*
|
|
|
|
* Input parameters:
|
|
|
|
* priv - Private state structure for this USB storage instance
|
|
|
|
* stall - true is the action failed and a stall is required
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
void usbmsc_deferredresponse(FAR struct usbmsc_dev_s *priv, bool failed)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
FAR struct usbdev_s *dev;
|
|
|
|
FAR struct usbdev_req_s *ctrlreq;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!priv || !priv->usbdev || !priv->ctrlreq)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_DEFERREDRESPINVALIDARGS), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
dev = priv->usbdev;
|
|
|
|
ctrlreq = priv->ctrlreq;
|
|
|
|
|
|
|
|
/* If no error occurs, respond to the deferred setup command with a null
|
|
|
|
* packet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!failed)
|
|
|
|
{
|
|
|
|
ctrlreq->len = 0;
|
|
|
|
ctrlreq->flags = USBDEV_REQFLAGS_NULLPKT;
|
|
|
|
ret = EP_SUBMIT(dev->ep0, ctrlreq);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_DEFERREDRESPSUBMIT),
|
2011-12-19 15:24:09 -04:00
|
|
|
(uint16_t)-ret);
|
|
|
|
#if 0 /* Not necessary */
|
|
|
|
ctrlreq->result = OK;
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_ep0incomplete(dev->ep0, ctrlreq);
|
2011-12-19 15:24:09 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* On a failure, the USB driver will stall. */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_DEVERROR(USBMSC_TRACEERR_DEFERREDRESPSTALLED), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
EP_STALL(dev->ep0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* User Interfaces
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_configure
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* One-time initialization of the USB storage driver. The initialization
|
|
|
|
* sequence is as follows:
|
|
|
|
*
|
2012-01-25 16:17:59 -04:00
|
|
|
* 1. Call usbmsc_configure to perform one-time initialization specifying
|
2011-12-19 15:24:09 -04:00
|
|
|
* the number of luns.
|
2012-01-25 16:17:59 -04:00
|
|
|
* 2. Call usbmsc_bindlun to configure each supported LUN
|
|
|
|
* 3. Call usbmsc_exportluns when all LUNs are configured
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* nluns - the number of LUNs that will be registered
|
|
|
|
* handle - Location to return a handle that is used in other API calls.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; a negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
int usbmsc_configure(unsigned int nluns, void **handle)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
FAR struct usbmsc_alloc_s *alloc;
|
|
|
|
FAR struct usbmsc_dev_s *priv;
|
|
|
|
FAR struct usbmsc_driver_s *drvr;
|
2011-12-19 15:24:09 -04:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (nluns > 15)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_TOOMANYLUNS), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -EDOM;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Allocate the structures needed */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
alloc = (FAR struct usbmsc_alloc_s*)kmalloc(sizeof(struct usbmsc_alloc_s));
|
2011-12-19 15:24:09 -04:00
|
|
|
if (!alloc)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_ALLOCDEVSTRUCT), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the USB storage driver structure */
|
|
|
|
|
|
|
|
priv = &alloc->dev;
|
2012-01-25 16:17:59 -04:00
|
|
|
memset(priv, 0, sizeof(struct usbmsc_dev_s));
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
pthread_mutex_init(&priv->mutex, NULL);
|
|
|
|
pthread_cond_init(&priv->cond, NULL);
|
|
|
|
sq_init(&priv->wrreqlist);
|
|
|
|
|
|
|
|
priv->nluns = nluns;
|
|
|
|
|
|
|
|
/* Allocate the LUN table */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->luntab = (struct usbmsc_lun_s*)kmalloc(priv->nluns*sizeof(struct usbmsc_lun_s));
|
2011-12-19 15:24:09 -04:00
|
|
|
if (!priv->luntab)
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
2012-01-25 16:17:59 -04:00
|
|
|
memset(priv->luntab, 0, priv->nluns * sizeof(struct usbmsc_lun_s));
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Initialize the USB class driver structure */
|
|
|
|
|
|
|
|
drvr = &alloc->drvr;
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
|
|
|
drvr->drvr.speed = USB_SPEED_HIGH;
|
|
|
|
#else
|
|
|
|
drvr->drvr.speed = USB_SPEED_FULL;
|
|
|
|
#endif
|
|
|
|
drvr->drvr.ops = &g_driverops;
|
|
|
|
drvr->dev = priv;
|
|
|
|
|
|
|
|
/* Return the handle and success */
|
|
|
|
|
|
|
|
*handle = (FAR void*)alloc;
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_uninitialize(alloc);
|
2011-12-19 15:24:09 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_bindlun
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Bind the block driver specified by drvrpath to a USB storage LUN.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2012-01-25 16:17:59 -04:00
|
|
|
* handle - The handle returned by a previous call to usbmsc_configure().
|
2011-12-19 15:24:09 -04:00
|
|
|
* drvrpath - the full path to the block driver
|
|
|
|
* startsector - A sector offset into the block driver to the start of the
|
|
|
|
* partition on drvrpath (0 if no partitions)
|
|
|
|
* nsectors - The number of sectors in the partition (if 0, all sectors
|
|
|
|
* to the end of the media will be exported).
|
|
|
|
* lunno - the LUN to bind to
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; a negated errno on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
int usbmsc_bindlun(FAR void *handle, FAR const char *drvrpath,
|
|
|
|
unsigned int lunno, off_t startsector, size_t nsectors,
|
|
|
|
bool readonly)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
FAR struct usbmsc_alloc_s *alloc = (FAR struct usbmsc_alloc_s *)handle;
|
|
|
|
FAR struct usbmsc_dev_s *priv;
|
|
|
|
FAR struct usbmsc_lun_s *lun;
|
2011-12-19 15:24:09 -04:00
|
|
|
FAR struct inode *inode;
|
|
|
|
struct geometry geo;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!alloc || !drvrpath || startsector < 0 || nsectors < 0)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_BINLUNINVALIDARGS1), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
priv = &alloc->dev;
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!priv->luntab)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_INTERNALCONFUSION1), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lunno > priv->nluns)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_BINDLUNINVALIDARGS2), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
lun = &priv->luntab[lunno];
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (lun->inode != NULL)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_LUNALREADYBOUND), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Open the block driver */
|
|
|
|
|
|
|
|
ret = open_blockdriver(drvrpath, 0, &inode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_BLKDRVEOPEN), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the drive geometry */
|
|
|
|
|
|
|
|
if (!inode || !inode->u.i_bops || !inode->u.i_bops->geometry ||
|
|
|
|
inode->u.i_bops->geometry(inode, &geo) != OK || !geo.geo_available)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_NOGEOMETRY), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that the partition parameters are valid */
|
|
|
|
|
|
|
|
if (startsector >= geo.geo_nsectors)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_BINDLUNINVALIDARGS3), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -EDOM;
|
|
|
|
}
|
|
|
|
else if (nsectors == 0)
|
|
|
|
{
|
|
|
|
nsectors = geo.geo_nsectors - startsector;
|
|
|
|
}
|
|
|
|
else if (startsector + nsectors >= geo.geo_nsectors)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_BINDLUNINVALIDARGS4), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -EDOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the LUN structure */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
memset(lun, 0, sizeof(struct usbmsc_lun_s *));
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Allocate an I/O buffer big enough to hold one hardware sector. SCSI commands
|
|
|
|
* are processed one at a time so all LUNs may share a single I/O buffer. The
|
|
|
|
* I/O buffer will be allocated so that is it as large as the largest block
|
|
|
|
* device sector size
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!priv->iobuffer)
|
|
|
|
{
|
|
|
|
priv->iobuffer = (uint8_t*)kmalloc(geo.geo_sectorsize);
|
|
|
|
if (!priv->iobuffer)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_ALLOCIOBUFFER), geo.geo_sectorsize);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
priv->iosize = geo.geo_sectorsize;
|
|
|
|
}
|
|
|
|
else if (priv->iosize < geo.geo_sectorsize)
|
|
|
|
{
|
|
|
|
void *tmp;
|
|
|
|
tmp = (uint8_t*)realloc(priv->iobuffer, geo.geo_sectorsize);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_REALLOCIOBUFFER), geo.geo_sectorsize);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->iobuffer = (uint8_t*)tmp;
|
|
|
|
priv->iosize = geo.geo_sectorsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
lun->inode = inode;
|
|
|
|
lun->startsector = startsector;
|
|
|
|
lun->nsectors = nsectors;
|
|
|
|
lun->sectorsize = geo.geo_sectorsize;
|
|
|
|
|
|
|
|
/* If the driver does not support the write method, then this is read-only */
|
|
|
|
|
|
|
|
if (!inode->u.i_bops->write)
|
|
|
|
{
|
|
|
|
lun->readonly = true;
|
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_unbindlun
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Un-bind the block driver for the specified LUN
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2012-01-25 16:17:59 -04:00
|
|
|
* handle - The handle returned by a previous call to usbmsc_configure().
|
2011-12-19 15:24:09 -04:00
|
|
|
* lun - the LUN to unbind from
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; a negated errno on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
int usbmsc_unbindlun(FAR void *handle, unsigned int lunno)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
FAR struct usbmsc_alloc_s *alloc = (FAR struct usbmsc_alloc_s *)handle;
|
|
|
|
FAR struct usbmsc_dev_s *priv;
|
|
|
|
FAR struct usbmsc_lun_s *lun;
|
2011-12-19 15:24:09 -04:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!alloc)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_UNBINDLUNINVALIDARGS1), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
priv = &alloc->dev;
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!priv->luntab)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_INTERNALCONFUSION2), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lunno > priv->nluns)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_UNBINDLUNINVALIDARGS2), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
lun = &priv->luntab[lunno];
|
|
|
|
pthread_mutex_lock(&priv->mutex);
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (lun->inode == NULL)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_LUNNOTBOUND), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
ret = -EBUSY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Close the block driver */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_lununinitialize(lun);
|
2011-12-19 15:24:09 -04:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&priv->mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_exportluns
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* After all of the LUNs have been bound, this function may be called
|
|
|
|
* in order to export those LUNs in the USB storage device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2012-01-25 16:17:59 -04:00
|
|
|
* handle - The handle returned by a previous call to usbmsc_configure().
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; a negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-26 19:14:27 -04:00
|
|
|
#ifndef CONFIG_USBMSC_COMPOSITE
|
|
|
|
static
|
|
|
|
#endif
|
2012-01-25 16:17:59 -04:00
|
|
|
int usbmsc_exportluns(FAR void *handle)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
FAR struct usbmsc_alloc_s *alloc = (FAR struct usbmsc_alloc_s *)handle;
|
|
|
|
FAR struct usbmsc_dev_s *priv;
|
|
|
|
FAR struct usbmsc_driver_s *drvr;
|
2011-12-19 15:24:09 -04:00
|
|
|
irqstate_t flags;
|
|
|
|
#ifdef SDCC
|
|
|
|
pthread_attr_t attr;
|
|
|
|
#endif
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!alloc)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EXPORTLUNSINVALIDARGS), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
priv = &alloc->dev;
|
|
|
|
drvr = &alloc->drvr;
|
|
|
|
|
|
|
|
/* Start the worker thread */
|
|
|
|
|
|
|
|
pthread_mutex_lock(&priv->mutex);
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->thstate = USBMSC_STATE_NOTSTARTED;
|
|
|
|
priv->theventset = USBMSC_EVENT_NOEVENTS;
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
#ifdef SDCC
|
|
|
|
(void)pthread_attr_init(&attr);
|
2012-01-25 16:17:59 -04:00
|
|
|
ret = pthread_create(&priv->thread, &attr, usbmsc_workerthread, (pthread_addr_t)priv);
|
2011-12-19 15:24:09 -04:00
|
|
|
#else
|
2012-01-25 16:17:59 -04:00
|
|
|
ret = pthread_create(&priv->thread, NULL, usbmsc_workerthread, (pthread_addr_t)priv);
|
2011-12-19 15:24:09 -04:00
|
|
|
#endif
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_THREADCREATE), (uint16_t)-ret);
|
2011-12-19 15:24:09 -04:00
|
|
|
goto errout_with_mutex;
|
|
|
|
}
|
|
|
|
|
2012-01-25 15:27:20 -04:00
|
|
|
/* Register the USB storage class driver (unless we are part of a composite device) */
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-01-25 19:04:17 -04:00
|
|
|
#ifndef CONFIG_USBMSC_COMPOSITE
|
2011-12-19 15:24:09 -04:00
|
|
|
ret = usbdev_register(&drvr->drvr);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_DEVREGISTER), (uint16_t)-ret);
|
2011-12-19 15:24:09 -04:00
|
|
|
goto errout_with_mutex;
|
|
|
|
}
|
2012-01-25 15:27:20 -04:00
|
|
|
#endif
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Signal to start the thread */
|
|
|
|
|
|
|
|
flags = irqsave();
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->theventset |= USBMSC_EVENT_READY;
|
2011-12-19 15:24:09 -04:00
|
|
|
pthread_cond_signal(&priv->cond);
|
|
|
|
irqrestore(flags);
|
|
|
|
|
|
|
|
errout_with_mutex:
|
|
|
|
pthread_mutex_unlock(&priv->mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-26 19:14:27 -04:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbmsc_classobject
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register USB mass storage device and return the class object.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* classdev - The location to return the CDC serial class' device
|
|
|
|
* instance.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; a negated errno on failure
|
|
|
|
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBMSC_COMPOSITE
|
|
|
|
int usbmsc_classobject(FAR void *handle,
|
|
|
|
FAR struct usbdevclass_driver_s **classdev)
|
|
|
|
{
|
2012-01-27 12:25:57 -04:00
|
|
|
FAR struct usbmsc_alloc_s *alloc = (FAR struct usbmsc_alloc_s *)handle;
|
2012-01-26 19:14:27 -04:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(handle && classdev);
|
|
|
|
|
|
|
|
/* Export the LUNs as with the "standalone" USB mass storage driver, but
|
|
|
|
* don't register the class instance with the USB device infrastructure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = usbmsc_exportluns(handle);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* On sucess, return an (typed) instance of the class instance */
|
|
|
|
|
2012-01-27 12:25:57 -04:00
|
|
|
*classdev = &alloc->drvr.drvr;
|
2012-01-26 19:14:27 -04:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-12-19 15:24:09 -04:00
|
|
|
/****************************************************************************
|
2012-01-25 16:17:59 -04:00
|
|
|
* Name: usbmsc_uninitialize
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Un-initialize the USB storage class driver
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2012-01-25 16:17:59 -04:00
|
|
|
* handle - The handle returned by a previous call to usbmsc_configure().
|
2011-12-19 15:24:09 -04:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
void usbmsc_uninitialize(FAR void *handle)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
FAR struct usbmsc_alloc_s *alloc = (FAR struct usbmsc_alloc_s *)handle;
|
|
|
|
FAR struct usbmsc_dev_s *priv;
|
2011-12-19 15:24:09 -04:00
|
|
|
irqstate_t flags;
|
|
|
|
#ifdef SDCC
|
|
|
|
pthread_addr_t result1, result2;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
#endif
|
|
|
|
void *value;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!handle)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_UNINITIALIZEINVALIDARGS), 0);
|
2011-12-19 15:24:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
priv = &alloc->dev;
|
|
|
|
|
|
|
|
/* If the thread hasn't already exitted, tell it to exit now */
|
|
|
|
|
2012-01-25 16:17:59 -04:00
|
|
|
if (priv->thstate != USBMSC_STATE_NOTSTARTED)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
/* The thread was started.. Is it still running? */
|
|
|
|
|
|
|
|
pthread_mutex_lock(&priv->mutex);
|
2012-01-25 16:17:59 -04:00
|
|
|
if (priv->thstate != USBMSC_STATE_TERMINATED)
|
2011-12-19 15:24:09 -04:00
|
|
|
{
|
|
|
|
/* Yes.. Ask the thread to stop */
|
|
|
|
|
|
|
|
flags = irqsave();
|
2012-01-25 16:17:59 -04:00
|
|
|
priv->theventset |= USBMSC_EVENT_TERMINATEREQUEST;
|
2011-12-19 15:24:09 -04:00
|
|
|
pthread_cond_signal(&priv->cond);
|
|
|
|
irqrestore(flags);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&priv->mutex);
|
|
|
|
|
|
|
|
/* Wait for the thread to exit. This is necessary even if the
|
|
|
|
* thread has already exitted in order to collect the join
|
|
|
|
* garbage
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = pthread_join(priv->thread, &value);
|
|
|
|
}
|
|
|
|
priv->thread = 0;
|
|
|
|
|
2012-01-25 15:27:20 -04:00
|
|
|
/* Unregister the driver (unless we are a part of a composite device */
|
2011-12-19 15:24:09 -04:00
|
|
|
|
2012-01-25 19:04:17 -04:00
|
|
|
#ifndef CONFIG_USBMSC_COMPOSITE
|
2011-12-19 15:24:09 -04:00
|
|
|
usbdev_unregister(&alloc->drvr.drvr);
|
2012-01-25 15:27:20 -04:00
|
|
|
#endif
|
2011-12-19 15:24:09 -04:00
|
|
|
|
|
|
|
/* Uninitialize and release the LUNs */
|
|
|
|
|
|
|
|
for (i = 0; i < priv->nluns; ++i)
|
|
|
|
{
|
2012-01-25 16:17:59 -04:00
|
|
|
usbmsc_lununinitialize(&priv->luntab[i]);
|
2011-12-19 15:24:09 -04:00
|
|
|
}
|
|
|
|
kfree(priv->luntab);
|
|
|
|
|
|
|
|
/* Release the I/O buffer */
|
|
|
|
|
|
|
|
if (priv->iobuffer)
|
|
|
|
{
|
|
|
|
kfree(priv->iobuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Uninitialize and release the driver structure */
|
|
|
|
|
|
|
|
pthread_mutex_destroy(&priv->mutex);
|
|
|
|
pthread_cond_destroy(&priv->cond);
|
|
|
|
|
|
|
|
kfree(priv);
|
|
|
|
}
|