cd TOC access

I have a SCSI CD-Rom drive with ID=3.  I am trying to access the TOC on the drive and continually get and error of -21 (badUnitErr).  I have downloaded samples from Apple, but i cannot get those to work either.  
I am using the PBControl call (this returns with -21) but fill in the paramblock with what i beleive are the correct values.

If you could send me some code that reads off theTOC from the disk (using the mode 4 which gives the starting address of all tracks), it would be very helpful.

thanks
kevin
meyerklAsked:
Who is Participating?
 
Paul BConnect With a Mentor Commented:
Here is a short program I wrote to show how to do this.
Apples example is screwed up.
If this does not work please tell me whos driver software you are using. It could be a driver bug!

The code is heavily commented
Notes:
You MUST know the drive number of the CD!
The drive MUST support mode 4 Read Toc
The CD MUST be in the drive

Paul Baxter
pbaxter@assistivetech.com

#include <string.h>
#include <stdio.h>

#define kREADTOC            100                              // Read Toc command
#define kGETALL                  4                              // mode of read toc
#define kWHOISTHERE            97                              // test to see if CD drive

#define kBADRESULT            0xDEAD                        // error code
#define kBUFSIZE            512                              // buffer size to read TOC into


/* Prototypes */
void ReadTocALL(void);
OSErr ReadToc(short driveNum, Ptr buffer);
Boolean IsCDDrive(short driveNum);
short GetDriverRefNum(short driveNum);
void ReadTocALL(void);

char gBuffer[kBUFSIZE];

void main(void)
{
      ReadTocALL();
}

// Read TOC for ALL CD Drives
void ReadTocALL(void)
{
       QHdrPtr                  drvQHdr;
      DrvQEl                  *CurrDrvPtr;
      short                  driveNum, count;
      OSErr                  err;

      count = 0;                                                // number of CD drives found
       drvQHdr = GetDrvQHdr();                              // get the drive que head
      CurrDrvPtr = (DrvQEl *)drvQHdr->qHead;      // set current entry
      while (CurrDrvPtr) {                              // loop until out of range
            driveNum = CurrDrvPtr->dQDrive;            // get drive number
                                                                  // Is this a CD drive
            if (IsCDDrive(driveNum)) {
                  count++;                                    // count number of CD drives
                                                                  // Read TOC for this drive
                  err = ReadToc(driveNum, gBuffer);
                  if (!err) {
                        // Success!!!!!!!!!!!!!!!!!!
                        // TOC data is in gBuffer
                  }
                  printf("drive %d Read TOC  result %d\n", driveNum, err);
            }

            CurrDrvPtr = (DrvQEl *)CurrDrvPtr->qLink;
      }
      if (count == 0)      printf("NO CD drive found!!\nMake sure CD is in drive\n");
}


// Read TOC mode 4 into a buffer
// buffer MUST be pre allocated
// driveNum == drive number of the CD (not SCSI ID)
OSErr ReadToc(short driveNum, Ptr buffer)
{
       CntrlParam ctlPB;                                    // param block
       OSErr      err;                                          // result
       
      memset(&ctlPB, 0, sizeof(CntrlParam));      // Clear param block
                                                                  // get the driver
      ctlPB.ioCRefNum = GetDriverRefNum(driveNum);
                                                                  // check for error
      if (ctlPB.ioCRefNum == kBADRESULT)      return badUnitErr;
      ctlPB.csCode = kREADTOC;                        // set Read TOC Command
       ctlPB.ioVRefNum = driveNum;                        // Drive number (NOT volume number)
       ctlPB.csParam[0] = kGETALL;                        // set mode
                                                                   // set buffer
      ctlPB.csParam[1] = HiWord((long)buffer);
      ctlPB.csParam[2] = LoWord((long)buffer);
                                                                  // call the driver
      err = PBControlSync((ParmBlkPtr) &ctlPB);
      if (err == noErr) err = ctlPB.ioResult;      // check ioResult field
      return err;                                                // return the result
}

// get the driver refnum given a drive number
short GetDriverRefNum(short driveNum)
{
       QHdrPtr                  drvQHdr;
      DrvQEl                  *CurrDrvPtr;

       drvQHdr = GetDrvQHdr();                              // get the drive que head
      CurrDrvPtr = (DrvQEl *)drvQHdr->qHead;      // set current entry
      while (CurrDrvPtr) {                              // loop until out of range
                                                                  // is this the right drive?
            if (CurrDrvPtr->dQDrive == driveNum) {      
                  return(CurrDrvPtr->dQRefNum);      // set return value
            }
            else {                                                // goto next element
                  CurrDrvPtr = (DrvQEl *)CurrDrvPtr->qLink;
            }
      }
      return kBADRESULT;                                    // return
}

// return true if the drive is a CD drive
Boolean IsCDDrive(short driveNum)
{
      short refnum;
      
       CntrlParam ctlPB;                                    // param block
       OSErr      err;                                          // result
       
      memset(&ctlPB, 0, sizeof(CntrlParam));      // Clear param block
                                                                  // get the driver
      ctlPB.ioCRefNum = GetDriverRefNum(driveNum);
                                                                  // check for error
      if (ctlPB.ioCRefNum == kBADRESULT)      return false;
      
      ctlPB.csCode = kWHOISTHERE;                        // id the a CD?
      err = PBStatusSync((ParmBlkPtr) &ctlPB);// call the drivr
      if (err == noErr) err = ctlPB.ioResult;      // check ioResult field
      return err == noErr;
}

0
 
meyerklAuthor Commented:
just a quick comment:
  I also want to be able to read the Q-subcode on different tracks.  I know that there is a different value you place in csCode but i was wondering if i could just use your functions to place the call with the different value.  ie: I know that Apple has some samples that do not work, just wondering how general your code is...

thanks alot!!!!!
0
 
Paul BCommented:
To ReadQ it is pretty much the same once you know the drive number.
ReadQ will return the data in csParams 0..8 so you should copy the data to somewhere else

This function will copy the data to the buffer passed

#define kREADQ                        101
#define kREADQSIZE 9

OSErr ReadQ(short driveNum, Ptr buffer)
{
       CntrlParam ctlPB;                                    // param block
       OSErr      err;                                                // result
       
      memset(&ctlPB, 0, sizeof(CntrlParam));      // Clear param block
                                                                  // get the driver
      ctlPB.ioCRefNum = GetDriverRefNum(driveNum);
                                                                  // check for error
      if (ctlPB.ioCRefNum == kBADRESULT)      return badUnitErr;
      ctlPB.csCode = kREADQ;                              // set Read TOC Command
       ctlPB.ioVRefNum = driveNum;                        // Drive number (NOT volume number)
       err = PBControlSync((ParmBlkPtr) &ctlPB);
      if (err == noErr) err = ctlPB.ioResult;      // check ioResult field
 if (err == noErr) {
          BlockMovedata(&ctlPB.csParam[0], buffer, kREADQSIZE);
 }
      return err;                                                // return the result
}
0
 
Paul BCommented:
ooops...BlockMovedata should be BlockMoveData :/


0
 
meyerklAuthor Commented:
Paul,
      I noticed on your Customer Infomation page here that you have written some software for CD-ROM mastering.  I am trying to write something like that but for my own use.  I was wondering if you have an e-mail address I could ask you questions and stuff.

thanks.
meyerkl@crux.rose-hulman.edu
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.