blob: ebc8b9d77613132f290b7372196ab50bfe416144 [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>
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
David Woodhouse1dff3142007-11-21 18:45:12 +010047#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
48
Ben Collins203c8012008-06-05 19:10:21 -040049static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * There is one mmc_blk_data per slot.
53 */
54struct mmc_blk_data {
55 spinlock_t lock;
56 struct gendisk *disk;
57 struct mmc_queue queue;
58
59 unsigned int usage;
60 unsigned int block_bits;
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) {
David Woodhouse1dff3142007-11-21 18:45:12 +010086 int devidx = md->disk->first_minor >> MMC_SHIFT;
87 __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
95static int mmc_blk_open(struct inode *inode, struct file *filp)
96{
97 struct mmc_blk_data *md;
98 int ret = -ENXIO;
99
100 md = mmc_blk_get(inode->i_bdev->bd_disk);
101 if (md) {
102 if (md->usage == 2)
103 check_disk_change(inode->i_bdev);
104 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700105
Andrew Morton70bb0892008-09-05 14:00:24 -0700106 if ((filp->f_mode & FMODE_WRITE) && md->read_only) {
107 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700108 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111
112 return ret;
113}
114
115static int mmc_blk_release(struct inode *inode, struct file *filp)
116{
117 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
118
119 mmc_blk_put(md);
120 return 0;
121}
122
123static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800124mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800126 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
127 geo->heads = 4;
128 geo->sectors = 16;
129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132static struct block_device_operations mmc_bdops = {
133 .open = mmc_blk_open,
134 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800135 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .owner = THIS_MODULE,
137};
138
139struct mmc_blk_request {
140 struct mmc_request mrq;
141 struct mmc_command cmd;
142 struct mmc_command stop;
143 struct mmc_data data;
144};
145
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700146static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
147{
148 int err;
149 u32 blocks;
150
151 struct mmc_request mrq;
152 struct mmc_command cmd;
153 struct mmc_data data;
154 unsigned int timeout_us;
155
156 struct scatterlist sg;
157
158 memset(&cmd, 0, sizeof(struct mmc_command));
159
160 cmd.opcode = MMC_APP_CMD;
161 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700162 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700163
164 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700165 if (err)
166 return (u32)-1;
167 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700168 return (u32)-1;
169
170 memset(&cmd, 0, sizeof(struct mmc_command));
171
172 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
173 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700174 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700175
176 memset(&data, 0, sizeof(struct mmc_data));
177
178 data.timeout_ns = card->csd.tacc_ns * 100;
179 data.timeout_clks = card->csd.tacc_clks * 100;
180
181 timeout_us = data.timeout_ns / 1000;
182 timeout_us += data.timeout_clks * 1000 /
183 (card->host->ios.clock / 1000);
184
185 if (timeout_us > 100000) {
186 data.timeout_ns = 100000000;
187 data.timeout_clks = 0;
188 }
189
190 data.blksz = 4;
191 data.blocks = 1;
192 data.flags = MMC_DATA_READ;
193 data.sg = &sg;
194 data.sg_len = 1;
195
196 memset(&mrq, 0, sizeof(struct mmc_request));
197
198 mrq.cmd = &cmd;
199 mrq.data = &data;
200
201 sg_init_one(&sg, &blocks, 4);
202
203 mmc_wait_for_req(card->host, &mrq);
204
Pierre Ossman17b04292007-07-22 22:18:46 +0200205 if (cmd.error || data.error)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700206 return (u32)-1;
207
208 blocks = ntohl(blocks);
209
210 return blocks;
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
214{
215 struct mmc_blk_data *md = mq->data;
216 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700217 struct mmc_blk_request brq;
Pierre Ossmanb41e9c72008-07-29 01:23:24 +0200218 int ret = 1, data_size, i;
219 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Pierre Ossmanb8558852007-01-03 19:47:29 +0100221 mmc_claim_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 struct mmc_command cmd;
Russell Kingdb53f282006-08-30 15:14:56 +0100225 u32 readcmd, writecmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 memset(&brq, 0, sizeof(struct mmc_blk_request));
228 brq.mrq.cmd = &brq.cmd;
229 brq.mrq.data = &brq.data;
230
Philip Langdalefba68bd2007-01-04 06:57:32 -0800231 brq.cmd.arg = req->sector;
232 if (!mmc_card_blockaddr(card))
233 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700234 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pavel Pisa2c171bf2006-05-19 21:48:03 +0100235 brq.data.blksz = 1 << md->block_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 brq.stop.opcode = MMC_STOP_TRANSMISSION;
237 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700238 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Pierre Ossman55db8902006-11-21 17:55:45 +0100239 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
240 if (brq.data.blocks > card->host->max_blk_count)
241 brq.data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Russell King788ee7b2006-01-09 21:12:17 +0000243 if (brq.data.blocks > 1) {
David Brownell7213d172007-08-08 09:10:23 -0700244 /* SPI multiblock writes terminate using a special
245 * token, not a STOP_TRANSMISSION request.
246 */
247 if (!mmc_host_is_spi(card->host)
248 || rq_data_dir(req) == READ)
249 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100250 readcmd = MMC_READ_MULTIPLE_BLOCK;
251 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000252 } else {
253 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100254 readcmd = MMC_READ_SINGLE_BLOCK;
255 writecmd = MMC_WRITE_BLOCK;
256 }
257
258 if (rq_data_dir(req) == READ) {
259 brq.cmd.opcode = readcmd;
260 brq.data.flags |= MMC_DATA_READ;
261 } else {
262 brq.cmd.opcode = writecmd;
263 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Pierre Ossmanb146d262007-07-24 19:16:54 +0200266 mmc_set_data_timeout(&brq.data, card);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200269 brq.data.sg_len = mmc_queue_map_sg(mq);
270
271 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Pierre Ossmanb41e9c72008-07-29 01:23:24 +0200273 /*
274 * Adjust the sg list so it is the same size as the
275 * request.
276 */
Alex Dubov14d836e2007-04-13 19:04:38 +0200277 if (brq.data.blocks !=
278 (req->nr_sectors >> (md->block_bits - 9))) {
279 data_size = brq.data.blocks * brq.data.blksz;
Pierre Ossmanb41e9c72008-07-29 01:23:24 +0200280 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
281 data_size -= sg->length;
Alex Dubov14d836e2007-04-13 19:04:38 +0200282 if (data_size <= 0) {
Pierre Ossmanb41e9c72008-07-29 01:23:24 +0200283 sg->length += data_size;
284 i++;
Alex Dubov14d836e2007-04-13 19:04:38 +0200285 break;
286 }
287 }
Pierre Ossmanb41e9c72008-07-29 01:23:24 +0200288 brq.data.sg_len = i;
Alex Dubov14d836e2007-04-13 19:04:38 +0200289 }
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200292
293 mmc_queue_bounce_post(mq);
294
Pierre Ossman979ce722008-06-29 12:19:47 +0200295 /*
296 * Check for errors here, but don't jump to cmd_err
297 * until later as we need to wait for the card to leave
298 * programming mode even when things go wrong.
299 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (brq.cmd.error) {
301 printk(KERN_ERR "%s: error %d sending read/write command\n",
302 req->rq_disk->disk_name, brq.cmd.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
305 if (brq.data.error) {
306 printk(KERN_ERR "%s: error %d transferring data\n",
307 req->rq_disk->disk_name, brq.data.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309
310 if (brq.stop.error) {
311 printk(KERN_ERR "%s: error %d sending stop command\n",
312 req->rq_disk->disk_name, brq.stop.error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
David Brownell7213d172007-08-08 09:10:23 -0700315 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King2ed6d222006-09-24 10:46:43 +0100316 do {
317 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Russell King2ed6d222006-09-24 10:46:43 +0100319 cmd.opcode = MMC_SEND_STATUS;
320 cmd.arg = card->rca << 16;
321 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
322 err = mmc_wait_for_cmd(card->host, &cmd, 5);
323 if (err) {
324 printk(KERN_ERR "%s: error %d requesting status\n",
325 req->rq_disk->disk_name, err);
326 goto cmd_err;
327 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100328 /*
329 * Some cards mishandle the status bits,
330 * so make sure to check both the busy
331 * indication and the card state.
332 */
333 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
334 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336#if 0
Russell King2ed6d222006-09-24 10:46:43 +0100337 if (cmd.resp[0] & ~0x00000900)
338 printk(KERN_ERR "%s: status = %08x\n",
339 req->rq_disk->disk_name, cmd.resp[0]);
340 if (mmc_decode_status(cmd.resp))
341 goto cmd_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342#endif
Russell King2ed6d222006-09-24 10:46:43 +0100343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Pierre Ossman979ce722008-06-29 12:19:47 +0200345 if (brq.cmd.error || brq.data.error || brq.stop.error)
346 goto cmd_err;
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /*
349 * A block was successfully transferred.
350 */
351 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500352 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 spin_unlock_irq(&md->lock);
354 } while (ret);
355
Pierre Ossmanb8558852007-01-03 19:47:29 +0100356 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 return 1;
359
360 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700361 /*
362 * If this is an SD card and we're writing, we can first
363 * mark the known good sectors as ok.
364 *
365 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +0200366 * as reported by the controller (which might be less than
367 * the real number of written sectors, but never more).
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 Ossman23af6032008-07-06 01:10:27 +0200372 if (rq_data_dir(req) != READ) {
373 if (mmc_card_sd(card)) {
374 u32 blocks;
375 unsigned int bytes;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700376
Pierre Ossman23af6032008-07-06 01:10:27 +0200377 blocks = mmc_sd_num_wr_blocks(card);
378 if (blocks != (u32)-1) {
379 if (card->csd.write_partial)
380 bytes = blocks << md->block_bits;
381 else
382 bytes = blocks << 9;
383 spin_lock_irq(&md->lock);
384 ret = __blk_end_request(req, 0, bytes);
385 spin_unlock_irq(&md->lock);
386 }
387 } else {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700388 spin_lock_irq(&md->lock);
Pierre Ossman23af6032008-07-06 01:10:27 +0200389 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700390 spin_unlock_irq(&md->lock);
391 }
Pierre Ossman176f00f2006-10-04 02:15:41 -0700392 }
393
Pierre Ossmanb8558852007-01-03 19:47:29 +0100394 mmc_release_host(card->host);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500397 while (ret)
398 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 spin_unlock_irq(&md->lock);
400
401 return 0;
402}
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Russell Kinga6f6c962006-01-03 22:38:44 +0000405static inline int mmc_blk_readonly(struct mmc_card *card)
406{
407 return mmc_card_readonly(card) ||
408 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
412{
413 struct mmc_blk_data *md;
414 int devidx, ret;
415
416 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
417 if (devidx >= MMC_NUM_MINORS)
418 return ERR_PTR(-ENOSPC);
419 __set_bit(devidx, dev_use);
420
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700421 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +0000422 if (!md) {
423 ret = -ENOMEM;
424 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Russell Kinga6f6c962006-01-03 22:38:44 +0000426
Russell Kinga6f6c962006-01-03 22:38:44 +0000427
428 /*
429 * Set the read-only status based on the supported commands
430 * and the write protect switch.
431 */
432 md->read_only = mmc_blk_readonly(card);
433
434 /*
Pierre Ossman6fe9feb2006-09-07 16:01:30 +0100435 * Both SD and MMC specifications state (although a bit
436 * unclearly in the MMC case) that a block size of 512
437 * bytes must always be supported by the card.
Russell Kinga6f6c962006-01-03 22:38:44 +0000438 */
Pierre Ossman6fe9feb2006-09-07 16:01:30 +0100439 md->block_bits = 9;
Russell Kinga6f6c962006-01-03 22:38:44 +0000440
441 md->disk = alloc_disk(1 << MMC_SHIFT);
442 if (md->disk == NULL) {
443 ret = -ENOMEM;
444 goto err_kfree;
445 }
446
447 spin_lock_init(&md->lock);
448 md->usage = 1;
449
450 ret = mmc_init_queue(&md->queue, card, &md->lock);
451 if (ret)
452 goto err_putdisk;
453
Russell Kinga6f6c962006-01-03 22:38:44 +0000454 md->queue.issue_fn = mmc_blk_issue_rq;
455 md->queue.data = md;
456
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200457 md->disk->major = MMC_BLOCK_MAJOR;
Russell Kinga6f6c962006-01-03 22:38:44 +0000458 md->disk->first_minor = devidx << MMC_SHIFT;
459 md->disk->fops = &mmc_bdops;
460 md->disk->private_data = md;
461 md->disk->queue = md->queue.queue;
462 md->disk->driverfs_dev = &card->dev;
463
464 /*
465 * As discussed on lkml, GENHD_FL_REMOVABLE should:
466 *
467 * - be set for removable media with permanent block devices
468 * - be unset for removable block devices with permanent media
469 *
470 * Since MMC block devices clearly fall under the second
471 * case, we do not set GENHD_FL_REMOVABLE. Userspace
472 * should use the block device creation/destruction hotplug
473 * messages to tell when the card is present.
474 */
475
476 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +0000477
478 blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
479
Pierre Ossman85a18ad2007-02-17 22:15:27 +0100480 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
481 /*
482 * The EXT_CSD sector count is in number or 512 byte
483 * sectors.
484 */
485 set_capacity(md->disk, card->ext_csd.sectors);
486 } else {
487 /*
488 * The CSD capacity field is in units of read_blkbits.
489 * set_capacity takes units of 512 bytes.
490 */
491 set_capacity(md->disk,
492 card->csd.capacity << (card->csd.read_blkbits - 9));
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +0000495
496 err_putdisk:
497 put_disk(md->disk);
498 err_kfree:
499 kfree(md);
500 out:
501 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504static int
505mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
506{
507 struct mmc_command cmd;
508 int err;
509
Philip Langdalefba68bd2007-01-04 06:57:32 -0800510 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
511 if (mmc_card_blockaddr(card))
512 return 0;
513
Pierre Ossmanb8558852007-01-03 19:47:29 +0100514 mmc_claim_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 cmd.opcode = MMC_SET_BLOCKLEN;
Russell Kingd2b18392005-12-22 23:21:38 +0000516 cmd.arg = 1 << md->block_bits;
David Brownell7213d172007-08-08 09:10:23 -0700517 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 err = mmc_wait_for_cmd(card->host, &cmd, 5);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100519 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 if (err) {
522 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
523 md->disk->disk_name, cmd.arg, err);
524 return -EINVAL;
525 }
526
527 return 0;
528}
529
530static int mmc_blk_probe(struct mmc_card *card)
531{
532 struct mmc_blk_data *md;
533 int err;
534
Pierre Ossman912490d2005-05-21 10:27:02 +0100535 /*
536 * Check that the card supports the command class(es) we need.
537 */
538 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return -ENODEV;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 md = mmc_blk_alloc(card);
542 if (IS_ERR(md))
543 return PTR_ERR(md);
544
545 err = mmc_blk_set_blksize(md, card);
546 if (err)
547 goto out;
548
Andrew Morton51828ab2006-01-13 21:44:18 +0000549 printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Andrew Morton51828ab2006-01-13 21:44:18 +0000551 (unsigned long long)(get_capacity(md->disk) >> 1),
552 md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 mmc_set_drvdata(card, md);
555 add_disk(md->disk);
556 return 0;
557
558 out:
559 mmc_blk_put(md);
560
561 return err;
562}
563
564static void mmc_blk_remove(struct mmc_card *card)
565{
566 struct mmc_blk_data *md = mmc_get_drvdata(card);
567
568 if (md) {
Pierre Ossman89b4e132006-11-14 22:08:16 +0100569 /* Stop new requests from getting into the queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 del_gendisk(md->disk);
571
Pierre Ossman89b4e132006-11-14 22:08:16 +0100572 /* Then flush out any already in there */
573 mmc_cleanup_queue(&md->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 mmc_blk_put(md);
576 }
577 mmc_set_drvdata(card, NULL);
578}
579
580#ifdef CONFIG_PM
581static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
582{
583 struct mmc_blk_data *md = mmc_get_drvdata(card);
584
585 if (md) {
586 mmc_queue_suspend(&md->queue);
587 }
588 return 0;
589}
590
591static int mmc_blk_resume(struct mmc_card *card)
592{
593 struct mmc_blk_data *md = mmc_get_drvdata(card);
594
595 if (md) {
596 mmc_blk_set_blksize(md, card);
597 mmc_queue_resume(&md->queue);
598 }
599 return 0;
600}
601#else
602#define mmc_blk_suspend NULL
603#define mmc_blk_resume NULL
604#endif
605
606static struct mmc_driver mmc_driver = {
607 .drv = {
608 .name = "mmcblk",
609 },
610 .probe = mmc_blk_probe,
611 .remove = mmc_blk_remove,
612 .suspend = mmc_blk_suspend,
613 .resume = mmc_blk_resume,
614};
615
616static int __init mmc_blk_init(void)
617{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900618 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200620 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
621 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900624 res = mmc_register_driver(&mmc_driver);
625 if (res)
626 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900628 return 0;
629 out2:
630 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 out:
632 return res;
633}
634
635static void __exit mmc_blk_exit(void)
636{
637 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200638 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641module_init(mmc_blk_init);
642module_exit(mmc_blk_exit);
643
644MODULE_LICENSE("GPL");
645MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
646