Subject: kern/5635: PAS driver doesn't do bus space allocation right
To: None <gnats-bugs@gnats.netbsd.org>
From: John F. Woods <jfw@jfwhome.funhouse.com>
List: netbsd-bugs
Date: 06/20/1998 21:14:05
>Number:         5635
>Category:       kern
>Synopsis:       PAS driver doesn't do bus space allocation right
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    kern-bug-people (Kernel Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Jun 22 08:50:01 1998
>Last-Modified:
>Originator:     John F. Woods
>Organization:
Misanthropes-R-Us
>Release:        Jun 19, 1998
>Environment:
	
System: NetBSD jfwhome.funhouse.com 1.3F NetBSD 1.3F (JFW) #5: Sat Jun 20 09:52:15 EDT 1998 jfw@jfwhome.funhouse.com:/usr/src/sys/arch/i386/compile/JFW i386


>Description:
	The PAS driver fail initialization in pasfind, claiming it cannot
allocate space from the IOmap at 0x220/10.
	The problem is that pasfind is called twice, once from the probe
routine and once from the attach routine; unfortunately, it does not unmap
the bus space that it maps when probing, so when it tries to attach, the
space is already reserved.
>How-To-Repeat:
	Config, compile, install card, boot.
>Fix:
	The following diffs cause the driver to work.  They don't strike me
as very tasteful, but I think they're straightforward enough to be pretty safe.
The comment isn't necessary, but might be worth keeping.  (Geez, I really need
to get a GOOD sound card one of these days...)

*** pas.c.orig	Wed Jun 17 07:55:44 1998
--- pas.c	Sat Jun 20 09:51:40 1998
***************
*** 45,50 ****
--- 45,60 ----
   * 	- look at other PAS drivers (for PAS native suport)
   * 	- use common sb.c once emulation is setup
   */
+ /*
+  * jfw 6/21/98 - WARNING:  the PAS native IO ports are scattered all around
+  * IO port space (0x0388, 0x738B, 0xBF88, 0x2789, ...) which will make proper
+  * reservation a real pain, so I'm not going to do it (while fixing the
+  * current reservation code to "work").  As a sanity check, I reserve the
+  * 0x0388 base address, but you probably shouldn't even think of trying this
+  * driver unless you're certain you have the hardware installed and it doesn't
+  * conflict with other hardware...
+  */
+ 
  
  #include <sys/param.h>
  #include <sys/systm.h>
