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
;
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
;
189 brq
.stop
.flags
= MMC_RSP_R1B
;
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
;
195 brq
.cmd
.opcode
= MMC_WRITE_BLOCK
;
196 brq
.cmd
.flags
= MMC_RSP_R1B
;
197 brq
.data
.flags
|= MMC_DATA_WRITE
;
200 brq
.mrq
.stop
= brq
.data
.blocks
> 1 ? &brq
.stop
: NULL
;
202 brq
.data
.sg
= mq
->sg
;
203 brq
.data
.sg_len
= blk_rq_map_sg(req
->q
, req
, brq
.data
.sg
);
205 mmc_wait_for_req(card
->host
, &brq
.mrq
);
207 printk(KERN_ERR
"%s: error %d sending read/write command\n",
208 req
->rq_disk
->disk_name
, brq
.cmd
.error
);
212 if (brq
.data
.error
) {
213 printk(KERN_ERR
"%s: error %d transferring data\n",
214 req
->rq_disk
->disk_name
, brq
.data
.error
);
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
);
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);
232 printk(KERN_ERR
"%s: error %d requesting status\n",
233 req
->rq_disk
->disk_name
, err
);
236 } while (!(cmd
.resp
[0] & R1_READY_FOR_DATA
));
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
))
247 * A block was successfully transferred.
249 spin_lock_irq(&md
->lock
);
250 ret
= end_that_request_chunk(req
, 1, brq
.data
.bytes_xfered
);
253 * The whole request completed successfully.
255 add_disk_randomness(req
->rq_disk
);
256 blkdev_dequeue_request(req
);
257 end_that_request_last(req
);
259 spin_unlock_irq(&md
->lock
);
262 mmc_card_release_host(card
);
267 mmc_card_release_host(card
);
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.
275 spin_lock_irq(&md
->lock
);
277 ret
= end_that_request_chunk(req
, 0,
278 req
->current_nr_sectors
<< 9);
281 add_disk_randomness(req
->rq_disk
);
282 blkdev_dequeue_request(req
);
283 end_that_request_last(req
);
284 spin_unlock_irq(&md
->lock
);
289 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
291 static unsigned long dev_use
[MMC_NUM_MINORS
/(8*sizeof(unsigned long))];
293 static struct mmc_blk_data
*mmc_blk_alloc(struct mmc_card
*card
)
295 struct mmc_blk_data
*md
;
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
);
303 md
= kmalloc(sizeof(struct mmc_blk_data
), GFP_KERNEL
);
305 memset(md
, 0, sizeof(struct mmc_blk_data
));
307 md
->disk
= alloc_disk(1 << MMC_SHIFT
);
308 if (md
->disk
== NULL
) {
310 md
= ERR_PTR(-ENOMEM
);
314 spin_lock_init(&md
->lock
);
317 ret
= mmc_init_queue(&md
->queue
, card
, &md
->lock
);
324 md
->queue
.prep_fn
= mmc_blk_prep_rq
;
325 md
->queue
.issue_fn
= mmc_blk_issue_rq
;
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
;
336 * As discussed on lkml, GENHD_FL_REMOVABLE should:
338 * - be set for removable media with permanent block devices
339 * - be unset for removable block devices with permanent media
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.
347 sprintf(md
->disk
->disk_name
, "mmcblk%d", devidx
);
348 sprintf(md
->disk
->devfs_name
, "mmc/blk%d", devidx
);
350 md
->block_bits
= card
->csd
.read_blkbits
;
352 blk_queue_hardsect_size(md
->queue
.queue
, 1 << md
->block_bits
);
353 set_capacity(md
->disk
, card
->csd
.capacity
);
360 mmc_blk_set_blksize(struct mmc_blk_data
*md
, struct mmc_card
*card
)
362 struct mmc_command cmd
;
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
);
373 printk(KERN_ERR
"%s: unable to set block size to %d: %d\n",
374 md
->disk
->disk_name
, cmd
.arg
, err
);
381 static int mmc_blk_probe(struct mmc_card
*card
)
383 struct mmc_blk_data
*md
;
387 * Check that the card supports the command class(es) we need.
389 if (!(card
->csd
.cmdclass
& CCC_BLOCK_READ
))
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
);
398 md
= mmc_blk_alloc(card
);
402 err
= mmc_blk_set_blksize(md
, card
);
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);
410 mmc_set_drvdata(card
, md
);
420 static void mmc_blk_remove(struct mmc_card
*card
)
422 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
427 del_gendisk(md
->disk
);
430 * I think this is needed.
432 md
->disk
->queue
= NULL
;
434 devidx
= md
->disk
->first_minor
>> MMC_SHIFT
;
435 __clear_bit(devidx
, dev_use
);
439 mmc_set_drvdata(card
, NULL
);
443 static int mmc_blk_suspend(struct mmc_card
*card
, pm_message_t state
)
445 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
448 mmc_queue_suspend(&md
->queue
);
453 static int mmc_blk_resume(struct mmc_card
*card
)
455 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
458 mmc_blk_set_blksize(md
, card
);
459 mmc_queue_resume(&md
->queue
);
464 #define mmc_blk_suspend NULL
465 #define mmc_blk_resume NULL
468 static struct mmc_driver mmc_driver
= {
472 .probe
= mmc_blk_probe
,
473 .remove
= mmc_blk_remove
,
474 .suspend
= mmc_blk_suspend
,
475 .resume
= mmc_blk_resume
,
478 static int __init
mmc_blk_init(void)
482 res
= register_blkdev(major
, "mmc");
484 printk(KERN_WARNING
"Unable to get major %d for MMC media: %d\n",
492 return mmc_register_driver(&mmc_driver
);
498 static void __exit
mmc_blk_exit(void)
500 mmc_unregister_driver(&mmc_driver
);
502 unregister_blkdev(major
, "mmc");
505 module_init(mmc_blk_init
);
506 module_exit(mmc_blk_exit
);
508 MODULE_LICENSE("GPL");
509 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
511 module_param(major
, int, 0444);
512 MODULE_PARM_DESC(major
, "specify the major device number for MMC block driver");