sdio.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*
  2. * linux/drivers/mmc/sdio.c
  3. *
  4. * Copyright 2006-2007 Pierre Ossman
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. */
  11. #include <linux/err.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/mmc/host.h>
  14. #include <linux/mmc/card.h>
  15. #include <linux/mmc/sdio.h>
  16. #include <linux/mmc/sdio_func.h>
  17. #include "core.h"
  18. #include "bus.h"
  19. #include "sd.h"
  20. #include "sdio_bus.h"
  21. #include "mmc_ops.h"
  22. #include "sd_ops.h"
  23. #include "sdio_ops.h"
  24. #include "sdio_cis.h"
  25. static int sdio_read_fbr(struct sdio_func *func)
  26. {
  27. int ret;
  28. unsigned char data;
  29. ret = mmc_io_rw_direct(func->card, 0, 0,
  30. SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
  31. if (ret)
  32. goto out;
  33. data &= 0x0f;
  34. if (data == 0x0f) {
  35. ret = mmc_io_rw_direct(func->card, 0, 0,
  36. SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
  37. if (ret)
  38. goto out;
  39. }
  40. func->class = data;
  41. out:
  42. return ret;
  43. }
  44. static int sdio_init_func(struct mmc_card *card, unsigned int fn)
  45. {
  46. int ret;
  47. struct sdio_func *func;
  48. BUG_ON(fn > SDIO_MAX_FUNCS);
  49. func = sdio_alloc_func(card);
  50. if (IS_ERR(func))
  51. return PTR_ERR(func);
  52. func->num = fn;
  53. if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
  54. ret = sdio_read_fbr(func);
  55. if (ret)
  56. goto fail;
  57. ret = sdio_read_func_cis(func);
  58. if (ret)
  59. goto fail;
  60. } else {
  61. func->vendor = func->card->cis.vendor;
  62. func->device = func->card->cis.device;
  63. func->max_blksize = func->card->cis.blksize;
  64. }
  65. card->sdio_func[fn - 1] = func;
  66. return 0;
  67. fail:
  68. /*
  69. * It is okay to remove the function here even though we hold
  70. * the host lock as we haven't registered the device yet.
  71. */
  72. sdio_remove_func(func);
  73. return ret;
  74. }
  75. static int sdio_read_cccr(struct mmc_card *card)
  76. {
  77. int ret;
  78. int cccr_vsn;
  79. unsigned char data;
  80. memset(&card->cccr, 0, sizeof(struct sdio_cccr));
  81. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
  82. if (ret)
  83. goto out;
  84. cccr_vsn = data & 0x0f;
  85. if (cccr_vsn > SDIO_CCCR_REV_1_20) {
  86. printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
  87. mmc_hostname(card->host), cccr_vsn);
  88. return -EINVAL;
  89. }
  90. card->cccr.sdio_vsn = (data & 0xf0) >> 4;
  91. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
  92. if (ret)
  93. goto out;
  94. if (data & SDIO_CCCR_CAP_SMB)
  95. card->cccr.multi_block = 1;
  96. if (data & SDIO_CCCR_CAP_LSC)
  97. card->cccr.low_speed = 1;
  98. if (data & SDIO_CCCR_CAP_4BLS)
  99. card->cccr.wide_bus = 1;
  100. if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
  101. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
  102. if (ret)
  103. goto out;
  104. if (data & SDIO_POWER_SMPC)
  105. card->cccr.high_power = 1;
  106. }
  107. if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
  108. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
  109. if (ret)
  110. goto out;
  111. if (data & SDIO_SPEED_SHS)
  112. card->cccr.high_speed = 1;
  113. }
  114. out:
  115. return ret;
  116. }
  117. static int sdio_enable_wide(struct mmc_card *card)
  118. {
  119. int ret;
  120. u8 ctrl;
  121. if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
  122. return 0;
  123. if (card->cccr.low_speed && !card->cccr.wide_bus)
  124. return 0;
  125. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  126. if (ret)
  127. return ret;
  128. ctrl |= SDIO_BUS_WIDTH_4BIT;
  129. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  130. if (ret)
  131. return ret;
  132. return 1;
  133. }
  134. /*
  135. * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
  136. * of the card. This may be required on certain setups of boards,
  137. * controllers and embedded sdio device which do not need the card's
  138. * pull-up. As a result, card detection is disabled and power is saved.
  139. */
  140. static int sdio_disable_cd(struct mmc_card *card)
  141. {
  142. int ret;
  143. u8 ctrl;
  144. if (!card->cccr.disable_cd)
  145. return 0;
  146. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  147. if (ret)
  148. return ret;
  149. ctrl |= SDIO_BUS_CD_DISABLE;
  150. return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  151. }
  152. /*
  153. * Devices that remain active during a system suspend are
  154. * put back into 1-bit mode.
  155. */
  156. static int sdio_disable_wide(struct mmc_card *card)
  157. {
  158. int ret;
  159. u8 ctrl;
  160. if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
  161. return 0;
  162. if (card->cccr.low_speed && !card->cccr.wide_bus)
  163. return 0;
  164. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  165. if (ret)
  166. return ret;
  167. if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
  168. return 0;
  169. ctrl &= ~SDIO_BUS_WIDTH_4BIT;
  170. ctrl |= SDIO_BUS_ASYNC_INT;
  171. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  172. if (ret)
  173. return ret;
  174. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
  175. return 0;
  176. }
  177. static int sdio_enable_4bit_bus(struct mmc_card *card)
  178. {
  179. int err;
  180. if (card->type == MMC_TYPE_SDIO)
  181. return sdio_enable_wide(card);
  182. if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
  183. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  184. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  185. if (err)
  186. return err;
  187. } else
  188. return 0;
  189. err = sdio_enable_wide(card);
  190. if (err <= 0)
  191. mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
  192. return err;
  193. }
  194. /*
  195. * Test if the card supports high-speed mode and, if so, switch to it.
  196. */
  197. static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
  198. {
  199. int ret;
  200. u8 speed;
  201. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  202. return 0;
  203. if (!card->cccr.high_speed)
  204. return 0;
  205. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  206. if (ret)
  207. return ret;
  208. if (enable)
  209. speed |= SDIO_SPEED_EHS;
  210. else
  211. speed &= ~SDIO_SPEED_EHS;
  212. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
  213. if (ret)
  214. return ret;
  215. return 1;
  216. }
  217. /*
  218. * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
  219. */
  220. static int sdio_enable_hs(struct mmc_card *card)
  221. {
  222. int ret;
  223. ret = mmc_sdio_switch_hs(card, true);
  224. if (ret <= 0 || card->type == MMC_TYPE_SDIO)
  225. return ret;
  226. ret = mmc_sd_switch_hs(card);
  227. if (ret <= 0)
  228. mmc_sdio_switch_hs(card, false);
  229. return ret;
  230. }
  231. static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
  232. {
  233. unsigned max_dtr;
  234. if (mmc_card_highspeed(card)) {
  235. /*
  236. * The SDIO specification doesn't mention how
  237. * the CIS transfer speed register relates to
  238. * high-speed, but it seems that 50 MHz is
  239. * mandatory.
  240. */
  241. max_dtr = 50000000;
  242. } else {
  243. max_dtr = card->cis.max_dtr;
  244. }
  245. if (card->type == MMC_TYPE_SD_COMBO)
  246. max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
  247. return max_dtr;
  248. }
  249. /*
  250. * Handle the detection and initialisation of a card.
  251. *
  252. * In the case of a resume, "oldcard" will contain the card
  253. * we're trying to reinitialise.
  254. */
  255. static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
  256. struct mmc_card *oldcard, int powered_resume)
  257. {
  258. struct mmc_card *card;
  259. int err;
  260. BUG_ON(!host);
  261. WARN_ON(!host->claimed);
  262. /*
  263. * Inform the card of the voltage
  264. */
  265. if (!powered_resume) {
  266. err = mmc_send_io_op_cond(host, host->ocr, &ocr);
  267. if (err)
  268. goto err;
  269. }
  270. /*
  271. * For SPI, enable CRC as appropriate.
  272. */
  273. if (mmc_host_is_spi(host)) {
  274. err = mmc_spi_set_crc(host, use_spi_crc);
  275. if (err)
  276. goto err;
  277. }
  278. /*
  279. * Allocate card structure.
  280. */
  281. card = mmc_alloc_card(host, NULL);
  282. if (IS_ERR(card)) {
  283. err = PTR_ERR(card);
  284. goto err;
  285. }
  286. if (ocr & R4_MEMORY_PRESENT
  287. && mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid) == 0) {
  288. card->type = MMC_TYPE_SD_COMBO;
  289. if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
  290. memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
  291. mmc_remove_card(card);
  292. return -ENOENT;
  293. }
  294. } else {
  295. card->type = MMC_TYPE_SDIO;
  296. if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
  297. mmc_remove_card(card);
  298. return -ENOENT;
  299. }
  300. }
  301. /*
  302. * Call the optional HC's init_card function to handle quirks.
  303. */
  304. if (host->ops->init_card)
  305. host->ops->init_card(host, card);
  306. /*
  307. * For native busses: set card RCA and quit open drain mode.
  308. */
  309. if (!powered_resume && !mmc_host_is_spi(host)) {
  310. err = mmc_send_relative_addr(host, &card->rca);
  311. if (err)
  312. goto remove;
  313. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  314. }
  315. /*
  316. * Read CSD, before selecting the card
  317. */
  318. if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
  319. err = mmc_sd_get_csd(host, card);
  320. if (err)
  321. return err;
  322. mmc_decode_cid(card);
  323. }
  324. /*
  325. * Select card, as all following commands rely on that.
  326. */
  327. if (!powered_resume && !mmc_host_is_spi(host)) {
  328. err = mmc_select_card(card);
  329. if (err)
  330. goto remove;
  331. }
  332. if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
  333. /*
  334. * This is non-standard SDIO device, meaning it doesn't
  335. * have any CIA (Common I/O area) registers present.
  336. * It's host's responsibility to fill cccr and cis
  337. * structures in init_card().
  338. */
  339. mmc_set_clock(host, card->cis.max_dtr);
  340. if (card->cccr.high_speed) {
  341. mmc_card_set_highspeed(card);
  342. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  343. }
  344. goto finish;
  345. }
  346. /*
  347. * Read the common registers.
  348. */
  349. err = sdio_read_cccr(card);
  350. if (err)
  351. goto remove;
  352. /*
  353. * Read the common CIS tuples.
  354. */
  355. err = sdio_read_common_cis(card);
  356. if (err)
  357. goto remove;
  358. if (oldcard) {
  359. int same = (card->cis.vendor == oldcard->cis.vendor &&
  360. card->cis.device == oldcard->cis.device);
  361. mmc_remove_card(card);
  362. if (!same)
  363. return -ENOENT;
  364. card = oldcard;
  365. }
  366. if (card->type == MMC_TYPE_SD_COMBO) {
  367. err = mmc_sd_setup_card(host, card, oldcard != NULL);
  368. /* handle as SDIO-only card if memory init failed */
  369. if (err) {
  370. mmc_go_idle(host);
  371. if (mmc_host_is_spi(host))
  372. /* should not fail, as it worked previously */
  373. mmc_spi_set_crc(host, use_spi_crc);
  374. card->type = MMC_TYPE_SDIO;
  375. } else
  376. card->dev.type = &sd_type;
  377. }
  378. /*
  379. * If needed, disconnect card detection pull-up resistor.
  380. */
  381. err = sdio_disable_cd(card);
  382. if (err)
  383. goto remove;
  384. /*
  385. * Switch to high-speed (if supported).
  386. */
  387. err = sdio_enable_hs(card);
  388. if (err > 0)
  389. mmc_sd_go_highspeed(card);
  390. else if (err)
  391. goto remove;
  392. /*
  393. * Change to the card's maximum speed.
  394. */
  395. mmc_set_clock(host, mmc_sdio_get_max_clock(card));
  396. /*
  397. * Switch to wider bus (if supported).
  398. */
  399. err = sdio_enable_4bit_bus(card);
  400. if (err > 0)
  401. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  402. else if (err)
  403. goto remove;
  404. finish:
  405. if (!oldcard)
  406. host->card = card;
  407. return 0;
  408. remove:
  409. if (!oldcard)
  410. mmc_remove_card(card);
  411. err:
  412. return err;
  413. }
  414. /*
  415. * Host is being removed. Free up the current card.
  416. */
  417. static void mmc_sdio_remove(struct mmc_host *host)
  418. {
  419. int i;
  420. BUG_ON(!host);
  421. BUG_ON(!host->card);
  422. for (i = 0;i < host->card->sdio_funcs;i++) {
  423. if (host->card->sdio_func[i]) {
  424. sdio_remove_func(host->card->sdio_func[i]);
  425. host->card->sdio_func[i] = NULL;
  426. }
  427. }
  428. mmc_remove_card(host->card);
  429. host->card = NULL;
  430. }
  431. /*
  432. * Card detection callback from host.
  433. */
  434. static void mmc_sdio_detect(struct mmc_host *host)
  435. {
  436. int err;
  437. BUG_ON(!host);
  438. BUG_ON(!host->card);
  439. mmc_claim_host(host);
  440. /*
  441. * Just check if our card has been removed.
  442. */
  443. err = mmc_select_card(host->card);
  444. mmc_release_host(host);
  445. if (err) {
  446. mmc_sdio_remove(host);
  447. mmc_claim_host(host);
  448. mmc_detach_bus(host);
  449. mmc_release_host(host);
  450. }
  451. }
  452. /*
  453. * SDIO suspend. We need to suspend all functions separately.
  454. * Therefore all registered functions must have drivers with suspend
  455. * and resume methods. Failing that we simply remove the whole card.
  456. */
  457. static int mmc_sdio_suspend(struct mmc_host *host)
  458. {
  459. int i, err = 0;
  460. for (i = 0; i < host->card->sdio_funcs; i++) {
  461. struct sdio_func *func = host->card->sdio_func[i];
  462. if (func && sdio_func_present(func) && func->dev.driver) {
  463. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  464. if (!pmops || !pmops->suspend || !pmops->resume) {
  465. /* force removal of entire card in that case */
  466. err = -ENOSYS;
  467. } else
  468. err = pmops->suspend(&func->dev);
  469. if (err)
  470. break;
  471. }
  472. }
  473. while (err && --i >= 0) {
  474. struct sdio_func *func = host->card->sdio_func[i];
  475. if (func && sdio_func_present(func) && func->dev.driver) {
  476. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  477. pmops->resume(&func->dev);
  478. }
  479. }
  480. if (!err && host->pm_flags & MMC_PM_KEEP_POWER) {
  481. mmc_claim_host(host);
  482. sdio_disable_wide(host->card);
  483. mmc_release_host(host);
  484. }
  485. return err;
  486. }
  487. static int mmc_sdio_resume(struct mmc_host *host)
  488. {
  489. int i, err;
  490. BUG_ON(!host);
  491. BUG_ON(!host->card);
  492. /* Basic card reinitialization. */
  493. mmc_claim_host(host);
  494. err = mmc_sdio_init_card(host, host->ocr, host->card,
  495. (host->pm_flags & MMC_PM_KEEP_POWER));
  496. if (!err && host->sdio_irqs)
  497. mmc_signal_sdio_irq(host);
  498. mmc_release_host(host);
  499. /*
  500. * If the card looked to be the same as before suspending, then
  501. * we proceed to resume all card functions. If one of them returns
  502. * an error then we simply return that error to the core and the
  503. * card will be redetected as new. It is the responsibility of
  504. * the function driver to perform further tests with the extra
  505. * knowledge it has of the card to confirm the card is indeed the
  506. * same as before suspending (same MAC address for network cards,
  507. * etc.) and return an error otherwise.
  508. */
  509. for (i = 0; !err && i < host->card->sdio_funcs; i++) {
  510. struct sdio_func *func = host->card->sdio_func[i];
  511. if (func && sdio_func_present(func) && func->dev.driver) {
  512. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  513. err = pmops->resume(&func->dev);
  514. }
  515. }
  516. return err;
  517. }
  518. static int mmc_sdio_power_restore(struct mmc_host *host)
  519. {
  520. int ret;
  521. BUG_ON(!host);
  522. BUG_ON(!host->card);
  523. mmc_claim_host(host);
  524. ret = mmc_sdio_init_card(host, host->ocr, host->card,
  525. (host->pm_flags & MMC_PM_KEEP_POWER));
  526. if (!ret && host->sdio_irqs)
  527. mmc_signal_sdio_irq(host);
  528. mmc_release_host(host);
  529. return ret;
  530. }
  531. static const struct mmc_bus_ops mmc_sdio_ops = {
  532. .remove = mmc_sdio_remove,
  533. .detect = mmc_sdio_detect,
  534. .suspend = mmc_sdio_suspend,
  535. .resume = mmc_sdio_resume,
  536. .power_restore = mmc_sdio_power_restore,
  537. };
  538. /*
  539. * Starting point for SDIO card init.
  540. */
  541. int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
  542. {
  543. int err;
  544. int i, funcs;
  545. struct mmc_card *card;
  546. BUG_ON(!host);
  547. WARN_ON(!host->claimed);
  548. mmc_attach_bus(host, &mmc_sdio_ops);
  549. /*
  550. * Sanity check the voltages that the card claims to
  551. * support.
  552. */
  553. if (ocr & 0x7F) {
  554. printk(KERN_WARNING "%s: card claims to support voltages "
  555. "below the defined range. These will be ignored.\n",
  556. mmc_hostname(host));
  557. ocr &= ~0x7F;
  558. }
  559. host->ocr = mmc_select_voltage(host, ocr);
  560. /*
  561. * Can we support the voltage(s) of the card(s)?
  562. */
  563. if (!host->ocr) {
  564. err = -EINVAL;
  565. goto err;
  566. }
  567. /*
  568. * Detect and init the card.
  569. */
  570. err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
  571. if (err)
  572. goto err;
  573. card = host->card;
  574. /*
  575. * Let runtime PM core know our card is active
  576. */
  577. err = pm_runtime_set_active(&card->dev);
  578. if (err)
  579. goto remove;
  580. /*
  581. * Enable runtime PM for this card
  582. */
  583. pm_runtime_enable(&card->dev);
  584. /*
  585. * The number of functions on the card is encoded inside
  586. * the ocr.
  587. */
  588. funcs = (ocr & 0x70000000) >> 28;
  589. card->sdio_funcs = 0;
  590. /*
  591. * Initialize (but don't add) all present functions.
  592. */
  593. for (i = 0; i < funcs; i++, card->sdio_funcs++) {
  594. err = sdio_init_func(host->card, i + 1);
  595. if (err)
  596. goto remove;
  597. /*
  598. * Enable Runtime PM for this func
  599. */
  600. pm_runtime_enable(&card->sdio_func[i]->dev);
  601. }
  602. mmc_release_host(host);
  603. /*
  604. * First add the card to the driver model...
  605. */
  606. err = mmc_add_card(host->card);
  607. if (err)
  608. goto remove_added;
  609. /*
  610. * ...then the SDIO functions.
  611. */
  612. for (i = 0;i < funcs;i++) {
  613. err = sdio_add_func(host->card->sdio_func[i]);
  614. if (err)
  615. goto remove_added;
  616. }
  617. return 0;
  618. remove_added:
  619. /* Remove without lock if the device has been added. */
  620. mmc_sdio_remove(host);
  621. mmc_claim_host(host);
  622. remove:
  623. /* And with lock if it hasn't been added. */
  624. if (host->card)
  625. mmc_sdio_remove(host);
  626. err:
  627. mmc_detach_bus(host);
  628. mmc_release_host(host);
  629. printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
  630. mmc_hostname(host), err);
  631. return err;
  632. }