From owner-FreeBSD-tech-jp@jp.freebsd.org  Wed Aug 20 02:09:45 1997
Received: by jaz.jp.freebsd.org (8.8.7+2.7Wbeta6/8.7.3) id CAA19105
	Wed, 20 Aug 1997 02:09:45 +0900 (JST)
Received: by jaz.jp.freebsd.org (8.8.7+2.7Wbeta6/8.7.3) with ESMTP id CAA19099
	for <FreeBSD-tech-jp@jp.freebsd.org>; Wed, 20 Aug 1997 02:09:43 +0900 (JST)
Received: from mailsv.nec.co.jp ([133.200.254.203]) by TYO9.gate.nec.co.jp (8.8.5+2.7Wbeta5/3.4Wb-NEC-TYO9) with ESMTP id CAA27020 for <FreeBSD-tech-jp@jp.freebsd.org>; Wed, 20 Aug 1997 02:09:42 +0900 (JST)
Received: from gw.ccs.mt.nec.co.jp (gw.ccs.mt.nec.co.jp [133.201.2.2]) by mailsv.nec.co.jp (8.8.5+2.7Wbeta5/3.4W-97062018) with ESMTP
	id CAA05492 for <FreeBSD-tech-jp@jp.freebsd.org>; Wed, 20 Aug 1997 02:09:42 +0900 (JST)
Received: from mail.ccs.mt.nec.co.jp (mail.ccs.mt.nec.co.jp [133.201.3.22]) by gw.ccs.mt.nec.co.jp (8.8.5+2.7Wbeta4/3.3W9-GW_CCS) with ESMTP id TAA13315 for <FreeBSD-tech-jp@jp.freebsd.org>; Tue, 19 Aug 1997 19:49:24 +0900 (JST)
Received: from spls63.ccs.mt.nec.co.jp by mail.ccs.mt.nec.co.jp (8.8.5+2.7Wbeta4/6.4J.6-ccs_mx)
	id TAA10496; Tue, 19 Aug 1997 19:49:23 +0900 (JST)
Received: from localhost by spls63.ccs.mt.nec.co.jp (8.6.12+2.4W/6.4J.6-slave-1.0)
	id TAA01345; Tue, 19 Aug 1997 19:50:10 +0900
Message-Id: <199708191050.TAA01345@spls63.ccs.mt.nec.co.jp>
To: FreeBSD-tech-jp@jp.freebsd.org
In-reply-to: Your message of "Wed, 06 Aug 1997 11:45:47 JST"
References: <199708060245.LAA05415@spls63.ccs.mt.nec.co.jp> 
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-2022-JP
Date: Tue, 19 Aug 1997 19:50:06 +0900
From: Nakagawa Yoshihisa <y-nakaga@ccs.mt.nec.co.jp>
Reply-To: FreeBSD-tech-jp@jp.freebsd.org
Precedence: bulk
X-Distribute: distribute [version 2.1 (Alpha) patchlevel=20]
X-Sequence: FreeBSD-tech-jp 433
Subject: [FreeBSD-tech-jp 433] Re: IDE auto configure 
Errors-To: owner-FreeBSD-tech-jp@jp.freebsd.org
Sender: owner-FreeBSD-tech-jp@jp.freebsd.org

$B$H$j$"$($:(B

