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