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 <libkern/OSByteOrder.h>
81 # include <sys/disk.h>
82 # include <IOKit/IOKitLib.h>
83 # include <IOKit/storage/IOMedia.h>
84 # include <IOKit/storage/IOCDMediaBSDClient.h>
85 # include <IOKit/storage/IODVDMediaBSDClient.h>
86 # include <IOKit/scsi/SCSITask.h>
87 # include <IOKit/scsi/SCSICmds_REQUEST_SENSE_Defs.h>
88 # define SENSEBUFLEN kSenseDefaultSize
98 uint8_t reserved0144[2];
106 #define DKIOCSCSICOMMAND _IOWR('d', 253, dk_scsi_command_t)
110 #define NONAMELESSUNION
111 #define NONAMELESSSTRUCT
112 #include "ntstatus.h"
113 #define WIN32_NO_STATUS
115 #include "winternl.h"
116 #include "winioctl.h"
117 #include "ntddstor.h"
118 #include "ntddcdrm.h"
119 #include "ddk/ntddcdvd.h"
120 #include "ntddscsi.h"
121 #include "ntdll_misc.h"
122 #include "wine/server.h"
123 #include "wine/library.h"
124 #include "wine/debug.h"
126 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
128 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
129 lack the following constants. */
132 # define CD_SECS 60 /* seconds per minute */
135 # define CD_FRAMES 75 /* frames per second */
138 static const struct iocodexs
143 #define X(x) { x, #x },
144 X(IOCTL_CDROM_CHECK_VERIFY)
145 X(IOCTL_CDROM_CURRENT_POSITION)
146 X(IOCTL_CDROM_DISK_TYPE)
147 X(IOCTL_CDROM_GET_CONTROL)
148 X(IOCTL_CDROM_GET_DRIVE_GEOMETRY)
149 X(IOCTL_CDROM_GET_VOLUME)
150 X(IOCTL_CDROM_LOAD_MEDIA)
151 X(IOCTL_CDROM_MEDIA_CATALOG)
152 X(IOCTL_CDROM_MEDIA_REMOVAL)
153 X(IOCTL_CDROM_PAUSE_AUDIO)
154 X(IOCTL_CDROM_PLAY_AUDIO_MSF)
155 X(IOCTL_CDROM_RAW_READ)
156 X(IOCTL_CDROM_READ_Q_CHANNEL)
157 X(IOCTL_CDROM_READ_TOC)
158 X(IOCTL_CDROM_RESUME_AUDIO)
159 X(IOCTL_CDROM_SEEK_AUDIO_MSF)
160 X(IOCTL_CDROM_SET_VOLUME)
161 X(IOCTL_CDROM_STOP_AUDIO)
162 X(IOCTL_CDROM_TRACK_ISRC)
163 X(IOCTL_DISK_MEDIA_REMOVAL)
164 X(IOCTL_DVD_END_SESSION)
165 X(IOCTL_DVD_GET_REGION)
166 X(IOCTL_DVD_READ_KEY)
167 X(IOCTL_DVD_READ_STRUCTURE)
168 X(IOCTL_DVD_SEND_KEY)
169 X(IOCTL_DVD_START_SESSION)
170 X(IOCTL_SCSI_GET_ADDRESS)
171 X(IOCTL_SCSI_GET_CAPABILITIES)
172 X(IOCTL_SCSI_GET_INQUIRY_DATA)
173 X(IOCTL_SCSI_PASS_THROUGH)
174 X(IOCTL_SCSI_PASS_THROUGH_DIRECT)
175 X(IOCTL_STORAGE_CHECK_VERIFY)
176 X(IOCTL_STORAGE_EJECTION_CONTROL)
177 X(IOCTL_STORAGE_EJECT_MEDIA)
178 X(IOCTL_STORAGE_GET_DEVICE_NUMBER)
179 X(IOCTL_STORAGE_GET_MEDIA_TYPES)
180 X(IOCTL_STORAGE_GET_MEDIA_TYPES_EX)
181 X(IOCTL_STORAGE_LOAD_MEDIA)
182 X(IOCTL_STORAGE_MEDIA_REMOVAL)
183 X(IOCTL_STORAGE_RESET_DEVICE)
186 static const char *iocodex(DWORD code)
189 static char buffer[25];
190 for(i=0; i<sizeof(iocodextable)/sizeof(struct iocodexs); i++)
191 if (code==iocodextable[i].code)
192 return iocodextable[i].codex;
193 sprintf(buffer, "IOCTL_CODE_%x", (int)code);
197 #define INQ_REPLY_LEN 36
198 #define INQ_CMD_LEN 6
200 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
201 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
202 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
203 #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;}
205 /* The documented format of DVD_LAYER_DESCRIPTOR is wrong. Even the format in the
206 * DDK's header is wrong. There are four bytes at the start defined by
207 * MMC-5. The first two are the size of the structure in big-endian order as
208 * defined by MMC-5. The other two are reserved.
214 UCHAR BookVersion : 4;
216 UCHAR MinimumRate : 4;
220 UCHAR NumberOfLayers : 2;
222 UCHAR TrackDensity : 4;
223 UCHAR LinearDensity : 4;
224 ULONG StartingDataSector;
226 ULONG EndLayerZeroSector;
230 } internal_dvd_layer_descriptor;
233 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
234 static NTSTATUS CDROM_GetStatusCode(int);
240 # define IDE6_MAJOR 88
243 # define IDE7_MAJOR 89
246 # ifdef CDROM_SEND_PACKET
247 /* structure for CDROM_PACKET_COMMAND ioctl */
248 /* not all Linux versions have all the fields, so we define the
249 * structure ourselves to make sure */
250 struct linux_cdrom_generic_command
252 unsigned char cmd[CDROM_PACKET_SIZE];
253 unsigned char *buffer;
256 struct request_sense *sense;
257 unsigned char data_direction;
262 # endif /* CDROM_SEND_PACKET */
266 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
267 * this should be removed when a proper device interface is implemented
269 * (WS) We need this to keep track of current position and to safely
270 * detect media changes. Besides this should provide a great speed up
276 char toc_good; /* if false, will reread TOC from disk */
278 SUB_Q_CURRENT_POSITION CurrentPosition;
280 /* who has more than 5 cdroms on his/her machine ?? */
281 /* FIXME: this should grow depending on the number of cdroms we install/configure
284 #define MAX_CACHE_ENTRIES 5
285 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
287 static RTL_CRITICAL_SECTION cache_section;
288 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
290 0, 0, &cache_section,
291 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
292 0, 0, { (DWORD_PTR)(__FILE__ ": cache_section") }
294 static RTL_CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
296 /* Proposed media change function: not really needed at this time */
297 /* This is a 1 or 0 type of function */
299 static int CDROM_MediaChanged(int dev)
303 struct cdrom_tochdr hdr;
304 struct cdrom_tocentry entry;
306 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
308 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
311 if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
314 for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
316 if (i == hdr.cdth_trk1 + 1)
318 entry.cdte_track = CDROM_LEADOUT;
320 entry.cdte_track = i;
322 entry.cdte_format = CDROM_MSF;
323 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
325 if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
326 sizeof(struct cdrom_tocentry)) )
334 /******************************************************************
337 * On Mac OS, get the device for the whole disk from a fd that points to a partition.
338 * This is ugly and inefficient, but we have no choice since the partition fd doesn't
339 * support the eject ioctl.
342 static NTSTATUS get_parent_device( int fd, char *name, size_t len )
344 NTSTATUS status = STATUS_NO_SUCH_FILE;
347 io_service_t service;
348 CFMutableDictionaryRef dict;
351 if (fstat( fd, &st ) == -1) return FILE_GetNtStatus();
352 if (!S_ISCHR( st.st_mode )) return STATUS_OBJECT_TYPE_MISMATCH;
354 /* create a dictionary with the right major/minor numbers */
356 if (!(dict = IOServiceMatching( kIOMediaClass ))) return STATUS_NO_MEMORY;
358 i = major( st.st_rdev );
359 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
360 CFDictionaryAddValue( dict, CFSTR( "BSD Major" ), val );
363 i = minor( st.st_rdev );
364 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
365 CFDictionaryAddValue( dict, CFSTR( "BSD Minor" ), val );
368 CFDictionaryAddValue( dict, CFSTR("Removable"), kCFBooleanTrue );
370 service = IOServiceGetMatchingService( kIOMasterPortDefault, dict );
372 /* now look for the parent that has the "Whole" attribute set to TRUE */
376 io_service_t parent = 0;
381 if (!IOObjectConformsTo( service, kIOMediaClass ))
383 if (!(whole = IORegistryEntryCreateCFProperty( service, CFSTR("Whole"), NULL, 0 )))
385 ok = (whole == kCFBooleanTrue);
389 if ((str = IORegistryEntryCreateCFProperty( service, CFSTR("BSD Name"), NULL, 0 )))
391 strcpy( name, "/dev/r" );
392 CFStringGetCString( str, name + 6, len - 6, kCFStringEncodingUTF8 );
394 status = STATUS_SUCCESS;
396 IOObjectRelease( service );
400 IORegistryEntryGetParentEntry( service, kIOServicePlane, &parent );
401 IOObjectRelease( service );
409 /******************************************************************
410 * CDROM_SyncCache [internal]
412 * Read the TOC in and store it in the cdrom_cache structure.
413 * Further requests for the TOC will be copied from the cache
414 * unless certain events like disk ejection is detected, in which
415 * case the cache will be cleared, causing it to be resynced.
416 * The cache section must be held by caller.
418 static NTSTATUS CDROM_SyncCache(int dev, int fd)
422 struct cdrom_tochdr hdr;
423 struct cdrom_tocentry entry;
425 CDROM_TOC *toc = &cdrom_cache[dev].toc;
426 cdrom_cache[dev].toc_good = 0;
428 if (ioctl(fd, CDROMREADTOCHDR, &hdr) == -1)
430 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
431 return FILE_GetNtStatus();
434 toc->FirstTrack = hdr.cdth_trk0;
435 toc->LastTrack = hdr.cdth_trk1;
436 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
437 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
438 toc->Length[0] = tsz >> 8;
439 toc->Length[1] = tsz;
441 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
443 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
445 if (i == toc->LastTrack + 1)
446 entry.cdte_track = CDROM_LEADOUT;
448 entry.cdte_track = i;
449 entry.cdte_format = CDROM_MSF;
450 if (ioctl(fd, CDROMREADTOCENTRY, &entry) == -1)
452 WARN("error read entry (%s)\n", strerror(errno));
453 return FILE_GetNtStatus();
455 toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
456 toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
457 /* marking last track with leadout value as index */
458 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
459 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
460 toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
461 toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
462 toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
464 cdrom_cache[dev].toc_good = 1;
465 return STATUS_SUCCESS;
467 #elif defined(__FreeBSD__) || defined(__NetBSD__)
470 struct ioc_toc_header hdr;
471 struct ioc_read_toc_entry entry;
472 struct cd_toc_entry toc_buffer;
474 CDROM_TOC *toc = &cdrom_cache[dev].toc;
475 cdrom_cache[dev].toc_good = 0;
477 if (ioctl(fd, CDIOREADTOCHEADER, &hdr) == -1)
479 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
480 return FILE_GetNtStatus();
482 toc->FirstTrack = hdr.starting_track;
483 toc->LastTrack = hdr.ending_track;
484 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
485 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
486 toc->Length[0] = tsz >> 8;
487 toc->Length[1] = tsz;
489 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
491 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
493 if (i == toc->LastTrack + 1)
496 entry.starting_track = LEADOUT;
498 entry.starting_track = i;
500 memset(&toc_buffer, 0, sizeof(toc_buffer));
501 entry.address_format = CD_MSF_FORMAT;
502 entry.data_len = sizeof(toc_buffer);
503 entry.data = &toc_buffer;
504 if (ioctl(fd, CDIOREADTOCENTRYS, &entry) == -1)
506 WARN("error read entry (%s)\n", strerror(errno));
507 return FILE_GetNtStatus();
509 toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
510 toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
511 /* marking last track with leadout value as index */
512 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
513 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
514 toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
515 toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
516 toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
518 cdrom_cache[dev].toc_good = 1;
519 return STATUS_SUCCESS;
521 #elif defined(__APPLE__)
523 dk_cd_read_toc_t hdr;
524 CDROM_TOC *toc = &cdrom_cache[dev].toc;
525 cdrom_cache[dev].toc_good = 0;
527 memset( &hdr, 0, sizeof(hdr) );
529 hdr.bufferLength = sizeof(*toc);
530 if (ioctl(fd, DKIOCCDREADTOC, &hdr) == -1)
532 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
533 return FILE_GetNtStatus();
535 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
537 /* convert address format */
538 TRACK_DATA *data = &toc->TrackData[i - toc->FirstTrack];
539 DWORD frame = (((DWORD)data->Address[0] << 24) | ((DWORD)data->Address[1] << 16) |
540 ((DWORD)data->Address[2] << 8) | data->Address[3]);
541 MSF_OF_FRAME( data->Address[1], frame );
542 data->Address[0] = 0;
545 cdrom_cache[dev].toc_good = 1;
546 return STATUS_SUCCESS;
548 return STATUS_NOT_SUPPORTED;
552 static void CDROM_ClearCacheEntry(int dev)
554 RtlEnterCriticalSection( &cache_section );
555 cdrom_cache[dev].toc_good = 0;
556 RtlLeaveCriticalSection( &cache_section );
561 /******************************************************************
562 * CDROM_GetInterfaceInfo
564 * Determines the ide interface (the number after the ide), and the
565 * number of the device on that interface for ide cdroms (*iface <= 1).
566 * Determines the scsi information for scsi cdroms (*iface >= 2).
567 * Returns false if the info cannot not be obtained.
569 static int CDROM_GetInterfaceInfo(int fd, UCHAR* iface, UCHAR* port, UCHAR* device, UCHAR* lun)
573 if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode)) return 0;
578 switch (major(st.st_rdev)) {
579 case IDE0_MAJOR: *iface = 0; break;
580 case IDE1_MAJOR: *iface = 1; break;
581 case IDE2_MAJOR: *iface = 2; break;
582 case IDE3_MAJOR: *iface = 3; break;
583 case IDE4_MAJOR: *iface = 4; break;
584 case IDE5_MAJOR: *iface = 5; break;
585 case IDE6_MAJOR: *iface = 6; break;
586 case IDE7_MAJOR: *iface = 7; break;
587 default: *port = 1; break;
591 *device = (minor(st.st_rdev) >> 6);
594 #ifdef SCSI_IOCTL_GET_IDLUN
596 if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, idlun) != -1)
598 *port = (idlun[0] >> 24) & 0xff;
599 *iface = ((idlun[0] >> 16) & 0xff) + 2;
600 *device = idlun[0] & 0xff;
601 *lun = (idlun[0] >> 8) & 0xff;
606 WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
611 #elif defined(__NetBSD__)
612 struct scsi_addr addr;
613 if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
619 *iface = addr.addr.scsi.scbus;
620 *device = addr.addr.scsi.target;
621 *lun = addr.addr.scsi.lun;
625 *iface = addr.addr.atapi.atbus;
626 *device = addr.addr.atapi.drive;
632 #elif defined(__FreeBSD__)
633 FIXME("not implemented for BSD\n");
636 FIXME("not implemented for nonlinux\n");
642 /******************************************************************
646 static NTSTATUS CDROM_Open(int fd, int* dev)
649 NTSTATUS ret = STATUS_SUCCESS;
654 RtlEnterCriticalSection( &cache_section );
655 for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
658 cdrom_cache[*dev].device == 0 &&
659 cdrom_cache[*dev].inode == 0)
661 else if (cdrom_cache[*dev].device == st.st_dev &&
662 cdrom_cache[*dev].inode == st.st_ino)
665 if (*dev == MAX_CACHE_ENTRIES)
667 if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
671 cdrom_cache[*dev].device = st.st_dev;
672 cdrom_cache[*dev].inode = st.st_ino;
675 RtlLeaveCriticalSection( &cache_section );
677 TRACE("%d, %d\n", *dev, fd);
681 /******************************************************************
682 * CDROM_GetStatusCode
686 static NTSTATUS CDROM_GetStatusCode(int io)
688 if (io == 0) return STATUS_SUCCESS;
689 return FILE_GetNtStatus();
692 /******************************************************************
696 static NTSTATUS CDROM_GetControl(int dev, int fd, CDROM_AUDIO_CONTROL* cac)
700 int io = ioctl( fd, DKIOCCDGETSPEED, &speed );
701 if (io != 0) return CDROM_GetStatusCode( io );
702 /* DKIOCCDGETSPEED returns the speed in kilobytes per second,
703 * so convert to logical blocks (assumed to be ~2 KB).
705 cac->LogicalBlocksPerSecond = speed/2;
707 cac->LogicalBlocksPerSecond = 1; /* FIXME */
709 cac->LbaFormat = 0; /* FIXME */
710 return STATUS_NOT_SUPPORTED;
713 /******************************************************************
714 * CDROM_GetDeviceNumber
717 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
720 devnum->DeviceType = FILE_DEVICE_DISK;
721 devnum->DeviceNumber = 1;
722 devnum->PartitionNumber = 1;
723 return STATUS_SUCCESS;
726 /******************************************************************
727 * CDROM_GetDriveGeometry
730 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
736 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
738 fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
739 - FRAME_OF_TOC(toc, 1); /* Total size in frames */
741 dg->Cylinders.u.LowPart = fsize / (64 * 32);
742 dg->Cylinders.u.HighPart = 0;
743 dg->MediaType = RemovableMedia;
744 dg->TracksPerCylinder = 64;
745 dg->SectorsPerTrack = 32;
746 dg->BytesPerSector= 2048;
750 /******************************************************************
754 static NTSTATUS CDROM_GetMediaType(int dev, GET_MEDIA_TYPES* medtype)
756 FIXME(": faking success\n");
757 medtype->DeviceType = FILE_DEVICE_CD_ROM;
758 medtype->MediaInfoCount = 0;
759 return STATUS_SUCCESS;
762 /**************************************************************************
763 * CDROM_Reset [internal]
765 static NTSTATUS CDROM_ResetAudio(int fd)
768 return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
769 #elif defined(__FreeBSD__) || defined(__NetBSD__)
770 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
772 return STATUS_NOT_SUPPORTED;
776 /******************************************************************
781 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
784 return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
785 #elif defined(__FreeBSD__) || defined(__NetBSD__)
786 return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
787 (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
788 (ioctl(fd, CDIOCPREVENT, NULL)));
789 #elif defined(__APPLE__)
790 if (doEject) return CDROM_GetStatusCode( ioctl( fd, DKIOCEJECT, NULL ) );
791 else return STATUS_NOT_SUPPORTED;
793 return STATUS_NOT_SUPPORTED;
797 /******************************************************************
798 * CDROM_ControlEjection
802 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
805 return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
806 #elif defined(__FreeBSD__) || defined(__NetBSD__)
807 return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
809 return STATUS_NOT_SUPPORTED;
813 /******************************************************************
818 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
820 NTSTATUS ret = STATUS_NOT_SUPPORTED;
822 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
823 return STATUS_INVALID_PARAMETER;
825 RtlEnterCriticalSection( &cache_section );
826 if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
828 *toc = cdrom_cache[dev].toc;
829 ret = STATUS_SUCCESS;
831 RtlLeaveCriticalSection( &cache_section );
835 /******************************************************************
840 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
846 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
848 for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
849 if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
850 data->DiskData |= CDROM_DISK_DATA_TRACK;
852 data->DiskData |= CDROM_DISK_AUDIO_TRACK;
854 return STATUS_SUCCESS;
857 /******************************************************************
862 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
863 SUB_Q_CHANNEL_DATA* data)
865 NTSTATUS ret = STATUS_NOT_SUPPORTED;
867 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
869 struct cdrom_subchnl sc;
870 sc.cdsc_format = CDROM_MSF;
872 io = ioctl(fd, CDROMSUBCHNL, &sc);
875 TRACE("opened or no_media (%s)!\n", strerror(errno));
876 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
877 CDROM_ClearCacheEntry(dev);
881 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
883 switch (sc.cdsc_audiostatus) {
884 case CDROM_AUDIO_INVALID:
885 CDROM_ClearCacheEntry(dev);
886 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
888 case CDROM_AUDIO_NO_STATUS:
889 CDROM_ClearCacheEntry(dev);
890 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
892 case CDROM_AUDIO_PLAY:
893 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
895 case CDROM_AUDIO_PAUSED:
896 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
898 case CDROM_AUDIO_COMPLETED:
899 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
901 case CDROM_AUDIO_ERROR:
902 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
905 TRACE("status=%02X !\n", sc.cdsc_audiostatus);
910 case IOCTL_CDROM_CURRENT_POSITION:
911 RtlEnterCriticalSection( &cache_section );
912 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
913 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
914 data->CurrentPosition.Control = sc.cdsc_ctrl;
915 data->CurrentPosition.ADR = sc.cdsc_adr;
916 data->CurrentPosition.TrackNumber = sc.cdsc_trk;
917 data->CurrentPosition.IndexNumber = sc.cdsc_ind;
919 data->CurrentPosition.AbsoluteAddress[0] = 0;
920 data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute;
921 data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
922 data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
924 data->CurrentPosition.TrackRelativeAddress[0] = 0;
925 data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute;
926 data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
927 data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
929 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
931 else /* not playing */
933 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
934 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
936 RtlLeaveCriticalSection( &cache_section );
938 case IOCTL_CDROM_MEDIA_CATALOG:
939 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
941 struct cdrom_mcn mcn;
942 if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
944 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
945 data->MediaCatalog.Mcval = 0; /* FIXME */
946 memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
947 data->MediaCatalog.MediaCatalog[14] = 0;
950 case IOCTL_CDROM_TRACK_ISRC:
951 FIXME("TrackIsrc: NIY on linux\n");
952 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
953 data->TrackIsrc.Tcval = 0;
959 ret = CDROM_GetStatusCode(io);
960 #elif defined(__FreeBSD__) || defined(__NetBSD__)
961 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
963 struct ioc_read_subchannel read_sc;
964 struct cd_sub_channel_info sc;
966 read_sc.address_format = CD_MSF_FORMAT;
968 read_sc.data_len = sizeof(sc);
972 case IOCTL_CDROM_CURRENT_POSITION:
973 read_sc.data_format = CD_CURRENT_POSITION;
975 case IOCTL_CDROM_MEDIA_CATALOG:
976 read_sc.data_format = CD_MEDIA_CATALOG;
978 case IOCTL_CDROM_TRACK_ISRC:
979 read_sc.data_format = CD_TRACK_INFO;
980 sc.what.track_info.track_number = data->TrackIsrc.Track;
983 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
986 TRACE("opened or no_media (%s)!\n", strerror(errno));
987 CDROM_ClearCacheEntry(dev);
988 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
992 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
994 switch (sc.header.audio_status) {
995 case CD_AS_AUDIO_INVALID:
996 CDROM_ClearCacheEntry(dev);
997 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
999 case CD_AS_NO_STATUS:
1000 CDROM_ClearCacheEntry(dev);
1001 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1003 case CD_AS_PLAY_IN_PROGRESS:
1004 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
1006 case CD_AS_PLAY_PAUSED:
1007 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
1009 case CD_AS_PLAY_COMPLETED:
1010 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
1012 case CD_AS_PLAY_ERROR:
1013 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
1016 TRACE("status=%02X !\n", sc.header.audio_status);
1018 switch (fmt->Format)
1020 case IOCTL_CDROM_CURRENT_POSITION:
1021 RtlEnterCriticalSection( &cache_section );
1022 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
1023 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1024 data->CurrentPosition.Control = sc.what.position.control;
1025 data->CurrentPosition.ADR = sc.what.position.addr_type;
1026 data->CurrentPosition.TrackNumber = sc.what.position.track_number;
1027 data->CurrentPosition.IndexNumber = sc.what.position.index_number;
1029 data->CurrentPosition.AbsoluteAddress[0] = 0;
1030 data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
1031 data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
1032 data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
1033 data->CurrentPosition.TrackRelativeAddress[0] = 0;
1034 data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
1035 data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
1036 data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
1037 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
1039 else { /* not playing */
1040 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
1041 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
1043 RtlLeaveCriticalSection( &cache_section );
1045 case IOCTL_CDROM_MEDIA_CATALOG:
1046 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
1047 data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
1048 memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
1050 case IOCTL_CDROM_TRACK_ISRC:
1051 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
1052 data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
1053 memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
1058 ret = CDROM_GetStatusCode(io);
1059 #elif defined(__APPLE__)
1060 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
1064 dk_cd_read_mcn_t mcn;
1065 dk_cd_read_isrc_t isrc;
1067 /* We need IOCDAudioControl for IOCTL_CDROM_CURRENT_POSITION */
1068 if (fmt->Format == IOCTL_CDROM_CURRENT_POSITION)
1071 return STATUS_NOT_SUPPORTED;
1073 /* No IOCDAudioControl support; just set the audio status to none */
1074 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1077 case IOCTL_CDROM_MEDIA_CATALOG:
1078 if ((io = ioctl(fd, DKIOCCDREADMCN, &ioc.mcn)) == -1) break;
1079 memcpy(data->MediaCatalog.MediaCatalog, ioc.mcn.mcn, kCDMCNMaxLength);
1080 data->MediaCatalog.Mcval = 1;
1082 case IOCTL_CDROM_TRACK_ISRC:
1083 ioc.isrc.track = fmt->Track;
1084 if ((io = ioctl(fd, DKIOCCDREADISRC, &ioc.isrc)) == -1) break;
1085 memcpy(data->TrackIsrc.TrackIsrc, ioc.isrc.isrc, kCDISRCMaxLength);
1086 data->TrackIsrc.Tcval = 1;
1087 data->TrackIsrc.Track = ioc.isrc.track;
1089 ret = CDROM_GetStatusCode(io);
1094 /******************************************************************
1096 * Implements: IOCTL_STORAGE_CHECK_VERIFY
1097 * IOCTL_CDROM_CHECK_VERIFY
1098 * IOCTL_DISK_CHECK_VERIFY
1101 static NTSTATUS CDROM_Verify(int dev, int fd)
1106 ret = ioctl(fd, CDROM_DRIVE_STATUS, NULL);
1108 TRACE("ioctl CDROM_DRIVE_STATUS failed(%s)!\n", strerror(errno));
1109 return CDROM_GetStatusCode(ret);
1112 if(ret == CDS_DISC_OK)
1113 return STATUS_SUCCESS;
1115 return STATUS_NO_MEDIA_IN_DEVICE;
1116 #elif defined(__FreeBSD__)
1118 ret = ioctl(fd, CDIOCSTART, NULL);
1120 return STATUS_SUCCESS;
1122 return STATUS_NO_MEDIA_IN_DEVICE;
1124 FIXME("not implemented for non-linux\n");
1125 return STATUS_NOT_SUPPORTED;
1129 /******************************************************************
1130 * CDROM_PlayAudioMSF
1134 static NTSTATUS CDROM_PlayAudioMSF(int fd, const CDROM_PLAY_AUDIO_MSF* audio_msf)
1136 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1138 struct cdrom_msf msf;
1141 msf.cdmsf_min0 = audio_msf->StartingM;
1142 msf.cdmsf_sec0 = audio_msf->StartingS;
1143 msf.cdmsf_frame0 = audio_msf->StartingF;
1144 msf.cdmsf_min1 = audio_msf->EndingM;
1145 msf.cdmsf_sec1 = audio_msf->EndingS;
1146 msf.cdmsf_frame1 = audio_msf->EndingF;
1148 io = ioctl(fd, CDROMSTART);
1151 WARN("motor doesn't start !\n");
1154 io = ioctl(fd, CDROMPLAYMSF, &msf);
1157 WARN("device doesn't play !\n");
1160 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1161 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
1162 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
1164 ret = CDROM_GetStatusCode(io);
1165 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1166 struct ioc_play_msf msf;
1169 msf.start_m = audio_msf->StartingM;
1170 msf.start_s = audio_msf->StartingS;
1171 msf.start_f = audio_msf->StartingF;
1172 msf.end_m = audio_msf->EndingM;
1173 msf.end_s = audio_msf->EndingS;
1174 msf.end_f = audio_msf->EndingF;
1176 io = ioctl(fd, CDIOCSTART, NULL);
1179 WARN("motor doesn't start !\n");
1182 io = ioctl(fd, CDIOCPLAYMSF, &msf);
1185 WARN("device doesn't play !\n");
1188 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1189 msf.start_m, msf.start_s, msf.start_f,
1190 msf.end_m, msf.end_s, msf.end_f);
1192 ret = CDROM_GetStatusCode(io);
1197 /******************************************************************
1198 * CDROM_SeekAudioMSF
1202 static NTSTATUS CDROM_SeekAudioMSF(int dev, int fd, const CDROM_SEEK_AUDIO_MSF* audio_msf)
1206 SUB_Q_CURRENT_POSITION *cp;
1208 struct cdrom_msf0 msf;
1209 struct cdrom_subchnl sc;
1210 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1211 struct ioc_play_msf msf;
1212 struct ioc_read_subchannel read_sc;
1213 struct cd_sub_channel_info sc;
1217 /* Use the information on the TOC to compute the new current
1218 * position, which is shadowed on the cache. [Portable]. */
1219 frame = FRAME_OF_MSF(*audio_msf);
1221 if ((io = CDROM_ReadTOC(dev, fd, &toc)) != 0) return io;
1223 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
1224 if (FRAME_OF_TOC(toc,i)>frame) break;
1225 if (i <= toc.FirstTrack || i > toc.LastTrack+1)
1226 return STATUS_INVALID_PARAMETER;
1228 RtlEnterCriticalSection( &cache_section );
1229 cp = &cdrom_cache[dev].CurrentPosition;
1230 cp->FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1231 cp->Control = toc.TrackData[i-toc.FirstTrack].Control;
1232 cp->ADR = toc.TrackData[i-toc.FirstTrack].Adr;
1233 cp->TrackNumber = toc.TrackData[i-toc.FirstTrack].TrackNumber;
1234 cp->IndexNumber = 0; /* FIXME: where do they keep these? */
1235 cp->AbsoluteAddress[0] = 0;
1236 cp->AbsoluteAddress[1] = toc.TrackData[i-toc.FirstTrack].Address[1];
1237 cp->AbsoluteAddress[2] = toc.TrackData[i-toc.FirstTrack].Address[2];
1238 cp->AbsoluteAddress[3] = toc.TrackData[i-toc.FirstTrack].Address[3];
1239 frame -= FRAME_OF_TOC(toc,i);
1240 cp->TrackRelativeAddress[0] = 0;
1241 MSF_OF_FRAME(cp->TrackRelativeAddress[1], frame);
1242 RtlLeaveCriticalSection( &cache_section );
1244 /* If playing, then issue a seek command, otherwise do nothing */
1246 sc.cdsc_format = CDROM_MSF;
1248 io = ioctl(fd, CDROMSUBCHNL, &sc);
1251 TRACE("opened or no_media (%s)!\n", strerror(errno));
1252 CDROM_ClearCacheEntry(dev);
1253 return CDROM_GetStatusCode(io);
1255 if (sc.cdsc_audiostatus==CDROM_AUDIO_PLAY)
1257 msf.minute = audio_msf->M;
1258 msf.second = audio_msf->S;
1259 msf.frame = audio_msf->F;
1260 return CDROM_GetStatusCode(ioctl(fd, CDROMSEEK, &msf));
1262 return STATUS_SUCCESS;
1263 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1264 read_sc.address_format = CD_MSF_FORMAT;
1266 read_sc.data_len = sizeof(sc);
1268 read_sc.data_format = CD_CURRENT_POSITION;
1270 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1273 TRACE("opened or no_media (%s)!\n", strerror(errno));
1274 CDROM_ClearCacheEntry(dev);
1275 return CDROM_GetStatusCode(io);
1277 if (sc.header.audio_status==CD_AS_PLAY_IN_PROGRESS)
1280 msf.start_m = audio_msf->M;
1281 msf.start_s = audio_msf->S;
1282 msf.start_f = audio_msf->F;
1283 final_frame = FRAME_OF_TOC(toc,toc.LastTrack+1)-1;
1284 MSF_OF_FRAME(msf.end_m, final_frame);
1286 return CDROM_GetStatusCode(ioctl(fd, CDIOCPLAYMSF, &msf));
1288 return STATUS_SUCCESS;
1290 return STATUS_NOT_SUPPORTED;
1294 /******************************************************************
1299 static NTSTATUS CDROM_PauseAudio(int fd)
1302 return CDROM_GetStatusCode(ioctl(fd, CDROMPAUSE));
1303 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1304 return CDROM_GetStatusCode(ioctl(fd, CDIOCPAUSE, NULL));
1306 return STATUS_NOT_SUPPORTED;
1310 /******************************************************************
1315 static NTSTATUS CDROM_ResumeAudio(int fd)
1318 return CDROM_GetStatusCode(ioctl(fd, CDROMRESUME));
1319 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1320 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESUME, NULL));
1322 return STATUS_NOT_SUPPORTED;
1326 /******************************************************************
1331 static NTSTATUS CDROM_StopAudio(int fd)
1334 return CDROM_GetStatusCode(ioctl(fd, CDROMSTOP));
1335 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1336 return CDROM_GetStatusCode(ioctl(fd, CDIOCSTOP, NULL));
1338 return STATUS_NOT_SUPPORTED;
1342 /******************************************************************
1347 static NTSTATUS CDROM_GetVolume(int fd, VOLUME_CONTROL* vc)
1350 struct cdrom_volctrl volc;
1353 io = ioctl(fd, CDROMVOLREAD, &volc);
1356 vc->PortVolume[0] = volc.channel0;
1357 vc->PortVolume[1] = volc.channel1;
1358 vc->PortVolume[2] = volc.channel2;
1359 vc->PortVolume[3] = volc.channel3;
1361 return CDROM_GetStatusCode(io);
1362 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1363 struct ioc_vol volc;
1366 io = ioctl(fd, CDIOCGETVOL, &volc);
1369 vc->PortVolume[0] = volc.vol[0];
1370 vc->PortVolume[1] = volc.vol[1];
1371 vc->PortVolume[2] = volc.vol[2];
1372 vc->PortVolume[3] = volc.vol[3];
1374 return CDROM_GetStatusCode(io);
1376 return STATUS_NOT_SUPPORTED;
1380 /******************************************************************
1385 static NTSTATUS CDROM_SetVolume(int fd, const VOLUME_CONTROL* vc)
1388 struct cdrom_volctrl volc;
1390 volc.channel0 = vc->PortVolume[0];
1391 volc.channel1 = vc->PortVolume[1];
1392 volc.channel2 = vc->PortVolume[2];
1393 volc.channel3 = vc->PortVolume[3];
1395 return CDROM_GetStatusCode(ioctl(fd, CDROMVOLCTRL, &volc));
1396 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1397 struct ioc_vol volc;
1399 volc.vol[0] = vc->PortVolume[0];
1400 volc.vol[1] = vc->PortVolume[1];
1401 volc.vol[2] = vc->PortVolume[2];
1402 volc.vol[3] = vc->PortVolume[3];
1404 return CDROM_GetStatusCode(ioctl(fd, CDIOCSETVOL, &volc));
1406 return STATUS_NOT_SUPPORTED;
1410 /******************************************************************
1413 * Some features of this IOCTL are rather poorly documented and
1414 * not really intuitive either:
1416 * 1. Although the DiskOffset parameter is meant to be a
1417 * byte offset into the disk, it is in fact the sector
1418 * number multiplied by 2048 regardless of the actual
1421 * 2. The least significant 11 bits of DiskOffset are ignored.
1423 * 3. The TrackMode parameter has no effect on the sector
1424 * size. The entire raw sector (i.e. 2352 bytes of data)
1425 * is always returned. IMO the TrackMode is only used
1426 * to check the correct sector type.
1429 static NTSTATUS CDROM_RawRead(int fd, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
1431 int ret = STATUS_NOT_SUPPORTED;
1434 TRACE("RAW_READ_INFO: DiskOffset=%i,%i SectorCount=%i TrackMode=%i\n buffer=%p len=%i sz=%p\n",
1435 raw->DiskOffset.u.HighPart, raw->DiskOffset.u.LowPart, raw->SectorCount, raw->TrackMode, buffer, len, sz);
1437 if (len < raw->SectorCount * 2352) return STATUS_BUFFER_TOO_SMALL;
1440 if (raw->DiskOffset.u.HighPart & ~2047) {
1441 WARN("DiskOffset points to a sector >= 2**32\n");
1445 switch (raw->TrackMode)
1450 DWORD lba = raw->DiskOffset.QuadPart >> 11;
1451 struct cdrom_msf* msf;
1452 PBYTE *bp; /* current buffer pointer */
1455 if ((lba + raw->SectorCount) >
1456 ((1 << 8*sizeof(msf->cdmsf_min0)) * CD_SECS * CD_FRAMES
1458 WARN("DiskOffset not accessible with MSF\n");
1462 /* Linux reads only one sector at a time.
1463 * ioctl CDROMREADRAW takes struct cdrom_msf as an argument
1464 * on the contrary to what header comments state.
1466 lba += CD_MSF_OFFSET;
1467 for (i = 0, bp = buffer; i < raw->SectorCount;
1468 i++, lba++, bp += 2352)
1470 msf = (struct cdrom_msf*)bp;
1471 msf->cdmsf_min0 = lba / CD_FRAMES / CD_SECS;
1472 msf->cdmsf_sec0 = lba / CD_FRAMES % CD_SECS;
1473 msf->cdmsf_frame0 = lba % CD_FRAMES;
1474 io = ioctl(fd, CDROMREADRAW, msf);
1478 return CDROM_GetStatusCode(io);
1486 struct cdrom_read_audio cdra;
1488 cdra.addr.lba = raw->DiskOffset.QuadPart >> 11;
1489 TRACE("reading at %u\n", cdra.addr.lba);
1490 cdra.addr_format = CDROM_LBA;
1491 cdra.nframes = raw->SectorCount;
1493 io = ioctl(fd, CDROMREADAUDIO, &cdra);
1498 FIXME("NIY: %d\n", raw->TrackMode);
1499 return STATUS_INVALID_PARAMETER;
1501 #elif defined(__APPLE__)
1502 switch (raw->TrackMode)
1506 /* Mac OS, on the other hand, DOES read only one part of the sector
1507 * at a time. Therefore, we have to read each part of the sector, in
1508 * order, to get the whole raw sector in.
1509 * This means that we have to read each sector one at a time, as on
1513 UInt64 lba = raw->DiskOffset.QuadPart >> 11;
1517 for (i = 0, bp = buffer; i < raw->SectorCount;
1518 i++, lba++, bp += kCDSectorSizeWhole)
1520 cdrd.offset = lba * kCDSectorSizeWhole;
1521 cdrd.sectorType = kCDSectorTypeMode2;
1523 /* First, the sync area */
1524 cdrd.sectorArea = kCDSectorAreaSync;
1526 cdrd.bufferLength = 12;
1527 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1530 *sz = kCDSectorSizeWhole * i;
1531 return CDROM_GetStatusCode(io);
1534 /* Then the header */
1536 cdrd.sectorArea = kCDSectorAreaHeader;
1537 cdrd.buffer = (PBYTE)cdrd.buffer + 12;
1538 cdrd.bufferLength = 4;
1539 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1542 *sz = kCDSectorSizeWhole * i + 12;
1543 return CDROM_GetStatusCode(io);
1546 /* And finally the sector proper */
1548 cdrd.sectorArea = kCDSectorAreaUser;
1549 cdrd.buffer = (PBYTE)cdrd.buffer + 4;
1550 cdrd.bufferLength = kCDSectorSizeMode2;
1551 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1554 *sz = kCDSectorSizeWhole * i + 16;
1555 return CDROM_GetStatusCode(io);
1566 UInt64 lba = raw->DiskOffset.QuadPart >> 11;
1570 for (i = 0, bp = buffer; i < raw->SectorCount;
1571 i++, lba++, bp += kCDSectorSizeWhole)
1573 cdrd.offset = lba * kCDSectorSizeWhole;
1574 cdrd.sectorType = kCDSectorTypeMode2Form2;
1576 /* First, the sync area */
1577 cdrd.sectorArea = kCDSectorAreaSync;
1579 cdrd.bufferLength = 12;
1580 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1583 *sz = kCDSectorSizeWhole * i;
1584 return CDROM_GetStatusCode(io);
1587 /* Then the header */
1589 cdrd.sectorArea = kCDSectorAreaHeader;
1590 cdrd.buffer = (PBYTE)cdrd.buffer + 12;
1591 cdrd.bufferLength = 4;
1592 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1595 *sz = kCDSectorSizeWhole * i + 12;
1596 return CDROM_GetStatusCode(io);
1599 /* And the sub-header */
1601 cdrd.sectorArea = kCDSectorAreaSubHeader;
1602 cdrd.buffer = (PBYTE)cdrd.buffer + 4;
1603 cdrd.bufferLength = 8;
1604 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1607 *sz = kCDSectorSizeWhole * i + 16;
1608 return CDROM_GetStatusCode(io);
1611 /* And finally the sector proper */
1613 cdrd.sectorArea = kCDSectorAreaUser;
1614 cdrd.buffer = (PBYTE)cdrd.buffer + 8;
1615 cdrd.bufferLength = kCDSectorSizeMode2;
1616 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1619 *sz = kCDSectorSizeWhole * i + 24;
1620 return CDROM_GetStatusCode(io);
1629 /* With CDDA, the whole raw sector is considered user data, so there's
1630 * no need to read one at a time.
1634 cdrd.offset = (raw->DiskOffset.QuadPart >> 11) * kCDSectorSizeCDDA;
1635 cdrd.sectorArea = kCDSectorAreaUser;
1636 cdrd.sectorType = kCDSectorTypeCDDA;
1637 cdrd.buffer = buffer;
1638 cdrd.bufferLength = len < raw->SectorCount*kCDSectorSizeCDDA ? len :
1639 raw->SectorCount*kCDSectorSizeCDDA;
1641 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1642 if (io != 0) return CDROM_GetStatusCode(io);
1647 FIXME("NIY: %d\n", raw->TrackMode);
1648 return STATUS_INVALID_PARAMETER;
1651 switch (raw->TrackMode)
1654 FIXME("YellowMode2: NIY\n");
1657 FIXME("XAForm2: NIY\n");
1660 FIXME("CDDA: NIY\n");
1663 FIXME("NIY: %d\n", raw->TrackMode);
1664 return STATUS_INVALID_PARAMETER;
1668 *sz = 2352 * raw->SectorCount;
1669 ret = CDROM_GetStatusCode(io);
1673 /******************************************************************
1674 * CDROM_ScsiPassThroughDirect
1675 * Implements IOCTL_SCSI_PASS_THROUGH_DIRECT
1678 static NTSTATUS CDROM_ScsiPassThroughDirect(int fd, PSCSI_PASS_THROUGH_DIRECT pPacket)
1680 int ret = STATUS_NOT_SUPPORTED;
1681 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1684 #elif defined HAVE_SCSIREQ_T_CMD
1687 #elif defined __APPLE__
1688 dk_scsi_command_t cmd;
1692 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1693 return STATUS_BUFFER_TOO_SMALL;
1695 if (pPacket->CdbLength > 16)
1696 return STATUS_INVALID_PARAMETER;
1699 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1700 return STATUS_INVALID_PARAMETER;
1701 #elif defined HAVE_REQUEST_SENSE
1702 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1703 return STATUS_INVALID_PARAMETER;
1706 if (pPacket->DataTransferLength > 0 && !pPacket->DataBuffer)
1707 return STATUS_INVALID_PARAMETER;
1709 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1710 RtlZeroMemory(&cmd, sizeof(cmd));
1712 cmd.interface_id = 'S';
1713 cmd.cmd_len = pPacket->CdbLength;
1714 cmd.mx_sb_len = pPacket->SenseInfoLength;
1715 cmd.dxfer_len = pPacket->DataTransferLength;
1716 cmd.dxferp = pPacket->DataBuffer;
1717 cmd.cmdp = pPacket->Cdb;
1718 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1719 cmd.timeout = pPacket->TimeOutValue*1000;
1721 switch (pPacket->DataIn)
1723 case SCSI_IOCTL_DATA_IN:
1724 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1726 case SCSI_IOCTL_DATA_OUT:
1727 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1729 case SCSI_IOCTL_DATA_UNSPECIFIED:
1730 cmd.dxfer_direction = SG_DXFER_NONE;
1733 return STATUS_INVALID_PARAMETER;
1736 io = ioctl(fd, SG_IO, &cmd);
1738 pPacket->ScsiStatus = cmd.status;
1739 pPacket->DataTransferLength = cmd.resid;
1740 pPacket->SenseInfoLength = cmd.sb_len_wr;
1742 ret = CDROM_GetStatusCode(io);
1744 #elif defined HAVE_SCSIREQ_T_CMD
1746 memset(&cmd, 0, sizeof(cmd));
1747 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1749 cmd.cmdlen = pPacket->CdbLength;
1750 cmd.databuf = pPacket->DataBuffer;
1751 cmd.datalen = pPacket->DataTransferLength;
1752 cmd.senselen = pPacket->SenseInfoLength;
1753 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1755 switch (pPacket->DataIn)
1757 case SCSI_IOCTL_DATA_OUT:
1758 cmd.flags |= SCCMD_WRITE;
1760 case SCSI_IOCTL_DATA_IN:
1761 cmd.flags |= SCCMD_READ;
1763 case SCSI_IOCTL_DATA_UNSPECIFIED:
1767 return STATUS_INVALID_PARAMETER;
1770 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1774 case SCCMD_OK: break;
1775 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1777 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1779 case SCCMD_SENSE: break;
1780 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1784 if (pPacket->SenseInfoLength != 0)
1786 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1787 cmd.sense, pPacket->SenseInfoLength);
1790 pPacket->ScsiStatus = cmd.status;
1792 ret = CDROM_GetStatusCode(io);
1794 #elif defined(__APPLE__)
1796 memset(&cmd, 0, sizeof(cmd));
1797 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1799 cmd.cdbSize = pPacket->CdbLength;
1800 cmd.buffer = pPacket->DataBuffer;
1801 cmd.bufferSize = pPacket->DataTransferLength;
1802 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1803 cmd.senseLen = pPacket->SenseInfoLength;
1804 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1806 switch (pPacket->DataIn)
1808 case SCSI_IOCTL_DATA_OUT:
1809 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
1811 case SCSI_IOCTL_DATA_IN:
1812 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
1814 case SCSI_IOCTL_DATA_UNSPECIFIED:
1815 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
1818 return STATUS_INVALID_PARAMETER;
1821 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
1823 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
1825 /* Command failed */
1828 case kSCSITaskStatus_TaskTimeoutOccurred: return STATUS_TIMEOUT;
1830 case kSCSITaskStatus_ProtocolTimeoutOccurred: return STATUS_IO_TIMEOUT;
1832 case kSCSITaskStatus_DeviceNotResponding: return STATUS_DEVICE_BUSY;
1834 case kSCSITaskStatus_DeviceNotPresent:
1835 return STATUS_NO_SUCH_DEVICE;
1837 case kSCSITaskStatus_DeliveryFailure:
1838 return STATUS_DEVICE_PROTOCOL_ERROR;
1840 case kSCSITaskStatus_No_Status:
1842 return STATUS_UNSUCCESSFUL;
1847 if (cmd.status != kSCSITaskStatus_No_Status)
1848 pPacket->ScsiStatus = cmd.status;
1850 ret = CDROM_GetStatusCode(io);
1855 /******************************************************************
1856 * CDROM_ScsiPassThrough
1857 * Implements IOCTL_SCSI_PASS_THROUGH
1860 static NTSTATUS CDROM_ScsiPassThrough(int fd, PSCSI_PASS_THROUGH pPacket)
1862 int ret = STATUS_NOT_SUPPORTED;
1863 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1866 #elif defined HAVE_SCSIREQ_T_CMD
1869 #elif defined __APPLE__
1870 dk_scsi_command_t cmd;
1874 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1875 return STATUS_BUFFER_TOO_SMALL;
1877 if (pPacket->CdbLength > 16)
1878 return STATUS_INVALID_PARAMETER;
1881 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1882 return STATUS_INVALID_PARAMETER;
1883 #elif defined HAVE_REQUEST_SENSE
1884 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1885 return STATUS_INVALID_PARAMETER;
1888 if (pPacket->DataTransferLength > 0 && pPacket->DataBufferOffset < sizeof(SCSI_PASS_THROUGH))
1889 return STATUS_INVALID_PARAMETER;
1891 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1892 RtlZeroMemory(&cmd, sizeof(cmd));
1894 cmd.interface_id = 'S';
1895 cmd.dxfer_len = pPacket->DataTransferLength;
1896 cmd.dxferp = (char*)pPacket + pPacket->DataBufferOffset;
1897 cmd.cmd_len = pPacket->CdbLength;
1898 cmd.cmdp = pPacket->Cdb;
1899 cmd.mx_sb_len = pPacket->SenseInfoLength;
1900 cmd.timeout = pPacket->TimeOutValue*1000;
1902 if(cmd.mx_sb_len > 0)
1903 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1905 switch (pPacket->DataIn)
1907 case SCSI_IOCTL_DATA_IN:
1908 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1910 case SCSI_IOCTL_DATA_OUT:
1911 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1913 case SCSI_IOCTL_DATA_UNSPECIFIED:
1914 cmd.dxfer_direction = SG_DXFER_NONE;
1917 return STATUS_INVALID_PARAMETER;
1920 io = ioctl(fd, SG_IO, &cmd);
1922 pPacket->ScsiStatus = cmd.status;
1923 pPacket->DataTransferLength = cmd.resid;
1924 pPacket->SenseInfoLength = cmd.sb_len_wr;
1926 ret = CDROM_GetStatusCode(io);
1928 #elif defined HAVE_SCSIREQ_T_CMD
1930 memset(&cmd, 0, sizeof(cmd));
1931 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1933 if ( pPacket->DataBufferOffset > 0x1000 )
1935 cmd.databuf = (void*)pPacket->DataBufferOffset;
1939 cmd.databuf = (char*)pPacket + pPacket->DataBufferOffset;
1942 cmd.cmdlen = pPacket->CdbLength;
1943 cmd.datalen = pPacket->DataTransferLength;
1944 cmd.senselen = pPacket->SenseInfoLength;
1945 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1947 switch (pPacket->DataIn)
1949 case SCSI_IOCTL_DATA_OUT:
1950 cmd.flags |= SCCMD_WRITE;
1952 case SCSI_IOCTL_DATA_IN:
1953 cmd.flags |= SCCMD_READ;
1955 case SCSI_IOCTL_DATA_UNSPECIFIED:
1959 return STATUS_INVALID_PARAMETER;
1962 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1966 case SCCMD_OK: break;
1967 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1969 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1971 case SCCMD_SENSE: break;
1972 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1976 if (pPacket->SenseInfoLength != 0)
1978 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1979 cmd.sense, pPacket->SenseInfoLength);
1982 pPacket->ScsiStatus = cmd.status;
1984 ret = CDROM_GetStatusCode(io);
1986 #elif defined(__APPLE__)
1988 memset(&cmd, 0, sizeof(cmd));
1989 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1991 cmd.cdbSize = pPacket->CdbLength;
1992 cmd.buffer = (char*)pPacket + pPacket->DataBufferOffset;
1993 cmd.bufferSize = pPacket->DataTransferLength;
1994 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1995 cmd.senseLen = pPacket->SenseInfoLength;
1996 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1998 switch (pPacket->DataIn)
2000 case SCSI_IOCTL_DATA_OUT:
2001 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
2003 case SCSI_IOCTL_DATA_IN:
2004 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
2006 case SCSI_IOCTL_DATA_UNSPECIFIED:
2007 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
2010 return STATUS_INVALID_PARAMETER;
2013 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
2015 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
2017 /* Command failed */
2020 case kSCSITaskStatus_TaskTimeoutOccurred:
2021 return STATUS_TIMEOUT;
2023 case kSCSITaskStatus_ProtocolTimeoutOccurred:
2024 return STATUS_IO_TIMEOUT;
2026 case kSCSITaskStatus_DeviceNotResponding:
2027 return STATUS_DEVICE_BUSY;
2029 case kSCSITaskStatus_DeviceNotPresent:
2030 return STATUS_NO_SUCH_DEVICE;
2032 case kSCSITaskStatus_DeliveryFailure:
2033 return STATUS_DEVICE_PROTOCOL_ERROR;
2035 case kSCSITaskStatus_No_Status:
2037 return STATUS_UNSUCCESSFUL;
2042 if (cmd.status != kSCSITaskStatus_No_Status)
2043 pPacket->ScsiStatus = cmd.status;
2045 ret = CDROM_GetStatusCode(io);
2050 /******************************************************************
2055 static NTSTATUS CDROM_ScsiGetCaps(int fd, PIO_SCSI_CAPABILITIES caps)
2057 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
2059 #ifdef SG_SCATTER_SZ
2060 caps->Length = sizeof(*caps);
2061 caps->MaximumTransferLength = SG_SCATTER_SZ; /* FIXME */
2062 caps->MaximumPhysicalPages = SG_SCATTER_SZ / getpagesize();
2063 caps->SupportedAsynchronousEvents = TRUE;
2064 caps->AlignmentMask = getpagesize();
2065 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
2066 caps->AdapterScansDown = FALSE; /* FIXME ? */
2067 caps->AdapterUsesPio = FALSE; /* FIXME ? */
2068 ret = STATUS_SUCCESS;
2069 #elif defined __APPLE__
2070 uint64_t bytesr, bytesw, align;
2071 int io = ioctl(fd, DKIOCGETMAXBYTECOUNTREAD, &bytesr);
2072 if (io != 0) return CDROM_GetStatusCode(io);
2073 io = ioctl(fd, DKIOCGETMAXBYTECOUNTWRITE, &bytesw);
2074 if (io != 0) return CDROM_GetStatusCode(io);
2075 io = ioctl(fd, DKIOCGETMINSEGMENTALIGNMENTBYTECOUNT, &align);
2076 if (io != 0) return CDROM_GetStatusCode(io);
2077 caps->Length = sizeof(*caps);
2078 caps->MaximumTransferLength = bytesr < bytesw ? bytesr : bytesw;
2079 caps->MaximumPhysicalPages = caps->MaximumTransferLength / getpagesize();
2080 caps->SupportedAsynchronousEvents = TRUE;
2081 caps->AlignmentMask = align-1;
2082 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
2083 caps->AdapterScansDown = FALSE; /* FIXME ? */
2084 caps->AdapterUsesPio = FALSE; /* FIXME ? */
2085 ret = STATUS_SUCCESS;
2087 FIXME("Unimplemented\n");
2092 /******************************************************************
2095 * implements IOCTL_SCSI_GET_ADDRESS
2097 static NTSTATUS CDROM_GetAddress(int fd, SCSI_ADDRESS* address)
2099 UCHAR portnum, busid, targetid, lun;
2101 address->Length = sizeof(SCSI_ADDRESS);
2102 if ( ! CDROM_GetInterfaceInfo(fd, &portnum, &busid, &targetid, &lun))
2103 return STATUS_NOT_SUPPORTED;
2105 address->PortNumber = portnum; /* primary=0 secondary=1 for ide */
2106 address->PathId = busid; /* always 0 for ide */
2107 address->TargetId = targetid; /* master=0 slave=1 for ide */
2109 return STATUS_SUCCESS;
2112 /******************************************************************
2117 static NTSTATUS DVD_StartSession(int fd, const DVD_SESSION_ID *sid_in, PDVD_SESSION_ID sid_out)
2120 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2121 dvd_authinfo auth_info;
2123 memset( &auth_info, 0, sizeof( auth_info ) );
2124 auth_info.type = DVD_LU_SEND_AGID;
2125 if (sid_in) auth_info.lsa.agid = *(const int*)sid_in; /* ?*/
2127 TRACE("fd 0x%08x\n",fd);
2128 ret =CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2129 *sid_out = auth_info.lsa.agid;
2131 #elif defined(__FreeBSD__) || defined(__NetBSD__)
2132 return STATUS_NOT_SUPPORTED;
2133 #elif defined(__APPLE__)
2134 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2135 dk_dvd_report_key_t dvdrk;
2136 DVDAuthenticationGrantIDInfo agid_info;
2138 dvdrk.format = kDVDKeyFormatAGID_CSS;
2139 dvdrk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2140 if(sid_in) dvdrk.grantID = *(uint8_t*)sid_in; /* ? */
2141 dvdrk.bufferLength = sizeof(DVDAuthenticationGrantIDInfo);
2142 dvdrk.buffer = &agid_info;
2144 ret = CDROM_GetStatusCode(ioctl(fd, DKIOCDVDREPORTKEY, &dvdrk));
2145 *sid_out = agid_info.grantID;
2148 return STATUS_NOT_SUPPORTED;
2152 /******************************************************************
2157 static NTSTATUS DVD_EndSession(int fd, const DVD_SESSION_ID *sid)
2160 dvd_authinfo auth_info;
2162 memset( &auth_info, 0, sizeof( auth_info ) );
2163 auth_info.type = DVD_INVALIDATE_AGID;
2164 auth_info.lsa.agid = *(const int*)sid;
2167 return CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2168 #elif defined(__FreeBSD__) || defined(__NetBSD__)
2169 return STATUS_NOT_SUPPORTED;
2170 #elif defined(__APPLE__)
2171 dk_dvd_send_key_t dvdsk;
2173 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2174 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2175 dvdsk.grantID = (uint8_t)*sid;
2177 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2179 return STATUS_NOT_SUPPORTED;
2183 /******************************************************************
2188 static NTSTATUS DVD_SendKey(int fd, const DVD_COPY_PROTECT_KEY *key)
2191 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2192 dvd_authinfo auth_info;
2194 memset( &auth_info, 0, sizeof( auth_info ) );
2195 switch (key->KeyType)
2197 case DvdChallengeKey:
2198 auth_info.type = DVD_HOST_SEND_CHALLENGE;
2199 auth_info.hsc.agid = (int)key->SessionId;
2200 TRACE("DvdChallengeKey ioc 0x%x\n", DVD_AUTH );
2201 memcpy( auth_info.hsc.chal, key->KeyData, DVD_CHALLENGE_SIZE );
2202 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2205 auth_info.type = DVD_HOST_SEND_KEY2;
2206 auth_info.hsk.agid = (int)key->SessionId;
2208 memcpy( auth_info.hsk.key, key->KeyData, DVD_KEY_SIZE );
2210 TRACE("DvdBusKey2\n");
2211 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2215 FIXME("Unknown Keytype 0x%x\n",key->KeyType);
2218 #elif defined(__APPLE__)
2219 dk_dvd_send_key_t dvdsk;
2222 DVDChallengeKeyInfo chal;
2226 switch(key->KeyType)
2228 case DvdChallengeKey:
2229 dvdsk.format = kDVDKeyFormatChallengeKey;
2230 dvdsk.bufferLength = sizeof(key_desc.chal);
2231 dvdsk.buffer = &key_desc.chal;
2232 OSWriteBigInt16(key_desc.chal.dataLength, 0, key->KeyLength);
2233 memcpy(key_desc.chal.challengeKeyValue, key->KeyData, key->KeyLength);
2236 dvdsk.format = kDVDKeyFormatKey2;
2237 dvdsk.bufferLength = sizeof(key_desc.key2);
2238 dvdsk.buffer = &key_desc.key2;
2239 OSWriteBigInt16(key_desc.key2.dataLength, 0, key->KeyLength);
2240 memcpy(key_desc.key2.key2Value, key->KeyData, key->KeyLength);
2242 case DvdInvalidateAGID:
2243 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2250 ERR("attempted to write read-only key type 0x%x\n", key->KeyType);
2251 return STATUS_NOT_SUPPORTED;
2253 FIXME("DvdSetRpcKey NIY\n");
2254 return STATUS_NOT_SUPPORTED;
2256 FIXME("got unknown key type 0x%x\n", key->KeyType);
2257 return STATUS_NOT_SUPPORTED;
2259 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2260 dvdsk.grantID = (uint8_t)key->SessionId;
2262 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2264 FIXME("unsupported on this platform\n");
2265 return STATUS_NOT_SUPPORTED;
2269 /******************************************************************
2274 static NTSTATUS DVD_ReadKey(int fd, PDVD_COPY_PROTECT_KEY key)
2277 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2279 dvd_authinfo auth_info;
2281 memset( &dvd, 0, sizeof( dvd_struct ) );
2282 memset( &auth_info, 0, sizeof( auth_info ) );
2283 switch (key->KeyType)
2287 dvd.type = DVD_STRUCT_DISCKEY;
2288 dvd.disckey.agid = (int)key->SessionId;
2289 memset( dvd.disckey.value, 0, DVD_DISCKEY_SIZE );
2291 TRACE("DvdDiskKey\n");
2292 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2293 if (ret == STATUS_SUCCESS)
2294 memcpy(key->KeyData,dvd.disckey.value,DVD_DISCKEY_SIZE);
2297 auth_info.type = DVD_LU_SEND_TITLE_KEY;
2298 auth_info.lstk.agid = (int)key->SessionId;
2299 auth_info.lstk.lba = (int)(key->Parameters.TitleOffset.QuadPart>>11);
2300 TRACE("DvdTitleKey session %d Quadpart 0x%08lx offset 0x%08x\n",
2301 (int)key->SessionId, (long)key->Parameters.TitleOffset.QuadPart,
2302 auth_info.lstk.lba);
2303 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2304 if (ret == STATUS_SUCCESS)
2305 memcpy(key->KeyData, auth_info.lstk.title_key, DVD_KEY_SIZE );
2307 case DvdChallengeKey:
2309 auth_info.type = DVD_LU_SEND_CHALLENGE;
2310 auth_info.lsc.agid = (int)key->SessionId;
2312 TRACE("DvdChallengeKey\n");
2313 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2314 if (ret == STATUS_SUCCESS)
2315 memcpy( key->KeyData, auth_info.lsc.chal, DVD_CHALLENGE_SIZE );
2318 auth_info.type = DVD_LU_SEND_ASF;
2320 auth_info.lsasf.asf=((PDVD_ASF)key->KeyData)->SuccessFlag;
2321 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2322 ((PDVD_ASF)key->KeyData)->SuccessFlag = auth_info.lsasf.asf;
2325 auth_info.type = DVD_LU_SEND_KEY1;
2326 auth_info.lsk.agid = (int)key->SessionId;
2328 TRACE("DvdBusKey1\n");
2329 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2331 if (ret == STATUS_SUCCESS)
2332 memcpy( key->KeyData, auth_info.lsk.key, DVD_KEY_SIZE );
2335 auth_info.type = DVD_LU_SEND_RPC_STATE;
2337 TRACE("DvdGetRpcKey\n");
2338 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2340 if (ret == STATUS_SUCCESS)
2342 ((PDVD_RPC_KEY)key->KeyData)->TypeCode = auth_info.lrpcs.type;
2343 ((PDVD_RPC_KEY)key->KeyData)->RegionMask = auth_info.lrpcs.region_mask;
2344 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme = auth_info.lrpcs.rpc_scheme;
2345 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable = auth_info.lrpcs.ucca;
2346 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable = auth_info.lrpcs.vra;
2350 FIXME("Unknown keytype 0x%x\n",key->KeyType);
2353 #elif defined(__FreeBSD__) || defined(__NetBSD__)
2355 return STATUS_NOT_SUPPORTED;
2356 #elif defined(__APPLE__)
2359 dk_dvd_report_key_t key;
2360 dk_dvd_read_structure_t disk_key;
2364 DVDDiscKeyInfo disk_key;
2365 DVDChallengeKeyInfo chal;
2367 DVDTitleKeyInfo title;
2368 DVDAuthenticationSuccessFlagInfo asf;
2369 DVDRegionPlaybackControlInfo rpc;
2371 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2373 switch(key->KeyType)
2375 case DvdChallengeKey:
2376 ioc.key.format = kDVDKeyFormatChallengeKey;
2377 ioc.key.grantID = (uint8_t)key->SessionId;
2378 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2379 ioc.key.bufferLength = sizeof(desc.chal);
2380 ioc.key.buffer = &desc.chal;
2381 OSWriteBigInt16(desc.chal.dataLength, 0, key->KeyLength);
2384 ioc.key.format = kDVDKeyFormatKey1;
2385 ioc.key.grantID = (uint8_t)key->SessionId;
2386 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2387 ioc.key.bufferLength = sizeof(desc.key1);
2388 ioc.key.buffer = &desc.key1;
2389 OSWriteBigInt16(desc.key1.dataLength, 0, key->KeyLength);
2392 ioc.key.format = kDVDKeyFormatTitleKey;
2393 ioc.key.grantID = (uint8_t)key->SessionId;
2394 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2395 ioc.key.bufferLength = sizeof(desc.title);
2396 ioc.key.buffer = &desc.title;
2397 ioc.key.address = (uint32_t)(key->Parameters.TitleOffset.QuadPart>>11);
2398 OSWriteBigInt16(desc.title.dataLength, 0, key->KeyLength);
2401 ioc.key.format = kDVDKeyFormatASF;
2402 ioc.key.grantID = (uint8_t)key->SessionId;
2403 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2404 ioc.key.bufferLength = sizeof(desc.asf);
2405 ioc.key.buffer = &desc.asf;
2406 OSWriteBigInt16(desc.asf.dataLength, 0, key->KeyLength);
2409 ioc.key.format = kDVDKeyFormatRegionState;
2410 ioc.key.grantID = (uint8_t)key->SessionId;
2411 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2412 ioc.key.bufferLength = sizeof(desc.rpc);
2413 ioc.key.buffer = &desc.rpc;
2414 OSWriteBigInt16(desc.rpc.dataLength, 0, key->KeyLength);
2417 ioc.disk_key.format = kDVDStructureFormatDiscKeyInfo;
2418 ioc.disk_key.grantID = (uint8_t)key->SessionId;
2419 ioc.disk_key.bufferLength = sizeof(desc.disk_key);
2420 ioc.disk_key.buffer = &desc.disk_key;
2422 case DvdInvalidateAGID:
2423 ioc.key.format = kDVDKeyFormatAGID_Invalidate;
2424 ioc.key.grantID = (uint8_t)key->SessionId;
2425 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2429 ERR("attempted to read write-only key type 0x%x\n", key->KeyType);
2430 return STATUS_NOT_SUPPORTED;
2432 FIXME("got unknown key type 0x%x\n", key->KeyType);
2433 return STATUS_NOT_SUPPORTED;
2436 ret = CDROM_GetStatusCode(ioctl(fd, (key->KeyType == DvdDiskKey ? DKIOCDVDREADSTRUCTURE : DKIOCDVDREPORTKEY), &ioc));
2438 if (ret == STATUS_SUCCESS)
2440 switch(key->KeyType)
2442 case DvdChallengeKey:
2443 key->KeyLength = OSReadBigInt16(desc.chal.dataLength, 0);
2444 memcpy(key->KeyData, desc.chal.challengeKeyValue, key->KeyLength);
2447 key->KeyLength = OSReadBigInt16(desc.key1.dataLength, 0);
2448 memcpy(key->KeyData, desc.key1.key1Value, key->KeyLength);
2451 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2452 memcpy(key->KeyData, desc.title.titleKeyValue, key->KeyLength);
2456 /*key->KeyFlags |= DVD_COPYRIGHTED;*/
2457 if (desc.title.CP_SEC) key->KeyFlags |= DVD_SECTOR_PROTECTED;
2458 /*else key->KeyFlags |= DVD_SECTOR_NOT_PROTECTED;*/
2460 switch (desc.title.CGMS)
2463 key->KeyFlags |= DVD_CGMS_COPY_PERMITTED;
2466 key->KeyFlags |= DVD_CGMS_COPY_ONCE;
2469 key->KeyFlags |= DVD_CGMS_NO_COPY;
2473 } /*else key->KeyFlags |= DVD_NOT_COPYRIGHTED;*/
2476 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2477 ((PDVD_ASF)key->KeyData)->SuccessFlag = desc.asf.successFlag;
2480 key->KeyLength = OSReadBigInt16(desc.rpc.dataLength, 0);
2481 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable =
2482 desc.rpc.numberUserResets;
2483 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable =
2484 desc.rpc.numberVendorResets;
2485 ((PDVD_RPC_KEY)key->KeyData)->TypeCode =
2487 ((PDVD_RPC_KEY)key->KeyData)->RegionMask =
2488 desc.rpc.driveRegion;
2489 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme =
2492 key->KeyLength = OSReadBigInt16(desc.disk_key.dataLength, 0);
2493 memcpy(key->KeyData, desc.disk_key.discKeyStructures, key->KeyLength);
2497 case DvdInvalidateAGID:
2499 /* Silence warning */
2506 return STATUS_NOT_SUPPORTED;
2510 /******************************************************************
2513 * This IOCTL combines information from both IOCTL_DVD_READ_KEY
2514 * with key type DvdGetRpcKey and IOCTL_DVD_READ_STRUCTURE with
2515 * structure type DvdCopyrightInformation into one structure.
2517 static NTSTATUS DVD_GetRegion(int fd, PDVD_REGION region)
2520 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2522 dvd_authinfo auth_info;
2524 dvd.type = DVD_STRUCT_COPYRIGHT;
2525 dvd.copyright.layer_num = 0;
2526 auth_info.type = DVD_LU_SEND_RPC_STATE;
2528 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2530 if (ret == STATUS_SUCCESS)
2532 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2534 if (ret == STATUS_SUCCESS)
2536 region->CopySystem = dvd.copyright.cpst;
2537 region->RegionData = dvd.copyright.rmi;
2538 region->SystemRegion = auth_info.lrpcs.region_mask;
2539 region->ResetCount = auth_info.lrpcs.ucca;
2543 #elif defined(__FreeBSD__) || defined(__NetBSD__)
2545 return STATUS_NOT_SUPPORTED;
2546 #elif defined(__APPLE__)
2547 dk_dvd_report_key_t key;
2548 dk_dvd_read_structure_t dvd;
2549 DVDRegionPlaybackControlInfo rpc;
2550 DVDCopyrightInfo copy;
2551 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2553 key.format = kDVDKeyFormatRegionState;
2554 key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2555 key.bufferLength = sizeof(rpc);
2557 dvd.format = kDVDStructureFormatCopyrightInfo;
2558 dvd.bufferLength = sizeof(copy);
2561 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREPORTKEY, &key ));
2563 if (ret == STATUS_SUCCESS)
2565 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREADSTRUCTURE, &dvd ));
2567 if (ret == STATUS_SUCCESS)
2569 region->CopySystem = copy.copyrightProtectionSystemType;
2570 region->RegionData = copy.regionMask;
2571 region->SystemRegion = rpc.driveRegion;
2572 region->ResetCount = rpc.numberUserResets;
2578 return STATUS_NOT_SUPPORTED;
2582 /******************************************************************
2587 static NTSTATUS DVD_ReadStructure(int dev, const DVD_READ_STRUCTURE *structure, PDVD_LAYER_DESCRIPTOR layer)
2589 #ifdef DVD_READ_STRUCT
2590 /* dvd_struct is not defined consistently across platforms */
2593 struct dvd_physical physical;
2594 struct dvd_copyright copyright;
2595 struct dvd_disckey disckey;
2597 struct dvd_manufact manufact;
2600 if (structure->BlockByteOffset.u.HighPart || structure->BlockByteOffset.u.LowPart)
2601 FIXME(": BlockByteOffset is not handled\n");
2603 switch (structure->Format)
2605 case DvdPhysicalDescriptor:
2606 s.physical.type = DVD_STRUCT_PHYSICAL;
2607 s.physical.layer_num = structure->LayerNumber;
2610 case DvdCopyrightDescriptor:
2611 s.copyright.type = DVD_STRUCT_COPYRIGHT;
2612 s.copyright.layer_num = structure->LayerNumber;
2615 case DvdDiskKeyDescriptor:
2616 s.disckey.type = DVD_STRUCT_DISCKEY;
2617 s.disckey.agid = structure->SessionId;
2620 case DvdBCADescriptor:
2621 s.bca.type = DVD_STRUCT_BCA;
2624 case DvdManufacturerDescriptor:
2625 s.manufact.type = DVD_STRUCT_MANUFACT;
2626 s.manufact.layer_num = structure->LayerNumber;
2629 case DvdMaxDescriptor: /* This is not a real request, no matter what MSDN says! */
2631 return STATUS_INVALID_PARAMETER;
2634 if (ioctl(dev, DVD_READ_STRUCT, &s) < 0)
2635 return STATUS_INVALID_PARAMETER;
2637 switch (structure->Format)
2639 case DvdPhysicalDescriptor:
2641 internal_dvd_layer_descriptor *p = (internal_dvd_layer_descriptor *) layer;
2642 struct dvd_layer *l = &s.physical.layer[s.physical.layer_num];
2644 p->DataLength[0] = 2;
2645 p->DataLength[1] = 8;
2646 p->Reserved0[0] = 0;
2647 p->Reserved0[1] = 0;
2648 p->BookVersion = l->book_version;
2649 p->BookType = l->book_type;
2650 p->MinimumRate = l->min_rate;
2651 p->DiskSize = l->disc_size;
2652 p->LayerType = l->layer_type;
2653 p->TrackPath = l->track_path;
2654 p->NumberOfLayers = l->nlayers;
2656 p->TrackDensity = l->track_density;
2657 p->LinearDensity = l->linear_density;
2658 p->StartingDataSector = l->start_sector;
2659 p->EndDataSector = l->end_sector;
2660 p->EndLayerZeroSector = l->end_sector_l0;
2662 p->BCAFlag = l->bca;
2667 case DvdCopyrightDescriptor:
2669 PDVD_COPYRIGHT_DESCRIPTOR p = (PDVD_COPYRIGHT_DESCRIPTOR) layer;
2671 p->CopyrightProtectionType = s.copyright.cpst;
2672 p->RegionManagementInformation = s.copyright.rmi;
2677 case DvdDiskKeyDescriptor:
2679 PDVD_DISK_KEY_DESCRIPTOR p = (PDVD_DISK_KEY_DESCRIPTOR) layer;
2681 memcpy(p->DiskKeyData, s.disckey.value, 2048);
2685 case DvdBCADescriptor:
2687 PDVD_BCA_DESCRIPTOR p = (PDVD_BCA_DESCRIPTOR) layer;
2689 memcpy(p->BCAInformation, s.bca.value, s.bca.len);
2693 case DvdManufacturerDescriptor:
2695 PDVD_MANUFACTURER_DESCRIPTOR p = (PDVD_MANUFACTURER_DESCRIPTOR) layer;
2697 memcpy(p->ManufacturingInformation, s.manufact.value, 2048);
2701 case DvdMaxDescriptor: /* Suppress warning */
2704 #elif defined(__APPLE__)
2705 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2706 dk_dvd_read_structure_t dvdrs;
2709 DVDPhysicalFormatInfo phys;
2710 DVDCopyrightInfo copy;
2711 DVDDiscKeyInfo disk_key;
2712 DVDManufacturingInfo manf;
2716 PDVD_LAYER_DESCRIPTOR layer;
2717 internal_dvd_layer_descriptor *xlayer;
2718 PDVD_COPYRIGHT_DESCRIPTOR copy;
2719 PDVD_DISK_KEY_DESCRIPTOR disk_key;
2720 PDVD_MANUFACTURER_DESCRIPTOR manf;
2723 nt_desc.layer = layer;
2724 dvdrs.address = (uint32_t)(structure->BlockByteOffset.QuadPart>>11);
2725 dvdrs.grantID = (uint8_t)structure->SessionId;
2726 dvdrs.layer = structure->LayerNumber;
2727 switch(structure->Format)
2729 case DvdPhysicalDescriptor:
2730 dvdrs.format = kDVDStructureFormatPhysicalFormatInfo;
2731 dvdrs.bufferLength = sizeof(desc.phys);
2732 dvdrs.buffer = &desc.phys;
2735 case DvdCopyrightDescriptor:
2736 dvdrs.format = kDVDStructureFormatCopyrightInfo;
2737 dvdrs.bufferLength = sizeof(desc.copy);
2738 dvdrs.buffer = &desc.copy;
2741 case DvdDiskKeyDescriptor:
2742 dvdrs.format = kDVDStructureFormatDiscKeyInfo;
2743 dvdrs.bufferLength = sizeof(desc.disk_key);
2744 dvdrs.buffer = &desc.disk_key;
2747 case DvdBCADescriptor:
2748 FIXME("DvdBCADescriptor NIY\n");
2749 return STATUS_NOT_SUPPORTED;
2751 case DvdManufacturerDescriptor:
2752 dvdrs.format = kDVDStructureFormatManufacturingInfo;
2753 dvdrs.bufferLength = sizeof(desc.manf);
2754 dvdrs.buffer = &desc.manf;
2757 case DvdMaxDescriptor:
2759 FIXME("got unknown structure type 0x%x\n", structure->Format);
2760 return STATUS_NOT_SUPPORTED;
2762 ret = CDROM_GetStatusCode(ioctl(dev, DKIOCDVDREADSTRUCTURE, &dvdrs));
2763 if(ret == STATUS_SUCCESS)
2765 switch(structure->Format)
2767 case DvdPhysicalDescriptor:
2768 nt_desc.xlayer->DataLength[0] = 2;
2769 nt_desc.xlayer->DataLength[1] = 8;
2770 nt_desc.xlayer->Reserved0[0] = 0;
2771 nt_desc.xlayer->Reserved0[1] = 0;
2772 nt_desc.xlayer->BookVersion = desc.phys.partVersion;
2773 nt_desc.xlayer->BookType = desc.phys.bookType;
2774 nt_desc.xlayer->MinimumRate = desc.phys.minimumRate;
2775 nt_desc.xlayer->DiskSize = desc.phys.discSize;
2776 nt_desc.xlayer->LayerType = desc.phys.layerType;
2777 nt_desc.xlayer->TrackPath = desc.phys.trackPath;
2778 nt_desc.xlayer->NumberOfLayers = desc.phys.numberOfLayers;
2779 nt_desc.xlayer->Reserved1 = 0;
2780 nt_desc.xlayer->TrackDensity = desc.phys.trackDensity;
2781 nt_desc.xlayer->LinearDensity = desc.phys.linearDensity;
2782 nt_desc.xlayer->BCAFlag = desc.phys.bcaFlag;
2783 nt_desc.xlayer->StartingDataSector = OSReadBigInt32(&desc.phys.zero1, 0);
2784 nt_desc.xlayer->EndDataSector = OSReadBigInt32(&desc.phys.zero2, 0);
2785 nt_desc.xlayer->EndLayerZeroSector = OSReadBigInt32(&desc.phys.zero3, 0);
2786 nt_desc.xlayer->Reserved5 = 0;
2787 nt_desc.xlayer->Reserved6 = 0;
2790 case DvdCopyrightDescriptor:
2791 nt_desc.copy->CopyrightProtectionType =
2792 desc.copy.copyrightProtectionSystemType;
2793 nt_desc.copy->RegionManagementInformation =
2794 desc.copy.regionMask;
2795 nt_desc.copy->Reserved = 0;
2798 case DvdDiskKeyDescriptor:
2800 nt_desc.disk_key->DiskKeyData,
2801 desc.disk_key.discKeyStructures,
2805 case DvdManufacturerDescriptor:
2807 nt_desc.manf->ManufacturingInformation,
2808 desc.manf.discManufacturingInfo,
2812 case DvdBCADescriptor:
2813 case DvdMaxDescriptor:
2815 /* Silence warning */
2823 return STATUS_SUCCESS;
2827 /******************************************************************
2829 * Implements the IOCTL_GET_INQUIRY_DATA ioctl.
2830 * Returns Inquiry data for all devices on the specified scsi bus
2831 * Returns STATUS_BUFFER_TOO_SMALL if the output buffer is too small,
2832 * STATUS_INVALID_DEVICE_REQUEST if the given handle isn't to a SCSI device,
2833 * or STATUS_NOT_SUPPORTED if the OS driver is too old
2835 static NTSTATUS GetInquiryData(int fd, PSCSI_ADAPTER_BUS_INFO BufferOut, DWORD OutBufferSize)
2837 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
2838 PSCSI_INQUIRY_DATA pInquiryData = NULL;
2839 UCHAR sense_buffer[32];
2842 UCHAR inquiry[INQ_CMD_LEN] = {INQUIRY, 0, 0, 0, INQ_REPLY_LEN, 0};
2844 /* Check we have a SCSI device and a supported driver */
2845 if(ioctl(fd, SG_GET_VERSION_NUM, &version) != 0)
2847 WARN("IOCTL_SCSI_GET_INQUIRY_DATA sg driver is not loaded\n");
2848 return STATUS_INVALID_DEVICE_REQUEST;
2850 if(version < 30000 )
2851 return STATUS_NOT_SUPPORTED;
2853 /* FIXME: Enumerate devices on the bus */
2854 BufferOut->NumberOfBuses = 1;
2855 BufferOut->BusData[0].NumberOfLogicalUnits = 1;
2856 BufferOut->BusData[0].InquiryDataOffset = sizeof(SCSI_ADAPTER_BUS_INFO);
2858 pInquiryData = (PSCSI_INQUIRY_DATA)(BufferOut + 1);
2860 RtlZeroMemory(&iocmd, sizeof(iocmd));
2861 iocmd.interface_id = 'S';
2862 iocmd.cmd_len = sizeof(inquiry);
2863 iocmd.mx_sb_len = sizeof(sense_buffer);
2864 iocmd.dxfer_direction = SG_DXFER_FROM_DEV;
2865 iocmd.dxfer_len = INQ_REPLY_LEN;
2866 iocmd.dxferp = pInquiryData->InquiryData;
2867 iocmd.cmdp = inquiry;
2868 iocmd.sbp = sense_buffer;
2869 iocmd.timeout = 1000;
2871 iochk = ioctl(fd, SG_IO, &iocmd);
2873 WARN("ioctl SG_IO returned %d, error (%s)\n", iochk, strerror(errno));
2875 CDROM_GetInterfaceInfo(fd, &BufferOut->BusData[0].InitiatorBusId, &pInquiryData->PathId, &pInquiryData->TargetId, &pInquiryData->Lun);
2876 pInquiryData->DeviceClaimed = TRUE;
2877 pInquiryData->InquiryDataLength = INQ_REPLY_LEN;
2878 pInquiryData->NextInquiryDataOffset = 0;
2879 return STATUS_SUCCESS;
2881 FIXME("not implemented for nonlinux\n");
2882 return STATUS_NOT_SUPPORTED;
2886 /******************************************************************
2887 * CDROM_DeviceIoControl
2891 NTSTATUS CDROM_DeviceIoControl(HANDLE hDevice,
2892 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
2893 PVOID UserApcContext,
2894 PIO_STATUS_BLOCK piosb,
2895 ULONG dwIoControlCode,
2896 LPVOID lpInBuffer, DWORD nInBufferSize,
2897 LPVOID lpOutBuffer, DWORD nOutBufferSize)
2900 NTSTATUS status = STATUS_SUCCESS;
2901 int fd, needs_close, dev;
2903 TRACE("%p %s %p %d %p %d %p\n",
2904 hDevice, iocodex(dwIoControlCode), lpInBuffer, nInBufferSize,
2905 lpOutBuffer, nOutBufferSize, piosb);
2907 piosb->Information = 0;
2909 if ((status = server_get_unix_fd( hDevice, 0, &fd, &needs_close, NULL, NULL )))
2911 if (status == STATUS_BAD_DEVICE_TYPE) return status; /* no associated fd */
2915 if ((status = CDROM_Open(fd, &dev)))
2917 if (needs_close) close( fd );
2925 /* This is ugly as hell, but Mac OS is unable to do anything from the
2926 * partition fd, it wants an fd for the whole device, and it sometimes
2927 * also requires the device fd to be closed first, so we have to close
2928 * the handle that the caller gave us.
2929 * Also for some reason it wants the fd to be closed before we even
2930 * open the parent if we're trying to eject the disk.
2932 if ((status = get_parent_device( fd, name, sizeof(name) ))) goto error;
2933 if (dwIoControlCode == IOCTL_STORAGE_EJECT_MEDIA)
2935 if (needs_close) close( fd );
2936 TRACE("opening parent %s\n", name );
2937 if ((fd = open( name, O_RDONLY )) == -1)
2939 status = FILE_GetNtStatus();
2946 switch (dwIoControlCode)
2948 case IOCTL_STORAGE_CHECK_VERIFY:
2949 case IOCTL_CDROM_CHECK_VERIFY:
2950 case IOCTL_DISK_CHECK_VERIFY:
2952 CDROM_ClearCacheEntry(dev);
2953 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2954 status = STATUS_INVALID_PARAMETER;
2955 else status = CDROM_Verify(dev, fd);
2958 /* EPP case IOCTL_STORAGE_CHECK_VERIFY2: */
2960 /* EPP case IOCTL_STORAGE_FIND_NEW_DEVICES: */
2961 /* EPP case IOCTL_CDROM_FIND_NEW_DEVICES: */
2963 case IOCTL_STORAGE_LOAD_MEDIA:
2964 case IOCTL_CDROM_LOAD_MEDIA:
2966 CDROM_ClearCacheEntry(dev);
2967 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2968 status = STATUS_INVALID_PARAMETER;
2969 else status = CDROM_SetTray(fd, FALSE);
2971 case IOCTL_STORAGE_EJECT_MEDIA:
2973 CDROM_ClearCacheEntry(dev);
2974 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2975 status = STATUS_INVALID_PARAMETER;
2977 status = CDROM_SetTray(fd, TRUE);
2980 case IOCTL_CDROM_MEDIA_REMOVAL:
2981 case IOCTL_DISK_MEDIA_REMOVAL:
2982 case IOCTL_STORAGE_MEDIA_REMOVAL:
2983 case IOCTL_STORAGE_EJECTION_CONTROL:
2984 /* FIXME the last ioctl:s is not the same as the two others...
2985 * lockcount/owner should be handled */
2987 CDROM_ClearCacheEntry(dev);
2988 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2989 else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) status = STATUS_BUFFER_TOO_SMALL;
2990 else status = CDROM_ControlEjection(fd, lpInBuffer);
2993 case IOCTL_STORAGE_GET_MEDIA_TYPES:
2994 case IOCTL_STORAGE_GET_MEDIA_TYPES_EX:
2995 sz = sizeof(GET_MEDIA_TYPES);
2996 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2997 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2998 else status = CDROM_GetMediaType(dev, lpOutBuffer);
3001 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
3002 sz = sizeof(STORAGE_DEVICE_NUMBER);
3003 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3004 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3005 else status = CDROM_GetDeviceNumber(dev, lpOutBuffer);
3008 case IOCTL_STORAGE_RESET_DEVICE:
3010 CDROM_ClearCacheEntry(dev);
3011 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3012 status = STATUS_INVALID_PARAMETER;
3013 else status = CDROM_ResetAudio(fd);
3016 case IOCTL_CDROM_GET_CONTROL:
3017 sz = sizeof(CDROM_AUDIO_CONTROL);
3018 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3019 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3020 else status = CDROM_GetControl(dev, fd, lpOutBuffer);
3023 case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
3024 sz = sizeof(DISK_GEOMETRY);
3025 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3026 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3027 else status = CDROM_GetDriveGeometry(dev, fd, lpOutBuffer);
3030 case IOCTL_CDROM_DISK_TYPE:
3031 sz = sizeof(CDROM_DISK_DATA);
3032 /* CDROM_ClearCacheEntry(dev); */
3033 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3034 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3035 else status = CDROM_GetDiskData(dev, fd, lpOutBuffer);
3038 /* EPP case IOCTL_CDROM_GET_LAST_SESSION: */
3040 case IOCTL_CDROM_READ_Q_CHANNEL:
3041 sz = sizeof(SUB_Q_CHANNEL_DATA);
3042 if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT))
3043 status = STATUS_INVALID_PARAMETER;
3044 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3045 else status = CDROM_ReadQChannel(dev, fd, lpInBuffer, lpOutBuffer);
3048 case IOCTL_CDROM_READ_TOC:
3049 sz = sizeof(CDROM_TOC);
3050 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3051 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3052 else status = CDROM_ReadTOC(dev, fd, lpOutBuffer);
3055 /* EPP case IOCTL_CDROM_READ_TOC_EX: */
3057 case IOCTL_CDROM_PAUSE_AUDIO:
3059 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3060 status = STATUS_INVALID_PARAMETER;
3061 else status = CDROM_PauseAudio(fd);
3063 case IOCTL_CDROM_PLAY_AUDIO_MSF:
3065 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3066 else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
3067 else status = CDROM_PlayAudioMSF(fd, lpInBuffer);
3069 case IOCTL_CDROM_RESUME_AUDIO:
3071 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3072 status = STATUS_INVALID_PARAMETER;
3073 else status = CDROM_ResumeAudio(fd);
3075 case IOCTL_CDROM_SEEK_AUDIO_MSF:
3077 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3078 else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
3079 else status = CDROM_SeekAudioMSF(dev, fd, lpInBuffer);
3081 case IOCTL_CDROM_STOP_AUDIO:
3083 CDROM_ClearCacheEntry(dev); /* Maybe intention is to change media */
3084 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3085 status = STATUS_INVALID_PARAMETER;
3086 else status = CDROM_StopAudio(fd);
3088 case IOCTL_CDROM_GET_VOLUME:
3089 sz = sizeof(VOLUME_CONTROL);
3090 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3091 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3092 else status = CDROM_GetVolume(fd, lpOutBuffer);
3094 case IOCTL_CDROM_SET_VOLUME:
3096 CDROM_ClearCacheEntry(dev);
3097 if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
3098 status = STATUS_INVALID_PARAMETER;
3099 else status = CDROM_SetVolume(fd, lpInBuffer);
3101 case IOCTL_CDROM_RAW_READ:
3103 if (nInBufferSize < sizeof(RAW_READ_INFO)) status = STATUS_INVALID_PARAMETER;
3104 else if (lpOutBuffer == NULL) status = STATUS_BUFFER_TOO_SMALL;
3105 else status = CDROM_RawRead(fd, lpInBuffer, lpOutBuffer,
3106 nOutBufferSize, &sz);
3108 case IOCTL_SCSI_GET_ADDRESS:
3109 sz = sizeof(SCSI_ADDRESS);
3110 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3111 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3112 else status = CDROM_GetAddress(fd, lpOutBuffer);
3114 case IOCTL_SCSI_PASS_THROUGH_DIRECT:
3115 sz = sizeof(SCSI_PASS_THROUGH_DIRECT);
3116 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3117 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH_DIRECT)) status = STATUS_BUFFER_TOO_SMALL;
3118 else status = CDROM_ScsiPassThroughDirect(fd, lpOutBuffer);
3120 case IOCTL_SCSI_PASS_THROUGH:
3121 sz = sizeof(SCSI_PASS_THROUGH);
3122 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3123 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH)) status = STATUS_BUFFER_TOO_SMALL;
3124 else status = CDROM_ScsiPassThrough(fd, lpOutBuffer);
3126 case IOCTL_SCSI_GET_CAPABILITIES:
3127 sz = sizeof(IO_SCSI_CAPABILITIES);
3128 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3129 else if (nOutBufferSize < sizeof(IO_SCSI_CAPABILITIES)) status = STATUS_BUFFER_TOO_SMALL;
3130 else status = CDROM_ScsiGetCaps(fd, lpOutBuffer);
3132 case IOCTL_DVD_START_SESSION:
3133 sz = sizeof(DVD_SESSION_ID);
3134 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3135 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3138 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3139 *(PDVD_SESSION_ID)lpOutBuffer);
3140 status = DVD_StartSession(fd, lpInBuffer, lpOutBuffer);
3141 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3142 *(PDVD_SESSION_ID)lpOutBuffer);
3145 case IOCTL_DVD_END_SESSION:
3146 sz = sizeof(DVD_SESSION_ID);
3147 if ((lpInBuffer == NULL) || (nInBufferSize < sz))status = STATUS_INVALID_PARAMETER;
3148 else status = DVD_EndSession(fd, lpInBuffer);
3150 case IOCTL_DVD_SEND_KEY:
3153 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3154 status = STATUS_INVALID_PARAMETER;
3157 TRACE("doing DVD_SendKey\n");
3158 status = DVD_SendKey(fd, lpInBuffer);
3161 case IOCTL_DVD_READ_KEY:
3163 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3164 status = STATUS_INVALID_PARAMETER;
3165 else if (lpInBuffer !=lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
3168 TRACE("doing DVD_READ_KEY\n");
3169 sz = ((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength;
3170 status = DVD_ReadKey(fd, lpInBuffer);
3173 case IOCTL_DVD_GET_REGION:
3174 sz = sizeof(DVD_REGION);
3175 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3176 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3177 TRACE("doing DVD_Get_REGION\n");
3178 status = DVD_GetRegion(fd, lpOutBuffer);
3180 case IOCTL_DVD_READ_STRUCTURE:
3181 sz = sizeof(DVD_LAYER_DESCRIPTOR);
3182 if (lpInBuffer == NULL || nInBufferSize != sizeof(DVD_READ_STRUCTURE)) status = STATUS_INVALID_PARAMETER;
3183 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3186 TRACE("doing DVD_READ_STRUCTURE\n");
3187 status = DVD_ReadStructure(fd, lpInBuffer, lpOutBuffer);
3191 case IOCTL_SCSI_GET_INQUIRY_DATA:
3193 status = GetInquiryData(fd, lpOutBuffer, nOutBufferSize);
3197 if (needs_close) close( fd );
3198 return STATUS_NOT_SUPPORTED;
3200 if (needs_close) close( fd );
3202 piosb->u.Status = status;
3203 piosb->Information = sz;
3204 if (hEvent) NtSetEvent(hEvent, NULL);