Home | History | Annotate | Line # | Download | only in dev
grf_compat.c revision 1.14
      1  1.14       chs /*	$NetBSD: grf_compat.c,v 1.14 2005/01/15 16:00:59 chs 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.13     lukem 
     34  1.13     lukem #include <sys/cdefs.h>
     35  1.14       chs __KERNEL_RCSID(0, "$NetBSD: grf_compat.c,v 1.14 2005/01/15 16:00:59 chs Exp $");
     36   1.2    scottr 
     37   1.2    scottr #include <sys/param.h>
     38   1.2    scottr #include <sys/systm.h>
     39   1.2    scottr #include <sys/cdefs.h>
     40   1.2    scottr #include <sys/conf.h>
     41   1.2    scottr #include <sys/device.h>
     42   1.2    scottr #include <sys/errno.h>
     43   1.2    scottr #include <sys/ioctl.h>
     44   1.2    scottr #include <sys/malloc.h>
     45   1.2    scottr #include <sys/mman.h>
     46   1.2    scottr #include <sys/proc.h>
     47   1.2    scottr #include <sys/resourcevar.h>
     48   1.2    scottr #include <sys/vnode.h>
     49   1.2    scottr 
     50   1.2    scottr #include <machine/autoconf.h>
     51   1.2    scottr #include <machine/bus.h>
     52   1.2    scottr #include <machine/grfioctl.h>
     53   1.2    scottr 
     54   1.2    scottr #include <mac68k/nubus/nubus.h>
     55   1.2    scottr #include <mac68k/dev/grfvar.h>
     56   1.2    scottr #include <mac68k/dev/macfbvar.h>
     57   1.2    scottr 
     58   1.2    scottr #include <miscfs/specfs/specdev.h>
     59   1.2    scottr 
     60   1.2    scottr #include <uvm/uvm_extern.h>
     61   1.2    scottr #include <uvm/uvm_map.h>
     62   1.2    scottr 
     63   1.8   gehenna dev_type_open(grfopen);
     64   1.8   gehenna dev_type_close(grfclose);
     65   1.8   gehenna dev_type_ioctl(grfioctl);
     66   1.8   gehenna dev_type_mmap(grfmmap);
     67   1.8   gehenna 
     68   1.8   gehenna const struct cdevsw grf_cdevsw = {
     69   1.8   gehenna 	grfopen, grfclose, noread, nowrite, grfioctl,
     70  1.10  jdolecek 	nostop, notty, nopoll, grfmmap, nokqfilter,
     71   1.8   gehenna };
     72   1.2    scottr 
     73  1.14       chs void	grf_scinit(struct grf_softc *, const char *, int);
     74  1.14       chs void	grf_init(int);
     75  1.14       chs void	grfattach(int);
     76  1.14       chs int	grfmap(dev_t, struct macfb_softc *, caddr_t *, struct proc *);
     77  1.14       chs int	grfunmap(dev_t, struct macfb_softc *, caddr_t, struct proc *);
     78   1.2    scottr 
     79   1.2    scottr /* Non-private for the benefit of libkvm. */
     80   1.2    scottr struct	grf_softc *grf_softc;
     81   1.2    scottr int	numgrf = 0;
     82   1.2    scottr 
     83   1.2    scottr /*
     84   1.2    scottr  * Initialize a softc to sane defaults.
     85   1.2    scottr  */
     86   1.2    scottr void
     87  1.14       chs grf_scinit(struct grf_softc *sc, const char *name, int unit)
     88   1.2    scottr {
     89   1.2    scottr 	memset(sc, 0, sizeof(struct grf_softc));
     90   1.2    scottr 	snprintf(sc->sc_xname, sizeof(sc->sc_xname), "%s%d", name, unit);
     91   1.2    scottr 	sc->mfb_sc = NULL;
     92   1.2    scottr }
     93   1.2    scottr 
     94   1.2    scottr /*
     95   1.2    scottr  * (Re-)initialize the grf_softc block so that at least the requested
     96   1.2    scottr  * number of elements has been allocated.  If this results in more
     97   1.2    scottr  * elements than we had prior to getting here, we initialize each of
     98   1.2    scottr  * them to avoid problems down the road.
     99   1.2    scottr  */
    100   1.2    scottr void
    101  1.14       chs grf_init(int n)
    102   1.2    scottr {
    103   1.2    scottr 	struct grf_softc *sc;
    104   1.2    scottr 	int i;
    105   1.2    scottr 
    106   1.2    scottr 	if (n >= numgrf) {
    107   1.2    scottr 		i = numgrf;
    108   1.2    scottr 		numgrf = n + 1;
    109   1.2    scottr 
    110   1.2    scottr 		if (grf_softc == NULL)
    111   1.2    scottr 			sc = (struct grf_softc *)
    112   1.2    scottr 			    malloc(numgrf * sizeof(*sc),
    113   1.2    scottr 			    M_DEVBUF, M_NOWAIT);
    114   1.2    scottr 		else
    115   1.2    scottr 			sc = (struct grf_softc *)
    116   1.2    scottr 			    realloc(grf_softc, numgrf * sizeof(*sc),
    117   1.2    scottr 			    M_DEVBUF, M_NOWAIT);
    118   1.2    scottr 		if (sc == NULL) {
    119   1.2    scottr 			printf("WARNING: no memory for grf emulation\n");
    120   1.2    scottr 			if (grf_softc != NULL)
    121   1.2    scottr 				free(grf_softc, M_DEVBUF);
    122   1.2    scottr 			return;
    123   1.2    scottr 		}
    124   1.2    scottr 		grf_softc = sc;
    125   1.2    scottr 
    126   1.2    scottr 		/* Initialize per-softc structures. */
    127   1.2    scottr 		while (i < numgrf) {
    128   1.2    scottr 			grf_scinit(&grf_softc[i], "grf", i);
    129   1.2    scottr 			i++;
    130   1.2    scottr 		}
    131   1.2    scottr 	}
    132   1.2    scottr }
    133   1.2    scottr 
    134   1.2    scottr /*
    135   1.2    scottr  * Called by main() during pseudo-device attachment.  If we had a
    136   1.2    scottr  * way to configure additional grf devices later, this would actually
    137   1.2    scottr  * allocate enough space for them.  As it stands, it's nonsensical,
    138   1.2    scottr  * so other than a basic sanity check we do nothing.
    139   1.2    scottr  */
    140   1.2    scottr void
    141  1.14       chs grfattach(int n)
    142   1.2    scottr {
    143   1.2    scottr 	if (n <= 0) {
    144   1.2    scottr #ifdef DIAGNOSTIC
    145   1.2    scottr 		panic("grfattach: count <= 0");
    146   1.2    scottr #endif
    147   1.2    scottr 		return;
    148   1.2    scottr 	}
    149   1.2    scottr 
    150   1.2    scottr #if 0 /* XXX someday, if we implement a way to attach after autoconfig */
    151   1.2    scottr 	grf_init(n);
    152   1.2    scottr #endif
    153   1.2    scottr }
    154   1.2    scottr 
    155   1.2    scottr /*
    156   1.2    scottr  * Called from macfb_attach() after setting up the frame buffer.  Since
    157   1.2    scottr  * there is a 1:1 correspondence between the macfb device and the grf
    158   1.2    scottr  * device, the only bit of information we really need is the macfb_softc.
    159   1.2    scottr  */
    160   1.2    scottr void
    161  1.14       chs grf_attach(struct macfb_softc *sc, int unit)
    162   1.2    scottr {
    163  1.14       chs 
    164   1.2    scottr 	grf_init(unit);
    165   1.2    scottr 	if (unit < numgrf)
    166   1.2    scottr 		grf_softc[unit].mfb_sc = sc;
    167   1.2    scottr }
    168   1.2    scottr 
    169   1.2    scottr /*
    170   1.2    scottr  * Standard device ops
    171   1.2    scottr  */
    172   1.2    scottr int
    173  1.14       chs grfopen(dev_t dev, int flag, int mode, struct proc *p)
    174   1.2    scottr {
    175   1.2    scottr 	struct grf_softc *sc;
    176   1.2    scottr 	int unit = GRFUNIT(dev);
    177   1.2    scottr 	int rv = 0;
    178   1.2    scottr 
    179   1.2    scottr 	if (grf_softc == NULL || unit >= numgrf)
    180   1.2    scottr 		return ENXIO;
    181   1.2    scottr 
    182   1.2    scottr 	sc = &grf_softc[unit];
    183   1.2    scottr 	if (sc->mfb_sc == NULL)
    184   1.2    scottr 		rv = ENXIO;
    185   1.2    scottr 
    186   1.2    scottr 	return rv;
    187   1.2    scottr }
    188   1.2    scottr 
    189   1.2    scottr int
    190  1.14       chs grfclose(dev_t dev, int flag, int mode, struct proc *p)
    191   1.2    scottr {
    192   1.2    scottr 	struct grf_softc *sc;
    193   1.2    scottr 	int unit = GRFUNIT(dev);
    194   1.2    scottr 	int rv = 0;
    195   1.2    scottr 
    196   1.2    scottr 	if (grf_softc == NULL || unit >= numgrf)
    197   1.2    scottr 		return ENXIO;
    198   1.2    scottr 
    199   1.2    scottr 	sc = &grf_softc[unit];
    200   1.2    scottr 	if (sc->mfb_sc != NULL)
    201   1.2    scottr 		macfb_clear(sc->mfb_sc->sc_dc);	/* clear the display */
    202   1.2    scottr 	else
    203   1.2    scottr 		rv = ENXIO;
    204   1.2    scottr 
    205   1.2    scottr 	return rv;
    206   1.2    scottr }
    207   1.2    scottr 
    208   1.2    scottr int
    209  1.14       chs grfioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    210   1.2    scottr {
    211   1.2    scottr 	struct grf_softc *sc;
    212   1.2    scottr 	struct macfb_devconfig *dc;
    213   1.3    scottr #if defined(GRF_COMPAT) || (NGRF > 0)
    214   1.2    scottr 	struct grfinfo *gd;
    215   1.3    scottr #endif /* GRF_COMPAT || (NGRF > 0) */
    216   1.2    scottr 	struct grfmode *gm;
    217   1.2    scottr 	int unit = GRFUNIT(dev);
    218   1.2    scottr 	int rv;
    219   1.2    scottr 
    220   1.2    scottr 	if (grf_softc == NULL || unit >= numgrf)
    221   1.2    scottr 		return ENXIO;
    222   1.2    scottr 
    223   1.2    scottr 	sc = &grf_softc[unit];
    224   1.2    scottr 	if (sc->mfb_sc == NULL)
    225   1.2    scottr 		return ENXIO;
    226   1.2    scottr 
    227   1.2    scottr 	dc = sc->mfb_sc->sc_dc;
    228   1.2    scottr 
    229   1.2    scottr 	switch (cmd) {
    230   1.3    scottr #if defined(GRF_COMPAT) || (NGRF > 0)
    231   1.2    scottr 	case GRFIOCGINFO:
    232   1.2    scottr 		gd = (struct grfinfo *)data;
    233   1.2    scottr 		memset(gd, 0, sizeof(struct grfinfo));
    234   1.2    scottr 		gd->gd_fbaddr     = (caddr_t)dc->dc_paddr;
    235   1.2    scottr 		gd->gd_fbsize     = dc->dc_size;
    236   1.2    scottr 		gd->gd_colors     = (short)(1 << dc->dc_depth);
    237   1.2    scottr 		gd->gd_planes     = (short)dc->dc_depth;
    238   1.2    scottr 		gd->gd_fbwidth    = dc->dc_wid;
    239   1.2    scottr 		gd->gd_fbheight   = dc->dc_ht;
    240   1.2    scottr 		gd->gd_fbrowbytes = dc->dc_rowbytes;
    241   1.2    scottr 		gd->gd_dwidth     = dc->dc_raster.width;
    242   1.2    scottr 		gd->gd_dheight    = dc->dc_raster.height;
    243   1.2    scottr 		rv = 0;
    244   1.2    scottr 		break;
    245   1.3    scottr #endif /* GRF_COMPAT || (NGRF > 0) */
    246   1.2    scottr 
    247   1.2    scottr 	case GRFIOCON:
    248   1.2    scottr 	case GRFIOCOFF:
    249   1.2    scottr 		/* Nothing to do */
    250   1.2    scottr 		rv = 0;
    251   1.2    scottr 		break;
    252   1.2    scottr 
    253   1.3    scottr #if defined(GRF_COMPAT) || (NGRF > 0)
    254   1.2    scottr 	case GRFIOCMAP:
    255   1.2    scottr 		rv = grfmap(dev, sc->mfb_sc, (caddr_t *)data, p);
    256   1.2    scottr 		break;
    257   1.2    scottr 
    258   1.2    scottr 	case GRFIOCUNMAP:
    259   1.2    scottr 		rv = grfunmap(dev, sc->mfb_sc, *(caddr_t *)data, p);
    260   1.2    scottr 		break;
    261   1.3    scottr #endif /* GRF_COMPAT || (NGRF > 0) */
    262   1.2    scottr 
    263   1.2    scottr 	case GRFIOCGMODE:
    264   1.2    scottr 		gm = (struct grfmode *)data;
    265   1.2    scottr 		memset(gm, 0, sizeof(struct grfmode));
    266   1.2    scottr 		gm->fbbase   = (char *)dc->dc_vaddr;
    267   1.2    scottr 		gm->fbsize   = dc->dc_size;
    268   1.2    scottr 		gm->fboff    = dc->dc_offset;
    269   1.2    scottr 		gm->rowbytes = dc->dc_rowbytes;
    270   1.2    scottr 		gm->width    = dc->dc_wid;
    271   1.2    scottr 		gm->height   = dc->dc_ht;
    272   1.2    scottr 		gm->psize    = dc->dc_depth;
    273   1.2    scottr 		rv = 0;
    274   1.2    scottr 		break;
    275   1.2    scottr 
    276   1.2    scottr 	case GRFIOCLISTMODES:
    277   1.2    scottr 	case GRFIOCGETMODE:
    278   1.2    scottr 	case GRFIOCSETMODE:
    279   1.2    scottr 		/* NONE of these operations are (officially) supported. */
    280   1.2    scottr 	default:
    281   1.2    scottr 		rv = EINVAL;
    282   1.2    scottr 		break;
    283   1.2    scottr 	}
    284   1.2    scottr 	return rv;
    285   1.2    scottr }
    286   1.2    scottr 
    287   1.4    simonb paddr_t
    288  1.14       chs grfmmap(dev_t dev, off_t off, int prot)
    289   1.2    scottr {
    290   1.2    scottr 	struct grf_softc *sc;
    291   1.2    scottr 	struct macfb_devconfig *dc;
    292   1.4    simonb 	paddr_t addr;
    293   1.2    scottr 	int unit = GRFUNIT(dev);
    294   1.2    scottr 
    295   1.2    scottr 	if (grf_softc == NULL || unit >= numgrf)
    296   1.2    scottr 		return ENXIO;
    297   1.2    scottr 
    298   1.2    scottr 	sc = &grf_softc[unit];
    299   1.2    scottr 	if (sc->mfb_sc == NULL)
    300   1.2    scottr 		return ENXIO;
    301   1.2    scottr 
    302   1.2    scottr 	dc = sc->mfb_sc->sc_dc;
    303   1.2    scottr 
    304   1.2    scottr 	if (off >= 0 &&
    305   1.2    scottr 	    off < m68k_round_page(dc->dc_offset + dc->dc_size))
    306   1.2    scottr 		addr = m68k_btop(dc->dc_paddr + off);
    307   1.2    scottr 	else
    308   1.2    scottr 		addr = (-1);	/* XXX bogus */
    309   1.2    scottr 
    310   1.4    simonb 	return addr;
    311   1.2    scottr }
    312   1.2    scottr 
    313   1.2    scottr int
    314  1.14       chs grfmap(dev_t dev, struct macfb_softc *sc, caddr_t *addrp, struct proc *p)
    315   1.2    scottr {
    316   1.2    scottr 	struct specinfo si;
    317   1.2    scottr 	struct vnode vn;
    318   1.2    scottr 	u_long len;
    319   1.2    scottr 	int error, flags;
    320   1.2    scottr 
    321   1.2    scottr 	len = m68k_round_page(sc->sc_dc->dc_offset + sc->sc_dc->dc_size);
    322  1.12    atatat 	*addrp = (caddr_t)VM_DEFAULT_ADDRESS(p->p_vmspace->vm_daddr, len);
    323   1.2    scottr 	flags = MAP_SHARED | MAP_FIXED;
    324   1.2    scottr 
    325   1.2    scottr 	vn.v_type = VCHR;		/* XXX */
    326   1.2    scottr 	vn.v_specinfo = &si;		/* XXX */
    327   1.2    scottr 	vn.v_rdev = dev;		/* XXX */
    328   1.2    scottr 
    329   1.2    scottr 	error = uvm_mmap(&p->p_vmspace->vm_map, (vaddr_t *)addrp,
    330   1.2    scottr 	    (vsize_t)len, VM_PROT_ALL, VM_PROT_ALL,
    331   1.2    scottr 	    flags, (caddr_t)&vn, 0, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
    332   1.2    scottr 
    333   1.2    scottr 	/* Offset into page: */
    334   1.2    scottr 	*addrp += sc->sc_dc->dc_offset;
    335   1.2    scottr 
    336   1.2    scottr 	return (error);
    337   1.2    scottr }
    338   1.2    scottr 
    339   1.2    scottr int
    340  1.14       chs grfunmap(dev_t dev, struct macfb_softc *sc, caddr_t addr, struct proc *p)
    341   1.2    scottr {
    342   1.2    scottr 	vm_size_t size;
    343   1.2    scottr 
    344   1.2    scottr 	addr -= sc->sc_dc->dc_offset;
    345   1.2    scottr 
    346   1.2    scottr 	if (addr <= 0)
    347   1.2    scottr 		return (-1);
    348   1.2    scottr 
    349   1.2    scottr 	size = m68k_round_page(sc->sc_dc->dc_offset + sc->sc_dc->dc_size);
    350   1.6       chs 	uvm_unmap(&p->p_vmspace->vm_map, (vaddr_t)addr, (vaddr_t)addr + size);
    351   1.6       chs 	return 0;
    352   1.2    scottr }
    353