xref: /netbsd-src/sys/arch/x68k/dev/grf.c (revision eb7c1594f145c931049e1fd9eb056a5987e87e59)
1 /*	$NetBSD: grf.c,v 1.28 2003/08/07 16:30:23 agc Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * the Systems Programming Group of the University of Utah Computer
9  * Science Department.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
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  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * from: Utah $Hdr: grf.c 1.36 93/08/13$
36  *
37  *	@(#)grf.c	8.4 (Berkeley) 1/12/94
38  */
39 /*
40  * Copyright (c) 1988 University of Utah.
41  *
42  * This code is derived from software contributed to Berkeley by
43  * the Systems Programming Group of the University of Utah Computer
44  * Science Department.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  *	This product includes software developed by the University of
57  *	California, Berkeley and its contributors.
58  * 4. Neither the name of the University nor the names of its contributors
59  *    may be used to endorse or promote products derived from this software
60  *    without specific prior written permission.
61  *
62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72  * SUCH DAMAGE.
73  *
74  * from: Utah $Hdr: grf.c 1.36 93/08/13$
75  *
76  *	@(#)grf.c	8.4 (Berkeley) 1/12/94
77  */
78 
79 /*
80  * Graphics display driver for the X68K machines.
81  * This is the hardware-independent portion of the driver.
82  * Hardware access is through the machine dependent grf switch routines.
83  */
84 
85 #include <sys/cdefs.h>
86 __KERNEL_RCSID(0, "$NetBSD: grf.c,v 1.28 2003/08/07 16:30:23 agc Exp $");
87 
88 #include "opt_compat_hpux.h"
89 
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/device.h>
93 #include <sys/proc.h>
94 #include <sys/resourcevar.h>
95 #include <sys/ioctl.h>
96 #include <sys/file.h>
97 #include <sys/malloc.h>
98 #include <sys/vnode.h>
99 #include <sys/mman.h>
100 #include <sys/conf.h>
101 
102 #include <machine/grfioctl.h>
103 
104 #include <x68k/dev/grfvar.h>
105 #include <x68k/dev/itevar.h>
106 
107 #include <machine/cpu.h>
108 
109 #ifdef COMPAT_HPUX
110 #include <compat/hpux/hpux.h>
111 extern struct emul emul_hpux;
112 #endif
113 
114 #include <uvm/uvm_extern.h>
115 #include <uvm/uvm_map.h>
116 
117 #include <miscfs/specfs/specdev.h>
118 
119 #include "ite.h"
120 #if NITE == 0
121 #define	iteon(u,f)
122 #define	iteoff(u,f)
123 #define	ite_reinit(u)
124 #endif
125 
126 #ifdef DEBUG
127 int grfdebug = 0;
128 #define GDB_DEVNO	0x01
129 #define GDB_MMAP	0x02
130 #define GDB_IOMAP	0x04
131 #define GDB_LOCK	0x08
132 #endif
133 
134 int grfon __P((dev_t));
135 int grfoff __P((dev_t));
136 off_t grfaddr __P((struct grf_softc *, off_t));
137 int grfmap __P((dev_t, caddr_t *, struct proc *));
138 int grfunmap __P((dev_t, caddr_t, struct proc *));
139 
140 extern struct cfdriver grf_cd;
141 
142 dev_type_open(grfopen);
143 dev_type_close(grfclose);
144 dev_type_ioctl(grfioctl);
145 dev_type_mmap(grfmmap);
146 
147 const struct cdevsw grf_cdevsw = {
148 	grfopen, grfclose, nullread, nullwrite, grfioctl,
149 	nostop, notty, nopoll, grfmmap, nokqfilter,
150 };
151 
152 /*ARGSUSED*/
153 int
154 grfopen(dev, flags, mode, p)
155 	dev_t dev;
156 	int flags, mode;
157 	struct proc *p;
158 {
159 	int unit = GRFUNIT(dev);
160 	register struct grf_softc *gp;
161 	int error = 0;
162 
163 	if (unit >= grf_cd.cd_ndevs ||
164 	    (gp = grf_cd.cd_devs[unit]) == NULL ||
165 	    (gp->g_flags & GF_ALIVE) == 0)
166 		return (ENXIO);
167 
168 	if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE))
169 		return(EBUSY);
170 #ifdef COMPAT_HPUX
171 	/*
172 	 * XXX: cannot handle both HPUX and BSD processes at the same time
173 	 */
174 	if (p->p_emul == &emul_hpux)
175 		if (gp->g_flags & GF_BSDOPEN)
176 			return(EBUSY);
177 		else
178 			gp->g_flags |= GF_HPUXOPEN;
179 	else
180 		if (gp->g_flags & GF_HPUXOPEN)
181 			return(EBUSY);
182 		else
183 			gp->g_flags |= GF_BSDOPEN;
184 #endif
185 	/*
186 	 * First open.
187 	 * XXX: always put in graphics mode.
188 	 */
189 	error = 0;
190 	if ((gp->g_flags & GF_OPEN) == 0) {
191 		gp->g_flags |= GF_OPEN;
192 		error = grfon(dev);
193 	}
194 	return(error);
195 }
196 
197 /*ARGSUSED*/
198 int
199 grfclose(dev, flags, mode, p)
200 	dev_t dev;
201 	int flags, mode;
202 	struct proc *p;
203 {
204 	register struct grf_softc *gp = grf_cd.cd_devs[GRFUNIT(dev)];
205 
206 	if ((gp->g_flags & GF_ALIVE) == 0)
207 		return (ENXIO);
208 
209 	(void) grfoff(dev);
210 #ifdef COMPAT_HPUX
211 	(void) grfunlock(gp);
212 #endif
213 	gp->g_flags &= GF_ALIVE;
214 	return(0);
215 }
216 
217 /*ARGSUSED*/
218 int
219 grfioctl(dev, cmd, data, flag, p)
220 	dev_t dev;
221 	u_long cmd;
222 	caddr_t data;
223 	int flag;
224 	struct proc *p;
225 {
226 	int unit = GRFUNIT(dev);
227 	register struct grf_softc *gp = grf_cd.cd_devs[unit];
228 	int error;
229 
230 	if ((gp->g_flags & GF_ALIVE) == 0)
231 		return (ENXIO);
232 
233 #ifdef COMPAT_HPUX
234 	if (p->p_emul == &emul_hpux)
235 		return(hpuxgrfioctl(dev, cmd, data, flag, p));
236 #endif
237 	error = 0;
238 	switch (cmd) {
239 
240 	case GRFIOCGINFO:
241 		memcpy(data, (caddr_t)&gp->g_display, sizeof(struct grfinfo));
242 		break;
243 
244 	case GRFIOCON:
245 		error = grfon(dev);
246 		break;
247 
248 	case GRFIOCOFF:
249 		error = grfoff(dev);
250 		break;
251 
252 	case GRFIOCMAP:
253 		error = grfmap(dev, (caddr_t *)data, p);
254 		break;
255 
256 	case GRFIOCUNMAP:
257 		error = grfunmap(dev, *(caddr_t *)data, p);
258 		break;
259 
260 	case GRFSETVMODE:
261 		error = (*gp->g_sw->gd_mode)(gp, GM_GRFSETVMODE, data);
262 		if (error == 0)
263 			ite_reinit(unit);
264 		break;
265 
266 	default:
267 		error = EINVAL;
268 		break;
269 
270 	}
271 	return(error);
272 }
273 
274 /*ARGSUSED*/
275 paddr_t
276 grfmmap(dev, off, prot)
277 	dev_t dev;
278 	off_t off;
279 	int prot;
280 {
281 
282 	return (grfaddr(grf_cd.cd_devs[GRFUNIT(dev)], off));
283 }
284 
285 int
286 grfon(dev)
287 	dev_t dev;
288 {
289 	int unit = GRFUNIT(dev);
290 	struct grf_softc *gp = grf_cd.cd_devs[unit];
291 
292 	/*
293 	 * XXX: iteoff call relies on devices being in same order
294 	 * as ITEs and the fact that iteoff only uses the minor part
295 	 * of the dev arg.
296 	 */
297 	iteoff(unit, 2);
298 	return((*gp->g_sw->gd_mode)(gp,
299 				    (dev&GRFOVDEV) ? GM_GRFOVON : GM_GRFON,
300 				    (caddr_t)0));
301 }
302 
303 int
304 grfoff(dev)
305 	dev_t dev;
306 {
307 	int unit = GRFUNIT(dev);
308 	struct grf_softc *gp = grf_cd.cd_devs[unit];
309 	int error;
310 
311 	(void) grfunmap(dev, (caddr_t)0, curproc);
312 	error = (*gp->g_sw->gd_mode)(gp,
313 				     (dev&GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF,
314 				     (caddr_t)0);
315 	/* XXX: see comment for iteoff above */
316 	iteon(unit, 2);
317 	return(error);
318 }
319 
320 off_t
321 grfaddr(gp, off)
322 	struct grf_softc *gp;
323 	off_t off;
324 {
325 	register struct grfinfo *gi = &gp->g_display;
326 
327 	/* control registers */
328 	if (off >= 0 && off < gi->gd_regsize)
329 		return(((u_int)gi->gd_regaddr + off) >> PGSHIFT);
330 
331 	/* frame buffer */
332 	if (off >= gi->gd_regsize && off < gi->gd_regsize+gi->gd_fbsize) {
333 		off -= gi->gd_regsize;
334 		return(((u_int)gi->gd_fbaddr + off) >> PGSHIFT);
335 	}
336 	/* bogus */
337 	return(-1);
338 }
339 
340 /*
341  * HP-UX compatibility routines
342  */
343 #ifdef COMPAT_HPUX
344 
345 /*ARGSUSED*/
346 int
347 hpuxgrfioctl(dev, cmd, data, flag, p)
348 	dev_t dev;
349 	int cmd;
350 	caddr_t data;
351 	int flag;
352 	struct proc *p;
353 {
354 	register struct grf_softc *gp = grf_cd.cd_devs[GRFUNIT(dev)];
355 	int error;
356 
357 	error = 0;
358 	switch (cmd) {
359 
360 	case GCID:
361 		*(int *)data = gp->g_display.gd_id;
362 		break;
363 
364 	case GCON:
365 		error = grfon(dev);
366 		break;
367 
368 	case GCOFF:
369 		error = grfoff(dev);
370 		break;
371 
372 	case GCLOCK:
373 		error = grflock(gp, 1);
374 		break;
375 
376 	case GCUNLOCK:
377 		error = grfunlock(gp);
378 		break;
379 
380 	case GCAON:
381 	case GCAOFF:
382 		break;
383 
384 	/* GCSTATIC is implied by our implementation */
385 	case GCSTATIC_CMAP:
386 	case GCVARIABLE_CMAP:
387 		break;
388 
389 	/* map in control regs and frame buffer */
390 	case GCMAP:
391 		error = grfmap(dev, (caddr_t *)data, p);
392 		break;
393 
394 	case GCUNMAP:
395 		error = grfunmap(dev, *(caddr_t *)data, p);
396 		/* XXX: HP-UX uses GCUNMAP to get rid of GCSLOT memory */
397 		if (error)
398 			error = grflckunmmap(dev, *(caddr_t *)data);
399 		break;
400 
401 	case GCSLOT:
402 	{
403 		struct grf_slot *sp = (struct grf_slot *)data;
404 
405 		sp->slot = grffindpid(gp);
406 		if (sp->slot) {
407 			error = grflckmmap(dev, (caddr_t *)&sp->addr);
408 			if (error && gp->g_pid) {
409 				free((caddr_t)gp->g_pid, M_DEVBUF);
410 				gp->g_pid = NULL;
411 			}
412 		} else
413 			error = EINVAL;		/* XXX */
414 		break;
415 	}
416 
417 	case GCDESCRIBE:
418 		error = (*gp->g_sw->gd_mode)(gp, GM_DESCRIBE, data);
419 		break;
420 
421 	/*
422 	 * XXX: only used right now to map in rbox control registers
423 	 * Will be replaced in the future with a real IOMAP interface.
424 	 */
425 	case IOMAPMAP:
426 		error = iommap(dev, (caddr_t *)data);
427 #if 0
428 		/*
429 		 * It may not be worth kludging this (using p_devtmp) to
430 		 * make this work.  It was an undocumented side-effect
431 		 * in HP-UX that the mapped address was the return value
432 		 * of the ioctl.  The only thing I remember that counted
433 		 * on this behavior was the rbox X10 server.
434 		 */
435 		if (!error)
436 			u.u_r.r_val1 = *(int *)data;	/* XXX: this sux */
437 #endif
438 		break;
439 
440 	case IOMAPUNMAP:
441 		error = iounmmap(dev, *(caddr_t *)data);
442 		break;
443 
444 	default:
445 		error = EINVAL;
446 		break;
447 	}
448 	return(error);
449 }
450 
451 int
452 grflock(gp, block)
453 	register struct grf_softc *gp;
454 	int block;
455 {
456 	struct proc *p = curproc;		/* XXX */
457 	int error;
458 	extern char devioc[];
459 
460 #ifdef DEBUG
461 	if (grfdebug & GDB_LOCK)
462 		printf("grflock(%d): flags %x lockpid %x\n",
463 		       p->p_pid, gp->g_flags,
464 		       gp->g_lockp ? gp->g_lockp->p_pid : -1);
465 #endif
466 	if (gp->g_pid) {
467 #ifdef DEBUG
468 		if (grfdebug & GDB_LOCK)
469 			printf(" lockpslot %d lockslot %d lock[lockslot] %d\n",
470 			       gp->g_lock->gl_lockslot, gp->g_lockpslot,
471 			       gp->g_lock->gl_locks[gp->g_lockpslot]);
472 #endif
473 		gp->g_lock->gl_lockslot = 0;
474 		if (gp->g_lock->gl_locks[gp->g_lockpslot] == 0) {
475 			gp->g_lockp = NULL;
476 			gp->g_lockpslot = 0;
477 		}
478 	}
479 	if (gp->g_lockp) {
480 		if (gp->g_lockp == p)
481 			return(EBUSY);
482 		if (!block)
483 			return(OEAGAIN);
484 		do {
485 			gp->g_flags |= GF_WANTED;
486 			if ((error = tsleep((caddr_t)&gp->g_flags,
487 					   (PZERO+1) | PCATCH, devioc, 0)))
488 				return (error);
489 		} while (gp->g_lockp);
490 	}
491 	gp->g_lockp = p;
492 	if (gp->g_pid) {
493 		int slot = grffindpid(gp);
494 
495 #ifdef DEBUG
496 		if (grfdebug & GDB_LOCK)
497 			printf("  slot %d\n", slot);
498 #endif
499 		gp->g_lockpslot = gp->g_lock->gl_lockslot = slot;
500 		gp->g_lock->gl_locks[slot] = 1;
501 	}
502 	return(0);
503 }
504 
505 int
506 grfunlock(gp)
507 	register struct grf_softc *gp;
508 {
509 #ifdef DEBUG
510 	if (grfdebug & GDB_LOCK)
511 		printf("grfunlock(%d): flags %x lockpid %d\n",
512 		       curproc->p_pid, gp->g_flags,
513 		       gp->g_lockp ? gp->g_lockp->p_pid : -1);
514 #endif
515 	if (gp->g_lockp != curproc)
516 		return(EBUSY);
517 	if (gp->g_pid) {
518 #ifdef DEBUG
519 		if (grfdebug & GDB_LOCK)
520 			printf(" lockpslot %d lockslot %d lock[lockslot] %d\n",
521 			       gp->g_lock->gl_lockslot, gp->g_lockpslot,
522 			       gp->g_lock->gl_locks[gp->g_lockpslot]);
523 #endif
524 		gp->g_lock->gl_locks[gp->g_lockpslot] = 0;
525 		gp->g_lockpslot = gp->g_lock->gl_lockslot = 0;
526 	}
527 	if (gp->g_flags & GF_WANTED) {
528 		wakeup((caddr_t)&gp->g_flags);
529 		gp->g_flags &= ~GF_WANTED;
530 	}
531 	gp->g_lockp = NULL;
532 	return(0);
533 }
534 
535 /*
536  * Convert a BSD style minor devno to HPUX style.
537  * We cannot just create HPUX style nodes as they require 24 bits
538  * of minor device number and we only have 8.
539  * XXX: This may give the wrong result for remote stats of other
540  * machines where device 10 exists.
541  */
542 int
543 grfdevno(dev)
544 	dev_t dev;
545 {
546 	int unit = GRFUNIT(dev);
547 	struct grf_softc *gp;
548 	int newdev;
549 
550 	if (unit >= grf_cd.cd_ndevs ||
551 	    (gp = grf_cd.cd_devs[unit]) == NULL ||
552 	    (gp->g_flags&GF_ALIVE) == 0)
553 		return(bsdtohpuxdev(dev));
554 	/* magic major number */
555 	newdev = 12 << 24;
556 	/* now construct minor number */
557 	if (gp->g_display.gd_regaddr != (caddr_t)GRFIADDR) {
558 		int sc = patosc(gp->g_display.gd_regaddr);
559 		newdev |= (sc << 16) | 0x200;
560 	}
561 	if (dev & GRFIMDEV)
562 		newdev |= 0x02;
563 	else if (dev & GRFOVDEV)
564 		newdev |= 0x01;
565 #ifdef DEBUG
566 	if (grfdebug & GDB_DEVNO)
567 		printf("grfdevno: dev %x newdev %x\n", dev, newdev);
568 #endif
569 	return(newdev);
570 }
571 
572 #endif	/* COMPAT_HPUX */
573 
574 int
575 grfmap(dev, addrp, p)
576 	dev_t dev;
577 	caddr_t *addrp;
578 	struct proc *p;
579 {
580 	struct grf_softc *gp = grf_cd.cd_devs[GRFUNIT(dev)];
581 	int len, error;
582 	struct vnode vn;
583 	struct specinfo si;
584 	int flags;
585 
586 #ifdef DEBUG
587 	if (grfdebug & GDB_MMAP)
588 		printf("grfmap(%d): addr %p\n", p->p_pid, *addrp);
589 #endif
590 	len = gp->g_display.gd_regsize + gp->g_display.gd_fbsize;
591 	flags = MAP_SHARED;
592 	if (*addrp)
593 		flags |= MAP_FIXED;
594 	else
595 		*addrp =
596 		    (caddr_t)VM_DEFAULT_ADDRESS(p->p_vmspace->vm_daddr, len);
597 	vn.v_type = VCHR;			/* XXX */
598 	vn.v_specinfo = &si;			/* XXX */
599 	vn.v_rdev = dev;			/* XXX */
600 	error = uvm_mmap(&p->p_vmspace->vm_map, (vaddr_t *)addrp,
601 			 (vsize_t)len, VM_PROT_ALL, VM_PROT_ALL,
602 			 flags, (caddr_t)&vn, 0,
603 			 p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
604 	if (error == 0)
605 		(void) (*gp->g_sw->gd_mode)(gp, GM_MAP, *addrp);
606 	return(error);
607 }
608 
609 int
610 grfunmap(dev, addr, p)
611 	dev_t dev;
612 	caddr_t addr;
613 	struct proc *p;
614 {
615 	struct grf_softc *gp = grf_cd.cd_devs[GRFUNIT(dev)];
616 	vsize_t size;
617 
618 #ifdef DEBUG
619 	if (grfdebug & GDB_MMAP)
620 		printf("grfunmap(%d): dev %x addr %p\n", p->p_pid, dev, addr);
621 #endif
622 	if (addr == 0)
623 		return(EINVAL);		/* XXX: how do we deal with this? */
624 	(void) (*gp->g_sw->gd_mode)(gp, GM_UNMAP, 0);
625 	size = round_page(gp->g_display.gd_regsize + gp->g_display.gd_fbsize);
626 	uvm_unmap(&p->p_vmspace->vm_map, (vaddr_t)addr,
627 	    (vaddr_t)addr + size);
628 	return 0;
629 }
630 
631 #ifdef COMPAT_HPUX
632 int
633 iommap(dev, addrp)
634 	dev_t dev;
635 	caddr_t *addrp;
636 {
637 
638 #ifdef DEBUG
639 	if (grfdebug & (GDB_MMAP|GDB_IOMAP))
640 		printf("iommap(%d): addr %p\n", curproc->p_pid, *addrp);
641 #endif
642 	return(EINVAL);
643 }
644 
645 int
646 iounmmap(dev, addr)
647 	dev_t dev;
648 	caddr_t addr;
649 {
650 #ifdef DEBUG
651 	int unit = minor(dev);
652 
653 	if (grfdebug & (GDB_MMAP|GDB_IOMAP))
654 		printf("iounmmap(%d): id %d addr %p\n",
655 		       curproc->p_pid, unit, addr);
656 #endif
657 	return(0);
658 }
659 
660 /*
661  * Processes involved in framebuffer mapping via GCSLOT are recorded in
662  * an array of pids.  The first element is used to record the last slot used
663  * (for faster lookups).  The remaining elements record up to GRFMAXLCK-1
664  * process ids.  Returns a slot number between 1 and GRFMAXLCK or 0 if no
665  * slot is available.
666  */
667 int
668 grffindpid(gp)
669 	struct grf_softc *gp;
670 {
671 	register short pid, *sp;
672 	register int i, limit;
673 	int ni;
674 
675 	if (gp->g_pid == NULL) {
676 		gp->g_pid = (short *)
677 			malloc(GRFMAXLCK * sizeof(short), M_DEVBUF, M_WAITOK);
678 		memset((caddr_t)gp->g_pid, 0, GRFMAXLCK * sizeof(short));
679 	}
680 	pid = curproc->p_pid;
681 	ni = limit = gp->g_pid[0];
682 	for (i = 1, sp = &gp->g_pid[1]; i <= limit; i++, sp++) {
683 		if (*sp == pid)
684 			goto done;
685 		if (*sp == 0)
686 			ni = i;
687 	}
688 	i = ni;
689 	if (i < limit) {
690 		gp->g_pid[i] = pid;
691 		goto done;
692 	}
693 	if (++i == GRFMAXLCK)
694 		return(0);
695 	gp->g_pid[0] = i;
696 	gp->g_pid[i] = pid;
697 done:
698 #ifdef DEBUG
699 	if (grfdebug & GDB_LOCK)
700 		printf("grffindpid(%d): slot %d of %d\n",
701 		       pid, i, gp->g_pid[0]);
702 #endif
703 	return(i);
704 }
705 
706 void
707 grfrmpid(gp)
708 	struct grf_softc *gp;
709 {
710 	register short pid, *sp;
711 	register int limit, i;
712 	int mi;
713 
714 	if (gp->g_pid == NULL || (limit = gp->g_pid[0]) == 0)
715 		return;
716 	pid = curproc->p_pid;
717 	limit = gp->g_pid[0];
718 	mi = 0;
719 	for (i = 1, sp = &gp->g_pid[1]; i <= limit; i++, sp++) {
720 		if (*sp == pid)
721 			*sp = 0;
722 		else if (*sp)
723 			mi = i;
724 	}
725 	i = mi;
726 	if (i < limit)
727 		gp->g_pid[0] = i;
728 #ifdef DEBUG
729 	if (grfdebug & GDB_LOCK)
730 		printf("grfrmpid(%d): slot %d of %d\n",
731 		       pid, sp-gp->g_pid, gp->g_pid[0]);
732 #endif
733 }
734 
735 int
736 grflckmmap(dev, addrp)
737 	dev_t dev;
738 	caddr_t *addrp;
739 {
740 #ifdef DEBUG
741 	struct proc *p = curproc;		/* XXX */
742 
743 	if (grfdebug & (GDB_MMAP|GDB_LOCK))
744 		printf("grflckmmap(%d): addr %p\n",
745 		       p->p_pid, *addrp);
746 #endif
747 	return(EINVAL);
748 }
749 
750 int
751 grflckunmmap(dev, addr)
752 	dev_t dev;
753 	caddr_t addr;
754 {
755 #ifdef DEBUG
756 	int unit = minor(dev);
757 
758 	if (grfdebug & (GDB_MMAP|GDB_LOCK))
759 		printf("grflckunmmap(%d): id %d addr %p\n",
760 		       curproc->p_pid, unit, addr);
761 #endif
762 	return(EINVAL);
763 }
764 #endif	/* COMPAT_HPUX */
765