~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/ntdll/cdrom.c

Version: ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~ [ wine-1.0-rc5 ] ~ [ wine-1.0-rc4 ] ~ [ wine-1.0-rc3 ] ~ [ wine-1.0-rc2 ] ~ [ wine-1.0-rc1 ] ~ [ wine-0.9.61 ] ~ [ wine-0.9.60 ] ~ [ wine-0.9.59 ] ~ [ wine-0.9.58 ] ~ [ wine-0.9.57 ] ~ [ wine-0.9.56 ] ~ [ wine-0.9.55 ] ~ [ wine-0.9.54 ] ~ [ wine-0.9.53 ] ~ [ wine-0.9.52 ] ~ [ wine-0.9.51 ] ~ [ wine-0.9.50 ] ~ [ wine-0.9.49 ] ~ [ wine-0.9.48 ] ~ [ wine-0.9.47 ] ~ [ wine-0.9.46 ] ~ [ wine-0.9.45 ] ~ [ wine-0.9.44 ] ~ [ wine-0.9.43 ] ~ [ wine-0.9.42 ] ~ [ wine-0.9.41 ] ~ [ wine-0.9.40 ] ~ [ wine-0.9.39 ] ~ [ wine-0.9.38 ] ~ [ wine-0.9.37 ] ~ [ wine-0.9.36 ] ~ [ wine-0.9.35 ] ~ [ wine-0.9.34 ] ~ [ wine-0.9.33 ] ~ [ wine-0.9.32 ] ~ [ wine-0.9.31 ] ~ [ wine-0.9.30 ] ~ [ wine-0.9.29 ] ~ [ wine-0.9.28 ] ~ [ wine-0.9.27 ] ~ [ wine-0.9.26 ] ~ [ wine-0.9.25 ] ~ [ wine-0.9.24 ] ~ [ wine-0.9.23 ] ~ [ wine-0.9.22 ] ~ [ wine-0.9.21 ] ~ [ wine-0.9.20 ] ~ [ wine-0.9.19 ] ~ [ wine-0.9.18 ] ~ [ wine-0.9.17 ] ~ [ wine-0.9.16 ] ~ [ wine-0.9.15 ] ~ [ wine-0.9.14 ] ~ [ wine-0.9.13 ] ~ [ wine-0.9.12 ] ~ [ wine-0.9.11 ] ~ [ wine-0.9.10 ] ~ [ wine-0.9.9 ] ~ [ wine-0.9.8 ] ~ [ wine-0.9.7 ] ~ [ wine-0.9.6 ] ~ [ wine-0.9.5 ] ~ [ wine-0.9.4 ] ~ [ wine-0.9.3 ] ~ [ wine-0.9.2 ] ~ [ wine-0.9.1 ] ~ [ wine-0.9 ] ~ [ wine20050930 ] ~ [ wine20050830 ] ~ [ wine20050725 ] ~ [ wine20050628 ] ~ [ wine20050524 ] ~ [ wine20050419 ] ~ [ wine20050310 ] ~ [ wine20050211 ] ~ [ wine20050111 ] ~ [ wine20041201 ] ~ [ wine20041019 ] ~ [ wine20040914 ] ~ [ wine20040813 ] ~ [ wine20040716 ] ~ [ wine20040615 ] ~ [ wine20040505 ] ~ [ wine20040408 ] ~ [ wine20040309 ] ~ [ wine20040213 ] ~ [ wine20040121 ] ~ [ wine20031212 ] ~ [ wine20031118 ] ~ [ wine20031016 ] ~ [ wine20030911 ] ~ [ wine20030813 ] ~ [ wine20030709 ] ~ [ wine20030618 ] ~ [ wine20030508 ] ~ [ wine20030408 ] ~ [ wine20030318 ] ~ [ wine20030219 ] ~ [ wine20030115 ] ~ [ wine20021219 ] ~ [ wine20021125 ] ~ [ wine20021031 ] ~ [ wine20021007 ] ~ [ wine20020904 ] ~ [ wine20020804 ] ~ [ wine20020710 ] ~ [ wine20020605 ] ~ [ wine20020509 ] ~ [ wine20020411 ] ~ [ wine20020310 ] ~ [ wine20020228 ] ~ [ wine20011226 ] ~ [ wine20011108 ] ~ [ wine20011004 ] ~ [ wine20010824 ] ~ [ wine20010731 ] ~ [ wine20010629 ] ~ [ wine20010510 ] ~ [ wine20010418 ] ~ [ wine20010326 ] ~ [ wine20010305 ] ~ [ wine20010216 ] ~ [ wine20010112 ] ~ [ wine20001222 ] ~ [ wine20001202 ] ~ [ wine20001026 ] ~ [ wine20001002 ] ~ [ wine20000909 ] ~ [ wine20000821 ] ~ [ wine20000801 ] ~ [ wine20000716 ] ~ [ wine20000326 ] ~ [ wine20000227 ] ~ [ wine20000130 ] ~ [ wine20000109 ] ~

  1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
  2 /* Main file for CD-ROM support
  3  *
  4  * Copyright 1994 Martin Ayotte
  5  * Copyright 1999, 2001, 2003 Eric Pouech
  6  * Copyright 2000 Andreas Mohr
  7  * Copyright 2005 Ivan Leo Puoti
  8  *
  9  * This library is free software; you can redistribute it and/or
 10  * modify it under the terms of the GNU Lesser General Public
 11  * License as published by the Free Software Foundation; either
 12  * version 2.1 of the License, or (at your option) any later version.
 13  *
 14  * This library is distributed in the hope that it will be useful,
 15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17  * Lesser General Public License for more details.
 18  *
 19  * You should have received a copy of the GNU Lesser General Public
 20  * License along with this library; if not, write to the Free Software
 21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 22  */
 23 
 24 #include "config.h"
 25 #include "wine/port.h"
 26 
 27 #include <errno.h>
 28 #include <string.h>
 29 #include <stdarg.h>
 30 #include <stdio.h>
 31 #ifdef HAVE_IO_H
 32 # include <io.h>
 33 #endif
 34 #ifdef HAVE_UNISTD_H
 35 # include <unistd.h>
 36 #endif
 37 #include <fcntl.h>
 38 #ifdef HAVE_SYS_STAT_H
 39 # include <sys/stat.h>
 40 #endif
 41 #include <sys/types.h>
 42 
 43 #ifdef HAVE_SYS_IOCTL_H
 44 #include <sys/ioctl.h>
 45 #endif
 46 #ifdef HAVE_SCSI_SG_H
 47 # include <scsi/sg.h>
 48 #endif
 49 #ifdef HAVE_SCSI_SCSI_H
 50 # include <scsi/scsi.h>
 51 # undef REASSIGN_BLOCKS  /* avoid conflict with winioctl.h */
 52 # undef FAILED           /* avoid conflict with winerror.h */
 53 #endif
 54 #ifdef HAVE_SCSI_SCSI_IOCTL_H
 55 # include <scsi/scsi_ioctl.h>
 56 #endif
 57 #ifdef HAVE_LINUX_MAJOR_H
 58 # include <linux/major.h>
 59 #endif
 60 #ifdef HAVE_LINUX_HDREG_H
 61 # include <linux/hdreg.h>
 62 #endif
 63 #ifdef HAVE_LINUX_PARAM_H
 64 # include <linux/param.h>
 65 #endif
 66 #ifdef HAVE_LINUX_CDROM_H
 67 # include <linux/cdrom.h>
 68 #endif
 69 #ifdef HAVE_LINUX_UCDROM_H
 70 # include <linux/ucdrom.h>
 71 #endif
 72 #ifdef HAVE_SYS_CDIO_H
 73 # include <sys/cdio.h>
 74 #endif
 75 #ifdef HAVE_SYS_SCSIIO_H
 76 # include <sys/scsiio.h>
 77 #endif
 78 
 79 #ifdef HAVE_IOKIT_IOKITLIB_H
 80 # include <sys/disk.h>
 81 # include <IOKit/IOKitLib.h>
 82 # include <IOKit/storage/IOMedia.h>
 83 # include <IOKit/storage/IOCDMediaBSDClient.h>
 84 # include <IOKit/scsi/SCSICmds_REQUEST_SENSE_Defs.h>
 85 # define SENSEBUFLEN kSenseDefaultSize
 86 #endif
 87 
 88 #define NONAMELESSUNION
 89 #define NONAMELESSSTRUCT
 90 #include "ntstatus.h"
 91 #define WIN32_NO_STATUS
 92 #include "windef.h"
 93 #include "winternl.h"
 94 #include "winioctl.h"
 95 #include "ntddstor.h"
 96 #include "ntddcdrm.h"
 97 #include "ddk/ntddcdvd.h"
 98 #include "ntddscsi.h"
 99 #include "ntdll_misc.h"
