[MTD] maps/plat-ram.c: Initialize owner in device_driver struct
[usb.git] / drivers / mtd / maps / plat-ram.c
blobacdf9f7157752e991df27c43ba8bd0422c8573f7
1 /* drivers/mtd/maps/plat-ram.c
3 * (c) 2004-2005 Simtec Electronics
4 * http://www.simtec.co.uk/products/SWLINUX/
5 * Ben Dooks <ben@simtec.co.uk>
7 * Generic platfrom device based RAM map
9 * $Id: plat-ram.c,v 1.4 2005/10/10 00:51:26 bjd Exp $
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/string.h>
31 #include <linux/ioport.h>
32 #include <linux/device.h>
33 #include <linux/slab.h>
34 #include <linux/platform_device.h>
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/map.h>
38 #include <linux/mtd/partitions.h>
39 #include <linux/mtd/plat-ram.h>
41 #include <asm/io.h>
43 /* private structure for each mtd platform ram device created */
45 struct platram_info {
46 struct device *dev;
47 struct mtd_info *mtd;
48 struct map_info map;
49 struct mtd_partition *partitions;
50 struct resource *area;
51 struct platdata_mtd_ram *pdata;
54 /* to_platram_info()
56 * device private data to struct platram_info conversion
59 static inline struct platram_info *to_platram_info(struct device *dev)
61 return (struct platram_info *)dev_get_drvdata(dev);
64 /* platram_setrw
66 * call the platform device's set rw/ro control
68 * to = 0 => read-only
69 * = 1 => read-write
72 static inline void platram_setrw(struct platram_info *info, int to)
74 if (info->pdata == NULL)
75 return;
77 if (info->pdata->set_rw != NULL)
78 (info->pdata->set_rw)(info->dev, to);
81 /* platram_remove
83 * called to remove the device from the driver's control
86 static int platram_remove(struct device *dev)
88 struct platram_info *info = to_platram_info(dev);
90 dev_set_drvdata(dev, NULL);
92 dev_dbg(dev, "removing device\n");
94 if (info == NULL)
95 return 0;
97 if (info->mtd) {
98 #ifdef CONFIG_MTD_PARTITIONS
99 if (info->partitions) {
100 del_mtd_partitions(info->mtd);
101 kfree(info->partitions);
103 #endif
104 del_mtd_device(info->mtd);
105 map_destroy(info->mtd);
108 /* ensure ram is left read-only */
110 platram_setrw(info, PLATRAM_RO);
112 /* release resources */
114 if (info->area) {
115 release_resource(info->area);
116 kfree(info->area);
119 if (info->map.virt != NULL)
120 iounmap(info->map.virt);
122 kfree(info);
124 return 0;
127 /* platram_probe
129 * called from device drive system when a device matching our
130 * driver is found.
133 static int platram_probe(struct device *dev)
135 struct platform_device *pd = to_platform_device(dev);
136 struct platdata_mtd_ram *pdata;
137 struct platram_info *info;
138 struct resource *res;
139 int err = 0;
141 dev_dbg(dev, "probe entered\n");
143 if (dev->platform_data == NULL) {
144 dev_err(dev, "no platform data supplied\n");
145 err = -ENOENT;
146 goto exit_error;
149 pdata = dev->platform_data;
151 info = kmalloc(sizeof(*info), GFP_KERNEL);
152 if (info == NULL) {
153 dev_err(dev, "no memory for flash info\n");
154 err = -ENOMEM;
155 goto exit_error;
158 memset(info, 0, sizeof(*info));
159 dev_set_drvdata(dev, info);
161 info->dev = dev;
162 info->pdata = pdata;
164 /* get the resource for the memory mapping */
166 res = platform_get_resource(pd, IORESOURCE_MEM, 0);
168 if (res == NULL) {
169 dev_err(dev, "no memory resource specified\n");
170 err = -ENOENT;
171 goto exit_free;
174 dev_dbg(dev, "got platform resource %p (0x%lx)\n", res, res->start);
176 /* setup map parameters */
178 info->map.phys = res->start;
179 info->map.size = (res->end - res->start) + 1;
180 info->map.name = pdata->mapname != NULL ? pdata->mapname : pd->name;
181 info->map.bankwidth = pdata->bankwidth;
183 /* register our usage of the memory area */
185 info->area = request_mem_region(res->start, info->map.size, pd->name);
186 if (info->area == NULL) {
187 dev_err(dev, "failed to request memory region\n");
188 err = -EIO;
189 goto exit_free;
192 /* remap the memory area */
194 info->map.virt = ioremap(res->start, info->map.size);
195 dev_dbg(dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
197 if (info->map.virt == NULL) {
198 dev_err(dev, "failed to ioremap() region\n");
199 err = -EIO;
200 goto exit_free;
203 simple_map_init(&info->map);
205 dev_dbg(dev, "initialised map, probing for mtd\n");
207 /* probe for the right mtd map driver */
209 info->mtd = do_map_probe("map_ram" , &info->map);
210 if (info->mtd == NULL) {
211 dev_err(dev, "failed to probe for map_ram\n");
212 err = -ENOMEM;
213 goto exit_free;
216 info->mtd->owner = THIS_MODULE;
218 platram_setrw(info, PLATRAM_RW);
220 /* check to see if there are any available partitions, or wether
221 * to add this device whole */
223 #ifdef CONFIG_MTD_PARTITIONS
224 if (pdata->nr_partitions > 0) {
225 const char **probes = { NULL };
227 if (pdata->probes)
228 probes = (const char **)pdata->probes;
230 err = parse_mtd_partitions(info->mtd, probes,
231 &info->partitions, 0);
232 if (err > 0) {
233 err = add_mtd_partitions(info->mtd, info->partitions,
234 err);
237 #endif /* CONFIG_MTD_PARTITIONS */
239 if (add_mtd_device(info->mtd)) {
240 dev_err(dev, "add_mtd_device() failed\n");
241 err = -ENOMEM;
244 dev_info(dev, "registered mtd device\n");
245 return err;
247 exit_free:
248 platram_remove(dev);
249 exit_error:
250 return err;
253 /* device driver info */
255 static struct device_driver platram_driver = {
256 .name = "mtd-ram",
257 .owner = THIS_MODULE,
258 .bus = &platform_bus_type,
259 .probe = platram_probe,
260 .remove = platram_remove,
263 /* module init/exit */
265 static int __init platram_init(void)
267 printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
268 return driver_register(&platram_driver);
271 static void __exit platram_exit(void)
273 driver_unregister(&platram_driver);
276 module_init(platram_init);
277 module_exit(platram_exit);
279 MODULE_LICENSE("GPL");
280 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
281 MODULE_DESCRIPTION("MTD platform RAM map driver");