blob: d4eee99c2bf65ffe86798deb86cdd5ef0a2e81da [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
5 *
6 * Use consistent with the GNU GPL is permitted,
7 * provided that this copyright notice is
8 * preserved in its entirety in all copies and derived works.
9 *
10 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
11 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
12 * FITNESS FOR ANY PARTICULAR PURPOSE.
13 *
14 * Many thanks to Alessandro Rubini and Jonathan Corbet!
15 *
16 * Author: Andrew Christian
17 * 28 May 2002
18 */
19#include <linux/moduleparam.h>
20#include <linux/module.h>
21#include <linux/init.h>
22
23#include <linux/sched.h>
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>
30#include <linux/devfs_fs_kernel.h>
31
32#include <linux/mmc/card.h>
33#include <linux/mmc/protocol.h>
34
35#include <asm/system.h>
36#include <asm/uaccess.h>
37
38#include "mmc_queue.h"
39
40/*
41 * max 8 partitions per card
42 */
43#define MMC_SHIFT 3
44
45static int major;
46
47/*
48 * There is one mmc_blk_data per slot.
49 */
50struct mmc_blk_data {
51 spinlock_t lock;
52 struct gendisk *disk;
53 struct mmc_queue queue;
54
55 unsigned int usage;
56 unsigned int block_bits;
57};
58
59static DECLARE_MUTEX(open_lock);
60
61static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
62{
63 struct mmc_blk_data *md;
64
65 down(&open_lock);
66 md = disk->private_data;
67 if (md && md->usage == 0)
68 md = NULL;
69 if (md)
70 md->usage++;
71 up(&open_lock);
72
73 return md;
74}
75
76static void mmc_blk_put(struct mmc_blk_data *md)
77{
78 down(&open_lock);
79 md->usage--;
80 if (md->usage == 0) {
81 put_disk(md->disk);
82 mmc_cleanup_queue(&md->queue);
83 kfree(md);
84 }
85 up(&open_lock);
86}
87
88static int mmc_blk_open(struct inode *inode, struct file *filp)
89{
90 struct mmc_blk_data *md;
91 int ret = -ENXIO;
92
93 md = mmc_blk_get(inode->i_bdev->bd_disk);
94 if (md) {
95 if (md->usage == 2)
96 check_disk_change(inode->i_bdev);
97 ret = 0;
98 }
99
100 return ret;
101}
102
103static int mmc_blk_release(struct inode *inode, struct file *filp)
104{
105 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
106
107 mmc_blk_put(md);
108 return 0;
109}
110
111static int
112mmc_blk_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
113{
114 struct block_device *bdev = inode->i_bdev;
115
116 if (cmd == HDIO_GETGEO) {
117 struct hd_geometry geo;
118
119 memset(&geo, 0, sizeof(struct hd_geometry));
120
121 geo.cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
122 geo.heads = 4;
123 geo.sectors = 16;
124 geo.start = get_start_sect(bdev);
125
126 return copy_to_user((void __user *)arg, &geo, sizeof(geo))
127 ? -EFAULT : 0;
128 }
129
130 return -ENOTTY;
131}
132
133static struct block_device_operations mmc_bdops = {
134 .open = mmc_blk_open,
135 .release = mmc_blk_release,
136 .ioctl = mmc_blk_ioctl,
137 .owner = THIS_MODULE,
138};
139
140struct 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
147static int mmc_blk_prep_rq(struct mmc_queue *mq, struct request *req)
148{
149 struct mmc_blk_data *md = mq->data;
150 int stat = BLKPREP_OK;
151
152 /*
153 * If we have no device, we haven't finished initialising.
154 */
155 if (!md || !mq->card) {
156 printk(KERN_ERR "%s: killing request - no device/host\n",
157 req->rq_disk->disk_name);
158 stat = BLKPREP_KILL;
159 }
160
161 return stat;
162}
163
164static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
165{
166 struct mmc_blk_data *md = mq->data;
167 struct mmc_card *card = md->queue.card;
168 int ret;
169
170 if (mmc_card_claim_host(card))
171 goto cmd_err;
172
173 do {
174 struct mmc_blk_request brq;
175 struct mmc_command cmd;
176
177 memset(&brq, 0, sizeof(struct mmc_blk_request));
178 brq.mrq.cmd = &brq.cmd;
179 brq.mrq.data = &brq.data;
180
181 brq.cmd.arg = req->sector << 9;
182 brq.cmd.flags = MMC_RSP_R1;
183 brq.data.timeout_ns = card->csd.tacc_ns * 10;
184 brq.data.timeout_clks = card->csd.tacc_clks * 10;
185 brq.data.blksz_bits = md->block_bits;
186 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
187 brq.stop.opcode = MMC_STOP_TRANSMISSION;
188 brq.stop.arg = 0;
189 brq.stop.flags = MMC_RSP_R1B;
190
191 if (rq_data_dir(req) == READ) {
192 brq.cmd.opcode = brq.data.blocks > 1 ? MMC_READ_MULTIPLE_BLOCK : MMC_READ_SINGLE_BLOCK;
193 brq.data.flags |= MMC_DATA_READ;
194 } else {
195 brq.cmd.opcode = MMC_WRITE_BLOCK;
196 brq.cmd.flags = MMC_RSP_R1B;
197 brq.data.flags |= MMC_DATA_WRITE;
198 brq.data.blocks = 1;
199 }
200 brq.mrq.stop = brq.data.blocks > 1 ? &brq.stop : NULL;
201
202 brq.data.sg = mq->sg;
203 brq.data.sg_len = blk_rq_map_sg(req->q, req, brq.data.sg);
204
205 mmc_wait_for_req(card->host, &brq.mrq);
206 if (brq.cmd.error) {
207 printk(KERN_ERR "%s: error %d sending read/write command\n",
208 req->rq_disk->disk_name, brq.cmd.error);
209 goto cmd_err;
210 }
211
212 if (brq.data.error) {
213 printk(KERN_ERR "%s: error %d transferring data\n",
214 req->rq_disk->disk_name, brq.data.error);
215 goto cmd_err;
216 }
217
218 if (brq.stop.error) {
219 printk(KERN_ERR "%s: error %d sending stop command\n",
220 req->rq_disk->disk_name, brq.stop.error);
221 goto cmd_err;
222 }
223
224 do {
225 int err;
226
227 cmd.opcode = MMC_SEND_STATUS;
228 cmd.arg = card->rca << 16;
229 cmd.flags = MMC_RSP_R1;
230 err = mmc_wait_for_cmd(card->host, &cmd, 5);
231 if (err) {
232 printk(KERN_ERR "%s: error %d requesting status\n",
233 req->rq_disk->disk_name, err);
234 goto cmd_err;
235 }
236 } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
237
238#if 0
239 if (cmd.resp[0] & ~0x00000900)
240 printk(KERN_ERR "%s: status = %08x\n",
241 req->rq_disk->disk_name, cmd.resp[0]);
242 if (mmc_decode_status(cmd.resp))
243 goto cmd_err;
244#endif
245
246 /*
247 * A block was successfully transferred.
248 */
249 spin_lock_irq(&md->lock);
250 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
251 if (!ret) {
252 /*
253 * The whole request completed successfully.
254 */
255 add_disk_randomness(req->rq_disk);
256 blkdev_dequeue_request(req);
257 end_that_request_last(req);
258 }
259 spin_unlock_irq(&md->lock);
260 } while (ret);
261
262 mmc_card_release_host(card);
263
264 return 1;
265
266 cmd_err:
267 mmc_card_release_host(card);
268
269 /*
270 * This is a little draconian, but until we get proper
271 * error handling sorted out here, its the best we can
272 * do - especially as some hosts have no idea how much
273 * data was transferred before the error occurred.
274 */
275 spin_lock_irq(&md->lock);
276 do {
277 ret = end_that_request_chunk(req, 0,
278 req->current_nr_sectors << 9);
279 } while (ret);
280
281 add_disk_randomness(req->rq_disk);
282 blkdev_dequeue_request(req);
283 end_that_request_last(req);
284 spin_unlock_irq(&md->lock);
285
286 return 0;
287}
288
289#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
290
291static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
292
293static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
294{
295 struct mmc_blk_data *md;
296 int devidx, ret;
297
298 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
299 if (devidx >= MMC_NUM_MINORS)
300 return ERR_PTR(-ENOSPC);
301 __set_bit(devidx, dev_use);
302
303 md = kmalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
304 if (md) {
305 memset(md, 0, sizeof(struct mmc_blk_data));
306
307 md->disk = alloc_disk(1 << MMC_SHIFT);
308 if (md->disk == NULL) {
309 kfree(md);
310 md = ERR_PTR(-ENOMEM);
311 goto out;
312 }
313
314 spin_lock_init(&md->lock);
315 md->usage = 1;
316
317 ret = mmc_init_queue(&md->queue, card, &md->lock);
318 if (ret) {
319 put_disk(md->disk);
320 kfree(md);
321 md = ERR_PTR(ret);
322 goto out;
323 }
324 md->queue.prep_fn = mmc_blk_prep_rq;
325 md->queue.issue_fn = mmc_blk_issue_rq;
326 md->queue.data = md;
327
328 md->disk->major = major;
329 md->disk->first_minor = devidx << MMC_SHIFT;
330 md->disk->fops = &mmc_bdops;
331 md->disk->private_data = md;
332 md->disk->queue = md->queue.queue;
333 md->disk->driverfs_dev = &card->dev;
334
335 /*
336 * As discussed on lkml, GENHD_FL_REMOVABLE should:
337 *
338 * - be set for removable media with permanent block devices
339 * - be unset for removable block devices with permanent media
340 *
341 * Since MMC block devices clearly fall under the second
342 * case, we do not set GENHD_FL_REMOVABLE. Userspace
343 * should use the block device creation/destruction hotplug
344 * messages to tell when the card is present.
345 */
346
347 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
348 sprintf(md->disk->devfs_name, "mmc/blk%d", devidx);
349
350 md->block_bits = card->csd.read_blkbits;
351
352 blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
353 set_capacity(md->disk, card->csd.capacity);
354 }
355 out:
356 return md;
357}
358
359static int
360mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
361{
362 struct mmc_command cmd;
363 int err;
364
365 mmc_card_claim_host(card);
366 cmd.opcode = MMC_SET_BLOCKLEN;
367 cmd.arg = 1 << card->csd.read_blkbits;
368 cmd.flags = MMC_RSP_R1;
369 err = mmc_wait_for_cmd(card->host, &cmd, 5);
370 mmc_card_release_host(card);
371
372 if (err) {
373 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
374 md->disk->disk_name, cmd.arg, err);
375 return -EINVAL;
376 }
377
378 return 0;
379}
380
381static int mmc_blk_probe(struct mmc_card *card)
382{
383 struct mmc_blk_data *md;
384 int err;
385
Pierre Ossman912490d2005-05-21 10:27:02 +0100386 /*
387 * Check that the card supports the command class(es) we need.
388 */
389 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return -ENODEV;
391
392 if (card->csd.read_blkbits < 9) {
393 printk(KERN_WARNING "%s: read blocksize too small (%u)\n",
394 mmc_card_id(card), 1 << card->csd.read_blkbits);
395 return -ENODEV;
396 }
397
398 md = mmc_blk_alloc(card);
399 if (IS_ERR(md))
400 return PTR_ERR(md);
401
402 err = mmc_blk_set_blksize(md, card);
403 if (err)
404 goto out;
405
406 printk(KERN_INFO "%s: %s %s %dKiB\n",
407 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
408 (card->csd.capacity << card->csd.read_blkbits) / 1024);
409
410 mmc_set_drvdata(card, md);
411 add_disk(md->disk);
412 return 0;
413
414 out:
415 mmc_blk_put(md);
416
417 return err;
418}
419
420static void mmc_blk_remove(struct mmc_card *card)
421{
422 struct mmc_blk_data *md = mmc_get_drvdata(card);
423
424 if (md) {
425 int devidx;
426
427 del_gendisk(md->disk);
428
429 /*
430 * I think this is needed.
431 */
432 md->disk->queue = NULL;
433
434 devidx = md->disk->first_minor >> MMC_SHIFT;
435 __clear_bit(devidx, dev_use);
436
437 mmc_blk_put(md);
438 }
439 mmc_set_drvdata(card, NULL);
440}
441
442#ifdef CONFIG_PM
443static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
444{
445 struct mmc_blk_data *md = mmc_get_drvdata(card);
446
447 if (md) {
448 mmc_queue_suspend(&md->queue);
449 }
450 return 0;
451}
452
453static int mmc_blk_resume(struct mmc_card *card)
454{
455 struct mmc_blk_data *md = mmc_get_drvdata(card);
456
457 if (md) {
458 mmc_blk_set_blksize(md, card);
459 mmc_queue_resume(&md->queue);
460 }
461 return 0;
462}
463#else
464#define mmc_blk_suspend NULL
465#define mmc_blk_resume NULL
466#endif
467
468static struct mmc_driver mmc_driver = {
469 .drv = {
470 .name = "mmcblk",
471 },
472 .probe = mmc_blk_probe,
473 .remove = mmc_blk_remove,
474 .suspend = mmc_blk_suspend,
475 .resume = mmc_blk_resume,
476};
477
478static int __init mmc_blk_init(void)
479{
480 int res = -ENOMEM;
481
482 res = register_blkdev(major, "mmc");
483 if (res < 0) {
484 printk(KERN_WARNING "Unable to get major %d for MMC media: %d\n",
485 major, res);
486 goto out;
487 }
488 if (major == 0)
489 major = res;
490
491 devfs_mk_dir("mmc");
492 return mmc_register_driver(&mmc_driver);
493
494 out:
495 return res;
496}
497
498static void __exit mmc_blk_exit(void)
499{
500 mmc_unregister_driver(&mmc_driver);
501 devfs_remove("mmc");
502 unregister_blkdev(major, "mmc");
503}
504
505module_init(mmc_blk_init);
506module_exit(mmc_blk_exit);
507
508MODULE_LICENSE("GPL");
509MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
510
511module_param(major, int, 0444);
512MODULE_PARM_DESC(major, "specify the major device number for MMC block driver");