core.c 18 KB

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