VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / base / platform.c
1 /*
2  * platform.c - platform 'pseudo' bus for legacy devices
3  *
4  * Copyright (c) 2002-3 Patrick Mochel
5  * Copyright (c) 2002-3 Open Source Development Labs
6  *
7  * This file is released under the GPLv2
8  *
9  * Please see Documentation/driver-model/platform.txt for more
10  * information.
11  */
12
13 #include <linux/device.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/bootmem.h>
18 #include <linux/err.h>
19
20 struct device platform_bus = {
21         .bus_id         = "platform",
22 };
23
24 /**
25  *      platform_get_resource - get a resource for a device
26  *      @dev: platform device
27  *      @type: resource type
28  *      @num: resource index
29  */
30 struct resource *
31 platform_get_resource(struct platform_device *dev, unsigned int type,
32                       unsigned int num)
33 {
34         int i;
35
36         for (i = 0; i < dev->num_resources; i++) {
37                 struct resource *r = &dev->resource[i];
38
39                 if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
40                                  IORESOURCE_IRQ|IORESOURCE_DMA))
41                     == type)
42                         if (num-- == 0)
43                                 return r;
44         }
45         return NULL;
46 }
47
48 /**
49  *      platform_get_irq - get an IRQ for a device
50  *      @dev: platform device
51  *      @num: IRQ number index
52  */
53 int platform_get_irq(struct platform_device *dev, unsigned int num)
54 {
55         struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
56
57         return r ? r->start : 0;
58 }
59
60 /**
61  *      platform_add_devices - add a numbers of platform devices
62  *      @devs: array of platform devices to add
63  *      @num: number of platform devices in array
64  */
65 int platform_add_devices(struct platform_device **devs, int num)
66 {
67         int i, ret = 0;
68
69         for (i = 0; i < num; i++) {
70                 ret = platform_device_register(devs[i]);
71                 if (ret) {
72                         while (--i >= 0)
73                                 platform_device_unregister(devs[i]);
74                         break;
75                 }
76         }
77
78         return ret;
79 }
80
81 /**
82  *      platform_device_register - add a platform-level device
83  *      @dev:   platform device we're adding
84  *
85  */
86 int platform_device_register(struct platform_device * pdev)
87 {
88         int i, ret = 0;
89
90         if (!pdev)
91                 return -EINVAL;
92
93         if (!pdev->dev.parent)
94                 pdev->dev.parent = &platform_bus;
95
96         pdev->dev.bus = &platform_bus_type;
97
98         if (pdev->id != -1)
99                 snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s%u", pdev->name, pdev->id);
100         else
101                 strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
102
103         for (i = 0; i < pdev->num_resources; i++) {
104                 struct resource *p, *r = &pdev->resource[i];
105
106                 r->name = pdev->dev.bus_id;
107
108                 p = NULL;
109                 if (r->flags & IORESOURCE_MEM)
110                         p = &iomem_resource;
111                 else if (r->flags & IORESOURCE_IO)
112                         p = &ioport_resource;
113
114                 if (p && request_resource(p, r)) {
115                         printk(KERN_ERR
116                                "%s: failed to claim resource %d\n",
117                                pdev->dev.bus_id, i);
118                         ret = -EBUSY;
119                         goto failed;
120                 }
121         }
122
123         pr_debug("Registering platform device '%s'. Parent at %s\n",
124                  pdev->dev.bus_id, pdev->dev.parent->bus_id);
125
126         ret = device_register(&pdev->dev);
127         if (ret == 0)
128                 return ret;
129
130  failed:
131         while (--i >= 0)
132                 if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
133                         release_resource(&pdev->resource[i]);
134         return ret;
135 }
136
137 /**
138  *      platform_device_unregister - remove a platform-level device
139  *      @dev:   platform device we're removing
140  *
141  *      Note that this function will also release all memory- and port-based
142  *      resources owned by the device (@dev->resource).
143  */
144 void platform_device_unregister(struct platform_device * pdev)
145 {
146         int i;
147
148         if (pdev) {
149                 for (i = 0; i < pdev->num_resources; i++) {
150                         struct resource *r = &pdev->resource[i];
151                         if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
152                                 release_resource(r);
153                 }
154
155                 device_unregister(&pdev->dev);
156         }
157 }
158
159 struct platform_object {
160         struct platform_device pdev;
161         struct resource resources[0];
162 };
163
164 static void platform_device_release_simple(struct device *dev)
165 {
166         struct platform_device *pdev = to_platform_device(dev);
167
168         kfree(container_of(pdev, struct platform_object, pdev));
169 }
170
171 /**
172  *      platform_device_register_simple
173  *      @name:  base name of the device we're adding
174  *      @id:    instance id
175  *      @res:   set of resources that needs to be allocated for the device
176  *      @num:   number of resources
177  *
178  *      This function creates a simple platform device that requires minimal
179  *      resource and memory management. Canned release function freeing
180  *      memory allocated for the device allows drivers using such devices
181  *      to be unloaded iwithout waiting for the last reference to the device
182  *      to be dropped.
183  */
184 struct platform_device *platform_device_register_simple(char *name, unsigned int id,
185                                                         struct resource *res, unsigned int num)
186 {
187         struct platform_object *pobj;
188         int retval;
189
190         pobj = kmalloc(sizeof(struct platform_object) + sizeof(struct resource) * num, GFP_KERNEL);
191         if (!pobj) {
192                 retval = -ENOMEM;
193                 goto error;
194         }
195
196         memset(pobj, 0, sizeof(*pobj));
197         pobj->pdev.name = name;
198         pobj->pdev.id = id;
199         pobj->pdev.dev.release = platform_device_release_simple;
200
201         if (num) {
202                 memcpy(pobj->resources, res, sizeof(struct resource) * num);
203                 pobj->pdev.resource = pobj->resources;
204                 pobj->pdev.num_resources = num;
205         }
206
207         retval = platform_device_register(&pobj->pdev);
208         if (retval)
209                 goto error;
210
211         return &pobj->pdev;
212
213 error:
214         kfree(pobj);
215         return ERR_PTR(retval);
216 }
217
218
219 /**
220  *      platform_match - bind platform device to platform driver.
221  *      @dev:   device.
222  *      @drv:   driver.
223  *
224  *      Platform device IDs are assumed to be encoded like this:
225  *      "<name><instance>", where <name> is a short description of the
226  *      type of device, like "pci" or "floppy", and <instance> is the
227  *      enumerated instance of the device, like '0' or '42'.
228  *      Driver IDs are simply "<name>".
229  *      So, extract the <name> from the platform_device structure,
230  *      and compare it against the name of the driver. Return whether
231  *      they match or not.
232  */
233
234 static int platform_match(struct device * dev, struct device_driver * drv)
235 {
236         struct platform_device *pdev = container_of(dev, struct platform_device, dev);
237
238         return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
239 }
240
241 static int platform_suspend(struct device * dev, u32 state)
242 {
243         int ret = 0;
244
245         if (dev->driver && dev->driver->suspend) {
246                 ret = dev->driver->suspend(dev, state, SUSPEND_DISABLE);
247                 if (ret == 0)
248                         ret = dev->driver->suspend(dev, state, SUSPEND_SAVE_STATE);
249                 if (ret == 0)
250                         ret = dev->driver->suspend(dev, state, SUSPEND_POWER_DOWN);
251         }
252         return ret;
253 }
254
255 static int platform_resume(struct device * dev)
256 {
257         int ret = 0;
258
259         if (dev->driver && dev->driver->resume) {
260                 ret = dev->driver->resume(dev, RESUME_POWER_ON);
261                 if (ret == 0)
262                         ret = dev->driver->resume(dev, RESUME_RESTORE_STATE);
263                 if (ret == 0)
264                         ret = dev->driver->resume(dev, RESUME_ENABLE);
265         }
266         return ret;
267 }
268
269 struct bus_type platform_bus_type = {
270         .name           = "platform",
271         .match          = platform_match,
272         .suspend        = platform_suspend,
273         .resume         = platform_resume,
274 };
275
276 int __init platform_bus_init(void)
277 {
278         device_register(&platform_bus);
279         return bus_register(&platform_bus_type);
280 }
281
282 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
283 u64 dma_get_required_mask(struct device *dev)
284 {
285         u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
286         u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
287         u64 mask;
288
289         if (!high_totalram) {
290                 /* convert to mask just covering totalram */
291                 low_totalram = (1 << (fls(low_totalram) - 1));
292                 low_totalram += low_totalram - 1;
293                 mask = low_totalram;
294         } else {
295                 high_totalram = (1 << (fls(high_totalram) - 1));
296                 high_totalram += high_totalram - 1;
297                 mask = (((u64)high_totalram) << 32) + 0xffffffff;
298         }
299         return mask & *dev->dma_mask;
300 }
301 EXPORT_SYMBOL(dma_get_required_mask);
302 #endif
303
304 EXPORT_SYMBOL(platform_bus);
305 EXPORT_SYMBOL(platform_bus_type);
306 EXPORT_SYMBOL(platform_device_register);
307 EXPORT_SYMBOL(platform_device_register_simple);
308 EXPORT_SYMBOL(platform_device_unregister);
309 EXPORT_SYMBOL(platform_get_irq);
310 EXPORT_SYMBOL(platform_get_resource);