2020-02-18 14:51:04 +01:00
|
|
|
/****************************************************************************
|
2021-12-31 01:39:28 +01:00
|
|
|
* boards/arm/samv7/common/src/sam_automount.c
|
2020-02-18 14:51:04 +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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_FS_AUTOMOUNTER_DEBUG) && !defined(CONFIG_DEBUG_FS)
|
|
|
|
# define CONFIG_DEBUG_FS 1
|
|
|
|
#endif
|
|
|
|
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2020-02-18 14:51:04 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <nuttx/clock.h>
|
|
|
|
#include <nuttx/fs/automount.h>
|
|
|
|
|
2021-12-31 01:39:28 +01:00
|
|
|
#include "sam_automount.h"
|
2020-02-18 14:51:04 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure represents the changeable state of the automounter */
|
|
|
|
|
|
|
|
struct sam_automount_state_s
|
|
|
|
{
|
|
|
|
volatile automount_handler_t handler; /* Upper half handler */
|
|
|
|
FAR void *arg; /* Handler argument */
|
|
|
|
bool enable; /* Fake interrupt enable */
|
|
|
|
bool pending; /* Set if there an event while disabled */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This structure represents the static configuration of an automounter */
|
|
|
|
|
|
|
|
struct sam_automount_config_s
|
|
|
|
{
|
|
|
|
/* This must be first thing in structure so that we can simply cast from
|
|
|
|
* struct automount_lower_s to struct sam_automount_config_s
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct automount_lower_s lower; /* Publicly visible part */
|
|
|
|
uint8_t hsmci; /* HSMCI0_SLOTNO or HSMCI1_SLOTNO */
|
|
|
|
FAR struct sam_automount_state_s *state; /* Changeable state */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int sam_attach(FAR const struct automount_lower_s *lower,
|
|
|
|
automount_handler_t isr, FAR void *arg);
|
|
|
|
static void sam_enable(FAR const struct automount_lower_s *lower,
|
|
|
|
bool enable);
|
|
|
|
static bool sam_inserted(FAR const struct automount_lower_s *lower);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-12-31 01:39:28 +01:00
|
|
|
#ifdef CONFIG_SAMV7_HSMCI0_AUTOMOUNT
|
2020-02-18 14:51:04 +01:00
|
|
|
static struct sam_automount_state_s g_hsmci0state;
|
|
|
|
static const struct sam_automount_config_s g_hsmci0config =
|
|
|
|
{
|
|
|
|
.lower =
|
|
|
|
{
|
2021-12-31 01:39:28 +01:00
|
|
|
.fstype = CONFIG_SAMV7_HSMCI0_AUTOMOUNT_FSTYPE,
|
|
|
|
.blockdev = CONFIG_SAMV7_HSMCI0_AUTOMOUNT_BLKDEV,
|
|
|
|
.mountpoint = CONFIG_SAMV7_HSMCI0_AUTOMOUNT_MOUNTPOINT,
|
|
|
|
.ddelay = MSEC2TICK(CONFIG_SAMV7_HSMCI0_AUTOMOUNT_DDELAY),
|
|
|
|
.udelay = MSEC2TICK(CONFIG_SAMV7_HSMCI0_AUTOMOUNT_UDELAY),
|
2020-02-18 14:51:04 +01:00
|
|
|
.attach = sam_attach,
|
|
|
|
.enable = sam_enable,
|
|
|
|
.inserted = sam_inserted
|
|
|
|
},
|
2022-01-19 10:30:26 +01:00
|
|
|
.hsmci = 0,
|
2020-02-18 14:51:04 +01:00
|
|
|
.state = &g_hsmci0state
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sam_attach
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Attach a new HSMCI event handler
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* lower - An instance of the auto-mounter lower half state structure
|
|
|
|
* isr - The new event handler to be attach
|
|
|
|
* arg - Client data to be provided when the event handler is invoked.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Always returns OK
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int sam_attach(FAR const struct automount_lower_s *lower,
|
|
|
|
automount_handler_t isr, FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR const struct sam_automount_config_s *config;
|
|
|
|
FAR struct sam_automount_state_s *state;
|
|
|
|
|
|
|
|
/* Recover references to our structure */
|
|
|
|
|
|
|
|
config = (FAR struct sam_automount_config_s *)lower;
|
|
|
|
DEBUGASSERT(config && config->state);
|
|
|
|
|
|
|
|
state = config->state;
|
|
|
|
|
|
|
|
/* Save the new handler info (clearing the handler first to eliminate race
|
|
|
|
* conditions).
|
|
|
|
*/
|
|
|
|
|
|
|
|
state->handler = NULL;
|
|
|
|
state->pending = false;
|
|
|
|
state->arg = arg;
|
|
|
|
state->handler = isr;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sam_enable
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Enable card insertion/removal event detection
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* lower - An instance of the auto-mounter lower half state structure
|
|
|
|
* enable - True: enable event detection; False: disable
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-06 12:13:09 +02:00
|
|
|
static void sam_enable(FAR const struct automount_lower_s *lower,
|
|
|
|
bool enable)
|
2020-02-18 14:51:04 +01:00
|
|
|
{
|
|
|
|
FAR const struct sam_automount_config_s *config;
|
|
|
|
FAR struct sam_automount_state_s *state;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* Recover references to our structure */
|
|
|
|
|
|
|
|
config = (FAR struct sam_automount_config_s *)lower;
|
|
|
|
DEBUGASSERT(config && config->state);
|
|
|
|
|
|
|
|
state = config->state;
|
|
|
|
|
|
|
|
/* Save the fake enable setting */
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
state->enable = enable;
|
|
|
|
|
|
|
|
/* Did an interrupt occur while interrupts were disabled? */
|
|
|
|
|
|
|
|
if (enable && state->pending)
|
|
|
|
{
|
2022-01-03 13:50:04 +01:00
|
|
|
/* Yes.. perform the fake interrupt if the interrupt is attached */
|
2020-02-18 14:51:04 +01:00
|
|
|
|
|
|
|
if (state->handler)
|
|
|
|
{
|
|
|
|
bool inserted = sam_cardinserted(config->hsmci);
|
|
|
|
(void)state->handler(&config->lower, state->arg, inserted);
|
|
|
|
}
|
|
|
|
|
|
|
|
state->pending = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sam_inserted
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if a card is inserted into the slot.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* lower - An instance of the auto-mounter lower half state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* True if the card is inserted; False otherwise
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool sam_inserted(FAR const struct automount_lower_s *lower)
|
|
|
|
{
|
|
|
|
FAR const struct sam_automount_config_s *config;
|
|
|
|
|
|
|
|
config = (FAR struct sam_automount_config_s *)lower;
|
|
|
|
DEBUGASSERT(config && config->state);
|
|
|
|
|
|
|
|
return sam_cardinserted(config->hsmci);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sam_automount_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure auto-mounters for each enable and so configured HSMCI
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void sam_automount_initialize(void)
|
|
|
|
{
|
|
|
|
FAR void *handle;
|
|
|
|
|
|
|
|
finfo("Initializing automounter(s)\n");
|
|
|
|
|
2021-12-31 01:39:28 +01:00
|
|
|
#ifdef CONFIG_SAMV7_HSMCI0_AUTOMOUNT
|
2020-02-18 14:51:04 +01:00
|
|
|
/* Initialize the HSMCI0 auto-mounter */
|
|
|
|
|
|
|
|
handle = automount_initialize(&g_hsmci0config.lower);
|
2022-01-19 10:30:26 +01:00
|
|
|
if (handle == NULL)
|
2020-02-18 14:51:04 +01:00
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to initialize auto-mounter for HSMCI0\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sam_automount_event
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The HSMCI card detection logic has detected an insertion or removal
|
|
|
|
* event.
|
|
|
|
* It has already scheduled the MMC/SD block driver operations.
|
|
|
|
* Now we need to schedule the auto-mount event which will occur with a
|
|
|
|
* substantial delay to make sure that everything has settle down.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* slotno - Identifies the HSMCI0 slot: HSMCI0_SLOTNO or HSMCI1_SLOTNO.
|
|
|
|
* There is a terminology problem here: Each HSMCI supports two slots,
|
|
|
|
* slot A and slot B.
|
|
|
|
* Only slot A is used. So this is not a really a slot, but an HSCMI
|
|
|
|
* peripheral number.
|
|
|
|
* inserted - True if the card is inserted in the slot. False otherwise.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Interrupts are disabled.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void sam_automount_event(int slotno, bool inserted)
|
|
|
|
{
|
|
|
|
FAR const struct sam_automount_config_s *config;
|
|
|
|
FAR struct sam_automount_state_s *state;
|
|
|
|
|
2021-12-31 01:39:28 +01:00
|
|
|
#ifdef CONFIG_SAMV7_HSMCI0_AUTOMOUNT
|
2020-02-18 14:51:04 +01:00
|
|
|
/* Is this a change in the HSMCI0 insertion state? */
|
|
|
|
|
2022-01-19 10:30:26 +01:00
|
|
|
if (g_hsmci0config.hsmci == slotno)
|
2020-02-18 14:51:04 +01:00
|
|
|
{
|
|
|
|
/* Yes.. Select the HSMCI0 automounter */
|
|
|
|
|
|
|
|
config = &g_hsmci0config;
|
|
|
|
state = &g_hsmci0state;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ferr("ERROR: Unsupported HSCMI%d\n", slotno);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the auto-mounter interrupt attached? */
|
|
|
|
|
|
|
|
if (state->handler)
|
|
|
|
{
|
|
|
|
/* Yes.. Have we been asked to hold off interrupts? */
|
|
|
|
|
|
|
|
if (!state->enable)
|
|
|
|
{
|
|
|
|
/* Yes.. just remember that there is a pending interrupt. We will
|
|
|
|
* deliver the interrupt when interrupts are "re-enabled."
|
|
|
|
*/
|
|
|
|
|
|
|
|
state->pending = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. forward the event to the handler */
|
|
|
|
|
|
|
|
(void)state->handler(&config->lower, state->arg, inserted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|