Staging: hv: hv_mouse: fix camelcase in struct synthhid_device_info_ack
[zen-stable.git] / lib / devres.c
blob6efddf53b90c7d53048241bdac70273041df07c1
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 iounmap(addr);
83 WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
84 (void *)addr));
86 EXPORT_SYMBOL(devm_iounmap);
88 #ifdef CONFIG_HAS_IOPORT
90 * Generic iomap devres
92 static void devm_ioport_map_release(struct device *dev, void *res)
94 ioport_unmap(*(void __iomem **)res);
97 static int devm_ioport_map_match(struct device *dev, void *res,
98 void *match_data)
100 return *(void **)res == match_data;
104 * devm_ioport_map - Managed ioport_map()
105 * @dev: Generic device to map ioport for
106 * @port: Port to map
107 * @nr: Number of ports to map
109 * Managed ioport_map(). Map is automatically unmapped on driver
110 * detach.
112 void __iomem * devm_ioport_map(struct device *dev, unsigned long port,
113 unsigned int nr)
115 void __iomem **ptr, *addr;
117 ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
118 if (!ptr)
119 return NULL;
121 addr = ioport_map(port, nr);
122 if (addr) {
123 *ptr = addr;
124 devres_add(dev, ptr);
125 } else
126 devres_free(ptr);
128 return addr;
130 EXPORT_SYMBOL(devm_ioport_map);
133 * devm_ioport_unmap - Managed ioport_unmap()
134 * @dev: Generic device to unmap for
135 * @addr: Address to unmap
137 * Managed ioport_unmap(). @addr must have been mapped using
138 * devm_ioport_map().
140 void devm_ioport_unmap(struct device *dev, void __iomem *addr)
142 ioport_unmap(addr);
143 WARN_ON(devres_destroy(dev, devm_ioport_map_release,
144 devm_ioport_map_match, (void *)addr));
146 EXPORT_SYMBOL(devm_ioport_unmap);
148 #ifdef CONFIG_PCI
150 * PCI iomap devres
152 #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
154 struct pcim_iomap_devres {
155 void __iomem *table[PCIM_IOMAP_MAX];
158 static void pcim_iomap_release(struct device *gendev, void *res)
160 struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
161 struct pcim_iomap_devres *this = res;
162 int i;
164 for (i = 0; i < PCIM_IOMAP_MAX; i++)
165 if (this->table[i])
166 pci_iounmap(dev, this->table[i]);
170 * pcim_iomap_table - access iomap allocation table
171 * @pdev: PCI device to access iomap table for
173 * Access iomap allocation table for @dev. If iomap table doesn't
174 * exist and @pdev is managed, it will be allocated. All iomaps
175 * recorded in the iomap table are automatically unmapped on driver
176 * detach.
178 * This function might sleep when the table is first allocated but can
179 * be safely called without context and guaranteed to succed once
180 * allocated.
182 void __iomem * const * pcim_iomap_table(struct pci_dev *pdev)
184 struct pcim_iomap_devres *dr, *new_dr;
186 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
187 if (dr)
188 return dr->table;
190 new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
191 if (!new_dr)
192 return NULL;
193 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
194 return dr->table;
196 EXPORT_SYMBOL(pcim_iomap_table);
199 * pcim_iomap - Managed pcim_iomap()
200 * @pdev: PCI device to iomap for
201 * @bar: BAR to iomap
202 * @maxlen: Maximum length of iomap
204 * Managed pci_iomap(). Map is automatically unmapped on driver
205 * detach.
207 void __iomem * pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
209 void __iomem **tbl;
211 BUG_ON(bar >= PCIM_IOMAP_MAX);
213 tbl = (void __iomem **)pcim_iomap_table(pdev);
214 if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
215 return NULL;
217 tbl[bar] = pci_iomap(pdev, bar, maxlen);
218 return tbl[bar];
220 EXPORT_SYMBOL(pcim_iomap);
223 * pcim_iounmap - Managed pci_iounmap()
224 * @pdev: PCI device to iounmap for
225 * @addr: Address to unmap
227 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
229 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
231 void __iomem **tbl;
232 int i;
234 pci_iounmap(pdev, addr);
236 tbl = (void __iomem **)pcim_iomap_table(pdev);
237 BUG_ON(!tbl);
239 for (i = 0; i < PCIM_IOMAP_MAX; i++)
240 if (tbl[i] == addr) {
241 tbl[i] = NULL;
242 return;
244 WARN_ON(1);
246 EXPORT_SYMBOL(pcim_iounmap);
249 * pcim_iomap_regions - Request and iomap PCI BARs
250 * @pdev: PCI device to map IO resources for
251 * @mask: Mask of BARs to request and iomap
252 * @name: Name used when requesting regions
254 * Request and iomap regions specified by @mask.
256 int pcim_iomap_regions(struct pci_dev *pdev, u16 mask, const char *name)
258 void __iomem * const *iomap;
259 int i, rc;
261 iomap = pcim_iomap_table(pdev);
262 if (!iomap)
263 return -ENOMEM;
265 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
266 unsigned long len;
268 if (!(mask & (1 << i)))
269 continue;
271 rc = -EINVAL;
272 len = pci_resource_len(pdev, i);
273 if (!len)
274 goto err_inval;
276 rc = pci_request_region(pdev, i, name);
277 if (rc)
278 goto err_inval;
280 rc = -ENOMEM;
281 if (!pcim_iomap(pdev, i, 0))
282 goto err_region;
285 return 0;
287 err_region:
288 pci_release_region(pdev, i);
289 err_inval:
290 while (--i >= 0) {
291 if (!(mask & (1 << i)))
292 continue;
293 pcim_iounmap(pdev, iomap[i]);
294 pci_release_region(pdev, i);
297 return rc;
299 EXPORT_SYMBOL(pcim_iomap_regions);
302 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
303 * @pdev: PCI device to map IO resources for
304 * @mask: Mask of BARs to iomap
305 * @name: Name used when requesting regions
307 * Request all PCI BARs and iomap regions specified by @mask.
309 int pcim_iomap_regions_request_all(struct pci_dev *pdev, u16 mask,
310 const char *name)
312 int request_mask = ((1 << 6) - 1) & ~mask;
313 int rc;
315 rc = pci_request_selected_regions(pdev, request_mask, name);
316 if (rc)
317 return rc;
319 rc = pcim_iomap_regions(pdev, mask, name);
320 if (rc)
321 pci_release_selected_regions(pdev, request_mask);
322 return rc;
324 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
327 * pcim_iounmap_regions - Unmap and release PCI BARs
328 * @pdev: PCI device to map IO resources for
329 * @mask: Mask of BARs to unmap and release
331 * Unmap and release regions specified by @mask.
333 void pcim_iounmap_regions(struct pci_dev *pdev, u16 mask)
335 void __iomem * const *iomap;
336 int i;
338 iomap = pcim_iomap_table(pdev);
339 if (!iomap)
340 return;
342 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
343 if (!(mask & (1 << i)))
344 continue;
346 pcim_iounmap(pdev, iomap[i]);
347 pci_release_region(pdev, i);
350 EXPORT_SYMBOL(pcim_iounmap_regions);
351 #endif
352 #endif