ntdll/tests: Add a FILE_APPEND_DATA test.
[wine.git] / dlls / ntdll / cdrom.c
blob59f85a0ff578b697d1e4d0433ef4764313c6b7cc
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 <libkern/OSByteOrder.h>
81 # include <sys/disk.h>
82 # include <IOKit/IOKitLib.h>
83 # include <IOKit/storage/IOMedia.h>
84 # include <IOKit/storage/IOCDMediaBSDClient.h>
85 # include <IOKit/storage/IODVDMediaBSDClient.h>
86 # include <IOKit/scsi/SCSITask.h>
87 # include <IOKit/scsi/SCSICmds_REQUEST_SENSE_Defs.h>
88 # define SENSEBUFLEN kSenseDefaultSize
90 typedef struct
92 uint32_t attribute;
93 uint32_t timeout;
94 uint32_t response;
95 uint32_t status;
96 uint8_t direction;
97 uint8_t cdbSize;
98 uint8_t reserved0144[2];
99 uint8_t cdb[16];
100 void* buffer;
101 uint64_t bufferSize;
102 void* sense;
103 uint64_t senseLen;
104 } dk_scsi_command_t;
106 #define DKIOCSCSICOMMAND _IOWR('d', 253, dk_scsi_command_t)
108 #endif
110 #define NONAMELESSUNION
111 #define NONAMELESSSTRUCT
112 #include "ntstatus.h"
113 #define WIN32_NO_STATUS
114 #include "windef.h"
115 #include "winternl.h"
116 #include "winioctl.h"
117 #include "ntddstor.h"
118 #include "ntddcdrm.h"
119 #include "ddk/ntddcdvd.h"
120 #include "ntddscsi.h"
121 #include "ntdll_misc.h"
122 #include "wine/server.h"
123 #include "wine/library.h"
124 #include "wine/debug.h"
126 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
128 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
129 lack the following constants. */
131 #ifndef CD_SECS
132 # define CD_SECS 60 /* seconds per minute */
133 #endif
134 #ifndef CD_FRAMES
135 # define CD_FRAMES 75 /* frames per second */
136 #endif
138 static const struct iocodexs
140 DWORD code;
141 const char *codex;
142 } iocodextable[] = {
143 #define X(x) { x, #x },
144 X(IOCTL_CDROM_CHECK_VERIFY)
145 X(IOCTL_CDROM_CURRENT_POSITION)
146 X(IOCTL_CDROM_DISK_TYPE)
147 X(IOCTL_CDROM_GET_CONTROL)
148 X(IOCTL_CDROM_GET_DRIVE_GEOMETRY)
149 X(IOCTL_CDROM_GET_VOLUME)
150 X(IOCTL_CDROM_LOAD_MEDIA)
151 X(IOCTL_CDROM_MEDIA_CATALOG)
152 X(IOCTL_CDROM_MEDIA_REMOVAL)
153 X(IOCTL_CDROM_PAUSE_AUDIO)
154 X(IOCTL_CDROM_PLAY_AUDIO_MSF)
155 X(IOCTL_CDROM_RAW_READ)
156 X(IOCTL_CDROM_READ_Q_CHANNEL)
157 X(IOCTL_CDROM_READ_TOC)
158 X(IOCTL_CDROM_RESUME_AUDIO)
159 X(IOCTL_CDROM_SEEK_AUDIO_MSF)
160 X(IOCTL_CDROM_SET_VOLUME)
161 X(IOCTL_CDROM_STOP_AUDIO)
162 X(IOCTL_CDROM_TRACK_ISRC)
163 X(IOCTL_DISK_MEDIA_REMOVAL)
164 X(IOCTL_DVD_END_SESSION)
165 X(IOCTL_DVD_GET_REGION)
166 X(IOCTL_DVD_READ_KEY)
167 X(IOCTL_DVD_READ_STRUCTURE)
168 X(IOCTL_DVD_SEND_KEY)
169 X(IOCTL_DVD_START_SESSION)
170 X(IOCTL_SCSI_GET_ADDRESS)
171 X(IOCTL_SCSI_GET_CAPABILITIES)
172 X(IOCTL_SCSI_GET_INQUIRY_DATA)
173 X(IOCTL_SCSI_PASS_THROUGH)
174 X(IOCTL_SCSI_PASS_THROUGH_DIRECT)
175 X(IOCTL_STORAGE_CHECK_VERIFY)
176 X(IOCTL_STORAGE_EJECTION_CONTROL)
177 X(IOCTL_STORAGE_EJECT_MEDIA)
178 X(IOCTL_STORAGE_GET_DEVICE_NUMBER)
179 X(IOCTL_STORAGE_GET_MEDIA_TYPES)
180 X(IOCTL_STORAGE_GET_MEDIA_TYPES_EX)
181 X(IOCTL_STORAGE_LOAD_MEDIA)
182 X(IOCTL_STORAGE_MEDIA_REMOVAL)
183 X(IOCTL_STORAGE_RESET_DEVICE)
184 #undef X
186 static const char *iocodex(DWORD code)
188 unsigned int i;
189 static char buffer[25];
190 for(i=0; i<sizeof(iocodextable)/sizeof(struct iocodexs); i++)
191 if (code==iocodextable[i].code)
192 return iocodextable[i].codex;
193 sprintf(buffer, "IOCTL_CODE_%x", (int)code);
194 return buffer;
197 #define INQ_REPLY_LEN 36
198 #define INQ_CMD_LEN 6
200 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
201 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
202 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
203 #define MSF_OF_FRAME(m,fr) {int f=(fr); ((UCHAR *)&(m))[2]=f%CD_FRAMES;f/=CD_FRAMES;((UCHAR *)&(m))[1]=f%CD_SECS;((UCHAR *)&(m))[0]=f/CD_SECS;}
205 /* The documented format of DVD_LAYER_DESCRIPTOR is wrong. Even the format in the
206 * DDK's header is wrong. There are four bytes at the start defined by
207 * MMC-5. The first two are the size of the structure in big-endian order as
208 * defined by MMC-5. The other two are reserved.
210 typedef struct
212 UCHAR DataLength[2];
213 UCHAR Reserved0[2];
214 UCHAR BookVersion : 4;
215 UCHAR BookType : 4;
216 UCHAR MinimumRate : 4;
217 UCHAR DiskSize : 4;
218 UCHAR LayerType : 4;
219 UCHAR TrackPath : 1;
220 UCHAR NumberOfLayers : 2;
221 UCHAR Reserved1 : 1;
222 UCHAR TrackDensity : 4;
223 UCHAR LinearDensity : 4;
224 ULONG StartingDataSector;
225 ULONG EndDataSector;
226 ULONG EndLayerZeroSector;
227 UCHAR Reserved5 : 7;
228 UCHAR BCAFlag : 1;
229 UCHAR Reserved6;
230 } internal_dvd_layer_descriptor;
233 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
234 static NTSTATUS CDROM_GetStatusCode(int);
237 #ifdef linux
239 # ifndef IDE6_MAJOR
240 # define IDE6_MAJOR 88
241 # endif
242 # ifndef IDE7_MAJOR
243 # define IDE7_MAJOR 89
244 # endif
246 # ifdef CDROM_SEND_PACKET
247 /* structure for CDROM_PACKET_COMMAND ioctl */
248 /* not all Linux versions have all the fields, so we define the
249 * structure ourselves to make sure */
250 struct linux_cdrom_generic_command
252 unsigned char cmd[CDROM_PACKET_SIZE];
253 unsigned char *buffer;
254 unsigned int buflen;
255 int stat;
256 struct request_sense *sense;
257 unsigned char data_direction;
258 int quiet;
259 int timeout;
260 void *reserved[1];
262 # endif /* CDROM_SEND_PACKET */
264 #endif /* linux */
266 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
267 * this should be removed when a proper device interface is implemented
269 * (WS) We need this to keep track of current position and to safely
270 * detect media changes. Besides this should provide a great speed up
271 * for toc inquiries.
273 struct cdrom_cache {
274 dev_t device;
275 ino_t inode;
276 char toc_good; /* if false, will reread TOC from disk */
277 CDROM_TOC toc;
278 SUB_Q_CURRENT_POSITION CurrentPosition;
280 /* who has more than 5 cdroms on his/her machine ?? */
281 /* FIXME: this should grow depending on the number of cdroms we install/configure
282 * at startup
284 #define MAX_CACHE_ENTRIES 5
285 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
287 static RTL_CRITICAL_SECTION cache_section;
288 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
290 0, 0, &cache_section,
291 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
292 0, 0, { (DWORD_PTR)(__FILE__ ": cache_section") }
294 static RTL_CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
296 /* Proposed media change function: not really needed at this time */
297 /* This is a 1 or 0 type of function */
298 #if 0
299 static int CDROM_MediaChanged(int dev)
301 int i;
303 struct cdrom_tochdr hdr;
304 struct cdrom_tocentry entry;
306 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
307 return 0;
308 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
309 return 0;
311 if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
312 return 1;
314 for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
316 if (i == hdr.cdth_trk1 + 1)
318 entry.cdte_track = CDROM_LEADOUT;
319 } else {
320 entry.cdte_track = i;
322 entry.cdte_format = CDROM_MSF;
323 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
324 return 0;
325 if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
326 sizeof(struct cdrom_tocentry)) )
327 return 1;
329 return 0;
331 #endif
334 /******************************************************************
335 * get_parent_device
337 * On Mac OS, get the device for the whole disk from a fd that points to a partition.
338 * This is ugly and inefficient, but we have no choice since the partition fd doesn't
339 * support the eject ioctl.
341 #ifdef __APPLE__
342 static NTSTATUS get_parent_device( int fd, char *name, size_t len )
344 NTSTATUS status = STATUS_NO_SUCH_FILE;
345 struct stat st;
346 int i;
347 io_service_t service;
348 CFMutableDictionaryRef dict;
349 CFTypeRef val;
351 if (fstat( fd, &st ) == -1) return FILE_GetNtStatus();
352 if (!S_ISCHR( st.st_mode )) return STATUS_OBJECT_TYPE_MISMATCH;
354 /* create a dictionary with the right major/minor numbers */
356 if (!(dict = IOServiceMatching( kIOMediaClass ))) return STATUS_NO_MEMORY;
358 i = major( st.st_rdev );
359 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
360 CFDictionaryAddValue( dict, CFSTR( "BSD Major" ), val );
361 CFRelease( val );
363 i = minor( st.st_rdev );
364 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
365 CFDictionaryAddValue( dict, CFSTR( "BSD Minor" ), val );
366 CFRelease( val );
368 CFDictionaryAddValue( dict, CFSTR("Removable"), kCFBooleanTrue );
370 service = IOServiceGetMatchingService( kIOMasterPortDefault, dict );
372 /* now look for the parent that has the "Whole" attribute set to TRUE */
374 while (service)
376 io_service_t parent = 0;
377 CFBooleanRef whole;
378 CFStringRef str;
379 int ok;
381 if (!IOObjectConformsTo( service, kIOMediaClass ))
382 goto next;
383 if (!(whole = IORegistryEntryCreateCFProperty( service, CFSTR("Whole"), NULL, 0 )))
384 goto next;
385 ok = (whole == kCFBooleanTrue);
386 CFRelease( whole );
387 if (!ok) goto next;
389 if ((str = IORegistryEntryCreateCFProperty( service, CFSTR("BSD Name"), NULL, 0 )))
391 strcpy( name, "/dev/r" );
392 CFStringGetCString( str, name + 6, len - 6, kCFStringEncodingUTF8 );
393 CFRelease( str );
394 status = STATUS_SUCCESS;
396 IOObjectRelease( service );
397 break;
399 next:
400 IORegistryEntryGetParentEntry( service, kIOServicePlane, &parent );
401 IOObjectRelease( service );
402 service = parent;
404 return status;
406 #endif
409 /******************************************************************
410 * CDROM_SyncCache [internal]
412 * Read the TOC in and store it in the cdrom_cache structure.
413 * Further requests for the TOC will be copied from the cache
414 * unless certain events like disk ejection is detected, in which
415 * case the cache will be cleared, causing it to be resynced.
416 * The cache section must be held by caller.
418 static NTSTATUS CDROM_SyncCache(int dev, int fd)
420 #ifdef linux
421 int i, tsz;
422 struct cdrom_tochdr hdr;
423 struct cdrom_tocentry entry;
425 CDROM_TOC *toc = &cdrom_cache[dev].toc;
426 cdrom_cache[dev].toc_good = 0;
428 if (ioctl(fd, CDROMREADTOCHDR, &hdr) == -1)
430 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
431 return FILE_GetNtStatus();
434 toc->FirstTrack = hdr.cdth_trk0;
435 toc->LastTrack = hdr.cdth_trk1;
436 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
437 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
438 toc->Length[0] = tsz >> 8;
439 toc->Length[1] = tsz;
441 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
443 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
445 if (i == toc->LastTrack + 1)
446 entry.cdte_track = CDROM_LEADOUT;
447 else
448 entry.cdte_track = i;
449 entry.cdte_format = CDROM_MSF;
450 if (ioctl(fd, CDROMREADTOCENTRY, &entry) == -1)
452 WARN("error read entry (%s)\n", strerror(errno));
453 return FILE_GetNtStatus();
455 toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
456 toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
457 /* marking last track with leadout value as index */
458 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
459 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
460 toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
461 toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
462 toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
464 cdrom_cache[dev].toc_good = 1;
465 return STATUS_SUCCESS;
467 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
469 int i, tsz;
470 struct ioc_toc_header hdr;
471 struct ioc_read_toc_entry entry;
472 struct cd_toc_entry toc_buffer;
474 CDROM_TOC *toc = &cdrom_cache[dev].toc;
475 cdrom_cache[dev].toc_good = 0;
477 if (ioctl(fd, CDIOREADTOCHEADER, &hdr) == -1)
479 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
480 return FILE_GetNtStatus();
482 toc->FirstTrack = hdr.starting_track;
483 toc->LastTrack = hdr.ending_track;
484 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
485 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
486 toc->Length[0] = tsz >> 8;
487 toc->Length[1] = tsz;
489 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
491 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
493 if (i == toc->LastTrack + 1)
495 #define LEADOUT 0xaa
496 entry.starting_track = LEADOUT;
497 } else {
498 entry.starting_track = i;
500 memset(&toc_buffer, 0, sizeof(toc_buffer));
501 entry.address_format = CD_MSF_FORMAT;
502 entry.data_len = sizeof(toc_buffer);
503 entry.data = &toc_buffer;
504 if (ioctl(fd, CDIOREADTOCENTRYS, &entry) == -1)
506 WARN("error read entry (%s)\n", strerror(errno));
507 return FILE_GetNtStatus();
509 toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
510 toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
511 /* marking last track with leadout value as index */
512 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
513 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
514 toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
515 toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
516 toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
518 cdrom_cache[dev].toc_good = 1;
519 return STATUS_SUCCESS;
521 #elif defined(__APPLE__)
522 int i;
523 dk_cd_read_toc_t hdr;
524 CDROM_TOC *toc = &cdrom_cache[dev].toc;
525 cdrom_cache[dev].toc_good = 0;
527 memset( &hdr, 0, sizeof(hdr) );
528 hdr.buffer = toc;
529 hdr.bufferLength = sizeof(*toc);
530 if (ioctl(fd, DKIOCCDREADTOC, &hdr) == -1)
532 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
533 return FILE_GetNtStatus();
535 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
537 /* convert address format */
538 TRACK_DATA *data = &toc->TrackData[i - toc->FirstTrack];
539 DWORD frame = (((DWORD)data->Address[0] << 24) | ((DWORD)data->Address[1] << 16) |
540 ((DWORD)data->Address[2] << 8) | data->Address[3]);
541 MSF_OF_FRAME( data->Address[1], frame );
542 data->Address[0] = 0;
545 cdrom_cache[dev].toc_good = 1;
546 return STATUS_SUCCESS;
547 #else
548 FIXME("not supported on this O/S\n");
549 return STATUS_NOT_SUPPORTED;
550 #endif
553 static void CDROM_ClearCacheEntry(int dev)
555 RtlEnterCriticalSection( &cache_section );
556 cdrom_cache[dev].toc_good = 0;
557 RtlLeaveCriticalSection( &cache_section );
562 /******************************************************************
563 * CDROM_GetInterfaceInfo
565 * Determines the ide interface (the number after the ide), and the
566 * number of the device on that interface for ide cdroms (*iface <= 1).
567 * Determines the scsi information for scsi cdroms (*iface >= 2).
568 * Returns false if the info cannot not be obtained.
570 static int CDROM_GetInterfaceInfo(int fd, UCHAR* iface, UCHAR* port, UCHAR* device, UCHAR* lun)
572 #if defined(linux)
573 struct stat st;
574 if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode)) return 0;
575 *port = 0;
576 *iface = 0;
577 *device = 0;
578 *lun = 0;
579 switch (major(st.st_rdev)) {
580 case IDE0_MAJOR: *iface = 0; break;
581 case IDE1_MAJOR: *iface = 1; break;
582 case IDE2_MAJOR: *iface = 2; break;
583 case IDE3_MAJOR: *iface = 3; break;
584 case IDE4_MAJOR: *iface = 4; break;
585 case IDE5_MAJOR: *iface = 5; break;
586 case IDE6_MAJOR: *iface = 6; break;
587 case IDE7_MAJOR: *iface = 7; break;
588 default: *port = 1; break;
591 if (*port == 0)
592 *device = (minor(st.st_rdev) >> 6);
593 else
595 #ifdef SCSI_IOCTL_GET_IDLUN
596 UINT32 idlun[2];
597 if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, idlun) != -1)
599 *port = (idlun[0] >> 24) & 0xff;
600 *iface = ((idlun[0] >> 16) & 0xff) + 2;
601 *device = idlun[0] & 0xff;
602 *lun = (idlun[0] >> 8) & 0xff;
604 else
605 #endif
607 WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
608 return 0;
611 return 1;
612 #elif defined(__NetBSD__)
613 struct scsi_addr addr;
614 if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
616 switch (addr.type)
618 case TYPE_SCSI:
619 *port = 1;
620 *iface = addr.addr.scsi.scbus;
621 *device = addr.addr.scsi.target;
622 *lun = addr.addr.scsi.lun;
623 return 1;
624 case TYPE_ATAPI:
625 *port = 0;
626 *iface = addr.addr.atapi.atbus;
627 *device = addr.addr.atapi.drive;
628 *lun = 0;
629 return 1;
632 return 0;
633 #else
634 FIXME("not implemented on this O/S\n");
635 return 0;
636 #endif
640 /******************************************************************
641 * CDROM_Open
644 static NTSTATUS CDROM_Open(int fd, int* dev)
646 struct stat st;
647 NTSTATUS ret = STATUS_SUCCESS;
648 int empty = -1;
650 fstat(fd, &st);
652 RtlEnterCriticalSection( &cache_section );
653 for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
655 if (empty == -1 &&
656 cdrom_cache[*dev].device == 0 &&
657 cdrom_cache[*dev].inode == 0)
658 empty = *dev;
659 else if (cdrom_cache[*dev].device == st.st_dev &&
660 cdrom_cache[*dev].inode == st.st_ino)
661 break;
663 if (*dev == MAX_CACHE_ENTRIES)
665 if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
666 else
668 *dev = empty;
669 cdrom_cache[*dev].device = st.st_dev;
670 cdrom_cache[*dev].inode = st.st_ino;
673 RtlLeaveCriticalSection( &cache_section );
675 TRACE("%d, %d\n", *dev, fd);
676 return ret;
679 /******************************************************************
680 * CDROM_GetStatusCode
684 static NTSTATUS CDROM_GetStatusCode(int io)
686 if (io == 0) return STATUS_SUCCESS;
687 return FILE_GetNtStatus();
690 /******************************************************************
691 * CDROM_GetControl
694 static NTSTATUS CDROM_GetControl(int dev, int fd, CDROM_AUDIO_CONTROL* cac)
696 #ifdef __APPLE__
697 uint16_t speed;
698 int io = ioctl( fd, DKIOCCDGETSPEED, &speed );
699 if (io != 0) return CDROM_GetStatusCode( io );
700 /* DKIOCCDGETSPEED returns the speed in kilobytes per second,
701 * so convert to logical blocks (assumed to be ~2 KB).
703 cac->LogicalBlocksPerSecond = speed/2;
704 #else
705 cac->LogicalBlocksPerSecond = 1; /* FIXME */
706 #endif
707 cac->LbaFormat = 0; /* FIXME */
708 return STATUS_NOT_SUPPORTED;
711 /******************************************************************
712 * CDROM_GetDeviceNumber
715 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
717 FIXME( "stub\n" );
718 devnum->DeviceType = FILE_DEVICE_DISK;
719 devnum->DeviceNumber = 1;
720 devnum->PartitionNumber = 1;
721 return STATUS_SUCCESS;
724 /******************************************************************
725 * CDROM_GetDriveGeometry
728 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
730 CDROM_TOC toc;
731 NTSTATUS ret = 0;
732 int fsize = 0;
734 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
736 fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
737 - FRAME_OF_TOC(toc, 1); /* Total size in frames */
739 dg->Cylinders.u.LowPart = fsize / (64 * 32);
740 dg->Cylinders.u.HighPart = 0;
741 dg->MediaType = RemovableMedia;
742 dg->TracksPerCylinder = 64;
743 dg->SectorsPerTrack = 32;
744 dg->BytesPerSector= 2048;
745 return ret;
748 /******************************************************************
749 * CDROM_GetMediaType
752 static NTSTATUS CDROM_GetMediaType(int dev, GET_MEDIA_TYPES* medtype)
754 FIXME(": faking success\n");
755 medtype->DeviceType = FILE_DEVICE_CD_ROM;
756 medtype->MediaInfoCount = 0;
757 return STATUS_SUCCESS;
760 /**************************************************************************
761 * CDROM_Reset [internal]
763 static NTSTATUS CDROM_ResetAudio(int fd)
765 #if defined(linux)
766 return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
767 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
768 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
769 #else
770 FIXME("not supported on this O/S\n");
771 return STATUS_NOT_SUPPORTED;
772 #endif
775 /******************************************************************
776 * CDROM_SetTray
780 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
782 #if defined(linux)
783 return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
784 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
785 return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
786 (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
787 (ioctl(fd, CDIOCPREVENT, NULL)));
788 #elif defined(__APPLE__)
789 if (doEject) return CDROM_GetStatusCode( ioctl( fd, DKIOCEJECT, NULL ) );
790 else return STATUS_NOT_SUPPORTED;
791 #else
792 FIXME("not supported on this O/S\n");
793 return STATUS_NOT_SUPPORTED;
794 #endif
797 /******************************************************************
798 * CDROM_ControlEjection
802 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
804 #if defined(linux)
805 return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
806 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
807 return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
808 #else
809 FIXME("not supported on this O/S\n");
810 return STATUS_NOT_SUPPORTED;
811 #endif
814 /******************************************************************
815 * CDROM_ReadTOC
819 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
821 NTSTATUS ret = STATUS_NOT_SUPPORTED;
823 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
824 return STATUS_INVALID_PARAMETER;
826 RtlEnterCriticalSection( &cache_section );
827 if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
829 *toc = cdrom_cache[dev].toc;
830 ret = STATUS_SUCCESS;
832 RtlLeaveCriticalSection( &cache_section );
833 return ret;
836 /******************************************************************
837 * CDROM_GetDiskData
841 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
843 CDROM_TOC toc;
844 NTSTATUS ret;
845 int i;
847 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
848 data->DiskData = 0;
849 for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
850 if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
851 data->DiskData |= CDROM_DISK_DATA_TRACK;
852 else
853 data->DiskData |= CDROM_DISK_AUDIO_TRACK;
855 return STATUS_SUCCESS;
858 /******************************************************************
859 * CDROM_ReadQChannel
863 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
864 SUB_Q_CHANNEL_DATA* data)
866 NTSTATUS ret = STATUS_NOT_SUPPORTED;
867 #ifdef linux
868 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
869 int io;
870 struct cdrom_subchnl sc;
871 sc.cdsc_format = CDROM_MSF;
873 io = ioctl(fd, CDROMSUBCHNL, &sc);
874 if (io == -1)
876 TRACE("opened or no_media (%s)!\n", strerror(errno));
877 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
878 CDROM_ClearCacheEntry(dev);
879 goto end;
882 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
884 switch (sc.cdsc_audiostatus) {
885 case CDROM_AUDIO_INVALID:
886 CDROM_ClearCacheEntry(dev);
887 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
888 break;
889 case CDROM_AUDIO_NO_STATUS:
890 CDROM_ClearCacheEntry(dev);
891 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
892 break;
893 case CDROM_AUDIO_PLAY:
894 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
895 break;
896 case CDROM_AUDIO_PAUSED:
897 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
898 break;
899 case CDROM_AUDIO_COMPLETED:
900 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
901 break;
902 case CDROM_AUDIO_ERROR:
903 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
904 break;
905 default:
906 TRACE("status=%02X !\n", sc.cdsc_audiostatus);
907 break;
909 switch (fmt->Format)
911 case IOCTL_CDROM_CURRENT_POSITION:
912 RtlEnterCriticalSection( &cache_section );
913 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
914 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
915 data->CurrentPosition.Control = sc.cdsc_ctrl;
916 data->CurrentPosition.ADR = sc.cdsc_adr;
917 data->CurrentPosition.TrackNumber = sc.cdsc_trk;
918 data->CurrentPosition.IndexNumber = sc.cdsc_ind;
920 data->CurrentPosition.AbsoluteAddress[0] = 0;
921 data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute;
922 data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
923 data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
925 data->CurrentPosition.TrackRelativeAddress[0] = 0;
926 data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute;
927 data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
928 data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
930 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
932 else /* not playing */
934 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
935 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
937 RtlLeaveCriticalSection( &cache_section );
938 break;
939 case IOCTL_CDROM_MEDIA_CATALOG:
940 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
942 struct cdrom_mcn mcn;
943 if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
945 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
946 data->MediaCatalog.Mcval = 0; /* FIXME */
947 memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
948 data->MediaCatalog.MediaCatalog[14] = 0;
950 break;
951 case IOCTL_CDROM_TRACK_ISRC:
952 FIXME("TrackIsrc: NIY on linux\n");
953 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
954 data->TrackIsrc.Tcval = 0;
955 io = 0;
956 break;
959 end:
960 ret = CDROM_GetStatusCode(io);
961 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
962 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
963 int io;
964 struct ioc_read_subchannel read_sc;
965 struct cd_sub_channel_info sc;
967 read_sc.address_format = CD_MSF_FORMAT;
968 read_sc.track = 0;
969 read_sc.data_len = sizeof(sc);
970 read_sc.data = &sc;
971 switch (fmt->Format)
973 case IOCTL_CDROM_CURRENT_POSITION:
974 read_sc.data_format = CD_CURRENT_POSITION;
975 break;
976 case IOCTL_CDROM_MEDIA_CATALOG:
977 read_sc.data_format = CD_MEDIA_CATALOG;
978 break;
979 case IOCTL_CDROM_TRACK_ISRC:
980 read_sc.data_format = CD_TRACK_INFO;
981 sc.what.track_info.track_number = data->TrackIsrc.Track;
982 break;
984 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
985 if (io == -1)
987 TRACE("opened or no_media (%s)!\n", strerror(errno));
988 CDROM_ClearCacheEntry(dev);
989 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
990 goto end;
993 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
995 switch (sc.header.audio_status) {
996 case CD_AS_AUDIO_INVALID:
997 CDROM_ClearCacheEntry(dev);
998 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
999 break;
1000 case CD_AS_NO_STATUS:
1001 CDROM_ClearCacheEntry(dev);
1002 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1003 break;
1004 case CD_AS_PLAY_IN_PROGRESS:
1005 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
1006 break;
1007 case CD_AS_PLAY_PAUSED:
1008 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
1009 break;
1010 case CD_AS_PLAY_COMPLETED:
1011 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
1012 break;
1013 case CD_AS_PLAY_ERROR:
1014 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
1015 break;
1016 default:
1017 TRACE("status=%02X !\n", sc.header.audio_status);
1019 switch (fmt->Format)
1021 case IOCTL_CDROM_CURRENT_POSITION:
1022 RtlEnterCriticalSection( &cache_section );
1023 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
1024 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1025 data->CurrentPosition.Control = sc.what.position.control;
1026 data->CurrentPosition.ADR = sc.what.position.addr_type;
1027 data->CurrentPosition.TrackNumber = sc.what.position.track_number;
1028 data->CurrentPosition.IndexNumber = sc.what.position.index_number;
1030 data->CurrentPosition.AbsoluteAddress[0] = 0;
1031 data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
1032 data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
1033 data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
1034 data->CurrentPosition.TrackRelativeAddress[0] = 0;
1035 data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
1036 data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
1037 data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
1038 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
1040 else { /* not playing */
1041 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
1042 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
1044 RtlLeaveCriticalSection( &cache_section );
1045 break;
1046 case IOCTL_CDROM_MEDIA_CATALOG:
1047 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
1048 data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
1049 memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
1050 break;
1051 case IOCTL_CDROM_TRACK_ISRC:
1052 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
1053 data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
1054 memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
1055 break;
1058 end:
1059 ret = CDROM_GetStatusCode(io);
1060 #elif defined(__APPLE__)
1061 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
1062 int io = 0;
1063 union
1065 dk_cd_read_mcn_t mcn;
1066 dk_cd_read_isrc_t isrc;
1067 } ioc;
1068 /* We need IOCDAudioControl for IOCTL_CDROM_CURRENT_POSITION */
1069 if (fmt->Format == IOCTL_CDROM_CURRENT_POSITION)
1071 FIXME("NIY\n");
1072 return STATUS_NOT_SUPPORTED;
1074 /* No IOCDAudioControl support; just set the audio status to none */
1075 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1076 switch(fmt->Format)
1078 case IOCTL_CDROM_MEDIA_CATALOG:
1079 if ((io = ioctl(fd, DKIOCCDREADMCN, &ioc.mcn)) == -1) break;
1080 memcpy(data->MediaCatalog.MediaCatalog, ioc.mcn.mcn, kCDMCNMaxLength);
1081 data->MediaCatalog.Mcval = 1;
1082 break;
1083 case IOCTL_CDROM_TRACK_ISRC:
1084 ioc.isrc.track = fmt->Track;
1085 if ((io = ioctl(fd, DKIOCCDREADISRC, &ioc.isrc)) == -1) break;
1086 memcpy(data->TrackIsrc.TrackIsrc, ioc.isrc.isrc, kCDISRCMaxLength);
1087 data->TrackIsrc.Tcval = 1;
1088 data->TrackIsrc.Track = ioc.isrc.track;
1090 ret = CDROM_GetStatusCode(io);
1091 #endif
1092 return ret;
1095 /******************************************************************
1096 * CDROM_Verify
1097 * Implements: IOCTL_STORAGE_CHECK_VERIFY
1098 * IOCTL_CDROM_CHECK_VERIFY
1099 * IOCTL_DISK_CHECK_VERIFY
1102 static NTSTATUS CDROM_Verify(int dev, int fd)
1104 #if defined(linux)
1105 int ret;
1107 ret = ioctl(fd, CDROM_DRIVE_STATUS, NULL);
1108 if(ret == -1) {
1109 TRACE("ioctl CDROM_DRIVE_STATUS failed(%s)!\n", strerror(errno));
1110 return CDROM_GetStatusCode(ret);
1113 if(ret == CDS_DISC_OK)
1114 return STATUS_SUCCESS;
1115 else
1116 return STATUS_NO_MEDIA_IN_DEVICE;
1117 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1118 int ret;
1119 ret = ioctl(fd, CDIOCSTART, NULL);
1120 if(ret == 0)
1121 return STATUS_SUCCESS;
1122 else
1123 return STATUS_NO_MEDIA_IN_DEVICE;
1124 #else
1125 FIXME("not supported on this O/S\n");
1126 return STATUS_NOT_SUPPORTED;
1127 #endif
1130 /******************************************************************
1131 * CDROM_PlayAudioMSF
1135 static NTSTATUS CDROM_PlayAudioMSF(int fd, const CDROM_PLAY_AUDIO_MSF* audio_msf)
1137 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1138 #ifdef linux
1139 struct cdrom_msf msf;
1140 int io;
1142 msf.cdmsf_min0 = audio_msf->StartingM;
1143 msf.cdmsf_sec0 = audio_msf->StartingS;
1144 msf.cdmsf_frame0 = audio_msf->StartingF;
1145 msf.cdmsf_min1 = audio_msf->EndingM;
1146 msf.cdmsf_sec1 = audio_msf->EndingS;
1147 msf.cdmsf_frame1 = audio_msf->EndingF;
1149 io = ioctl(fd, CDROMSTART);
1150 if (io == -1)
1152 WARN("motor doesn't start !\n");
1153 goto end;
1155 io = ioctl(fd, CDROMPLAYMSF, &msf);
1156 if (io == -1)
1158 WARN("device doesn't play !\n");
1159 goto end;
1161 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1162 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
1163 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
1164 end:
1165 ret = CDROM_GetStatusCode(io);
1166 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
1167 struct ioc_play_msf msf;
1168 int io;
1170 msf.start_m = audio_msf->StartingM;
1171 msf.start_s = audio_msf->StartingS;
1172 msf.start_f = audio_msf->StartingF;
1173 msf.end_m = audio_msf->EndingM;
1174 msf.end_s = audio_msf->EndingS;
1175 msf.end_f = audio_msf->EndingF;
1177 io = ioctl(fd, CDIOCSTART, NULL);
1178 if (io == -1)
1180 WARN("motor doesn't start !\n");
1181 goto end;
1183 io = ioctl(fd, CDIOCPLAYMSF, &msf);
1184 if (io == -1)
1186 WARN("device doesn't play !\n");
1187 goto end;
1189 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1190 msf.start_m, msf.start_s, msf.start_f,
1191 msf.end_m, msf.end_s, msf.end_f);
1192 end:
1193 ret = CDROM_GetStatusCode(io);
1194 #endif
1195 return ret;
1198 /******************************************************************
1199 * CDROM_SeekAudioMSF
1203 static NTSTATUS CDROM_SeekAudioMSF(int dev, int fd, const CDROM_SEEK_AUDIO_MSF* audio_msf)
1205 CDROM_TOC toc;
1206 int i, io, frame;
1207 SUB_Q_CURRENT_POSITION *cp;
1208 #if defined(linux)
1209 struct cdrom_msf0 msf;
1210 struct cdrom_subchnl sc;
1211 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
1212 struct ioc_play_msf msf;
1213 struct ioc_read_subchannel read_sc;
1214 struct cd_sub_channel_info sc;
1215 int final_frame;
1216 #endif
1218 /* Use the information on the TOC to compute the new current
1219 * position, which is shadowed on the cache. [Portable]. */
1220 frame = FRAME_OF_MSF(*audio_msf);
1222 if ((io = CDROM_ReadTOC(dev, fd, &toc)) != 0) return io;
1224 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
1225 if (FRAME_OF_TOC(toc,i)>frame) break;
1226 if (i <= toc.FirstTrack || i > toc.LastTrack+1)
1227 return STATUS_INVALID_PARAMETER;
1228 i--;
1229 RtlEnterCriticalSection( &cache_section );
1230 cp = &cdrom_cache[dev].CurrentPosition;
1231 cp->FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1232 cp->Control = toc.TrackData[i-toc.FirstTrack].Control;
1233 cp->ADR = toc.TrackData[i-toc.FirstTrack].Adr;
1234 cp->TrackNumber = toc.TrackData[i-toc.FirstTrack].TrackNumber;
1235 cp->IndexNumber = 0; /* FIXME: where do they keep these? */
1236 cp->AbsoluteAddress[0] = 0;
1237 cp->AbsoluteAddress[1] = toc.TrackData[i-toc.FirstTrack].Address[1];
1238 cp->AbsoluteAddress[2] = toc.TrackData[i-toc.FirstTrack].Address[2];
1239 cp->AbsoluteAddress[3] = toc.TrackData[i-toc.FirstTrack].Address[3];
1240 frame -= FRAME_OF_TOC(toc,i);
1241 cp->TrackRelativeAddress[0] = 0;
1242 MSF_OF_FRAME(cp->TrackRelativeAddress[1], frame);
1243 RtlLeaveCriticalSection( &cache_section );
1245 /* If playing, then issue a seek command, otherwise do nothing */
1246 #ifdef linux
1247 sc.cdsc_format = CDROM_MSF;
1249 io = ioctl(fd, CDROMSUBCHNL, &sc);
1250 if (io == -1)
1252 TRACE("opened or no_media (%s)!\n", strerror(errno));
1253 CDROM_ClearCacheEntry(dev);
1254 return CDROM_GetStatusCode(io);
1256 if (sc.cdsc_audiostatus==CDROM_AUDIO_PLAY)
1258 msf.minute = audio_msf->M;
1259 msf.second = audio_msf->S;
1260 msf.frame = audio_msf->F;
1261 return CDROM_GetStatusCode(ioctl(fd, CDROMSEEK, &msf));
1263 return STATUS_SUCCESS;
1264 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
1265 read_sc.address_format = CD_MSF_FORMAT;
1266 read_sc.track = 0;
1267 read_sc.data_len = sizeof(sc);
1268 read_sc.data = &sc;
1269 read_sc.data_format = CD_CURRENT_POSITION;
1271 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1272 if (io == -1)
1274 TRACE("opened or no_media (%s)!\n", strerror(errno));
1275 CDROM_ClearCacheEntry(dev);
1276 return CDROM_GetStatusCode(io);
1278 if (sc.header.audio_status==CD_AS_PLAY_IN_PROGRESS)
1281 msf.start_m = audio_msf->M;
1282 msf.start_s = audio_msf->S;
1283 msf.start_f = audio_msf->F;
1284 final_frame = FRAME_OF_TOC(toc,toc.LastTrack+1)-1;
1285 MSF_OF_FRAME(msf.end_m, final_frame);
1287 return CDROM_GetStatusCode(ioctl(fd, CDIOCPLAYMSF, &msf));
1289 return STATUS_SUCCESS;
1290 #else
1291 FIXME("not supported on this O/S\n");
1292 return STATUS_NOT_SUPPORTED;
1293 #endif
1296 /******************************************************************
1297 * CDROM_PauseAudio
1301 static NTSTATUS CDROM_PauseAudio(int fd)
1303 #if defined(linux)
1304 return CDROM_GetStatusCode(ioctl(fd, CDROMPAUSE));
1305 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
1306 return CDROM_GetStatusCode(ioctl(fd, CDIOCPAUSE, NULL));
1307 #else
1308 FIXME(": not supported on this O/S\n");
1309 return STATUS_NOT_SUPPORTED;
1310 #endif
1313 /******************************************************************
1314 * CDROM_ResumeAudio
1318 static NTSTATUS CDROM_ResumeAudio(int fd)
1320 #if defined(linux)
1321 return CDROM_GetStatusCode(ioctl(fd, CDROMRESUME));
1322 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
1323 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESUME, NULL));
1324 #else
1325 FIXME("not supported on this O/S\n");
1326 return STATUS_NOT_SUPPORTED;
1327 #endif
1330 /******************************************************************
1331 * CDROM_StopAudio
1335 static NTSTATUS CDROM_StopAudio(int fd)
1337 #if defined(linux)
1338 return CDROM_GetStatusCode(ioctl(fd, CDROMSTOP));
1339 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
1340 return CDROM_GetStatusCode(ioctl(fd, CDIOCSTOP, NULL));
1341 #else
1342 FIXME("not supported on this O/S\n");
1343 return STATUS_NOT_SUPPORTED;
1344 #endif
1347 /******************************************************************
1348 * CDROM_GetVolume
1352 static NTSTATUS CDROM_GetVolume(int fd, VOLUME_CONTROL* vc)
1354 #if defined(linux)
1355 struct cdrom_volctrl volc;
1356 int io;
1358 io = ioctl(fd, CDROMVOLREAD, &volc);
1359 if (io != -1)
1361 vc->PortVolume[0] = volc.channel0;
1362 vc->PortVolume[1] = volc.channel1;
1363 vc->PortVolume[2] = volc.channel2;
1364 vc->PortVolume[3] = volc.channel3;
1366 return CDROM_GetStatusCode(io);
1367 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
1368 struct ioc_vol volc;
1369 int io;
1371 io = ioctl(fd, CDIOCGETVOL, &volc);
1372 if (io != -1)
1374 vc->PortVolume[0] = volc.vol[0];
1375 vc->PortVolume[1] = volc.vol[1];
1376 vc->PortVolume[2] = volc.vol[2];
1377 vc->PortVolume[3] = volc.vol[3];
1379 return CDROM_GetStatusCode(io);
1380 #else
1381 FIXME("not supported on this O/S\n");
1382 return STATUS_NOT_SUPPORTED;
1383 #endif
1386 /******************************************************************
1387 * CDROM_SetVolume
1391 static NTSTATUS CDROM_SetVolume(int fd, const VOLUME_CONTROL* vc)
1393 #if defined(linux)
1394 struct cdrom_volctrl volc;
1396 volc.channel0 = vc->PortVolume[0];
1397 volc.channel1 = vc->PortVolume[1];
1398 volc.channel2 = vc->PortVolume[2];
1399 volc.channel3 = vc->PortVolume[3];
1401 return CDROM_GetStatusCode(ioctl(fd, CDROMVOLCTRL, &volc));
1402 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
1403 struct ioc_vol volc;
1405 volc.vol[0] = vc->PortVolume[0];
1406 volc.vol[1] = vc->PortVolume[1];
1407 volc.vol[2] = vc->PortVolume[2];
1408 volc.vol[3] = vc->PortVolume[3];
1410 return CDROM_GetStatusCode(ioctl(fd, CDIOCSETVOL, &volc));
1411 #else
1412 FIXME(": not supported on this O/S\n");
1413 return STATUS_NOT_SUPPORTED;
1414 #endif
1417 /******************************************************************
1418 * CDROM_RawRead
1420 * Some features of this IOCTL are rather poorly documented and
1421 * not really intuitive either:
1423 * 1. Although the DiskOffset parameter is meant to be a
1424 * byte offset into the disk, it is in fact the sector
1425 * number multiplied by 2048 regardless of the actual
1426 * sector size.
1428 * 2. The least significant 11 bits of DiskOffset are ignored.
1430 * 3. The TrackMode parameter has no effect on the sector
1431 * size. The entire raw sector (i.e. 2352 bytes of data)
1432 * is always returned. IMO the TrackMode is only used
1433 * to check the correct sector type.
1436 static NTSTATUS CDROM_RawRead(int fd, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
1438 int ret = STATUS_NOT_SUPPORTED;
1439 int io = -1;
1441 TRACE("RAW_READ_INFO: DiskOffset=%i,%i SectorCount=%i TrackMode=%i\n buffer=%p len=%i sz=%p\n",
1442 raw->DiskOffset.u.HighPart, raw->DiskOffset.u.LowPart, raw->SectorCount, raw->TrackMode, buffer, len, sz);
1444 if (len < raw->SectorCount * 2352) return STATUS_BUFFER_TOO_SMALL;
1446 #if defined(linux)
1447 if (raw->DiskOffset.u.HighPart & ~2047) {
1448 WARN("DiskOffset points to a sector >= 2**32\n");
1449 return ret;
1452 switch (raw->TrackMode)
1454 case YellowMode2:
1455 case XAForm2:
1457 DWORD lba = raw->DiskOffset.QuadPart >> 11;
1458 struct cdrom_msf* msf;
1459 PBYTE *bp; /* current buffer pointer */
1460 DWORD i;
1462 if ((lba + raw->SectorCount) >
1463 ((1 << 8*sizeof(msf->cdmsf_min0)) * CD_SECS * CD_FRAMES
1464 - CD_MSF_OFFSET)) {
1465 WARN("DiskOffset not accessible with MSF\n");
1466 return ret;
1469 /* Linux reads only one sector at a time.
1470 * ioctl CDROMREADRAW takes struct cdrom_msf as an argument
1471 * on the contrary to what header comments state.
1473 lba += CD_MSF_OFFSET;
1474 for (i = 0, bp = buffer; i < raw->SectorCount;
1475 i++, lba++, bp += 2352)
1477 msf = (struct cdrom_msf*)bp;
1478 msf->cdmsf_min0 = lba / CD_FRAMES / CD_SECS;
1479 msf->cdmsf_sec0 = lba / CD_FRAMES % CD_SECS;
1480 msf->cdmsf_frame0 = lba % CD_FRAMES;
1481 io = ioctl(fd, CDROMREADRAW, msf);
1482 if (io != 0)
1484 *sz = 2352 * i;
1485 return CDROM_GetStatusCode(io);
1488 break;
1491 case CDDA:
1493 struct cdrom_read_audio cdra;
1495 cdra.addr.lba = raw->DiskOffset.QuadPart >> 11;
1496 TRACE("reading at %u\n", cdra.addr.lba);
1497 cdra.addr_format = CDROM_LBA;
1498 cdra.nframes = raw->SectorCount;
1499 cdra.buf = buffer;
1500 io = ioctl(fd, CDROMREADAUDIO, &cdra);
1501 break;
1504 default:
1505 FIXME("NIY: %d\n", raw->TrackMode);
1506 return STATUS_INVALID_PARAMETER;
1508 #elif defined(__APPLE__)
1509 switch (raw->TrackMode)
1511 case YellowMode2:
1513 /* Mac OS, on the other hand, DOES read only one part of the sector
1514 * at a time. Therefore, we have to read each part of the sector, in
1515 * order, to get the whole raw sector in.
1516 * This means that we have to read each sector one at a time, as on
1517 * Linux.
1519 dk_cd_read_t cdrd;
1520 UInt64 lba = raw->DiskOffset.QuadPart >> 11;
1521 PBYTE bp;
1522 int i;
1524 for (i = 0, bp = buffer; i < raw->SectorCount;
1525 i++, lba++, bp += kCDSectorSizeWhole)
1527 cdrd.offset = lba * kCDSectorSizeWhole;
1528 cdrd.sectorType = kCDSectorTypeMode2;
1530 /* First, the sync area */
1531 cdrd.sectorArea = kCDSectorAreaSync;
1532 cdrd.buffer = bp;
1533 cdrd.bufferLength = 12;
1534 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1535 if (io != 0)
1537 *sz = kCDSectorSizeWhole * i;
1538 return CDROM_GetStatusCode(io);
1541 /* Then the header */
1542 cdrd.offset += 12;
1543 cdrd.sectorArea = kCDSectorAreaHeader;
1544 cdrd.buffer = (PBYTE)cdrd.buffer + 12;
1545 cdrd.bufferLength = 4;
1546 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1547 if (io != 0)
1549 *sz = kCDSectorSizeWhole * i + 12;
1550 return CDROM_GetStatusCode(io);
1553 /* And finally the sector proper */
1554 cdrd.offset += 4;
1555 cdrd.sectorArea = kCDSectorAreaUser;
1556 cdrd.buffer = (PBYTE)cdrd.buffer + 4;
1557 cdrd.bufferLength = kCDSectorSizeMode2;
1558 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1559 if (io != 0)
1561 *sz = kCDSectorSizeWhole * i + 16;
1562 return CDROM_GetStatusCode(io);
1566 break;
1569 case XAForm2:
1571 /* Same here */
1572 dk_cd_read_t cdrd;
1573 UInt64 lba = raw->DiskOffset.QuadPart >> 11;
1574 PBYTE bp;
1575 int i;
1577 for (i = 0, bp = buffer; i < raw->SectorCount;
1578 i++, lba++, bp += kCDSectorSizeWhole)
1580 cdrd.offset = lba * kCDSectorSizeWhole;
1581 cdrd.sectorType = kCDSectorTypeMode2Form2;
1583 /* First, the sync area */
1584 cdrd.sectorArea = kCDSectorAreaSync;
1585 cdrd.buffer = bp;
1586 cdrd.bufferLength = 12;
1587 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1588 if (io != 0)
1590 *sz = kCDSectorSizeWhole * i;
1591 return CDROM_GetStatusCode(io);
1594 /* Then the header */
1595 cdrd.offset += 12;
1596 cdrd.sectorArea = kCDSectorAreaHeader;
1597 cdrd.buffer = (PBYTE)cdrd.buffer + 12;
1598 cdrd.bufferLength = 4;
1599 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1600 if (io != 0)
1602 *sz = kCDSectorSizeWhole * i + 12;
1603 return CDROM_GetStatusCode(io);
1606 /* And the sub-header */
1607 cdrd.offset += 4;
1608 cdrd.sectorArea = kCDSectorAreaSubHeader;
1609 cdrd.buffer = (PBYTE)cdrd.buffer + 4;
1610 cdrd.bufferLength = 8;
1611 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1612 if (io != 0)
1614 *sz = kCDSectorSizeWhole * i + 16;
1615 return CDROM_GetStatusCode(io);
1618 /* And finally the sector proper */
1619 cdrd.offset += 8;
1620 cdrd.sectorArea = kCDSectorAreaUser;
1621 cdrd.buffer = (PBYTE)cdrd.buffer + 8;
1622 cdrd.bufferLength = kCDSectorSizeMode2;
1623 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1624 if (io != 0)
1626 *sz = kCDSectorSizeWhole * i + 24;
1627 return CDROM_GetStatusCode(io);
1631 break;
1634 case CDDA:
1636 /* With CDDA, the whole raw sector is considered user data, so there's
1637 * no need to read one at a time.
1639 dk_cd_read_t cdrd;
1641 cdrd.offset = (raw->DiskOffset.QuadPart >> 11) * kCDSectorSizeCDDA;
1642 cdrd.sectorArea = kCDSectorAreaUser;
1643 cdrd.sectorType = kCDSectorTypeCDDA;
1644 cdrd.buffer = buffer;
1645 cdrd.bufferLength = len < raw->SectorCount*kCDSectorSizeCDDA ? len :
1646 raw->SectorCount*kCDSectorSizeCDDA;
1648 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1649 if (io != 0) return CDROM_GetStatusCode(io);
1650 break;
1653 default:
1654 FIXME("NIY: %d\n", raw->TrackMode);
1655 return STATUS_INVALID_PARAMETER;
1657 #else
1658 switch (raw->TrackMode)
1660 case YellowMode2:
1661 FIXME("YellowMode2: NIY\n");
1662 return ret;
1663 case XAForm2:
1664 FIXME("XAForm2: NIY\n");
1665 return ret;
1666 case CDDA:
1667 FIXME("CDDA: NIY\n");
1668 return ret;
1669 default:
1670 FIXME("NIY: %d\n", raw->TrackMode);
1671 return STATUS_INVALID_PARAMETER;
1673 #endif
1675 *sz = 2352 * raw->SectorCount;
1676 ret = CDROM_GetStatusCode(io);
1677 return ret;
1680 /******************************************************************
1681 * CDROM_ScsiPassThroughDirect
1682 * Implements IOCTL_SCSI_PASS_THROUGH_DIRECT
1685 static NTSTATUS CDROM_ScsiPassThroughDirect(int fd, PSCSI_PASS_THROUGH_DIRECT pPacket)
1687 int ret = STATUS_NOT_SUPPORTED;
1688 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1689 sg_io_hdr_t cmd;
1690 int io;
1691 #elif defined HAVE_SCSIREQ_T_CMD
1692 scsireq_t cmd;
1693 int io;
1694 #elif defined __APPLE__
1695 dk_scsi_command_t cmd;
1696 int io;
1697 #endif
1699 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1700 return STATUS_BUFFER_TOO_SMALL;
1702 if (pPacket->CdbLength > 16)
1703 return STATUS_INVALID_PARAMETER;
1705 #ifdef SENSEBUFLEN
1706 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1707 return STATUS_INVALID_PARAMETER;
1708 #elif defined HAVE_REQUEST_SENSE
1709 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1710 return STATUS_INVALID_PARAMETER;
1711 #endif
1713 if (pPacket->DataTransferLength > 0 && !pPacket->DataBuffer)
1714 return STATUS_INVALID_PARAMETER;
1716 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1717 RtlZeroMemory(&cmd, sizeof(cmd));
1719 cmd.interface_id = 'S';
1720 cmd.cmd_len = pPacket->CdbLength;
1721 cmd.mx_sb_len = pPacket->SenseInfoLength;
1722 cmd.dxfer_len = pPacket->DataTransferLength;
1723 cmd.dxferp = pPacket->DataBuffer;
1724 cmd.cmdp = pPacket->Cdb;
1725 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1726 cmd.timeout = pPacket->TimeOutValue*1000;
1728 switch (pPacket->DataIn)
1730 case SCSI_IOCTL_DATA_IN:
1731 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1732 break;
1733 case SCSI_IOCTL_DATA_OUT:
1734 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1735 break;
1736 case SCSI_IOCTL_DATA_UNSPECIFIED:
1737 cmd.dxfer_direction = SG_DXFER_NONE;
1738 break;
1739 default:
1740 return STATUS_INVALID_PARAMETER;
1743 io = ioctl(fd, SG_IO, &cmd);
1745 pPacket->ScsiStatus = cmd.status;
1746 pPacket->DataTransferLength = cmd.resid;
1747 pPacket->SenseInfoLength = cmd.sb_len_wr;
1749 ret = CDROM_GetStatusCode(io);
1751 #elif defined HAVE_SCSIREQ_T_CMD
1753 memset(&cmd, 0, sizeof(cmd));
1754 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1756 cmd.cmdlen = pPacket->CdbLength;
1757 cmd.databuf = pPacket->DataBuffer;
1758 cmd.datalen = pPacket->DataTransferLength;
1759 cmd.senselen = pPacket->SenseInfoLength;
1760 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1762 switch (pPacket->DataIn)
1764 case SCSI_IOCTL_DATA_OUT:
1765 cmd.flags |= SCCMD_WRITE;
1766 break;
1767 case SCSI_IOCTL_DATA_IN:
1768 cmd.flags |= SCCMD_READ;
1769 break;
1770 case SCSI_IOCTL_DATA_UNSPECIFIED:
1771 cmd.flags = 0;
1772 break;
1773 default:
1774 return STATUS_INVALID_PARAMETER;
1777 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1779 switch (cmd.retsts)
1781 case SCCMD_OK: break;
1782 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1783 break;
1784 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1785 break;
1786 case SCCMD_SENSE: break;
1787 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1788 break;
1791 if (pPacket->SenseInfoLength != 0)
1793 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1794 cmd.sense, pPacket->SenseInfoLength);
1797 pPacket->ScsiStatus = cmd.status;
1799 ret = CDROM_GetStatusCode(io);
1801 #elif defined(__APPLE__)
1803 memset(&cmd, 0, sizeof(cmd));
1804 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1806 cmd.cdbSize = pPacket->CdbLength;
1807 cmd.buffer = pPacket->DataBuffer;
1808 cmd.bufferSize = pPacket->DataTransferLength;
1809 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1810 cmd.senseLen = pPacket->SenseInfoLength;
1811 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1813 switch (pPacket->DataIn)
1815 case SCSI_IOCTL_DATA_OUT:
1816 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
1817 break;
1818 case SCSI_IOCTL_DATA_IN:
1819 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
1820 break;
1821 case SCSI_IOCTL_DATA_UNSPECIFIED:
1822 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
1823 break;
1824 default:
1825 return STATUS_INVALID_PARAMETER;
1828 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
1830 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
1832 /* Command failed */
1833 switch (cmd.status)
1835 case kSCSITaskStatus_TaskTimeoutOccurred: return STATUS_TIMEOUT;
1836 break;
1837 case kSCSITaskStatus_ProtocolTimeoutOccurred: return STATUS_IO_TIMEOUT;
1838 break;
1839 case kSCSITaskStatus_DeviceNotResponding: return STATUS_DEVICE_BUSY;
1840 break;
1841 case kSCSITaskStatus_DeviceNotPresent:
1842 return STATUS_NO_SUCH_DEVICE;
1843 break;
1844 case kSCSITaskStatus_DeliveryFailure:
1845 return STATUS_DEVICE_PROTOCOL_ERROR;
1846 break;
1847 case kSCSITaskStatus_No_Status:
1848 default:
1849 return STATUS_UNSUCCESSFUL;
1850 break;
1854 if (cmd.status != kSCSITaskStatus_No_Status)
1855 pPacket->ScsiStatus = cmd.status;
1857 ret = CDROM_GetStatusCode(io);
1858 #endif
1859 return ret;
1862 /******************************************************************
1863 * CDROM_ScsiPassThrough
1864 * Implements IOCTL_SCSI_PASS_THROUGH
1867 static NTSTATUS CDROM_ScsiPassThrough(int fd, PSCSI_PASS_THROUGH pPacket)
1869 int ret = STATUS_NOT_SUPPORTED;
1870 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1871 sg_io_hdr_t cmd;
1872 int io;
1873 #elif defined HAVE_SCSIREQ_T_CMD
1874 scsireq_t cmd;
1875 int io;
1876 #elif defined __APPLE__
1877 dk_scsi_command_t cmd;
1878 int io;
1879 #endif
1881 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1882 return STATUS_BUFFER_TOO_SMALL;
1884 if (pPacket->CdbLength > 16)
1885 return STATUS_INVALID_PARAMETER;
1887 #ifdef SENSEBUFLEN
1888 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1889 return STATUS_INVALID_PARAMETER;
1890 #elif defined HAVE_REQUEST_SENSE
1891 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1892 return STATUS_INVALID_PARAMETER;
1893 #endif
1895 if (pPacket->DataTransferLength > 0 && pPacket->DataBufferOffset < sizeof(SCSI_PASS_THROUGH))
1896 return STATUS_INVALID_PARAMETER;
1898 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1899 RtlZeroMemory(&cmd, sizeof(cmd));
1901 cmd.interface_id = 'S';
1902 cmd.dxfer_len = pPacket->DataTransferLength;
1903 cmd.dxferp = (char*)pPacket + pPacket->DataBufferOffset;
1904 cmd.cmd_len = pPacket->CdbLength;
1905 cmd.cmdp = pPacket->Cdb;
1906 cmd.mx_sb_len = pPacket->SenseInfoLength;
1907 cmd.timeout = pPacket->TimeOutValue*1000;
1909 if(cmd.mx_sb_len > 0)
1910 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1912 switch (pPacket->DataIn)
1914 case SCSI_IOCTL_DATA_IN:
1915 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1916 break;
1917 case SCSI_IOCTL_DATA_OUT:
1918 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1919 break;
1920 case SCSI_IOCTL_DATA_UNSPECIFIED:
1921 cmd.dxfer_direction = SG_DXFER_NONE;
1922 break;
1923 default:
1924 return STATUS_INVALID_PARAMETER;
1927 io = ioctl(fd, SG_IO, &cmd);
1929 pPacket->ScsiStatus = cmd.status;
1930 pPacket->DataTransferLength = cmd.resid;
1931 pPacket->SenseInfoLength = cmd.sb_len_wr;
1933 ret = CDROM_GetStatusCode(io);
1935 #elif defined HAVE_SCSIREQ_T_CMD
1937 memset(&cmd, 0, sizeof(cmd));
1938 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1940 if ( pPacket->DataBufferOffset > 0x1000 )
1942 cmd.databuf = (void*)pPacket->DataBufferOffset;
1944 else
1946 cmd.databuf = (char*)pPacket + pPacket->DataBufferOffset;
1949 cmd.cmdlen = pPacket->CdbLength;
1950 cmd.datalen = pPacket->DataTransferLength;
1951 cmd.senselen = pPacket->SenseInfoLength;
1952 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1954 switch (pPacket->DataIn)
1956 case SCSI_IOCTL_DATA_OUT:
1957 cmd.flags |= SCCMD_WRITE;
1958 break;
1959 case SCSI_IOCTL_DATA_IN:
1960 cmd.flags |= SCCMD_READ;
1961 break;
1962 case SCSI_IOCTL_DATA_UNSPECIFIED:
1963 cmd.flags = 0;
1964 break;
1965 default:
1966 return STATUS_INVALID_PARAMETER;
1969 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1971 switch (cmd.retsts)
1973 case SCCMD_OK: break;
1974 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1975 break;
1976 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1977 break;
1978 case SCCMD_SENSE: break;
1979 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1980 break;
1983 if (pPacket->SenseInfoLength != 0)
1985 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1986 cmd.sense, pPacket->SenseInfoLength);
1989 pPacket->ScsiStatus = cmd.status;
1991 ret = CDROM_GetStatusCode(io);
1993 #elif defined(__APPLE__)
1995 memset(&cmd, 0, sizeof(cmd));
1996 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1998 cmd.cdbSize = pPacket->CdbLength;
1999 cmd.buffer = (char*)pPacket + pPacket->DataBufferOffset;
2000 cmd.bufferSize = pPacket->DataTransferLength;
2001 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
2002 cmd.senseLen = pPacket->SenseInfoLength;
2003 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
2005 switch (pPacket->DataIn)
2007 case SCSI_IOCTL_DATA_OUT:
2008 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
2009 break;
2010 case SCSI_IOCTL_DATA_IN:
2011 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
2012 break;
2013 case SCSI_IOCTL_DATA_UNSPECIFIED:
2014 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
2015 break;
2016 default:
2017 return STATUS_INVALID_PARAMETER;
2020 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
2022 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
2024 /* Command failed */
2025 switch (cmd.status)
2027 case kSCSITaskStatus_TaskTimeoutOccurred:
2028 return STATUS_TIMEOUT;
2029 break;
2030 case kSCSITaskStatus_ProtocolTimeoutOccurred:
2031 return STATUS_IO_TIMEOUT;
2032 break;
2033 case kSCSITaskStatus_DeviceNotResponding:
2034 return STATUS_DEVICE_BUSY;
2035 break;
2036 case kSCSITaskStatus_DeviceNotPresent:
2037 return STATUS_NO_SUCH_DEVICE;
2038 break;
2039 case kSCSITaskStatus_DeliveryFailure:
2040 return STATUS_DEVICE_PROTOCOL_ERROR;
2041 break;
2042 case kSCSITaskStatus_No_Status:
2043 default:
2044 return STATUS_UNSUCCESSFUL;
2045 break;
2049 if (cmd.status != kSCSITaskStatus_No_Status)
2050 pPacket->ScsiStatus = cmd.status;
2052 ret = CDROM_GetStatusCode(io);
2053 #endif
2054 return ret;
2057 /******************************************************************
2058 * CDROM_ScsiGetCaps
2062 static NTSTATUS CDROM_ScsiGetCaps(int fd, PIO_SCSI_CAPABILITIES caps)
2064 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
2066 #ifdef SG_SCATTER_SZ
2067 caps->Length = sizeof(*caps);
2068 caps->MaximumTransferLength = SG_SCATTER_SZ; /* FIXME */
2069 caps->MaximumPhysicalPages = SG_SCATTER_SZ / getpagesize();
2070 caps->SupportedAsynchronousEvents = TRUE;
2071 caps->AlignmentMask = getpagesize();
2072 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
2073 caps->AdapterScansDown = FALSE; /* FIXME ? */
2074 caps->AdapterUsesPio = FALSE; /* FIXME ? */
2075 ret = STATUS_SUCCESS;
2076 #elif defined __APPLE__
2077 uint64_t bytesr, bytesw, align;
2078 int io = ioctl(fd, DKIOCGETMAXBYTECOUNTREAD, &bytesr);
2079 if (io != 0) return CDROM_GetStatusCode(io);
2080 io = ioctl(fd, DKIOCGETMAXBYTECOUNTWRITE, &bytesw);
2081 if (io != 0) return CDROM_GetStatusCode(io);
2082 io = ioctl(fd, DKIOCGETMINSEGMENTALIGNMENTBYTECOUNT, &align);
2083 if (io != 0) return CDROM_GetStatusCode(io);
2084 caps->Length = sizeof(*caps);
2085 caps->MaximumTransferLength = bytesr < bytesw ? bytesr : bytesw;
2086 caps->MaximumPhysicalPages = caps->MaximumTransferLength / getpagesize();
2087 caps->SupportedAsynchronousEvents = TRUE;
2088 caps->AlignmentMask = align-1;
2089 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
2090 caps->AdapterScansDown = FALSE; /* FIXME ? */
2091 caps->AdapterUsesPio = FALSE; /* FIXME ? */
2092 ret = STATUS_SUCCESS;
2093 #else
2094 FIXME("Unimplemented\n");
2095 #endif
2096 return ret;
2099 /******************************************************************
2100 * CDROM_GetAddress
2102 * implements IOCTL_SCSI_GET_ADDRESS
2104 static NTSTATUS CDROM_GetAddress(int fd, SCSI_ADDRESS* address)
2106 UCHAR portnum, busid, targetid, lun;
2108 address->Length = sizeof(SCSI_ADDRESS);
2109 if ( ! CDROM_GetInterfaceInfo(fd, &portnum, &busid, &targetid, &lun))
2110 return STATUS_NOT_SUPPORTED;
2112 address->PortNumber = portnum; /* primary=0 secondary=1 for ide */
2113 address->PathId = busid; /* always 0 for ide */
2114 address->TargetId = targetid; /* master=0 slave=1 for ide */
2115 address->Lun = lun;
2116 return STATUS_SUCCESS;
2119 /******************************************************************
2120 * DVD_StartSession
2124 static NTSTATUS DVD_StartSession(int fd, const DVD_SESSION_ID *sid_in, PDVD_SESSION_ID sid_out)
2126 #if defined(linux)
2127 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2128 dvd_authinfo auth_info;
2130 memset( &auth_info, 0, sizeof( auth_info ) );
2131 auth_info.type = DVD_LU_SEND_AGID;
2132 if (sid_in) auth_info.lsa.agid = *(const int*)sid_in; /* ?*/
2134 TRACE("fd 0x%08x\n",fd);
2135 ret =CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2136 *sid_out = auth_info.lsa.agid;
2137 return ret;
2138 #elif defined(__APPLE__)
2139 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2140 dk_dvd_report_key_t dvdrk;
2141 DVDAuthenticationGrantIDInfo agid_info;
2143 dvdrk.format = kDVDKeyFormatAGID_CSS;
2144 dvdrk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2145 if(sid_in) dvdrk.grantID = *(uint8_t*)sid_in; /* ? */
2146 dvdrk.bufferLength = sizeof(DVDAuthenticationGrantIDInfo);
2147 dvdrk.buffer = &agid_info;
2149 ret = CDROM_GetStatusCode(ioctl(fd, DKIOCDVDREPORTKEY, &dvdrk));
2150 *sid_out = agid_info.grantID;
2151 return ret;
2152 #else
2153 FIXME("not supported on this O/S\n");
2154 return STATUS_NOT_SUPPORTED;
2155 #endif
2158 /******************************************************************
2159 * DVD_EndSession
2163 static NTSTATUS DVD_EndSession(int fd, const DVD_SESSION_ID *sid)
2165 #if defined(linux)
2166 dvd_authinfo auth_info;
2168 memset( &auth_info, 0, sizeof( auth_info ) );
2169 auth_info.type = DVD_INVALIDATE_AGID;
2170 auth_info.lsa.agid = *(const int*)sid;
2172 TRACE("\n");
2173 return CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2174 #elif defined(__APPLE__)
2175 dk_dvd_send_key_t dvdsk;
2177 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2178 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2179 dvdsk.grantID = (uint8_t)*sid;
2181 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2182 #else
2183 FIXME("not supported on this O/S\n");
2184 return STATUS_NOT_SUPPORTED;
2185 #endif
2188 /******************************************************************
2189 * DVD_SendKey
2193 static NTSTATUS DVD_SendKey(int fd, const DVD_COPY_PROTECT_KEY *key)
2195 #if defined(linux)
2196 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2197 dvd_authinfo auth_info;
2199 memset( &auth_info, 0, sizeof( auth_info ) );
2200 switch (key->KeyType)
2202 case DvdChallengeKey:
2203 auth_info.type = DVD_HOST_SEND_CHALLENGE;
2204 auth_info.hsc.agid = (int)key->SessionId;
2205 TRACE("DvdChallengeKey ioc 0x%x\n", DVD_AUTH );
2206 memcpy( auth_info.hsc.chal, key->KeyData, DVD_CHALLENGE_SIZE );
2207 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2208 break;
2209 case DvdBusKey2:
2210 auth_info.type = DVD_HOST_SEND_KEY2;
2211 auth_info.hsk.agid = (int)key->SessionId;
2213 memcpy( auth_info.hsk.key, key->KeyData, DVD_KEY_SIZE );
2215 TRACE("DvdBusKey2\n");
2216 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2217 break;
2219 default:
2220 FIXME("Unknown Keytype 0x%x\n",key->KeyType);
2222 return ret;
2223 #elif defined(__APPLE__)
2224 dk_dvd_send_key_t dvdsk;
2225 union
2227 DVDChallengeKeyInfo chal;
2228 DVDKey2Info key2;
2229 } key_desc;
2231 switch(key->KeyType)
2233 case DvdChallengeKey:
2234 dvdsk.format = kDVDKeyFormatChallengeKey;
2235 dvdsk.bufferLength = sizeof(key_desc.chal);
2236 dvdsk.buffer = &key_desc.chal;
2237 OSWriteBigInt16(key_desc.chal.dataLength, 0, key->KeyLength);
2238 memcpy(key_desc.chal.challengeKeyValue, key->KeyData, key->KeyLength);
2239 break;
2240 case DvdBusKey2:
2241 dvdsk.format = kDVDKeyFormatKey2;
2242 dvdsk.bufferLength = sizeof(key_desc.key2);
2243 dvdsk.buffer = &key_desc.key2;
2244 OSWriteBigInt16(key_desc.key2.dataLength, 0, key->KeyLength);
2245 memcpy(key_desc.key2.key2Value, key->KeyData, key->KeyLength);
2246 break;
2247 case DvdInvalidateAGID:
2248 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2249 break;
2250 case DvdBusKey1:
2251 case DvdTitleKey:
2252 case DvdAsf:
2253 case DvdGetRpcKey:
2254 case DvdDiskKey:
2255 ERR("attempted to write read-only key type 0x%x\n", key->KeyType);
2256 return STATUS_NOT_SUPPORTED;
2257 case DvdSetRpcKey:
2258 FIXME("DvdSetRpcKey NIY\n");
2259 return STATUS_NOT_SUPPORTED;
2260 default:
2261 FIXME("got unknown key type 0x%x\n", key->KeyType);
2262 return STATUS_NOT_SUPPORTED;
2264 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2265 dvdsk.grantID = (uint8_t)key->SessionId;
2267 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2268 #else
2269 FIXME("not supported on this O/S\n");
2270 return STATUS_NOT_SUPPORTED;
2271 #endif
2274 /******************************************************************
2275 * DVD_ReadKey
2279 static NTSTATUS DVD_ReadKey(int fd, PDVD_COPY_PROTECT_KEY key)
2281 #if defined(linux)
2282 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2283 dvd_struct dvd;
2284 dvd_authinfo auth_info;
2286 memset( &dvd, 0, sizeof( dvd_struct ) );
2287 memset( &auth_info, 0, sizeof( auth_info ) );
2288 switch (key->KeyType)
2290 case DvdDiskKey:
2292 dvd.type = DVD_STRUCT_DISCKEY;
2293 dvd.disckey.agid = (int)key->SessionId;
2294 memset( dvd.disckey.value, 0, DVD_DISCKEY_SIZE );
2296 TRACE("DvdDiskKey\n");
2297 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2298 if (ret == STATUS_SUCCESS)
2299 memcpy(key->KeyData,dvd.disckey.value,DVD_DISCKEY_SIZE);
2300 break;
2301 case DvdTitleKey:
2302 auth_info.type = DVD_LU_SEND_TITLE_KEY;
2303 auth_info.lstk.agid = (int)key->SessionId;
2304 auth_info.lstk.lba = (int)(key->Parameters.TitleOffset.QuadPart>>11);
2305 TRACE("DvdTitleKey session %d Quadpart 0x%08lx offset 0x%08x\n",
2306 (int)key->SessionId, (long)key->Parameters.TitleOffset.QuadPart,
2307 auth_info.lstk.lba);
2308 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2309 if (ret == STATUS_SUCCESS)
2310 memcpy(key->KeyData, auth_info.lstk.title_key, DVD_KEY_SIZE );
2311 break;
2312 case DvdChallengeKey:
2314 auth_info.type = DVD_LU_SEND_CHALLENGE;
2315 auth_info.lsc.agid = (int)key->SessionId;
2317 TRACE("DvdChallengeKey\n");
2318 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2319 if (ret == STATUS_SUCCESS)
2320 memcpy( key->KeyData, auth_info.lsc.chal, DVD_CHALLENGE_SIZE );
2321 break;
2322 case DvdAsf:
2323 auth_info.type = DVD_LU_SEND_ASF;
2324 TRACE("DvdAsf\n");
2325 auth_info.lsasf.asf=((PDVD_ASF)key->KeyData)->SuccessFlag;
2326 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2327 ((PDVD_ASF)key->KeyData)->SuccessFlag = auth_info.lsasf.asf;
2328 break;
2329 case DvdBusKey1:
2330 auth_info.type = DVD_LU_SEND_KEY1;
2331 auth_info.lsk.agid = (int)key->SessionId;
2333 TRACE("DvdBusKey1\n");
2334 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2336 if (ret == STATUS_SUCCESS)
2337 memcpy( key->KeyData, auth_info.lsk.key, DVD_KEY_SIZE );
2338 break;
2339 case DvdGetRpcKey:
2340 auth_info.type = DVD_LU_SEND_RPC_STATE;
2342 TRACE("DvdGetRpcKey\n");
2343 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2345 if (ret == STATUS_SUCCESS)
2347 ((PDVD_RPC_KEY)key->KeyData)->TypeCode = auth_info.lrpcs.type;
2348 ((PDVD_RPC_KEY)key->KeyData)->RegionMask = auth_info.lrpcs.region_mask;
2349 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme = auth_info.lrpcs.rpc_scheme;
2350 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable = auth_info.lrpcs.ucca;
2351 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable = auth_info.lrpcs.vra;
2353 break;
2354 default:
2355 FIXME("Unknown keytype 0x%x\n",key->KeyType);
2357 return ret;
2358 #elif defined(__APPLE__)
2359 union
2361 dk_dvd_report_key_t key;
2362 dk_dvd_read_structure_t disk_key;
2363 } ioc;
2364 union
2366 DVDDiscKeyInfo disk_key;
2367 DVDChallengeKeyInfo chal;
2368 DVDKey1Info key1;
2369 DVDTitleKeyInfo title;
2370 DVDAuthenticationSuccessFlagInfo asf;
2371 DVDRegionPlaybackControlInfo rpc;
2372 } desc;
2373 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2375 switch(key->KeyType)
2377 case DvdChallengeKey:
2378 ioc.key.format = kDVDKeyFormatChallengeKey;
2379 ioc.key.grantID = (uint8_t)key->SessionId;
2380 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2381 ioc.key.bufferLength = sizeof(desc.chal);
2382 ioc.key.buffer = &desc.chal;
2383 OSWriteBigInt16(desc.chal.dataLength, 0, key->KeyLength);
2384 break;
2385 case DvdBusKey1:
2386 ioc.key.format = kDVDKeyFormatKey1;
2387 ioc.key.grantID = (uint8_t)key->SessionId;
2388 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2389 ioc.key.bufferLength = sizeof(desc.key1);
2390 ioc.key.buffer = &desc.key1;
2391 OSWriteBigInt16(desc.key1.dataLength, 0, key->KeyLength);
2392 break;
2393 case DvdTitleKey:
2394 ioc.key.format = kDVDKeyFormatTitleKey;
2395 ioc.key.grantID = (uint8_t)key->SessionId;
2396 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2397 ioc.key.bufferLength = sizeof(desc.title);
2398 ioc.key.buffer = &desc.title;
2399 ioc.key.address = (uint32_t)(key->Parameters.TitleOffset.QuadPart>>11);
2400 OSWriteBigInt16(desc.title.dataLength, 0, key->KeyLength);
2401 break;
2402 case DvdAsf:
2403 ioc.key.format = kDVDKeyFormatASF;
2404 ioc.key.grantID = (uint8_t)key->SessionId;
2405 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2406 ioc.key.bufferLength = sizeof(desc.asf);
2407 ioc.key.buffer = &desc.asf;
2408 OSWriteBigInt16(desc.asf.dataLength, 0, key->KeyLength);
2409 break;
2410 case DvdGetRpcKey:
2411 ioc.key.format = kDVDKeyFormatRegionState;
2412 ioc.key.grantID = (uint8_t)key->SessionId;
2413 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2414 ioc.key.bufferLength = sizeof(desc.rpc);
2415 ioc.key.buffer = &desc.rpc;
2416 OSWriteBigInt16(desc.rpc.dataLength, 0, key->KeyLength);
2417 break;
2418 case DvdDiskKey:
2419 ioc.disk_key.format = kDVDStructureFormatDiscKeyInfo;
2420 ioc.disk_key.grantID = (uint8_t)key->SessionId;
2421 ioc.disk_key.bufferLength = sizeof(desc.disk_key);
2422 ioc.disk_key.buffer = &desc.disk_key;
2423 break;
2424 case DvdInvalidateAGID:
2425 ioc.key.format = kDVDKeyFormatAGID_Invalidate;
2426 ioc.key.grantID = (uint8_t)key->SessionId;
2427 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2428 break;
2429 case DvdBusKey2:
2430 case DvdSetRpcKey:
2431 ERR("attempted to read write-only key type 0x%x\n", key->KeyType);
2432 return STATUS_NOT_SUPPORTED;
2433 default:
2434 FIXME("got unknown key type 0x%x\n", key->KeyType);
2435 return STATUS_NOT_SUPPORTED;
2438 ret = CDROM_GetStatusCode(ioctl(fd, (key->KeyType == DvdDiskKey ? DKIOCDVDREADSTRUCTURE : DKIOCDVDREPORTKEY), &ioc));
2440 if (ret == STATUS_SUCCESS)
2442 switch(key->KeyType)
2444 case DvdChallengeKey:
2445 key->KeyLength = OSReadBigInt16(desc.chal.dataLength, 0);
2446 memcpy(key->KeyData, desc.chal.challengeKeyValue, key->KeyLength);
2447 break;
2448 case DvdBusKey1:
2449 key->KeyLength = OSReadBigInt16(desc.key1.dataLength, 0);
2450 memcpy(key->KeyData, desc.key1.key1Value, key->KeyLength);
2451 break;
2452 case DvdTitleKey:
2453 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2454 memcpy(key->KeyData, desc.title.titleKeyValue, key->KeyLength);
2455 key->KeyFlags = 0;
2456 if (desc.title.CPM)
2458 /*key->KeyFlags |= DVD_COPYRIGHTED;*/
2459 if (desc.title.CP_SEC) key->KeyFlags |= DVD_SECTOR_PROTECTED;
2460 /*else key->KeyFlags |= DVD_SECTOR_NOT_PROTECTED;*/
2461 #if 0
2462 switch (desc.title.CGMS)
2464 case 0:
2465 key->KeyFlags |= DVD_CGMS_COPY_PERMITTED;
2466 break;
2467 case 2:
2468 key->KeyFlags |= DVD_CGMS_COPY_ONCE;
2469 break;
2470 case 3:
2471 key->KeyFlags |= DVD_CGMS_NO_COPY;
2472 break;
2474 #endif
2475 } /*else key->KeyFlags |= DVD_NOT_COPYRIGHTED;*/
2476 break;
2477 case DvdAsf:
2478 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2479 ((PDVD_ASF)key->KeyData)->SuccessFlag = desc.asf.successFlag;
2480 break;
2481 case DvdGetRpcKey:
2482 key->KeyLength = OSReadBigInt16(desc.rpc.dataLength, 0);
2483 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable =
2484 desc.rpc.numberUserResets;
2485 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable =
2486 desc.rpc.numberVendorResets;
2487 ((PDVD_RPC_KEY)key->KeyData)->TypeCode =
2488 desc.rpc.typeCode;
2489 ((PDVD_RPC_KEY)key->KeyData)->RegionMask =
2490 desc.rpc.driveRegion;
2491 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme =
2492 desc.rpc.rpcScheme;
2493 case DvdDiskKey:
2494 key->KeyLength = OSReadBigInt16(desc.disk_key.dataLength, 0);
2495 memcpy(key->KeyData, desc.disk_key.discKeyStructures, key->KeyLength);
2496 break;
2497 case DvdBusKey2:
2498 case DvdSetRpcKey:
2499 case DvdInvalidateAGID:
2500 default:
2501 /* Silence warning */
2505 return ret;
2506 #else
2507 FIXME("not supported on this O/S\n");
2508 return STATUS_NOT_SUPPORTED;
2509 #endif
2512 /******************************************************************
2513 * DVD_GetRegion
2515 * This IOCTL combines information from both IOCTL_DVD_READ_KEY
2516 * with key type DvdGetRpcKey and IOCTL_DVD_READ_STRUCTURE with
2517 * structure type DvdCopyrightInformation into one structure.
2519 static NTSTATUS DVD_GetRegion(int fd, PDVD_REGION region)
2521 #if defined(linux)
2522 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2523 dvd_struct dvd;
2524 dvd_authinfo auth_info;
2526 dvd.type = DVD_STRUCT_COPYRIGHT;
2527 dvd.copyright.layer_num = 0;
2528 auth_info.type = DVD_LU_SEND_RPC_STATE;
2530 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2532 if (ret == STATUS_SUCCESS)
2534 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2536 if (ret == STATUS_SUCCESS)
2538 region->CopySystem = dvd.copyright.cpst;
2539 region->RegionData = dvd.copyright.rmi;
2540 region->SystemRegion = auth_info.lrpcs.region_mask;
2541 region->ResetCount = auth_info.lrpcs.ucca;
2544 return ret;
2545 #elif defined(__APPLE__)
2546 dk_dvd_report_key_t key;
2547 dk_dvd_read_structure_t dvd;
2548 DVDRegionPlaybackControlInfo rpc;
2549 DVDCopyrightInfo copy;
2550 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2552 key.format = kDVDKeyFormatRegionState;
2553 key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2554 key.bufferLength = sizeof(rpc);
2555 key.buffer = &rpc;
2556 dvd.format = kDVDStructureFormatCopyrightInfo;
2557 dvd.bufferLength = sizeof(copy);
2558 dvd.buffer = &copy;
2560 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREPORTKEY, &key ));
2562 if (ret == STATUS_SUCCESS)
2564 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREADSTRUCTURE, &dvd ));
2566 if (ret == STATUS_SUCCESS)
2568 region->CopySystem = copy.copyrightProtectionSystemType;
2569 region->RegionData = copy.regionMask;
2570 region->SystemRegion = rpc.driveRegion;
2571 region->ResetCount = rpc.numberUserResets;
2574 return ret;
2575 #else
2576 FIXME("not supported on this O/S\n");
2577 return STATUS_NOT_SUPPORTED;
2578 #endif
2581 /******************************************************************
2582 * DVD_ReadStructure
2586 static NTSTATUS DVD_ReadStructure(int dev, const DVD_READ_STRUCTURE *structure, PDVD_LAYER_DESCRIPTOR layer)
2588 #ifdef DVD_READ_STRUCT
2589 /* dvd_struct is not defined consistently across platforms */
2590 union
2592 struct dvd_physical physical;
2593 struct dvd_copyright copyright;
2594 struct dvd_disckey disckey;
2595 struct dvd_bca bca;
2596 struct dvd_manufact manufact;
2597 } s;
2599 if (structure->BlockByteOffset.u.HighPart || structure->BlockByteOffset.u.LowPart)
2600 FIXME(": BlockByteOffset is not handled\n");
2602 switch (structure->Format)
2604 case DvdPhysicalDescriptor:
2605 s.physical.type = DVD_STRUCT_PHYSICAL;
2606 s.physical.layer_num = structure->LayerNumber;
2607 break;
2609 case DvdCopyrightDescriptor:
2610 s.copyright.type = DVD_STRUCT_COPYRIGHT;
2611 s.copyright.layer_num = structure->LayerNumber;
2612 break;
2614 case DvdDiskKeyDescriptor:
2615 s.disckey.type = DVD_STRUCT_DISCKEY;
2616 s.disckey.agid = structure->SessionId;
2617 break;
2619 case DvdBCADescriptor:
2620 s.bca.type = DVD_STRUCT_BCA;
2621 break;
2623 case DvdManufacturerDescriptor:
2624 s.manufact.type = DVD_STRUCT_MANUFACT;
2625 s.manufact.layer_num = structure->LayerNumber;
2626 break;
2628 case DvdMaxDescriptor: /* This is not a real request, no matter what MSDN says! */
2629 default:
2630 return STATUS_INVALID_PARAMETER;
2633 if (ioctl(dev, DVD_READ_STRUCT, &s) < 0)
2634 return STATUS_INVALID_PARAMETER;
2636 switch (structure->Format)
2638 case DvdPhysicalDescriptor:
2640 internal_dvd_layer_descriptor *p = (internal_dvd_layer_descriptor *) layer;
2641 struct dvd_layer *l = &s.physical.layer[s.physical.layer_num];
2643 p->DataLength[0] = 2;
2644 p->DataLength[1] = 8;
2645 p->Reserved0[0] = 0;
2646 p->Reserved0[1] = 0;
2647 p->BookVersion = l->book_version;
2648 p->BookType = l->book_type;
2649 p->MinimumRate = l->min_rate;
2650 p->DiskSize = l->disc_size;
2651 p->LayerType = l->layer_type;
2652 p->TrackPath = l->track_path;
2653 p->NumberOfLayers = l->nlayers;
2654 p->Reserved1 = 0;
2655 p->TrackDensity = l->track_density;
2656 p->LinearDensity = l->linear_density;
2657 p->StartingDataSector = l->start_sector;
2658 p->EndDataSector = l->end_sector;
2659 p->EndLayerZeroSector = l->end_sector_l0;
2660 p->Reserved5 = 0;
2661 p->BCAFlag = l->bca;
2662 p->Reserved6 = 0;
2664 break;
2666 case DvdCopyrightDescriptor:
2668 PDVD_COPYRIGHT_DESCRIPTOR p = (PDVD_COPYRIGHT_DESCRIPTOR) layer;
2670 p->CopyrightProtectionType = s.copyright.cpst;
2671 p->RegionManagementInformation = s.copyright.rmi;
2672 p->Reserved = 0;
2674 break;
2676 case DvdDiskKeyDescriptor:
2678 PDVD_DISK_KEY_DESCRIPTOR p = (PDVD_DISK_KEY_DESCRIPTOR) layer;
2680 memcpy(p->DiskKeyData, s.disckey.value, 2048);
2682 break;
2684 case DvdBCADescriptor:
2686 PDVD_BCA_DESCRIPTOR p = (PDVD_BCA_DESCRIPTOR) layer;
2688 memcpy(p->BCAInformation, s.bca.value, s.bca.len);
2690 break;
2692 case DvdManufacturerDescriptor:
2694 PDVD_MANUFACTURER_DESCRIPTOR p = (PDVD_MANUFACTURER_DESCRIPTOR) layer;
2696 memcpy(p->ManufacturingInformation, s.manufact.value, 2048);
2698 break;
2700 case DvdMaxDescriptor: /* Suppress warning */
2701 break;
2703 #elif defined(__APPLE__)
2704 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2705 dk_dvd_read_structure_t dvdrs;
2706 union
2708 DVDPhysicalFormatInfo phys;
2709 DVDCopyrightInfo copy;
2710 DVDDiscKeyInfo disk_key;
2711 DVDManufacturingInfo manf;
2712 } desc;
2713 union
2715 PDVD_LAYER_DESCRIPTOR layer;
2716 internal_dvd_layer_descriptor *xlayer;
2717 PDVD_COPYRIGHT_DESCRIPTOR copy;
2718 PDVD_DISK_KEY_DESCRIPTOR disk_key;
2719 PDVD_MANUFACTURER_DESCRIPTOR manf;
2720 } nt_desc;
2722 nt_desc.layer = layer;
2723 dvdrs.address = (uint32_t)(structure->BlockByteOffset.QuadPart>>11);
2724 dvdrs.grantID = (uint8_t)structure->SessionId;
2725 dvdrs.layer = structure->LayerNumber;
2726 switch(structure->Format)
2728 case DvdPhysicalDescriptor:
2729 dvdrs.format = kDVDStructureFormatPhysicalFormatInfo;
2730 dvdrs.bufferLength = sizeof(desc.phys);
2731 dvdrs.buffer = &desc.phys;
2732 break;
2734 case DvdCopyrightDescriptor:
2735 dvdrs.format = kDVDStructureFormatCopyrightInfo;
2736 dvdrs.bufferLength = sizeof(desc.copy);
2737 dvdrs.buffer = &desc.copy;
2738 break;
2740 case DvdDiskKeyDescriptor:
2741 dvdrs.format = kDVDStructureFormatDiscKeyInfo;
2742 dvdrs.bufferLength = sizeof(desc.disk_key);
2743 dvdrs.buffer = &desc.disk_key;
2744 break;
2746 case DvdBCADescriptor:
2747 FIXME("DvdBCADescriptor NIY\n");
2748 return STATUS_NOT_SUPPORTED;
2750 case DvdManufacturerDescriptor:
2751 dvdrs.format = kDVDStructureFormatManufacturingInfo;
2752 dvdrs.bufferLength = sizeof(desc.manf);
2753 dvdrs.buffer = &desc.manf;
2754 break;
2756 case DvdMaxDescriptor:
2757 default:
2758 FIXME("got unknown structure type 0x%x\n", structure->Format);
2759 return STATUS_NOT_SUPPORTED;
2761 ret = CDROM_GetStatusCode(ioctl(dev, DKIOCDVDREADSTRUCTURE, &dvdrs));
2762 if(ret == STATUS_SUCCESS)
2764 switch(structure->Format)
2766 case DvdPhysicalDescriptor:
2767 nt_desc.xlayer->DataLength[0] = 2;
2768 nt_desc.xlayer->DataLength[1] = 8;
2769 nt_desc.xlayer->Reserved0[0] = 0;
2770 nt_desc.xlayer->Reserved0[1] = 0;
2771 nt_desc.xlayer->BookVersion = desc.phys.partVersion;
2772 nt_desc.xlayer->BookType = desc.phys.bookType;
2773 nt_desc.xlayer->MinimumRate = desc.phys.minimumRate;
2774 nt_desc.xlayer->DiskSize = desc.phys.discSize;
2775 nt_desc.xlayer->LayerType = desc.phys.layerType;
2776 nt_desc.xlayer->TrackPath = desc.phys.trackPath;
2777 nt_desc.xlayer->NumberOfLayers = desc.phys.numberOfLayers;
2778 nt_desc.xlayer->Reserved1 = 0;
2779 nt_desc.xlayer->TrackDensity = desc.phys.trackDensity;
2780 nt_desc.xlayer->LinearDensity = desc.phys.linearDensity;
2781 nt_desc.xlayer->BCAFlag = desc.phys.bcaFlag;
2782 nt_desc.xlayer->StartingDataSector = OSReadBigInt32(&desc.phys.zero1, 0);
2783 nt_desc.xlayer->EndDataSector = OSReadBigInt32(&desc.phys.zero2, 0);
2784 nt_desc.xlayer->EndLayerZeroSector = OSReadBigInt32(&desc.phys.zero3, 0);
2785 nt_desc.xlayer->Reserved5 = 0;
2786 nt_desc.xlayer->Reserved6 = 0;
2787 break;
2789 case DvdCopyrightDescriptor:
2790 nt_desc.copy->CopyrightProtectionType =
2791 desc.copy.copyrightProtectionSystemType;
2792 nt_desc.copy->RegionManagementInformation =
2793 desc.copy.regionMask;
2794 nt_desc.copy->Reserved = 0;
2795 break;
2797 case DvdDiskKeyDescriptor:
2798 memcpy(
2799 nt_desc.disk_key->DiskKeyData,
2800 desc.disk_key.discKeyStructures,
2801 2048);
2802 break;
2804 case DvdManufacturerDescriptor:
2805 memcpy(
2806 nt_desc.manf->ManufacturingInformation,
2807 desc.manf.discManufacturingInfo,
2808 2048);
2809 break;
2811 case DvdBCADescriptor:
2812 case DvdMaxDescriptor:
2813 default:
2814 /* Silence warning */
2815 break;
2818 return ret;
2819 #else
2820 FIXME("\n");
2821 #endif
2822 return STATUS_SUCCESS;
2826 /******************************************************************
2827 * GetInquiryData
2828 * Implements the IOCTL_GET_INQUIRY_DATA ioctl.
2829 * Returns Inquiry data for all devices on the specified scsi bus
2830 * Returns STATUS_BUFFER_TOO_SMALL if the output buffer is too small,
2831 * STATUS_INVALID_DEVICE_REQUEST if the given handle isn't to a SCSI device,
2832 * or STATUS_NOT_SUPPORTED if the OS driver is too old
2834 static NTSTATUS GetInquiryData(int fd, PSCSI_ADAPTER_BUS_INFO BufferOut, DWORD OutBufferSize)
2836 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
2837 PSCSI_INQUIRY_DATA pInquiryData = NULL;
2838 UCHAR sense_buffer[32];
2839 int iochk, version;
2840 sg_io_hdr_t iocmd;
2841 UCHAR inquiry[INQ_CMD_LEN] = {INQUIRY, 0, 0, 0, INQ_REPLY_LEN, 0};
2843 /* Check we have a SCSI device and a supported driver */
2844 if(ioctl(fd, SG_GET_VERSION_NUM, &version) != 0)
2846 WARN("IOCTL_SCSI_GET_INQUIRY_DATA sg driver is not loaded\n");
2847 return STATUS_INVALID_DEVICE_REQUEST;
2849 if(version < 30000 )
2850 return STATUS_NOT_SUPPORTED;
2852 /* FIXME: Enumerate devices on the bus */
2853 BufferOut->NumberOfBuses = 1;
2854 BufferOut->BusData[0].NumberOfLogicalUnits = 1;
2855 BufferOut->BusData[0].InquiryDataOffset = sizeof(SCSI_ADAPTER_BUS_INFO);
2857 pInquiryData = (PSCSI_INQUIRY_DATA)(BufferOut + 1);
2859 RtlZeroMemory(&iocmd, sizeof(iocmd));
2860 iocmd.interface_id = 'S';
2861 iocmd.cmd_len = sizeof(inquiry);
2862 iocmd.mx_sb_len = sizeof(sense_buffer);
2863 iocmd.dxfer_direction = SG_DXFER_FROM_DEV;
2864 iocmd.dxfer_len = INQ_REPLY_LEN;
2865 iocmd.dxferp = pInquiryData->InquiryData;
2866 iocmd.cmdp = inquiry;
2867 iocmd.sbp = sense_buffer;
2868 iocmd.timeout = 1000;
2870 iochk = ioctl(fd, SG_IO, &iocmd);
2871 if(iochk != 0)
2872 WARN("ioctl SG_IO returned %d, error (%s)\n", iochk, strerror(errno));
2874 CDROM_GetInterfaceInfo(fd, &BufferOut->BusData[0].InitiatorBusId, &pInquiryData->PathId, &pInquiryData->TargetId, &pInquiryData->Lun);
2875 pInquiryData->DeviceClaimed = TRUE;
2876 pInquiryData->InquiryDataLength = INQ_REPLY_LEN;
2877 pInquiryData->NextInquiryDataOffset = 0;
2878 return STATUS_SUCCESS;
2879 #else
2880 FIXME("not supported on this O/S\n");
2881 return STATUS_NOT_SUPPORTED;
2882 #endif
2885 /******************************************************************
2886 * CDROM_DeviceIoControl
2890 NTSTATUS CDROM_DeviceIoControl(HANDLE hDevice,
2891 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
2892 PVOID UserApcContext,
2893 PIO_STATUS_BLOCK piosb,
2894 ULONG dwIoControlCode,
2895 LPVOID lpInBuffer, DWORD nInBufferSize,
2896 LPVOID lpOutBuffer, DWORD nOutBufferSize)
2898 DWORD sz = 0;
2899 NTSTATUS status = STATUS_SUCCESS;
2900 int fd, needs_close, dev;
2902 TRACE("%p %s %p %d %p %d %p\n",
2903 hDevice, iocodex(dwIoControlCode), lpInBuffer, nInBufferSize,
2904 lpOutBuffer, nOutBufferSize, piosb);
2906 piosb->Information = 0;
2908 if ((status = server_get_unix_fd( hDevice, 0, &fd, &needs_close, NULL, NULL )))
2910 if (status == STATUS_BAD_DEVICE_TYPE) return status; /* no associated fd */
2911 goto error;
2914 if ((status = CDROM_Open(fd, &dev)))
2916 if (needs_close) close( fd );
2917 goto error;
2920 #ifdef __APPLE__
2922 char name[100];
2924 /* This is ugly as hell, but Mac OS is unable to do anything from the
2925 * partition fd, it wants an fd for the whole device, and it sometimes
2926 * also requires the device fd to be closed first, so we have to close
2927 * the handle that the caller gave us.
2928 * Also for some reason it wants the fd to be closed before we even
2929 * open the parent if we're trying to eject the disk.
2931 if ((status = get_parent_device( fd, name, sizeof(name) ))) goto error;
2932 if (dwIoControlCode == IOCTL_STORAGE_EJECT_MEDIA)
2933 NtClose( hDevice );
2934 if (needs_close) close( fd );
2935 TRACE("opening parent %s\n", name );
2936 if ((fd = open( name, O_RDONLY )) == -1)
2938 status = FILE_GetNtStatus();
2939 goto error;
2941 needs_close = 1;
2943 #endif
2945 switch (dwIoControlCode)
2947 case IOCTL_STORAGE_CHECK_VERIFY:
2948 case IOCTL_CDROM_CHECK_VERIFY:
2949 case IOCTL_DISK_CHECK_VERIFY:
2950 sz = 0;
2951 CDROM_ClearCacheEntry(dev);
2952 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2953 status = STATUS_INVALID_PARAMETER;
2954 else status = CDROM_Verify(dev, fd);
2955 break;
2957 /* EPP case IOCTL_STORAGE_CHECK_VERIFY2: */
2959 /* EPP case IOCTL_STORAGE_FIND_NEW_DEVICES: */
2960 /* EPP case IOCTL_CDROM_FIND_NEW_DEVICES: */
2962 case IOCTL_STORAGE_LOAD_MEDIA:
2963 case IOCTL_CDROM_LOAD_MEDIA:
2964 sz = 0;
2965 CDROM_ClearCacheEntry(dev);
2966 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2967 status = STATUS_INVALID_PARAMETER;
2968 else status = CDROM_SetTray(fd, FALSE);
2969 break;
2970 case IOCTL_STORAGE_EJECT_MEDIA:
2971 sz = 0;
2972 CDROM_ClearCacheEntry(dev);
2973 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2974 status = STATUS_INVALID_PARAMETER;
2975 else
2976 status = CDROM_SetTray(fd, TRUE);
2977 break;
2979 case IOCTL_CDROM_MEDIA_REMOVAL:
2980 case IOCTL_DISK_MEDIA_REMOVAL:
2981 case IOCTL_STORAGE_MEDIA_REMOVAL:
2982 case IOCTL_STORAGE_EJECTION_CONTROL:
2983 /* FIXME the last ioctl:s is not the same as the two others...
2984 * lockcount/owner should be handled */
2985 sz = 0;
2986 CDROM_ClearCacheEntry(dev);
2987 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2988 else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) status = STATUS_BUFFER_TOO_SMALL;
2989 else status = CDROM_ControlEjection(fd, lpInBuffer);
2990 break;
2992 case IOCTL_STORAGE_GET_MEDIA_TYPES:
2993 case IOCTL_STORAGE_GET_MEDIA_TYPES_EX:
2994 sz = sizeof(GET_MEDIA_TYPES);
2995 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2996 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2997 else status = CDROM_GetMediaType(dev, lpOutBuffer);
2998 break;
3000 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
3001 sz = sizeof(STORAGE_DEVICE_NUMBER);
3002 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3003 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3004 else status = CDROM_GetDeviceNumber(dev, lpOutBuffer);
3005 break;
3007 case IOCTL_STORAGE_RESET_DEVICE:
3008 sz = 0;
3009 CDROM_ClearCacheEntry(dev);
3010 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3011 status = STATUS_INVALID_PARAMETER;
3012 else status = CDROM_ResetAudio(fd);
3013 break;
3015 case IOCTL_CDROM_GET_CONTROL:
3016 sz = sizeof(CDROM_AUDIO_CONTROL);
3017 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3018 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3019 else status = CDROM_GetControl(dev, fd, lpOutBuffer);
3020 break;
3022 case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
3023 sz = sizeof(DISK_GEOMETRY);
3024 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3025 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3026 else status = CDROM_GetDriveGeometry(dev, fd, lpOutBuffer);
3027 break;
3029 case IOCTL_CDROM_DISK_TYPE:
3030 sz = sizeof(CDROM_DISK_DATA);
3031 /* CDROM_ClearCacheEntry(dev); */
3032 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3033 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3034 else status = CDROM_GetDiskData(dev, fd, lpOutBuffer);
3035 break;
3037 /* EPP case IOCTL_CDROM_GET_LAST_SESSION: */
3039 case IOCTL_CDROM_READ_Q_CHANNEL:
3040 sz = sizeof(SUB_Q_CHANNEL_DATA);
3041 if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT))
3042 status = STATUS_INVALID_PARAMETER;
3043 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3044 else status = CDROM_ReadQChannel(dev, fd, lpInBuffer, lpOutBuffer);
3045 break;
3047 case IOCTL_CDROM_READ_TOC:
3048 sz = sizeof(CDROM_TOC);
3049 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3050 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3051 else status = CDROM_ReadTOC(dev, fd, lpOutBuffer);
3052 break;
3054 /* EPP case IOCTL_CDROM_READ_TOC_EX: */
3056 case IOCTL_CDROM_PAUSE_AUDIO:
3057 sz = 0;
3058 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3059 status = STATUS_INVALID_PARAMETER;
3060 else status = CDROM_PauseAudio(fd);
3061 break;
3062 case IOCTL_CDROM_PLAY_AUDIO_MSF:
3063 sz = 0;
3064 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3065 else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
3066 else status = CDROM_PlayAudioMSF(fd, lpInBuffer);
3067 break;
3068 case IOCTL_CDROM_RESUME_AUDIO:
3069 sz = 0;
3070 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3071 status = STATUS_INVALID_PARAMETER;
3072 else status = CDROM_ResumeAudio(fd);
3073 break;
3074 case IOCTL_CDROM_SEEK_AUDIO_MSF:
3075 sz = 0;
3076 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3077 else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
3078 else status = CDROM_SeekAudioMSF(dev, fd, lpInBuffer);
3079 break;
3080 case IOCTL_CDROM_STOP_AUDIO:
3081 sz = 0;
3082 CDROM_ClearCacheEntry(dev); /* Maybe intention is to change media */
3083 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3084 status = STATUS_INVALID_PARAMETER;
3085 else status = CDROM_StopAudio(fd);
3086 break;
3087 case IOCTL_CDROM_GET_VOLUME:
3088 sz = sizeof(VOLUME_CONTROL);
3089 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3090 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3091 else status = CDROM_GetVolume(fd, lpOutBuffer);
3092 break;
3093 case IOCTL_CDROM_SET_VOLUME:
3094 sz = 0;
3095 CDROM_ClearCacheEntry(dev);
3096 if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
3097 status = STATUS_INVALID_PARAMETER;
3098 else status = CDROM_SetVolume(fd, lpInBuffer);
3099 break;
3100 case IOCTL_CDROM_RAW_READ:
3101 sz = 0;
3102 if (nInBufferSize < sizeof(RAW_READ_INFO)) status = STATUS_INVALID_PARAMETER;
3103 else if (lpOutBuffer == NULL) status = STATUS_BUFFER_TOO_SMALL;
3104 else status = CDROM_RawRead(fd, lpInBuffer, lpOutBuffer,
3105 nOutBufferSize, &sz);
3106 break;
3107 case IOCTL_SCSI_GET_ADDRESS:
3108 sz = sizeof(SCSI_ADDRESS);
3109 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3110 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3111 else status = CDROM_GetAddress(fd, lpOutBuffer);
3112 break;
3113 case IOCTL_SCSI_PASS_THROUGH_DIRECT:
3114 sz = sizeof(SCSI_PASS_THROUGH_DIRECT);
3115 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3116 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH_DIRECT)) status = STATUS_BUFFER_TOO_SMALL;
3117 else status = CDROM_ScsiPassThroughDirect(fd, lpOutBuffer);
3118 break;
3119 case IOCTL_SCSI_PASS_THROUGH:
3120 sz = sizeof(SCSI_PASS_THROUGH);
3121 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3122 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH)) status = STATUS_BUFFER_TOO_SMALL;
3123 else status = CDROM_ScsiPassThrough(fd, lpOutBuffer);
3124 break;
3125 case IOCTL_SCSI_GET_CAPABILITIES:
3126 sz = sizeof(IO_SCSI_CAPABILITIES);
3127 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3128 else if (nOutBufferSize < sizeof(IO_SCSI_CAPABILITIES)) status = STATUS_BUFFER_TOO_SMALL;
3129 else status = CDROM_ScsiGetCaps(fd, lpOutBuffer);
3130 break;
3131 case IOCTL_DVD_START_SESSION:
3132 sz = sizeof(DVD_SESSION_ID);
3133 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3134 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3135 else
3137 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3138 *(PDVD_SESSION_ID)lpOutBuffer);
3139 status = DVD_StartSession(fd, lpInBuffer, lpOutBuffer);
3140 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3141 *(PDVD_SESSION_ID)lpOutBuffer);
3143 break;
3144 case IOCTL_DVD_END_SESSION:
3145 sz = sizeof(DVD_SESSION_ID);
3146 if ((lpInBuffer == NULL) || (nInBufferSize < sz))status = STATUS_INVALID_PARAMETER;
3147 else status = DVD_EndSession(fd, lpInBuffer);
3148 break;
3149 case IOCTL_DVD_SEND_KEY:
3150 sz = 0;
3151 if (!lpInBuffer ||
3152 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3153 status = STATUS_INVALID_PARAMETER;
3154 else
3156 TRACE("doing DVD_SendKey\n");
3157 status = DVD_SendKey(fd, lpInBuffer);
3159 break;
3160 case IOCTL_DVD_READ_KEY:
3161 if (!lpInBuffer ||
3162 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3163 status = STATUS_INVALID_PARAMETER;
3164 else if (lpInBuffer !=lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
3165 else
3167 TRACE("doing DVD_READ_KEY\n");
3168 sz = ((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength;
3169 status = DVD_ReadKey(fd, lpInBuffer);
3171 break;
3172 case IOCTL_DVD_GET_REGION:
3173 sz = sizeof(DVD_REGION);
3174 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3175 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3176 TRACE("doing DVD_Get_REGION\n");
3177 status = DVD_GetRegion(fd, lpOutBuffer);
3178 break;
3179 case IOCTL_DVD_READ_STRUCTURE:
3180 sz = sizeof(DVD_LAYER_DESCRIPTOR);
3181 if (lpInBuffer == NULL || nInBufferSize != sizeof(DVD_READ_STRUCTURE)) status = STATUS_INVALID_PARAMETER;
3182 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3183 else
3185 TRACE("doing DVD_READ_STRUCTURE\n");
3186 status = DVD_ReadStructure(fd, lpInBuffer, lpOutBuffer);
3188 break;
3190 case IOCTL_SCSI_GET_INQUIRY_DATA:
3191 sz = INQ_REPLY_LEN;
3192 status = GetInquiryData(fd, lpOutBuffer, nOutBufferSize);
3193 break;
3195 default:
3196 if (needs_close) close( fd );
3197 return STATUS_NOT_SUPPORTED;
3199 if (needs_close) close( fd );
3200 error:
3201 piosb->u.Status = status;
3202 piosb->Information = sz;
3203 if (hEvent) NtSetEvent(hEvent, NULL);
3204 return status;