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"
40 #if ( defined CONFIG_ARCH_W341 ) || (defined CONFIG_ARCH_W345 ) || ( defined CONFIG_ARCH_W345_IMP1 )
41 #include <asm/arch/gpio.h>
44 * max 8 partitions per card
48 #if 0 // mask by Victor Yu. 11-28-2005
54 #if ( defined CONFIG_ARCH_W341 ) || (defined CONFIG_ARCH_W345 ) || (defined CONFIG_ARCH_W345_IMP1 )
55 #define SD_LED ( 1 << 19 )
59 * There is one mmc_blk_data per slot.
64 struct mmc_queue queue
;
67 unsigned int block_bits
;
70 static DECLARE_MUTEX(open_lock
);
72 static struct mmc_blk_data
*mmc_blk_get(struct gendisk
*disk
)
74 struct mmc_blk_data
*md
;
77 md
= disk
->private_data
;
78 if (md
&& md
->usage
== 0)
87 static void mmc_blk_put(struct mmc_blk_data
*md
)
93 mmc_cleanup_queue(&md
->queue
);
99 static int mmc_blk_open(struct inode
*inode
, struct file
*filp
)
101 struct mmc_blk_data
*md
;
104 md
= mmc_blk_get(inode
->i_bdev
->bd_disk
);
107 check_disk_change(inode
->i_bdev
);
110 if ((filp
->f_mode
& FMODE_WRITE
) &&
111 mmc_card_readonly(md
->queue
.card
))
118 static int mmc_blk_release(struct inode
*inode
, struct file
*filp
)
120 struct mmc_blk_data
*md
= inode
->i_bdev
->bd_disk
->private_data
;
127 mmc_blk_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int cmd
, unsigned long arg
)
129 struct block_device
*bdev
= inode
->i_bdev
;
131 if (cmd
== HDIO_GETGEO
) {
132 struct hd_geometry geo
;
134 memset(&geo
, 0, sizeof(struct hd_geometry
));
136 geo
.cylinders
= get_capacity(bdev
->bd_disk
) / (4 * 16);
139 geo
.start
= get_start_sect(bdev
);
141 return copy_to_user((void __user
*)arg
, &geo
, sizeof(geo
))
148 static struct block_device_operations mmc_bdops
= {
149 .open
= mmc_blk_open
,
150 .release
= mmc_blk_release
,
151 .ioctl
= mmc_blk_ioctl
,
152 .owner
= THIS_MODULE
,
155 struct mmc_blk_request
{
156 struct mmc_request mrq
;
157 struct mmc_command cmd
;
158 struct mmc_command stop
;
159 struct mmc_data data
;
162 static int mmc_blk_prep_rq(struct mmc_queue
*mq
, struct request
*req
)
164 struct mmc_blk_data
*md
= mq
->data
;
165 int stat
= BLKPREP_OK
;
168 * If we have no device, we haven't finished initialising.
170 if (!md
|| !mq
->card
) {
171 printk(KERN_ERR
"%s: killing request - no device/host\n",
172 req
->rq_disk
->disk_name
);
179 static int mmc_blk_issue_rq(struct mmc_queue
*mq
, struct request
*req
)
181 struct mmc_blk_data
*md
= mq
->data
;
182 struct mmc_card
*card
= md
->queue
.card
;
185 if (mmc_card_claim_host(card
))
189 struct mmc_blk_request brq
;
190 struct mmc_command cmd
;
192 memset(&brq
, 0, sizeof(struct mmc_blk_request
));
193 brq
.mrq
.cmd
= &brq
.cmd
;
194 brq
.mrq
.data
= &brq
.data
;
196 brq
.cmd
.arg
= req
->sector
<< 9;
197 brq
.cmd
.flags
= MMC_RSP_R1
;
198 brq
.data
.timeout_ns
= card
->csd
.tacc_ns
* 10;
199 brq
.data
.timeout_clks
= card
->csd
.tacc_clks
* 10;
200 brq
.data
.blksz_bits
= md
->block_bits
;
201 brq
.data
.blocks
= req
->nr_sectors
>> (md
->block_bits
- 9);
202 brq
.stop
.opcode
= MMC_STOP_TRANSMISSION
;
204 brq
.stop
.flags
= MMC_RSP_R1B
;
206 if (rq_data_dir(req
) == READ
) {
207 brq
.cmd
.opcode
= brq
.data
.blocks
> 1 ? MMC_READ_MULTIPLE_BLOCK
: MMC_READ_SINGLE_BLOCK
;
208 brq
.data
.flags
|= MMC_DATA_READ
;
210 brq
.cmd
.opcode
= MMC_WRITE_BLOCK
;
211 brq
.cmd
.flags
= MMC_RSP_R1B
;
212 brq
.data
.flags
|= MMC_DATA_WRITE
;
215 brq
.mrq
.stop
= brq
.data
.blocks
> 1 ? &brq
.stop
: NULL
;
217 brq
.data
.sg
= mq
->sg
;
218 brq
.data
.sg_len
= blk_rq_map_sg(req
->q
, req
, brq
.data
.sg
);
220 mmc_wait_for_req(card
->host
, &brq
.mrq
);
222 printk(KERN_ERR
"%s: error %d sending read/write command\n",
223 req
->rq_disk
->disk_name
, brq
.cmd
.error
);
227 if (brq
.data
.error
) {
228 printk(KERN_ERR
"%s: error %d transferring data\n",
229 req
->rq_disk
->disk_name
, brq
.data
.error
);
233 if (brq
.stop
.error
) {
234 printk(KERN_ERR
"%s: error %d sending stop command\n",
235 req
->rq_disk
->disk_name
, brq
.stop
.error
);
242 cmd
.opcode
= MMC_SEND_STATUS
;
243 cmd
.arg
= card
->rca
<< 16;
244 cmd
.flags
= MMC_RSP_R1
;
245 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 5);
247 printk(KERN_ERR
"%s: error %d requesting status\n",
248 req
->rq_disk
->disk_name
, err
);
251 } while (!(cmd
.resp
[0] & R1_READY_FOR_DATA
));
254 if (cmd
.resp
[0] & ~0x00000900)
255 printk(KERN_ERR
"%s: status = %08x\n",
256 req
->rq_disk
->disk_name
, cmd
.resp
[0]);
257 if (mmc_decode_status(cmd
.resp
))
262 * A block was successfully transferred.
264 spin_lock_irq(&md
->lock
);
265 ret
= end_that_request_chunk(req
, 1, brq
.data
.bytes_xfered
);
268 * The whole request completed successfully.
270 add_disk_randomness(req
->rq_disk
);
271 blkdev_dequeue_request(req
);
272 end_that_request_last(req
);
274 spin_unlock_irq(&md
->lock
);
277 mmc_card_release_host(card
);
282 mmc_card_release_host(card
);
285 * This is a little draconian, but until we get proper
286 * error handling sorted out here, its the best we can
287 * do - especially as some hosts have no idea how much
288 * data was transferred before the error occurred.
290 spin_lock_irq(&md
->lock
);
292 ret
= end_that_request_chunk(req
, 0,
293 req
->current_nr_sectors
<< 9);
296 add_disk_randomness(req
->rq_disk
);
297 blkdev_dequeue_request(req
);
298 end_that_request_last(req
);
299 spin_unlock_irq(&md
->lock
);
304 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
306 static unsigned long dev_use
[MMC_NUM_MINORS
/(8*sizeof(unsigned long))];
308 static struct mmc_blk_data
*mmc_blk_alloc(struct mmc_card
*card
)
310 struct mmc_blk_data
*md
;
313 devidx
= find_first_zero_bit(dev_use
, MMC_NUM_MINORS
);
314 if (devidx
>= MMC_NUM_MINORS
)
315 return ERR_PTR(-ENOSPC
);
316 __set_bit(devidx
, dev_use
);
318 md
= kmalloc(sizeof(struct mmc_blk_data
), GFP_KERNEL
);
320 memset(md
, 0, sizeof(struct mmc_blk_data
));
322 md
->disk
= alloc_disk(1 << MMC_SHIFT
);
323 if (md
->disk
== NULL
) {
325 md
= ERR_PTR(-ENOMEM
);
329 spin_lock_init(&md
->lock
);
332 ret
= mmc_init_queue(&md
->queue
, card
, &md
->lock
);
339 md
->queue
.prep_fn
= mmc_blk_prep_rq
;
340 md
->queue
.issue_fn
= mmc_blk_issue_rq
;
343 md
->disk
->major
= major
;
344 md
->disk
->first_minor
= devidx
<< MMC_SHIFT
;
345 md
->disk
->fops
= &mmc_bdops
;
346 md
->disk
->private_data
= md
;
347 md
->disk
->queue
= md
->queue
.queue
;
348 md
->disk
->driverfs_dev
= &card
->dev
;
351 * As discussed on lkml, GENHD_FL_REMOVABLE should:
353 * - be set for removable media with permanent block devices
354 * - be unset for removable block devices with permanent media
356 * Since MMC block devices clearly fall under the second
357 * case, we do not set GENHD_FL_REMOVABLE. Userspace
358 * should use the block device creation/destruction hotplug
359 * messages to tell when the card is present.
362 sprintf(md
->disk
->disk_name
, "mmcblk%d", devidx
);
363 sprintf(md
->disk
->devfs_name
, "mmc/blk%d", devidx
);
365 md
->block_bits
= card
->csd
.read_blkbits
;
367 blk_queue_hardsect_size(md
->queue
.queue
, 1 << md
->block_bits
);
368 set_capacity(md
->disk
, card
->csd
.capacity
);
375 mmc_blk_set_blksize(struct mmc_blk_data
*md
, struct mmc_card
*card
)
377 struct mmc_command cmd
;
380 mmc_card_claim_host(card
);
381 cmd
.opcode
= MMC_SET_BLOCKLEN
;
382 cmd
.arg
= 1 << card
->csd
.read_blkbits
;
383 cmd
.flags
= MMC_RSP_R1
;
384 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 5);
385 mmc_card_release_host(card
);
388 printk(KERN_ERR
"%s: unable to set block size to %d: %d\n",
389 md
->disk
->disk_name
, cmd
.arg
, err
);
396 #if 1 // add by Victor Yu. 12-15-2005
397 #include <linux/kmod.h>
398 static void mountsd(char *action
)
400 char *argv
[2], **envp
, *buf
, *scratch
;
403 if ( in_interrupt() )
405 if ( !current
->fs
->root
)
407 if ( !(envp
=(char **)kmalloc(10*sizeof(char *), GFP_KERNEL
)) )
409 if ( !(buf
=kmalloc(256, GFP_KERNEL
)) )
411 argv
[0] = "/bin/sdpnp";
413 envp
[i
++] = "HOME=/";
414 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
417 scratch
+= sprintf(scratch
, "ACTION=%s", action
) + 1;
419 call_usermodehelper(argv
[0], argv
, envp
, 0);
425 static int mmc_blk_probe(struct mmc_card
*card
)
427 struct mmc_blk_data
*md
;
431 * Check that the card supports the command class(es) we need.
433 if (!(card
->csd
.cmdclass
& CCC_BLOCK_READ
))
436 if (card
->csd
.read_blkbits
< 9) {
437 printk(KERN_WARNING
"%s: read blocksize too small (%u)\n",
438 mmc_card_id(card
), 1 << card
->csd
.read_blkbits
);
442 md
= mmc_blk_alloc(card
);
446 err
= mmc_blk_set_blksize(md
, card
);
450 printk(KERN_INFO
"%s: %s %s %dKiB %s\n",
451 md
->disk
->disk_name
, mmc_card_id(card
), mmc_card_name(card
),
452 (card
->csd
.capacity
<< card
->csd
.read_blkbits
) / 1024,
453 mmc_card_readonly(card
)?"(ro)":"");
455 mmc_set_drvdata(card
, md
);
457 #if 1 // add by Victor Yu. 12-15-2005
459 #if ( defined CONFIG_ARCH_W341 ) || ( defined CONFIG_ARCH_W345 ) || (defined CONFIG_ARCH_W345_IMP1 )
460 mcpu_gpio_inout( SD_LED
, MCPU_GPIO_OUTPUT
);
461 mcpu_gpio_set( SD_LED
, MCPU_GPIO_LOW
);
473 static void mmc_blk_remove(struct mmc_card
*card
)
475 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
480 #if 1 // add by Victor Yu. 12-15-2005
482 #if ( defined CONFIG_ARCH_W341 ) || ( defined CONFIG_ARCH_W345 ) || (defined CONFIG_ARCH_W345_IMP1 )
483 mcpu_gpio_inout( SD_LED
, MCPU_GPIO_OUTPUT
);
484 mcpu_gpio_set( SD_LED
, MCPU_GPIO_HIGH
);
488 del_gendisk(md
->disk
);
491 * I think this is needed.
493 md
->disk
->queue
= NULL
;
495 devidx
= md
->disk
->first_minor
>> MMC_SHIFT
;
496 __clear_bit(devidx
, dev_use
);
500 mmc_set_drvdata(card
, NULL
);
504 static int mmc_blk_suspend(struct mmc_card
*card
, pm_message_t state
)
506 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
509 mmc_queue_suspend(&md
->queue
);
514 static int mmc_blk_resume(struct mmc_card
*card
)
516 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
519 mmc_blk_set_blksize(md
, card
);
520 mmc_queue_resume(&md
->queue
);
525 #define mmc_blk_suspend NULL
526 #define mmc_blk_resume NULL
529 static struct mmc_driver mmc_driver
= {
533 .probe
= mmc_blk_probe
,
534 .remove
= mmc_blk_remove
,
535 .suspend
= mmc_blk_suspend
,
536 .resume
= mmc_blk_resume
,
539 static int __init
mmc_blk_init(void)
543 res
= register_blkdev(major
, "mmc");
545 printk(KERN_WARNING
"Unable to get major %d for MMC media: %d\n",
554 #if ( defined CONFIG_ARCH_W341 ) || ( defined CONFIG_ARCH_W345 ) || (defined CONFIG_ARCH_W345_IMP1 )
555 mcpu_gpio_inout( SD_LED
, MCPU_GPIO_OUTPUT
);
556 mcpu_gpio_set( SD_LED
, MCPU_GPIO_HIGH
);
559 return mmc_register_driver(&mmc_driver
);
565 static void __exit
mmc_blk_exit(void)
567 mmc_unregister_driver(&mmc_driver
);
569 unregister_blkdev(major
, "mmc");
572 module_init(mmc_blk_init
);
573 module_exit(mmc_blk_exit
);
575 MODULE_LICENSE("GPL");
576 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
578 module_param(major
, int, 0444);
579 MODULE_PARM_DESC(major
, "specify the major device number for MMC block driver");