volume.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. unsigned int priority;
  29. char *name;
  30. volume_probe_t probe;
  31. volume_init_t init;
  32. volume_stop_t stop;
  33. volume_find_t find;
  34. volume_identify_t identify;
  35. volume_read_t read;
  36. volume_write_t write;
  37. volume_erase_t erase;
  38. volume_erase_all_t erase_all;
  39. };
  40. enum {
  41. UNKNOWN_TYPE,
  42. NANDFLASH,
  43. NORFLASH,
  44. UBIVOLUME,
  45. BLOCKDEV,
  46. };
  47. struct volume {
  48. struct driver *drv;
  49. char *name;
  50. char *blk;
  51. __u64 size;
  52. __u32 block_size;
  53. int type;
  54. };
  55. extern struct volume* volume_find(char *name);
  56. extern void volume_register_driver(struct driver *drv);
  57. static inline int volume_init(struct volume *v)
  58. {
  59. if (v && v->drv->init)
  60. return v->drv->init(v);
  61. return -1;
  62. }
  63. static inline int volume_identify(struct volume *v)
  64. {
  65. if (v && v->drv->identify)
  66. return v->drv->identify(v);
  67. return -1;
  68. }
  69. static inline int volume_erase(struct volume *v, int offset, int len)
  70. {
  71. if (v && v->drv->erase)
  72. return v->drv->erase(v, offset, len);
  73. return -1;
  74. }
  75. static inline int volume_erase_all(struct volume *v)
  76. {
  77. if (v && v->drv->erase_all)
  78. return v->drv->erase_all(v);
  79. return -1;
  80. }
  81. static inline int volume_read(struct volume *v, void *buf, int offset, int length)
  82. {
  83. if (v && v->drv->read)
  84. return v->drv->read(v, buf, offset, length);
  85. return -1;
  86. }
  87. static inline int volume_write(struct volume *v, void *buf, int offset, int length)
  88. {
  89. if (v && v->drv->write)
  90. return v->drv->write(v, buf, offset, length);
  91. return -1;
  92. }
  93. #define DRIVER(x) \
  94. static void __attribute__((constructor)) \
  95. drv_register_##x(void) { \
  96. volume_register_driver(&x); \
  97. }
  98. #endif