core.c 18 KB

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