Subject: port-mac68k/6478: .bcscreen device driver(LKM)
To: None <gnats-bugs@gnats.netbsd.org>
From: None <fk200329@fsinet.or.jp>
List: netbsd-bugs
Date: 11/22/1998 17:13:11
>Number:         6478
>Category:       port-mac68k
>Synopsis:       .bcscreen device driver(LKM)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    gnats-admin (GNATS administrator)
>State:          open
>Class:          support
>Submitter-Id:   net
>Arrival-Date:   Sun Nov 22 00:20:01 1998
>Last-Modified:
>Originator:     Hiroyuki Yanai
>Organization:
--
>Release:        GENERIC
>Environment:
	
System: NetBSD LC520 1.3.2 NetBSD 1.3.2 (MYKERNEL) #5: Thu Oct 29 01:43:25 JST 1998 root@LC520:/usr/src/sys/arch/mac68k/compile/MYKERNEL mac68k
Apple Macintosh LC520
68030(ClockUp 25MHz -> 33MHz)+68882/33MHz

GENERIC-SBC kernel


>Description:
	This source-code is "bcscreen" device driver (LKM) for internal CRT control.
	This driver is experimental. Therefore it's may be dirty and bad method.


	Supported machines:

		*LC520
		*Performa520? (unclear)
		*LC/Performa550? (doubtful)



	Supported features:

		*CRT Brightness Control
		*CRT Contrast Control
		*CRT Power Control


	To Do:
	
		Use bus_space_*() functions.
		etc.



>How-To-Repeat:
	
>Fix:


===================================================================
bcscreen_dev.h
===================================================================
/*

  bcscreen_dev.h

  version 1.0.3.1
  1998/11/22  H.Yanai

*/

#include <sys/ioccom.h>



#define STAT_CRT_POWER_ON    1
#define STAT_CRT_POWER_OFF   0



typedef struct bcscreen_params {
  int  value;
} bcscreen_params;

typedef bcscreen_params *bcscreen_params_ptr;



/* control */
#define CMD_CRT_BRIGHTNESS             _IOW('*', 1, bcscreen_params)   
#define CMD_CRT_CONTRAST               _IOW('*', 2, bcscreen_params)
#define CMD_CRT_POWER_ON               _IO('*',  3)
#define CMD_CRT_POWER_OFF              _IO('*',  4)

/* query */
#define CMD_CRT_STAT_BRIGHTNESS        _IOR('*', 5, bcscreen_params)
#define CMD_CRT_STAT_CONTRAST          _IOR('*', 6, bcscreen_params)
#define CMD_CRT_STAT_POWER             _IOR('*', 7, bcscreen_params)


===================================================================
bcscreen_dev.c
===================================================================
/*
  bcscreen_dev.c

  version 1.0.3.1
  1998/11/22  By H.Yanai

*/

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <sys/exec.h>
#include <sys/lkm.h>
#include <sys/errno.h>


#define MODULE_NAME_STR  "bcscreen_dev"

        
extern  int bcscreen_dev_open(dev_t dev, int oflags, int devtype, struct proc *p);
extern  int bcscreen_dev_close(dev_t dev, int fflags, int devtype, struct proc *p);
extern  int bcscreen_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p);

extern void bcscreen_dev_init();



static struct cdevsw bcscreen_dev_cdevsw = {
        bcscreen_dev_open,                  /* d_open   */
        bcscreen_dev_close,                 /* d_close  */
        (dev_type_read ((*))) enodev,       /* d_read   */
        (dev_type_write ((*))) enodev,      /* d_write  */
        bcscreen_dev_ioctl,                 /* d_ioctl  */
        (dev_type_stop ((*))) enodev,       /* d_stop   */
        (dev_type_tty ((*))) enodev,        /* d_tty    */

        (dev_type_poll ((*))) enodev,       /* d_select(d_poll?) */
        (dev_type_mmap ((*))) enodev,       /* d_mmap   */
        0,                                  /* d_type   */
};



MOD_DEV(MODULE_NAME_STR, LM_DT_CHAR, -1, &bcscreen_dev_cdevsw);



static int module_load(struct lkm_table *lkmtp, int cmd) {

  if(cmd == LKM_E_LOAD) {
    printf("\n.BCScreen Device Driver v1.0.3.1.\n");
    printf("By Hiroyuki Yanai. (Umarete sumimasen) :D\n\n");

    bcscreen_dev_init();
  }

  return 0;
}



bcscreen_dev(struct lkm_table *lkmtp, int cmd, int ver) {
  DISPATCH(lkmtp, cmd, ver, module_load, lkm_nofunc, lkm_nofunc);
}


===================================================================
bcscreen_dev_func.c
===================================================================
/*
  bcscreen_dev_func.c

  version 1.0.3.1
  1998/11/22  H.Yanai
*/

#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/systm.h>
#include <sys/errno.h>

#include <machine/viareg.h>


#include "bcscreen_dev.h"



#ifdef MACH_LC520

#define vCRT_CTRL_BRIGHTNESS      0x18000L
#define vCRT_CTRL_CONTRAST        0x18001L
#define vCRT_CTRL_ONOFF           0x2600aL

#else  /* !MACH_LC520 */

#define vCRT_CTRL_BRIGHTNESS      0x00000L
#define vCRT_CTRL_CONTRAST        0x00000L
#define vCRT_CTRL_ONOFF           0x00000L

