Home | History | Annotate | Line # | Download | only in dev
grf_compat.c revision 1.7.2.2
      1  1.7.2.2  jdolecek /*	$NetBSD: grf_compat.c,v 1.7.2.2 2002/10/10 18:33:46 jdolecek Exp $	*/
      2      1.2    scottr 
      3      1.2    scottr /*
      4      1.2    scottr  * Copyright (C) 1999 Scott Reynolds
      5      1.2    scottr  * All rights reserved.
      6      1.2    scottr  *
      7      1.2    scottr  * Redistribution and use in source and binary forms, with or without
      8      1.2    scottr  * modification, are permitted provided that the following conditions
      9      1.2    scottr  * are met:
     10      1.2    scottr  * 1. Redistributions of source code must retain the above copyright
     11      1.2    scottr  *    notice, this list of conditions and the following disclaimer.
     12      1.2    scottr  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.2    scottr  *    notice, this list of conditions and the following disclaimer in the
     14      1.2    scottr  *    documentation and/or other materials provided with the distribution.
     15      1.2    scottr  * 3. The name of the author may not be used to endorse or promote products
     16      1.2    scottr  *    derived from this software without specific prior written permission.
     17      1.2    scottr  *
     18      1.2    scottr  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19      1.2    scottr  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20      1.2    scottr  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21      1.2    scottr  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22      1.2    scottr  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23      1.2    scottr  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24      1.2    scottr  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25      1.2    scottr  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26      1.2    scottr  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27      1.2    scottr  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28      1.2    scottr  */
     29      1.2    scottr 
     30      1.2    scottr /*
     31      1.2    scottr  * macfb compatibility with legacy grf devices
     32      1.2    scottr  */
     33      1.2    scottr 
     34      1.2    scottr #include <sys/param.h>
     35      1.2    scottr #include <sys/systm.h>
     36      1.2    scottr #include <sys/cdefs.h>
     37      1.2    scottr #include <sys/conf.h>
     38      1.2    scottr #include <sys/device.h>
     39      1.2    scottr #include <sys/errno.h>
     40      1.2    scottr #include <sys/ioctl.h>
     41      1.2    scottr #include <sys/malloc.h>
     42      1.2    scottr #include <sys/mman.h>
     43      1.2    scottr #include <sys/proc.h>
     44      1.2    scottr #include <sys/resourcevar.h>
     45      1.2    scottr #include <sys/vnode.h>
     46      1.2    scottr 
     47      1.2    scottr #include <machine/autoconf.h>
     48      1.2    scottr #include <machine/bus.h>
     49      1.2    scottr #include <machine/grfioctl.h>
     50      1.2    scottr 
     51      1.2    scottr #include <mac68k/nubus/nubus.h>
     52      1.2    scottr #include <mac68k/dev/grfvar.h>
     53      1.2    scottr #include <mac68k/dev/macfbvar.h>
     54      1.2    scottr 
     55      1.2    scottr #include <miscfs/specfs/specdev.h>
     56      1.2    scottr 
     57      1.2    scottr #include <uvm/uvm_extern.h>
     58      1.2    scottr #include <uvm/uvm_map.h>
     59      1.2    scottr 
     60  1.7.2.2  jdolecek dev_type_open(grfopen);
     61  1.7.2.2  jdolecek dev_type_close(grfclose);
     62  1.7.2.2  jdolecek dev_type_ioctl(grfioctl);
     63  1.7.2.2  jdolecek dev_type_poll(grfpoll);
     64  1.7.2.2  jdolecek dev_type_mmap(grfmmap);
     65  1.7.2.2  jdolecek dev_type_kqfilter(grfkqfilter);
     66  1.7.2.2  jdolecek 
     67  1.7.2.2  jdolecek const struct cdevsw grf_cdevsw = {
     68  1.7.2.2  jdolecek 	grfopen, grfclose, noread, nowrite, grfioctl,
     69  1.7.2.2  jdolecek 	nostop, notty, grfpoll, grfmmap, grfkqfilter,
     70  1.7.2.2  jdolecek };
     71      1.2    scottr 
     72      1.2    scottr void	grf_scinit __P((struct grf_softc *, const char *, int));
     73      1.2    scottr void	grf_init __P((int));
     74      1.2    scottr void	grfattach __P((int));
     75      1.2    scottr int	grfmap __P((dev_t, struct macfb_softc *, caddr_t *, struct proc *));
     76      1.2    scottr int	grfunmap __P((dev_t, struct macfb_softc *, caddr_t, struct proc *));
     77      1.2    scottr 
     78      1.2    scottr /* Non-private for the benefit of libkvm. */
     79      1.2    scottr struct	grf_softc *grf_softc;
     80      1.2    scottr int	numgrf = 0;
     81      1.2    scottr 
     82      1.2    scottr /*
     83      1.2    scottr  * Initialize a softc to sane defaults.
     84      1.2    scottr  */
     85      1.2    scottr void
     86      1.2    scottr grf_scinit(sc, name, unit)
     87      1.2    scottr 	struct grf_softc *sc;
     88      1.2    scottr 	const char *name;
     89      1.2    scottr 	int unit;
     90      1.2    scottr {
     91      1.2    scottr 	memset(sc, 0, sizeof(struct grf_softc));
     92      1.2    scottr 	snprintf(sc->sc_xname, sizeof(sc->sc_xname), "%s%d", name, unit);
     93      1.2    scottr 	sc->mfb_sc = NULL;
     94      1.2    scottr }
     95      1.2    scottr 
     96      1.2    scottr /*
     97      1.2    scottr  * (Re-)initialize the grf_softc block so that at least the requested
     98      1.2    scottr  * number of elements has been allocated.  If this results in more
     99      1.2    scottr  * elements than we had prior to getting here, we initialize each of
    100      1.2    scottr  * them to avoid problems down the road.
    101      1.2    scottr  */
    102      1.2    scottr void
    103      1.2    scottr grf_init(n)
    104      1.2    scottr 	int n;
    105      1.2    scottr {
    106      1.2    scottr 	struct grf_softc *sc;
    107      1.2    scottr 	int i;
    108      1.2    scottr 
    109      1.2    scottr 	if (n >= numgrf) {
    110      1.2    scottr 		i = numgrf;
    111      1.2    scottr 		numgrf = n + 1;
    112      1.2    scottr 
    113      1.2    scottr 		if (grf_softc == NULL)
    114      1.2    scottr 			sc = (struct grf_softc *)
    115      1.2    scottr 			    malloc(numgrf * sizeof(*sc),
    116      1.2    scottr 			    M_DEVBUF, M_NOWAIT);
    117      1.2    scottr 		else
    118      1.2    scottr 			sc = (struct grf_softc *)
    119      1.2    scottr 			    realloc(grf_softc, numgrf * sizeof(*sc),
    120      1.2    scottr 			    M_DEVBUF, M_NOWAIT);
    121      1.2    scottr 		if (sc == NULL) {
    122      1.2    scottr 			printf("WARNING: no memory for grf emulation\n");
    123      1.2    scottr 			if (grf_softc != NULL)
    124      1.2    scottr 				free(grf_softc, M_DEVBUF);
    125      1.2    scottr 			return;
    126      1.2    scottr 		}
    127      1.2    scottr 		grf_softc = sc;
    128      1.2    scottr 
    129      1.2    scottr 		/* Initialize per-softc structures. */
    130      1.2    scottr 		while (i < numgrf) {
    131      1.2    scottr 			grf_scinit(&grf_softc[i], "grf", i);
    132      1.2    scottr 			i++;
    133      1.2    scottr 		}
    134      1.2    scottr 	}
    135      1.2    scottr }
    136      1.2    scottr 
    137      1.2    scottr /*
    138      1.2    scottr  * Called by main() during pseudo-device attachment.  If we had a
    139      1.2    scottr  * way to configure additional grf devices later, this would actually
    140      1.2    scottr  * allocate enough space for them.  As it stands, it's nonsensical,
    141      1.2    scottr  * so other than a basic sanity check we do nothing.
    142      1.2    scottr  */
    143      1.2    scottr void
    144      1.2    scottr grfattach(n)
    145      1.2    scottr 	int n;
    146      1.2    scottr {
    147      1.2    scottr 	if (n <= 0) {
    148      1.2    scottr #ifdef DIAGNOSTIC
    149      1.2    scottr 		panic("grfattach: count <= 0");
    150      1.2    scottr #endif
    151      1.2    scottr 		return;
    152      1.2    scottr 	}
    153      1.2    scottr 
    154      1.2    scottr #if 0 /* XXX someday, if we implement a way to attach after autoconfig */
    155      1.2    scottr 	grf_init(n);
    156      1.2    scottr #endif
    157      1.2    scottr }
    158      1.2    scottr 
    159      1.2    scottr /*
    160      1.2    scottr  * Called from macfb_attach() after setting up the frame buffer.  Since
    161      1.2    scottr  * there is a 1:1 correspondence between the macfb device and the grf
    162      1.2    scottr  * device, the only bit of information we really need is the macfb_softc.
    163      1.2    scottr  */
    164      1.2    scottr void
    165      1.2    scottr grf_attach(sc, unit)
    166      1.2    scottr 	struct macfb_softc *sc;
    167      1.2    scottr 	int unit;
    168      1.2    scottr {
    169      1.2    scottr 	grf_init(unit);
    170      1.2    scottr 
    171      1.2    scottr 	if (unit < numgrf)
    172      1.2    scottr 		grf_softc[unit].mfb_sc = sc;
    173      1.2    scottr }
    174      1.2    scottr 
    175      1.2    scottr /*
    176      1.2    scottr  * Standard device ops
    177      1.2    scottr  */
    178      1.2    scottr int
    179      1.2    scottr grfopen(dev, flag, mode, p)
    180      1.2    scottr 	dev_t dev;
    181      1.2    scottr 	int flag;
    182      1.2    scottr 	int mode;
    183      1.2    scottr 	struct proc *p;
    184      1.2    scottr {
    185      1.2    scottr 	struct grf_softc *sc;
    186      1.2    scottr 	int unit = GRFUNIT(dev);
    187      1.2    scottr 	int rv = 0;
    188      1.2    scottr 
    189      1.2    scottr 	if (grf_softc == NULL || unit >= numgrf)
    190      1.2    scottr 		return ENXIO;
    191      1.2    scottr 
    192      1.2    scottr 	sc = &grf_softc[unit];
    193      1.2    scottr 	if (sc->mfb_sc == NULL)
    194      1.2    scottr 		rv = ENXIO;
    195      1.2    scottr 
    196      1.2    scottr 	return rv;
    197      1.2    scottr }
    198      1.2    scottr 
    199      1.2    scottr int
    200      1.2    scottr grfclose(dev, flag, mode, p)
    201      1.2    scottr 	dev_t dev;
    202      1.2    scottr 	int flag;
    203      1.2    scottr 	int mode;
    204      1.2    scottr 	struct proc *p;
    205      1.2    scottr {
    206      1.2    scottr 	struct grf_softc *sc;
    207      1.2    scottr 	int unit = GRFUNIT(dev);
    208      1.2    scottr 	int rv = 0;
    209      1.2    scottr 
    210      1.2    scottr 	if (grf_softc == NULL || unit >= numgrf)
    211      1.2    scottr 		return ENXIO;
    212      1.2    scottr 
    213      1.2    scottr 	sc = &grf_softc[unit];
    214      1.2    scottr 	if (sc->mfb_sc != NULL)
    215      1.2    scottr 		macfb_clear(sc->mfb_sc->sc_dc);	/* clear the display */
    216      1.2    scottr 	else
    217      1.2    scottr 		rv = ENXIO;
    218      1.2    scottr 
    219      1.2    scottr 	return rv;
    220      1.2    scottr }
    221      1.2    scottr 
    222      1.2    scottr int
    223      1.2    scottr grfioctl(dev, cmd, data, flag, p)
    224      1.2    scottr 	dev_t dev;
    225      1.2    scottr 	u_long cmd;
    226      1.2    scottr 	caddr_t data;
    227      1.2    scottr 	int flag;
    228      1.2    scottr 	struct proc *p;
    229      1.2    scottr {
    230      1.2    scottr 	struct grf_softc *sc;
    231      1.2    scottr 	struct macfb_devconfig *dc;
    232      1.3    scottr #if defined(GRF_COMPAT) || (NGRF > 0)
    233      1.2    scottr 	struct grfinfo *gd;
    234      1.3    scottr #endif /* GRF_COMPAT || (NGRF > 0) */
    235      1.2    scottr 	struct grfmode *gm;
    236      1.2    scottr 	int unit = GRFUNIT(dev);
    237      1.2    scottr 	int rv;
    238      1.2    scottr 
    239      1.2    scottr 	if (grf_softc == NULL || unit >= numgrf)
    240      1.2    scottr 		return ENXIO;
    241      1.2    scottr 
    242      1.2    scottr 	sc = &grf_softc[unit];
    243      1.2    scottr 	if (sc->mfb_sc == NULL)
    244      1.2    scottr 		return ENXIO;
    245      1.2    scottr 
    246      1.2    scottr 	dc = sc->mfb_sc->sc_dc;
    247      1.2    scottr 
    248      1.2    scottr 	switch (cmd) {
    249      1.3    scottr #if defined(GRF_COMPAT) || (NGRF > 0)
    250      1.2    scottr 	case GRFIOCGINFO:
    251      1.2    scottr 		gd = (struct grfinfo *)data;
    252      1.2    scottr 		memset(gd, 0, sizeof(struct grfinfo));
    253      1.2    scottr 		gd->gd_fbaddr     = (caddr_t)dc->dc_paddr;
    254      1.2    scottr 		gd->gd_fbsize     = dc->dc_size;
    255      1.2    scottr 		gd->gd_colors     = (short)(1 << dc->dc_depth);
    256      1.2    scottr 		gd->gd_planes     = (short)dc->dc_depth;
    257      1.2    scottr 		gd->gd_fbwidth    = dc->dc_wid;
    258      1.2    scottr 		gd->gd_fbheight   = dc->dc_ht;
    259      1.2    scottr 		gd->gd_fbrowbytes = dc->dc_rowbytes;
    260      1.2    scottr 		gd->gd_dwidth     = dc->dc_raster.width;
    261      1.2    scottr 		gd->gd_dheight    = dc->dc_raster.height;
    262      1.2    scottr 		rv = 0;
    263      1.2    scottr 		break;
    264      1.3    scottr #endif /* GRF_COMPAT || (NGRF > 0) */
    265      1.2    scottr 
    266      1.2    scottr 	case GRFIOCON:
    267      1.2    scottr 	case GRFIOCOFF:
    268      1.2    scottr 		/* Nothing to do */
    269      1.2    scottr 		rv = 0;
    270      1.2    scottr 		break;
    271      1.2    scottr 
    272      1.3    scottr #if defined(GRF_COMPAT) || (NGRF > 0)
    273      1.2    scottr 	case GRFIOCMAP:
    274      1.2    scottr 		rv = grfmap(dev, sc->mfb_sc, (caddr_t *)data, p);
    275      1.2    scottr 		break;
    276      1.2    scottr 
    277      1.2    scottr 	case GRFIOCUNMAP:
    278      1.2    scottr 		rv = grfunmap(dev, sc->mfb_sc, *(caddr_t *)data, p);
    279      1.2    scottr 		break;
    280      1.3    scottr #endif /* GRF_COMPAT || (NGRF > 0) */
    281      1.2    scottr 
    282      1.2    scottr 	case GRFIOCGMODE:
    283      1.2    scottr 		gm = (struct grfmode *)data;
    284      1.2    scottr 		memset(gm, 0, sizeof(struct grfmode));
    285      1.2    scottr 		gm->fbbase   = (char *)dc->dc_vaddr;
    286      1.2    scottr 		gm->fbsize   = dc->dc_size;
    287      1.2    scottr 		gm->fboff    = dc->dc_offset;
    288      1.2    scottr 		gm->rowbytes = dc->dc_rowbytes;
    289      1.2    scottr 		gm->width    = dc->dc_wid;
    290      1.2    scottr 		gm->height   = dc->dc_ht;
    291      1.2    scottr 		gm->psize    = dc->dc_depth;
    292      1.2    scottr 		rv = 0;
    293      1.2    scottr 		break;
    294      1.2    scottr 
    295      1.2    scottr 	case GRFIOCLISTMODES:
    296      1.2    scottr 	case GRFIOCGETMODE:
    297      1.2    scottr 	case GRFIOCSETMODE:
    298      1.2    scottr 		/* NONE of these operations are (officially) supported. */
    299      1.2    scottr 	default:
    300      1.2    scottr 		rv = EINVAL;
    301      1.2    scottr 		break;
    302      1.2    scottr 	}
    303      1.2    scottr 	return rv;
    304      1.2    scottr }
    305      1.2    scottr 
    306      1.2    scottr int
    307      1.2    scottr grfpoll(dev, events, p)
    308      1.2    scottr 	dev_t dev;
    309      1.2    scottr 	int events;
    310      1.2    scottr 	struct proc *p;
    311      1.2    scottr {
    312      1.2    scottr 	return EINVAL;
    313  1.7.2.1  jdolecek }
    314  1.7.2.1  jdolecek 
    315  1.7.2.1  jdolecek int
    316  1.7.2.1  jdolecek grfkqfilter(dev_t dev, struct knote *kn)
    317  1.7.2.1  jdolecek {
    318  1.7.2.1  jdolecek 
    319  1.7.2.1  jdolecek 	/* XXXLUKEM (thorpej): not supported -- why is poll? */
    320  1.7.2.1  jdolecek 	return (1);
    321      1.2    scottr }
    322      1.2    scottr 
    323      1.4    simonb paddr_t
    324      1.2    scottr grfmmap(dev, off, prot)
    325      1.2    scottr 	dev_t dev;
    326      1.4    simonb 	off_t off;
    327      1.2    scottr 	int prot;
    328      1.2    scottr {
    329      1.2    scottr 	struct grf_softc *sc;
    330      1.2    scottr 	struct macfb_devconfig *dc;
    331      1.4    simonb 	paddr_t addr;
    332      1.2    scottr 	int unit = GRFUNIT(dev);
    333      1.2    scottr 
    334      1.2    scottr 	if (grf_softc == NULL || unit >= numgrf)
    335      1.2    scottr 		return ENXIO;
    336      1.2    scottr 
    337      1.2    scottr 	sc = &grf_softc[unit];
    338      1.2    scottr 	if (sc->mfb_sc == NULL)
    339      1.2    scottr 		return ENXIO;
    340      1.2    scottr 
    341      1.2    scottr 	dc = sc->mfb_sc->sc_dc;
    342      1.2    scottr 
    343      1.2    scottr 	if (off >= 0 &&
    344      1.2    scottr 	    off < m68k_round_page(dc->dc_offset + dc->dc_size))
    345      1.2    scottr 		addr = m68k_btop(dc->dc_paddr + off);
    346      1.2    scottr 	else
    347      1.2    scottr 		addr = (-1);	/* XXX bogus */
    348      1.2    scottr 
    349      1.4    simonb 	return addr;
    350      1.2    scottr }
    351      1.2    scottr 
    352      1.2    scottr int
    353      1.2    scottr grfmap(dev, sc, addrp, p)
    354      1.2    scottr 	dev_t dev;
    355      1.2    scottr 	struct macfb_softc *sc;
    356      1.2    scottr 	caddr_t *addrp;
    357      1.2    scottr 	struct proc *p;
    358      1.2    scottr {
    359      1.2    scottr 	struct specinfo si;
    360      1.2    scottr 	struct vnode vn;
    361      1.2    scottr 	u_long len;
    362      1.2    scottr 	int error, flags;
    363      1.2    scottr 
    364      1.2    scottr 	*addrp = (caddr_t)sc->sc_dc->dc_paddr;
    365      1.2    scottr 	len = m68k_round_page(sc->sc_dc->dc_offset + sc->sc_dc->dc_size);
    366      1.2    scottr 	flags = MAP_SHARED | MAP_FIXED;
    367      1.2    scottr 
    368      1.2    scottr 	vn.v_type = VCHR;		/* XXX */
    369      1.2    scottr 	vn.v_specinfo = &si;		/* XXX */
    370      1.2    scottr 	vn.v_rdev = dev;		/* XXX */
    371      1.2    scottr 
    372      1.2    scottr 	error = uvm_mmap(&p->p_vmspace->vm_map, (vaddr_t *)addrp,
    373      1.2    scottr 	    (vsize_t)len, VM_PROT_ALL, VM_PROT_ALL,
    374      1.2    scottr 	    flags, (caddr_t)&vn, 0, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
    375      1.2    scottr 
    376      1.2    scottr 	/* Offset into page: */
    377      1.2    scottr 	*addrp += sc->sc_dc->dc_offset;
    378      1.2    scottr 
    379      1.2    scottr 	return (error);
    380      1.2    scottr }
    381      1.2    scottr 
    382      1.2    scottr int
    383      1.2    scottr grfunmap(dev, sc, addr, p)
    384      1.2    scottr 	dev_t dev;
    385      1.2    scottr 	struct macfb_softc *sc;
    386      1.2    scottr 	caddr_t addr;
    387      1.2    scottr 	struct proc *p;
    388      1.2    scottr {
    389      1.2    scottr 	vm_size_t size;
    390      1.2    scottr 
    391      1.2    scottr 	addr -= sc->sc_dc->dc_offset;
    392      1.2    scottr 
    393      1.2    scottr 	if (addr <= 0)
    394      1.2    scottr 		return (-1);
    395      1.2    scottr 
    396      1.2    scottr 	size = m68k_round_page(sc->sc_dc->dc_offset + sc->sc_dc->dc_size);
    397      1.6       chs 	uvm_unmap(&p->p_vmspace->vm_map, (vaddr_t)addr, (vaddr_t)addr + size);
    398      1.6       chs 	return 0;
    399      1.2    scottr }
    400