Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
/*
 * resource.c - contains resource management algorithms
 *
 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@suse.cz>
 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
 *
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/kernel.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <asm/irq.h>
#include <linux/ioport.h>
#include <linux/config.h>
#include <linux/init.h>

#ifdef CONFIG_PNP_DEBUG
	#define DEBUG
#else
	#undef DEBUG
#endif

#include <linux/pnp.h>
#include "base.h"

int pnp_allow_dma0 = -1;		        /* allow dma 0 during auto activation: -1=off (:default), 0=off (set by user), 1=on */
int pnp_skip_pci_scan;				/* skip PCI resource scanning */
int pnp_reserve_irq[16] = { [0 ... 15] = -1 };	/* reserve (don't use) some IRQ */
int pnp_reserve_dma[8] = { [0 ... 7] = -1 };	/* reserve (don't use) some DMA */
int pnp_reserve_io[16] = { [0 ... 15] = -1 };	/* reserve (don't use) some I/O region */
int pnp_reserve_mem[16] = { [0 ... 15] = -1 };	/* reserve (don't use) some memory region */


/* resource information adding functions */

struct pnp_resources * pnp_build_resource(struct pnp_dev *dev, int dependent)
{
	struct pnp_resources *res, *ptr, *ptra;

	res = pnp_alloc(sizeof(struct pnp_resources));
	if (!res)
		return NULL;
	ptr = dev->res;
	if (ptr && ptr->dependent && dependent) { /* add to another list */
		ptra = ptr->dep;
		while (ptra && ptra->dep)
			ptra = ptra->dep;
		if (!ptra)
			ptr->dep = res;
		else
			ptra->dep = res;
	} else {
		if (!ptr){
			dev->res = res;
		}
		else{
			kfree(res);
			return NULL;
		}
	}
	if (dependent) {
		res->priority = dependent & 0xff;
		if (res->priority > PNP_RES_PRIORITY_FUNCTIONAL)
			res->priority = PNP_RES_PRIORITY_INVALID;
		res->dependent = 1;
	} else {
		res->priority = PNP_RES_PRIORITY_PREFERRED;
		res->dependent = 1;
	}
	return res;
}

struct pnp_resources * pnp_find_resources(struct pnp_dev *dev, int depnum)
{
	int i;
	struct pnp_resources *res;
	if (!dev)
		return NULL;
	res = dev->res;
	if (!res)
		return NULL;
	for (i = 0; i < depnum; i++)
	{
		if (res->dep)
			res = res->dep;
		else
			return NULL;
	}
	return res;
}

int pnp_get_max_depnum(struct pnp_dev *dev)
{
	int num = 0;
	struct pnp_resources *res;
	if (!dev)
		return -EINVAL;
	res = dev->res;
	if (!res)
		return -EINVAL;
	while (res->dep){
		res = res->dep;
		num++;
	}
	return num;
}

/*
 *  Add IRQ resource to resources list.
 */

int pnp_add_irq_resource(struct pnp_dev *dev, int depnum, struct pnp_irq *data)
{
	int i;
	struct pnp_resources *res;
	struct pnp_irq *ptr;
	res = pnp_find_resources(dev,depnum);
	if (!res)
		return -EINVAL;
	if (!data)
		return -EINVAL;
	ptr = res->irq;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
		ptr->next = data;
	else
		res->irq = data;
#ifdef CONFIG_PCI
	for (i=0; i<16; i++)
		if (data->map & (1<<i))
			pcibios_penalize_isa_irq(i);
#endif
	return 0;
}

/*
 *  Add DMA resource to resources list.
 */

int pnp_add_dma_resource(struct pnp_dev *dev, int depnum, struct pnp_dma *data)
{
	struct pnp_resources *res;
	struct pnp_dma *ptr;
	res = pnp_find_resources(dev,depnum);
	if (!res)
		return -EINVAL;
	if (!data)
		return -EINVAL;
	ptr = res->dma;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
		ptr->next = data;
	else
		res->dma = data;
	return 0;
}

/*
 *  Add port resource to resources list.
 */

int pnp_add_port_resource(struct pnp_dev *dev, int depnum, struct pnp_port *data)
{
	struct pnp_resources *res;
	struct pnp_port *ptr;
	res = pnp_find_resources(dev,depnum);
	if (res==NULL)
		return -EINVAL;
	if (!data)
		return -EINVAL;
	data->res = res;
	ptr = res->port;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
		ptr->next = data;
	else
		res->port = data;
	return 0;
}

