user32: In MENU_SuspendPopup() make sure the right message is removed.
[wine/hacks.git] / dlls / ntdll / cdrom.c
bloba967f9c3e958c6a6dbb6610f794c49794e92d0df
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 #include <sys/types.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 #include <sys/ioctl.h>
45 #endif
46 #ifdef HAVE_SCSI_SG_H
47 # include <scsi/sg.h>
48 #endif
49 #ifdef HAVE_SCSI_SCSI_H
50 # include <scsi/scsi.h>
51 # undef REASSIGN_BLOCKS /* avoid conflict with winioctl.h */
52 # undef FAILED /* avoid conflict with winerror.h */
53 #endif
54 #ifdef HAVE_SCSI_SCSI_IOCTL_H
55 # include <scsi/scsi_ioctl.h>
56 #endif
57 #ifdef HAVE_LINUX_MAJOR_H
58 # include <linux/major.h>
59 #endif
60 #ifdef HAVE_LINUX_HDREG_H
61 # include <linux/hdreg.h>
62 #endif
63 #ifdef HAVE_LINUX_PARAM_H
64 # include <linux/param.h>
65 #endif
66 #ifdef HAVE_LINUX_CDROM_H
67 # include <linux/cdrom.h>
68 #endif
69 #ifdef HAVE_LINUX_UCDROM_H
70 # include <linux/ucdrom.h>
71 #endif
72 #ifdef HAVE_SYS_CDIO_H
73 # include <sys/cdio.h>
74 #endif
75 #ifdef HAVE_SYS_SCSIIO_H
76 # include <sys/scsiio.h>
77 #endif
79 #ifdef HAVE_IOKIT_IOKITLIB_H
80 # include <sys/disk.h>
81 # include <IOKit/IOKitLib.h>
82 # include <IOKit/storage/IOMedia.h>
83 # include <IOKit/storage/IOCDMediaBSDClient.h>
84 # include <IOKit/scsi/SCSICmds_REQUEST_SENSE_Defs.h>
85 # define SENSEBUFLEN kSenseDefaultSize
86 #endif
88 #define NONAMELESSUNION
89 #define NONAMELESSSTRUCT
90 #include "ntstatus.h"
91 #define WIN32_NO_STATUS
92 #include "windef.h"
93 #include "winternl.h"
94 #include "winioctl.h"
95 #include "ntddstor.h"
96 #include "ntddcdrm.h"
97 #include "ddk/ntddcdvd.h"
98 #include "ntddscsi.h"
99 #include "ntdll_misc.h"
100 #include "wine/server.h"
101 #include "wine/library.h"
102 #include "wine/debug.h"
104 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
106 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
107 lack the following constants. */
109 #ifndef CD_SECS
110 # define CD_SECS 60 /* seconds per minute */
111 #endif
112 #ifndef CD_FRAMES
113 # define CD_FRAMES 75 /* frames per second */
114 #endif
116 static const struct iocodexs
118 DWORD code;
119 const char *codex;
120 } iocodextable[] = {
121 #define X(x) { x, #x },
122 X(IOCTL_CDROM_CHECK_VERIFY)
123 X(IOCTL_CDROM_CURRENT_POSITION)
124 X(IOCTL_CDROM_DISK_TYPE)
125 X(IOCTL_CDROM_GET_CONTROL)
126 X(IOCTL_CDROM_GET_DRIVE_GEOMETRY)
127 X(IOCTL_CDROM_GET_VOLUME)
128 X(IOCTL_CDROM_LOAD_MEDIA)
129 X(IOCTL_CDROM_MEDIA_CATALOG)
130 X(IOCTL_CDROM_MEDIA_REMOVAL)
131 X(IOCTL_CDROM_PAUSE_AUDIO)
132 X(IOCTL_CDROM_PLAY_AUDIO_MSF)
133 X(IOCTL_CDROM_RAW_READ)
134 X(IOCTL_CDROM_READ_Q_CHANNEL)
135 X(IOCTL_CDROM_READ_TOC)
136 X(IOCTL_CDROM_RESUME_AUDIO)
137 X(IOCTL_CDROM_SEEK_AUDIO_MSF)
138 X(IOCTL_CDROM_SET_VOLUME)
139 X(IOCTL_CDROM_STOP_AUDIO)
140 X(IOCTL_CDROM_TRACK_ISRC)
141 X(IOCTL_DISK_MEDIA_REMOVAL)
142 X(IOCTL_DVD_END_SESSION)
143 X(IOCTL_DVD_GET_REGION)
144 X(IOCTL_DVD_READ_KEY)
145 X(IOCTL_DVD_READ_STRUCTURE)
146 X(IOCTL_DVD_SEND_KEY)
147 X(IOCTL_DVD_START_SESSION)
148 X(IOCTL_SCSI_GET_ADDRESS)
149 X(IOCTL_SCSI_GET_CAPABILITIES)
150 X(IOCTL_SCSI_GET_INQUIRY_DATA)
151 X(IOCTL_SCSI_PASS_THROUGH)
152 X(IOCTL_SCSI_PASS_THROUGH_DIRECT)
153 X(IOCTL_STORAGE_CHECK_VERIFY)
154 X(IOCTL_STORAGE_EJECTION_CONTROL)
155 X(IOCTL_STORAGE_EJECT_MEDIA)
156 X(IOCTL_STORAGE_GET_DEVICE_NUMBER)
157 X(IOCTL_STORAGE_LOAD_MEDIA)
158 X(IOCTL_STORAGE_MEDIA_REMOVAL)
159 X(IOCTL_STORAGE_RESET_DEVICE)
160 #undef X
162 static const char *iocodex(DWORD code)
164 unsigned int i;
165 static char buffer[25];
166 for(i=0; i<sizeof(iocodextable)/sizeof(struct iocodexs); i++)
167 if (code==iocodextable[i].code)
168 return iocodextable[i].codex;
169 sprintf(buffer, "IOCTL_CODE_%x", (int)code);
170 return buffer;
173 #define INQ_REPLY_LEN 36
174 #define INQ_CMD_LEN 6
176 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
177 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
178 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
179 #define MSF_OF_FRAME(m,fr) {int f=(fr); ((UCHAR *)&(m))[2]=f%CD_FRAMES;f/=CD_FRAMES;((UCHAR *)&(m))[1]=f%CD_SECS;((UCHAR *)&(m))[0]=f/CD_SECS;}
181 /* The documented format of DVD_LAYER_DESCRIPTOR is wrong. Even the format in the
182 * DDK's header is wrong. There are four bytes at the start which always seem to
183 * follow the sequence "02 08 00 00".
185 typedef struct
187 UCHAR MagicHeader[4];
188 UCHAR BookVersion : 4;
189 UCHAR BookType : 4;
190 UCHAR MinimumRate : 4;
191 UCHAR DiskSize : 4;
192 UCHAR LayerType : 4;
193 UCHAR TrackPath : 1;
194 UCHAR NumberOfLayers : 2;
195 UCHAR Reserved1 : 1;
196 UCHAR TrackDensity : 4;
197 UCHAR LinearDensity : 4;
198 ULONG StartingDataSector;
199 ULONG EndDataSector;
200 ULONG EndLayerZeroSector;
201 UCHAR Reserved5 : 7;
202 UCHAR BCAFlag : 1;
203 UCHAR Reserved6;
204 } internal_dvd_layer_descriptor;
207 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
208 static NTSTATUS CDROM_GetStatusCode(int);
211 #ifdef linux
213 # ifndef IDE6_MAJOR
214 # define IDE6_MAJOR 88
215 # endif
216 # ifndef IDE7_MAJOR
217 # define IDE7_MAJOR 89
218 # endif
220 # ifdef CDROM_SEND_PACKET
221 /* structure for CDROM_PACKET_COMMAND ioctl */
222 /* not all Linux versions have all the fields, so we define the
223 * structure ourselves to make sure */
224 struct linux_cdrom_generic_command
226 unsigned char cmd[CDROM_PACKET_SIZE];
227 unsigned char *buffer;
228 unsigned int buflen;
229 int stat;
230 struct request_sense *sense;
231 unsigned char data_direction;
232 int quiet;
233 int timeout;
234 void *reserved[1];
236 # endif /* CDROM_SEND_PACKET */
238 #endif /* linux */
240 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
241 * this should be removed when a proper device interface is implemented
243 * (WS) We need this to keep track of current position and to safely
244 * detect media changes. Besides this should provide a great speed up
245 * for toc inquiries.
247 struct cdrom_cache {
248 dev_t device;
249 ino_t inode;
250 char toc_good; /* if false, will reread TOC from disk */
251 CDROM_TOC toc;
252 SUB_Q_CURRENT_POSITION CurrentPosition;
254 /* who has more than 5 cdroms on his/her machine ?? */
255 /* FIXME: this should grow depending on the number of cdroms we install/configure
256 * at startup
258 #define MAX_CACHE_ENTRIES 5
259 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
261 static RTL_CRITICAL_SECTION cache_section;
262 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
264 0, 0, &cache_section,
265 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
266 0, 0, { (DWORD_PTR)(__FILE__ ": cache_section") }
268 static RTL_CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
270 /* Proposed media change function: not really needed at this time */
271 /* This is a 1 or 0 type of function */
272 #if 0
273 static int CDROM_MediaChanged(int dev)
275 int i;
277 struct cdrom_tochdr hdr;
278 struct cdrom_tocentry entry;
280 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
281 return 0;
282 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
283 return 0;
285 if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
286 return 1;
288 for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
290 if (i == hdr.cdth_trk1 + 1)
292 entry.cdte_track = CDROM_LEADOUT;
293 } else {
294 entry.cdte_track = i;
296 entry.cdte_format = CDROM_MSF;
297 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
298 return 0;
299 if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
300 sizeof(struct cdrom_tocentry)) )
301 return 1;
303 return 0;
305 #endif
308 /******************************************************************
309 * get_parent_device
311 * On Mac OS, get the device for the whole disk from a fd that points to a partition.
312 * This is ugly and inefficient, but we have no choice since the partition fd doesn't
313 * support the eject ioctl.
315 #ifdef __APPLE__
316 static NTSTATUS get_parent_device( int fd, char *name, size_t len )
318 NTSTATUS status = STATUS_NO_SUCH_FILE;
319 struct stat st;
320 int i;
321 io_service_t service;
322 CFMutableDictionaryRef dict;
323 CFTypeRef val;
325 if (fstat( fd, &st ) == -1) return FILE_GetNtStatus();
326 if (!S_ISCHR( st.st_mode )) return STATUS_OBJECT_TYPE_MISMATCH;
328 /* create a dictionary with the right major/minor numbers */
330 if (!(dict = IOServiceMatching( kIOMediaClass ))) return STATUS_NO_MEMORY;
332 i = major( st.st_rdev );
333 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
334 CFDictionaryAddValue( dict, CFSTR( "BSD Major" ), val );
335 CFRelease( val );
337 i = minor( st.st_rdev );
338 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
339 CFDictionaryAddValue( dict, CFSTR( "BSD Minor" ), val );
340 CFRelease( val );
342 CFDictionaryAddValue( dict, CFSTR("Removable"), kCFBooleanTrue );
344 service = IOServiceGetMatchingService( kIOMasterPortDefault, dict );
346 /* now look for the parent that has the "Whole" attribute set to TRUE */
348 while (service)
350 io_service_t parent = 0;
351 CFBooleanRef whole;
352 CFStringRef str;
353 int ok;
355 if (!IOObjectConformsTo( service, kIOMediaClass ))
356 goto next;
357 if (!(whole = IORegistryEntryCreateCFProperty( service, CFSTR("Whole"), NULL, 0 )))
358 goto next;
359 ok = (whole == kCFBooleanTrue);
360 CFRelease( whole );
361 if (!ok) goto next;
363 if ((str = IORegistryEntryCreateCFProperty( service, CFSTR("BSD Name"), NULL, 0 )))
365 strcpy( name, "/dev/r" );
366 CFStringGetCString( str, name + 6, len - 6, kCFStringEncodingUTF8 );
367 CFRelease( str );
368 status = STATUS_SUCCESS;
370 IOObjectRelease( service );
371 break;
373 next:
374 IORegistryEntryGetParentEntry( service, kIOServicePlane, &parent );
375 IOObjectRelease( service );
376 service = parent;
378 return status;
380 #endif
383 /******************************************************************
384 * CDROM_SyncCache [internal]
386 * Read the TOC in and store it in the cdrom_cache structure.
387 * Further requests for the TOC will be copied from the cache
388 * unless certain events like disk ejection is detected, in which
389 * case the cache will be cleared, causing it to be resynced.
390 * The cache section must be held by caller.
392 static NTSTATUS CDROM_SyncCache(int dev, int fd)
394 #ifdef linux
395 int i, tsz;
396 struct cdrom_tochdr hdr;
397 struct cdrom_tocentry entry;
399 CDROM_TOC *toc = &cdrom_cache[dev].toc;
400 cdrom_cache[dev].toc_good = 0;
402 if (ioctl(fd, CDROMREADTOCHDR, &hdr) == -1)
404 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
405 return FILE_GetNtStatus();
408 toc->FirstTrack = hdr.cdth_trk0;
409 toc->LastTrack = hdr.cdth_trk1;
410 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
411 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
412 toc->Length[0] = tsz >> 8;
413 toc->Length[1] = tsz;
415 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
417 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
419 if (i == toc->LastTrack + 1)
420 entry.cdte_track = CDROM_LEADOUT;
421 else
422 entry.cdte_track = i;
423 entry.cdte_format = CDROM_MSF;
424 if (ioctl(fd, CDROMREADTOCENTRY, &entry) == -1)
426 WARN("error read entry (%s)\n", strerror(errno));
427 return FILE_GetNtStatus();
429 toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
430 toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
431 /* marking last track with leadout value as index */
432 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
433 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
434 toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
435 toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
436 toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
438 cdrom_cache[dev].toc_good = 1;
439 return STATUS_SUCCESS;
441 #elif defined(__FreeBSD__) || defined(__NetBSD__)
443 int i, tsz;
444 struct ioc_toc_header hdr;
445 struct ioc_read_toc_entry entry;
446 struct cd_toc_entry toc_buffer;
448 CDROM_TOC *toc = &cdrom_cache[dev].toc;
449 cdrom_cache[dev].toc_good = 0;
451 if (ioctl(fd, CDIOREADTOCHEADER, &hdr) == -1)
453 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
454 return FILE_GetNtStatus();
456 toc->FirstTrack = hdr.starting_track;
457 toc->LastTrack = hdr.ending_track;
458 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
459 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
460 toc->Length[0] = tsz >> 8;
461 toc->Length[1] = tsz;
463 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
465 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
467 if (i == toc->LastTrack + 1)
469 #define LEADOUT 0xaa
470 entry.starting_track = LEADOUT;
471 } else {
472 entry.starting_track = i;
474 memset(&toc_buffer, 0, sizeof(toc_buffer));
475 entry.address_format = CD_MSF_FORMAT;
476 entry.data_len = sizeof(toc_buffer);
477 entry.data = &toc_buffer;
478 if (ioctl(fd, CDIOREADTOCENTRYS, &entry) == -1)
480 WARN("error read entry (%s)\n", strerror(errno));
481 return FILE_GetNtStatus();
483 toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
484 toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
485 /* marking last track with leadout value as index */
486 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
487 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
488 toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
489 toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
490 toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
492 cdrom_cache[dev].toc_good = 1;
493 return STATUS_SUCCESS;
495 #elif defined(__APPLE__)
496 int i;
497 dk_cd_read_toc_t hdr;
498 CDROM_TOC *toc = &cdrom_cache[dev].toc;
499 cdrom_cache[dev].toc_good = 0;
501 memset( &hdr, 0, sizeof(hdr) );
502 hdr.buffer = toc;
503 hdr.bufferLength = sizeof(*toc);
504 if (ioctl(fd, DKIOCCDREADTOC, &hdr) == -1)
506 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
507 return FILE_GetNtStatus();
509 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
511 /* convert address format */
512 TRACK_DATA *data = &toc->TrackData[i - toc->FirstTrack];
513 DWORD frame = (((DWORD)data->Address[0] << 24) | ((DWORD)data->Address[1] << 16) |
514 ((DWORD)data->Address[2] << 8) | data->Address[3]);
515 MSF_OF_FRAME( data->Address[1], frame );
516 data->Address[0] = 0;
519 cdrom_cache[dev].toc_good = 1;
520 return STATUS_SUCCESS;
521 #else
522 return STATUS_NOT_SUPPORTED;
523 #endif
526 static void CDROM_ClearCacheEntry(int dev)
528 RtlEnterCriticalSection( &cache_section );
529 cdrom_cache[dev].toc_good = 0;
530 RtlLeaveCriticalSection( &cache_section );
535 /******************************************************************
536 * CDROM_GetInterfaceInfo
538 * Determines the ide interface (the number after the ide), and the
539 * number of the device on that interface for ide cdroms (*iface <= 1).
540 * Determines the scsi information for scsi cdroms (*iface >= 2).
541 * Returns false if the info cannot not be obtained.
543 static int CDROM_GetInterfaceInfo(int fd, UCHAR* iface, UCHAR* port, UCHAR* device, UCHAR* lun)
545 #if defined(linux)
546 struct stat st;
547 if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode)) return 0;
548 *port = 0;
549 *iface = 0;
550 *device = 0;
551 *lun = 0;
552 switch (major(st.st_rdev)) {
553 case IDE0_MAJOR: *iface = 0; break;
554 case IDE1_MAJOR: *iface = 1; break;
555 case IDE2_MAJOR: *iface = 2; break;
556 case IDE3_MAJOR: *iface = 3; break;
557 case IDE4_MAJOR: *iface = 4; break;
558 case IDE5_MAJOR: *iface = 5; break;
559 case IDE6_MAJOR: *iface = 6; break;
560 case IDE7_MAJOR: *iface = 7; break;
561 default: *port = 1; break;
564 if (*port == 0)
565 *device = (minor(st.st_rdev) >> 6);
566 else
568 #ifdef SCSI_IOCTL_GET_IDLUN
569 UINT32 idlun[2];
570 if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, idlun) != -1)
572 *port = (idlun[0] >> 24) & 0xff;
573 *iface = ((idlun[0] >> 16) & 0xff) + 2;
574 *device = idlun[0] & 0xff;
575 *lun = (idlun[0] >> 8) & 0xff;
577 else
578 #endif
580 WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
581 return 0;
584 return 1;
585 #elif defined(__NetBSD__)
586 struct scsi_addr addr;
587 if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
589 switch (addr.type)
591 case TYPE_SCSI: *port = 1;
592 *iface = addr.addr.scsi.scbus;
593 *device = addr.addr.scsi.target;
594 *lun = addr.addr.scsi.lun;
595 break;
596 case TYPE_ATAPI: *port = 0;
597 *iface = addr.addr.atapi.atbus;
598 *device = addr.addr.atapi.drive;
599 *lun = 0;
600 break;
602 return 1;
604 return 0;
605 #elif defined(__FreeBSD__)
606 FIXME("not implemented for BSD\n");
607 return 0;
608 #else
609 FIXME("not implemented for nonlinux\n");
610 return 0;
611 #endif
615 /******************************************************************
616 * CDROM_Open
619 static NTSTATUS CDROM_Open(int fd, int* dev)
621 struct stat st;
622 NTSTATUS ret = STATUS_SUCCESS;
623 int empty = -1;
625 fstat(fd, &st);
627 RtlEnterCriticalSection( &cache_section );
628 for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
630 if (empty == -1 &&
631 cdrom_cache[*dev].device == 0 &&
632 cdrom_cache[*dev].inode == 0)
633 empty = *dev;
634 else if (cdrom_cache[*dev].device == st.st_dev &&
635 cdrom_cache[*dev].inode == st.st_ino)
636 break;
638 if (*dev == MAX_CACHE_ENTRIES)
640 if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
641 else
643 *dev = empty;
644 cdrom_cache[*dev].device = st.st_dev;
645 cdrom_cache[*dev].inode = st.st_ino;
648 RtlLeaveCriticalSection( &cache_section );
650 TRACE("%d, %d\n", *dev, fd);
651 return ret;
654 /******************************************************************
655 * CDROM_GetStatusCode
659 static NTSTATUS CDROM_GetStatusCode(int io)
661 if (io == 0) return STATUS_SUCCESS;
662 return FILE_GetNtStatus();
665 /******************************************************************
666 * CDROM_GetControl
669 static NTSTATUS CDROM_GetControl(int dev, CDROM_AUDIO_CONTROL* cac)
671 cac->LbaFormat = 0; /* FIXME */
672 cac->LogicalBlocksPerSecond = 1; /* FIXME */
673 return STATUS_NOT_SUPPORTED;
676 /******************************************************************
677 * CDROM_GetDeviceNumber
680 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
682 FIXME( "stub\n" );
683 devnum->DeviceType = FILE_DEVICE_DISK;
684 devnum->DeviceNumber = 1;
685 devnum->PartitionNumber = 1;
686 return STATUS_SUCCESS;
689 /******************************************************************
690 * CDROM_GetDriveGeometry
693 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
695 CDROM_TOC toc;
696 NTSTATUS ret = 0;
697 int fsize = 0;
699 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
701 fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
702 - FRAME_OF_TOC(toc, 1); /* Total size in frames */
704 dg->Cylinders.u.LowPart = fsize / (64 * 32);
705 dg->Cylinders.u.HighPart = 0;
706 dg->MediaType = RemovableMedia;
707 dg->TracksPerCylinder = 64;
708 dg->SectorsPerTrack = 32;
709 dg->BytesPerSector= 2048;
710 return ret;
713 /**************************************************************************
714 * CDROM_Reset [internal]
716 static NTSTATUS CDROM_ResetAudio(int fd)
718 #if defined(linux)
719 return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
720 #elif defined(__FreeBSD__) || defined(__NetBSD__)
721 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
722 #else
723 return STATUS_NOT_SUPPORTED;
724 #endif
727 /******************************************************************
728 * CDROM_SetTray
732 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
734 #if defined(linux)
735 return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
736 #elif defined(__FreeBSD__) || defined(__NetBSD__)
737 return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
738 (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
739 (ioctl(fd, CDIOCPREVENT, NULL)));
740 #elif defined(__APPLE__)
741 if (doEject) return CDROM_GetStatusCode( ioctl( fd, DKIOCEJECT, NULL ) );
742 else return STATUS_NOT_SUPPORTED;
743 #else
744 return STATUS_NOT_SUPPORTED;
745 #endif
748 /******************************************************************
749 * CDROM_ControlEjection
753 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
755 #if defined(linux)
756 return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
757 #elif defined(__FreeBSD__) || defined(__NetBSD__)
758 return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
759 #else
760 return STATUS_NOT_SUPPORTED;
761 #endif
764 /******************************************************************
765 * CDROM_ReadTOC
769 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
771 NTSTATUS ret = STATUS_NOT_SUPPORTED;
773 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
774 return STATUS_INVALID_PARAMETER;
776 RtlEnterCriticalSection( &cache_section );
777 if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
779 *toc = cdrom_cache[dev].toc;
780 ret = STATUS_SUCCESS;
782 RtlLeaveCriticalSection( &cache_section );
783 return ret;
786 /******************************************************************
787 * CDROM_GetDiskData
791 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
793 CDROM_TOC toc;
794 NTSTATUS ret;
795 int i;
797 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
798 data->DiskData = 0;
799 for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
800 if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
801 data->DiskData |= CDROM_DISK_DATA_TRACK;
802 else
803 data->DiskData |= CDROM_DISK_AUDIO_TRACK;
805 return STATUS_SUCCESS;
808 /******************************************************************
809 * CDROM_ReadQChannel
813 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
814 SUB_Q_CHANNEL_DATA* data)
816 NTSTATUS ret = STATUS_NOT_SUPPORTED;
817 #ifdef linux
818 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
819 int io;
820 struct cdrom_subchnl sc;
821 sc.cdsc_format = CDROM_MSF;
823 io = ioctl(fd, CDROMSUBCHNL, &sc);
824 if (io == -1)
826 TRACE("opened or no_media (%s)!\n", strerror(errno));
827 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
828 CDROM_ClearCacheEntry(dev);
829 goto end;
832 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
834 switch (sc.cdsc_audiostatus) {
835 case CDROM_AUDIO_INVALID:
836 CDROM_ClearCacheEntry(dev);
837 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
838 break;
839 case CDROM_AUDIO_NO_STATUS:
840 CDROM_ClearCacheEntry(dev);
841 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
842 break;
843 case CDROM_AUDIO_PLAY:
844 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
845 break;
846 case CDROM_AUDIO_PAUSED:
847 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
848 break;
849 case CDROM_AUDIO_COMPLETED:
850 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
851 break;
852 case CDROM_AUDIO_ERROR:
853 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
854 break;
855 default:
856 TRACE("status=%02X !\n", sc.cdsc_audiostatus);
857 break;
859 switch (fmt->Format)
861 case IOCTL_CDROM_CURRENT_POSITION:
862 RtlEnterCriticalSection( &cache_section );
863 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
864 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
865 data->CurrentPosition.Control = sc.cdsc_ctrl;
866 data->CurrentPosition.ADR = sc.cdsc_adr;
867 data->CurrentPosition.TrackNumber = sc.cdsc_trk;
868 data->CurrentPosition.IndexNumber = sc.cdsc_ind;
870 data->CurrentPosition.AbsoluteAddress[0] = 0;
871 data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute;
872 data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
873 data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
875 data->CurrentPosition.TrackRelativeAddress[0] = 0;
876 data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute;
877 data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
878 data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
880 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
882 else /* not playing */
884 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
885 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
887 RtlLeaveCriticalSection( &cache_section );
888 break;
889 case IOCTL_CDROM_MEDIA_CATALOG:
890 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
892 struct cdrom_mcn mcn;
893 if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
895 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
896 data->MediaCatalog.Mcval = 0; /* FIXME */
897 memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
898 data->MediaCatalog.MediaCatalog[14] = 0;
900 break;
901 case IOCTL_CDROM_TRACK_ISRC:
902 FIXME("TrackIsrc: NIY on linux\n");
903 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
904 data->TrackIsrc.Tcval = 0;
905 io = 0;
906 break;
909 end:
910 ret = CDROM_GetStatusCode(io);
911 #elif defined(__FreeBSD__) || defined(__NetBSD__)
912 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
913 int io;
914 struct ioc_read_subchannel read_sc;
915 struct cd_sub_channel_info sc;
917 read_sc.address_format = CD_MSF_FORMAT;
918 read_sc.track = 0;
919 read_sc.data_len = sizeof(sc);
920 read_sc.data = &sc;
921 switch (fmt->Format)
923 case IOCTL_CDROM_CURRENT_POSITION:
924 read_sc.data_format = CD_CURRENT_POSITION;
925 break;
926 case IOCTL_CDROM_MEDIA_CATALOG:
927 read_sc.data_format = CD_MEDIA_CATALOG;
928 break;
929 case IOCTL_CDROM_TRACK_ISRC:
930 read_sc.data_format = CD_TRACK_INFO;
931 sc.what.track_info.track_number = data->TrackIsrc.Track;
932 break;
934 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
935 if (io == -1)
937 TRACE("opened or no_media (%s)!\n", strerror(errno));
938 CDROM_ClearCacheEntry(dev);
939 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
940 goto end;
943 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
945 switch (sc.header.audio_status) {
946 case CD_AS_AUDIO_INVALID:
947 CDROM_ClearCacheEntry(dev);
948 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
949 break;
950 case CD_AS_NO_STATUS:
951 CDROM_ClearCacheEntry(dev);
952 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
953 break;
954 case CD_AS_PLAY_IN_PROGRESS:
955 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
956 break;
957 case CD_AS_PLAY_PAUSED:
958 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
959 break;
960 case CD_AS_PLAY_COMPLETED:
961 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
962 break;
963 case CD_AS_PLAY_ERROR:
964 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
965 break;
966 default:
967 TRACE("status=%02X !\n", sc.header.audio_status);
969 switch (fmt->Format)
971 case IOCTL_CDROM_CURRENT_POSITION:
972 RtlEnterCriticalSection( &cache_section );
973 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
974 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
975 data->CurrentPosition.Control = sc.what.position.control;
976 data->CurrentPosition.ADR = sc.what.position.addr_type;
977 data->CurrentPosition.TrackNumber = sc.what.position.track_number;
978 data->CurrentPosition.IndexNumber = sc.what.position.index_number;
980 data->CurrentPosition.AbsoluteAddress[0] = 0;
981 data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
982 data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
983 data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
984 data->CurrentPosition.TrackRelativeAddress[0] = 0;
985 data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
986 data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
987 data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
988 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
990 else { /* not playing */
991 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
992 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
994 RtlLeaveCriticalSection( &cache_section );
995 break;
996 case IOCTL_CDROM_MEDIA_CATALOG:
997 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
998 data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
999 memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
1000 break;
1001 case IOCTL_CDROM_TRACK_ISRC:
1002 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
1003 data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
1004 memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
1005 break;
1008 end:
1009 ret = CDROM_GetStatusCode(io);
1010 #endif
1011 return ret;
1014 /******************************************************************
1015 * CDROM_Verify
1016 * Implements: IOCTL_STORAGE_CHECK_VERIFY
1017 * IOCTL_CDROM_CHECK_VERIFY
1018 * IOCTL_DISK_CHECK_VERIFY
1021 static NTSTATUS CDROM_Verify(int dev, int fd)
1023 #if defined(linux)
1024 int ret;
1026 ret = ioctl(fd, CDROM_DRIVE_STATUS, NULL);
1027 if(ret == -1) {
1028 TRACE("ioctl CDROM_DRIVE_STATUS failed(%s)!\n", strerror(errno));
1029 return CDROM_GetStatusCode(ret);
1032 if(ret == CDS_DISC_OK)
1033 return STATUS_SUCCESS;
1034 else
1035 return STATUS_NO_MEDIA_IN_DEVICE;
1036 #elif defined(__FreeBSD__)
1037 int ret;
1038 ret = ioctl(fd, CDIOCSTART, NULL);
1039 if(ret == 0)
1040 return STATUS_SUCCESS;
1041 else
1042 return STATUS_NO_MEDIA_IN_DEVICE;
1043 #else
1044 FIXME("not implemented for non-linux\n");
1045 return STATUS_NOT_SUPPORTED;
1046 #endif
1049 /******************************************************************
1050 * CDROM_PlayAudioMSF
1054 static NTSTATUS CDROM_PlayAudioMSF(int fd, const CDROM_PLAY_AUDIO_MSF* audio_msf)
1056 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1057 #ifdef linux
1058 struct cdrom_msf msf;
1059 int io;
1061 msf.cdmsf_min0 = audio_msf->StartingM;
1062 msf.cdmsf_sec0 = audio_msf->StartingS;
1063 msf.cdmsf_frame0 = audio_msf->StartingF;
1064 msf.cdmsf_min1 = audio_msf->EndingM;
1065 msf.cdmsf_sec1 = audio_msf->EndingS;
1066 msf.cdmsf_frame1 = audio_msf->EndingF;
1068 io = ioctl(fd, CDROMSTART);
1069 if (io == -1)
1071 WARN("motor doesn't start !\n");
1072 goto end;
1074 io = ioctl(fd, CDROMPLAYMSF, &msf);
1075 if (io == -1)
1077 WARN("device doesn't play !\n");
1078 goto end;
1080 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1081 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
1082 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
1083 end:
1084 ret = CDROM_GetStatusCode(io);
1085 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1086 struct ioc_play_msf msf;
1087 int io;
1089 msf.start_m = audio_msf->StartingM;
1090 msf.start_s = audio_msf->StartingS;
1091 msf.start_f = audio_msf->StartingF;
1092 msf.end_m = audio_msf->EndingM;
1093 msf.end_s = audio_msf->EndingS;
1094 msf.end_f = audio_msf->EndingF;
1096 io = ioctl(fd, CDIOCSTART, NULL);
1097 if (io == -1)
1099 WARN("motor doesn't start !\n");
1100 goto end;
1102 io = ioctl(fd, CDIOCPLAYMSF, &msf);
1103 if (io == -1)
1105 WARN("device doesn't play !\n");
1106 goto end;
1108 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1109 msf.start_m, msf.start_s, msf.start_f,
1110 msf.end_m, msf.end_s, msf.end_f);
1111 end:
1112 ret = CDROM_GetStatusCode(io);
1113 #endif
1114 return ret;
1117 /******************************************************************
1118 * CDROM_SeekAudioMSF
1122 static NTSTATUS CDROM_SeekAudioMSF(int dev, int fd, const CDROM_SEEK_AUDIO_MSF* audio_msf)
1124 CDROM_TOC toc;
1125 int i, io, frame;
1126 SUB_Q_CURRENT_POSITION *cp;
1127 #if defined(linux)
1128 struct cdrom_msf0 msf;
1129 struct cdrom_subchnl sc;
1130 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1131 struct ioc_play_msf msf;
1132 struct ioc_read_subchannel read_sc;
1133 struct cd_sub_channel_info sc;
1134 int final_frame;
1135 #endif
1137 /* Use the information on the TOC to compute the new current
1138 * position, which is shadowed on the cache. [Portable]. */
1139 frame = FRAME_OF_MSF(*audio_msf);
1141 if ((io = CDROM_ReadTOC(dev, fd, &toc)) != 0) return io;
1143 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
1144 if (FRAME_OF_TOC(toc,i)>frame) break;
1145 if (i <= toc.FirstTrack || i > toc.LastTrack+1)
1146 return STATUS_INVALID_PARAMETER;
1147 i--;
1148 RtlEnterCriticalSection( &cache_section );
1149 cp = &cdrom_cache[dev].CurrentPosition;
1150 cp->FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1151 cp->Control = toc.TrackData[i-toc.FirstTrack].Control;
1152 cp->ADR = toc.TrackData[i-toc.FirstTrack].Adr;
1153 cp->TrackNumber = toc.TrackData[i-toc.FirstTrack].TrackNumber;
1154 cp->IndexNumber = 0; /* FIXME: where do they keep these? */
1155 cp->AbsoluteAddress[0] = 0;
1156 cp->AbsoluteAddress[1] = toc.TrackData[i-toc.FirstTrack].Address[1];
1157 cp->AbsoluteAddress[2] = toc.TrackData[i-toc.FirstTrack].Address[2];
1158 cp->AbsoluteAddress[3] = toc.TrackData[i-toc.FirstTrack].Address[3];
1159 frame -= FRAME_OF_TOC(toc,i);
1160 cp->TrackRelativeAddress[0] = 0;
1161 MSF_OF_FRAME(cp->TrackRelativeAddress[1], frame);
1162 RtlLeaveCriticalSection( &cache_section );
1164 /* If playing, then issue a seek command, otherwise do nothing */
1165 #ifdef linux
1166 sc.cdsc_format = CDROM_MSF;
1168 io = ioctl(fd, CDROMSUBCHNL, &sc);
1169 if (io == -1)
1171 TRACE("opened or no_media (%s)!\n", strerror(errno));
1172 CDROM_ClearCacheEntry(dev);
1173 return CDROM_GetStatusCode(io);
1175 if (sc.cdsc_audiostatus==CDROM_AUDIO_PLAY)
1177 msf.minute = audio_msf->M;
1178 msf.second = audio_msf->S;
1179 msf.frame = audio_msf->F;
1180 return CDROM_GetStatusCode(ioctl(fd, CDROMSEEK, &msf));
1182 return STATUS_SUCCESS;
1183 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1184 read_sc.address_format = CD_MSF_FORMAT;
1185 read_sc.track = 0;
1186 read_sc.data_len = sizeof(sc);
1187 read_sc.data = &sc;
1188 read_sc.data_format = CD_CURRENT_POSITION;
1190 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1191 if (io == -1)
1193 TRACE("opened or no_media (%s)!\n", strerror(errno));
1194 CDROM_ClearCacheEntry(dev);
1195 return CDROM_GetStatusCode(io);
1197 if (sc.header.audio_status==CD_AS_PLAY_IN_PROGRESS)
1200 msf.start_m = audio_msf->M;
1201 msf.start_s = audio_msf->S;
1202 msf.start_f = audio_msf->F;
1203 final_frame = FRAME_OF_TOC(toc,toc.LastTrack+1)-1;
1204 MSF_OF_FRAME(msf.end_m, final_frame);
1206 return CDROM_GetStatusCode(ioctl(fd, CDIOCPLAYMSF, &msf));
1208 return STATUS_SUCCESS;
1209 #else
1210 return STATUS_NOT_SUPPORTED;
1211 #endif
1214 /******************************************************************
1215 * CDROM_PauseAudio
1219 static NTSTATUS CDROM_PauseAudio(int fd)
1221 #if defined(linux)
1222 return CDROM_GetStatusCode(ioctl(fd, CDROMPAUSE));
1223 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1224 return CDROM_GetStatusCode(ioctl(fd, CDIOCPAUSE, NULL));
1225 #else
1226 return STATUS_NOT_SUPPORTED;
1227 #endif
1230 /******************************************************************
1231 * CDROM_ResumeAudio
1235 static NTSTATUS CDROM_ResumeAudio(int fd)
1237 #if defined(linux)
1238 return CDROM_GetStatusCode(ioctl(fd, CDROMRESUME));
1239 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1240 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESUME, NULL));
1241 #else
1242 return STATUS_NOT_SUPPORTED;
1243 #endif
1246 /******************************************************************
1247 * CDROM_StopAudio
1251 static NTSTATUS CDROM_StopAudio(int fd)
1253 #if defined(linux)
1254 return CDROM_GetStatusCode(ioctl(fd, CDROMSTOP));
1255 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1256 return CDROM_GetStatusCode(ioctl(fd, CDIOCSTOP, NULL));
1257 #else
1258 return STATUS_NOT_SUPPORTED;
1259 #endif
1262 /******************************************************************
1263 * CDROM_GetVolume
1267 static NTSTATUS CDROM_GetVolume(int fd, VOLUME_CONTROL* vc)
1269 #if defined(linux)
1270 struct cdrom_volctrl volc;
1271 int io;
1273 io = ioctl(fd, CDROMVOLREAD, &volc);
1274 if (io != -1)
1276 vc->PortVolume[0] = volc.channel0;
1277 vc->PortVolume[1] = volc.channel1;
1278 vc->PortVolume[2] = volc.channel2;
1279 vc->PortVolume[3] = volc.channel3;
1281 return CDROM_GetStatusCode(io);
1282 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1283 struct ioc_vol volc;
1284 int io;
1286 io = ioctl(fd, CDIOCGETVOL, &volc);
1287 if (io != -1)
1289 vc->PortVolume[0] = volc.vol[0];
1290 vc->PortVolume[1] = volc.vol[1];
1291 vc->PortVolume[2] = volc.vol[2];
1292 vc->PortVolume[3] = volc.vol[3];
1294 return CDROM_GetStatusCode(io);
1295 #else
1296 return STATUS_NOT_SUPPORTED;
1297 #endif
1300 /******************************************************************
1301 * CDROM_SetVolume
1305 static NTSTATUS CDROM_SetVolume(int fd, const VOLUME_CONTROL* vc)
1307 #if defined(linux)
1308 struct cdrom_volctrl volc;
1310 volc.channel0 = vc->PortVolume[0];
1311 volc.channel1 = vc->PortVolume[1];
1312 volc.channel2 = vc->PortVolume[2];
1313 volc.channel3 = vc->PortVolume[3];
1315 return CDROM_GetStatusCode(ioctl(fd, CDROMVOLCTRL, &volc));
1316 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1317 struct ioc_vol volc;
1319 volc.vol[0] = vc->PortVolume[0];
1320 volc.vol[1] = vc->PortVolume[1];
1321 volc.vol[2] = vc->PortVolume[2];
1322 volc.vol[3] = vc->PortVolume[3];
1324 return CDROM_GetStatusCode(ioctl(fd, CDIOCSETVOL, &volc));
1325 #else
1326 return STATUS_NOT_SUPPORTED;
1327 #endif
1330 /******************************************************************
1331 * CDROM_RawRead
1333 * Some features of this IOCTL are rather poorly documented and
1334 * not really intuitive either:
1336 * 1. Although the DiskOffset parameter is meant to be a
1337 * byte offset into the disk, it is in fact the sector
1338 * number multiplied by 2048 regardless of the actual
1339 * sector size.
1341 * 2. The least significant 11 bits of DiskOffset are ignored.
1343 * 3. The TrackMode parameter has no effect on the sector
1344 * size. The entire raw sector (i.e. 2352 bytes of data)
1345 * is always returned. IMO the TrackMode is only used
1346 * to check the correct sector type.
1349 static NTSTATUS CDROM_RawRead(int fd, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
1351 int ret = STATUS_NOT_SUPPORTED;
1352 int io = -1;
1354 TRACE("RAW_READ_INFO: DiskOffset=%i,%i SectorCount=%i TrackMode=%i\n buffer=%p len=%i sz=%p\n",
1355 raw->DiskOffset.u.HighPart, raw->DiskOffset.u.LowPart, raw->SectorCount, raw->TrackMode, buffer, len, sz);
1357 if (len < raw->SectorCount * 2352) return STATUS_BUFFER_TOO_SMALL;
1359 #if defined(linux)
1360 if (raw->DiskOffset.u.HighPart & ~2047) {
1361 WARN("DiskOffset points to a sector >= 2**32\n");
1362 return ret;
1365 switch (raw->TrackMode)
1367 case YellowMode2:
1368 case XAForm2:
1370 DWORD lba = raw->DiskOffset.QuadPart >> 11;
1371 struct cdrom_msf* msf;
1372 PBYTE *bp; /* current buffer pointer */
1373 DWORD i;
1375 if ((lba + raw->SectorCount) >
1376 ((1 << 8*sizeof(msf->cdmsf_min0)) * CD_SECS * CD_FRAMES
1377 - CD_MSF_OFFSET)) {
1378 WARN("DiskOffset not accessible with MSF\n");
1379 return ret;
1382 /* Linux reads only one sector at a time.
1383 * ioctl CDROMREADRAW takes struct cdrom_msf as an argument
1384 * on the contrary to what header comments state.
1386 lba += CD_MSF_OFFSET;
1387 for (i = 0, bp = buffer; i < raw->SectorCount;
1388 i++, lba++, bp += 2352)
1390 msf = (struct cdrom_msf*)bp;
1391 msf->cdmsf_min0 = lba / CD_FRAMES / CD_SECS;
1392 msf->cdmsf_sec0 = lba / CD_FRAMES % CD_SECS;
1393 msf->cdmsf_frame0 = lba % CD_FRAMES;
1394 io = ioctl(fd, CDROMREADRAW, msf);
1395 if (io != 0)
1397 *sz = 2352 * i;
1398 return CDROM_GetStatusCode(io);
1401 break;
1404 case CDDA:
1406 struct cdrom_read_audio cdra;
1408 cdra.addr.lba = raw->DiskOffset.QuadPart >> 11;
1409 TRACE("reading at %u\n", cdra.addr.lba);
1410 cdra.addr_format = CDROM_LBA;
1411 cdra.nframes = raw->SectorCount;
1412 cdra.buf = buffer;
1413 io = ioctl(fd, CDROMREADAUDIO, &cdra);
1414 break;
1417 default:
1418 FIXME("NIY: %d\n", raw->TrackMode);
1419 return STATUS_INVALID_PARAMETER;
1421 #else
1422 switch (raw->TrackMode)
1424 case YellowMode2:
1425 FIXME("YellowMode2: NIY\n");
1426 return ret;
1427 case XAForm2:
1428 FIXME("XAForm2: NIY\n");
1429 return ret;
1430 case CDDA:
1431 FIXME("CDDA: NIY\n");
1432 return ret;
1433 default:
1434 FIXME("NIY: %d\n", raw->TrackMode);
1435 return STATUS_INVALID_PARAMETER;
1437 #endif
1439 *sz = 2352 * raw->SectorCount;
1440 ret = CDROM_GetStatusCode(io);
1441 return ret;
1444 /******************************************************************
1445 * CDROM_ScsiPassThroughDirect
1446 * Implements IOCTL_SCSI_PASS_THROUGH_DIRECT
1449 static NTSTATUS CDROM_ScsiPassThroughDirect(int fd, PSCSI_PASS_THROUGH_DIRECT pPacket)
1451 int ret = STATUS_NOT_SUPPORTED;
1452 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1453 sg_io_hdr_t cmd;
1454 int io;
1455 #elif defined HAVE_SCSIREQ_T_CMD
1456 scsireq_t cmd;
1457 int io;
1458 #endif
1460 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1461 return STATUS_BUFFER_TOO_SMALL;
1463 if (pPacket->CdbLength > 16)
1464 return STATUS_INVALID_PARAMETER;
1466 #ifdef SENSEBUFLEN
1467 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1468 return STATUS_INVALID_PARAMETER;
1469 #elif defined HAVE_REQUEST_SENSE
1470 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1471 return STATUS_INVALID_PARAMETER;
1472 #endif
1474 if (pPacket->DataTransferLength > 0 && !pPacket->DataBuffer)
1475 return STATUS_INVALID_PARAMETER;
1477 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1478 RtlZeroMemory(&cmd, sizeof(cmd));
1480 cmd.interface_id = 'S';
1481 cmd.cmd_len = pPacket->CdbLength;
1482 cmd.mx_sb_len = pPacket->SenseInfoLength;
1483 cmd.dxfer_len = pPacket->DataTransferLength;
1484 cmd.dxferp = pPacket->DataBuffer;
1485 cmd.cmdp = pPacket->Cdb;
1486 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1487 cmd.timeout = pPacket->TimeOutValue*1000;
1489 switch (pPacket->DataIn)
1491 case SCSI_IOCTL_DATA_IN:
1492 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1493 break;
1494 case SCSI_IOCTL_DATA_OUT:
1495 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1496 break;
1497 case SCSI_IOCTL_DATA_UNSPECIFIED:
1498 cmd.dxfer_direction = SG_DXFER_NONE;
1499 break;
1500 default:
1501 return STATUS_INVALID_PARAMETER;
1504 io = ioctl(fd, SG_IO, &cmd);
1506 pPacket->ScsiStatus = cmd.status;
1507 pPacket->DataTransferLength = cmd.resid;
1508 pPacket->SenseInfoLength = cmd.sb_len_wr;
1510 ret = CDROM_GetStatusCode(io);
1512 #elif defined HAVE_SCSIREQ_T_CMD
1514 memset(&cmd, 0, sizeof(cmd));
1515 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1517 cmd.cmdlen = pPacket->CdbLength;
1518 cmd.databuf = pPacket->DataBuffer;
1519 cmd.datalen = pPacket->DataTransferLength;
1520 cmd.senselen = pPacket->SenseInfoLength;
1521 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1523 switch (pPacket->DataIn)
1525 case SCSI_IOCTL_DATA_OUT:
1526 cmd.flags |= SCCMD_WRITE;
1527 break;
1528 case SCSI_IOCTL_DATA_IN:
1529 cmd.flags |= SCCMD_READ;
1530 break;
1531 case SCSI_IOCTL_DATA_UNSPECIFIED:
1532 cmd.flags = 0;
1533 break;
1534 default:
1535 return STATUS_INVALID_PARAMETER;
1538 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1540 switch (cmd.retsts)
1542 case SCCMD_OK: break;
1543 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1544 break;
1545 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1546 break;
1547 case SCCMD_SENSE: break;
1548 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1549 break;
1552 if (pPacket->SenseInfoLength != 0)
1554 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1555 cmd.sense, pPacket->SenseInfoLength);
1558 pPacket->ScsiStatus = cmd.status;
1560 ret = CDROM_GetStatusCode(io);
1561 #endif
1562 return ret;
1565 /******************************************************************
1566 * CDROM_ScsiPassThrough
1567 * Implements IOCTL_SCSI_PASS_THROUGH
1570 static NTSTATUS CDROM_ScsiPassThrough(int fd, PSCSI_PASS_THROUGH pPacket)
1572 int ret = STATUS_NOT_SUPPORTED;
1573 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1574 sg_io_hdr_t cmd;
1575 int io;
1576 #elif defined HAVE_SCSIREQ_T_CMD
1577 scsireq_t cmd;
1578 int io;
1579 #endif
1581 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1582 return STATUS_BUFFER_TOO_SMALL;
1584 if (pPacket->CdbLength > 16)
1585 return STATUS_INVALID_PARAMETER;
1587 #ifdef SENSEBUFLEN
1588 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1589 return STATUS_INVALID_PARAMETER;
1590 #elif defined HAVE_REQUEST_SENSE
1591 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1592 return STATUS_INVALID_PARAMETER;
1593 #endif
1595 if (pPacket->DataTransferLength > 0 && pPacket->DataBufferOffset < sizeof(SCSI_PASS_THROUGH))
1596 return STATUS_INVALID_PARAMETER;
1598 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1599 RtlZeroMemory(&cmd, sizeof(cmd));
1601 cmd.interface_id = 'S';
1602 cmd.dxfer_len = pPacket->DataTransferLength;
1603 cmd.dxferp = (char*)pPacket + pPacket->DataBufferOffset;
1604 cmd.cmd_len = pPacket->CdbLength;
1605 cmd.cmdp = pPacket->Cdb;
1606 cmd.mx_sb_len = pPacket->SenseInfoLength;
1607 cmd.timeout = pPacket->TimeOutValue*1000;
1609 if(cmd.mx_sb_len > 0)
1610 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1612 switch (pPacket->DataIn)
1614 case SCSI_IOCTL_DATA_IN:
1615 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1616 break;
1617 case SCSI_IOCTL_DATA_OUT:
1618 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1619 break;
1620 case SCSI_IOCTL_DATA_UNSPECIFIED:
1621 cmd.dxfer_direction = SG_DXFER_NONE;
1622 break;
1623 default:
1624 return STATUS_INVALID_PARAMETER;
1627 io = ioctl(fd, SG_IO, &cmd);
1629 pPacket->ScsiStatus = cmd.status;
1630 pPacket->DataTransferLength = cmd.resid;
1631 pPacket->SenseInfoLength = cmd.sb_len_wr;
1633 ret = CDROM_GetStatusCode(io);
1635 #elif defined HAVE_SCSIREQ_T_CMD
1637 memset(&cmd, 0, sizeof(cmd));
1638 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1640 if ( pPacket->DataBufferOffset > 0x1000 )
1642 cmd.databuf = (void*)pPacket->DataBufferOffset;
1644 else
1646 cmd.databuf = (char*)pPacket + pPacket->DataBufferOffset;
1649 cmd.cmdlen = pPacket->CdbLength;
1650 cmd.datalen = pPacket->DataTransferLength;
1651 cmd.senselen = pPacket->SenseInfoLength;
1652 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1654 switch (pPacket->DataIn)
1656 case SCSI_IOCTL_DATA_OUT:
1657 cmd.flags |= SCCMD_WRITE;
1658 break;
1659 case SCSI_IOCTL_DATA_IN:
1660 cmd.flags |= SCCMD_READ;
1661 break;
1662 case SCSI_IOCTL_DATA_UNSPECIFIED:
1663 cmd.flags = 0;
1664 break;
1665 default:
1666 return STATUS_INVALID_PARAMETER;
1669 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1671 switch (cmd.retsts)
1673 case SCCMD_OK: break;
1674 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1675 break;
1676 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1677 break;
1678 case SCCMD_SENSE: break;
1679 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1680 break;
1683 if (pPacket->SenseInfoLength != 0)
1685 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1686 cmd.sense, pPacket->SenseInfoLength);
1689 pPacket->ScsiStatus = cmd.status;
1691 ret = CDROM_GetStatusCode(io);
1692 #endif
1693 return ret;
1696 /******************************************************************
1697 * CDROM_ScsiGetCaps
1701 static NTSTATUS CDROM_ScsiGetCaps(PIO_SCSI_CAPABILITIES caps)
1703 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
1705 caps->Length = sizeof(*caps);
1706 #ifdef SG_SCATTER_SZ
1707 caps->MaximumTransferLength = SG_SCATTER_SZ; /* FIXME */
1708 caps->MaximumPhysicalPages = SG_SCATTER_SZ / getpagesize();
1709 caps->SupportedAsynchronousEvents = TRUE;
1710 caps->AlignmentMask = getpagesize();
1711 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
1712 caps->AdapterScansDown = FALSE; /* FIXME ? */
1713 caps->AdapterUsesPio = FALSE; /* FIXME ? */
1714 ret = STATUS_SUCCESS;
1715 #else
1716 FIXME("Unimplemented\n");
1717 #endif
1718 return ret;
1721 /******************************************************************
1722 * CDROM_GetAddress
1724 * implements IOCTL_SCSI_GET_ADDRESS
1726 static NTSTATUS CDROM_GetAddress(int fd, SCSI_ADDRESS* address)
1728 UCHAR portnum, busid, targetid, lun;
1730 address->Length = sizeof(SCSI_ADDRESS);
1731 if ( ! CDROM_GetInterfaceInfo(fd, &portnum, &busid, &targetid, &lun))
1732 return STATUS_NOT_SUPPORTED;
1734 address->PortNumber = portnum; /* primary=0 secondary=1 for ide */
1735 address->PathId = busid; /* always 0 for ide */
1736 address->TargetId = targetid; /* master=0 slave=1 for ide */
1737 address->Lun = lun;
1738 return STATUS_SUCCESS;
1741 /******************************************************************
1742 * DVD_StartSession
1746 static NTSTATUS DVD_StartSession(int fd, const DVD_SESSION_ID *sid_in, PDVD_SESSION_ID sid_out)
1748 #if defined(linux)
1749 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1750 dvd_authinfo auth_info;
1752 memset( &auth_info, 0, sizeof( auth_info ) );
1753 auth_info.type = DVD_LU_SEND_AGID;
1754 if (sid_in) auth_info.lsa.agid = *(const int*)sid_in; /* ?*/
1756 TRACE("fd 0x%08x\n",fd);
1757 ret =CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
1758 *sid_out = auth_info.lsa.agid;
1759 return ret;
1760 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1761 return STATUS_NOT_SUPPORTED;
1762 #else
1763 return STATUS_NOT_SUPPORTED;
1764 #endif
1767 /******************************************************************
1768 * DVD_EndSession
1772 static NTSTATUS DVD_EndSession(int fd, const DVD_SESSION_ID *sid)
1774 #if defined(linux)
1775 dvd_authinfo auth_info;
1777 memset( &auth_info, 0, sizeof( auth_info ) );
1778 auth_info.type = DVD_INVALIDATE_AGID;
1779 auth_info.lsa.agid = *(const int*)sid;
1781 TRACE("\n");
1782 return CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
1783 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1784 return STATUS_NOT_SUPPORTED;
1785 #else
1786 return STATUS_NOT_SUPPORTED;
1787 #endif
1790 /******************************************************************
1791 * DVD_SendKey
1795 static NTSTATUS DVD_SendKey(int fd, const DVD_COPY_PROTECT_KEY *key)
1797 #if defined(linux)
1798 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1799 dvd_authinfo auth_info;
1801 memset( &auth_info, 0, sizeof( auth_info ) );
1802 switch (key->KeyType)
1804 case DvdChallengeKey:
1805 auth_info.type = DVD_HOST_SEND_CHALLENGE;
1806 auth_info.hsc.agid = (int)key->SessionId;
1807 TRACE("DvdChallengeKey ioc 0x%x\n", DVD_AUTH );
1808 memcpy( auth_info.hsc.chal, key->KeyData, DVD_CHALLENGE_SIZE );
1809 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1810 break;
1811 case DvdBusKey2:
1812 auth_info.type = DVD_HOST_SEND_KEY2;
1813 auth_info.hsk.agid = (int)key->SessionId;
1815 memcpy( auth_info.hsk.key, key->KeyData, DVD_KEY_SIZE );
1817 TRACE("DvdBusKey2\n");
1818 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1819 break;
1821 default:
1822 FIXME("Unknown Keytype 0x%x\n",key->KeyType);
1824 return ret;
1825 #else
1826 FIXME("unsupported on this platform\n");
1827 return STATUS_NOT_SUPPORTED;
1828 #endif
1831 /******************************************************************
1832 * DVD_ReadKey
1836 static NTSTATUS DVD_ReadKey(int fd, PDVD_COPY_PROTECT_KEY key)
1838 #if defined(linux)
1839 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1840 dvd_struct dvd;
1841 dvd_authinfo auth_info;
1843 memset( &dvd, 0, sizeof( dvd_struct ) );
1844 memset( &auth_info, 0, sizeof( auth_info ) );
1845 switch (key->KeyType)
1847 case DvdDiskKey:
1849 dvd.type = DVD_STRUCT_DISCKEY;
1850 dvd.disckey.agid = (int)key->SessionId;
1851 memset( dvd.disckey.value, 0, DVD_DISCKEY_SIZE );
1853 TRACE("DvdDiskKey\n");
1854 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
1855 if (ret == STATUS_SUCCESS)
1856 memcpy(key->KeyData,dvd.disckey.value,DVD_DISCKEY_SIZE);
1857 break;
1858 case DvdTitleKey:
1859 auth_info.type = DVD_LU_SEND_TITLE_KEY;
1860 auth_info.lstk.agid = (int)key->SessionId;
1861 auth_info.lstk.lba = (int)(key->Parameters.TitleOffset.QuadPart>>11);
1862 TRACE("DvdTitleKey session %d Quadpart 0x%08lx offset 0x%08x\n",
1863 (int)key->SessionId, (long)key->Parameters.TitleOffset.QuadPart,
1864 auth_info.lstk.lba);
1865 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1866 if (ret == STATUS_SUCCESS)
1867 memcpy(key->KeyData, auth_info.lstk.title_key, DVD_KEY_SIZE );
1868 break;
1869 case DvdChallengeKey:
1871 auth_info.type = DVD_LU_SEND_CHALLENGE;
1872 auth_info.lsc.agid = (int)key->SessionId;
1874 TRACE("DvdChallengeKey\n");
1875 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1876 if (ret == STATUS_SUCCESS)
1877 memcpy( key->KeyData, auth_info.lsc.chal, DVD_CHALLENGE_SIZE );
1878 break;
1879 case DvdAsf:
1880 auth_info.type = DVD_LU_SEND_ASF;
1881 TRACE("DvdAsf\n");
1882 auth_info.lsasf.asf=((PDVD_ASF)key->KeyData)->SuccessFlag;
1883 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1884 ((PDVD_ASF)key->KeyData)->SuccessFlag = auth_info.lsasf.asf;
1885 break;
1886 case DvdBusKey1:
1887 auth_info.type = DVD_LU_SEND_KEY1;
1888 auth_info.lsk.agid = (int)key->SessionId;
1890 TRACE("DvdBusKey1\n");
1891 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1893 if (ret == STATUS_SUCCESS)
1894 memcpy( key->KeyData, auth_info.lsk.key, DVD_KEY_SIZE );
1895 break;
1896 case DvdGetRpcKey:
1897 auth_info.type = DVD_LU_SEND_RPC_STATE;
1899 TRACE("DvdGetRpcKey\n");
1900 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1902 if (ret == STATUS_SUCCESS)
1904 ((PDVD_RPC_KEY)key->KeyData)->TypeCode = auth_info.lrpcs.type;
1905 ((PDVD_RPC_KEY)key->KeyData)->RegionMask = auth_info.lrpcs.region_mask;
1906 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme = auth_info.lrpcs.rpc_scheme;
1908 break;
1909 default:
1910 FIXME("Unknown keytype 0x%x\n",key->KeyType);
1912 return ret;
1913 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1914 TRACE("bsd\n");
1915 return STATUS_NOT_SUPPORTED;
1916 #else
1917 TRACE("outside\n");
1918 return STATUS_NOT_SUPPORTED;
1919 #endif
1922 /******************************************************************
1923 * DVD_GetRegion
1927 static NTSTATUS DVD_GetRegion(int dev, PDVD_REGION region)
1929 FIXME("\n");
1930 return STATUS_SUCCESS;
1934 /******************************************************************
1935 * DVD_GetRegion
1939 static NTSTATUS DVD_ReadStructure(int dev, const DVD_READ_STRUCTURE *structure, PDVD_LAYER_DESCRIPTOR layer)
1941 #ifdef DVD_READ_STRUCT
1942 dvd_struct s;
1944 if (structure->BlockByteOffset.u.HighPart || structure->BlockByteOffset.u.LowPart)
1945 FIXME(": BlockByteOffset is not handled\n");
1947 switch (structure->Format)
1949 case DvdPhysicalDescriptor:
1950 s.type = DVD_STRUCT_PHYSICAL;
1951 s.physical.layer_num = structure->LayerNumber;
1952 break;
1954 case DvdCopyrightDescriptor:
1955 s.type = DVD_STRUCT_COPYRIGHT;
1956 s.copyright.layer_num = structure->LayerNumber;
1957 break;
1959 case DvdDiskKeyDescriptor:
1960 s.type = DVD_STRUCT_DISCKEY;
1961 s.disckey.agid = structure->SessionId;
1962 break;
1964 case DvdBCADescriptor:
1965 s.type = DVD_STRUCT_BCA;
1966 break;
1968 case DvdManufacturerDescriptor:
1969 s.type = DVD_STRUCT_MANUFACT;
1970 s.manufact.layer_num = structure->LayerNumber;
1971 break;
1973 case DvdMaxDescriptor: /* This is not a real request, no matter what MSDN says! */
1974 default:
1975 return STATUS_INVALID_PARAMETER;
1978 if (ioctl(dev, DVD_READ_STRUCT, &s) < 0)
1979 return STATUS_INVALID_PARAMETER;
1981 switch (structure->Format)
1983 case DvdPhysicalDescriptor:
1985 internal_dvd_layer_descriptor *p = (internal_dvd_layer_descriptor *) layer;
1986 struct dvd_layer *l = &s.physical.layer[s.physical.layer_num];
1988 p->MagicHeader[0] = 2;
1989 p->MagicHeader[1] = 8;
1990 p->MagicHeader[2] = 0;
1991 p->MagicHeader[3] = 0;
1992 p->BookVersion = l->book_version;
1993 p->BookType = l->book_type;
1994 p->MinimumRate = l->min_rate;
1995 p->DiskSize = l->disc_size;
1996 p->LayerType = l->layer_type;
1997 p->TrackPath = l->track_path;
1998 p->NumberOfLayers = l->nlayers;
1999 p->Reserved1 = 0;
2000 p->TrackDensity = l->track_density;
2001 p->LinearDensity = l->linear_density;
2002 p->StartingDataSector = l->start_sector;
2003 p->EndDataSector = l->end_sector;
2004 p->EndLayerZeroSector = l->end_sector_l0;
2005 p->Reserved5 = 0;
2006 p->BCAFlag = l->bca;
2007 p->Reserved6 = 0;
2009 break;
2011 case DvdCopyrightDescriptor:
2013 PDVD_COPYRIGHT_DESCRIPTOR p = (PDVD_COPYRIGHT_DESCRIPTOR) layer;
2015 p->CopyrightProtectionType = s.copyright.cpst;
2016 p->RegionManagementInformation = s.copyright.rmi;
2017 p->Reserved = 0;
2019 break;
2021 case DvdDiskKeyDescriptor:
2023 PDVD_DISK_KEY_DESCRIPTOR p = (PDVD_DISK_KEY_DESCRIPTOR) layer;
2025 memcpy(p->DiskKeyData, s.disckey.value, 2048);
2027 break;
2029 case DvdBCADescriptor:
2031 PDVD_BCA_DESCRIPTOR p = (PDVD_BCA_DESCRIPTOR) layer;
2033 memcpy(p->BCAInformation, s.bca.value, s.bca.len);
2035 break;
2037 case DvdManufacturerDescriptor:
2039 PDVD_MANUFACTURER_DESCRIPTOR p = (PDVD_MANUFACTURER_DESCRIPTOR) layer;
2041 memcpy(p->ManufacturingInformation, s.manufact.value, 2048);
2043 break;
2045 case DvdMaxDescriptor: /* Suppress warning */
2046 break;
2048 #else
2049 FIXME("\n");
2050 #endif
2051 return STATUS_SUCCESS;
2055 /******************************************************************
2056 * GetInquiryData
2057 * Implements the IOCTL_GET_INQUIRY_DATA ioctl.
2058 * Returns Inquiry data for all devices on the specified scsi bus
2059 * Returns STATUS_BUFFER_TOO_SMALL if the output buffer is too small,
2060 * STATUS_INVALID_DEVICE_REQUEST if the given handle isn't to a SCSI device,
2061 * or STATUS_NOT_SUPPORTED if the OS driver is too old
2063 static NTSTATUS GetInquiryData(int fd, PSCSI_ADAPTER_BUS_INFO BufferOut, DWORD OutBufferSize)
2065 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
2066 PSCSI_INQUIRY_DATA pInquiryData = NULL;
2067 UCHAR sense_buffer[32];
2068 int iochk, version;
2069 sg_io_hdr_t iocmd;
2070 UCHAR inquiry[INQ_CMD_LEN] = {INQUIRY, 0, 0, 0, INQ_REPLY_LEN, 0};
2072 /* Check we have a SCSI device and a supported driver */
2073 if(ioctl(fd, SG_GET_VERSION_NUM, &version) != 0)
2075 WARN("IOCTL_SCSI_GET_INQUIRY_DATA sg driver is not loaded\n");
2076 return STATUS_INVALID_DEVICE_REQUEST;
2078 if(version < 30000 )
2079 return STATUS_NOT_SUPPORTED;
2081 /* FIXME: Enumerate devices on the bus */
2082 BufferOut->NumberOfBuses = 1;
2083 BufferOut->BusData[0].NumberOfLogicalUnits = 1;
2084 BufferOut->BusData[0].InquiryDataOffset = sizeof(SCSI_ADAPTER_BUS_INFO);
2086 pInquiryData = (PSCSI_INQUIRY_DATA)(BufferOut + 1);
2088 RtlZeroMemory(&iocmd, sizeof(iocmd));
2089 iocmd.interface_id = 'S';
2090 iocmd.cmd_len = sizeof(inquiry);
2091 iocmd.mx_sb_len = sizeof(sense_buffer);
2092 iocmd.dxfer_direction = SG_DXFER_FROM_DEV;
2093 iocmd.dxfer_len = INQ_REPLY_LEN;
2094 iocmd.dxferp = pInquiryData->InquiryData;
2095 iocmd.cmdp = inquiry;
2096 iocmd.sbp = sense_buffer;
2097 iocmd.timeout = 1000;
2099 iochk = ioctl(fd, SG_IO, &iocmd);
2100 if(iochk != 0)
2101 WARN("ioctl SG_IO returned %d, error (%s)\n", iochk, strerror(errno));
2103 CDROM_GetInterfaceInfo(fd, &BufferOut->BusData[0].InitiatorBusId, &pInquiryData->PathId, &pInquiryData->TargetId, &pInquiryData->Lun);
2104 pInquiryData->DeviceClaimed = TRUE;
2105 pInquiryData->InquiryDataLength = INQ_REPLY_LEN;
2106 pInquiryData->NextInquiryDataOffset = 0;
2107 return STATUS_SUCCESS;
2108 #else
2109 FIXME("not implemented for nonlinux\n");
2110 return STATUS_NOT_SUPPORTED;
2111 #endif
2114 /******************************************************************
2115 * CDROM_DeviceIoControl
2119 NTSTATUS CDROM_DeviceIoControl(HANDLE hDevice,
2120 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
2121 PVOID UserApcContext,
2122 PIO_STATUS_BLOCK piosb,
2123 ULONG dwIoControlCode,
2124 LPVOID lpInBuffer, DWORD nInBufferSize,
2125 LPVOID lpOutBuffer, DWORD nOutBufferSize)
2127 DWORD sz = 0;
2128 NTSTATUS status = STATUS_SUCCESS;
2129 int fd, needs_close, dev;
2131 TRACE("%p %s %p %d %p %d %p\n",
2132 hDevice, iocodex(dwIoControlCode), lpInBuffer, nInBufferSize,
2133 lpOutBuffer, nOutBufferSize, piosb);
2135 piosb->Information = 0;
2137 if ((status = server_get_unix_fd( hDevice, 0, &fd, &needs_close, NULL, NULL )))
2139 if (status == STATUS_BAD_DEVICE_TYPE) return status; /* no associated fd */
2140 goto error;
2143 if ((status = CDROM_Open(fd, &dev)))
2145 if (needs_close) close( fd );
2146 goto error;
2149 switch (dwIoControlCode)
2151 case IOCTL_STORAGE_CHECK_VERIFY:
2152 case IOCTL_CDROM_CHECK_VERIFY:
2153 case IOCTL_DISK_CHECK_VERIFY:
2154 sz = 0;
2155 CDROM_ClearCacheEntry(dev);
2156 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2157 status = STATUS_INVALID_PARAMETER;
2158 else status = CDROM_Verify(dev, fd);
2159 break;
2161 /* EPP case IOCTL_STORAGE_CHECK_VERIFY2: */
2163 /* EPP case IOCTL_STORAGE_FIND_NEW_DEVICES: */
2164 /* EPP case IOCTL_CDROM_FIND_NEW_DEVICES: */
2166 case IOCTL_STORAGE_LOAD_MEDIA:
2167 case IOCTL_CDROM_LOAD_MEDIA:
2168 sz = 0;
2169 CDROM_ClearCacheEntry(dev);
2170 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2171 status = STATUS_INVALID_PARAMETER;
2172 else status = CDROM_SetTray(fd, FALSE);
2173 break;
2174 case IOCTL_STORAGE_EJECT_MEDIA:
2175 sz = 0;
2176 CDROM_ClearCacheEntry(dev);
2177 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2178 status = STATUS_INVALID_PARAMETER;
2179 else
2181 #ifdef __APPLE__
2182 char name[100];
2184 /* This is ugly as hell, but Mac OS is unable to eject from the device fd,
2185 * it wants an fd for the whole device, and it also requires the device fd
2186 * to be closed first, so we have to close the handle that the caller gave us.
2187 * Also for some reason it wants the fd to be closed before we even open the parent.
2189 if ((status = get_parent_device( fd, name, sizeof(name) ))) break;
2190 NtClose( hDevice );
2191 if (needs_close) close( fd );
2192 TRACE("opening parent %s\n", name );
2193 if ((fd = open( name, O_RDONLY )) == -1)
2195 status = FILE_GetNtStatus();
2196 break;
2198 needs_close = 1;
2199 #endif
2200 status = CDROM_SetTray(fd, TRUE);
2202 break;
2204 case IOCTL_CDROM_MEDIA_REMOVAL:
2205 case IOCTL_DISK_MEDIA_REMOVAL:
2206 case IOCTL_STORAGE_MEDIA_REMOVAL:
2207 case IOCTL_STORAGE_EJECTION_CONTROL:
2208 /* FIXME the last ioctl:s is not the same as the two others...
2209 * lockcount/owner should be handled */
2210 sz = 0;
2211 CDROM_ClearCacheEntry(dev);
2212 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2213 else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) status = STATUS_BUFFER_TOO_SMALL;
2214 else status = CDROM_ControlEjection(fd, lpInBuffer);
2215 break;
2217 /* EPP case IOCTL_STORAGE_GET_MEDIA_TYPES: */
2219 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
2220 sz = sizeof(STORAGE_DEVICE_NUMBER);
2221 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2222 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2223 else status = CDROM_GetDeviceNumber(dev, lpOutBuffer);
2224 break;
2226 case IOCTL_STORAGE_RESET_DEVICE:
2227 sz = 0;
2228 CDROM_ClearCacheEntry(dev);
2229 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2230 status = STATUS_INVALID_PARAMETER;
2231 else status = CDROM_ResetAudio(fd);
2232 break;
2234 case IOCTL_CDROM_GET_CONTROL:
2235 sz = sizeof(CDROM_AUDIO_CONTROL);
2236 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2237 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2238 else status = CDROM_GetControl(dev, lpOutBuffer);
2239 break;
2241 case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
2242 sz = sizeof(DISK_GEOMETRY);
2243 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2244 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2245 else status = CDROM_GetDriveGeometry(dev, fd, lpOutBuffer);
2246 break;
2248 case IOCTL_CDROM_DISK_TYPE:
2249 sz = sizeof(CDROM_DISK_DATA);
2250 /* CDROM_ClearCacheEntry(dev); */
2251 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2252 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2253 else status = CDROM_GetDiskData(dev, fd, lpOutBuffer);
2254 break;
2256 /* EPP case IOCTL_CDROM_GET_LAST_SESSION: */
2258 case IOCTL_CDROM_READ_Q_CHANNEL:
2259 sz = sizeof(SUB_Q_CHANNEL_DATA);
2260 if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT))
2261 status = STATUS_INVALID_PARAMETER;
2262 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2263 else status = CDROM_ReadQChannel(dev, fd, lpInBuffer, lpOutBuffer);
2264 break;
2266 case IOCTL_CDROM_READ_TOC:
2267 sz = sizeof(CDROM_TOC);
2268 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2269 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2270 else status = CDROM_ReadTOC(dev, fd, lpOutBuffer);
2271 break;
2273 /* EPP case IOCTL_CDROM_READ_TOC_EX: */
2275 case IOCTL_CDROM_PAUSE_AUDIO:
2276 sz = 0;
2277 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2278 status = STATUS_INVALID_PARAMETER;
2279 else status = CDROM_PauseAudio(fd);
2280 break;
2281 case IOCTL_CDROM_PLAY_AUDIO_MSF:
2282 sz = 0;
2283 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2284 else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
2285 else status = CDROM_PlayAudioMSF(fd, lpInBuffer);
2286 break;
2287 case IOCTL_CDROM_RESUME_AUDIO:
2288 sz = 0;
2289 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2290 status = STATUS_INVALID_PARAMETER;
2291 else status = CDROM_ResumeAudio(fd);
2292 break;
2293 case IOCTL_CDROM_SEEK_AUDIO_MSF:
2294 sz = 0;
2295 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2296 else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
2297 else status = CDROM_SeekAudioMSF(dev, fd, lpInBuffer);
2298 break;
2299 case IOCTL_CDROM_STOP_AUDIO:
2300 sz = 0;
2301 CDROM_ClearCacheEntry(dev); /* Maybe intention is to change media */
2302 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2303 status = STATUS_INVALID_PARAMETER;
2304 else status = CDROM_StopAudio(fd);
2305 break;
2306 case IOCTL_CDROM_GET_VOLUME:
2307 sz = sizeof(VOLUME_CONTROL);
2308 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2309 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2310 else status = CDROM_GetVolume(fd, lpOutBuffer);
2311 break;
2312 case IOCTL_CDROM_SET_VOLUME:
2313 sz = 0;
2314 CDROM_ClearCacheEntry(dev);
2315 if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
2316 status = STATUS_INVALID_PARAMETER;
2317 else status = CDROM_SetVolume(fd, lpInBuffer);
2318 break;
2319 case IOCTL_CDROM_RAW_READ:
2320 sz = 0;
2321 if (nInBufferSize < sizeof(RAW_READ_INFO)) status = STATUS_INVALID_PARAMETER;
2322 else if (lpOutBuffer == NULL) status = STATUS_BUFFER_TOO_SMALL;
2323 else status = CDROM_RawRead(fd, lpInBuffer, lpOutBuffer,
2324 nOutBufferSize, &sz);
2325 break;
2326 case IOCTL_SCSI_GET_ADDRESS:
2327 sz = sizeof(SCSI_ADDRESS);
2328 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2329 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2330 else status = CDROM_GetAddress(fd, lpOutBuffer);
2331 break;
2332 case IOCTL_SCSI_PASS_THROUGH_DIRECT:
2333 sz = sizeof(SCSI_PASS_THROUGH_DIRECT);
2334 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2335 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH_DIRECT)) status = STATUS_BUFFER_TOO_SMALL;
2336 else status = CDROM_ScsiPassThroughDirect(fd, lpOutBuffer);
2337 break;
2338 case IOCTL_SCSI_PASS_THROUGH:
2339 sz = sizeof(SCSI_PASS_THROUGH);
2340 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2341 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH)) status = STATUS_BUFFER_TOO_SMALL;
2342 else status = CDROM_ScsiPassThrough(fd, lpOutBuffer);
2343 break;
2344 case IOCTL_SCSI_GET_CAPABILITIES:
2345 sz = sizeof(IO_SCSI_CAPABILITIES);
2346 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2347 else if (nOutBufferSize < sizeof(IO_SCSI_CAPABILITIES)) status = STATUS_BUFFER_TOO_SMALL;
2348 else status = CDROM_ScsiGetCaps(lpOutBuffer);
2349 break;
2350 case IOCTL_DVD_START_SESSION:
2351 sz = sizeof(DVD_SESSION_ID);
2352 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2353 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2354 else
2356 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
2357 *(PDVD_SESSION_ID)lpOutBuffer);
2358 status = DVD_StartSession(fd, lpInBuffer, lpOutBuffer);
2359 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
2360 *(PDVD_SESSION_ID)lpOutBuffer);
2362 break;
2363 case IOCTL_DVD_END_SESSION:
2364 sz = sizeof(DVD_SESSION_ID);
2365 if ((lpInBuffer == NULL) || (nInBufferSize < sz))status = STATUS_INVALID_PARAMETER;
2366 else status = DVD_EndSession(fd, lpInBuffer);
2367 break;
2368 case IOCTL_DVD_SEND_KEY:
2369 sz = 0;
2370 if (!lpInBuffer ||
2371 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
2372 status = STATUS_INVALID_PARAMETER;
2373 else
2375 TRACE("doing DVD_SendKey\n");
2376 status = DVD_SendKey(fd, lpInBuffer);
2378 break;
2379 case IOCTL_DVD_READ_KEY:
2380 if (!lpInBuffer ||
2381 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
2382 status = STATUS_INVALID_PARAMETER;
2383 else if (lpInBuffer !=lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
2384 else
2386 TRACE("doing DVD_READ_KEY\n");
2387 sz = ((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength;
2388 status = DVD_ReadKey(fd, lpInBuffer);
2390 break;
2391 case IOCTL_DVD_GET_REGION:
2392 sz = sizeof(DVD_REGION);
2393 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2394 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2395 TRACE("doing DVD_Get_REGION\n");
2396 status = DVD_GetRegion(fd, lpOutBuffer);
2397 break;
2398 case IOCTL_DVD_READ_STRUCTURE:
2399 sz = sizeof(DVD_LAYER_DESCRIPTOR);
2400 if (lpInBuffer == NULL || nInBufferSize != sizeof(DVD_READ_STRUCTURE)) status = STATUS_INVALID_PARAMETER;
2401 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2402 else
2404 TRACE("doing DVD_READ_STRUCTURE\n");
2405 status = DVD_ReadStructure(fd, lpInBuffer, lpOutBuffer);
2407 break;
2409 case IOCTL_SCSI_GET_INQUIRY_DATA:
2410 sz = INQ_REPLY_LEN;
2411 status = GetInquiryData(fd, lpOutBuffer, nOutBufferSize);
2412 break;
2414 default:
2415 if (needs_close) close( fd );
2416 return STATUS_NOT_SUPPORTED;
2418 if (needs_close) close( fd );
2419 error:
2420 piosb->u.Status = status;
2421 piosb->Information = sz;
2422 if (hEvent) NtSetEvent(hEvent, NULL);
2423 return status;