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