blob: 3d067c35185db9704157d8bc4bddbecc20bf22c7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
26#include <linux/errno.h>
27#include <linux/hdreg.h>
28#include <linux/kdev_t.h>
29#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000030#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070031#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020032#include <linux/string_helpers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020035#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010036#include <linux/mmc/mmc.h>
37#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/system.h>
40#include <asm/uaccess.h>
41
Pierre Ossman98ac2162006-12-23 20:03:02 +010042#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * max 8 partitions per card
46 */
47#define MMC_SHIFT 3
David Woodhouse1dff3142007-11-21 18:45:12 +010048#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
49
Ben Collins203c8012008-06-05 19:10:21 -040050static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * There is one mmc_blk_data per slot.
54 */
55struct mmc_blk_data {
56 spinlock_t lock;
57 struct gendisk *disk;
58 struct mmc_queue queue;
59
60 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +000061 unsigned int read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
Arjan van de Vena621aae2006-01-12 18:43:35 +000064static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
67{
68 struct mmc_blk_data *md;
69
Arjan van de Vena621aae2006-01-12 18:43:35 +000070 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 md = disk->private_data;
72 if (md && md->usage == 0)
73 md = NULL;
74 if (md)
75 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +000076 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 return md;
79}
80
81static void mmc_blk_put(struct mmc_blk_data *md)
82{
Arjan van de Vena621aae2006-01-12 18:43:35 +000083 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 md->usage--;
85 if (md->usage == 0) {
Tejun Heof331c022008-09-03 09:01:48 +020086 int devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
David Woodhouse1dff3142007-11-21 18:45:12 +010087 __clear_bit(devidx, dev_use);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 kfree(md);
91 }
Arjan van de Vena621aae2006-01-12 18:43:35 +000092 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Al Viroa5a15612008-03-02 10:33:30 -050095static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Al Viroa5a15612008-03-02 10:33:30 -050097 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 int ret = -ENXIO;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (md) {
101 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500102 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700104
Al Viroa5a15612008-03-02 10:33:30 -0500105 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700106 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700107 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110
111 return ret;
112}
113
Al Viroa5a15612008-03-02 10:33:30 -0500114static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Al Viroa5a15612008-03-02 10:33:30 -0500116 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 mmc_blk_put(md);
119 return 0;
120}
121
122static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800123mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800125 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
126 geo->heads = 4;
127 geo->sectors = 16;
128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131static struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500132 .open = mmc_blk_open,
133 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800134 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .owner = THIS_MODULE,
136};
137
138struct 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
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700145static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
146{
147 int err;
148 u32 blocks;
149
150 struct mmc_request mrq;
151 struct mmc_command cmd;
152 struct mmc_data data;
153 unsigned int timeout_us;
154
155 struct scatterlist sg;
156
157 memset(&cmd, 0, sizeof(struct mmc_command));
158
159 cmd.opcode = MMC_APP_CMD;
160 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700161 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700162
163 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700164 if (err)
165 return (u32)-1;
166 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700167 return (u32)-1;
168
169 memset(&cmd, 0, sizeof(struct mmc_command));
170
171 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
172 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700173 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700174
175 memset(&data, 0, sizeof(struct mmc_data));
176
177 data.timeout_ns = card->csd.tacc_ns * 100;
178 data.timeout_clks = card->csd.tacc_clks * 100;
179
180 timeout_us = data.timeout_ns / 1000;
181 timeout_us += data.timeout_clks * 1000 /
182 (card->host->ios.clock / 1000);
183
184 if (timeout_us > 100000) {
185 data.timeout_ns = 100000000;
186 data.timeout_clks = 0;
187 }
188
189 data.blksz = 4;
190 data.blocks = 1;
191 data.flags = MMC_DATA_READ;
192 data.sg = &sg;
193 data.sg_len = 1;
194
195 memset(&mrq, 0, sizeof(struct mmc_request));
196
197 mrq.cmd = &cmd;
198 mrq.data = &data;
199
200 sg_init_one(&sg, &blocks, 4);
201
202 mmc_wait_for_req(card->host, &mrq);
203
Pierre Ossman17b04292007-07-22 22:18:46 +0200204 if (cmd.error || data.error)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700205 return (u32)-1;
206
207 blocks = ntohl(blocks);
208
209 return blocks;
210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
213{
214 struct mmc_blk_data *md = mq->data;
215 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700216 struct mmc_blk_request brq;
Pierre Ossmanf3eb0aaa2008-08-16 21:34:02 +0200217 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Pierre Ossmanb8558852007-01-03 19:47:29 +0100219 mmc_claim_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct mmc_command cmd;
Russell Kingdb53f282006-08-30 15:14:56 +0100223 u32 readcmd, writecmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 memset(&brq, 0, sizeof(struct mmc_blk_request));
226 brq.mrq.cmd = &brq.cmd;
227 brq.mrq.data = &brq.data;
228
Philip Langdalefba68bd2007-01-04 06:57:32 -0800229 brq.cmd.arg = req->sector;
230 if (!mmc_card_blockaddr(card))
231 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700232 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossman08846692008-08-31 14:10:08 +0200233 brq.data.blksz = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 brq.stop.opcode = MMC_STOP_TRANSMISSION;
235 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700236 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Pierre Ossman08846692008-08-31 14:10:08 +0200237 brq.data.blocks = req->nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Russell King788ee7b2006-01-09 21:12:17 +0000239 if (brq.data.blocks > 1) {
David Brownell7213d172007-08-08 09:10:23 -0700240 /* SPI multiblock writes terminate using a special
241 * token, not a STOP_TRANSMISSION request.
242 */
243 if (!mmc_host_is_spi(card->host)
244 || rq_data_dir(req) == READ)
245 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100246 readcmd = MMC_READ_MULTIPLE_BLOCK;
247 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000248 } else {
249 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100250 readcmd = MMC_READ_SINGLE_BLOCK;
251 writecmd = MMC_WRITE_BLOCK;
252 }
253
254 if (rq_data_dir(req) == READ) {
255 brq.cmd.opcode = readcmd;
256 brq.data.flags |= MMC_DATA_READ;
257 } else {
258 brq.cmd.opcode = writecmd;
259 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Pierre Ossmanb146d262007-07-24 19:16:54 +0200262 mmc_set_data_timeout(&brq.data, card);
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200265 brq.data.sg_len = mmc_queue_map_sg(mq);
266
267 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200270
271 mmc_queue_bounce_post(mq);
272
Pierre Ossman979ce722008-06-29 12:19:47 +0200273 /*
274 * Check for errors here, but don't jump to cmd_err
275 * until later as we need to wait for the card to leave
276 * programming mode even when things go wrong.
277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (brq.cmd.error) {
279 printk(KERN_ERR "%s: error %d sending read/write command\n",
280 req->rq_disk->disk_name, brq.cmd.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
283 if (brq.data.error) {
284 printk(KERN_ERR "%s: error %d transferring data\n",
285 req->rq_disk->disk_name, brq.data.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287
288 if (brq.stop.error) {
289 printk(KERN_ERR "%s: error %d sending stop command\n",
290 req->rq_disk->disk_name, brq.stop.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
David Brownell7213d172007-08-08 09:10:23 -0700293 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King2ed6d222006-09-24 10:46:43 +0100294 do {
295 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Russell King2ed6d222006-09-24 10:46:43 +0100297 cmd.opcode = MMC_SEND_STATUS;
298 cmd.arg = card->rca << 16;
299 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
300 err = mmc_wait_for_cmd(card->host, &cmd, 5);
301 if (err) {
302 printk(KERN_ERR "%s: error %d requesting status\n",
303 req->rq_disk->disk_name, err);
304 goto cmd_err;
305 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100306 /*
307 * Some cards mishandle the status bits,
308 * so make sure to check both the busy
309 * indication and the card state.
310 */
311 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
312 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314#if 0
Russell King2ed6d222006-09-24 10:46:43 +0100315 if (cmd.resp[0] & ~0x00000900)
316 printk(KERN_ERR "%s: status = %08x\n",
317 req->rq_disk->disk_name, cmd.resp[0]);
318 if (mmc_decode_status(cmd.resp))
319 goto cmd_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320#endif
Russell King2ed6d222006-09-24 10:46:43 +0100321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Pierre Ossman979ce722008-06-29 12:19:47 +0200323 if (brq.cmd.error || brq.data.error || brq.stop.error)
324 goto cmd_err;
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /*
327 * A block was successfully transferred.
328 */
329 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500330 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 spin_unlock_irq(&md->lock);
332 } while (ret);
333
Pierre Ossmanb8558852007-01-03 19:47:29 +0100334 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 return 1;
337
338 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700339 /*
340 * If this is an SD card and we're writing, we can first
341 * mark the known good sectors as ok.
342 *
343 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +0200344 * as reported by the controller (which might be less than
345 * the real number of written sectors, but never more).
Pierre Ossman176f00f2006-10-04 02:15:41 -0700346 *
347 * For reads we just fail the entire chunk as that should
348 * be safe in all cases.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 */
Pierre Ossman23af6032008-07-06 01:10:27 +0200350 if (rq_data_dir(req) != READ) {
351 if (mmc_card_sd(card)) {
352 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700353
Pierre Ossman23af6032008-07-06 01:10:27 +0200354 blocks = mmc_sd_num_wr_blocks(card);
355 if (blocks != (u32)-1) {
Pierre Ossman23af6032008-07-06 01:10:27 +0200356 spin_lock_irq(&md->lock);
Pierre Ossman08846692008-08-31 14:10:08 +0200357 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossman23af6032008-07-06 01:10:27 +0200358 spin_unlock_irq(&md->lock);
359 }
360 } else {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700361 spin_lock_irq(&md->lock);
Pierre Ossman23af6032008-07-06 01:10:27 +0200362 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700363 spin_unlock_irq(&md->lock);
364 }
Pierre Ossman176f00f2006-10-04 02:15:41 -0700365 }
366
Pierre Ossmanb8558852007-01-03 19:47:29 +0100367 mmc_release_host(card->host);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500370 while (ret)
371 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 spin_unlock_irq(&md->lock);
373
374 return 0;
375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Russell Kinga6f6c962006-01-03 22:38:44 +0000378static inline int mmc_blk_readonly(struct mmc_card *card)
379{
380 return mmc_card_readonly(card) ||
381 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
385{
386 struct mmc_blk_data *md;
387 int devidx, ret;
388
389 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
390 if (devidx >= MMC_NUM_MINORS)
391 return ERR_PTR(-ENOSPC);
392 __set_bit(devidx, dev_use);
393
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700394 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +0000395 if (!md) {
396 ret = -ENOMEM;
397 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Russell Kinga6f6c962006-01-03 22:38:44 +0000399
Russell Kinga6f6c962006-01-03 22:38:44 +0000400
401 /*
402 * Set the read-only status based on the supported commands
403 * and the write protect switch.
404 */
405 md->read_only = mmc_blk_readonly(card);
406
Russell Kinga6f6c962006-01-03 22:38:44 +0000407 md->disk = alloc_disk(1 << MMC_SHIFT);
408 if (md->disk == NULL) {
409 ret = -ENOMEM;
410 goto err_kfree;
411 }
412
413 spin_lock_init(&md->lock);
414 md->usage = 1;
415
416 ret = mmc_init_queue(&md->queue, card, &md->lock);
417 if (ret)
418 goto err_putdisk;
419
Russell Kinga6f6c962006-01-03 22:38:44 +0000420 md->queue.issue_fn = mmc_blk_issue_rq;
421 md->queue.data = md;
422
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200423 md->disk->major = MMC_BLOCK_MAJOR;
Russell Kinga6f6c962006-01-03 22:38:44 +0000424 md->disk->first_minor = devidx << MMC_SHIFT;
425 md->disk->fops = &mmc_bdops;
426 md->disk->private_data = md;
427 md->disk->queue = md->queue.queue;
428 md->disk->driverfs_dev = &card->dev;
429
430 /*
431 * As discussed on lkml, GENHD_FL_REMOVABLE should:
432 *
433 * - be set for removable media with permanent block devices
434 * - be unset for removable block devices with permanent media
435 *
436 * Since MMC block devices clearly fall under the second
437 * case, we do not set GENHD_FL_REMOVABLE. Userspace
438 * should use the block device creation/destruction hotplug
439 * messages to tell when the card is present.
440 */
441
442 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +0000443
Pierre Ossman08846692008-08-31 14:10:08 +0200444 blk_queue_hardsect_size(md->queue.queue, 512);
Russell Kinga6f6c962006-01-03 22:38:44 +0000445
Pierre Ossman85a18ad2007-02-17 22:15:27 +0100446 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
447 /*
448 * The EXT_CSD sector count is in number or 512 byte
449 * sectors.
450 */
451 set_capacity(md->disk, card->ext_csd.sectors);
452 } else {
453 /*
454 * The CSD capacity field is in units of read_blkbits.
455 * set_capacity takes units of 512 bytes.
456 */
457 set_capacity(md->disk,
458 card->csd.capacity << (card->csd.read_blkbits - 9));
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +0000461
462 err_putdisk:
463 put_disk(md->disk);
464 err_kfree:
465 kfree(md);
466 out:
467 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470static int
471mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
472{
473 struct mmc_command cmd;
474 int err;
475
Philip Langdalefba68bd2007-01-04 06:57:32 -0800476 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
477 if (mmc_card_blockaddr(card))
478 return 0;
479
Pierre Ossmanb8558852007-01-03 19:47:29 +0100480 mmc_claim_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 cmd.opcode = MMC_SET_BLOCKLEN;
Pierre Ossman08846692008-08-31 14:10:08 +0200482 cmd.arg = 512;
David Brownell7213d172007-08-08 09:10:23 -0700483 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 err = mmc_wait_for_cmd(card->host, &cmd, 5);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100485 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 if (err) {
488 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
489 md->disk->disk_name, cmd.arg, err);
490 return -EINVAL;
491 }
492
493 return 0;
494}
495
496static int mmc_blk_probe(struct mmc_card *card)
497{
498 struct mmc_blk_data *md;
499 int err;
500
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200501 char cap_str[10];
502
Pierre Ossman912490d2005-05-21 10:27:02 +0100503 /*
504 * Check that the card supports the command class(es) we need.
505 */
506 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return -ENODEV;
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 md = mmc_blk_alloc(card);
510 if (IS_ERR(md))
511 return PTR_ERR(md);
512
513 err = mmc_blk_set_blksize(md, card);
514 if (err)
515 goto out;
516
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200517 string_get_size(get_capacity(md->disk) << 9, STRING_UNITS_2,
518 cap_str, sizeof(cap_str));
519 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200521 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 mmc_set_drvdata(card, md);
524 add_disk(md->disk);
525 return 0;
526
527 out:
528 mmc_blk_put(md);
529
530 return err;
531}
532
533static void mmc_blk_remove(struct mmc_card *card)
534{
535 struct mmc_blk_data *md = mmc_get_drvdata(card);
536
537 if (md) {
Pierre Ossman89b4e132006-11-14 22:08:16 +0100538 /* Stop new requests from getting into the queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 del_gendisk(md->disk);
540
Pierre Ossman89b4e132006-11-14 22:08:16 +0100541 /* Then flush out any already in there */
542 mmc_cleanup_queue(&md->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 mmc_blk_put(md);
545 }
546 mmc_set_drvdata(card, NULL);
547}
548
549#ifdef CONFIG_PM
550static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
551{
552 struct mmc_blk_data *md = mmc_get_drvdata(card);
553
554 if (md) {
555 mmc_queue_suspend(&md->queue);
556 }
557 return 0;
558}
559
560static int mmc_blk_resume(struct mmc_card *card)
561{
562 struct mmc_blk_data *md = mmc_get_drvdata(card);
563
564 if (md) {
565 mmc_blk_set_blksize(md, card);
566 mmc_queue_resume(&md->queue);
567 }
568 return 0;
569}
570#else
571#define mmc_blk_suspend NULL
572#define mmc_blk_resume NULL
573#endif
574
575static struct mmc_driver mmc_driver = {
576 .drv = {
577 .name = "mmcblk",
578 },
579 .probe = mmc_blk_probe,
580 .remove = mmc_blk_remove,
581 .suspend = mmc_blk_suspend,
582 .resume = mmc_blk_resume,
583};
584
585static int __init mmc_blk_init(void)
586{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900587 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200589 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
590 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900593 res = mmc_register_driver(&mmc_driver);
594 if (res)
595 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900597 return 0;
598 out2:
599 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 out:
601 return res;
602}
603
604static void __exit mmc_blk_exit(void)
605{
606 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200607 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610module_init(mmc_blk_init);
611module_exit(mmc_blk_exit);
612
613MODULE_LICENSE("GPL");
614MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
615