1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1998 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The DragonFly Project
8 * by Sascha Wildner <saw@online.de>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer as
15 *    the first lines of this file unmodified.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD$");
35
36#include "opt_vga.h"
37
38#ifndef VGA_NO_MODE_CHANGE
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/conf.h>
43#include <sys/tty.h>
44#include <sys/kernel.h>
45#include <sys/fbio.h>
46#include <sys/consio.h>
47
48#include <dev/fb/vesa.h>
49
50#include <dev/fb/fbreg.h>
51#include <dev/syscons/syscons.h>
52
53static tsw_ioctl_t *prev_user_ioctl;
54
55static int
56vesa_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
57{
58	scr_stat *scp;
59	int mode;
60
61	scp = SC_STAT(tp);
62
63	switch (cmd) {
64
65	/* generic text modes */
66	case SW_TEXT_132x25: case SW_TEXT_132x30:
67	case SW_TEXT_132x43: case SW_TEXT_132x50:
68	case SW_TEXT_132x60:
69		if (!(scp->sc->adp->va_flags & V_ADP_MODECHANGE))
70			return ENODEV;
71		return sc_set_text_mode(scp, tp, cmd & 0xff, 0, 0, 0, 0);
72
73	/* text modes */
74	case SW_VESA_C80x60:
75	case SW_VESA_C132x25:
76	case SW_VESA_C132x43:
77	case SW_VESA_C132x50:
78	case SW_VESA_C132x60:
79		if (!(scp->sc->adp->va_flags & V_ADP_MODECHANGE))
80			return ENODEV;
81		mode = (cmd & 0xff) + M_VESA_BASE;
82		return sc_set_text_mode(scp, tp, mode, 0, 0, 0, 0);
83
84	/* graphics modes */
85	case SW_VESA_32K_320: 	case SW_VESA_64K_320:
86	case SW_VESA_FULL_320:
87
88	case SW_VESA_CG640x400:
89
90	case SW_VESA_CG640x480:
91	case SW_VESA_32K_640:	case SW_VESA_64K_640:
92	case SW_VESA_FULL_640:
93
94	case SW_VESA_800x600:	case SW_VESA_CG800x600:
95	case SW_VESA_32K_800:	case SW_VESA_64K_800:
96	case SW_VESA_FULL_800:
97
98	case SW_VESA_1024x768:	case SW_VESA_CG1024x768:
99	case SW_VESA_32K_1024:	case SW_VESA_64K_1024:
100	case SW_VESA_FULL_1024:
101
102	case SW_VESA_1280x1024:	case SW_VESA_CG1280x1024:
103	case SW_VESA_32K_1280:	case SW_VESA_64K_1280:
104	case SW_VESA_FULL_1280:
105		if (!(scp->sc->adp->va_flags & V_ADP_MODECHANGE))
106			return ENODEV;
107		mode = (cmd & 0xff) + M_VESA_BASE;
108		return sc_set_graphics_mode(scp, tp, mode);
109	default:
110		if (IOCGROUP(cmd) == 'V') {
111			if (!(scp->sc->adp->va_flags & V_ADP_MODECHANGE))
112				return ENODEV;
113
114			mode = (cmd & 0xff) + M_VESA_BASE;
115
116			if (((cmd & IOC_DIRMASK) == IOC_VOID) &&
117			    (mode > M_VESA_FULL_1280) &&
118			    (mode < M_VESA_MODE_MAX))
119				return sc_set_graphics_mode(scp, tp, mode);
120		}
121	}
122
123	if (prev_user_ioctl)
124		return (*prev_user_ioctl)(tp, cmd, data, td);
125	else
126		return ENOIOCTL;
127}
128
129int
130vesa_load_ioctl(void)
131{
132	if (prev_user_ioctl)
133		return EBUSY;
134	prev_user_ioctl = sc_user_ioctl;
135	sc_user_ioctl = vesa_ioctl;
136	return 0;
137}
138
139int
140vesa_unload_ioctl(void)
141{
142	if (sc_user_ioctl != vesa_ioctl)
143		return EBUSY;
144	sc_user_ioctl = prev_user_ioctl;
145	prev_user_ioctl = NULL;
146	return 0;
147}
148
149#endif /* VGA_NO_MODE_CHANGE */
150