1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /* Main file for CD-ROM support
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1999, 2001, 2003 Eric Pouech
6 * Copyright 2000 Andreas Mohr
7 * Copyright 2005 Ivan Leo Puoti
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.
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.
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
25 #include "wine/port.h"
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
41 #include <sys/types.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 #include <sys/ioctl.h>
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 */
54 #ifdef HAVE_SCSI_SCSI_IOCTL_H
55 # include <scsi/scsi_ioctl.h>
57 #ifdef HAVE_LINUX_MAJOR_H
58 # include <linux/major.h>
60 #ifdef HAVE_LINUX_HDREG_H
61 # include <linux/hdreg.h>
63 #ifdef HAVE_LINUX_PARAM_H
64 # include <linux/param.h>
66 #ifdef HAVE_LINUX_CDROM_H
67 # include <linux/cdrom.h>
69 #ifdef HAVE_LINUX_UCDROM_H
70 # include <linux/ucdrom.h>
72 #ifdef HAVE_SYS_CDIO_H
73 # include <sys/cdio.h>
75 #ifdef HAVE_SYS_SCSIIO_H
76 # include <sys/scsiio.h>
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
88 #define NONAMELESSUNION
89 #define NONAMELESSSTRUCT
91 #define WIN32_NO_STATUS
97 #include "ddk/ntddcdvd.h"
99 #include "ntdll_misc.h"
100 #include "wine/server.h"
101 #include "wine/library.h"
102 #include "wine/debug.h"
104 WINE_DEFAULT_DEBUG_CHANNEL(cdrom
);
106 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
107 lack the following constants. */
110 # define CD_SECS 60 /* seconds per minute */
113 # define CD_FRAMES 75 /* frames per second */
116 static const struct iocodexs
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
)
162 static const char *iocodex(DWORD code
)
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
);
173 #define INQ_REPLY_LEN 36
174 #define INQ_CMD_LEN 6
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;}
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".
187 UCHAR MagicHeader
[4];
188 UCHAR BookVersion
: 4;
190 UCHAR MinimumRate
: 4;
194 UCHAR NumberOfLayers
: 2;
196 UCHAR TrackDensity
: 4;
197 UCHAR LinearDensity
: 4;
198 ULONG StartingDataSector
;
200 ULONG EndLayerZeroSector
;
204 } internal_dvd_layer_descriptor
;
207 static NTSTATUS
CDROM_ReadTOC(int, int, CDROM_TOC
*);
208 static NTSTATUS
CDROM_GetStatusCode(int);
214 # define IDE6_MAJOR 88
217 # define IDE7_MAJOR 89
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
226 unsigned char cmd
[CDROM_PACKET_SIZE
];
227 unsigned char *buffer
;
230 struct request_sense
*sense
;
231 unsigned char data_direction
;
236 # endif /* CDROM_SEND_PACKET */
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
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
250 char toc_good
; /* if false, will reread TOC from disk */
252 SUB_Q_CURRENT_POSITION CurrentPosition
;
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
258 #define MAX_CACHE_ENTRIES 5
259 static struct cdrom_cache cdrom_cache
[MAX_CACHE_ENTRIES
];
261 static RTL_CRITICAL_SECTION cache_section
;
262 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
264 0, 0, &cache_section
,
265 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
266 0, 0, { (DWORD_PTR
)(__FILE__
": cache_section") }
268 static RTL_CRITICAL_SECTION cache_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
270 /* Proposed media change function: not really needed at this time */
271 /* This is a 1 or 0 type of function */
273 static int CDROM_MediaChanged(int dev
)
277 struct cdrom_tochdr hdr
;
278 struct cdrom_tocentry entry
;
280 if (dev
< 0 || dev
>= MAX_CACHE_ENTRIES
)
282 if ( ioctl(cdrom_cache
[dev
].fd
, CDROMREADTOCHDR
, &hdr
) == -1 )
285 if ( memcmp(&hdr
, &cdrom_cache
[dev
].hdr
, sizeof(struct cdrom_tochdr
)) )
288 for (i
=hdr
.cdth_trk0
; i
<=hdr
.cdth_trk1
+1; i
++)
290 if (i
== hdr
.cdth_trk1
+ 1)
292 entry
.cdte_track
= CDROM_LEADOUT
;
294 entry
.cdte_track
= i
;
296 entry
.cdte_format
= CDROM_MSF
;
297 if ( ioctl(cdrom_cache
[dev
].fd
, CDROMREADTOCENTRY
, &entry
) == -1)
299 if ( memcmp(&entry
, cdrom_cache
[dev
].entry
+i
-hdr
.cdth_trk0
,
300 sizeof(struct cdrom_tocentry
)) )
308 /******************************************************************
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.
316 static NTSTATUS
get_parent_device( int fd
, char *name
, size_t len
)
318 NTSTATUS status
= STATUS_NO_SUCH_FILE
;
321 io_service_t service
;
322 CFMutableDictionaryRef dict
;
325 if (fstat( fd
, &st
) == -1) return FILE_GetNtStatus();
326 if (!S_ISCHR( st
.st_mode
)) return STATUS_OBJECT_TYPE_MISMATCH
;
328 /* create a dictionary with the right major/minor numbers */
330 if (!(dict
= IOServiceMatching( kIOMediaClass
))) return STATUS_NO_MEMORY
;
332 i
= major( st
.st_rdev
);
333 val
= CFNumberCreate( NULL
, kCFNumberIntType
, &i
);
334 CFDictionaryAddValue( dict
, CFSTR( "BSD Major" ), val
);
337 i
= minor( st
.st_rdev
);
338 val
= CFNumberCreate( NULL
, kCFNumberIntType
, &i
);
339 CFDictionaryAddValue( dict
, CFSTR( "BSD Minor" ), val
);
342 CFDictionaryAddValue( dict
, CFSTR("Removable"), kCFBooleanTrue
);
344 service
= IOServiceGetMatchingService( kIOMasterPortDefault
, dict
);
346 /* now look for the parent that has the "Whole" attribute set to TRUE */
350 io_service_t parent
= 0;
355 if (!IOObjectConformsTo( service
, kIOMediaClass
))
357 if (!(whole
= IORegistryEntryCreateCFProperty( service
, CFSTR("Whole"), NULL
, 0 )))
359 ok
= (whole
== kCFBooleanTrue
);
363 if ((str
= IORegistryEntryCreateCFProperty( service
, CFSTR("BSD Name"), NULL
, 0 )))
365 strcpy( name
, "/dev/r" );
366 CFStringGetCString( str
, name
+ 6, len
- 6, kCFStringEncodingUTF8
);
368 status
= STATUS_SUCCESS
;
370 IOObjectRelease( service
);
374 IORegistryEntryGetParentEntry( service
, kIOServicePlane
, &parent
);
375 IOObjectRelease( service
);
383 /******************************************************************
384 * CDROM_SyncCache [internal]
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.
392 static NTSTATUS
CDROM_SyncCache(int dev
, int fd
)
396 struct cdrom_tochdr hdr
;
397 struct cdrom_tocentry entry
;
399 CDROM_TOC
*toc
= &cdrom_cache
[dev
].toc
;
400 cdrom_cache
[dev
].toc_good
= 0;
402 if (ioctl(fd
, CDROMREADTOCHDR
, &hdr
) == -1)
404 WARN("(%d) -- Error occurred (%s)!\n", dev
, strerror(errno
));
405 return FILE_GetNtStatus();
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
;
415 TRACE("caching toc from=%d to=%d\n", toc
->FirstTrack
, toc
->LastTrack
);
417 for (i
= toc
->FirstTrack
; i
<= toc
->LastTrack
+ 1; i
++)
419 if (i
== toc
->LastTrack
+ 1)
420 entry
.cdte_track
= CDROM_LEADOUT
;
422 entry
.cdte_track
= i
;
423 entry
.cdte_format
= CDROM_MSF
;
424 if (ioctl(fd
, CDROMREADTOCENTRY
, &entry
) == -1)
426 WARN("error read entry (%s)\n", strerror(errno
));
427 return FILE_GetNtStatus();
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
;
438 cdrom_cache
[dev
].toc_good
= 1;
439 return STATUS_SUCCESS
;
441 #elif defined(__FreeBSD__) || defined(__NetBSD__)
444 struct ioc_toc_header hdr
;
445 struct ioc_read_toc_entry entry
;
446 struct cd_toc_entry toc_buffer
;
448 CDROM_TOC
*toc
= &cdrom_cache
[dev
].toc
;
449 cdrom_cache
[dev
].toc_good
= 0;
451 if (ioctl(fd
, CDIOREADTOCHEADER
, &hdr
) == -1)
453 WARN("(%d) -- Error occurred (%s)!\n", dev
, strerror(errno
));
454 return FILE_GetNtStatus();
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
;
463 TRACE("caching toc from=%d to=%d\n", toc
->FirstTrack
, toc
->LastTrack
);
465 for (i
= toc
->FirstTrack
; i
<= toc
->LastTrack
+ 1; i
++)
467 if (i
== toc
->LastTrack
+ 1)
470 entry
.starting_track
= LEADOUT
;
472 entry
.starting_track
= i
;
474 memset(&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)
480 WARN("error read entry (%s)\n", strerror(errno
));
481 return FILE_GetNtStatus();
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
;
492 cdrom_cache
[dev
].toc_good
= 1;
493 return STATUS_SUCCESS
;
495 #elif defined(__APPLE__)
497 dk_cd_read_toc_t hdr
;
498 CDROM_TOC
*toc
= &cdrom_cache
[dev
].toc
;
499 cdrom_cache
[dev
].toc_good
= 0;
501 memset( &hdr
, 0, sizeof(hdr
) );
503 hdr
.bufferLength
= sizeof(*toc
);
504 if (ioctl(fd
, DKIOCCDREADTOC
, &hdr
) == -1)
506 WARN("(%d) -- Error occurred (%s)!\n", dev
, strerror(errno
));
507 return FILE_GetNtStatus();
509 for (i
= toc
->FirstTrack
; i
<= toc
->LastTrack
+ 1; i
++)
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;
519 cdrom_cache
[dev
].toc_good
= 1;
520 return STATUS_SUCCESS
;
522 return STATUS_NOT_SUPPORTED
;
526 static void CDROM_ClearCacheEntry(int dev
)
528 RtlEnterCriticalSection( &cache_section
);
529 cdrom_cache
[dev
].toc_good
= 0;
530 RtlLeaveCriticalSection( &cache_section
);
535 /******************************************************************
536 * CDROM_GetInterfaceInfo
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.
543 static int CDROM_GetInterfaceInfo(int fd
, UCHAR
* iface
, UCHAR
* port
, UCHAR
* device
, UCHAR
* lun
)
547 if ( fstat(fd
, &st
) == -1 || ! S_ISBLK(st
.st_mode
)) return 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;
565 *device
= (minor(st
.st_rdev
) >> 6);
568 #ifdef SCSI_IOCTL_GET_IDLUN
570 if (ioctl(fd
, SCSI_IOCTL_GET_IDLUN
, idlun
) != -1)
572 *port
= (idlun
[0] >> 24) & 0xff;
573 *iface
= ((idlun
[0] >> 16) & 0xff) + 2;
574 *device
= idlun
[0] & 0xff;
575 *lun
= (idlun
[0] >> 8) & 0xff;
580 WARN("CD-ROM device (%d, %d) not supported\n", major(st
.st_rdev
), minor(st
.st_rdev
));
585 #elif defined(__NetBSD__)
586 struct scsi_addr addr
;
587 if (ioctl(fd
, SCIOCIDENTIFY
, &addr
) != -1)
591 case TYPE_SCSI
: *port
= 1;
592 *iface
= addr
.addr
.scsi
.scbus
;
593 *device
= addr
.addr
.scsi
.target
;
594 *lun
= addr
.addr
.scsi
.lun
;
596 case TYPE_ATAPI
: *port
= 0;
597 *iface
= addr
.addr
.atapi
.atbus
;
598 *device
= addr
.addr
.atapi
.drive
;
605 #elif defined(__FreeBSD__)
606 FIXME("not implemented for BSD\n");
609 FIXME("not implemented for nonlinux\n");
615 /******************************************************************
619 static NTSTATUS
CDROM_Open(int fd
, int* dev
)
622 NTSTATUS ret
= STATUS_SUCCESS
;
627 RtlEnterCriticalSection( &cache_section
);
628 for (*dev
= 0; *dev
< MAX_CACHE_ENTRIES
; (*dev
)++)
631 cdrom_cache
[*dev
].device
== 0 &&
632 cdrom_cache
[*dev
].inode
== 0)
634 else if (cdrom_cache
[*dev
].device
== st
.st_dev
&&
635 cdrom_cache
[*dev
].inode
== st
.st_ino
)
638 if (*dev
== MAX_CACHE_ENTRIES
)
640 if (empty
== -1) ret
= STATUS_NOT_IMPLEMENTED
;
644 cdrom_cache
[*dev
].device
= st
.st_dev
;
645 cdrom_cache
[*dev
].inode
= st
.st_ino
;
648 RtlLeaveCriticalSection( &cache_section
);
650 TRACE("%d, %d\n", *dev
, fd
);
654 /******************************************************************
655 * CDROM_GetStatusCode
659 static NTSTATUS
CDROM_GetStatusCode(int io
)
661 if (io
== 0) return STATUS_SUCCESS
;
662 return FILE_GetNtStatus();
665 /******************************************************************
669 static NTSTATUS
CDROM_GetControl(int dev
, CDROM_AUDIO_CONTROL
* cac
)
671 cac
->LbaFormat
= 0; /* FIXME */
672 cac
->LogicalBlocksPerSecond
= 1; /* FIXME */
673 return STATUS_NOT_SUPPORTED
;
676 /******************************************************************
677 * CDROM_GetDeviceNumber
680 static NTSTATUS
CDROM_GetDeviceNumber(int dev
, STORAGE_DEVICE_NUMBER
* devnum
)
683 devnum
->DeviceType
= FILE_DEVICE_DISK
;
684 devnum
->DeviceNumber
= 1;
685 devnum
->PartitionNumber
= 1;
686 return STATUS_SUCCESS
;
689 /******************************************************************
690 * CDROM_GetDriveGeometry
693 static NTSTATUS
CDROM_GetDriveGeometry(int dev
, int fd
, DISK_GEOMETRY
* dg
)
699 if ((ret
= CDROM_ReadTOC(dev
, fd
, &toc
)) != 0) return ret
;
701 fsize
= FRAME_OF_TOC(toc
, toc
.LastTrack
+1)
702 - FRAME_OF_TOC(toc
, 1); /* Total size in frames */
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;
713 /**************************************************************************
714 * CDROM_Reset [internal]
716 static NTSTATUS
CDROM_ResetAudio(int fd
)
719 return CDROM_GetStatusCode(ioctl(fd
, CDROMRESET
));
720 #elif defined(__FreeBSD__) || defined(__NetBSD__)
721 return CDROM_GetStatusCode(ioctl(fd
, CDIOCRESET
, NULL
));
723 return STATUS_NOT_SUPPORTED
;
727 /******************************************************************
732 static NTSTATUS
CDROM_SetTray(int fd
, BOOL doEject
)
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
;
744 return STATUS_NOT_SUPPORTED
;
748 /******************************************************************
749 * CDROM_ControlEjection
753 static NTSTATUS
CDROM_ControlEjection(int fd
, const PREVENT_MEDIA_REMOVAL
* rmv
)
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
));
760 return STATUS_NOT_SUPPORTED
;
764 /******************************************************************
769 static NTSTATUS
CDROM_ReadTOC(int dev
, int fd
, CDROM_TOC
* toc
)
771 NTSTATUS ret
= STATUS_NOT_SUPPORTED
;
773 if (dev
< 0 || dev
>= MAX_CACHE_ENTRIES
)
774 return STATUS_INVALID_PARAMETER
;
776 RtlEnterCriticalSection( &cache_section
);
777 if (cdrom_cache
[dev
].toc_good
|| !(ret
= CDROM_SyncCache(dev
, fd
)))
779 *toc
= cdrom_cache
[dev
].toc
;
780 ret
= STATUS_SUCCESS
;
782 RtlLeaveCriticalSection( &cache_section
);
786 /******************************************************************
791 static NTSTATUS
CDROM_GetDiskData(int dev
, int fd
, CDROM_DISK_DATA
* data
)
797 if ((ret
= CDROM_ReadTOC(dev
, fd
, &toc
)) != 0) return ret
;
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
;
803 data
->DiskData
|= CDROM_DISK_AUDIO_TRACK
;
805 return STATUS_SUCCESS
;
808 /******************************************************************
813 static NTSTATUS
CDROM_ReadQChannel(int dev
, int fd
, const CDROM_SUB_Q_DATA_FORMAT
* fmt
,
814 SUB_Q_CHANNEL_DATA
* data
)
816 NTSTATUS ret
= STATUS_NOT_SUPPORTED
;
818 SUB_Q_HEADER
* hdr
= (SUB_Q_HEADER
*)data
;
820 struct cdrom_subchnl sc
;
821 sc
.cdsc_format
= CDROM_MSF
;
823 io
= ioctl(fd
, CDROMSUBCHNL
, &sc
);
826 TRACE("opened or no_media (%s)!\n", strerror(errno
));
827 hdr
->AudioStatus
= AUDIO_STATUS_NO_STATUS
;
828 CDROM_ClearCacheEntry(dev
);
832 hdr
->AudioStatus
= AUDIO_STATUS_NOT_SUPPORTED
;
834 switch (sc
.cdsc_audiostatus
) {
835 case CDROM_AUDIO_INVALID
:
836 CDROM_ClearCacheEntry(dev
);
837 hdr
->AudioStatus
= AUDIO_STATUS_NOT_SUPPORTED
;
839 case CDROM_AUDIO_NO_STATUS
:
840 CDROM_ClearCacheEntry(dev
);
841 hdr
->AudioStatus
= AUDIO_STATUS_NO_STATUS
;
843 case CDROM_AUDIO_PLAY
:
844 hdr
->AudioStatus
= AUDIO_STATUS_IN_PROGRESS
;
846 case CDROM_AUDIO_PAUSED
:
847 hdr
->AudioStatus
= AUDIO_STATUS_PAUSED
;
849 case CDROM_AUDIO_COMPLETED
:
850 hdr
->AudioStatus
= AUDIO_STATUS_PLAY_COMPLETE
;
852 case CDROM_AUDIO_ERROR
:
853 hdr
->AudioStatus
= AUDIO_STATUS_PLAY_ERROR
;
856 TRACE("status=%02X !\n", sc
.cdsc_audiostatus
);
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
;
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
;
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
;
880 cdrom_cache
[dev
].CurrentPosition
= data
->CurrentPosition
;
882 else /* not playing */
884 cdrom_cache
[dev
].CurrentPosition
.Header
= *hdr
; /* Preserve header info */
885 data
->CurrentPosition
= cdrom_cache
[dev
].CurrentPosition
;
887 RtlLeaveCriticalSection( &cache_section
);
889 case IOCTL_CDROM_MEDIA_CATALOG
:
890 data
->MediaCatalog
.FormatCode
= IOCTL_CDROM_MEDIA_CATALOG
;
892 struct cdrom_mcn mcn
;
893 if ((io
= ioctl(fd
, CDROM_GET_MCN
, &mcn
)) == -1) goto end
;
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;
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;
910 ret
= CDROM_GetStatusCode(io
);
911 #elif defined(__FreeBSD__) || defined(__NetBSD__)
912 SUB_Q_HEADER
* hdr
= (SUB_Q_HEADER
*)data
;
914 struct ioc_read_subchannel read_sc
;
915 struct cd_sub_channel_info sc
;
917 read_sc
.address_format
= CD_MSF_FORMAT
;
919 read_sc
.data_len
= sizeof(sc
);
923 case IOCTL_CDROM_CURRENT_POSITION
:
924 read_sc
.data_format
= CD_CURRENT_POSITION
;
926 case IOCTL_CDROM_MEDIA_CATALOG
:
927 read_sc
.data_format
= CD_MEDIA_CATALOG
;
929 case IOCTL_CDROM_TRACK_ISRC
:
930 read_sc
.data_format
= CD_TRACK_INFO
;
931 sc
.what
.track_info
.track_number
= data
->TrackIsrc
.Track
;
934 io
= ioctl(fd
, CDIOCREADSUBCHANNEL
, &read_sc
);
937 TRACE("opened or no_media (%s)!\n", strerror(errno
));
938 CDROM_ClearCacheEntry(dev
);
939 hdr
->AudioStatus
= AUDIO_STATUS_NO_STATUS
;
943 hdr
->AudioStatus
= AUDIO_STATUS_NOT_SUPPORTED
;
945 switch (sc
.header
.audio_status
) {
946 case CD_AS_AUDIO_INVALID
:
947 CDROM_ClearCacheEntry(dev
);
948 hdr
->AudioStatus
= AUDIO_STATUS_NOT_SUPPORTED
;
950 case CD_AS_NO_STATUS
:
951 CDROM_ClearCacheEntry(dev
);
952 hdr
->AudioStatus
= AUDIO_STATUS_NO_STATUS
;
954 case CD_AS_PLAY_IN_PROGRESS
:
955 hdr
->AudioStatus
= AUDIO_STATUS_IN_PROGRESS
;
957 case CD_AS_PLAY_PAUSED
:
958 hdr
->AudioStatus
= AUDIO_STATUS_PAUSED
;
960 case CD_AS_PLAY_COMPLETED
:
961 hdr
->AudioStatus
= AUDIO_STATUS_PLAY_COMPLETE
;
963 case CD_AS_PLAY_ERROR
:
964 hdr
->AudioStatus
= AUDIO_STATUS_PLAY_ERROR
;
967 TRACE("status=%02X !\n", sc
.header
.audio_status
);
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
;
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
;
990 else { /* not playing */
991 cdrom_cache
[dev
].CurrentPosition
.Header
= *hdr
; /* Preserve header info */
992 data
->CurrentPosition
= cdrom_cache
[dev
].CurrentPosition
;
994 RtlLeaveCriticalSection( &cache_section
);
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);
1001 case IOCTL_CDROM_TRACK_ISRC
:
1002 data
->TrackIsrc
.FormatCode
= IOCTL_CDROM_TRACK_ISRC
;
1003 data
->TrackIsrc
.Tcval
= sc
.what
.track_info
.ti_valid
;
1004 memcpy(data
->TrackIsrc
.TrackIsrc
, sc
.what
.track_info
.ti_number
, 15);
1009 ret
= CDROM_GetStatusCode(io
);
1014 /******************************************************************
1016 * Implements: IOCTL_STORAGE_CHECK_VERIFY
1017 * IOCTL_CDROM_CHECK_VERIFY
1018 * IOCTL_DISK_CHECK_VERIFY
1021 static NTSTATUS
CDROM_Verify(int dev
, int fd
)
1026 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, NULL
);
1028 TRACE("ioctl CDROM_DRIVE_STATUS failed(%s)!\n", strerror(errno
));
1029 return CDROM_GetStatusCode(ret
);
1032 if(ret
== CDS_DISC_OK
)
1033 return STATUS_SUCCESS
;
1035 return STATUS_NO_MEDIA_IN_DEVICE
;
1036 #elif defined(__FreeBSD__)
1038 ret
= ioctl(fd
, CDIOCSTART
, NULL
);
1040 return STATUS_SUCCESS
;
1042 return STATUS_NO_MEDIA_IN_DEVICE
;
1044 FIXME("not implemented for non-linux\n");
1045 return STATUS_NOT_SUPPORTED
;
1049 /******************************************************************
1050 * CDROM_PlayAudioMSF
1054 static NTSTATUS
CDROM_PlayAudioMSF(int fd
, const CDROM_PLAY_AUDIO_MSF
* audio_msf
)
1056 NTSTATUS ret
= STATUS_NOT_SUPPORTED
;
1058 struct cdrom_msf msf
;
1061 msf
.cdmsf_min0
= audio_msf
->StartingM
;
1062 msf
.cdmsf_sec0
= audio_msf
->StartingS
;
1063 msf
.cdmsf_frame0
= audio_msf
->StartingF
;
1064 msf
.cdmsf_min1
= audio_msf
->EndingM
;
1065 msf
.cdmsf_sec1
= audio_msf
->EndingS
;
1066 msf
.cdmsf_frame1
= audio_msf
->EndingF
;
1068 io
= ioctl(fd
, CDROMSTART
);
1071 WARN("motor doesn't start !\n");
1074 io
= ioctl(fd
, CDROMPLAYMSF
, &msf
);
1077 WARN("device doesn't play !\n");
1080 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1081 msf
.cdmsf_min0
, msf
.cdmsf_sec0
, msf
.cdmsf_frame0
,
1082 msf
.cdmsf_min1
, msf
.cdmsf_sec1
, msf
.cdmsf_frame1
);
1084 ret
= CDROM_GetStatusCode(io
);
1085 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1086 struct ioc_play_msf msf
;
1089 msf
.start_m
= audio_msf
->StartingM
;
1090 msf
.start_s
= audio_msf
->StartingS
;
1091 msf
.start_f
= audio_msf
->StartingF
;
1092 msf
.end_m
= audio_msf
->EndingM
;
1093 msf
.end_s
= audio_msf
->EndingS
;
1094 msf
.end_f
= audio_msf
->EndingF
;
1096 io
= ioctl(fd
, CDIOCSTART
, NULL
);
1099 WARN("motor doesn't start !\n");
1102 io
= ioctl(fd
, CDIOCPLAYMSF
, &msf
);
1105 WARN("device doesn't play !\n");
1108 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1109 msf
.start_m
, msf
.start_s
, msf
.start_f
,
1110 msf
.end_m
, msf
.end_s
, msf
.end_f
);
1112 ret
= CDROM_GetStatusCode(io
);
1117 /******************************************************************
1118 * CDROM_SeekAudioMSF
1122 static NTSTATUS
CDROM_SeekAudioMSF(int dev
, int fd
, const CDROM_SEEK_AUDIO_MSF
* audio_msf
)
1126 SUB_Q_CURRENT_POSITION
*cp
;
1128 struct cdrom_msf0 msf
;
1129 struct cdrom_subchnl sc
;
1130 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1131 struct ioc_play_msf msf
;
1132 struct ioc_read_subchannel read_sc
;
1133 struct cd_sub_channel_info sc
;
1137 /* Use the information on the TOC to compute the new current
1138 * position, which is shadowed on the cache. [Portable]. */
1139 frame
= FRAME_OF_MSF(*audio_msf
);
1141 if ((io
= CDROM_ReadTOC(dev
, fd
, &toc
)) != 0) return io
;
1143 for(i
=toc
.FirstTrack
;i
<=toc
.LastTrack
+1;i
++)
1144 if (FRAME_OF_TOC(toc
,i
)>frame
) break;
1145 if (i
<= toc
.FirstTrack
|| i
> toc
.LastTrack
+1)
1146 return STATUS_INVALID_PARAMETER
;
1148 RtlEnterCriticalSection( &cache_section
);
1149 cp
= &cdrom_cache
[dev
].CurrentPosition
;
1150 cp
->FormatCode
= IOCTL_CDROM_CURRENT_POSITION
;
1151 cp
->Control
= toc
.TrackData
[i
-toc
.FirstTrack
].Control
;
1152 cp
->ADR
= toc
.TrackData
[i
-toc
.FirstTrack
].Adr
;
1153 cp
->TrackNumber
= toc
.TrackData
[i
-toc
.FirstTrack
].TrackNumber
;
1154 cp
->IndexNumber
= 0; /* FIXME: where do they keep these? */
1155 cp
->AbsoluteAddress
[0] = 0;
1156 cp
->AbsoluteAddress
[1] = toc
.TrackData
[i
-toc
.FirstTrack
].Address
[1];
1157 cp
->AbsoluteAddress
[2] = toc
.TrackData
[i
-toc
.FirstTrack
].Address
[2];
1158 cp
->AbsoluteAddress
[3] = toc
.TrackData
[i
-toc
.FirstTrack
].Address
[3];
1159 frame
-= FRAME_OF_TOC(toc
,i
);
1160 cp
->TrackRelativeAddress
[0] = 0;
1161 MSF_OF_FRAME(cp
->TrackRelativeAddress
[1], frame
);
1162 RtlLeaveCriticalSection( &cache_section
);
1164 /* If playing, then issue a seek command, otherwise do nothing */
1166 sc
.cdsc_format
= CDROM_MSF
;
1168 io
= ioctl(fd
, CDROMSUBCHNL
, &sc
);
1171 TRACE("opened or no_media (%s)!\n", strerror(errno
));
1172 CDROM_ClearCacheEntry(dev
);
1173 return CDROM_GetStatusCode(io
);
1175 if (sc
.cdsc_audiostatus
==CDROM_AUDIO_PLAY
)
1177 msf
.minute
= audio_msf
->M
;
1178 msf
.second
= audio_msf
->S
;
1179 msf
.frame
= audio_msf
->F
;
1180 return CDROM_GetStatusCode(ioctl(fd
, CDROMSEEK
, &msf
));
1182 return STATUS_SUCCESS
;
1183 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1184 read_sc
.address_format
= CD_MSF_FORMAT
;
1186 read_sc
.data_len
= sizeof(sc
);
1188 read_sc
.data_format
= CD_CURRENT_POSITION
;
1190 io
= ioctl(fd
, CDIOCREADSUBCHANNEL
, &read_sc
);
1193 TRACE("opened or no_media (%s)!\n", strerror(errno
));
1194 CDROM_ClearCacheEntry(dev
);
1195 return CDROM_GetStatusCode(io
);
1197 if (sc
.header
.audio_status
==CD_AS_PLAY_IN_PROGRESS
)
1200 msf
.start_m
= audio_msf
->M
;
1201 msf
.start_s
= audio_msf
->S
;
1202 msf
.start_f
= audio_msf
->F
;
1203 final_frame
= FRAME_OF_TOC(toc
,toc
.LastTrack
+1)-1;
1204 MSF_OF_FRAME(msf
.end_m
, final_frame
);
1206 return CDROM_GetStatusCode(ioctl(fd
, CDIOCPLAYMSF
, &msf
));
1208 return STATUS_SUCCESS
;
1210 return STATUS_NOT_SUPPORTED
;
1214 /******************************************************************
1219 static NTSTATUS
CDROM_PauseAudio(int fd
)
1222 return CDROM_GetStatusCode(ioctl(fd
, CDROMPAUSE
));
1223 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1224 return CDROM_GetStatusCode(ioctl(fd
, CDIOCPAUSE
, NULL
));
1226 return STATUS_NOT_SUPPORTED
;
1230 /******************************************************************
1235 static NTSTATUS
CDROM_ResumeAudio(int fd
)
1238 return CDROM_GetStatusCode(ioctl(fd
, CDROMRESUME
));
1239 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1240 return CDROM_GetStatusCode(ioctl(fd
, CDIOCRESUME
, NULL
));
1242 return STATUS_NOT_SUPPORTED
;
1246 /******************************************************************
1251 static NTSTATUS
CDROM_StopAudio(int fd
)
1254 return CDROM_GetStatusCode(ioctl(fd
, CDROMSTOP
));
1255 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1256 return CDROM_GetStatusCode(ioctl(fd
, CDIOCSTOP
, NULL
));
1258 return STATUS_NOT_SUPPORTED
;
1262 /******************************************************************
1267 static NTSTATUS
CDROM_GetVolume(int fd
, VOLUME_CONTROL
* vc
)
1270 struct cdrom_volctrl volc
;
1273 io
= ioctl(fd
, CDROMVOLREAD
, &volc
);
1276 vc
->PortVolume
[0] = volc
.channel0
;
1277 vc
->PortVolume
[1] = volc
.channel1
;
1278 vc
->PortVolume
[2] = volc
.channel2
;
1279 vc
->PortVolume
[3] = volc
.channel3
;
1281 return CDROM_GetStatusCode(io
);
1282 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1283 struct ioc_vol volc
;
1286 io
= ioctl(fd
, CDIOCGETVOL
, &volc
);
1289 vc
->PortVolume
[0] = volc
.vol
[0];
1290 vc
->PortVolume
[1] = volc
.vol
[1];
1291 vc
->PortVolume
[2] = volc
.vol
[2];
1292 vc
->PortVolume
[3] = volc
.vol
[3];
1294 return CDROM_GetStatusCode(io
);
1296 return STATUS_NOT_SUPPORTED
;
1300 /******************************************************************
1305 static NTSTATUS
CDROM_SetVolume(int fd
, const VOLUME_CONTROL
* vc
)
1308 struct cdrom_volctrl volc
;
1310 volc
.channel0
= vc
->PortVolume
[0];
1311 volc
.channel1
= vc
->PortVolume
[1];
1312 volc
.channel2
= vc
->PortVolume
[2];
1313 volc
.channel3
= vc
->PortVolume
[3];
1315 return CDROM_GetStatusCode(ioctl(fd
, CDROMVOLCTRL
, &volc
));
1316 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1317 struct ioc_vol volc
;
1319 volc
.vol
[0] = vc
->PortVolume
[0];
1320 volc
.vol
[1] = vc
->PortVolume
[1];
1321 volc
.vol
[2] = vc
->PortVolume
[2];
1322 volc
.vol
[3] = vc
->PortVolume
[3];
1324 return CDROM_GetStatusCode(ioctl(fd
, CDIOCSETVOL
, &volc
));
1326 return STATUS_NOT_SUPPORTED
;
1330 /******************************************************************
1333 * Some features of this IOCTL are rather poorly documented and
1334 * not really intuitive either:
1336 * 1. Although the DiskOffset parameter is meant to be a
1337 * byte offset into the disk, it is in fact the sector
1338 * number multiplied by 2048 regardless of the actual
1341 * 2. The least significant 11 bits of DiskOffset are ignored.
1343 * 3. The TrackMode parameter has no effect on the sector
1344 * size. The entire raw sector (i.e. 2352 bytes of data)
1345 * is always returned. IMO the TrackMode is only used
1346 * to check the correct sector type.
1349 static NTSTATUS
CDROM_RawRead(int fd
, const RAW_READ_INFO
* raw
, void* buffer
, DWORD len
, DWORD
* sz
)
1351 int ret
= STATUS_NOT_SUPPORTED
;
1354 TRACE("RAW_READ_INFO: DiskOffset=%i,%i SectorCount=%i TrackMode=%i\n buffer=%p len=%i sz=%p\n",
1355 raw
->DiskOffset
.u
.HighPart
, raw
->DiskOffset
.u
.LowPart
, raw
->SectorCount
, raw
->TrackMode
, buffer
, len
, sz
);
1357 if (len
< raw
->SectorCount
* 2352) return STATUS_BUFFER_TOO_SMALL
;
1360 if (raw
->DiskOffset
.u
.HighPart
& ~2047) {
1361 WARN("DiskOffset points to a sector >= 2**32\n");
1365 switch (raw
->TrackMode
)
1370 DWORD lba
= raw
->DiskOffset
.QuadPart
>> 11;
1371 struct cdrom_msf
* msf
;
1372 PBYTE
*bp
; /* current buffer pointer */
1375 if ((lba
+ raw
->SectorCount
) >
1376 ((1 << 8*sizeof(msf
->cdmsf_min0
)) * CD_SECS
* CD_FRAMES
1378 WARN("DiskOffset not accessible with MSF\n");
1382 /* Linux reads only one sector at a time.
1383 * ioctl CDROMREADRAW takes struct cdrom_msf as an argument
1384 * on the contrary to what header comments state.
1386 lba
+= CD_MSF_OFFSET
;
1387 for (i
= 0, bp
= buffer
; i
< raw
->SectorCount
;
1388 i
++, lba
++, bp
+= 2352)
1390 msf
= (struct cdrom_msf
*)bp
;
1391 msf
->cdmsf_min0
= lba
/ CD_FRAMES
/ CD_SECS
;
1392 msf
->cdmsf_sec0
= lba
/ CD_FRAMES
% CD_SECS
;
1393 msf
->cdmsf_frame0
= lba
% CD_FRAMES
;
1394 io
= ioctl(fd
, CDROMREADRAW
, msf
);
1398 return CDROM_GetStatusCode(io
);
1406 struct cdrom_read_audio cdra
;
1408 cdra
.addr
.lba
= raw
->DiskOffset
.QuadPart
>> 11;
1409 TRACE("reading at %u\n", cdra
.addr
.lba
);
1410 cdra
.addr_format
= CDROM_LBA
;
1411 cdra
.nframes
= raw
->SectorCount
;
1413 io
= ioctl(fd
, CDROMREADAUDIO
, &cdra
);
1418 FIXME("NIY: %d\n", raw
->TrackMode
);
1419 return STATUS_INVALID_PARAMETER
;
1422 switch (raw
->TrackMode
)
1425 FIXME("YellowMode2: NIY\n");
1428 FIXME("XAForm2: NIY\n");
1431 FIXME("CDDA: NIY\n");
1434 FIXME("NIY: %d\n", raw
->TrackMode
);
1435 return STATUS_INVALID_PARAMETER
;
1439 *sz
= 2352 * raw
->SectorCount
;
1440 ret
= CDROM_GetStatusCode(io
);
1444 /******************************************************************
1445 * CDROM_ScsiPassThroughDirect
1446 * Implements IOCTL_SCSI_PASS_THROUGH_DIRECT
1449 static NTSTATUS
CDROM_ScsiPassThroughDirect(int fd
, PSCSI_PASS_THROUGH_DIRECT pPacket
)
1451 int ret
= STATUS_NOT_SUPPORTED
;
1452 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1455 #elif defined HAVE_SCSIREQ_T_CMD
1460 if (pPacket
->Length
< sizeof(SCSI_PASS_THROUGH_DIRECT
))
1461 return STATUS_BUFFER_TOO_SMALL
;
1463 if (pPacket
->CdbLength
> 16)
1464 return STATUS_INVALID_PARAMETER
;
1467 if (pPacket
->SenseInfoLength
> SENSEBUFLEN
)
1468 return STATUS_INVALID_PARAMETER
;
1469 #elif defined HAVE_REQUEST_SENSE
1470 if (pPacket
->SenseInfoLength
> sizeof(struct request_sense
))
1471 return STATUS_INVALID_PARAMETER
;
1474 if (pPacket
->DataTransferLength
> 0 && !pPacket
->DataBuffer
)
1475 return STATUS_INVALID_PARAMETER
;
1477 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1478 RtlZeroMemory(&cmd
, sizeof(cmd
));
1480 cmd
.interface_id
= 'S';
1481 cmd
.cmd_len
= pPacket
->CdbLength
;
1482 cmd
.mx_sb_len
= pPacket
->SenseInfoLength
;
1483 cmd
.dxfer_len
= pPacket
->DataTransferLength
;
1484 cmd
.dxferp
= pPacket
->DataBuffer
;
1485 cmd
.cmdp
= pPacket
->Cdb
;
1486 cmd
.sbp
= (unsigned char*)pPacket
+ pPacket
->SenseInfoOffset
;
1487 cmd
.timeout
= pPacket
->TimeOutValue
*1000;
1489 switch (pPacket
->DataIn
)
1491 case SCSI_IOCTL_DATA_IN
:
1492 cmd
.dxfer_direction
= SG_DXFER_FROM_DEV
;
1494 case SCSI_IOCTL_DATA_OUT
:
1495 cmd
.dxfer_direction
= SG_DXFER_TO_DEV
;
1497 case SCSI_IOCTL_DATA_UNSPECIFIED
:
1498 cmd
.dxfer_direction
= SG_DXFER_NONE
;
1501 return STATUS_INVALID_PARAMETER
;
1504 io
= ioctl(fd
, SG_IO
, &cmd
);
1506 pPacket
->ScsiStatus
= cmd
.status
;
1507 pPacket
->DataTransferLength
= cmd
.resid
;
1508 pPacket
->SenseInfoLength
= cmd
.sb_len_wr
;
1510 ret
= CDROM_GetStatusCode(io
);
1512 #elif defined HAVE_SCSIREQ_T_CMD
1514 memset(&cmd
, 0, sizeof(cmd
));
1515 memcpy(&(cmd
.cmd
), &(pPacket
->Cdb
), pPacket
->CdbLength
);
1517 cmd
.cmdlen
= pPacket
->CdbLength
;
1518 cmd
.databuf
= pPacket
->DataBuffer
;
1519 cmd
.datalen
= pPacket
->DataTransferLength
;
1520 cmd
.senselen
= pPacket
->SenseInfoLength
;
1521 cmd
.timeout
= pPacket
->TimeOutValue
*1000; /* in milliseconds */
1523 switch (pPacket
->DataIn
)
1525 case SCSI_IOCTL_DATA_OUT
:
1526 cmd
.flags
|= SCCMD_WRITE
;
1528 case SCSI_IOCTL_DATA_IN
:
1529 cmd
.flags
|= SCCMD_READ
;
1531 case SCSI_IOCTL_DATA_UNSPECIFIED
:
1535 return STATUS_INVALID_PARAMETER
;
1538 io
= ioctl(fd
, SCIOCCOMMAND
, &cmd
);
1542 case SCCMD_OK
: break;
1543 case SCCMD_TIMEOUT
: return STATUS_TIMEOUT
;
1545 case SCCMD_BUSY
: return STATUS_DEVICE_BUSY
;
1547 case SCCMD_SENSE
: break;
1548 case SCCMD_UNKNOWN
: return STATUS_UNSUCCESSFUL
;
1552 if (pPacket
->SenseInfoLength
!= 0)
1554 memcpy((char*)pPacket
+ pPacket
->SenseInfoOffset
,
1555 cmd
.sense
, pPacket
->SenseInfoLength
);
1558 pPacket
->ScsiStatus
= cmd
.status
;
1560 ret
= CDROM_GetStatusCode(io
);
1565 /******************************************************************
1566 * CDROM_ScsiPassThrough
1567 * Implements IOCTL_SCSI_PASS_THROUGH
1570 static NTSTATUS
CDROM_ScsiPassThrough(int fd
, PSCSI_PASS_THROUGH pPacket
)
1572 int ret
= STATUS_NOT_SUPPORTED
;
1573 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1576 #elif defined HAVE_SCSIREQ_T_CMD
1581 if (pPacket
->Length
< sizeof(SCSI_PASS_THROUGH
))
1582 return STATUS_BUFFER_TOO_SMALL
;
1584 if (pPacket
->CdbLength
> 16)
1585 return STATUS_INVALID_PARAMETER
;
1588 if (pPacket
->SenseInfoLength
> SENSEBUFLEN
)
1589 return STATUS_INVALID_PARAMETER
;
1590 #elif defined HAVE_REQUEST_SENSE
1591 if (pPacket
->SenseInfoLength
> sizeof(struct request_sense
))
1592 return STATUS_INVALID_PARAMETER
;
1595 if (pPacket
->DataTransferLength
> 0 && pPacket
->DataBufferOffset
< sizeof(SCSI_PASS_THROUGH
))
1596 return STATUS_INVALID_PARAMETER
;
1598 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1599 RtlZeroMemory(&cmd
, sizeof(cmd
));
1601 cmd
.interface_id
= 'S';
1602 cmd
.dxfer_len
= pPacket
->DataTransferLength
;
1603 cmd
.dxferp
= (char*)pPacket
+ pPacket
->DataBufferOffset
;
1604 cmd
.cmd_len
= pPacket
->CdbLength
;
1605 cmd
.cmdp
= pPacket
->Cdb
;
1606 cmd
.mx_sb_len
= pPacket
->SenseInfoLength
;
1607 cmd
.timeout
= pPacket
->TimeOutValue
*1000;
1609 if(cmd
.mx_sb_len
> 0)
1610 cmd
.sbp
= (unsigned char*)pPacket
+ pPacket
->SenseInfoOffset
;
1612 switch (pPacket
->DataIn
)
1614 case SCSI_IOCTL_DATA_IN
:
1615 cmd
.dxfer_direction
= SG_DXFER_FROM_DEV
;
1617 case SCSI_IOCTL_DATA_OUT
:
1618 cmd
.dxfer_direction
= SG_DXFER_TO_DEV
;
1620 case SCSI_IOCTL_DATA_UNSPECIFIED
:
1621 cmd
.dxfer_direction
= SG_DXFER_NONE
;
1624 return STATUS_INVALID_PARAMETER
;
1627 io
= ioctl(fd
, SG_IO
, &cmd
);
1629 pPacket
->ScsiStatus
= cmd
.status
;
1630 pPacket
->DataTransferLength
= cmd
.resid
;
1631 pPacket
->SenseInfoLength
= cmd
.sb_len_wr
;
1633 ret
= CDROM_GetStatusCode(io
);
1635 #elif defined HAVE_SCSIREQ_T_CMD
1637 memset(&cmd
, 0, sizeof(cmd
));
1638 memcpy(&(cmd
.cmd
), &(pPacket
->Cdb
), pPacket
->CdbLength
);
1640 if ( pPacket
->DataBufferOffset
> 0x1000 )
1642 cmd
.databuf
= (void*)pPacket
->DataBufferOffset
;
1646 cmd
.databuf
= (char*)pPacket
+ pPacket
->DataBufferOffset
;
1649 cmd
.cmdlen
= pPacket
->CdbLength
;
1650 cmd
.datalen
= pPacket
->DataTransferLength
;
1651 cmd
.senselen
= pPacket
->SenseInfoLength
;
1652 cmd
.timeout
= pPacket
->TimeOutValue
*1000; /* in milliseconds */
1654 switch (pPacket
->DataIn
)
1656 case SCSI_IOCTL_DATA_OUT
:
1657 cmd
.flags
|= SCCMD_WRITE
;
1659 case SCSI_IOCTL_DATA_IN
:
1660 cmd
.flags
|= SCCMD_READ
;
1662 case SCSI_IOCTL_DATA_UNSPECIFIED
:
1666 return STATUS_INVALID_PARAMETER
;
1669 io
= ioctl(fd
, SCIOCCOMMAND
, &cmd
);
1673 case SCCMD_OK
: break;
1674 case SCCMD_TIMEOUT
: return STATUS_TIMEOUT
;
1676 case SCCMD_BUSY
: return STATUS_DEVICE_BUSY
;
1678 case SCCMD_SENSE
: break;
1679 case SCCMD_UNKNOWN
: return STATUS_UNSUCCESSFUL
;
1683 if (pPacket
->SenseInfoLength
!= 0)
1685 memcpy((char*)pPacket
+ pPacket
->SenseInfoOffset
,
1686 cmd
.sense
, pPacket
->SenseInfoLength
);
1689 pPacket
->ScsiStatus
= cmd
.status
;
1691 ret
= CDROM_GetStatusCode(io
);
1696 /******************************************************************
1701 static NTSTATUS
CDROM_ScsiGetCaps(PIO_SCSI_CAPABILITIES caps
)
1703 NTSTATUS ret
= STATUS_NOT_IMPLEMENTED
;
1705 caps
->Length
= sizeof(*caps
);
1706 #ifdef SG_SCATTER_SZ
1707 caps
->MaximumTransferLength
= SG_SCATTER_SZ
; /* FIXME */
1708 caps
->MaximumPhysicalPages
= SG_SCATTER_SZ
/ getpagesize();
1709 caps
->SupportedAsynchronousEvents
= TRUE
;
1710 caps
->AlignmentMask
= getpagesize();
1711 caps
->TaggedQueuing
= FALSE
; /* we could check that it works and answer TRUE */
1712 caps
->AdapterScansDown
= FALSE
; /* FIXME ? */
1713 caps
->AdapterUsesPio
= FALSE
; /* FIXME ? */
1714 ret
= STATUS_SUCCESS
;
1716 FIXME("Unimplemented\n");
1721 /******************************************************************
1724 * implements IOCTL_SCSI_GET_ADDRESS
1726 static NTSTATUS
CDROM_GetAddress(int fd
, SCSI_ADDRESS
* address
)
1728 UCHAR portnum
, busid
, targetid
, lun
;
1730 address
->Length
= sizeof(SCSI_ADDRESS
);
1731 if ( ! CDROM_GetInterfaceInfo(fd
, &portnum
, &busid
, &targetid
, &lun
))
1732 return STATUS_NOT_SUPPORTED
;
1734 address
->PortNumber
= portnum
; /* primary=0 secondary=1 for ide */
1735 address
->PathId
= busid
; /* always 0 for ide */
1736 address
->TargetId
= targetid
; /* master=0 slave=1 for ide */
1738 return STATUS_SUCCESS
;
1741 /******************************************************************
1746 static NTSTATUS
DVD_StartSession(int fd
, const DVD_SESSION_ID
*sid_in
, PDVD_SESSION_ID sid_out
)
1749 NTSTATUS ret
= STATUS_NOT_SUPPORTED
;
1750 dvd_authinfo auth_info
;
1752 memset( &auth_info
, 0, sizeof( auth_info
) );
1753 auth_info
.type
= DVD_LU_SEND_AGID
;
1754 if (sid_in
) auth_info
.lsa
.agid
= *(const int*)sid_in
; /* ?*/
1756 TRACE("fd 0x%08x\n",fd
);
1757 ret
=CDROM_GetStatusCode(ioctl(fd
, DVD_AUTH
, &auth_info
));
1758 *sid_out
= auth_info
.lsa
.agid
;
1760 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1761 return STATUS_NOT_SUPPORTED
;
1763 return STATUS_NOT_SUPPORTED
;
1767 /******************************************************************
1772 static NTSTATUS
DVD_EndSession(int fd
, const DVD_SESSION_ID
*sid
)
1775 dvd_authinfo auth_info
;
1777 memset( &auth_info
, 0, sizeof( auth_info
) );
1778 auth_info
.type
= DVD_INVALIDATE_AGID
;
1779 auth_info
.lsa
.agid
= *(const int*)sid
;
1782 return CDROM_GetStatusCode(ioctl(fd
, DVD_AUTH
, &auth_info
));
1783 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1784 return STATUS_NOT_SUPPORTED
;
1786 return STATUS_NOT_SUPPORTED
;
1790 /******************************************************************
1795 static NTSTATUS
DVD_SendKey(int fd
, const DVD_COPY_PROTECT_KEY
*key
)
1798 NTSTATUS ret
= STATUS_NOT_SUPPORTED
;
1799 dvd_authinfo auth_info
;
1801 memset( &auth_info
, 0, sizeof( auth_info
) );
1802 switch (key
->KeyType
)
1804 case DvdChallengeKey
:
1805 auth_info
.type
= DVD_HOST_SEND_CHALLENGE
;
1806 auth_info
.hsc
.agid
= (int)key
->SessionId
;
1807 TRACE("DvdChallengeKey ioc 0x%x\n", DVD_AUTH
);
1808 memcpy( auth_info
.hsc
.chal
, key
->KeyData
, DVD_CHALLENGE_SIZE
);
1809 ret
= CDROM_GetStatusCode(ioctl( fd
, DVD_AUTH
, &auth_info
));
1812 auth_info
.type
= DVD_HOST_SEND_KEY2
;
1813 auth_info
.hsk
.agid
= (int)key
->SessionId
;
1815 memcpy( auth_info
.hsk
.key
, key
->KeyData
, DVD_KEY_SIZE
);
1817 TRACE("DvdBusKey2\n");
1818 ret
= CDROM_GetStatusCode(ioctl( fd
, DVD_AUTH
, &auth_info
));
1822 FIXME("Unknown Keytype 0x%x\n",key
->KeyType
);
1826 FIXME("unsupported on this platform\n");
1827 return STATUS_NOT_SUPPORTED
;
1831 /******************************************************************
1836 static NTSTATUS
DVD_ReadKey(int fd
, PDVD_COPY_PROTECT_KEY key
)
1839 NTSTATUS ret
= STATUS_NOT_SUPPORTED
;
1841 dvd_authinfo auth_info
;
1843 memset( &dvd
, 0, sizeof( dvd_struct
) );
1844 memset( &auth_info
, 0, sizeof( auth_info
) );
1845 switch (key
->KeyType
)
1849 dvd
.type
= DVD_STRUCT_DISCKEY
;
1850 dvd
.disckey
.agid
= (int)key
->SessionId
;
1851 memset( dvd
.disckey
.value
, 0, DVD_DISCKEY_SIZE
);
1853 TRACE("DvdDiskKey\n");
1854 ret
= CDROM_GetStatusCode(ioctl( fd
, DVD_READ_STRUCT
, &dvd
));
1855 if (ret
== STATUS_SUCCESS
)
1856 memcpy(key
->KeyData
,dvd
.disckey
.value
,DVD_DISCKEY_SIZE
);
1859 auth_info
.type
= DVD_LU_SEND_TITLE_KEY
;
1860 auth_info
.lstk
.agid
= (int)key
->SessionId
;
1861 auth_info
.lstk
.lba
= (int)(key
->Parameters
.TitleOffset
.QuadPart
>>11);
1862 TRACE("DvdTitleKey session %d Quadpart 0x%08lx offset 0x%08x\n",
1863 (int)key
->SessionId
, (long)key
->Parameters
.TitleOffset
.QuadPart
,
1864 auth_info
.lstk
.lba
);
1865 ret
= CDROM_GetStatusCode(ioctl( fd
, DVD_AUTH
, &auth_info
));
1866 if (ret
== STATUS_SUCCESS
)
1867 memcpy(key
->KeyData
, auth_info
.lstk
.title_key
, DVD_KEY_SIZE
);
1869 case DvdChallengeKey
:
1871 auth_info
.type
= DVD_LU_SEND_CHALLENGE
;
1872 auth_info
.lsc
.agid
= (int)key
->SessionId
;
1874 TRACE("DvdChallengeKey\n");
1875 ret
= CDROM_GetStatusCode(ioctl( fd
, DVD_AUTH
, &auth_info
));
1876 if (ret
== STATUS_SUCCESS
)
1877 memcpy( key
->KeyData
, auth_info
.lsc
.chal
, DVD_CHALLENGE_SIZE
);
1880 auth_info
.type
= DVD_LU_SEND_ASF
;
1882 auth_info
.lsasf
.asf
=((PDVD_ASF
)key
->KeyData
)->SuccessFlag
;
1883 ret
= CDROM_GetStatusCode(ioctl( fd
, DVD_AUTH
, &auth_info
));
1884 ((PDVD_ASF
)key
->KeyData
)->SuccessFlag
= auth_info
.lsasf
.asf
;
1887 auth_info
.type
= DVD_LU_SEND_KEY1
;
1888 auth_info
.lsk
.agid
= (int)key
->SessionId
;
1890 TRACE("DvdBusKey1\n");
1891 ret
= CDROM_GetStatusCode(ioctl( fd
, DVD_AUTH
, &auth_info
));
1893 if (ret
== STATUS_SUCCESS
)
1894 memcpy( key
->KeyData
, auth_info
.lsk
.key
, DVD_KEY_SIZE
);
1897 auth_info
.type
= DVD_LU_SEND_RPC_STATE
;
1899 TRACE("DvdGetRpcKey\n");
1900 ret
= CDROM_GetStatusCode(ioctl( fd
, DVD_AUTH
, &auth_info
));
1902 if (ret
== STATUS_SUCCESS
)
1904 ((PDVD_RPC_KEY
)key
->KeyData
)->TypeCode
= auth_info
.lrpcs
.type
;
1905 ((PDVD_RPC_KEY
)key
->KeyData
)->RegionMask
= auth_info
.lrpcs
.region_mask
;
1906 ((PDVD_RPC_KEY
)key
->KeyData
)->RpcScheme
= auth_info
.lrpcs
.rpc_scheme
;
1910 FIXME("Unknown keytype 0x%x\n",key
->KeyType
);
1913 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1915 return STATUS_NOT_SUPPORTED
;
1918 return STATUS_NOT_SUPPORTED
;
1922 /******************************************************************
1927 static NTSTATUS
DVD_GetRegion(int dev
, PDVD_REGION region
)
1930 return STATUS_SUCCESS
;
1934 /******************************************************************
1939 static NTSTATUS
DVD_ReadStructure(int dev
, const DVD_READ_STRUCTURE
*structure
, PDVD_LAYER_DESCRIPTOR layer
)
1941 #ifdef DVD_READ_STRUCT
1944 if (structure
->BlockByteOffset
.u
.HighPart
|| structure
->BlockByteOffset
.u
.LowPart
)
1945 FIXME(": BlockByteOffset is not handled\n");
1947 switch (structure
->Format
)
1949 case DvdPhysicalDescriptor
:
1950 s
.type
= DVD_STRUCT_PHYSICAL
;
1951 s
.physical
.layer_num
= structure
->LayerNumber
;
1954 case DvdCopyrightDescriptor
:
1955 s
.type
= DVD_STRUCT_COPYRIGHT
;
1956 s
.copyright
.layer_num
= structure
->LayerNumber
;
1959 case DvdDiskKeyDescriptor
:
1960 s
.type
= DVD_STRUCT_DISCKEY
;
1961 s
.disckey
.agid
= structure
->SessionId
;
1964 case DvdBCADescriptor
:
1965 s
.type
= DVD_STRUCT_BCA
;
1968 case DvdManufacturerDescriptor
:
1969 s
.type
= DVD_STRUCT_MANUFACT
;
1970 s
.manufact
.layer_num
= structure
->LayerNumber
;
1973 case DvdMaxDescriptor
: /* This is not a real request, no matter what MSDN says! */
1975 return STATUS_INVALID_PARAMETER
;
1978 if (ioctl(dev
, DVD_READ_STRUCT
, &s
) < 0)
1979 return STATUS_INVALID_PARAMETER
;
1981 switch (structure
->Format
)
1983 case DvdPhysicalDescriptor
:
1985 internal_dvd_layer_descriptor
*p
= (internal_dvd_layer_descriptor
*) layer
;
1986 struct dvd_layer
*l
= &s
.physical
.layer
[s
.physical
.layer_num
];
1988 p
->MagicHeader
[0] = 2;
1989 p
->MagicHeader
[1] = 8;
1990 p
->MagicHeader
[2] = 0;
1991 p
->MagicHeader
[3] = 0;
1992 p
->BookVersion
= l
->book_version
;
1993 p
->BookType
= l
->book_type
;
1994 p
->MinimumRate
= l
->min_rate
;
1995 p
->DiskSize
= l
->disc_size
;
1996 p
->LayerType
= l
->layer_type
;
1997 p
->TrackPath
= l
->track_path
;
1998 p
->NumberOfLayers
= l
->nlayers
;
2000 p
->TrackDensity
= l
->track_density
;
2001 p
->LinearDensity
= l
->linear_density
;
2002 p
->StartingDataSector
= l
->start_sector
;
2003 p
->EndDataSector
= l
->end_sector
;
2004 p
->EndLayerZeroSector
= l
->end_sector_l0
;
2006 p
->BCAFlag
= l
->bca
;
2011 case DvdCopyrightDescriptor
:
2013 PDVD_COPYRIGHT_DESCRIPTOR p
= (PDVD_COPYRIGHT_DESCRIPTOR
) layer
;
2015 p
->CopyrightProtectionType
= s
.copyright
.cpst
;
2016 p
->RegionManagementInformation
= s
.copyright
.rmi
;
2021 case DvdDiskKeyDescriptor
:
2023 PDVD_DISK_KEY_DESCRIPTOR p
= (PDVD_DISK_KEY_DESCRIPTOR
) layer
;
2025 memcpy(p
->DiskKeyData
, s
.disckey
.value
, 2048);
2029 case DvdBCADescriptor
:
2031 PDVD_BCA_DESCRIPTOR p
= (PDVD_BCA_DESCRIPTOR
) layer
;
2033 memcpy(p
->BCAInformation
, s
.bca
.value
, s
.bca
.len
);
2037 case DvdManufacturerDescriptor
:
2039 PDVD_MANUFACTURER_DESCRIPTOR p
= (PDVD_MANUFACTURER_DESCRIPTOR
) layer
;
2041 memcpy(p
->ManufacturingInformation
, s
.manufact
.value
, 2048);
2045 case DvdMaxDescriptor
: /* Suppress warning */
2051 return STATUS_SUCCESS
;
2055 /******************************************************************
2057 * Implements the IOCTL_GET_INQUIRY_DATA ioctl.
2058 * Returns Inquiry data for all devices on the specified scsi bus
2059 * Returns STATUS_BUFFER_TOO_SMALL if the output buffer is too small,
2060 * STATUS_INVALID_DEVICE_REQUEST if the given handle isn't to a SCSI device,
2061 * or STATUS_NOT_SUPPORTED if the OS driver is too old
2063 static NTSTATUS
GetInquiryData(int fd
, PSCSI_ADAPTER_BUS_INFO BufferOut
, DWORD OutBufferSize
)
2065 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
2066 PSCSI_INQUIRY_DATA pInquiryData
= NULL
;
2067 UCHAR sense_buffer
[32];
2070 UCHAR inquiry
[INQ_CMD_LEN
] = {INQUIRY
, 0, 0, 0, INQ_REPLY_LEN
, 0};
2072 /* Check we have a SCSI device and a supported driver */
2073 if(ioctl(fd
, SG_GET_VERSION_NUM
, &version
) != 0)
2075 WARN("IOCTL_SCSI_GET_INQUIRY_DATA sg driver is not loaded\n");
2076 return STATUS_INVALID_DEVICE_REQUEST
;
2078 if(version
< 30000 )
2079 return STATUS_NOT_SUPPORTED
;
2081 /* FIXME: Enumerate devices on the bus */
2082 BufferOut
->NumberOfBuses
= 1;
2083 BufferOut
->BusData
[0].NumberOfLogicalUnits
= 1;
2084 BufferOut
->BusData
[0].InquiryDataOffset
= sizeof(SCSI_ADAPTER_BUS_INFO
);
2086 pInquiryData
= (PSCSI_INQUIRY_DATA
)(BufferOut
+ 1);
2088 RtlZeroMemory(&iocmd
, sizeof(iocmd
));
2089 iocmd
.interface_id
= 'S';
2090 iocmd
.cmd_len
= sizeof(inquiry
);
2091 iocmd
.mx_sb_len
= sizeof(sense_buffer
);
2092 iocmd
.dxfer_direction
= SG_DXFER_FROM_DEV
;
2093 iocmd
.dxfer_len
= INQ_REPLY_LEN
;
2094 iocmd
.dxferp
= pInquiryData
->InquiryData
;
2095 iocmd
.cmdp
= inquiry
;
2096 iocmd
.sbp
= sense_buffer
;
2097 iocmd
.timeout
= 1000;
2099 iochk
= ioctl(fd
, SG_IO
, &iocmd
);
2101 WARN("ioctl SG_IO returned %d, error (%s)\n", iochk
, strerror(errno
));
2103 CDROM_GetInterfaceInfo(fd
, &BufferOut
->BusData
[0].InitiatorBusId
, &pInquiryData
->PathId
, &pInquiryData
->TargetId
, &pInquiryData
->Lun
);
2104 pInquiryData
->DeviceClaimed
= TRUE
;
2105 pInquiryData
->InquiryDataLength
= INQ_REPLY_LEN
;
2106 pInquiryData
->NextInquiryDataOffset
= 0;
2107 return STATUS_SUCCESS
;
2109 FIXME("not implemented for nonlinux\n");
2110 return STATUS_NOT_SUPPORTED
;
2114 /******************************************************************
2115 * CDROM_DeviceIoControl
2119 NTSTATUS
CDROM_DeviceIoControl(HANDLE hDevice
,
2120 HANDLE hEvent
, PIO_APC_ROUTINE UserApcRoutine
,
2121 PVOID UserApcContext
,
2122 PIO_STATUS_BLOCK piosb
,
2123 ULONG dwIoControlCode
,
2124 LPVOID lpInBuffer
, DWORD nInBufferSize
,
2125 LPVOID lpOutBuffer
, DWORD nOutBufferSize
)
2128 NTSTATUS status
= STATUS_SUCCESS
;
2129 int fd
, needs_close
, dev
;
2131 TRACE("%p %s %p %d %p %d %p\n",
2132 hDevice
, iocodex(dwIoControlCode
), lpInBuffer
, nInBufferSize
,
2133 lpOutBuffer
, nOutBufferSize
, piosb
);
2135 piosb
->Information
= 0;
2137 if ((status
= server_get_unix_fd( hDevice
, 0, &fd
, &needs_close
, NULL
, NULL
)))
2139 if (status
== STATUS_BAD_DEVICE_TYPE
) return status
; /* no associated fd */
2143 if ((status
= CDROM_Open(fd
, &dev
)))
2145 if (needs_close
) close( fd
);
2149 switch (dwIoControlCode
)
2151 case IOCTL_STORAGE_CHECK_VERIFY
:
2152 case IOCTL_CDROM_CHECK_VERIFY
:
2153 case IOCTL_DISK_CHECK_VERIFY
:
2155 CDROM_ClearCacheEntry(dev
);
2156 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0 || lpOutBuffer
!= NULL
|| nOutBufferSize
!= 0)
2157 status
= STATUS_INVALID_PARAMETER
;
2158 else status
= CDROM_Verify(dev
, fd
);
2161 /* EPP case IOCTL_STORAGE_CHECK_VERIFY2: */
2163 /* EPP case IOCTL_STORAGE_FIND_NEW_DEVICES: */
2164 /* EPP case IOCTL_CDROM_FIND_NEW_DEVICES: */
2166 case IOCTL_STORAGE_LOAD_MEDIA
:
2167 case IOCTL_CDROM_LOAD_MEDIA
:
2169 CDROM_ClearCacheEntry(dev
);
2170 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0 || lpOutBuffer
!= NULL
|| nOutBufferSize
!= 0)
2171 status
= STATUS_INVALID_PARAMETER
;
2172 else status
= CDROM_SetTray(fd
, FALSE
);
2174 case IOCTL_STORAGE_EJECT_MEDIA
:
2176 CDROM_ClearCacheEntry(dev
);
2177 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0 || lpOutBuffer
!= NULL
|| nOutBufferSize
!= 0)
2178 status
= STATUS_INVALID_PARAMETER
;
2184 /* This is ugly as hell, but Mac OS is unable to eject from the device fd,
2185 * it wants an fd for the whole device, and it also requires the device fd
2186 * to be closed first, so we have to close the handle that the caller gave us.
2187 * Also for some reason it wants the fd to be closed before we even open the parent.
2189 if ((status
= get_parent_device( fd
, name
, sizeof(name
) ))) break;
2191 if (needs_close
) close( fd
);
2192 TRACE("opening parent %s\n", name
);
2193 if ((fd
= open( name
, O_RDONLY
)) == -1)
2195 status
= FILE_GetNtStatus();
2200 status
= CDROM_SetTray(fd
, TRUE
);
2204 case IOCTL_CDROM_MEDIA_REMOVAL
:
2205 case IOCTL_DISK_MEDIA_REMOVAL
:
2206 case IOCTL_STORAGE_MEDIA_REMOVAL
:
2207 case IOCTL_STORAGE_EJECTION_CONTROL
:
2208 /* FIXME the last ioctl:s is not the same as the two others...
2209 * lockcount/owner should be handled */
2211 CDROM_ClearCacheEntry(dev
);
2212 if (lpOutBuffer
!= NULL
|| nOutBufferSize
!= 0) status
= STATUS_INVALID_PARAMETER
;
2213 else if (nInBufferSize
< sizeof(PREVENT_MEDIA_REMOVAL
)) status
= STATUS_BUFFER_TOO_SMALL
;
2214 else status
= CDROM_ControlEjection(fd
, lpInBuffer
);
2217 /* EPP case IOCTL_STORAGE_GET_MEDIA_TYPES: */
2219 case IOCTL_STORAGE_GET_DEVICE_NUMBER
:
2220 sz
= sizeof(STORAGE_DEVICE_NUMBER
);
2221 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0) status
= STATUS_INVALID_PARAMETER
;
2222 else if (nOutBufferSize
< sz
) status
= STATUS_BUFFER_TOO_SMALL
;
2223 else status
= CDROM_GetDeviceNumber(dev
, lpOutBuffer
);
2226 case IOCTL_STORAGE_RESET_DEVICE
:
2228 CDROM_ClearCacheEntry(dev
);
2229 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0 || lpOutBuffer
!= NULL
|| nOutBufferSize
!= 0)
2230 status
= STATUS_INVALID_PARAMETER
;
2231 else status
= CDROM_ResetAudio(fd
);
2234 case IOCTL_CDROM_GET_CONTROL
:
2235 sz
= sizeof(CDROM_AUDIO_CONTROL
);
2236 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0) status
= STATUS_INVALID_PARAMETER
;
2237 else if (nOutBufferSize
< sz
) status
= STATUS_BUFFER_TOO_SMALL
;
2238 else status
= CDROM_GetControl(dev
, lpOutBuffer
);
2241 case IOCTL_CDROM_GET_DRIVE_GEOMETRY
:
2242 sz
= sizeof(DISK_GEOMETRY
);
2243 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0) status
= STATUS_INVALID_PARAMETER
;
2244 else if (nOutBufferSize
< sz
) status
= STATUS_BUFFER_TOO_SMALL
;
2245 else status
= CDROM_GetDriveGeometry(dev
, fd
, lpOutBuffer
);
2248 case IOCTL_CDROM_DISK_TYPE
:
2249 sz
= sizeof(CDROM_DISK_DATA
);
2250 /* CDROM_ClearCacheEntry(dev); */
2251 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0) status
= STATUS_INVALID_PARAMETER
;
2252 else if (nOutBufferSize
< sz
) status
= STATUS_BUFFER_TOO_SMALL
;
2253 else status
= CDROM_GetDiskData(dev
, fd
, lpOutBuffer
);
2256 /* EPP case IOCTL_CDROM_GET_LAST_SESSION: */
2258 case IOCTL_CDROM_READ_Q_CHANNEL
:
2259 sz
= sizeof(SUB_Q_CHANNEL_DATA
);
2260 if (lpInBuffer
== NULL
|| nInBufferSize
< sizeof(CDROM_SUB_Q_DATA_FORMAT
))
2261 status
= STATUS_INVALID_PARAMETER
;
2262 else if (nOutBufferSize
< sz
) status
= STATUS_BUFFER_TOO_SMALL
;
2263 else status
= CDROM_ReadQChannel(dev
, fd
, lpInBuffer
, lpOutBuffer
);
2266 case IOCTL_CDROM_READ_TOC
:
2267 sz
= sizeof(CDROM_TOC
);
2268 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0) status
= STATUS_INVALID_PARAMETER
;
2269 else if (nOutBufferSize
< sz
) status
= STATUS_BUFFER_TOO_SMALL
;
2270 else status
= CDROM_ReadTOC(dev
, fd
, lpOutBuffer
);
2273 /* EPP case IOCTL_CDROM_READ_TOC_EX: */
2275 case IOCTL_CDROM_PAUSE_AUDIO
:
2277 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0 || lpOutBuffer
!= NULL
|| nOutBufferSize
!= 0)
2278 status
= STATUS_INVALID_PARAMETER
;
2279 else status
= CDROM_PauseAudio(fd
);
2281 case IOCTL_CDROM_PLAY_AUDIO_MSF
:
2283 if (lpOutBuffer
!= NULL
|| nOutBufferSize
!= 0) status
= STATUS_INVALID_PARAMETER
;
2284 else if (nInBufferSize
< sizeof(CDROM_PLAY_AUDIO_MSF
)) status
= STATUS_BUFFER_TOO_SMALL
;
2285 else status
= CDROM_PlayAudioMSF(fd
, lpInBuffer
);
2287 case IOCTL_CDROM_RESUME_AUDIO
:
2289 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0 || lpOutBuffer
!= NULL
|| nOutBufferSize
!= 0)
2290 status
= STATUS_INVALID_PARAMETER
;
2291 else status
= CDROM_ResumeAudio(fd
);
2293 case IOCTL_CDROM_SEEK_AUDIO_MSF
:
2295 if (lpOutBuffer
!= NULL
|| nOutBufferSize
!= 0) status
= STATUS_INVALID_PARAMETER
;
2296 else if (nInBufferSize
< sizeof(CDROM_SEEK_AUDIO_MSF
)) status
= STATUS_BUFFER_TOO_SMALL
;
2297 else status
= CDROM_SeekAudioMSF(dev
, fd
, lpInBuffer
);
2299 case IOCTL_CDROM_STOP_AUDIO
:
2301 CDROM_ClearCacheEntry(dev
); /* Maybe intention is to change media */
2302 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0 || lpOutBuffer
!= NULL
|| nOutBufferSize
!= 0)
2303 status
= STATUS_INVALID_PARAMETER
;
2304 else status
= CDROM_StopAudio(fd
);
2306 case IOCTL_CDROM_GET_VOLUME
:
2307 sz
= sizeof(VOLUME_CONTROL
);
2308 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0) status
= STATUS_INVALID_PARAMETER
;
2309 else if (nOutBufferSize
< sz
) status
= STATUS_BUFFER_TOO_SMALL
;
2310 else status
= CDROM_GetVolume(fd
, lpOutBuffer
);
2312 case IOCTL_CDROM_SET_VOLUME
:
2314 CDROM_ClearCacheEntry(dev
);
2315 if (lpInBuffer
== NULL
|| nInBufferSize
< sizeof(VOLUME_CONTROL
) || lpOutBuffer
!= NULL
)
2316 status
= STATUS_INVALID_PARAMETER
;
2317 else status
= CDROM_SetVolume(fd
, lpInBuffer
);
2319 case IOCTL_CDROM_RAW_READ
:
2321 if (nInBufferSize
< sizeof(RAW_READ_INFO
)) status
= STATUS_INVALID_PARAMETER
;
2322 else if (lpOutBuffer
== NULL
) status
= STATUS_BUFFER_TOO_SMALL
;
2323 else status
= CDROM_RawRead(fd
, lpInBuffer
, lpOutBuffer
,
2324 nOutBufferSize
, &sz
);
2326 case IOCTL_SCSI_GET_ADDRESS
:
2327 sz
= sizeof(SCSI_ADDRESS
);
2328 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0) status
= STATUS_INVALID_PARAMETER
;
2329 else if (nOutBufferSize
< sz
) status
= STATUS_BUFFER_TOO_SMALL
;
2330 else status
= CDROM_GetAddress(fd
, lpOutBuffer
);
2332 case IOCTL_SCSI_PASS_THROUGH_DIRECT
:
2333 sz
= sizeof(SCSI_PASS_THROUGH_DIRECT
);
2334 if (lpOutBuffer
== NULL
) status
= STATUS_INVALID_PARAMETER
;
2335 else if (nOutBufferSize
< sizeof(SCSI_PASS_THROUGH_DIRECT
)) status
= STATUS_BUFFER_TOO_SMALL
;
2336 else status
= CDROM_ScsiPassThroughDirect(fd
, lpOutBuffer
);
2338 case IOCTL_SCSI_PASS_THROUGH
:
2339 sz
= sizeof(SCSI_PASS_THROUGH
);
2340 if (lpOutBuffer
== NULL
) status
= STATUS_INVALID_PARAMETER
;
2341 else if (nOutBufferSize
< sizeof(SCSI_PASS_THROUGH
)) status
= STATUS_BUFFER_TOO_SMALL
;
2342 else status
= CDROM_ScsiPassThrough(fd
, lpOutBuffer
);
2344 case IOCTL_SCSI_GET_CAPABILITIES
:
2345 sz
= sizeof(IO_SCSI_CAPABILITIES
);
2346 if (lpOutBuffer
== NULL
) status
= STATUS_INVALID_PARAMETER
;
2347 else if (nOutBufferSize
< sizeof(IO_SCSI_CAPABILITIES
)) status
= STATUS_BUFFER_TOO_SMALL
;
2348 else status
= CDROM_ScsiGetCaps(lpOutBuffer
);
2350 case IOCTL_DVD_START_SESSION
:
2351 sz
= sizeof(DVD_SESSION_ID
);
2352 if (lpOutBuffer
== NULL
) status
= STATUS_INVALID_PARAMETER
;
2353 else if (nOutBufferSize
< sz
) status
= STATUS_BUFFER_TOO_SMALL
;
2356 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer
)?*(PDVD_SESSION_ID
)lpInBuffer
:0,
2357 *(PDVD_SESSION_ID
)lpOutBuffer
);
2358 status
= DVD_StartSession(fd
, lpInBuffer
, lpOutBuffer
);
2359 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer
)?*(PDVD_SESSION_ID
)lpInBuffer
:0,
2360 *(PDVD_SESSION_ID
)lpOutBuffer
);
2363 case IOCTL_DVD_END_SESSION
:
2364 sz
= sizeof(DVD_SESSION_ID
);
2365 if ((lpInBuffer
== NULL
) || (nInBufferSize
< sz
))status
= STATUS_INVALID_PARAMETER
;
2366 else status
= DVD_EndSession(fd
, lpInBuffer
);
2368 case IOCTL_DVD_SEND_KEY
:
2371 (((PDVD_COPY_PROTECT_KEY
)lpInBuffer
)->KeyLength
!= nInBufferSize
))
2372 status
= STATUS_INVALID_PARAMETER
;
2375 TRACE("doing DVD_SendKey\n");
2376 status
= DVD_SendKey(fd
, lpInBuffer
);
2379 case IOCTL_DVD_READ_KEY
:
2381 (((PDVD_COPY_PROTECT_KEY
)lpInBuffer
)->KeyLength
!= nInBufferSize
))
2382 status
= STATUS_INVALID_PARAMETER
;
2383 else if (lpInBuffer
!=lpOutBuffer
) status
= STATUS_BUFFER_TOO_SMALL
;
2386 TRACE("doing DVD_READ_KEY\n");
2387 sz
= ((PDVD_COPY_PROTECT_KEY
)lpInBuffer
)->KeyLength
;
2388 status
= DVD_ReadKey(fd
, lpInBuffer
);
2391 case IOCTL_DVD_GET_REGION
:
2392 sz
= sizeof(DVD_REGION
);
2393 if (lpInBuffer
!= NULL
|| nInBufferSize
!= 0) status
= STATUS_INVALID_PARAMETER
;
2394 else if (nOutBufferSize
< sz
) status
= STATUS_BUFFER_TOO_SMALL
;
2395 TRACE("doing DVD_Get_REGION\n");
2396 status
= DVD_GetRegion(fd
, lpOutBuffer
);
2398 case IOCTL_DVD_READ_STRUCTURE
:
2399 sz
= sizeof(DVD_LAYER_DESCRIPTOR
);
2400 if (lpInBuffer
== NULL
|| nInBufferSize
!= sizeof(DVD_READ_STRUCTURE
)) status
= STATUS_INVALID_PARAMETER
;
2401 else if (nOutBufferSize
< sz
) status
= STATUS_BUFFER_TOO_SMALL
;
2404 TRACE("doing DVD_READ_STRUCTURE\n");
2405 status
= DVD_ReadStructure(fd
, lpInBuffer
, lpOutBuffer
);
2409 case IOCTL_SCSI_GET_INQUIRY_DATA
:
2411 status
= GetInquiryData(fd
, lpOutBuffer
, nOutBufferSize
);
2415 if (needs_close
) close( fd
);
2416 return STATUS_NOT_SUPPORTED
;
2418 if (needs_close
) close( fd
);
2420 piosb
->u
.Status
= status
;
2421 piosb
->Information
= sz
;
2422 if (hEvent
) NtSetEvent(hEvent
, NULL
);