/*
 *  Add memory resource to resources list.
 */

int pnp_add_mem_resource(struct pnp_dev *dev, int depnum, struct pnp_mem *data)
{
	struct pnp_resources *res;
	struct pnp_mem *ptr;
	res = pnp_find_resources(dev,depnum);
	if (!res)
		return -EINVAL;
	if (!data)
		return -EINVAL;
	ptr = res->mem;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
		ptr->next = data;
	else
		res->mem = data;
	return 0;
}

/*
 *  Add 32-bit memory resource to resources list.
 */

int pnp_add_mem32_resource(struct pnp_dev *dev, int depnum, struct pnp_mem32 *data)
{
	struct pnp_resources *res;
	struct pnp_mem32 *ptr;
	res = pnp_find_resources(dev,depnum);
	if (!res)
		return -EINVAL;
	if (!data)
		return -EINVAL;
	ptr = res->mem32;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
		ptr->next = data;
	else
		res->mem32 = data;
	return 0;
}


/* resource removing functions */

static void pnp_free_port(struct pnp_port *port)
{
	struct pnp_port *next;

	while (port) {
		next = port->next;
		kfree(port);
		port = next;
	}
}

static void pnp_free_irq(struct pnp_irq *irq)
{
	struct pnp_irq *next;

	while (irq) {
		next = irq->next;
		kfree(irq);
		irq = next;
	}
}

static void pnp_free_dma(struct pnp_dma *dma)
{
	struct pnp_dma *next;

	while (dma) {
		next = dma->next;
		kfree(dma);
		dma = next;
	}
}

static void pnp_free_mem(struct pnp_mem *mem)
{
	struct pnp_mem *next;

	while (mem) {
		next = mem->next;
		kfree(mem);
		mem = next;
	}
}

static void pnp_free_mem32(struct pnp_mem32 *mem32)
{
	struct pnp_mem32 *next;

	while (mem32) {
		next = mem32->next;
		kfree(mem32);
		mem32 = next;
	}
}

void pnp_free_resources(struct pnp_resources *resources)
{
	struct pnp_resources *next;

	while (resources) {
		next = resources->dep;
		pnp_free_port(resources->port);
		pnp_free_irq(resources->irq);
		pnp_free_dma(resources->dma);
		pnp_free_mem(resources->mem);
		pnp_free_mem32(resources->mem32);
		kfree(resources);
		resources = next;
	}
}


/* resource validity checking functions */

static int pnp_check_port(int port, int size, int idx, struct pnp_cfg *config)
{
	int i, tmp, rport, rsize;
	struct pnp_dev *dev;

	if (check_region(port, size))
		return 1;
	for (i = 0; i < 8; i++) {
		rport = pnp_reserve_io[i << 1];
		rsize = pnp_reserve_io[(i << 1) + 1];
		if (port >= rport && port < rport + rsize)
			return 1;
		if (port + size > rport && port + size < (rport + rsize) - 1)
			return 1;
	}

	pnp_for_each_dev(dev) {
		if (dev->active) {
			for (tmp = 0; tmp < 8; tmp++) {
				if (dev->resource[tmp].flags) {
					rport = dev->resource[tmp].start;
					rsize = (dev->resource[tmp].end - rport) + 1;
					if (port >= rport && port < rport + rsize)
						return 1;
					if (port + size > rport && port + size < (rport + rsize) - 1)
						return 1;
				}
			}
		}
	}
	for (tmp = 0; tmp < 8 && tmp != idx; tmp++) {
		if (dev->resource[tmp].flags) {
			rport = config->request.resource[tmp].start;
			rsize = (config->request.resource[tmp].end - rport) + 1;
			if (port >= rport && port < rport + rsize)
				return 1;
			if (port + size > rport && port + size < (rport + rsize) - 1)
				return 1;
		}
	}
	return 0;
}

