rwbuffer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /****************************************************************************
  2. * include/nuttx/drivers/rwbuffer.h
  3. *
  4. * Copyright (C) 2009, 2014, 2020 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. #ifndef __INCLUDE_NUTTX_DRIVERS_RWBUFFER_H
  36. #define __INCLUDE_NUTTX_DRIVERS_RWBUFFER_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <sys/types.h>
  42. #include <stdint.h>
  43. #include <nuttx/semaphore.h>
  44. #include <nuttx/wqueue.h>
  45. #if defined(CONFIG_DRVR_WRITEBUFFER) || defined(CONFIG_DRVR_READAHEAD)
  46. /****************************************************************************
  47. * Pre-processor Definitions
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Public Types
  51. ****************************************************************************/
  52. /* Data transfer callouts. These must be provided by the block driver
  53. * logic in order to flush the write buffer when appropriate or to
  54. * reload the read-ahead buffer, when appropriate.
  55. */
  56. typedef CODE ssize_t (*rwbreload_t)(FAR void *dev, FAR uint8_t *buffer,
  57. off_t startblock, size_t nblocks);
  58. typedef CODE ssize_t (*rwbflush_t)(FAR void *dev, FAR const uint8_t *buffer,
  59. off_t startblock, size_t nblocks);
  60. /* This structure holds the state of the buffers. In typical usage,
  61. * an instance of this structure is declared within each block driver
  62. * status structure like:
  63. *
  64. * struct foo_dev_s
  65. * {
  66. * ...
  67. * struct rwbuffer_s rwbuffer;
  68. * ...
  69. * };
  70. *
  71. * Note that this supports buffering for multiple block devices or for
  72. * multiple instances of same block device, because each rwbuffer instance
  73. * supports independent buffering.
  74. *
  75. * A reference to the struct rwbuffer_s instance is then passed to each
  76. * interface like:
  77. *
  78. * FAR struct foo_dev_s *priv;
  79. * ...
  80. * ... [Setup blocksize, nblocks, dev, wrmaxblocks, wrflush,
  81. * rhmaxblocks, rhreload] ...
  82. * ret = rwb_initialize(&priv->rwbuffer);
  83. */
  84. struct rwbuffer_s
  85. {
  86. /**************************************************************************/
  87. /* These values must be provided by the user prior to calling
  88. * rwb_initialize()
  89. */
  90. /* Supported geometry */
  91. uint16_t blocksize; /* The size of one block */
  92. size_t nblocks; /* The total number blocks supported */
  93. /* Read-ahead/Write buffer sizes. Buffering can be disabled (even if it
  94. * is enabled in the configuration) by setting the buffer size to zero
  95. * blocks.
  96. */
  97. #ifdef CONFIG_DRVR_WRITEBUFFER
  98. uint16_t wrmaxblocks; /* The number of blocks to buffer in memory */
  99. uint16_t wralignblocks; /* The buffer to be flash is always multiplied by this
  100. * number. It must be 0 or divisible by wrmaxblocks.
  101. */
  102. #endif
  103. #ifdef CONFIG_DRVR_READAHEAD
  104. uint16_t rhmaxblocks; /* The number of blocks to buffer in memory */
  105. #endif
  106. /* Callback functions.
  107. *
  108. * wrflush. This callback is normally used to flush the contents of
  109. * the write buffer. If write buffering is disabled, then this
  110. * function will instead be used to perform unbuffered writes.
  111. * rhrelad. This callback is normally used to read new data into the
  112. * read-ahead buffer. If read-ahead buffering is disabled, then this
  113. * function will instead be used to perform unbuffered reads.
  114. */
  115. FAR void *dev; /* Device state passed to callout functions */
  116. rwbflush_t wrflush; /* Callout to flush the write buffer */
  117. rwbreload_t rhreload; /* Callout to reload the read-ahead buffer */
  118. /**************************************************************************/
  119. /* The user should never modify any of the remaining fields */
  120. /* This is the state of the write buffering */
  121. #ifdef CONFIG_DRVR_WRITEBUFFER
  122. sem_t wrsem; /* Enforces exclusive access to the write buffer */
  123. struct work_s work; /* Delayed work to flush buffer after a delay with no activity */
  124. uint8_t *wrbuffer; /* Allocated write buffer */
  125. uint16_t wrnblocks; /* Number of blocks in write buffer */
  126. off_t wrblockstart; /* First block in write buffer */
  127. #endif
  128. /* This is the state of the read-ahead buffering */
  129. #ifdef CONFIG_DRVR_READAHEAD
  130. sem_t rhsem; /* Enforces exclusive access to the write buffer */
  131. uint8_t *rhbuffer; /* Allocated read-ahead buffer */
  132. uint16_t rhnblocks; /* Number of blocks in read-ahead buffer */
  133. off_t rhblockstart; /* First block in read-ahead buffer */
  134. #endif
  135. };
  136. /****************************************************************************
  137. * Public Data
  138. ****************************************************************************/
  139. #undef EXTERN
  140. #if defined(__cplusplus)
  141. #define EXTERN extern "C"
  142. extern "C"
  143. {
  144. #else
  145. #define EXTERN extern
  146. #endif
  147. /****************************************************************************
  148. * Public Function Prototypes
  149. ****************************************************************************/
  150. /* Buffer initialization */
  151. int rwb_initialize(FAR struct rwbuffer_s *rwb);
  152. void rwb_uninitialize(FAR struct rwbuffer_s *rwb);
  153. /* Block oriented transfers */
  154. ssize_t rwb_read(FAR struct rwbuffer_s *rwb, off_t startblock,
  155. size_t blockcount, FAR uint8_t *rdbuffer);
  156. ssize_t rwb_write(FAR struct rwbuffer_s *rwb,
  157. off_t startblock, size_t blockcount,
  158. FAR const uint8_t *wrbuffer);
  159. /* Character oriented transfers */
  160. #ifdef CONFIG_DRVR_READBYTES
  161. ssize_t rwb_readbytes(FAR struct rwbuffer_s *dev, off_t offset,
  162. size_t nbytes, FAR uint8_t *buffer);
  163. #endif
  164. /* Media events */
  165. #ifdef CONFIG_DRVR_REMOVABLE
  166. int rwb_mediaremoved(FAR struct rwbuffer_s *rwb);
  167. #endif
  168. #ifdef CONFIG_DRVR_INVALIDATE
  169. int rwb_invalidate(FAR struct rwbuffer_s *rwb,
  170. off_t startblock, size_t blockcount);
  171. #endif
  172. #ifdef CONFIG_DRVR_WRITEBUFFER
  173. int rwb_flush(FAR struct rwbuffer_s *rwb);
  174. #endif
  175. #undef EXTERN
  176. #if defined(__cplusplus)
  177. }
  178. #endif
  179. #endif /* CONFIG_DRVR_WRITEBUFFER || CONFIG_DRVR_READAHEAD */
  180. #endif /* __INCLUDE_NUTTX_DRIVERS_RWBUFFER_H */