block.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * Block driver for media (i.e., flash cards)
  3. *
  4. * Copyright 2002 Hewlett-Packard Company
  5. * Copyright 2005-2008 Pierre Ossman
  6. *
  7. * Use consistent with the GNU GPL is permitted,
  8. * provided that this copyright notice is
  9. * preserved in its entirety in all copies and derived works.
  10. *
  11. * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
  12. * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
  13. * FITNESS FOR ANY PARTICULAR PURPOSE.
  14. *
  15. * Many thanks to Alessandro Rubini and Jonathan Corbet!
  16. *
  17. * Author: Andrew Christian
  18. * 28 May 2002
  19. */
  20. #include <linux/moduleparam.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/fs.h>
  25. #include <linux/slab.h>
  26. #include <linux/errno.h>
  27. #include <linux/hdreg.h>
  28. #include <linux/kdev_t.h>
  29. #include <linux/blkdev.h>
  30. #include <linux/mutex.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/scatterlist.h>
  33. #include <linux/string_helpers.h>
  34. #include <linux/mmc/card.h>
  35. #include <linux/mmc/host.h>
  36. #include <linux/mmc/mmc.h>
  37. #include <linux/mmc/sd.h>
  38. #include <asm/system.h>
  39. #include <asm/uaccess.h>
  40. #include "queue.h"
  41. MODULE_ALIAS("mmc:block");
  42. /*
  43. * max 8 partitions per card
  44. */
  45. #define MMC_SHIFT 3
  46. #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
  47. static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
  48. /*
  49. * There is one mmc_blk_data per slot.
  50. */
  51. struct mmc_blk_data {
  52. spinlock_t lock;
  53. struct gendisk *disk;
  54. struct mmc_queue queue;
  55. unsigned int usage;
  56. unsigned int read_only;
  57. };
  58. static DEFINE_MUTEX(open_lock);
  59. static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
  60. {
  61. struct mmc_blk_data *md;
  62. mutex_lock(&open_lock);
  63. md = disk->private_data;
  64. if (md && md->usage == 0)
  65. md = NULL;
  66. if (md)
  67. md->usage++;
  68. mutex_unlock(&open_lock);
  69. return md;
  70. }
  71. static void mmc_blk_put(struct mmc_blk_data *md)
  72. {
  73. mutex_lock(&open_lock);
  74. md->usage--;
  75. if (md->usage == 0) {
  76. int devmaj = MAJOR(disk_devt(md->disk));
  77. int devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
  78. if (!devmaj)
  79. devidx = md->disk->first_minor >> MMC_SHIFT;
  80. blk_cleanup_queue(md->queue.queue);
  81. __clear_bit(devidx, dev_use);
  82. put_disk(md->disk);
  83. kfree(md);
  84. }
  85. mutex_unlock(&open_lock);
  86. }
  87. static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
  88. {
  89. struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
  90. int ret = -ENXIO;
  91. lock_kernel();
  92. if (md) {
  93. if (md->usage == 2)
  94. check_disk_change(bdev);
  95. ret = 0;
  96. if ((mode & FMODE_WRITE) && md->read_only) {
  97. mmc_blk_put(md);
  98. ret = -EROFS;
  99. }
  100. }
  101. unlock_kernel();
  102. return ret;
  103. }
  104. static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
  105. {
  106. struct mmc_blk_data *md = disk->private_data;
  107. lock_kernel();
  108. mmc_blk_put(md);
  109. unlock_kernel();
  110. return 0;
  111. }
  112. static int
  113. mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  114. {
  115. geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  116. geo->heads = 4;
  117. geo->sectors = 16;
  118. return 0;
  119. }
  120. static const struct block_device_operations mmc_bdops = {
  121. .open = mmc_blk_open,
  122. .release = mmc_blk_release,
  123. .getgeo = mmc_blk_getgeo,
  124. .owner = THIS_MODULE,
  125. };
  126. struct mmc_blk_request {
  127. struct mmc_request mrq;
  128. struct mmc_command cmd;
  129. struct mmc_command stop;
  130. struct mmc_data data;
  131. };
  132. static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
  133. {
  134. int err;
  135. u32 result;
  136. __be32 *blocks;
  137. struct mmc_request mrq;
  138. struct mmc_command cmd;
  139. struct mmc_data data;
  140. unsigned int timeout_us;
  141. struct scatterlist sg;
  142. memset(&cmd, 0, sizeof(struct mmc_command));
  143. cmd.opcode = MMC_APP_CMD;
  144. cmd.arg = card->rca << 16;
  145. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  146. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  147. if (err)
  148. return (u32)-1;
  149. if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
  150. return (u32)-1;
  151. memset(&cmd, 0, sizeof(struct mmc_command));
  152. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  153. cmd.arg = 0;
  154. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  155. memset(&data, 0, sizeof(struct mmc_data));
  156. data.timeout_ns = card->csd.tacc_ns * 100;
  157. data.timeout_clks = card->csd.tacc_clks * 100;
  158. timeout_us = data.timeout_ns / 1000;
  159. timeout_us += data.timeout_clks * 1000 /
  160. (card->host->ios.clock / 1000);
  161. if (timeout_us > 100000) {
  162. data.timeout_ns = 100000000;
  163. data.timeout_clks = 0;
  164. }
  165. data.blksz = 4;
  166. data.blocks = 1;
  167. data.flags = MMC_DATA_READ;
  168. data.sg = &sg;
  169. data.sg_len = 1;
  170. memset(&mrq, 0, sizeof(struct mmc_request));
  171. mrq.cmd = &cmd;
  172. mrq.data = &data;
  173. blocks = kmalloc(4, GFP_KERNEL);
  174. if (!blocks)
  175. return (u32)-1;
  176. sg_init_one(&sg, blocks, 4);
  177. mmc_wait_for_req(card->host, &mrq);
  178. result = ntohl(*blocks);
  179. kfree(blocks);
  180. if (cmd.error || data.error)
  181. result = (u32)-1;
  182. return result;
  183. }
  184. static u32 get_card_status(struct mmc_card *card, struct request *req)
  185. {
  186. struct mmc_command cmd;
  187. int err;
  188. memset(&cmd, 0, sizeof(struct mmc_command));
  189. cmd.opcode = MMC_SEND_STATUS;
  190. if (!mmc_host_is_spi(card->host))
  191. cmd.arg = card->rca << 16;
  192. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  193. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  194. if (err)
  195. printk(KERN_ERR "%s: error %d sending status comand",
  196. req->rq_disk->disk_name, err);
  197. return cmd.resp[0];
  198. }
  199. static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  200. {
  201. struct mmc_blk_data *md = mq->data;
  202. struct mmc_card *card = md->queue.card;
  203. unsigned int from, nr, arg;
  204. int err = 0;
  205. mmc_claim_host(card->host);
  206. if (!mmc_can_erase(card)) {
  207. err = -EOPNOTSUPP;
  208. goto out;
  209. }
  210. from = blk_rq_pos(req);
  211. nr = blk_rq_sectors(req);
  212. if (mmc_can_trim(card))
  213. arg = MMC_TRIM_ARG;
  214. else
  215. arg = MMC_ERASE_ARG;
  216. err = mmc_erase(card, from, nr, arg);
  217. out:
  218. spin_lock_irq(&md->lock);
  219. __blk_end_request(req, err, blk_rq_bytes(req));
  220. spin_unlock_irq(&md->lock);
  221. mmc_release_host(card->host);
  222. return err ? 0 : 1;
  223. }
  224. static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  225. struct request *req)
  226. {
  227. struct mmc_blk_data *md = mq->data;
  228. struct mmc_card *card = md->queue.card;
  229. unsigned int from, nr, arg;
  230. int err = 0;
  231. mmc_claim_host(card->host);
  232. if (!mmc_can_secure_erase_trim(card)) {
  233. err = -EOPNOTSUPP;
  234. goto out;
  235. }
  236. from = blk_rq_pos(req);
  237. nr = blk_rq_sectors(req);
  238. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  239. arg = MMC_SECURE_TRIM1_ARG;
  240. else
  241. arg = MMC_SECURE_ERASE_ARG;
  242. err = mmc_erase(card, from, nr, arg);
  243. if (!err && arg == MMC_SECURE_TRIM1_ARG)
  244. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  245. out:
  246. spin_lock_irq(&md->lock);
  247. __blk_end_request(req, err, blk_rq_bytes(req));
  248. spin_unlock_irq(&md->lock);
  249. mmc_release_host(card->host);
  250. return err ? 0 : 1;
  251. }
  252. static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
  253. {
  254. struct mmc_blk_data *md = mq->data;
  255. struct mmc_card *card = md->queue.card;
  256. struct mmc_blk_request brq;
  257. int ret = 1, disable_multi = 0;
  258. mmc_claim_host(card->host);
  259. do {
  260. struct mmc_command cmd;
  261. u32 readcmd, writecmd, status = 0;
  262. memset(&brq, 0, sizeof(struct mmc_blk_request));
  263. brq.mrq.cmd = &brq.cmd;
  264. brq.mrq.data = &brq.data;
  265. brq.cmd.arg = blk_rq_pos(req);
  266. if (!mmc_card_blockaddr(card))
  267. brq.cmd.arg <<= 9;
  268. brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  269. brq.data.blksz = 512;
  270. brq.stop.opcode = MMC_STOP_TRANSMISSION;
  271. brq.stop.arg = 0;
  272. brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  273. brq.data.blocks = blk_rq_sectors(req);
  274. /*
  275. * The block layer doesn't support all sector count
  276. * restrictions, so we need to be prepared for too big
  277. * requests.
  278. */
  279. if (brq.data.blocks > card->host->max_blk_count)
  280. brq.data.blocks = card->host->max_blk_count;
  281. /*
  282. * After a read error, we redo the request one sector at a time
  283. * in order to accurately determine which sectors can be read
  284. * successfully.
  285. */
  286. if (disable_multi && brq.data.blocks > 1)
  287. brq.data.blocks = 1;
  288. if (brq.data.blocks > 1) {
  289. /* SPI multiblock writes terminate using a special
  290. * token, not a STOP_TRANSMISSION request.
  291. */
  292. if (!mmc_host_is_spi(card->host)
  293. || rq_data_dir(req) == READ)
  294. brq.mrq.stop = &brq.stop;
  295. readcmd = MMC_READ_MULTIPLE_BLOCK;
  296. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  297. } else {
  298. brq.mrq.stop = NULL;
  299. readcmd = MMC_READ_SINGLE_BLOCK;
  300. writecmd = MMC_WRITE_BLOCK;
  301. }
  302. if (rq_data_dir(req) == READ) {
  303. brq.cmd.opcode = readcmd;
  304. brq.data.flags |= MMC_DATA_READ;
  305. } else {
  306. brq.cmd.opcode = writecmd;
  307. brq.data.flags |= MMC_DATA_WRITE;
  308. }
  309. mmc_set_data_timeout(&brq.data, card);
  310. brq.data.sg = mq->sg;
  311. brq.data.sg_len = mmc_queue_map_sg(mq);
  312. /*
  313. * Adjust the sg list so it is the same size as the
  314. * request.
  315. */
  316. if (brq.data.blocks != blk_rq_sectors(req)) {
  317. int i, data_size = brq.data.blocks << 9;
  318. struct scatterlist *sg;
  319. for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
  320. data_size -= sg->length;
  321. if (data_size <= 0) {
  322. sg->length += data_size;
  323. i++;
  324. break;
  325. }
  326. }
  327. brq.data.sg_len = i;
  328. }
  329. mmc_queue_bounce_pre(mq);
  330. mmc_wait_for_req(card->host, &brq.mrq);
  331. mmc_queue_bounce_post(mq);
  332. /*
  333. * Check for errors here, but don't jump to cmd_err
  334. * until later as we need to wait for the card to leave
  335. * programming mode even when things go wrong.
  336. */
  337. if (brq.cmd.error || brq.data.error || brq.stop.error) {
  338. if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
  339. /* Redo read one sector at a time */
  340. printk(KERN_WARNING "%s: retrying using single "
  341. "block read\n", req->rq_disk->disk_name);
  342. disable_multi = 1;
  343. continue;
  344. }
  345. status = get_card_status(card, req);
  346. }
  347. if (brq.cmd.error) {
  348. printk(KERN_ERR "%s: error %d sending read/write "
  349. "command, response %#x, card status %#x\n",
  350. req->rq_disk->disk_name, brq.cmd.error,
  351. brq.cmd.resp[0], status);
  352. }
  353. if (brq.data.error) {
  354. if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
  355. /* 'Stop' response contains card status */
  356. status = brq.mrq.stop->resp[0];
  357. printk(KERN_ERR "%s: error %d transferring data,"
  358. " sector %u, nr %u, card status %#x\n",
  359. req->rq_disk->disk_name, brq.data.error,
  360. (unsigned)blk_rq_pos(req),
  361. (unsigned)blk_rq_sectors(req), status);
  362. }
  363. if (brq.stop.error) {
  364. printk(KERN_ERR "%s: error %d sending stop command, "
  365. "response %#x, card status %#x\n",
  366. req->rq_disk->disk_name, brq.stop.error,
  367. brq.stop.resp[0], status);
  368. }
  369. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  370. do {
  371. int err;
  372. cmd.opcode = MMC_SEND_STATUS;
  373. cmd.arg = card->rca << 16;
  374. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  375. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  376. if (err) {
  377. printk(KERN_ERR "%s: error %d requesting status\n",
  378. req->rq_disk->disk_name, err);
  379. goto cmd_err;
  380. }
  381. /*
  382. * Some cards mishandle the status bits,
  383. * so make sure to check both the busy
  384. * indication and the card state.
  385. */
  386. } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
  387. (R1_CURRENT_STATE(cmd.resp[0]) == 7));
  388. #if 0
  389. if (cmd.resp[0] & ~0x00000900)
  390. printk(KERN_ERR "%s: status = %08x\n",
  391. req->rq_disk->disk_name, cmd.resp[0]);
  392. if (mmc_decode_status(cmd.resp))
  393. goto cmd_err;
  394. #endif
  395. }
  396. if (brq.cmd.error || brq.stop.error || brq.data.error) {
  397. if (rq_data_dir(req) == READ) {
  398. /*
  399. * After an error, we redo I/O one sector at a
  400. * time, so we only reach here after trying to
  401. * read a single sector.
  402. */
  403. spin_lock_irq(&md->lock);
  404. ret = __blk_end_request(req, -EIO, brq.data.blksz);
  405. spin_unlock_irq(&md->lock);
  406. continue;
  407. }
  408. goto cmd_err;
  409. }
  410. /*
  411. * A block was successfully transferred.
  412. */
  413. spin_lock_irq(&md->lock);
  414. ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
  415. spin_unlock_irq(&md->lock);
  416. } while (ret);
  417. mmc_release_host(card->host);
  418. return 1;
  419. cmd_err:
  420. /*
  421. * If this is an SD card and we're writing, we can first
  422. * mark the known good sectors as ok.
  423. *
  424. * If the card is not SD, we can still ok written sectors
  425. * as reported by the controller (which might be less than
  426. * the real number of written sectors, but never more).
  427. */
  428. if (mmc_card_sd(card)) {
  429. u32 blocks;
  430. blocks = mmc_sd_num_wr_blocks(card);
  431. if (blocks != (u32)-1) {
  432. spin_lock_irq(&md->lock);
  433. ret = __blk_end_request(req, 0, blocks << 9);
  434. spin_unlock_irq(&md->lock);
  435. }
  436. } else {
  437. spin_lock_irq(&md->lock);
  438. ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
  439. spin_unlock_irq(&md->lock);
  440. }
  441. mmc_release_host(card->host);
  442. spin_lock_irq(&md->lock);
  443. while (ret)
  444. ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
  445. spin_unlock_irq(&md->lock);
  446. return 0;
  447. }
  448. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  449. {
  450. if (req->cmd_flags & REQ_DISCARD) {
  451. if (req->cmd_flags & REQ_SECURE)
  452. return mmc_blk_issue_secdiscard_rq(mq, req);
  453. else
  454. return mmc_blk_issue_discard_rq(mq, req);
  455. } else {
  456. return mmc_blk_issue_rw_rq(mq, req);
  457. }
  458. }
  459. static inline int mmc_blk_readonly(struct mmc_card *card)
  460. {
  461. return mmc_card_readonly(card) ||
  462. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  463. }
  464. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  465. {
  466. struct mmc_blk_data *md;
  467. int devidx, ret;
  468. devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
  469. if (devidx >= MMC_NUM_MINORS)
  470. return ERR_PTR(-ENOSPC);
  471. __set_bit(devidx, dev_use);
  472. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  473. if (!md) {
  474. ret = -ENOMEM;
  475. goto out;
  476. }
  477. /*
  478. * Set the read-only status based on the supported commands
  479. * and the write protect switch.
  480. */
  481. md->read_only = mmc_blk_readonly(card);
  482. md->disk = alloc_disk(1 << MMC_SHIFT);
  483. if (md->disk == NULL) {
  484. ret = -ENOMEM;
  485. goto err_kfree;
  486. }
  487. spin_lock_init(&md->lock);
  488. md->usage = 1;
  489. ret = mmc_init_queue(&md->queue, card, &md->lock);
  490. if (ret)
  491. goto err_putdisk;
  492. md->queue.issue_fn = mmc_blk_issue_rq;
  493. md->queue.data = md;
  494. md->disk->major = MMC_BLOCK_MAJOR;
  495. md->disk->first_minor = devidx << MMC_SHIFT;
  496. md->disk->fops = &mmc_bdops;
  497. md->disk->private_data = md;
  498. md->disk->queue = md->queue.queue;
  499. md->disk->driverfs_dev = &card->dev;
  500. /*
  501. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  502. *
  503. * - be set for removable media with permanent block devices
  504. * - be unset for removable block devices with permanent media
  505. *
  506. * Since MMC block devices clearly fall under the second
  507. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  508. * should use the block device creation/destruction hotplug
  509. * messages to tell when the card is present.
  510. */
  511. sprintf(md->disk->disk_name, "mmcblk%d", devidx);
  512. blk_queue_logical_block_size(md->queue.queue, 512);
  513. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  514. /*
  515. * The EXT_CSD sector count is in number or 512 byte
  516. * sectors.
  517. */
  518. set_capacity(md->disk, card->ext_csd.sectors);
  519. } else {
  520. /*
  521. * The CSD capacity field is in units of read_blkbits.
  522. * set_capacity takes units of 512 bytes.
  523. */
  524. set_capacity(md->disk,
  525. card->csd.capacity << (card->csd.read_blkbits - 9));
  526. }
  527. return md;
  528. err_putdisk:
  529. put_disk(md->disk);
  530. err_kfree:
  531. kfree(md);
  532. out:
  533. return ERR_PTR(ret);
  534. }
  535. static int
  536. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  537. {
  538. struct mmc_command cmd;
  539. int err;
  540. /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
  541. if (mmc_card_blockaddr(card))
  542. return 0;
  543. mmc_claim_host(card->host);
  544. cmd.opcode = MMC_SET_BLOCKLEN;
  545. cmd.arg = 512;
  546. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  547. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  548. mmc_release_host(card->host);
  549. if (err) {
  550. printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
  551. md->disk->disk_name, cmd.arg, err);
  552. return -EINVAL;
  553. }
  554. return 0;
  555. }
  556. static int mmc_blk_probe(struct mmc_card *card)
  557. {
  558. struct mmc_blk_data *md;
  559. int err;
  560. char cap_str[10];
  561. /*
  562. * Check that the card supports the command class(es) we need.
  563. */
  564. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  565. return -ENODEV;
  566. md = mmc_blk_alloc(card);
  567. if (IS_ERR(md))
  568. return PTR_ERR(md);
  569. err = mmc_blk_set_blksize(md, card);
  570. if (err)
  571. goto out;
  572. string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
  573. cap_str, sizeof(cap_str));
  574. printk(KERN_INFO "%s: %s %s %s %s\n",
  575. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  576. cap_str, md->read_only ? "(ro)" : "");
  577. mmc_set_drvdata(card, md);
  578. add_disk(md->disk);
  579. return 0;
  580. out:
  581. mmc_cleanup_queue(&md->queue);
  582. mmc_blk_put(md);
  583. return err;
  584. }
  585. static void mmc_blk_remove(struct mmc_card *card)
  586. {
  587. struct mmc_blk_data *md = mmc_get_drvdata(card);
  588. if (md) {
  589. /* Stop new requests from getting into the queue */
  590. del_gendisk(md->disk);
  591. /* Then flush out any already in there */
  592. mmc_cleanup_queue(&md->queue);
  593. mmc_blk_put(md);
  594. }
  595. mmc_set_drvdata(card, NULL);
  596. }
  597. #ifdef CONFIG_PM
  598. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  599. {
  600. struct mmc_blk_data *md = mmc_get_drvdata(card);
  601. if (md) {
  602. mmc_queue_suspend(&md->queue);
  603. }
  604. return 0;
  605. }
  606. static int mmc_blk_resume(struct mmc_card *card)
  607. {
  608. struct mmc_blk_data *md = mmc_get_drvdata(card);
  609. if (md) {
  610. mmc_blk_set_blksize(md, card);
  611. mmc_queue_resume(&md->queue);
  612. }
  613. return 0;
  614. }
  615. #else
  616. #define mmc_blk_suspend NULL
  617. #define mmc_blk_resume NULL
  618. #endif
  619. static struct mmc_driver mmc_driver = {
  620. .drv = {
  621. .name = "mmcblk",
  622. },
  623. .probe = mmc_blk_probe,
  624. .remove = mmc_blk_remove,
  625. .suspend = mmc_blk_suspend,
  626. .resume = mmc_blk_resume,
  627. };
  628. static int __init mmc_blk_init(void)
  629. {
  630. int res;
  631. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  632. if (res)
  633. goto out;
  634. res = mmc_register_driver(&mmc_driver);
  635. if (res)
  636. goto out2;
  637. return 0;
  638. out2:
  639. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  640. out:
  641. return res;
  642. }
  643. static void __exit mmc_blk_exit(void)
  644. {
  645. mmc_unregister_driver(&mmc_driver);
  646. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  647. }
  648. module_init(mmc_blk_init);
  649. module_exit(mmc_blk_exit);
  650. MODULE_LICENSE("GPL");
  651. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");