storage.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2016 Actions Corporation
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. The name of the author may not be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /**
  27. * @file
  28. * @brief Public API for usb-storage drivers
  29. */
  30. #ifndef __USB_STORAGE_H__
  31. #define __USB_STORAGE_H__
  32. /**
  33. * @brief USB disk IO Interface
  34. * @defgroup storage_interface USB disk IO Interface
  35. * @ingroup io_interfaces
  36. * @{
  37. */
  38. #include <stdint.h>
  39. #include <stddef.h>
  40. #include <sys/types.h>
  41. #include <device.h>
  42. #include <disk/disk_access.h>
  43. #include <drivers/mmc/sd.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. typedef int (*storage_api_read)(struct device *dev, off_t offset, void *data,
  48. size_t len);
  49. typedef int (*storage_api_write)(struct device *dev, off_t offset,
  50. const void *data, size_t len);
  51. typedef int (*storage_api_ioctl)(struct device *dev, uint8_t cmd, void *buff);
  52. struct storage_driver_api {
  53. storage_api_read read;
  54. storage_api_write write;
  55. storage_api_ioctl ioctl;
  56. };
  57. /* Get the number of sectors in the usb disk */
  58. #define USB_DISK_IOCTL_GET_SECTOR_COUNT 1
  59. /* Get the size of a usb disk SECTOR in bytes */
  60. #define USB_DISK_IOCTL_GET_SECTOR_SIZE 2
  61. /* Get the size of the usb disk in bytes */
  62. #define USB_DISK_IOCTL_GET_DISK_SIZE 3
  63. /* Get usb disk status: connected/disconnected */
  64. #define USB_DISK_IOCTL_GET_STATUS 4
  65. /**
  66. * @brief Read data from usb disk
  67. *
  68. * @param dev : USB disk dev
  69. * @param offset : Offset (byte aligned) to read
  70. * @param data : Buffer to store read data
  71. * @param len : Number of sectors to read.
  72. *
  73. * @return 0 on success, negative errno code on fail.
  74. */
  75. static inline int storage_read(struct device *dev, off_t offset, void *data,
  76. size_t len)
  77. {
  78. const struct storage_driver_api *api = dev->api;
  79. return api->read(dev, offset, data, len);
  80. }
  81. /**
  82. * @brief Write buffer into usb disk.
  83. *
  84. * @param dev : USB disk device
  85. * @param offset : starting offset for the write
  86. * @param data : data to write
  87. * @param len : Number of sectors to write
  88. *
  89. * @return 0 on success, negative errno code on fail.
  90. */
  91. static inline int storage_write(struct device *dev, off_t offset,
  92. const void *data, size_t len)
  93. {
  94. const struct storage_driver_api *api = dev->api;
  95. return api->write(dev, offset, data, len);
  96. }
  97. /**
  98. * @brief Read meta data from usb disk
  99. *
  100. * @param dev : USB disk dev
  101. * @param cmd : USB disk ioctl cmd type
  102. * @param buff : Buffer to store read data
  103. *
  104. * @return 0 on success, negative errno code on fail.
  105. */
  106. static inline int storage_ioctl(struct device *dev, uint8_t cmd, void *buff)
  107. {
  108. const struct storage_driver_api *api = dev->api;
  109. return api->ioctl(dev, cmd, buff);
  110. }
  111. /**
  112. * USB disk hotplug support
  113. * NOTE: should keep pace with Disk Status Bits (DSTATUS)
  114. * which defined in diskio.h.
  115. */
  116. #define USB_DISK_NOOP 0x0
  117. /* STA_DISK_OK */
  118. #define USB_DISK_CONNECTED STA_DISK_OK
  119. /* STA_NODISK */
  120. #define USB_DISK_DISCONNECTED STA_NODISK
  121. #ifdef __cplusplus
  122. }
  123. #endif
  124. /**
  125. * @}
  126. */
  127. #endif /* __USB_STORAGE_H__ */