Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mmc/core/host.c |
| 3 | * |
| 4 | * Copyright (C) 2003 Russell King, All Rights Reserved. |
Pierre Ossman | ff3112f | 2008-03-08 23:43:19 +0100 | [diff] [blame] | 5 | * Copyright (C) 2007-2008 Pierre Ossman |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * MMC host class device management |
| 12 | */ |
| 13 | |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/idr.h> |
| 17 | #include <linux/pagemap.h> |
Pierre Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 18 | #include <linux/leds.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 20 | |
| 21 | #include <linux/mmc/host.h> |
| 22 | |
| 23 | #include "core.h" |
| 24 | #include "host.h" |
| 25 | |
| 26 | #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev) |
| 27 | |
| 28 | static void mmc_host_classdev_release(struct device *dev) |
| 29 | { |
| 30 | struct mmc_host *host = cls_dev_to_mmc_host(dev); |
| 31 | kfree(host); |
| 32 | } |
| 33 | |
| 34 | static struct class mmc_host_class = { |
| 35 | .name = "mmc_host", |
| 36 | .dev_release = mmc_host_classdev_release, |
| 37 | }; |
| 38 | |
| 39 | int mmc_register_host_class(void) |
| 40 | { |
| 41 | return class_register(&mmc_host_class); |
| 42 | } |
| 43 | |
| 44 | void mmc_unregister_host_class(void) |
| 45 | { |
| 46 | class_unregister(&mmc_host_class); |
| 47 | } |
| 48 | |
| 49 | static DEFINE_IDR(mmc_host_idr); |
| 50 | static DEFINE_SPINLOCK(mmc_host_lock); |
| 51 | |
| 52 | /** |
| 53 | * mmc_alloc_host - initialise the per-host structure. |
| 54 | * @extra: sizeof private data structure |
| 55 | * @dev: pointer to host device model structure |
| 56 | * |
| 57 | * Initialise the per-host structure. |
| 58 | */ |
| 59 | struct mmc_host *mmc_alloc_host(int extra, struct device *dev) |
| 60 | { |
Pierre Ossman | ff3112f | 2008-03-08 23:43:19 +0100 | [diff] [blame] | 61 | int err; |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 62 | struct mmc_host *host; |
| 63 | |
Pierre Ossman | ff3112f | 2008-03-08 23:43:19 +0100 | [diff] [blame] | 64 | if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL)) |
| 65 | return NULL; |
| 66 | |
Mariusz Kozlowski | be760a9 | 2007-08-10 14:00:50 -0700 | [diff] [blame] | 67 | host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL); |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 68 | if (!host) |
| 69 | return NULL; |
| 70 | |
Pierre Ossman | ff3112f | 2008-03-08 23:43:19 +0100 | [diff] [blame] | 71 | spin_lock(&mmc_host_lock); |
| 72 | err = idr_get_new(&mmc_host_idr, host, &host->index); |
| 73 | spin_unlock(&mmc_host_lock); |
| 74 | if (err) |
| 75 | goto free; |
| 76 | |
Kay Sievers | d1b2686 | 2008-11-08 21:37:46 +0100 | [diff] [blame] | 77 | dev_set_name(&host->class_dev, "mmc%d", host->index); |
Pierre Ossman | ff3112f | 2008-03-08 23:43:19 +0100 | [diff] [blame] | 78 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 79 | host->parent = dev; |
| 80 | host->class_dev.parent = dev; |
| 81 | host->class_dev.class = &mmc_host_class; |
| 82 | device_initialize(&host->class_dev); |
| 83 | |
| 84 | spin_lock_init(&host->lock); |
| 85 | init_waitqueue_head(&host->wq); |
| 86 | INIT_DELAYED_WORK(&host->detect, mmc_rescan); |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 87 | INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable); |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * By default, hosts do not support SGIO or large requests. |
| 91 | * They have to set these according to their abilities. |
| 92 | */ |
| 93 | host->max_hw_segs = 1; |
| 94 | host->max_phys_segs = 1; |
| 95 | host->max_seg_size = PAGE_CACHE_SIZE; |
| 96 | |
| 97 | host->max_req_size = PAGE_CACHE_SIZE; |
| 98 | host->max_blk_size = 512; |
| 99 | host->max_blk_count = PAGE_CACHE_SIZE / 512; |
| 100 | |
| 101 | return host; |
Pierre Ossman | ff3112f | 2008-03-08 23:43:19 +0100 | [diff] [blame] | 102 | |
| 103 | free: |
| 104 | kfree(host); |
| 105 | return NULL; |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | EXPORT_SYMBOL(mmc_alloc_host); |
| 109 | |
| 110 | /** |
| 111 | * mmc_add_host - initialise host hardware |
| 112 | * @host: mmc host |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 113 | * |
| 114 | * Register the host with the driver model. The host must be |
| 115 | * prepared to start servicing requests before this function |
| 116 | * completes. |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 117 | */ |
| 118 | int mmc_add_host(struct mmc_host *host) |
| 119 | { |
| 120 | int err; |
| 121 | |
Nicolas Pitre | 17b759a | 2007-07-24 02:09:39 -0400 | [diff] [blame] | 122 | WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) && |
| 123 | !host->ops->enable_sdio_irq); |
| 124 | |
Kay Sievers | d1b2686 | 2008-11-08 21:37:46 +0100 | [diff] [blame] | 125 | led_trigger_register_simple(dev_name(&host->class_dev), &host->led); |
Pierre Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 126 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 127 | err = device_add(&host->class_dev); |
| 128 | if (err) |
| 129 | return err; |
| 130 | |
Haavard Skinnemoen | 6edd8ee | 2008-07-24 14:18:57 +0200 | [diff] [blame] | 131 | #ifdef CONFIG_DEBUG_FS |
| 132 | mmc_add_host_debugfs(host); |
| 133 | #endif |
| 134 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 135 | mmc_start_host(host); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | EXPORT_SYMBOL(mmc_add_host); |
| 141 | |
| 142 | /** |
| 143 | * mmc_remove_host - remove host hardware |
| 144 | * @host: mmc host |
| 145 | * |
| 146 | * Unregister and remove all cards associated with this host, |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 147 | * and power down the MMC bus. No new requests will be issued |
| 148 | * after this function has returned. |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 149 | */ |
| 150 | void mmc_remove_host(struct mmc_host *host) |
| 151 | { |
| 152 | mmc_stop_host(host); |
| 153 | |
Haavard Skinnemoen | 6edd8ee | 2008-07-24 14:18:57 +0200 | [diff] [blame] | 154 | #ifdef CONFIG_DEBUG_FS |
| 155 | mmc_remove_host_debugfs(host); |
| 156 | #endif |
| 157 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 158 | device_del(&host->class_dev); |
| 159 | |
Pierre Ossman | 77f1fd6 | 2007-10-12 22:48:46 +0200 | [diff] [blame] | 160 | led_trigger_unregister_simple(host->led); |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | EXPORT_SYMBOL(mmc_remove_host); |
| 164 | |
| 165 | /** |
| 166 | * mmc_free_host - free the host structure |
| 167 | * @host: mmc host |
| 168 | * |
| 169 | * Free the host once all references to it have been dropped. |
| 170 | */ |
| 171 | void mmc_free_host(struct mmc_host *host) |
| 172 | { |
Pierre Ossman | ff3112f | 2008-03-08 23:43:19 +0100 | [diff] [blame] | 173 | spin_lock(&mmc_host_lock); |
| 174 | idr_remove(&mmc_host_idr, host->index); |
| 175 | spin_unlock(&mmc_host_lock); |
| 176 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 177 | put_device(&host->class_dev); |
| 178 | } |
| 179 | |
| 180 | EXPORT_SYMBOL(mmc_free_host); |
| 181 | |