summaryrefslogtreecommitdiffstats
path: root/cpukit/include/rtems/ramdisk.h
blob: dcc60483ddf69f8ab2986b9b9e25c9f6d66c3445 (plain) (blame)
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
/**
 * @file
 *
 * @brief RAM Disk Block Device API
 */

/*
 * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
 * Author: Victor V. Vengerov <vvv@oktet.ru>
 */

#ifndef _RTEMS_RAMDISK_H
#define _RTEMS_RAMDISK_H


#include <rtems.h>
#include <rtems/blkdev.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @defgroup rtems_ramdisk RAM Disk Device
 *
 * @ingroup rtems_blkdev
 *
 */
/**@{**/

/**
 * @name Static Configuration
 */
/**@{**/

/**
 * @brief RAM disk configuration table entry.
 */
typedef struct rtems_ramdisk_config {
  /**
   * @brief RAM disk block size.
   */
  uint32_t block_size;

  /**
   * @brief Number of blocks on this RAM disk.
   */
  rtems_blkdev_bnum block_num;

  /**
   * @brief RAM disk location or @c NULL if RAM disk memory should be allocated
   * dynamically.
   */
  void *location;
} rtems_ramdisk_config;

/**
 * @brief External reference to the RAM disk configuration table describing
 * each RAM disk in the system.
 *
 * The configuration table is provided by the application.
 */
extern rtems_ramdisk_config rtems_ramdisk_configuration [];

/**
 * @brief External reference the size of the RAM disk configuration table
 * @ref rtems_ramdisk_configuration.
 *
 * The configuration table size is provided by the application.
 */
extern size_t rtems_ramdisk_configuration_size;

/**
 * @brief RAM disk driver initialization entry point.
 */
rtems_device_driver ramdisk_initialize(
 rtems_device_major_number major,
 rtems_device_minor_number minor,
 void *arg
);

/**
 * RAM disk driver table entry.
 */
#define RAMDISK_DRIVER_TABLE_ENTRY \
  { \
    ramdisk_initialize, \
    NULL, NULL, NULL, NULL, NULL \
  }

#define RAMDISK_DEVICE_BASE_NAME "/dev/rd"

/** @} */

/**
 * @name Runtime Configuration
 */
/**@{**/

/**
 * @brief RAM disk descriptor.
 */
typedef struct ramdisk {
  /**
   * @brief RAM disk block size, the media size.
   */
  uint32_t block_size;

  /**
   * @brief Number of blocks on this RAM disk.
   */
  rtems_blkdev_bnum block_num;

  /**
   * @brief RAM disk memory area.
   */
  void *area;

  /**
   * @brief RAM disk is initialized.
   */
  bool initialized;

  /**
   * @brief Indicates if memory is allocated by malloc() for this RAM disk.
   */
  bool malloced;

  /**
   * @brief Trace enable.
   */
  bool trace;

  /**
   * @brief Free the RAM disk at the block device delete request.
   */
  bool free_at_delete_request;
} ramdisk;

int ramdisk_ioctl(rtems_disk_device *dd, uint32_t req, void *argp);

/**
 * @brief Allocates and initializes a RAM disk descriptor.
 *
 * The block size will be @a media_block_size.  The block count will be
 * @a media_block_count.  The disk storage area begins at @a area_begin.  If
 * @a area_begin is @c NULL, the memory will be allocated and zeroed.  Sets the
 * trace enable to @a trace.
 *
 * @return Pointer to allocated and initialized ramdisk structure, or @c NULL
 * if no memory is available.
 *
 * @note
 * Runtime configuration example:
 * @code
 * #include <rtems/ramdisk.h>
 *
 * rtems_status_code create_ramdisk(
 *   const char *device,
 *   uint32_t media_block_size,
 *   rtems_blkdev_bnum media_block_count
 * )
 * {
 *   rtems_status_code sc;
 *   ramdisk *rd;
 *
 *   rd = ramdisk_allocate(NULL, media_block_size, media_block_count, false);
 *   if (rd != NULL) {
 *     sc = rtems_blkdev_create(
 *       device,
 *       media_block_size,
 *       media_block_count,
 *       ramdisk_ioctl,
 *       rd
 *     );
 *   } else {
 *     sc = RTEMS_UNSATISFIED;
 *   }
 *
 *   return sc;
 * }
 * @endcode
 */
ramdisk *ramdisk_allocate(
  void *area_begin,
  uint32_t media_block_size,
  rtems_blkdev_bnum media_block_count,
  bool trace
);

void ramdisk_free(ramdisk *rd);

static inline void ramdisk_enable_free_at_delete_request(ramdisk *rd)
{
  rd->free_at_delete_request = true;
}

/**
 * @brief Allocates, initializes and registers a RAM disk.
 *
 * The block size will be @a media_block_size.  The block count will be
 * @a media_block_count.  The disk storage will be allocated.  Sets the trace
 * enable to @a trace.  Registers a device node with disk name path @a disk.
 * The registered device number will be returned in @a dev.
 *
 * @retval RTEMS_SUCCESSFUL Successful operation.
 * @retval RTEMS_UNSATISFIED Something is wrong.
 */
rtems_status_code ramdisk_register(
  uint32_t media_block_size,
  rtems_blkdev_bnum media_block_count,
  bool trace,
  const char *disk
);

/** @} */

/** @} */

#ifdef __cplusplus
}
#endif

#endif