Home | History | Annotate | Line # | Download | only in dev
ofb.c revision 1.61
      1  1.61  macallan /*	$NetBSD: ofb.c,v 1.61 2007/08/05 04:36:18 macallan Exp $	*/
      2   1.1    tsubai 
      3   1.1    tsubai /*
      4   1.1    tsubai  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5   1.1    tsubai  * All rights reserved.
      6   1.1    tsubai  *
      7   1.1    tsubai  * Author: Chris G. Demetriou
      8   1.1    tsubai  *
      9   1.1    tsubai  * Permission to use, copy, modify and distribute this software and
     10   1.1    tsubai  * its documentation is hereby granted, provided that both the copyright
     11   1.1    tsubai  * notice and this permission notice appear in all copies of the
     12   1.1    tsubai  * software, derivative works or modified versions, and any portions
     13   1.1    tsubai  * thereof, and that both notices appear in supporting documentation.
     14   1.1    tsubai  *
     15   1.1    tsubai  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16   1.1    tsubai  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17   1.1    tsubai  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18   1.1    tsubai  *
     19   1.1    tsubai  * Carnegie Mellon requests users of this software to return to
     20   1.1    tsubai  *
     21   1.1    tsubai  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22   1.1    tsubai  *  School of Computer Science
     23   1.1    tsubai  *  Carnegie Mellon University
     24   1.1    tsubai  *  Pittsburgh PA 15213-3890
     25   1.1    tsubai  *
     26   1.1    tsubai  * any improvements or extensions that they make and grant Carnegie the
     27   1.1    tsubai  * rights to redistribute these changes.
     28   1.1    tsubai  */
     29  1.36     lukem 
     30  1.36     lukem #include <sys/cdefs.h>
     31  1.61  macallan __KERNEL_RCSID(0, "$NetBSD: ofb.c,v 1.61 2007/08/05 04:36:18 macallan Exp $");
     32   1.1    tsubai 
     33   1.1    tsubai #include <sys/param.h>
     34   1.1    tsubai #include <sys/buf.h>
     35   1.1    tsubai #include <sys/conf.h>
     36   1.1    tsubai #include <sys/device.h>
     37   1.1    tsubai #include <sys/ioctl.h>
     38   1.1    tsubai #include <sys/kernel.h>
     39   1.1    tsubai #include <sys/malloc.h>
     40   1.1    tsubai #include <sys/systm.h>
     41  1.49      elad #include <sys/kauth.h>
     42  1.59        ad #include <sys/lwp.h>
     43   1.1    tsubai 
     44  1.15       mrg #include <uvm/uvm_extern.h>
     45  1.11    tsubai 
     46   1.3    tsubai #include <dev/pci/pcidevs.h>
     47   1.1    tsubai #include <dev/pci/pcireg.h>
     48   1.1    tsubai #include <dev/pci/pcivar.h>
     49  1.27   nathanw #include <dev/pci/pciio.h>
     50   1.1    tsubai 
     51   1.1    tsubai #include <dev/wscons/wsconsio.h>
     52   1.1    tsubai #include <dev/wscons/wsdisplayvar.h>
     53  1.11    tsubai #include <dev/rasops/rasops.h>
     54  1.45  macallan #include <dev/wsfont/wsfont.h>
     55   1.1    tsubai 
     56  1.20      matt #include <dev/ofw/openfirm.h>
     57  1.13    tsubai #include <dev/ofw/ofw_pci.h>
     58  1.13    tsubai 
     59   1.1    tsubai #include <machine/bus.h>
     60  1.20      matt #include <machine/autoconf.h>
     61   1.1    tsubai #include <machine/grfioctl.h>
     62  1.37      matt 
     63  1.52  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     64   1.1    tsubai 
     65   1.1    tsubai #include <macppc/dev/ofbvar.h>
     66   1.1    tsubai 
     67  1.52  macallan struct ofb_softc {
     68  1.52  macallan 	struct	device sc_dev;
     69  1.52  macallan 
     70  1.52  macallan 	pci_chipset_tag_t sc_pc;
     71  1.52  macallan 	pcitag_t sc_pcitag;
     72  1.52  macallan 	bus_space_tag_t sc_memt;
     73  1.52  macallan 	bus_space_tag_t sc_iot;
     74  1.52  macallan 
     75  1.52  macallan 	u_int32_t sc_addrs[30];		/* "assigned-addresses" storage */
     76  1.52  macallan 	u_char sc_cmap_red[256];
     77  1.52  macallan 	u_char sc_cmap_green[256];
     78  1.52  macallan 	u_char sc_cmap_blue[256];
     79  1.52  macallan 
     80  1.52  macallan 	int sc_node, sc_ih, sc_mode;
     81  1.52  macallan 	paddr_t sc_fbaddr;
     82  1.61  macallan 	uint32_t sc_fbsize;
     83  1.61  macallan 
     84  1.52  macallan 	struct vcons_data vd;
     85  1.52  macallan };
     86  1.18    tsubai 
     87  1.44  macallan static int	ofbmatch(struct device *, struct cfdata *, void *);
     88  1.44  macallan static void	ofbattach(struct device *, struct device *, void *);
     89   1.1    tsubai 
     90  1.32   thorpej CFATTACH_DECL(ofb, sizeof(struct ofb_softc),
     91  1.32   thorpej     ofbmatch, ofbattach, NULL, NULL);
     92   1.1    tsubai 
     93   1.1    tsubai const struct wsscreen_descr *_ofb_scrlist[] = {
     94   1.1    tsubai 	&ofb_stdscreen,
     95   1.1    tsubai 	/* XXX other formats, graphics screen? */
     96   1.1    tsubai };
     97   1.1    tsubai 
     98   1.1    tsubai struct wsscreen_list ofb_screenlist = {
     99   1.1    tsubai 	sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
    100   1.1    tsubai };
    101   1.1    tsubai 
    102  1.56  christos static int	ofb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    103  1.48      jmmv static paddr_t	ofb_mmap(void *, void *, off_t, int);
    104  1.45  macallan 
    105  1.45  macallan static void	ofb_init_screen(void *, struct vcons_screen *, int, long *);
    106   1.1    tsubai 
    107   1.1    tsubai struct wsdisplay_accessops ofb_accessops = {
    108   1.1    tsubai 	ofb_ioctl,
    109   1.1    tsubai 	ofb_mmap,
    110  1.45  macallan 	NULL,		/* vcons_alloc_screen */
    111  1.45  macallan 	NULL,		/* vcons_free_screen */
    112  1.45  macallan 	NULL,		/* vcons_show_screen */
    113  1.45  macallan 	NULL		/* load_font */
    114   1.1    tsubai };
    115   1.1    tsubai 
    116  1.45  macallan static void	ofb_putpalreg(struct ofb_softc *, int, uint8_t, uint8_t,
    117  1.45  macallan     uint8_t);
    118  1.45  macallan 
    119  1.45  macallan static int	ofb_getcmap(struct ofb_softc *, struct wsdisplay_cmap *);
    120  1.45  macallan static int	ofb_putcmap(struct ofb_softc *, struct wsdisplay_cmap *);
    121  1.45  macallan static void	ofb_init_cmap(struct ofb_softc *);
    122  1.57  macallan static int	ofb_drm_print(void *, const char *);
    123  1.11    tsubai 
    124  1.45  macallan extern const u_char rasops_cmap[768];
    125   1.1    tsubai 
    126  1.52  macallan extern int console_node;
    127  1.52  macallan extern int console_instance;
    128  1.52  macallan 
    129  1.45  macallan static int
    130  1.44  macallan ofbmatch(struct device *parent, struct cfdata *match, void *aux)
    131   1.1    tsubai {
    132   1.1    tsubai 	struct pci_attach_args *pa = aux;
    133   1.1    tsubai 
    134   1.3    tsubai 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
    135  1.11    tsubai 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
    136   1.3    tsubai 		return 1;
    137   1.1    tsubai 
    138   1.3    tsubai 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
    139   1.3    tsubai 		return 1;
    140   1.3    tsubai 
    141   1.3    tsubai 	return 0;
    142   1.1    tsubai }
    143   1.1    tsubai 
    144  1.44  macallan static void
    145  1.44  macallan ofbattach(struct device *parent, struct device *self, void *aux)
    146   1.1    tsubai {
    147  1.22    tsubai 	struct ofb_softc *sc = (struct ofb_softc *)self;
    148   1.1    tsubai 	struct pci_attach_args *pa = aux;
    149   1.1    tsubai 	struct wsemuldisplaydev_attach_args a;
    150  1.45  macallan 	struct rasops_info *ri = &ofb_console_screen.scr_ri;
    151  1.45  macallan 	long defattr;
    152  1.52  macallan 	int console, node, sub;
    153   1.5    tsubai 	char devinfo[256];
    154   1.1    tsubai 
    155  1.53  macallan 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    156  1.53  macallan 	printf(": %s\n", devinfo);
    157  1.53  macallan 
    158  1.53  macallan 	if (console_node == 0)
    159  1.53  macallan 		return;
    160  1.53  macallan 
    161  1.46  macallan 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    162  1.46  macallan 	console = (node == console_node);
    163  1.47  macallan 	if (!console) {
    164  1.47  macallan 		/* check if any of the childs matches */
    165  1.47  macallan 		sub = OF_child(node);
    166  1.47  macallan 		while ((sub != 0) && (sub != console_node)) {
    167  1.47  macallan 			sub = OF_peer(sub);
    168  1.47  macallan 		}
    169  1.47  macallan 		if (sub == console_node) {
    170  1.55   thorpej 			console = true;
    171  1.47  macallan 		}
    172  1.47  macallan 	}
    173  1.46  macallan 
    174  1.42  macallan 	sc->sc_memt = pa->pa_memt;
    175  1.42  macallan 	sc->sc_iot = pa->pa_iot;
    176  1.45  macallan 	sc->sc_pc = pa->pa_pc;
    177  1.45  macallan 	sc->sc_pcitag = pa->pa_tag;
    178  1.45  macallan 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    179  1.46  macallan 
    180  1.46  macallan 	if (!console)
    181  1.46  macallan 		return;
    182  1.45  macallan 
    183  1.45  macallan 	vcons_init(&sc->vd, sc, &ofb_stdscreen, &ofb_accessops);
    184  1.45  macallan 	sc->vd.init_screen = ofb_init_screen;
    185  1.45  macallan 
    186  1.52  macallan 	sc->sc_node = console_node;
    187   1.1    tsubai 
    188  1.52  macallan 	sc->sc_ih = console_instance;
    189  1.52  macallan 	vcons_init_screen(&sc->vd, &ofb_console_screen, 1, &defattr);
    190  1.52  macallan 	ofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    191  1.52  macallan 
    192  1.52  macallan 	printf("%s: %d x %d, %dbpp\n", self->dv_xname,
    193  1.52  macallan 	       ri->ri_width, ri->ri_height, ri->ri_depth);
    194  1.45  macallan 
    195  1.45  macallan 	sc->sc_fbaddr = 0;
    196  1.45  macallan 	if (OF_getprop(sc->sc_node, "address", &sc->sc_fbaddr, 4) != 4)
    197  1.58  macallan 		OF_interpret("frame-buffer-adr", 0, 1, &sc->sc_fbaddr);
    198  1.45  macallan 	if (sc->sc_fbaddr == 0) {
    199  1.45  macallan 		printf("%s: Unable to find the framebuffer address.\n",
    200  1.45  macallan 		    sc->sc_dev.dv_xname);
    201  1.45  macallan 		return;
    202   1.1    tsubai 	}
    203  1.61  macallan 	sc->sc_fbsize = round_page(ri->ri_stride * ri->ri_height);
    204  1.61  macallan 
    205  1.16    tsubai 	/* XXX */
    206  1.45  macallan 	if (OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
    207  1.16    tsubai 	    sizeof(sc->sc_addrs)) == -1) {
    208  1.45  macallan 		sc->sc_node = OF_parent(sc->sc_node);
    209  1.45  macallan 		OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
    210  1.16    tsubai 		    sizeof(sc->sc_addrs));
    211  1.16    tsubai 	}
    212  1.13    tsubai 
    213  1.52  macallan 	ofb_init_cmap(sc);
    214   1.1    tsubai 
    215   1.1    tsubai 	a.console = console;
    216   1.1    tsubai 	a.scrdata = &ofb_screenlist;
    217   1.1    tsubai 	a.accessops = &ofb_accessops;
    218  1.45  macallan 	a.accesscookie = &sc->vd;
    219   1.1    tsubai 
    220   1.1    tsubai 	config_found(self, &a, wsemuldisplaydevprint);
    221  1.57  macallan 
    222  1.57  macallan 	config_found_ia(self, "drm", aux, ofb_drm_print);
    223  1.57  macallan }
    224  1.57  macallan 
    225  1.57  macallan static int
    226  1.57  macallan ofb_drm_print(void *aux, const char *pnp)
    227  1.57  macallan {
    228  1.57  macallan 	if (pnp)
    229  1.57  macallan 		aprint_normal("direct rendering for %s", pnp);
    230  1.57  macallan 	return (UNSUPP);
    231   1.1    tsubai }
    232   1.1    tsubai 
    233  1.44  macallan static void
    234  1.45  macallan ofb_init_screen(void *cookie, struct vcons_screen *scr,
    235  1.45  macallan     int existing, long *defattr)
    236   1.1    tsubai {
    237  1.45  macallan 	struct ofb_softc *sc = cookie;
    238  1.45  macallan 	struct rasops_info *ri = &scr->scr_ri;
    239  1.45  macallan 
    240  1.45  macallan 	if (scr != &ofb_console_screen) {
    241  1.45  macallan 		ofb_init_rasops(sc->sc_node, ri);
    242  1.45  macallan 	}
    243  1.45  macallan }
    244   1.1    tsubai 
    245  1.45  macallan static int
    246  1.56  christos ofb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    247   1.1    tsubai {
    248  1.45  macallan 	struct vcons_data *vd = v;
    249  1.45  macallan 	struct ofb_softc *sc = vd->cookie;
    250   1.1    tsubai 	struct wsdisplay_fbinfo *wdf;
    251  1.45  macallan 	struct vcons_screen *ms = vd->active;
    252   1.1    tsubai 	struct grfinfo *gm;
    253   1.1    tsubai 
    254   1.1    tsubai 	switch (cmd) {
    255   1.1    tsubai 	case WSDISPLAYIO_GTYPE:
    256   1.1    tsubai 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;	/* XXX ? */
    257   1.1    tsubai 		return 0;
    258   1.1    tsubai 
    259   1.1    tsubai 	case WSDISPLAYIO_GINFO:
    260  1.45  macallan 		/* we won't get here without any screen anyway */
    261  1.45  macallan 		if (ms != NULL) {
    262  1.45  macallan 			wdf = (void *)data;
    263  1.61  macallan 			wdf->height = ms->scr_ri.ri_height;
    264  1.61  macallan 			wdf->width = ms->scr_ri.ri_width;
    265  1.45  macallan 			wdf->depth = ms->scr_ri.ri_depth;
    266  1.45  macallan 			wdf->cmsize = 256;
    267  1.45  macallan 			return 0;
    268  1.45  macallan 		} else
    269  1.45  macallan 			return ENODEV;
    270   1.1    tsubai 
    271  1.11    tsubai 	case WSDISPLAYIO_GETCMAP:
    272  1.11    tsubai 		return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
    273  1.11    tsubai 
    274   1.1    tsubai 	case WSDISPLAYIO_PUTCMAP:
    275  1.11    tsubai 		return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
    276   1.1    tsubai 
    277   1.1    tsubai 	/* XXX There are no way to know framebuffer pa from a user program. */
    278   1.1    tsubai 	case GRFIOCGINFO:
    279  1.45  macallan 		if (ms != NULL) {
    280  1.45  macallan 			gm = (void *)data;
    281  1.45  macallan 			memset(gm, 0, sizeof(struct grfinfo));
    282  1.56  christos 			gm->gd_fbaddr = (void *)sc->sc_fbaddr;
    283  1.45  macallan 			gm->gd_fbrowbytes = ms->scr_ri.ri_stride;
    284  1.45  macallan 			return 0;
    285  1.45  macallan 		} else
    286  1.45  macallan 			return ENODEV;
    287  1.60  macallan 	case WSDISPLAYIO_LINEBYTES:
    288  1.60  macallan 		{
    289  1.60  macallan 			*(int *)data = ms->scr_ri.ri_stride;
    290  1.60  macallan 			return 0;
    291  1.60  macallan 		}
    292  1.45  macallan 	case WSDISPLAYIO_SMODE:
    293  1.45  macallan 		{
    294  1.45  macallan 			int new_mode = *(int*)data;
    295  1.45  macallan 			if (new_mode != sc->sc_mode)
    296  1.45  macallan 			{
    297  1.45  macallan 				sc->sc_mode = new_mode;
    298  1.45  macallan 				if (new_mode == WSDISPLAYIO_MODE_EMUL)
    299  1.45  macallan 				{
    300  1.45  macallan 					ofb_init_cmap(sc);
    301  1.45  macallan 					vcons_redraw_screen(ms);
    302  1.45  macallan 				}
    303  1.45  macallan 			}
    304  1.45  macallan 		}
    305   1.1    tsubai 		return 0;
    306  1.27   nathanw 	/* PCI config read/write passthrough. */
    307  1.27   nathanw 	case PCI_IOC_CFGREAD:
    308  1.27   nathanw 	case PCI_IOC_CFGWRITE:
    309  1.27   nathanw 		return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    310  1.43  christos 		    cmd, data, flag, l));
    311   1.1    tsubai 	}
    312  1.26    atatat 	return EPASSTHROUGH;
    313   1.1    tsubai }
    314   1.1    tsubai 
    315  1.44  macallan static paddr_t
    316  1.48      jmmv ofb_mmap(void *v, void *vs, off_t offset, int prot)
    317   1.1    tsubai {
    318  1.45  macallan 	struct vcons_data *vd = v;
    319  1.45  macallan 	struct ofb_softc *sc = vd->cookie;
    320  1.45  macallan 	struct rasops_info *ri;
    321  1.13    tsubai 	u_int32_t *ap = sc->sc_addrs;
    322  1.50        ad 	struct lwp *me;
    323  1.13    tsubai 	int i;
    324  1.13    tsubai 
    325  1.45  macallan 	if (vd->active == NULL) {
    326  1.45  macallan 		printf("%s: no active screen.\n", sc->sc_dev.dv_xname);
    327  1.45  macallan 		return -1;
    328  1.45  macallan 	}
    329  1.45  macallan 
    330  1.45  macallan 	ri = &vd->active->scr_ri;
    331  1.45  macallan 
    332  1.42  macallan 	/* framebuffer at offset 0 */
    333  1.61  macallan 	if ((offset >= 0) && (offset < sc->sc_fbsize))
    334  1.61  macallan 		return bus_space_mmap(sc->sc_memt, sc->sc_fbaddr, offset, prot,
    335  1.61  macallan 		    BUS_SPACE_MAP_LINEAR);
    336  1.13    tsubai 
    337  1.42  macallan 	/*
    338  1.42  macallan 	 * restrict all other mappings to processes with superuser privileges
    339  1.42  macallan 	 * or the kernel itself
    340  1.42  macallan 	 */
    341  1.50        ad 	me = curlwp;
    342  1.42  macallan 	if (me != NULL) {
    343  1.50        ad 		if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER,
    344  1.50        ad 		    NULL) != 0) {
    345  1.45  macallan 			printf("%s: mmap() rejected.\n", sc->sc_dev.dv_xname);
    346  1.42  macallan 			return -1;
    347  1.42  macallan 		}
    348  1.42  macallan 	}
    349  1.42  macallan 
    350  1.42  macallan 	/* let them mmap() 0xa0000 - 0xbffff if it's not covered above */
    351  1.42  macallan #ifdef OFB_FAKE_VGA_FB
    352  1.42  macallan 	if (offset >=0xa0000 && offset < 0xbffff)
    353  1.45  macallan 		return sc->sc_fbaddr + offset - 0xa0000;
    354  1.42  macallan #endif
    355  1.42  macallan 
    356  1.42  macallan 	/* allow to map our IO space */
    357  1.42  macallan 	if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
    358  1.42  macallan 		return bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
    359  1.42  macallan 		    BUS_SPACE_MAP_LINEAR);
    360  1.42  macallan 	}
    361  1.42  macallan 
    362  1.13    tsubai 	for (i = 0; i < 6; i++) {
    363  1.13    tsubai 		switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
    364  1.13    tsubai 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
    365  1.42  macallan 			if (offset >= ap[2] && offset < ap[2] + ap[4])
    366  1.42  macallan 				return bus_space_mmap(sc->sc_memt, offset, 0,
    367  1.42  macallan 				    prot, BUS_SPACE_MAP_LINEAR);
    368  1.13    tsubai 		}
    369  1.13    tsubai 		ap += 5;
    370  1.13    tsubai 	}
    371  1.13    tsubai 	return -1;
    372   1.1    tsubai }
    373   1.1    tsubai 
    374  1.44  macallan static int
    375  1.44  macallan ofb_getcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
    376  1.11    tsubai {
    377  1.11    tsubai 	u_int index = cm->index;
    378  1.11    tsubai 	u_int count = cm->count;
    379  1.11    tsubai 	int error;
    380  1.11    tsubai 
    381  1.11    tsubai 	if (index >= 256 || count > 256 || index + count > 256)
    382  1.11    tsubai 		return EINVAL;
    383  1.11    tsubai 
    384  1.11    tsubai 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    385  1.11    tsubai 	if (error)
    386  1.11    tsubai 		return error;
    387  1.11    tsubai 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    388  1.11    tsubai 	if (error)
    389  1.11    tsubai 		return error;
    390  1.11    tsubai 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    391  1.11    tsubai 	if (error)
    392  1.11    tsubai 		return error;
    393   1.8    tsubai 
    394   1.1    tsubai 	return 0;
    395   1.1    tsubai }
    396   1.1    tsubai 
    397  1.45  macallan int
    398  1.44  macallan ofb_putcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
    399   1.1    tsubai {
    400  1.25  jdolecek 	u_int index = cm->index;
    401  1.25  jdolecek 	u_int count = cm->count;
    402  1.38       chs 	int i, error;
    403  1.38       chs 	u_char rbuf[256], gbuf[256], bbuf[256];
    404   1.1    tsubai 	u_char *r, *g, *b;
    405   1.1    tsubai 
    406   1.1    tsubai 	if (cm->index >= 256 || cm->count > 256 ||
    407   1.1    tsubai 	    (cm->index + cm->count) > 256)
    408   1.1    tsubai 		return EINVAL;
    409  1.38       chs 	error = copyin(cm->red, &rbuf[index], count);
    410  1.38       chs 	if (error)
    411  1.38       chs 		return error;
    412  1.38       chs 	error = copyin(cm->green, &gbuf[index], count);
    413  1.38       chs 	if (error)
    414  1.38       chs 		return error;
    415  1.38       chs 	error = copyin(cm->blue, &bbuf[index], count);
    416  1.38       chs 	if (error)
    417  1.38       chs 		return error;
    418  1.38       chs 
    419  1.38       chs 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    420  1.38       chs 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    421  1.38       chs 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    422   1.1    tsubai 
    423   1.1    tsubai 	r = &sc->sc_cmap_red[index];
    424   1.1    tsubai 	g = &sc->sc_cmap_green[index];
    425   1.1    tsubai 	b = &sc->sc_cmap_blue[index];
    426   1.1    tsubai 	for (i = 0; i < count; i++) {
    427  1.45  macallan 		OF_call_method_1("color!", sc->sc_ih, 4, *r, *g, *b, index);
    428   1.1    tsubai 		r++, g++, b++, index++;
    429   1.1    tsubai 	}
    430   1.1    tsubai 	return 0;
    431   1.1    tsubai }
    432  1.45  macallan 
    433  1.45  macallan static void
    434  1.45  macallan ofb_putpalreg(struct ofb_softc *sc, int idx, uint8_t r, uint8_t g, uint8_t b)
    435  1.45  macallan {
    436  1.45  macallan 	if ((idx<0) || (idx > 255))
    437  1.45  macallan 		return;
    438  1.52  macallan 	if (sc != NULL) {
    439  1.52  macallan 		OF_call_method_1("color!", sc->sc_ih, 4, r, g, b, idx);
    440  1.52  macallan 		sc->sc_cmap_red[idx] = r;
    441  1.52  macallan 		sc->sc_cmap_green[idx] = g;
    442  1.52  macallan 		sc->sc_cmap_blue[idx] = b;
    443  1.52  macallan 	} else {
    444  1.52  macallan 		OF_call_method_1("color!", console_instance, 4, r, g, b, idx);
    445  1.52  macallan 	}
    446  1.45  macallan }
    447  1.45  macallan 
    448  1.45  macallan static void
    449  1.45  macallan ofb_init_cmap(struct ofb_softc *sc)
    450  1.45  macallan {
    451  1.45  macallan 	int idx, i;
    452  1.45  macallan 	/* mess with the palette only when we're running in 8 bit */
    453  1.45  macallan 	if (ofb_console_screen.scr_ri.ri_depth == 8) {
    454  1.45  macallan 		idx = 0;
    455  1.45  macallan 		for (i = 0; i < 256; i++) {
    456  1.52  macallan 			ofb_putpalreg(sc, i, rasops_cmap[(i * 3) + 0],
    457  1.52  macallan 			     rasops_cmap[(i * 3) + 1],
    458  1.52  macallan 			     rasops_cmap[(i * 3) + 2]);
    459  1.45  macallan 		}
    460  1.45  macallan 	}
    461  1.45  macallan }
    462