ddraw/tests: Rewrite LimitTest().
[wine.git] / dlls / ntdll / cdrom.c
blob2c64106072d747528fedd1bb88b0d34f1c949bf3
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
24 #include "config.h"
25 #include "wine/port.h"
27 #include <errno.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_IO_H
32 # include <io.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <fcntl.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
40 #endif
41 #ifdef MAJOR_IN_MKDEV
42 # include <sys/mkdev.h>
43 #elif defined(MAJOR_IN_SYSMACROS)
44 # include <sys/sysmacros.h>
45 #endif
46 #include <sys/types.h>
48 #ifdef HAVE_SYS_IOCTL_H
49 #include <sys/ioctl.h>
50 #endif
51 #ifdef HAVE_SCSI_SG_H
52 # include <scsi/sg.h>
53 #endif
54 #ifdef HAVE_SCSI_SCSI_H
55 # include <scsi/scsi.h>
56 # undef REASSIGN_BLOCKS /* avoid conflict with winioctl.h */
57 # undef FAILED /* avoid conflict with winerror.h */
58 #endif
59 #ifdef HAVE_SCSI_SCSI_IOCTL_H
60 # include <scsi/scsi_ioctl.h>
61 #endif
62 #ifdef HAVE_LINUX_MAJOR_H
63 # include <linux/major.h>
64 #endif
65 #ifdef HAVE_LINUX_HDREG_H
66 # include <linux/hdreg.h>
67 #endif
68 #ifdef HAVE_LINUX_PARAM_H
69 # include <linux/param.h>
70 #endif
71 #ifdef HAVE_LINUX_CDROM_H
72 # include <linux/cdrom.h>
73 #endif
74 #ifdef HAVE_LINUX_UCDROM_H
75 # include <linux/ucdrom.h>
76 #endif
77 #ifdef HAVE_SYS_CDIO_H
78 # include <sys/cdio.h>
79 #endif
80 #ifdef HAVE_SYS_SCSIIO_H
81 # include <sys/scsiio.h>
82 #endif
84 #ifdef HAVE_IOKIT_IOKITLIB_H
85 # include <libkern/OSByteOrder.h>
86 # include <sys/disk.h>
87 # include <IOKit/IOKitLib.h>
88 # include <IOKit/storage/IOMedia.h>
89 # include <IOKit/storage/IOCDMediaBSDClient.h>
90 # include <IOKit/storage/IODVDMediaBSDClient.h>
91 # include <IOKit/scsi/SCSITask.h>
92 # include <IOKit/scsi/SCSICmds_REQUEST_SENSE_Defs.h>
93 # define SENSEBUFLEN kSenseDefaultSize
95 typedef struct
97 uint32_t attribute;
98 uint32_t timeout;
99 uint32_t response;
100 uint32_t status;
101 uint8_t direction;
102 uint8_t cdbSize;
103 uint8_t reserved0144[2];
104 uint8_t cdb[16];
105 void* buffer;
106 uint64_t bufferSize;
107 void* sense;
108 uint64_t senseLen;
109 } dk_scsi_command_t;
111 typedef struct
113 uint64_t bus;
114 uint64_t port;
115 uint64_t target;
116 uint64_t lun;
117 } dk_scsi_identify_t;
119 #define DKIOCSCSICOMMAND _IOWR('d', 253, dk_scsi_command_t)
120 #define DKIOCSCSIIDENTIFY _IOR('d', 254, dk_scsi_identify_t)
122 #endif
124 #include "ntstatus.h"
125 #define WIN32_NO_STATUS
126 #define NONAMELESSUNION
127 #include "windef.h"
128 #include "winternl.h"
129 #include "winioctl.h"
130 #include "ntddstor.h"
131 #include "ntddcdrm.h"
132 #include "ddk/ntddcdvd.h"
133 #include "ntddscsi.h"
134 #include "ntdll_misc.h"
135 #include "wine/server.h"
136 #include "wine/library.h"
137 #include "wine/debug.h"
139 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
141 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
142 lack the following constants. */
144 #ifndef CD_SECS
145 # define CD_SECS 60 /* seconds per minute */
146 #endif
147 #ifndef CD_FRAMES
148 # define CD_FRAMES 75 /* frames per second */
149 #endif
151 #ifdef WORDS_BIGENDIAN
152 #define GET_BE_DWORD(x) (x)
153 #else
154 #define GET_BE_DWORD(x) RtlUlongByteSwap(x)
155 #endif
157 static const struct iocodexs
159 DWORD code;
160 const char *codex;
161 } iocodextable[] = {
162 #define X(x) { x, #x },
163 X(IOCTL_CDROM_CHECK_VERIFY)
164 X(IOCTL_CDROM_CURRENT_POSITION)
165 X(IOCTL_CDROM_DISK_TYPE)
166 X(IOCTL_CDROM_GET_CONTROL)
167 X(IOCTL_CDROM_GET_DRIVE_GEOMETRY)
168 X(IOCTL_CDROM_GET_VOLUME)
169 X(IOCTL_CDROM_LOAD_MEDIA)
170 X(IOCTL_CDROM_MEDIA_CATALOG)
171 X(IOCTL_CDROM_MEDIA_REMOVAL)
172 X(IOCTL_CDROM_PAUSE_AUDIO)
173 X(IOCTL_CDROM_PLAY_AUDIO_MSF)
174 X(IOCTL_CDROM_RAW_READ)
175 X(IOCTL_CDROM_READ_Q_CHANNEL)
176 X(IOCTL_CDROM_READ_TOC)
177 X(IOCTL_CDROM_RESUME_AUDIO)
178 X(IOCTL_CDROM_SEEK_AUDIO_MSF)
179 X(IOCTL_CDROM_SET_VOLUME)
180 X(IOCTL_CDROM_STOP_AUDIO)
181 X(IOCTL_CDROM_TRACK_ISRC)
182 X(IOCTL_DISK_GET_MEDIA_TYPES)
183 X(IOCTL_DISK_MEDIA_REMOVAL)
184 X(IOCTL_DVD_END_SESSION)
185 X(IOCTL_DVD_GET_REGION)
186 X(IOCTL_DVD_READ_KEY)
187 X(IOCTL_DVD_READ_STRUCTURE)
188 X(IOCTL_DVD_SEND_KEY)
189 X(IOCTL_DVD_START_SESSION)
190 X(IOCTL_SCSI_GET_ADDRESS)
191 X(IOCTL_SCSI_GET_CAPABILITIES)
192 X(IOCTL_SCSI_GET_INQUIRY_DATA)
193 X(IOCTL_SCSI_PASS_THROUGH)
194 X(IOCTL_SCSI_PASS_THROUGH_DIRECT)
195 X(IOCTL_STORAGE_CHECK_VERIFY)
196 X(IOCTL_STORAGE_CHECK_VERIFY2)
197 X(IOCTL_STORAGE_EJECTION_CONTROL)
198 X(IOCTL_STORAGE_EJECT_MEDIA)
199 X(IOCTL_STORAGE_GET_DEVICE_NUMBER)
200 X(IOCTL_STORAGE_GET_MEDIA_TYPES)
201 X(IOCTL_STORAGE_GET_MEDIA_TYPES_EX)
202 X(IOCTL_STORAGE_LOAD_MEDIA)
203 X(IOCTL_STORAGE_MEDIA_REMOVAL)
204 X(IOCTL_STORAGE_RESET_DEVICE)
205 #undef X
207 static const char *iocodex(DWORD code)
209 unsigned int i;
210 static char buffer[25];
211 for(i=0; i<sizeof(iocodextable)/sizeof(struct iocodexs); i++)
212 if (code==iocodextable[i].code)
213 return iocodextable[i].codex;
214 sprintf(buffer, "IOCTL_CODE_%x", (int)code);
215 return buffer;
218 #define INQ_REPLY_LEN 36
219 #define INQ_CMD_LEN 6
221 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
222 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
223 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
224 #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;}
226 /* The documented format of DVD_LAYER_DESCRIPTOR is wrong. Even the format in the
227 * DDK's header is wrong. There are four bytes at the start defined by
228 * MMC-5. The first two are the size of the structure in big-endian order as
229 * defined by MMC-5. The other two are reserved.
231 typedef struct
233 DVD_DESCRIPTOR_HEADER Header;
234 DVD_LAYER_DESCRIPTOR Descriptor;
235 UCHAR Padding;
236 } internal_dvd_layer_descriptor;
237 C_ASSERT(sizeof(internal_dvd_layer_descriptor) == 22);
239 typedef struct
241 DVD_DESCRIPTOR_HEADER Header;
242 DVD_MANUFACTURER_DESCRIPTOR Descriptor;
243 UCHAR Padding;
244 } internal_dvd_manufacturer_descriptor;
245 C_ASSERT(sizeof(internal_dvd_manufacturer_descriptor) == 2053);
247 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
248 static NTSTATUS CDROM_GetStatusCode(int);
251 #ifdef linux
253 # ifndef IDE6_MAJOR
254 # define IDE6_MAJOR 88
255 # endif
256 # ifndef IDE7_MAJOR
257 # define IDE7_MAJOR 89
258 # endif
260 # ifdef CDROM_SEND_PACKET
261 /* structure for CDROM_PACKET_COMMAND ioctl */
262 /* not all Linux versions have all the fields, so we define the
263 * structure ourselves to make sure */
264 struct linux_cdrom_generic_command
266 unsigned char cmd[CDROM_PACKET_SIZE];
267 unsigned char *buffer;
268 unsigned int buflen;
269 int stat;
270 struct request_sense *sense;
271 unsigned char data_direction;
272 int quiet;
273 int timeout;
274 void *reserved[1];
276 # endif /* CDROM_SEND_PACKET */
278 #endif /* linux */
280 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
281 * this should be removed when a proper device interface is implemented
283 * (WS) We need this to keep track of current position and to safely
284 * detect media changes. Besides this should provide a great speed up
285 * for toc inquiries.
287 struct cdrom_cache {
288 dev_t device;
289 ino_t inode;
290 char toc_good; /* if false, will reread TOC from disk */
291 CDROM_TOC toc;
292 SUB_Q_CURRENT_POSITION CurrentPosition;
294 /* who has more than 5 cdroms on his/her machine ?? */
295 /* FIXME: this should grow depending on the number of cdroms we install/configure
296 * at startup
298 #define MAX_CACHE_ENTRIES 5
299 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
301 static RTL_CRITICAL_SECTION cache_section;
302 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
304 0, 0, &cache_section,
305 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
306 0, 0, { (DWORD_PTR)(__FILE__ ": cache_section") }
308 static RTL_CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
310 /* Proposed media change function: not really needed at this time */
311 /* This is a 1 or 0 type of function */
312 #if 0
313 static int CDROM_MediaChanged(int dev)
315 int i;
317 struct cdrom_tochdr hdr;
318 struct cdrom_tocentry entry;
320 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
321 return 0;
322 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
323 return 0;
325 if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
326 return 1;
328 for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
330 if (i == hdr.cdth_trk1 + 1)
332 entry.cdte_track = CDROM_LEADOUT;
333 } else {
334 entry.cdte_track = i;
336 entry.cdte_format = CDROM_MSF;
337 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
338 return 0;
339 if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
340 sizeof(struct cdrom_tocentry)) )
341 return 1;
343 return 0;
345 #endif
348 /******************************************************************
349 * get_parent_device
351 * On Mac OS, get the device for the whole disk from a fd that points to a partition.
352 * This is ugly and inefficient, but we have no choice since the partition fd doesn't
353 * support the eject ioctl.
355 #ifdef __APPLE__
356 static NTSTATUS get_parent_device( int fd, char *name, size_t len )
358 NTSTATUS status = STATUS_NO_SUCH_FILE;
359 struct stat st;
360 int i;
361 io_service_t service;
362 CFMutableDictionaryRef dict;
363 CFTypeRef val;
365 if (fstat( fd, &st ) == -1) return FILE_GetNtStatus();
366 if (!S_ISCHR( st.st_mode )) return STATUS_OBJECT_TYPE_MISMATCH;
368 /* create a dictionary with the right major/minor numbers */
370 if (!(dict = IOServiceMatching( kIOMediaClass ))) return STATUS_NO_MEMORY;
372 i = major( st.st_rdev );
373 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
374 CFDictionaryAddValue( dict, CFSTR( "BSD Major" ), val );
375 CFRelease( val );
377 i = minor( st.st_rdev );
378 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
379 CFDictionaryAddValue( dict, CFSTR( "BSD Minor" ), val );
380 CFRelease( val );
382 CFDictionaryAddValue( dict, CFSTR("Removable"), kCFBooleanTrue );
384 service = IOServiceGetMatchingService( kIOMasterPortDefault, dict );
386 /* now look for the parent that has the "Whole" attribute set to TRUE */
388 while (service)
390 io_service_t parent = 0;
391 CFBooleanRef whole;
392 CFStringRef str;
393 int ok;
395 if (!IOObjectConformsTo( service, kIOMediaClass ))
396 goto next;
397 if (!(whole = IORegistryEntryCreateCFProperty( service, CFSTR("Whole"), NULL, 0 )))
398 goto next;
399 ok = (whole == kCFBooleanTrue);
400 CFRelease( whole );
401 if (!ok) goto next;
403 if ((str = IORegistryEntryCreateCFProperty( service, CFSTR("BSD Name"), NULL, 0 )))
405 strcpy( name, "/dev/r" );
406 CFStringGetCString( str, name + 6, len - 6, kCFStringEncodingUTF8 );
407 CFRelease( str );
408 status = STATUS_SUCCESS;
410 IOObjectRelease( service );
411 break;
413 next:
414 IORegistryEntryGetParentEntry( service, kIOServicePlane, &parent );
415 IOObjectRelease( service );
416 service = parent;
418 return status;
420 #endif
423 /******************************************************************
424 * CDROM_SyncCache [internal]
426 * Read the TOC in and store it in the cdrom_cache structure.
427 * Further requests for the TOC will be copied from the cache
428 * unless certain events like disk ejection is detected, in which
429 * case the cache will be cleared, causing it to be resynced.
430 * The cache section must be held by caller.
432 static NTSTATUS CDROM_SyncCache(int dev, int fd)
434 #ifdef linux
435 int i, tsz;
436 struct cdrom_tochdr hdr;
437 struct cdrom_tocentry entry;
439 CDROM_TOC *toc = &cdrom_cache[dev].toc;
440 cdrom_cache[dev].toc_good = 0;
442 if (ioctl(fd, CDROMREADTOCHDR, &hdr) == -1)
444 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
445 return FILE_GetNtStatus();
448 toc->FirstTrack = hdr.cdth_trk0;
449 toc->LastTrack = hdr.cdth_trk1;
450 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
451 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
452 toc->Length[0] = tsz >> 8;
453 toc->Length[1] = tsz;
455 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
457 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
459 if (i == toc->LastTrack + 1)
460 entry.cdte_track = CDROM_LEADOUT;
461 else
462 entry.cdte_track = i;
463 entry.cdte_format = CDROM_MSF;
464 if (ioctl(fd, CDROMREADTOCENTRY, &entry) == -1)
466 WARN("error read entry (%s)\n", strerror(errno));
467 return FILE_GetNtStatus();
469 toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
470 toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
471 /* marking last track with leadout value as index */
472 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
473 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
474 toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
475 toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
476 toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
478 cdrom_cache[dev].toc_good = 1;
479 return STATUS_SUCCESS;
481 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
483 int i, tsz;
484 struct ioc_toc_header hdr;
485 struct ioc_read_toc_entry entry;
486 struct cd_toc_entry toc_buffer;
488 CDROM_TOC *toc = &cdrom_cache[dev].toc;
489 cdrom_cache[dev].toc_good = 0;
491 if (ioctl(fd, CDIOREADTOCHEADER, &hdr) == -1)
493 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
494 return FILE_GetNtStatus();
496 toc->FirstTrack = hdr.starting_track;
497 toc->LastTrack = hdr.ending_track;
498 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
499 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
500 toc->Length[0] = tsz >> 8;
501 toc->Length[1] = tsz;
503 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
505 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
507 if (i == toc->LastTrack + 1)
509 #define LEADOUT 0xaa
510 entry.starting_track = LEADOUT;
511 } else {
512 entry.starting_track = i;
514 memset(&toc_buffer, 0, sizeof(toc_buffer));
515 entry.address_format = CD_MSF_FORMAT;
516 entry.data_len = sizeof(toc_buffer);
517 entry.data = &toc_buffer;
518 if (ioctl(fd, CDIOREADTOCENTRYS, &entry) == -1)
520 WARN("error read entry (%s)\n", strerror(errno));
521 return FILE_GetNtStatus();
523 toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
524 toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
525 /* marking last track with leadout value as index */
526 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
527 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
528 toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
529 toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
530 toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
532 cdrom_cache[dev].toc_good = 1;
533 return STATUS_SUCCESS;
535 #elif defined(__APPLE__)
536 int i;
537 dk_cd_read_toc_t hdr;
538 CDROM_TOC *toc = &cdrom_cache[dev].toc;
539 cdrom_cache[dev].toc_good = 0;
541 memset( &hdr, 0, sizeof(hdr) );
542 hdr.buffer = toc;
543 hdr.bufferLength = sizeof(*toc);
544 if (ioctl(fd, DKIOCCDREADTOC, &hdr) == -1)
546 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
547 return FILE_GetNtStatus();
549 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
551 /* convert address format */
552 TRACK_DATA *data = &toc->TrackData[i - toc->FirstTrack];
553 DWORD frame = (((DWORD)data->Address[0] << 24) | ((DWORD)data->Address[1] << 16) |
554 ((DWORD)data->Address[2] << 8) | data->Address[3]);
555 MSF_OF_FRAME( data->Address[1], frame );
556 data->Address[0] = 0;
559 cdrom_cache[dev].toc_good = 1;
560 return STATUS_SUCCESS;
561 #else
562 FIXME("not supported on this O/S\n");
563 return STATUS_NOT_SUPPORTED;
564 #endif
567 static void CDROM_ClearCacheEntry(int dev)
569 RtlEnterCriticalSection( &cache_section );
570 cdrom_cache[dev].toc_good = 0;
571 RtlLeaveCriticalSection( &cache_section );
576 /******************************************************************
577 * CDROM_GetInterfaceInfo
579 * Determines the ide interface (the number after the ide), and the
580 * number of the device on that interface for ide cdroms (*iface <= 1).
581 * Determines the scsi information for scsi cdroms (*iface >= 2).
582 * Returns FALSE if the info cannot not be obtained.
584 static BOOL CDROM_GetInterfaceInfo(int fd, UCHAR* iface, UCHAR* port, UCHAR* device, UCHAR* lun)
586 #if defined(linux)
587 struct stat st;
588 if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode)) return FALSE;
589 *port = 0;
590 *iface = 0;
591 *device = 0;
592 *lun = 0;
593 switch (major(st.st_rdev)) {
594 case IDE0_MAJOR: *iface = 0; break;
595 case IDE1_MAJOR: *iface = 1; break;
596 case IDE2_MAJOR: *iface = 2; break;
597 case IDE3_MAJOR: *iface = 3; break;
598 case IDE4_MAJOR: *iface = 4; break;
599 case IDE5_MAJOR: *iface = 5; break;
600 case IDE6_MAJOR: *iface = 6; break;
601 case IDE7_MAJOR: *iface = 7; break;
602 default: *port = 1; break;
605 if (*port == 0)
606 *device = (minor(st.st_rdev) >> 6);
607 else
609 #ifdef SCSI_IOCTL_GET_IDLUN
610 UINT32 idlun[2];
611 if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, idlun) != -1)
613 *port = (idlun[0] >> 24) & 0xff;
614 *iface = ((idlun[0] >> 16) & 0xff) + 2;
615 *device = idlun[0] & 0xff;
616 *lun = (idlun[0] >> 8) & 0xff;
618 else
619 #endif
621 WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
622 return FALSE;
625 return TRUE;
626 #elif defined(__NetBSD__)
627 struct scsi_addr addr;
628 if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
630 switch (addr.type)
632 case TYPE_SCSI:
633 *port = 1;
634 *iface = addr.addr.scsi.scbus;
635 *device = addr.addr.scsi.target;
636 *lun = addr.addr.scsi.lun;
637 return TRUE;
638 case TYPE_ATAPI:
639 *port = 0;
640 *iface = addr.addr.atapi.atbus;
641 *device = addr.addr.atapi.drive;
642 *lun = 0;
643 return TRUE;
646 return FALSE;
647 #elif defined(__APPLE__)
648 dk_scsi_identify_t addr;
649 if (ioctl(fd, DKIOCSCSIIDENTIFY, &addr) != -1)
651 *port = addr.bus;
652 *iface = addr.port;
653 *device = addr.target;
654 *lun = addr.lun;
655 return TRUE;
657 return FALSE;
658 #else
659 FIXME("not implemented on this O/S\n");
660 return FALSE;
661 #endif
665 /******************************************************************
666 * CDROM_Open
669 static NTSTATUS CDROM_Open(int fd, int* dev)
671 struct stat st;
672 NTSTATUS ret = STATUS_SUCCESS;
673 int empty = -1;
675 if (fstat(fd, &st) == -1) return FILE_GetNtStatus();
677 RtlEnterCriticalSection( &cache_section );
678 for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
680 if (empty == -1 &&
681 cdrom_cache[*dev].device == 0 &&
682 cdrom_cache[*dev].inode == 0)
683 empty = *dev;
684 else if (cdrom_cache[*dev].device == st.st_dev &&
685 cdrom_cache[*dev].inode == st.st_ino)
686 break;
688 if (*dev == MAX_CACHE_ENTRIES)
690 if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
691 else
693 *dev = empty;
694 cdrom_cache[*dev].device = st.st_dev;
695 cdrom_cache[*dev].inode = st.st_ino;
698 RtlLeaveCriticalSection( &cache_section );
700 TRACE("%d, %d\n", *dev, fd);
701 return ret;
704 /******************************************************************
705 * CDROM_GetStatusCode
709 static NTSTATUS CDROM_GetStatusCode(int io)
711 if (io == 0) return STATUS_SUCCESS;
712 return FILE_GetNtStatus();
715 /******************************************************************
716 * CDROM_GetControl
719 static NTSTATUS CDROM_GetControl(int dev, int fd, CDROM_AUDIO_CONTROL* cac)
721 #ifdef __APPLE__
722 uint16_t speed;
723 int io = ioctl( fd, DKIOCCDGETSPEED, &speed );
724 if (io != 0) return CDROM_GetStatusCode( io );
725 /* DKIOCCDGETSPEED returns the speed in kilobytes per second,
726 * so convert to logical blocks (assumed to be ~2 KB).
728 cac->LogicalBlocksPerSecond = speed/2;
729 #else
730 cac->LogicalBlocksPerSecond = 1; /* FIXME */
731 #endif
732 cac->LbaFormat = 0; /* FIXME */
733 return STATUS_NOT_SUPPORTED;
736 /******************************************************************
737 * CDROM_GetDeviceNumber
740 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
742 FIXME( "stub\n" );
743 devnum->DeviceType = FILE_DEVICE_DISK;
744 devnum->DeviceNumber = 1;
745 devnum->PartitionNumber = 1;
746 return STATUS_SUCCESS;
749 /******************************************************************
750 * CDROM_GetDriveGeometry
753 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
755 CDROM_TOC toc;
756 NTSTATUS ret;
757 int fsize;
759 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
761 fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
762 - FRAME_OF_TOC(toc, 1); /* Total size in frames */
764 dg->Cylinders.QuadPart = fsize / (64 * 32);
765 dg->MediaType = RemovableMedia;
766 dg->TracksPerCylinder = 64;
767 dg->SectorsPerTrack = 32;
768 dg->BytesPerSector= 2048;
769 return ret;
772 /******************************************************************
773 * CDROM_GetMediaType
776 static NTSTATUS CDROM_GetMediaType(int dev, GET_MEDIA_TYPES* medtype)
778 FIXME(": faking success\n");
779 medtype->DeviceType = FILE_DEVICE_CD_ROM;
780 medtype->MediaInfoCount = 0;
781 return STATUS_SUCCESS;
784 /**************************************************************************
785 * CDROM_Reset [internal]
787 static NTSTATUS CDROM_ResetAudio(int fd)
789 #if defined(linux)
790 return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
791 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
792 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
793 #else
794 FIXME("not supported on this O/S\n");
795 return STATUS_NOT_SUPPORTED;
796 #endif
799 /******************************************************************
800 * CDROM_SetTray
804 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
806 #if defined(linux)
807 return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
808 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
809 return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
810 (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
811 (ioctl(fd, CDIOCPREVENT, NULL)));
812 #elif defined(__APPLE__)
813 if (doEject) return CDROM_GetStatusCode( ioctl( fd, DKIOCEJECT, NULL ) );
814 else return STATUS_NOT_SUPPORTED;
815 #else
816 FIXME("not supported on this O/S\n");
817 return STATUS_NOT_SUPPORTED;
818 #endif
821 /******************************************************************
822 * CDROM_ControlEjection
826 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
828 #if defined(linux)
829 return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
830 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
831 return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
832 #else
833 FIXME("not supported on this O/S\n");
834 return STATUS_NOT_SUPPORTED;
835 #endif
838 /******************************************************************
839 * CDROM_ReadTOC
843 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
845 NTSTATUS ret = STATUS_NOT_SUPPORTED;
847 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
848 return STATUS_INVALID_PARAMETER;
850 RtlEnterCriticalSection( &cache_section );
851 if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
853 *toc = cdrom_cache[dev].toc;
854 ret = STATUS_SUCCESS;
856 RtlLeaveCriticalSection( &cache_section );
857 return ret;
860 /******************************************************************
861 * CDROM_GetDiskData
865 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
867 CDROM_TOC toc;
868 NTSTATUS ret;
869 int i;
871 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
872 data->DiskData = 0;
873 for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
874 if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
875 data->DiskData |= CDROM_DISK_DATA_TRACK;
876 else
877 data->DiskData |= CDROM_DISK_AUDIO_TRACK;
879 return STATUS_SUCCESS;
882 /******************************************************************
883 * CDROM_ReadQChannel
887 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
888 SUB_Q_CHANNEL_DATA* data)
890 NTSTATUS ret = STATUS_NOT_SUPPORTED;
891 #ifdef linux
892 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
893 int io;
894 struct cdrom_subchnl sc;
895 sc.cdsc_format = CDROM_MSF;
897 io = ioctl(fd, CDROMSUBCHNL, &sc);
898 if (io == -1)
900 TRACE("opened or no_media (%s)!\n", strerror(errno));
901 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
902 CDROM_ClearCacheEntry(dev);
903 goto end;
906 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
908 switch (sc.cdsc_audiostatus) {
909 case CDROM_AUDIO_INVALID:
910 CDROM_ClearCacheEntry(dev);
911 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
912 break;
913 case CDROM_AUDIO_NO_STATUS:
914 CDROM_ClearCacheEntry(dev);
915 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
916 break;
917 case CDROM_AUDIO_PLAY:
918 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
919 break;
920 case CDROM_AUDIO_PAUSED:
921 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
922 break;
923 case CDROM_AUDIO_COMPLETED:
924 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
925 break;
926 case CDROM_AUDIO_ERROR:
927 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
928 break;
929 default:
930 TRACE("status=%02X !\n", sc.cdsc_audiostatus);
931 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
933 switch (fmt->Format)
935 case IOCTL_CDROM_CURRENT_POSITION:
936 RtlEnterCriticalSection( &cache_section );
937 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
938 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
939 data->CurrentPosition.Control = sc.cdsc_ctrl;
940 data->CurrentPosition.ADR = sc.cdsc_adr;
941 data->CurrentPosition.TrackNumber = sc.cdsc_trk;
942 data->CurrentPosition.IndexNumber = sc.cdsc_ind;
944 data->CurrentPosition.AbsoluteAddress[0] = 0;
945 data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute;
946 data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
947 data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
949 data->CurrentPosition.TrackRelativeAddress[0] = 0;
950 data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute;
951 data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
952 data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
954 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
956 else /* not playing */
958 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
959 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
961 RtlLeaveCriticalSection( &cache_section );
962 break;
963 case IOCTL_CDROM_MEDIA_CATALOG:
964 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
966 struct cdrom_mcn mcn;
967 if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
969 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
970 data->MediaCatalog.Mcval = 0; /* FIXME */
971 memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
972 data->MediaCatalog.MediaCatalog[14] = 0;
974 break;
975 case IOCTL_CDROM_TRACK_ISRC:
976 FIXME("TrackIsrc: NIY on linux\n");
977 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
978 data->TrackIsrc.Tcval = 0;
979 io = 0;
980 break;
983 end:
984 ret = CDROM_GetStatusCode(io);
985 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
986 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
987 int io;
988 struct ioc_read_subchannel read_sc;
989 struct cd_sub_channel_info sc;
991 read_sc.address_format = CD_MSF_FORMAT;
992 read_sc.track = 0;
993 read_sc.data_len = sizeof(sc);
994 read_sc.data = &sc;
995 switch (fmt->Format)
997 case IOCTL_CDROM_CURRENT_POSITION:
998 read_sc.data_format = CD_CURRENT_POSITION;
999 break;
1000 case IOCTL_CDROM_MEDIA_CATALOG:
1001 read_sc.data_format = CD_MEDIA_CATALOG;
1002 break;
1003 case IOCTL_CDROM_TRACK_ISRC:
1004 read_sc.data_format = CD_TRACK_INFO;
1005 sc.what.track_info.track_number = data->TrackIsrc.Track;
1006 break;
1008 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1009 if (io == -1)
1011 TRACE("opened or no_media (%s)!\n", strerror(errno));
1012 CDROM_ClearCacheEntry(dev);
1013 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1014 goto end;
1017 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
1019 switch (sc.header.audio_status) {
1020 case CD_AS_AUDIO_INVALID:
1021 CDROM_ClearCacheEntry(dev);
1022 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
1023 break;
1024 case CD_AS_NO_STATUS:
1025 CDROM_ClearCacheEntry(dev);
1026 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1027 break;
1028 case CD_AS_PLAY_IN_PROGRESS:
1029 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
1030 break;
1031 case CD_AS_PLAY_PAUSED:
1032 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
1033 break;
1034 case CD_AS_PLAY_COMPLETED:
1035 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
1036 break;
1037 case CD_AS_PLAY_ERROR:
1038 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
1039 break;
1040 default:
1041 TRACE("status=%02X !\n", sc.header.audio_status);
1042 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1044 switch (fmt->Format)
1046 case IOCTL_CDROM_CURRENT_POSITION:
1047 RtlEnterCriticalSection( &cache_section );
1048 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
1049 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1050 data->CurrentPosition.Control = sc.what.position.control;
1051 data->CurrentPosition.ADR = sc.what.position.addr_type;
1052 data->CurrentPosition.TrackNumber = sc.what.position.track_number;
1053 data->CurrentPosition.IndexNumber = sc.what.position.index_number;
1055 data->CurrentPosition.AbsoluteAddress[0] = 0;
1056 data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
1057 data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
1058 data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
1059 data->CurrentPosition.TrackRelativeAddress[0] = 0;
1060 data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
1061 data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
1062 data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
1063 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
1065 else { /* not playing */
1066 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
1067 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
1069 RtlLeaveCriticalSection( &cache_section );
1070 break;
1071 case IOCTL_CDROM_MEDIA_CATALOG:
1072 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
1073 data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
1074 memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
1075 break;
1076 case IOCTL_CDROM_TRACK_ISRC:
1077 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
1078 data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
1079 memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
1080 break;
1083 end:
1084 ret = CDROM_GetStatusCode(io);
1085 #elif defined(__APPLE__)
1086 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
1087 int io = 0;
1088 union
1090 dk_cd_read_mcn_t mcn;
1091 dk_cd_read_isrc_t isrc;
1092 } ioc;
1093 /* We need IOCDAudioControl for IOCTL_CDROM_CURRENT_POSITION */
1094 if (fmt->Format == IOCTL_CDROM_CURRENT_POSITION)
1096 FIXME("NIY\n");
1097 return STATUS_NOT_SUPPORTED;
1099 /* No IOCDAudioControl support; just set the audio status to none */
1100 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1101 switch(fmt->Format)
1103 case IOCTL_CDROM_MEDIA_CATALOG:
1104 if ((io = ioctl(fd, DKIOCCDREADMCN, &ioc.mcn)) == -1) break;
1105 memcpy(data->MediaCatalog.MediaCatalog, ioc.mcn.mcn, kCDMCNMaxLength);
1106 data->MediaCatalog.Mcval = 1;
1107 break;
1108 case IOCTL_CDROM_TRACK_ISRC:
1109 ioc.isrc.track = fmt->Track;
1110 if ((io = ioctl(fd, DKIOCCDREADISRC, &ioc.isrc)) == -1) break;
1111 memcpy(data->TrackIsrc.TrackIsrc, ioc.isrc.isrc, kCDISRCMaxLength);
1112 data->TrackIsrc.Tcval = 1;
1113 data->TrackIsrc.Track = ioc.isrc.track;
1115 ret = CDROM_GetStatusCode(io);
1116 #endif
1117 return ret;
1120 /******************************************************************
1121 * CDROM_Verify
1122 * Implements: IOCTL_STORAGE_CHECK_VERIFY
1123 * IOCTL_CDROM_CHECK_VERIFY
1124 * IOCTL_DISK_CHECK_VERIFY
1127 static NTSTATUS CDROM_Verify(int dev, int fd)
1129 #if defined(linux)
1130 int ret;
1132 ret = ioctl(fd, CDROM_DRIVE_STATUS, NULL);
1133 if(ret == -1) {
1134 TRACE("ioctl CDROM_DRIVE_STATUS failed(%s)!\n", strerror(errno));
1135 return CDROM_GetStatusCode(ret);
1138 if(ret == CDS_DISC_OK)
1139 return STATUS_SUCCESS;
1140 else
1141 return STATUS_NO_MEDIA_IN_DEVICE;
1142 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1143 int ret;
1144 ret = ioctl(fd, CDIOCSTART, NULL);
1145 if(ret == 0)
1146 return STATUS_SUCCESS;
1147 else
1148 return STATUS_NO_MEDIA_IN_DEVICE;
1149 #elif defined(__APPLE__)
1150 /* At this point, we know that we have media, because in Mac OS X, the
1151 * device file is only created when media is present. */
1152 return STATUS_SUCCESS;
1153 #else
1154 FIXME("not supported on this O/S\n");
1155 return STATUS_NOT_SUPPORTED;
1156 #endif
1159 /******************************************************************
1160 * CDROM_PlayAudioMSF
1164 static NTSTATUS CDROM_PlayAudioMSF(int fd, const CDROM_PLAY_AUDIO_MSF* audio_msf)
1166 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1167 #ifdef linux
1168 struct cdrom_msf msf;
1169 int io;
1171 msf.cdmsf_min0 = audio_msf->StartingM;
1172 msf.cdmsf_sec0 = audio_msf->StartingS;
1173 msf.cdmsf_frame0 = audio_msf->StartingF;
1174 msf.cdmsf_min1 = audio_msf->EndingM;
1175 msf.cdmsf_sec1 = audio_msf->EndingS;
1176 msf.cdmsf_frame1 = audio_msf->EndingF;
1178 io = ioctl(fd, CDROMSTART);
1179 if (io == -1)
1181 WARN("motor doesn't start !\n");
1182 goto end;
1184 io = ioctl(fd, CDROMPLAYMSF, &msf);
1185 if (io == -1)
1187 WARN("device doesn't play !\n");
1188 goto end;
1190 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1191 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
1192 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
1193 end:
1194 ret = CDROM_GetStatusCode(io);
1195 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1196 struct ioc_play_msf msf;
1197 int io;
1199 msf.start_m = audio_msf->StartingM;
1200 msf.start_s = audio_msf->StartingS;
1201 msf.start_f = audio_msf->StartingF;
1202 msf.end_m = audio_msf->EndingM;
1203 msf.end_s = audio_msf->EndingS;
1204 msf.end_f = audio_msf->EndingF;
1206 io = ioctl(fd, CDIOCSTART, NULL);
1207 if (io == -1)
1209 WARN("motor doesn't start !\n");
1210 goto end;
1212 io = ioctl(fd, CDIOCPLAYMSF, &msf);
1213 if (io == -1)
1215 WARN("device doesn't play !\n");
1216 goto end;
1218 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1219 msf.start_m, msf.start_s, msf.start_f,
1220 msf.end_m, msf.end_s, msf.end_f);
1221 end:
1222 ret = CDROM_GetStatusCode(io);
1223 #endif
1224 return ret;
1227 /******************************************************************
1228 * CDROM_SeekAudioMSF
1232 static NTSTATUS CDROM_SeekAudioMSF(int dev, int fd, const CDROM_SEEK_AUDIO_MSF* audio_msf)
1234 CDROM_TOC toc;
1235 int i, io, frame;
1236 SUB_Q_CURRENT_POSITION *cp;
1237 #if defined(linux)
1238 struct cdrom_msf0 msf;
1239 struct cdrom_subchnl sc;
1240 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1241 struct ioc_play_msf msf;
1242 struct ioc_read_subchannel read_sc;
1243 struct cd_sub_channel_info sc;
1244 int final_frame;
1245 #endif
1247 /* Use the information on the TOC to compute the new current
1248 * position, which is shadowed on the cache. [Portable]. */
1249 frame = FRAME_OF_MSF(*audio_msf);
1251 if ((io = CDROM_ReadTOC(dev, fd, &toc)) != 0) return io;
1253 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
1254 if (FRAME_OF_TOC(toc,i)>frame) break;
1255 if (i <= toc.FirstTrack || i > toc.LastTrack+1)
1256 return STATUS_INVALID_PARAMETER;
1257 i--;
1258 RtlEnterCriticalSection( &cache_section );
1259 cp = &cdrom_cache[dev].CurrentPosition;
1260 cp->FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1261 cp->Control = toc.TrackData[i-toc.FirstTrack].Control;
1262 cp->ADR = toc.TrackData[i-toc.FirstTrack].Adr;
1263 cp->TrackNumber = toc.TrackData[i-toc.FirstTrack].TrackNumber;
1264 cp->IndexNumber = 0; /* FIXME: where do they keep these? */
1265 cp->AbsoluteAddress[0] = 0;
1266 cp->AbsoluteAddress[1] = toc.TrackData[i-toc.FirstTrack].Address[1];
1267 cp->AbsoluteAddress[2] = toc.TrackData[i-toc.FirstTrack].Address[2];
1268 cp->AbsoluteAddress[3] = toc.TrackData[i-toc.FirstTrack].Address[3];
1269 frame -= FRAME_OF_TOC(toc,i);
1270 cp->TrackRelativeAddress[0] = 0;
1271 MSF_OF_FRAME(cp->TrackRelativeAddress[1], frame);
1272 RtlLeaveCriticalSection( &cache_section );
1274 /* If playing, then issue a seek command, otherwise do nothing */
1275 #ifdef linux
1276 sc.cdsc_format = CDROM_MSF;
1278 io = ioctl(fd, CDROMSUBCHNL, &sc);
1279 if (io == -1)
1281 TRACE("opened or no_media (%s)!\n", strerror(errno));
1282 CDROM_ClearCacheEntry(dev);
1283 return CDROM_GetStatusCode(io);
1285 if (sc.cdsc_audiostatus==CDROM_AUDIO_PLAY)
1287 msf.minute = audio_msf->M;
1288 msf.second = audio_msf->S;
1289 msf.frame = audio_msf->F;
1290 return CDROM_GetStatusCode(ioctl(fd, CDROMSEEK, &msf));
1292 return STATUS_SUCCESS;
1293 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1294 read_sc.address_format = CD_MSF_FORMAT;
1295 read_sc.track = 0;
1296 read_sc.data_len = sizeof(sc);
1297 read_sc.data = &sc;
1298 read_sc.data_format = CD_CURRENT_POSITION;
1300 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1301 if (io == -1)
1303 TRACE("opened or no_media (%s)!\n", strerror(errno));
1304 CDROM_ClearCacheEntry(dev);
1305 return CDROM_GetStatusCode(io);
1307 if (sc.header.audio_status==CD_AS_PLAY_IN_PROGRESS)
1310 msf.start_m = audio_msf->M;
1311 msf.start_s = audio_msf->S;
1312 msf.start_f = audio_msf->F;
1313 final_frame = FRAME_OF_TOC(toc,toc.LastTrack+1)-1;
1314 MSF_OF_FRAME(msf.end_m, final_frame);
1316 return CDROM_GetStatusCode(ioctl(fd, CDIOCPLAYMSF, &msf));
1318 return STATUS_SUCCESS;
1319 #else
1320 FIXME("not supported on this O/S\n");
1321 return STATUS_NOT_SUPPORTED;
1322 #endif
1325 /******************************************************************
1326 * CDROM_PauseAudio
1330 static NTSTATUS CDROM_PauseAudio(int fd)
1332 #if defined(linux)
1333 return CDROM_GetStatusCode(ioctl(fd, CDROMPAUSE));
1334 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1335 return CDROM_GetStatusCode(ioctl(fd, CDIOCPAUSE, NULL));
1336 #else
1337 FIXME(": not supported on this O/S\n");
1338 return STATUS_NOT_SUPPORTED;
1339 #endif
1342 /******************************************************************
1343 * CDROM_ResumeAudio
1347 static NTSTATUS CDROM_ResumeAudio(int fd)
1349 #if defined(linux)
1350 return CDROM_GetStatusCode(ioctl(fd, CDROMRESUME));
1351 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1352 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESUME, NULL));
1353 #else
1354 FIXME("not supported on this O/S\n");
1355 return STATUS_NOT_SUPPORTED;
1356 #endif
1359 /******************************************************************
1360 * CDROM_StopAudio
1364 static NTSTATUS CDROM_StopAudio(int fd)
1366 #if defined(linux)
1367 return CDROM_GetStatusCode(ioctl(fd, CDROMSTOP));
1368 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1369 return CDROM_GetStatusCode(ioctl(fd, CDIOCSTOP, NULL));
1370 #else
1371 FIXME("not supported on this O/S\n");
1372 return STATUS_NOT_SUPPORTED;
1373 #endif
1376 /******************************************************************
1377 * CDROM_GetVolume
1381 static NTSTATUS CDROM_GetVolume(int fd, VOLUME_CONTROL* vc)
1383 #if defined(linux)
1384 struct cdrom_volctrl volc;
1385 int io;
1387 io = ioctl(fd, CDROMVOLREAD, &volc);
1388 if (io != -1)
1390 vc->PortVolume[0] = volc.channel0;
1391 vc->PortVolume[1] = volc.channel1;
1392 vc->PortVolume[2] = volc.channel2;
1393 vc->PortVolume[3] = volc.channel3;
1395 return CDROM_GetStatusCode(io);
1396 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1397 struct ioc_vol volc;
1398 int io;
1400 io = ioctl(fd, CDIOCGETVOL, &volc);
1401 if (io != -1)
1403 vc->PortVolume[0] = volc.vol[0];
1404 vc->PortVolume[1] = volc.vol[1];
1405 vc->PortVolume[2] = volc.vol[2];
1406 vc->PortVolume[3] = volc.vol[3];
1408 return CDROM_GetStatusCode(io);
1409 #else
1410 FIXME("not supported on this O/S\n");
1411 return STATUS_NOT_SUPPORTED;
1412 #endif
1415 /******************************************************************
1416 * CDROM_SetVolume
1420 static NTSTATUS CDROM_SetVolume(int fd, const VOLUME_CONTROL* vc)
1422 #if defined(linux)
1423 struct cdrom_volctrl volc;
1425 volc.channel0 = vc->PortVolume[0];
1426 volc.channel1 = vc->PortVolume[1];
1427 volc.channel2 = vc->PortVolume[2];
1428 volc.channel3 = vc->PortVolume[3];
1430 return CDROM_GetStatusCode(ioctl(fd, CDROMVOLCTRL, &volc));
1431 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1432 struct ioc_vol volc;
1434 volc.vol[0] = vc->PortVolume[0];
1435 volc.vol[1] = vc->PortVolume[1];
1436 volc.vol[2] = vc->PortVolume[2];
1437 volc.vol[3] = vc->PortVolume[3];
1439 return CDROM_GetStatusCode(ioctl(fd, CDIOCSETVOL, &volc));
1440 #else
1441 FIXME(": not supported on this O/S\n");
1442 return STATUS_NOT_SUPPORTED;
1443 #endif
1446 /******************************************************************
1447 * CDROM_RawRead
1449 * Some features of this IOCTL are rather poorly documented and
1450 * not really intuitive either:
1452 * 1. Although the DiskOffset parameter is meant to be a
1453 * byte offset into the disk, it is in fact the sector
1454 * number multiplied by 2048 regardless of the actual
1455 * sector size.
1457 * 2. The least significant 11 bits of DiskOffset are ignored.
1459 * 3. The TrackMode parameter has no effect on the sector
1460 * size. The entire raw sector (i.e. 2352 bytes of data)
1461 * is always returned. IMO the TrackMode is only used
1462 * to check the correct sector type.
1465 static NTSTATUS CDROM_RawRead(int fd, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
1467 int ret = STATUS_NOT_SUPPORTED;
1468 int io = -1;
1469 #ifdef __APPLE__
1470 dk_cd_read_t cdrd;
1471 #endif
1473 TRACE("RAW_READ_INFO: DiskOffset=%i,%i SectorCount=%i TrackMode=%i\n buffer=%p len=%i sz=%p\n",
1474 raw->DiskOffset.u.HighPart, raw->DiskOffset.u.LowPart, raw->SectorCount, raw->TrackMode, buffer, len, sz);
1476 if (len < raw->SectorCount * 2352) return STATUS_BUFFER_TOO_SMALL;
1478 #if defined(linux)
1479 if (raw->DiskOffset.u.HighPart & ~2047) {
1480 WARN("DiskOffset points to a sector >= 2**32\n");
1481 return ret;
1484 switch (raw->TrackMode)
1486 case YellowMode2:
1487 case XAForm2:
1489 DWORD lba = raw->DiskOffset.QuadPart >> 11;
1490 struct cdrom_msf* msf;
1491 PBYTE *bp; /* current buffer pointer */
1492 DWORD i;
1494 if ((lba + raw->SectorCount) >
1495 ((1 << 8*sizeof(msf->cdmsf_min0)) * CD_SECS * CD_FRAMES
1496 - CD_MSF_OFFSET)) {
1497 WARN("DiskOffset not accessible with MSF\n");
1498 return ret;
1501 /* Linux reads only one sector at a time.
1502 * ioctl CDROMREADRAW takes struct cdrom_msf as an argument
1503 * on the contrary to what header comments state.
1505 lba += CD_MSF_OFFSET;
1506 for (i = 0, bp = buffer; i < raw->SectorCount;
1507 i++, lba++, bp += 2352)
1509 msf = (struct cdrom_msf*)bp;
1510 msf->cdmsf_min0 = lba / CD_FRAMES / CD_SECS;
1511 msf->cdmsf_sec0 = lba / CD_FRAMES % CD_SECS;
1512 msf->cdmsf_frame0 = lba % CD_FRAMES;
1513 io = ioctl(fd, CDROMREADRAW, msf);
1514 if (io != 0)
1516 *sz = 2352 * i;
1517 return CDROM_GetStatusCode(io);
1520 break;
1523 case CDDA:
1525 struct cdrom_read_audio cdra;
1527 cdra.addr.lba = raw->DiskOffset.QuadPart >> 11;
1528 TRACE("reading at %u\n", cdra.addr.lba);
1529 cdra.addr_format = CDROM_LBA;
1530 cdra.nframes = raw->SectorCount;
1531 cdra.buf = buffer;
1532 io = ioctl(fd, CDROMREADAUDIO, &cdra);
1533 break;
1536 default:
1537 FIXME("NIY: %d\n", raw->TrackMode);
1538 return STATUS_INVALID_PARAMETER;
1540 #elif defined(__APPLE__)
1541 /* Mac OS lets us read multiple parts of the sector at a time.
1542 * We can read all the sectors in at once, unlike Linux.
1544 memset(&cdrd, 0, sizeof(cdrd));
1545 cdrd.offset = (raw->DiskOffset.QuadPart >> 11) * kCDSectorSizeWhole;
1546 cdrd.buffer = buffer;
1547 cdrd.bufferLength = raw->SectorCount * kCDSectorSizeWhole;
1548 switch (raw->TrackMode)
1550 case YellowMode2:
1551 cdrd.sectorType = kCDSectorTypeMode2;
1552 cdrd.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader | kCDSectorAreaUser;
1553 break;
1555 case XAForm2:
1556 cdrd.sectorType = kCDSectorTypeMode2Form2;
1557 cdrd.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader | kCDSectorAreaSubHeader | kCDSectorAreaUser;
1558 break;
1560 case CDDA:
1561 cdrd.sectorType = kCDSectorTypeCDDA;
1562 cdrd.sectorArea = kCDSectorAreaUser;
1563 break;
1565 default:
1566 FIXME("NIY: %d\n", raw->TrackMode);
1567 return STATUS_INVALID_PARAMETER;
1569 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1570 if (io != 0)
1572 *sz = cdrd.bufferLength;
1573 return CDROM_GetStatusCode(io);
1575 #else
1576 switch (raw->TrackMode)
1578 case YellowMode2:
1579 FIXME("YellowMode2: NIY\n");
1580 return ret;
1581 case XAForm2:
1582 FIXME("XAForm2: NIY\n");
1583 return ret;
1584 case CDDA:
1585 FIXME("CDDA: NIY\n");
1586 return ret;
1587 default:
1588 FIXME("NIY: %d\n", raw->TrackMode);
1589 return STATUS_INVALID_PARAMETER;
1591 #endif
1593 *sz = 2352 * raw->SectorCount;
1594 ret = CDROM_GetStatusCode(io);
1595 return ret;
1598 /******************************************************************
1599 * CDROM_ScsiPassThroughDirect
1600 * Implements IOCTL_SCSI_PASS_THROUGH_DIRECT
1603 static NTSTATUS CDROM_ScsiPassThroughDirect(int fd, PSCSI_PASS_THROUGH_DIRECT pPacket)
1605 int ret = STATUS_NOT_SUPPORTED;
1606 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1607 sg_io_hdr_t cmd;
1608 int io;
1609 #elif defined HAVE_SCSIREQ_T_CMD
1610 scsireq_t cmd;
1611 int io;
1612 #elif defined __APPLE__
1613 dk_scsi_command_t cmd;
1614 int io;
1615 #endif
1617 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1618 return STATUS_BUFFER_TOO_SMALL;
1620 if (pPacket->CdbLength > 16)
1621 return STATUS_INVALID_PARAMETER;
1623 #ifdef SENSEBUFLEN
1624 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1625 return STATUS_INVALID_PARAMETER;
1626 #elif defined HAVE_REQUEST_SENSE
1627 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1628 return STATUS_INVALID_PARAMETER;
1629 #endif
1631 if (pPacket->DataTransferLength > 0 && !pPacket->DataBuffer)
1632 return STATUS_INVALID_PARAMETER;
1634 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1635 RtlZeroMemory(&cmd, sizeof(cmd));
1637 cmd.interface_id = 'S';
1638 cmd.cmd_len = pPacket->CdbLength;
1639 cmd.mx_sb_len = pPacket->SenseInfoLength;
1640 cmd.dxfer_len = pPacket->DataTransferLength;
1641 cmd.dxferp = pPacket->DataBuffer;
1642 cmd.cmdp = pPacket->Cdb;
1643 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1644 cmd.timeout = pPacket->TimeOutValue*1000;
1646 switch (pPacket->DataIn)
1648 case SCSI_IOCTL_DATA_IN:
1649 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1650 break;
1651 case SCSI_IOCTL_DATA_OUT:
1652 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1653 break;
1654 case SCSI_IOCTL_DATA_UNSPECIFIED:
1655 cmd.dxfer_direction = SG_DXFER_NONE;
1656 break;
1657 default:
1658 return STATUS_INVALID_PARAMETER;
1661 io = ioctl(fd, SG_IO, &cmd);
1663 pPacket->ScsiStatus = cmd.status;
1664 pPacket->DataTransferLength = cmd.resid;
1665 pPacket->SenseInfoLength = cmd.sb_len_wr;
1667 ret = CDROM_GetStatusCode(io);
1669 #elif defined HAVE_SCSIREQ_T_CMD
1671 memset(&cmd, 0, sizeof(cmd));
1672 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1674 cmd.cmdlen = pPacket->CdbLength;
1675 cmd.databuf = pPacket->DataBuffer;
1676 cmd.datalen = pPacket->DataTransferLength;
1677 cmd.senselen = pPacket->SenseInfoLength;
1678 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1680 switch (pPacket->DataIn)
1682 case SCSI_IOCTL_DATA_OUT:
1683 cmd.flags |= SCCMD_WRITE;
1684 break;
1685 case SCSI_IOCTL_DATA_IN:
1686 cmd.flags |= SCCMD_READ;
1687 break;
1688 case SCSI_IOCTL_DATA_UNSPECIFIED:
1689 cmd.flags = 0;
1690 break;
1691 default:
1692 return STATUS_INVALID_PARAMETER;
1695 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1697 switch (cmd.retsts)
1699 case SCCMD_OK: break;
1700 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1701 break;
1702 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1703 break;
1704 case SCCMD_SENSE: break;
1705 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1706 break;
1709 if (pPacket->SenseInfoLength != 0)
1711 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1712 cmd.sense, pPacket->SenseInfoLength);
1715 pPacket->ScsiStatus = cmd.status;
1717 ret = CDROM_GetStatusCode(io);
1719 #elif defined(__APPLE__)
1721 memset(&cmd, 0, sizeof(cmd));
1722 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1724 cmd.cdbSize = pPacket->CdbLength;
1725 cmd.buffer = pPacket->DataBuffer;
1726 cmd.bufferSize = pPacket->DataTransferLength;
1727 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1728 cmd.senseLen = pPacket->SenseInfoLength;
1729 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1731 switch (pPacket->DataIn)
1733 case SCSI_IOCTL_DATA_OUT:
1734 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
1735 break;
1736 case SCSI_IOCTL_DATA_IN:
1737 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
1738 break;
1739 case SCSI_IOCTL_DATA_UNSPECIFIED:
1740 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
1741 break;
1742 default:
1743 return STATUS_INVALID_PARAMETER;
1746 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
1748 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
1750 /* Command failed */
1751 switch (cmd.status)
1753 case kSCSITaskStatus_TaskTimeoutOccurred: return STATUS_TIMEOUT;
1754 break;
1755 case kSCSITaskStatus_ProtocolTimeoutOccurred: return STATUS_IO_TIMEOUT;
1756 break;
1757 case kSCSITaskStatus_DeviceNotResponding: return STATUS_DEVICE_BUSY;
1758 break;
1759 case kSCSITaskStatus_DeviceNotPresent:
1760 return STATUS_NO_SUCH_DEVICE;
1761 break;
1762 case kSCSITaskStatus_DeliveryFailure:
1763 return STATUS_DEVICE_PROTOCOL_ERROR;
1764 break;
1765 case kSCSITaskStatus_No_Status:
1766 default:
1767 return STATUS_UNSUCCESSFUL;
1768 break;
1772 if (cmd.status != kSCSITaskStatus_No_Status)
1773 pPacket->ScsiStatus = cmd.status;
1775 ret = CDROM_GetStatusCode(io);
1776 #endif
1777 return ret;
1780 /******************************************************************
1781 * CDROM_ScsiPassThrough
1782 * Implements IOCTL_SCSI_PASS_THROUGH
1785 static NTSTATUS CDROM_ScsiPassThrough(int fd, PSCSI_PASS_THROUGH pPacket)
1787 int ret = STATUS_NOT_SUPPORTED;
1788 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1789 sg_io_hdr_t cmd;
1790 int io;
1791 #elif defined HAVE_SCSIREQ_T_CMD
1792 scsireq_t cmd;
1793 int io;
1794 #elif defined __APPLE__
1795 dk_scsi_command_t cmd;
1796 int io;
1797 #endif
1799 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1800 return STATUS_BUFFER_TOO_SMALL;
1802 if (pPacket->CdbLength > 16)
1803 return STATUS_INVALID_PARAMETER;
1805 #ifdef SENSEBUFLEN
1806 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1807 return STATUS_INVALID_PARAMETER;
1808 #elif defined HAVE_REQUEST_SENSE
1809 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1810 return STATUS_INVALID_PARAMETER;
1811 #endif
1813 if (pPacket->DataTransferLength > 0 && pPacket->DataBufferOffset < sizeof(SCSI_PASS_THROUGH))
1814 return STATUS_INVALID_PARAMETER;
1816 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1817 RtlZeroMemory(&cmd, sizeof(cmd));
1819 cmd.interface_id = 'S';
1820 cmd.dxfer_len = pPacket->DataTransferLength;
1821 cmd.dxferp = (char*)pPacket + pPacket->DataBufferOffset;
1822 cmd.cmd_len = pPacket->CdbLength;
1823 cmd.cmdp = pPacket->Cdb;
1824 cmd.mx_sb_len = pPacket->SenseInfoLength;
1825 cmd.timeout = pPacket->TimeOutValue*1000;
1827 if(cmd.mx_sb_len > 0)
1828 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1830 switch (pPacket->DataIn)
1832 case SCSI_IOCTL_DATA_IN:
1833 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1834 break;
1835 case SCSI_IOCTL_DATA_OUT:
1836 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1837 break;
1838 case SCSI_IOCTL_DATA_UNSPECIFIED:
1839 cmd.dxfer_direction = SG_DXFER_NONE;
1840 break;
1841 default:
1842 return STATUS_INVALID_PARAMETER;
1845 io = ioctl(fd, SG_IO, &cmd);
1847 pPacket->ScsiStatus = cmd.status;
1848 pPacket->DataTransferLength = cmd.resid;
1849 pPacket->SenseInfoLength = cmd.sb_len_wr;
1851 ret = CDROM_GetStatusCode(io);
1853 #elif defined HAVE_SCSIREQ_T_CMD
1855 memset(&cmd, 0, sizeof(cmd));
1856 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1858 if ( pPacket->DataBufferOffset > 0x1000 )
1860 cmd.databuf = (void*)pPacket->DataBufferOffset;
1862 else
1864 cmd.databuf = (char*)pPacket + pPacket->DataBufferOffset;
1867 cmd.cmdlen = pPacket->CdbLength;
1868 cmd.datalen = pPacket->DataTransferLength;
1869 cmd.senselen = pPacket->SenseInfoLength;
1870 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1872 switch (pPacket->DataIn)
1874 case SCSI_IOCTL_DATA_OUT:
1875 cmd.flags |= SCCMD_WRITE;
1876 break;
1877 case SCSI_IOCTL_DATA_IN:
1878 cmd.flags |= SCCMD_READ;
1879 break;
1880 case SCSI_IOCTL_DATA_UNSPECIFIED:
1881 cmd.flags = 0;
1882 break;
1883 default:
1884 return STATUS_INVALID_PARAMETER;
1887 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1889 switch (cmd.retsts)
1891 case SCCMD_OK: break;
1892 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1893 break;
1894 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1895 break;
1896 case SCCMD_SENSE: break;
1897 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1898 break;
1901 if (pPacket->SenseInfoLength != 0)
1903 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1904 cmd.sense, pPacket->SenseInfoLength);
1907 pPacket->ScsiStatus = cmd.status;
1909 ret = CDROM_GetStatusCode(io);
1911 #elif defined(__APPLE__)
1913 memset(&cmd, 0, sizeof(cmd));
1914 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1916 cmd.cdbSize = pPacket->CdbLength;
1917 cmd.buffer = (char*)pPacket + pPacket->DataBufferOffset;
1918 cmd.bufferSize = pPacket->DataTransferLength;
1919 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1920 cmd.senseLen = pPacket->SenseInfoLength;
1921 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1923 switch (pPacket->DataIn)
1925 case SCSI_IOCTL_DATA_OUT:
1926 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
1927 break;
1928 case SCSI_IOCTL_DATA_IN:
1929 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
1930 break;
1931 case SCSI_IOCTL_DATA_UNSPECIFIED:
1932 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
1933 break;
1934 default:
1935 return STATUS_INVALID_PARAMETER;
1938 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
1940 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
1942 /* Command failed */
1943 switch (cmd.status)
1945 case kSCSITaskStatus_TaskTimeoutOccurred:
1946 return STATUS_TIMEOUT;
1947 break;
1948 case kSCSITaskStatus_ProtocolTimeoutOccurred:
1949 return STATUS_IO_TIMEOUT;
1950 break;
1951 case kSCSITaskStatus_DeviceNotResponding:
1952 return STATUS_DEVICE_BUSY;
1953 break;
1954 case kSCSITaskStatus_DeviceNotPresent:
1955 return STATUS_NO_SUCH_DEVICE;
1956 break;
1957 case kSCSITaskStatus_DeliveryFailure:
1958 return STATUS_DEVICE_PROTOCOL_ERROR;
1959 break;
1960 case kSCSITaskStatus_No_Status:
1961 default:
1962 return STATUS_UNSUCCESSFUL;
1963 break;
1967 if (cmd.status != kSCSITaskStatus_No_Status)
1968 pPacket->ScsiStatus = cmd.status;
1970 ret = CDROM_GetStatusCode(io);
1971 #endif
1972 return ret;
1975 /******************************************************************
1976 * CDROM_ScsiGetCaps
1980 static NTSTATUS CDROM_ScsiGetCaps(int fd, PIO_SCSI_CAPABILITIES caps)
1982 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
1984 #ifdef SG_SCATTER_SZ
1985 caps->Length = sizeof(*caps);
1986 caps->MaximumTransferLength = SG_SCATTER_SZ; /* FIXME */
1987 caps->MaximumPhysicalPages = SG_SCATTER_SZ / page_size;
1988 caps->SupportedAsynchronousEvents = TRUE;
1989 caps->AlignmentMask = page_size;
1990 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
1991 caps->AdapterScansDown = FALSE; /* FIXME ? */
1992 caps->AdapterUsesPio = FALSE; /* FIXME ? */
1993 ret = STATUS_SUCCESS;
1994 #elif defined __APPLE__
1995 uint64_t bytesr, bytesw, align;
1996 int io = ioctl(fd, DKIOCGETMAXBYTECOUNTREAD, &bytesr);
1997 if (io != 0) return CDROM_GetStatusCode(io);
1998 io = ioctl(fd, DKIOCGETMAXBYTECOUNTWRITE, &bytesw);
1999 if (io != 0) return CDROM_GetStatusCode(io);
2000 io = ioctl(fd, DKIOCGETMINSEGMENTALIGNMENTBYTECOUNT, &align);
2001 if (io != 0) return CDROM_GetStatusCode(io);
2002 caps->Length = sizeof(*caps);
2003 caps->MaximumTransferLength = bytesr < bytesw ? bytesr : bytesw;
2004 caps->MaximumPhysicalPages = caps->MaximumTransferLength / page_size;
2005 caps->SupportedAsynchronousEvents = TRUE;
2006 caps->AlignmentMask = align-1;
2007 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
2008 caps->AdapterScansDown = FALSE; /* FIXME ? */
2009 caps->AdapterUsesPio = FALSE; /* FIXME ? */
2010 ret = STATUS_SUCCESS;
2011 #else
2012 FIXME("Unimplemented\n");
2013 #endif
2014 return ret;
2017 /******************************************************************
2018 * CDROM_GetAddress
2020 * implements IOCTL_SCSI_GET_ADDRESS
2022 static NTSTATUS CDROM_GetAddress(int fd, SCSI_ADDRESS* address)
2024 UCHAR portnum, busid, targetid, lun;
2026 address->Length = sizeof(SCSI_ADDRESS);
2027 if ( ! CDROM_GetInterfaceInfo(fd, &portnum, &busid, &targetid, &lun))
2028 return STATUS_NOT_SUPPORTED;
2030 address->PortNumber = portnum; /* primary=0 secondary=1 for ide */
2031 address->PathId = busid; /* always 0 for ide */
2032 address->TargetId = targetid; /* master=0 slave=1 for ide */
2033 address->Lun = lun;
2034 return STATUS_SUCCESS;
2037 /******************************************************************
2038 * DVD_StartSession
2042 static NTSTATUS DVD_StartSession(int fd, const DVD_SESSION_ID *sid_in, PDVD_SESSION_ID sid_out)
2044 #if defined(linux)
2045 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2046 dvd_authinfo auth_info;
2048 memset( &auth_info, 0, sizeof( auth_info ) );
2049 auth_info.type = DVD_LU_SEND_AGID;
2050 if (sid_in) auth_info.lsa.agid = *(const int*)sid_in; /* ?*/
2052 TRACE("fd 0x%08x\n",fd);
2053 ret =CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2054 *sid_out = auth_info.lsa.agid;
2055 return ret;
2056 #elif defined(__APPLE__)
2057 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2058 dk_dvd_report_key_t dvdrk;
2059 DVDAuthenticationGrantIDInfo agid_info;
2061 dvdrk.format = kDVDKeyFormatAGID_CSS;
2062 dvdrk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2063 if(sid_in) dvdrk.grantID = *(uint8_t*)sid_in; /* ? */
2064 dvdrk.bufferLength = sizeof(DVDAuthenticationGrantIDInfo);
2065 dvdrk.buffer = &agid_info;
2067 ret = CDROM_GetStatusCode(ioctl(fd, DKIOCDVDREPORTKEY, &dvdrk));
2068 *sid_out = agid_info.grantID;
2069 return ret;
2070 #else
2071 FIXME("not supported on this O/S\n");
2072 return STATUS_NOT_SUPPORTED;
2073 #endif
2076 /******************************************************************
2077 * DVD_EndSession
2081 static NTSTATUS DVD_EndSession(int fd, const DVD_SESSION_ID *sid)
2083 #if defined(linux)
2084 dvd_authinfo auth_info;
2086 memset( &auth_info, 0, sizeof( auth_info ) );
2087 auth_info.type = DVD_INVALIDATE_AGID;
2088 auth_info.lsa.agid = *(const int*)sid;
2090 TRACE("\n");
2091 return CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2092 #elif defined(__APPLE__)
2093 dk_dvd_send_key_t dvdsk;
2095 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2096 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2097 dvdsk.grantID = (uint8_t)*sid;
2099 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2100 #else
2101 FIXME("not supported on this O/S\n");
2102 return STATUS_NOT_SUPPORTED;
2103 #endif
2106 /******************************************************************
2107 * DVD_SendKey
2111 static NTSTATUS DVD_SendKey(int fd, const DVD_COPY_PROTECT_KEY *key)
2113 #if defined(linux)
2114 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2115 dvd_authinfo auth_info;
2117 memset( &auth_info, 0, sizeof( auth_info ) );
2118 switch (key->KeyType)
2120 case DvdChallengeKey:
2121 auth_info.type = DVD_HOST_SEND_CHALLENGE;
2122 auth_info.hsc.agid = (int)key->SessionId;
2123 TRACE("DvdChallengeKey ioc 0x%x\n", DVD_AUTH );
2124 memcpy( auth_info.hsc.chal, key->KeyData, DVD_CHALLENGE_SIZE );
2125 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2126 break;
2127 case DvdBusKey2:
2128 auth_info.type = DVD_HOST_SEND_KEY2;
2129 auth_info.hsk.agid = (int)key->SessionId;
2131 memcpy( auth_info.hsk.key, key->KeyData, DVD_KEY_SIZE );
2133 TRACE("DvdBusKey2\n");
2134 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2135 break;
2137 default:
2138 FIXME("Unknown Keytype 0x%x\n",key->KeyType);
2140 return ret;
2141 #elif defined(__APPLE__)
2142 dk_dvd_send_key_t dvdsk;
2143 union
2145 DVDChallengeKeyInfo chal;
2146 DVDKey2Info key2;
2147 } key_desc;
2149 switch(key->KeyType)
2151 case DvdChallengeKey:
2152 dvdsk.format = kDVDKeyFormatChallengeKey;
2153 dvdsk.bufferLength = sizeof(key_desc.chal);
2154 dvdsk.buffer = &key_desc.chal;
2155 OSWriteBigInt16(key_desc.chal.dataLength, 0, key->KeyLength);
2156 memcpy(key_desc.chal.challengeKeyValue, key->KeyData, key->KeyLength);
2157 break;
2158 case DvdBusKey2:
2159 dvdsk.format = kDVDKeyFormatKey2;
2160 dvdsk.bufferLength = sizeof(key_desc.key2);
2161 dvdsk.buffer = &key_desc.key2;
2162 OSWriteBigInt16(key_desc.key2.dataLength, 0, key->KeyLength);
2163 memcpy(key_desc.key2.key2Value, key->KeyData, key->KeyLength);
2164 break;
2165 case DvdInvalidateAGID:
2166 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2167 break;
2168 case DvdBusKey1:
2169 case DvdTitleKey:
2170 case DvdAsf:
2171 case DvdGetRpcKey:
2172 case DvdDiskKey:
2173 ERR("attempted to write read-only key type 0x%x\n", key->KeyType);
2174 return STATUS_NOT_SUPPORTED;
2175 case DvdSetRpcKey:
2176 FIXME("DvdSetRpcKey NIY\n");
2177 return STATUS_NOT_SUPPORTED;
2178 default:
2179 FIXME("got unknown key type 0x%x\n", key->KeyType);
2180 return STATUS_NOT_SUPPORTED;
2182 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2183 dvdsk.grantID = (uint8_t)key->SessionId;
2185 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2186 #else
2187 FIXME("not supported on this O/S\n");
2188 return STATUS_NOT_SUPPORTED;
2189 #endif
2192 /******************************************************************
2193 * DVD_ReadKey
2197 static NTSTATUS DVD_ReadKey(int fd, PDVD_COPY_PROTECT_KEY key)
2199 #if defined(linux)
2200 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2201 dvd_struct dvd;
2202 dvd_authinfo auth_info;
2204 memset( &dvd, 0, sizeof( dvd_struct ) );
2205 memset( &auth_info, 0, sizeof( auth_info ) );
2206 switch (key->KeyType)
2208 case DvdDiskKey:
2210 dvd.type = DVD_STRUCT_DISCKEY;
2211 dvd.disckey.agid = (int)key->SessionId;
2212 memset( dvd.disckey.value, 0, DVD_DISCKEY_SIZE );
2214 TRACE("DvdDiskKey\n");
2215 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2216 if (ret == STATUS_SUCCESS)
2217 memcpy(key->KeyData,dvd.disckey.value,DVD_DISCKEY_SIZE);
2218 break;
2219 case DvdTitleKey:
2220 auth_info.type = DVD_LU_SEND_TITLE_KEY;
2221 auth_info.lstk.agid = (int)key->SessionId;
2222 auth_info.lstk.lba = (int)(key->Parameters.TitleOffset.QuadPart>>11);
2223 TRACE("DvdTitleKey session %d Quadpart 0x%08lx offset 0x%08x\n",
2224 (int)key->SessionId, (long)key->Parameters.TitleOffset.QuadPart,
2225 auth_info.lstk.lba);
2226 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2227 if (ret == STATUS_SUCCESS)
2228 memcpy(key->KeyData, auth_info.lstk.title_key, DVD_KEY_SIZE );
2229 break;
2230 case DvdChallengeKey:
2232 auth_info.type = DVD_LU_SEND_CHALLENGE;
2233 auth_info.lsc.agid = (int)key->SessionId;
2235 TRACE("DvdChallengeKey\n");
2236 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2237 if (ret == STATUS_SUCCESS)
2238 memcpy( key->KeyData, auth_info.lsc.chal, DVD_CHALLENGE_SIZE );
2239 break;
2240 case DvdAsf:
2241 auth_info.type = DVD_LU_SEND_ASF;
2242 TRACE("DvdAsf\n");
2243 auth_info.lsasf.asf=((PDVD_ASF)key->KeyData)->SuccessFlag;
2244 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2245 ((PDVD_ASF)key->KeyData)->SuccessFlag = auth_info.lsasf.asf;
2246 break;
2247 case DvdBusKey1:
2248 auth_info.type = DVD_LU_SEND_KEY1;
2249 auth_info.lsk.agid = (int)key->SessionId;
2251 TRACE("DvdBusKey1\n");
2252 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2254 if (ret == STATUS_SUCCESS)
2255 memcpy( key->KeyData, auth_info.lsk.key, DVD_KEY_SIZE );
2256 break;
2257 case DvdGetRpcKey:
2258 auth_info.type = DVD_LU_SEND_RPC_STATE;
2260 TRACE("DvdGetRpcKey\n");
2261 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2263 if (ret == STATUS_SUCCESS)
2265 ((PDVD_RPC_KEY)key->KeyData)->TypeCode = auth_info.lrpcs.type;
2266 ((PDVD_RPC_KEY)key->KeyData)->RegionMask = auth_info.lrpcs.region_mask;
2267 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme = auth_info.lrpcs.rpc_scheme;
2268 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable = auth_info.lrpcs.ucca;
2269 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable = auth_info.lrpcs.vra;
2271 break;
2272 default:
2273 FIXME("Unknown keytype 0x%x\n",key->KeyType);
2275 return ret;
2276 #elif defined(__APPLE__)
2277 union
2279 dk_dvd_report_key_t key;
2280 dk_dvd_read_structure_t disk_key;
2281 } ioc;
2282 union
2284 DVDDiscKeyInfo disk_key;
2285 DVDChallengeKeyInfo chal;
2286 DVDKey1Info key1;
2287 DVDTitleKeyInfo title;
2288 DVDAuthenticationSuccessFlagInfo asf;
2289 DVDRegionPlaybackControlInfo rpc;
2290 } desc;
2291 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2293 switch(key->KeyType)
2295 case DvdChallengeKey:
2296 ioc.key.format = kDVDKeyFormatChallengeKey;
2297 ioc.key.grantID = (uint8_t)key->SessionId;
2298 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2299 ioc.key.bufferLength = sizeof(desc.chal);
2300 ioc.key.buffer = &desc.chal;
2301 OSWriteBigInt16(desc.chal.dataLength, 0, key->KeyLength);
2302 break;
2303 case DvdBusKey1:
2304 ioc.key.format = kDVDKeyFormatKey1;
2305 ioc.key.grantID = (uint8_t)key->SessionId;
2306 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2307 ioc.key.bufferLength = sizeof(desc.key1);
2308 ioc.key.buffer = &desc.key1;
2309 OSWriteBigInt16(desc.key1.dataLength, 0, key->KeyLength);
2310 break;
2311 case DvdTitleKey:
2312 ioc.key.format = kDVDKeyFormatTitleKey;
2313 ioc.key.grantID = (uint8_t)key->SessionId;
2314 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2315 ioc.key.bufferLength = sizeof(desc.title);
2316 ioc.key.buffer = &desc.title;
2317 ioc.key.address = (uint32_t)(key->Parameters.TitleOffset.QuadPart>>11);
2318 OSWriteBigInt16(desc.title.dataLength, 0, key->KeyLength);
2319 break;
2320 case DvdAsf:
2321 ioc.key.format = kDVDKeyFormatASF;
2322 ioc.key.grantID = (uint8_t)key->SessionId;
2323 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2324 ioc.key.bufferLength = sizeof(desc.asf);
2325 ioc.key.buffer = &desc.asf;
2326 OSWriteBigInt16(desc.asf.dataLength, 0, key->KeyLength);
2327 break;
2328 case DvdGetRpcKey:
2329 ioc.key.format = kDVDKeyFormatRegionState;
2330 ioc.key.grantID = (uint8_t)key->SessionId;
2331 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2332 ioc.key.bufferLength = sizeof(desc.rpc);
2333 ioc.key.buffer = &desc.rpc;
2334 OSWriteBigInt16(desc.rpc.dataLength, 0, key->KeyLength);
2335 break;
2336 case DvdDiskKey:
2337 ioc.disk_key.format = kDVDStructureFormatDiscKeyInfo;
2338 ioc.disk_key.grantID = (uint8_t)key->SessionId;
2339 ioc.disk_key.bufferLength = sizeof(desc.disk_key);
2340 ioc.disk_key.buffer = &desc.disk_key;
2341 break;
2342 case DvdInvalidateAGID:
2343 ioc.key.format = kDVDKeyFormatAGID_Invalidate;
2344 ioc.key.grantID = (uint8_t)key->SessionId;
2345 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2346 break;
2347 case DvdBusKey2:
2348 case DvdSetRpcKey:
2349 ERR("attempted to read write-only key type 0x%x\n", key->KeyType);
2350 return STATUS_NOT_SUPPORTED;
2351 default:
2352 FIXME("got unknown key type 0x%x\n", key->KeyType);
2353 return STATUS_NOT_SUPPORTED;
2356 ret = CDROM_GetStatusCode(ioctl(fd, (key->KeyType == DvdDiskKey ? DKIOCDVDREADSTRUCTURE : DKIOCDVDREPORTKEY), &ioc));
2358 if (ret == STATUS_SUCCESS)
2360 switch(key->KeyType)
2362 case DvdChallengeKey:
2363 key->KeyLength = OSReadBigInt16(desc.chal.dataLength, 0);
2364 memcpy(key->KeyData, desc.chal.challengeKeyValue, key->KeyLength);
2365 break;
2366 case DvdBusKey1:
2367 key->KeyLength = OSReadBigInt16(desc.key1.dataLength, 0);
2368 memcpy(key->KeyData, desc.key1.key1Value, key->KeyLength);
2369 break;
2370 case DvdTitleKey:
2371 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2372 memcpy(key->KeyData, desc.title.titleKeyValue, key->KeyLength);
2373 key->KeyFlags = 0;
2374 if (desc.title.CPM)
2376 /*key->KeyFlags |= DVD_COPYRIGHTED;*/
2377 if (desc.title.CP_SEC) key->KeyFlags |= DVD_SECTOR_PROTECTED;
2378 /*else key->KeyFlags |= DVD_SECTOR_NOT_PROTECTED;*/
2379 #if 0
2380 switch (desc.title.CGMS)
2382 case 0:
2383 key->KeyFlags |= DVD_CGMS_COPY_PERMITTED;
2384 break;
2385 case 2:
2386 key->KeyFlags |= DVD_CGMS_COPY_ONCE;
2387 break;
2388 case 3:
2389 key->KeyFlags |= DVD_CGMS_NO_COPY;
2390 break;
2392 #endif
2393 } /*else key->KeyFlags |= DVD_NOT_COPYRIGHTED;*/
2394 break;
2395 case DvdAsf:
2396 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2397 ((PDVD_ASF)key->KeyData)->SuccessFlag = desc.asf.successFlag;
2398 break;
2399 case DvdGetRpcKey:
2400 key->KeyLength = OSReadBigInt16(desc.rpc.dataLength, 0);
2401 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable =
2402 desc.rpc.numberUserResets;
2403 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable =
2404 desc.rpc.numberVendorResets;
2405 ((PDVD_RPC_KEY)key->KeyData)->TypeCode =
2406 desc.rpc.typeCode;
2407 ((PDVD_RPC_KEY)key->KeyData)->RegionMask =
2408 desc.rpc.driveRegion;
2409 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme =
2410 desc.rpc.rpcScheme;
2411 case DvdDiskKey:
2412 key->KeyLength = OSReadBigInt16(desc.disk_key.dataLength, 0);
2413 memcpy(key->KeyData, desc.disk_key.discKeyStructures, key->KeyLength);
2414 break;
2415 case DvdBusKey2:
2416 case DvdSetRpcKey:
2417 case DvdInvalidateAGID:
2418 default:
2419 /* Silence warning */
2423 return ret;
2424 #else
2425 FIXME("not supported on this O/S\n");
2426 return STATUS_NOT_SUPPORTED;
2427 #endif
2430 /******************************************************************
2431 * DVD_GetRegion
2433 * This IOCTL combines information from both IOCTL_DVD_READ_KEY
2434 * with key type DvdGetRpcKey and IOCTL_DVD_READ_STRUCTURE with
2435 * structure type DvdCopyrightInformation into one structure.
2437 static NTSTATUS DVD_GetRegion(int fd, PDVD_REGION region)
2439 #if defined(linux)
2440 NTSTATUS ret;
2441 dvd_struct dvd;
2442 dvd_authinfo auth_info;
2444 dvd.type = DVD_STRUCT_COPYRIGHT;
2445 dvd.copyright.layer_num = 0;
2446 auth_info.type = DVD_LU_SEND_RPC_STATE;
2448 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2450 if (ret == STATUS_SUCCESS)
2452 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2454 if (ret == STATUS_SUCCESS)
2456 region->CopySystem = dvd.copyright.cpst;
2457 region->RegionData = dvd.copyright.rmi;
2458 region->SystemRegion = auth_info.lrpcs.region_mask;
2459 region->ResetCount = auth_info.lrpcs.ucca;
2462 return ret;
2463 #elif defined(__APPLE__)
2464 dk_dvd_report_key_t key;
2465 dk_dvd_read_structure_t dvd;
2466 DVDRegionPlaybackControlInfo rpc;
2467 DVDCopyrightInfo copy;
2468 NTSTATUS ret;
2470 key.format = kDVDKeyFormatRegionState;
2471 key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2472 key.bufferLength = sizeof(rpc);
2473 key.buffer = &rpc;
2474 dvd.format = kDVDStructureFormatCopyrightInfo;
2475 dvd.bufferLength = sizeof(copy);
2476 dvd.buffer = &copy;
2478 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREPORTKEY, &key ));
2480 if (ret == STATUS_SUCCESS)
2482 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREADSTRUCTURE, &dvd ));
2484 if (ret == STATUS_SUCCESS)
2486 region->CopySystem = copy.copyrightProtectionSystemType;
2487 region->RegionData = copy.regionMask;
2488 region->SystemRegion = rpc.driveRegion;
2489 region->ResetCount = rpc.numberUserResets;
2492 return ret;
2493 #else
2494 FIXME("not supported on this O/S\n");
2495 return STATUS_NOT_SUPPORTED;
2496 #endif
2499 static DWORD DVD_ReadStructureSize(const DVD_READ_STRUCTURE *structure, DWORD size)
2501 if (!structure || size != sizeof(DVD_READ_STRUCTURE))
2502 return 0;
2504 switch (structure->Format)
2506 case DvdPhysicalDescriptor:
2507 return sizeof(DVD_LAYER_DESCRIPTOR);
2508 case DvdCopyrightDescriptor:
2509 return sizeof(DVD_COPYRIGHT_DESCRIPTOR);
2510 case DvdDiskKeyDescriptor:
2511 return sizeof(DVD_DISK_KEY_DESCRIPTOR);
2512 case DvdBCADescriptor:
2513 return sizeof(DVD_BCA_DESCRIPTOR);
2514 case DvdManufacturerDescriptor:
2515 return sizeof(DVD_MANUFACTURER_DESCRIPTOR);
2516 default:
2517 return 0;
2521 /******************************************************************
2522 * DVD_ReadStructure
2526 static NTSTATUS DVD_ReadStructure(int dev, const DVD_READ_STRUCTURE *structure, PDVD_LAYER_DESCRIPTOR layer)
2528 #ifdef DVD_READ_STRUCT
2529 /* dvd_struct is not defined consistently across platforms */
2530 union
2532 struct dvd_physical physical;
2533 struct dvd_copyright copyright;
2534 struct dvd_disckey disckey;
2535 struct dvd_bca bca;
2536 struct dvd_manufact manufact;
2537 } s;
2539 if (structure->BlockByteOffset.QuadPart) FIXME(": BlockByteOffset is not handled\n");
2541 switch (structure->Format)
2543 case DvdPhysicalDescriptor:
2544 s.physical.type = DVD_STRUCT_PHYSICAL;
2545 s.physical.layer_num = structure->LayerNumber;
2546 break;
2548 case DvdCopyrightDescriptor:
2549 s.copyright.type = DVD_STRUCT_COPYRIGHT;
2550 s.copyright.layer_num = structure->LayerNumber;
2551 break;
2553 case DvdDiskKeyDescriptor:
2554 s.disckey.type = DVD_STRUCT_DISCKEY;
2555 s.disckey.agid = structure->SessionId;
2556 break;
2558 case DvdBCADescriptor:
2559 s.bca.type = DVD_STRUCT_BCA;
2560 break;
2562 case DvdManufacturerDescriptor:
2563 s.manufact.type = DVD_STRUCT_MANUFACT;
2564 s.manufact.layer_num = structure->LayerNumber;
2565 break;
2567 case DvdMaxDescriptor: /* This is not a real request, no matter what MSDN says! */
2568 default:
2569 return STATUS_INVALID_PARAMETER;
2572 if (ioctl(dev, DVD_READ_STRUCT, &s) < 0)
2573 return STATUS_INVALID_PARAMETER;
2575 switch (structure->Format)
2577 case DvdPhysicalDescriptor:
2579 internal_dvd_layer_descriptor *p = (internal_dvd_layer_descriptor *) layer;
2580 struct dvd_layer *l = &s.physical.layer[s.physical.layer_num];
2582 p->Header.Length = 0x0802;
2583 p->Header.Reserved[0] = 0;
2584 p->Header.Reserved[1] = 0;
2585 p->Descriptor.BookVersion = l->book_version;
2586 p->Descriptor.BookType = l->book_type;
2587 p->Descriptor.MinimumRate = l->min_rate;
2588 p->Descriptor.DiskSize = l->disc_size;
2589 p->Descriptor.LayerType = l->layer_type;
2590 p->Descriptor.TrackPath = l->track_path;
2591 p->Descriptor.NumberOfLayers = l->nlayers;
2592 p->Descriptor.Reserved1 = 0;
2593 p->Descriptor.TrackDensity = l->track_density;
2594 p->Descriptor.LinearDensity = l->linear_density;
2595 p->Descriptor.StartingDataSector = GET_BE_DWORD(l->start_sector);
2596 p->Descriptor.EndDataSector = GET_BE_DWORD(l->end_sector);
2597 p->Descriptor.EndLayerZeroSector = GET_BE_DWORD(l->end_sector_l0);
2598 p->Descriptor.Reserved5 = 0;
2599 p->Descriptor.BCAFlag = l->bca;
2601 break;
2603 case DvdCopyrightDescriptor:
2605 PDVD_COPYRIGHT_DESCRIPTOR p = (PDVD_COPYRIGHT_DESCRIPTOR) layer;
2607 p->CopyrightProtectionType = s.copyright.cpst;
2608 p->RegionManagementInformation = s.copyright.rmi;
2609 p->Reserved = 0;
2611 break;
2613 case DvdDiskKeyDescriptor:
2615 PDVD_DISK_KEY_DESCRIPTOR p = (PDVD_DISK_KEY_DESCRIPTOR) layer;
2617 memcpy(p->DiskKeyData, s.disckey.value, 2048);
2619 break;
2621 case DvdBCADescriptor:
2623 PDVD_BCA_DESCRIPTOR p = (PDVD_BCA_DESCRIPTOR) layer;
2625 memcpy(p->BCAInformation, s.bca.value, s.bca.len);
2627 break;
2629 case DvdManufacturerDescriptor:
2631 internal_dvd_manufacturer_descriptor *p = (internal_dvd_manufacturer_descriptor*) layer;
2633 p->Header.Length = 0x0802;
2634 p->Header.Reserved[0] = 0;
2635 p->Header.Reserved[1] = 0;
2636 memcpy(p->Descriptor.ManufacturingInformation, s.manufact.value, 2048);
2638 break;
2640 case DvdMaxDescriptor: /* Suppress warning */
2641 break;
2643 #elif defined(__APPLE__)
2644 NTSTATUS ret;
2645 dk_dvd_read_structure_t dvdrs;
2646 union
2648 DVDPhysicalFormatInfo phys;
2649 DVDCopyrightInfo copy;
2650 DVDDiscKeyInfo disk_key;
2651 DVDManufacturingInfo manf;
2652 } desc;
2653 union
2655 PDVD_LAYER_DESCRIPTOR layer;
2656 internal_dvd_layer_descriptor *xlayer;
2657 PDVD_COPYRIGHT_DESCRIPTOR copy;
2658 PDVD_DISK_KEY_DESCRIPTOR disk_key;
2659 internal_dvd_manufacturer_descriptor *manf;
2660 } nt_desc;
2662 nt_desc.layer = layer;
2663 RtlZeroMemory(&dvdrs, sizeof(dvdrs));
2664 dvdrs.address = (uint32_t)(structure->BlockByteOffset.QuadPart>>11);
2665 dvdrs.grantID = (uint8_t)structure->SessionId;
2666 dvdrs.layer = structure->LayerNumber;
2667 switch(structure->Format)
2669 case DvdPhysicalDescriptor:
2670 dvdrs.format = kDVDStructureFormatPhysicalFormatInfo;
2671 dvdrs.bufferLength = sizeof(desc.phys);
2672 dvdrs.buffer = &desc.phys;
2673 break;
2675 case DvdCopyrightDescriptor:
2676 dvdrs.format = kDVDStructureFormatCopyrightInfo;
2677 dvdrs.bufferLength = sizeof(desc.copy);
2678 dvdrs.buffer = &desc.copy;
2679 break;
2681 case DvdDiskKeyDescriptor:
2682 dvdrs.format = kDVDStructureFormatDiscKeyInfo;
2683 dvdrs.bufferLength = sizeof(desc.disk_key);
2684 dvdrs.buffer = &desc.disk_key;
2685 break;
2687 case DvdBCADescriptor:
2688 FIXME("DvdBCADescriptor NIY\n");
2689 return STATUS_NOT_SUPPORTED;
2691 case DvdManufacturerDescriptor:
2692 dvdrs.format = kDVDStructureFormatManufacturingInfo;
2693 dvdrs.bufferLength = sizeof(desc.manf);
2694 dvdrs.buffer = &desc.manf;
2695 break;
2697 case DvdMaxDescriptor:
2698 default:
2699 FIXME("got unknown structure type 0x%x\n", structure->Format);
2700 return STATUS_NOT_SUPPORTED;
2702 ret = CDROM_GetStatusCode(ioctl(dev, DKIOCDVDREADSTRUCTURE, &dvdrs));
2703 if(ret == STATUS_SUCCESS)
2705 switch(structure->Format)
2707 case DvdPhysicalDescriptor:
2708 nt_desc.xlayer->Header.Length = 0x0802;
2709 nt_desc.xlayer->Header.Reserved[0] = 0;
2710 nt_desc.xlayer->Header.Reserved[1] = 0;
2711 nt_desc.xlayer->Descriptor.BookVersion = desc.phys.partVersion;
2712 nt_desc.xlayer->Descriptor.BookType = desc.phys.bookType;
2713 nt_desc.xlayer->Descriptor.MinimumRate = desc.phys.minimumRate;
2714 nt_desc.xlayer->Descriptor.DiskSize = desc.phys.discSize;
2715 nt_desc.xlayer->Descriptor.LayerType = desc.phys.layerType;
2716 nt_desc.xlayer->Descriptor.TrackPath = desc.phys.trackPath;
2717 nt_desc.xlayer->Descriptor.NumberOfLayers = desc.phys.numberOfLayers;
2718 nt_desc.xlayer->Descriptor.Reserved1 = 0;
2719 nt_desc.xlayer->Descriptor.TrackDensity = desc.phys.trackDensity;
2720 nt_desc.xlayer->Descriptor.LinearDensity = desc.phys.linearDensity;
2721 nt_desc.xlayer->Descriptor.StartingDataSector = GET_BE_DWORD(*(DWORD *)&desc.phys.zero1);
2722 nt_desc.xlayer->Descriptor.EndDataSector = GET_BE_DWORD(*(DWORD *)&desc.phys.zero2);
2723 nt_desc.xlayer->Descriptor.EndLayerZeroSector = GET_BE_DWORD(*(DWORD *)&desc.phys.zero3);
2724 nt_desc.xlayer->Descriptor.Reserved5 = 0;
2725 nt_desc.xlayer->Descriptor.BCAFlag = desc.phys.bcaFlag;
2726 break;
2728 case DvdCopyrightDescriptor:
2729 nt_desc.copy->CopyrightProtectionType =
2730 desc.copy.copyrightProtectionSystemType;
2731 nt_desc.copy->RegionManagementInformation =
2732 desc.copy.regionMask;
2733 nt_desc.copy->Reserved = 0;
2734 break;
2736 case DvdDiskKeyDescriptor:
2737 memcpy(
2738 nt_desc.disk_key->DiskKeyData,
2739 desc.disk_key.discKeyStructures,
2740 2048);
2741 break;
2743 case DvdManufacturerDescriptor:
2744 nt_desc.manf->Header.Length = 0x0802;
2745 nt_desc.manf->Header.Reserved[0] = 0;
2746 nt_desc.manf->Header.Reserved[1] = 0;
2747 memcpy(
2748 nt_desc.manf->Descriptor.ManufacturingInformation,
2749 desc.manf.discManufacturingInfo,
2750 2048);
2751 break;
2753 case DvdBCADescriptor:
2754 case DvdMaxDescriptor:
2755 default:
2756 /* Silence warning */
2757 break;
2760 return ret;
2761 #else
2762 FIXME("\n");
2763 #endif
2764 return STATUS_SUCCESS;
2768 /******************************************************************
2769 * GetInquiryData
2770 * Implements the IOCTL_GET_INQUIRY_DATA ioctl.
2771 * Returns Inquiry data for all devices on the specified scsi bus
2772 * Returns STATUS_BUFFER_TOO_SMALL if the output buffer is too small,
2773 * STATUS_INVALID_DEVICE_REQUEST if the given handle isn't to a SCSI device,
2774 * or STATUS_NOT_SUPPORTED if the OS driver is too old
2776 static NTSTATUS GetInquiryData(int fd, PSCSI_ADAPTER_BUS_INFO BufferOut, DWORD OutBufferSize)
2778 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
2779 PSCSI_INQUIRY_DATA pInquiryData;
2780 UCHAR sense_buffer[32];
2781 int iochk, version;
2782 sg_io_hdr_t iocmd;
2783 UCHAR inquiry[INQ_CMD_LEN] = {INQUIRY, 0, 0, 0, INQ_REPLY_LEN, 0};
2785 /* Check we have a SCSI device and a supported driver */
2786 if(ioctl(fd, SG_GET_VERSION_NUM, &version) != 0)
2788 WARN("IOCTL_SCSI_GET_INQUIRY_DATA sg driver is not loaded\n");
2789 return STATUS_INVALID_DEVICE_REQUEST;
2791 if(version < 30000 )
2792 return STATUS_NOT_SUPPORTED;
2794 /* FIXME: Enumerate devices on the bus */
2795 BufferOut->NumberOfBuses = 1;
2796 BufferOut->BusData[0].NumberOfLogicalUnits = 1;
2797 BufferOut->BusData[0].InquiryDataOffset = sizeof(SCSI_ADAPTER_BUS_INFO);
2799 pInquiryData = (PSCSI_INQUIRY_DATA)(BufferOut + 1);
2801 RtlZeroMemory(&iocmd, sizeof(iocmd));
2802 iocmd.interface_id = 'S';
2803 iocmd.cmd_len = sizeof(inquiry);
2804 iocmd.mx_sb_len = sizeof(sense_buffer);
2805 iocmd.dxfer_direction = SG_DXFER_FROM_DEV;
2806 iocmd.dxfer_len = INQ_REPLY_LEN;
2807 iocmd.dxferp = pInquiryData->InquiryData;
2808 iocmd.cmdp = inquiry;
2809 iocmd.sbp = sense_buffer;
2810 iocmd.timeout = 1000;
2812 iochk = ioctl(fd, SG_IO, &iocmd);
2813 if(iochk != 0)
2814 WARN("ioctl SG_IO returned %d, error (%s)\n", iochk, strerror(errno));
2816 CDROM_GetInterfaceInfo(fd, &BufferOut->BusData[0].InitiatorBusId, &pInquiryData->PathId, &pInquiryData->TargetId, &pInquiryData->Lun);
2817 pInquiryData->DeviceClaimed = TRUE;
2818 pInquiryData->InquiryDataLength = INQ_REPLY_LEN;
2819 pInquiryData->NextInquiryDataOffset = 0;
2820 return STATUS_SUCCESS;
2821 #else
2822 FIXME("not supported on this O/S\n");
2823 return STATUS_NOT_SUPPORTED;
2824 #endif
2827 /******************************************************************
2828 * CDROM_DeviceIoControl
2832 NTSTATUS CDROM_DeviceIoControl(HANDLE hDevice,
2833 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
2834 PVOID UserApcContext,
2835 PIO_STATUS_BLOCK piosb,
2836 ULONG dwIoControlCode,
2837 LPVOID lpInBuffer, DWORD nInBufferSize,
2838 LPVOID lpOutBuffer, DWORD nOutBufferSize)
2840 DWORD sz = 0;
2841 NTSTATUS status = STATUS_SUCCESS;
2842 int fd, needs_close, dev = 0;
2844 TRACE("%p %s %p %d %p %d %p\n",
2845 hDevice, iocodex(dwIoControlCode), lpInBuffer, nInBufferSize,
2846 lpOutBuffer, nOutBufferSize, piosb);
2848 piosb->Information = 0;
2850 if ((status = server_get_unix_fd( hDevice, 0, &fd, &needs_close, NULL, NULL )))
2852 if (status == STATUS_BAD_DEVICE_TYPE) return status; /* no associated fd */
2853 goto error;
2856 if ((status = CDROM_Open(fd, &dev)))
2858 if (needs_close) close( fd );
2859 goto error;
2862 #ifdef __APPLE__
2864 char name[100];
2866 /* This is ugly as hell, but Mac OS is unable to do anything from the
2867 * partition fd, it wants an fd for the whole device, and it sometimes
2868 * also requires the device fd to be closed first, so we have to close
2869 * the handle that the caller gave us.
2870 * Also for some reason it wants the fd to be closed before we even
2871 * open the parent if we're trying to eject the disk.
2873 if ((status = get_parent_device( fd, name, sizeof(name) ))) goto error;
2874 if (dwIoControlCode == IOCTL_STORAGE_EJECT_MEDIA)
2875 NtClose( hDevice );
2876 if (needs_close) close( fd );
2877 TRACE("opening parent %s\n", name );
2878 if ((fd = open( name, O_RDONLY )) == -1)
2880 status = FILE_GetNtStatus();
2881 goto error;
2883 needs_close = 1;
2885 #endif
2887 switch (dwIoControlCode)
2889 case IOCTL_CDROM_CHECK_VERIFY:
2890 case IOCTL_DISK_CHECK_VERIFY:
2891 case IOCTL_STORAGE_CHECK_VERIFY:
2892 case IOCTL_STORAGE_CHECK_VERIFY2:
2893 sz = 0;
2894 CDROM_ClearCacheEntry(dev);
2895 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2896 status = STATUS_INVALID_PARAMETER;
2897 else status = CDROM_Verify(dev, fd);
2898 break;
2900 /* EPP case IOCTL_STORAGE_CHECK_VERIFY2: */
2902 /* EPP case IOCTL_STORAGE_FIND_NEW_DEVICES: */
2903 /* EPP case IOCTL_CDROM_FIND_NEW_DEVICES: */
2905 case IOCTL_STORAGE_LOAD_MEDIA:
2906 case IOCTL_CDROM_LOAD_MEDIA:
2907 sz = 0;
2908 CDROM_ClearCacheEntry(dev);
2909 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2910 status = STATUS_INVALID_PARAMETER;
2911 else status = CDROM_SetTray(fd, FALSE);
2912 break;
2913 case IOCTL_STORAGE_EJECT_MEDIA:
2914 sz = 0;
2915 CDROM_ClearCacheEntry(dev);
2916 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2917 status = STATUS_INVALID_PARAMETER;
2918 else
2919 status = CDROM_SetTray(fd, TRUE);
2920 break;
2922 case IOCTL_CDROM_MEDIA_REMOVAL:
2923 case IOCTL_DISK_MEDIA_REMOVAL:
2924 case IOCTL_STORAGE_MEDIA_REMOVAL:
2925 case IOCTL_STORAGE_EJECTION_CONTROL:
2926 /* FIXME the last ioctl:s is not the same as the two others...
2927 * lockcount/owner should be handled */
2928 sz = 0;
2929 CDROM_ClearCacheEntry(dev);
2930 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2931 else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) status = STATUS_BUFFER_TOO_SMALL;
2932 else status = CDROM_ControlEjection(fd, lpInBuffer);
2933 break;
2935 case IOCTL_DISK_GET_MEDIA_TYPES:
2936 case IOCTL_STORAGE_GET_MEDIA_TYPES:
2937 case IOCTL_STORAGE_GET_MEDIA_TYPES_EX:
2938 sz = sizeof(GET_MEDIA_TYPES);
2939 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2940 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2941 else status = CDROM_GetMediaType(dev, lpOutBuffer);
2942 break;
2944 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
2945 sz = sizeof(STORAGE_DEVICE_NUMBER);
2946 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2947 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2948 else status = CDROM_GetDeviceNumber(dev, lpOutBuffer);
2949 break;
2951 case IOCTL_STORAGE_RESET_DEVICE:
2952 sz = 0;
2953 CDROM_ClearCacheEntry(dev);
2954 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2955 status = STATUS_INVALID_PARAMETER;
2956 else status = CDROM_ResetAudio(fd);
2957 break;
2959 case IOCTL_CDROM_GET_CONTROL:
2960 sz = sizeof(CDROM_AUDIO_CONTROL);
2961 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2962 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2963 else status = CDROM_GetControl(dev, fd, lpOutBuffer);
2964 break;
2966 case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
2967 sz = sizeof(DISK_GEOMETRY);
2968 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2969 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2970 else status = CDROM_GetDriveGeometry(dev, fd, lpOutBuffer);
2971 break;
2973 case IOCTL_CDROM_DISK_TYPE:
2974 sz = sizeof(CDROM_DISK_DATA);
2975 /* CDROM_ClearCacheEntry(dev); */
2976 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2977 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2978 else status = CDROM_GetDiskData(dev, fd, lpOutBuffer);
2979 break;
2981 /* EPP case IOCTL_CDROM_GET_LAST_SESSION: */
2983 case IOCTL_CDROM_READ_Q_CHANNEL:
2984 sz = sizeof(SUB_Q_CHANNEL_DATA);
2985 if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT))
2986 status = STATUS_INVALID_PARAMETER;
2987 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2988 else status = CDROM_ReadQChannel(dev, fd, lpInBuffer, lpOutBuffer);
2989 break;
2991 case IOCTL_CDROM_READ_TOC:
2992 sz = sizeof(CDROM_TOC);
2993 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2994 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2995 else status = CDROM_ReadTOC(dev, fd, lpOutBuffer);
2996 break;
2998 /* EPP case IOCTL_CDROM_READ_TOC_EX: */
3000 case IOCTL_CDROM_PAUSE_AUDIO:
3001 sz = 0;
3002 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3003 status = STATUS_INVALID_PARAMETER;
3004 else status = CDROM_PauseAudio(fd);
3005 break;
3006 case IOCTL_CDROM_PLAY_AUDIO_MSF:
3007 sz = 0;
3008 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3009 else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
3010 else status = CDROM_PlayAudioMSF(fd, lpInBuffer);
3011 break;
3012 case IOCTL_CDROM_RESUME_AUDIO:
3013 sz = 0;
3014 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3015 status = STATUS_INVALID_PARAMETER;
3016 else status = CDROM_ResumeAudio(fd);
3017 break;
3018 case IOCTL_CDROM_SEEK_AUDIO_MSF:
3019 sz = 0;
3020 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3021 else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
3022 else status = CDROM_SeekAudioMSF(dev, fd, lpInBuffer);
3023 break;
3024 case IOCTL_CDROM_STOP_AUDIO:
3025 sz = 0;
3026 CDROM_ClearCacheEntry(dev); /* Maybe intention is to change media */
3027 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3028 status = STATUS_INVALID_PARAMETER;
3029 else status = CDROM_StopAudio(fd);
3030 break;
3031 case IOCTL_CDROM_GET_VOLUME:
3032 sz = sizeof(VOLUME_CONTROL);
3033 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3034 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3035 else status = CDROM_GetVolume(fd, lpOutBuffer);
3036 break;
3037 case IOCTL_CDROM_SET_VOLUME:
3038 sz = 0;
3039 CDROM_ClearCacheEntry(dev);
3040 if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
3041 status = STATUS_INVALID_PARAMETER;
3042 else status = CDROM_SetVolume(fd, lpInBuffer);
3043 break;
3044 case IOCTL_CDROM_RAW_READ:
3045 sz = 0;
3046 if (nInBufferSize < sizeof(RAW_READ_INFO)) status = STATUS_INVALID_PARAMETER;
3047 else if (lpOutBuffer == NULL) status = STATUS_BUFFER_TOO_SMALL;
3048 else status = CDROM_RawRead(fd, lpInBuffer, lpOutBuffer,
3049 nOutBufferSize, &sz);
3050 break;
3051 case IOCTL_SCSI_GET_ADDRESS:
3052 sz = sizeof(SCSI_ADDRESS);
3053 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3054 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3055 else status = CDROM_GetAddress(fd, lpOutBuffer);
3056 break;
3057 case IOCTL_SCSI_PASS_THROUGH_DIRECT:
3058 sz = sizeof(SCSI_PASS_THROUGH_DIRECT);
3059 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3060 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH_DIRECT)) status = STATUS_BUFFER_TOO_SMALL;
3061 else status = CDROM_ScsiPassThroughDirect(fd, lpOutBuffer);
3062 break;
3063 case IOCTL_SCSI_PASS_THROUGH:
3064 sz = sizeof(SCSI_PASS_THROUGH);
3065 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3066 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH)) status = STATUS_BUFFER_TOO_SMALL;
3067 else status = CDROM_ScsiPassThrough(fd, lpOutBuffer);
3068 break;
3069 case IOCTL_SCSI_GET_CAPABILITIES:
3070 sz = sizeof(IO_SCSI_CAPABILITIES);
3071 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3072 else if (nOutBufferSize < sizeof(IO_SCSI_CAPABILITIES)) status = STATUS_BUFFER_TOO_SMALL;
3073 else status = CDROM_ScsiGetCaps(fd, lpOutBuffer);
3074 break;
3075 case IOCTL_DVD_START_SESSION:
3076 sz = sizeof(DVD_SESSION_ID);
3077 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3078 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3079 else
3081 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3082 *(PDVD_SESSION_ID)lpOutBuffer);
3083 status = DVD_StartSession(fd, lpInBuffer, lpOutBuffer);
3084 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3085 *(PDVD_SESSION_ID)lpOutBuffer);
3087 break;
3088 case IOCTL_DVD_END_SESSION:
3089 sz = sizeof(DVD_SESSION_ID);
3090 if ((lpInBuffer == NULL) || (nInBufferSize < sz))status = STATUS_INVALID_PARAMETER;
3091 else status = DVD_EndSession(fd, lpInBuffer);
3092 break;
3093 case IOCTL_DVD_SEND_KEY:
3094 sz = 0;
3095 if (!lpInBuffer ||
3096 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3097 status = STATUS_INVALID_PARAMETER;
3098 else
3100 TRACE("doing DVD_SendKey\n");
3101 status = DVD_SendKey(fd, lpInBuffer);
3103 break;
3104 case IOCTL_DVD_READ_KEY:
3105 if (!lpInBuffer ||
3106 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3107 status = STATUS_INVALID_PARAMETER;
3108 else if (lpInBuffer !=lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
3109 else
3111 TRACE("doing DVD_READ_KEY\n");
3112 sz = ((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength;
3113 status = DVD_ReadKey(fd, lpInBuffer);
3115 break;
3116 case IOCTL_DVD_GET_REGION:
3117 sz = sizeof(DVD_REGION);
3118 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3119 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3120 else
3122 TRACE("doing DVD_Get_REGION\n");
3123 status = DVD_GetRegion(fd, lpOutBuffer);
3125 break;
3126 case IOCTL_DVD_READ_STRUCTURE:
3127 sz = DVD_ReadStructureSize(lpInBuffer, nInBufferSize);
3128 if (lpInBuffer == NULL || nInBufferSize != sizeof(DVD_READ_STRUCTURE)) status = STATUS_INVALID_PARAMETER;
3129 else if (nOutBufferSize < sz || !lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
3130 else
3132 TRACE("doing DVD_READ_STRUCTURE\n");
3133 status = DVD_ReadStructure(fd, lpInBuffer, lpOutBuffer);
3135 break;
3137 case IOCTL_SCSI_GET_INQUIRY_DATA:
3138 sz = INQ_REPLY_LEN;
3139 status = GetInquiryData(fd, lpOutBuffer, nOutBufferSize);
3140 break;
3142 default:
3143 if (needs_close) close( fd );
3144 return STATUS_NOT_SUPPORTED;
3146 if (needs_close) close( fd );
3147 error:
3148 piosb->u.Status = status;
3149 piosb->Information = sz;
3150 if (hEvent) NtSetEvent(hEvent, NULL);
3151 return status;