fb.c revision 1.2 1 /*
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Sony Corp. and Kazumasa Utashiro of Software Research Associates, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: $Hdr: fb.c,v 4.300 91/06/27 20:43:06 root Rel41 $ SONY
37 *
38 * @(#)fb.c 8.1 (Berkeley) 6/11/93
39 */
40
41 #include "fb.h"
42 #if NFB > 0
43 /*
44 * Frame buffer driver
45 */
46
47 #include <sys/param.h>
48 #include <sys/types.h>
49 #include <sys/device.h>
50 #include <sys/systm.h>
51 #include <sys/proc.h>
52
53 #include <machine/cpu.h>
54 #include <machine/autoconf.h>
55 #include <machine/framebuf.h>
56
57 #include <newsmips/dev/fbreg.h>
58
59 #define FB_USED 1
60 #define VIDEO_USED 2
61
62 struct fb_softc {
63 struct device sc_dev;
64 int fbs_state;
65 int fbs_device;
66 lScrType fbs_type;
67 int fbs_flag;
68 struct fbreg fbreg;
69 };
70
71 static int fbmatch __P((struct device *, struct cfdata *, void *));
72 static void fbattach __P((struct device *, struct device *, void *));
73 static void fblock __P((void));
74 static void fbunlock __P((int));
75 static int fbinit __P((struct fbreg *));
76 static void fbreset __P((struct fbreg *));
77
78 struct cfattach fb_ca = {
79 sizeof(struct fb_softc), fbmatch, fbattach
80 };
81
82 extern struct cfdriver fb_cd;
83
84 static char *devname[] = {
85 /* 0*/ "",
86 /* 1*/ "NWB-512",
87 /* 2*/ "NWB-225",
88 /* 3*/ "POP-MONO",
89 /* 4*/ "POP-COLOR",
90 /* 5*/ "NWB-514",
91 /* 6*/ "NWB-251",
92 /* 7*/ "LCD-MONO",
93 /* 8*/ "LDC-COLOR",
94 /* 9*/ "NWB-518",
95 /*10*/ "NWB-252",
96 /*11*/ "NWB-253",
97 /*12*/ "NWB-254",
98 /*13*/ "NWB-255",
99 /*14*/ "SLB-101",
100 /*15*/ "NWB-256",
101 /*16*/ "NWB-257",
102 };
103
104 static int fbstate = 0;
105
106 extern int fb253_probe();
107 extern void bmcnattach();
108 extern int fbstart __P((struct fbreg *, int));
109 extern int search_fbdev __P((int, int));
110 extern int fbgetscrtype();
111 extern int fbsetpalette();
112 extern int fbgetpalette();
113 extern int fbsetcursor();
114 extern int fbnsetcursor();
115 extern int fbsetxy();
116
117 static int
118 fbmatch(parent, cf, aux)
119 struct device *parent;
120 struct cfdata *cf;
121 void *aux;
122 {
123 struct confargs *ca = aux;
124 struct fbreg fbreg;
125 struct fbreg *fbp = &fbreg;
126 int unit = cf->cf_unit;
127
128 if (strcmp(ca->ca_name, "fb"))
129 return 0;
130
131 fbp->fb_command = FB_CPROBE;
132 fbp->fb_device = 0;
133 fbp->fb_unit = unit;
134 fbp->fb_data = -1;
135 fbstart(fbp, 1);
136
137 if (fbp->fb_result != FB_ROK)
138 return 0;
139
140 return 1;
141 }
142
143 static void
144 fbattach(parent, self, aux)
145 struct device *parent;
146 struct device *self;
147 void *aux;
148 {
149 struct fb_softc *fb = (struct fb_softc *)self;
150 struct fbreg *fbp = &fb->fbreg;
151 int unit = fb->sc_dev.dv_cfdata->cf_unit;
152 extern int tty00_is_console;
153
154 fb->fbs_device = unit;
155
156 fbp->fb_command = FB_CATTACH;
157 fbp->fb_device = fb->fbs_device;
158 fbstart(fbp, 1);
159 fb->fbs_type = fbp->fb_scrtype;
160
161 if (fb->fbs_type.type) {
162 fb->fbs_state = 0;
163 fb->fbs_flag = 0;
164 printf(": %s",
165 (fb->fbs_type.type < sizeof(devname)/sizeof(*devname))
166 ? devname[(int)fb->fbs_type.type] : "UNKNOWN");
167 printf(" (%d x %d %d plane)",
168 fb->fbs_type.visiblerect.extent.x,
169 fb->fbs_type.visiblerect.extent.y,
170 fb->fbs_type.plane);
171 }
172
173 if (! tty00_is_console) { /* XXX */
174 printf(" (console)");
175 bmcnattach();
176 }
177 printf("\n");
178 }
179
180 int
181 fbopen(dev, flags, mode, p)
182 dev_t dev;
183 int flags, mode;
184 struct proc *p;
185 {
186 register int unit = FBUNIT(dev);
187 register struct fb_softc *fb = fb_cd.cd_devs[unit];
188 register struct fbreg *fbp = &fb->fbreg;
189
190 if (unit >= NFB)
191 return ENXIO;
192 if (fb->fbs_flag && !FBVIDEO(dev))
193 return EBUSY;
194
195 if (fb->fbs_state == 0) {
196 fbp->fb_device = fb->fbs_device;
197 if (fbinit(fbp))
198 return EBUSY;
199 }
200
201 fb->fbs_state |= FBVIDEO(dev) ? VIDEO_USED : FB_USED;
202
203 return 0;
204 }
205
206 int
207 fbclose(dev, flags, mode, p)
208 dev_t dev;
209 int flags, mode;
210 struct proc *p;
211 {
212 register int unit = FBUNIT(dev);
213 register struct fb_softc *fb = fb_cd.cd_devs[unit];
214 register struct fbreg *fbp = &fb->fbreg;
215
216 if (unit >= NFB)
217 return ENXIO;
218
219 if (fb->fbs_state == 0)
220 return 0;
221
222 if (!FBVIDEO(dev))
223 fb->fbs_flag = 0;
224
225 fb->fbs_state &= ~(FBVIDEO(dev) ? VIDEO_USED : FB_USED);
226
227 if (fb->fbs_state == 0) {
228 fbp->fb_device = fb->fbs_device;
229 fbreset(fbp);
230 }
231
232 return 0;
233 }
234
235 int
236 fbioctl(dev, cmd, data, flags, p)
237 dev_t dev;
238 u_long cmd;
239 caddr_t data;
240 int flags;
241 struct proc *p;
242 {
243 register int unit = FBUNIT(dev);
244 register struct fb_softc *fb = fb_cd.cd_devs[unit];
245 register struct fbreg *fbp = &fb->fbreg;
246 register int error = 0;
247
248 if (unit >= NFB)
249 return ENXIO;
250
251 fblock();
252
253 fbp->fb_device = fb->fbs_device;
254
255 switch (cmd) {
256 case FBIOCENABLE:
257 fb->fbs_flag = 0;
258 break;
259 case FBIOCDISABLE:
260 fb->fbs_flag = 1;
261 break;
262 case FBIOCAUTODIM:
263 fbp->fb_command = FB_CAUTODIM;
264 fbp->fb_data = *((int *)data);
265 fbstart(fbp, 0);
266 break;
267 case FBIOCSETDIM:
268 fbp->fb_command = FB_CSETDIM;
269 fbp->fb_data = *((int*)data);
270 fbstart(fbp, 0);
271 break;
272 case FBIOCGETDIM:
273 fbp->fb_command = FB_CGETDIM;
274 fbstart(fbp, 1);
275 *((int*)data) = fbp->fb_data;
276 break;
277 #ifdef notyet
278 case FBIOCBITBLT:
279 error = fbbitblt(fbp, (sBitblt *)data);
280 break;
281 case FBIOCNBITBLT:
282 error = fbnbitblt(fbp, (lBitblt *)data, UIO_USERSPACE);
283 break;
284 case FBIOCBATCHBITBLT:
285 error = fbbatchbitblt(fbp, (sBatchBitblt*)data, UIO_USERSPACE);
286 break;
287 case FBIOCNBATCHBITBLT:
288 error = fbnbatchbitblt(fbp, (lBatchBitblt*)data, UIO_USERSPACE);
289 break;
290 case FBIOCTILEBITBLT:
291 error = fbtilebitblt(fbp, (sTileBitblt *)data);
292 break;
293 case FBIOCNTILEBITBLT:
294 error = fbntilebitblt(fbp, (lTileBitblt *)data);
295 break;
296 case FBIOCBITBLT3:
297 error = fbbitblt3(fbp, (sBitblt3 *)data);
298 break;
299 case FBIOCNBITBLT3:
300 error = fbnbitblt3(fbp, (lBitblt3 *)data);
301 break;
302 case FBIOCPOLYLINE:
303 error = fbpolyline(fbp, (sPrimLine *)data, 0);
304 break;
305 case FBIOCNPOLYLINE:
306 error = fbnpolyline(fbp, (lPrimLine *)data, 0, UIO_USERSPACE);
307 break;
308 case FBIOCDJPOLYLINE:
309 error = fbpolyline(fbp, (sPrimLine *)data, 1);
310 break;
311 case FBIOCNDJPOLYLINE:
312 error = fbnpolyline(fbp, (lPrimLine *)data, 1, UIO_USERSPACE);
313 break;
314 case FBIOCPOLYMARKER:
315 error = fbpolymarker(fbp, (sPrimMarker *)data);
316 break;
317 case FBIOCNPOLYMARKER:
318 error = fbnpolymarker(fbp, (lPrimMarker *)data, UIO_USERSPACE);
319 break;
320 case FBIOCRECTANGLE:
321 error = fbrectangle(fbp, (sPrimRect *)data);
322 break;
323 case FBIOCNRECTANGLE:
324 error = fbnrectangle(fbp, (lPrimRect *)data);
325 break;
326 case FBIOCFILLSCAN:
327 error = fbfillscan(fbp, (sPrimFill *)data);
328 break;
329 case FBIOCNFILLSCAN:
330 error = fbnfillscan(fbp, (lPrimFill *)data, UIO_USERSPACE);
331 break;
332 case FBIOCTEXT:
333 error = fbtext(fbp, (sPrimText *)data);
334 break;
335 case FBIOCNTEXT:
336 error = fbntext(fbp, (lPrimText *)data);
337 break;
338 case FBIOCPOLYDOT:
339 error = fbpolydot(fbp, (sPrimDot *)data);
340 break;
341 case FBIOCNPOLYDOT:
342 error = fbnpolydot(fbp, (lPrimDot *)data, UIO_USERSPACE);
343 break;
344 #endif
345
346 case FBIOCGETSCRTYPE:
347 fbgetscrtype(fbp, (sScrType *)data);
348 break;
349 case FBIOCNGETSCRTYPE:
350 fbp->fb_command = FB_CGETSCRTYPE;
351 fbstart(fbp, 1);
352 *((lScrType*)data) = fbp->fb_scrtype;
353 break;
354 case FBIOCSETPALETTE:
355 fbsetpalette(fbp, (sPalette *)data);
356 break;
357 #ifdef notyet
358 case FBIOCNSETPALETTE:
359 error = fbnsetpalette(fbp, (lPalette *)data);
360 break;
361 #endif
362 case FBIOCGETPALETTE:
363 fbgetpalette(fbp, (sPalette *)data);
364 break;
365 #ifdef notyet
366 case FBIOCNGETPALETTE:
367 error = fbngetpalette(fbp, (lPalette *)data);
368 break;
369 #endif
370 case FBIOCSETCURSOR:
371 fbsetcursor(fbp, (sCursor *)data);
372 break;
373 case FBIOCNSETCURSOR:
374 fbnsetcursor(fbp, (lCursor *)data);
375 break;
376 case FBIOCNSETCURSOR2:
377 fbp->fb_command = FB_CSETCURSOR;
378 fbp->fb_cursor = *((lCursor2 *)data);
379 fbstart(fbp, 0);
380 break;
381 case FBIOCUNSETCURSOR:
382 fbp->fb_command = FB_CUNSETCURSOR;
383 fbstart(fbp, 0);
384 break;
385 case FBIOCNUNSETCURSOR:
386 fbp->fb_command = FB_CUNSETCURSOR;
387 fbstart(fbp, 0);
388 break;
389 case FBIOCSHOWCURSOR:
390 fbp->fb_command = FB_CSHOWCURSOR;
391 fbstart(fbp, 0);
392 break;
393 case FBIOCNSHOWCURSOR:
394 fbp->fb_command = FB_CSHOWCURSOR;
395 fbstart(fbp, 0);
396 break;
397 case FBIOCHIDECURSOR:
398 fbp->fb_command = FB_CHIDECURSOR;
399 fbstart(fbp, 0);
400 break;
401 case FBIOCNHIDECURSOR:
402 fbp->fb_command = FB_CHIDECURSOR;
403 fbstart(fbp, 0);
404 break;
405 case FBIOCSETXY:
406 fbsetxy(fbp, (sPoint *)data);
407 break;
408 case FBIOCNSETXY:
409 fbp->fb_command = FB_CSETXY;
410 fbp->fb_point = *((lPoint *)data);
411 fbstart(fbp, 0);
412 break;
413 case FBIOCNSETPALETTEMODE:
414 fbp->fb_command = FB_CSETPMODE;
415 fbp->fb_data = *((int*)data);
416 fbstart(fbp, 0);
417 break;
418 case FBIOCNGETPALETTEMODE:
419 fbp->fb_command = FB_CGETPMODE;
420 fbstart(fbp, 1);
421 *((int*)data) = fbp->fb_data;
422 break;
423 case FBIOCNSETVIDEO:
424 fbp->fb_command = FB_CSETVIDEO;
425 fbp->fb_videoctl = *((lVideoCtl*)data);
426 fbstart(fbp, 0);
427 break;
428 case FBIOCNGETVIDEO:
429 fbp->fb_command = FB_CGETVIDEO;
430 fbp->fb_videostatus.request = VIDEO_STATUS;
431 fbstart(fbp, 1);
432 *((lVideoStatus*)data) = fbp->fb_videostatus;
433 error = fbp->fb_result;
434 break;
435 case FBIOCNIOCTL:
436 fbp->fb_command = FB_CIOCTL;
437 fbp->fb_fbioctl = *((lFbIoctl*)data);
438 fbstart(fbp, 1);
439 *((lFbIoctl*)data) = fbp->fb_fbioctl;
440 if (fbp->fb_result == FB_RERROR)
441 error = EINVAL;
442 break;
443
444 default:
445 error = ENXIO;
446 break;
447 }
448
449 fbunlock(error);
450
451 return error;
452 }
453
454 int
455 fbmmap(dev, off, prot)
456 dev_t dev;
457 int off, prot;
458 {
459 register int unit = FBUNIT(dev);
460 register struct fb_softc *fb = fb_cd.cd_devs[unit];
461 register struct fbreg *fbp = &fb->fbreg;
462 register int page;
463
464 if (unit >= NFB)
465 return -1;
466
467 if (off & PGOFSET)
468 return -1;
469
470 if (off < 0)
471 return -1;
472
473 fblock();
474 fbp->fb_device = fb->fbs_device;
475 fbp->fb_command = FB_CGETPAGE;
476 fbp->fb_data = off;
477 fbstart(fbp, 1);
478 page = fbp->fb_data;
479 if (fbp->fb_result == FB_RERROR)
480 page = -1;
481 else
482 fb->fbs_flag = 1;
483 fbunlock(fbp->fb_result);
484
485 return page;
486 }
487
488 #if 0 /* not needed? */
489 int
490 fbpoll(dev, events, p)
491 dev_t dev;
492 int events;
493 struct proc *p;
494 {
495 return -1;
496 }
497 #endif
498
499 static void
500 fblock()
501 {
502 int s;
503
504 #ifdef USE_RAW_INTR
505 fbwait();
506 #endif
507 s = splfb();
508 while (fbstate & FB_BUSY) {
509 fbstate |= FB_WANTED;
510 tsleep(&fbstate, FBPRI | PCATCH, "fblock", 0);
511 }
512 fbstate |= FB_BUSY;
513 splx(s);
514 }
515
516 static void
517 fbunlock(error)
518 int error;
519 {
520 int s = splfb();
521
522 #ifdef CPU_SINGLE
523 fbstate &= ~FB_BUSY;
524 if (fbstate & FB_WANTED) {
525 fbstate &= ~FB_WANTED;
526 wakeup(&fbstate);
527 }
528 #else
529 #ifdef USE_RAW_INTR
530 fbstate &= ~FB_BUSY;
531 if (fbstate & FB_WANTED) {
532 fbstate &= ~FB_WANTED;
533 wakeup(&fbstate);
534 }
535 #else
536 if (error || (fbstate & FB_DELAY) == 0) {
537 fbstate &= ~(FB_BUSY | FB_WAIT | FB_DELAY);
538 if (fbstate & FB_WANTED) {
539 fbstate &= ~FB_WANTED;
540 wakeup(&fbstate);
541 }
542 }
543 if (fbstate & FB_DELAY) {
544 fbstate &= ~FB_DELAY;
545 fbstate |= FB_DELAY2;
546 }
547 #endif
548 #endif /* CPU_SINGLE */
549 splx(s);
550 }
551
552 static int
553 fbinit(fbp)
554 struct fbreg *fbp;
555 {
556 fblock();
557
558 fbp->fb_command = FB_COPEN;
559 fbstart(fbp, 1);
560 if (fbp->fb_result != FB_ROK) {
561 fbunlock(0);
562 return FB_RERROR;
563 }
564
565 fbp->fb_command = FB_CUNSETCURSOR;
566 fbstart(fbp, 0);
567
568 fbunlock(0);
569
570 return FB_ROK;
571 }
572
573 static void
574 fbreset(fbp)
575 struct fbreg *fbp;
576 {
577 fblock();
578
579 fbp->fb_command = FB_CUNSETCURSOR;
580 fbstart(fbp, 1);
581
582 fbp->fb_command = FB_CCLOSE;
583 fbstart(fbp, 0);
584
585 fbunlock(0);
586 }
587 #endif /* NFB > 0 */
588