block.c 18 KB

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