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