Commit | Line | Data |
---|---|---|
b93931a6 PO |
1 | /* |
2 | * linux/drivers/mmc/core/host.c | |
3 | * | |
4 | * Copyright (C) 2003 Russell King, All Rights Reserved. | |
5 | * Copyright (C) 2007 Pierre Ossman | |
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> | |
18 | ||
19 | #include <linux/mmc/host.h> | |
20 | ||
21 | #include "core.h" | |
22 | #include "host.h" | |
23 | ||
24 | #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev) | |
25 | ||
26 | static void mmc_host_classdev_release(struct device *dev) | |
27 | { | |
28 | struct mmc_host *host = cls_dev_to_mmc_host(dev); | |
29 | kfree(host); | |
30 | } | |
31 | ||
32 | static struct class mmc_host_class = { | |
33 | .name = "mmc_host", | |
34 | .dev_release = mmc_host_classdev_release, | |
35 | }; | |
36 | ||
37 | int mmc_register_host_class(void) | |
38 | { | |
39 | return class_register(&mmc_host_class); | |
40 | } | |
41 | ||
42 | void mmc_unregister_host_class(void) | |
43 | { | |
44 | class_unregister(&mmc_host_class); | |
45 | } | |
46 | ||
47 | static DEFINE_IDR(mmc_host_idr); | |
48 | static DEFINE_SPINLOCK(mmc_host_lock); | |
49 | ||
50 | /** | |
51 | * mmc_alloc_host - initialise the per-host structure. | |
52 | * @extra: sizeof private data structure | |
53 | * @dev: pointer to host device model structure | |
54 | * | |
55 | * Initialise the per-host structure. | |
56 | */ | |
57 | struct mmc_host *mmc_alloc_host(int extra, struct device *dev) | |
58 | { | |
59 | struct mmc_host *host; | |
60 | ||
be760a9d | 61 | host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL); |
b93931a6 PO |
62 | if (!host) |
63 | return NULL; | |
64 | ||
b93931a6 PO |
65 | host->parent = dev; |
66 | host->class_dev.parent = dev; | |
67 | host->class_dev.class = &mmc_host_class; | |
68 | device_initialize(&host->class_dev); | |
69 | ||
70 | spin_lock_init(&host->lock); | |
71 | init_waitqueue_head(&host->wq); | |
72 | INIT_DELAYED_WORK(&host->detect, mmc_rescan); | |
73 | ||
74 | /* | |
75 | * By default, hosts do not support SGIO or large requests. | |
76 | * They have to set these according to their abilities. | |
77 | */ | |
78 | host->max_hw_segs = 1; | |
79 | host->max_phys_segs = 1; | |
80 | host->max_seg_size = PAGE_CACHE_SIZE; | |
81 | ||
82 | host->max_req_size = PAGE_CACHE_SIZE; | |
83 | host->max_blk_size = 512; | |
84 | host->max_blk_count = PAGE_CACHE_SIZE / 512; | |
85 | ||
86 | return host; | |
87 | } | |
88 | ||
89 | EXPORT_SYMBOL(mmc_alloc_host); | |
90 | ||
91 | /** | |
92 | * mmc_add_host - initialise host hardware | |
93 | * @host: mmc host | |
67a61c48 PO |
94 | * |
95 | * Register the host with the driver model. The host must be | |
96 | * prepared to start servicing requests before this function | |
97 | * completes. | |
b93931a6 PO |
98 | */ |
99 | int mmc_add_host(struct mmc_host *host) | |
100 | { | |
101 | int err; | |
102 | ||
17b759af NP |
103 | WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) && |
104 | !host->ops->enable_sdio_irq); | |
105 | ||
b93931a6 PO |
106 | if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL)) |
107 | return -ENOMEM; | |
108 | ||
109 | spin_lock(&mmc_host_lock); | |
110 | err = idr_get_new(&mmc_host_idr, host, &host->index); | |
111 | spin_unlock(&mmc_host_lock); | |
112 | if (err) | |
113 | return err; | |
114 | ||
115 | snprintf(host->class_dev.bus_id, BUS_ID_SIZE, | |
116 | "mmc%d", host->index); | |
117 | ||
118 | err = device_add(&host->class_dev); | |
119 | if (err) | |
120 | return err; | |
121 | ||
122 | mmc_start_host(host); | |
123 | ||
124 | return 0; | |
125 | } | |
126 | ||
127 | EXPORT_SYMBOL(mmc_add_host); | |
128 | ||
129 | /** | |
130 | * mmc_remove_host - remove host hardware | |
131 | * @host: mmc host | |
132 | * | |
133 | * Unregister and remove all cards associated with this host, | |
67a61c48 PO |
134 | * and power down the MMC bus. No new requests will be issued |
135 | * after this function has returned. | |
b93931a6 PO |
136 | */ |
137 | void mmc_remove_host(struct mmc_host *host) | |
138 | { | |
139 | mmc_stop_host(host); | |
140 | ||
141 | device_del(&host->class_dev); | |
142 | ||
143 | spin_lock(&mmc_host_lock); | |
144 | idr_remove(&mmc_host_idr, host->index); | |
145 | spin_unlock(&mmc_host_lock); | |
146 | } | |
147 | ||
148 | EXPORT_SYMBOL(mmc_remove_host); | |
149 | ||
150 | /** | |
151 | * mmc_free_host - free the host structure | |
152 | * @host: mmc host | |
153 | * | |
154 | * Free the host once all references to it have been dropped. | |
155 | */ | |
156 | void mmc_free_host(struct mmc_host *host) | |
157 | { | |
158 | put_device(&host->class_dev); | |
159 | } | |
160 | ||
161 | EXPORT_SYMBOL(mmc_free_host); | |
162 |