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