Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * resource.c - Contains functions for registering and analyzing resource information |
| 3 | * |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 4 | * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Copyright 2003 Adam Belay <ambx1@neo.rr.com> |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 6 | * Copyright (C) 2008 Hewlett-Packard Development Company, L.P. |
| 7 | * Bjorn Helgaas <bjorn.helgaas@hp.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/errno.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <asm/io.h> |
| 16 | #include <asm/dma.h> |
| 17 | #include <asm/irq.h> |
| 18 | #include <linux/pci.h> |
| 19 | #include <linux/ioport.h> |
| 20 | #include <linux/init.h> |
| 21 | |
| 22 | #include <linux/pnp.h> |
| 23 | #include "base.h" |
| 24 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 25 | static int pnp_reserve_irq[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some IRQ */ |
| 26 | static int pnp_reserve_dma[8] = {[0 ... 7] = -1 }; /* reserve (don't use) some DMA */ |
| 27 | static int pnp_reserve_io[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some I/O region */ |
| 28 | static int pnp_reserve_mem[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some memory region */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * option registration |
| 32 | */ |
| 33 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 34 | struct pnp_option *pnp_build_option(struct pnp_dev *dev, unsigned long type, |
| 35 | unsigned int option_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 37 | struct pnp_option *option; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 39 | option = kzalloc(sizeof(struct pnp_option), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | if (!option) |
| 41 | return NULL; |
| 42 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 43 | option->flags = option_flags; |
| 44 | option->type = type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 46 | list_add_tail(&option->list, &dev->options); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | return option; |
| 48 | } |
| 49 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 50 | int pnp_register_irq_resource(struct pnp_dev *dev, unsigned int option_flags, |
Bjorn Helgaas | c227536 | 2008-06-27 16:57:11 -0600 | [diff] [blame] | 51 | pnp_irq_mask_t *map, unsigned char flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 53 | struct pnp_option *option; |
| 54 | struct pnp_irq *irq; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 55 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 56 | option = pnp_build_option(dev, IORESOURCE_IRQ, option_flags); |
| 57 | if (!option) |
Bjorn Helgaas | c227536 | 2008-06-27 16:57:11 -0600 | [diff] [blame] | 58 | return -ENOMEM; |
| 59 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 60 | irq = &option->u.irq; |
Bjorn Helgaas | 2d29a7a | 2008-06-27 16:57:13 -0600 | [diff] [blame] | 61 | irq->map = *map; |
| 62 | irq->flags = flags; |
Bjorn Helgaas | c227536 | 2008-06-27 16:57:11 -0600 | [diff] [blame] | 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #ifdef CONFIG_PCI |
| 65 | { |
| 66 | int i; |
| 67 | |
| 68 | for (i = 0; i < 16; i++) |
Bjorn Helgaas | 2d29a7a | 2008-06-27 16:57:13 -0600 | [diff] [blame] | 69 | if (test_bit(i, irq->map.bits)) |
David Shaohua Li | c9c3e45 | 2005-04-01 00:07:31 -0500 | [diff] [blame] | 70 | pcibios_penalize_isa_irq(i, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | #endif |
Bjorn Helgaas | c1caf06 | 2008-04-28 16:34:04 -0600 | [diff] [blame] | 73 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 74 | dbg_pnp_show_option(dev, option); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 78 | int pnp_register_dma_resource(struct pnp_dev *dev, unsigned int option_flags, |
Bjorn Helgaas | c227536 | 2008-06-27 16:57:11 -0600 | [diff] [blame] | 79 | unsigned char map, unsigned char flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 81 | struct pnp_option *option; |
| 82 | struct pnp_dma *dma; |
Bjorn Helgaas | c227536 | 2008-06-27 16:57:11 -0600 | [diff] [blame] | 83 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 84 | option = pnp_build_option(dev, IORESOURCE_DMA, option_flags); |
| 85 | if (!option) |
Bjorn Helgaas | c227536 | 2008-06-27 16:57:11 -0600 | [diff] [blame] | 86 | return -ENOMEM; |
| 87 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 88 | dma = &option->u.dma; |
Bjorn Helgaas | 2d29a7a | 2008-06-27 16:57:13 -0600 | [diff] [blame] | 89 | dma->map = map; |
| 90 | dma->flags = flags; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 91 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 92 | dbg_pnp_show_option(dev, option); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 96 | int pnp_register_port_resource(struct pnp_dev *dev, unsigned int option_flags, |
Bjorn Helgaas | c227536 | 2008-06-27 16:57:11 -0600 | [diff] [blame] | 97 | resource_size_t min, resource_size_t max, |
| 98 | resource_size_t align, resource_size_t size, |
| 99 | unsigned char flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 101 | struct pnp_option *option; |
| 102 | struct pnp_port *port; |
Bjorn Helgaas | c227536 | 2008-06-27 16:57:11 -0600 | [diff] [blame] | 103 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 104 | option = pnp_build_option(dev, IORESOURCE_IO, option_flags); |
| 105 | if (!option) |
Bjorn Helgaas | c227536 | 2008-06-27 16:57:11 -0600 | [diff] [blame] | 106 | return -ENOMEM; |
| 107 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 108 | port = &option->u.port; |
Bjorn Helgaas | 2d29a7a | 2008-06-27 16:57:13 -0600 | [diff] [blame] | 109 | port->min = min; |
| 110 | port->max = max; |
| 111 | port->align = align; |
| 112 | port->size = size; |
| 113 | port->flags = flags; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 114 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 115 | dbg_pnp_show_option(dev, option); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 119 | int pnp_register_mem_resource(struct pnp_dev *dev, unsigned int option_flags, |
Bjorn Helgaas | c227536 | 2008-06-27 16:57:11 -0600 | [diff] [blame] | 120 | resource_size_t min, resource_size_t max, |
| 121 | resource_size_t align, resource_size_t size, |
| 122 | unsigned char flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 124 | struct pnp_option *option; |
| 125 | struct pnp_mem *mem; |
Bjorn Helgaas | c227536 | 2008-06-27 16:57:11 -0600 | [diff] [blame] | 126 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 127 | option = pnp_build_option(dev, IORESOURCE_MEM, option_flags); |
| 128 | if (!option) |
Bjorn Helgaas | c227536 | 2008-06-27 16:57:11 -0600 | [diff] [blame] | 129 | return -ENOMEM; |
| 130 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 131 | mem = &option->u.mem; |
Bjorn Helgaas | 2d29a7a | 2008-06-27 16:57:13 -0600 | [diff] [blame] | 132 | mem->min = min; |
| 133 | mem->max = max; |
| 134 | mem->align = align; |
| 135 | mem->size = size; |
| 136 | mem->flags = flags; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 137 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 138 | dbg_pnp_show_option(dev, option); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 142 | void pnp_free_options(struct pnp_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 144 | struct pnp_option *option, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 146 | list_for_each_entry_safe(option, tmp, &dev->options, list) { |
| 147 | list_del(&option->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | kfree(option); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* |
| 153 | * resource validity checking |
| 154 | */ |
| 155 | |
| 156 | #define length(start, end) (*(end) - *(start) + 1) |
| 157 | |
| 158 | /* Two ranges conflict if one doesn't end before the other starts */ |
| 159 | #define ranged_conflict(starta, enda, startb, endb) \ |
| 160 | !((*(enda) < *(startb)) || (*(endb) < *(starta))) |
| 161 | |
| 162 | #define cannot_compare(flags) \ |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 163 | ((flags) & IORESOURCE_DISABLED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 165 | int pnp_check_port(struct pnp_dev *dev, struct resource *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 167 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | struct pnp_dev *tdev; |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 169 | struct resource *tres; |
Greg Kroah-Hartman | b60ba83 | 2006-06-12 17:07:07 -0700 | [diff] [blame] | 170 | resource_size_t *port, *end, *tport, *tend; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 171 | |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 172 | port = &res->start; |
| 173 | end = &res->end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | /* if the resource doesn't exist, don't complain about it */ |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 176 | if (cannot_compare(res->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return 1; |
| 178 | |
| 179 | /* check if the resource is already in use, skip if the |
| 180 | * device is active because it itself may be in use */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 181 | if (!dev->active) { |
| 182 | if (__check_region(&ioport_resource, *port, length(port, end))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /* check if the resource is reserved */ |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 187 | for (i = 0; i < 8; i++) { |
| 188 | int rport = pnp_reserve_io[i << 1]; |
| 189 | int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 190 | if (ranged_conflict(port, end, &rport, &rend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | /* check for internal conflicts */ |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 195 | for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) { |
| 196 | if (tres != res && tres->flags & IORESOURCE_IO) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 197 | tport = &tres->start; |
| 198 | tend = &tres->end; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 199 | if (ranged_conflict(port, end, tport, tend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* check for conflicts with other pnp devices */ |
| 205 | pnp_for_each_dev(tdev) { |
| 206 | if (tdev == dev) |
| 207 | continue; |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 208 | for (i = 0; |
| 209 | (tres = pnp_get_resource(tdev, IORESOURCE_IO, i)); |
| 210 | i++) { |
| 211 | if (tres->flags & IORESOURCE_IO) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 212 | if (cannot_compare(tres->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | continue; |
Bjorn Helgaas | 11439a6 | 2010-05-03 10:47:21 -0600 | [diff] [blame] | 214 | if (tres->flags & IORESOURCE_WINDOW) |
| 215 | continue; |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 216 | tport = &tres->start; |
| 217 | tend = &tres->end; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 218 | if (ranged_conflict(port, end, tport, tend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return 1; |
| 225 | } |
| 226 | |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 227 | int pnp_check_mem(struct pnp_dev *dev, struct resource *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 229 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | struct pnp_dev *tdev; |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 231 | struct resource *tres; |
Greg Kroah-Hartman | b60ba83 | 2006-06-12 17:07:07 -0700 | [diff] [blame] | 232 | resource_size_t *addr, *end, *taddr, *tend; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 233 | |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 234 | addr = &res->start; |
| 235 | end = &res->end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | /* if the resource doesn't exist, don't complain about it */ |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 238 | if (cannot_compare(res->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | return 1; |
| 240 | |
| 241 | /* check if the resource is already in use, skip if the |
| 242 | * device is active because it itself may be in use */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 243 | if (!dev->active) { |
| 244 | if (check_mem_region(*addr, length(addr, end))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | /* check if the resource is reserved */ |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 249 | for (i = 0; i < 8; i++) { |
| 250 | int raddr = pnp_reserve_mem[i << 1]; |
| 251 | int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 252 | if (ranged_conflict(addr, end, &raddr, &rend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /* check for internal conflicts */ |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 257 | for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) { |
| 258 | if (tres != res && tres->flags & IORESOURCE_MEM) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 259 | taddr = &tres->start; |
| 260 | tend = &tres->end; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 261 | if (ranged_conflict(addr, end, taddr, tend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* check for conflicts with other pnp devices */ |
| 267 | pnp_for_each_dev(tdev) { |
| 268 | if (tdev == dev) |
| 269 | continue; |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 270 | for (i = 0; |
| 271 | (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i)); |
| 272 | i++) { |
| 273 | if (tres->flags & IORESOURCE_MEM) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 274 | if (cannot_compare(tres->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | continue; |
Bjorn Helgaas | 11439a6 | 2010-05-03 10:47:21 -0600 | [diff] [blame] | 276 | if (tres->flags & IORESOURCE_WINDOW) |
| 277 | continue; |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 278 | taddr = &tres->start; |
| 279 | tend = &tres->end; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 280 | if (ranged_conflict(addr, end, taddr, tend)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | return 0; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | return 1; |
| 287 | } |
| 288 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 289 | static irqreturn_t pnp_test_handler(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
| 291 | return IRQ_HANDLED; |
| 292 | } |
| 293 | |
Bjorn Helgaas | 84684c7 | 2008-06-27 16:57:18 -0600 | [diff] [blame] | 294 | #ifdef CONFIG_PCI |
| 295 | static int pci_dev_uses_irq(struct pnp_dev *pnp, struct pci_dev *pci, |
| 296 | unsigned int irq) |
| 297 | { |
| 298 | u32 class; |
| 299 | u8 progif; |
| 300 | |
| 301 | if (pci->irq == irq) { |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 302 | pnp_dbg(&pnp->dev, " device %s using irq %d\n", |
Bjorn Helgaas | 84684c7 | 2008-06-27 16:57:18 -0600 | [diff] [blame] | 303 | pci_name(pci), irq); |
| 304 | return 1; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * See pci_setup_device() and ata_pci_sff_activate_host() for |
| 309 | * similar IDE legacy detection. |
| 310 | */ |
| 311 | pci_read_config_dword(pci, PCI_CLASS_REVISION, &class); |
| 312 | class >>= 8; /* discard revision ID */ |
| 313 | progif = class & 0xff; |
| 314 | class >>= 8; |
| 315 | |
| 316 | if (class == PCI_CLASS_STORAGE_IDE) { |
| 317 | /* |
| 318 | * Unless both channels are native-PCI mode only, |
| 319 | * treat the compatibility IRQs as busy. |
| 320 | */ |
| 321 | if ((progif & 0x5) != 0x5) |
| 322 | if (pci_get_legacy_ide_irq(pci, 0) == irq || |
| 323 | pci_get_legacy_ide_irq(pci, 1) == irq) { |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 324 | pnp_dbg(&pnp->dev, " legacy IDE device %s " |
Bjorn Helgaas | 84684c7 | 2008-06-27 16:57:18 -0600 | [diff] [blame] | 325 | "using irq %d\n", pci_name(pci), irq); |
| 326 | return 1; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | #endif |
| 333 | |
| 334 | static int pci_uses_irq(struct pnp_dev *pnp, unsigned int irq) |
| 335 | { |
| 336 | #ifdef CONFIG_PCI |
| 337 | struct pci_dev *pci = NULL; |
| 338 | |
| 339 | for_each_pci_dev(pci) { |
| 340 | if (pci_dev_uses_irq(pnp, pci, irq)) { |
| 341 | pci_dev_put(pci); |
| 342 | return 1; |
| 343 | } |
| 344 | } |
| 345 | #endif |
| 346 | return 0; |
| 347 | } |
| 348 | |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 349 | int pnp_check_irq(struct pnp_dev *dev, struct resource *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | { |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 351 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | struct pnp_dev *tdev; |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 353 | struct resource *tres; |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 354 | resource_size_t *irq; |
| 355 | |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 356 | irq = &res->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
| 358 | /* if the resource doesn't exist, don't complain about it */ |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 359 | if (cannot_compare(res->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | return 1; |
| 361 | |
| 362 | /* check if the resource is valid */ |
| 363 | if (*irq < 0 || *irq > 15) |
| 364 | return 0; |
| 365 | |
| 366 | /* check if the resource is reserved */ |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 367 | for (i = 0; i < 16; i++) { |
| 368 | if (pnp_reserve_irq[i] == *irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | /* check for internal conflicts */ |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 373 | for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) { |
| 374 | if (tres != res && tres->flags & IORESOURCE_IRQ) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 375 | if (tres->start == *irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | } |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /* check if the resource is being used by a pci device */ |
Bjorn Helgaas | 84684c7 | 2008-06-27 16:57:18 -0600 | [diff] [blame] | 381 | if (pci_uses_irq(dev, *irq)) |
| 382 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
| 384 | /* check if the resource is already in use, skip if the |
| 385 | * device is active because it itself may be in use */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 386 | if (!dev->active) { |
Andrew Morton | 0cadaf4 | 2006-07-01 04:36:37 -0700 | [diff] [blame] | 387 | if (request_irq(*irq, pnp_test_handler, |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 388 | IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | return 0; |
| 390 | free_irq(*irq, NULL); |
| 391 | } |
| 392 | |
| 393 | /* check for conflicts with other pnp devices */ |
| 394 | pnp_for_each_dev(tdev) { |
| 395 | if (tdev == dev) |
| 396 | continue; |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 397 | for (i = 0; |
| 398 | (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i)); |
| 399 | i++) { |
| 400 | if (tres->flags & IORESOURCE_IRQ) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 401 | if (cannot_compare(tres->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | continue; |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 403 | if (tres->start == *irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | return 0; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | return 1; |
| 410 | } |
| 411 | |
David Rientjes | 586f83e | 2011-03-22 16:34:56 -0700 | [diff] [blame] | 412 | #ifdef CONFIG_ISA_DMA_API |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 413 | int pnp_check_dma(struct pnp_dev *dev, struct resource *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 415 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | struct pnp_dev *tdev; |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 417 | struct resource *tres; |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 418 | resource_size_t *dma; |
| 419 | |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 420 | dma = &res->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
| 422 | /* if the resource doesn't exist, don't complain about it */ |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 423 | if (cannot_compare(res->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | return 1; |
| 425 | |
| 426 | /* check if the resource is valid */ |
| 427 | if (*dma < 0 || *dma == 4 || *dma > 7) |
| 428 | return 0; |
| 429 | |
| 430 | /* check if the resource is reserved */ |
Bjorn Helgaas | ecfa935 | 2008-04-28 16:34:17 -0600 | [diff] [blame] | 431 | for (i = 0; i < 8; i++) { |
| 432 | if (pnp_reserve_dma[i] == *dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /* check for internal conflicts */ |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 437 | for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) { |
| 438 | if (tres != res && tres->flags & IORESOURCE_DMA) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 439 | if (tres->start == *dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /* check if the resource is already in use, skip if the |
| 445 | * device is active because it itself may be in use */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 446 | if (!dev->active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | if (request_dma(*dma, "pnp")) |
| 448 | return 0; |
| 449 | free_dma(*dma); |
| 450 | } |
| 451 | |
| 452 | /* check for conflicts with other pnp devices */ |
| 453 | pnp_for_each_dev(tdev) { |
| 454 | if (tdev == dev) |
| 455 | continue; |
Bjorn Helgaas | 95ab366 | 2008-04-28 16:34:26 -0600 | [diff] [blame] | 456 | for (i = 0; |
| 457 | (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i)); |
| 458 | i++) { |
| 459 | if (tres->flags & IORESOURCE_DMA) { |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 460 | if (cannot_compare(tres->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | continue; |
Bjorn Helgaas | 30c016a | 2008-04-28 16:34:19 -0600 | [diff] [blame] | 462 | if (tres->start == *dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | return 0; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
David Rientjes | 586f83e | 2011-03-22 16:34:56 -0700 | [diff] [blame] | 470 | #endif /* CONFIG_ISA_DMA_API */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Rene Herman | b563cf5 | 2008-10-15 22:03:58 -0700 | [diff] [blame] | 472 | unsigned long pnp_resource_type(struct resource *res) |
Bjorn Helgaas | 940e98d | 2008-06-27 16:56:54 -0600 | [diff] [blame] | 473 | { |
| 474 | return res->flags & (IORESOURCE_IO | IORESOURCE_MEM | |
Bjorn Helgaas | 7e0e9c0 | 2010-03-05 10:47:57 -0700 | [diff] [blame] | 475 | IORESOURCE_IRQ | IORESOURCE_DMA | |
| 476 | IORESOURCE_BUS); |
Bjorn Helgaas | 940e98d | 2008-06-27 16:56:54 -0600 | [diff] [blame] | 477 | } |
| 478 | |
Bjorn Helgaas | 0a977f1 | 2008-04-28 16:34:31 -0600 | [diff] [blame] | 479 | struct resource *pnp_get_resource(struct pnp_dev *dev, |
Rene Herman | b563cf5 | 2008-10-15 22:03:58 -0700 | [diff] [blame] | 480 | unsigned long type, unsigned int num) |
Bjorn Helgaas | 0a977f1 | 2008-04-28 16:34:31 -0600 | [diff] [blame] | 481 | { |
| 482 | struct pnp_resource *pnp_res; |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 483 | struct resource *res; |
Bjorn Helgaas | 0a977f1 | 2008-04-28 16:34:31 -0600 | [diff] [blame] | 484 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 485 | list_for_each_entry(pnp_res, &dev->resources, list) { |
| 486 | res = &pnp_res->res; |
| 487 | if (pnp_resource_type(res) == type && num-- == 0) |
| 488 | return res; |
| 489 | } |
Bjorn Helgaas | 0a977f1 | 2008-04-28 16:34:31 -0600 | [diff] [blame] | 490 | return NULL; |
| 491 | } |
Bjorn Helgaas | b90eca0 | 2008-04-28 16:34:14 -0600 | [diff] [blame] | 492 | EXPORT_SYMBOL(pnp_get_resource); |
| 493 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 494 | static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev) |
Bjorn Helgaas | a50b6d7 | 2008-04-28 16:34:33 -0600 | [diff] [blame] | 495 | { |
| 496 | struct pnp_resource *pnp_res; |
Bjorn Helgaas | a50b6d7 | 2008-04-28 16:34:33 -0600 | [diff] [blame] | 497 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 498 | pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL); |
| 499 | if (!pnp_res) |
| 500 | return NULL; |
| 501 | |
| 502 | list_add_tail(&pnp_res->list, &dev->resources); |
| 503 | return pnp_res; |
Bjorn Helgaas | a50b6d7 | 2008-04-28 16:34:33 -0600 | [diff] [blame] | 504 | } |
| 505 | |
Bjorn Helgaas | dbddd03 | 2008-04-28 16:34:34 -0600 | [diff] [blame] | 506 | struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq, |
| 507 | int flags) |
| 508 | { |
| 509 | struct pnp_resource *pnp_res; |
| 510 | struct resource *res; |
Bjorn Helgaas | dbddd03 | 2008-04-28 16:34:34 -0600 | [diff] [blame] | 511 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 512 | pnp_res = pnp_new_resource(dev); |
Bjorn Helgaas | dbddd03 | 2008-04-28 16:34:34 -0600 | [diff] [blame] | 513 | if (!pnp_res) { |
Bjorn Helgaas | 25d39c3 | 2008-06-27 16:56:59 -0600 | [diff] [blame] | 514 | dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq); |
Bjorn Helgaas | dbddd03 | 2008-04-28 16:34:34 -0600 | [diff] [blame] | 515 | return NULL; |
| 516 | } |
| 517 | |
| 518 | res = &pnp_res->res; |
| 519 | res->flags = IORESOURCE_IRQ | flags; |
| 520 | res->start = irq; |
| 521 | res->end = irq; |
| 522 | |
Bjorn Helgaas | c1f3f28 | 2010-09-29 12:24:23 -0600 | [diff] [blame] | 523 | dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res); |
Bjorn Helgaas | dbddd03 | 2008-04-28 16:34:34 -0600 | [diff] [blame] | 524 | return pnp_res; |
| 525 | } |
| 526 | |
Bjorn Helgaas | dc16f5f | 2008-04-28 16:34:35 -0600 | [diff] [blame] | 527 | struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma, |
| 528 | int flags) |
| 529 | { |
| 530 | struct pnp_resource *pnp_res; |
| 531 | struct resource *res; |
Bjorn Helgaas | dc16f5f | 2008-04-28 16:34:35 -0600 | [diff] [blame] | 532 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 533 | pnp_res = pnp_new_resource(dev); |
Bjorn Helgaas | dc16f5f | 2008-04-28 16:34:35 -0600 | [diff] [blame] | 534 | if (!pnp_res) { |
Bjorn Helgaas | 25d39c3 | 2008-06-27 16:56:59 -0600 | [diff] [blame] | 535 | dev_err(&dev->dev, "can't add resource for DMA %d\n", dma); |
Bjorn Helgaas | dc16f5f | 2008-04-28 16:34:35 -0600 | [diff] [blame] | 536 | return NULL; |
| 537 | } |
| 538 | |
| 539 | res = &pnp_res->res; |
| 540 | res->flags = IORESOURCE_DMA | flags; |
| 541 | res->start = dma; |
| 542 | res->end = dma; |
| 543 | |
Bjorn Helgaas | c1f3f28 | 2010-09-29 12:24:23 -0600 | [diff] [blame] | 544 | dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res); |
Bjorn Helgaas | dc16f5f | 2008-04-28 16:34:35 -0600 | [diff] [blame] | 545 | return pnp_res; |
| 546 | } |
| 547 | |
Bjorn Helgaas | cc8c2e3 | 2008-04-28 16:34:36 -0600 | [diff] [blame] | 548 | struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev, |
| 549 | resource_size_t start, |
| 550 | resource_size_t end, int flags) |
| 551 | { |
| 552 | struct pnp_resource *pnp_res; |
| 553 | struct resource *res; |
Bjorn Helgaas | cc8c2e3 | 2008-04-28 16:34:36 -0600 | [diff] [blame] | 554 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 555 | pnp_res = pnp_new_resource(dev); |
Bjorn Helgaas | cc8c2e3 | 2008-04-28 16:34:36 -0600 | [diff] [blame] | 556 | if (!pnp_res) { |
Bjorn Helgaas | 25d39c3 | 2008-06-27 16:56:59 -0600 | [diff] [blame] | 557 | dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n", |
| 558 | (unsigned long long) start, |
| 559 | (unsigned long long) end); |
Bjorn Helgaas | cc8c2e3 | 2008-04-28 16:34:36 -0600 | [diff] [blame] | 560 | return NULL; |
| 561 | } |
| 562 | |
| 563 | res = &pnp_res->res; |
| 564 | res->flags = IORESOURCE_IO | flags; |
| 565 | res->start = start; |
| 566 | res->end = end; |
| 567 | |
Bjorn Helgaas | c1f3f28 | 2010-09-29 12:24:23 -0600 | [diff] [blame] | 568 | dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res); |
Bjorn Helgaas | cc8c2e3 | 2008-04-28 16:34:36 -0600 | [diff] [blame] | 569 | return pnp_res; |
| 570 | } |
| 571 | |
Bjorn Helgaas | d6180f3 | 2008-04-28 16:34:37 -0600 | [diff] [blame] | 572 | struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev, |
| 573 | resource_size_t start, |
| 574 | resource_size_t end, int flags) |
| 575 | { |
| 576 | struct pnp_resource *pnp_res; |
| 577 | struct resource *res; |
Bjorn Helgaas | d6180f3 | 2008-04-28 16:34:37 -0600 | [diff] [blame] | 578 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 579 | pnp_res = pnp_new_resource(dev); |
Bjorn Helgaas | d6180f3 | 2008-04-28 16:34:37 -0600 | [diff] [blame] | 580 | if (!pnp_res) { |
Bjorn Helgaas | 25d39c3 | 2008-06-27 16:56:59 -0600 | [diff] [blame] | 581 | dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n", |
| 582 | (unsigned long long) start, |
| 583 | (unsigned long long) end); |
Bjorn Helgaas | d6180f3 | 2008-04-28 16:34:37 -0600 | [diff] [blame] | 584 | return NULL; |
| 585 | } |
| 586 | |
| 587 | res = &pnp_res->res; |
| 588 | res->flags = IORESOURCE_MEM | flags; |
| 589 | res->start = start; |
| 590 | res->end = end; |
| 591 | |
Bjorn Helgaas | c1f3f28 | 2010-09-29 12:24:23 -0600 | [diff] [blame] | 592 | dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res); |
Bjorn Helgaas | d6180f3 | 2008-04-28 16:34:37 -0600 | [diff] [blame] | 593 | return pnp_res; |
| 594 | } |
| 595 | |
Bjorn Helgaas | 7e0e9c0 | 2010-03-05 10:47:57 -0700 | [diff] [blame] | 596 | struct pnp_resource *pnp_add_bus_resource(struct pnp_dev *dev, |
| 597 | resource_size_t start, |
| 598 | resource_size_t end) |
| 599 | { |
| 600 | struct pnp_resource *pnp_res; |
| 601 | struct resource *res; |
| 602 | |
| 603 | pnp_res = pnp_new_resource(dev); |
| 604 | if (!pnp_res) { |
| 605 | dev_err(&dev->dev, "can't add resource for BUS %#llx-%#llx\n", |
| 606 | (unsigned long long) start, |
| 607 | (unsigned long long) end); |
| 608 | return NULL; |
| 609 | } |
| 610 | |
| 611 | res = &pnp_res->res; |
| 612 | res->flags = IORESOURCE_BUS; |
| 613 | res->start = start; |
| 614 | res->end = end; |
| 615 | |
Bjorn Helgaas | c1f3f28 | 2010-09-29 12:24:23 -0600 | [diff] [blame] | 616 | dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res); |
Bjorn Helgaas | 7e0e9c0 | 2010-03-05 10:47:57 -0700 | [diff] [blame] | 617 | return pnp_res; |
| 618 | } |
| 619 | |
Bjorn Helgaas | 57fd51a | 2008-06-27 16:57:01 -0600 | [diff] [blame] | 620 | /* |
| 621 | * Determine whether the specified resource is a possible configuration |
| 622 | * for this device. |
| 623 | */ |
| 624 | int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start, |
| 625 | resource_size_t size) |
| 626 | { |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 627 | struct pnp_option *option; |
| 628 | struct pnp_port *port; |
| 629 | struct pnp_mem *mem; |
| 630 | struct pnp_irq *irq; |
| 631 | struct pnp_dma *dma; |
Bjorn Helgaas | 57fd51a | 2008-06-27 16:57:01 -0600 | [diff] [blame] | 632 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 633 | list_for_each_entry(option, &dev->options, list) { |
| 634 | if (option->type != type) |
| 635 | continue; |
| 636 | |
| 637 | switch (option->type) { |
| 638 | case IORESOURCE_IO: |
| 639 | port = &option->u.port; |
| 640 | if (port->min == start && port->size == size) |
| 641 | return 1; |
| 642 | break; |
| 643 | case IORESOURCE_MEM: |
| 644 | mem = &option->u.mem; |
| 645 | if (mem->min == start && mem->size == size) |
| 646 | return 1; |
| 647 | break; |
| 648 | case IORESOURCE_IRQ: |
| 649 | irq = &option->u.irq; |
| 650 | if (start < PNP_IRQ_NR && |
| 651 | test_bit(start, irq->map.bits)) |
| 652 | return 1; |
| 653 | break; |
| 654 | case IORESOURCE_DMA: |
| 655 | dma = &option->u.dma; |
| 656 | if (dma->map & (1 << start)) |
| 657 | return 1; |
| 658 | break; |
| 659 | } |
| 660 | } |
Bjorn Helgaas | 57fd51a | 2008-06-27 16:57:01 -0600 | [diff] [blame] | 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | EXPORT_SYMBOL(pnp_possible_config); |
| 665 | |
Bjorn Helgaas | 1b8e696 | 2009-06-05 14:37:23 +0000 | [diff] [blame] | 666 | int pnp_range_reserved(resource_size_t start, resource_size_t end) |
| 667 | { |
| 668 | struct pnp_dev *dev; |
| 669 | struct pnp_resource *pnp_res; |
| 670 | resource_size_t *dev_start, *dev_end; |
| 671 | |
| 672 | pnp_for_each_dev(dev) { |
| 673 | list_for_each_entry(pnp_res, &dev->resources, list) { |
| 674 | dev_start = &pnp_res->res.start; |
| 675 | dev_end = &pnp_res->res.end; |
| 676 | if (ranged_conflict(&start, &end, dev_start, dev_end)) |
| 677 | return 1; |
| 678 | } |
| 679 | } |
| 680 | return 0; |
| 681 | } |
| 682 | EXPORT_SYMBOL(pnp_range_reserved); |
| 683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | /* format is: pnp_reserve_irq=irq1[,irq2] .... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | static int __init pnp_setup_reserve_irq(char *str) |
| 686 | { |
| 687 | int i; |
| 688 | |
| 689 | for (i = 0; i < 16; i++) |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 690 | if (get_option(&str, &pnp_reserve_irq[i]) != 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | break; |
| 692 | return 1; |
| 693 | } |
| 694 | |
| 695 | __setup("pnp_reserve_irq=", pnp_setup_reserve_irq); |
| 696 | |
| 697 | /* format is: pnp_reserve_dma=dma1[,dma2] .... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | static int __init pnp_setup_reserve_dma(char *str) |
| 699 | { |
| 700 | int i; |
| 701 | |
| 702 | for (i = 0; i < 8; i++) |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 703 | if (get_option(&str, &pnp_reserve_dma[i]) != 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | break; |
| 705 | return 1; |
| 706 | } |
| 707 | |
| 708 | __setup("pnp_reserve_dma=", pnp_setup_reserve_dma); |
| 709 | |
| 710 | /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | static int __init pnp_setup_reserve_io(char *str) |
| 712 | { |
| 713 | int i; |
| 714 | |
| 715 | for (i = 0; i < 16; i++) |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 716 | if (get_option(&str, &pnp_reserve_io[i]) != 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | break; |
| 718 | return 1; |
| 719 | } |
| 720 | |
| 721 | __setup("pnp_reserve_io=", pnp_setup_reserve_io); |
| 722 | |
| 723 | /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | static int __init pnp_setup_reserve_mem(char *str) |
| 725 | { |
| 726 | int i; |
| 727 | |
| 728 | for (i = 0; i < 16; i++) |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 729 | if (get_option(&str, &pnp_reserve_mem[i]) != 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | break; |
| 731 | return 1; |
| 732 | } |
| 733 | |
| 734 | __setup("pnp_reserve_mem=", pnp_setup_reserve_mem); |