$OpenBSD: patch-cd_c,v 1.2 2001/06/25 12:44:33 lebel Exp $
--- cd.c.orig	Mon Jun 25 08:35:29 2001
+++ cd.c	Mon Jun 25 08:35:35 2001
@@ -46,7 +46,7 @@
 #include <sys/cdio.h>
 #endif
 
-#if defined(__FreeBSD__)
+#if defined(__FreeBSD__) || defined(__OpenBSD__) 
     #define CDIOREADSUBCHANNEL CDIOCREADSUBCHANNEL
 #endif
 
@@ -55,6 +55,10 @@
 #include <io/cam/cdrom.h>
 #endif
 
+ /* for OS's that need to re-open the CD after a disc change */
+extern char cddevice[];
+extern int g_cd_desc;
+
 /* Initialize the CD-ROM for playing audio CDs */
 
 int CDInitDevice(char *device_name)
@@ -128,9 +132,28 @@ int CDStat(int cd_desc,struct disc_info 
 #ifdef CDIOREADTOCHEADER
   struct ioc_toc_header cdth;
 #endif
+  int ret;
 
   int readtracks,frame[MAX_TRACKS],pos;
-  int retcode;
+
+#ifdef __OpenBSD__		/* fix for disc info never updating */
+  if ((disc->disc_present == 0) && (cd_desc < 0)) {
+      if((cd_desc = g_cd_desc = open(cddevice, O_RDONLY
+# ifdef NON_BLOCKING
+			 | O_NONBLOCK
+# endif
+			 )) < 0){
+# ifdef DEBUG
+	  // fprintf(stderr, "open failed (%s)\n", cddevice);
+	  fprintf(stderr, "o");
+# endif
+	  return -1;
+      }
+# ifdef DEBUG
+      fprintf(stderr, "*");
+# endif
+  }
+#endif /* __OpenBSD__  fix for disc info never updating */
 
 #ifdef CDROM_DRIVE_STATUS
   retcode = ioctl(cd_desc, CDROM_DRIVE_STATUS, CDSL_CURRENT);
@@ -143,29 +166,41 @@ int CDStat(int cd_desc,struct disc_info 
     return -1;
 #endif
 
-#ifdef CDIOREADSUBCHANNEL
+#ifdef CDIOREADSUBCHANNEL	/* netbsd */
   bzero(&cdsc, sizeof(cdsc));
   cdsc.data = &data;
   cdsc.data_len = sizeof(data);
   cdsc.data_format = CD_CURRENT_POSITION;
   cdsc.address_format = CD_MSF_FORMAT;
    
-  if(ioctl(cd_desc, CDIOCREADSUBCHANNEL, (char *)&cdsc) < 0)
+  if((ret = ioctl(cd_desc, CDIOCREADSUBCHANNEL, (char *)&cdsc)) < 0)
 #endif
 #ifdef CDROM_READ_SUBCHANNEL
     sch.sch_data_format = CDROM_CURRENT_POSITION;
 
   sch.sch_address_format = CDROM_MSF_FORMAT;
+
+  g_message("CDIOCREADSUBCHANNEL %d %m", ret);
    
   if(ioctl(cd_desc, CDROM_READ_SUBCHANNEL, &sch) < 0)
 #endif
-#ifdef CDROMSUBCHNL
+#ifdef CDROMSUBCHNL		/* netbsd - w. no toc flag, new disc */
     cdsc.cdsc_format = CDROM_MSF;
 
-    if(ioctl(cd_desc, CDROMSUBCHNL, &cdsc) < 0)
+    errno = 0;
+
+    if((ret = ioctl(cd_desc, CDROMSUBCHNL, &cdsc)) < 0)
 #endif
       {
+	  /* errno 5 = EIO, 9 = BAD_FD */
+	  g_message("CDROMSUBCHNL %d %d", ret, errno);
 	disc->disc_present = 0;
+#ifdef __OpenBSD__		/* fix for disc info never updating */
+	if (cd_desc >= 0){
+	    close(cd_desc);
+	    g_cd_desc = -1;
+	}
+#endif /* __OpenBSD__  fix for disc info never updating */
 
 	return 0;
       }
@@ -177,6 +212,12 @@ int CDStat(int cd_desc,struct disc_info 
      (cdsc.cdsc_audiostatus<0x11||cdsc.cdsc_audiostatus>0x15))
     {
       disc->disc_present = 0;
+#ifdef __OpenBSD__		/* fix for disc info never updating */
+      if (cd_desc >= 0){
+	  close(cd_desc);
+	  g_cd_desc = -1;
+      }
+#endif /* __OpenBSD__  fix for disc info never updating */
 
       return 0;
     }
@@ -240,7 +281,7 @@ int CDStat(int cd_desc,struct disc_info 
 
     /* Read the Table Of Contents header */
 
-#ifdef CDIOREADTOCHEADER
+#ifdef CDIOREADTOCHEADER	/* netbsd */
     if(ioctl(cd_desc, CDIOREADTOCHEADER, (char *)&cdth) < 0) {
       printf("Error: Failed to read disc contents\n");
 
@@ -261,7 +302,7 @@ int CDStat(int cd_desc,struct disc_info 
     
     /* Read the table of contents */
     
-#ifdef CDIOREADTOCENTRYS
+#ifdef CDIOREADTOCENTRYS	/* netbsd */
     cdte.address_format = CD_MSF_FORMAT;
     cdte.starting_track = 0;
     cdte.data = toc_buffer;
@@ -547,6 +588,9 @@ int CDEject(int cd_desc)
     perror("CDIOCEJECT");
     return -1;
   }
+#ifdef __OpenBSD__
+  close( cd_desc );
+#endif
 #endif
 #ifdef CDROMEJECT
   if(ioctl(cd_desc, CDROMEJECT) < 0)