static int pnp_check_mem(unsigned int addr, unsigned int size, int idx, struct pnp_cfg *config)
{
	int i, tmp;
	unsigned int raddr, rsize;
	struct pnp_dev *dev;

	for (i = 0; i < 8; i++) {
		raddr = (unsigned int)pnp_reserve_mem[i << 1];
		rsize = (unsigned int)pnp_reserve_mem[(i << 1) + 1];
		if (addr >= raddr && addr < raddr + rsize)
			return 1;
		if (addr + size > raddr && addr + size < (raddr + rsize) - 1)
			return 1;
		if (__check_region(&iomem_resource, addr, size))
			return 1;
	}
	pnp_for_each_dev(dev) {
		if (dev->active) {
			for (tmp = 0; tmp < 4; tmp++) {
				if (dev->resource[tmp + 8].flags) {
					raddr = dev->resource[tmp + 8].start;
					rsize = (dev->resource[tmp + 8].end - raddr) + 1;
					if (addr >= raddr && addr < raddr + rsize)
						return 1;
					if (addr + size > raddr && addr + size < (raddr + rsize) - 1)
						return 1;
				}
			}
		}
	}
	for (tmp = 0; tmp < 4 && tmp != idx; tmp++) {
		if (dev->resource[tmp + 8].flags) {
			raddr = config->request.resource[tmp + 8].start;
			rsize = (config->request.resource[tmp + 8].end - raddr) + 1;
			if (addr >= raddr && addr < raddr + rsize)
				return 1;
			if (addr + size > raddr && addr + size < (raddr + rsize) - 1)
				return 1;
		}
	}
	return 0;
}

static void pnp_test_handler(int irq, void *dev_id, struct pt_regs *regs)
{
}

static int pnp_check_interrupt(int irq, struct pnp_cfg *config)
{
	int i;
#ifdef CONFIG_PCI
	struct pci_dev *pci;
#endif
	struct pnp_dev *dev;
	if (!config)
		return 1;

	if (irq < 0 || irq > 15)
		return 1;
	for (i = 0; i < 16; i++) {
		if (pnp_reserve_irq[i] == irq)
			return 1;
	}
	pnp_for_each_dev(dev) {
		if (dev->active) {
			if ((dev->irq_resource[0].flags && dev->irq_resource[0].start == irq) ||
			    (dev->irq_resource[1].flags && dev->irq_resource[1].start == irq))
				return 1;
		}
	}
	if (config->request.irq_resource[0].flags && config->request.irq_resource[1].flags &&
	   (config->request.irq_resource[0].start == irq))
		return 1;
#ifdef CONFIG_PCI
	if (!pnp_skip_pci_scan) {
		pci_for_each_dev(pci) {
			if (pci->irq == irq)
				return 1;
		}
	}
#endif
	if (request_irq(irq, pnp_test_handler, SA_INTERRUPT, "pnp", NULL))
		return 1;
	free_irq(irq, NULL);
	return 0;
}

static int pnp_check_dma(int dma, struct pnp_cfg *config)
{
	int i, mindma = 1;
	struct pnp_dev *dev;
	if (!config)
		return 1;

	if (pnp_allow_dma0 == 1)
		mindma = 0;
	if (dma < mindma || dma == 4 || dma > 7)
		return 1;
	for (i = 0; i < 8; i++) {
		if (pnp_reserve_dma[i] == dma)
			return 1;
	}
	pnp_for_each_dev(dev) {
		if (dev->active) {
			if ((dev->dma_resource[0].flags && dev->dma_resource[0].start == dma) ||
			    (dev->dma_resource[1].flags && dev->dma_resource[1].start == dma))
				return 1;
		}
	}
	if (config->request.dma_resource[0].flags && config->request.dma_resource[1].flags &&
	   (config->request.dma_resource[0].start == dma))
		return 1;
	if (request_dma(dma, "pnp"))
		return 1;
	free_dma(dma);
	return 0;
}


/* config generation functions */
static int pnp_generate_port(struct pnp_cfg *config, int num)
{
	struct pnp_port *port;
	unsigned long *value1, *value2, *value3;
	if (!config || num < 0 || num > 7)
		return -EINVAL;
	port = config->port[num];
	if (!port)
		return 0;
	value1 = &config->request.resource[num].start;
	value2 = &config->request.resource[num].end;
	value3 = &config->request.resource[num].flags;
	*value1 = port->min;
	*value2 = *value1 + port->size -1;
	*value3 = port->flags | IORESOURCE_IO;
	while (pnp_check_port(*value1, port->size, num, config)) {
		*value1 += port->align;
		*value2 = *value1 + port->size - 1;
		if (*value1 > port->max || !port->align)
			return -ENOENT;
	}
	return 0;
}

