2 * Block driver for media (i.e., flash cards)
4 * Copyright 2002 Hewlett-Packard Company
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.
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.
14 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 * Author: Andrew Christian
19 #include <linux/moduleparam.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.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>
32 #include <linux/mmc/card.h>
33 #include <linux/mmc/protocol.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
38 #include "mmc_queue.h"
41 * max 8 partitions per card
48 * There is one mmc_blk_data per slot.
53 struct mmc_queue queue
;
56 unsigned int block_bits
;
59 static DECLARE_MUTEX(open_lock
);
61 static struct mmc_blk_data
*mmc_blk_get(struct gendisk
*disk
)
63 struct mmc_blk_data
*md
;
66 md
= disk
->private_data
;
67 if (md
&& md
->usage
== 0)
76 static void mmc_blk_put(struct mmc_blk_data
*md
)
82 mmc_cleanup_queue(&md
->queue
);
88 static int mmc_blk_open(struct inode
*inode
, struct file
*filp
)
90 struct mmc_blk_data
*md
;
93 md
= mmc_blk_get(inode
->i_bdev
->bd_disk
);
96 check_disk_change(inode
->i_bdev
);
103 static int mmc_blk_release(struct inode
*inode
, struct file
*filp
)
105 struct mmc_blk_data
*md
= inode
->i_bdev
->bd_disk
->private_data
;
112 mmc_blk_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int cmd
, unsigned long arg
)
114 struct block_device
*bdev
= inode
->i_bdev
;
116 if (cmd
== HDIO_GETGEO
) {
117 struct hd_geometry geo
;
119 memset(&geo
, 0, sizeof(struct hd_geometry
));
121 geo
.cylinders
= get_capacity(bdev
->bd_disk
) / (4 * 16);
124 geo
.start
= get_start_sect(bdev
);
126 return copy_to_user((void __user
*)arg
, &geo
, sizeof(geo
))
133 static 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
,
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
;
147 static int mmc_blk_prep_rq(struct mmc_queue
*mq
, struct request
*req
)
149 struct mmc_blk_data
*md
= mq
->data
;
150 int stat
= BLKPREP_OK
;
153 * If we have no device, we haven't finished initialising.
155 if (!md
|| !mq
->card
) {
156 printk(KERN_ERR
"%s: killing request - no device/host\n",
157 req
->rq_disk
->disk_name
);
164 static int mmc_blk_issue_rq(struct mmc_queue
*mq
, struct request
*req
)
166 struct mmc_blk_data
*md
= mq
->data
;
167 struct mmc_card
*card
= md
->queue
.card
;
170 if (mmc_card_claim_host(card
))
174 struct mmc_blk_request brq
;
175 struct mmc_command cmd
;
177 memset(&brq
, 0, sizeof(struct mmc_blk_request
));
178 brq
.mrq
.cmd
= &brq
.cmd
;
179 brq
.mrq
.data
= &brq
.data
;
181 brq
.cmd
.arg
= req
->sector
<< 9;
182 brq
.cmd
.flags
= MMC_RSP_R1
;
184 brq
.data
.timeout_ns
= card
->csd
.tacc_ns
* 10;
185 brq
.data
.timeout_clks
= card
->csd
.tacc_clks
* 10;
186 brq
.data
.blksz_bits
= md
->block_bits
;
187 brq
.data
.blocks
= req
->nr_sectors
>> (md
->block_bits
- 9);
188 brq
.stop
.opcode
= MMC_STOP_TRANSMISSION
;
190 brq
.stop
.flags
= MMC_RSP_R1B
;
192 if (rq_data_dir(req
) == READ
) {
193 brq
.cmd
.opcode
= brq
.data
.blocks
> 1 ? MMC_READ_MULTIPLE_BLOCK
: MMC_READ_SINGLE_BLOCK
;
194 brq
.data
.flags
|= MMC_DATA_READ
;
196 brq
.cmd
.opcode
= MMC_WRITE_BLOCK
;
197 brq
.cmd
.flags
= MMC_RSP_R1B
;
198 brq
.data
.flags
|= MMC_DATA_WRITE
;
201 brq
.mrq
.stop
= brq
.data
.blocks
> 1 ? &brq
.stop
: NULL
;
203 mmc_wait_for_req(card
->host
, &brq
.mrq
);
205 printk(KERN_ERR
"%s: error %d sending read/write command\n",
206 req
->rq_disk
->disk_name
, brq
.cmd
.error
);
210 if (brq
.data
.error
) {
211 printk(KERN_ERR
"%s: error %d transferring data\n",
212 req
->rq_disk
->disk_name
, brq
.data
.error
);
216 if (brq
.stop
.error
) {
217 printk(KERN_ERR
"%s: error %d sending stop command\n",
218 req
->rq_disk
->disk_name
, brq
.stop
.error
);
225 cmd
.opcode
= MMC_SEND_STATUS
;
226 cmd
.arg
= card
->rca
<< 16;
227 cmd
.flags
= MMC_RSP_R1
;
228 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 5);
230 printk(KERN_ERR
"%s: error %d requesting status\n",
231 req
->rq_disk
->disk_name
, err
);
234 } while (!(cmd
.resp
[0] & R1_READY_FOR_DATA
));
237 if (cmd
.resp
[0] & ~0x00000900)
238 printk(KERN_ERR
"%s: status = %08x\n",
239 req
->rq_disk
->disk_name
, cmd
.resp
[0]);
240 if (mmc_decode_status(cmd
.resp
))
245 * A block was successfully transferred.
247 spin_lock_irq(&md
->lock
);
248 ret
= end_that_request_chunk(req
, 1, brq
.data
.bytes_xfered
);
251 * The whole request completed successfully.
253 add_disk_randomness(req
->rq_disk
);
254 blkdev_dequeue_request(req
);
255 end_that_request_last(req
);
257 spin_unlock_irq(&md
->lock
);
260 mmc_card_release_host(card
);
265 mmc_card_release_host(card
);
268 * This is a little draconian, but until we get proper
269 * error handling sorted out here, its the best we can
270 * do - especially as some hosts have no idea how much
271 * data was transferred before the error occurred.
273 spin_lock_irq(&md
->lock
);
275 ret
= end_that_request_chunk(req
, 0,
276 req
->current_nr_sectors
<< 9);
279 add_disk_randomness(req
->rq_disk
);
280 blkdev_dequeue_request(req
);
281 end_that_request_last(req
);
282 spin_unlock_irq(&md
->lock
);
287 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
289 static unsigned long dev_use
[MMC_NUM_MINORS
/(8*sizeof(unsigned long))];
291 static struct mmc_blk_data
*mmc_blk_alloc(struct mmc_card
*card
)
293 struct mmc_blk_data
*md
;
296 devidx
= find_first_zero_bit(dev_use
, MMC_NUM_MINORS
);
297 if (devidx
>= MMC_NUM_MINORS
)
298 return ERR_PTR(-ENOSPC
);
299 __set_bit(devidx
, dev_use
);
301 md
= kmalloc(sizeof(struct mmc_blk_data
), GFP_KERNEL
);
303 memset(md
, 0, sizeof(struct mmc_blk_data
));
305 md
->disk
= alloc_disk(1 << MMC_SHIFT
);
306 if (md
->disk
== NULL
) {
308 md
= ERR_PTR(-ENOMEM
);
312 spin_lock_init(&md
->lock
);
315 ret
= mmc_init_queue(&md
->queue
, card
, &md
->lock
);
322 md
->queue
.prep_fn
= mmc_blk_prep_rq
;
323 md
->queue
.issue_fn
= mmc_blk_issue_rq
;
326 md
->disk
->major
= major
;
327 md
->disk
->first_minor
= devidx
<< MMC_SHIFT
;
328 md
->disk
->fops
= &mmc_bdops
;
329 md
->disk
->private_data
= md
;
330 md
->disk
->queue
= md
->queue
.queue
;
331 md
->disk
->driverfs_dev
= &card
->dev
;
333 sprintf(md
->disk
->disk_name
, "mmcblk%d", devidx
);
334 sprintf(md
->disk
->devfs_name
, "mmc/blk%d", devidx
);
336 md
->block_bits
= card
->csd
.read_blkbits
;
338 blk_queue_hardsect_size(md
->queue
.queue
, 1 << md
->block_bits
);
339 set_capacity(md
->disk
, card
->csd
.capacity
);
346 mmc_blk_set_blksize(struct mmc_blk_data
*md
, struct mmc_card
*card
)
348 struct mmc_command cmd
;
351 mmc_card_claim_host(card
);
352 cmd
.opcode
= MMC_SET_BLOCKLEN
;
353 cmd
.arg
= 1 << card
->csd
.read_blkbits
;
354 cmd
.flags
= MMC_RSP_R1
;
355 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 5);
356 mmc_card_release_host(card
);
359 printk(KERN_ERR
"%s: unable to set block size to %d: %d\n",
360 md
->disk
->disk_name
, cmd
.arg
, err
);
367 static int mmc_blk_probe(struct mmc_card
*card
)
369 struct mmc_blk_data
*md
;
372 if (card
->csd
.cmdclass
& ~0x1ff)
375 if (card
->csd
.read_blkbits
< 9) {
376 printk(KERN_WARNING
"%s: read blocksize too small (%u)\n",
377 mmc_card_id(card
), 1 << card
->csd
.read_blkbits
);
381 md
= mmc_blk_alloc(card
);
385 err
= mmc_blk_set_blksize(md
, card
);
389 printk(KERN_INFO
"%s: %s %s %dKiB\n",
390 md
->disk
->disk_name
, mmc_card_id(card
), mmc_card_name(card
),
391 (card
->csd
.capacity
<< card
->csd
.read_blkbits
) / 1024);
393 mmc_set_drvdata(card
, md
);
403 static void mmc_blk_remove(struct mmc_card
*card
)
405 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
410 del_gendisk(md
->disk
);
413 * I think this is needed.
415 md
->disk
->queue
= NULL
;
417 devidx
= md
->disk
->first_minor
>> MMC_SHIFT
;
418 __clear_bit(devidx
, dev_use
);
422 mmc_set_drvdata(card
, NULL
);
426 static int mmc_blk_suspend(struct mmc_card
*card
, u32 state
)
428 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
431 mmc_queue_suspend(&md
->queue
);
436 static int mmc_blk_resume(struct mmc_card
*card
)
438 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
441 mmc_blk_set_blksize(md
, card
);
442 mmc_queue_resume(&md
->queue
);
447 #define mmc_blk_suspend NULL
448 #define mmc_blk_resume NULL
451 static struct mmc_driver mmc_driver
= {
455 .probe
= mmc_blk_probe
,
456 .remove
= mmc_blk_remove
,
457 .suspend
= mmc_blk_suspend
,
458 .resume
= mmc_blk_resume
,
461 static int __init
mmc_blk_init(void)
465 res
= register_blkdev(major
, "mmc");
467 printk(KERN_WARNING
"Unable to get major %d for MMC media: %d\n",
475 return mmc_register_driver(&mmc_driver
);
481 static void __exit
mmc_blk_exit(void)
483 mmc_unregister_driver(&mmc_driver
);
485 unregister_blkdev(major
, "mmc");
488 module_init(mmc_blk_init
);
489 module_exit(mmc_blk_exit
);
491 MODULE_LICENSE("GPL");
492 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
494 module_param(major
, int, 0444);
495 MODULE_PARM_DESC(major
, "specify the major device number for MMC block driver");