core.c 18 KB

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