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