api_storage.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * (C) Copyright 2007 Semihalf
  3. *
  4. * Written by: Rafal Jaworowski <raj@semihalf.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. */
  25. #include <config.h>
  26. #if defined(CONFIG_API)
  27. #include <common.h>
  28. #include <api_public.h>
  29. #define DEBUG
  30. #undef DEBUG
  31. #ifdef DEBUG
  32. #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
  33. #else
  34. #define debugf(fmt, args...)
  35. #endif
  36. #define errf(fmt, args...) do { printf("ERROR @ %s(): ", __func__); printf(fmt, ##args); } while (0)
  37. #define ENUM_IDE 0
  38. #define ENUM_USB 1
  39. #define ENUM_SCSI 2
  40. #define ENUM_MMC 3
  41. #define ENUM_MAX 4
  42. struct stor_spec {
  43. int max_dev;
  44. int enum_started;
  45. int enum_ended;
  46. int type; /* "external" type: DT_STOR_{IDE,USB,etc} */
  47. char name[4];
  48. };
  49. static struct stor_spec specs[ENUM_MAX] = { { 0, 0, 0, 0, "" }, };
  50. void dev_stor_init(void)
  51. {
  52. #if defined(CONFIG_CMD_IDE)
  53. specs[ENUM_IDE].max_dev = CFG_IDE_MAXDEVICE;
  54. specs[ENUM_IDE].enum_started = 0;
  55. specs[ENUM_IDE].enum_ended = 0;
  56. specs[ENUM_IDE].type = DEV_TYP_STOR | DT_STOR_IDE;
  57. specs[ENUM_IDE].name = "ide";
  58. #endif
  59. #if defined(CONFIG_CMD_USB)
  60. specs[ENUM_USB].max_dev = USB_MAX_STOR_DEV;
  61. specs[ENUM_USB].enum_started = 0;
  62. specs[ENUM_USB].enum_ended = 0;
  63. specs[ENUM_USB].type = DEV_TYP_STOR | DT_STOR_USB;
  64. specs[ENUM_USB].name = "usb";
  65. #endif
  66. #if defined(CONFIG_CMD_SCSI)
  67. specs[ENUM_SCSI].max_dev = CFG_SCSI_MAX_DEVICE;
  68. specs[ENUM_SCSI].enum_started = 0;
  69. specs[ENUM_SCSI].enum_ended = 0;
  70. specs[ENUM_SCSI].type = DEV_TYP_STOR | DT_STOR_SCSI;
  71. specs[ENUM_SCSI].name = "scsi";
  72. #endif
  73. }
  74. /*
  75. * Finds next available device in the storage group
  76. *
  77. * type: storage group type - ENUM_IDE, ENUM_SCSI etc.
  78. *
  79. * first: if 1 the first device in the storage group is returned (if
  80. * exists), if 0 the next available device is searched
  81. *
  82. * more: returns 0/1 depending if there are more devices in this group
  83. * available (for future iterations)
  84. *
  85. * returns: 0/1 depending if device found in this iteration
  86. */
  87. static int dev_stor_get(int type, int first, int *more, struct device_info *di)
  88. {
  89. int found = 0;
  90. *more = 0;
  91. int i;
  92. block_dev_desc_t *dd;
  93. if (first) {
  94. di->cookie = (void *)get_dev(specs[type].name, 0);
  95. found = 1;
  96. } else {
  97. for (i = 0; i < specs[type].max_dev; i++)
  98. if (di->cookie == (void *)get_dev(specs[type].name, i)) {
  99. /* previous cookie found -- advance to the
  100. * next device, if possible */
  101. if (++i >= specs[type].max_dev) {
  102. /* out of range, no more to enum */
  103. di->cookie = NULL;
  104. break;
  105. }
  106. di->cookie = (void *)get_dev(specs[type].name, i);
  107. found = 1;
  108. /* provide hint if there are more devices in
  109. * this group to enumerate */
  110. if ((i + 1) < specs[type].max_dev)
  111. *more = 1;
  112. break;
  113. }
  114. }
  115. if (found) {
  116. di->type = specs[type].type;
  117. if (di->cookie != NULL) {
  118. dd = (block_dev_desc_t *)di->cookie;
  119. if (dd->type == DEV_TYPE_UNKNOWN) {
  120. debugf("device instance exists, but is not active..");
  121. found = 0;
  122. } else {
  123. di->di_stor.block_count = dd->lba;
  124. di->di_stor.block_size = dd->blksz;
  125. }
  126. }
  127. } else
  128. di->cookie = NULL;
  129. return found;
  130. }
  131. /*
  132. * returns: ENUM_IDE, ENUM_USB etc. based on block_dev_desc_t
  133. */
  134. static int dev_stor_type(block_dev_desc_t *dd)
  135. {
  136. int i, j;
  137. for (i = ENUM_IDE; i < ENUM_MAX; i++)
  138. for (j = 0; j < specs[i].max_dev; j++)
  139. if (dd == get_dev(specs[i].name, j))
  140. return i;
  141. return ENUM_MAX;
  142. }
  143. /*
  144. * returns: 0/1 whether cookie points to some device in this group
  145. */
  146. static int dev_is_stor(int type, struct device_info *di)
  147. {
  148. return (dev_stor_type(di->cookie) == type) ? 1 : 0;
  149. }
  150. static int dev_enum_stor(int type, struct device_info *di)
  151. {
  152. int found = 0, more = 0;
  153. debugf("called, type %d\n", type);
  154. /*
  155. * Formulae for enumerating storage devices:
  156. * 1. if cookie (hint from previous enum call) is NULL we start again
  157. * with enumeration, so return the first available device, done.
  158. *
  159. * 2. if cookie is not NULL, check if it identifies some device in
  160. * this group:
  161. *
  162. * 2a. if cookie is a storage device from our group (IDE, USB etc.),
  163. * return next available (if exists) in this group
  164. *
  165. * 2b. if it isn't device from our group, check if such devices were
  166. * ever enumerated before:
  167. * - if not, return the first available device from this group
  168. * - else return 0
  169. */
  170. if (di->cookie == NULL) {
  171. debugf("group%d - enum restart\n", type);
  172. /*
  173. * 1. Enumeration (re-)started: take the first available
  174. * device, if exists
  175. */
  176. found = dev_stor_get(type, 1, &more, di);
  177. specs[type].enum_started = 1;
  178. } else if (dev_is_stor(type, di)) {
  179. debugf("group%d - enum continued for the next device\n", type);
  180. if (specs[type].enum_ended) {
  181. debugf("group%d - nothing more to enum!\n", type);
  182. return 0;
  183. }
  184. /* 2a. Attempt to take a next available device in the group */
  185. found = dev_stor_get(type, 0, &more, di);
  186. } else {
  187. if (specs[type].enum_ended) {
  188. debugf("group %d - already enumerated, skipping\n", type);
  189. return 0;
  190. }
  191. debugf("group%d - first time enum\n", type);
  192. if (specs[type].enum_started == 0) {
  193. /*
  194. * 2b. If enumerating devices in this group did not
  195. * happen before, it means the cookie pointed to a
  196. * device frome some other group (another storage
  197. * group, or network); in this case try to take the
  198. * first available device from our group
  199. */
  200. specs[type].enum_started = 1;
  201. /*
  202. * Attempt to take the first device in this group:
  203. *'first element' flag is set
  204. */
  205. found = dev_stor_get(type, 1, &more, di);
  206. } else {
  207. errf("group%d - out of order iteration\n", type);
  208. found = 0;
  209. more = 0;
  210. }
  211. }
  212. /*
  213. * If there are no more devices in this group, consider its
  214. * enumeration finished
  215. */
  216. specs[type].enum_ended = (!more) ? 1 : 0;
  217. if (found)
  218. debugf("device found, returning cookie 0x%08x\n",
  219. (u_int32_t)di->cookie);
  220. else
  221. debugf("no device found\n");
  222. return found;
  223. }
  224. void dev_enum_reset(void)
  225. {
  226. int i;
  227. for (i = 0; i < ENUM_MAX; i ++) {
  228. specs[i].enum_started = 0;
  229. specs[i].enum_ended = 0;
  230. }
  231. }
  232. int dev_enum_storage(struct device_info *di)
  233. {
  234. int i;
  235. /*
  236. * check: ide, usb, scsi, mmc
  237. */
  238. for (i = ENUM_IDE; i < ENUM_MAX; i ++) {
  239. if (dev_enum_stor(i, di))
  240. return 1;
  241. }
  242. return 0;
  243. }
  244. static int dev_stor_is_valid(int type, block_dev_desc_t *dd)
  245. {
  246. int i;
  247. for (i = 0; i < specs[type].max_dev; i++)
  248. if (dd == get_dev(specs[type].name, i))
  249. if (dd->type != DEV_TYPE_UNKNOWN)
  250. return 1;
  251. return 0;
  252. }
  253. int dev_open_stor(void *cookie)
  254. {
  255. int type = dev_stor_type(cookie);
  256. if (type == ENUM_MAX)
  257. return API_ENODEV;
  258. if (dev_stor_is_valid(type, (block_dev_desc_t *)cookie))
  259. return 0;
  260. return API_ENODEV;
  261. }
  262. int dev_close_stor(void *cookie)
  263. {
  264. /*
  265. * Not much to do as we actually do not alter storage devices upon
  266. * close
  267. */
  268. return 0;
  269. }
  270. static int dev_stor_index(block_dev_desc_t *dd)
  271. {
  272. int i, type;
  273. type = dev_stor_type(dd);
  274. for (i = 0; i < specs[type].max_dev; i++)
  275. if (dd == get_dev(specs[type].name, i))
  276. return i;
  277. return (specs[type].max_dev);
  278. }
  279. lbasize_t dev_read_stor(void *cookie, void *buf, lbasize_t len, lbastart_t start)
  280. {
  281. int type;
  282. block_dev_desc_t *dd = (block_dev_desc_t *)cookie;
  283. if ((type = dev_stor_type(dd)) == ENUM_MAX)
  284. return 0;
  285. if (!dev_stor_is_valid(type, dd))
  286. return 0;
  287. if ((dd->block_read) == NULL) {
  288. debugf("no block_read() for device 0x%08x\n");
  289. return 0;
  290. }
  291. return (dd->block_read(dev_stor_index(dd), start, len, buf));
  292. }
  293. #endif /* CONFIG_API */