100 #include "wine/server.h"
101 #include "wine/library.h"
102 #include "wine/debug.h"
103 
104 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
105 
106 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
107    lack the following constants. */
108 
109 #ifndef CD_SECS
110 # define CD_SECS              60 /* seconds per minute */
111 #endif
112 #ifndef CD_FRAMES
113 # define CD_FRAMES            75 /* frames per second */
114 #endif
115 
116 static const struct iocodexs
117 {
118   DWORD code;
119   const char *codex;
120 } iocodextable[] = {
121 #define X(x) { x, #x },
122 X(IOCTL_CDROM_CHECK_VERIFY)
123 X(IOCTL_CDROM_CURRENT_POSITION)
124 X(IOCTL_CDROM_DISK_TYPE)
125 X(IOCTL_CDROM_GET_CONTROL)
126 X(IOCTL_CDROM_GET_DRIVE_GEOMETRY)
127 X(IOCTL_CDROM_GET_VOLUME)
128 X(IOCTL_CDROM_LOAD_MEDIA)
129 X(IOCTL_CDROM_MEDIA_CATALOG)
130 X(IOCTL_CDROM_MEDIA_REMOVAL)
131 X(IOCTL_CDROM_PAUSE_AUDIO)
132 X(IOCTL_CDROM_PLAY_AUDIO_MSF)
133 X(IOCTL_CDROM_RAW_READ)
134 X(IOCTL_CDROM_READ_Q_CHANNEL)
135 X(IOCTL_CDROM_READ_TOC)
136 X(IOCTL_CDROM_RESUME_AUDIO)
137 X(IOCTL_CDROM_SEEK_AUDIO_MSF)
138 X(IOCTL_CDROM_SET_VOLUME)
139 X(IOCTL_CDROM_STOP_AUDIO)
140 X(IOCTL_CDROM_TRACK_ISRC)
141 X(IOCTL_DISK_MEDIA_REMOVAL)
142 X(IOCTL_DVD_END_SESSION)
143 X(IOCTL_DVD_GET_REGION)
144 X(IOCTL_DVD_READ_KEY)
145 X(IOCTL_DVD_READ_STRUCTURE)
146 X(IOCTL_DVD_SEND_KEY)
147 X(IOCTL_DVD_START_SESSION)
148 X(IOCTL_SCSI_GET_ADDRESS)
149 X(IOCTL_SCSI_GET_CAPABILITIES)
150 X(IOCTL_SCSI_GET_INQUIRY_DATA)
151 X(IOCTL_SCSI_PASS_THROUGH)
152 X(IOCTL_SCSI_PASS_THROUGH_DIRECT)
153 X(IOCTL_STORAGE_CHECK_VERIFY)
154 X(IOCTL_STORAGE_EJECTION_CONTROL)
155 X(IOCTL_STORAGE_EJECT_MEDIA)
156 X(IOCTL_STORAGE_GET_DEVICE_NUMBER)
157 X(IOCTL_STORAGE_LOAD_MEDIA)
158 X(IOCTL_STORAGE_MEDIA_REMOVAL)
159 X(IOCTL_STORAGE_RESET_DEVICE)
160 #undef X
161 };
162 static const char *iocodex(DWORD code)
163 {
164    unsigned int i;
165    static char buffer[25];
166    for(i=0; i<sizeof(iocodextable)/sizeof(struct iocodexs); i++)
167       if (code==iocodextable[i].code)
168          return iocodextable[i].codex;
169    sprintf(buffer, "IOCTL_CODE_%x", (int)code);
170    return buffer;
171 }
172 
173 #define INQ_REPLY_LEN 36
174 #define INQ_CMD_LEN 6
175 
176 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
177 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
178 #define FRAME_OF_TOC(toc, idx)  FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
179 #define MSF_OF_FRAME(m,fr) {int f=(fr); ((UCHAR *)&(m))[2]=f%CD_FRAMES;f/=CD_FRAMES;((UCHAR *)&(m))[1]=f%CD_SECS;((UCHAR *)&(m))[0]=f/CD_SECS;}
180 
181 /* The documented format of DVD_LAYER_DESCRIPTOR is wrong. Even the format in the
182  * DDK's header is wrong. There are four bytes at the start which always seem to
183  * follow the sequence "02 08 00 00".
184  */
185 typedef struct
186 {
187     UCHAR MagicHeader[4];
188     UCHAR BookVersion : 4;
189     UCHAR BookType : 4;
190     UCHAR MinimumRate : 4;
191     UCHAR DiskSize : 4;
192     UCHAR LayerType : 4;
193     UCHAR TrackPath : 1;
194     UCHAR NumberOfLayers : 2;
195     UCHAR Reserved1 : 1;
196     UCHAR TrackDensity : 4;
197     UCHAR LinearDensity : 4;
198     ULONG StartingDataSector;
199     ULONG EndDataSector;
200     ULONG EndLayerZeroSector;
201     UCHAR Reserved5 : 7;
202     UCHAR BCAFlag : 1;
203     UCHAR Reserved6;
204 } internal_dvd_layer_descriptor;
205 
206 
207 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
208 static NTSTATUS CDROM_GetStatusCode(int);
209 
210 
211 #ifdef linux
212 
213 # ifndef IDE6_MAJOR
214 #  define IDE6_MAJOR 88
215 # endif
216 # ifndef IDE7_MAJOR
217 #  define IDE7_MAJOR 89
218 # endif
219 
220 # ifdef CDROM_SEND_PACKET
221 /* structure for CDROM_PACKET_COMMAND ioctl */
222 /* not all Linux versions have all the fields, so we define the
223  * structure ourselves to make sure */
224 struct linux_cdrom_generic_command
225 {
226     unsigned char          cmd[CDROM_PACKET_SIZE];
227     unsigned char         *buffer;
228     unsigned int           buflen;
229     int                    stat;
230     struct request_sense  *sense;
231     unsigned char          data_direction;
232     int                    quiet;
233     int                    timeout;
234     void                  *reserved[1];
235 };
236 # endif  /* CDROM_SEND_PACKET */
237 
238 #endif  /* linux */
239 
240 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
241  * this should be removed when a proper device interface is implemented
242  * 
243  * (WS) We need this to keep track of current position and to safely
244  * detect media changes. Besides this should provide a great speed up
245  * for toc inquiries.
246  */
247 struct cdrom_cache {
248     dev_t       device;
249     ino_t       inode;
250     char        toc_good; /* if false, will reread TOC from disk */
251     CDROM_TOC   toc;
252     SUB_Q_CURRENT_POSITION CurrentPosition;
253 };
254 /* who has more than 5 cdroms on his/her machine ?? */
255 /* FIXME: this should grow depending on the number of cdroms we install/configure 
256  * at startup
257  */
258 #define MAX_CACHE_ENTRIES       5
259 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
260 
261 static RTL_CRITICAL_SECTION cache_section;
262 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
263 {
264     0, 0, &cache_section,
265     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
266       0, 0, { (DWORD_PTR)(__FILE__ ": cache_section") }
267 };
268 static RTL_CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
269 
270 /* Proposed media change function: not really needed at this time */
271 /* This is a 1 or 0 type of function */
272 #if 0
273 static int CDROM_MediaChanged(int dev)
274 {
275    int i;
276 
277    struct cdrom_tochdr  hdr;
278    struct cdrom_tocentry entry;
279 
280    if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
281       return 0;
282    if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
283       return 0;
284 
285    if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
286       return 1;
287 
288    for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
289    {
290       if (i == hdr.cdth_trk1 + 1)
291       {
292          entry.cdte_track = CDROM_LEADOUT;
293       } else {
294          entry.cdte_track = i;
295       }
296       entry.cdte_format = CDROM_MSF;
297       if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
298          return 0;
299       if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
300                               sizeof(struct cdrom_tocentry)) )
301          return 1;
302    }
303    return 0;
304 }
305 #endif
306 
307 
308 /******************************************************************
309  *              get_parent_device
310  *
311  * On Mac OS, get the device for the whole disk from a fd that points to a partition.
312  * This is ugly and inefficient, but we have no choice since the partition fd doesn't
313  * support the eject ioctl.
314  */
315 #ifdef __APPLE__
316 static NTSTATUS get_parent_device( int fd, char *name, size_t len )
317 {
318     NTSTATUS status = STATUS_NO_SUCH_FILE;
319     struct stat st;
320     int i;
321     io_service_t service;
322     CFMutableDictionaryRef dict;
323     CFTypeRef val;
324 
325     if (fstat( fd, &st ) == -1) return FILE_GetNtStatus();
326     if (!S_ISCHR( st.st_mode )) return STATUS_OBJECT_TYPE_MISMATCH;
327 
328     /* create a dictionary with the right major/minor numbers */
329 
330     if (!(dict = IOServiceMatching( kIOMediaClass ))) return STATUS_NO_MEMORY;
331 
332     i = major( st.st_rdev );
333     val = CFNumberCreate( NULL, kCFNumberIntType, &i );
334     CFDictionaryAddValue( dict, CFSTR( "BSD Major" ), val );
335     CFRelease( val );
336 
337     i = minor( st.st_rdev );
338     val = CFNumberCreate( NULL, kCFNumberIntType, &i );
339     CFDictionaryAddValue( dict, CFSTR( "BSD Minor" ), val );
340     CFRelease( val );
341 
342     CFDictionaryAddValue( dict, CFSTR("Removable"), kCFBooleanTrue );
343 
344     service = IOServiceGetMatchingService( kIOMasterPortDefault, dict );
345 
346     /* now look for the parent that has the "Whole" attribute set to TRUE */
347 
348     while (service)
349     {
350         io_service_t parent = 0;
351         CFBooleanRef whole;
352         CFStringRef str;
353         int ok;
354 
355         if (!IOObjectConformsTo( service, kIOMediaClass ))
356             goto next;
357         if (!(whole = IORegistryEntryCreateCFProperty( service, CFSTR("Whole"), NULL, 0 )))
358             goto next;
359         ok = (whole == kCFBooleanTrue);
360         CFRelease( whole );
361         if (!ok) goto next;
362 
363         if ((str = IORegistryEntryCreateCFProperty( service, CFSTR("BSD Name"), NULL, 0 )))
364         {
365             strcpy( name, "/dev/r" );
366             CFStringGetCString( str, name + 6, len - 6, kCFStringEncodingUTF8 );
367             CFRelease( str );
368             status = STATUS_SUCCESS;
369         }
370         IOObjectRelease( service );
371         break;
372 
373 next:
374         IORegistryEntryGetParentEntry( service, kIOServicePlane, &parent );
375         IOObjectRelease( service );
376         service = parent;
377     }
378     return status;
379 }
380 #endif
381 
382 
383 /******************************************************************
384  *              CDROM_SyncCache                          [internal]
385  *
386  * Read the TOC in and store it in the cdrom_cache structure.
387  * Further requests for the TOC will be copied from the cache
388  * unless certain events like disk ejection is detected, in which
389  * case the cache will be cleared, causing it to be resynced.
390  * The cache section must be held by caller.
391  */
392 static NTSTATUS CDROM_SyncCache(int dev, int fd)
393 {
394 #ifdef linux
395    int i, tsz;
396    struct cdrom_tochdr          hdr;
397    struct cdrom_tocentry        entry;
398 
399    CDROM_TOC *toc = &cdrom_cache[dev].toc;
400    cdrom_cache[dev].toc_good = 0;
401 
402    if (ioctl(fd, CDROMREADTOCHDR, &hdr) == -1)
403    {
404       WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
405       return FILE_GetNtStatus();
406    }
407 
408    toc->FirstTrack = hdr.cdth_trk0;
409    toc->LastTrack  = hdr.cdth_trk1;
410    tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
411        + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
412    toc->Length[0] = tsz >> 8;
413    toc->Length[1] = tsz;
414 
415    TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
416 
417    for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
418    {
419      if (i == toc->LastTrack + 1)
420        entry.cdte_track = CDROM_LEADOUT;
421      else 
422        entry.cdte_track = i;
423      entry.cdte_format = CDROM_MSF;
424      if (ioctl(fd, CDROMREADTOCENTRY, &entry) == -1)
425      {
426        WARN("error read entry (%s)\n", strerror(errno));
427        return FILE_GetNtStatus();
428      }
429      toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
430      toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
431      /* marking last track with leadout value as index */
432      toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
433      toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
434      toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
435      toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
436      toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
437    }
438    cdrom_cache[dev].toc_good = 1;
439    return STATUS_SUCCESS;
440 
441 #elif defined(__FreeBSD__) || defined(__NetBSD__)
442 
443    int i, tsz;
444    struct ioc_toc_header hdr;
445    struct ioc_read_toc_entry entry;
446    struct cd_toc_entry toc_buffer;
447 
448    CDROM_TOC *toc = &cdrom_cache[dev].toc;
449    cdrom_cache[dev].toc_good = 0;
450 
451     if (ioctl(fd, CDIOREADTOCHEADER, &hdr) == -1)
452     {
453         WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
454         return FILE_GetNtStatus();
455     }
456     toc->FirstTrack = hdr.starting_track;
457     toc->LastTrack  = hdr.ending_track;
458     tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
459         + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
460     toc->Length[0] = tsz >> 8;
461     toc->Length[1] = tsz;
462 
463     TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
464 
465     for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
466     {
467         if (i == toc->LastTrack + 1)
468         {
469 #define LEADOUT 0xaa
470             entry.starting_track = LEADOUT;
471         } else {
472             entry.starting_track = i;
473         }
474         memset((char *)&toc_buffer, 0, sizeof(toc_buffer));
475         entry.address_format = CD_MSF_FORMAT;
476         entry.data_len = sizeof(toc_buffer);
477         entry.data = &toc_buffer;
478         if (ioctl(fd, CDIOREADTOCENTRYS, &entry) == -1)
479         {
480             WARN("error read entry (%s)\n", strerror(errno));
481             return FILE_GetNtStatus();
482         }
483         toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
484         toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
485         /* marking last track with leadout value as index */
486         toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
487         toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
488         toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
489         toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
490         toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
491     }
492     cdrom_cache[dev].toc_good = 1;
493     return STATUS_SUCCESS;
494 
495 #elif defined(__APPLE__)
496     int i;
497     dk_cd_read_toc_t hdr;
498     CDROM_TOC *toc = &cdrom_cache[dev].toc;
499     cdrom_cache[dev].toc_good = 0;
500 
501     memset( &hdr, 0, sizeof(hdr) );
502     hdr.buffer = toc;
503     hdr.bufferLength = sizeof(*toc);
504     if (ioctl(fd, DKIOCCDREADTOC, &hdr) == -1)
505     {
506         WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
507         return FILE_GetNtStatus();
508     }
509     for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
510     {
511         /* convert address format */
512         TRACK_DATA *data = &toc->TrackData[i - toc->FirstTrack];
513         DWORD frame = (((DWORD)data->Address[0] << 24) | ((DWORD)data->Address[1] << 16) |
514                        ((DWORD)data->Address[2] << 8) | data->Address[3]);
515         MSF_OF_FRAME( data->Address[1], frame );
516         data->Address[0] = 0;
517     }
518 
519     cdrom_cache[dev].toc_good = 1;
520     return STATUS_SUCCESS;
521 #else
522     return STATUS_NOT_SUPPORTED;
523 #endif
524 }
525 
526 static void CDROM_ClearCacheEntry(int dev)
527 {
528     RtlEnterCriticalSection( &cache_section );
529     cdrom_cache[dev].toc_good = 0;
530     RtlLeaveCriticalSection( &cache_section );
531 }
532 
533 
534 
535 /******************************************************************
536  *              CDROM_GetInterfaceInfo
537  *
538  * Determines the ide interface (the number after the ide), and the
539  * number of the device on that interface for ide cdroms (*iface <= 1).
540  * Determines the scsi information for scsi cdroms (*iface >= 2).
541  * Returns false if the info cannot not be obtained.
542  */
543 static int CDROM_GetInterfaceInfo(int fd, UCHAR* iface, UCHAR* port, UCHAR* device, UCHAR* lun)
544 {
545 #if defined(linux)
546     struct stat st;
547     if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode)) return 0;
548     *port = 0;
549     *iface = 0;
550     *device = 0;
551     *lun = 0;
552     switch (major(st.st_rdev)) {
553     case IDE0_MAJOR: *iface = 0; break;
554     case IDE1_MAJOR: *iface = 1; break;
555     case IDE2_MAJOR: *iface = 2; break;
556     case IDE3_MAJOR: *iface = 3; break;
557     case IDE4_MAJOR: *iface = 4; break;
558     case IDE5_MAJOR: *iface = 5; break;
559     case IDE6_MAJOR: *iface = 6; break;
560     case IDE7_MAJOR: *iface = 7; break;
561     default: *port = 1; break;
562     }
563 
564     if (*port == 0)
565         *device = (minor(st.st_rdev) >> 6);
566     else
567     {
568 #ifdef SCSI_IOCTL_GET_IDLUN
569         UINT32 idlun[2];
570         if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, idlun) != -1)
571         {
572             *port = (idlun[0] >> 24) & 0xff;
573             *iface = ((idlun[0] >> 16) & 0xff) + 2;
574             *device = idlun[0] & 0xff;
575             *lun = (idlun[0] >> 8) & 0xff;
576         }
577         else
578 #endif
579         {
580             WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
581             return 0;
582         }
583     }
584     return 1;
585 #elif defined(__NetBSD__)
586     struct scsi_addr addr;
587     if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
588     {
589         switch (addr.type) 
590         {
591         case TYPE_SCSI:  *port = 1;
592             *iface = addr.addr.scsi.scbus;
593             *device = addr.addr.scsi.target;
594             *lun = addr.addr.scsi.lun;
595             break;
596         case TYPE_ATAPI: *port = 0;
597             *iface = addr.addr.atapi.atbus;
598             *device = addr.addr.atapi.drive;
599             *lun = 0;
600             break;
601         }
602         return 1;
603     }
604     return 0;
605 #elif defined(__FreeBSD__)
606     FIXME("not implemented for BSD\n");
607     return 0;
608 #else
609     FIXME("not implemented for nonlinux\n");
610     return 0;
611 #endif
612 }
613 
614 
615 /******************************************************************
616  *              CDROM_Open
617  *
618  */
619 static NTSTATUS CDROM_Open(int fd, int* dev)
620 {
621     struct stat st;
622     NTSTATUS ret = STATUS_SUCCESS;
623     int         empty = -1;
624 
625     fstat(fd, &st);
626 
627     RtlEnterCriticalSection( &cache_section );
628     for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
629     {
630         if (empty == -1 &&
631             cdrom_cache[*dev].device == 0 &&
632             cdrom_cache[*dev].inode == 0)
633             empty = *dev;
634         else if (cdrom_cache[*dev].device == st.st_dev &&
635                  cdrom_cache[*dev].inode == st.st_ino)
636             break;
637     }
638     if (*dev == MAX_CACHE_ENTRIES)
639     {
640         if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
641         else
642         {
643             *dev = empty;
644             cdrom_cache[*dev].device  = st.st_dev;
645             cdrom_cache[*dev].inode   = st.st_ino;
646         }
647     }
648     RtlLeaveCriticalSection( &cache_section );
649 
650     TRACE("%d, %d\n", *dev, fd);
651     return ret;
652 }
653 
654 /******************************************************************
655  *              CDROM_GetStatusCode
656  *
657  *
658  */
659 static NTSTATUS CDROM_GetStatusCode(int io)
660 {
661     if (io == 0) return STATUS_SUCCESS;
662     return FILE_GetNtStatus();
663 }
664 
665 /******************************************************************
666  *              CDROM_GetControl
667  *
668  */
669 static NTSTATUS CDROM_GetControl(int dev, CDROM_AUDIO_CONTROL* cac)
670 {
671     cac->LbaFormat = 0; /* FIXME */
672     cac->LogicalBlocksPerSecond = 1; /* FIXME */
673     return  STATUS_NOT_SUPPORTED;
674 }
675 
676 /******************************************************************
677  *              CDROM_GetDeviceNumber
678  *
679  */
680 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
681 {
682     FIXME( "stub\n" );
683     devnum->DeviceType = FILE_DEVICE_DISK;
684     devnum->DeviceNumber = 1;
685     devnum->PartitionNumber = 1;
686     return STATUS_SUCCESS;
687 }
688 
689 /******************************************************************
690  *              CDROM_GetDriveGeometry
691  *
692  */
693 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
694 {
695   CDROM_TOC     toc;
696   NTSTATUS      ret = 0;
697   int           fsize = 0;
698 
699   if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
700 
701   fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
702         - FRAME_OF_TOC(toc, 1); /* Total size in frames */
703   
704   dg->Cylinders.u.LowPart = fsize / (64 * 32); 
705   dg->Cylinders.u.HighPart = 0; 
706   dg->MediaType = RemovableMedia;  
707   dg->TracksPerCylinder = 64; 
708   dg->SectorsPerTrack = 32;  
709   dg->BytesPerSector= 2048; 
710   return ret;
711 }
712 
713 /**************************************************************************
714  *                              CDROM_Reset                     [internal]
715  */
716 static NTSTATUS CDROM_ResetAudio(int fd)
717 {
718 #if defined(linux)
719     return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
720 #elif defined(__FreeBSD__) || defined(__NetBSD__)
721     return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
722 #else
723     return STATUS_NOT_SUPPORTED;
724 #endif
725 }
726 
727 /******************************************************************
728  *              CDROM_SetTray
729  *
730  *
731  */
732 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
733 {
734 #if defined(linux)
735     return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
736 #elif defined(__FreeBSD__) || defined(__NetBSD__)
737     return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
738                                (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
739                                (ioctl(fd, CDIOCPREVENT, NULL)));
740 #elif defined(__APPLE__)
741     if (doEject) return CDROM_GetStatusCode( ioctl( fd, DKIOCEJECT, NULL ) );
742     else return STATUS_NOT_SUPPORTED;
743 #else
744     return STATUS_NOT_SUPPORTED;
745 #endif
746 }
747 
748 /******************************************************************
749  *              CDROM_ControlEjection
750  *
751  *
752  */
753 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
754 {
755 #if defined(linux)
756     return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
757 #elif defined(__FreeBSD__) || defined(__NetBSD__)
758     return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
759 #else
760     return STATUS_NOT_SUPPORTED;
761 #endif
762 }
763 
764 /******************************************************************
765  *              CDROM_ReadTOC
766  *
767  *
768  */
769 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
770 {
771     NTSTATUS       ret = STATUS_NOT_SUPPORTED;
772 
773     if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
774         return STATUS_INVALID_PARAMETER;
775 
776     RtlEnterCriticalSection( &cache_section );
777     if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
778     {
779         *toc = cdrom_cache[dev].toc;
780         ret = STATUS_SUCCESS;
781     }
782     RtlLeaveCriticalSection( &cache_section );
783     return ret;
784 }
785 
786 /******************************************************************
787  *              CDROM_GetDiskData
788  *
789  *
790  */
791 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
792 {
793     CDROM_TOC   toc;
794     NTSTATUS    ret;
795     int         i;
796 
797     if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
798     data->DiskData = 0;
799     for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
800         if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
801             data->DiskData |= CDROM_DISK_DATA_TRACK;
802         else
803             data->DiskData |= CDROM_DISK_AUDIO_TRACK;
804     }
805     return STATUS_SUCCESS;
806 }
807 
808 /******************************************************************
809  *              CDROM_ReadQChannel
810  *
811  *
812  */
813 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
814                                    SUB_Q_CHANNEL_DATA* data)
815 {
816     NTSTATUS            ret = STATUS_NOT_SUPPORTED;
817 #ifdef linux
818     SUB_Q_HEADER*       hdr = (SUB_Q_HEADER*)data;
819     int                 io;
820     struct cdrom_subchnl        sc;
821     sc.cdsc_format = CDROM_MSF;
822 
823     io = ioctl(fd, CDROMSUBCHNL, &sc);
824     if (io == -1)
825     {
826         TRACE("opened or no_media (%s)!\n", strerror(errno));
827         hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
828         CDROM_ClearCacheEntry(dev);
829         goto end;
830     }
831 
832     hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
833 
834     switch (sc.cdsc_audiostatus) {
835     case CDROM_AUDIO_INVALID:
836         CDROM_ClearCacheEntry(dev);
837         hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
838         break;
839     case CDROM_AUDIO_NO_STATUS:
840         CDROM_ClearCacheEntry(dev);
841         hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
842         break;
843     case CDROM_AUDIO_PLAY:
844         hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
845         break;
846     case CDROM_AUDIO_PAUSED:
847         hdr->AudioStatus = AUDIO_STATUS_PAUSED;
848         break;
849     case CDROM_AUDIO_COMPLETED:
850         hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
851         break;
852     case CDROM_AUDIO_ERROR:
853         hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
854         break;
855     default:
856         TRACE("status=%02X !\n", sc.cdsc_audiostatus);
857         break;
858     }
859     switch (fmt->Format)
860     {
861     case IOCTL_CDROM_CURRENT_POSITION:
862         RtlEnterCriticalSection( &cache_section );
863         if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
864           data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
865           data->CurrentPosition.Control = sc.cdsc_ctrl; 
866           data->CurrentPosition.ADR = sc.cdsc_adr; 
867           data->CurrentPosition.TrackNumber = sc.cdsc_trk; 
868           data->CurrentPosition.IndexNumber = sc.cdsc_ind; 
869 
870           data->CurrentPosition.AbsoluteAddress[0] = 0; 
871           data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute; 
872           data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
873           data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
874  
875           data->CurrentPosition.TrackRelativeAddress[0] = 0; 
876           data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute; 
877           data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
878           data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
879 
880           cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
881         }
882         else /* not playing */
883         {
884           cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
885           data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
886         }
887         RtlLeaveCriticalSection( &cache_section );
888         break;
889     case IOCTL_CDROM_MEDIA_CATALOG:
890         data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
891         {
892             struct cdrom_mcn mcn;
893             if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
894 
895             data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
896             data->MediaCatalog.Mcval = 0; /* FIXME */
897             memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
898             data->MediaCatalog.MediaCatalog[14] = 0;
899         }
900         break;
901     case IOCTL_CDROM_TRACK_ISRC:
902         FIXME("TrackIsrc: NIY on linux\n");
903         data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
904         data->TrackIsrc.Tcval = 0;
905         io = 0;
906         break;
907     }
908 
909  end:
910     ret = CDROM_GetStatusCode(io);
911 #elif defined(__FreeBSD__) || defined(__NetBSD__)
912     SUB_Q_HEADER*       hdr = (SUB_Q_HEADER*)data;
913     int                 io;
914     struct ioc_read_subchannel  read_sc;
915     struct cd_sub_channel_info  sc;
916 
917     read_sc.address_format = CD_MSF_FORMAT;
918     read_sc.track          = 0;
919     read_sc.data_len       = sizeof(sc);
920     read_sc.data           = &sc;
921     switch (fmt->Format)
922     {
923     case IOCTL_CDROM_CURRENT_POSITION:
924         read_sc.data_format    = CD_CURRENT_POSITION;
925         break;
926     case IOCTL_CDROM_MEDIA_CATALOG:
927         read_sc.data_format    = CD_MEDIA_CATALOG;
928         break;
929     case IOCTL_CDROM_TRACK_ISRC:
930         read_sc.data_format    = CD_TRACK_INFO;
931         sc.what.track_info.track_number = data->TrackIsrc.Track;
932         break;
933     }
934     io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
935     if (io == -1)
936     {
937         TRACE("opened or no_media (%s)!\n", strerror(errno));
938         CDROM_ClearCacheEntry(dev);
939         hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
940         goto end;
941     }
942 
943     hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
944 
945     switch (sc.header.audio_status) {
946     case CD_AS_AUDIO_INVALID:
947         CDROM_ClearCacheEntry(dev);
948         hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
949         break;
950     case CD_AS_NO_STATUS:
951         CDROM_ClearCacheEntry(dev);
952         hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
953         break;
954     case CD_AS_PLAY_IN_PROGRESS:
955         hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
956         break;
957     case CD_AS_PLAY_PAUSED:
958         hdr->AudioStatus = AUDIO_STATUS_PAUSED;
959         break;
960     case CD_AS_PLAY_COMPLETED:
961         hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
962         break;
963     case CD_AS_PLAY_ERROR:
964         hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
965         break;
966     default:
967         TRACE("status=%02X !\n", sc.header.audio_status);
968     }
969     switch (fmt->Format)
970     {
971     case IOCTL_CDROM_CURRENT_POSITION:
972         RtlEnterCriticalSection( &cache_section );
973         if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
974           data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
975           data->CurrentPosition.Control = sc.what.position.control;
976           data->CurrentPosition.ADR = sc.what.position.addr_type;
977           data->CurrentPosition.TrackNumber = sc.what.position.track_number;
978           data->CurrentPosition.IndexNumber = sc.what.position.index_number;
979 
980           data->CurrentPosition.AbsoluteAddress[0] = 0;
981           data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
982           data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
983           data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
984           data->CurrentPosition.TrackRelativeAddress[0] = 0;
985           data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
986           data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
987           data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
988           cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
989         }
990         else { /* not playing */
991           cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
992           data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
993         }
994         RtlLeaveCriticalSection( &cache_section );
995         break;
996     case IOCTL_CDROM_MEDIA_CATALOG:
997         data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
998         data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
999         memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
1000         break;
1001     case IOCTL_CDROM_TRACK_ISRC:
1002         data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
1003         data->TrackIsrc.Tcval = sc.what.track_