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>
22 #include <linux/config.h>
24 #include <linux/sched.h>
25 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/hdreg.h>
29 #include <linux/kdev_t.h>
30 #include <linux/blkdev.h>
31 #include <linux/mutex.h>
32 #include <linux/scatterlist.h>
34 #include <linux/mmc/card.h>
35 #include <linux/mmc/host.h>
36 #include <linux/mmc/protocol.h>
37 #include <linux/mmc/host.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41 #ifdef CONFIG_ARCH_UC7101
42 #include <asm/arch/gpio.h>
45 #include "mmc_queue.h"
48 * max 8 partitions per card
52 #if 0 // mask by Victor Yu. 03-07-2007
59 * There is one mmc_blk_data per slot.
64 struct mmc_queue queue
;
67 unsigned int block_bits
;
68 unsigned int read_only
;
71 static DEFINE_MUTEX(open_lock
);
73 static struct mmc_blk_data
*mmc_blk_get(struct gendisk
*disk
)
75 struct mmc_blk_data
*md
;
77 mutex_lock(&open_lock
);
78 md
= disk
->private_data
;
79 if (md
&& md
->usage
== 0)
83 mutex_unlock(&open_lock
);
88 static void mmc_blk_put(struct mmc_blk_data
*md
)
90 mutex_lock(&open_lock
);
94 mmc_cleanup_queue(&md
->queue
);
97 mutex_unlock(&open_lock
);
100 static int mmc_blk_open(struct inode
*inode
, struct file
*filp
)
102 struct mmc_blk_data
*md
;
105 #if 0 // mask by Victor Yu. 03-07-2007
106 md
= mmc_blk_get(inode
->i_bdev
->bd_disk
);
108 md
= mmc_blk_get(inode
->u
.i_bdev
->bd_disk
);
112 #if 0 // mask by Victor Yu. 03-07-2007
113 check_disk_change(inode
->i_bdev
);
115 check_disk_change(inode
->u
.i_bdev
);
119 if ((filp
->f_mode
& FMODE_WRITE
) && md
->read_only
)
126 static int mmc_blk_release(struct inode
*inode
, struct file
*filp
)
128 #if 0 // mask by Victor Yu. 03-07-2007
129 struct mmc_blk_data
*md
= inode
->i_bdev
->bd_disk
->private_data
;
131 struct mmc_blk_data
*md
= inode
->u
.i_bdev
->bd_disk
->private_data
;
139 mmc_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
141 geo
->cylinders
= get_capacity(bdev
->bd_disk
) / (4 * 16);
147 static struct block_device_operations mmc_bdops
= {
148 .open
= mmc_blk_open
,
149 .release
= mmc_blk_release
,
150 .getgeo
= mmc_blk_getgeo
,
151 .owner
= THIS_MODULE
,
154 struct mmc_blk_request
{
155 struct mmc_request mrq
;
156 struct mmc_command cmd
;
157 struct mmc_command stop
;
158 struct mmc_data data
;
161 static int mmc_blk_prep_rq(struct mmc_queue
*mq
, struct request
*req
)
163 struct mmc_blk_data
*md
= mq
->data
;
164 int stat
= BLKPREP_OK
;
167 * If we have no device, we haven't finished initialising.
169 if (!md
|| !mq
->card
) {
170 printk(KERN_ERR
"%s: killing request - no device/host\n",
171 req
->rq_disk
->disk_name
);
178 static u32
mmc_sd_num_wr_blocks(struct mmc_card
*card
)
183 struct mmc_request mrq
;
184 struct mmc_command cmd
;
185 struct mmc_data data
;
186 unsigned int timeout_us
;
188 struct scatterlist sg
;
190 memset(&cmd
, 0, sizeof(struct mmc_command
));
192 cmd
.opcode
= MMC_APP_CMD
;
193 cmd
.arg
= card
->rca
<< 16;
194 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
196 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
197 if ((err
!= MMC_ERR_NONE
) || !(cmd
.resp
[0] & R1_APP_CMD
))
200 memset(&cmd
, 0, sizeof(struct mmc_command
));
202 cmd
.opcode
= SD_APP_SEND_NUM_WR_BLKS
;
204 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_ADTC
;
206 memset(&data
, 0, sizeof(struct mmc_data
));
208 data
.timeout_ns
= card
->csd
.tacc_ns
* 100;
209 data
.timeout_clks
= card
->csd
.tacc_clks
* 100;
211 timeout_us
= data
.timeout_ns
/ 1000;
212 timeout_us
+= data
.timeout_clks
* 1000 /
213 (card
->host
->ios
.clock
/ 1000);
215 if (timeout_us
> 100000) {
216 data
.timeout_ns
= 100000000;
217 data
.timeout_clks
= 0;
222 data
.flags
= MMC_DATA_READ
;
226 memset(&mrq
, 0, sizeof(struct mmc_request
));
231 sg_init_one(&sg
, &blocks
, 4);
233 mmc_wait_for_req(card
->host
, &mrq
);
235 if (cmd
.error
!= MMC_ERR_NONE
|| data
.error
!= MMC_ERR_NONE
)
238 blocks
= ntohl(blocks
);
243 static int mmc_blk_issue_rq(struct mmc_queue
*mq
, struct request
*req
)
245 struct mmc_blk_data
*md
= mq
->data
;
246 struct mmc_card
*card
= md
->queue
.card
;
247 struct mmc_blk_request brq
;
250 if (mmc_card_claim_host(card
))
254 struct mmc_command cmd
;
255 u32 readcmd
, writecmd
;
257 memset(&brq
, 0, sizeof(struct mmc_blk_request
));
258 brq
.mrq
.cmd
= &brq
.cmd
;
259 brq
.mrq
.data
= &brq
.data
;
261 brq
.cmd
.arg
= req
->sector
<< 9;
262 brq
.cmd
.flags
= MMC_RSP_R1
| MMC_CMD_ADTC
;
263 brq
.data
.blksz
= 1 << md
->block_bits
;
264 brq
.data
.blocks
= req
->nr_sectors
>> (md
->block_bits
- 9);
265 brq
.stop
.opcode
= MMC_STOP_TRANSMISSION
;
267 brq
.stop
.flags
= MMC_RSP_R1B
| MMC_CMD_AC
;
269 mmc_set_data_timeout(&brq
.data
, card
, rq_data_dir(req
) != READ
);
272 * If the host doesn't support multiple block writes, force
273 * block writes to single block. SD cards are excepted from
274 * this rule as they support querying the number of
275 * successfully written sectors.
277 if (rq_data_dir(req
) != READ
&&
278 !(card
->host
->caps
& MMC_CAP_MULTIWRITE
) &&
282 if (brq
.data
.blocks
> 1) {
283 brq
.data
.flags
|= MMC_DATA_MULTI
;
284 brq
.mrq
.stop
= &brq
.stop
;
285 readcmd
= MMC_READ_MULTIPLE_BLOCK
;
286 writecmd
= MMC_WRITE_MULTIPLE_BLOCK
;
289 readcmd
= MMC_READ_SINGLE_BLOCK
;
290 writecmd
= MMC_WRITE_BLOCK
;
293 if (rq_data_dir(req
) == READ
) {
294 brq
.cmd
.opcode
= readcmd
;
295 brq
.data
.flags
|= MMC_DATA_READ
;
297 brq
.cmd
.opcode
= writecmd
;
298 brq
.data
.flags
|= MMC_DATA_WRITE
;
301 brq
.data
.sg
= mq
->sg
;
302 brq
.data
.sg_len
= blk_rq_map_sg(req
->q
, req
, brq
.data
.sg
);
304 mmc_wait_for_req(card
->host
, &brq
.mrq
);
306 printk(KERN_ERR
"%s: victor error %d sending read/write command\n",
307 req
->rq_disk
->disk_name
, brq
.cmd
.error
);
311 if (brq
.data
.error
) {
312 printk(KERN_ERR
"%s: error %d transferring data\n",
313 req
->rq_disk
->disk_name
, brq
.data
.error
);
317 if (brq
.stop
.error
) {
318 printk(KERN_ERR
"%s: error %d sending stop command\n",
319 req
->rq_disk
->disk_name
, brq
.stop
.error
);
323 if (rq_data_dir(req
) != READ
) {
327 cmd
.opcode
= MMC_SEND_STATUS
;
328 cmd
.arg
= card
->rca
<< 16;
329 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
330 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 5);
332 printk(KERN_ERR
"%s: error %d requesting status\n",
333 req
->rq_disk
->disk_name
, err
);
336 } while (!(cmd
.resp
[0] & R1_READY_FOR_DATA
));
339 if (cmd
.resp
[0] & ~0x00000900)
340 printk(KERN_ERR
"%s: status = %08x\n",
341 req
->rq_disk
->disk_name
, cmd
.resp
[0]);
342 if (mmc_decode_status(cmd
.resp
))
348 * A block was successfully transferred.
350 spin_lock_irq(&md
->lock
);
351 ret
= end_that_request_chunk(req
, 1, brq
.data
.bytes_xfered
);
354 * The whole request completed successfully.
356 add_disk_randomness(req
->rq_disk
);
357 blkdev_dequeue_request(req
);
358 end_that_request_last(req
, 1);
360 spin_unlock_irq(&md
->lock
);
363 mmc_card_release_host(card
);
371 * If this is an SD card and we're writing, we can first
372 * mark the known good sectors as ok.
374 * If the card is not SD, we can still ok written sectors
375 * if the controller can do proper error reporting.
377 * For reads we just fail the entire chunk as that should
378 * be safe in all cases.
380 if (rq_data_dir(req
) != READ
&& mmc_card_sd(card
)) {
384 blocks
= mmc_sd_num_wr_blocks(card
);
385 if (blocks
!= (u32
)-1) {
386 if (card
->csd
.write_partial
)
387 bytes
= blocks
<< md
->block_bits
;
390 spin_lock_irq(&md
->lock
);
391 ret
= end_that_request_chunk(req
, 1, bytes
);
392 spin_unlock_irq(&md
->lock
);
394 } else if (rq_data_dir(req
) != READ
&&
395 (card
->host
->caps
& MMC_CAP_MULTIWRITE
)) {
396 spin_lock_irq(&md
->lock
);
397 ret
= end_that_request_chunk(req
, 1, brq
.data
.bytes_xfered
);
398 spin_unlock_irq(&md
->lock
);
401 mmc_card_release_host(card
);
403 spin_lock_irq(&md
->lock
);
405 ret
= end_that_request_chunk(req
, 0,
406 req
->current_nr_sectors
<< 9);
409 add_disk_randomness(req
->rq_disk
);
410 blkdev_dequeue_request(req
);
411 end_that_request_last(req
, 0);
412 spin_unlock_irq(&md
->lock
);
417 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
419 static unsigned long dev_use
[MMC_NUM_MINORS
/(8*sizeof(unsigned long))];
421 static inline int mmc_blk_readonly(struct mmc_card
*card
)
423 return mmc_card_readonly(card
) ||
424 !(card
->csd
.cmdclass
& CCC_BLOCK_WRITE
);
427 static struct mmc_blk_data
*mmc_blk_alloc(struct mmc_card
*card
)
429 struct mmc_blk_data
*md
;
432 devidx
= find_first_zero_bit(dev_use
, MMC_NUM_MINORS
);
433 if (devidx
>= MMC_NUM_MINORS
)
434 return ERR_PTR(-ENOSPC
);
435 __set_bit(devidx
, dev_use
);
437 md
= kmalloc(sizeof(struct mmc_blk_data
), GFP_KERNEL
);
443 memset(md
, 0, sizeof(struct mmc_blk_data
));
446 * Set the read-only status based on the supported commands
447 * and the write protect switch.
449 md
->read_only
= mmc_blk_readonly(card
);
452 * Both SD and MMC specifications state (although a bit
453 * unclearly in the MMC case) that a block size of 512
454 * bytes must always be supported by the card.
458 md
->disk
= alloc_disk(1 << MMC_SHIFT
);
459 if (md
->disk
== NULL
) {
464 spin_lock_init(&md
->lock
);
467 ret
= mmc_init_queue(&md
->queue
, card
, &md
->lock
);
471 md
->queue
.prep_fn
= mmc_blk_prep_rq
;
472 md
->queue
.issue_fn
= mmc_blk_issue_rq
;
475 md
->disk
->major
= major
;
476 md
->disk
->first_minor
= devidx
<< MMC_SHIFT
;
477 md
->disk
->fops
= &mmc_bdops
;
478 md
->disk
->private_data
= md
;
479 md
->disk
->queue
= md
->queue
.queue
;
480 md
->disk
->driverfs_dev
= &card
->dev
;
483 * As discussed on lkml, GENHD_FL_REMOVABLE should:
485 * - be set for removable media with permanent block devices
486 * - be unset for removable block devices with permanent media
488 * Since MMC block devices clearly fall under the second
489 * case, we do not set GENHD_FL_REMOVABLE. Userspace
490 * should use the block device creation/destruction hotplug
491 * messages to tell when the card is present.
494 sprintf(md
->disk
->disk_name
, "mmcblk%d", devidx
);
496 blk_queue_hardsect_size(md
->queue
.queue
, 1 << md
->block_bits
);
499 * The CSD capacity field is in units of read_blkbits.
500 * set_capacity takes units of 512 bytes.
502 set_capacity(md
->disk
, card
->csd
.capacity
<< (card
->csd
.read_blkbits
- 9));
514 mmc_blk_set_blksize(struct mmc_blk_data
*md
, struct mmc_card
*card
)
516 struct mmc_command cmd
;
519 mmc_card_claim_host(card
);
520 cmd
.opcode
= MMC_SET_BLOCKLEN
;
521 cmd
.arg
= 1 << md
->block_bits
;
522 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
523 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 5);
524 mmc_card_release_host(card
);
527 printk(KERN_ERR
"%s: unable to set block size to %d: %d\n",
528 md
->disk
->disk_name
, cmd
.arg
, err
);
535 #if 1 // add by Victor Yu. 03-07-2007
536 #ifdef CONFIG_ARCH_UC7101
537 #define SD_LED (1<<30)
539 static void moxa_pnp(int status
)
541 char *argv
[3] , *envp
[4] ;
544 envp
[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin" ;
545 envp
[2] = "TERM=console" ;
553 if(call_usermodehelper("/bin/pnp",argv
,envp
,0)==-1)
554 printk("execute mount failure\n") ;
555 #ifdef CONFIG_ARCH_UC7101
557 mcpu_gpio_set(SD_LED
, MCPU_GPIO_LOW
);
561 if(call_usermodehelper("/bin/pnp",argv
,envp
,0)==-1)
562 printk("execute umount failure\n") ;
563 #ifdef CONFIG_ARCH_UC7101
565 mcpu_gpio_set(SD_LED
, MCPU_GPIO_HIGH
);
571 static int mmc_blk_probe(struct mmc_card
*card
)
573 struct mmc_blk_data
*md
;
577 * Check that the card supports the command class(es) we need.
579 if (!(card
->csd
.cmdclass
& CCC_BLOCK_READ
))
582 md
= mmc_blk_alloc(card
);
586 err
= mmc_blk_set_blksize(md
, card
);
590 printk(KERN_INFO
"%s: %s %s %lluKiB %s\n",
591 md
->disk
->disk_name
, mmc_card_id(card
), mmc_card_name(card
),
592 (unsigned long long)(get_capacity(md
->disk
) >> 1),
593 md
->read_only
? "(ro)" : "");
595 mmc_set_drvdata(card
, md
);
597 #if 1 // add by Victor Yu. 03-07-2007
608 static void mmc_blk_remove(struct mmc_card
*card
)
610 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
615 del_gendisk(md
->disk
);
618 * I think this is needed.
620 md
->disk
->queue
= NULL
;
622 devidx
= md
->disk
->first_minor
>> MMC_SHIFT
;
623 __clear_bit(devidx
, dev_use
);
627 mmc_set_drvdata(card
, NULL
);
628 #if 1 // add by Victor Yu. 03-07-2007
634 static int mmc_blk_suspend(struct mmc_card
*card
, pm_message_t state
)
636 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
639 mmc_queue_suspend(&md
->queue
);
644 static int mmc_blk_resume(struct mmc_card
*card
)
646 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
649 mmc_blk_set_blksize(md
, card
);
650 mmc_queue_resume(&md
->queue
);
655 #define mmc_blk_suspend NULL
656 #define mmc_blk_resume NULL
659 static struct mmc_driver mmc_driver
= {
663 .probe
= mmc_blk_probe
,
664 .remove
= mmc_blk_remove
,
665 .suspend
= mmc_blk_suspend
,
666 .resume
= mmc_blk_resume
,
669 static int __init
mmc_blk_init(void)
673 res
= register_blkdev(major
, "mmc");
675 printk(KERN_WARNING
"Unable to get major %d for MMC media: %d\n",
682 return mmc_register_driver(&mmc_driver
);
688 static void __exit
mmc_blk_exit(void)
690 mmc_unregister_driver(&mmc_driver
);
691 unregister_blkdev(major
, "mmc");
694 module_init(mmc_blk_init
);
695 module_exit(mmc_blk_exit
);
697 MODULE_LICENSE("GPL");
698 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
700 module_param(major
, int, 0444);
701 MODULE_PARM_DESC(major
, "specify the major device number for MMC block driver");