static int pnp_generate_mem(struct pnp_cfg *config, int num)
{
	struct pnp_mem *mem;
	unsigned long *value1, *value2, *value3;
	if (!config || num < 0 || num > 3)
		return -EINVAL;
	mem = config->mem[num];
	if (!mem)
		return 0;
	value1 = &config->request.resource[num + 8].start;
	value2 = &config->request.resource[num + 8].end;
	value3 = &config->request.resource[num].flags;
	*value1 = mem->min;
	*value2 = *value1 + mem->size -1;
	*value3 = mem->flags | IORESOURCE_MEM;
	if (!(mem->flags & IORESOURCE_MEM_WRITEABLE))
		*value3 |= IORESOURCE_READONLY;
	if (mem->flags & IORESOURCE_MEM_CACHEABLE)
		*value3 |= IORESOURCE_CACHEABLE;
	if (mem->flags & IORESOURCE_MEM_RANGELENGTH)
		*value3 |= IORESOURCE_RANGELENGTH;
	if (mem->flags & IORESOURCE_MEM_SHADOWABLE)
		*value3 |= IORESOURCE_SHADOWABLE;
	while (pnp_check_mem(*value1, mem->size, num, config)) {
		*value1 += mem->align;
		*value2 = *value1 + mem->size - 1;
		if (*value1 > mem->max || !mem->align)
			return -ENOENT;
	}
	return 0;
}

static int pnp_generate_irq(struct pnp_cfg *config, int num)
{
	struct pnp_irq *irq;
	unsigned long *value1, *value2, *value3;
	/* IRQ priority: this table is good for i386 */
	static unsigned short xtab[16] = {
		5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
	};
	int i;
	if (!config || num < 0 || num > 1)
		return -EINVAL;
	irq = config->irq[num];
	if (!irq)
		return 0;
	value1 = &config->request.irq_resource[num].start;
	value2 = &config->request.irq_resource[num].end;
	value3 = &config->request.irq_resource[num].flags;
	*value3 = irq->flags | IORESOURCE_IRQ;

	for (i=0; i < 16; i++)
	{
		if(irq->map & (1<<xtab[i])) {
		*value1 = *value2 = xtab[i];
		if(pnp_check_interrupt(*value1,config)==0)
			return 0;
		}
	}
	return -ENOENT;
}

static int pnp_generate_dma(struct pnp_cfg *config, int num)
{
	struct pnp_dma *dma;
	unsigned long *value1, *value2, *value3;
	/* DMA priority: this table is good for i386 */
	static unsigned short xtab[16] = {
		1, 3, 5, 6, 7, 0, 2, 4
	};
	int i;
	if (!config || num < 0 || num > 1)
		return -EINVAL;
	dma = config->dma[num];
	if (!dma)
		return 0;
	value1 = &config->request.dma_resource[num].start;
	value2 = &config->request.dma_resource[num].end;
	value3 = &config->request.dma_resource[num].flags;
	*value3 = dma->flags | IORESOURCE_DMA;

	for (i=0; i < 8; i++)
	{
		if(dma->map & (1<<xtab[i])) {
		*value1 = *value2 = xtab[i];
		if(pnp_check_dma(*value1,config)==0)
			return 0;
		}
	}
	return -ENOENT;
}

static int pnp_prepare_request(struct pnp_cfg *config)
{
	struct pnp_dev *dev;
	int idx;
	if (!config)
		return -EINVAL;
	dev = &config->request;
	if (dev == NULL)
		return -EINVAL;
	if (dev->active || dev->ro)
		return -EBUSY;
	for (idx = 0; idx < DEVICE_COUNT_IRQ; idx++) {
		dev->irq_resource[idx].name = NULL;
		dev->irq_resource[idx].start = -1;
		dev->irq_resource[idx].end = -1;
		dev->irq_resource[idx].flags = 0;
	}
	for (idx = 0; idx < DEVICE_COUNT_DMA; idx++) {
		dev->dma_resource[idx].name = NULL;
		dev->dma_resource[idx].start = -1;
		dev->dma_resource[idx].end = -1;
		dev->dma_resource[idx].flags = 0;
	}
	for (idx = 0; idx < DEVICE_COUNT_RESOURCE; idx++) {
		dev->resource[idx].name = NULL;
		dev->resource[idx].start = 0;
		dev->resource[idx].end = 0;
		dev->resource[idx].flags = 0;
	}
	return 0;
}

