net: dcb: getnumtcs()/setnumtcs() should return an int
[linux-2.6.git] / lib / devres.c
blob9676617b44863fb1b19148abf2964beff1739c13
1 #include <linux/pci.h>
2 #include <linux/io.h>
3 #include <linux/gfp.h>
4 #include <linux/module.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_request_and_ioremap() - Check, request region, and ioremap resource
90 * @dev: Generic device to handle the resource for
91 * @res: resource to be handled
93 * Takes all necessary steps to ioremap a mem resource. Uses managed device, so
94 * everything is undone on driver detach. Checks arguments, so you can feed
95 * it the result from e.g. platform_get_resource() directly. Returns the
96 * remapped pointer or NULL on error. Usage example:
98 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
99 * base = devm_request_and_ioremap(&pdev->dev, res);
100 * if (!base)
101 * return -EADDRNOTAVAIL;
103 void __iomem *devm_request_and_ioremap(struct device *dev,
104 struct resource *res)
106 resource_size_t size;
107 const char *name;
108 void __iomem *dest_ptr;
110 BUG_ON(!dev);
112 if (!res || resource_type(res) != IORESOURCE_MEM) {
113 dev_err(dev, "invalid resource\n");
114 return NULL;
117 size = resource_size(res);
118 name = res->name ?: dev_name(dev);
120 if (!devm_request_mem_region(dev, res->start, size, name)) {
121 dev_err(dev, "can't request region for resource %pR\n", res);
122 return NULL;
125 if (res->flags & IORESOURCE_CACHEABLE)
126 dest_ptr = devm_ioremap(dev, res->start, size);
127 else
128 dest_ptr = devm_ioremap_nocache(dev, res->start, size);
130 if (!dest_ptr) {
131 dev_err(dev, "ioremap failed for resource %pR\n", res);
132 devm_release_mem_region(dev, res->start, size);
135 return dest_ptr;
137 EXPORT_SYMBOL(devm_request_and_ioremap);
139 #ifdef CONFIG_HAS_IOPORT
141 * Generic iomap devres
143 static void devm_ioport_map_release(struct device *dev, void *res)
145 ioport_unmap(*(void __iomem **)res);
148 static int devm_ioport_map_match(struct device *dev, void *res,
149 void *match_data)
151 return *(void **)res == match_data;
155 * devm_ioport_map - Managed ioport_map()
156 * @dev: Generic device to map ioport for
157 * @port: Port to map
158 * @nr: Number of ports to map
160 * Managed ioport_map(). Map is automatically unmapped on driver
161 * detach.
163 void __iomem * devm_ioport_map(struct device *dev, unsigned long port,
164 unsigned int nr)
166 void __iomem **ptr, *addr;
168 ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
169 if (!ptr)
170 return NULL;
172 addr = ioport_map(port, nr);
173 if (addr) {
174 *ptr = addr;
175 devres_add(dev, ptr);
176 } else
177 devres_free(ptr);
179 return addr;
181 EXPORT_SYMBOL(devm_ioport_map);
184 * devm_ioport_unmap - Managed ioport_unmap()
185 * @dev: Generic device to unmap for
186 * @addr: Address to unmap
188 * Managed ioport_unmap(). @addr must have been mapped using
189 * devm_ioport_map().
191 void devm_ioport_unmap(struct device *dev, void __iomem *addr)
193 ioport_unmap(addr);
194 WARN_ON(devres_destroy(dev, devm_ioport_map_release,
195 devm_ioport_map_match, (void *)addr));
197 EXPORT_SYMBOL(devm_ioport_unmap);
199 #ifdef CONFIG_PCI
201 * PCI iomap devres
203 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
205 struct pcim_iomap_devres {
206 void __iomem *table[PCIM_IOMAP_MAX];
209 static void pcim_iomap_release(struct device *gendev, void *res)
211 struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
212 struct pcim_iomap_devres *this = res;
213 int i;
215 for (i = 0; i < PCIM_IOMAP_MAX; i++)
216 if (this->table[i])
217 pci_iounmap(dev, this->table[i]);
221 * pcim_iomap_table - access iomap allocation table
222 * @pdev: PCI device to access iomap table for
224 * Access iomap allocation table for @dev. If iomap table doesn't
225 * exist and @pdev is managed, it will be allocated. All iomaps
226 * recorded in the iomap table are automatically unmapped on driver
227 * detach.
229 * This function might sleep when the table is first allocated but can
230 * be safely called without context and guaranteed to succed once
231 * allocated.
233 void __iomem * const * pcim_iomap_table(struct pci_dev *pdev)
235 struct pcim_iomap_devres *dr, *new_dr;
237 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
238 if (dr)
239 return dr->table;
241 new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
242 if (!new_dr)
243 return NULL;
244 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
245 return dr->table;
247 EXPORT_SYMBOL(pcim_iomap_table);
250 * pcim_iomap - Managed pcim_iomap()
251 * @pdev: PCI device to iomap for
252 * @bar: BAR to iomap
253 * @maxlen: Maximum length of iomap
255 * Managed pci_iomap(). Map is automatically unmapped on driver
256 * detach.
258 void __iomem * pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
260 void __iomem **tbl;
262 BUG_ON(bar >= PCIM_IOMAP_MAX);
264 tbl = (void __iomem **)pcim_iomap_table(pdev);
265 if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
266 return NULL;
268 tbl[bar] = pci_iomap(pdev, bar, maxlen);
269 return tbl[bar];
271 EXPORT_SYMBOL(pcim_iomap);
274 * pcim_iounmap - Managed pci_iounmap()
275 * @pdev: PCI device to iounmap for
276 * @addr: Address to unmap
278 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
280 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
282 void __iomem **tbl;
283 int i;
285 pci_iounmap(pdev, addr);
287 tbl = (void __iomem **)pcim_iomap_table(pdev);
288 BUG_ON(!tbl);
290 for (i = 0; i < PCIM_IOMAP_MAX; i++)
291 if (tbl[i] == addr) {
292 tbl[i] = NULL;
293 return;
295 WARN_ON(1);
297 EXPORT_SYMBOL(pcim_iounmap);
300 * pcim_iomap_regions - Request and iomap PCI BARs
301 * @pdev: PCI device to map IO resources for
302 * @mask: Mask of BARs to request and iomap
303 * @name: Name used when requesting regions
305 * Request and iomap regions specified by @mask.
307 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
309 void __iomem * const *iomap;
310 int i, rc;
312 iomap = pcim_iomap_table(pdev);
313 if (!iomap)
314 return -ENOMEM;
316 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
317 unsigned long len;
319 if (!(mask & (1 << i)))
320 continue;
322 rc = -EINVAL;
323 len = pci_resource_len(pdev, i);
324 if (!len)
325 goto err_inval;
327 rc = pci_request_region(pdev, i, name);
328 if (rc)
329 goto err_inval;
331 rc = -ENOMEM;
332 if (!pcim_iomap(pdev, i, 0))
333 goto err_region;
336 return 0;
338 err_region:
339 pci_release_region(pdev, i);
340 err_inval:
341 while (--i >= 0) {
342 if (!(mask & (1 << i)))
343 continue;
344 pcim_iounmap(pdev, iomap[i]);
345 pci_release_region(pdev, i);
348 return rc;
350 EXPORT_SYMBOL(pcim_iomap_regions);
353 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
354 * @pdev: PCI device to map IO resources for
355 * @mask: Mask of BARs to iomap
356 * @name: Name used when requesting regions
358 * Request all PCI BARs and iomap regions specified by @mask.
360 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
361 const char *name)
363 int request_mask = ((1 << 6) - 1) & ~mask;
364 int rc;
366 rc = pci_request_selected_regions(pdev, request_mask, name);
367 if (rc)
368 return rc;
370 rc = pcim_iomap_regions(pdev, mask, name);
371 if (rc)
372 pci_release_selected_regions(pdev, request_mask);
373 return rc;
375 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
378 * pcim_iounmap_regions - Unmap and release PCI BARs
379 * @pdev: PCI device to map IO resources for
380 * @mask: Mask of BARs to unmap and release
382 * Unmap and release regions specified by @mask.
384 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
386 void __iomem * const *iomap;
387 int i;
389 iomap = pcim_iomap_table(pdev);
390 if (!iomap)
391 return;
393 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
394 if (!(mask & (1 << i)))
395 continue;
397 pcim_iounmap(pdev, iomap[i]);
398 pci_release_region(pdev, i);
401 EXPORT_SYMBOL(pcim_iounmap_regions);
402 #endif /* CONFIG_PCI */
403 #endif /* CONFIG_HAS_IOPORT */