LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to force a CDROM drive to re-scan a disc (https://www.linuxquestions.org/questions/programming-9/how-to-force-a-cdrom-drive-to-re-scan-a-disc-621095/)

ajparr 02-14-2008 09:37 AM

How to force a CDROM drive to re-scan a disc
 
I wrote a small C program that uses ioctls to determine the drive status and disc status of the cd-rom drive on my Debian Etch laptop. The problem is that after every reboot or power cycle, I have to eject and re-insert the disc. If I don't do this, the drive still seems to see the disc, but doesn't have information about the data scheme of the disc and can't do anything with it until re-insertion.

Is there any way to force the cd drive to re-scan the disc without ejecting the cd tray?

Program source:
Code:

#define CDDEVICE "/dev/cdrom" // CDROM device
int cd_drive_status(int cd_drive_fd);
int cd_drive_disc_status(int cd_drive_fd);
int cd_drive_reset(int cd_drive_fd);


int main(int argc,char **argv)
{
int cd_drive_fd=-999; // CDROM device file descriptor

// open the cd drive
if ((cd_drive_fd = open(CDDEVICE,O_RDONLY | O_NONBLOCK)) < 0) {
perror("open");
exit(1);
}

cd_drive_reset(cd_drive_fd); printf("\n");
cd_drive_status(cd_drive_fd); printf("\n");
cd_drive_disc_status(cd_drive_fd); printf("\n");

close(cd_drive_fd);
return 0;
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
int cd_drive_reset(int cd_drive_fd){
int retVal=-999;
retVal=ioctl(cd_drive_fd, CDROMRESET, NULL);
printf("Resetting CDROM: %d - %s\n", retVal, strerror(retVal));
return retVal;
}


int cd_drive_status(int cd_drive_fd){
int retVal;
retVal=ioctl(cd_drive_fd, CDROM_DRIVE_STATUS, 0);
printf("CDROM Drive Status: %d - ", retVal);
switch(retVal){
#define PRINT_AND_BREAK( a ) printf( a ); break // Gotta love the MACRO's Smile
case CDS_NO_INFO: PRINT_AND_BREAK("CDS_NO_INFO\n");
case CDS_NO_DISC: PRINT_AND_BREAK("CDS_NO_DISC\n");
case CDS_TRAY_OPEN: PRINT_AND_BREAK("CDS_TRAY_OPEN\n");
case CDS_DRIVE_NOT_READY: PRINT_AND_BREAK("CDS_DRIVE_NOT_READY\n");
case CDS_DISC_OK: PRINT_AND_BREAK("CDS_DISC_OK\n");
case -1: PRINT_AND_BREAK("ioctl function error\n");
// case ENOSYS: PRINT_AND_BREAK("Drive can't detect drive status\n");
// case EINVAL: PRINT_AND_BREAK("Slot number beyond capacity of drive\n");
// case ENOMEM: PRINT_AND_BREAK("Insufficient memory\n");
}
return retVal;
}


int cd_drive_disc_status(int cd_drive_fd){
int retVal=-999;

retVal=ioctl(cd_drive_fd, CDROM_DISC_STATUS, 0);
printf("CDROM Drive Disc Status: %d - ", retVal);
switch(retVal){
#define CASE_PRINT( value , string )\
case value:\
printf("%s\n", string);\
break;
CASE_PRINT(CDS_NO_INFO, "CDS_NO_INFO");
CASE_PRINT(CDS_NO_DISC, "CDS_NO_DISC");
CASE_PRINT(CDS_TRAY_OPEN, "CDS_TRAY_OPEN");
CASE_PRINT(CDS_DRIVE_NOT_READY, "CDS_DRIVE_NOT_READY");
CASE_PRINT(CDS_DISC_OK, "CDS_DISC_OK");
CASE_PRINT(CDS_AUDIO, "CDS_AUDIO");
CASE_PRINT(CDS_DATA_1, "CDS_DATA_1");
CASE_PRINT(CDS_DATA_2, "CDS_DATA_2");
CASE_PRINT(CDS_XA_2_1, "CDS_XA_2_1");
CASE_PRINT(CDS_XA_2_2, "CDS_XA_2_2");
CASE_PRINT(CDS_MIXED, "CDS_MIXED");
default:
printf("unknown\n");
break;
}
return retVal;
}


Program output after reset:
Code:

Resetting CDROM: 0 - Success

CDROM Drive Status: 4 - CDS_DISC_OK

CDROM Drive Disc Status: 0 - CDS_NO_INFO

(dmesg also contains the following line:
"cdrom: This disc doesn't have any tracks I recognize!")

Program output after re-inserting disc:
Code:

Resetting CDROM: 0 - Success

CDROM Drive Status: 4 - CDS_DISC_OK

CDROM Drive Disc Status: 101 - CDS_DATA_1





Any help is greatly appreciated.

-Aaron


All times are GMT -5. The time now is 12:48 PM.