static int pnp_generate_request(struct pnp_cfg *config)
{
	int i;
	if (!config)
		return -EINVAL;
	if (pnp_prepare_request<0)
		return -ENOENT;
	for (i=0; i<=7; i++)
	{
		if(pnp_generate_port(config,i)<0)
			return -ENOENT;
	}
	for (i=0; i<=3; i++)
	{
		if(pnp_generate_mem(config,i)<0)
			return -ENOENT;
	}
	for (i=0; i<=1; i++)
	{
		if(pnp_generate_irq(config,i)<0)
			return -ENOENT;
	}
	for (i=0; i<=1; i++)
	{
		if(pnp_generate_dma(config,i)<0)
			return -ENOENT;
	}
	return 0;
}



static struct pnp_cfg * pnp_generate_config(struct pnp_dev *dev, int depnum)
{
	struct pnp_cfg * config;
	int nport = 0, nirq = 0, ndma = 0, nmem = 0;
	struct pnp_resources * res;
	struct pnp_port * port;
	struct pnp_mem * mem;
	struct pnp_irq * irq;
	struct pnp_dma * dma;
	if (!dev)
		return NULL;
	if (depnum < 0)
		return NULL;
	config = pnp_alloc(sizeof(struct pnp_cfg));
	if (!config)
		return NULL;

	/* independent */
	res = pnp_find_resources(dev, 0);
	if (!res)
		goto fail;
	port = res->port;
	mem = res->mem;
	irq = res->irq;
	dma = res->dma;
	while (port){
		config->port[nport] = port;
		nport++;
		port = port->next;
	}
	while (mem){
		config->mem[nmem] = mem;
		nmem++;
		mem = mem->next;
	}
	while (irq){
		config->irq[nirq] = irq;
		nirq++;
		irq = irq->next;
	}
	while (dma){
		config->dma[ndma] = dma;
		ndma++;
		dma = dma->next;
	}

	/* dependent */
	if (depnum == 0)
		return config;
	res = pnp_find_resources(dev, depnum);
	if (!res)
		goto fail;
	port = res->port;
	mem = res->mem;
	irq = res->irq;
	dma = res->dma;
	while (port){
		config->port[nport] = port;
		nport++;
		port = port->next;
	}
	while (mem){
		config->mem[nmem] = mem;
		nmem++;
		mem = mem->next;
	}

	while (irq){
		config->irq[nirq] = irq;
		nirq++;
		irq = irq->next;
	}
	while (dma){
		config->dma[ndma] = dma;
		ndma++;
		dma = dma->next;
	}
	return config;
	
	fail:
	kfree(config);
	return NULL;
}

/* PnP Device Resource Management */

/**
 * pnp_activate_dev - activates a PnP device for use
 * @dev: pointer to the desired device
 *
 * finds the best resource configuration and then informs the correct pnp protocol
 */

int pnp_activate_dev(struct pnp_dev *dev)
{
	int depnum, max;
	struct pnp_cfg *config;
	if (!dev)
		return -EINVAL;
        max = pnp_get_max_depnum(dev);
	if (dev->active)
		return -EBUSY;
	if (dev->driver){
		printk(KERN_INFO "pnp: Automatic configuration failed because the PnP device '%s' is busy\n", dev->dev.bus_id);
		return -EINVAL;
	}
	if (!dev->protocol->get || !dev->protocol->set)
		return -EINVAL;
	if (max == 0)
		return 0;
	for (depnum=1; depnum <= max; depnum++)
	{
		config = pnp_generate_config(dev,depnum);
		if (!config)
			return -EINVAL;
		if (pnp_generate_request(config)==0)
			goto done;
		kfree(config);
	}
	printk(KERN_ERR "pnp: Automatic configuration failed for device '%s' due to resource conflicts\n", dev->dev.bus_id);
	return -ENOENT;

	done:
	pnp_dbg("the device '%s' has been activated", dev->dev.bus_id);
	dev->protocol->set(dev,config,0);
	if (dev->protocol->get)
		dev->protocol->get(dev);
	kfree(config);
	return 0;
}

