core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /*
  2. * linux/drivers/mmc/core/core.c
  3. *
  4. * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
  5. * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
  6. * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
  7. * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/completion.h>
  17. #include <linux/device.h>
  18. #include <linux/delay.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/err.h>
  21. #include <linux/leds.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/mmc/card.h>
  24. #include <linux/mmc/host.h>
  25. #include <linux/mmc/mmc.h>
  26. #include <linux/mmc/sd.h>
  27. #include "core.h"
  28. #include "bus.h"
  29. #include "host.h"
  30. #include "sdio_bus.h"
  31. #include "mmc_ops.h"
  32. #include "sd_ops.h"
  33. #include "sdio_ops.h"
  34. static struct workqueue_struct *workqueue;
  35. /*
  36. * Enabling software CRCs on the data blocks can be a significant (30%)
  37. * performance cost, and for other reasons may not always be desired.
  38. * So we allow it it to be disabled.
  39. */
  40. int use_spi_crc = 1;
  41. module_param(use_spi_crc, bool, 0);
  42. /*
  43. * Internal function. Schedule delayed work in the MMC work queue.
  44. */
  45. static int mmc_schedule_delayed_work(struct delayed_work *work,
  46. unsigned long delay)
  47. {
  48. return queue_delayed_work(workqueue, work, delay);
  49. }
  50. /*
  51. * Internal function. Flush all scheduled work from the MMC work queue.
  52. */
  53. static void mmc_flush_scheduled_work(void)
  54. {
  55. flush_workqueue(workqueue);
  56. }
  57. /**
  58. * mmc_request_done - finish processing an MMC request
  59. * @host: MMC host which completed request
  60. * @mrq: MMC request which request
  61. *
  62. * MMC drivers should call this function when they have completed
  63. * their processing of a request.
  64. */
  65. void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
  66. {
  67. struct mmc_command *cmd = mrq->cmd;
  68. int err = cmd->error;
  69. if (err && cmd->retries && mmc_host_is_spi(host)) {
  70. if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
  71. cmd->retries = 0;
  72. }
  73. if (err && cmd->retries) {
  74. pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
  75. mmc_hostname(host), cmd->opcode, err);
  76. cmd->retries--;
  77. cmd->error = 0;
  78. host->ops->request(host, mrq);
  79. } else {
  80. led_trigger_event(host->led, LED_OFF);
  81. pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
  82. mmc_hostname(host), cmd->opcode, err,
  83. cmd->resp[0], cmd->resp[1],
  84. cmd->resp[2], cmd->resp[3]);
  85. if (mrq->data) {
  86. pr_debug("%s: %d bytes transferred: %d\n",
  87. mmc_hostname(host),
  88. mrq->data->bytes_xfered, mrq->data->error);
  89. }
  90. if (mrq->stop) {
  91. pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
  92. mmc_hostname(host), mrq->stop->opcode,
  93. mrq->stop->error,
  94. mrq->stop->resp[0], mrq->stop->resp[1],
  95. mrq->stop->resp[2], mrq->stop->resp[3]);
  96. }
  97. if (mrq->done)
  98. mrq->done(mrq);
  99. }
  100. }
  101. EXPORT_SYMBOL(mmc_request_done);
  102. static void
  103. mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  104. {
  105. #ifdef CONFIG_MMC_DEBUG
  106. unsigned int i, sz;
  107. struct scatterlist *sg;
  108. #endif
  109. pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
  110. mmc_hostname(host), mrq->cmd->opcode,
  111. mrq->cmd->arg, mrq->cmd->flags);
  112. if (mrq->data) {
  113. pr_debug("%s: blksz %d blocks %d flags %08x "
  114. "tsac %d ms nsac %d\n",
  115. mmc_hostname(host), mrq->data->blksz,
  116. mrq->data->blocks, mrq->data->flags,
  117. mrq->data->timeout_ns / 1000000,
  118. mrq->data->timeout_clks);
  119. }
  120. if (mrq->stop) {
  121. pr_debug("%s: CMD%u arg %08x flags %08x\n",
  122. mmc_hostname(host), mrq->stop->opcode,
  123. mrq->stop->arg, mrq->stop->flags);
  124. }
  125. WARN_ON(!host->claimed);
  126. led_trigger_event(host->led, LED_FULL);
  127. mrq->cmd->error = 0;
  128. mrq->cmd->mrq = mrq;
  129. if (mrq->data) {
  130. BUG_ON(mrq->data->blksz > host->max_blk_size);
  131. BUG_ON(mrq->data->blocks > host->max_blk_count);
  132. BUG_ON(mrq->data->blocks * mrq->data->blksz >
  133. host->max_req_size);
  134. #ifdef CONFIG_MMC_DEBUG
  135. sz = 0;
  136. for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
  137. sz += sg->length;
  138. BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
  139. #endif
  140. mrq->cmd->data = mrq->data;
  141. mrq->data->error = 0;
  142. mrq->data->mrq = mrq;
  143. if (mrq->stop) {
  144. mrq->data->stop = mrq->stop;
  145. mrq->stop->error = 0;
  146. mrq->stop->mrq = mrq;
  147. }
  148. }
  149. host->ops->request(host, mrq);
  150. }
  151. static void mmc_wait_done(struct mmc_request *mrq)
  152. {
  153. complete(mrq->done_data);
  154. }
  155. /**
  156. * mmc_wait_for_req - start a request and wait for completion
  157. * @host: MMC host to start command
  158. * @mrq: MMC request to start
  159. *
  160. * Start a new MMC custom command request for a host, and wait
  161. * for the command to complete. Does not attempt to parse the
  162. * response.
  163. */
  164. void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
  165. {
  166. DECLARE_COMPLETION_ONSTACK(complete);
  167. mrq->done_data = &complete;
  168. mrq->done = mmc_wait_done;
  169. mmc_start_request(host, mrq);
  170. wait_for_completion(&complete);
  171. }
  172. EXPORT_SYMBOL(mmc_wait_for_req);
  173. /**
  174. * mmc_wait_for_cmd - start a command and wait for completion
  175. * @host: MMC host to start command
  176. * @cmd: MMC command to start
  177. * @retries: maximum number of retries
  178. *
  179. * Start a new MMC command for a host, and wait for the command
  180. * to complete. Return any error that occurred while the command
  181. * was executing. Do not attempt to parse the response.
  182. */
  183. int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
  184. {
  185. struct mmc_request mrq;
  186. WARN_ON(!host->claimed);
  187. memset(&mrq, 0, sizeof(struct mmc_request));
  188. memset(cmd->resp, 0, sizeof(cmd->resp));
  189. cmd->retries = retries;
  190. mrq.cmd = cmd;
  191. cmd->data = NULL;
  192. mmc_wait_for_req(host, &mrq);
  193. return cmd->error;
  194. }
  195. EXPORT_SYMBOL(mmc_wait_for_cmd);
  196. /**
  197. * mmc_set_data_timeout - set the timeout for a data command
  198. * @data: data phase for command
  199. * @card: the MMC card associated with the data transfer
  200. *
  201. * Computes the data timeout parameters according to the
  202. * correct algorithm given the card type.
  203. */
  204. void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
  205. {
  206. unsigned int mult;
  207. /*
  208. * SDIO cards only define an upper 1 s limit on access.
  209. */
  210. if (mmc_card_sdio(card)) {
  211. data->timeout_ns = 1000000000;
  212. data->timeout_clks = 0;
  213. return;
  214. }
  215. /*
  216. * SD cards use a 100 multiplier rather than 10
  217. */
  218. mult = mmc_card_sd(card) ? 100 : 10;
  219. /*
  220. * Scale up the multiplier (and therefore the timeout) by
  221. * the r2w factor for writes.
  222. */
  223. if (data->flags & MMC_DATA_WRITE)
  224. mult <<= card->csd.r2w_factor;
  225. data->timeout_ns = card->csd.tacc_ns * mult;
  226. data->timeout_clks = card->csd.tacc_clks * mult;
  227. /*
  228. * SD cards also have an upper limit on the timeout.
  229. */
  230. if (mmc_card_sd(card)) {
  231. unsigned int timeout_us, limit_us;
  232. timeout_us = data->timeout_ns / 1000;
  233. timeout_us += data->timeout_clks * 1000 /
  234. (card->host->ios.clock / 1000);
  235. if (data->flags & MMC_DATA_WRITE)
  236. limit_us = 250000;
  237. else
  238. limit_us = 100000;
  239. /*
  240. * SDHC cards always use these fixed values.
  241. */
  242. if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
  243. data->timeout_ns = limit_us * 1000;
  244. data->timeout_clks = 0;
  245. }
  246. }
  247. }
  248. EXPORT_SYMBOL(mmc_set_data_timeout);
  249. /**
  250. * mmc_align_data_size - pads a transfer size to a more optimal value
  251. * @card: the MMC card associated with the data transfer
  252. * @sz: original transfer size
  253. *
  254. * Pads the original data size with a number of extra bytes in
  255. * order to avoid controller bugs and/or performance hits
  256. * (e.g. some controllers revert to PIO for certain sizes).
  257. *
  258. * Returns the improved size, which might be unmodified.
  259. *
  260. * Note that this function is only relevant when issuing a
  261. * single scatter gather entry.
  262. */
  263. unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
  264. {
  265. /*
  266. * FIXME: We don't have a system for the controller to tell
  267. * the core about its problems yet, so for now we just 32-bit
  268. * align the size.
  269. */
  270. sz = ((sz + 3) / 4) * 4;
  271. return sz;
  272. }
  273. EXPORT_SYMBOL(mmc_align_data_size);
  274. /**
  275. * __mmc_claim_host - exclusively claim a host
  276. * @host: mmc host to claim
  277. * @abort: whether or not the operation should be aborted
  278. *
  279. * Claim a host for a set of operations. If @abort is non null and
  280. * dereference a non-zero value then this will return prematurely with
  281. * that non-zero value without acquiring the lock. Returns zero
  282. * with the lock held otherwise.
  283. */
  284. int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
  285. {
  286. DECLARE_WAITQUEUE(wait, current);
  287. unsigned long flags;
  288. int stop;
  289. might_sleep();
  290. add_wait_queue(&host->wq, &wait);
  291. spin_lock_irqsave(&host->lock, flags);
  292. while (1) {
  293. set_current_state(TASK_UNINTERRUPTIBLE);
  294. stop = abort ? atomic_read(abort) : 0;
  295. if (stop || !host->claimed)
  296. break;
  297. spin_unlock_irqrestore(&host->lock, flags);
  298. schedule();
  299. spin_lock_irqsave(&host->lock, flags);
  300. }
  301. set_current_state(TASK_RUNNING);
  302. if (!stop)
  303. host->claimed = 1;
  304. else
  305. wake_up(&host->wq);
  306. spin_unlock_irqrestore(&host->lock, flags);
  307. remove_wait_queue(&host->wq, &wait);
  308. return stop;
  309. }
  310. EXPORT_SYMBOL(__mmc_claim_host);
  311. /**
  312. * mmc_release_host - release a host
  313. * @host: mmc host to release
  314. *
  315. * Release a MMC host, allowing others to claim the host
  316. * for their operations.
  317. */
  318. void mmc_release_host(struct mmc_host *host)
  319. {
  320. unsigned long flags;
  321. WARN_ON(!host->claimed);
  322. spin_lock_irqsave(&host->lock, flags);
  323. host->claimed = 0;
  324. spin_unlock_irqrestore(&host->lock, flags);
  325. wake_up(&host->wq);
  326. }
  327. EXPORT_SYMBOL(mmc_release_host);
  328. /*
  329. * Internal function that does the actual ios call to the host driver,
  330. * optionally printing some debug output.
  331. */
  332. static inline void mmc_set_ios(struct mmc_host *host)
  333. {
  334. struct mmc_ios *ios = &host->ios;
  335. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  336. "width %u timing %u\n",
  337. mmc_hostname(host), ios->clock, ios->bus_mode,
  338. ios->power_mode, ios->chip_select, ios->vdd,
  339. ios->bus_width, ios->timing);
  340. host->ops->set_ios(host, ios);
  341. }
  342. /*
  343. * Control chip select pin on a host.
  344. */
  345. void mmc_set_chip_select(struct mmc_host *host, int mode)
  346. {
  347. host->ios.chip_select = mode;
  348. mmc_set_ios(host);
  349. }
  350. /*
  351. * Sets the host clock to the highest possible frequency that
  352. * is below "hz".
  353. */
  354. void mmc_set_clock(struct mmc_host *host, unsigned int hz)
  355. {
  356. WARN_ON(hz < host->f_min);
  357. if (hz > host->f_max)
  358. hz = host->f_max;
  359. host->ios.clock = hz;
  360. mmc_set_ios(host);
  361. }
  362. /*
  363. * Change the bus mode (open drain/push-pull) of a host.
  364. */
  365. void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
  366. {
  367. host->ios.bus_mode = mode;
  368. mmc_set_ios(host);
  369. }
  370. /*
  371. * Change data bus width of a host.
  372. */
  373. void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
  374. {
  375. host->ios.bus_width = width;
  376. mmc_set_ios(host);
  377. }
  378. /*
  379. * Mask off any voltages we don't support and select
  380. * the lowest voltage
  381. */
  382. u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  383. {
  384. int bit;
  385. ocr &= host->ocr_avail;
  386. bit = ffs(ocr);
  387. if (bit) {
  388. bit -= 1;
  389. ocr &= 3 << bit;
  390. host->ios.vdd = bit;
  391. mmc_set_ios(host);
  392. } else {
  393. ocr = 0;
  394. }
  395. return ocr;
  396. }
  397. /*
  398. * Select timing parameters for host.
  399. */
  400. void mmc_set_timing(struct mmc_host *host, unsigned int timing)
  401. {
  402. host->ios.timing = timing;
  403. mmc_set_ios(host);
  404. }
  405. /*
  406. * Apply power to the MMC stack. This is a two-stage process.
  407. * First, we enable power to the card without the clock running.
  408. * We then wait a bit for the power to stabilise. Finally,
  409. * enable the bus drivers and clock to the card.
  410. *
  411. * We must _NOT_ enable the clock prior to power stablising.
  412. *
  413. * If a host does all the power sequencing itself, ignore the
  414. * initial MMC_POWER_UP stage.
  415. */
  416. static void mmc_power_up(struct mmc_host *host)
  417. {
  418. int bit = fls(host->ocr_avail) - 1;
  419. host->ios.vdd = bit;
  420. if (mmc_host_is_spi(host)) {
  421. host->ios.chip_select = MMC_CS_HIGH;
  422. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  423. } else {
  424. host->ios.chip_select = MMC_CS_DONTCARE;
  425. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  426. }
  427. host->ios.power_mode = MMC_POWER_UP;
  428. host->ios.bus_width = MMC_BUS_WIDTH_1;
  429. host->ios.timing = MMC_TIMING_LEGACY;
  430. mmc_set_ios(host);
  431. /*
  432. * This delay should be sufficient to allow the power supply
  433. * to reach the minimum voltage.
  434. */
  435. mmc_delay(2);
  436. host->ios.clock = host->f_min;
  437. host->ios.power_mode = MMC_POWER_ON;
  438. mmc_set_ios(host);
  439. /*
  440. * This delay must be at least 74 clock sizes, or 1 ms, or the
  441. * time required to reach a stable voltage.
  442. */
  443. mmc_delay(2);
  444. }
  445. static void mmc_power_off(struct mmc_host *host)
  446. {
  447. host->ios.clock = 0;
  448. host->ios.vdd = 0;
  449. if (!mmc_host_is_spi(host)) {
  450. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  451. host->ios.chip_select = MMC_CS_DONTCARE;
  452. }
  453. host->ios.power_mode = MMC_POWER_OFF;
  454. host->ios.bus_width = MMC_BUS_WIDTH_1;
  455. host->ios.timing = MMC_TIMING_LEGACY;
  456. mmc_set_ios(host);
  457. }
  458. /*
  459. * Cleanup when the last reference to the bus operator is dropped.
  460. */
  461. static void __mmc_release_bus(struct mmc_host *host)
  462. {
  463. BUG_ON(!host);
  464. BUG_ON(host->bus_refs);
  465. BUG_ON(!host->bus_dead);
  466. host->bus_ops = NULL;
  467. }
  468. /*
  469. * Increase reference count of bus operator
  470. */
  471. static inline void mmc_bus_get(struct mmc_host *host)
  472. {
  473. unsigned long flags;
  474. spin_lock_irqsave(&host->lock, flags);
  475. host->bus_refs++;
  476. spin_unlock_irqrestore(&host->lock, flags);
  477. }
  478. /*
  479. * Decrease reference count of bus operator and free it if
  480. * it is the last reference.
  481. */
  482. static inline void mmc_bus_put(struct mmc_host *host)
  483. {
  484. unsigned long flags;
  485. spin_lock_irqsave(&host->lock, flags);
  486. host->bus_refs--;
  487. if ((host->bus_refs == 0) && host->bus_ops)
  488. __mmc_release_bus(host);
  489. spin_unlock_irqrestore(&host->lock, flags);
  490. }
  491. /*
  492. * Assign a mmc bus handler to a host. Only one bus handler may control a
  493. * host at any given time.
  494. */
  495. void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
  496. {
  497. unsigned long flags;
  498. BUG_ON(!host);
  499. BUG_ON(!ops);
  500. WARN_ON(!host->claimed);
  501. spin_lock_irqsave(&host->lock, flags);
  502. BUG_ON(host->bus_ops);
  503. BUG_ON(host->bus_refs);
  504. host->bus_ops = ops;
  505. host->bus_refs = 1;
  506. host->bus_dead = 0;
  507. spin_unlock_irqrestore(&host->lock, flags);
  508. }
  509. /*
  510. * Remove the current bus handler from a host. Assumes that there are
  511. * no interesting cards left, so the bus is powered down.
  512. */
  513. void mmc_detach_bus(struct mmc_host *host)
  514. {
  515. unsigned long flags;
  516. BUG_ON(!host);
  517. WARN_ON(!host->claimed);
  518. WARN_ON(!host->bus_ops);
  519. spin_lock_irqsave(&host->lock, flags);
  520. host->bus_dead = 1;
  521. spin_unlock_irqrestore(&host->lock, flags);
  522. mmc_power_off(host);
  523. mmc_bus_put(host);
  524. }
  525. /**
  526. * mmc_detect_change - process change of state on a MMC socket
  527. * @host: host which changed state.
  528. * @delay: optional delay to wait before detection (jiffies)
  529. *
  530. * MMC drivers should call this when they detect a card has been
  531. * inserted or removed. The MMC layer will confirm that any
  532. * present card is still functional, and initialize any newly
  533. * inserted.
  534. */
  535. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  536. {
  537. #ifdef CONFIG_MMC_DEBUG
  538. unsigned long flags;
  539. spin_lock_irqsave(&host->lock, flags);
  540. WARN_ON(host->removed);
  541. spin_unlock_irqrestore(&host->lock, flags);
  542. #endif
  543. mmc_schedule_delayed_work(&host->detect, delay);
  544. }
  545. EXPORT_SYMBOL(mmc_detect_change);
  546. void mmc_rescan(struct work_struct *work)
  547. {
  548. struct mmc_host *host =
  549. container_of(work, struct mmc_host, detect.work);
  550. u32 ocr;
  551. int err;
  552. mmc_bus_get(host);
  553. if (host->bus_ops == NULL) {
  554. /*
  555. * Only we can add a new handler, so it's safe to
  556. * release the lock here.
  557. */
  558. mmc_bus_put(host);
  559. if (host->ops->get_cd && host->ops->get_cd(host) == 0)
  560. goto out;
  561. mmc_claim_host(host);
  562. mmc_power_up(host);
  563. mmc_go_idle(host);
  564. mmc_send_if_cond(host, host->ocr_avail);
  565. /*
  566. * First we search for SDIO...
  567. */
  568. err = mmc_send_io_op_cond(host, 0, &ocr);
  569. if (!err) {
  570. if (mmc_attach_sdio(host, ocr))
  571. mmc_power_off(host);
  572. goto out;
  573. }
  574. /*
  575. * ...then normal SD...
  576. */
  577. err = mmc_send_app_op_cond(host, 0, &ocr);
  578. if (!err) {
  579. if (mmc_attach_sd(host, ocr))
  580. mmc_power_off(host);
  581. goto out;
  582. }
  583. /*
  584. * ...and finally MMC.
  585. */
  586. err = mmc_send_op_cond(host, 0, &ocr);
  587. if (!err) {
  588. if (mmc_attach_mmc(host, ocr))
  589. mmc_power_off(host);
  590. goto out;
  591. }
  592. mmc_release_host(host);
  593. mmc_power_off(host);
  594. } else {
  595. if (host->bus_ops->detect && !host->bus_dead)
  596. host->bus_ops->detect(host);
  597. mmc_bus_put(host);
  598. }
  599. out:
  600. if (host->caps & MMC_CAP_NEEDS_POLL)
  601. mmc_schedule_delayed_work(&host->detect, HZ);
  602. }
  603. void mmc_start_host(struct mmc_host *host)
  604. {
  605. mmc_power_off(host);
  606. mmc_detect_change(host, 0);
  607. }
  608. void mmc_stop_host(struct mmc_host *host)
  609. {
  610. #ifdef CONFIG_MMC_DEBUG
  611. unsigned long flags;
  612. spin_lock_irqsave(&host->lock, flags);
  613. host->removed = 1;
  614. spin_unlock_irqrestore(&host->lock, flags);
  615. #endif
  616. mmc_flush_scheduled_work();
  617. mmc_bus_get(host);
  618. if (host->bus_ops && !host->bus_dead) {
  619. if (host->bus_ops->remove)
  620. host->bus_ops->remove(host);
  621. mmc_claim_host(host);
  622. mmc_detach_bus(host);
  623. mmc_release_host(host);
  624. }
  625. mmc_bus_put(host);
  626. BUG_ON(host->card);
  627. mmc_power_off(host);
  628. }
  629. #ifdef CONFIG_PM
  630. /**
  631. * mmc_suspend_host - suspend a host
  632. * @host: mmc host
  633. * @state: suspend mode (PM_SUSPEND_xxx)
  634. */
  635. int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
  636. {
  637. mmc_flush_scheduled_work();
  638. mmc_bus_get(host);
  639. if (host->bus_ops && !host->bus_dead) {
  640. if (host->bus_ops->suspend)
  641. host->bus_ops->suspend(host);
  642. if (!host->bus_ops->resume) {
  643. if (host->bus_ops->remove)
  644. host->bus_ops->remove(host);
  645. mmc_claim_host(host);
  646. mmc_detach_bus(host);
  647. mmc_release_host(host);
  648. }
  649. }
  650. mmc_bus_put(host);
  651. mmc_power_off(host);
  652. return 0;
  653. }
  654. EXPORT_SYMBOL(mmc_suspend_host);
  655. /**
  656. * mmc_resume_host - resume a previously suspended host
  657. * @host: mmc host
  658. */
  659. int mmc_resume_host(struct mmc_host *host)
  660. {
  661. mmc_bus_get(host);
  662. if (host->bus_ops && !host->bus_dead) {
  663. mmc_power_up(host);
  664. BUG_ON(!host->bus_ops->resume);
  665. host->bus_ops->resume(host);
  666. }
  667. mmc_bus_put(host);
  668. /*
  669. * We add a slight delay here so that resume can progress
  670. * in parallel.
  671. */
  672. mmc_detect_change(host, 1);
  673. return 0;
  674. }
  675. EXPORT_SYMBOL(mmc_resume_host);
  676. #endif
  677. static int __init mmc_init(void)
  678. {
  679. int ret;
  680. workqueue = create_singlethread_workqueue("kmmcd");
  681. if (!workqueue)
  682. return -ENOMEM;
  683. ret = mmc_register_bus();
  684. if (ret)
  685. goto destroy_workqueue;
  686. ret = mmc_register_host_class();
  687. if (ret)
  688. goto unregister_bus;
  689. ret = sdio_register_bus();
  690. if (ret)
  691. goto unregister_host_class;
  692. return 0;
  693. unregister_host_class:
  694. mmc_unregister_host_class();
  695. unregister_bus:
  696. mmc_unregister_bus();
  697. destroy_workqueue:
  698. destroy_workqueue(workqueue);
  699. return ret;
  700. }
  701. static void __exit mmc_exit(void)
  702. {
  703. sdio_unregister_bus();
  704. mmc_unregister_host_class();
  705. mmc_unregister_bus();
  706. destroy_workqueue(workqueue);
  707. }
  708. subsys_initcall(mmc_init);
  709. module_exit(mmc_exit);
  710. MODULE_LICENSE("GPL");