Linux Audio

Check our new training course

Loading...
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * resource.c - Contains functions for registering and analyzing resource information
  4 *
  5 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
  6 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
  7 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
  8 *	Bjorn Helgaas <bjorn.helgaas@hp.com>
  9 */
 10
 11#include <linux/module.h>
 12#include <linux/slab.h>
 13#include <linux/errno.h>
 14#include <linux/interrupt.h>
 15#include <linux/kernel.h>
 16#include <asm/io.h>
 17#include <asm/dma.h>
 18#include <asm/irq.h>
 19#include <linux/pci.h>
 20#include <linux/ioport.h>
 21#include <linux/init.h>
 22
 23#include <linux/pnp.h>
 24#include "base.h"
 25
 26static int pnp_reserve_irq[16] = {[0 ... 15] = -1 };	/* reserve (don't use) some IRQ */
 27static int pnp_reserve_dma[8] = {[0 ... 7] = -1 };	/* reserve (don't use) some DMA */
 28static int pnp_reserve_io[16] = {[0 ... 15] = -1 };	/* reserve (don't use) some I/O region */
 29static int pnp_reserve_mem[16] = {[0 ... 15] = -1 };	/* reserve (don't use) some memory region */
 30
 31/*
 32 * option registration
 33 */
 34
 35static struct pnp_option *pnp_build_option(struct pnp_dev *dev, unsigned long type,
 36				    unsigned int option_flags)
 37{
 38	struct pnp_option *option;
 39
 40	option = kzalloc(sizeof(struct pnp_option), GFP_KERNEL);
 41	if (!option)
 42		return NULL;
 43
 44	option->flags = option_flags;
 45	option->type = type;
 46
 47	list_add_tail(&option->list, &dev->options);
 48	return option;
 49}
 50
 51int pnp_register_irq_resource(struct pnp_dev *dev, unsigned int option_flags,
 52			      pnp_irq_mask_t *map, unsigned char flags)
 53{
 54	struct pnp_option *option;
 55	struct pnp_irq *irq;
 56
 57	option = pnp_build_option(dev, IORESOURCE_IRQ, option_flags);
 58	if (!option)
 59		return -ENOMEM;
 60
 61	irq = &option->u.irq;
 62	irq->map = *map;
 63	irq->flags = flags;
 64
 65#ifdef CONFIG_PCI
 66	{
 67		int i;
 68
 69		for (i = 0; i < 16; i++)
 70			if (test_bit(i, irq->map.bits))
 71				pcibios_penalize_isa_irq(i, 0);
 72	}
 73#endif
 74
 75	dbg_pnp_show_option(dev, option);
 76	return 0;
 77}
 78
 79int pnp_register_dma_resource(struct pnp_dev *dev, unsigned int option_flags,
 80			      unsigned char map, unsigned char flags)
 81{
 82	struct pnp_option *option;
 83	struct pnp_dma *dma;
 84
 85	option = pnp_build_option(dev, IORESOURCE_DMA, option_flags);
 86	if (!option)
 87		return -ENOMEM;
 88
 89	dma = &option->u.dma;
 90	dma->map = map;
 91	dma->flags = flags;
 92
 93	dbg_pnp_show_option(dev, option);
 94	return 0;
 95}
 96
 97int pnp_register_port_resource(struct pnp_dev *dev, unsigned int option_flags,
 98			       resource_size_t min, resource_size_t max,
 99			       resource_size_t align, resource_size_t size,
100			       unsigned char flags)
101{
102	struct pnp_option *option;
103	struct pnp_port *port;
104
105	option = pnp_build_option(dev, IORESOURCE_IO, option_flags);
106	if (!option)
107		return -ENOMEM;
108
109	port = &option->u.port;
110	port->min = min;
111	port->max = max;
112	port->align = align;
113	port->size = size;
114	port->flags = flags;
115
116	dbg_pnp_show_option(dev, option);
117	return 0;
118}
119
120int pnp_register_mem_resource(struct pnp_dev *dev, unsigned int option_flags,
121			      resource_size_t min, resource_size_t max,
122			      resource_size_t align, resource_size_t size,
123			      unsigned char flags)
124{
125	struct pnp_option *option;
126	struct pnp_mem *mem;
127
128	option = pnp_build_option(dev, IORESOURCE_MEM, option_flags);
129	if (!option)
130		return -ENOMEM;
131
132	mem = &option->u.mem;
133	mem->min = min;
134	mem->max = max;
135	mem->align = align;
136	mem->size = size;
137	mem->flags = flags;
138
139	dbg_pnp_show_option(dev, option);
140	return 0;
141}
142
143void pnp_free_options(struct pnp_dev *dev)
144{
145	struct pnp_option *option, *tmp;
146
147	list_for_each_entry_safe(option, tmp, &dev->options, list) {
148		list_del(&option->list);
149		kfree(option);
150	}
151}
152
153/*
154 * resource validity checking
155 */
156
157#define length(start, end) (*(end) - *(start) + 1)
158
159/* Two ranges conflict if one doesn't end before the other starts */
160#define ranged_conflict(starta, enda, startb, endb) \
161	!((*(enda) < *(startb)) || (*(endb) < *(starta)))
162
163#define cannot_compare(flags) \
164((flags) & IORESOURCE_DISABLED)
165
166int pnp_check_port(struct pnp_dev *dev, struct resource *res)
167{
168	int i;
169	struct pnp_dev *tdev;
170	struct resource *tres;
171	resource_size_t *port, *end, *tport, *tend;
172
173	port = &res->start;
174	end = &res->end;
175
176	/* if the resource doesn't exist, don't complain about it */
177	if (cannot_compare(res->flags))
178		return 1;
179
180	/* check if the resource is already in use, skip if the
181	 * device is active because it itself may be in use */
182	if (!dev->active) {
183		if (!request_region(*port, length(port, end), "pnp"))
184			return 0;
185		release_region(*port, length(port, end));
186	}
187
188	/* check if the resource is reserved */
189	for (i = 0; i < 8; i++) {
190		int rport = pnp_reserve_io[i << 1];
191		int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1;
192		if (ranged_conflict(port, end, &rport, &rend))
193			return 0;
194	}
195
196	/* check for internal conflicts */
197	for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
198		if (tres != res && tres->flags & IORESOURCE_IO) {
199			tport = &tres->start;
200			tend = &tres->end;
201			if (ranged_conflict(port, end, tport, tend))
202				return 0;
203		}
204	}
205
206	/* check for conflicts with other pnp devices */
207	pnp_for_each_dev(tdev) {
208		if (tdev == dev)
209			continue;
210		for (i = 0;
211		     (tres = pnp_get_resource(tdev, IORESOURCE_IO, i));
212		     i++) {
213			if (tres->flags & IORESOURCE_IO) {
214				if (cannot_compare(tres->flags))
215					continue;
216				if (tres->flags & IORESOURCE_WINDOW)
217					continue;
218				tport = &tres->start;
219				tend = &tres->end;
220				if (ranged_conflict(port, end, tport, tend))
221					return 0;
222			}
223		}
224	}
225
226	return 1;
227}
228
229int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
230{
231	int i;
232	struct pnp_dev *tdev;
233	struct resource *tres;
234	resource_size_t *addr, *end, *taddr, *tend;
235
236	addr = &res->start;
237	end = &res->end;
238
239	/* if the resource doesn't exist, don't complain about it */
240	if (cannot_compare(res->flags))
241		return 1;
242
243	/* check if the resource is already in use, skip if the
244	 * device is active because it itself may be in use */
245	if (!dev->active) {
246		if (!request_mem_region(*addr, length(addr, end), "pnp"))
247			return 0;
248		release_mem_region(*addr, length(addr, end));
249	}
250
251	/* check if the resource is reserved */
252	for (i = 0; i < 8; i++) {
253		int raddr = pnp_reserve_mem[i << 1];
254		int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
255		if (ranged_conflict(addr, end, &raddr, &rend))
256			return 0;
257	}
258
259	/* check for internal conflicts */
260	for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
261		if (tres != res && tres->flags & IORESOURCE_MEM) {
262			taddr = &tres->start;
263			tend = &tres->end;
264			if (ranged_conflict(addr, end, taddr, tend))
265				return 0;
266		}
267	}
268
269	/* check for conflicts with other pnp devices */
270	pnp_for_each_dev(tdev) {
271		if (tdev == dev)
272			continue;
273		for (i = 0;
274		     (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
275		     i++) {
276			if (tres->flags & IORESOURCE_MEM) {
277				if (cannot_compare(tres->flags))
278					continue;
279				if (tres->flags & IORESOURCE_WINDOW)
280					continue;
281				taddr = &tres->start;
282				tend = &tres->end;
283				if (ranged_conflict(addr, end, taddr, tend))
284					return 0;
285			}
286		}
287	}
288
289	return 1;
290}
291
292static irqreturn_t pnp_test_handler(int irq, void *dev_id)
293{
294	return IRQ_HANDLED;
295}
296
297#ifdef CONFIG_PCI
298static int pci_dev_uses_irq(struct pnp_dev *pnp, struct pci_dev *pci,
299			    unsigned int irq)
300{
301	u32 class;
302	u8 progif;
303
304	if (pci->irq == irq) {
305		pnp_dbg(&pnp->dev, "  device %s using irq %d\n",
306			pci_name(pci), irq);
307		return 1;
308	}
309
310	/*
311	 * See pci_setup_device() and ata_pci_sff_activate_host() for
312	 * similar IDE legacy detection.
313	 */
314	pci_read_config_dword(pci, PCI_CLASS_REVISION, &class);
315	class >>= 8;		/* discard revision ID */
316	progif = class & 0xff;
317	class >>= 8;
318
319	if (class == PCI_CLASS_STORAGE_IDE) {
320		/*
321		 * Unless both channels are native-PCI mode only,
322		 * treat the compatibility IRQs as busy.
323		 */
324		if ((progif & 0x5) != 0x5)
325			if (pci_get_legacy_ide_irq(pci, 0) == irq ||
326			    pci_get_legacy_ide_irq(pci, 1) == irq) {
327				pnp_dbg(&pnp->dev, "  legacy IDE device %s "
328					"using irq %d\n", pci_name(pci), irq);
329				return 1;
330			}
331	}
332
333	return 0;
334}
335#endif
336
337static int pci_uses_irq(struct pnp_dev *pnp, unsigned int irq)
338{
339#ifdef CONFIG_PCI
340	struct pci_dev *pci = NULL;
341
342	for_each_pci_dev(pci) {
343		if (pci_dev_uses_irq(pnp, pci, irq)) {
344			pci_dev_put(pci);
345			return 1;
346		}
347	}
348#endif
349	return 0;
350}
351
352int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
353{
354	int i;
355	struct pnp_dev *tdev;
356	struct resource *tres;
357	resource_size_t *irq;
358
359	irq = &res->start;
360
361	/* if the resource doesn't exist, don't complain about it */
362	if (cannot_compare(res->flags))
363		return 1;
364
365	/* check if the resource is valid */
366	if (*irq > 15)
367		return 0;
368
369	/* check if the resource is reserved */
370	for (i = 0; i < 16; i++) {
371		if (pnp_reserve_irq[i] == *irq)
372			return 0;
373	}
374
375	/* check for internal conflicts */
376	for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
377		if (tres != res && tres->flags & IORESOURCE_IRQ) {
378			if (tres->start == *irq)
379				return 0;
380		}
381	}
382
383	/* check if the resource is being used by a pci device */
384	if (pci_uses_irq(dev, *irq))
385		return 0;
386
387	/* check if the resource is already in use, skip if the
388	 * device is active because it itself may be in use */
389	if (!dev->active) {
390		if (request_irq(*irq, pnp_test_handler,
391				IRQF_PROBE_SHARED, "pnp", NULL))
392			return 0;
393		free_irq(*irq, NULL);
394	}
395
396	/* check for conflicts with other pnp devices */
397	pnp_for_each_dev(tdev) {
398		if (tdev == dev)
399			continue;
400		for (i = 0;
401		     (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
402		     i++) {
403			if (tres->flags & IORESOURCE_IRQ) {
404				if (cannot_compare(tres->flags))
405					continue;
406				if (tres->start == *irq)
407					return 0;
408			}
409		}
410	}
411
412	return 1;
413}
414
415#ifdef CONFIG_ISA_DMA_API
416int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
417{
418	int i;
419	struct pnp_dev *tdev;
420	struct resource *tres;
421	resource_size_t *dma;
422
423	dma = &res->start;
424
425	/* if the resource doesn't exist, don't complain about it */
426	if (cannot_compare(res->flags))
427		return 1;
428
429	/* check if the resource is valid */
430	if (*dma == 4 || *dma > 7)
431		return 0;
432
433	/* check if the resource is reserved */
434	for (i = 0; i < 8; i++) {
435		if (pnp_reserve_dma[i] == *dma)
436			return 0;
437	}
438
439	/* check for internal conflicts */
440	for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
441		if (tres != res && tres->flags & IORESOURCE_DMA) {
442			if (tres->start == *dma)
443				return 0;
444		}
445	}
446
447	/* check if the resource is already in use, skip if the
448	 * device is active because it itself may be in use */
449	if (!dev->active) {
450		if (request_dma(*dma, "pnp"))
451			return 0;
452		free_dma(*dma);
453	}
454
455	/* check for conflicts with other pnp devices */
456	pnp_for_each_dev(tdev) {
457		if (tdev == dev)
458			continue;
459		for (i = 0;
460		     (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
461		     i++) {
462			if (tres->flags & IORESOURCE_DMA) {
463				if (cannot_compare(tres->flags))
464					continue;
465				if (tres->start == *dma)
466					return 0;
467			}
468		}
469	}
470
471	return 1;
472}
473#endif /* CONFIG_ISA_DMA_API */
474
475unsigned long pnp_resource_type(struct resource *res)
476{
477	return res->flags & (IORESOURCE_IO  | IORESOURCE_MEM |
478			     IORESOURCE_IRQ | IORESOURCE_DMA |
479			     IORESOURCE_BUS);
480}
481
482struct resource *pnp_get_resource(struct pnp_dev *dev,
483				  unsigned long type, unsigned int num)
484{
485	struct pnp_resource *pnp_res;
486	struct resource *res;
487
488	list_for_each_entry(pnp_res, &dev->resources, list) {
489		res = &pnp_res->res;
490		if (pnp_resource_type(res) == type && num-- == 0)
491			return res;
492	}
493	return NULL;
494}
495EXPORT_SYMBOL(pnp_get_resource);
496
497static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
498{
499	struct pnp_resource *pnp_res;
500
501	pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
502	if (!pnp_res)
503		return NULL;
504
505	list_add_tail(&pnp_res->list, &dev->resources);
506	return pnp_res;
507}
508
509struct pnp_resource *pnp_add_resource(struct pnp_dev *dev,
510				      struct resource *res)
511{
512	struct pnp_resource *pnp_res;
513
514	pnp_res = pnp_new_resource(dev);
515	if (!pnp_res) {
516		dev_err(&dev->dev, "can't add resource %pR\n", res);
517		return NULL;
518	}
519
520	pnp_res->res = *res;
521	pnp_res->res.name = dev->name;
522	dev_dbg(&dev->dev, "%pR\n", res);
523	return pnp_res;
524}
525
526struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
527					  int flags)
528{
529	struct pnp_resource *pnp_res;
530	struct resource *res;
531
532	pnp_res = pnp_new_resource(dev);
533	if (!pnp_res) {
534		dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
535		return NULL;
536	}
537
538	res = &pnp_res->res;
539	res->flags = IORESOURCE_IRQ | flags;
540	res->start = irq;
541	res->end = irq;
542
543	dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
544	return pnp_res;
545}
546
547struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
548					  int flags)
549{
550	struct pnp_resource *pnp_res;
551	struct resource *res;
552
553	pnp_res = pnp_new_resource(dev);
554	if (!pnp_res) {
555		dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
556		return NULL;
557	}
558
559	res = &pnp_res->res;
560	res->flags = IORESOURCE_DMA | flags;
561	res->start = dma;
562	res->end = dma;
563
564	dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
565	return pnp_res;
566}
567
568struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
569					 resource_size_t start,
570					 resource_size_t end, int flags)
571{
572	struct pnp_resource *pnp_res;
573	struct resource *res;
574
575	pnp_res = pnp_new_resource(dev);
576	if (!pnp_res) {
577		dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
578			(unsigned long long) start,
579			(unsigned long long) end);
580		return NULL;
581	}
582
583	res = &pnp_res->res;
584	res->flags = IORESOURCE_IO | flags;
585	res->start = start;
586	res->end = end;
587
588	dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
589	return pnp_res;
590}
591
592struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
593					  resource_size_t start,
594					  resource_size_t end, int flags)
595{
596	struct pnp_resource *pnp_res;
597	struct resource *res;
598
599	pnp_res = pnp_new_resource(dev);
600	if (!pnp_res) {
601		dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
602			(unsigned long long) start,
603			(unsigned long long) end);
604		return NULL;
605	}
606
607	res = &pnp_res->res;
608	res->flags = IORESOURCE_MEM | flags;
609	res->start = start;
610	res->end = end;
611
612	dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
613	return pnp_res;
614}
615
616struct pnp_resource *pnp_add_bus_resource(struct pnp_dev *dev,
617					  resource_size_t start,
618					  resource_size_t end)
619{
620	struct pnp_resource *pnp_res;
621	struct resource *res;
622
623	pnp_res = pnp_new_resource(dev);
624	if (!pnp_res) {
625		dev_err(&dev->dev, "can't add resource for BUS %#llx-%#llx\n",
626			(unsigned long long) start,
627			(unsigned long long) end);
628		return NULL;
629	}
630
631	res = &pnp_res->res;
632	res->flags = IORESOURCE_BUS;
633	res->start = start;
634	res->end = end;
635
636	dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
637	return pnp_res;
638}
639
640/*
641 * Determine whether the specified resource is a possible configuration
642 * for this device.
643 */
644int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
645			resource_size_t size)
646{
647	struct pnp_option *option;
648	struct pnp_port *port;
649	struct pnp_mem *mem;
650	struct pnp_irq *irq;
651	struct pnp_dma *dma;
652
653	list_for_each_entry(option, &dev->options, list) {
654		if (option->type != type)
655			continue;
656
657		switch (option->type) {
658		case IORESOURCE_IO:
659			port = &option->u.port;
660			if (port->min == start && port->size == size)
661				return 1;
662			break;
663		case IORESOURCE_MEM:
664			mem = &option->u.mem;
665			if (mem->min == start && mem->size == size)
666				return 1;
667			break;
668		case IORESOURCE_IRQ:
669			irq = &option->u.irq;
670			if (start < PNP_IRQ_NR &&
671			    test_bit(start, irq->map.bits))
672				return 1;
673			break;
674		case IORESOURCE_DMA:
675			dma = &option->u.dma;
676			if (dma->map & (1 << start))
677				return 1;
678			break;
679		}
680	}
681
682	return 0;
683}
684EXPORT_SYMBOL(pnp_possible_config);
685
686int pnp_range_reserved(resource_size_t start, resource_size_t end)
687{
688	struct pnp_dev *dev;
689	struct pnp_resource *pnp_res;
690	resource_size_t *dev_start, *dev_end;
691
692	pnp_for_each_dev(dev) {
693		list_for_each_entry(pnp_res, &dev->resources, list) {
694			dev_start = &pnp_res->res.start;
695			dev_end   = &pnp_res->res.end;
696			if (ranged_conflict(&start, &end, dev_start, dev_end))
697				return 1;
698		}
699	}
700	return 0;
701}
702EXPORT_SYMBOL(pnp_range_reserved);
703
704/* format is: pnp_reserve_irq=irq1[,irq2] .... */
705static int __init pnp_setup_reserve_irq(char *str)
706{
707	int i;
708
709	for (i = 0; i < 16; i++)
710		if (get_option(&str, &pnp_reserve_irq[i]) != 2)
711			break;
712	return 1;
713}
714
715__setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
716
717/* format is: pnp_reserve_dma=dma1[,dma2] .... */
718static int __init pnp_setup_reserve_dma(char *str)
719{
720	int i;
721
722	for (i = 0; i < 8; i++)
723		if (get_option(&str, &pnp_reserve_dma[i]) != 2)
724			break;
725	return 1;
726}
727
728__setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
729
730/* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
731static int __init pnp_setup_reserve_io(char *str)
732{
733	int i;
734
735	for (i = 0; i < 16; i++)
736		if (get_option(&str, &pnp_reserve_io[i]) != 2)
737			break;
738	return 1;
739}
740
741__setup("pnp_reserve_io=", pnp_setup_reserve_io);
742
743/* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
744static int __init pnp_setup_reserve_mem(char *str)
745{
746	int i;
747
748	for (i = 0; i < 16; i++)
749		if (get_option(&str, &pnp_reserve_mem[i]) != 2)
750			break;
751	return 1;
752}
753
754__setup("pnp_reserve_mem=", pnp_setup_reserve_mem);
  1/*
  2 * resource.c - Contains functions for registering and analyzing resource information
  3 *
  4 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
  5 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
  6 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
  7 *	Bjorn Helgaas <bjorn.helgaas@hp.com>
  8 */
  9
 10#include <linux/module.h>
 11#include <linux/slab.h>
 12#include <linux/errno.h>
 13#include <linux/interrupt.h>
 14#include <linux/kernel.h>
 15#include <asm/io.h>
 16#include <asm/dma.h>
 17#include <asm/irq.h>
 18#include <linux/pci.h>
 19#include <linux/ioport.h>
 20#include <linux/init.h>
 21
 22#include <linux/pnp.h>
 23#include "base.h"
 24
 25static int pnp_reserve_irq[16] = {[0 ... 15] = -1 };	/* reserve (don't use) some IRQ */
 26static int pnp_reserve_dma[8] = {[0 ... 7] = -1 };	/* reserve (don't use) some DMA */
 27static int pnp_reserve_io[16] = {[0 ... 15] = -1 };	/* reserve (don't use) some I/O region */
 28static int pnp_reserve_mem[16] = {[0 ... 15] = -1 };	/* reserve (don't use) some memory region */
 29
 30/*
 31 * option registration
 32 */
 33
 34struct pnp_option *pnp_build_option(struct pnp_dev *dev, unsigned long type,
 35				    unsigned int option_flags)
 36{
 37	struct pnp_option *option;
 38
 39	option = kzalloc(sizeof(struct pnp_option), GFP_KERNEL);
 40	if (!option)
 41		return NULL;
 42
 43	option->flags = option_flags;
 44	option->type = type;
 45
 46	list_add_tail(&option->list, &dev->options);
 47	return option;
 48}
 49
 50int pnp_register_irq_resource(struct pnp_dev *dev, unsigned int option_flags,
 51			      pnp_irq_mask_t *map, unsigned char flags)
 52{
 53	struct pnp_option *option;
 54	struct pnp_irq *irq;
 55
 56	option = pnp_build_option(dev, IORESOURCE_IRQ, option_flags);
 57	if (!option)
 58		return -ENOMEM;
 59
 60	irq = &option->u.irq;
 61	irq->map = *map;
 62	irq->flags = flags;
 63
 64#ifdef CONFIG_PCI
 65	{
 66		int i;
 67
 68		for (i = 0; i < 16; i++)
 69			if (test_bit(i, irq->map.bits))
 70				pcibios_penalize_isa_irq(i, 0);
 71	}
 72#endif
 73
 74	dbg_pnp_show_option(dev, option);
 75	return 0;
 76}
 77
 78int pnp_register_dma_resource(struct pnp_dev *dev, unsigned int option_flags,
 79			      unsigned char map, unsigned char flags)
 80{
 81	struct pnp_option *option;
 82	struct pnp_dma *dma;
 83
 84	option = pnp_build_option(dev, IORESOURCE_DMA, option_flags);
 85	if (!option)
 86		return -ENOMEM;
 87
 88	dma = &option->u.dma;
 89	dma->map = map;
 90	dma->flags = flags;
 91
 92	dbg_pnp_show_option(dev, option);
 93	return 0;
 94}
 95
 96int pnp_register_port_resource(struct pnp_dev *dev, unsigned int option_flags,
 97			       resource_size_t min, resource_size_t max,
 98			       resource_size_t align, resource_size_t size,
 99			       unsigned char flags)
