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 */
53 #ifdef HAVE_SCSI_SCSI_IOCTL_H
54 # include <scsi/scsi_ioctl.h>
56 #ifdef HAVE_LINUX_MAJOR_H
57 # include <linux/major.h>
59 #ifdef HAVE_LINUX_HDREG_H
60 # include <linux/hdreg.h>
62 #ifdef HAVE_LINUX_PARAM_H
63 # include <linux/param.h>
65 #ifdef HAVE_LINUX_CDROM_H
66 # include <linux/cdrom.h>
68 #ifdef HAVE_LINUX_UCDROM_H
69 # include <linux/ucdrom.h>
71 #ifdef HAVE_SYS_CDIO_H
72 # include <sys/cdio.h>
74 #ifdef HAVE_SYS_SCSIIO_H
75 # include <sys/scsiio.h>
78 #ifdef HAVE_IOKIT_IOKITLIB_H
79 # include <sys/disk.h>
80 # include <IOKit/IOKitLib.h>
81 # include <IOKit/storage/IOMedia.h>
82 # include <IOKit/storage/IOCDMediaBSDClient.h>
83 # include <IOKit/scsi/SCSICmds_REQUEST_SENSE_Defs.h>
84 # define SENSEBUFLEN kSenseDefaultSize
87 #define NONAMELESSUNION
88 #define NONAMELESSSTRUCT
90 #define WIN32_NO_STATUS
96 #include "ddk/ntddcdvd.h"
98 #include "ntdll_misc.h"
99 #include "wine/server.h"
100 #include "wine/library.h"
101 #include "wine/debug.h"
103 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
105 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
106 lack the following constants. */
109 # define CD_SECS 60 /* seconds per minute */
112 # define CD_FRAMES 75 /* frames per second */
115 static const struct iocodexs
120 #define X(x) { x, #x },
121 X(IOCTL_CDROM_CHECK_VERIFY)
122 X(IOCTL_CDROM_CURRENT_POSITION)
123 X(IOCTL_CDROM_DISK_TYPE)
124 X(IOCTL_CDROM_GET_CONTROL)
125 X(IOCTL_CDROM_GET_DRIVE_GEOMETRY)
126 X(IOCTL_CDROM_GET_VOLUME)
127 X(IOCTL_CDROM_LOAD_MEDIA)
128 X(IOCTL_CDROM_MEDIA_CATALOG)
129 X(IOCTL_CDROM_MEDIA_REMOVAL)
130 X(IOCTL_CDROM_PAUSE_AUDIO)
131 X(IOCTL_CDROM_PLAY_AUDIO_MSF)
132 X(IOCTL_CDROM_RAW_READ)
133 X(IOCTL_CDROM_READ_Q_CHANNEL)
134 X(IOCTL_CDROM_READ_TOC)
135 X(IOCTL_CDROM_RESUME_AUDIO)
136 X(IOCTL_CDROM_SEEK_AUDIO_MSF)
137 X(IOCTL_CDROM_SET_VOLUME)
138 X(IOCTL_CDROM_STOP_AUDIO)
139 X(IOCTL_CDROM_TRACK_ISRC)
140 X(IOCTL_DISK_MEDIA_REMOVAL)
141 X(IOCTL_DVD_END_SESSION)
142 X(IOCTL_DVD_GET_REGION)
143 X(IOCTL_DVD_READ_KEY)
144 X(IOCTL_DVD_READ_STRUCTURE)
145 X(IOCTL_DVD_SEND_KEY)
146 X(IOCTL_DVD_START_SESSION)
147 X(IOCTL_SCSI_GET_ADDRESS)
148 X(IOCTL_SCSI_GET_CAPABILITIES)
149 X(IOCTL_SCSI_GET_INQUIRY_DATA)
150 X(IOCTL_SCSI_PASS_THROUGH)
151 X(IOCTL_SCSI_PASS_THROUGH_DIRECT)
152 X(IOCTL_STORAGE_CHECK_VERIFY)
153 X(IOCTL_STORAGE_EJECTION_CONTROL)
154 X(IOCTL_STORAGE_EJECT_MEDIA)
155 X(IOCTL_STORAGE_GET_DEVICE_NUMBER)
156 X(IOCTL_STORAGE_LOAD_MEDIA)
157 X(IOCTL_STORAGE_MEDIA_REMOVAL)
158 X(IOCTL_STORAGE_RESET_DEVICE)
161 static const char *iocodex(DWORD code)
164 static char buffer[25];
165 for(i=0; i<sizeof(iocodextable)/sizeof(struct iocodexs); i++)
166 if (code==iocodextable[i].code)
167 return iocodextable[i].codex;
168 sprintf(buffer, "IOCTL_CODE_%x", (int)code);
172 #define INQ_REPLY_LEN 36
173 #define INQ_CMD_LEN 6
175 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
176 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
177 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
178 #define MSF_OF_FRAME(m,fr) {int f=(fr); ((UCHAR *)&(m))[2]=f%CD_FRAMES;f/=CD_FRAMES;((UCHAR *)&(m))[1]=f%CD_SECS;((UCHAR *)&(m))[0]=f/CD_SECS;}
180 /* The documented format of DVD_LAYER_DESCRIPTOR is wrong. Even the format in the
181 * DDK's header is wrong. There are four bytes at the start which always seem to
182 * follow the sequence "02 08 00 00".
186 UCHAR MagicHeader[4];
187 UCHAR BookVersion : 4;
189 UCHAR MinimumRate : 4;
193 UCHAR NumberOfLayers : 2;
195 UCHAR TrackDensity : 4;
196 UCHAR LinearDensity : 4;
197 ULONG StartingDataSector;
199 ULONG EndLayerZeroSector;
203 } internal_dvd_layer_descriptor;
206 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
207 static NTSTATUS CDROM_GetStatusCode(int);
213 # define IDE6_MAJOR 88
216 # define IDE7_MAJOR 89
219 # ifdef CDROM_SEND_PACKET
220 /* structure for CDROM_PACKET_COMMAND ioctl */
221 /* not all Linux versions have all the fields, so we define the
222 * structure ourselves to make sure */
223 struct linux_cdrom_generic_command
225 unsigned char cmd[CDROM_PACKET_SIZE];
226 unsigned char *buffer;
229 struct request_sense *sense;
230 unsigned char data_direction;
235 # endif /* CDROM_SEND_PACKET */
239 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
240 * this should be removed when a proper device interface is implemented
242 * (WS) We need this to keep track of current position and to safely
243 * detect media changes. Besides this should provide a great speed up
249 char toc_good; /* if false, will reread TOC from disk */
251 SUB_Q_CURRENT_POSITION CurrentPosition;
253 /* who has more than 5 cdroms on his/her machine ?? */
254 /* FIXME: this should grow depending on the number of cdroms we install/configure
257 #define MAX_CACHE_ENTRIES 5
258 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
260 static RTL_CRITICAL_SECTION cache_section;
261 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
263 0, 0, &cache_section,
264 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
265 0, 0, { (DWORD_PTR)(__FILE__ ": cache_section") }
267 static RTL_CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
269 /* Proposed media change function: not really needed at this time */
270 /* This is a 1 or 0 type of function */
272 static int CDROM_MediaChanged(int dev)
276 struct cdrom_tochdr hdr;
277 struct cdrom_tocentry entry;
279 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
281 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
284 if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
287 for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
289 if (i == hdr.cdth_trk1 + 1)
291 entry.cdte_track = CDROM_LEADOUT;
293 entry.cdte_track = i;
295 entry.cdte_format = CDROM_MSF;
296 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
298 if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
299 sizeof(struct cdrom_tocentry)) )
307 /******************************************************************
310 * On Mac OS, get the device for the whole disk from a fd that points to a partition.
311 * This is ugly and inefficient, but we have no choice since the partition fd doesn't
312 * support the eject ioctl.
315 static NTSTATUS get_parent_device( int fd, char *name, size_t len )
317 NTSTATUS status = STATUS_NO_SUCH_FILE;
320 io_service_t service;
321 CFMutableDictionaryRef dict;
324 if (fstat( fd, &st ) == -1) return FILE_GetNtStatus();
325 if (!S_ISCHR( st.st_mode )) return STATUS_OBJECT_TYPE_MISMATCH;
327 /* create a dictionary with the right major/minor numbers */
329 if (!(dict = IOServiceMatching( kIOMediaClass ))) return STATUS_NO_MEMORY;
331 i = major( st.st_rdev );
332 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
333 CFDictionaryAddValue( dict, CFSTR( "BSD Major" ), val );
336 i = minor( st.st_rdev );
337 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
338 CFDictionaryAddValue( dict, CFSTR( "BSD Minor" ), val );
341 CFDictionaryAddValue( dict, CFSTR("Removable"), kCFBooleanTrue );
343 service = IOServiceGetMatchingService( kIOMasterPortDefault, dict );
345 /* now look for the parent that has the "Whole" attribute set to TRUE */
349 io_service_t parent = 0;
354 if (!IOObjectConformsTo( service, kIOMediaClass ))
356 if (!(whole = IORegistryEntryCreateCFProperty( service, CFSTR("Whole"), NULL, 0 )))
358 ok = (whole == kCFBooleanTrue);
362 if ((str = IORegistryEntryCreateCFProperty( service, CFSTR("BSD Name"), NULL, 0 )))
364 strcpy( name, "/dev/r" );
365 CFStringGetCString( str, name + 6, len - 6, kCFStringEncodingUTF8 );
367 status = STATUS_SUCCESS;
369 IOObjectRelease( service );
373 IORegistryEntryGetParentEntry( service, kIOServicePlane, &parent );
374 IOObjectRelease( service );
382 /******************************************************************
383 * CDROM_SyncCache [internal]
385 * Read the TOC in and store it in the cdrom_cache structure.
386 * Further requests for the TOC will be copied from the cache
387 * unless certain events like disk ejection is detected, in which
388 * case the cache will be cleared, causing it to be resynced.
389 * The cache section must be held by caller.
391 static NTSTATUS CDROM_SyncCache(int dev, int fd)
395 struct cdrom_tochdr hdr;
396 struct cdrom_tocentry entry;
398 CDROM_TOC *toc = &cdrom_cache[dev].toc;
399 cdrom_cache[dev].toc_good = 0;
401 if (ioctl(fd, CDROMREADTOCHDR, &hdr) == -1)
403 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
404 return FILE_GetNtStatus();
407 toc->FirstTrack = hdr.cdth_trk0;
408 toc->LastTrack = hdr.cdth_trk1;
409 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
410 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
411 toc->Length[0] = tsz >> 8;
412 toc->Length[1] = tsz;
414 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
416 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
418 if (i == toc->LastTrack + 1)
419 entry.cdte_track = CDROM_LEADOUT;
421 entry.cdte_track = i;
422 entry.cdte_format = CDROM_MSF;
423 if (ioctl(fd, CDROMREADTOCENTRY, &entry) == -1)
425 WARN("error read entry (%s)\n", strerror(errno));
426 return FILE_GetNtStatus();
428 toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
429 toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
430 /* marking last track with leadout value as index */
431 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
432 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
433 toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
434 toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
435 toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
437 cdrom_cache[dev].toc_good = 1;
438 return STATUS_SUCCESS;
440 #elif defined(__FreeBSD__) || defined(__NetBSD__)
443 struct ioc_toc_header hdr;
444 struct ioc_read_toc_entry entry;
445 struct cd_toc_entry toc_buffer;
447 CDROM_TOC *toc = &cdrom_cache[dev].toc;
448 cdrom_cache[dev].toc_good = 0;
450 if (ioctl(fd, CDIOREADTOCHEADER, &hdr) == -1)
452 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
453 return FILE_GetNtStatus();
455 toc->FirstTrack = hdr.starting_track;
456 toc->LastTrack = hdr.ending_track;
457 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
458 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
459 toc->Length[0] = tsz >> 8;
460 toc->Length[1] = tsz;
462 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
464 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
466 if (i == toc->LastTrack + 1)
469 entry.starting_track = LEADOUT;
471 entry.starting_track = i;
473 memset((char *)&toc_buffer, 0, sizeof(toc_buffer));
474 entry.address_format = CD_MSF_FORMAT;
475 entry.data_len = sizeof(toc_buffer);
476 entry.data = &toc_buffer;
477 if (ioctl(fd, CDIOREADTOCENTRYS, &entry) == -1)
479 WARN("error read entry (%s)\n", strerror(errno));
480 return FILE_GetNtStatus();
482 toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
483 toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
484 /* marking last track with leadout value as index */
485 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
486 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
487 toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
488 toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
489 toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
491 cdrom_cache[dev].toc_good = 1;
492 return STATUS_SUCCESS;
494 #elif defined(__APPLE__)
496 dk_cd_read_toc_t hdr;
497 CDROM_TOC *toc = &cdrom_cache[dev].toc;
498 cdrom_cache[dev].toc_good = 0;
500 memset( &hdr, 0, sizeof(hdr) );
502 hdr.bufferLength = sizeof(*toc);
503 if (ioctl(fd, DKIOCCDREADTOC, &hdr) == -1)
505 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
506 return FILE_GetNtStatus();
508 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
510 /* convert address format */
511 TRACK_DATA *data = &toc->TrackData[i - toc->FirstTrack];
512 DWORD frame = (((DWORD)data->Address[0] << 24) | ((DWORD)data->Address[1] << 16) |
513 ((DWORD)data->Address[2] << 8) | data->Address[3]);
514 MSF_OF_FRAME( data->Address[1], frame );
515 data->Address[0] = 0;
518 cdrom_cache[dev].toc_good = 1;
519 return STATUS_SUCCESS;
521 return STATUS_NOT_SUPPORTED;
525 static void CDROM_ClearCacheEntry(int dev)
527 RtlEnterCriticalSection( &cache_section );
528 cdrom_cache[dev].toc_good = 0;
529 RtlLeaveCriticalSection( &cache_section );
534 /******************************************************************
535 * CDROM_GetInterfaceInfo
537 * Determines the ide interface (the number after the ide), and the
538 * number of the device on that interface for ide cdroms (*iface <= 1).
539 * Determines the scsi information for scsi cdroms (*iface >= 2).
540 * Returns false if the info cannot not be obtained.
542 static int CDROM_GetInterfaceInfo(int fd, UCHAR* iface, UCHAR* port, UCHAR* device, UCHAR* lun)
546 if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode)) return 0;
551 switch (major(st.st_rdev)) {
552 case IDE0_MAJOR: *iface = 0; break;
553 case IDE1_MAJOR: *iface = 1; break;
554 case IDE2_MAJOR: *iface = 2; break;
555 case IDE3_MAJOR: *iface = 3; break;
556 case IDE4_MAJOR: *iface = 4; break;
557 case IDE5_MAJOR: *iface = 5; break;
558 case IDE6_MAJOR: *iface = 6; break;
559 case IDE7_MAJOR: *iface = 7; break;
560 default: *port = 1; break;
564 *device = (minor(st.st_rdev) >> 6);
567 #ifdef SCSI_IOCTL_GET_IDLUN
569 if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, &idlun) != -1)
571 *port = (idlun[0] >> 24) & 0xff;
572 *iface = ((idlun[0] >> 16) & 0xff) + 2;
573 *device = idlun[0] & 0xff;
574 *lun = (idlun[0] >> 8) & 0xff;
579 WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
584 #elif defined(__NetBSD__)
585 struct scsi_addr addr;
586 if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
590 case TYPE_SCSI: *port = 1;
591 *iface = addr.addr.scsi.scbus;
592 *device = addr.addr.scsi.target;
593 *lun = addr.addr.scsi.lun;
595 case TYPE_ATAPI: *port = 0;
596 *iface = addr.addr.atapi.atbus;
597 *device = addr.addr.atapi.drive;
604 #elif defined(__FreeBSD__)
605 FIXME("not implemented for BSD\n");
608 FIXME("not implemented for nonlinux\n");
614 /******************************************************************
618 static NTSTATUS CDROM_Open(int fd, int* dev)
621 NTSTATUS ret = STATUS_SUCCESS;
626 RtlEnterCriticalSection( &cache_section );
627 for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
630 cdrom_cache[*dev].device == 0 &&
631 cdrom_cache[*dev].inode == 0)
633 else if (cdrom_cache[*dev].device == st.st_dev &&
634 cdrom_cache[*dev].inode == st.st_ino)
637 if (*dev == MAX_CACHE_ENTRIES)
639 if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
643 cdrom_cache[*dev].device = st.st_dev;
644 cdrom_cache[*dev].inode = st.st_ino;
647 RtlLeaveCriticalSection( &cache_section );
649 TRACE("%d, %d\n", *dev, fd);
653 /******************************************************************
654 * CDROM_GetStatusCode
658 static NTSTATUS CDROM_GetStatusCode(int io)
660 if (io == 0) return STATUS_SUCCESS;
661 return FILE_GetNtStatus();
664 /******************************************************************
668 static NTSTATUS CDROM_GetControl(int dev, CDROM_AUDIO_CONTROL* cac)
670 cac->LbaFormat = 0; /* FIXME */
671 cac->LogicalBlocksPerSecond = 1; /* FIXME */
672 return STATUS_NOT_SUPPORTED;
675 /******************************************************************
676 * CDROM_GetDeviceNumber
679 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
682 devnum->DeviceType = FILE_DEVICE_DISK;
683 devnum->DeviceNumber = 1;
684 devnum->PartitionNumber = 1;
685 return STATUS_SUCCESS;
688 /******************************************************************
689 * CDROM_GetDriveGeometry
692 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
698 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
700 fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
701 - FRAME_OF_TOC(toc, 1); /* Total size in frames */
703 dg->Cylinders.u.LowPart = fsize / (64 * 32);
704 dg->Cylinders.u.HighPart = 0;
705 dg->MediaType = RemovableMedia;
706 dg->TracksPerCylinder = 64;
707 dg->SectorsPerTrack = 32;
708 dg->BytesPerSector= 2048;
712 /**************************************************************************
713 * CDROM_Reset [internal]
715 static NTSTATUS CDROM_ResetAudio(int fd)
718 return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
719 #elif defined(__FreeBSD__) || defined(__NetBSD__)
720 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
722 return STATUS_NOT_SUPPORTED;
726 /******************************************************************
731 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
734 return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
735 #elif defined(__FreeBSD__) || defined(__NetBSD__)
736 return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
737 (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
738 (ioctl(fd, CDIOCPREVENT, NULL)));
739 #elif defined(__APPLE__)
740 if (doEject) return CDROM_GetStatusCode( ioctl( fd, DKIOCEJECT, NULL ) );
741 else return STATUS_NOT_SUPPORTED;
743 return STATUS_NOT_SUPPORTED;
747 /******************************************************************
748 * CDROM_ControlEjection
752 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
755 return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
756 #elif defined(__FreeBSD__) || defined(__NetBSD__)
757 return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
759 return STATUS_NOT_SUPPORTED;
763 /******************************************************************
768 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
770 NTSTATUS ret = STATUS_NOT_SUPPORTED;
772 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
773 return STATUS_INVALID_PARAMETER;
775 RtlEnterCriticalSection( &cache_section );
776 if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
778 *toc = cdrom_cache[dev].toc;
779 ret = STATUS_SUCCESS;
781 RtlLeaveCriticalSection( &cache_section );
785 /******************************************************************
790 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
796 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
798 for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
799 if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
800 data->DiskData |= CDROM_DISK_DATA_TRACK;
802 data->DiskData |= CDROM_DISK_AUDIO_TRACK;
804 return STATUS_SUCCESS;
807 /******************************************************************
812 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
813 SUB_Q_CHANNEL_DATA* data)
815 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 size = sizeof(SUB_Q_CURRENT_POSITION);
863 RtlEnterCriticalSection( &cache_section );
864 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
865 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
866 data->CurrentPosition.Control = sc.cdsc_ctrl;
867 data->CurrentPosition.ADR = sc.cdsc_adr;
868 data->CurrentPosition.TrackNumber = sc.cdsc_trk;
869 data->CurrentPosition.IndexNumber = sc.cdsc_ind;
871 data->CurrentPosition.AbsoluteAddress[0] = 0;
872 data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute;
873 data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
874 data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
876 data->CurrentPosition.TrackRelativeAddress[0] = 0;
877 data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute;
878 data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
879 data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
881 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
883 else /* not playing */
885 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
886 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
888 RtlLeaveCriticalSection( &cache_section );
890 case IOCTL_CDROM_MEDIA_CATALOG:
891 size = sizeof(SUB_Q_MEDIA_CATALOG_NUMBER);
892 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
894 struct cdrom_mcn mcn;
895 if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
897 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
898 data->MediaCatalog.Mcval = 0; /* FIXME */
899 memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
900 data->MediaCatalog.MediaCatalog[14] = 0;
903 case IOCTL_CDROM_TRACK_ISRC:
904 size = sizeof(SUB_Q_CURRENT_POSITION);
905 FIXME("TrackIsrc: NIY on linux\n");
906 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
907 data->TrackIsrc.Tcval = 0;
913 ret = CDROM_GetStatusCode(io);
914 #elif defined(__FreeBSD__) || defined(__NetBSD__)
916 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
918 struct ioc_read_subchannel read_sc;
919 struct cd_sub_channel_info sc;
921 read_sc.address_format = CD_MSF_FORMAT;
923 read_sc.data_len = sizeof(sc);
927 case IOCTL_CDROM_CURRENT_POSITION:
928 read_sc.data_format = CD_CURRENT_POSITION;
930 case IOCTL_CDROM_MEDIA_CATALOG:
931 read_sc.data_format = CD_MEDIA_CATALOG;
933 case IOCTL_CDROM_TRACK_ISRC:
934 read_sc.data_format = CD_TRACK_INFO;
935 sc.what.track_info.track_number = data->TrackIsrc.Track;
938 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
941 TRACE("opened or no_media (%s)!\n", strerror(errno));
942 CDROM_ClearCacheEntry(dev);
943 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
947 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
949 switch (sc.header.audio_status) {
950 case CD_AS_AUDIO_INVALID:
951 CDROM_ClearCacheEntry(dev);
952 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
954 case CD_AS_NO_STATUS:
955 CDROM_ClearCacheEntry(dev);
956 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
958 case CD_AS_PLAY_IN_PROGRESS:
959 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
961 case CD_AS_PLAY_PAUSED:
962 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
964 case CD_AS_PLAY_COMPLETED:
965 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
967 case CD_AS_PLAY_ERROR:
968 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
971 TRACE("status=%02X !\n", sc.header.audio_status);
975 case IOCTL_CDROM_CURRENT_POSITION:
976 size = sizeof(SUB_Q_CURRENT_POSITION);
977 RtlEnterCriticalSection( &cache_section );
978 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
979 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
980 data->CurrentPosition.Control = sc.what.position.control;
981 data->CurrentPosition.ADR = sc.what.position.addr_type;
982 data->CurrentPosition.TrackNumber = sc.what.position.track_number;
983 data->CurrentPosition.IndexNumber = sc.what.position.index_number;
985 data->CurrentPosition.AbsoluteAddress[0] = 0;
986 data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
987 data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
988 data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
989 data->CurrentPosition.TrackRelativeAddress[0] = 0;
990 data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
991 data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
992 data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
993 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
995 else { /* not playing */
996 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
997 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
999 RtlLeaveCriticalSection( &cache_section );
1001 case IOCTL_CDROM_MEDIA_CATALOG:
1002 size = sizeof(SUB_Q_MEDIA_CATALOG_NUMBER);
1003 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
1004 data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
1005 memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
1007 case IOCTL_CDROM_TRACK_ISRC:
1008 size = sizeof(SUB_Q_CURRENT_POSITION);
1009 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
1010 data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
1011 memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
1016 ret = CDROM_GetStatusCode(io);
1021 /******************************************************************
1023 * Implements: IOCTL_STORAGE_CHECK_VERIFY
1024 * IOCTL_CDROM_CHECK_VERIFY
1025 * IOCTL_DISK_CHECK_VERIFY
1028 static NTSTATUS CDROM_Verify(int dev, int fd)
1033 ret = ioctl(fd, CDROM_DRIVE_STATUS, NULL);
1035 TRACE("ioctl CDROM_DRIVE_STATUS failed(%s)!\n", strerror(errno));
1036 return CDROM_GetStatusCode(ret);
1039 if(ret == CDS_DISC_OK)
1040 return STATUS_SUCCESS;
1042 return STATUS_NO_MEDIA_IN_DEVICE;
1043 #elif defined(__FreeBSD__)
1045 ret = ioctl(fd, CDIOCSTART, NULL);
1047 return STATUS_SUCCESS;
1049 return STATUS_NO_MEDIA_IN_DEVICE;
1051 FIXME("not implemented for non-linux\n");
1052 return STATUS_NOT_SUPPORTED;
1056 /******************************************************************
1057 * CDROM_PlayAudioMSF
1061 static NTSTATUS CDROM_PlayAudioMSF(int fd, const CDROM_PLAY_AUDIO_MSF* audio_msf)
1063 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1065 struct cdrom_msf msf;
1068 msf.cdmsf_min0 = audio_msf->StartingM;
1069 msf.cdmsf_sec0 = audio_msf->StartingS;
1070 msf.cdmsf_frame0 = audio_msf->StartingF;
1071 msf.cdmsf_min1 = audio_msf->EndingM;
1072 msf.cdmsf_sec1 = audio_msf->EndingS;
1073 msf.cdmsf_frame1 = audio_msf->EndingF;
1075 io = ioctl(fd, CDROMSTART);
1078 WARN("motor doesn't start !\n");
1081 io = ioctl(fd, CDROMPLAYMSF, &msf);
1084 WARN("device doesn't play !\n");
1087 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1088 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
1089 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
1091 ret = CDROM_GetStatusCode(io);
1092 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1093 struct ioc_play_msf msf;
1096 msf.start_m = audio_msf->StartingM;
1097 msf.start_s = audio_msf->StartingS;
1098 msf.start_f = audio_msf->StartingF;
1099 msf.end_m = audio_msf->EndingM;
1100 msf.end_s = audio_msf->EndingS;
1101 msf.end_f = audio_msf->EndingF;
1103 io = ioctl(fd, CDIOCSTART, NULL);
1106 WARN("motor doesn't start !\n");
1109 io = ioctl(fd, CDIOCPLAYMSF, &msf);
1112 WARN("device doesn't play !\n");
1115 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1116 msf.start_m, msf.start_s, msf.start_f,
1117 msf.end_m, msf.end_s, msf.end_f);
1119 ret = CDROM_GetStatusCode(io);
1124 /******************************************************************
1125 * CDROM_SeekAudioMSF
1129 static NTSTATUS CDROM_SeekAudioMSF(int dev, int fd, const CDROM_SEEK_AUDIO_MSF* audio_msf)
1133 SUB_Q_CURRENT_POSITION *cp;
1135 struct cdrom_msf0 msf;
1136 struct cdrom_subchnl sc;
1137 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1138 struct ioc_play_msf msf;
1139 struct ioc_read_subchannel read_sc;
1140 struct cd_sub_channel_info sc;
1144 /* Use the information on the TOC to compute the new current
1145 * position, which is shadowed on the cache. [Portable]. */
1146 frame = FRAME_OF_MSF(*audio_msf);
1148 if ((io = CDROM_ReadTOC(dev, fd, &toc)) != 0) return io;
1150 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
1151 if (FRAME_OF_TOC(toc,i)>frame) break;
1152 if (i <= toc.FirstTrack || i > toc.LastTrack+1)
1153 return STATUS_INVALID_PARAMETER;
1155 RtlEnterCriticalSection( &cache_section );
1156 cp = &cdrom_cache[dev].CurrentPosition;
1157 cp->FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1158 cp->Control = toc.TrackData[i-toc.FirstTrack].Control;
1159 cp->ADR = toc.TrackData[i-toc.FirstTrack].Adr;
1160 cp->TrackNumber = toc.TrackData[i-toc.FirstTrack].TrackNumber;
1161 cp->IndexNumber = 0; /* FIXME: where do they keep these? */
1162 cp->AbsoluteAddress[0] = 0;
1163 cp->AbsoluteAddress[1] = toc.TrackData[i-toc.FirstTrack].Address[1];
1164 cp->AbsoluteAddress[2] = toc.TrackData[i-toc.FirstTrack].Address[2];
1165 cp->AbsoluteAddress[3] = toc.TrackData[i-toc.FirstTrack].Address[3];
1166 frame -= FRAME_OF_TOC(toc,i);
1167 cp->TrackRelativeAddress[0] = 0;
1168 MSF_OF_FRAME(cp->TrackRelativeAddress[1], frame);
1169 RtlLeaveCriticalSection( &cache_section );
1171 /* If playing, then issue a seek command, otherwise do nothing */
1173 sc.cdsc_format = CDROM_MSF;
1175 io = ioctl(fd, CDROMSUBCHNL, &sc);
1178 TRACE("opened or no_media (%s)!\n", strerror(errno));
1179 CDROM_ClearCacheEntry(dev);
1180 return CDROM_GetStatusCode(io);
1182 if (sc.cdsc_audiostatus==CDROM_AUDIO_PLAY)
1184 msf.minute = audio_msf->M;
1185 msf.second = audio_msf->S;
1186 msf.frame = audio_msf->F;
1187 return CDROM_GetStatusCode(ioctl(fd, CDROMSEEK, &msf));
1189 return STATUS_SUCCESS;
1190 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1191 read_sc.address_format = CD_MSF_FORMAT;
1193 read_sc.data_len = sizeof(sc);
1195 read_sc.data_format = CD_CURRENT_POSITION;
1197 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1200 TRACE("opened or no_media (%s)!\n", strerror(errno));
1201 CDROM_ClearCacheEntry(dev);
1202 return CDROM_GetStatusCode(io);
1204 if (sc.header.audio_status==CD_AS_PLAY_IN_PROGRESS)
1207 msf.start_m = audio_msf->M;
1208 msf.start_s = audio_msf->S;
1209 msf.start_f = audio_msf->F;
1210 final_frame = FRAME_OF_TOC(toc,toc.LastTrack+1)-1;
1211 MSF_OF_FRAME(msf.end_m, final_frame);
1213 return CDROM_GetStatusCode(ioctl(fd, CDIOCPLAYMSF, &msf));
1215 return STATUS_SUCCESS;
1217 return STATUS_NOT_SUPPORTED;
1221 /******************************************************************
1226 static NTSTATUS CDROM_PauseAudio(int fd)
1229 return CDROM_GetStatusCode(ioctl(fd, CDROMPAUSE));
1230 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1231 return CDROM_GetStatusCode(ioctl(fd, CDIOCPAUSE, NULL));
1233 return STATUS_NOT_SUPPORTED;
1237 /******************************************************************
1242 static NTSTATUS CDROM_ResumeAudio(int fd)
1245 return CDROM_GetStatusCode(ioctl(fd, CDROMRESUME));
1246 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1247 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESUME, NULL));
1249 return STATUS_NOT_SUPPORTED;
1253 /******************************************************************
1258 static NTSTATUS CDROM_StopAudio(int fd)
1261 return CDROM_GetStatusCode(ioctl(fd, CDROMSTOP));
1262 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1263 return CDROM_GetStatusCode(ioctl(fd, CDIOCSTOP, NULL));
1265 return STATUS_NOT_SUPPORTED;
1269 /******************************************************************
1274 static NTSTATUS CDROM_GetVolume(int fd, VOLUME_CONTROL* vc)
1277 struct cdrom_volctrl volc;
1280 io = ioctl(fd, CDROMVOLREAD, &volc);
1283 vc->PortVolume[0] = volc.channel0;
1284 vc->PortVolume[1] = volc.channel1;
1285 vc->PortVolume[2] = volc.channel2;
1286 vc->PortVolume[3] = volc.channel3;
1288 return CDROM_GetStatusCode(io);
1289 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1290 struct ioc_vol volc;
1293 io = ioctl(fd, CDIOCGETVOL, &volc);
1296 vc->PortVolume[0] = volc.vol[0];
1297 vc->PortVolume[1] = volc.vol[1];
1298 vc->PortVolume[2] = volc.vol[2];
1299 vc->PortVolume[3] = volc.vol[3];
1301 return CDROM_GetStatusCode(io);
1303 return STATUS_NOT_SUPPORTED;
1307 /******************************************************************
1312 static NTSTATUS CDROM_SetVolume(int fd, const VOLUME_CONTROL* vc)
1315 struct cdrom_volctrl volc;
1317 volc.channel0 = vc->PortVolume[0];
1318 volc.channel1 = vc->PortVolume[1];
1319 volc.channel2 = vc->PortVolume[2];
1320 volc.channel3 = vc->PortVolume[3];
1322 return CDROM_GetStatusCode(ioctl(fd, CDROMVOLCTRL, &volc));
1323 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1324 struct ioc_vol volc;
1326 volc.vol[0] = vc->PortVolume[0];
1327 volc.vol[1] = vc->PortVolume[1];
1328 volc.vol[2] = vc->PortVolume[2];
1329 volc.vol[3] = vc->PortVolume[3];
1331 return CDROM_GetStatusCode(ioctl(fd, CDIOCSETVOL, &volc));
1333 return STATUS_NOT_SUPPORTED;
1337 /******************************************************************
1340 * Some features of this IOCTL are rather poorly documented and
1341 * not really intuitive either:
1343 * 1. Although the DiskOffset parameter is meant to be a
1344 * byte offset into the disk, it is in fact the sector
1345 * number multiplied by 2048 regardless of the actual
1348 * 2. The least significant 11 bits of DiskOffset are ignored.
1350 * 3. The TrackMode parameter has no effect on the sector
1351 * size. The entire raw sector (i.e. 2352 bytes of data)
1352 * is always returned. IMO the TrackMode is only used
1353 * to check the correct sector type.
1356 static NTSTATUS CDROM_RawRead(int fd, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
1358 int ret = STATUS_NOT_SUPPORTED;
1361 TRACE("RAW_READ_INFO: DiskOffset=%i,%i SectorCount=%i TrackMode=%i\n buffer=%p len=%i sz=%p\n",
1362 raw->DiskOffset.u.HighPart, raw->DiskOffset.u.LowPart, raw->SectorCount, raw->TrackMode, buffer, len, sz);
1364 if (len < raw->SectorCount * 2352) return STATUS_BUFFER_TOO_SMALL;
1367 if (raw->DiskOffset.u.HighPart & ~2047) {
1368 WARN("DiskOffset points to a sector >= 2**32\n");
1372 switch (raw->TrackMode)
1377 DWORD lba = raw->DiskOffset.QuadPart >> 11;
1378 struct cdrom_msf* msf;
1379 PBYTE *bp; /* current buffer pointer */
1382 if ((lba + raw->SectorCount) >
1383 ((1 << 8*sizeof(msf->cdmsf_min0)) * CD_SECS * CD_FRAMES
1385 WARN("DiskOffset not accessible with MSF\n");
1389 /* Linux reads only one sector at a time.
1390 * ioctl CDROMREADRAW takes struct cdrom_msf as an argument
1391 * on the contrary to what header comments state.
1393 lba += CD_MSF_OFFSET;
1394 for (i = 0, bp = buffer; i < raw->SectorCount;
1395 i++, lba++, bp += 2352)
1397 msf = (struct cdrom_msf*)bp;
1398 msf->cdmsf_min0 = lba / CD_FRAMES / CD_SECS;
1399 msf->cdmsf_sec0 = lba / CD_FRAMES % CD_SECS;
1400 msf->cdmsf_frame0 = lba % CD_FRAMES;
1401 io = ioctl(fd, CDROMREADRAW, msf);
1405 return CDROM_GetStatusCode(io);
1413 struct cdrom_read_audio cdra;
1415 cdra.addr.lba = raw->DiskOffset.QuadPart >> 11;
1416 TRACE("reading at %u\n", cdra.addr.lba);
1417 cdra.addr_format = CDROM_LBA;
1418 cdra.nframes = raw->SectorCount;
1420 io = ioctl(fd, CDROMREADAUDIO, &cdra);
1425 FIXME("NIY: %d\n", raw->TrackMode);
1426 return STATUS_INVALID_PARAMETER;
1429 switch (raw->TrackMode)
1432 FIXME("YellowMode2: NIY\n");
1435 FIXME("XAForm2: NIY\n");
1438 FIXME("CDDA: NIY\n");
1441 FIXME("NIY: %d\n", raw->TrackMode);
1442 return STATUS_INVALID_PARAMETER;
1446 *sz = 2352 * raw->SectorCount;
1447 ret = CDROM_GetStatusCode(io);
1451 /******************************************************************
1452 * CDROM_ScsiPassThroughDirect
1453 * Implements IOCTL_SCSI_PASS_THROUGH_DIRECT
1456 static NTSTATUS CDROM_ScsiPassThroughDirect(int fd, PSCSI_PASS_THROUGH_DIRECT pPacket)
1458 int ret = STATUS_NOT_SUPPORTED;
1459 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1462 #elif defined HAVE_SCSIREQ_T_CMD
1467 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1468 return STATUS_BUFFER_TOO_SMALL;
1470 if (pPacket->CdbLength > 16)
1471 return STATUS_INVALID_PARAMETER;
1474 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1475 return STATUS_INVALID_PARAMETER;
1476 #elif defined HAVE_REQUEST_SENSE
1477 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1478 return STATUS_INVALID_PARAMETER;
1481 if (pPacket->DataTransferLength > 0 && !pPacket->DataBuffer)
1482 return STATUS_INVALID_PARAMETER;
1484 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1485 RtlZeroMemory(&cmd, sizeof(cmd));
1487 cmd.interface_id = 'S';
1488 cmd.cmd_len = pPacket->CdbLength;
1489 cmd.mx_sb_len = pPacket->SenseInfoLength;
1490 cmd.dxfer_len = pPacket->DataTransferLength;
1491 cmd.dxferp = pPacket->DataBuffer;
1492 cmd.cmdp = pPacket->Cdb;
1493 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1494 cmd.timeout = pPacket->TimeOutValue*1000;
1496 switch (pPacket->DataIn)
1498 case SCSI_IOCTL_DATA_IN:
1499 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1501 case SCSI_IOCTL_DATA_OUT:
1502 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1504 case SCSI_IOCTL_DATA_UNSPECIFIED:
1505 cmd.dxfer_direction = SG_DXFER_NONE;
1508 return STATUS_INVALID_PARAMETER;
1511 io = ioctl(fd, SG_IO, &cmd);
1513 pPacket->ScsiStatus = cmd.status;
1514 pPacket->DataTransferLength = cmd.resid;
1515 pPacket->SenseInfoLength = cmd.sb_len_wr;
1517 ret = CDROM_GetStatusCode(io);
1519 #elif defined HAVE_SCSIREQ_T_CMD
1521 memset(&cmd, 0, sizeof(cmd));
1522 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1524 cmd.cmdlen = pPacket->CdbLength;
1525 cmd.databuf = pPacket->DataBuffer;
1526 cmd.datalen = pPacket->DataTransferLength;
1527 cmd.senselen = pPacket->SenseInfoLength;
1528 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1530 switch (pPacket->DataIn)
1532 case SCSI_IOCTL_DATA_OUT:
1533 cmd.flags |= SCCMD_WRITE;
1535 case SCSI_IOCTL_DATA_IN:
1536 cmd.flags |= SCCMD_READ;
1538 case SCSI_IOCTL_DATA_UNSPECIFIED:
1542 return STATUS_INVALID_PARAMETER;
1545 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1549 case SCCMD_OK: break;
1550 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1552 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1554 case SCCMD_SENSE: break;
1555 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1559 if (pPacket->SenseInfoLength != 0)
1561 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1562 cmd.sense, pPacket->SenseInfoLength);
1565 pPacket->ScsiStatus = cmd.status;
1567 ret = CDROM_GetStatusCode(io);
1572 /******************************************************************
1573 * CDROM_ScsiPassThrough
1574 * Implements IOCTL_SCSI_PASS_THROUGH
1577 static NTSTATUS CDROM_ScsiPassThrough(int fd, PSCSI_PASS_THROUGH pPacket)
1579 int ret = STATUS_NOT_SUPPORTED;
1580 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1583 #elif defined HAVE_SCSIREQ_T_CMD
1588 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1589 return STATUS_BUFFER_TOO_SMALL;
1591 if (pPacket->CdbLength > 16)
1592 return STATUS_INVALID_PARAMETER;
1595 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1596 return STATUS_INVALID_PARAMETER;
1597 #elif defined HAVE_REQUEST_SENSE
1598 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1599 return STATUS_INVALID_PARAMETER;
1602 if (pPacket->DataTransferLength > 0 && pPacket->DataBufferOffset < sizeof(SCSI_PASS_THROUGH))
1603 return STATUS_INVALID_PARAMETER;
1605 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1606 RtlZeroMemory(&cmd, sizeof(cmd));
1608 cmd.interface_id = 'S';
1609 cmd.dxfer_len = pPacket->DataTransferLength;
1610 cmd.dxferp = (char*)pPacket + pPacket->DataBufferOffset;
1611 cmd.cmd_len = pPacket->CdbLength;
1612 cmd.cmdp = pPacket->Cdb;
1613 cmd.mx_sb_len = pPacket->SenseInfoLength;
1614 cmd.timeout = pPacket->TimeOutValue*1000;
1616 if(cmd.mx_sb_len > 0)
1617 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1619 switch (pPacket->DataIn)
1621 case SCSI_IOCTL_DATA_IN:
1622 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1624 case SCSI_IOCTL_DATA_OUT:
1625 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1627 case SCSI_IOCTL_DATA_UNSPECIFIED:
1628 cmd.dxfer_direction = SG_DXFER_NONE;
1631 return STATUS_INVALID_PARAMETER;
1634 io = ioctl(fd, SG_IO, &cmd);
1636 pPacket->ScsiStatus = cmd.status;
1637 pPacket->DataTransferLength = cmd.resid;
1638 pPacket->SenseInfoLength = cmd.sb_len_wr;
1640 ret = CDROM_GetStatusCode(io);
1642 #elif defined HAVE_SCSIREQ_T_CMD
1644 memset(&cmd, 0, sizeof(cmd));
1645 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1647 if ( pPacket->DataBufferOffset > 0x1000 )
1649 cmd.databuf = (void*)pPacket->DataBufferOffset;
1653 cmd.databuf = (char*)pPacket + pPacket->DataBufferOffset;
1656 cmd.cmdlen = pPacket->CdbLength;
1657 cmd.datalen = pPacket->DataTransferLength;
1658 cmd.senselen = pPacket->SenseInfoLength;
1659 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1661 switch (pPacket->DataIn)
1663 case SCSI_IOCTL_DATA_OUT:
1664 cmd.flags |= SCCMD_WRITE;
1666 case SCSI_IOCTL_DATA_IN:
1667 cmd.flags |= SCCMD_READ;
1669 case SCSI_IOCTL_DATA_UNSPECIFIED:
1673 return STATUS_INVALID_PARAMETER;
1676 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1680 case SCCMD_OK: break;
1681 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1683 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1685 case SCCMD_SENSE: break;
1686 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1690 if (pPacket->SenseInfoLength != 0)
1692 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1693 cmd.sense, pPacket->SenseInfoLength);
1696 pPacket->ScsiStatus = cmd.status;
1698 ret = CDROM_GetStatusCode(io);
1703 /******************************************************************
1708 static NTSTATUS CDROM_ScsiGetCaps(PIO_SCSI_CAPABILITIES caps)
1710 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
1712 caps->Length = sizeof(*caps);
1713 #ifdef SG_SCATTER_SZ
1714 caps->MaximumTransferLength = SG_SCATTER_SZ; /* FIXME */
1715 caps->MaximumPhysicalPages = SG_SCATTER_SZ / getpagesize();
1716 caps->SupportedAsynchronousEvents = TRUE;
1717 caps->AlignmentMask = getpagesize();
1718 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
1719 caps->AdapterScansDown = FALSE; /* FIXME ? */
1720 caps->AdapterUsesPio = FALSE; /* FIXME ? */
1721 ret = STATUS_SUCCESS;
1723 FIXME("Unimplemented\n");
1728 /******************************************************************
1731 * implements IOCTL_SCSI_GET_ADDRESS
1733 static NTSTATUS CDROM_GetAddress(int fd, SCSI_ADDRESS* address)
1735 UCHAR portnum, busid, targetid, lun;
1737 address->Length = sizeof(SCSI_ADDRESS);
1738 if ( ! CDROM_GetInterfaceInfo(fd, &portnum, &busid, &targetid, &lun))
1739 return STATUS_NOT_SUPPORTED;
1741 address->PortNumber = portnum; /* primary=0 secondary=1 for ide */
1742 address->PathId = busid; /* always 0 for ide */
1743 address->TargetId = targetid; /* master=0 slave=1 for ide */
1745 return STATUS_SUCCESS;
1748 /******************************************************************
1753 static NTSTATUS DVD_StartSession(int fd, const DVD_SESSION_ID *sid_in, PDVD_SESSION_ID sid_out)
1756 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1757 dvd_authinfo auth_info;
1759 memset( &auth_info, 0, sizeof( auth_info ) );
1760 auth_info.type = DVD_LU_SEND_AGID;
1761 if (sid_in) auth_info.lsa.agid = *(const int*)sid_in; /* ?*/
1763 TRACE("fd 0x%08x\n",fd);
1764 ret =CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
1765 *sid_out = auth_info.lsa.agid;
1767 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1768 return STATUS_NOT_SUPPORTED;
1770 return STATUS_NOT_SUPPORTED;
1774 /******************************************************************
1779 static NTSTATUS DVD_EndSession(int fd, const DVD_SESSION_ID *sid)
1782 dvd_authinfo auth_info;
1784 memset( &auth_info, 0, sizeof( auth_info ) );
1785 auth_info.type = DVD_INVALIDATE_AGID;
1786 auth_info.lsa.agid = *(const int*)sid;
1789 return CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
1790 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1791 return STATUS_NOT_SUPPORTED;
1793 return STATUS_NOT_SUPPORTED;
1797 /******************************************************************
1802 static NTSTATUS DVD_SendKey(int fd, const DVD_COPY_PROTECT_KEY *key)
1805 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1806 dvd_authinfo auth_info;
1808 memset( &auth_info, 0, sizeof( auth_info ) );
1809 switch (key->KeyType)
1811 case DvdChallengeKey:
1812 auth_info.type = DVD_HOST_SEND_CHALLENGE;
1813 auth_info.hsc.agid = (int)key->SessionId;
1814 TRACE("DvdChallengeKey ioc 0x%x\n", DVD_AUTH );
1815 memcpy( auth_info.hsc.chal, key->KeyData, DVD_CHALLENGE_SIZE );
1816 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1819 auth_info.type = DVD_HOST_SEND_KEY2;
1820 auth_info.hsk.agid = (int)key->SessionId;
1822 memcpy( auth_info.hsk.key, key->KeyData, DVD_KEY_SIZE );
1824 TRACE("DvdBusKey2\n");
1825 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1829 FIXME("Unknown Keytype 0x%x\n",key->KeyType);
1833 FIXME("unsupported on this platform\n");
1834 return STATUS_NOT_SUPPORTED;
1838 /******************************************************************
1843 static NTSTATUS DVD_ReadKey(int fd, PDVD_COPY_PROTECT_KEY key)
1846 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1848 dvd_authinfo auth_info;
1850 memset( &dvd, 0, sizeof( dvd_struct ) );
1851 memset( &auth_info, 0, sizeof( auth_info ) );
1852 switch (key->KeyType)
1856 dvd.type = DVD_STRUCT_DISCKEY;
1857 dvd.disckey.agid = (int)key->SessionId;
1858 memset( dvd.disckey.value, 0, DVD_DISCKEY_SIZE );
1860 TRACE("DvdDiskKey\n");
1861 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
1862 if (ret == STATUS_SUCCESS)
1863 memcpy(key->KeyData,dvd.disckey.value,DVD_DISCKEY_SIZE);
1866 auth_info.type = DVD_LU_SEND_TITLE_KEY;
1867 auth_info.lstk.agid = (int)key->SessionId;
1868 auth_info.lstk.lba = (int)(key->Parameters.TitleOffset.QuadPart>>11);
1869 TRACE("DvdTitleKey session %d Quadpart 0x%08lx offset 0x%08x\n",
1870 (int)key->SessionId, (long)key->Parameters.TitleOffset.QuadPart,
1871 auth_info.lstk.lba);
1872 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1873 if (ret == STATUS_SUCCESS)
1874 memcpy(key->KeyData, auth_info.lstk.title_key, DVD_KEY_SIZE );
1876 case DvdChallengeKey:
1878 auth_info.type = DVD_LU_SEND_CHALLENGE;
1879 auth_info.lsc.agid = (int)key->SessionId;
1881 TRACE("DvdChallengeKey\n");
1882 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1883 if (ret == STATUS_SUCCESS)
1884 memcpy( key->KeyData, auth_info.lsc.chal, DVD_CHALLENGE_SIZE );
1887 auth_info.type = DVD_LU_SEND_ASF;
1889 auth_info.lsasf.asf=((PDVD_ASF)key->KeyData)->SuccessFlag;
1890 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1891 ((PDVD_ASF)key->KeyData)->SuccessFlag = auth_info.lsasf.asf;
1894 auth_info.type = DVD_LU_SEND_KEY1;
1895 auth_info.lsk.agid = (int)key->SessionId;
1897 TRACE("DvdBusKey1\n");
1898 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1900 if (ret == STATUS_SUCCESS)
1901 memcpy( key->KeyData, auth_info.lsk.key, DVD_KEY_SIZE );
1904 auth_info.type = DVD_LU_SEND_RPC_STATE;
1906 TRACE("DvdGetRpcKey\n");
1907 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1909 if (ret == STATUS_SUCCESS)
1911 ((PDVD_RPC_KEY)key->KeyData)->TypeCode = auth_info.lrpcs.type;
1912 ((PDVD_RPC_KEY)key->KeyData)->RegionMask = auth_info.lrpcs.region_mask;
1913 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme = auth_info.lrpcs.rpc_scheme;
1917 FIXME("Unknown keytype 0x%x\n",key->KeyType);
1920 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1922 return STATUS_NOT_SUPPORTED;
1925 return STATUS_NOT_SUPPORTED;
1929 /******************************************************************
1934 static NTSTATUS DVD_GetRegion(int dev, PDVD_REGION region)
1937 return STATUS_SUCCESS;
1941 /******************************************************************
1946 static NTSTATUS DVD_ReadStructure(int dev, const DVD_READ_STRUCTURE *structure, PDVD_LAYER_DESCRIPTOR layer)
1948 #ifdef DVD_READ_STRUCT
1951 if (structure->BlockByteOffset.u.HighPart || structure->BlockByteOffset.u.LowPart)
1952 FIXME(": BlockByteOffset is not handled\n");
1954 switch (structure->Format)
1956 case DvdPhysicalDescriptor:
1957 s.type = DVD_STRUCT_PHYSICAL;
1958 s.physical.layer_num = structure->LayerNumber;
1961 case DvdCopyrightDescriptor:
1962 s.type = DVD_STRUCT_COPYRIGHT;
1963 s.copyright.layer_num = structure->LayerNumber;
1966 case DvdDiskKeyDescriptor:
1967 s.type = DVD_STRUCT_DISCKEY;
1968 s.disckey.agid = structure->SessionId;
1971 case DvdBCADescriptor:
1972 s.type = DVD_STRUCT_BCA;
1975 case DvdManufacturerDescriptor:
1976 s.type = DVD_STRUCT_MANUFACT;
1977 s.manufact.layer_num = structure->LayerNumber;
1980 case DvdMaxDescriptor: /* This is not a real request, no matter what MSDN says! */
1982 return STATUS_INVALID_PARAMETER;
1985 if (ioctl(dev, DVD_READ_STRUCT, &s) < 0)
1986 return STATUS_INVALID_PARAMETER;
1988 switch (structure->Format)
1990 case DvdPhysicalDescriptor:
1992 internal_dvd_layer_descriptor *p = (internal_dvd_layer_descriptor *) layer;
1993 struct dvd_layer *l = &s.physical.layer[s.physical.layer_num];
1995 p->MagicHeader[0] = 2;
1996 p->MagicHeader[1] = 8;
1997 p->MagicHeader[2] = 0;
1998 p->MagicHeader[3] = 0;
1999 p->BookVersion = l->book_version;
2000 p->BookType = l->book_type;
2001 p->MinimumRate = l->min_rate;
2002 p->DiskSize = l->disc_size;
2003 p->LayerType = l->layer_type;
2004 p->TrackPath = l->track_path;
2005 p->NumberOfLayers = l->nlayers;
2007 p->TrackDensity = l->track_density;
2008 p->LinearDensity = l->linear_density;
2009 p->StartingDataSector = l->start_sector;
2010 p->EndDataSector = l->end_sector;
2011 p->EndLayerZeroSector = l->end_sector_l0;
2013 p->BCAFlag = l->bca;
2018 case DvdCopyrightDescriptor:
2020 PDVD_COPYRIGHT_DESCRIPTOR p = (PDVD_COPYRIGHT_DESCRIPTOR) layer;
2022 p->CopyrightProtectionType = s.copyright.cpst;
2023 p->RegionManagementInformation = s.copyright.rmi;
2028 case DvdDiskKeyDescriptor:
2030 PDVD_DISK_KEY_DESCRIPTOR p = (PDVD_DISK_KEY_DESCRIPTOR) layer;
2032 memcpy(p->DiskKeyData, s.disckey.value, 2048);
2036 case DvdBCADescriptor:
2038 PDVD_BCA_DESCRIPTOR p = (PDVD_BCA_DESCRIPTOR) layer;
2040 memcpy(p->BCAInformation, s.bca.value, s.bca.len);
2044 case DvdManufacturerDescriptor:
2046 PDVD_MANUFACTURER_DESCRIPTOR p = (PDVD_MANUFACTURER_DESCRIPTOR) layer;
2048 memcpy(p->ManufacturingInformation, s.manufact.value, 2048);
2052 case DvdMaxDescriptor: /* Suppress warning */
2058 return STATUS_SUCCESS;
2062 /******************************************************************
2064 * Implements the IOCTL_GET_INQUIRY_DATA ioctl.
2065 * Returns Inquiry data for all devices on the specified scsi bus
2066 * Returns STATUS_BUFFER_TOO_SMALL if the output buffer is too small,
2067 * STATUS_INVALID_DEVICE_REQUEST if the given handle isn't to a SCSI device,
2068 * or STATUS_NOT_SUPPORTED if the OS driver is too old
2070 static NTSTATUS GetInquiryData(int fd, PSCSI_ADAPTER_BUS_INFO BufferOut, DWORD OutBufferSize)
2072 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
2073 PSCSI_INQUIRY_DATA pInquiryData = NULL;
2074 UCHAR sense_buffer[32];
2077 UCHAR inquiry[INQ_CMD_LEN] = {INQUIRY, 0, 0, 0, INQ_REPLY_LEN, 0};
2079 /* Check we have a SCSI device and a supported driver */
2080 if(ioctl(fd, SG_GET_VERSION_NUM, &version) != 0)
2082 WARN("IOCTL_SCSI_GET_INQUIRY_DATA sg driver is not loaded\n");
2083 return STATUS_INVALID_DEVICE_REQUEST;
2085 if(version < 30000 )
2086 return STATUS_NOT_SUPPORTED;
2088 /* FIXME: Enumerate devices on the bus */
2089 BufferOut->NumberOfBuses = 1;
2090 BufferOut->BusData[0].NumberOfLogicalUnits = 1;
2091 BufferOut->BusData[0].InquiryDataOffset = sizeof(SCSI_ADAPTER_BUS_INFO);
2093 pInquiryData = (PSCSI_INQUIRY_DATA)(BufferOut + 1);
2095 RtlZeroMemory(&iocmd, sizeof(iocmd));
2096 iocmd.interface_id = 'S';
2097 iocmd.cmd_len = sizeof(inquiry);
2098 iocmd.mx_sb_len = sizeof(sense_buffer);
2099 iocmd.dxfer_direction = SG_DXFER_FROM_DEV;
2100 iocmd.dxfer_len = INQ_REPLY_LEN;
2101 iocmd.dxferp = pInquiryData->InquiryData;
2102 iocmd.cmdp = inquiry;
2103 iocmd.sbp = sense_buffer;
2104 iocmd.timeout = 1000;
2106 iochk = ioctl(fd, SG_IO, &iocmd);
2108 WARN("ioctl SG_IO returned %d, error (%s)\n", iochk, strerror(errno));
2110 CDROM_GetInterfaceInfo(fd, &BufferOut->BusData[0].InitiatorBusId, &pInquiryData->PathId, &pInquiryData->TargetId, &pInquiryData->Lun);
2111 pInquiryData->DeviceClaimed = TRUE;
2112 pInquiryData->InquiryDataLength = INQ_REPLY_LEN;
2113 pInquiryData->NextInquiryDataOffset = 0;
2114 return STATUS_SUCCESS;
2116 FIXME("not implemented for nonlinux\n");
2117 return STATUS_NOT_SUPPORTED;
2121 /******************************************************************
2122 * CDROM_DeviceIoControl
2126 NTSTATUS CDROM_DeviceIoControl(HANDLE hDevice,
2127 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
2128 PVOID UserApcContext,
2129 PIO_STATUS_BLOCK piosb,
2130 ULONG dwIoControlCode,
2131 LPVOID lpInBuffer, DWORD nInBufferSize,
2132 LPVOID lpOutBuffer, DWORD nOutBufferSize)
2135 NTSTATUS status = STATUS_SUCCESS;
2136 int fd, needs_close, dev;
2138 TRACE("%p %s %p %d %p %d %p\n",
2139 hDevice, iocodex(dwIoControlCode), lpInBuffer, nInBufferSize,
2140 lpOutBuffer, nOutBufferSize, piosb);
2142 piosb->Information = 0;
2144 if ((status = server_get_unix_fd( hDevice, 0, &fd, &needs_close, NULL, NULL )))
2146 if (status == STATUS_BAD_DEVICE_TYPE) return status; /* no associated fd */
2150 if ((status = CDROM_Open(fd, &dev)))
2152 if (needs_close) close( fd );
2156 switch (dwIoControlCode)
2158 case IOCTL_STORAGE_CHECK_VERIFY:
2159 case IOCTL_CDROM_CHECK_VERIFY:
2160 case IOCTL_DISK_CHECK_VERIFY:
2162 CDROM_ClearCacheEntry(dev);
2163 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2164 status = STATUS_INVALID_PARAMETER;
2165 else status = CDROM_Verify(dev, fd);
2168 /* EPP case IOCTL_STORAGE_CHECK_VERIFY2: */
2170 /* EPP case IOCTL_STORAGE_FIND_NEW_DEVICES: */
2171 /* EPP case IOCTL_CDROM_FIND_NEW_DEVICES: */
2173 case IOCTL_STORAGE_LOAD_MEDIA:
2174 case IOCTL_CDROM_LOAD_MEDIA:
2176 CDROM_ClearCacheEntry(dev);
2177 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2178 status = STATUS_INVALID_PARAMETER;
2179 else status = CDROM_SetTray(fd, FALSE);
2181 case IOCTL_STORAGE_EJECT_MEDIA:
2183 CDROM_ClearCacheEntry(dev);
2184 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2185 status = STATUS_INVALID_PARAMETER;
2191 /* This is ugly as hell, but Mac OS is unable to eject from the device fd,
2192 * it wants an fd for the whole device, and it also requires the device fd
2193 * to be closed first, so we have to close the handle that the caller gave us.
2194 * Also for some reason it wants the fd to be closed before we even open the parent.
2196 if ((status = get_parent_device( fd, name, sizeof(name) ))) break;
2198 if (needs_close) close( fd );
2199 TRACE("opening parent %s\n", name );
2200 if ((fd = open( name, O_RDONLY )) == -1)
2202 status = FILE_GetNtStatus();
2207 status = CDROM_SetTray(fd, TRUE);
2211 case IOCTL_CDROM_MEDIA_REMOVAL:
2212 case IOCTL_DISK_MEDIA_REMOVAL:
2213 case IOCTL_STORAGE_MEDIA_REMOVAL:
2214 case IOCTL_STORAGE_EJECTION_CONTROL:
2215 /* FIXME the last ioctl:s is not the same as the two others...
2216 * lockcount/owner should be handled */
2218 CDROM_ClearCacheEntry(dev);
2219 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2220 else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) status = STATUS_BUFFER_TOO_SMALL;
2221 else status = CDROM_ControlEjection(fd, (const PREVENT_MEDIA_REMOVAL*)lpInBuffer);
2224 /* EPP case IOCTL_STORAGE_GET_MEDIA_TYPES: */
2226 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
2227 sz = sizeof(STORAGE_DEVICE_NUMBER);
2228 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2229 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2230 else status = CDROM_GetDeviceNumber(dev, (STORAGE_DEVICE_NUMBER*)lpOutBuffer);
2233 case IOCTL_STORAGE_RESET_DEVICE:
2235 CDROM_ClearCacheEntry(dev);
2236 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2237 status = STATUS_INVALID_PARAMETER;
2238 else status = CDROM_ResetAudio(fd);
2241 case IOCTL_CDROM_GET_CONTROL:
2242 sz = sizeof(CDROM_AUDIO_CONTROL);
2243 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2244 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2245 else status = CDROM_GetControl(dev, (CDROM_AUDIO_CONTROL*)lpOutBuffer);
2248 case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
2249 sz = sizeof(DISK_GEOMETRY);
2250 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2251 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2252 else status = CDROM_GetDriveGeometry(dev, fd, (DISK_GEOMETRY*)lpOutBuffer);
2255 case IOCTL_CDROM_DISK_TYPE:
2256 sz = sizeof(CDROM_DISK_DATA);
2257 /* CDROM_ClearCacheEntry(dev); */
2258 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2259 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2260 else status = CDROM_GetDiskData(dev, fd, (CDROM_DISK_DATA*)lpOutBuffer);
2263 /* EPP case IOCTL_CDROM_GET_LAST_SESSION: */
2265 case IOCTL_CDROM_READ_Q_CHANNEL:
2266 sz = sizeof(SUB_Q_CHANNEL_DATA);
2267 if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT))
2268 status = STATUS_INVALID_PARAMETER;
2269 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2270 else status = CDROM_ReadQChannel(dev, fd, (const CDROM_SUB_Q_DATA_FORMAT*)lpInBuffer,
2271 (SUB_Q_CHANNEL_DATA*)lpOutBuffer);
2274 case IOCTL_CDROM_READ_TOC:
2275 sz = sizeof(CDROM_TOC);
2276 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2277 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2278 else status = CDROM_ReadTOC(dev, fd, (CDROM_TOC*)lpOutBuffer);
2281 /* EPP case IOCTL_CDROM_READ_TOC_EX: */
2283 case IOCTL_CDROM_PAUSE_AUDIO:
2285 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2286 status = STATUS_INVALID_PARAMETER;
2287 else status = CDROM_PauseAudio(fd);
2289 case IOCTL_CDROM_PLAY_AUDIO_MSF:
2291 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2292 else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
2293 else status = CDROM_PlayAudioMSF(fd, (const CDROM_PLAY_AUDIO_MSF*)lpInBuffer);
2295 case IOCTL_CDROM_RESUME_AUDIO:
2297 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2298 status = STATUS_INVALID_PARAMETER;
2299 else status = CDROM_ResumeAudio(fd);
2301 case IOCTL_CDROM_SEEK_AUDIO_MSF:
2303 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2304 else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
2305 else status = CDROM_SeekAudioMSF(dev, fd, (const CDROM_SEEK_AUDIO_MSF*)lpInBuffer);
2307 case IOCTL_CDROM_STOP_AUDIO:
2309 CDROM_ClearCacheEntry(dev); /* Maybe intention is to change media */
2310 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2311 status = STATUS_INVALID_PARAMETER;
2312 else status = CDROM_StopAudio(fd);
2314 case IOCTL_CDROM_GET_VOLUME:
2315 sz = sizeof(VOLUME_CONTROL);
2316 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2317 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2318 else status = CDROM_GetVolume(fd, (VOLUME_CONTROL*)lpOutBuffer);
2320 case IOCTL_CDROM_SET_VOLUME:
2322 CDROM_ClearCacheEntry(dev);
2323 if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
2324 status = STATUS_INVALID_PARAMETER;
2325 else status = CDROM_SetVolume(fd, (const VOLUME_CONTROL*)lpInBuffer);
2327 case IOCTL_CDROM_RAW_READ:
2329 if (nInBufferSize < sizeof(RAW_READ_INFO)) status = STATUS_INVALID_PARAMETER;
2330 else if (lpOutBuffer == NULL) status = STATUS_BUFFER_TOO_SMALL;
2331 else status = CDROM_RawRead(fd, (const RAW_READ_INFO*)lpInBuffer,
2332 lpOutBuffer, nOutBufferSize, &sz);
2334 case IOCTL_SCSI_GET_ADDRESS:
2335 sz = sizeof(SCSI_ADDRESS);
2336 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2337 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2338 else status = CDROM_GetAddress(fd, (SCSI_ADDRESS*)lpOutBuffer);
2340 case IOCTL_SCSI_PASS_THROUGH_DIRECT:
2341 sz = sizeof(SCSI_PASS_THROUGH_DIRECT);
2342 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2343 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH_DIRECT)) status = STATUS_BUFFER_TOO_SMALL;
2344 else status = CDROM_ScsiPassThroughDirect(fd, (PSCSI_PASS_THROUGH_DIRECT)lpOutBuffer);
2346 case IOCTL_SCSI_PASS_THROUGH:
2347 sz = sizeof(SCSI_PASS_THROUGH);
2348 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2349 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH)) status = STATUS_BUFFER_TOO_SMALL;
2350 else status = CDROM_ScsiPassThrough(fd, (PSCSI_PASS_THROUGH)lpOutBuffer);
2352 case IOCTL_SCSI_GET_CAPABILITIES:
2353 sz = sizeof(IO_SCSI_CAPABILITIES);
2354 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2355 else if (nOutBufferSize < sizeof(IO_SCSI_CAPABILITIES)) status = STATUS_BUFFER_TOO_SMALL;
2356 else status = CDROM_ScsiGetCaps((PIO_SCSI_CAPABILITIES)lpOutBuffer);
2358 case IOCTL_DVD_START_SESSION:
2359 sz = sizeof(DVD_SESSION_ID);
2360 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2361 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2364 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
2365 *(PDVD_SESSION_ID)lpOutBuffer);
2366 status = DVD_StartSession(fd, (PDVD_SESSION_ID)lpInBuffer, (PDVD_SESSION_ID)lpOutBuffer);
2367 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
2368 *(PDVD_SESSION_ID)lpOutBuffer);
2371 case IOCTL_DVD_END_SESSION:
2372 sz = sizeof(DVD_SESSION_ID);
2373 if ((lpInBuffer == NULL) || (nInBufferSize < sz))status = STATUS_INVALID_PARAMETER;
2374 else status = DVD_EndSession(fd, (PDVD_SESSION_ID)lpInBuffer);
2376 case IOCTL_DVD_SEND_KEY:
2379 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
2380 status = STATUS_INVALID_PARAMETER;
2383 TRACE("doing DVD_SendKey\n");
2384 status = DVD_SendKey(fd, (PDVD_COPY_PROTECT_KEY)lpInBuffer);
2387 case IOCTL_DVD_READ_KEY:
2389 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
2390 status = STATUS_INVALID_PARAMETER;
2391 else if (lpInBuffer !=lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
2394 TRACE("doing DVD_READ_KEY\n");
2395 sz = ((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength;
2396 status = DVD_ReadKey(fd, (PDVD_COPY_PROTECT_KEY)lpInBuffer);
2399 case IOCTL_DVD_GET_REGION:
2400 sz = sizeof(DVD_REGION);
2401 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2402 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2403 TRACE("doing DVD_Get_REGION\n");
2404 status = DVD_GetRegion(fd, (PDVD_REGION)lpOutBuffer);
2406 case IOCTL_DVD_READ_STRUCTURE:
2407 sz = sizeof(DVD_LAYER_DESCRIPTOR);
2408 if (lpInBuffer == NULL || nInBufferSize != sizeof(DVD_READ_STRUCTURE)) status = STATUS_INVALID_PARAMETER;
2409 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2412 TRACE("doing DVD_READ_STRUCTURE\n");
2413 status = DVD_ReadStructure(fd, (PDVD_READ_STRUCTURE)lpInBuffer, (PDVD_LAYER_DESCRIPTOR)lpOutBuffer);
2417 case IOCTL_SCSI_GET_INQUIRY_DATA:
2419 status = GetInquiryData(fd, (PSCSI_ADAPTER_BUS_INFO)lpOutBuffer, nOutBufferSize);
2423 if (needs_close) close( fd );
2424 return STATUS_NOT_SUPPORTED;
2426 if (needs_close) close( fd );
2428 piosb->u.Status = status;
2429 piosb->Information = sz;
2430 if (hEvent) NtSetEvent(hEvent, NULL);