MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / mmc.old / mmc_block.c
blobc60c2da4bbc5e346aeadfd7f135d0e93a3ca490f
1 /*
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
17 * 28 May 2002
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>
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>
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>
42 #endif
44 * max 8 partitions per card
46 #define MMC_SHIFT 3
48 #if 0 // mask by Victor Yu. 11-28-2005
49 static int major;
50 #else
51 static int major=121;
52 #endif
54 #if ( defined CONFIG_ARCH_W341 ) || (defined CONFIG_ARCH_W345 ) || (defined CONFIG_ARCH_W345_IMP1 )
55 #define SD_LED ( 1 << 19 )
56 #endif
59 * There is one mmc_blk_data per slot.
61 struct mmc_blk_data {
62 spinlock_t lock;
63 struct gendisk *disk;
64 struct mmc_queue queue;
66 unsigned int usage;
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;
76 down(&open_lock);
77 md = disk->private_data;
78 if (md && md->usage == 0)
79 md = NULL;
80 if (md)
81 md->usage++;
82 up(&open_lock);
84 return md;
87 static void mmc_blk_put(struct mmc_blk_data *md)
89 down(&open_lock);
90 md->usage--;
91 if (md->usage == 0) {
92 put_disk(md->disk);
93 mmc_cleanup_queue(&md->queue);
94 kfree(md);
96 up(&open_lock);
99 static int mmc_blk_open(struct inode *inode, struct file *filp)
101 struct mmc_blk_data *md;
102 int ret = -ENXIO;
104 md = mmc_blk_get(inode->i_bdev->bd_disk);
105 if (md) {
106 if (md->usage == 2)
107 check_disk_change(inode->i_bdev);
108 ret = 0;
110 if ((filp->f_mode & FMODE_WRITE) &&
111 mmc_card_readonly(md->queue.card))
112 ret = -EROFS;
115 return ret;
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;
122 mmc_blk_put(md);
123 return 0;
126 static int
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);
137 geo.heads = 4;
138 geo.sectors = 16;
139 geo.start = get_start_sect(bdev);
141 return copy_to_user((void __user *)arg, &geo, sizeof(geo))
142 ? -EFAULT : 0;
145 return -ENOTTY;
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);
173 stat = BLKPREP_KILL;
176 return stat;
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;
183 int ret;
185 if (mmc_card_claim_host(card))
186 goto cmd_err;
188 do {
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;
203 brq.stop.arg = 0;
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;
209 } else {
210 brq.cmd.opcode = MMC_WRITE_BLOCK;
211 brq.cmd.flags = MMC_RSP_R1B;
212 brq.data.flags |= MMC_DATA_WRITE;
213 brq.data.blocks = 1;
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);
221 if (brq.cmd.error) {
222 printk(KERN_ERR "%s: error %d sending read/write command\n",
223 req->rq_disk->disk_name, brq.cmd.error);
224 goto cmd_err;
227 if (brq.data.error) {
228 printk(KERN_ERR "%s: error %d transferring data\n",
229 req->rq_disk->disk_name, brq.data.error);
230 goto cmd_err;
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);
236 goto cmd_err;
239 do {
240 int err;
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);
246 if (err) {
247 printk(KERN_ERR "%s: error %d requesting status\n",
248 req->rq_disk->disk_name, err);
249 goto cmd_err;
251 } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
253 #if 0
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))
258 goto cmd_err;
259 #endif
262 * A block was successfully transferred.
264 spin_lock_irq(&md->lock);
265 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
266 if (!ret) {
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);
275 } while (ret);
277 mmc_card_release_host(card);
279 return 1;
281 cmd_err:
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);
291 do {
292 ret = end_that_request_chunk(req, 0,
293 req->current_nr_sectors << 9);
294 } while (ret);
296 add_disk_randomness(req->rq_disk);
297 blkdev_dequeue_request(req);
298 end_that_request_last(req);
299 spin_unlock_irq(&md->lock);
301 return 0;
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;
311 int devidx, ret;
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);
319 if (md) {
320 memset(md, 0, sizeof(struct mmc_blk_data));
322 md->disk = alloc_disk(1 << MMC_SHIFT);
323 if (md->disk == NULL) {
324 kfree(md);
325 md = ERR_PTR(-ENOMEM);
326 goto out;
329 spin_lock_init(&md->lock);
330 md->usage = 1;
332 ret = mmc_init_queue(&md->queue, card, &md->lock);
333 if (ret) {
334 put_disk(md->disk);
335 kfree(md);
336 md = ERR_PTR(ret);
337 goto out;
339 md->queue.prep_fn = mmc_blk_prep_rq;
340 md->queue.issue_fn = mmc_blk_issue_rq;
341 md->queue.data = md;
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);
370 out:
371 return md;
374 static int
375 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
377 struct mmc_command cmd;
378 int err;
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);
387 if (err) {
388 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
389 md->disk->disk_name, cmd.arg, err);
390 return -EINVAL;
393 return 0;
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;
401 int i=0;
403 if ( in_interrupt() )
404 return;
405 if ( !current->fs->root )
406 return;
407 if ( !(envp=(char **)kmalloc(10*sizeof(char *), GFP_KERNEL)) )
408 return;
409 if ( !(buf=kmalloc(256, GFP_KERNEL)) )
410 return;
411 argv[0] = "/bin/sdpnp";
412 argv[1] = 0;
413 envp[i++] = "HOME=/";
414 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
415 scratch = buf;
416 envp[i++] = scratch;
417 scratch += sprintf(scratch, "ACTION=%s", action) + 1;
418 envp[i++] = 0;
419 call_usermodehelper(argv[0], argv, envp, 0);
420 kfree(envp);
421 kfree(buf);
423 #endif // 12-15-2005
425 static int mmc_blk_probe(struct mmc_card *card)
427 struct mmc_blk_data *md;
428 int err;
431 * Check that the card supports the command class(es) we need.
433 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
434 return -ENODEV;
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);
439 return -ENODEV;
442 md = mmc_blk_alloc(card);
443 if (IS_ERR(md))
444 return PTR_ERR(md);
446 err = mmc_blk_set_blksize(md, card);
447 if (err)
448 goto out;
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);
456 add_disk(md->disk);
457 #if 1 // add by Victor Yu. 12-15-2005
458 mountsd("add");
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 );
462 #endif
464 #endif // 12-15-2005
465 return 0;
467 out:
468 mmc_blk_put(md);
470 return err;
473 static void mmc_blk_remove(struct mmc_card *card)
475 struct mmc_blk_data *md = mmc_get_drvdata(card);
477 if (md) {
478 int devidx;
480 #if 1 // add by Victor Yu. 12-15-2005
481 mountsd("remove");
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 );
485 #endif
487 #endif // 12-15-2005
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);
498 mmc_blk_put(md);
500 mmc_set_drvdata(card, NULL);
503 #ifdef CONFIG_PM
504 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
506 struct mmc_blk_data *md = mmc_get_drvdata(card);
508 if (md) {
509 mmc_queue_suspend(&md->queue);
511 return 0;
514 static int mmc_blk_resume(struct mmc_card *card)
516 struct mmc_blk_data *md = mmc_get_drvdata(card);
518 if (md) {
519 mmc_blk_set_blksize(md, card);
520 mmc_queue_resume(&md->queue);
522 return 0;
524 #else
525 #define mmc_blk_suspend NULL
526 #define mmc_blk_resume NULL
527 #endif
529 static struct mmc_driver mmc_driver = {
530 .drv = {
531 .name = "mmcblk",
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)
541 int res = -ENOMEM;
543 res = register_blkdev(major, "mmc");
544 if (res < 0) {
545 printk(KERN_WARNING "Unable to get major %d for MMC media: %d\n",
546 major, res);
547 goto out;
549 if (major == 0)
550 major = res;
552 devfs_mk_dir("mmc");
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 );
557 #endif
559 return mmc_register_driver(&mmc_driver);
561 out:
562 return res;
565 static void __exit mmc_blk_exit(void)
567 mmc_unregister_driver(&mmc_driver);
568 devfs_remove("mmc");
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");