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