100{
101	struct pnp_option *option;
102	struct pnp_port *port;
103
104	option = pnp_build_option(dev, IORESOURCE_IO, option_flags);
105	if (!option)
106		return -ENOMEM;
107
108	port = &option->u.port;
109	port->min = min;
110	port->max = max;
111	port->align = align;
112	port->size = size;
113	port->flags = flags;
114
115	dbg_pnp_show_option(dev, option);
116	return 0;
117}
118
119int pnp_register_mem_resource(struct pnp_dev *dev, unsigned int option_flags,
120			      resource_size_t min, resource_size_t max,
121			      resource_size_t align, resource_size_t size,
122			      unsigned char flags)
123{
124	struct pnp_option *option;
125	struct pnp_mem *mem;
126
127	option = pnp_build_option(dev, IORESOURCE_MEM, option_flags);
128	if (!option)
129		return -ENOMEM;
130
131	mem = &option->u.mem;
132	mem->min = min;
133	mem->max = max;
134	mem->align = align;
135	mem->size = size;
136	mem->flags = flags;
137
138	dbg_pnp_show_option(dev, option);
139	return 0;
140}
141
142void pnp_free_options(struct pnp_dev *dev)
143{
144	struct pnp_option *option, *tmp;
145
146	list_for_each_entry_safe(option, tmp, &dev->options, list) {
147		list_del(&option->list);
148		kfree(option);
149	}
150}
151
152/*
153 * resource validity checking
154 */
155
156#define length(start, end) (*(end) - *(start) + 1)
157
158/* Two ranges conflict if one doesn't end before the other starts */
159#define ranged_conflict(starta, enda, startb, endb) \
160	!((*(enda) < *(startb)) || (*(endb) < *(starta)))
161
162#define cannot_compare(flags) \
163((flags) & IORESOURCE_DISABLED)
164
165int pnp_check_port(struct pnp_dev *dev, struct resource *res)
166{
167	int i;
168	struct pnp_dev *tdev;
169	struct resource *tres;
170	resource_size_t *port, *end, *tport, *tend;
171
172	port = &res->start;
173	end = &res->end;
174
175	/* if the resource doesn't exist, don't complain about it */
176	if (cannot_compare(res->flags))
177		return 1;
178
179	/* check if the resource is already in use, skip if the
180	 * device is active because it itself may be in use */
181	if (!dev->active) {
182		if (__check_region(&ioport_resource, *port, length(port, end)))
183			return 0;
184	}
185
186	/* check if the resource is reserved */
187	for (i = 0; i < 8; i++) {
188		int rport = pnp_reserve_io[i << 1];
189		int rend = pnp_reserve_io[(i << 1) + 1] + rport - 1;
190		if (ranged_conflict(port, end, &rport, &rend))
191			return 0;
192	}
193
194	/* check for internal conflicts */
195	for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
196		if (tres != res && tres->flags & IORESOURCE_IO) {
197			tport = &tres->start;
198			tend = &tres->end;
199			if (ranged_conflict(port, end, tport, tend))
200				return 0;
201		}
202	}
203
204	/* check for conflicts with other pnp devices */
205	pnp_for_each_dev(tdev) {
206		if (tdev == dev)
207			continue;
208		for (i = 0;
209		     (tres = pnp_get_resource(tdev, IORESOURCE_IO, i));
210		     i++) {
211			if (tres->flags & IORESOURCE_IO) {
212				if (cannot_compare(tres->flags))
213					continue;
214				if (tres->flags & IORESOURCE_WINDOW)
215					continue;
216				tport = &tres->start;
217				tend = &tres->end;
218				if (ranged_conflict(port, end, tport, tend))
219					return 0;
220			}
221		}
222	}
223
224	return 1;
225}
226
227int pnp_check_mem(struct pnp_dev *dev, struct resource *res)
228{
229	int i;
230	struct pnp_dev *tdev;
231	struct resource *tres;
232	resource_size_t *addr, *end, *taddr, *tend;
233
234	addr = &res->start;
235	end = &res->end;
236
237	/* if the resource doesn't exist, don't complain about it */
238	if (cannot_compare(res->flags))
239		return 1;
240
241	/* check if the resource is already in use, skip if the
242	 * device is active because it itself may be in use */
243	if (!dev->active) {
244		if (check_mem_region(*addr, length(addr, end)))
245			return 0;
246	}
247
248	/* check if the resource is reserved */
249	for (i = 0; i < 8; i++) {
250		int raddr = pnp_reserve_mem[i << 1];
251		int rend = pnp_reserve_mem[(i << 1) + 1] + raddr - 1;
252		if (ranged_conflict(addr, end, &raddr, &rend))
253			return 0;
254	}
255
256	/* check for internal conflicts */
257	for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
258		if (tres != res && tres->flags & IORESOURCE_MEM) {
259			taddr = &tres->start;
260			tend = &tres->end;
261			if (ranged_conflict(addr, end, taddr, tend))
262				return 0;
263		}
264	}
265
266	/* check for conflicts with other pnp devices */
267	pnp_for_each_dev(tdev) {
268		if (tdev == dev)
269			continue;
270		for (i = 0;
271		     (tres = pnp_get_resource(tdev, IORESOURCE_MEM, i));
272		     i++) {
273			if (tres->flags & IORESOURCE_MEM) {
274				if (cannot_compare(tres->flags))
275					continue;
276				if (tres->flags & IORESOURCE_WINDOW)
277					continue;
278				taddr = &tres->start;
279				tend = &tres->end;
280				if (ranged_conflict(addr, end, taddr, tend))
281					return 0;
282			}
283		}
284	}
285
286	return 1;
287}
288
289static irqreturn_t pnp_test_handler(int irq, void *dev_id)
290{
291	return IRQ_HANDLED;
292}
293
294#ifdef CONFIG_PCI
295static int pci_dev_uses_irq(struct pnp_dev *pnp, struct pci_dev *pci,
296			    unsigned int irq)
297{
298	u32 class;
299	u8 progif;
300
301	if (pci->irq == irq) {
302		pnp_dbg(&pnp->dev, "  device %s using irq %d\n",
303			pci_name(pci), irq);
304		return 1;
305	}
306
307	/*
308	 * See pci_setup_device() and ata_pci_sff_activate_host() for
309	 * similar IDE legacy detection.
310	 */
311	pci_read_config_dword(pci, PCI_CLASS_REVISION, &class);
312	class >>= 8;		/* discard revision ID */
313	progif = class & 0xff;
314	class >>= 8;
315
316	if (class == PCI_CLASS_STORAGE_IDE) {
317		/*
318		 * Unless both channels are native-PCI mode only,
319		 * treat the compatibility IRQs as busy.
320		 */
321		if ((progif & 0x5) != 0x5)
322			if (pci_get_legacy_ide_irq(pci, 0) == irq ||
323			    pci_get_legacy_ide_irq(pci, 1) == irq) {
324				pnp_dbg(&pnp->dev, "  legacy IDE device %s "
325					"using irq %d\n", pci_name(pci), irq);
326				return 1;
327			}
328	}
329
330	return 0;
331}
332#endif
333
334static int pci_uses_irq(struct pnp_dev *pnp, unsigned int irq)
335{
336#ifdef CONFIG_PCI
337	struct pci_dev *pci = NULL;
338
339	for_each_pci_dev(pci) {
340		if (pci_dev_uses_irq(pnp, pci, irq)) {
341			pci_dev_put(pci);
342			return 1;
343		}
344	}
345#endif
346	return 0;
347}
348
349int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
350{
351	int i;
352	struct pnp_dev *tdev;
353	struct resource *tres;
354	resource_size_t *irq;
355
356	irq = &res->start;
357
358	/* if the resource doesn't exist, don't complain about it */
359	if (cannot_compare(res->flags))
360		return 1;
361
362	/* check if the resource is valid */
363	if (*irq < 0 || *irq > 15)
364		return 0;
365
366	/* check if the resource is reserved */
367	for (i = 0; i < 16; i++) {
368		if (pnp_reserve_irq[i] == *irq)
369			return 0;
370	}
371
372	/* check for internal conflicts */
373	for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
374		if (tres != res && tres->flags & IORESOURCE_IRQ) {
375			if (tres->start == *irq)
376				return 0;
377		}
378	}
379
380	/* check if the resource is being used by a pci device */
381	if (pci_uses_irq(dev, *irq))
382		return 0;
383
384	/* check if the resource is already in use, skip if the
385	 * device is active because it itself may be in use */
386	if (!dev->active) {
387		if (request_irq(*irq, pnp_test_handler,
388				IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
389			return 0;
390		free_irq(*irq, NULL);
391	}
392
393	/* check for conflicts with other pnp devices */
394	pnp_for_each_dev(tdev) {
395		if (tdev == dev)
396			continue;
397		for (i = 0;
398		     (tres = pnp_get_resource(tdev, IORESOURCE_IRQ, i));
399		     i++) {
400			if (tres->flags & IORESOURCE_IRQ) {
401				if (cannot_compare(tres->flags))
402					continue;
403				if (tres->start == *irq)
404					return 0;
405			}
406		}
407	}
408
409	return 1;
410}
411
412#ifdef CONFIG_ISA_DMA_API
413int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
414{
415	int i;
416	struct pnp_dev *tdev;
417	struct resource *tres;
418	resource_size_t *dma;
419
420	dma = &res->start;
421
422	/* if the resource doesn't exist, don't complain about it */
423	if (cannot_compare(res->flags))
424		return 1;
425
426	/* check if the resource is valid */
427	if (*dma < 0 || *dma == 4 || *dma > 7)
428		return 0;
429
430	/* check if the resource is reserved */
431	for (i = 0; i < 8; i++) {
432		if (pnp_reserve_dma[i] == *dma)
433			return 0;
434	}
435
436	/* check for internal conflicts */
437	for (i = 0; (tres = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
438		if (tres != res && tres->flags & IORESOURCE_DMA) {
439			if (tres->start == *dma)
440				return 0;
441		}
442	}
443
444	/* check if the resource is already in use, skip if the
445	 * device is active because it itself may be in use */
446	if (!dev->active) {
447		if (request_dma(*dma, "pnp"))
448			return 0;
449		free_dma(*dma);
450	}
451
452	/* check for conflicts with other pnp devices */
453	pnp_for_each_dev(tdev) {
454		if (tdev == dev)
455			continue;
456		for (i = 0;
457		     (tres = pnp_get_resource(tdev, IORESOURCE_DMA, i));
458		     i++) {
459			if (tres->flags & IORESOURCE_DMA) {
460				if (cannot_compare(tres->flags))
461					continue;
462				if (tres->start == *dma)
463					return 0;
464			}
465		}
466	}
467
468	return 1;
469}
470#endif /* CONFIG_ISA_DMA_API */
471
472unsigned long pnp_resource_type(struct resource *res)
473{
474	return res->flags & (IORESOURCE_IO  | IORESOURCE_MEM |
475			     IORESOURCE_IRQ | IORESOURCE_DMA |
476			     IORESOURCE_BUS);
477}
478
479struct resource *pnp_get_resource(struct pnp_dev *dev,
480				  unsigned long type, unsigned int num)
481{
482	struct pnp_resource *pnp_res;
483	struct resource *res;
484
485	list_for_each_entry(pnp_res, &dev->resources, list) {
486		res = &pnp_res->res;
487		if (pnp_resource_type(res) == type && num-- == 0)
488			return res;
489	}
490	return NULL;
491}
492EXPORT_SYMBOL(pnp_get_resource);
493
494static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
495{
496	struct pnp_resource *pnp_res;
497
498	pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
499	if (!pnp_res)
500		return NULL;
501
502	list_add_tail(&pnp_res->list, &dev->resources);
503	return pnp_res;
504}
505
506struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
507					  int flags)
508{
509	struct pnp_resource *pnp_res;
510	struct resource *res;
511
512	pnp_res = pnp_new_resource(dev);
513	if (!pnp_res) {
514		dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
515		return NULL;
516	}
517
518	res = &pnp_res->res;
519	res->flags = IORESOURCE_IRQ | flags;
520	res->start = irq;
521	res->end = irq;
522
523	dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
524	return pnp_res;
525}
526
527struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
528					  int flags)
529{
530	struct pnp_resource *pnp_res;
531	struct resource *res;
532
533	pnp_res = pnp_new_resource(dev);
534	if (!pnp_res) {
535		dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
536		return NULL;
537	}
538
539	res = &pnp_res->res;
540	res->flags = IORESOURCE_DMA | flags;
541	res->start = dma;
542	res->end = dma;
543
544	dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
545	return pnp_res;
546}
547
548struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
549					 resource_size_t start,
550					 resource_size_t end, int flags)
551{
552	struct pnp_resource *pnp_res;
553	struct resource *res;
554
555	pnp_res = pnp_new_resource(dev);
556	if (!pnp_res) {
557		dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
558			(unsigned long long) start,
559			(unsigned long long) end);
560		return NULL;
561	}
562
563	res = &pnp_res->res;
564	res->flags = IORESOURCE_IO | flags;
565	res->start = start;
566	res->end = end;
567
568	dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
569	return pnp_res;
570}
571
572struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
573					  resource_size_t start,
574					  resource_size_t end, int flags)
575{
576	struct pnp_resource *pnp_res;
577	struct resource *res;
578
579	pnp_res = pnp_new_resource(dev);
580	if (!pnp_res) {
581		dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
582			(unsigned long long) start,
583			(unsigned long long) end);
584		return NULL;
585	}
586
587	res = &pnp_res->res;
588	res->flags = IORESOURCE_MEM | flags;
589	res->start = start;
590	res->end = end;
591
592	dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
593	return pnp_res;
594}
595
596struct pnp_resource *pnp_add_bus_resource(struct pnp_dev *dev,
597					  resource_size_t start,
598					  resource_size_t end)
599{
600	struct pnp_resource *pnp_res;
601	struct resource *res;
602
603	pnp_res = pnp_new_resource(dev);
604	if (!pnp_res) {
605		dev_err(&dev->dev, "can't add resource for BUS %#llx-%#llx\n",
606			(unsigned long long) start,
607			(unsigned long long) end);
608		return NULL;
609	}
610
611	res = &pnp_res->res;
612	res->flags = IORESOURCE_BUS;
613	res->start = start;
614	res->end = end;
615
616	dev_printk(KERN_DEBUG, &dev->dev, "%pR\n", res);
617	return pnp_res;
618}
619
620/*
621 * Determine whether the specified resource is a possible configuration
622 * for this device.
623 */
624int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
625			resource_size_t size)
626{
627	struct pnp_option *option;
628	struct pnp_port *port;
629	struct pnp_mem *mem;
630	struct pnp_irq *irq;
631	struct pnp_dma *dma;
632
633	list_for_each_entry(option, &dev->options, list) {
634		if (option->type != type)
635			continue;
636
637		switch (option->type) {
638		case IORESOURCE_IO:
639			port = &option->u.port;
640			if (port->min == start && port->size == size)
641				return 1;
642			break;
643		case IORESOURCE_MEM:
644			mem = &option->u.mem;
645			if (mem->min == start && mem->size == size)
646				return 1;
647			break;
648		case IORESOURCE_IRQ:
649			irq = &option->u.irq;
650			if (start < PNP_IRQ_NR &&
651			    test_bit(start, irq->map.bits))
652				return 1;
653			break;
654		case IORESOURCE_DMA:
655			dma = &option->u.dma;
656			if (dma->map & (1 << start))
657				return 1;
658			break;
659		}
660	}
661
662	return 0;
663}
664EXPORT_SYMBOL(pnp_possible_config);
665
666int pnp_range_reserved(resource_size_t start, resource_size_t end)
667{
668	struct pnp_dev *dev;
669	struct pnp_resource *pnp_res;
670	resource_size_t *dev_start, *dev_end;
671
672	pnp_for_each_dev(dev) {
673		list_for_each_entry(pnp_res, &dev->resources, list) {
674			dev_start = &pnp_res->res.start;
675			dev_end   = &pnp_res->res.end;
676			if (ranged_conflict(&start, &end, dev_start, dev_end))
677				return 1;
678		}
679	}
680	return 0;
681}
682EXPORT_SYMBOL(pnp_range_reserved);
683
684/* format is: pnp_reserve_irq=irq1[,irq2] .... */
685static int __init pnp_setup_reserve_irq(char *str)
686{
687	int i;
688
689	for (i = 0; i < 16; i++)
690		if (get_option(&str, &pnp_reserve_irq[i]) != 2)
691			break;
692	return 1;
693}
694
695__setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
696
697/* format is: pnp_reserve_dma=dma1[,dma2] .... */
698static int __init pnp_setup_reserve_dma(char *str)
699{
700	int i;
701
702	for (i = 0; i < 8; i++)
703		if (get_option(&str, &pnp_reserve_dma[i]) != 2)
704			break;
705	return 1;
706}
707
708__setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
709
710/* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
711static int __init pnp_setup_reserve_io(char *str)
712{
713	int i;
714
715	for (i = 0; i < 16; i++)
716		if (get_option(&str, &pnp_reserve_io[i]) != 2)
717			break;
718	return 1;
719}
720
721__setup("pnp_reserve_io=", pnp_setup_reserve_io);
722
723/* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
724static int __init pnp_setup_reserve_mem(char *str)
725{
726	int i;
727
728	for (i = 0; i < 16; i++)
729		if (get_option(&str, &pnp_reserve_mem[i]) != 2)
730			break;
731	return 1;
732}
733
734__setup("pnp_reserve_mem=", pnp_setup_reserve_mem);