wsfontdev.c revision 1.19
11.19Smacallan/* $NetBSD: wsfontdev.c,v 1.19 2021/04/24 00:15:37 macallan Exp $ */
21.1Sdrochner
31.1Sdrochner/*
41.1Sdrochner * Copyright (c) 2001
51.1Sdrochner * 	Matthias Drochner.  All rights reserved.
61.1Sdrochner *
71.1Sdrochner * Redistribution and use in source and binary forms, with or without
81.1Sdrochner * modification, are permitted provided that the following conditions
91.1Sdrochner * are met:
101.1Sdrochner * 1. Redistributions of source code must retain the above copyright
111.1Sdrochner *    notice, this list of conditions, and the following disclaimer.
121.1Sdrochner * 2. Redistributions in binary form must reproduce the above copyright
131.1Sdrochner *    notice, this list of conditions and the following disclaimer in the
141.1Sdrochner *    documentation and/or other materials provided with the distribution.
151.1Sdrochner *
161.1Sdrochner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
171.1Sdrochner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181.1Sdrochner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191.1Sdrochner * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
201.1Sdrochner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211.1Sdrochner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221.1Sdrochner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231.1Sdrochner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241.1Sdrochner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sdrochner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sdrochner * SUCH DAMAGE.
271.1Sdrochner */
281.3Slukem
291.3Slukem#include <sys/cdefs.h>
301.19Smacallan__KERNEL_RCSID(0, "$NetBSD: wsfontdev.c,v 1.19 2021/04/24 00:15:37 macallan Exp $");
311.1Sdrochner
321.1Sdrochner#include <sys/param.h>
331.1Sdrochner#include <sys/systm.h>
341.1Sdrochner#include <sys/conf.h>
351.1Sdrochner#include <sys/ioctl.h>
361.1Sdrochner#include <sys/malloc.h>
371.6Sjdolecek#include <sys/event.h>
381.1Sdrochner
391.1Sdrochner#include <dev/wsfont/wsfont.h>
401.1Sdrochner#include <dev/wscons/wsconsio.h> /* XXX */
411.1Sdrochner
421.17Schristos#include "ioconf.h"
431.4Sgehenna
441.19Smacallan#ifdef WSFONT_DEBUG
451.19Smacallan#define DPRINTF printf
461.19Smacallan#else
471.19Smacallan#define DPRINTF while (0) printf
481.19Smacallan#endif
491.1Sdrochnerstatic int wsfont_isopen;
501.1Sdrochner
511.19Smacallan
521.1Sdrochnervoid
531.13Schristoswsfontattach(int n)
541.1Sdrochner{
551.1Sdrochner
561.1Sdrochner	wsfont_init();
571.1Sdrochner}
581.1Sdrochner
591.9Sthorpejstatic int
601.13Schristoswsfontopen(dev_t dev, int flag, int mode,
611.13Schristos    struct lwp *l)
621.1Sdrochner{
631.1Sdrochner
641.1Sdrochner	if (wsfont_isopen)
651.1Sdrochner		return (EBUSY);
661.1Sdrochner	wsfont_isopen = 1;
671.1Sdrochner	return (0);
681.1Sdrochner}
691.1Sdrochner
701.9Sthorpejstatic int
711.13Schristoswsfontclose(dev_t dev, int flag, int mode,
721.13Schristos    struct lwp *l)
731.1Sdrochner{
741.1Sdrochner
751.1Sdrochner	wsfont_isopen = 0;
761.1Sdrochner	return (0);
771.1Sdrochner}
781.1Sdrochner
791.19Smacallanstatic void
801.19Smacallanfontmatchfunc(struct wsdisplay_font *f, void *cookie, int fontcookie)
811.19Smacallan{
821.19Smacallan	struct wsdisplayio_fontinfo *fi = cookie;
831.19Smacallan	struct wsdisplayio_fontdesc fd;
841.19Smacallan	int offset;
851.19Smacallan
861.19Smacallan	DPRINTF("%s %dx%d\n", f->name, f->fontwidth, f->fontheight);
871.19Smacallan	if (fi->fi_fonts != NULL && fi->fi_buffersize > 0) {
881.19Smacallan		memset(&fd, 0, sizeof(fd));
891.19Smacallan		strncpy(fd.fd_name, f->name, sizeof(fd.fd_name) - 1);
901.19Smacallan		fd.fd_width = f->fontwidth;
911.19Smacallan		fd.fd_height = f->fontheight;
921.19Smacallan		offset = sizeof(struct wsdisplayio_fontdesc) * (fi->fi_numentries + 1);
931.19Smacallan		if (offset > fi->fi_buffersize) {
941.19Smacallan			fi->fi_fonts = NULL;
951.19Smacallan		} else
961.19Smacallan			copyout(&fd, &fi->fi_fonts[fi->fi_numentries],
971.19Smacallan			    sizeof(struct wsdisplayio_fontdesc));
981.19Smacallan	}
991.19Smacallan	fi->fi_numentries++;
1001.19Smacallan}
1011.19Smacallan
1021.19Smacallanstatic int
1031.19Smacallanwsdisplayio_listfonts(struct wsdisplayio_fontinfo *f)
1041.19Smacallan{
1051.19Smacallan	void *addr = f->fi_fonts;
1061.19Smacallan	DPRINTF("%s: %d %d\n", __func__, f->fi_buffersize, f->fi_numentries);
1071.19Smacallan	f->fi_numentries = 0;
1081.19Smacallan	wsfont_walk(fontmatchfunc, f);
1091.19Smacallan	/* check if we ran out of buffer space */
1101.19Smacallan	if (f->fi_fonts == NULL && addr != NULL) return ENOMEM;
1111.19Smacallan	return 0;
1121.19Smacallan}
1131.19Smacallan
1141.9Sthorpejstatic int
1151.14Schristoswsfontioctl(dev_t dev, u_long cmd, void *data, int flag,
1161.13Schristos    struct lwp *l)
1171.1Sdrochner{
1181.18Smacallan	char nbuf[64];
1191.1Sdrochner	void *buf;
1201.1Sdrochner	int res;
1211.1Sdrochner
1221.1Sdrochner	switch (cmd) {
1231.1Sdrochner	case WSDISPLAYIO_LDFONT:
1241.1Sdrochner#define d ((struct wsdisplay_font *)data)
1251.1Sdrochner		if (d->name) {
1261.1Sdrochner			res = copyinstr(d->name, nbuf, sizeof(nbuf), 0);
1271.1Sdrochner			if (res)
1281.1Sdrochner				return (res);
1291.1Sdrochner			d->name = nbuf;
1301.1Sdrochner		} else
1311.1Sdrochner			d->name = "loaded"; /* ??? */
1321.1Sdrochner		buf = malloc(d->fontheight * d->stride * d->numchars,
1331.1Sdrochner			     M_DEVBUF, M_WAITOK);
1341.1Sdrochner		res = copyin(d->data, buf,
1351.1Sdrochner			     d->fontheight * d->stride * d->numchars);
1361.1Sdrochner		if (res) {
1371.1Sdrochner			free(buf, M_DEVBUF);
1381.1Sdrochner			return (res);
1391.1Sdrochner		}
1401.1Sdrochner		d->data = buf;
1411.1Sdrochner		res = wsfont_add(d, 1);
1421.1Sdrochner		free(buf, M_DEVBUF);
1431.1Sdrochner#undef d
1441.1Sdrochner		return (res);
1451.19Smacallan	case WSDISPLAYIO_LISTFONTS:
1461.19Smacallan		return wsdisplayio_listfonts(data);
1471.1Sdrochner	default:
1481.1Sdrochner		return (EINVAL);
1491.1Sdrochner	}
1501.1Sdrochner}
1511.9Sthorpej
1521.9Sthorpejconst struct cdevsw wsfont_cdevsw = {
1531.15Sdholland	.d_open = wsfontopen,
1541.15Sdholland	.d_close = wsfontclose,
1551.15Sdholland	.d_read = noread,
1561.15Sdholland	.d_write = nowrite,
1571.15Sdholland	.d_ioctl = wsfontioctl,
1581.15Sdholland	.d_stop = nostop,
1591.15Sdholland	.d_tty = notty,
1601.15Sdholland	.d_poll = nopoll,
1611.15Sdholland	.d_mmap = nommap,
1621.15Sdholland	.d_kqfilter = nokqfilter,
1631.16Sdholland	.d_discard = nodiscard,
1641.15Sdholland	.d_flag = D_OTHER
1651.9Sthorpej};
166