Solved

problem using ioctl , CDROM_DRIVE_STATUS

Posted on 2006-07-11
4
1,305 Views
Last Modified: 2012-05-05
Hi,
I'm using the ubuntu version of Linux and C++.  I'm trying to use ioctl to detect the status of the cdrom drive.  according to the documentation http://www.mjmwired.net/kernel/Documentation/ioctl/cdrom.txt (line 682) CDROM_DRIVE_STATUS should return one of 5 states:

CDS_NO_INFO      
CDS_NO_DISC
CDS_TRAY_OPEN
CDS_DRIVE_NOT_READY
CDS_DISC_OK

I have only been able to detect two states: CDS_DISC_OK and CDS_TRAY_OPEN.  I'm getting CDS_TRAY_OPEN when there is no disc in the drive, even when the tray is closed.  I need to know if the tray is closed but there is no disc in the drive.  Is there something I'm doing wrong (my code is below)?


Thanks,
Nick



#include <sys/ioctl.h>
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/cdrom.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>


using std::cout;
using std::endl;





int main(char** argv)
{

    int fd;
    int ret,d;


    while(1)
    {
    if ((fd = open("/dev/cdrom",O_RDONLY | O_NONBLOCK)) < 0) {
        perror("open");
        exit(1);
    }
   
   
      ret = ioctl(fd,CDROM_DRIVE_STATUS,0);

      cout<<ret<<endl;

      switch(ret) {
      case CDS_NO_INFO:
          cout<<"CDS_NO_INFO"<<endl;
          break;
      case CDS_NO_DISC:
          cout<<"CDS_NO_DISC"<<endl;
          break;
      case CDS_TRAY_OPEN:
          cout<<"CDS_TRAY_OPEN"<<endl;
          d = ioctl(fd,CDROM_DISC_STATUS,0);
          cout<<d<<endl;
          break;
      case CDS_DRIVE_NOT_READY:
          cout<<"CDS_DRIVE_NOT_READY"<<endl;
          break;
      case CDS_DISC_OK:
          cout<<"CDS_DISC_OK"<<endl;
          d = ioctl(fd,CDROM_DISC_STATUS,0);
          cout<<d<<endl;
          switch(d){
          case CDS_NO_INFO:
            cout<<"CDS_NO_INFO"<<endl;
            break;
          case CDS_AUDIO:
            cout<<"CDS_AUDIO"<<endl;
            break;
          case CDS_XA_2_2:
            cout<<"CDS_XA_2_2"<<endl;
            break;
          case CDS_XA_2_1:
            cout<<"CDS_XA_2_1"<<endl;
            break;
          case CDS_DATA_1:
            cout<<"CDS_DATA_1"<<endl;
            break;
          case CDS_MIXED:
            cout<<"CDS_MIXED"<<endl;
            break;
          default:
            cout<<"SUPER ERROR 2"<<endl;
          }
          break;
      case -1:
          cout<<"ERROR"<<endl;
          cout<<errno<<endl;
          switch(errno) {
          case ENOSYS:
            cout<<"ENOSYS"<<endl;
            break;
          case EINVAL:
            cout<<"EINVAL"<<endl;
            break;
          case ENOMEM:
            cout<<"ENOMEM"<<endl;
            break;
          default:
            cout<<"ERRNO ERROR"<<endl;
          }
          break;
      default:
          cout<<"SUPER ERROR"<<endl;
      }
      usleep(100);
    }
    return 0;

}


0
Comment
Question by:spikey_nick
  • 3
4 Comments
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17087215
That's weird.
what happens when you give this ioctl CDROMCLOSETRAY before getting drive status.

have you tried it with vanilla kernel (latest one)? Maybe the driver in that kernel is broken.

and what does uname -a give

regards
Manish Regmi
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17087245
Also see what information kernel gives during the execution of your program.
Do "dmesg | tail"

regards
Manish
0
 

Author Comment

by:spikey_nick
ID: 17095075
Ok, well it turns out that this problem only occurs on my machine and non of my coworkers machines.  Since the program I'm writing only needs to work on specific hardware setup, I'm not going to worry about this issue because it's not worth my time.

I was wondering though, does anybody know when a CDS_DRIVE_NOT_READY state occurs
0
 
LVL 8

Accepted Solution

by:
manish_regmi earned 500 total points
ID: 17096239
>> does anybody know when a CDS_DRIVE_NOT_READY state occurs

It means the drive is busy and cannot accept any commands given to it. It might be busy working on previous command like reading data etc.

this cdrom howto might be helpful to you.
http://www.faqs.org/docs/Linux-HOWTO/CDROM-HOWTO.html

If you like to know the CDROM detals, see the spec. for atapi it is www.t13.org.


regards
Manish Regmi
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now