1/* $NetBSD: wsfontdev.c,v 1.20 2022/05/12 23:17:42 uwe Exp $ */ 2 3/* 4 * Copyright (c) 2001 5 * Matthias Drochner. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions, and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__KERNEL_RCSID(0, "$NetBSD: wsfontdev.c,v 1.20 2022/05/12 23:17:42 uwe Exp $"); 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/conf.h> 35#include <sys/fcntl.h> 36#include <sys/ioctl.h> 37#include <sys/malloc.h> 38#include <sys/event.h> 39 40#include <dev/wsfont/wsfont.h> 41#include <dev/wscons/wsconsio.h> /* XXX */ 42 43#include "ioconf.h" 44 45#ifdef WSFONT_DEBUG 46#define DPRINTF printf 47#else 48#define DPRINTF while (0) printf 49#endif 50static int wsfont_isopen; 51 52 53void 54wsfontattach(int n) 55{ 56 57 wsfont_init(); 58} 59 60static int 61wsfontopen(dev_t dev, int flag, int mode, 62 struct lwp *l) 63{ 64 65 if (wsfont_isopen) 66 return (EBUSY); 67 wsfont_isopen = 1; 68 return (0); 69} 70 71static int 72wsfontclose(dev_t dev, int flag, int mode, 73 struct lwp *l) 74{ 75 76 wsfont_isopen = 0; 77 return (0); 78} 79 80static void 81fontmatchfunc(struct wsdisplay_font *f, void *cookie, int fontcookie) 82{ 83 struct wsdisplayio_fontinfo *fi = cookie; 84 struct wsdisplayio_fontdesc fd; 85 int offset; 86 87 DPRINTF("%s %dx%d\n", f->name, f->fontwidth, f->fontheight); 88 if (fi->fi_fonts != NULL && fi->fi_buffersize > 0) { 89 memset(&fd, 0, sizeof(fd)); 90 strncpy(fd.fd_name, f->name, sizeof(fd.fd_name) - 1); 91 fd.fd_width = f->fontwidth; 92 fd.fd_height = f->fontheight; 93 offset = sizeof(struct wsdisplayio_fontdesc) * (fi->fi_numentries + 1); 94 if (offset > fi->fi_buffersize) { 95 fi->fi_fonts = NULL; 96 } else 97 copyout(&fd, &fi->fi_fonts[fi->fi_numentries], 98 sizeof(struct wsdisplayio_fontdesc)); 99 } 100 fi->fi_numentries++; 101} 102 103static int 104wsdisplayio_listfonts(struct wsdisplayio_fontinfo *f) 105{ 106 void *addr = f->fi_fonts; 107 DPRINTF("%s: %d %d\n", __func__, f->fi_buffersize, f->fi_numentries); 108 f->fi_numentries = 0; 109 wsfont_walk(fontmatchfunc, f); 110 /* check if we ran out of buffer space */ 111 if (f->fi_fonts == NULL && addr != NULL) return ENOMEM; 112 return 0; 113} 114 115static int 116wsfontioctl(dev_t dev, u_long cmd, void *data, int flag, 117 struct lwp *l) 118{ 119 char nbuf[64]; 120 void *buf; 121 int res; 122 123 switch (cmd) { 124 case WSDISPLAYIO_LDFONT: 125#define d ((struct wsdisplay_font *)data) 126 if ((flag & FWRITE) == 0) 127 return EPERM; 128 129 if (d->name) { 130 res = copyinstr(d->name, nbuf, sizeof(nbuf), 0); 131 if (res) 132 return (res); 133 d->name = nbuf; 134 } else 135 d->name = "loaded"; /* ??? */ 136 buf = malloc(d->fontheight * d->stride * d->numchars, 137 M_DEVBUF, M_WAITOK); 138 res = copyin(d->data, buf, 139 d->fontheight * d->stride * d->numchars); 140 if (res) { 141 free(buf, M_DEVBUF); 142 return (res); 143 } 144 d->data = buf; 145 res = wsfont_add(d, 1); 146 free(buf, M_DEVBUF); 147#undef d 148 return (res); 149 case WSDISPLAYIO_LISTFONTS: 150 return wsdisplayio_listfonts(data); 151 default: 152 return (EINVAL); 153 } 154} 155 156const struct cdevsw wsfont_cdevsw = { 157 .d_open = wsfontopen, 158 .d_close = wsfontclose, 159 .d_read = noread, 160 .d_write = nowrite, 161 .d_ioctl = wsfontioctl, 162 .d_stop = nostop, 163 .d_tty = notty, 164 .d_poll = nopoll, 165 .d_mmap = nommap, 166 .d_kqfilter = nokqfilter, 167 .d_discard = nodiscard, 168 .d_flag = D_OTHER 169}; 170