#endif  /* MACH_LC520 */



#ifdef MACH_LC520

#define CRTOFF_WAITVAL_HI   0xff380000L
#define CRTON_WAITVAL_HI    0xfce00000L
#define CRT_WAITVAL_LO      0x1579       /* TimeDBRA (0x0d00) */

#else  /* !MACH_LC520 */

#define CRTOFF_WAITVAL_HI   0x00000000L
#define CRTON_WAITVAL_HI    0x00000000L
#define CRT_WAITVAL_LO      0x0000       /* TimeDBRA (0x0d00) */

#endif   /* MACH_LC520 */



#define ON     -1
#define OFF    0


#define crt_control(r, v)    via_reg(VIA1, r) = (u_char) v
#define crt_off()            via_reg(VIA1, vCRT_CTRL_ONOFF) |= 0x01
#define crt_on()             via_reg(VIA1, vCRT_CTRL_ONOFF) &= 0xFE



static int cur_contrast_value;
static int cur_brightness_value;
static int cur_crtpower_state;



void bcscreen_dev_init();
int  bcscreen_dev_open(dev_t dev, int oflags, int devtype, struct proc *p);
int  bcscreen_dev_close(dev_t dev, int fflags, int devtype, struct proc *p);
int  bcscreen_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p);

static void crt_onoff_wait(int onoff);



void bcscreen_dev_init() {
  cur_brightness_value = 20;
  cur_contrast_value = 120;
  cur_crtpower_state = ON;
  crt_control(vCRT_CTRL_BRIGHTNESS, cur_brightness_value);
  crt_control(vCRT_CTRL_CONTRAST, cur_contrast_value);
}



static void  crt_onoff_wait(int onoff) {
  u_int32_t wtime_hi;
  u_int16_t wtime_lo;
  
  wtime_hi = (onoff == ON) ? (CRTON_WAITVAL_HI) : (CRTOFF_WAITVAL_HI);
  wtime_lo = CRT_WAITVAL_LO;

  asm volatile ("     movl  %0,          d0
                      movw  %1,          d1
                 L1:  movw  d1,          d0
                 L2:  dbf   d0,          L2
                      addl  #1,          d0
                      bne   L1
                "
      :
      :"g"(wtime_hi), "g"(wtime_lo)
      :"d0", "d1");
}



int bcscreen_dev_open(dev_t dev, int oflags, int devtype, struct proc *p) {
  return 0;
}



int bcscreen_dev_close(dev_t dev, int fflags, int devtype, struct proc *p) {
  return 0;
}



int bcscreen_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p) {
  bcscreen_params_ptr params;
  int err = 0;

  switch(cmd) {
    /* brightness */
    case CMD_CRT_BRIGHTNESS:
      params = (bcscreen_params_ptr) data;
      crt_control(vCRT_CTRL_BRIGHTNESS, params->value);
      cur_brightness_value = params->value;
      break;
    
    /* contrast */
    case CMD_CRT_CONTRAST:
      params = (bcscreen_params_ptr) data;
      crt_control(vCRT_CTRL_CONTRAST, params->value);
      cur_contrast_value = params->value;
      break;

    /* power off */
    case CMD_CRT_POWER_OFF:
      if(cur_crtpower_state == ON) {
	crt_control(vCRT_CTRL_CONTRAST, 0);
	crt_onoff_wait(OFF);
	crt_off();
	cur_crtpower_state = OFF;
      }
      break;

    /* power on */
    case CMD_CRT_POWER_ON:
      if(cur_crtpower_state == OFF) {
	crt_on();
	crt_onoff_wait(ON);
	crt_control(vCRT_CTRL_CONTRAST, cur_contrast_value);
	cur_crtpower_state = ON;
      }
     break;

    /* get current brightness value */
    case CMD_CRT_STAT_BRIGHTNESS:
      params = (bcscreen_params_ptr) data;
      params->value = cur_brightness_value;
      break;
    
    /* get current contrast value */
    case CMD_CRT_STAT_CONTRAST:
      params = (bcscreen_params_ptr) data;
      params->value =  cur_contrast_value;
      break;

    /* get current CRT power state */
    case CMD_CRT_STAT_POWER:
      params = (bcscreen_params_ptr) data;
      params->value = (cur_crtpower_state == ON) ? (STAT_CRT_POWER_ON) : (STAT_CRT_POWER_OFF);
      break;

    default:
      err = ENOTTY;
      break;
  }
  
  return err;
}

===================================================================
Makefile
===================================================================
SRCS = bcscreen_dev.c bcscreen_dev_func.c
OBJS = ${SRCS:.c=.o}

MODOBJ = bcscreen_dev_lkm.o

KMOD = bcscreen_dev

CFLAGS = -DMACH_LC520 -D_KERNEL -I/sys

MAKE = make    # make
LD = ld        # link editor

all:	${MODOBJ}

clean:
	rm -f ${OBJS} ${MODOBJ} ${KMOD}

ngclean:
	rm -f *~

load:
	modload -o ${KMOD} -e ${KMOD} ${MODOBJ}

unload:
	modunload -n ${KMOD}

${MODOBJ}: ${OBJS}
	${LD} -r -o ${MODOBJ} ${OBJS}

>Audit-Trail:
>Unformatted: