LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 06-15-2005, 01:05 PM   #1
Chrax
Member
 
Registered: Apr 2004
Distribution: Dapper
Posts: 167

Rep: Reputation: 31
CDROM_GET_CAPABILITIES ioctl


I have need to determine the capabilities of an optical drive. Right now I'm using SDL to find the drives, and ioctl to get the capabilities. Unfortunately, based on the output that I'm getting, either my method of interpretation is bad or these drives are misrepresenting themselves (which they shouldn't be, as other programs can use the abilites they would here appear to be lacking). If anyone has experience with the CDROM_GET_CAPABILITIES ioctl, could you please tell me where I am going amiss?

I'm using Linux 2.6.11.10 with gcc 3.4.3.

Thanks,
Chris

Code:
#include <stdio.h>
#include <SDL.h>
#include <linux/cdrom.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>

int main(){
  /* Initialize CDROM subsystem */
  int init = SDL_Init(SDL_INIT_CDROM);

  /* Get number of drives */
  int num = SDL_CDNumDrives();
  printf("Initialized: %d\nNumber of Drives: %d\n",init,num);

  int i;
  for(i=0;i < num; ++i){
    /* System-dependent drive name (i.e. /dev/cdrom or E:)*/
    const char *name = SDL_CDName(i);
    printf("Name: %s\nID: %d\n",name,i);

    /* Open device */
    int drive = open(name,0);
    if(drive < 0){
            printf("Cannot open drive: %s.\n",name);
            return 1;
    }

    /* Pointer to hold output */
    int *argp;

    /* Get drive capabilities, store in argp */
    int retvalue = ioctl(drive,CDROM_GET_CAPABILITY,argp);

    /* First attempt at argp interpretation */
    if(*argp & CDC_CD_R)
            printf("Can write CD-Rs\n");
    if(*argp & CDC_CD_RW)
            printf("Can write CD-RWs\n");
    if(*argp & CDC_DVD)
            printf("Can read DVDs\n");
    if(*argp & CDC_DVD_R)
            printf("Can write DVD-Rs\n");
    if(*argp & CDC_DVD_RAM)
            printf("Can write DVD-RAMs\n");

    /* Most likely gibberish output, but I still want to see it. */
    printf("Return: %d\nArgp: %Ld\n",retvalue,argp);
    close(drive);
  }
  return 0; 
}
 
Old 06-16-2005, 09:59 AM   #2
Chrax
Member
 
Registered: Apr 2004
Distribution: Dapper
Posts: 167

Original Poster
Rep: Reputation: 31
Alright, I figured out my problem. that particular ioctl puts its information in the return value, and the pointer (third parameter) is set to point to gibberish.

That said, I have a little request for all of you that run some form of Linux (*BSD is not yet supported):
First, if you don't have the basic SDL libraries, get them. If you've never run h2ph, run it like so:
Code:
# cd /usr/include; find . -name '*.h' -print | xargs h2ph
Next, compile cd.c with
Code:
cc `sdl-config --cflags` `sdl-config --libs` cd.c
and run the resulting a.out.

Then run perl readdevs.pl.

Obviously you shouldn't run these if you don't trust me, so here's a description of what they do: The C program utilizes the SDL library to find the number of drives and their names, and then finds the capabilities of each with an ioctl call. The Perl uses one ioctl to find all of the information it needs. Only optical drives respond >0 to that call, and that return value contains all of the capabilities of the drive. Pretty much the only difference is the C program will print how many drives it found, and it will try to find names like /dev/cdrom, while the Perl program prefers names like /dev/dvd.

Please comment back regarding whether or not the output properly described your drive. For the record, it should show every capability of the following that applies to your drive: Writes CD-Rs; Writes CD-RWs; Reads DVDs; Writes DVD-Rs; Writes DVD-RAMs;

There is no capability flag for DVD-RW in the cdrom.h file. Yet.

Thanks.
 
  


Reply



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
HIDDEV and ioctl td873 Linux - Hardware 0 04-14-2005 01:22 PM
Serial / IOCTL fgordon Programming 2 08-24-2004 08:51 AM
ioctl.h blackzone Programming 1 07-26-2004 03:30 AM
ioctl dummyagain Programming 2 10-06-2003 07:22 AM
ioctl unosoft Linux - Software 0 09-12-2003 06:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration