volume.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #ifndef _VOLUME_H__
  14. #define _VOLUME_H__
  15. #include <asm/byteorder.h>
  16. struct volume;
  17. typedef int (*volume_probe_t)(void);
  18. typedef int (*volume_init_t)(struct volume *v);
  19. typedef void (*volume_stop_t)(struct volume *v);
  20. typedef struct volume *(*volume_find_t)(char *name);
  21. typedef int (*volume_identify_t)(struct volume *v);
  22. typedef int (*volume_read_t)(struct volume *v, void *buf, int offset, int length);
  23. typedef int (*volume_write_t)(struct volume *v, void *buf, int offset, int length);
  24. typedef int (*volume_erase_t)(struct volume *v, int start, int len);
  25. typedef int (*volume_erase_all_t)(struct volume *v);
  26. struct driver {
  27. struct list_head list;
  28. char *name;
  29. volume_probe_t probe;
  30. volume_init_t init;
  31. volume_stop_t stop;
  32. volume_find_t find;
  33. volume_identify_t identify;
  34. volume_read_t read;
  35. volume_write_t write;
  36. volume_erase_t erase;
  37. volume_erase_all_t erase_all;
  38. };
  39. enum {
  40. UNKNOWN_TYPE,
  41. NANDFLASH,
  42. NORFLASH,
  43. UBIVOLUME,
  44. };
  45. struct volume {
  46. struct driver *drv;
  47. char *name;
  48. char *blk;
  49. __u64 size;
  50. __u32 block_size;
  51. int type;
  52. };
  53. extern struct volume* volume_find(char *name);
  54. extern void volume_register_driver(struct driver *drv);
  55. static inline int volume_init(struct volume *v)
  56. {
  57. if (v && v->drv->init)
  58. return v->drv->init(v);
  59. return -1;
  60. }
  61. static inline int volume_identify(struct volume *v)
  62. {
  63. if (v && v->drv->identify)
  64. return v->drv->identify(v);
  65. return -1;
  66. }
  67. static inline int volume_erase(struct volume *v, int offset, int len)
  68. {
  69. if (v && v->drv->erase)
  70. return v->drv->erase(v, offset, len);
  71. return -1;
  72. }
  73. static inline int volume_erase_all(struct volume *v)
  74. {
  75. if (v && v->drv->erase_all)
  76. return v->drv->erase_all(v);
  77. return -1;
  78. }
  79. static inline int volume_read(struct volume *v, void *buf, int offset, int length)
  80. {
  81. if (v && v->drv->read)
  82. return v->drv->read(v, buf, offset, length);
  83. return -1;
  84. }
  85. static inline int volume_write(struct volume *v, void *buf, int offset, int length)
  86. {
  87. if (v && v->drv->write)
  88. return v->drv->write(v, buf, offset, length);
  89. return -1;
  90. }
  91. #define DRIVER(x) \
  92. static void __attribute__((constructor)) \
  93. drv_register_##x(void) { \
  94. volume_register_driver(&x); \
  95. }
  96. #endif