blob: acaa05200ae75e51ea3b28a373d46fa8c30daa40 [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 Ossman98ac2162006-12-23 20:03:02 +01005 * Copyright 2005-2007 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020034#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010035#include <linux/mmc/mmc.h>
36#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/system.h>
39#include <asm/uaccess.h>
40
Pierre Ossman98ac2162006-12-23 20:03:02 +010041#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * max 8 partitions per card
45 */
46#define MMC_SHIFT 3
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * There is one mmc_blk_data per slot.
50 */
51struct mmc_blk_data {
52 spinlock_t lock;
53 struct gendisk *disk;
54 struct mmc_queue queue;
55
56 unsigned int usage;
57 unsigned int block_bits;
Russell Kinga6f6c962006-01-03 22:38:44 +000058 unsigned int read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Arjan van de Vena621aae2006-01-12 18:43:35 +000061static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
64{
65 struct mmc_blk_data *md;
66
Arjan van de Vena621aae2006-01-12 18:43:35 +000067 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 md = disk->private_data;
69 if (md && md->usage == 0)
70 md = NULL;
71 if (md)
72 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +000073 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 return md;
76}
77
78static void mmc_blk_put(struct mmc_blk_data *md)
79{
Arjan van de Vena621aae2006-01-12 18:43:35 +000080 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 md->usage--;
82 if (md->usage == 0) {
83 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 kfree(md);
85 }
Arjan van de Vena621aae2006-01-12 18:43:35 +000086 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89static int mmc_blk_open(struct inode *inode, struct file *filp)
90{
91 struct mmc_blk_data *md;
92 int ret = -ENXIO;
93
94 md = mmc_blk_get(inode->i_bdev->bd_disk);
95 if (md) {
96 if (md->usage == 2)
97 check_disk_change(inode->i_bdev);
98 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -070099
Russell Kinga6f6c962006-01-03 22:38:44 +0000100 if ((filp->f_mode & FMODE_WRITE) && md->read_only)
Pierre Ossmana00fc092005-09-06 15:18:52 -0700101 ret = -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103
104 return ret;
105}
106
107static int mmc_blk_release(struct inode *inode, struct file *filp)
108{
109 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
110
111 mmc_blk_put(md);
112 return 0;
113}
114
115static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800116mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800118 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
119 geo->heads = 4;
120 geo->sectors = 16;
121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static struct block_device_operations mmc_bdops = {
125 .open = mmc_blk_open,
126 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800127 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .owner = THIS_MODULE,
129};
130
131struct mmc_blk_request {
132 struct mmc_request mrq;
133 struct mmc_command cmd;
134 struct mmc_command stop;
135 struct mmc_data data;
136};
137
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700138static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
139{
140 int err;
141 u32 blocks;
142
143 struct mmc_request mrq;
144 struct mmc_command cmd;
145 struct mmc_data data;
146 unsigned int timeout_us;
147
148 struct scatterlist sg;
149
150 memset(&cmd, 0, sizeof(struct mmc_command));
151
152 cmd.opcode = MMC_APP_CMD;
153 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700154 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700155
156 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700157 if (err)
158 return (u32)-1;
159 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700160 return (u32)-1;
161
162 memset(&cmd, 0, sizeof(struct mmc_command));
163
164 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
165 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700166 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700167
168 memset(&data, 0, sizeof(struct mmc_data));
169
170 data.timeout_ns = card->csd.tacc_ns * 100;
171 data.timeout_clks = card->csd.tacc_clks * 100;
172
173 timeout_us = data.timeout_ns / 1000;
174 timeout_us += data.timeout_clks * 1000 /
175 (card->host->ios.clock / 1000);
176
177 if (timeout_us > 100000) {
178 data.timeout_ns = 100000000;
179 data.timeout_clks = 0;
180 }
181
182 data.blksz = 4;
183 data.blocks = 1;
184 data.flags = MMC_DATA_READ;
185 data.sg = &sg;
186 data.sg_len = 1;
187
188 memset(&mrq, 0, sizeof(struct mmc_request));
189
190 mrq.cmd = &cmd;
191 mrq.data = &data;
192
193 sg_init_one(&sg, &blocks, 4);
194
195 mmc_wait_for_req(card->host, &mrq);
196
Pierre Ossman17b04292007-07-22 22:18:46 +0200197 if (cmd.error || data.error)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700198 return (u32)-1;
199
200 blocks = ntohl(blocks);
201
202 return blocks;
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
206{
207 struct mmc_blk_data *md = mq->data;
208 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700209 struct mmc_blk_request brq;
Alex Dubov14d836e2007-04-13 19:04:38 +0200210 int ret = 1, sg_pos, data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Pierre Ossmanb8558852007-01-03 19:47:29 +0100212 mmc_claim_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct mmc_command cmd;
Russell Kingdb53f282006-08-30 15:14:56 +0100216 u32 readcmd, writecmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 memset(&brq, 0, sizeof(struct mmc_blk_request));
219 brq.mrq.cmd = &brq.cmd;
220 brq.mrq.data = &brq.data;
221
Philip Langdalefba68bd2007-01-04 06:57:32 -0800222 brq.cmd.arg = req->sector;
223 if (!mmc_card_blockaddr(card))
224 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700225 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100226 brq.data.blksz = 1 << md->block_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 brq.stop.opcode = MMC_STOP_TRANSMISSION;
228 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700229 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Pierre Ossman55db8902006-11-21 17:55:45 +0100230 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
231 if (brq.data.blocks > card->host->max_blk_count)
232 brq.data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Russell Kingdb53f282006-08-30 15:14:56 +0100234 /*
235 * If the host doesn't support multiple block writes, force
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700236 * block writes to single block. SD cards are excepted from
237 * this rule as they support querying the number of
238 * successfully written sectors.
Russell Kingdb53f282006-08-30 15:14:56 +0100239 */
240 if (rq_data_dir(req) != READ &&
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700241 !(card->host->caps & MMC_CAP_MULTIWRITE) &&
242 !mmc_card_sd(card))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 brq.data.blocks = 1;
Russell King788ee7b2006-01-09 21:12:17 +0000244
245 if (brq.data.blocks > 1) {
David Brownell7213d172007-08-08 09:10:23 -0700246 /* SPI multiblock writes terminate using a special
247 * token, not a STOP_TRANSMISSION request.
248 */
249 if (!mmc_host_is_spi(card->host)
250 || rq_data_dir(req) == READ)
251 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100252 readcmd = MMC_READ_MULTIPLE_BLOCK;
253 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000254 } else {
255 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100256 readcmd = MMC_READ_SINGLE_BLOCK;
257 writecmd = MMC_WRITE_BLOCK;
258 }
259
260 if (rq_data_dir(req) == READ) {
261 brq.cmd.opcode = readcmd;
262 brq.data.flags |= MMC_DATA_READ;
263 } else {
264 brq.cmd.opcode = writecmd;
265 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Pierre Ossmanb146d262007-07-24 19:16:54 +0200268 mmc_set_data_timeout(&brq.data, card);
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200271 brq.data.sg_len = mmc_queue_map_sg(mq);
272
273 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Alex Dubov14d836e2007-04-13 19:04:38 +0200275 if (brq.data.blocks !=
276 (req->nr_sectors >> (md->block_bits - 9))) {
277 data_size = brq.data.blocks * brq.data.blksz;
278 for (sg_pos = 0; sg_pos < brq.data.sg_len; sg_pos++) {
279 data_size -= mq->sg[sg_pos].length;
280 if (data_size <= 0) {
281 mq->sg[sg_pos].length += data_size;
282 sg_pos++;
283 break;
284 }
285 }
286 brq.data.sg_len = sg_pos;
287 }
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200290
291 mmc_queue_bounce_post(mq);
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (brq.cmd.error) {
294 printk(KERN_ERR "%s: error %d sending read/write command\n",
295 req->rq_disk->disk_name, brq.cmd.error);
296 goto cmd_err;
297 }
298
299 if (brq.data.error) {
300 printk(KERN_ERR "%s: error %d transferring data\n",
301 req->rq_disk->disk_name, brq.data.error);
302 goto cmd_err;
303 }
304
305 if (brq.stop.error) {
306 printk(KERN_ERR "%s: error %d sending stop command\n",
307 req->rq_disk->disk_name, brq.stop.error);
308 goto cmd_err;
309 }
310
David Brownell7213d172007-08-08 09:10:23 -0700311 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King2ed6d222006-09-24 10:46:43 +0100312 do {
313 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Russell King2ed6d222006-09-24 10:46:43 +0100315 cmd.opcode = MMC_SEND_STATUS;
316 cmd.arg = card->rca << 16;
317 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
318 err = mmc_wait_for_cmd(card->host, &cmd, 5);
319 if (err) {
320 printk(KERN_ERR "%s: error %d requesting status\n",
321 req->rq_disk->disk_name, err);
322 goto cmd_err;
323 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100324 /*
325 * Some cards mishandle the status bits,
326 * so make sure to check both the busy
327 * indication and the card state.
328 */
329 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
330 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332#if 0
Russell King2ed6d222006-09-24 10:46:43 +0100333 if (cmd.resp[0] & ~0x00000900)
334 printk(KERN_ERR "%s: status = %08x\n",
335 req->rq_disk->disk_name, cmd.resp[0]);
336 if (mmc_decode_status(cmd.resp))
337 goto cmd_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#endif
Russell King2ed6d222006-09-24 10:46:43 +0100339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /*
342 * A block was successfully transferred.
343 */
344 spin_lock_irq(&md->lock);
345 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
346 if (!ret) {
347 /*
348 * The whole request completed successfully.
349 */
350 add_disk_randomness(req->rq_disk);
351 blkdev_dequeue_request(req);
Tejun Heo8ffdc652006-01-06 09:49:03 +0100352 end_that_request_last(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 spin_unlock_irq(&md->lock);
355 } while (ret);
356
Pierre Ossmanb8558852007-01-03 19:47:29 +0100357 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 return 1;
360
361 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700362 /*
363 * If this is an SD card and we're writing, we can first
364 * mark the known good sectors as ok.
365 *
366 * If the card is not SD, we can still ok written sectors
367 * if the controller can do proper error reporting.
Pierre Ossman176f00f2006-10-04 02:15:41 -0700368 *
369 * For reads we just fail the entire chunk as that should
370 * be safe in all cases.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700372 if (rq_data_dir(req) != READ && mmc_card_sd(card)) {
373 u32 blocks;
374 unsigned int bytes;
375
376 blocks = mmc_sd_num_wr_blocks(card);
377 if (blocks != (u32)-1) {
378 if (card->csd.write_partial)
379 bytes = blocks << md->block_bits;
380 else
381 bytes = blocks << 9;
382 spin_lock_irq(&md->lock);
383 ret = end_that_request_chunk(req, 1, bytes);
384 spin_unlock_irq(&md->lock);
385 }
386 } else if (rq_data_dir(req) != READ &&
387 (card->host->caps & MMC_CAP_MULTIWRITE)) {
Pierre Ossman176f00f2006-10-04 02:15:41 -0700388 spin_lock_irq(&md->lock);
389 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
390 spin_unlock_irq(&md->lock);
391 }
392
Pierre Ossmanb8558852007-01-03 19:47:29 +0100393 mmc_release_host(card->host);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 spin_lock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -0700396 while (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 ret = end_that_request_chunk(req, 0,
398 req->current_nr_sectors << 9);
Pierre Ossman176f00f2006-10-04 02:15:41 -0700399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 add_disk_randomness(req->rq_disk);
402 blkdev_dequeue_request(req);
Tejun Heo8ffdc652006-01-06 09:49:03 +0100403 end_that_request_last(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 spin_unlock_irq(&md->lock);
405
406 return 0;
407}
408
409#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
410
411static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
412
Russell Kinga6f6c962006-01-03 22:38:44 +0000413static inline int mmc_blk_readonly(struct mmc_card *card)
414{
415 return mmc_card_readonly(card) ||
416 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
420{
421 struct mmc_blk_data *md;
422 int devidx, ret;
423
424 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
425 if (devidx >= MMC_NUM_MINORS)
426 return ERR_PTR(-ENOSPC);
427 __set_bit(devidx, dev_use);
428
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700429 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +0000430 if (!md) {
431 ret = -ENOMEM;
432 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
Russell Kinga6f6c962006-01-03 22:38:44 +0000434
Russell Kinga6f6c962006-01-03 22:38:44 +0000435
436 /*
437 * Set the read-only status based on the supported commands
438 * and the write protect switch.
439 */
440 md->read_only = mmc_blk_readonly(card);
441
442 /*
Pierre Ossman6fe9feb2006-09-07 16:01:30 +0100443 * Both SD and MMC specifications state (although a bit
444 * unclearly in the MMC case) that a block size of 512
445 * bytes must always be supported by the card.
Russell Kinga6f6c962006-01-03 22:38:44 +0000446 */
Pierre Ossman6fe9feb2006-09-07 16:01:30 +0100447 md->block_bits = 9;
Russell Kinga6f6c962006-01-03 22:38:44 +0000448
449 md->disk = alloc_disk(1 << MMC_SHIFT);
450 if (md->disk == NULL) {
451 ret = -ENOMEM;
452 goto err_kfree;
453 }
454
455 spin_lock_init(&md->lock);
456 md->usage = 1;
457
458 ret = mmc_init_queue(&md->queue, card, &md->lock);
459 if (ret)
460 goto err_putdisk;
461
Russell Kinga6f6c962006-01-03 22:38:44 +0000462 md->queue.issue_fn = mmc_blk_issue_rq;
463 md->queue.data = md;
464
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200465 md->disk->major = MMC_BLOCK_MAJOR;
Russell Kinga6f6c962006-01-03 22:38:44 +0000466 md->disk->first_minor = devidx << MMC_SHIFT;
467 md->disk->fops = &mmc_bdops;
468 md->disk->private_data = md;
469 md->disk->queue = md->queue.queue;
470 md->disk->driverfs_dev = &card->dev;
471
472 /*
473 * As discussed on lkml, GENHD_FL_REMOVABLE should:
474 *
475 * - be set for removable media with permanent block devices
476 * - be unset for removable block devices with permanent media
477 *
478 * Since MMC block devices clearly fall under the second
479 * case, we do not set GENHD_FL_REMOVABLE. Userspace
480 * should use the block device creation/destruction hotplug
481 * messages to tell when the card is present.
482 */
483
484 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +0000485
486 blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
487
Pierre Ossman85a18ad2007-02-17 22:15:27 +0100488 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
489 /*
490 * The EXT_CSD sector count is in number or 512 byte
491 * sectors.
492 */
493 set_capacity(md->disk, card->ext_csd.sectors);
494 } else {
495 /*
496 * The CSD capacity field is in units of read_blkbits.
497 * set_capacity takes units of 512 bytes.
498 */
499 set_capacity(md->disk,
500 card->csd.capacity << (card->csd.read_blkbits - 9));
501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +0000503
504 err_putdisk:
505 put_disk(md->disk);
506 err_kfree:
507 kfree(md);
508 out:
509 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512static int
513mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
514{
515 struct mmc_command cmd;
516 int err;
517
Philip Langdalefba68bd2007-01-04 06:57:32 -0800518 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
519 if (mmc_card_blockaddr(card))
520 return 0;
521
Pierre Ossmanb8558852007-01-03 19:47:29 +0100522 mmc_claim_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 cmd.opcode = MMC_SET_BLOCKLEN;
Russell Kingd2b18392005-12-22 23:21:38 +0000524 cmd.arg = 1 << md->block_bits;
David Brownell7213d172007-08-08 09:10:23 -0700525 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 err = mmc_wait_for_cmd(card->host, &cmd, 5);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100527 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (err) {
530 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
531 md->disk->disk_name, cmd.arg, err);
532 return -EINVAL;
533 }
534
535 return 0;
536}
537
538static int mmc_blk_probe(struct mmc_card *card)
539{
540 struct mmc_blk_data *md;
541 int err;
542
Pierre Ossman912490d2005-05-21 10:27:02 +0100543 /*
544 * Check that the card supports the command class(es) we need.
545 */
546 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return -ENODEV;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 md = mmc_blk_alloc(card);
550 if (IS_ERR(md))
551 return PTR_ERR(md);
552
553 err = mmc_blk_set_blksize(md, card);
554 if (err)
555 goto out;
556
Andrew Morton51828ab2006-01-13 21:44:18 +0000557 printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Andrew Morton51828ab2006-01-13 21:44:18 +0000559 (unsigned long long)(get_capacity(md->disk) >> 1),
560 md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 mmc_set_drvdata(card, md);
563 add_disk(md->disk);
564 return 0;
565
566 out:
567 mmc_blk_put(md);
568
569 return err;
570}
571
572static void mmc_blk_remove(struct mmc_card *card)
573{
574 struct mmc_blk_data *md = mmc_get_drvdata(card);
575
576 if (md) {
577 int devidx;
578
Pierre Ossman89b4e132006-11-14 22:08:16 +0100579 /* Stop new requests from getting into the queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 del_gendisk(md->disk);
581
Pierre Ossman89b4e132006-11-14 22:08:16 +0100582 /* Then flush out any already in there */
583 mmc_cleanup_queue(&md->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 devidx = md->disk->first_minor >> MMC_SHIFT;
586 __clear_bit(devidx, dev_use);
587
588 mmc_blk_put(md);
589 }
590 mmc_set_drvdata(card, NULL);
591}
592
593#ifdef CONFIG_PM
594static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
595{
596 struct mmc_blk_data *md = mmc_get_drvdata(card);
597
598 if (md) {
599 mmc_queue_suspend(&md->queue);
600 }
601 return 0;
602}
603
604static int mmc_blk_resume(struct mmc_card *card)
605{
606 struct mmc_blk_data *md = mmc_get_drvdata(card);
607
608 if (md) {
609 mmc_blk_set_blksize(md, card);
610 mmc_queue_resume(&md->queue);
611 }
612 return 0;
613}
614#else
615#define mmc_blk_suspend NULL
616#define mmc_blk_resume NULL
617#endif
618
619static 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
629static int __init mmc_blk_init(void)
630{
631 int res = -ENOMEM;
632
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200633 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
634 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return mmc_register_driver(&mmc_driver);
638
639 out:
640 return res;
641}
642
643static void __exit mmc_blk_exit(void)
644{
645 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200646 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649module_init(mmc_blk_init);
650module_exit(mmc_blk_exit);
651
652MODULE_LICENSE("GPL");
653MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
654