]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/fb/fbd.c
zfs: merge openzfs/zfs@043c6ee3b
[FreeBSD/FreeBSD.git] / sys / dev / fb / fbd.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013 The FreeBSD Foundation
5  *
6  * This software was developed by Aleksandr Rybalko under sponsorship from the
7  * FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30
31 /* Generic framebuffer */
32 /* TODO unlink from VT(9) */
33 /* TODO done normal /dev/fb methods */
34
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 #include <sys/eventhandler.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/queue.h>
45 #include <sys/fbio.h>
46
47 #include <machine/bus.h>
48
49 #include <dev/vt/vt.h>
50 #include <dev/vt/hw/fb/vt_fb.h>
51
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54
55 #include "fb_if.h"
56
57 LIST_HEAD(fb_list_head_t, fb_list_entry) fb_list_head =
58     LIST_HEAD_INITIALIZER(fb_list_head);
59 struct fb_list_entry {
60         struct fb_info  *fb_info;
61         struct cdev     *fb_si;
62         LIST_ENTRY(fb_list_entry) fb_list;
63 };
64
65 struct fbd_softc {
66         device_t        sc_dev;
67         struct fb_info  *sc_info;
68 };
69
70 static void fbd_evh_init(void *);
71 /* SI_ORDER_SECOND, just after EVENTHANDLERs initialized. */
72 SYSINIT(fbd_evh_init, SI_SUB_CONFIGURE, SI_ORDER_SECOND, fbd_evh_init, NULL);
73
74 static d_open_t         fb_open;
75 static d_close_t        fb_close;
76 static d_read_t         fb_read;
77 static d_write_t        fb_write;
78 static d_ioctl_t        fb_ioctl;
79 static d_mmap_t         fb_mmap;
80
81 static struct cdevsw fb_cdevsw = {
82         .d_version =    D_VERSION,
83         .d_flags =      D_NEEDGIANT,
84         .d_open =       fb_open,
85         .d_close =      fb_close,
86         .d_read =       fb_read,
87         .d_write =      fb_write,
88         .d_ioctl =      fb_ioctl,
89         .d_mmap =       fb_mmap,
90         .d_name =       "fb",
91 };
92
93 static int framebuffer_dev_unit = 0;
94
95 static int
96 fb_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
97 {
98
99         return (0);
100 }
101
102 static int
103 fb_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
104 {
105
106         return (0);
107 }
108
109 static int
110 fb_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
111     struct thread *td)
112 {
113         struct fb_info *info;
114         int error;
115
116         error = 0;
117         info = dev->si_drv1;
118
119         switch (cmd) {
120         case FBIOGTYPE:
121                 bcopy(info, (struct fbtype *)data, sizeof(struct fbtype));
122                 break;
123
124         case FBIO_GETWINORG:    /* get frame buffer window origin */
125                 *(u_int *)data = 0;
126                 break;
127
128         case FBIO_GETDISPSTART: /* get display start address */
129                 ((video_display_start_t *)data)->x = 0;
130                 ((video_display_start_t *)data)->y = 0;
131                 break;
132
133         case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
134                 *(u_int *)data = info->fb_stride;
135                 break;
136
137         case FBIO_BLANK:        /* blank display */
138                 if (info->setblankmode != NULL)
139                         error = info->setblankmode(info->fb_priv, *(int *)data);
140                 break;
141
142         default:
143                 error = ENOIOCTL;
144                 break;
145         }
146         return (error);
147 }
148
149 static int
150 fb_read(struct cdev *dev, struct uio *uio, int ioflag)
151 {
152
153         return (0); /* XXX nothing to read, yet */
154 }
155
156 static int
157 fb_write(struct cdev *dev, struct uio *uio, int ioflag)
158 {
159
160         return (0); /* XXX nothing written */
161 }
162
163 static int
164 fb_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
165     vm_memattr_t *memattr)
166 {
167         struct fb_info *info;
168
169         info = dev->si_drv1;
170
171         if (info->fb_flags & FB_FLAG_NOMMAP)
172                 return (ENODEV);
173
174         if (offset < info->fb_size) {
175                 if (info->fb_pbase == 0)
176                         *paddr = vtophys((uint8_t *)info->fb_vbase + offset);
177                 else
178                         *paddr = info->fb_pbase + offset;
179                 if (info->fb_flags & FB_FLAG_MEMATTR)
180                         *memattr = info->fb_memattr;
181                 return (0);
182         }
183         return (EINVAL);
184 }
185
186 static int
187 fb_init(struct fb_list_entry *entry, int unit)
188 {
189         struct fb_info *info;
190
191         info = entry->fb_info;
192         entry->fb_si = make_dev(&fb_cdevsw, unit, UID_ROOT, GID_WHEEL,
193             0600, "fb%d", unit);
194         entry->fb_si->si_drv1 = info;
195         info->fb_cdev = entry->fb_si;
196
197         return (0);
198 }
199
200 int
201 fbd_list(void)
202 {
203         struct fb_list_entry *entry;
204
205         if (LIST_EMPTY(&fb_list_head))
206                 return (ENOENT);
207
208         LIST_FOREACH(entry, &fb_list_head, fb_list) {
209                 printf("FB %s @%#jx\n", entry->fb_info->fb_name,
210                     (uintmax_t)entry->fb_info->fb_pbase);
211         }
212
213         return (0);
214 }
215
216 static struct fb_list_entry *
217 fbd_find(struct fb_info* info)
218 {
219         struct fb_list_entry *entry, *tmp;
220
221         LIST_FOREACH_SAFE(entry, &fb_list_head, fb_list, tmp) {
222                 if (entry->fb_info == info) {
223                         return (entry);
224                 }
225         }
226
227         return (NULL);
228 }
229
230 int
231 fbd_register(struct fb_info* info)
232 {
233         struct fb_list_entry *entry;
234         int err, first;
235
236         first = 0;
237         if (LIST_EMPTY(&fb_list_head))
238                 first++;
239
240         entry = fbd_find(info);
241         if (entry != NULL) {
242                 /* XXX Update framebuffer params */
243                 return (0);
244         }
245
246         entry = malloc(sizeof(struct fb_list_entry), M_DEVBUF, M_WAITOK|M_ZERO);
247         entry->fb_info = info;
248
249         LIST_INSERT_HEAD(&fb_list_head, entry, fb_list);
250
251         err = fb_init(entry, framebuffer_dev_unit++);
252         if (err)
253                 return (err);
254
255         if (first) {
256                 err = vt_fb_attach(info);
257                 if (err)
258                         return (err);
259         }
260
261         return (0);
262 }
263
264 int
265 fbd_unregister(struct fb_info* info)
266 {
267         struct fb_list_entry *entry, *tmp;
268
269         LIST_FOREACH_SAFE(entry, &fb_list_head, fb_list, tmp) {
270                 if (entry->fb_info == info) {
271                         LIST_REMOVE(entry, fb_list);
272                         if (LIST_EMPTY(&fb_list_head))
273                                 vt_fb_detach(info);
274                         free(entry, M_DEVBUF);
275                         return (0);
276                 }
277         }
278
279         return (ENOENT);
280 }
281
282 static void
283 register_fb_wrap(void *arg, void *ptr)
284 {
285
286         fbd_register((struct fb_info *)ptr);
287 }
288
289 static void
290 unregister_fb_wrap(void *arg, void *ptr)
291 {
292
293         fbd_unregister((struct fb_info *)ptr);
294 }
295
296 static void
297 fbd_evh_init(void *ctx)
298 {
299
300         EVENTHANDLER_REGISTER(register_framebuffer, register_fb_wrap, NULL,
301             EVENTHANDLER_PRI_ANY);
302         EVENTHANDLER_REGISTER(unregister_framebuffer, unregister_fb_wrap, NULL,
303             EVENTHANDLER_PRI_ANY);
304 }
305
306 /* Newbus methods. */
307 static int
308 fbd_probe(device_t dev)
309 {
310
311         return (BUS_PROBE_NOWILDCARD);
312 }
313
314 static int
315 fbd_attach(device_t dev)
316 {
317         struct fbd_softc *sc;
318         int err;
319
320         sc = device_get_softc(dev);
321
322         sc->sc_dev = dev;
323         sc->sc_info = FB_GETINFO(device_get_parent(dev));
324         if (sc->sc_info == NULL)
325                 return (ENXIO);
326         err = fbd_register(sc->sc_info);
327
328         return (err);
329 }
330
331 static int
332 fbd_detach(device_t dev)
333 {
334         struct fbd_softc *sc;
335         int err;
336
337         sc = device_get_softc(dev);
338
339         err = fbd_unregister(sc->sc_info);
340
341         return (err);
342 }
343
344 static device_method_t fbd_methods[] = {
345         /* Device interface */
346         DEVMETHOD(device_probe,         fbd_probe),
347         DEVMETHOD(device_attach,        fbd_attach),
348         DEVMETHOD(device_detach,        fbd_detach),
349
350         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
351
352         { 0, 0 }
353 };
354
355 driver_t fbd_driver = {
356         "fbd",
357         fbd_methods,
358         sizeof(struct fbd_softc)
359 };
360
361 DRIVER_MODULE(fbd, fb, fbd_driver, 0, 0);
362 DRIVER_MODULE(fbd, drmn, fbd_driver, 0, 0);
363 DRIVER_MODULE(fbd, udl, fbd_driver, 0, 0);
364 MODULE_VERSION(fbd, 1);
365