Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * manager.c - Resource Management, Conflict Resolution, Activation and Disabling of Devices |
| 4 | * |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 5 | * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Copyright 2003 Adam Belay <ambx1@neo.rr.com> |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 7 | * Copyright (C) 2008 Hewlett-Packard Development Company, L.P. |
| 8 | * Bjorn Helgaas <bjorn.helgaas@hp.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/errno.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/pnp.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 16 | #include <linux/bitmap.h> |
Daniel Walker | b3bd86e | 2008-02-06 01:40:04 -0800 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "base.h" |
| 19 | |
Daniel Walker | b3bd86e | 2008-02-06 01:40:04 -0800 | [diff] [blame] | 20 | DEFINE_MUTEX(pnp_res_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Witold Szczeponik | 13cde3b | 2012-12-15 01:02:08 +0100 | [diff] [blame] | 22 | static struct resource *pnp_find_resource(struct pnp_dev *dev, |
| 23 | unsigned char rule, |
| 24 | unsigned long type, |
| 25 | unsigned int bar) |
| 26 | { |
| 27 | struct resource *res = pnp_get_resource(dev, type, bar); |
| 28 | |
| 29 | /* when the resource already exists, set its resource bits from rule */ |
| 30 | if (res) { |
| 31 | res->flags &= ~IORESOURCE_BITS; |
| 32 | res->flags |= rule & IORESOURCE_BITS; |
| 33 | } |
| 34 | |
| 35 | return res; |
| 36 | } |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx) |
| 39 | { |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 40 | struct resource *res, local_res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Witold Szczeponik | 13cde3b | 2012-12-15 01:02:08 +0100 | [diff] [blame] | 42 | res = pnp_find_resource(dev, rule->flags, IORESOURCE_IO, idx); |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 43 | if (res) { |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 44 | pnp_dbg(&dev->dev, " io %d already set to %#llx-%#llx " |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 45 | "flags %#lx\n", idx, (unsigned long long) res->start, |
| 46 | (unsigned long long) res->end, res->flags); |
Bjorn Helgaas | 6e906f0 | 2008-06-27 16:57:09 -0600 | [diff] [blame] | 47 | return 0; |
Bjorn Helgaas | 81b5c75 | 2008-04-28 16:34:08 -0600 | [diff] [blame] | 48 | } |
| 49 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 50 | res = &local_res; |
| 51 | res->flags = rule->flags | IORESOURCE_AUTO; |
| 52 | res->start = 0; |
| 53 | res->end = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | if (!rule->size) { |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 56 | res->flags |= IORESOURCE_DISABLED; |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 57 | pnp_dbg(&dev->dev, " io %d disabled\n", idx); |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 58 | goto __add; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 61 | res->start = rule->min; |
| 62 | res->end = res->start + rule->size - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 64 | while (!pnp_check_port(dev, res)) { |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 65 | res->start += rule->align; |
| 66 | res->end = res->start + rule->size - 1; |
| 67 | if (res->start > rule->max || !rule->align) { |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 68 | pnp_dbg(&dev->dev, " couldn't assign io %d " |
Bjorn Helgaas | fcfb7ce | 2008-06-27 16:57:07 -0600 | [diff] [blame] | 69 | "(min %#llx max %#llx)\n", idx, |
| 70 | (unsigned long long) rule->min, |
| 71 | (unsigned long long) rule->max); |
Bjorn Helgaas | 6e906f0 | 2008-06-27 16:57:09 -0600 | [diff] [blame] | 72 | return -EBUSY; |
Bjorn Helgaas | 81b5c75 | 2008-04-28 16:34:08 -0600 | [diff] [blame] | 73 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 75 | |
| 76 | __add: |
| 77 | pnp_add_io_resource(dev, res->start, res->end, res->flags); |
Bjorn Helgaas | 6e906f0 | 2008-06-27 16:57:09 -0600 | [diff] [blame] | 78 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx) |
| 82 | { |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 83 | struct resource *res, local_res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Witold Szczeponik | 13cde3b | 2012-12-15 01:02:08 +0100 | [diff] [blame] | 85 | res = pnp_find_resource(dev, rule->flags, IORESOURCE_MEM, idx); |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 86 | if (res) { |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 87 | pnp_dbg(&dev->dev, " mem %d already set to %#llx-%#llx " |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 88 | "flags %#lx\n", idx, (unsigned long long) res->start, |
| 89 | (unsigned long long) res->end, res->flags); |
Bjorn Helgaas | 6e906f0 | 2008-06-27 16:57:09 -0600 | [diff] [blame] | 90 | return 0; |
Bjorn Helgaas | 81b5c75 | 2008-04-28 16:34:08 -0600 | [diff] [blame] | 91 | } |
| 92 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 93 | res = &local_res; |
| 94 | res->flags = rule->flags | IORESOURCE_AUTO; |
| 95 | res->start = 0; |
| 96 | res->end = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Witold Szczeponik | 13cde3b | 2012-12-15 01:02:08 +0100 | [diff] [blame] | 98 | /* ??? rule->flags restricted to 8 bits, all tests bogus ??? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | if (!(rule->flags & IORESOURCE_MEM_WRITEABLE)) |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 100 | res->flags |= IORESOURCE_READONLY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | if (rule->flags & IORESOURCE_MEM_RANGELENGTH) |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 102 | res->flags |= IORESOURCE_RANGELENGTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | if (rule->flags & IORESOURCE_MEM_SHADOWABLE) |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 104 | res->flags |= IORESOURCE_SHADOWABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | if (!rule->size) { |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 107 | res->flags |= IORESOURCE_DISABLED; |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 108 | pnp_dbg(&dev->dev, " mem %d disabled\n", idx); |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 109 | goto __add; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 112 | res->start = rule->min; |
| 113 | res->end = res->start + rule->size - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Bjorn Helgaas | f5d94ff | 2008-04-28 16:34:22 -0600 | [diff] [blame] | 115 | while (!pnp_check_mem(dev, res)) { |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 116 | res->start += rule->align; |
| 117 | res->end = res->start + rule->size - 1; |
| 118 | if (res->start > rule->max || !rule->align) { |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 119 | pnp_dbg(&dev->dev, " couldn't assign mem %d " |
Bjorn Helgaas | fcfb7ce | 2008-06-27 16:57:07 -0600 | [diff] [blame] | 120 | "(min %#llx max %#llx)\n", idx, |
| 121 | (unsigned long long) rule->min, |
| 122 | (unsigned long long) rule->max); |
Bjorn Helgaas | 6e906f0 | 2008-06-27 16:57:09 -0600 | [diff] [blame] | 123 | return -EBUSY; |
Bjorn Helgaas | 81b5c75 | 2008-04-28 16:34:08 -0600 | [diff] [blame] | 124 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 126 | |
| 127 | __add: |
| 128 | pnp_add_mem_resource(dev, res->start, res->end, res->flags); |
Bjorn Helgaas | 6e906f0 | 2008-06-27 16:57:09 -0600 | [diff] [blame] | 129 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 132 | static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 134 | struct resource *res, local_res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | int i; |
| 136 | |
| 137 | /* IRQ priority: this table is good for i386 */ |
| 138 | static unsigned short xtab[16] = { |
| 139 | 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2 |
| 140 | }; |
| 141 | |
Witold Szczeponik | 13cde3b | 2012-12-15 01:02:08 +0100 | [diff] [blame] | 142 | res = pnp_find_resource(dev, rule->flags, IORESOURCE_IRQ, idx); |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 143 | if (res) { |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 144 | pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n", |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 145 | idx, (int) res->start, res->flags); |
Bjorn Helgaas | 6e906f0 | 2008-06-27 16:57:09 -0600 | [diff] [blame] | 146 | return 0; |
Bjorn Helgaas | 81b5c75 | 2008-04-28 16:34:08 -0600 | [diff] [blame] | 147 | } |
| 148 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 149 | res = &local_res; |
| 150 | res->flags = rule->flags | IORESOURCE_AUTO; |
| 151 | res->start = -1; |
| 152 | res->end = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Bjorn Helgaas | 7aefff5 | 2008-06-27 16:57:05 -0600 | [diff] [blame] | 154 | if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) { |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 155 | res->flags |= IORESOURCE_DISABLED; |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 156 | pnp_dbg(&dev->dev, " irq %d disabled\n", idx); |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 157 | goto __add; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* TBD: need check for >16 IRQ */ |
Bjorn Helgaas | 7aefff5 | 2008-06-27 16:57:05 -0600 | [diff] [blame] | 161 | res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16); |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 162 | if (res->start < PNP_IRQ_NR) { |
| 163 | res->end = res->start; |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 164 | goto __add; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | for (i = 0; i < 16; i++) { |
Bjorn Helgaas | 7aefff5 | 2008-06-27 16:57:05 -0600 | [diff] [blame] | 167 | if (test_bit(xtab[i], rule->map.bits)) { |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 168 | res->start = res->end = xtab[i]; |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 169 | if (pnp_check_irq(dev, res)) |
| 170 | goto __add; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
Bjorn Helgaas | d5ebde6 | 2008-06-27 16:57:14 -0600 | [diff] [blame] | 173 | |
| 174 | if (rule->flags & IORESOURCE_IRQ_OPTIONAL) { |
| 175 | res->start = -1; |
| 176 | res->end = -1; |
| 177 | res->flags |= IORESOURCE_DISABLED; |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 178 | pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx); |
Bjorn Helgaas | d5ebde6 | 2008-06-27 16:57:14 -0600 | [diff] [blame] | 179 | goto __add; |
| 180 | } |
| 181 | |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 182 | pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx); |
Bjorn Helgaas | 6e906f0 | 2008-06-27 16:57:09 -0600 | [diff] [blame] | 183 | return -EBUSY; |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 184 | |
| 185 | __add: |
| 186 | pnp_add_irq_resource(dev, res->start, res->flags); |
Bjorn Helgaas | 6e906f0 | 2008-06-27 16:57:09 -0600 | [diff] [blame] | 187 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
David Rientjes | 586f83e | 2011-03-22 16:34:56 -0700 | [diff] [blame] | 190 | #ifdef CONFIG_ISA_DMA_API |
Bjorn Helgaas | 6e906f0 | 2008-06-27 16:57:09 -0600 | [diff] [blame] | 191 | static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 193 | struct resource *res, local_res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | int i; |
| 195 | |
| 196 | /* DMA priority: this table is good for i386 */ |
| 197 | static unsigned short xtab[8] = { |
| 198 | 1, 3, 5, 6, 7, 0, 2, 4 |
| 199 | }; |
| 200 | |
Witold Szczeponik | 13cde3b | 2012-12-15 01:02:08 +0100 | [diff] [blame] | 201 | res = pnp_find_resource(dev, rule->flags, IORESOURCE_DMA, idx); |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 202 | if (res) { |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 203 | pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n", |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 204 | idx, (int) res->start, res->flags); |
Bjorn Helgaas | 6e906f0 | 2008-06-27 16:57:09 -0600 | [diff] [blame] | 205 | return 0; |
Bjorn Helgaas | 81b5c75 | 2008-04-28 16:34:08 -0600 | [diff] [blame] | 206 | } |
| 207 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 208 | res = &local_res; |
| 209 | res->flags = rule->flags | IORESOURCE_AUTO; |
| 210 | res->start = -1; |
| 211 | res->end = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
David Flater | bdf0eb3 | 2013-05-10 14:37:43 +0200 | [diff] [blame] | 213 | if (!rule->map) { |
| 214 | res->flags |= IORESOURCE_DISABLED; |
| 215 | pnp_dbg(&dev->dev, " dma %d disabled\n", idx); |
| 216 | goto __add; |
| 217 | } |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | for (i = 0; i < 8; i++) { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 220 | if (rule->map & (1 << xtab[i])) { |
Bjorn Helgaas | 28ccffc | 2008-04-28 16:34:18 -0600 | [diff] [blame] | 221 | res->start = res->end = xtab[i]; |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 222 | if (pnp_check_dma(dev, res)) |
| 223 | goto __add; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
David Flater | bdf0eb3 | 2013-05-10 14:37:43 +0200 | [diff] [blame] | 226 | |
| 227 | pnp_dbg(&dev->dev, " couldn't assign dma %d\n", idx); |
| 228 | return -EBUSY; |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 229 | |
| 230 | __add: |
| 231 | pnp_add_dma_resource(dev, res->start, res->flags); |
Bjorn Helgaas | 6e906f0 | 2008-06-27 16:57:09 -0600 | [diff] [blame] | 232 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
David Rientjes | 586f83e | 2011-03-22 16:34:56 -0700 | [diff] [blame] | 234 | #endif /* CONFIG_ISA_DMA_API */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Bjorn Helgaas | 2cd139309 | 2008-04-28 16:34:11 -0600 | [diff] [blame] | 236 | void pnp_init_resources(struct pnp_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 238 | pnp_free_resources(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Bjorn Helgaas | 6969c7e | 2008-04-28 16:34:10 -0600 | [diff] [blame] | 241 | static void pnp_clean_resource_table(struct pnp_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 243 | struct pnp_resource *pnp_res, *tmp; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 244 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 245 | list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) { |
| 246 | if (pnp_res->res.flags & IORESOURCE_AUTO) |
| 247 | pnp_free_resource(pnp_res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * pnp_assign_resources - assigns resources to the device based on the specified dependent number |
| 253 | * @dev: pointer to the desired device |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 254 | * @set: the dependent function number |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | */ |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 256 | static int pnp_assign_resources(struct pnp_dev *dev, int set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 258 | struct pnp_option *option; |
David Rientjes | 586f83e | 2011-03-22 16:34:56 -0700 | [diff] [blame] | 259 | int nport = 0, nmem = 0, nirq = 0; |
| 260 | int ndma __maybe_unused = 0; |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 261 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 263 | pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set); |
Daniel Walker | b3bd86e | 2008-02-06 01:40:04 -0800 | [diff] [blame] | 264 | mutex_lock(&pnp_res_mutex); |
Bjorn Helgaas | 6969c7e | 2008-04-28 16:34:10 -0600 | [diff] [blame] | 265 | pnp_clean_resource_table(dev); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 266 | |
| 267 | list_for_each_entry(option, &dev->options, list) { |
| 268 | if (pnp_option_is_dependent(option) && |
| 269 | pnp_option_set(option) != set) |
| 270 | continue; |
| 271 | |
| 272 | switch (option->type) { |
| 273 | case IORESOURCE_IO: |
| 274 | ret = pnp_assign_port(dev, &option->u.port, nport++); |
| 275 | break; |
| 276 | case IORESOURCE_MEM: |
| 277 | ret = pnp_assign_mem(dev, &option->u.mem, nmem++); |
| 278 | break; |
| 279 | case IORESOURCE_IRQ: |
| 280 | ret = pnp_assign_irq(dev, &option->u.irq, nirq++); |
| 281 | break; |
David Rientjes | 586f83e | 2011-03-22 16:34:56 -0700 | [diff] [blame] | 282 | #ifdef CONFIG_ISA_DMA_API |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 283 | case IORESOURCE_DMA: |
| 284 | ret = pnp_assign_dma(dev, &option->u.dma, ndma++); |
| 285 | break; |
David Rientjes | 586f83e | 2011-03-22 16:34:56 -0700 | [diff] [blame] | 286 | #endif |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 287 | default: |
| 288 | ret = -EINVAL; |
| 289 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 291 | if (ret < 0) |
| 292 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Daniel Walker | b3bd86e | 2008-02-06 01:40:04 -0800 | [diff] [blame] | 295 | mutex_unlock(&pnp_res_mutex); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 296 | if (ret < 0) { |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 297 | pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 298 | pnp_clean_resource_table(dev); |
| 299 | } else |
| 300 | dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded"); |
| 301 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | * pnp_auto_config_dev - automatically assigns resources to a device |
| 306 | * @dev: pointer to the desired device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | */ |
| 308 | int pnp_auto_config_dev(struct pnp_dev *dev) |
| 309 | { |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 310 | int i, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 312 | if (!pnp_can_configure(dev)) { |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 313 | pnp_dbg(&dev->dev, "configuration not supported\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | return -ENODEV; |
| 315 | } |
| 316 | |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 317 | ret = pnp_assign_resources(dev, 0); |
| 318 | if (ret == 0) |
| 319 | return 0; |
| 320 | |
| 321 | for (i = 1; i < dev->num_dependent_sets; i++) { |
| 322 | ret = pnp_assign_resources(dev, i); |
| 323 | if (ret == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Bjorn Helgaas | a05d078 | 2007-10-16 23:31:10 -0700 | [diff] [blame] | 327 | dev_err(&dev->dev, "unable to assign resources\n"); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 328 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
Pierre Ossman | 68094e3 | 2005-11-29 09:09:32 +0100 | [diff] [blame] | 332 | * pnp_start_dev - low-level start of the PnP device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | * @dev: pointer to the desired device |
| 334 | * |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 335 | * assumes that resources have already been allocated |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | */ |
Pierre Ossman | 68094e3 | 2005-11-29 09:09:32 +0100 | [diff] [blame] | 337 | int pnp_start_dev(struct pnp_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | if (!pnp_can_write(dev)) { |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 340 | pnp_dbg(&dev->dev, "activation not supported\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return -EINVAL; |
| 342 | } |
| 343 | |
Bjorn Helgaas | 81b5c75 | 2008-04-28 16:34:08 -0600 | [diff] [blame] | 344 | dbg_pnp_show_resources(dev, "pnp_start_dev"); |
Bjorn Helgaas | 59284cb | 2008-04-28 16:34:05 -0600 | [diff] [blame] | 345 | if (dev->protocol->set(dev) < 0) { |
Bjorn Helgaas | a05d078 | 2007-10-16 23:31:10 -0700 | [diff] [blame] | 346 | dev_err(&dev->dev, "activation failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | return -EIO; |
| 348 | } |
| 349 | |
Bjorn Helgaas | a05d078 | 2007-10-16 23:31:10 -0700 | [diff] [blame] | 350 | dev_info(&dev->dev, "activated\n"); |
Pierre Ossman | 68094e3 | 2005-11-29 09:09:32 +0100 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * pnp_stop_dev - low-level disable of the PnP device |
| 356 | * @dev: pointer to the desired device |
| 357 | * |
| 358 | * does not free resources |
| 359 | */ |
Pierre Ossman | 68094e3 | 2005-11-29 09:09:32 +0100 | [diff] [blame] | 360 | int pnp_stop_dev(struct pnp_dev *dev) |
| 361 | { |
| 362 | if (!pnp_can_disable(dev)) { |
Bjorn Helgaas | 2f53432 | 2008-08-19 16:53:47 -0600 | [diff] [blame] | 363 | pnp_dbg(&dev->dev, "disabling not supported\n"); |
Pierre Ossman | 68094e3 | 2005-11-29 09:09:32 +0100 | [diff] [blame] | 364 | return -EINVAL; |
| 365 | } |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 366 | if (dev->protocol->disable(dev) < 0) { |
Bjorn Helgaas | a05d078 | 2007-10-16 23:31:10 -0700 | [diff] [blame] | 367 | dev_err(&dev->dev, "disable failed\n"); |
Pierre Ossman | 68094e3 | 2005-11-29 09:09:32 +0100 | [diff] [blame] | 368 | return -EIO; |
| 369 | } |
| 370 | |
Bjorn Helgaas | a05d078 | 2007-10-16 23:31:10 -0700 | [diff] [blame] | 371 | dev_info(&dev->dev, "disabled\n"); |
Pierre Ossman | 68094e3 | 2005-11-29 09:09:32 +0100 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * pnp_activate_dev - activates a PnP device for use |
| 377 | * @dev: pointer to the desired device |
| 378 | * |
| 379 | * does not validate or set resources so be careful. |
| 380 | */ |
| 381 | int pnp_activate_dev(struct pnp_dev *dev) |
| 382 | { |
| 383 | int error; |
| 384 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 385 | if (dev->active) |
Bjorn Helgaas | cc8259a | 2008-02-06 01:40:02 -0800 | [diff] [blame] | 386 | return 0; |
Pierre Ossman | 68094e3 | 2005-11-29 09:09:32 +0100 | [diff] [blame] | 387 | |
| 388 | /* ensure resources are allocated */ |
| 389 | if (pnp_auto_config_dev(dev)) |
| 390 | return -EBUSY; |
| 391 | |
| 392 | error = pnp_start_dev(dev); |
| 393 | if (error) |
| 394 | return error; |
| 395 | |
| 396 | dev->active = 1; |
Bjorn Helgaas | cc8259a | 2008-02-06 01:40:02 -0800 | [diff] [blame] | 397 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /** |
| 401 | * pnp_disable_dev - disables device |
| 402 | * @dev: pointer to the desired device |
| 403 | * |
| 404 | * inform the correct pnp protocol so that resources can be used by other devices |
| 405 | */ |
| 406 | int pnp_disable_dev(struct pnp_dev *dev) |
| 407 | { |
Pierre Ossman | 68094e3 | 2005-11-29 09:09:32 +0100 | [diff] [blame] | 408 | int error; |
| 409 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 410 | if (!dev->active) |
Bjorn Helgaas | cc8259a | 2008-02-06 01:40:02 -0800 | [diff] [blame] | 411 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Pierre Ossman | 68094e3 | 2005-11-29 09:09:32 +0100 | [diff] [blame] | 413 | error = pnp_stop_dev(dev); |
| 414 | if (error) |
| 415 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
| 417 | dev->active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
| 419 | /* release the resources so that other devices can use them */ |
Daniel Walker | b3bd86e | 2008-02-06 01:40:04 -0800 | [diff] [blame] | 420 | mutex_lock(&pnp_res_mutex); |
Bjorn Helgaas | 6969c7e | 2008-04-28 16:34:10 -0600 | [diff] [blame] | 421 | pnp_clean_resource_table(dev); |
Daniel Walker | b3bd86e | 2008-02-06 01:40:04 -0800 | [diff] [blame] | 422 | mutex_unlock(&pnp_res_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Bjorn Helgaas | cc8259a | 2008-02-06 01:40:02 -0800 | [diff] [blame] | 424 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Pierre Ossman | 68094e3 | 2005-11-29 09:09:32 +0100 | [diff] [blame] | 427 | EXPORT_SYMBOL(pnp_start_dev); |
| 428 | EXPORT_SYMBOL(pnp_stop_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | EXPORT_SYMBOL(pnp_activate_dev); |
| 430 | EXPORT_SYMBOL(pnp_disable_dev); |