/**
 * pnp_disable_dev - disables device
 * @dev: pointer to the desired device
 *
 * inform the correct pnp protocol so that resources can be used by other devices
 */

int pnp_disable_dev(struct pnp_dev *dev)
{
        if (!dev)
                return -EINVAL;
	if (dev->driver){
		printk(KERN_INFO "pnp: Disable failed becuase the PnP device '%s' is busy\n", dev->dev.bus_id);
		return -EINVAL;
	}
	if (!dev->protocol->disable || !dev->active)
		return -EINVAL;
	pnp_dbg("the device '%s' has been disabled", dev->dev.bus_id);
	return dev->protocol->disable(dev);
}

/**
 * pnp_raw_set_dev - same as pnp_activate_dev except the resource config can be specified
 * @dev: pointer to the desired device
 * @depnum: resource dependent function
 * @mode: static or dynamic
 *
 */

int pnp_raw_set_dev(struct pnp_dev *dev, int depnum, int mode)
{
	struct pnp_cfg *config;
	if (!dev)
		return -EINVAL;
        config = pnp_generate_config(dev,depnum);
	if (dev->driver){
		printk(KERN_INFO "pnp: Unable to set resources becuase the PnP device '%s' is busy\n", dev->dev.bus_id);
		return -EINVAL;
	}
	if (!dev->protocol->get || !dev->protocol->set)
		return -EINVAL;
	if (!config)
		return -EINVAL;
	if (pnp_generate_request(config)==0)
		goto done;
	kfree(config);
	printk(KERN_ERR "pnp: Manual configuration failed for device '%s' due to resource conflicts\n", dev->dev.bus_id);
	return -ENOENT;

	done:
	dev->protocol->set(dev,config,mode);
	if (dev->protocol->get)
		dev->protocol->get(dev);
	kfree(config);
	return 0;
}

EXPORT_SYMBOL(pnp_build_resource);
EXPORT_SYMBOL(pnp_find_resources);
EXPORT_SYMBOL(pnp_get_max_depnum);
EXPORT_SYMBOL(pnp_add_irq_resource);
EXPORT_SYMBOL(pnp_add_dma_resource);
EXPORT_SYMBOL(pnp_add_port_resource);
EXPORT_SYMBOL(pnp_add_mem_resource);
EXPORT_SYMBOL(pnp_add_mem32_resource);
EXPORT_SYMBOL(pnp_activate_dev);
EXPORT_SYMBOL(pnp_disable_dev);
EXPORT_SYMBOL(pnp_raw_set_dev);

/* format is: allowdma0 */

static int __init pnp_allowdma0(char *str)
{
        pnp_allow_dma0 = 1;
	return 1;
}

__setup("allowdma0", pnp_allowdma0);

/* format is: pnp_reserve_irq=irq1[,irq2] .... */

static int __init pnp_setup_reserve_irq(char *str)
{
	int i;

	for (i = 0; i < 16; i++)
		if (get_option(&str,&pnp_reserve_irq[i]) != 2)
			break;
	return 1;
}

__setup("pnp_reserve_irq=", pnp_setup_reserve_irq);

/* format is: pnp_reserve_dma=dma1[,dma2] .... */

static int __init pnp_setup_reserve_dma(char *str)
{
	int i;

	for (i = 0; i < 8; i++)
		if (get_option(&str,&pnp_reserve_dma[i]) != 2)
			break;
	return 1;
}

__setup("pnp_reserve_dma=", pnp_setup_reserve_dma);

/* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */

static int __init pnp_setup_reserve_io(char *str)
{
	int i;

	for (i = 0; i < 16; i++)
		if (get_option(&str,&pnp_reserve_io[i]) != 2)
			break;
	return 1;
}

__setup("pnp_reserve_io=", pnp_setup_reserve_io);

/* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */

static int __init pnp_setup_reserve_mem(char *str)
{
	int i;

	for (i = 0; i < 16; i++)
		if (get_option(&str,&pnp_reserve_mem[i]) != 2)
			break;
	return 1;
}

__setup("pnp_reserve_mem=", pnp_setup_reserve_mem);