2019-08-12 18:06:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* boards/arm/kinetis/twr-k64f120m/src/k64_automount.c
|
2017-02-19 21:20:56 +01:00
|
|
|
*
|
2021-03-17 18:14:12 +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
|
2017-02-19 21:20:56 +01:00
|
|
|
*
|
2021-03-17 18:14:12 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-02-19 21:20:56 +01:00
|
|
|
*
|
2021-03-17 18:14:12 +01:00
|
|
|
* 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.
|
2017-02-19 21:20:56 +01:00
|
|
|
*
|
2019-08-12 18:06:40 +02:00
|
|
|
****************************************************************************/
|
2017-02-19 21:20:56 +01:00
|
|
|
|
2019-08-12 18:06:40 +02:00
|
|
|
/****************************************************************************
|
2017-02-19 21:20:56 +01:00
|
|
|
* Included Files
|
2019-08-12 18:06:40 +02:00
|
|
|
****************************************************************************/
|
2017-02-19 21:20:56 +01:00
|
|
|
|
|
|
|
#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>
|
2017-02-19 21:20:56 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <nuttx/clock.h>
|
|
|
|
#include <nuttx/fs/automount.h>
|
|
|
|
|
|
|
|
#include "twrk64.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_AUTOMOUNTER
|
|
|
|
|
2019-08-12 18:06:40 +02:00
|
|
|
/****************************************************************************
|
2017-02-19 21:20:56 +01:00
|
|
|
* Private Types
|
2019-08-12 18:06:40 +02:00
|
|
|
****************************************************************************/
|
2017-02-19 21:58:37 +01:00
|
|
|
|
2017-02-19 21:20:56 +01:00
|
|
|
/* This structure represents the changeable state of the automounter */
|
|
|
|
|
|
|
|
struct k64_automount_state_s
|
|
|
|
{
|
2022-04-17 08:01:48 +02:00
|
|
|
volatile automount_handler_t handler; /* Upper half handler */
|
|
|
|
void *arg; /* Handler argument */
|
|
|
|
bool enable; /* Fake interrupt enable */
|
|
|
|
bool pending; /* Set if there an event while disabled */
|
2017-02-19 21:20:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This structure represents the static configuration of an automounter */
|
|
|
|
|
|
|
|
struct k64_automount_config_s
|
|
|
|
{
|
2019-08-12 18:06:40 +02:00
|
|
|
/* This must be first thing in structure so that we can simply cast from
|
|
|
|
* struct automount_lower_s to struct k64_automount_config_s
|
2017-02-19 21:20:56 +01:00
|
|
|
*/
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
struct automount_lower_s lower; /* Publicly visible part */
|
|
|
|
struct k64_automount_state_s *state; /* Changeable state */
|
2017-02-19 21:20:56 +01:00
|
|
|
};
|
|
|
|
|
2019-08-12 18:06:40 +02:00
|
|
|
/****************************************************************************
|
2017-02-19 21:20:56 +01:00
|
|
|
* Private Function Prototypes
|
2019-08-12 18:06:40 +02:00
|
|
|
****************************************************************************/
|
2017-02-19 21:20:56 +01:00
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
static int k64_attach(const struct automount_lower_s *lower,
|
|
|
|
automount_handler_t isr, void *arg);
|
|
|
|
static void k64_enable(const struct automount_lower_s *lower,
|
2019-08-12 18:06:40 +02:00
|
|
|
bool enable);
|
2022-04-17 08:01:48 +02:00
|
|
|
static bool k64_inserted(const struct automount_lower_s *lower);
|
2017-02-19 21:20:56 +01:00
|
|
|
|
2019-08-12 18:06:40 +02:00
|
|
|
/****************************************************************************
|
2017-02-19 21:20:56 +01:00
|
|
|
* Private Data
|
2019-08-12 18:06:40 +02:00
|
|
|
****************************************************************************/
|
2017-02-19 21:20:56 +01:00
|
|
|
|
|
|
|
static struct k64_automount_state_s g_sdhc_state;
|
|
|
|
static const struct k64_automount_config_s g_sdhc_config =
|
|
|
|
{
|
|
|
|
.lower =
|
|
|
|
{
|
|
|
|
.fstype = CONFIG_TWR_K64F120M_SDHC_AUTOMOUNT_FSTYPE,
|
|
|
|
.blockdev = CONFIG_TWR_K64F120M_SDHC_AUTOMOUNT_BLKDEV,
|
|
|
|
.mountpoint = CONFIG_TWR_K64F120M_SDHC_AUTOMOUNT_MOUNTPOINT,
|
|
|
|
.ddelay = MSEC2TICK(CONFIG_TWR_K64F120M_SDHC_AUTOMOUNT_DDELAY),
|
|
|
|
.udelay = MSEC2TICK(CONFIG_TWR_K64F120M_SDHC_AUTOMOUNT_UDELAY),
|
|
|
|
.attach = k64_attach,
|
|
|
|
.enable = k64_enable,
|
|
|
|
.inserted = k64_inserted
|
|
|
|
},
|
|
|
|
.state = &g_sdhc_state
|
|
|
|
};
|
|
|
|
|
2019-08-12 18:06:40 +02:00
|
|
|
/****************************************************************************
|
2017-02-19 21:20:56 +01:00
|
|
|
* Private Functions
|
2019-08-12 18:06:40 +02:00
|
|
|
****************************************************************************/
|
2017-02-19 21:20:56 +01:00
|
|
|
|
2019-08-12 18:06:40 +02:00
|
|
|
/****************************************************************************
|
2017-02-19 21:20:56 +01:00
|
|
|
* Name: k64_attach
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Attach a new SDHC 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
|
|
|
|
*
|
2019-08-12 18:06:40 +02:00
|
|
|
****************************************************************************/
|
2017-02-19 21:20:56 +01:00
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
static int k64_attach(const struct automount_lower_s *lower,
|
|
|
|
automount_handler_t isr, void *arg)
|
2017-02-19 21:20:56 +01:00
|
|
|
{
|
2022-04-17 08:01:48 +02:00
|
|
|
const struct k64_automount_config_s *config;
|
|
|
|
struct k64_automount_state_s *state;
|
2017-02-19 21:20:56 +01:00
|
|
|
|
|
|
|
/* Recover references to our structure */
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
config = (struct k64_automount_config_s *)lower;
|
2017-02-19 21:20:56 +01:00
|
|
|
DEBUGASSERT(config != NULL && config->state != NULL);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:06:40 +02:00
|
|
|
/****************************************************************************
|
2017-02-19 21:20:56 +01:00
|
|
|
* Name: k64_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
|
|
|
|
*
|
2019-08-12 18:06:40 +02:00
|
|
|
****************************************************************************/
|
2017-02-19 21:20:56 +01:00
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
static void k64_enable(const struct automount_lower_s *lower,
|
2021-03-18 09:57:48 +01:00
|
|
|
bool enable)
|
2017-02-19 21:20:56 +01:00
|
|
|
{
|
2022-04-17 08:01:48 +02:00
|
|
|
const struct k64_automount_config_s *config;
|
|
|
|
struct k64_automount_state_s *state;
|
2017-02-19 21:20:56 +01:00
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* Recover references to our structure */
|
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
config = (struct k64_automount_config_s *)lower;
|
2017-02-19 21:20:56 +01:00
|
|
|
DEBUGASSERT(config != NULL && config->state != NULL);
|
|
|
|
|
|
|
|
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 */
|
2017-02-19 21:20:56 +01:00
|
|
|
|
|
|
|
if (state->handler)
|
|
|
|
{
|
|
|
|
bool inserted = k64_cardinserted();
|
2020-01-02 17:49:34 +01:00
|
|
|
state->handler(&config->lower, state->arg, inserted);
|
2017-02-19 21:20:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
state->pending = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:06:40 +02:00
|
|
|
/****************************************************************************
|
2017-02-19 21:20:56 +01:00
|
|
|
* Name: k64_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
|
|
|
|
*
|
2019-08-12 18:06:40 +02:00
|
|
|
****************************************************************************/
|
2017-02-19 21:20:56 +01:00
|
|
|
|
2022-04-17 08:01:48 +02:00
|
|
|
static bool k64_inserted(const struct automount_lower_s *lower)
|
2017-02-19 21:20:56 +01:00
|
|
|
{
|
|
|
|
return k64_cardinserted();
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:06:40 +02:00
|
|
|
/****************************************************************************
|
2017-02-19 21:20:56 +01:00
|
|
|
* Public Functions
|
2019-08-12 18:06:40 +02:00
|
|
|
****************************************************************************/
|
2017-02-19 21:20:56 +01:00
|
|
|
|
2019-08-12 18:06:40 +02:00
|
|
|
/****************************************************************************
|
2017-02-19 21:20:56 +01:00
|
|
|
* Name: k64_automount_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure auto-mounters for each enable and so configured SDHC
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2019-08-12 18:06:40 +02:00
|
|
|
****************************************************************************/
|
2017-02-19 21:20:56 +01:00
|
|
|
|
|
|
|
void k64_automount_initialize(void)
|
|
|
|
{
|
2022-04-17 08:01:48 +02:00
|
|
|
void *handle;
|
2017-02-19 21:20:56 +01:00
|
|
|
|
|
|
|
finfo("Initializing automounter(s)\n");
|
|
|
|
|
|
|
|
/* Initialize the SDHC0 auto-mounter */
|
|
|
|
|
|
|
|
handle = automount_initialize(&g_sdhc_config.lower);
|
|
|
|
if (!handle)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to initialize auto-mounter for SDHC0\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:06:40 +02:00
|
|
|
/****************************************************************************
|
2017-02-19 21:20:56 +01:00
|
|
|
* Name: k64_automount_event
|
|
|
|
*
|
|
|
|
* Description:
|
2021-03-18 09:57:48 +01:00
|
|
|
* The SDHC card detection logic has detected an insertion or removal
|
|
|
|
* event.
|
2019-08-12 18:06:40 +02:00
|
|
|
* 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.
|
2017-02-19 21:20:56 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2019-08-12 18:06:40 +02:00
|
|
|
* slotno - Identifies the SDHC0 slot: SDHC0_SLOTNO or SDHC1_SLOTNO.
|
|
|
|
* There is a terminology problem here: Each SDHC 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.
|
2017-02-19 21:20:56 +01:00
|
|
|
* inserted - True if the card is inserted in the slot. False otherwise.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Interrupts are disabled.
|
|
|
|
*
|
2019-08-12 18:06:40 +02:00
|
|
|
****************************************************************************/
|
2017-02-19 21:20:56 +01:00
|
|
|
|
|
|
|
void k64_automount_event(bool inserted)
|
|
|
|
{
|
2022-04-17 08:01:48 +02:00
|
|
|
const struct k64_automount_config_s *config = &g_sdhc_config;
|
|
|
|
struct k64_automount_state_s *state = &g_sdhc_state;
|
2017-02-19 21:20:56 +01:00
|
|
|
|
|
|
|
/* Is the auto-mounter interrupt attached? */
|
|
|
|
|
|
|
|
if (state->handler)
|
|
|
|
{
|
|
|
|
/* Yes.. Have we been asked to hold off interrupts? */
|
|
|
|
|
|
|
|
if (!state->enable)
|
|
|
|
{
|
2017-05-11 21:35:56 +02:00
|
|
|
/* Yes.. just remember that there is a pending interrupt. We will
|
2017-02-19 21:20:56 +01:00
|
|
|
* deliver the interrupt when interrupts are "re-enabled."
|
|
|
|
*/
|
|
|
|
|
|
|
|
state->pending = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. forward the event to the handler */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
state->handler(&config->lower, state->arg, inserted);
|
2017-02-19 21:20:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_AUTOMOUNTER */
|