Back to index

lshw  02.16
cdrom.cc
Go to the documentation of this file.
00001 /*
00002  * cdrom.cc
00003  *
00004  * This scan reports optical media-specific capabilities:
00005  * - audio CD playback
00006  * - CD-R burning
00007  * - CD-RW burning
00008  * - DVD reading
00009  * - DVD-R burning
00010  * - DVD-RAM burning
00011  *
00012  * NOTE: DVD+R/RW are not covered by the ioctls used in this code and, therefore,
00013  * not reported.
00014  *
00015  *
00016  */
00017 
00018 #include "version.h"
00019 #include "cdrom.h"
00020 #include "partitions.h"
00021 #include <sys/types.h>
00022 #include <sys/stat.h>
00023 #include <sys/ioctl.h>
00024 #include <fcntl.h>
00025 #include <unistd.h>
00026 #include <limits.h>
00027 #include <linux/cdrom.h>
00028 
00029 __ID("@(#) $Id: cdrom.cc 2433 2012-01-10 22:01:30Z lyonel $");
00030 
00031 #ifndef CDC_CD_R
00032 #define CDC_CD_R 0x2000
00033 #endif
00034 #ifndef CDC_CD_RW
00035 #define CDC_CD_RW 0x4000
00036 #endif
00037 #ifndef CDC_DVD
00038 #define CDC_DVD 0x8000
00039 #endif
00040 #ifndef CDC_DVD_R
00041 #define CDC_DVD_R 0x10000
00042 #endif
00043 #ifndef CDC_DVD_RAM
00044 #define CDC_DVD_RAM 0x20000
00045 #endif
00046 
00047 bool scan_cdrom(hwNode & n)
00048 {
00049   if (n.getLogicalName() == "")
00050     return false;
00051 
00052   n.addHint("icon", string("cd"));
00053 
00054   int fd = open(n.getLogicalName().c_str(), O_RDONLY | O_NONBLOCK);
00055 
00056   if (fd < 0)
00057     return false;
00058 
00059   int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
00060   if (status < 0)
00061   {
00062     close(fd);
00063     return false;
00064   }
00065 
00066   int capabilities = ioctl(fd, CDROM_GET_CAPABILITY);
00067 
00068   if (capabilities < 0)
00069   {
00070     close(fd);
00071     return false;
00072   }
00073 
00074   if (capabilities & CDC_PLAY_AUDIO)
00075     n.addCapability("audio", "Audio CD playback");
00076   if (capabilities & CDC_CD_R)
00077   {
00078     n.addCapability("cd-r", "CD-R burning");
00079     n.setDescription("CD-R writer");
00080   }
00081   if (capabilities & CDC_CD_RW)
00082   {
00083     n.addCapability("cd-rw", "CD-RW burning");
00084     n.setDescription("CD-R/CD-RW writer");
00085   }
00086   if (capabilities & CDC_DVD)
00087   {
00088     n.addCapability("dvd", "DVD playback");
00089     n.setDescription("DVD reader");
00090   }
00091   if (capabilities & CDC_DVD_R)
00092   {
00093     n.addCapability("dvd-r", "DVD-R burning");
00094     n.setDescription("DVD writer");
00095   }
00096   if (capabilities & CDC_DVD_RAM)
00097   {
00098     n.addCapability("dvd-ram", "DVD-RAM burning");
00099     n.setDescription("DVD-RAM writer");
00100   }
00101 
00102   switch(ioctl(fd, CDROM_DRIVE_STATUS, 0))
00103   {
00104     case CDS_NO_INFO:
00105     case CDS_NO_DISC:
00106       n.setConfig("status", "nodisc");
00107       break;
00108     case CDS_TRAY_OPEN:
00109       n.setConfig("status", "open");
00110       break;
00111     case CDS_DRIVE_NOT_READY:
00112       n.setConfig("status", "busy");
00113       break;
00114     case CDS_DISC_OK:
00115       n.setConfig("status", "ready");
00116       scan_partitions(n);
00117       break;
00118   }
00119   close(fd);
00120 
00121   return true;
00122 }