136285Sbrian/*-
236285Sbrian * Copyright (c) 2015 Marcel Moolenaar
336285Sbrian * All rights reserved.
436285Sbrian *
536285Sbrian * Redistribution and use in source and binary forms, with or without
636285Sbrian * modification, are permitted provided that the following conditions
736285Sbrian * are met:
836285Sbrian * 1. Redistributions of source code must retain the above copyright
936285Sbrian *    notice, this list of conditions and the following disclaimer.
1036285Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1136285Sbrian *    notice, this list of conditions and the following disclaimer in the
1236285Sbrian *    documentation and/or other materials provided with the distribution.
1336285Sbrian *
1436285Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1536285Sbrian * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1636285Sbrian * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1736285Sbrian * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1836285Sbrian * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1936285Sbrian * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2036285Sbrian * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2136285Sbrian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2236285Sbrian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2336285Sbrian * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2436285Sbrian */
2536285Sbrian
2636285Sbrian#include <sys/param.h>
2736285Sbrian#include <sys/systm.h>
2836285Sbrian#include <sys/bus.h>
2936285Sbrian#include <sys/conf.h>
3036285Sbrian#include <sys/kernel.h>
3136285Sbrian#include <sys/module.h>
3236285Sbrian#include <machine/bus.h>
3336285Sbrian#include <sys/rman.h>
3436285Sbrian#include <machine/resource.h>
3536285Sbrian
3636285Sbrian#include <isa/isavar.h>
3736466Sbrian#include <isa/pnpvar.h>
3836285Sbrian
3936285Sbrian#include <dev/proto/proto.h>
4036285Sbrian
4136285Sbrianstatic int proto_isa_probe(device_t dev);
4236285Sbrianstatic int proto_isa_attach(device_t dev);
4336774Sbrian
4436774Sbrianstatic device_method_t proto_isa_methods[] = {
4536285Sbrian	/* Device interface */
4636285Sbrian	DEVMETHOD(device_probe,		proto_isa_probe),
4736285Sbrian	DEVMETHOD(device_attach,	proto_isa_attach),
4836285Sbrian	DEVMETHOD(device_detach,	proto_detach),
4936285Sbrian	DEVMETHOD_END
5036285Sbrian};
5136285Sbrian
5236285Sbrianstatic driver_t proto_isa_driver = {
5336285Sbrian	proto_driver_name,
5436285Sbrian	proto_isa_methods,
5536466Sbrian	sizeof(struct proto_softc),
5636285Sbrian};
5736285Sbrian
5836285Sbrianstatic char proto_isa_prefix[] = "isa";
5936285Sbrianstatic char **proto_isa_devnames;
6036285Sbrian
6136285Sbrianstatic int
6236285Sbrianproto_isa_probe(device_t dev)
6336285Sbrian{
6436285Sbrian	struct resource *res;
6536285Sbrian	int rid, type;
6636285Sbrian
6736285Sbrian	rid = 0;
6836285Sbrian	type = SYS_RES_IOPORT;
6936285Sbrian	res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
7036285Sbrian	if (res == NULL) {
7136285Sbrian		type = SYS_RES_MEMORY;
7236466Sbrian		res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
7336466Sbrian	}
7441754Sbrian	if (res == NULL)
7541754Sbrian		return (ENODEV);
7641754Sbrian
7744073Sbrian	device_set_descf(dev, "%s:%#jx", proto_isa_prefix, rman_get_start(res));
7844073Sbrian	bus_release_resource(dev, type, rid, res);
7944261Sbrian	return (proto_probe(dev, proto_isa_prefix, &proto_isa_devnames));
8044261Sbrian}
8144796Sbrian
8244796Sbrianstatic int
8346102Sbrianproto_isa_alloc(device_t dev, int type, int nrids)
8446102Sbrian{
8546102Sbrian	struct resource *res;
86	struct proto_softc *sc;
87	int count, rid;
88
89	sc = device_get_softc(dev);
90	count = 0;
91	for (rid = 0; rid < nrids; rid++) {
92		res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
93		if (res == NULL)
94			break;
95		proto_add_resource(sc, type, rid, res);
96		count++;
97	}
98	if (type == SYS_RES_DRQ && count > 0)
99		proto_add_resource(sc, PROTO_RES_BUSDMA, 0, NULL);
100	return (count);
101}
102
103static int
104proto_isa_attach(device_t dev)
105{
106
107	proto_isa_alloc(dev, SYS_RES_IRQ, ISA_NIRQ);
108	proto_isa_alloc(dev, SYS_RES_DRQ, ISA_NDRQ);
109	proto_isa_alloc(dev, SYS_RES_IOPORT, ISA_NPORT);
110	proto_isa_alloc(dev, SYS_RES_MEMORY, ISA_NMEM);
111	return (proto_attach(dev));
112}
113
114DRIVER_MODULE(proto, acpi, proto_isa_driver, NULL, NULL);
115DRIVER_MODULE(proto, isa, proto_isa_driver, NULL, NULL);
116