***************
*** 94,99 ****
--- 104,110 ----
   */
  struct pas_softc {
  	struct sbdsp_softc sc_sbdsp;	/* base device, &c. */
+         bus_space_handle_t pas_port_handle;    /* the pas-specific port */
  
  	int model;
  	int rev;
***************
*** 238,244 ****
  int	pasprobe __P((struct device *, struct cfdata *, void *));
  void	pasattach __P((struct device *, struct device *, void *));
  static	int pasfind __P((struct device *, struct pas_softc *, 
! 			struct isa_attach_args *));
  
  struct cfattach pas_ca = {
  	sizeof(struct pas_softc), pasprobe, pasattach
--- 249,258 ----
  int	pasprobe __P((struct device *, struct cfdata *, void *));
  void	pasattach __P((struct device *, struct device *, void *));
  static	int pasfind __P((struct device *, struct pas_softc *, 
! 			struct isa_attach_args *, int));
! /* argument to pasfind */
! #define PASPROBE  1
! #define PASATTACH 0
  
  struct cfattach pas_ca = {
  	sizeof(struct pas_softc), pasprobe, pasattach
***************
*** 259,278 ****
  	bzero(sc, sizeof *sc);
  	sc->sc_sbdsp.sc_dev.dv_cfdata = match;
  	strcpy(sc->sc_sbdsp.sc_dev.dv_xname, "pas");
! 	return pasfind(parent, sc, aux);
  }
  
  /*
   * Probe for the soundblaster hardware.
   */
  static int
! pasfind(parent, sc, ia)
  	struct device *parent;
  	struct pas_softc *sc;
  	struct isa_attach_args *ia;
  {
  	int iobase;
  	u_char id, t;
  
          /* ensure we can set this up as a sound blaster */
         	if (!SB_BASE_VALID(ia->ia_iobase)) {
--- 273,294 ----
  	bzero(sc, sizeof *sc);
  	sc->sc_sbdsp.sc_dev.dv_cfdata = match;
  	strcpy(sc->sc_sbdsp.sc_dev.dv_xname, "pas");
! 	return pasfind(parent, sc, aux, PASPROBE);
  }
  
  /*
   * Probe for the soundblaster hardware.
   */
  static int
! pasfind(parent, sc, ia, probing)
  	struct device *parent;
  	struct pas_softc *sc;
  	struct isa_attach_args *ia;
+ 	int probing;
  {
  	int iobase;
  	u_char id, t;
+ 	int rc = 0;  /* failure */
  
          /* ensure we can set this up as a sound blaster */
         	if (!SB_BASE_VALID(ia->ia_iobase)) {
***************
*** 280,285 ****
--- 296,308 ----
  		return 0;
  	}
  
+ 	if (bus_space_map(sc->sc_sbdsp.sc_iot, PAS_DEFAULT_BASE, 1, 0,
+                           &sc->pas_port_handle)) {
+ 		printf("pas: can't map base register %x in probe\n",
+ 		       PAS_DEFAULT_BASE);
+ 		return 0;
+ 	}
+ 
  	/*
  	 * WARNING: Setting an option like W:1 or so that disables
  	 * warm boot reset of the card will screw up this detect code
***************
*** 311,317 ****
  	if (id == 0xff || id == 0xfe) {
  		/* sanity */
  		DPRINTF(("pas: bogus card id\n"));
! 		return 0;
  	}
  	/*
  	 * We probably have a PAS-series board, now check for a
--- 334,340 ----
  	if (id == 0xff || id == 0xfe) {
  		/* sanity */
  		DPRINTF(("pas: bogus card id\n"));
! 		goto unmap1;
  	}
  	/*
  	 * We probably have a PAS-series board, now check for a
***************
*** 327,333 ****
  	if (t != id) {
  		/* Not a PAS2 */
  		printf("pas: detected card but PAS2 test failed\n");
! 		return 0;
  	}
  	/*XXX*/
  	t = pasread(OPERATION_MODE_1) & 0xf;
--- 350,356 ----
  	if (t != id) {
  		/* Not a PAS2 */
  		printf("pas: detected card but PAS2 test failed\n");
! 		goto unmap1;
  	}
  	/*XXX*/
  	t = pasread(OPERATION_MODE_1) & 0xf;
***************
*** 337,354 ****
  	}
  	else {
  		DPRINTF(("pas: bogus model id\n"));
! 		return 0;
  	}
  
          if (sc->model >= 0) {
                  if (ia->ia_irq == IRQUNK) {
                          printf("pas: sb emulation requires known irq\n");
!                         return (0);
                  } 
                  pasconf(sc->model, ia->ia_iobase, ia->ia_irq, 1);
          } else {
                  DPRINTF(("pas: could not probe pas\n"));
!                 return (0);
          }
  
  	/* Now a SoundBlaster, so set up proper bus-space hooks
--- 360,377 ----
  	}
  	else {
  		DPRINTF(("pas: bogus model id\n"));
! 		goto unmap1;
  	}
  
          if (sc->model >= 0) {
                  if (ia->ia_irq == IRQUNK) {
                          printf("pas: sb emulation requires known irq\n");
! 			goto unmap1;
                  } 
                  pasconf(sc->model, ia->ia_iobase, ia->ia_irq, 1);
          } else {
                  DPRINTF(("pas: could not probe pas\n"));
! 		goto unmap1;
          }
  
  	/* Now a SoundBlaster, so set up proper bus-space hooks
***************
*** 363,369 ****
  	    &sc->sc_sbdsp.sc_ioh)) {
  		printf("pas: can't map i/o space 0x%x/%d in probe\n",
  		    ia->ia_iobase, SBP_NPORT);
! 		return 0;
  	}
  
  	if (sbdsp_reset(&sc->sc_sbdsp) < 0) {
--- 386,392 ----
  	    &sc->sc_sbdsp.sc_ioh)) {
  		printf("pas: can't map i/o space 0x%x/%d in probe\n",
  		    ia->ia_iobase, SBP_NPORT);
! 		goto unmap;
  	}
  
  	if (sbdsp_reset(&sc->sc_sbdsp) < 0) {
***************
*** 405,416 ****
  		goto unmap;
  	}
  
  	ia->ia_iosize = SB_NPORT;
- 	return 1;
  
   unmap:
! 	bus_space_unmap(sc->sc_sbdsp.sc_iot, sc->sc_sbdsp.sc_ioh, SBP_NPORT);
! 	return 0;
  }
  
  #ifdef NEWCONFIG
--- 428,443 ----
  		goto unmap;
  	}
  
+ 	rc = 1;
  	ia->ia_iosize = SB_NPORT;
  
   unmap:
! 	if (rc == 0 || probing)
! 	        bus_space_unmap(sc->sc_sbdsp.sc_iot, sc->sc_sbdsp.sc_ioh, SBP_NPORT);
!  unmap1:
! 	if (rc == 0 || probing)
! 	        bus_space_unmap(sc->sc_sbdsp.sc_iot, PAS_DEFAULT_BASE, 1);
! 	return rc;
  }
  
  #ifdef NEWCONFIG
***************
*** 454,460 ****
  	struct isa_attach_args *ia = (struct isa_attach_args *)aux;
  	int iobase = ia->ia_iobase;
  	
! 	if (!pasfind(parent, sc, ia)) {
  		printf("%s: pasfind failed\n", sc->sc_sbdsp.sc_dev.dv_xname);
  		return;
  	}
--- 481,487 ----
  	struct isa_attach_args *ia = (struct isa_attach_args *)aux;
  	int iobase = ia->ia_iobase;
  	
! 	if (!pasfind(parent, sc, ia, PASATTACH)) {
  		printf("%s: pasfind failed\n", sc->sc_sbdsp.sc_dev.dv_xname);
  		return;
  	}
>Audit-Trail:
>Unformatted: