bchlib_setup.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /****************************************************************************
  2. * drivers/bch/bchlib_setup.c
  3. *
  4. * Copyright (C) 2008-2009, 2011 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. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <sys/mount.h>
  41. #include <stdint.h>
  42. #include <stdbool.h>
  43. #include <stdlib.h>
  44. #include <errno.h>
  45. #include <assert.h>
  46. #include <debug.h>
  47. #include <nuttx/kmalloc.h>
  48. #include <nuttx/fs/fs.h>
  49. #include "bch_internal.h"
  50. /****************************************************************************
  51. * Private Types
  52. ****************************************************************************/
  53. /****************************************************************************
  54. * Private Function Prototypes
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Private Data
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Private Functions
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Public Functions
  64. ****************************************************************************/
  65. /****************************************************************************
  66. * Name: bchlib_setup
  67. *
  68. * Description:
  69. * Setup so that the block driver referenced by 'blkdev' can be accessed
  70. * similar to a character device.
  71. *
  72. ****************************************************************************/
  73. int bchlib_setup(const char *blkdev, bool readonly, FAR void **handle)
  74. {
  75. FAR struct bchlib_s *bch;
  76. struct geometry geo;
  77. int ret;
  78. DEBUGASSERT(blkdev);
  79. /* Allocate the BCH state structure */
  80. bch = (FAR struct bchlib_s*)kzalloc(sizeof(struct bchlib_s));
  81. if (!bch)
  82. {
  83. fdbg("Failed to allocate BCH structure\n");
  84. return -ENOMEM;
  85. }
  86. /* Open the block driver */
  87. ret = open_blockdriver(blkdev, readonly ? MS_RDONLY : 0, &bch->inode);
  88. if (ret < 0)
  89. {
  90. fdbg("Failed to open driver %s: %d\n", blkdev, -ret);
  91. goto errout_with_bch;
  92. }
  93. DEBUGASSERT(bch->inode && bch->inode->u.i_bops && bch->inode->u.i_bops->geometry);
  94. ret = bch->inode->u.i_bops->geometry(bch->inode, &geo);
  95. if (ret < 0)
  96. {
  97. fdbg("geometry failed: %d\n", -ret);
  98. goto errout_with_bch;
  99. }
  100. if (!geo.geo_available)
  101. {
  102. fdbg("geometry failed: %d\n", -ret);
  103. ret = -ENODEV;
  104. goto errout_with_bch;
  105. }
  106. if (!readonly && (!bch->inode->u.i_bops->write || !geo.geo_writeenabled))
  107. {
  108. fdbg("write access not supported\n");
  109. ret = -EACCES;
  110. goto errout_with_bch;
  111. }
  112. /* Save the geometry info and complete initialization of the structure */
  113. sem_init(&bch->sem, 0, 1);
  114. bch->nsectors = geo.geo_nsectors;
  115. bch->sectsize = geo.geo_sectorsize;
  116. bch->sector = (size_t)-1;
  117. bch->readonly = readonly;
  118. /* Allocate the sector I/O buffer */
  119. bch->buffer = (FAR uint8_t *)kmalloc(bch->sectsize);
  120. if (!bch->buffer)
  121. {
  122. fdbg("Failed to allocate sector buffer\n");
  123. ret = -ENOMEM;
  124. goto errout_with_bch;
  125. }
  126. *handle = bch;
  127. return OK;
  128. errout_with_bch:
  129. kfree(bch);
  130. return ret;
  131. }