blob: 9b629856c7358bc8061d8413b13e4ab331071426 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
5 *
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.
9 *
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.
13 *
14 * Many thanks to Alessandro Rubini and Jonathan Corbet!
15 *
16 * Author: Andrew Christian
17 * 28 May 2002
18 */
19#include <linux/moduleparam.h>
20#include <linux/module.h>
21#include <linux/init.h>
22
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>
31
32#include <linux/mmc/card.h>
33#include <linux/mmc/protocol.h>
34
35#include <asm/system.h>
36#include <asm/uaccess.h>
37
38#include "mmc_queue.h"
39
40/*
41 * max 8 partitions per card
42 */
43#define MMC_SHIFT 3
44
45static int major;
46
47/*
48 * There is one mmc_blk_data per slot.
49 */
50struct mmc_blk_data {
51 spinlock_t lock;
52 struct gendisk *disk;
53 struct mmc_queue queue;
54
55 unsigned int usage;
56 unsigned int block_bits;
57};
58
59static DECLARE_MUTEX(open_lock);
60
61static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
62{
63 struct mmc_blk_data *md;
64
65 down(&open_lock);
66 md = disk->private_data;
67 if (md && md->usage == 0)
68 md = NULL;
69 if (md)
70 md->usage++;
71 up(&open_lock);
72
73 return md;
74}
75
76static void mmc_blk_put(struct mmc_blk_data *md)
77{
78 down(&open_lock);
79 md->usage--;
80 if (md->usage == 0) {
81 put_disk(md->disk);
82 mmc_cleanup_queue(&md->queue);
83 kfree(md);
84 }
85 up(&open_lock);
86}
87
Pierre Ossman936d8592005-10-30 10:15:58 +000088static inline int mmc_blk_readonly(struct mmc_card *card)
89{
90 return mmc_card_readonly(card) ||
91 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
92}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int mmc_blk_open(struct inode *inode, struct file *filp)
95{
96 struct mmc_blk_data *md;
97 int ret = -ENXIO;
98
99 md = mmc_blk_get(inode->i_bdev->bd_disk);
100 if (md) {
101 if (md->usage == 2)
102 check_disk_change(inode->i_bdev);
103 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700104
105 if ((filp->f_mode & FMODE_WRITE) &&
Pierre Ossman936d8592005-10-30 10:15:58 +0000106 mmc_blk_readonly(md->queue.card))
Pierre Ossmana00fc092005-09-06 15:18:52 -0700107 ret = -EROFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109
110 return ret;
111}
112
113static int mmc_blk_release(struct inode *inode, struct file *filp)
114{
115 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
116
117 mmc_blk_put(md);
118 return 0;
119}
120
121static int
122mmc_blk_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
123{
124 struct block_device *bdev = inode->i_bdev;
125
126 if (cmd == HDIO_GETGEO) {
127 struct hd_geometry geo;
128
129 memset(&geo, 0, sizeof(struct hd_geometry));
130
131 geo.cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
132 geo.heads = 4;
133 geo.sectors = 16;
134 geo.start = get_start_sect(bdev);
135
136 return copy_to_user((void __user *)arg, &geo, sizeof(geo))
137 ? -EFAULT : 0;
138 }
139
140 return -ENOTTY;
141}
142
143static struct block_device_operations mmc_bdops = {
144 .open = mmc_blk_open,
145 .release = mmc_blk_release,
146 .ioctl = mmc_blk_ioctl,
147 .owner = THIS_MODULE,
148};
149
150struct mmc_blk_request {
151 struct mmc_request mrq;
152 struct mmc_command cmd;
153 struct mmc_command stop;
154 struct mmc_data data;
155};
156
157static int mmc_blk_prep_rq(struct mmc_queue *mq, struct request *req)
158{
159 struct mmc_blk_data *md = mq->data;
160 int stat = BLKPREP_OK;
161
162 /*
163 * If we have no device, we haven't finished initialising.
164 */
165 if (!md || !mq->card) {
166 printk(KERN_ERR "%s: killing request - no device/host\n",
167 req->rq_disk->disk_name);
168 stat = BLKPREP_KILL;
169 }
170
171 return stat;
172}
173
174static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
175{
176 struct mmc_blk_data *md = mq->data;
177 struct mmc_card *card = md->queue.card;
178 int ret;
179
180 if (mmc_card_claim_host(card))
181 goto cmd_err;
182
183 do {
184 struct mmc_blk_request brq;
185 struct mmc_command cmd;
186
187 memset(&brq, 0, sizeof(struct mmc_blk_request));
188 brq.mrq.cmd = &brq.cmd;
189 brq.mrq.data = &brq.data;
190
191 brq.cmd.arg = req->sector << 9;
192 brq.cmd.flags = MMC_RSP_R1;
193 brq.data.timeout_ns = card->csd.tacc_ns * 10;
194 brq.data.timeout_clks = card->csd.tacc_clks * 10;
195 brq.data.blksz_bits = md->block_bits;
196 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
197 brq.stop.opcode = MMC_STOP_TRANSMISSION;
198 brq.stop.arg = 0;
199 brq.stop.flags = MMC_RSP_R1B;
200
201 if (rq_data_dir(req) == READ) {
202 brq.cmd.opcode = brq.data.blocks > 1 ? MMC_READ_MULTIPLE_BLOCK : MMC_READ_SINGLE_BLOCK;
203 brq.data.flags |= MMC_DATA_READ;
204 } else {
205 brq.cmd.opcode = MMC_WRITE_BLOCK;
206 brq.cmd.flags = MMC_RSP_R1B;
207 brq.data.flags |= MMC_DATA_WRITE;
208 brq.data.blocks = 1;
209 }
210 brq.mrq.stop = brq.data.blocks > 1 ? &brq.stop : NULL;
211
212 brq.data.sg = mq->sg;
213 brq.data.sg_len = blk_rq_map_sg(req->q, req, brq.data.sg);
214
215 mmc_wait_for_req(card->host, &brq.mrq);
216 if (brq.cmd.error) {
217 printk(KERN_ERR "%s: error %d sending read/write command\n",
218 req->rq_disk->disk_name, brq.cmd.error);
219 goto cmd_err;
220 }
221
222 if (brq.data.error) {
223 printk(KERN_ERR "%s: error %d transferring data\n",
224 req->rq_disk->disk_name, brq.data.error);
225 goto cmd_err;
226 }
227
228 if (brq.stop.error) {
229 printk(KERN_ERR "%s: error %d sending stop command\n",
230 req->rq_disk->disk_name, brq.stop.error);
231 goto cmd_err;
232 }
233
234 do {
235 int err;
236
237 cmd.opcode = MMC_SEND_STATUS;
238 cmd.arg = card->rca << 16;
239 cmd.flags = MMC_RSP_R1;
240 err = mmc_wait_for_cmd(card->host, &cmd, 5);
241 if (err) {
242 printk(KERN_ERR "%s: error %d requesting status\n",
243 req->rq_disk->disk_name, err);
244 goto cmd_err;
245 }
246 } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
247
248#if 0
249 if (cmd.resp[0] & ~0x00000900)
250 printk(KERN_ERR "%s: status = %08x\n",
251 req->rq_disk->disk_name, cmd.resp[0]);
252 if (mmc_decode_status(cmd.resp))
253 goto cmd_err;
254#endif
255
256 /*
257 * A block was successfully transferred.
258 */
259 spin_lock_irq(&md->lock);
260 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
261 if (!ret) {
262 /*
263 * The whole request completed successfully.
264 */
265 add_disk_randomness(req->rq_disk);
266 blkdev_dequeue_request(req);
267 end_that_request_last(req);
268 }
269 spin_unlock_irq(&md->lock);
270 } while (ret);
271
272 mmc_card_release_host(card);
273
274 return 1;
275
276 cmd_err:
277 mmc_card_release_host(card);
278
279 /*
280 * This is a little draconian, but until we get proper
281 * error handling sorted out here, its the best we can
282 * do - especially as some hosts have no idea how much
283 * data was transferred before the error occurred.
284 */
285 spin_lock_irq(&md->lock);
286 do {
287 ret = end_that_request_chunk(req, 0,
288 req->current_nr_sectors << 9);
289 } while (ret);
290
291 add_disk_randomness(req->rq_disk);
292 blkdev_dequeue_request(req);
293 end_that_request_last(req);
294 spin_unlock_irq(&md->lock);
295
296 return 0;
297}
298
299#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
300
301static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
302
303static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
304{
305 struct mmc_blk_data *md;
306 int devidx, ret;
307
308 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
309 if (devidx >= MMC_NUM_MINORS)
310 return ERR_PTR(-ENOSPC);
311 __set_bit(devidx, dev_use);
312
313 md = kmalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
314 if (md) {
315 memset(md, 0, sizeof(struct mmc_blk_data));
316
317 md->disk = alloc_disk(1 << MMC_SHIFT);
318 if (md->disk == NULL) {
319 kfree(md);
320 md = ERR_PTR(-ENOMEM);
321 goto out;
322 }
323
324 spin_lock_init(&md->lock);
325 md->usage = 1;
326
327 ret = mmc_init_queue(&md->queue, card, &md->lock);
328 if (ret) {
329 put_disk(md->disk);
330 kfree(md);
331 md = ERR_PTR(ret);
332 goto out;
333 }
334 md->queue.prep_fn = mmc_blk_prep_rq;
335 md->queue.issue_fn = mmc_blk_issue_rq;
336 md->queue.data = md;
337
338 md->disk->major = major;
339 md->disk->first_minor = devidx << MMC_SHIFT;
340 md->disk->fops = &mmc_bdops;
341 md->disk->private_data = md;
342 md->disk->queue = md->queue.queue;
343 md->disk->driverfs_dev = &card->dev;
344
345 /*
346 * As discussed on lkml, GENHD_FL_REMOVABLE should:
347 *
348 * - be set for removable media with permanent block devices
349 * - be unset for removable block devices with permanent media
350 *
351 * Since MMC block devices clearly fall under the second
352 * case, we do not set GENHD_FL_REMOVABLE. Userspace
353 * should use the block device creation/destruction hotplug
354 * messages to tell when the card is present.
355 */
356
357 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
358 sprintf(md->disk->devfs_name, "mmc/blk%d", devidx);
359
360 md->block_bits = card->csd.read_blkbits;
361
362 blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
363 set_capacity(md->disk, card->csd.capacity);
364 }
365 out:
366 return md;
367}
368
369static int
370mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
371{
372 struct mmc_command cmd;
373 int err;
374
375 mmc_card_claim_host(card);
376 cmd.opcode = MMC_SET_BLOCKLEN;
377 cmd.arg = 1 << card->csd.read_blkbits;
378 cmd.flags = MMC_RSP_R1;
379 err = mmc_wait_for_cmd(card->host, &cmd, 5);
380 mmc_card_release_host(card);
381
382 if (err) {
383 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
384 md->disk->disk_name, cmd.arg, err);
385 return -EINVAL;
386 }
387
388 return 0;
389}
390
391static int mmc_blk_probe(struct mmc_card *card)
392{
393 struct mmc_blk_data *md;
394 int err;
395
Pierre Ossman912490d2005-05-21 10:27:02 +0100396 /*
397 * Check that the card supports the command class(es) we need.
398 */
399 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return -ENODEV;
401
402 if (card->csd.read_blkbits < 9) {
403 printk(KERN_WARNING "%s: read blocksize too small (%u)\n",
404 mmc_card_id(card), 1 << card->csd.read_blkbits);
405 return -ENODEV;
406 }
407
408 md = mmc_blk_alloc(card);
409 if (IS_ERR(md))
410 return PTR_ERR(md);
411
412 err = mmc_blk_set_blksize(md, card);
413 if (err)
414 goto out;
415
Pierre Ossmana00fc092005-09-06 15:18:52 -0700416 printk(KERN_INFO "%s: %s %s %dKiB %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana00fc092005-09-06 15:18:52 -0700418 (card->csd.capacity << card->csd.read_blkbits) / 1024,
Pierre Ossman936d8592005-10-30 10:15:58 +0000419 mmc_blk_readonly(card)?"(ro)":"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 mmc_set_drvdata(card, md);
422 add_disk(md->disk);
423 return 0;
424
425 out:
426 mmc_blk_put(md);
427
428 return err;
429}
430
431static void mmc_blk_remove(struct mmc_card *card)
432{
433 struct mmc_blk_data *md = mmc_get_drvdata(card);
434
435 if (md) {
436 int devidx;
437
438 del_gendisk(md->disk);
439
440 /*
441 * I think this is needed.
442 */
443 md->disk->queue = NULL;
444
445 devidx = md->disk->first_minor >> MMC_SHIFT;
446 __clear_bit(devidx, dev_use);
447
448 mmc_blk_put(md);
449 }
450 mmc_set_drvdata(card, NULL);
451}
452
453#ifdef CONFIG_PM
454static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
455{
456 struct mmc_blk_data *md = mmc_get_drvdata(card);
457
458 if (md) {
459 mmc_queue_suspend(&md->queue);
460 }
461 return 0;
462}
463
464static int mmc_blk_resume(struct mmc_card *card)
465{
466 struct mmc_blk_data *md = mmc_get_drvdata(card);
467
468 if (md) {
469 mmc_blk_set_blksize(md, card);
470 mmc_queue_resume(&md->queue);
471 }
472 return 0;
473}
474#else
475#define mmc_blk_suspend NULL
476#define mmc_blk_resume NULL
477#endif
478
479static struct mmc_driver mmc_driver = {
480 .drv = {
481 .name = "mmcblk",
482 },
483 .probe = mmc_blk_probe,
484 .remove = mmc_blk_remove,
485 .suspend = mmc_blk_suspend,
486 .resume = mmc_blk_resume,
487};
488
489static int __init mmc_blk_init(void)
490{
491 int res = -ENOMEM;
492
493 res = register_blkdev(major, "mmc");
494 if (res < 0) {
495 printk(KERN_WARNING "Unable to get major %d for MMC media: %d\n",
496 major, res);
497 goto out;
498 }
499 if (major == 0)
500 major = res;
501
502 devfs_mk_dir("mmc");
503 return mmc_register_driver(&mmc_driver);
504
505 out:
506 return res;
507}
508
509static void __exit mmc_blk_exit(void)
510{
511 mmc_unregister_driver(&mmc_driver);
512 devfs_remove("mmc");
513 unregister_blkdev(major, "mmc");
514}
515
516module_init(mmc_blk_init);
517module_exit(mmc_blk_exit);
518
519MODULE_LICENSE("GPL");
520MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
521
522module_param(major, int, 0444);
523MODULE_PARM_DESC(major, "specify the major device number for MMC block driver");