-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathsam_automount.c
330 lines (273 loc) · 9.95 KB
/
sam_automount.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/****************************************************************************
* boards/arm/samv7/common/src/sam_automount.c
*
* 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
#include <assert.h>
#include <debug.h>
#include <nuttx/irq.h>
#include <nuttx/clock.h>
#include <nuttx/fs/automount.h>
#include "board_hsmci.h"
#include "sam_automount.h"
/****************************************************************************
* 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 */
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 */
struct sam_automount_state_s *state; /* Changeable state */
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int sam_attach(const struct automount_lower_s *lower,
automount_handler_t isr, void *arg);
static void sam_enable(const struct automount_lower_s *lower,
bool enable);
static bool sam_inserted(const struct automount_lower_s *lower);
/****************************************************************************
* Private Data
****************************************************************************/
#ifdef CONFIG_SAMV7_HSMCI0_AUTOMOUNT
static struct sam_automount_state_s g_hsmci0state;
static const struct sam_automount_config_s g_hsmci0config =
{
.lower =
{
.fstype = CONFIG_SAMV7_HSMCI0_AUTOMOUNT_FSTYPE,
.blockdev = CONFIG_SAMV7_HSMCI0_AUTOMOUNT_BLKDEV,
.mountpoint = CONFIG_SAMV7_HSMCI0_AUTOMOUNT_MOUNTPOINT,
.ddelay = MSEC2TICK_SLOW(CONFIG_SAMV7_HSMCI0_AUTOMOUNT_DDELAY),
.udelay = MSEC2TICK_SLOW(CONFIG_SAMV7_HSMCI0_AUTOMOUNT_UDELAY),
.attach = sam_attach,
.enable = sam_enable,
.inserted = sam_inserted
},
.hsmci = 0,
.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(const struct automount_lower_s *lower,
automount_handler_t isr, void *arg)
{
const struct sam_automount_config_s *config;
struct sam_automount_state_s *state;
/* Recover references to our structure */
config = (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
*
****************************************************************************/
static void sam_enable(const struct automount_lower_s *lower,
bool enable)
{
const struct sam_automount_config_s *config;
struct sam_automount_state_s *state;
irqstate_t flags;
/* Recover references to our structure */
config = (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)
{
/* Yes.. perform the fake interrupt if the interrupt is attached */
if (state->handler)
{
bool inserted = sam_cardinserted(config->hsmci);
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(const struct automount_lower_s *lower)
{
const struct sam_automount_config_s *config;
config = (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)
{
void *handle;
finfo("Initializing automounter(s)\n");
#ifdef CONFIG_SAMV7_HSMCI0_AUTOMOUNT
/* Initialize the HSMCI0 auto-mounter */
handle = automount_initialize(&g_hsmci0config.lower);
if (handle == NULL)
{
ferr("ERROR: Failed to initialize auto-mounter for HSMCI0\n");
}
#endif /* CONFIG_SAMV7_HSMCI0_AUTOMOUNT */
}
/****************************************************************************
* 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)
{
const struct sam_automount_config_s *config;
struct sam_automount_state_s *state;
#ifdef CONFIG_SAMV7_HSMCI0_AUTOMOUNT
/* Is this a change in the HSMCI0 insertion state? */
if (g_hsmci0config.hsmci == slotno)
{
/* 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 */
state->handler(&config->lower, state->arg, inserted);
}
}
}