blob: 407b4eaddcbffe6202ae40623430f9f638b23cb2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * pnpacpi -- PnP ACPI driver
3 *
4 * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr>
5 * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com>
Bob Moore50eca3e2005-09-30 19:03:00 -04006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21#include <linux/kernel.h>
22#include <linux/acpi.h>
23#include <linux/pci.h>
24#include "pnpacpi.h"
25
26#ifdef CONFIG_IA64
27#define valid_IRQ(i) (1)
28#else
29#define valid_IRQ(i) (((i) != 0) && ((i) != 2))
30#endif
31
32/*
33 * Allocated Resources
34 */
Bob Moore50eca3e2005-09-30 19:03:00 -040035static int irq_flags(int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 int flag;
Bob Moore50eca3e2005-09-30 19:03:00 -040038 if (triggering == ACPI_LEVEL_SENSITIVE) {
39 if(polarity == ACPI_ACTIVE_LOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 flag = IORESOURCE_IRQ_LOWLEVEL;
41 else
42 flag = IORESOURCE_IRQ_HIGHLEVEL;
43 }
44 else {
Bob Moore50eca3e2005-09-30 19:03:00 -040045 if(polarity == ACPI_ACTIVE_LOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 flag = IORESOURCE_IRQ_LOWEDGE;
47 else
48 flag = IORESOURCE_IRQ_HIGHEDGE;
49 }
50 return flag;
51}
52
Bob Moore50eca3e2005-09-30 19:03:00 -040053static void decode_irq_flags(int flag, int *triggering, int *polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 switch (flag) {
56 case IORESOURCE_IRQ_LOWLEVEL:
Bob Moore50eca3e2005-09-30 19:03:00 -040057 *triggering = ACPI_LEVEL_SENSITIVE;
58 *polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 break;
60 case IORESOURCE_IRQ_HIGHLEVEL:
Bob Moore50eca3e2005-09-30 19:03:00 -040061 *triggering = ACPI_LEVEL_SENSITIVE;
62 *polarity = ACPI_ACTIVE_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 break;
64 case IORESOURCE_IRQ_LOWEDGE:
Bob Moore50eca3e2005-09-30 19:03:00 -040065 *triggering = ACPI_EDGE_SENSITIVE;
66 *polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 break;
68 case IORESOURCE_IRQ_HIGHEDGE:
Bob Moore50eca3e2005-09-30 19:03:00 -040069 *triggering = ACPI_EDGE_SENSITIVE;
70 *polarity = ACPI_ACTIVE_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 break;
72 }
73}
74
75static void
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -040076pnpacpi_parse_allocated_irqresource(struct pnp_resource_table * res, u32 gsi,
Bob Moore50eca3e2005-09-30 19:03:00 -040077 int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 int i = 0;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -040080 int irq;
81
82 if (!valid_IRQ(gsi))
83 return;
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) &&
86 i < PNP_MAX_IRQ)
87 i++;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -040088 if (i >= PNP_MAX_IRQ)
89 return;
90
91 res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag
Bob Moore50eca3e2005-09-30 19:03:00 -040092 irq = acpi_register_gsi(gsi, triggering, polarity);
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -040093 if (irq < 0) {
94 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
95 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -040097
98 res->irq_resource[i].start = irq;
99 res->irq_resource[i].end = irq;
100 pcibios_penalize_isa_irq(irq, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103static void
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400104pnpacpi_parse_allocated_dmaresource(struct pnp_resource_table * res, u32 dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 int i = 0;
Adrian Bunk0dec63b2005-05-01 08:59:30 -0700107 while (i < PNP_MAX_DMA &&
108 !(res->dma_resource[i].flags & IORESOURCE_UNSET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 i++;
110 if (i < PNP_MAX_DMA) {
111 res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
112 if (dma == -1) {
113 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
114 return;
115 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400116 res->dma_resource[i].start = dma;
117 res->dma_resource[i].end = dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119}
120
121static void
122pnpacpi_parse_allocated_ioresource(struct pnp_resource_table * res,
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400123 u32 io, u32 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 int i = 0;
126 while (!(res->port_resource[i].flags & IORESOURCE_UNSET) &&
127 i < PNP_MAX_PORT)
128 i++;
129 if (i < PNP_MAX_PORT) {
130 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
131 if (len <= 0 || (io + len -1) >= 0x10003) {
132 res->port_resource[i].flags |= IORESOURCE_DISABLED;
133 return;
134 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400135 res->port_resource[i].start = io;
136 res->port_resource[i].end = io + len - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138}
139
140static void
141pnpacpi_parse_allocated_memresource(struct pnp_resource_table * res,
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400142 u64 mem, u64 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 int i = 0;
145 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) &&
146 (i < PNP_MAX_MEM))
147 i++;
148 if (i < PNP_MAX_MEM) {
149 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
150 if (len <= 0) {
151 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
152 return;
153 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400154 res->mem_resource[i].start = mem;
155 res->mem_resource[i].end = mem + len - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157}
158
159
160static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
161 void *data)
162{
163 struct pnp_resource_table * res_table = (struct pnp_resource_table *)data;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400164 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Len Browneca008c2005-09-22 00:25:18 -0400166 switch (res->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400167 case ACPI_RESOURCE_TYPE_IRQ:
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400168 /*
169 * Per spec, only one interrupt per descriptor is allowed in
170 * _CRS, but some firmware violates this, so parse them all.
171 */
Bob Moore50eca3e2005-09-30 19:03:00 -0400172 for (i = 0; i < res->data.irq.interrupt_count; i++) {
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400173 pnpacpi_parse_allocated_irqresource(res_table,
174 res->data.irq.interrupts[i],
Bob Moore50eca3e2005-09-30 19:03:00 -0400175 res->data.irq.triggering,
176 res->data.irq.polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178 break;
179
Bob Moore50eca3e2005-09-30 19:03:00 -0400180 case ACPI_RESOURCE_TYPE_DMA:
181 if (res->data.dma.channel_count > 0)
182 pnpacpi_parse_allocated_dmaresource(res_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 res->data.dma.channels[0]);
184 break;
Len Brown0af58532006-01-20 01:11:37 -0500185
Bob Moore50eca3e2005-09-30 19:03:00 -0400186 case ACPI_RESOURCE_TYPE_IO:
187 pnpacpi_parse_allocated_ioresource(res_table,
188 res->data.io.minimum,
189 res->data.io.address_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 break;
Len Brown0af58532006-01-20 01:11:37 -0500191
192 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
193 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
194 break;
195
Bob Moore50eca3e2005-09-30 19:03:00 -0400196 case ACPI_RESOURCE_TYPE_FIXED_IO:
197 pnpacpi_parse_allocated_ioresource(res_table,
198 res->data.fixed_io.address,
199 res->data.fixed_io.address_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 break;
Len Brown0af58532006-01-20 01:11:37 -0500201
202 case ACPI_RESOURCE_TYPE_VENDOR:
203 break;
204
205 case ACPI_RESOURCE_TYPE_END_TAG:
206 break;
207
Bob Moore50eca3e2005-09-30 19:03:00 -0400208 case ACPI_RESOURCE_TYPE_MEMORY24:
209 pnpacpi_parse_allocated_memresource(res_table,
210 res->data.memory24.minimum,
211 res->data.memory24.address_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400213 case ACPI_RESOURCE_TYPE_MEMORY32:
214 pnpacpi_parse_allocated_memresource(res_table,
215 res->data.memory32.minimum,
216 res->data.memory32.address_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400218 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
219 pnpacpi_parse_allocated_memresource(res_table,
220 res->data.fixed_memory32.address,
221 res->data.fixed_memory32.address_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400223 case ACPI_RESOURCE_TYPE_ADDRESS16:
224 pnpacpi_parse_allocated_memresource(res_table,
225 res->data.address16.minimum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 res->data.address16.address_length);
227 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400228 case ACPI_RESOURCE_TYPE_ADDRESS32:
229 pnpacpi_parse_allocated_memresource(res_table,
230 res->data.address32.minimum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 res->data.address32.address_length);
232 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400233 case ACPI_RESOURCE_TYPE_ADDRESS64:
234 pnpacpi_parse_allocated_memresource(res_table,
235 res->data.address64.minimum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 res->data.address64.address_length);
237 break;
Len Brown0af58532006-01-20 01:11:37 -0500238
239 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 break;
Len Brown0af58532006-01-20 01:11:37 -0500241
242 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
243 for (i = 0; i < res->data.extended_irq.interrupt_count; i++) {
244 pnpacpi_parse_allocated_irqresource(res_table,
245 res->data.extended_irq.interrupts[i],
246 res->data.extended_irq.triggering,
247 res->data.extended_irq.polarity);
248 }
249 break;
250
251 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
252 break;
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 default:
Len Browneca008c2005-09-22 00:25:18 -0400255 pnp_warn("PnPACPI: unknown resource type %d", res->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return AE_ERROR;
257 }
258
259 return AE_OK;
260}
261
262acpi_status pnpacpi_parse_allocated_resource(acpi_handle handle, struct pnp_resource_table * res)
263{
264 /* Blank the resource table values */
265 pnp_init_resource_table(res);
266
267 return acpi_walk_resources(handle, METHOD_NAME__CRS, pnpacpi_allocated_resource, res);
268}
269
270static void pnpacpi_parse_dma_option(struct pnp_option *option, struct acpi_resource_dma *p)
271{
272 int i;
273 struct pnp_dma * dma;
274
Bob Moore50eca3e2005-09-30 19:03:00 -0400275 if (p->channel_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700277 dma = kcalloc(1, sizeof(struct pnp_dma), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (!dma)
279 return;
280
Bob Moore50eca3e2005-09-30 19:03:00 -0400281 for(i = 0; i < p->channel_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 dma->map |= 1 << p->channels[i];
283 dma->flags = 0;
284 if (p->bus_master)
285 dma->flags |= IORESOURCE_DMA_MASTER;
286 switch (p->type) {
287 case ACPI_COMPATIBILITY:
288 dma->flags |= IORESOURCE_DMA_COMPATIBLE;
289 break;
290 case ACPI_TYPE_A:
291 dma->flags |= IORESOURCE_DMA_TYPEA;
292 break;
293 case ACPI_TYPE_B:
294 dma->flags |= IORESOURCE_DMA_TYPEB;
295 break;
296 case ACPI_TYPE_F:
297 dma->flags |= IORESOURCE_DMA_TYPEF;
298 break;
299 default:
300 /* Set a default value ? */
301 dma->flags |= IORESOURCE_DMA_COMPATIBLE;
302 pnp_err("Invalid DMA type");
303 }
304 switch (p->transfer) {
305 case ACPI_TRANSFER_8:
306 dma->flags |= IORESOURCE_DMA_8BIT;
307 break;
308 case ACPI_TRANSFER_8_16:
309 dma->flags |= IORESOURCE_DMA_8AND16BIT;
310 break;
311 case ACPI_TRANSFER_16:
312 dma->flags |= IORESOURCE_DMA_16BIT;
313 break;
314 default:
315 /* Set a default value ? */
316 dma->flags |= IORESOURCE_DMA_8AND16BIT;
317 pnp_err("Invalid DMA transfer type");
318 }
319
320 pnp_register_dma_resource(option,dma);
321 return;
322}
323
324
325static void pnpacpi_parse_irq_option(struct pnp_option *option,
326 struct acpi_resource_irq *p)
327{
328 int i;
329 struct pnp_irq * irq;
330
Bob Moore50eca3e2005-09-30 19:03:00 -0400331 if (p->interrupt_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700333 irq = kcalloc(1, sizeof(struct pnp_irq), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (!irq)
335 return;
336
Bob Moore50eca3e2005-09-30 19:03:00 -0400337 for(i = 0; i < p->interrupt_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (p->interrupts[i])
339 __set_bit(p->interrupts[i], irq->map);
Bob Moore50eca3e2005-09-30 19:03:00 -0400340 irq->flags = irq_flags(p->triggering, p->polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 pnp_register_irq_resource(option, irq);
343 return;
344}
345
346static void pnpacpi_parse_ext_irq_option(struct pnp_option *option,
Bob Moore50eca3e2005-09-30 19:03:00 -0400347 struct acpi_resource_extended_irq *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 int i;
350 struct pnp_irq * irq;
351
Bob Moore50eca3e2005-09-30 19:03:00 -0400352 if (p->interrupt_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700354 irq = kcalloc(1, sizeof(struct pnp_irq), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (!irq)
356 return;
357
Bob Moore50eca3e2005-09-30 19:03:00 -0400358 for(i = 0; i < p->interrupt_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (p->interrupts[i])
360 __set_bit(p->interrupts[i], irq->map);
Bob Moore50eca3e2005-09-30 19:03:00 -0400361 irq->flags = irq_flags(p->triggering, p->polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 pnp_register_irq_resource(option, irq);
364 return;
365}
366
367static void
368pnpacpi_parse_port_option(struct pnp_option *option,
369 struct acpi_resource_io *io)
370{
371 struct pnp_port * port;
372
Bob Moore50eca3e2005-09-30 19:03:00 -0400373 if (io->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700375 port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (!port)
377 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400378 port->min = io->minimum;
379 port->max = io->maximum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 port->align = io->alignment;
Bob Moore50eca3e2005-09-30 19:03:00 -0400381 port->size = io->address_length;
382 port->flags = ACPI_DECODE_16 == io->io_decode ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 PNP_PORT_FLAG_16BITADDR : 0;
384 pnp_register_port_resource(option,port);
385 return;
386}
387
388static void
389pnpacpi_parse_fixed_port_option(struct pnp_option *option,
390 struct acpi_resource_fixed_io *io)
391{
392 struct pnp_port * port;
393
Bob Moore50eca3e2005-09-30 19:03:00 -0400394 if (io->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700396 port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!port)
398 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400399 port->min = port->max = io->address;
400 port->size = io->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 port->align = 0;
402 port->flags = PNP_PORT_FLAG_FIXED;
403 pnp_register_port_resource(option,port);
404 return;
405}
406
407static void
408pnpacpi_parse_mem24_option(struct pnp_option *option,
Bob Moore50eca3e2005-09-30 19:03:00 -0400409 struct acpi_resource_memory24 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 struct pnp_mem * mem;
412
Bob Moore50eca3e2005-09-30 19:03:00 -0400413 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700415 mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (!mem)
417 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400418 mem->min = p->minimum;
419 mem->max = p->maximum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 mem->align = p->alignment;
Bob Moore50eca3e2005-09-30 19:03:00 -0400421 mem->size = p->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Bob Moore08978312005-10-21 00:00:00 -0400423 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 IORESOURCE_MEM_WRITEABLE : 0;
425
426 pnp_register_mem_resource(option,mem);
427 return;
428}
429
430static void
431pnpacpi_parse_mem32_option(struct pnp_option *option,
Bob Moore50eca3e2005-09-30 19:03:00 -0400432 struct acpi_resource_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct pnp_mem * mem;
435
Bob Moore50eca3e2005-09-30 19:03:00 -0400436 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700438 mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (!mem)
440 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400441 mem->min = p->minimum;
442 mem->max = p->maximum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 mem->align = p->alignment;
Bob Moore50eca3e2005-09-30 19:03:00 -0400444 mem->size = p->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Bob Moore08978312005-10-21 00:00:00 -0400446 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 IORESOURCE_MEM_WRITEABLE : 0;
448
449 pnp_register_mem_resource(option,mem);
450 return;
451}
452
453static void
454pnpacpi_parse_fixed_mem32_option(struct pnp_option *option,
Bob Moore50eca3e2005-09-30 19:03:00 -0400455 struct acpi_resource_fixed_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 struct pnp_mem * mem;
458
Bob Moore50eca3e2005-09-30 19:03:00 -0400459 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700461 mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (!mem)
463 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400464 mem->min = mem->max = p->address;
465 mem->size = p->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 mem->align = 0;
467
Bob Moore08978312005-10-21 00:00:00 -0400468 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 IORESOURCE_MEM_WRITEABLE : 0;
470
471 pnp_register_mem_resource(option,mem);
472 return;
473}
474
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400475static void
476pnpacpi_parse_address_option(struct pnp_option *option, struct acpi_resource *r)
477{
478 struct acpi_resource_address64 addr, *p = &addr;
479 acpi_status status;
480 struct pnp_mem * mem;
481 struct pnp_port * port;
482
483 status = acpi_resource_to_address64(r, p);
484 if (!ACPI_SUCCESS(status)) {
Len Browned03f432006-01-07 03:50:18 -0500485 pnp_warn("PnPACPI: failed to convert resource type %d", r->type);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400486 return;
487 }
488
489 if (p->address_length == 0)
490 return;
491
492 if (p->resource_type == ACPI_MEMORY_RANGE) {
493 mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
494 if (!mem)
495 return;
Len Browned03f432006-01-07 03:50:18 -0500496 mem->min = mem->max = p->minimum;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400497 mem->size = p->address_length;
498 mem->align = 0;
Len Browned03f432006-01-07 03:50:18 -0500499 mem->flags = (p->info.mem.write_protect ==
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400500 ACPI_READ_WRITE_MEMORY) ? IORESOURCE_MEM_WRITEABLE : 0;
501 pnp_register_mem_resource(option,mem);
502 } else if (p->resource_type == ACPI_IO_RANGE) {
503 port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
504 if (!port)
505 return;
Len Browned03f432006-01-07 03:50:18 -0500506 port->min = port->max = p->minimum;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400507 port->size = p->address_length;
508 port->align = 0;
509 port->flags = PNP_PORT_FLAG_FIXED;
510 pnp_register_port_resource(option,port);
511 }
512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514struct acpipnp_parse_option_s {
515 struct pnp_option *option;
Matthieu Castetb008b8d2005-03-25 12:03:15 -0500516 struct pnp_option *option_independent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 struct pnp_dev *dev;
518};
519
Bob Moore50eca3e2005-09-30 19:03:00 -0400520static acpi_status pnpacpi_option_resource(struct acpi_resource *res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 void *data)
522{
523 int priority = 0;
524 struct acpipnp_parse_option_s *parse_data = (struct acpipnp_parse_option_s *)data;
525 struct pnp_dev *dev = parse_data->dev;
526 struct pnp_option *option = parse_data->option;
527
Len Browneca008c2005-09-22 00:25:18 -0400528 switch (res->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400529 case ACPI_RESOURCE_TYPE_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 pnpacpi_parse_irq_option(option, &res->data.irq);
531 break;
Len Brown0af58532006-01-20 01:11:37 -0500532
Bob Moore50eca3e2005-09-30 19:03:00 -0400533 case ACPI_RESOURCE_TYPE_DMA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 pnpacpi_parse_dma_option(option, &res->data.dma);
535 break;
Len Brown0af58532006-01-20 01:11:37 -0500536
Bob Moore50eca3e2005-09-30 19:03:00 -0400537 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 switch (res->data.start_dpf.compatibility_priority) {
539 case ACPI_GOOD_CONFIGURATION:
540 priority = PNP_RES_PRIORITY_PREFERRED;
541 break;
542
543 case ACPI_ACCEPTABLE_CONFIGURATION:
544 priority = PNP_RES_PRIORITY_ACCEPTABLE;
545 break;
546
547 case ACPI_SUB_OPTIMAL_CONFIGURATION:
548 priority = PNP_RES_PRIORITY_FUNCTIONAL;
549 break;
550 default:
551 priority = PNP_RES_PRIORITY_INVALID;
552 break;
553 }
554 /* TBD: Considering performace/robustness bits */
555 option = pnp_register_dependent_option(dev, priority);
556 if (!option)
557 return AE_ERROR;
558 parse_data->option = option;
559 break;
Len Brown0af58532006-01-20 01:11:37 -0500560
Bob Moore50eca3e2005-09-30 19:03:00 -0400561 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Matthieu Castetb008b8d2005-03-25 12:03:15 -0500562 /*only one EndDependentFn is allowed*/
563 if (!parse_data->option_independent) {
564 pnp_warn("PnPACPI: more than one EndDependentFn");
565 return AE_ERROR;
566 }
567 parse_data->option = parse_data->option_independent;
568 parse_data->option_independent = NULL;
569 break;
Len Brown0af58532006-01-20 01:11:37 -0500570
571 case ACPI_RESOURCE_TYPE_IO:
572 pnpacpi_parse_port_option(option, &res->data.io);
573 break;
574
575 case ACPI_RESOURCE_TYPE_FIXED_IO:
576 pnpacpi_parse_fixed_port_option(option,
577 &res->data.fixed_io);
578 break;
579
580 case ACPI_RESOURCE_TYPE_VENDOR:
581 case ACPI_RESOURCE_TYPE_END_TAG:
582 break;
583
584 case ACPI_RESOURCE_TYPE_MEMORY24:
585 pnpacpi_parse_mem24_option(option, &res->data.memory24);
586 break;
587
588 case ACPI_RESOURCE_TYPE_MEMORY32:
589 pnpacpi_parse_mem32_option(option, &res->data.memory32);
590 break;
591
592 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
593 pnpacpi_parse_fixed_mem32_option(option,
594 &res->data.fixed_memory32);
595 break;
596
597 case ACPI_RESOURCE_TYPE_ADDRESS16:
598 case ACPI_RESOURCE_TYPE_ADDRESS32:
599 case ACPI_RESOURCE_TYPE_ADDRESS64:
600 pnpacpi_parse_address_option(option, res);
601 break;
602
603 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
604 break;
605
606 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
607 pnpacpi_parse_ext_irq_option(option,
608 &res->data.extended_irq);
609 break;
610
611 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
612 break;
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 default:
Len Browneca008c2005-09-22 00:25:18 -0400615 pnp_warn("PnPACPI: unknown resource type %d", res->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return AE_ERROR;
617 }
618
619 return AE_OK;
620}
621
Bob Moore50eca3e2005-09-30 19:03:00 -0400622acpi_status pnpacpi_parse_resource_option_data(acpi_handle handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct pnp_dev *dev)
624{
625 acpi_status status;
626 struct acpipnp_parse_option_s parse_data;
627
628 parse_data.option = pnp_register_independent_option(dev);
629 if (!parse_data.option)
630 return AE_ERROR;
Matthieu Castetb008b8d2005-03-25 12:03:15 -0500631 parse_data.option_independent = parse_data.option;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 parse_data.dev = dev;
Bob Moore50eca3e2005-09-30 19:03:00 -0400633 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 pnpacpi_option_resource, &parse_data);
635
636 return status;
637}
638
639/*
640 * Set resource
641 */
642static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
643 void *data)
644{
645 int *res_cnt = (int *)data;
Len Browneca008c2005-09-22 00:25:18 -0400646 switch (res->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400647 case ACPI_RESOURCE_TYPE_IRQ:
Bob Moore50eca3e2005-09-30 19:03:00 -0400648 case ACPI_RESOURCE_TYPE_DMA:
649 case ACPI_RESOURCE_TYPE_IO:
650 case ACPI_RESOURCE_TYPE_FIXED_IO:
651 case ACPI_RESOURCE_TYPE_MEMORY24:
652 case ACPI_RESOURCE_TYPE_MEMORY32:
653 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Bob Moore50eca3e2005-09-30 19:03:00 -0400654 case ACPI_RESOURCE_TYPE_ADDRESS16:
655 case ACPI_RESOURCE_TYPE_ADDRESS32:
656 case ACPI_RESOURCE_TYPE_ADDRESS64:
Len Brown0af58532006-01-20 01:11:37 -0500657 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 (*res_cnt) ++;
Len Brown0af58532006-01-20 01:11:37 -0500659 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
660 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
661 case ACPI_RESOURCE_TYPE_VENDOR:
662 case ACPI_RESOURCE_TYPE_END_TAG:
663 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 default:
665 return AE_OK;
666 }
667 return AE_OK;
668}
669
670static acpi_status pnpacpi_type_resources(struct acpi_resource *res,
671 void *data)
672{
673 struct acpi_resource **resource = (struct acpi_resource **)data;
Len Browneca008c2005-09-22 00:25:18 -0400674 switch (res->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400675 case ACPI_RESOURCE_TYPE_IRQ:
Bob Moore50eca3e2005-09-30 19:03:00 -0400676 case ACPI_RESOURCE_TYPE_DMA:
677 case ACPI_RESOURCE_TYPE_IO:
678 case ACPI_RESOURCE_TYPE_FIXED_IO:
679 case ACPI_RESOURCE_TYPE_MEMORY24:
680 case ACPI_RESOURCE_TYPE_MEMORY32:
681 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Bob Moore50eca3e2005-09-30 19:03:00 -0400682 case ACPI_RESOURCE_TYPE_ADDRESS16:
683 case ACPI_RESOURCE_TYPE_ADDRESS32:
684 case ACPI_RESOURCE_TYPE_ADDRESS64:
Len Brown0af58532006-01-20 01:11:37 -0500685 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Len Browneca008c2005-09-22 00:25:18 -0400686 (*resource)->type = res->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 (*resource)++;
Len Brown0af58532006-01-20 01:11:37 -0500688 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
689 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
690 case ACPI_RESOURCE_TYPE_VENDOR:
691 case ACPI_RESOURCE_TYPE_END_TAG:
692 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 default:
694 return AE_OK;
695 }
696
697 return AE_OK;
698}
699
Bob Moore50eca3e2005-09-30 19:03:00 -0400700int pnpacpi_build_resource_template(acpi_handle handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct acpi_buffer *buffer)
702{
703 struct acpi_resource *resource;
704 int res_cnt = 0;
705 acpi_status status;
706
Bob Moore50eca3e2005-09-30 19:03:00 -0400707 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 pnpacpi_count_resources, &res_cnt);
709 if (ACPI_FAILURE(status)) {
710 pnp_err("Evaluate _CRS failed");
711 return -EINVAL;
712 }
713 if (!res_cnt)
714 return -EINVAL;
715 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700716 buffer->pointer = kcalloc(1, buffer->length - 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (!buffer->pointer)
718 return -ENOMEM;
719 pnp_dbg("Res cnt %d", res_cnt);
720 resource = (struct acpi_resource *)buffer->pointer;
Bob Moore50eca3e2005-09-30 19:03:00 -0400721 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 pnpacpi_type_resources, &resource);
723 if (ACPI_FAILURE(status)) {
724 kfree(buffer->pointer);
725 pnp_err("Evaluate _CRS failed");
726 return -EINVAL;
727 }
728 /* resource will pointer the end resource now */
Bob Moore50eca3e2005-09-30 19:03:00 -0400729 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 return 0;
732}
733
Bob Moore50eca3e2005-09-30 19:03:00 -0400734static void pnpacpi_encode_irq(struct acpi_resource *resource,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct resource *p)
736{
Bob Moore50eca3e2005-09-30 19:03:00 -0400737 int triggering, polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Bob Moore50eca3e2005-09-30 19:03:00 -0400739 decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering,
740 &polarity);
741 resource->type = ACPI_RESOURCE_TYPE_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 resource->length = sizeof(struct acpi_resource);
Bob Moore50eca3e2005-09-30 19:03:00 -0400743 resource->data.irq.triggering = triggering;
744 resource->data.irq.polarity = polarity;
745 if (triggering == ACPI_EDGE_SENSITIVE)
746 resource->data.irq.sharable = ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 else
Bob Moore50eca3e2005-09-30 19:03:00 -0400748 resource->data.irq.sharable = ACPI_SHARED;
749 resource->data.irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 resource->data.irq.interrupts[0] = p->start;
751}
752
753static void pnpacpi_encode_ext_irq(struct acpi_resource *resource,
754 struct resource *p)
755{
Bob Moore50eca3e2005-09-30 19:03:00 -0400756 int triggering, polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Bob Moore50eca3e2005-09-30 19:03:00 -0400758 decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering,
759 &polarity);
760 resource->type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 resource->length = sizeof(struct acpi_resource);
762 resource->data.extended_irq.producer_consumer = ACPI_CONSUMER;
Bob Moore50eca3e2005-09-30 19:03:00 -0400763 resource->data.extended_irq.triggering = triggering;
764 resource->data.extended_irq.polarity = polarity;
765 if (triggering == ACPI_EDGE_SENSITIVE)
766 resource->data.irq.sharable = ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 else
Bob Moore50eca3e2005-09-30 19:03:00 -0400768 resource->data.irq.sharable = ACPI_SHARED;
769 resource->data.extended_irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 resource->data.extended_irq.interrupts[0] = p->start;
771}
772
773static void pnpacpi_encode_dma(struct acpi_resource *resource,
774 struct resource *p)
775{
Bob Moore50eca3e2005-09-30 19:03:00 -0400776 resource->type = ACPI_RESOURCE_TYPE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 resource->length = sizeof(struct acpi_resource);
778 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
779 if (p->flags & IORESOURCE_DMA_COMPATIBLE)
780 resource->data.dma.type = ACPI_COMPATIBILITY;
781 else if (p->flags & IORESOURCE_DMA_TYPEA)
782 resource->data.dma.type = ACPI_TYPE_A;
783 else if (p->flags & IORESOURCE_DMA_TYPEB)
784 resource->data.dma.type = ACPI_TYPE_B;
785 else if (p->flags & IORESOURCE_DMA_TYPEF)
786 resource->data.dma.type = ACPI_TYPE_F;
787 if (p->flags & IORESOURCE_DMA_8BIT)
788 resource->data.dma.transfer = ACPI_TRANSFER_8;
789 else if (p->flags & IORESOURCE_DMA_8AND16BIT)
790 resource->data.dma.transfer = ACPI_TRANSFER_8_16;
791 else if (p->flags & IORESOURCE_DMA_16BIT)
792 resource->data.dma.transfer = ACPI_TRANSFER_16;
793 resource->data.dma.bus_master = p->flags & IORESOURCE_DMA_MASTER;
Bob Moore50eca3e2005-09-30 19:03:00 -0400794 resource->data.dma.channel_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 resource->data.dma.channels[0] = p->start;
796}
797
798static void pnpacpi_encode_io(struct acpi_resource *resource,
799 struct resource *p)
800{
Bob Moore50eca3e2005-09-30 19:03:00 -0400801 resource->type = ACPI_RESOURCE_TYPE_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 resource->length = sizeof(struct acpi_resource);
803 /* Note: pnp_assign_port will copy pnp_port->flags into p->flags */
804 resource->data.io.io_decode = (p->flags & PNP_PORT_FLAG_16BITADDR)?
Bob Moore50eca3e2005-09-30 19:03:00 -0400805 ACPI_DECODE_16 : ACPI_DECODE_10;
806 resource->data.io.minimum = p->start;
807 resource->data.io.maximum = p->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 resource->data.io.alignment = 0; /* Correct? */
Bob Moore50eca3e2005-09-30 19:03:00 -0400809 resource->data.io.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812static void pnpacpi_encode_fixed_io(struct acpi_resource *resource,
813 struct resource *p)
814{
Bob Moore50eca3e2005-09-30 19:03:00 -0400815 resource->type = ACPI_RESOURCE_TYPE_FIXED_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 resource->length = sizeof(struct acpi_resource);
Bob Moore50eca3e2005-09-30 19:03:00 -0400817 resource->data.fixed_io.address = p->start;
818 resource->data.fixed_io.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
821static void pnpacpi_encode_mem24(struct acpi_resource *resource,
822 struct resource *p)
823{
Bob Moore50eca3e2005-09-30 19:03:00 -0400824 resource->type = ACPI_RESOURCE_TYPE_MEMORY24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 resource->length = sizeof(struct acpi_resource);
826 /* Note: pnp_assign_mem will copy pnp_mem->flags into p->flags */
Bob Moore08978312005-10-21 00:00:00 -0400827 resource->data.memory24.write_protect =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
829 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
Bob Moore50eca3e2005-09-30 19:03:00 -0400830 resource->data.memory24.minimum = p->start;
831 resource->data.memory24.maximum = p->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 resource->data.memory24.alignment = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400833 resource->data.memory24.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
836static void pnpacpi_encode_mem32(struct acpi_resource *resource,
837 struct resource *p)
838{
Bob Moore50eca3e2005-09-30 19:03:00 -0400839 resource->type = ACPI_RESOURCE_TYPE_MEMORY32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 resource->length = sizeof(struct acpi_resource);
Bob Moore08978312005-10-21 00:00:00 -0400841 resource->data.memory32.write_protect =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
843 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
Bob Moore50eca3e2005-09-30 19:03:00 -0400844 resource->data.memory32.minimum = p->start;
845 resource->data.memory32.maximum = p->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 resource->data.memory32.alignment = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400847 resource->data.memory32.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850static void pnpacpi_encode_fixed_mem32(struct acpi_resource *resource,
851 struct resource *p)
852{
Bob Moore50eca3e2005-09-30 19:03:00 -0400853 resource->type = ACPI_RESOURCE_TYPE_FIXED_MEMORY32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 resource->length = sizeof(struct acpi_resource);
Bob Moore08978312005-10-21 00:00:00 -0400855 resource->data.fixed_memory32.write_protect =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
857 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
Bob Moore50eca3e2005-09-30 19:03:00 -0400858 resource->data.fixed_memory32.address = p->start;
859 resource->data.fixed_memory32.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Bob Moore50eca3e2005-09-30 19:03:00 -0400862int pnpacpi_encode_resources(struct pnp_resource_table *res_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 struct acpi_buffer *buffer)
864{
865 int i = 0;
866 /* pnpacpi_build_resource_template allocates extra mem */
867 int res_cnt = (buffer->length - 1)/sizeof(struct acpi_resource) - 1;
868 struct acpi_resource *resource = (struct acpi_resource*)buffer->pointer;
869 int port = 0, irq = 0, dma = 0, mem = 0;
870
871 pnp_dbg("res cnt %d", res_cnt);
872 while (i < res_cnt) {
Len Browneca008c2005-09-22 00:25:18 -0400873 switch(resource->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400874 case ACPI_RESOURCE_TYPE_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 pnp_dbg("Encode irq");
Bob Moore50eca3e2005-09-30 19:03:00 -0400876 pnpacpi_encode_irq(resource,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 &res_table->irq_resource[irq]);
878 irq++;
879 break;
880
Bob Moore50eca3e2005-09-30 19:03:00 -0400881 case ACPI_RESOURCE_TYPE_DMA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 pnp_dbg("Encode dma");
Bob Moore50eca3e2005-09-30 19:03:00 -0400883 pnpacpi_encode_dma(resource,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 &res_table->dma_resource[dma]);
885 dma ++;
886 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400887 case ACPI_RESOURCE_TYPE_IO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 pnp_dbg("Encode io");
Bob Moore50eca3e2005-09-30 19:03:00 -0400889 pnpacpi_encode_io(resource,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 &res_table->port_resource[port]);
891 port ++;
892 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400893 case ACPI_RESOURCE_TYPE_FIXED_IO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 pnp_dbg("Encode fixed io");
895 pnpacpi_encode_fixed_io(resource,
896 &res_table->port_resource[port]);
897 port ++;
898 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400899 case ACPI_RESOURCE_TYPE_MEMORY24:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 pnp_dbg("Encode mem24");
901 pnpacpi_encode_mem24(resource,
902 &res_table->mem_resource[mem]);
903 mem ++;
904 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400905 case ACPI_RESOURCE_TYPE_MEMORY32:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 pnp_dbg("Encode mem32");
907 pnpacpi_encode_mem32(resource,
908 &res_table->mem_resource[mem]);
909 mem ++;
910 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400911 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 pnp_dbg("Encode fixed mem32");
913 pnpacpi_encode_fixed_mem32(resource,
914 &res_table->mem_resource[mem]);
915 mem ++;
916 break;
Len Brown0af58532006-01-20 01:11:37 -0500917 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
918 pnp_dbg("Encode ext irq");
919 pnpacpi_encode_ext_irq(resource,
920 &res_table->irq_resource[irq]);
921 irq++;
922 break;
923 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
924 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
925 case ACPI_RESOURCE_TYPE_VENDOR:
926 case ACPI_RESOURCE_TYPE_END_TAG:
927 case ACPI_RESOURCE_TYPE_ADDRESS16:
928 case ACPI_RESOURCE_TYPE_ADDRESS32:
929 case ACPI_RESOURCE_TYPE_ADDRESS64:
930 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
931 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 default: /* other type */
Len Browneca008c2005-09-22 00:25:18 -0400933 pnp_warn("unknown resource type %d", resource->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return -EINVAL;
935 }
936 resource ++;
937 i ++;
938 }
939 return 0;
940}