api_storage.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * (C) Copyright 2007-2008 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. #include <common.h>
  27. #include <api_public.h>
  28. #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
  29. #include <usb.h>
  30. #endif
  31. #define DEBUG
  32. #undef DEBUG
  33. #ifdef DEBUG
  34. #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
  35. #else
  36. #define debugf(fmt, args...)
  37. #endif
  38. #define errf(fmt, args...) do { printf("ERROR @ %s(): ", __func__); printf(fmt, ##args); } while (0)
  39. #define ENUM_IDE 0
  40. #define ENUM_USB 1
  41. #define ENUM_SCSI 2
  42. #define ENUM_MMC 3
  43. #define ENUM_SATA 4
  44. #define ENUM_MAX 5
  45. struct stor_spec {
  46. int max_dev;
  47. int enum_started;
  48. int enum_ended;
  49. int type; /* "external" type: DT_STOR_{IDE,USB,etc} */
  50. char *name;
  51. };
  52. static struct stor_spec specs[ENUM_MAX] = { { 0, 0, 0, 0, "" }, };
  53. void dev_stor_init(void)
  54. {
  55. #if defined(CONFIG_CMD_IDE)
  56. specs[ENUM_IDE].max_dev = CONFIG_SYS_IDE_MAXDEVICE;
  57. specs[ENUM_IDE].enum_started = 0;
  58. specs[ENUM_IDE].enum_ended = 0;
  59. specs[ENUM_IDE].type = DEV_TYP_STOR | DT_STOR_IDE;
  60. specs[ENUM_IDE].name = "ide";
  61. #endif
  62. #if defined(CONFIG_CMD_MMC)
  63. specs[ENUM_MMC].max_dev = CONFIG_SYS_MMC_MAX_DEVICE;
  64. specs[ENUM_MMC].enum_started = 0;
  65. specs[ENUM_MMC].enum_ended = 0;
  66. specs[ENUM_MMC].type = DEV_TYP_STOR | DT_STOR_MMC;
  67. specs[ENUM_MMC].name = "mmc";
  68. #endif
  69. #if defined(CONFIG_CMD_SATA)
  70. specs[ENUM_SATA].max_dev = CONFIG_SYS_SATA_MAX_DEVICE;
  71. specs[ENUM_SATA].enum_started = 0;
  72. specs[ENUM_SATA].enum_ended = 0;
  73. specs[ENUM_SATA].type = DEV_TYP_STOR | DT_STOR_SATA;
  74. specs[ENUM_SATA].name = "sata";
  75. #endif
  76. #if defined(CONFIG_CMD_SCSI)
  77. specs[ENUM_SCSI].max_dev = CONFIG_SYS_SCSI_MAX_DEVICE;
  78. specs[ENUM_SCSI].enum_started = 0;
  79. specs[ENUM_SCSI].enum_ended = 0;
  80. specs[ENUM_SCSI].type = DEV_TYP_STOR | DT_STOR_SCSI;
  81. specs[ENUM_SCSI].name = "scsi";
  82. #endif
  83. #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
  84. specs[ENUM_USB].max_dev = USB_MAX_STOR_DEV;
  85. specs[ENUM_USB].enum_started = 0;
  86. specs[ENUM_USB].enum_ended = 0;
  87. specs[ENUM_USB].type = DEV_TYP_STOR | DT_STOR_USB;
  88. specs[ENUM_USB].name = "usb";
  89. #endif
  90. }
  91. /*
  92. * Finds next available device in the storage group
  93. *
  94. * type: storage group type - ENUM_IDE, ENUM_SCSI etc.
  95. *
  96. * first: if 1 the first device in the storage group is returned (if
  97. * exists), if 0 the next available device is searched
  98. *
  99. * more: returns 0/1 depending if there are more devices in this group
  100. * available (for future iterations)
  101. *
  102. * returns: 0/1 depending if device found in this iteration
  103. */
  104. static int dev_stor_get(int type, int first, int *more, struct device_info *di)
  105. {
  106. int found = 0;
  107. *more = 0;
  108. int i;
  109. block_dev_desc_t *dd;
  110. if (first) {
  111. di->cookie = (void *)get_dev(specs[type].name, 0);
  112. if (di->cookie == NULL)
  113. return 0;
  114. else
  115. found = 1;
  116. } else {
  117. for (i = 0; i < specs[type].max_dev; i++)
  118. if (di->cookie == (void *)get_dev(specs[type].name, i)) {
  119. /* previous cookie found -- advance to the
  120. * next device, if possible */
  121. if (++i >= specs[type].max_dev) {
  122. /* out of range, no more to enum */
  123. di->cookie = NULL;
  124. break;
  125. }
  126. di->cookie = (void *)get_dev(specs[type].name, i);
  127. if (di->cookie == NULL)
  128. return 0;
  129. else
  130. found = 1;
  131. /* provide hint if there are more devices in
  132. * this group to enumerate */
  133. if ((i + 1) < specs[type].max_dev)
  134. *more = 1;
  135. break;
  136. }
  137. }
  138. if (found) {
  139. di->type = specs[type].type;
  140. if (di->cookie != NULL) {
  141. dd = (block_dev_desc_t *)di->cookie;
  142. if (dd->type == DEV_TYPE_UNKNOWN) {
  143. debugf("device instance exists, but is not active..");
  144. found = 0;
  145. } else {
  146. di->di_stor.block_count = dd->lba;
  147. di->di_stor.block_size = dd->blksz;
  148. }
  149. }
  150. } else
  151. di->cookie = NULL;
  152. return found;
  153. }
  154. /*
  155. * returns: ENUM_IDE, ENUM_USB etc. based on block_dev_desc_t
  156. */
  157. static int dev_stor_type(block_dev_desc_t *dd)
  158. {
  159. int i, j;
  160. for (i = ENUM_IDE; i < ENUM_MAX; i++)
  161. for (j = 0; j < specs[i].max_dev; j++)
  162. if (dd == get_dev(specs[i].name, j))
  163. return i;
  164. return ENUM_MAX;
  165. }
  166. /*
  167. * returns: 0/1 whether cookie points to some device in this group
  168. */
  169. static int dev_is_stor(int type, struct device_info *di)
  170. {
  171. return (dev_stor_type(di->cookie) == type) ? 1 : 0;
  172. }
  173. static int dev_enum_stor(int type, struct device_info *di)
  174. {
  175. int found = 0, more = 0;
  176. debugf("called, type %d\n", type);
  177. /*
  178. * Formulae for enumerating storage devices:
  179. * 1. if cookie (hint from previous enum call) is NULL we start again
  180. * with enumeration, so return the first available device, done.
  181. *
  182. * 2. if cookie is not NULL, check if it identifies some device in
  183. * this group:
  184. *
  185. * 2a. if cookie is a storage device from our group (IDE, USB etc.),
  186. * return next available (if exists) in this group
  187. *
  188. * 2b. if it isn't device from our group, check if such devices were
  189. * ever enumerated before:
  190. * - if not, return the first available device from this group
  191. * - else return 0
  192. */
  193. if (di->cookie == NULL) {
  194. debugf("group%d - enum restart\n", type);
  195. /*
  196. * 1. Enumeration (re-)started: take the first available
  197. * device, if exists
  198. */
  199. found = dev_stor_get(type, 1, &more, di);
  200. specs[type].enum_started = 1;
  201. } else if (dev_is_stor(type, di)) {
  202. debugf("group%d - enum continued for the next device\n", type);
  203. if (specs[type].enum_ended) {
  204. debugf("group%d - nothing more to enum!\n", type);
  205. return 0;
  206. }
  207. /* 2a. Attempt to take a next available device in the group */
  208. found = dev_stor_get(type, 0, &more, di);
  209. } else {
  210. if (specs[type].enum_ended) {
  211. debugf("group %d - already enumerated, skipping\n", type);
  212. return 0;
  213. }
  214. debugf("group%d - first time enum\n", type);
  215. if (specs[type].enum_started == 0) {
  216. /*
  217. * 2b. If enumerating devices in this group did not
  218. * happen before, it means the cookie pointed to a
  219. * device frome some other group (another storage
  220. * group, or network); in this case try to take the
  221. * first available device from our group
  222. */
  223. specs[type].enum_started = 1;
  224. /*
  225. * Attempt to take the first device in this group:
  226. *'first element' flag is set
  227. */
  228. found = dev_stor_get(type, 1, &more, di);
  229. } else {
  230. errf("group%d - out of order iteration\n", type);
  231. found = 0;
  232. more = 0;
  233. }
  234. }
  235. /*
  236. * If there are no more devices in this group, consider its
  237. * enumeration finished
  238. */
  239. specs[type].enum_ended = (!more) ? 1 : 0;
  240. if (found)
  241. debugf("device found, returning cookie 0x%08x\n",
  242. (u_int32_t)di->cookie);
  243. else
  244. debugf("no device found\n");
  245. return found;
  246. }
  247. void dev_enum_reset(void)
  248. {
  249. int i;
  250. for (i = 0; i < ENUM_MAX; i ++) {
  251. specs[i].enum_started = 0;
  252. specs[i].enum_ended = 0;
  253. }
  254. }
  255. int dev_enum_storage(struct device_info *di)
  256. {
  257. int i;
  258. /*
  259. * check: ide, usb, scsi, mmc
  260. */
  261. for (i = ENUM_IDE; i < ENUM_MAX; i ++) {
  262. if (dev_enum_stor(i, di))
  263. return 1;
  264. }
  265. return 0;
  266. }
  267. static int dev_stor_is_valid(int type, block_dev_desc_t *dd)
  268. {
  269. int i;
  270. for (i = 0; i < specs[type].max_dev; i++)
  271. if (dd == get_dev(specs[type].name, i))
  272. if (dd->type != DEV_TYPE_UNKNOWN)
  273. return 1;
  274. return 0;
  275. }
  276. int dev_open_stor(void *cookie)
  277. {
  278. int type = dev_stor_type(cookie);
  279. if (type == ENUM_MAX)
  280. return API_ENODEV;
  281. if (dev_stor_is_valid(type, (block_dev_desc_t *)cookie))
  282. return 0;
  283. return API_ENODEV;
  284. }
  285. int dev_close_stor(void *cookie)
  286. {
  287. /*
  288. * Not much to do as we actually do not alter storage devices upon
  289. * close
  290. */
  291. return 0;
  292. }
  293. static int dev_stor_index(block_dev_desc_t *dd)
  294. {
  295. int i, type;
  296. type = dev_stor_type(dd);
  297. for (i = 0; i < specs[type].max_dev; i++)
  298. if (dd == get_dev(specs[type].name, i))
  299. return i;
  300. return (specs[type].max_dev);
  301. }
  302. lbasize_t dev_read_stor(void *cookie, void *buf, lbasize_t len, lbastart_t start)
  303. {
  304. int type;
  305. block_dev_desc_t *dd = (block_dev_desc_t *)cookie;
  306. if ((type = dev_stor_type(dd)) == ENUM_MAX)
  307. return 0;
  308. if (!dev_stor_is_valid(type, dd))
  309. return 0;
  310. if ((dd->block_read) == NULL) {
  311. debugf("no block_read() for device 0x%08x\n", cookie);
  312. return 0;
  313. }
  314. return (dd->block_read(dev_stor_index(dd), start, len, buf));
  315. }