Change the kernel dev_t, representing a pointer to a specinfo structure,
[dragonfly.git] / sys / bus / isa / vga_isa.c
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/isa/vga_isa.c,v 1.17 2000/01/29 15:08:56 peter Exp $
27  * $DragonFly: src/sys/bus/isa/vga_isa.c,v 1.12 2006/09/10 01:26:33 dillon Exp $
28  */
29
30 #include "opt_vga.h"
31 #include "opt_fb.h"
32 #include "opt_syscons.h"        /* should be removed in the future, XXX */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/conf.h>
39 #include <sys/device.h>
40 #include <sys/bus.h>
41 #include <sys/fbio.h>
42
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45
46 #include <sys/rman.h>
47
48 #include <vm/vm.h>
49 #include <vm/pmap.h>
50
51 #include <machine/md_var.h>
52 #include <machine/pc/bios.h>
53
54 #include <dev/video/fb/fbreg.h>
55 #include <dev/video/fb/vgareg.h>
56
57 #include "isareg.h"
58 #include "isavar.h"
59
60 #define VGA_SOFTC(unit)         \
61         ((vga_softc_t *)devclass_get_softc(isavga_devclass, unit))
62
63 static devclass_t       isavga_devclass;
64
65 static int              isavga_probe(device_t dev);
66 static int              isavga_attach(device_t dev);
67 static int              isavga_suspend(device_t dev);
68 static int              isavga_resume(device_t dev);
69
70 static device_method_t isavga_methods[] = {
71         DEVMETHOD(device_probe,         isavga_probe),
72         DEVMETHOD(device_attach,        isavga_attach),
73         DEVMETHOD(device_suspend,       isavga_suspend),
74         DEVMETHOD(device_resume,        isavga_resume),
75
76         DEVMETHOD(bus_print_child,      bus_generic_print_child),
77         { 0, 0 }
78 };
79
80 static driver_t isavga_driver = {
81         VGA_DRIVER_NAME,
82         isavga_methods,
83         sizeof(vga_softc_t),
84 };
85
86 DRIVER_MODULE(vga, isa, isavga_driver, isavga_devclass, 0, 0);
87
88 #ifdef FB_INSTALL_CDEV
89
90 static d_open_t         isavga_open;
91 static d_close_t        isavga_close;
92 static d_read_t         isavga_read;
93 static d_write_t        isavga_write;
94 static d_ioctl_t        isavga_ioctl;
95 static d_mmap_t         isavga_mmap;
96
97 static struct dev_ops isavga_ops = {
98         { VGA_DRIVER_NAME, -1, 0 },
99         .d_open =       isavga_open,
100         .d_close =      isavga_close,
101         .d_read =       isavga_read,
102         .d_write =      isavga_write,
103         .d_ioctl =      isavga_ioctl,
104         .d_mmap =       isavga_mmap,
105 };
106
107 #endif /* FB_INSTALL_CDEV */
108
109 static int
110 isavga_probe(device_t dev)
111 {
112         video_adapter_t adp;
113         device_t bus;
114         int error;
115
116         /* No pnp support */
117         if (isa_get_vendorid(dev))
118                 return (ENXIO);
119
120         device_set_desc(dev, "Generic ISA VGA");
121         error = vga_probe_unit(device_get_unit(dev), &adp, device_get_flags(dev));
122         if (error == 0) {
123                 bus = device_get_parent(dev);
124                 bus_set_resource(dev, SYS_RES_IOPORT, 0,
125                                  adp.va_io_base, adp.va_io_size);
126                 bus_set_resource(dev, SYS_RES_MEMORY, 0,
127                                  adp.va_mem_base, adp.va_mem_size);
128 #if 0
129                 isa_set_port(dev, adp.va_io_base);
130                 isa_set_portsize(dev, adp.va_io_size);
131                 isa_set_maddr(dev, adp.va_mem_base);
132                 isa_set_msize(dev, adp.va_mem_size);
133 #endif
134         }
135         return error;
136 }
137
138 static int
139 isavga_attach(device_t dev)
140 {
141         vga_softc_t *sc;
142         struct resource *port;
143         struct resource *mem;
144         int unit;
145         int rid;
146         int error;
147
148         unit = device_get_unit(dev);
149         sc = device_get_softc(dev);
150
151         rid = 0;
152         port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
153                                   0, ~0, 0, RF_ACTIVE | RF_SHAREABLE);
154         rid = 0;
155         mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
156                                  0, ~0, 0, RF_ACTIVE | RF_SHAREABLE);
157
158         error = vga_attach_unit(unit, sc, device_get_flags(dev));
159         if (error)
160                 return error;
161
162 #ifdef FB_INSTALL_CDEV
163         /* attach a virtual frame buffer device */
164         dev_ops_add(&isavga_ops, VGA_MKMINOR(-1), VGA_MKMINOR(unit));
165         sc->devt = make_dev(&isavga_ops, VGA_MKMINOR(unit), 0, 0, 02660, "vga%x", VGA_MKMINOR(unit));
166         reference_dev(sc->devt);
167         error = fb_attach(sc->devt, sc->adp);
168         if (error)
169                 return error;
170 #endif /* FB_INSTALL_CDEV */
171
172         if (bootverbose)
173                 (*vidsw[sc->adp->va_index]->diag)(sc->adp, bootverbose);
174
175 #if experimental
176         device_add_child(dev, "fb", -1);
177         bus_generic_attach(dev);
178 #endif
179
180         return 0;
181 }
182
183 static int
184 isavga_suspend(device_t dev)
185 {
186         vga_softc_t *sc;
187         int err, nbytes;
188
189         sc = device_get_softc(dev);
190         err = bus_generic_suspend(dev);
191         if (err)
192                 return (err);
193
194         /* Save the video state across the suspend. */
195         if (sc->state_buf != NULL) {
196                 kfree(sc->state_buf, M_TEMP);
197                 sc->state_buf = NULL;
198         }
199         nbytes = (*vidsw[sc->adp->va_index]->save_state)(sc->adp, NULL, 0);
200         if (nbytes <= 0)
201                 return (0);
202         sc->state_buf = kmalloc(nbytes, M_TEMP, M_NOWAIT | M_ZERO);
203         if (sc->state_buf == NULL)
204                 return (0);
205         if (bootverbose)
206                 device_printf(dev, "saving %d bytes of video state\n", nbytes);
207         if ((*vidsw[sc->adp->va_index]->save_state)(sc->adp, sc->state_buf,
208             nbytes) != 0) {
209                 device_printf(dev, "failed to save state (nbytes=%d)\n",
210                     nbytes);
211                 kfree(sc->state_buf, M_TEMP);
212                 sc->state_buf = NULL;
213         }
214         return (0);
215 }
216
217 static int
218 isavga_resume(device_t dev)
219 {
220         vga_softc_t *sc;
221
222         sc = device_get_softc(dev);
223         if (sc->state_buf != NULL) {
224                 if ((*vidsw[sc->adp->va_index]->load_state)(sc->adp,
225                     sc->state_buf) != 0)
226                         device_printf(dev, "failed to reload state\n");
227                 kfree(sc->state_buf, M_TEMP);
228                 sc->state_buf = NULL;
229         }
230
231         bus_generic_resume(dev);
232         return 0;
233 }
234
235 #ifdef FB_INSTALL_CDEV
236
237 static int
238 isavga_open(struct dev_open_args *ap)
239 {
240         cdev_t dev = ap->a_head.a_dev;
241
242         return vga_open(dev, VGA_SOFTC(VGA_UNIT(dev)), ap->a_oflags,
243                         ap->a_devtype, ap->a_cred);
244 }
245
246 static int
247 isavga_close(struct dev_close_args *ap)
248 {
249         cdev_t dev = ap->a_head.a_dev;
250
251         return vga_close(dev, VGA_SOFTC(VGA_UNIT(dev)),
252                          ap->a_fflag, ap->a_devtype);
253 }
254
255 static int
256 isavga_read(struct dev_read_args *ap)
257 {
258         cdev_t dev = ap->a_head.a_dev;
259
260         return vga_read(dev, VGA_SOFTC(VGA_UNIT(dev)), ap->a_uio, ap->a_ioflag);
261 }
262
263 static int
264 isavga_write(struct dev_write_args *ap)
265 {
266         cdev_t dev = ap->a_head.a_dev;
267
268         return vga_write(dev, VGA_SOFTC(VGA_UNIT(dev)), ap->a_uio, ap->a_ioflag);
269 }
270
271 static int
272 isavga_ioctl(struct dev_ioctl_args *ap)
273 {
274         cdev_t dev = ap->a_head.a_dev;
275
276         return vga_ioctl(dev, VGA_SOFTC(VGA_UNIT(dev)), ap->a_cmd, ap->a_data, ap->a_fflag, ap->a_cred);
277 }
278
279 static int
280 isavga_mmap(struct dev_mmap_args *ap)
281 {
282         cdev_t dev = ap->a_head.a_dev;
283
284         ap->a_result = vga_mmap(dev, VGA_SOFTC(VGA_UNIT(dev)), ap->a_offset, ap->a_nprot);
285         return(0);
286 }
287
288 #endif /* FB_INSTALL_CDEV */