| $B!&(BPAO $B$G(B ATAPI CD $B$rH4$-:9$7$9$k$H(B wcd0, 1, 2... $B$HA}$($F$$$/!)(B
| $B!J%3!<%I$r8+$k$H!"(Bdetach $B$7$F$$$k$h$&$K$O8+$($J$$!#!K(B

$B$N(B ATAPI_DETACH $B$@$1$G$-$^$7$?!#!J(BLKM $B$O$G$-$F$^$;$s!#!K(BIDE disk 
$B$NJ}$O;~4VE*LdBj$H8GDj3d$jEv$F$N;~$NLdBj$,$"$C$F$^$@$G$-$F$^$;$s!#(B

| ATAPI CD $B$H$+;}$C$F$J$$$s$G!"%Q%C%A$,$G$-$?$iC/$+%F%9%H$7$F$/$@$5(B
| $B$$$M!#(B

$B$H$$$&$3$H$J$N$G!"C/$+%F%9%H$*4j$$$7$^$9!#(Bmake $B$,DL$k$3$H$H(B IDE
disk $B$X$NI{:nMQ$O$J$5$=$&$J$3$H$@$1$7$+3NG'$7$F$^$;$s!#(B

-- 
	$BF|K\EE5$3t<02q<R(B $B%M%C%H%o!<%-%s%05;=Q8&5f=j(B $B%$%s%?%M%C%H5;=QIt(B
		$BCf@n(B $B9%5W(B	y-nakaga@ccs.mt.nec.co.jp
				jj3mxl@eatell.msr.prug.or.jp

diff -rc i386/isa.org/atapi.c i386/isa/atapi.c
*** i386/isa.org/atapi.c	Tue Aug 19 19:39:46 1997
--- i386/isa/atapi.c	Tue Aug 19 19:39:30 1997
***************
*** 143,148 ****
--- 143,154 ----
  	++atapi_ndrv;
  	return (1);
  }
+ 
+ #ifdef ATAPI_DETACH
+ int atapi_detach (int ctlr, int unit, int port)
+ {
+ }
+ #endif /* ATAPI_DETACH */
  #else /* ATAPI_STATIC */
  /* this code is compiled part of the module */
  
***************
*** 172,177 ****
--- 178,186 ----
  
  extern int wdstart (int ctrlr);
  extern int wcdattach(struct atapi*, int, struct atapi_params*, int);
+ #ifdef ATAPI_DETACH
+ extern int wcddetach(struct atapi*, int, struct atapi_params*, int);
+ #endif /* ATAPI_DETACH */
  
  /*
   * Probe the ATAPI device at IDE controller `ctlr', drive `unit'.
***************
*** 306,311 ****
--- 315,373 ----
  	return (0);
  #endif /* ATAPI_MODULE */
  }
+ 
+ #ifdef ATAPI_DETACH
+ #ifdef ATAPI_MODULE
+ static
+ #endif
+ int atapi_detach (int ctlr, int unit, int port)
+ {
+ 	struct atapi *ata = atapitab + ctlr;
+ 	struct atapi_params *ap;
+ 
+ 	if (ata->ctrlr == ctlr &&
+ 	    ata->port == port) {
+ 		ap = ata->params[unit];
+ 		switch (ap->devtype) {
+ 			default:
+ 				/* unknown ATAPI device */
+ 				break;
+ 
+ 			case AT_TYPE_DIRECT:            /* direct-access */
+ 			case AT_TYPE_CDROM:             /* CD-ROM device */
+ #if NWCD > 0
+ 				/* ATAPI CD-ROM */
+ 				if (wcddetach (ata, unit, ap, ata->debug) < 0)
+ 					break;
+ 				/* Device detached successfully. */
+ 				ata->attached[unit] = 0;
+ #endif
+ 				break;
+ 
+ 			case AT_TYPE_TAPE:              /* streaming tape (QIC-121 model) */
+ #if NWMT > 0
+ 				/* Add your driver here */
+ #endif
+ 				break;
+ 
+ 			case AT_TYPE_OPTICAL:           /* optical disk */
+ #if NWMD > 0
+ 				/* Add your driver here */
+ #endif
+ 				break;
+ 		}
+ 		/* Detach failed. */
+ 		if (ata->attached[unit])
+ 			return EBUSY;
+ 
+ 		if (ata->params[unit]) {
+ 			free (ata->params[unit], M_TEMP);
+ 			ata->params[unit] = 0;
+ 		}
+ 	}
+ 	return (1);
+ }
+ #endif /* ATAPI_DETACH */
  
  static char *cmdname (u_char cmd)
  {
diff -rc i386/isa.org/atapi.h i386/isa/atapi.h
*** i386/isa.org/atapi.h	Tue Aug 19 19:39:46 1997
--- i386/isa/atapi.h	Tue Aug 19 19:39:30 1997
***************
*** 259,264 ****
--- 259,267 ----
  
  #ifndef ATAPI_MODULE
  int atapi_attach (int ctlr, int unit, int port);
+ #ifdef ATAPI_DETACH
+ int atapi_detach (int ctlr, int unit, int port);
+ #endif
  #endif
  
  /*
diff -rc i386/isa.org/wcd.c i386/isa/wcd.c
*** i386/isa.org/wcd.c	Tue Aug 19 19:39:46 1997
--- i386/isa/wcd.c	Tue Aug 19 19:39:30 1997
***************
*** 63,68 ****
--- 63,74 ----
  static
  #endif
  int  wcdattach(struct atapi*, int, struct atapi_params*, int);
+ #ifdef ATAPI_DETACH
+ #ifndef ATAPI_STATIC
+ static
+ #endif
+ int  wcddetach(struct atapi*, int, struct atapi_params*, int);
+ #endif /* ATAPI_DETACH */
  
  #define NUNIT   (NWDC*2)                /* Max. number of devices */
  #define UNIT(d) ((minor(d) >> 3) & 3)   /* Unit part of minor device number */
***************
*** 234,240 ****
--- 240,250 ----
  };
  
  struct wcd *wcdtab[NUNIT];      /* Drive info by unit number */
+ #ifndef ATAPI_DETACH
  static int wcdnlun = 0;         /* Number of configured drives */
+ #else /* ATAPI_DETACH */
+ static int wcd_unit_use = 0;    /* Number of configured drives */
+ #endif /* ATAPI_DETACH */
  
  static void wcd_start (struct wcd *t);
  static void wcd_done (struct wcd *t, struct buf *bp, int resid,
***************
*** 273,282 ****
--- 283,303 ----
  	struct atapires result;
  	int lun;
  
+ #ifndef ATAPI_DETACH
  	if (wcdnlun >= NUNIT) {
  		printf ("wcd: too many units\n");
  		return (0);
  	}
+ #else /* ATAPI_DETACH */
+ 	for (lun=0; lun < NUNIT; lun++) {
+ 		if (!(wcd_unit_use & (1 << lun)))
+ 			break;
+ 	}
+ 	if (lun >= NUNIT) {
+ 		printf ("wcd: too many units\n");
+ 		return (0);
+ 	}
+ #endif /* ATAPI_DETACH */
  	if (!atapi_request_immediate) {
  		printf("wcd: configuration error, ATAPI core code not present!\n");
  		printf("wcd: check `options ATAPI_STATIC' in your kernel config file!\n");
***************
*** 287,297 ****
--- 308,327 ----
  		printf ("wcd: out of memory\n");
  		return (0);
  	}
+ #ifndef ATAPI_DETACH
  	wcdtab[wcdnlun] = t;
+ #else /* ATAPI_DETACH */
+ 	wcdtab[lun] = t;
+ #endif /* ATAPI_DETACH */
  	bzero (t, sizeof (struct wcd));
  	t->ata = ata;
  	t->unit = unit;
+ #ifndef ATAPI_DETACH
  	lun = t->lun = wcdnlun++;
+ #else /* ATAPI_DETACH */
+ 	t->lun = lun;
+ 	wcd_unit_use |= (1 << lun);
+ #endif /* ATAPI_DETACH */
  	t->param = ap;
  	t->flags = F_MEDIA_CHANGED;
  	t->refcnt = 0;
***************
*** 346,351 ****
--- 376,419 ----
  	return (1);
  }
  
+ #ifdef ATAPI_DETACH
+ #ifndef ATAPI_STATIC
+ static
+ #endif
+ int 
+ wcddetach (struct atapi *ata, int unit, struct atapi_params *ap, int debug)
+ {
+ 	struct wcd **t;
+ 	int lun;
+ 
+ 	for (lun=0; lun < NUNIT; lun++) {
+ 		if ((wcd_unit_use & (1 << lun))) {
+ 			t = wcdtab + lun;
+ 			if ((*t)->ata->port == ata->port &&
+ 			    (*t)->ata->ctrlr == ata->ctrlr &&
+ 			    (*t)->unit == unit)
+ 				break;
+ 		}
+ 	}
+ 	if (lun >= NUNIT) {
+ 		printf ("wcd: too many units\n");
+ 		return (0);
+ 	}
+ 
+ 	t = wcdtab + lun;
+ 	if (((*t)->flags & F_BOPEN) || (*t)->refcnt)
+ 		/* The device is opened, cannot unload the driver. */
+ 		return EBUSY;
+ 	(*t)->ata->attached[(*t)->unit] = 0;
+ 	free (*t, M_TEMP);
+ 	wcdtab[lun] = NULL;
+ 
+ 	wcd_unit_use &= ~(1 << lun);
+ 
+ 	return 0;
+ }
+ #endif /* ATAPI_DETACH */
+ 
  void wcd_describe (struct wcd *t)
  {
  	char *m;
***************
*** 412,419 ****
--- 480,492 ----
  
  	/* Check that the device number is legal
  	 * and the ATAPI driver is loaded. */
+ #ifndef ATAPI_DETACH
  	if (lun >= wcdnlun || ! atapi_request_immediate)
  		return (ENXIO);
+ #else /* ATAPI_DETACH */
+ 	if (!(wcd_unit_use & (1 << lun)) || ! atapi_request_immediate)
+ 		return (ENXIO);
+ #endif /* ATAPI_DETACH */
  	t = wcdtab[lun];
  
  	/* On the first open, read the table of contents. */
***************
*** 1150,1155 ****
--- 1223,1229 ----
  {
  	struct wcd **t;
  
+ #ifndef ATAPI_DETACH
  	for (t=wcdtab; t<wcdtab+wcdnlun; ++t)
  		if (((*t)->flags & F_BOPEN) || (*t)->refcnt)
  			/* The device is opened, cannot unload the driver. */
***************
*** 1159,1164 ****
--- 1233,1254 ----
  		free (*t, M_TEMP);
  	}
  	wcdnlun = 0;
+ #else /* ATAPI_DETACH */
+ 	for (int lun=0; lun < NUNIT; ++lun)
+ 	    if (wcd_unit_use & (1 << lun)) {
+ 		t = wcdtab + lun;
+ 		if (((*t)->flags & F_BOPEN) || (*t)->refcnt)
+ 			/* The device is opened, cannot unload the driver. */
+ 			return EBUSY;
+ 	    }
+ 	for (int lun=0; lun < NUNIT; ++lun)
+ 	    if (wcd_unit_use & (1 << lun)) {
+ 		t = wcdtab + lun;
+ 		(*t)->ata->attached[(*t)->unit] = 0;
+ 		free (*t, M_TEMP);
+ 	    }
+ 	wcd_unit_use = 0;
+ #endif /* ATAPI_DETACH */
  	bzero (wcdtab, sizeof(wcdtab));
  	return 0;
  }
diff -rc i386/isa.org/wd.c i386/isa/wd.c
*** i386/isa.org/wd.c	Tue Aug 19 19:39:46 1997
--- i386/isa/wd.c	Tue Aug 19 19:39:30 1997
***************
*** 375,380 ****
--- 375,384 ----
  	}
  	ctrlr_in_use &= ~(1<<ctrlr);
  	/* should unload atapi unit */
+ #ifdef ATAPI_DETACH
+ 	for (lunit=0; lunit<2; lunit++)
+ 		atapi_detach (ctrlr, lunit, dp->isahd.id_iobase);
+ #endif
  
  	printf("wdc%d: unloading -- ", ctrlr);
  	if (wdtab[ctrlr].b_active != 0)
