drivers/net/wireless/ipw2x00: remove depends on CONFIG_EXPERIMENTAL
[linux/fpc-iii.git] / lib / devres.c
blob9c76b3a9cc722ab1116a9fa5821a8e05bc39fe35
1 #include <linux/pci.h>
2 #include <linux/io.h>
3 #include <linux/gfp.h>
4 #include <linux/export.h>
6 void devm_ioremap_release(struct device *dev, void *res)
8 iounmap(*(void __iomem **)res);
11 static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
13 return *(void **)res == match_data;
16 /**
17 * devm_ioremap - Managed ioremap()
18 * @dev: Generic device to remap IO address for
19 * @offset: BUS offset to map
20 * @size: Size of map
22 * Managed ioremap(). Map is automatically unmapped on driver detach.
24 void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
25 unsigned long size)
27 void __iomem **ptr, *addr;
29 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
30 if (!ptr)
31 return NULL;
33 addr = ioremap(offset, size);
34 if (addr) {
35 *ptr = addr;
36 devres_add(dev, ptr);
37 } else
38 devres_free(ptr);
40 return addr;
42 EXPORT_SYMBOL(devm_ioremap);
44 /**
45 * devm_ioremap_nocache - Managed ioremap_nocache()
46 * @dev: Generic device to remap IO address for
47 * @offset: BUS offset to map
48 * @size: Size of map
50 * Managed ioremap_nocache(). Map is automatically unmapped on driver
51 * detach.
53 void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
54 unsigned long size)
56 void __iomem **ptr, *addr;
58 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
59 if (!ptr)
60 return NULL;
62 addr = ioremap_nocache(offset, size);
63 if (addr) {
64 *ptr = addr;
65 devres_add(dev, ptr);
66 } else
67 devres_free(ptr);
69 return addr;
71 EXPORT_SYMBOL(devm_ioremap_nocache);
73 /**
74 * devm_iounmap - Managed iounmap()
75 * @dev: Generic device to unmap for
76 * @addr: Address to unmap
78 * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
80 void devm_iounmap(struct device *dev, void __iomem *addr)
82 WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
83 (void *)addr));
84 iounmap(addr);
86 EXPORT_SYMBOL(devm_iounmap);
88 /**
89 * devm_ioremap_resource() - check, request region, and ioremap resource
90 * @dev: generic device to handle the resource for
91 * @res: resource to be handled
93 * Checks that a resource is a valid memory region, requests the memory region
94 * and ioremaps it either as cacheable or as non-cacheable memory depending on
95 * the resource's flags. All operations are managed and will be undone on
96 * driver detach.
98 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
99 * on failure. Usage example:
101 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
102 * base = devm_ioremap_resource(&pdev->dev, res);
103 * if (IS_ERR(base))
104 * return PTR_ERR(base);
106 void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res)
108 resource_size_t size;
109 const char *name;
110 void __iomem *dest_ptr;
112 BUG_ON(!dev);
114 if (!res || resource_type(res) != IORESOURCE_MEM) {
115 dev_err(dev, "invalid resource\n");
116 return ERR_PTR(-EINVAL);
119 size = resource_size(res);
120 name = res->name ?: dev_name(dev);
122 if (!devm_request_mem_region(dev, res->start, size, name)) {
123 dev_err(dev, "can't request region for resource %pR\n", res);
124 return ERR_PTR(-EBUSY);
127 if (res->flags & IORESOURCE_CACHEABLE)
128 dest_ptr = devm_ioremap(dev, res->start, size);
129 else
130 dest_ptr = devm_ioremap_nocache(dev, res->start, size);
132 if (!dest_ptr) {
133 dev_err(dev, "ioremap failed for resource %pR\n", res);
134 devm_release_mem_region(dev, res->start, size);
135 dest_ptr = ERR_PTR(-ENOMEM);
138 return dest_ptr;
140 EXPORT_SYMBOL(devm_ioremap_resource);
143 * devm_request_and_ioremap() - Check, request region, and ioremap resource
144 * @dev: Generic device to handle the resource for
145 * @res: resource to be handled
147 * Takes all necessary steps to ioremap a mem resource. Uses managed device, so
148 * everything is undone on driver detach. Checks arguments, so you can feed
149 * it the result from e.g. platform_get_resource() directly. Returns the
150 * remapped pointer or NULL on error. Usage example:
152 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
153 * base = devm_request_and_ioremap(&pdev->dev, res);
154 * if (!base)
155 * return -EADDRNOTAVAIL;
157 void __iomem *devm_request_and_ioremap(struct device *device,
158 struct resource *res)
160 void __iomem *dest_ptr;
162 dest_ptr = devm_ioremap_resource(device, res);
163 if (IS_ERR(dest_ptr))
164 return NULL;
166 return dest_ptr;
168 EXPORT_SYMBOL(devm_request_and_ioremap);
170 #ifdef CONFIG_HAS_IOPORT
172 * Generic iomap devres
174 static void devm_ioport_map_release(struct device *dev, void *res)
176 ioport_unmap(*(void __iomem **)res);
179 static int devm_ioport_map_match(struct device *dev, void *res,
180 void *match_data)
182 return *(void **)res == match_data;
186 * devm_ioport_map - Managed ioport_map()
187 * @dev: Generic device to map ioport for
188 * @port: Port to map
189 * @nr: Number of ports to map
191 * Managed ioport_map(). Map is automatically unmapped on driver
192 * detach.
194 void __iomem * devm_ioport_map(struct device *dev, unsigned long port,
195 unsigned int nr)
197 void __iomem **ptr, *addr;
199 ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
200 if (!ptr)
201 return NULL;
203 addr = ioport_map(port, nr);
204 if (addr) {
205 *ptr = addr;
206 devres_add(dev, ptr);
207 } else
208 devres_free(ptr);
210 return addr;
212 EXPORT_SYMBOL(devm_ioport_map);
215 * devm_ioport_unmap - Managed ioport_unmap()
216 * @dev: Generic device to unmap for
217 * @addr: Address to unmap
219 * Managed ioport_unmap(). @addr must have been mapped using
220 * devm_ioport_map().
222 void devm_ioport_unmap(struct device *dev, void __iomem *addr)
224 ioport_unmap(addr);
225 WARN_ON(devres_destroy(dev, devm_ioport_map_release,
226 devm_ioport_map_match, (void *)addr));
228 EXPORT_SYMBOL(devm_ioport_unmap);
230 #ifdef CONFIG_PCI
232 * PCI iomap devres
234 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
236 struct pcim_iomap_devres {
237 void __iomem *table[PCIM_IOMAP_MAX];
240 static void pcim_iomap_release(struct device *gendev, void *res)
242 struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
243 struct pcim_iomap_devres *this = res;
244 int i;
246 for (i = 0; i < PCIM_IOMAP_MAX; i++)
247 if (this->table[i])
248 pci_iounmap(dev, this->table[i]);
252 * pcim_iomap_table - access iomap allocation table
253 * @pdev: PCI device to access iomap table for
255 * Access iomap allocation table for @dev. If iomap table doesn't
256 * exist and @pdev is managed, it will be allocated. All iomaps
257 * recorded in the iomap table are automatically unmapped on driver
258 * detach.
260 * This function might sleep when the table is first allocated but can
261 * be safely called without context and guaranteed to succed once
262 * allocated.
264 void __iomem * const * pcim_iomap_table(struct pci_dev *pdev)
266 struct pcim_iomap_devres *dr, *new_dr;
268 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
269 if (dr)
270 return dr->table;
272 new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
273 if (!new_dr)
274 return NULL;
275 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
276 return dr->table;
278 EXPORT_SYMBOL(pcim_iomap_table);
281 * pcim_iomap - Managed pcim_iomap()
282 * @pdev: PCI device to iomap for
283 * @bar: BAR to iomap
284 * @maxlen: Maximum length of iomap
286 * Managed pci_iomap(). Map is automatically unmapped on driver
287 * detach.
289 void __iomem * pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
291 void __iomem **tbl;
293 BUG_ON(bar >= PCIM_IOMAP_MAX);
295 tbl = (void __iomem **)pcim_iomap_table(pdev);
296 if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
297 return NULL;
299 tbl[bar] = pci_iomap(pdev, bar, maxlen);
300 return tbl[bar];
302 EXPORT_SYMBOL(pcim_iomap);
305 * pcim_iounmap - Managed pci_iounmap()
306 * @pdev: PCI device to iounmap for
307 * @addr: Address to unmap
309 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
311 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
313 void __iomem **tbl;
314 int i;
316 pci_iounmap(pdev, addr);
318 tbl = (void __iomem **)pcim_iomap_table(pdev);
319 BUG_ON(!tbl);
321 for (i = 0; i < PCIM_IOMAP_MAX; i++)
322 if (tbl[i] == addr) {
323 tbl[i] = NULL;
324 return;
326 WARN_ON(1);
328 EXPORT_SYMBOL(pcim_iounmap);
331 * pcim_iomap_regions - Request and iomap PCI BARs
332 * @pdev: PCI device to map IO resources for
333 * @mask: Mask of BARs to request and iomap
334 * @name: Name used when requesting regions
336 * Request and iomap regions specified by @mask.
338 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
340 void __iomem * const *iomap;
341 int i, rc;
343 iomap = pcim_iomap_table(pdev);
344 if (!iomap)
345 return -ENOMEM;
347 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
348 unsigned long len;
350 if (!(mask & (1 << i)))
351 continue;
353 rc = -EINVAL;
354 len = pci_resource_len(pdev, i);
355 if (!len)
356 goto err_inval;
358 rc = pci_request_region(pdev, i, name);
359 if (rc)
360 goto err_inval;
362 rc = -ENOMEM;
363 if (!pcim_iomap(pdev, i, 0))
364 goto err_region;
367 return 0;
369 err_region:
370 pci_release_region(pdev, i);
371 err_inval:
372 while (--i >= 0) {
373 if (!(mask & (1 << i)))
374 continue;
375 pcim_iounmap(pdev, iomap[i]);
376 pci_release_region(pdev, i);
379 return rc;
381 EXPORT_SYMBOL(pcim_iomap_regions);
384 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
385 * @pdev: PCI device to map IO resources for
386 * @mask: Mask of BARs to iomap
387 * @name: Name used when requesting regions
389 * Request all PCI BARs and iomap regions specified by @mask.
391 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
392 const char *name)
394 int request_mask = ((1 << 6) - 1) & ~mask;
395 int rc;
397 rc = pci_request_selected_regions(pdev, request_mask, name);
398 if (rc)
399 return rc;
401 rc = pcim_iomap_regions(pdev, mask, name);
402 if (rc)
403 pci_release_selected_regions(pdev, request_mask);
404 return rc;
406 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
409 * pcim_iounmap_regions - Unmap and release PCI BARs
410 * @pdev: PCI device to map IO resources for
411 * @mask: Mask of BARs to unmap and release
413 * Unmap and release regions specified by @mask.
415 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
417 void __iomem * const *iomap;
418 int i;
420 iomap = pcim_iomap_table(pdev);
421 if (!iomap)
422 return;
424 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
425 if (!(mask & (1 << i)))
426 continue;
428 pcim_iounmap(pdev, iomap[i]);
429 pci_release_region(pdev, i);
432 EXPORT_SYMBOL(pcim_iounmap_regions);
433 #endif /* CONFIG_PCI */
434 #endif /* CONFIG_HAS_IOPORT */