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