LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-14-2008, 09:37 AM   #1
ajparr
LQ Newbie
 
Registered: Feb 2008
Posts: 2

Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What command to type on frozen run scan disc manually page jerryjg Mandriva 5 01-02-2006 08:53 PM
Force unmount of cdrom tessx Linux - Newbie 6 08-19-2005 03:45 AM
Cannot access mandriva installation disc in your cdrom drive Jack_Quack Mandriva 2 08-13-2005 07:17 PM
is there a way to force your cdrom to eject no matter what??? mojarron Slackware 9 03-21-2005 08:05 PM
Trying to access a CDROM disc - drake 9.1 sillykids Linux - Software 2 07-16-2003 04:03 PM


All times are GMT -5. The time now is 02:21 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration