block.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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/delay.h>
  34. #include <linux/capability.h>
  35. #include <linux/compat.h>
  36. #include <linux/mmc/ioctl.h>
  37. #include <linux/mmc/card.h>
  38. #include <linux/mmc/host.h>
  39. #include <linux/mmc/mmc.h>
  40. #include <linux/mmc/sd.h>
  41. #include <asm/system.h>
  42. #include <asm/uaccess.h>
  43. #include "queue.h"
  44. MODULE_ALIAS("mmc:block");
  45. #ifdef MODULE_PARAM_PREFIX
  46. #undef MODULE_PARAM_PREFIX
  47. #endif
  48. #define MODULE_PARAM_PREFIX "mmcblk."
  49. #define INAND_CMD38_ARG_EXT_CSD 113
  50. #define INAND_CMD38_ARG_ERASE 0x00
  51. #define INAND_CMD38_ARG_TRIM 0x01
  52. #define INAND_CMD38_ARG_SECERASE 0x80
  53. #define INAND_CMD38_ARG_SECTRIM1 0x81
  54. #define INAND_CMD38_ARG_SECTRIM2 0x88
  55. #define REL_WRITES_SUPPORTED(card) (mmc_card_mmc((card)) && \
  56. (((card)->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || \
  57. ((card)->ext_csd.rel_sectors)))
  58. static DEFINE_MUTEX(block_mutex);
  59. /*
  60. * The defaults come from config options but can be overriden by module
  61. * or bootarg options.
  62. */
  63. static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
  64. /*
  65. * We've only got one major, so number of mmcblk devices is
  66. * limited to 256 / number of minors per device.
  67. */
  68. static int max_devices;
  69. /* 256 minors, so at most 256 separate devices */
  70. static DECLARE_BITMAP(dev_use, 256);
  71. static DECLARE_BITMAP(name_use, 256);
  72. /*
  73. * There is one mmc_blk_data per slot.
  74. */
  75. struct mmc_blk_data {
  76. spinlock_t lock;
  77. struct gendisk *disk;
  78. struct mmc_queue queue;
  79. struct list_head part;
  80. unsigned int usage;
  81. unsigned int read_only;
  82. unsigned int part_type;
  83. unsigned int name_idx;
  84. /*
  85. * Only set in main mmc_blk_data associated
  86. * with mmc_card with mmc_set_drvdata, and keeps
  87. * track of the current selected device partition.
  88. */
  89. unsigned int part_curr;
  90. struct device_attribute force_ro;
  91. };
  92. static DEFINE_MUTEX(open_lock);
  93. module_param(perdev_minors, int, 0444);
  94. MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
  95. static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
  96. {
  97. struct mmc_blk_data *md;
  98. mutex_lock(&open_lock);
  99. md = disk->private_data;
  100. if (md && md->usage == 0)
  101. md = NULL;
  102. if (md)
  103. md->usage++;
  104. mutex_unlock(&open_lock);
  105. return md;
  106. }
  107. static inline int mmc_get_devidx(struct gendisk *disk)
  108. {
  109. int devmaj = MAJOR(disk_devt(disk));
  110. int devidx = MINOR(disk_devt(disk)) / perdev_minors;
  111. if (!devmaj)
  112. devidx = disk->first_minor / perdev_minors;
  113. return devidx;
  114. }
  115. static void mmc_blk_put(struct mmc_blk_data *md)
  116. {
  117. mutex_lock(&open_lock);
  118. md->usage--;
  119. if (md->usage == 0) {
  120. int devidx = mmc_get_devidx(md->disk);
  121. blk_cleanup_queue(md->queue.queue);
  122. __clear_bit(devidx, dev_use);
  123. put_disk(md->disk);
  124. kfree(md);
  125. }
  126. mutex_unlock(&open_lock);
  127. }
  128. static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
  129. char *buf)
  130. {
  131. int ret;
  132. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  133. ret = snprintf(buf, PAGE_SIZE, "%d",
  134. get_disk_ro(dev_to_disk(dev)) ^
  135. md->read_only);
  136. mmc_blk_put(md);
  137. return ret;
  138. }
  139. static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
  140. const char *buf, size_t count)
  141. {
  142. int ret;
  143. char *end;
  144. struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
  145. unsigned long set = simple_strtoul(buf, &end, 0);
  146. if (end == buf) {
  147. ret = -EINVAL;
  148. goto out;
  149. }
  150. set_disk_ro(dev_to_disk(dev), set || md->read_only);
  151. ret = count;
  152. out:
  153. mmc_blk_put(md);
  154. return ret;
  155. }
  156. static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
  157. {
  158. struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
  159. int ret = -ENXIO;
  160. mutex_lock(&block_mutex);
  161. if (md) {
  162. if (md->usage == 2)
  163. check_disk_change(bdev);
  164. ret = 0;
  165. if ((mode & FMODE_WRITE) && md->read_only) {
  166. mmc_blk_put(md);
  167. ret = -EROFS;
  168. }
  169. }
  170. mutex_unlock(&block_mutex);
  171. return ret;
  172. }
  173. static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
  174. {
  175. struct mmc_blk_data *md = disk->private_data;
  176. mutex_lock(&block_mutex);
  177. mmc_blk_put(md);
  178. mutex_unlock(&block_mutex);
  179. return 0;
  180. }
  181. static int
  182. mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  183. {
  184. geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  185. geo->heads = 4;
  186. geo->sectors = 16;
  187. return 0;
  188. }
  189. struct mmc_blk_ioc_data {
  190. struct mmc_ioc_cmd ic;
  191. unsigned char *buf;
  192. u64 buf_bytes;
  193. };
  194. static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
  195. struct mmc_ioc_cmd __user *user)
  196. {
  197. struct mmc_blk_ioc_data *idata;
  198. int err;
  199. idata = kzalloc(sizeof(*idata), GFP_KERNEL);
  200. if (!idata) {
  201. err = -ENOMEM;
  202. goto out;
  203. }
  204. if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
  205. err = -EFAULT;
  206. goto idata_err;
  207. }
  208. idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
  209. if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
  210. err = -EOVERFLOW;
  211. goto idata_err;
  212. }
  213. idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
  214. if (!idata->buf) {
  215. err = -ENOMEM;
  216. goto idata_err;
  217. }
  218. if (copy_from_user(idata->buf, (void __user *)(unsigned long)
  219. idata->ic.data_ptr, idata->buf_bytes)) {
  220. err = -EFAULT;
  221. goto copy_err;
  222. }
  223. return idata;
  224. copy_err:
  225. kfree(idata->buf);
  226. idata_err:
  227. kfree(idata);
  228. out:
  229. return ERR_PTR(err);
  230. }
  231. static int mmc_blk_ioctl_cmd(struct block_device *bdev,
  232. struct mmc_ioc_cmd __user *ic_ptr)
  233. {
  234. struct mmc_blk_ioc_data *idata;
  235. struct mmc_blk_data *md;
  236. struct mmc_card *card;
  237. struct mmc_command cmd = {0};
  238. struct mmc_data data = {0};
  239. struct mmc_request mrq = {0};
  240. struct scatterlist sg;
  241. int err;
  242. /*
  243. * The caller must have CAP_SYS_RAWIO, and must be calling this on the
  244. * whole block device, not on a partition. This prevents overspray
  245. * between sibling partitions.
  246. */
  247. if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
  248. return -EPERM;
  249. idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
  250. if (IS_ERR(idata))
  251. return PTR_ERR(idata);
  252. cmd.opcode = idata->ic.opcode;
  253. cmd.arg = idata->ic.arg;
  254. cmd.flags = idata->ic.flags;
  255. data.sg = &sg;
  256. data.sg_len = 1;
  257. data.blksz = idata->ic.blksz;
  258. data.blocks = idata->ic.blocks;
  259. sg_init_one(data.sg, idata->buf, idata->buf_bytes);
  260. if (idata->ic.write_flag)
  261. data.flags = MMC_DATA_WRITE;
  262. else
  263. data.flags = MMC_DATA_READ;
  264. mrq.cmd = &cmd;
  265. mrq.data = &data;
  266. md = mmc_blk_get(bdev->bd_disk);
  267. if (!md) {
  268. err = -EINVAL;
  269. goto cmd_done;
  270. }
  271. card = md->queue.card;
  272. if (IS_ERR(card)) {
  273. err = PTR_ERR(card);
  274. goto cmd_done;
  275. }
  276. mmc_claim_host(card->host);
  277. if (idata->ic.is_acmd) {
  278. err = mmc_app_cmd(card->host, card);
  279. if (err)
  280. goto cmd_rel_host;
  281. }
  282. /* data.flags must already be set before doing this. */
  283. mmc_set_data_timeout(&data, card);
  284. /* Allow overriding the timeout_ns for empirical tuning. */
  285. if (idata->ic.data_timeout_ns)
  286. data.timeout_ns = idata->ic.data_timeout_ns;
  287. if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
  288. /*
  289. * Pretend this is a data transfer and rely on the host driver
  290. * to compute timeout. When all host drivers support
  291. * cmd.cmd_timeout for R1B, this can be changed to:
  292. *
  293. * mrq.data = NULL;
  294. * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
  295. */
  296. data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
  297. }
  298. mmc_wait_for_req(card->host, &mrq);
  299. if (cmd.error) {
  300. dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
  301. __func__, cmd.error);
  302. err = cmd.error;
  303. goto cmd_rel_host;
  304. }
  305. if (data.error) {
  306. dev_err(mmc_dev(card->host), "%s: data error %d\n",
  307. __func__, data.error);
  308. err = data.error;
  309. goto cmd_rel_host;
  310. }
  311. /*
  312. * According to the SD specs, some commands require a delay after
  313. * issuing the command.
  314. */
  315. if (idata->ic.postsleep_min_us)
  316. usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
  317. if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
  318. err = -EFAULT;
  319. goto cmd_rel_host;
  320. }
  321. if (!idata->ic.write_flag) {
  322. if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
  323. idata->buf, idata->buf_bytes)) {
  324. err = -EFAULT;
  325. goto cmd_rel_host;
  326. }
  327. }
  328. cmd_rel_host:
  329. mmc_release_host(card->host);
  330. cmd_done:
  331. mmc_blk_put(md);
  332. kfree(idata->buf);
  333. kfree(idata);
  334. return err;
  335. }
  336. static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
  337. unsigned int cmd, unsigned long arg)
  338. {
  339. int ret = -EINVAL;
  340. if (cmd == MMC_IOC_CMD)
  341. ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
  342. return ret;
  343. }
  344. #ifdef CONFIG_COMPAT
  345. static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
  346. unsigned int cmd, unsigned long arg)
  347. {
  348. return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
  349. }
  350. #endif
  351. static const struct block_device_operations mmc_bdops = {
  352. .open = mmc_blk_open,
  353. .release = mmc_blk_release,
  354. .getgeo = mmc_blk_getgeo,
  355. .owner = THIS_MODULE,
  356. .ioctl = mmc_blk_ioctl,
  357. #ifdef CONFIG_COMPAT
  358. .compat_ioctl = mmc_blk_compat_ioctl,
  359. #endif
  360. };
  361. struct mmc_blk_request {
  362. struct mmc_request mrq;
  363. struct mmc_command cmd;
  364. struct mmc_command stop;
  365. struct mmc_data data;
  366. };
  367. static inline int mmc_blk_part_switch(struct mmc_card *card,
  368. struct mmc_blk_data *md)
  369. {
  370. int ret;
  371. struct mmc_blk_data *main_md = mmc_get_drvdata(card);
  372. if (main_md->part_curr == md->part_type)
  373. return 0;
  374. if (mmc_card_mmc(card)) {
  375. card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  376. card->ext_csd.part_config |= md->part_type;
  377. ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  378. EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
  379. card->ext_csd.part_time);
  380. if (ret)
  381. return ret;
  382. }
  383. main_md->part_curr = md->part_type;
  384. return 0;
  385. }
  386. static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
  387. {
  388. int err;
  389. u32 result;
  390. __be32 *blocks;
  391. struct mmc_request mrq = {0};
  392. struct mmc_command cmd = {0};
  393. struct mmc_data data = {0};
  394. unsigned int timeout_us;
  395. struct scatterlist sg;
  396. cmd.opcode = MMC_APP_CMD;
  397. cmd.arg = card->rca << 16;
  398. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  399. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  400. if (err)
  401. return (u32)-1;
  402. if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
  403. return (u32)-1;
  404. memset(&cmd, 0, sizeof(struct mmc_command));
  405. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  406. cmd.arg = 0;
  407. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  408. data.timeout_ns = card->csd.tacc_ns * 100;
  409. data.timeout_clks = card->csd.tacc_clks * 100;
  410. timeout_us = data.timeout_ns / 1000;
  411. timeout_us += data.timeout_clks * 1000 /
  412. (card->host->ios.clock / 1000);
  413. if (timeout_us > 100000) {
  414. data.timeout_ns = 100000000;
  415. data.timeout_clks = 0;
  416. }
  417. data.blksz = 4;
  418. data.blocks = 1;
  419. data.flags = MMC_DATA_READ;
  420. data.sg = &sg;
  421. data.sg_len = 1;
  422. mrq.cmd = &cmd;
  423. mrq.data = &data;
  424. blocks = kmalloc(4, GFP_KERNEL);
  425. if (!blocks)
  426. return (u32)-1;
  427. sg_init_one(&sg, blocks, 4);
  428. mmc_wait_for_req(card->host, &mrq);
  429. result = ntohl(*blocks);
  430. kfree(blocks);
  431. if (cmd.error || data.error)
  432. result = (u32)-1;
  433. return result;
  434. }
  435. static u32 get_card_status(struct mmc_card *card, struct request *req)
  436. {
  437. struct mmc_command cmd = {0};
  438. int err;
  439. cmd.opcode = MMC_SEND_STATUS;
  440. if (!mmc_host_is_spi(card->host))
  441. cmd.arg = card->rca << 16;
  442. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  443. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  444. if (err)
  445. printk(KERN_ERR "%s: error %d sending status command",
  446. req->rq_disk->disk_name, err);
  447. return cmd.resp[0];
  448. }
  449. static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
  450. {
  451. struct mmc_blk_data *md = mq->data;
  452. struct mmc_card *card = md->queue.card;
  453. unsigned int from, nr, arg;
  454. int err = 0;
  455. if (!mmc_can_erase(card)) {
  456. err = -EOPNOTSUPP;
  457. goto out;
  458. }
  459. from = blk_rq_pos(req);
  460. nr = blk_rq_sectors(req);
  461. if (mmc_can_trim(card))
  462. arg = MMC_TRIM_ARG;
  463. else
  464. arg = MMC_ERASE_ARG;
  465. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  466. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  467. INAND_CMD38_ARG_EXT_CSD,
  468. arg == MMC_TRIM_ARG ?
  469. INAND_CMD38_ARG_TRIM :
  470. INAND_CMD38_ARG_ERASE,
  471. 0);
  472. if (err)
  473. goto out;
  474. }
  475. err = mmc_erase(card, from, nr, arg);
  476. out:
  477. spin_lock_irq(&md->lock);
  478. __blk_end_request(req, err, blk_rq_bytes(req));
  479. spin_unlock_irq(&md->lock);
  480. return err ? 0 : 1;
  481. }
  482. static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
  483. struct request *req)
  484. {
  485. struct mmc_blk_data *md = mq->data;
  486. struct mmc_card *card = md->queue.card;
  487. unsigned int from, nr, arg;
  488. int err = 0;
  489. if (!mmc_can_secure_erase_trim(card)) {
  490. err = -EOPNOTSUPP;
  491. goto out;
  492. }
  493. from = blk_rq_pos(req);
  494. nr = blk_rq_sectors(req);
  495. if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
  496. arg = MMC_SECURE_TRIM1_ARG;
  497. else
  498. arg = MMC_SECURE_ERASE_ARG;
  499. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  500. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  501. INAND_CMD38_ARG_EXT_CSD,
  502. arg == MMC_SECURE_TRIM1_ARG ?
  503. INAND_CMD38_ARG_SECTRIM1 :
  504. INAND_CMD38_ARG_SECERASE,
  505. 0);
  506. if (err)
  507. goto out;
  508. }
  509. err = mmc_erase(card, from, nr, arg);
  510. if (!err && arg == MMC_SECURE_TRIM1_ARG) {
  511. if (card->quirks & MMC_QUIRK_INAND_CMD38) {
  512. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  513. INAND_CMD38_ARG_EXT_CSD,
  514. INAND_CMD38_ARG_SECTRIM2,
  515. 0);
  516. if (err)
  517. goto out;
  518. }
  519. err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
  520. }
  521. out:
  522. spin_lock_irq(&md->lock);
  523. __blk_end_request(req, err, blk_rq_bytes(req));
  524. spin_unlock_irq(&md->lock);
  525. return err ? 0 : 1;
  526. }
  527. static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
  528. {
  529. struct mmc_blk_data *md = mq->data;
  530. /*
  531. * No-op, only service this because we need REQ_FUA for reliable
  532. * writes.
  533. */
  534. spin_lock_irq(&md->lock);
  535. __blk_end_request_all(req, 0);
  536. spin_unlock_irq(&md->lock);
  537. return 1;
  538. }
  539. /*
  540. * Reformat current write as a reliable write, supporting
  541. * both legacy and the enhanced reliable write MMC cards.
  542. * In each transfer we'll handle only as much as a single
  543. * reliable write can handle, thus finish the request in
  544. * partial completions.
  545. */
  546. static inline int mmc_apply_rel_rw(struct mmc_blk_request *brq,
  547. struct mmc_card *card,
  548. struct request *req)
  549. {
  550. int err;
  551. struct mmc_command set_count = {0};
  552. if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
  553. /* Legacy mode imposes restrictions on transfers. */
  554. if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
  555. brq->data.blocks = 1;
  556. if (brq->data.blocks > card->ext_csd.rel_sectors)
  557. brq->data.blocks = card->ext_csd.rel_sectors;
  558. else if (brq->data.blocks < card->ext_csd.rel_sectors)
  559. brq->data.blocks = 1;
  560. }
  561. set_count.opcode = MMC_SET_BLOCK_COUNT;
  562. set_count.arg = brq->data.blocks | (1 << 31);
  563. set_count.flags = MMC_RSP_R1 | MMC_CMD_AC;
  564. err = mmc_wait_for_cmd(card->host, &set_count, 0);
  565. if (err)
  566. printk(KERN_ERR "%s: error %d SET_BLOCK_COUNT\n",
  567. req->rq_disk->disk_name, err);
  568. return err;
  569. }
  570. static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
  571. {
  572. struct mmc_blk_data *md = mq->data;
  573. struct mmc_card *card = md->queue.card;
  574. struct mmc_blk_request brq;
  575. int ret = 1, disable_multi = 0;
  576. /*
  577. * Reliable writes are used to implement Forced Unit Access and
  578. * REQ_META accesses, and are supported only on MMCs.
  579. */
  580. bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
  581. (req->cmd_flags & REQ_META)) &&
  582. (rq_data_dir(req) == WRITE) &&
  583. REL_WRITES_SUPPORTED(card);
  584. do {
  585. struct mmc_command cmd = {0};
  586. u32 readcmd, writecmd, status = 0;
  587. memset(&brq, 0, sizeof(struct mmc_blk_request));
  588. brq.mrq.cmd = &brq.cmd;
  589. brq.mrq.data = &brq.data;
  590. brq.cmd.arg = blk_rq_pos(req);
  591. if (!mmc_card_blockaddr(card))
  592. brq.cmd.arg <<= 9;
  593. brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  594. brq.data.blksz = 512;
  595. brq.stop.opcode = MMC_STOP_TRANSMISSION;
  596. brq.stop.arg = 0;
  597. brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  598. brq.data.blocks = blk_rq_sectors(req);
  599. /*
  600. * The block layer doesn't support all sector count
  601. * restrictions, so we need to be prepared for too big
  602. * requests.
  603. */
  604. if (brq.data.blocks > card->host->max_blk_count)
  605. brq.data.blocks = card->host->max_blk_count;
  606. /*
  607. * After a read error, we redo the request one sector at a time
  608. * in order to accurately determine which sectors can be read
  609. * successfully.
  610. */
  611. if (disable_multi && brq.data.blocks > 1)
  612. brq.data.blocks = 1;
  613. if (brq.data.blocks > 1 || do_rel_wr) {
  614. /* SPI multiblock writes terminate using a special
  615. * token, not a STOP_TRANSMISSION request. Reliable
  616. * writes use SET_BLOCK_COUNT and do not use a
  617. * STOP_TRANSMISSION request either.
  618. */
  619. if ((!mmc_host_is_spi(card->host) && !do_rel_wr) ||
  620. rq_data_dir(req) == READ)
  621. brq.mrq.stop = &brq.stop;
  622. readcmd = MMC_READ_MULTIPLE_BLOCK;
  623. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  624. } else {
  625. brq.mrq.stop = NULL;
  626. readcmd = MMC_READ_SINGLE_BLOCK;
  627. writecmd = MMC_WRITE_BLOCK;
  628. }
  629. if (rq_data_dir(req) == READ) {
  630. brq.cmd.opcode = readcmd;
  631. brq.data.flags |= MMC_DATA_READ;
  632. } else {
  633. brq.cmd.opcode = writecmd;
  634. brq.data.flags |= MMC_DATA_WRITE;
  635. }
  636. if (do_rel_wr && mmc_apply_rel_rw(&brq, card, req))
  637. goto cmd_err;
  638. mmc_set_data_timeout(&brq.data, card);
  639. brq.data.sg = mq->sg;
  640. brq.data.sg_len = mmc_queue_map_sg(mq);
  641. /*
  642. * Adjust the sg list so it is the same size as the
  643. * request.
  644. */
  645. if (brq.data.blocks != blk_rq_sectors(req)) {
  646. int i, data_size = brq.data.blocks << 9;
  647. struct scatterlist *sg;
  648. for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
  649. data_size -= sg->length;
  650. if (data_size <= 0) {
  651. sg->length += data_size;
  652. i++;
  653. break;
  654. }
  655. }
  656. brq.data.sg_len = i;
  657. }
  658. mmc_queue_bounce_pre(mq);
  659. mmc_wait_for_req(card->host, &brq.mrq);
  660. mmc_queue_bounce_post(mq);
  661. /*
  662. * Check for errors here, but don't jump to cmd_err
  663. * until later as we need to wait for the card to leave
  664. * programming mode even when things go wrong.
  665. */
  666. if (brq.cmd.error || brq.data.error || brq.stop.error) {
  667. if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
  668. /* Redo read one sector at a time */
  669. printk(KERN_WARNING "%s: retrying using single "
  670. "block read\n", req->rq_disk->disk_name);
  671. disable_multi = 1;
  672. continue;
  673. }
  674. status = get_card_status(card, req);
  675. }
  676. if (brq.cmd.error) {
  677. printk(KERN_ERR "%s: error %d sending read/write "
  678. "command, response %#x, card status %#x\n",
  679. req->rq_disk->disk_name, brq.cmd.error,
  680. brq.cmd.resp[0], status);
  681. }
  682. if (brq.data.error) {
  683. if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
  684. /* 'Stop' response contains card status */
  685. status = brq.mrq.stop->resp[0];
  686. printk(KERN_ERR "%s: error %d transferring data,"
  687. " sector %u, nr %u, card status %#x\n",
  688. req->rq_disk->disk_name, brq.data.error,
  689. (unsigned)blk_rq_pos(req),
  690. (unsigned)blk_rq_sectors(req), status);
  691. }
  692. if (brq.stop.error) {
  693. printk(KERN_ERR "%s: error %d sending stop command, "
  694. "response %#x, card status %#x\n",
  695. req->rq_disk->disk_name, brq.stop.error,
  696. brq.stop.resp[0], status);
  697. }
  698. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  699. do {
  700. int err;
  701. cmd.opcode = MMC_SEND_STATUS;
  702. cmd.arg = card->rca << 16;
  703. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  704. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  705. if (err) {
  706. printk(KERN_ERR "%s: error %d requesting status\n",
  707. req->rq_disk->disk_name, err);
  708. goto cmd_err;
  709. }
  710. /*
  711. * Some cards mishandle the status bits,
  712. * so make sure to check both the busy
  713. * indication and the card state.
  714. */
  715. } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
  716. (R1_CURRENT_STATE(cmd.resp[0]) == 7));
  717. #if 0
  718. if (cmd.resp[0] & ~0x00000900)
  719. printk(KERN_ERR "%s: status = %08x\n",
  720. req->rq_disk->disk_name, cmd.resp[0]);
  721. if (mmc_decode_status(cmd.resp))
  722. goto cmd_err;
  723. #endif
  724. }
  725. if (brq.cmd.error || brq.stop.error || brq.data.error) {
  726. if (rq_data_dir(req) == READ) {
  727. /*
  728. * After an error, we redo I/O one sector at a
  729. * time, so we only reach here after trying to
  730. * read a single sector.
  731. */
  732. spin_lock_irq(&md->lock);
  733. ret = __blk_end_request(req, -EIO, brq.data.blksz);
  734. spin_unlock_irq(&md->lock);
  735. continue;
  736. }
  737. goto cmd_err;
  738. }
  739. /*
  740. * A block was successfully transferred.
  741. */
  742. spin_lock_irq(&md->lock);
  743. ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
  744. spin_unlock_irq(&md->lock);
  745. } while (ret);
  746. return 1;
  747. cmd_err:
  748. /*
  749. * If this is an SD card and we're writing, we can first
  750. * mark the known good sectors as ok.
  751. *
  752. * If the card is not SD, we can still ok written sectors
  753. * as reported by the controller (which might be less than
  754. * the real number of written sectors, but never more).
  755. */
  756. if (mmc_card_sd(card)) {
  757. u32 blocks;
  758. blocks = mmc_sd_num_wr_blocks(card);
  759. if (blocks != (u32)-1) {
  760. spin_lock_irq(&md->lock);
  761. ret = __blk_end_request(req, 0, blocks << 9);
  762. spin_unlock_irq(&md->lock);
  763. }
  764. } else {
  765. spin_lock_irq(&md->lock);
  766. ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
  767. spin_unlock_irq(&md->lock);
  768. }
  769. spin_lock_irq(&md->lock);
  770. while (ret)
  771. ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
  772. spin_unlock_irq(&md->lock);
  773. return 0;
  774. }
  775. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  776. {
  777. int ret;
  778. struct mmc_blk_data *md = mq->data;
  779. struct mmc_card *card = md->queue.card;
  780. mmc_claim_host(card->host);
  781. ret = mmc_blk_part_switch(card, md);
  782. if (ret) {
  783. ret = 0;
  784. goto out;
  785. }
  786. if (req->cmd_flags & REQ_DISCARD) {
  787. if (req->cmd_flags & REQ_SECURE)
  788. ret = mmc_blk_issue_secdiscard_rq(mq, req);
  789. else
  790. ret = mmc_blk_issue_discard_rq(mq, req);
  791. } else if (req->cmd_flags & REQ_FLUSH) {
  792. ret = mmc_blk_issue_flush(mq, req);
  793. } else {
  794. ret = mmc_blk_issue_rw_rq(mq, req);
  795. }
  796. out:
  797. mmc_release_host(card->host);
  798. return ret;
  799. }
  800. static inline int mmc_blk_readonly(struct mmc_card *card)
  801. {
  802. return mmc_card_readonly(card) ||
  803. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  804. }
  805. static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
  806. struct device *parent,
  807. sector_t size,
  808. bool default_ro,
  809. const char *subname)
  810. {
  811. struct mmc_blk_data *md;
  812. int devidx, ret;
  813. devidx = find_first_zero_bit(dev_use, max_devices);
  814. if (devidx >= max_devices)
  815. return ERR_PTR(-ENOSPC);
  816. __set_bit(devidx, dev_use);
  817. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  818. if (!md) {
  819. ret = -ENOMEM;
  820. goto out;
  821. }
  822. /*
  823. * !subname implies we are creating main mmc_blk_data that will be
  824. * associated with mmc_card with mmc_set_drvdata. Due to device
  825. * partitions, devidx will not coincide with a per-physical card
  826. * index anymore so we keep track of a name index.
  827. */
  828. if (!subname) {
  829. md->name_idx = find_first_zero_bit(name_use, max_devices);
  830. __set_bit(md->name_idx, name_use);
  831. }
  832. else
  833. md->name_idx = ((struct mmc_blk_data *)
  834. dev_to_disk(parent)->private_data)->name_idx;
  835. /*
  836. * Set the read-only status based on the supported commands
  837. * and the write protect switch.
  838. */
  839. md->read_only = mmc_blk_readonly(card);
  840. md->disk = alloc_disk(perdev_minors);
  841. if (md->disk == NULL) {
  842. ret = -ENOMEM;
  843. goto err_kfree;
  844. }
  845. spin_lock_init(&md->lock);
  846. INIT_LIST_HEAD(&md->part);
  847. md->usage = 1;
  848. ret = mmc_init_queue(&md->queue, card, &md->lock);
  849. if (ret)
  850. goto err_putdisk;
  851. md->queue.issue_fn = mmc_blk_issue_rq;
  852. md->queue.data = md;
  853. md->disk->major = MMC_BLOCK_MAJOR;
  854. md->disk->first_minor = devidx * perdev_minors;
  855. md->disk->fops = &mmc_bdops;
  856. md->disk->private_data = md;
  857. md->disk->queue = md->queue.queue;
  858. md->disk->driverfs_dev = parent;
  859. set_disk_ro(md->disk, md->read_only || default_ro);
  860. if (REL_WRITES_SUPPORTED(card))
  861. blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
  862. /*
  863. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  864. *
  865. * - be set for removable media with permanent block devices
  866. * - be unset for removable block devices with permanent media
  867. *
  868. * Since MMC block devices clearly fall under the second
  869. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  870. * should use the block device creation/destruction hotplug
  871. * messages to tell when the card is present.
  872. */
  873. snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
  874. "mmcblk%d%s", md->name_idx, subname ? subname : "");
  875. blk_queue_logical_block_size(md->queue.queue, 512);
  876. set_capacity(md->disk, size);
  877. return md;
  878. err_putdisk:
  879. put_disk(md->disk);
  880. err_kfree:
  881. kfree(md);
  882. out:
  883. return ERR_PTR(ret);
  884. }
  885. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  886. {
  887. sector_t size;
  888. struct mmc_blk_data *md;
  889. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  890. /*
  891. * The EXT_CSD sector count is in number or 512 byte
  892. * sectors.
  893. */
  894. size = card->ext_csd.sectors;
  895. } else {
  896. /*
  897. * The CSD capacity field is in units of read_blkbits.
  898. * set_capacity takes units of 512 bytes.
  899. */
  900. size = card->csd.capacity << (card->csd.read_blkbits - 9);
  901. }
  902. md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
  903. return md;
  904. }
  905. static int mmc_blk_alloc_part(struct mmc_card *card,
  906. struct mmc_blk_data *md,
  907. unsigned int part_type,
  908. sector_t size,
  909. bool default_ro,
  910. const char *subname)
  911. {
  912. char cap_str[10];
  913. struct mmc_blk_data *part_md;
  914. part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
  915. subname);
  916. if (IS_ERR(part_md))
  917. return PTR_ERR(part_md);
  918. part_md->part_type = part_type;
  919. list_add(&part_md->part, &md->part);
  920. string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
  921. cap_str, sizeof(cap_str));
  922. printk(KERN_INFO "%s: %s %s partition %u %s\n",
  923. part_md->disk->disk_name, mmc_card_id(card),
  924. mmc_card_name(card), part_md->part_type, cap_str);
  925. return 0;
  926. }
  927. static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
  928. {
  929. int ret = 0;
  930. if (!mmc_card_mmc(card))
  931. return 0;
  932. if (card->ext_csd.boot_size) {
  933. ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
  934. card->ext_csd.boot_size >> 9,
  935. true,
  936. "boot0");
  937. if (ret)
  938. return ret;
  939. ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
  940. card->ext_csd.boot_size >> 9,
  941. true,
  942. "boot1");
  943. if (ret)
  944. return ret;
  945. }
  946. return ret;
  947. }
  948. static int
  949. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  950. {
  951. int err;
  952. mmc_claim_host(card->host);
  953. err = mmc_set_blocklen(card, 512);
  954. mmc_release_host(card->host);
  955. if (err) {
  956. printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
  957. md->disk->disk_name, err);
  958. return -EINVAL;
  959. }
  960. return 0;
  961. }
  962. static void mmc_blk_remove_req(struct mmc_blk_data *md)
  963. {
  964. if (md) {
  965. if (md->disk->flags & GENHD_FL_UP) {
  966. device_remove_file(disk_to_dev(md->disk), &md->force_ro);
  967. /* Stop new requests from getting into the queue */
  968. del_gendisk(md->disk);
  969. }
  970. /* Then flush out any already in there */
  971. mmc_cleanup_queue(&md->queue);
  972. mmc_blk_put(md);
  973. }
  974. }
  975. static void mmc_blk_remove_parts(struct mmc_card *card,
  976. struct mmc_blk_data *md)
  977. {
  978. struct list_head *pos, *q;
  979. struct mmc_blk_data *part_md;
  980. __clear_bit(md->name_idx, name_use);
  981. list_for_each_safe(pos, q, &md->part) {
  982. part_md = list_entry(pos, struct mmc_blk_data, part);
  983. list_del(pos);
  984. mmc_blk_remove_req(part_md);
  985. }
  986. }
  987. static int mmc_add_disk(struct mmc_blk_data *md)
  988. {
  989. int ret;
  990. add_disk(md->disk);
  991. md->force_ro.show = force_ro_show;
  992. md->force_ro.store = force_ro_store;
  993. sysfs_attr_init(&md->force_ro.attr);
  994. md->force_ro.attr.name = "force_ro";
  995. md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
  996. ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
  997. if (ret)
  998. del_gendisk(md->disk);
  999. return ret;
  1000. }
  1001. static const struct mmc_fixup blk_fixups[] =
  1002. {
  1003. MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1004. MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1005. MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1006. MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1007. MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
  1008. END_FIXUP
  1009. };
  1010. static int mmc_blk_probe(struct mmc_card *card)
  1011. {
  1012. struct mmc_blk_data *md, *part_md;
  1013. int err;
  1014. char cap_str[10];
  1015. /*
  1016. * Check that the card supports the command class(es) we need.
  1017. */
  1018. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  1019. return -ENODEV;
  1020. md = mmc_blk_alloc(card);
  1021. if (IS_ERR(md))
  1022. return PTR_ERR(md);
  1023. err = mmc_blk_set_blksize(md, card);
  1024. if (err)
  1025. goto out;
  1026. string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
  1027. cap_str, sizeof(cap_str));
  1028. printk(KERN_INFO "%s: %s %s %s %s\n",
  1029. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  1030. cap_str, md->read_only ? "(ro)" : "");
  1031. if (mmc_blk_alloc_parts(card, md))
  1032. goto out;
  1033. mmc_set_drvdata(card, md);
  1034. mmc_fixup_device(card, blk_fixups);
  1035. if (mmc_add_disk(md))
  1036. goto out;
  1037. list_for_each_entry(part_md, &md->part, part) {
  1038. if (mmc_add_disk(part_md))
  1039. goto out;
  1040. }
  1041. return 0;
  1042. out:
  1043. mmc_blk_remove_parts(card, md);
  1044. mmc_blk_remove_req(md);
  1045. return err;
  1046. }
  1047. static void mmc_blk_remove(struct mmc_card *card)
  1048. {
  1049. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1050. mmc_blk_remove_parts(card, md);
  1051. mmc_blk_remove_req(md);
  1052. mmc_set_drvdata(card, NULL);
  1053. }
  1054. #ifdef CONFIG_PM
  1055. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  1056. {
  1057. struct mmc_blk_data *part_md;
  1058. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1059. if (md) {
  1060. mmc_queue_suspend(&md->queue);
  1061. list_for_each_entry(part_md, &md->part, part) {
  1062. mmc_queue_suspend(&part_md->queue);
  1063. }
  1064. }
  1065. return 0;
  1066. }
  1067. static int mmc_blk_resume(struct mmc_card *card)
  1068. {
  1069. struct mmc_blk_data *part_md;
  1070. struct mmc_blk_data *md = mmc_get_drvdata(card);
  1071. if (md) {
  1072. mmc_blk_set_blksize(md, card);
  1073. /*
  1074. * Resume involves the card going into idle state,
  1075. * so current partition is always the main one.
  1076. */
  1077. md->part_curr = md->part_type;
  1078. mmc_queue_resume(&md->queue);
  1079. list_for_each_entry(part_md, &md->part, part) {
  1080. mmc_queue_resume(&part_md->queue);
  1081. }
  1082. }
  1083. return 0;
  1084. }
  1085. #else
  1086. #define mmc_blk_suspend NULL
  1087. #define mmc_blk_resume NULL
  1088. #endif
  1089. static struct mmc_driver mmc_driver = {
  1090. .drv = {
  1091. .name = "mmcblk",
  1092. },
  1093. .probe = mmc_blk_probe,
  1094. .remove = mmc_blk_remove,
  1095. .suspend = mmc_blk_suspend,
  1096. .resume = mmc_blk_resume,
  1097. };
  1098. static int __init mmc_blk_init(void)
  1099. {
  1100. int res;
  1101. if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
  1102. pr_info("mmcblk: using %d minors per device\n", perdev_minors);
  1103. max_devices = 256 / perdev_minors;
  1104. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1105. if (res)
  1106. goto out;
  1107. res = mmc_register_driver(&mmc_driver);
  1108. if (res)
  1109. goto out2;
  1110. return 0;
  1111. out2:
  1112. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1113. out:
  1114. return res;
  1115. }
  1116. static void __exit mmc_blk_exit(void)
  1117. {
  1118. mmc_unregister_driver(&mmc_driver);
  1119. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  1120. }
  1121. module_init(mmc_blk_init);
  1122. module_exit(mmc_blk_exit);
  1123. MODULE_LICENSE("GPL");
  1124. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");