kernel32/tests: Add a IOCTL_DVD_READ_STRUCTURE (DvdPhysicalDescriptor) test.
[wine/multimedia.git] / dlls / ntdll / cdrom.c
bloba58701e15b0ed5d960eba1594578d887e7e2245d
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 typedef struct
108 uint64_t bus;
109 uint64_t port;
110 uint64_t target;
111 uint64_t lun;
112 } dk_scsi_identify_t;
114 #define DKIOCSCSICOMMAND _IOWR('d', 253, dk_scsi_command_t)
115 #define DKIOCSCSIIDENTIFY _IOR('d', 254, dk_scsi_identify_t)
117 #endif
119 #define NONAMELESSUNION
120 #define NONAMELESSSTRUCT
121 #include "ntstatus.h"
122 #define WIN32_NO_STATUS
123 #include "windef.h"
124 #include "winternl.h"
125 #include "winioctl.h"
126 #include "ntddstor.h"
127 #include "ntddcdrm.h"
128 #include "ddk/ntddcdvd.h"
129 #include "ntddscsi.h"
130 #include "ntdll_misc.h"
131 #include "wine/server.h"
132 #include "wine/library.h"
133 #include "wine/debug.h"
135 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
137 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
138 lack the following constants. */
140 #ifndef CD_SECS
141 # define CD_SECS 60 /* seconds per minute */
142 #endif
143 #ifndef CD_FRAMES
144 # define CD_FRAMES 75 /* frames per second */
145 #endif
147 #ifdef WORDS_BIGENDIAN
148 #define GET_BE_DWORD(x) (x)
149 #else
150 #define GET_BE_DWORD(x) RtlUlongByteSwap(x)
151 #endif
153 static const struct iocodexs
155 DWORD code;
156 const char *codex;
157 } iocodextable[] = {
158 #define X(x) { x, #x },
159 X(IOCTL_CDROM_CHECK_VERIFY)
160 X(IOCTL_CDROM_CURRENT_POSITION)
161 X(IOCTL_CDROM_DISK_TYPE)
162 X(IOCTL_CDROM_GET_CONTROL)
163 X(IOCTL_CDROM_GET_DRIVE_GEOMETRY)
164 X(IOCTL_CDROM_GET_VOLUME)
165 X(IOCTL_CDROM_LOAD_MEDIA)
166 X(IOCTL_CDROM_MEDIA_CATALOG)
167 X(IOCTL_CDROM_MEDIA_REMOVAL)
168 X(IOCTL_CDROM_PAUSE_AUDIO)
169 X(IOCTL_CDROM_PLAY_AUDIO_MSF)
170 X(IOCTL_CDROM_RAW_READ)
171 X(IOCTL_CDROM_READ_Q_CHANNEL)
172 X(IOCTL_CDROM_READ_TOC)
173 X(IOCTL_CDROM_RESUME_AUDIO)
174 X(IOCTL_CDROM_SEEK_AUDIO_MSF)
175 X(IOCTL_CDROM_SET_VOLUME)
176 X(IOCTL_CDROM_STOP_AUDIO)
177 X(IOCTL_CDROM_TRACK_ISRC)
178 X(IOCTL_DISK_MEDIA_REMOVAL)
179 X(IOCTL_DVD_END_SESSION)
180 X(IOCTL_DVD_GET_REGION)
181 X(IOCTL_DVD_READ_KEY)
182 X(IOCTL_DVD_READ_STRUCTURE)
183 X(IOCTL_DVD_SEND_KEY)
184 X(IOCTL_DVD_START_SESSION)
185 X(IOCTL_SCSI_GET_ADDRESS)
186 X(IOCTL_SCSI_GET_CAPABILITIES)
187 X(IOCTL_SCSI_GET_INQUIRY_DATA)
188 X(IOCTL_SCSI_PASS_THROUGH)
189 X(IOCTL_SCSI_PASS_THROUGH_DIRECT)
190 X(IOCTL_STORAGE_CHECK_VERIFY)
191 X(IOCTL_STORAGE_CHECK_VERIFY2)
192 X(IOCTL_STORAGE_EJECTION_CONTROL)
193 X(IOCTL_STORAGE_EJECT_MEDIA)
194 X(IOCTL_STORAGE_GET_DEVICE_NUMBER)
195 X(IOCTL_STORAGE_GET_MEDIA_TYPES)
196 X(IOCTL_STORAGE_GET_MEDIA_TYPES_EX)
197 X(IOCTL_STORAGE_LOAD_MEDIA)
198 X(IOCTL_STORAGE_MEDIA_REMOVAL)
199 X(IOCTL_STORAGE_RESET_DEVICE)
200 #undef X
202 static const char *iocodex(DWORD code)
204 unsigned int i;
205 static char buffer[25];
206 for(i=0; i<sizeof(iocodextable)/sizeof(struct iocodexs); i++)
207 if (code==iocodextable[i].code)
208 return iocodextable[i].codex;
209 sprintf(buffer, "IOCTL_CODE_%x", (int)code);
210 return buffer;
213 #define INQ_REPLY_LEN 36
214 #define INQ_CMD_LEN 6
216 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
217 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
218 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
219 #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;}
221 /* The documented format of DVD_LAYER_DESCRIPTOR is wrong. Even the format in the
222 * DDK's header is wrong. There are four bytes at the start defined by
223 * MMC-5. The first two are the size of the structure in big-endian order as
224 * defined by MMC-5. The other two are reserved.
226 typedef struct
228 DVD_DESCRIPTOR_HEADER Header;
229 DVD_LAYER_DESCRIPTOR Descriptor;
230 UCHAR Padding;
231 } internal_dvd_layer_descriptor;
232 C_ASSERT(sizeof(internal_dvd_layer_descriptor) == 22);
235 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
236 static NTSTATUS CDROM_GetStatusCode(int);
239 #ifdef linux
241 # ifndef IDE6_MAJOR
242 # define IDE6_MAJOR 88
243 # endif
244 # ifndef IDE7_MAJOR
245 # define IDE7_MAJOR 89
246 # endif
248 # ifdef CDROM_SEND_PACKET
249 /* structure for CDROM_PACKET_COMMAND ioctl */
250 /* not all Linux versions have all the fields, so we define the
251 * structure ourselves to make sure */
252 struct linux_cdrom_generic_command
254 unsigned char cmd[CDROM_PACKET_SIZE];
255 unsigned char *buffer;
256 unsigned int buflen;
257 int stat;
258 struct request_sense *sense;
259 unsigned char data_direction;
260 int quiet;
261 int timeout;
262 void *reserved[1];
264 # endif /* CDROM_SEND_PACKET */
266 #endif /* linux */
268 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
269 * this should be removed when a proper device interface is implemented
271 * (WS) We need this to keep track of current position and to safely
272 * detect media changes. Besides this should provide a great speed up
273 * for toc inquiries.
275 struct cdrom_cache {
276 dev_t device;
277 ino_t inode;
278 char toc_good; /* if false, will reread TOC from disk */
279 CDROM_TOC toc;
280 SUB_Q_CURRENT_POSITION CurrentPosition;
282 /* who has more than 5 cdroms on his/her machine ?? */
283 /* FIXME: this should grow depending on the number of cdroms we install/configure
284 * at startup
286 #define MAX_CACHE_ENTRIES 5
287 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
289 static RTL_CRITICAL_SECTION cache_section;
290 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
292 0, 0, &cache_section,
293 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
294 0, 0, { (DWORD_PTR)(__FILE__ ": cache_section") }
296 static RTL_CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
298 /* Proposed media change function: not really needed at this time */
299 /* This is a 1 or 0 type of function */
300 #if 0
301 static int CDROM_MediaChanged(int dev)
303 int i;
305 struct cdrom_tochdr hdr;
306 struct cdrom_tocentry entry;
308 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
309 return 0;
310 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
311 return 0;
313 if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
314 return 1;
316 for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
318 if (i == hdr.cdth_trk1 + 1)
320 entry.cdte_track = CDROM_LEADOUT;
321 } else {
322 entry.cdte_track = i;
324 entry.cdte_format = CDROM_MSF;
325 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
326 return 0;
327 if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
328 sizeof(struct cdrom_tocentry)) )
329 return 1;
331 return 0;
333 #endif
336 /******************************************************************
337 * get_parent_device
339 * On Mac OS, get the device for the whole disk from a fd that points to a partition.
340 * This is ugly and inefficient, but we have no choice since the partition fd doesn't
341 * support the eject ioctl.
343 #ifdef __APPLE__
344 static NTSTATUS get_parent_device( int fd, char *name, size_t len )
346 NTSTATUS status = STATUS_NO_SUCH_FILE;
347 struct stat st;
348 int i;
349 io_service_t service;
350 CFMutableDictionaryRef dict;
351 CFTypeRef val;
353 if (fstat( fd, &st ) == -1) return FILE_GetNtStatus();
354 if (!S_ISCHR( st.st_mode )) return STATUS_OBJECT_TYPE_MISMATCH;
356 /* create a dictionary with the right major/minor numbers */
358 if (!(dict = IOServiceMatching( kIOMediaClass ))) return STATUS_NO_MEMORY;
360 i = major( st.st_rdev );
361 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
362 CFDictionaryAddValue( dict, CFSTR( "BSD Major" ), val );
363 CFRelease( val );
365 i = minor( st.st_rdev );
366 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
367 CFDictionaryAddValue( dict, CFSTR( "BSD Minor" ), val );
368 CFRelease( val );
370 CFDictionaryAddValue( dict, CFSTR("Removable"), kCFBooleanTrue );
372 service = IOServiceGetMatchingService( kIOMasterPortDefault, dict );
374 /* now look for the parent that has the "Whole" attribute set to TRUE */
376 while (service)
378 io_service_t parent = 0;
379 CFBooleanRef whole;
380 CFStringRef str;
381 int ok;
383 if (!IOObjectConformsTo( service, kIOMediaClass ))
384 goto next;
385 if (!(whole = IORegistryEntryCreateCFProperty( service, CFSTR("Whole"), NULL, 0 )))
386 goto next;
387 ok = (whole == kCFBooleanTrue);
388 CFRelease( whole );
389 if (!ok) goto next;
391 if ((str = IORegistryEntryCreateCFProperty( service, CFSTR("BSD Name"), NULL, 0 )))
393 strcpy( name, "/dev/r" );
394 CFStringGetCString( str, name + 6, len - 6, kCFStringEncodingUTF8 );
395 CFRelease( str );
396 status = STATUS_SUCCESS;
398 IOObjectRelease( service );
399 break;
401 next:
402 IORegistryEntryGetParentEntry( service, kIOServicePlane, &parent );
403 IOObjectRelease( service );
404 service = parent;
406 return status;
408 #endif
411 /******************************************************************
412 * CDROM_SyncCache [internal]
414 * Read the TOC in and store it in the cdrom_cache structure.
415 * Further requests for the TOC will be copied from the cache
416 * unless certain events like disk ejection is detected, in which
417 * case the cache will be cleared, causing it to be resynced.
418 * The cache section must be held by caller.
420 static NTSTATUS CDROM_SyncCache(int dev, int fd)
422 #ifdef linux
423 int i, tsz;
424 struct cdrom_tochdr hdr;
425 struct cdrom_tocentry entry;
427 CDROM_TOC *toc = &cdrom_cache[dev].toc;
428 cdrom_cache[dev].toc_good = 0;
430 if (ioctl(fd, CDROMREADTOCHDR, &hdr) == -1)
432 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
433 return FILE_GetNtStatus();
436 toc->FirstTrack = hdr.cdth_trk0;
437 toc->LastTrack = hdr.cdth_trk1;
438 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
439 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
440 toc->Length[0] = tsz >> 8;
441 toc->Length[1] = tsz;
443 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
445 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
447 if (i == toc->LastTrack + 1)
448 entry.cdte_track = CDROM_LEADOUT;
449 else
450 entry.cdte_track = i;
451 entry.cdte_format = CDROM_MSF;
452 if (ioctl(fd, CDROMREADTOCENTRY, &entry) == -1)
454 WARN("error read entry (%s)\n", strerror(errno));
455 return FILE_GetNtStatus();
457 toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
458 toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
459 /* marking last track with leadout value as index */
460 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
461 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
462 toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
463 toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
464 toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
466 cdrom_cache[dev].toc_good = 1;
467 return STATUS_SUCCESS;
469 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
471 int i, tsz;
472 struct ioc_toc_header hdr;
473 struct ioc_read_toc_entry entry;
474 struct cd_toc_entry toc_buffer;
476 CDROM_TOC *toc = &cdrom_cache[dev].toc;
477 cdrom_cache[dev].toc_good = 0;
479 if (ioctl(fd, CDIOREADTOCHEADER, &hdr) == -1)
481 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
482 return FILE_GetNtStatus();
484 toc->FirstTrack = hdr.starting_track;
485 toc->LastTrack = hdr.ending_track;
486 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
487 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
488 toc->Length[0] = tsz >> 8;
489 toc->Length[1] = tsz;
491 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
493 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
495 if (i == toc->LastTrack + 1)
497 #define LEADOUT 0xaa
498 entry.starting_track = LEADOUT;
499 } else {
500 entry.starting_track = i;
502 memset(&toc_buffer, 0, sizeof(toc_buffer));
503 entry.address_format = CD_MSF_FORMAT;
504 entry.data_len = sizeof(toc_buffer);
505 entry.data = &toc_buffer;
506 if (ioctl(fd, CDIOREADTOCENTRYS, &entry) == -1)
508 WARN("error read entry (%s)\n", strerror(errno));
509 return FILE_GetNtStatus();
511 toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
512 toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
513 /* marking last track with leadout value as index */
514 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
515 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
516 toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
517 toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
518 toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
520 cdrom_cache[dev].toc_good = 1;
521 return STATUS_SUCCESS;
523 #elif defined(__APPLE__)
524 int i;
525 dk_cd_read_toc_t hdr;
526 CDROM_TOC *toc = &cdrom_cache[dev].toc;
527 cdrom_cache[dev].toc_good = 0;
529 memset( &hdr, 0, sizeof(hdr) );
530 hdr.buffer = toc;
531 hdr.bufferLength = sizeof(*toc);
532 if (ioctl(fd, DKIOCCDREADTOC, &hdr) == -1)
534 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
535 return FILE_GetNtStatus();
537 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
539 /* convert address format */
540 TRACK_DATA *data = &toc->TrackData[i - toc->FirstTrack];
541 DWORD frame = (((DWORD)data->Address[0] << 24) | ((DWORD)data->Address[1] << 16) |
542 ((DWORD)data->Address[2] << 8) | data->Address[3]);
543 MSF_OF_FRAME( data->Address[1], frame );
544 data->Address[0] = 0;
547 cdrom_cache[dev].toc_good = 1;
548 return STATUS_SUCCESS;
549 #else
550 FIXME("not supported on this O/S\n");
551 return STATUS_NOT_SUPPORTED;
552 #endif
555 static void CDROM_ClearCacheEntry(int dev)
557 RtlEnterCriticalSection( &cache_section );
558 cdrom_cache[dev].toc_good = 0;
559 RtlLeaveCriticalSection( &cache_section );
564 /******************************************************************
565 * CDROM_GetInterfaceInfo
567 * Determines the ide interface (the number after the ide), and the
568 * number of the device on that interface for ide cdroms (*iface <= 1).
569 * Determines the scsi information for scsi cdroms (*iface >= 2).
570 * Returns false if the info cannot not be obtained.
572 static int CDROM_GetInterfaceInfo(int fd, UCHAR* iface, UCHAR* port, UCHAR* device, UCHAR* lun)
574 #if defined(linux)
575 struct stat st;
576 if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode)) return 0;
577 *port = 0;
578 *iface = 0;
579 *device = 0;
580 *lun = 0;
581 switch (major(st.st_rdev)) {
582 case IDE0_MAJOR: *iface = 0; break;
583 case IDE1_MAJOR: *iface = 1; break;
584 case IDE2_MAJOR: *iface = 2; break;
585 case IDE3_MAJOR: *iface = 3; break;
586 case IDE4_MAJOR: *iface = 4; break;
587 case IDE5_MAJOR: *iface = 5; break;
588 case IDE6_MAJOR: *iface = 6; break;
589 case IDE7_MAJOR: *iface = 7; break;
590 default: *port = 1; break;
593 if (*port == 0)
594 *device = (minor(st.st_rdev) >> 6);
595 else
597 #ifdef SCSI_IOCTL_GET_IDLUN
598 UINT32 idlun[2];
599 if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, idlun) != -1)
601 *port = (idlun[0] >> 24) & 0xff;
602 *iface = ((idlun[0] >> 16) & 0xff) + 2;
603 *device = idlun[0] & 0xff;
604 *lun = (idlun[0] >> 8) & 0xff;
606 else
607 #endif
609 WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
610 return 0;
613 return 1;
614 #elif defined(__NetBSD__)
615 struct scsi_addr addr;
616 if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
618 switch (addr.type)
620 case TYPE_SCSI:
621 *port = 1;
622 *iface = addr.addr.scsi.scbus;
623 *device = addr.addr.scsi.target;
624 *lun = addr.addr.scsi.lun;
625 return 1;
626 case TYPE_ATAPI:
627 *port = 0;
628 *iface = addr.addr.atapi.atbus;
629 *device = addr.addr.atapi.drive;
630 *lun = 0;
631 return 1;
634 return 0;
635 #elif defined(__APPLE__)
636 dk_scsi_identify_t addr;
637 if (ioctl(fd, DKIOCSCSIIDENTIFY, &addr) != -1)
639 *port = addr.bus;
640 *iface = addr.port;
641 *device = addr.target;
642 *lun = addr.lun;
643 return 1;
645 return 0;
646 #else
647 FIXME("not implemented on this O/S\n");
648 return 0;
649 #endif
653 /******************************************************************
654 * CDROM_Open
657 static NTSTATUS CDROM_Open(int fd, int* dev)
659 struct stat st;
660 NTSTATUS ret = STATUS_SUCCESS;
661 int empty = -1;
663 fstat(fd, &st);
665 RtlEnterCriticalSection( &cache_section );
666 for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
668 if (empty == -1 &&
669 cdrom_cache[*dev].device == 0 &&
670 cdrom_cache[*dev].inode == 0)
671 empty = *dev;
672 else if (cdrom_cache[*dev].device == st.st_dev &&
673 cdrom_cache[*dev].inode == st.st_ino)
674 break;
676 if (*dev == MAX_CACHE_ENTRIES)
678 if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
679 else
681 *dev = empty;
682 cdrom_cache[*dev].device = st.st_dev;
683 cdrom_cache[*dev].inode = st.st_ino;
686 RtlLeaveCriticalSection( &cache_section );
688 TRACE("%d, %d\n", *dev, fd);
689 return ret;
692 /******************************************************************
693 * CDROM_GetStatusCode
697 static NTSTATUS CDROM_GetStatusCode(int io)
699 if (io == 0) return STATUS_SUCCESS;
700 return FILE_GetNtStatus();
703 /******************************************************************
704 * CDROM_GetControl
707 static NTSTATUS CDROM_GetControl(int dev, int fd, CDROM_AUDIO_CONTROL* cac)
709 #ifdef __APPLE__
710 uint16_t speed;
711 int io = ioctl( fd, DKIOCCDGETSPEED, &speed );
712 if (io != 0) return CDROM_GetStatusCode( io );
713 /* DKIOCCDGETSPEED returns the speed in kilobytes per second,
714 * so convert to logical blocks (assumed to be ~2 KB).
716 cac->LogicalBlocksPerSecond = speed/2;
717 #else
718 cac->LogicalBlocksPerSecond = 1; /* FIXME */
719 #endif
720 cac->LbaFormat = 0; /* FIXME */
721 return STATUS_NOT_SUPPORTED;
724 /******************************************************************
725 * CDROM_GetDeviceNumber
728 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
730 FIXME( "stub\n" );
731 devnum->DeviceType = FILE_DEVICE_DISK;
732 devnum->DeviceNumber = 1;
733 devnum->PartitionNumber = 1;
734 return STATUS_SUCCESS;
737 /******************************************************************
738 * CDROM_GetDriveGeometry
741 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
743 CDROM_TOC toc;
744 NTSTATUS ret = 0;
745 int fsize = 0;
747 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
749 fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
750 - FRAME_OF_TOC(toc, 1); /* Total size in frames */
752 dg->Cylinders.u.LowPart = fsize / (64 * 32);
753 dg->Cylinders.u.HighPart = 0;
754 dg->MediaType = RemovableMedia;
755 dg->TracksPerCylinder = 64;
756 dg->SectorsPerTrack = 32;
757 dg->BytesPerSector= 2048;
758 return ret;
761 /******************************************************************
762 * CDROM_GetMediaType
765 static NTSTATUS CDROM_GetMediaType(int dev, GET_MEDIA_TYPES* medtype)
767 FIXME(": faking success\n");
768 medtype->DeviceType = FILE_DEVICE_CD_ROM;
769 medtype->MediaInfoCount = 0;
770 return STATUS_SUCCESS;
773 /**************************************************************************
774 * CDROM_Reset [internal]
776 static NTSTATUS CDROM_ResetAudio(int fd)
778 #if defined(linux)
779 return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
780 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
781 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
782 #else
783 FIXME("not supported on this O/S\n");
784 return STATUS_NOT_SUPPORTED;
785 #endif
788 /******************************************************************
789 * CDROM_SetTray
793 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
795 #if defined(linux)
796 return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
797 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
798 return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
799 (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
800 (ioctl(fd, CDIOCPREVENT, NULL)));
801 #elif defined(__APPLE__)
802 if (doEject) return CDROM_GetStatusCode( ioctl( fd, DKIOCEJECT, NULL ) );
803 else return STATUS_NOT_SUPPORTED;
804 #else
805 FIXME("not supported on this O/S\n");
806 return STATUS_NOT_SUPPORTED;
807 #endif
810 /******************************************************************
811 * CDROM_ControlEjection
815 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
817 #if defined(linux)
818 return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
819 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
820 return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
821 #else
822 FIXME("not supported on this O/S\n");
823 return STATUS_NOT_SUPPORTED;
824 #endif
827 /******************************************************************
828 * CDROM_ReadTOC
832 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
834 NTSTATUS ret = STATUS_NOT_SUPPORTED;
836 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
837 return STATUS_INVALID_PARAMETER;
839 RtlEnterCriticalSection( &cache_section );
840 if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
842 *toc = cdrom_cache[dev].toc;
843 ret = STATUS_SUCCESS;
845 RtlLeaveCriticalSection( &cache_section );
846 return ret;
849 /******************************************************************
850 * CDROM_GetDiskData
854 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
856 CDROM_TOC toc;
857 NTSTATUS ret;
858 int i;
860 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
861 data->DiskData = 0;
862 for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
863 if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
864 data->DiskData |= CDROM_DISK_DATA_TRACK;
865 else
866 data->DiskData |= CDROM_DISK_AUDIO_TRACK;
868 return STATUS_SUCCESS;
871 /******************************************************************
872 * CDROM_ReadQChannel
876 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
877 SUB_Q_CHANNEL_DATA* data)
879 NTSTATUS ret = STATUS_NOT_SUPPORTED;
880 #ifdef linux
881 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
882 int io;
883 struct cdrom_subchnl sc;
884 sc.cdsc_format = CDROM_MSF;
886 io = ioctl(fd, CDROMSUBCHNL, &sc);
887 if (io == -1)
889 TRACE("opened or no_media (%s)!\n", strerror(errno));
890 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
891 CDROM_ClearCacheEntry(dev);
892 goto end;
895 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
897 switch (sc.cdsc_audiostatus) {
898 case CDROM_AUDIO_INVALID:
899 CDROM_ClearCacheEntry(dev);
900 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
901 break;
902 case CDROM_AUDIO_NO_STATUS:
903 CDROM_ClearCacheEntry(dev);
904 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
905 break;
906 case CDROM_AUDIO_PLAY:
907 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
908 break;
909 case CDROM_AUDIO_PAUSED:
910 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
911 break;
912 case CDROM_AUDIO_COMPLETED:
913 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
914 break;
915 case CDROM_AUDIO_ERROR:
916 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
917 break;
918 default:
919 TRACE("status=%02X !\n", sc.cdsc_audiostatus);
920 break;
922 switch (fmt->Format)
924 case IOCTL_CDROM_CURRENT_POSITION:
925 RtlEnterCriticalSection( &cache_section );
926 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
927 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
928 data->CurrentPosition.Control = sc.cdsc_ctrl;
929 data->CurrentPosition.ADR = sc.cdsc_adr;
930 data->CurrentPosition.TrackNumber = sc.cdsc_trk;
931 data->CurrentPosition.IndexNumber = sc.cdsc_ind;
933 data->CurrentPosition.AbsoluteAddress[0] = 0;
934 data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute;
935 data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
936 data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
938 data->CurrentPosition.TrackRelativeAddress[0] = 0;
939 data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute;
940 data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
941 data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
943 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
945 else /* not playing */
947 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
948 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
950 RtlLeaveCriticalSection( &cache_section );
951 break;
952 case IOCTL_CDROM_MEDIA_CATALOG:
953 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
955 struct cdrom_mcn mcn;
956 if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
958 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
959 data->MediaCatalog.Mcval = 0; /* FIXME */
960 memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
961 data->MediaCatalog.MediaCatalog[14] = 0;
963 break;
964 case IOCTL_CDROM_TRACK_ISRC:
965 FIXME("TrackIsrc: NIY on linux\n");
966 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
967 data->TrackIsrc.Tcval = 0;
968 io = 0;
969 break;
972 end:
973 ret = CDROM_GetStatusCode(io);
974 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
975 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
976 int io;
977 struct ioc_read_subchannel read_sc;
978 struct cd_sub_channel_info sc;
980 read_sc.address_format = CD_MSF_FORMAT;
981 read_sc.track = 0;
982 read_sc.data_len = sizeof(sc);
983 read_sc.data = &sc;
984 switch (fmt->Format)
986 case IOCTL_CDROM_CURRENT_POSITION:
987 read_sc.data_format = CD_CURRENT_POSITION;
988 break;
989 case IOCTL_CDROM_MEDIA_CATALOG:
990 read_sc.data_format = CD_MEDIA_CATALOG;
991 break;
992 case IOCTL_CDROM_TRACK_ISRC:
993 read_sc.data_format = CD_TRACK_INFO;
994 sc.what.track_info.track_number = data->TrackIsrc.Track;
995 break;
997 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
998 if (io == -1)
1000 TRACE("opened or no_media (%s)!\n", strerror(errno));
1001 CDROM_ClearCacheEntry(dev);
1002 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1003 goto end;
1006 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
1008 switch (sc.header.audio_status) {
1009 case CD_AS_AUDIO_INVALID:
1010 CDROM_ClearCacheEntry(dev);
1011 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
1012 break;
1013 case CD_AS_NO_STATUS:
1014 CDROM_ClearCacheEntry(dev);
1015 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1016 break;
1017 case CD_AS_PLAY_IN_PROGRESS:
1018 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
1019 break;
1020 case CD_AS_PLAY_PAUSED:
1021 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
1022 break;
1023 case CD_AS_PLAY_COMPLETED:
1024 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
1025 break;
1026 case CD_AS_PLAY_ERROR:
1027 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
1028 break;
1029 default:
1030 TRACE("status=%02X !\n", sc.header.audio_status);
1032 switch (fmt->Format)
1034 case IOCTL_CDROM_CURRENT_POSITION:
1035 RtlEnterCriticalSection( &cache_section );
1036 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
1037 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1038 data->CurrentPosition.Control = sc.what.position.control;
1039 data->CurrentPosition.ADR = sc.what.position.addr_type;
1040 data->CurrentPosition.TrackNumber = sc.what.position.track_number;
1041 data->CurrentPosition.IndexNumber = sc.what.position.index_number;
1043 data->CurrentPosition.AbsoluteAddress[0] = 0;
1044 data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
1045 data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
1046 data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
1047 data->CurrentPosition.TrackRelativeAddress[0] = 0;
1048 data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
1049 data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
1050 data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
1051 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
1053 else { /* not playing */
1054 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
1055 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
1057 RtlLeaveCriticalSection( &cache_section );
1058 break;
1059 case IOCTL_CDROM_MEDIA_CATALOG:
1060 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
1061 data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
1062 memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
1063 break;
1064 case IOCTL_CDROM_TRACK_ISRC:
1065 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
1066 data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
1067 memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
1068 break;
1071 end:
1072 ret = CDROM_GetStatusCode(io);
1073 #elif defined(__APPLE__)
1074 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
1075 int io = 0;
1076 union
1078 dk_cd_read_mcn_t mcn;
1079 dk_cd_read_isrc_t isrc;
1080 } ioc;
1081 /* We need IOCDAudioControl for IOCTL_CDROM_CURRENT_POSITION */
1082 if (fmt->Format == IOCTL_CDROM_CURRENT_POSITION)
1084 FIXME("NIY\n");
1085 return STATUS_NOT_SUPPORTED;
1087 /* No IOCDAudioControl support; just set the audio status to none */
1088 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1089 switch(fmt->Format)
1091 case IOCTL_CDROM_MEDIA_CATALOG:
1092 if ((io = ioctl(fd, DKIOCCDREADMCN, &ioc.mcn)) == -1) break;
1093 memcpy(data->MediaCatalog.MediaCatalog, ioc.mcn.mcn, kCDMCNMaxLength);
1094 data->MediaCatalog.Mcval = 1;
1095 break;
1096 case IOCTL_CDROM_TRACK_ISRC:
1097 ioc.isrc.track = fmt->Track;
1098 if ((io = ioctl(fd, DKIOCCDREADISRC, &ioc.isrc)) == -1) break;
1099 memcpy(data->TrackIsrc.TrackIsrc, ioc.isrc.isrc, kCDISRCMaxLength);
1100 data->TrackIsrc.Tcval = 1;
1101 data->TrackIsrc.Track = ioc.isrc.track;
1103 ret = CDROM_GetStatusCode(io);
1104 #endif
1105 return ret;
1108 /******************************************************************
1109 * CDROM_Verify
1110 * Implements: IOCTL_STORAGE_CHECK_VERIFY
1111 * IOCTL_CDROM_CHECK_VERIFY
1112 * IOCTL_DISK_CHECK_VERIFY
1115 static NTSTATUS CDROM_Verify(int dev, int fd)
1117 #if defined(linux)
1118 int ret;
1120 ret = ioctl(fd, CDROM_DRIVE_STATUS, NULL);
1121 if(ret == -1) {
1122 TRACE("ioctl CDROM_DRIVE_STATUS failed(%s)!\n", strerror(errno));
1123 return CDROM_GetStatusCode(ret);
1126 if(ret == CDS_DISC_OK)
1127 return STATUS_SUCCESS;
1128 else
1129 return STATUS_NO_MEDIA_IN_DEVICE;
1130 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1131 int ret;
1132 ret = ioctl(fd, CDIOCSTART, NULL);
1133 if(ret == 0)
1134 return STATUS_SUCCESS;
1135 else
1136 return STATUS_NO_MEDIA_IN_DEVICE;
1137 #elif defined(__APPLE__)
1138 /* At this point, we know that we have media, because in Mac OS X, the
1139 * device file is only created when media is present. */
1140 return STATUS_SUCCESS;
1141 #else
1142 FIXME("not supported on this O/S\n");
1143 return STATUS_NOT_SUPPORTED;
1144 #endif
1147 /******************************************************************
1148 * CDROM_PlayAudioMSF
1152 static NTSTATUS CDROM_PlayAudioMSF(int fd, const CDROM_PLAY_AUDIO_MSF* audio_msf)
1154 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1155 #ifdef linux
1156 struct cdrom_msf msf;
1157 int io;
1159 msf.cdmsf_min0 = audio_msf->StartingM;
1160 msf.cdmsf_sec0 = audio_msf->StartingS;
1161 msf.cdmsf_frame0 = audio_msf->StartingF;
1162 msf.cdmsf_min1 = audio_msf->EndingM;
1163 msf.cdmsf_sec1 = audio_msf->EndingS;
1164 msf.cdmsf_frame1 = audio_msf->EndingF;
1166 io = ioctl(fd, CDROMSTART);
1167 if (io == -1)
1169 WARN("motor doesn't start !\n");
1170 goto end;
1172 io = ioctl(fd, CDROMPLAYMSF, &msf);
1173 if (io == -1)
1175 WARN("device doesn't play !\n");
1176 goto end;
1178 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1179 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
1180 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
1181 end:
1182 ret = CDROM_GetStatusCode(io);
1183 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1184 struct ioc_play_msf msf;
1185 int io;
1187 msf.start_m = audio_msf->StartingM;
1188 msf.start_s = audio_msf->StartingS;
1189 msf.start_f = audio_msf->StartingF;
1190 msf.end_m = audio_msf->EndingM;
1191 msf.end_s = audio_msf->EndingS;
1192 msf.end_f = audio_msf->EndingF;
1194 io = ioctl(fd, CDIOCSTART, NULL);
1195 if (io == -1)
1197 WARN("motor doesn't start !\n");
1198 goto end;
1200 io = ioctl(fd, CDIOCPLAYMSF, &msf);
1201 if (io == -1)
1203 WARN("device doesn't play !\n");
1204 goto end;
1206 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1207 msf.start_m, msf.start_s, msf.start_f,
1208 msf.end_m, msf.end_s, msf.end_f);
1209 end:
1210 ret = CDROM_GetStatusCode(io);
1211 #endif
1212 return ret;
1215 /******************************************************************
1216 * CDROM_SeekAudioMSF
1220 static NTSTATUS CDROM_SeekAudioMSF(int dev, int fd, const CDROM_SEEK_AUDIO_MSF* audio_msf)
1222 CDROM_TOC toc;
1223 int i, io, frame;
1224 SUB_Q_CURRENT_POSITION *cp;
1225 #if defined(linux)
1226 struct cdrom_msf0 msf;
1227 struct cdrom_subchnl sc;
1228 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1229 struct ioc_play_msf msf;
1230 struct ioc_read_subchannel read_sc;
1231 struct cd_sub_channel_info sc;
1232 int final_frame;
1233 #endif
1235 /* Use the information on the TOC to compute the new current
1236 * position, which is shadowed on the cache. [Portable]. */
1237 frame = FRAME_OF_MSF(*audio_msf);
1239 if ((io = CDROM_ReadTOC(dev, fd, &toc)) != 0) return io;
1241 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
1242 if (FRAME_OF_TOC(toc,i)>frame) break;
1243 if (i <= toc.FirstTrack || i > toc.LastTrack+1)
1244 return STATUS_INVALID_PARAMETER;
1245 i--;
1246 RtlEnterCriticalSection( &cache_section );
1247 cp = &cdrom_cache[dev].CurrentPosition;
1248 cp->FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1249 cp->Control = toc.TrackData[i-toc.FirstTrack].Control;
1250 cp->ADR = toc.TrackData[i-toc.FirstTrack].Adr;
1251 cp->TrackNumber = toc.TrackData[i-toc.FirstTrack].TrackNumber;
1252 cp->IndexNumber = 0; /* FIXME: where do they keep these? */
1253 cp->AbsoluteAddress[0] = 0;
1254 cp->AbsoluteAddress[1] = toc.TrackData[i-toc.FirstTrack].Address[1];
1255 cp->AbsoluteAddress[2] = toc.TrackData[i-toc.FirstTrack].Address[2];
1256 cp->AbsoluteAddress[3] = toc.TrackData[i-toc.FirstTrack].Address[3];
1257 frame -= FRAME_OF_TOC(toc,i);
1258 cp->TrackRelativeAddress[0] = 0;
1259 MSF_OF_FRAME(cp->TrackRelativeAddress[1], frame);
1260 RtlLeaveCriticalSection( &cache_section );
1262 /* If playing, then issue a seek command, otherwise do nothing */
1263 #ifdef linux
1264 sc.cdsc_format = CDROM_MSF;
1266 io = ioctl(fd, CDROMSUBCHNL, &sc);
1267 if (io == -1)
1269 TRACE("opened or no_media (%s)!\n", strerror(errno));
1270 CDROM_ClearCacheEntry(dev);
1271 return CDROM_GetStatusCode(io);
1273 if (sc.cdsc_audiostatus==CDROM_AUDIO_PLAY)
1275 msf.minute = audio_msf->M;
1276 msf.second = audio_msf->S;
1277 msf.frame = audio_msf->F;
1278 return CDROM_GetStatusCode(ioctl(fd, CDROMSEEK, &msf));
1280 return STATUS_SUCCESS;
1281 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1282 read_sc.address_format = CD_MSF_FORMAT;
1283 read_sc.track = 0;
1284 read_sc.data_len = sizeof(sc);
1285 read_sc.data = &sc;
1286 read_sc.data_format = CD_CURRENT_POSITION;
1288 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1289 if (io == -1)
1291 TRACE("opened or no_media (%s)!\n", strerror(errno));
1292 CDROM_ClearCacheEntry(dev);
1293 return CDROM_GetStatusCode(io);
1295 if (sc.header.audio_status==CD_AS_PLAY_IN_PROGRESS)
1298 msf.start_m = audio_msf->M;
1299 msf.start_s = audio_msf->S;
1300 msf.start_f = audio_msf->F;
1301 final_frame = FRAME_OF_TOC(toc,toc.LastTrack+1)-1;
1302 MSF_OF_FRAME(msf.end_m, final_frame);
1304 return CDROM_GetStatusCode(ioctl(fd, CDIOCPLAYMSF, &msf));
1306 return STATUS_SUCCESS;
1307 #else
1308 FIXME("not supported on this O/S\n");
1309 return STATUS_NOT_SUPPORTED;
1310 #endif
1313 /******************************************************************
1314 * CDROM_PauseAudio
1318 static NTSTATUS CDROM_PauseAudio(int fd)
1320 #if defined(linux)
1321 return CDROM_GetStatusCode(ioctl(fd, CDROMPAUSE));
1322 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1323 return CDROM_GetStatusCode(ioctl(fd, CDIOCPAUSE, NULL));
1324 #else
1325 FIXME(": not supported on this O/S\n");
1326 return STATUS_NOT_SUPPORTED;
1327 #endif
1330 /******************************************************************
1331 * CDROM_ResumeAudio
1335 static NTSTATUS CDROM_ResumeAudio(int fd)
1337 #if defined(linux)
1338 return CDROM_GetStatusCode(ioctl(fd, CDROMRESUME));
1339 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1340 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESUME, NULL));
1341 #else
1342 FIXME("not supported on this O/S\n");
1343 return STATUS_NOT_SUPPORTED;
1344 #endif
1347 /******************************************************************
1348 * CDROM_StopAudio
1352 static NTSTATUS CDROM_StopAudio(int fd)
1354 #if defined(linux)
1355 return CDROM_GetStatusCode(ioctl(fd, CDROMSTOP));
1356 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1357 return CDROM_GetStatusCode(ioctl(fd, CDIOCSTOP, NULL));
1358 #else
1359 FIXME("not supported on this O/S\n");
1360 return STATUS_NOT_SUPPORTED;
1361 #endif
1364 /******************************************************************
1365 * CDROM_GetVolume
1369 static NTSTATUS CDROM_GetVolume(int fd, VOLUME_CONTROL* vc)
1371 #if defined(linux)
1372 struct cdrom_volctrl volc;
1373 int io;
1375 io = ioctl(fd, CDROMVOLREAD, &volc);
1376 if (io != -1)
1378 vc->PortVolume[0] = volc.channel0;
1379 vc->PortVolume[1] = volc.channel1;
1380 vc->PortVolume[2] = volc.channel2;
1381 vc->PortVolume[3] = volc.channel3;
1383 return CDROM_GetStatusCode(io);
1384 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1385 struct ioc_vol volc;
1386 int io;
1388 io = ioctl(fd, CDIOCGETVOL, &volc);
1389 if (io != -1)
1391 vc->PortVolume[0] = volc.vol[0];
1392 vc->PortVolume[1] = volc.vol[1];
1393 vc->PortVolume[2] = volc.vol[2];
1394 vc->PortVolume[3] = volc.vol[3];
1396 return CDROM_GetStatusCode(io);
1397 #else
1398 FIXME("not supported on this O/S\n");
1399 return STATUS_NOT_SUPPORTED;
1400 #endif
1403 /******************************************************************
1404 * CDROM_SetVolume
1408 static NTSTATUS CDROM_SetVolume(int fd, const VOLUME_CONTROL* vc)
1410 #if defined(linux)
1411 struct cdrom_volctrl volc;
1413 volc.channel0 = vc->PortVolume[0];
1414 volc.channel1 = vc->PortVolume[1];
1415 volc.channel2 = vc->PortVolume[2];
1416 volc.channel3 = vc->PortVolume[3];
1418 return CDROM_GetStatusCode(ioctl(fd, CDROMVOLCTRL, &volc));
1419 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1420 struct ioc_vol volc;
1422 volc.vol[0] = vc->PortVolume[0];
1423 volc.vol[1] = vc->PortVolume[1];
1424 volc.vol[2] = vc->PortVolume[2];
1425 volc.vol[3] = vc->PortVolume[3];
1427 return CDROM_GetStatusCode(ioctl(fd, CDIOCSETVOL, &volc));
1428 #else
1429 FIXME(": not supported on this O/S\n");
1430 return STATUS_NOT_SUPPORTED;
1431 #endif
1434 /******************************************************************
1435 * CDROM_RawRead
1437 * Some features of this IOCTL are rather poorly documented and
1438 * not really intuitive either:
1440 * 1. Although the DiskOffset parameter is meant to be a
1441 * byte offset into the disk, it is in fact the sector
1442 * number multiplied by 2048 regardless of the actual
1443 * sector size.
1445 * 2. The least significant 11 bits of DiskOffset are ignored.
1447 * 3. The TrackMode parameter has no effect on the sector
1448 * size. The entire raw sector (i.e. 2352 bytes of data)
1449 * is always returned. IMO the TrackMode is only used
1450 * to check the correct sector type.
1453 static NTSTATUS CDROM_RawRead(int fd, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
1455 int ret = STATUS_NOT_SUPPORTED;
1456 int io = -1;
1457 #ifdef __APPLE__
1458 dk_cd_read_t cdrd;
1459 #endif
1461 TRACE("RAW_READ_INFO: DiskOffset=%i,%i SectorCount=%i TrackMode=%i\n buffer=%p len=%i sz=%p\n",
1462 raw->DiskOffset.u.HighPart, raw->DiskOffset.u.LowPart, raw->SectorCount, raw->TrackMode, buffer, len, sz);
1464 if (len < raw->SectorCount * 2352) return STATUS_BUFFER_TOO_SMALL;
1466 #if defined(linux)
1467 if (raw->DiskOffset.u.HighPart & ~2047) {
1468 WARN("DiskOffset points to a sector >= 2**32\n");
1469 return ret;
1472 switch (raw->TrackMode)
1474 case YellowMode2:
1475 case XAForm2:
1477 DWORD lba = raw->DiskOffset.QuadPart >> 11;
1478 struct cdrom_msf* msf;
1479 PBYTE *bp; /* current buffer pointer */
1480 DWORD i;
1482 if ((lba + raw->SectorCount) >
1483 ((1 << 8*sizeof(msf->cdmsf_min0)) * CD_SECS * CD_FRAMES
1484 - CD_MSF_OFFSET)) {
1485 WARN("DiskOffset not accessible with MSF\n");
1486 return ret;
1489 /* Linux reads only one sector at a time.
1490 * ioctl CDROMREADRAW takes struct cdrom_msf as an argument
1491 * on the contrary to what header comments state.
1493 lba += CD_MSF_OFFSET;
1494 for (i = 0, bp = buffer; i < raw->SectorCount;
1495 i++, lba++, bp += 2352)
1497 msf = (struct cdrom_msf*)bp;
1498 msf->cdmsf_min0 = lba / CD_FRAMES / CD_SECS;
1499 msf->cdmsf_sec0 = lba / CD_FRAMES % CD_SECS;
1500 msf->cdmsf_frame0 = lba % CD_FRAMES;
1501 io = ioctl(fd, CDROMREADRAW, msf);
1502 if (io != 0)
1504 *sz = 2352 * i;
1505 return CDROM_GetStatusCode(io);
1508 break;
1511 case CDDA:
1513 struct cdrom_read_audio cdra;
1515 cdra.addr.lba = raw->DiskOffset.QuadPart >> 11;
1516 TRACE("reading at %u\n", cdra.addr.lba);
1517 cdra.addr_format = CDROM_LBA;
1518 cdra.nframes = raw->SectorCount;
1519 cdra.buf = buffer;
1520 io = ioctl(fd, CDROMREADAUDIO, &cdra);
1521 break;
1524 default:
1525 FIXME("NIY: %d\n", raw->TrackMode);
1526 return STATUS_INVALID_PARAMETER;
1528 #elif defined(__APPLE__)
1529 /* Mac OS lets us read multiple parts of the sector at a time.
1530 * We can read all the sectors in at once, unlike Linux.
1532 memset(&cdrd, 0, sizeof(cdrd));
1533 cdrd.offset = (raw->DiskOffset.QuadPart >> 11) * kCDSectorSizeWhole;
1534 cdrd.buffer = buffer;
1535 cdrd.bufferLength = raw->SectorCount * kCDSectorSizeWhole;
1536 switch (raw->TrackMode)
1538 case YellowMode2:
1539 cdrd.sectorType = kCDSectorTypeMode2;
1540 cdrd.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader | kCDSectorAreaUser;
1541 break;
1543 case XAForm2:
1544 cdrd.sectorType = kCDSectorTypeMode2Form2;
1545 cdrd.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader | kCDSectorAreaSubHeader | kCDSectorAreaUser;
1546 break;
1548 case CDDA:
1549 cdrd.sectorType = kCDSectorTypeCDDA;
1550 cdrd.sectorArea = kCDSectorAreaUser;
1551 break;
1553 default:
1554 FIXME("NIY: %d\n", raw->TrackMode);
1555 return STATUS_INVALID_PARAMETER;
1557 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1558 if (io != 0)
1560 *sz = cdrd.bufferLength;
1561 return CDROM_GetStatusCode(io);
1563 #else
1564 switch (raw->TrackMode)
1566 case YellowMode2:
1567 FIXME("YellowMode2: NIY\n");
1568 return ret;
1569 case XAForm2:
1570 FIXME("XAForm2: NIY\n");
1571 return ret;
1572 case CDDA:
1573 FIXME("CDDA: NIY\n");
1574 return ret;
1575 default:
1576 FIXME("NIY: %d\n", raw->TrackMode);
1577 return STATUS_INVALID_PARAMETER;
1579 #endif
1581 *sz = 2352 * raw->SectorCount;
1582 ret = CDROM_GetStatusCode(io);
1583 return ret;
1586 /******************************************************************
1587 * CDROM_ScsiPassThroughDirect
1588 * Implements IOCTL_SCSI_PASS_THROUGH_DIRECT
1591 static NTSTATUS CDROM_ScsiPassThroughDirect(int fd, PSCSI_PASS_THROUGH_DIRECT pPacket)
1593 int ret = STATUS_NOT_SUPPORTED;
1594 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1595 sg_io_hdr_t cmd;
1596 int io;
1597 #elif defined HAVE_SCSIREQ_T_CMD
1598 scsireq_t cmd;
1599 int io;
1600 #elif defined __APPLE__
1601 dk_scsi_command_t cmd;
1602 int io;
1603 #endif
1605 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1606 return STATUS_BUFFER_TOO_SMALL;
1608 if (pPacket->CdbLength > 16)
1609 return STATUS_INVALID_PARAMETER;
1611 #ifdef SENSEBUFLEN
1612 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1613 return STATUS_INVALID_PARAMETER;
1614 #elif defined HAVE_REQUEST_SENSE
1615 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1616 return STATUS_INVALID_PARAMETER;
1617 #endif
1619 if (pPacket->DataTransferLength > 0 && !pPacket->DataBuffer)
1620 return STATUS_INVALID_PARAMETER;
1622 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1623 RtlZeroMemory(&cmd, sizeof(cmd));
1625 cmd.interface_id = 'S';
1626 cmd.cmd_len = pPacket->CdbLength;
1627 cmd.mx_sb_len = pPacket->SenseInfoLength;
1628 cmd.dxfer_len = pPacket->DataTransferLength;
1629 cmd.dxferp = pPacket->DataBuffer;
1630 cmd.cmdp = pPacket->Cdb;
1631 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1632 cmd.timeout = pPacket->TimeOutValue*1000;
1634 switch (pPacket->DataIn)
1636 case SCSI_IOCTL_DATA_IN:
1637 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1638 break;
1639 case SCSI_IOCTL_DATA_OUT:
1640 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1641 break;
1642 case SCSI_IOCTL_DATA_UNSPECIFIED:
1643 cmd.dxfer_direction = SG_DXFER_NONE;
1644 break;
1645 default:
1646 return STATUS_INVALID_PARAMETER;
1649 io = ioctl(fd, SG_IO, &cmd);
1651 pPacket->ScsiStatus = cmd.status;
1652 pPacket->DataTransferLength = cmd.resid;
1653 pPacket->SenseInfoLength = cmd.sb_len_wr;
1655 ret = CDROM_GetStatusCode(io);
1657 #elif defined HAVE_SCSIREQ_T_CMD
1659 memset(&cmd, 0, sizeof(cmd));
1660 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1662 cmd.cmdlen = pPacket->CdbLength;
1663 cmd.databuf = pPacket->DataBuffer;
1664 cmd.datalen = pPacket->DataTransferLength;
1665 cmd.senselen = pPacket->SenseInfoLength;
1666 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1668 switch (pPacket->DataIn)
1670 case SCSI_IOCTL_DATA_OUT:
1671 cmd.flags |= SCCMD_WRITE;
1672 break;
1673 case SCSI_IOCTL_DATA_IN:
1674 cmd.flags |= SCCMD_READ;
1675 break;
1676 case SCSI_IOCTL_DATA_UNSPECIFIED:
1677 cmd.flags = 0;
1678 break;
1679 default:
1680 return STATUS_INVALID_PARAMETER;
1683 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1685 switch (cmd.retsts)
1687 case SCCMD_OK: break;
1688 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1689 break;
1690 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1691 break;
1692 case SCCMD_SENSE: break;
1693 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1694 break;
1697 if (pPacket->SenseInfoLength != 0)
1699 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1700 cmd.sense, pPacket->SenseInfoLength);
1703 pPacket->ScsiStatus = cmd.status;
1705 ret = CDROM_GetStatusCode(io);
1707 #elif defined(__APPLE__)
1709 memset(&cmd, 0, sizeof(cmd));
1710 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1712 cmd.cdbSize = pPacket->CdbLength;
1713 cmd.buffer = pPacket->DataBuffer;
1714 cmd.bufferSize = pPacket->DataTransferLength;
1715 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1716 cmd.senseLen = pPacket->SenseInfoLength;
1717 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1719 switch (pPacket->DataIn)
1721 case SCSI_IOCTL_DATA_OUT:
1722 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
1723 break;
1724 case SCSI_IOCTL_DATA_IN:
1725 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
1726 break;
1727 case SCSI_IOCTL_DATA_UNSPECIFIED:
1728 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
1729 break;
1730 default:
1731 return STATUS_INVALID_PARAMETER;
1734 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
1736 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
1738 /* Command failed */
1739 switch (cmd.status)
1741 case kSCSITaskStatus_TaskTimeoutOccurred: return STATUS_TIMEOUT;
1742 break;
1743 case kSCSITaskStatus_ProtocolTimeoutOccurred: return STATUS_IO_TIMEOUT;
1744 break;
1745 case kSCSITaskStatus_DeviceNotResponding: return STATUS_DEVICE_BUSY;
1746 break;
1747 case kSCSITaskStatus_DeviceNotPresent:
1748 return STATUS_NO_SUCH_DEVICE;
1749 break;
1750 case kSCSITaskStatus_DeliveryFailure:
1751 return STATUS_DEVICE_PROTOCOL_ERROR;
1752 break;
1753 case kSCSITaskStatus_No_Status:
1754 default:
1755 return STATUS_UNSUCCESSFUL;
1756 break;
1760 if (cmd.status != kSCSITaskStatus_No_Status)
1761 pPacket->ScsiStatus = cmd.status;
1763 ret = CDROM_GetStatusCode(io);
1764 #endif
1765 return ret;
1768 /******************************************************************
1769 * CDROM_ScsiPassThrough
1770 * Implements IOCTL_SCSI_PASS_THROUGH
1773 static NTSTATUS CDROM_ScsiPassThrough(int fd, PSCSI_PASS_THROUGH pPacket)
1775 int ret = STATUS_NOT_SUPPORTED;
1776 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1777 sg_io_hdr_t cmd;
1778 int io;
1779 #elif defined HAVE_SCSIREQ_T_CMD
1780 scsireq_t cmd;
1781 int io;
1782 #elif defined __APPLE__
1783 dk_scsi_command_t cmd;
1784 int io;
1785 #endif
1787 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1788 return STATUS_BUFFER_TOO_SMALL;
1790 if (pPacket->CdbLength > 16)
1791 return STATUS_INVALID_PARAMETER;
1793 #ifdef SENSEBUFLEN
1794 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1795 return STATUS_INVALID_PARAMETER;
1796 #elif defined HAVE_REQUEST_SENSE
1797 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1798 return STATUS_INVALID_PARAMETER;
1799 #endif
1801 if (pPacket->DataTransferLength > 0 && pPacket->DataBufferOffset < sizeof(SCSI_PASS_THROUGH))
1802 return STATUS_INVALID_PARAMETER;
1804 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1805 RtlZeroMemory(&cmd, sizeof(cmd));
1807 cmd.interface_id = 'S';
1808 cmd.dxfer_len = pPacket->DataTransferLength;
1809 cmd.dxferp = (char*)pPacket + pPacket->DataBufferOffset;
1810 cmd.cmd_len = pPacket->CdbLength;
1811 cmd.cmdp = pPacket->Cdb;
1812 cmd.mx_sb_len = pPacket->SenseInfoLength;
1813 cmd.timeout = pPacket->TimeOutValue*1000;
1815 if(cmd.mx_sb_len > 0)
1816 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1818 switch (pPacket->DataIn)
1820 case SCSI_IOCTL_DATA_IN:
1821 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1822 break;
1823 case SCSI_IOCTL_DATA_OUT:
1824 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1825 break;
1826 case SCSI_IOCTL_DATA_UNSPECIFIED:
1827 cmd.dxfer_direction = SG_DXFER_NONE;
1828 break;
1829 default:
1830 return STATUS_INVALID_PARAMETER;
1833 io = ioctl(fd, SG_IO, &cmd);
1835 pPacket->ScsiStatus = cmd.status;
1836 pPacket->DataTransferLength = cmd.resid;
1837 pPacket->SenseInfoLength = cmd.sb_len_wr;
1839 ret = CDROM_GetStatusCode(io);
1841 #elif defined HAVE_SCSIREQ_T_CMD
1843 memset(&cmd, 0, sizeof(cmd));
1844 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1846 if ( pPacket->DataBufferOffset > 0x1000 )
1848 cmd.databuf = (void*)pPacket->DataBufferOffset;
1850 else
1852 cmd.databuf = (char*)pPacket + pPacket->DataBufferOffset;
1855 cmd.cmdlen = pPacket->CdbLength;
1856 cmd.datalen = pPacket->DataTransferLength;
1857 cmd.senselen = pPacket->SenseInfoLength;
1858 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1860 switch (pPacket->DataIn)
1862 case SCSI_IOCTL_DATA_OUT:
1863 cmd.flags |= SCCMD_WRITE;
1864 break;
1865 case SCSI_IOCTL_DATA_IN:
1866 cmd.flags |= SCCMD_READ;
1867 break;
1868 case SCSI_IOCTL_DATA_UNSPECIFIED:
1869 cmd.flags = 0;
1870 break;
1871 default:
1872 return STATUS_INVALID_PARAMETER;
1875 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1877 switch (cmd.retsts)
1879 case SCCMD_OK: break;
1880 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1881 break;
1882 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1883 break;
1884 case SCCMD_SENSE: break;
1885 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1886 break;
1889 if (pPacket->SenseInfoLength != 0)
1891 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1892 cmd.sense, pPacket->SenseInfoLength);
1895 pPacket->ScsiStatus = cmd.status;
1897 ret = CDROM_GetStatusCode(io);
1899 #elif defined(__APPLE__)
1901 memset(&cmd, 0, sizeof(cmd));
1902 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1904 cmd.cdbSize = pPacket->CdbLength;
1905 cmd.buffer = (char*)pPacket + pPacket->DataBufferOffset;
1906 cmd.bufferSize = pPacket->DataTransferLength;
1907 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1908 cmd.senseLen = pPacket->SenseInfoLength;
1909 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1911 switch (pPacket->DataIn)
1913 case SCSI_IOCTL_DATA_OUT:
1914 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
1915 break;
1916 case SCSI_IOCTL_DATA_IN:
1917 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
1918 break;
1919 case SCSI_IOCTL_DATA_UNSPECIFIED:
1920 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
1921 break;
1922 default:
1923 return STATUS_INVALID_PARAMETER;
1926 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
1928 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
1930 /* Command failed */
1931 switch (cmd.status)
1933 case kSCSITaskStatus_TaskTimeoutOccurred:
1934 return STATUS_TIMEOUT;
1935 break;
1936 case kSCSITaskStatus_ProtocolTimeoutOccurred:
1937 return STATUS_IO_TIMEOUT;
1938 break;
1939 case kSCSITaskStatus_DeviceNotResponding:
1940 return STATUS_DEVICE_BUSY;
1941 break;
1942 case kSCSITaskStatus_DeviceNotPresent:
1943 return STATUS_NO_SUCH_DEVICE;
1944 break;
1945 case kSCSITaskStatus_DeliveryFailure:
1946 return STATUS_DEVICE_PROTOCOL_ERROR;
1947 break;
1948 case kSCSITaskStatus_No_Status:
1949 default:
1950 return STATUS_UNSUCCESSFUL;
1951 break;
1955 if (cmd.status != kSCSITaskStatus_No_Status)
1956 pPacket->ScsiStatus = cmd.status;
1958 ret = CDROM_GetStatusCode(io);
1959 #endif
1960 return ret;
1963 /******************************************************************
1964 * CDROM_ScsiGetCaps
1968 static NTSTATUS CDROM_ScsiGetCaps(int fd, PIO_SCSI_CAPABILITIES caps)
1970 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
1972 #ifdef SG_SCATTER_SZ
1973 caps->Length = sizeof(*caps);
1974 caps->MaximumTransferLength = SG_SCATTER_SZ; /* FIXME */
1975 caps->MaximumPhysicalPages = SG_SCATTER_SZ / getpagesize();
1976 caps->SupportedAsynchronousEvents = TRUE;
1977 caps->AlignmentMask = getpagesize();
1978 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
1979 caps->AdapterScansDown = FALSE; /* FIXME ? */
1980 caps->AdapterUsesPio = FALSE; /* FIXME ? */
1981 ret = STATUS_SUCCESS;
1982 #elif defined __APPLE__
1983 uint64_t bytesr, bytesw, align;
1984 int io = ioctl(fd, DKIOCGETMAXBYTECOUNTREAD, &bytesr);
1985 if (io != 0) return CDROM_GetStatusCode(io);
1986 io = ioctl(fd, DKIOCGETMAXBYTECOUNTWRITE, &bytesw);
1987 if (io != 0) return CDROM_GetStatusCode(io);
1988 io = ioctl(fd, DKIOCGETMINSEGMENTALIGNMENTBYTECOUNT, &align);
1989 if (io != 0) return CDROM_GetStatusCode(io);
1990 caps->Length = sizeof(*caps);
1991 caps->MaximumTransferLength = bytesr < bytesw ? bytesr : bytesw;
1992 caps->MaximumPhysicalPages = caps->MaximumTransferLength / getpagesize();
1993 caps->SupportedAsynchronousEvents = TRUE;
1994 caps->AlignmentMask = align-1;
1995 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
1996 caps->AdapterScansDown = FALSE; /* FIXME ? */
1997 caps->AdapterUsesPio = FALSE; /* FIXME ? */
1998 ret = STATUS_SUCCESS;
1999 #else
2000 FIXME("Unimplemented\n");
2001 #endif
2002 return ret;
2005 /******************************************************************
2006 * CDROM_GetAddress
2008 * implements IOCTL_SCSI_GET_ADDRESS
2010 static NTSTATUS CDROM_GetAddress(int fd, SCSI_ADDRESS* address)
2012 UCHAR portnum, busid, targetid, lun;
2014 address->Length = sizeof(SCSI_ADDRESS);
2015 if ( ! CDROM_GetInterfaceInfo(fd, &portnum, &busid, &targetid, &lun))
2016 return STATUS_NOT_SUPPORTED;
2018 address->PortNumber = portnum; /* primary=0 secondary=1 for ide */
2019 address->PathId = busid; /* always 0 for ide */
2020 address->TargetId = targetid; /* master=0 slave=1 for ide */
2021 address->Lun = lun;
2022 return STATUS_SUCCESS;
2025 /******************************************************************
2026 * DVD_StartSession
2030 static NTSTATUS DVD_StartSession(int fd, const DVD_SESSION_ID *sid_in, PDVD_SESSION_ID sid_out)
2032 #if defined(linux)
2033 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2034 dvd_authinfo auth_info;
2036 memset( &auth_info, 0, sizeof( auth_info ) );
2037 auth_info.type = DVD_LU_SEND_AGID;
2038 if (sid_in) auth_info.lsa.agid = *(const int*)sid_in; /* ?*/
2040 TRACE("fd 0x%08x\n",fd);
2041 ret =CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2042 *sid_out = auth_info.lsa.agid;
2043 return ret;
2044 #elif defined(__APPLE__)
2045 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2046 dk_dvd_report_key_t dvdrk;
2047 DVDAuthenticationGrantIDInfo agid_info;
2049 dvdrk.format = kDVDKeyFormatAGID_CSS;
2050 dvdrk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2051 if(sid_in) dvdrk.grantID = *(uint8_t*)sid_in; /* ? */
2052 dvdrk.bufferLength = sizeof(DVDAuthenticationGrantIDInfo);
2053 dvdrk.buffer = &agid_info;
2055 ret = CDROM_GetStatusCode(ioctl(fd, DKIOCDVDREPORTKEY, &dvdrk));
2056 *sid_out = agid_info.grantID;
2057 return ret;
2058 #else
2059 FIXME("not supported on this O/S\n");
2060 return STATUS_NOT_SUPPORTED;
2061 #endif
2064 /******************************************************************
2065 * DVD_EndSession
2069 static NTSTATUS DVD_EndSession(int fd, const DVD_SESSION_ID *sid)
2071 #if defined(linux)
2072 dvd_authinfo auth_info;
2074 memset( &auth_info, 0, sizeof( auth_info ) );
2075 auth_info.type = DVD_INVALIDATE_AGID;
2076 auth_info.lsa.agid = *(const int*)sid;
2078 TRACE("\n");
2079 return CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2080 #elif defined(__APPLE__)
2081 dk_dvd_send_key_t dvdsk;
2083 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2084 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2085 dvdsk.grantID = (uint8_t)*sid;
2087 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2088 #else
2089 FIXME("not supported on this O/S\n");
2090 return STATUS_NOT_SUPPORTED;
2091 #endif
2094 /******************************************************************
2095 * DVD_SendKey
2099 static NTSTATUS DVD_SendKey(int fd, const DVD_COPY_PROTECT_KEY *key)
2101 #if defined(linux)
2102 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2103 dvd_authinfo auth_info;
2105 memset( &auth_info, 0, sizeof( auth_info ) );
2106 switch (key->KeyType)
2108 case DvdChallengeKey:
2109 auth_info.type = DVD_HOST_SEND_CHALLENGE;
2110 auth_info.hsc.agid = (int)key->SessionId;
2111 TRACE("DvdChallengeKey ioc 0x%x\n", DVD_AUTH );
2112 memcpy( auth_info.hsc.chal, key->KeyData, DVD_CHALLENGE_SIZE );
2113 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2114 break;
2115 case DvdBusKey2:
2116 auth_info.type = DVD_HOST_SEND_KEY2;
2117 auth_info.hsk.agid = (int)key->SessionId;
2119 memcpy( auth_info.hsk.key, key->KeyData, DVD_KEY_SIZE );
2121 TRACE("DvdBusKey2\n");
2122 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2123 break;
2125 default:
2126 FIXME("Unknown Keytype 0x%x\n",key->KeyType);
2128 return ret;
2129 #elif defined(__APPLE__)
2130 dk_dvd_send_key_t dvdsk;
2131 union
2133 DVDChallengeKeyInfo chal;
2134 DVDKey2Info key2;
2135 } key_desc;
2137 switch(key->KeyType)
2139 case DvdChallengeKey:
2140 dvdsk.format = kDVDKeyFormatChallengeKey;
2141 dvdsk.bufferLength = sizeof(key_desc.chal);
2142 dvdsk.buffer = &key_desc.chal;
2143 OSWriteBigInt16(key_desc.chal.dataLength, 0, key->KeyLength);
2144 memcpy(key_desc.chal.challengeKeyValue, key->KeyData, key->KeyLength);
2145 break;
2146 case DvdBusKey2:
2147 dvdsk.format = kDVDKeyFormatKey2;
2148 dvdsk.bufferLength = sizeof(key_desc.key2);
2149 dvdsk.buffer = &key_desc.key2;
2150 OSWriteBigInt16(key_desc.key2.dataLength, 0, key->KeyLength);
2151 memcpy(key_desc.key2.key2Value, key->KeyData, key->KeyLength);
2152 break;
2153 case DvdInvalidateAGID:
2154 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2155 break;
2156 case DvdBusKey1:
2157 case DvdTitleKey:
2158 case DvdAsf:
2159 case DvdGetRpcKey:
2160 case DvdDiskKey:
2161 ERR("attempted to write read-only key type 0x%x\n", key->KeyType);
2162 return STATUS_NOT_SUPPORTED;
2163 case DvdSetRpcKey:
2164 FIXME("DvdSetRpcKey NIY\n");
2165 return STATUS_NOT_SUPPORTED;
2166 default:
2167 FIXME("got unknown key type 0x%x\n", key->KeyType);
2168 return STATUS_NOT_SUPPORTED;
2170 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2171 dvdsk.grantID = (uint8_t)key->SessionId;
2173 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2174 #else
2175 FIXME("not supported on this O/S\n");
2176 return STATUS_NOT_SUPPORTED;
2177 #endif
2180 /******************************************************************
2181 * DVD_ReadKey
2185 static NTSTATUS DVD_ReadKey(int fd, PDVD_COPY_PROTECT_KEY key)
2187 #if defined(linux)
2188 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2189 dvd_struct dvd;
2190 dvd_authinfo auth_info;
2192 memset( &dvd, 0, sizeof( dvd_struct ) );
2193 memset( &auth_info, 0, sizeof( auth_info ) );
2194 switch (key->KeyType)
2196 case DvdDiskKey:
2198 dvd.type = DVD_STRUCT_DISCKEY;
2199 dvd.disckey.agid = (int)key->SessionId;
2200 memset( dvd.disckey.value, 0, DVD_DISCKEY_SIZE );
2202 TRACE("DvdDiskKey\n");
2203 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2204 if (ret == STATUS_SUCCESS)
2205 memcpy(key->KeyData,dvd.disckey.value,DVD_DISCKEY_SIZE);
2206 break;
2207 case DvdTitleKey:
2208 auth_info.type = DVD_LU_SEND_TITLE_KEY;
2209 auth_info.lstk.agid = (int)key->SessionId;
2210 auth_info.lstk.lba = (int)(key->Parameters.TitleOffset.QuadPart>>11);
2211 TRACE("DvdTitleKey session %d Quadpart 0x%08lx offset 0x%08x\n",
2212 (int)key->SessionId, (long)key->Parameters.TitleOffset.QuadPart,
2213 auth_info.lstk.lba);
2214 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2215 if (ret == STATUS_SUCCESS)
2216 memcpy(key->KeyData, auth_info.lstk.title_key, DVD_KEY_SIZE );
2217 break;
2218 case DvdChallengeKey:
2220 auth_info.type = DVD_LU_SEND_CHALLENGE;
2221 auth_info.lsc.agid = (int)key->SessionId;
2223 TRACE("DvdChallengeKey\n");
2224 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2225 if (ret == STATUS_SUCCESS)
2226 memcpy( key->KeyData, auth_info.lsc.chal, DVD_CHALLENGE_SIZE );
2227 break;
2228 case DvdAsf:
2229 auth_info.type = DVD_LU_SEND_ASF;
2230 TRACE("DvdAsf\n");
2231 auth_info.lsasf.asf=((PDVD_ASF)key->KeyData)->SuccessFlag;
2232 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2233 ((PDVD_ASF)key->KeyData)->SuccessFlag = auth_info.lsasf.asf;
2234 break;
2235 case DvdBusKey1:
2236 auth_info.type = DVD_LU_SEND_KEY1;
2237 auth_info.lsk.agid = (int)key->SessionId;
2239 TRACE("DvdBusKey1\n");
2240 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2242 if (ret == STATUS_SUCCESS)
2243 memcpy( key->KeyData, auth_info.lsk.key, DVD_KEY_SIZE );
2244 break;
2245 case DvdGetRpcKey:
2246 auth_info.type = DVD_LU_SEND_RPC_STATE;
2248 TRACE("DvdGetRpcKey\n");
2249 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2251 if (ret == STATUS_SUCCESS)
2253 ((PDVD_RPC_KEY)key->KeyData)->TypeCode = auth_info.lrpcs.type;
2254 ((PDVD_RPC_KEY)key->KeyData)->RegionMask = auth_info.lrpcs.region_mask;
2255 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme = auth_info.lrpcs.rpc_scheme;
2256 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable = auth_info.lrpcs.ucca;
2257 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable = auth_info.lrpcs.vra;
2259 break;
2260 default:
2261 FIXME("Unknown keytype 0x%x\n",key->KeyType);
2263 return ret;
2264 #elif defined(__APPLE__)
2265 union
2267 dk_dvd_report_key_t key;
2268 dk_dvd_read_structure_t disk_key;
2269 } ioc;
2270 union
2272 DVDDiscKeyInfo disk_key;
2273 DVDChallengeKeyInfo chal;
2274 DVDKey1Info key1;
2275 DVDTitleKeyInfo title;
2276 DVDAuthenticationSuccessFlagInfo asf;
2277 DVDRegionPlaybackControlInfo rpc;
2278 } desc;
2279 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2281 switch(key->KeyType)
2283 case DvdChallengeKey:
2284 ioc.key.format = kDVDKeyFormatChallengeKey;
2285 ioc.key.grantID = (uint8_t)key->SessionId;
2286 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2287 ioc.key.bufferLength = sizeof(desc.chal);
2288 ioc.key.buffer = &desc.chal;
2289 OSWriteBigInt16(desc.chal.dataLength, 0, key->KeyLength);
2290 break;
2291 case DvdBusKey1:
2292 ioc.key.format = kDVDKeyFormatKey1;
2293 ioc.key.grantID = (uint8_t)key->SessionId;
2294 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2295 ioc.key.bufferLength = sizeof(desc.key1);
2296 ioc.key.buffer = &desc.key1;
2297 OSWriteBigInt16(desc.key1.dataLength, 0, key->KeyLength);
2298 break;
2299 case DvdTitleKey:
2300 ioc.key.format = kDVDKeyFormatTitleKey;
2301 ioc.key.grantID = (uint8_t)key->SessionId;
2302 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2303 ioc.key.bufferLength = sizeof(desc.title);
2304 ioc.key.buffer = &desc.title;
2305 ioc.key.address = (uint32_t)(key->Parameters.TitleOffset.QuadPart>>11);
2306 OSWriteBigInt16(desc.title.dataLength, 0, key->KeyLength);
2307 break;
2308 case DvdAsf:
2309 ioc.key.format = kDVDKeyFormatASF;
2310 ioc.key.grantID = (uint8_t)key->SessionId;
2311 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2312 ioc.key.bufferLength = sizeof(desc.asf);
2313 ioc.key.buffer = &desc.asf;
2314 OSWriteBigInt16(desc.asf.dataLength, 0, key->KeyLength);
2315 break;
2316 case DvdGetRpcKey:
2317 ioc.key.format = kDVDKeyFormatRegionState;
2318 ioc.key.grantID = (uint8_t)key->SessionId;
2319 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2320 ioc.key.bufferLength = sizeof(desc.rpc);
2321 ioc.key.buffer = &desc.rpc;
2322 OSWriteBigInt16(desc.rpc.dataLength, 0, key->KeyLength);
2323 break;
2324 case DvdDiskKey:
2325 ioc.disk_key.format = kDVDStructureFormatDiscKeyInfo;
2326 ioc.disk_key.grantID = (uint8_t)key->SessionId;
2327 ioc.disk_key.bufferLength = sizeof(desc.disk_key);
2328 ioc.disk_key.buffer = &desc.disk_key;
2329 break;
2330 case DvdInvalidateAGID:
2331 ioc.key.format = kDVDKeyFormatAGID_Invalidate;
2332 ioc.key.grantID = (uint8_t)key->SessionId;
2333 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2334 break;
2335 case DvdBusKey2:
2336 case DvdSetRpcKey:
2337 ERR("attempted to read write-only key type 0x%x\n", key->KeyType);
2338 return STATUS_NOT_SUPPORTED;
2339 default:
2340 FIXME("got unknown key type 0x%x\n", key->KeyType);
2341 return STATUS_NOT_SUPPORTED;
2344 ret = CDROM_GetStatusCode(ioctl(fd, (key->KeyType == DvdDiskKey ? DKIOCDVDREADSTRUCTURE : DKIOCDVDREPORTKEY), &ioc));
2346 if (ret == STATUS_SUCCESS)
2348 switch(key->KeyType)
2350 case DvdChallengeKey:
2351 key->KeyLength = OSReadBigInt16(desc.chal.dataLength, 0);
2352 memcpy(key->KeyData, desc.chal.challengeKeyValue, key->KeyLength);
2353 break;
2354 case DvdBusKey1:
2355 key->KeyLength = OSReadBigInt16(desc.key1.dataLength, 0);
2356 memcpy(key->KeyData, desc.key1.key1Value, key->KeyLength);
2357 break;
2358 case DvdTitleKey:
2359 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2360 memcpy(key->KeyData, desc.title.titleKeyValue, key->KeyLength);
2361 key->KeyFlags = 0;
2362 if (desc.title.CPM)
2364 /*key->KeyFlags |= DVD_COPYRIGHTED;*/
2365 if (desc.title.CP_SEC) key->KeyFlags |= DVD_SECTOR_PROTECTED;
2366 /*else key->KeyFlags |= DVD_SECTOR_NOT_PROTECTED;*/
2367 #if 0
2368 switch (desc.title.CGMS)
2370 case 0:
2371 key->KeyFlags |= DVD_CGMS_COPY_PERMITTED;
2372 break;
2373 case 2:
2374 key->KeyFlags |= DVD_CGMS_COPY_ONCE;
2375 break;
2376 case 3:
2377 key->KeyFlags |= DVD_CGMS_NO_COPY;
2378 break;
2380 #endif
2381 } /*else key->KeyFlags |= DVD_NOT_COPYRIGHTED;*/
2382 break;
2383 case DvdAsf:
2384 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2385 ((PDVD_ASF)key->KeyData)->SuccessFlag = desc.asf.successFlag;
2386 break;
2387 case DvdGetRpcKey:
2388 key->KeyLength = OSReadBigInt16(desc.rpc.dataLength, 0);
2389 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable =
2390 desc.rpc.numberUserResets;
2391 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable =
2392 desc.rpc.numberVendorResets;
2393 ((PDVD_RPC_KEY)key->KeyData)->TypeCode =
2394 desc.rpc.typeCode;
2395 ((PDVD_RPC_KEY)key->KeyData)->RegionMask =
2396 desc.rpc.driveRegion;
2397 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme =
2398 desc.rpc.rpcScheme;
2399 case DvdDiskKey:
2400 key->KeyLength = OSReadBigInt16(desc.disk_key.dataLength, 0);
2401 memcpy(key->KeyData, desc.disk_key.discKeyStructures, key->KeyLength);
2402 break;
2403 case DvdBusKey2:
2404 case DvdSetRpcKey:
2405 case DvdInvalidateAGID:
2406 default:
2407 /* Silence warning */
2411 return ret;
2412 #else
2413 FIXME("not supported on this O/S\n");
2414 return STATUS_NOT_SUPPORTED;
2415 #endif
2418 /******************************************************************
2419 * DVD_GetRegion
2421 * This IOCTL combines information from both IOCTL_DVD_READ_KEY
2422 * with key type DvdGetRpcKey and IOCTL_DVD_READ_STRUCTURE with
2423 * structure type DvdCopyrightInformation into one structure.
2425 static NTSTATUS DVD_GetRegion(int fd, PDVD_REGION region)
2427 #if defined(linux)
2428 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2429 dvd_struct dvd;
2430 dvd_authinfo auth_info;
2432 dvd.type = DVD_STRUCT_COPYRIGHT;
2433 dvd.copyright.layer_num = 0;
2434 auth_info.type = DVD_LU_SEND_RPC_STATE;
2436 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2438 if (ret == STATUS_SUCCESS)
2440 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2442 if (ret == STATUS_SUCCESS)
2444 region->CopySystem = dvd.copyright.cpst;
2445 region->RegionData = dvd.copyright.rmi;
2446 region->SystemRegion = auth_info.lrpcs.region_mask;
2447 region->ResetCount = auth_info.lrpcs.ucca;
2450 return ret;
2451 #elif defined(__APPLE__)
2452 dk_dvd_report_key_t key;
2453 dk_dvd_read_structure_t dvd;
2454 DVDRegionPlaybackControlInfo rpc;
2455 DVDCopyrightInfo copy;
2456 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2458 key.format = kDVDKeyFormatRegionState;
2459 key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2460 key.bufferLength = sizeof(rpc);
2461 key.buffer = &rpc;
2462 dvd.format = kDVDStructureFormatCopyrightInfo;
2463 dvd.bufferLength = sizeof(copy);
2464 dvd.buffer = &copy;
2466 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREPORTKEY, &key ));
2468 if (ret == STATUS_SUCCESS)
2470 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREADSTRUCTURE, &dvd ));
2472 if (ret == STATUS_SUCCESS)
2474 region->CopySystem = copy.copyrightProtectionSystemType;
2475 region->RegionData = copy.regionMask;
2476 region->SystemRegion = rpc.driveRegion;
2477 region->ResetCount = rpc.numberUserResets;
2480 return ret;
2481 #else
2482 FIXME("not supported on this O/S\n");
2483 return STATUS_NOT_SUPPORTED;
2484 #endif
2487 /******************************************************************
2488 * DVD_ReadStructure
2492 static NTSTATUS DVD_ReadStructure(int dev, const DVD_READ_STRUCTURE *structure, PDVD_LAYER_DESCRIPTOR layer)
2494 #ifdef DVD_READ_STRUCT
2495 /* dvd_struct is not defined consistently across platforms */
2496 union
2498 struct dvd_physical physical;
2499 struct dvd_copyright copyright;
2500 struct dvd_disckey disckey;
2501 struct dvd_bca bca;
2502 struct dvd_manufact manufact;
2503 } s;
2505 if (structure->BlockByteOffset.u.HighPart || structure->BlockByteOffset.u.LowPart)
2506 FIXME(": BlockByteOffset is not handled\n");
2508 switch (structure->Format)
2510 case DvdPhysicalDescriptor:
2511 s.physical.type = DVD_STRUCT_PHYSICAL;
2512 s.physical.layer_num = structure->LayerNumber;
2513 break;
2515 case DvdCopyrightDescriptor:
2516 s.copyright.type = DVD_STRUCT_COPYRIGHT;
2517 s.copyright.layer_num = structure->LayerNumber;
2518 break;
2520 case DvdDiskKeyDescriptor:
2521 s.disckey.type = DVD_STRUCT_DISCKEY;
2522 s.disckey.agid = structure->SessionId;
2523 break;
2525 case DvdBCADescriptor:
2526 s.bca.type = DVD_STRUCT_BCA;
2527 break;
2529 case DvdManufacturerDescriptor:
2530 s.manufact.type = DVD_STRUCT_MANUFACT;
2531 s.manufact.layer_num = structure->LayerNumber;
2532 break;
2534 case DvdMaxDescriptor: /* This is not a real request, no matter what MSDN says! */
2535 default:
2536 return STATUS_INVALID_PARAMETER;
2539 if (ioctl(dev, DVD_READ_STRUCT, &s) < 0)
2540 return STATUS_INVALID_PARAMETER;
2542 switch (structure->Format)
2544 case DvdPhysicalDescriptor:
2546 internal_dvd_layer_descriptor *p = (internal_dvd_layer_descriptor *) layer;
2547 struct dvd_layer *l = &s.physical.layer[s.physical.layer_num];
2549 p->Header.Length = 0x0802;
2550 p->Header.Reserved[0] = 0;
2551 p->Header.Reserved[1] = 0;
2552 p->Descriptor.BookVersion = l->book_version;
2553 p->Descriptor.BookType = l->book_type;
2554 p->Descriptor.MinimumRate = l->min_rate;
2555 p->Descriptor.DiskSize = l->disc_size;
2556 p->Descriptor.LayerType = l->layer_type;
2557 p->Descriptor.TrackPath = l->track_path;
2558 p->Descriptor.NumberOfLayers = l->nlayers;
2559 p->Descriptor.Reserved1 = 0;
2560 p->Descriptor.TrackDensity = l->track_density;
2561 p->Descriptor.LinearDensity = l->linear_density;
2562 p->Descriptor.StartingDataSector = GET_BE_DWORD(l->start_sector);
2563 p->Descriptor.EndDataSector = GET_BE_DWORD(l->end_sector);
2564 p->Descriptor.EndLayerZeroSector = GET_BE_DWORD(l->end_sector_l0);
2565 p->Descriptor.Reserved5 = 0;
2566 p->Descriptor.BCAFlag = l->bca;
2568 break;
2570 case DvdCopyrightDescriptor:
2572 PDVD_COPYRIGHT_DESCRIPTOR p = (PDVD_COPYRIGHT_DESCRIPTOR) layer;
2574 p->CopyrightProtectionType = s.copyright.cpst;
2575 p->RegionManagementInformation = s.copyright.rmi;
2576 p->Reserved = 0;
2578 break;
2580 case DvdDiskKeyDescriptor:
2582 PDVD_DISK_KEY_DESCRIPTOR p = (PDVD_DISK_KEY_DESCRIPTOR) layer;
2584 memcpy(p->DiskKeyData, s.disckey.value, 2048);
2586 break;
2588 case DvdBCADescriptor:
2590 PDVD_BCA_DESCRIPTOR p = (PDVD_BCA_DESCRIPTOR) layer;
2592 memcpy(p->BCAInformation, s.bca.value, s.bca.len);
2594 break;
2596 case DvdManufacturerDescriptor:
2598 PDVD_MANUFACTURER_DESCRIPTOR p = (PDVD_MANUFACTURER_DESCRIPTOR) layer;
2600 memcpy(p->ManufacturingInformation, s.manufact.value, 2048);
2602 break;
2604 case DvdMaxDescriptor: /* Suppress warning */
2605 break;
2607 #elif defined(__APPLE__)
2608 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2609 dk_dvd_read_structure_t dvdrs;
2610 union
2612 DVDPhysicalFormatInfo phys;
2613 DVDCopyrightInfo copy;
2614 DVDDiscKeyInfo disk_key;
2615 DVDManufacturingInfo manf;
2616 } desc;
2617 union
2619 PDVD_LAYER_DESCRIPTOR layer;
2620 internal_dvd_layer_descriptor *xlayer;
2621 PDVD_COPYRIGHT_DESCRIPTOR copy;
2622 PDVD_DISK_KEY_DESCRIPTOR disk_key;
2623 PDVD_MANUFACTURER_DESCRIPTOR manf;
2624 } nt_desc;
2626 nt_desc.layer = layer;
2627 RtlZeroMemory(&dvdrs, sizeof(dvdrs));
2628 dvdrs.address = (uint32_t)(structure->BlockByteOffset.QuadPart>>11);
2629 dvdrs.grantID = (uint8_t)structure->SessionId;
2630 dvdrs.layer = structure->LayerNumber;
2631 switch(structure->Format)
2633 case DvdPhysicalDescriptor:
2634 dvdrs.format = kDVDStructureFormatPhysicalFormatInfo;
2635 dvdrs.bufferLength = sizeof(desc.phys);
2636 dvdrs.buffer = &desc.phys;
2637 break;
2639 case DvdCopyrightDescriptor:
2640 dvdrs.format = kDVDStructureFormatCopyrightInfo;
2641 dvdrs.bufferLength = sizeof(desc.copy);
2642 dvdrs.buffer = &desc.copy;
2643 break;
2645 case DvdDiskKeyDescriptor:
2646 dvdrs.format = kDVDStructureFormatDiscKeyInfo;
2647 dvdrs.bufferLength = sizeof(desc.disk_key);
2648 dvdrs.buffer = &desc.disk_key;
2649 break;
2651 case DvdBCADescriptor:
2652 FIXME("DvdBCADescriptor NIY\n");
2653 return STATUS_NOT_SUPPORTED;
2655 case DvdManufacturerDescriptor:
2656 dvdrs.format = kDVDStructureFormatManufacturingInfo;
2657 dvdrs.bufferLength = sizeof(desc.manf);
2658 dvdrs.buffer = &desc.manf;
2659 break;
2661 case DvdMaxDescriptor:
2662 default:
2663 FIXME("got unknown structure type 0x%x\n", structure->Format);
2664 return STATUS_NOT_SUPPORTED;
2666 ret = CDROM_GetStatusCode(ioctl(dev, DKIOCDVDREADSTRUCTURE, &dvdrs));
2667 if(ret == STATUS_SUCCESS)
2669 switch(structure->Format)
2671 case DvdPhysicalDescriptor:
2672 nt_desc.xlayer->Header.Length = 0x0802;
2673 nt_desc.xlayer->Header.Reserved[0] = 0;
2674 nt_desc.xlayer->Header.Reserved[1] = 0;
2675 nt_desc.xlayer->Descriptor.BookVersion = desc.phys.partVersion;
2676 nt_desc.xlayer->Descriptor.BookType = desc.phys.bookType;
2677 nt_desc.xlayer->Descriptor.MinimumRate = desc.phys.minimumRate;
2678 nt_desc.xlayer->Descriptor.DiskSize = desc.phys.discSize;
2679 nt_desc.xlayer->Descriptor.LayerType = desc.phys.layerType;
2680 nt_desc.xlayer->Descriptor.TrackPath = desc.phys.trackPath;
2681 nt_desc.xlayer->Descriptor.NumberOfLayers = desc.phys.numberOfLayers;
2682 nt_desc.xlayer->Descriptor.Reserved1 = 0;
2683 nt_desc.xlayer->Descriptor.TrackDensity = desc.phys.trackDensity;
2684 nt_desc.xlayer->Descriptor.LinearDensity = desc.phys.linearDensity;
2685 nt_desc.xlayer->Descriptor.StartingDataSector = GET_BE_DWORD(*(DWORD *)&desc.phys.zero1);
2686 nt_desc.xlayer->Descriptor.EndDataSector = GET_BE_DWORD(*(DWORD *)&desc.phys.zero2);
2687 nt_desc.xlayer->Descriptor.EndLayerZeroSector = GET_BE_DWORD(*(DWORD *)&desc.phys.zero3);
2688 nt_desc.xlayer->Descriptor.Reserved5 = 0;
2689 nt_desc.xlayer->Descriptor.BCAFlag = desc.phys.bcaFlag;
2690 break;
2692 case DvdCopyrightDescriptor:
2693 nt_desc.copy->CopyrightProtectionType =
2694 desc.copy.copyrightProtectionSystemType;
2695 nt_desc.copy->RegionManagementInformation =
2696 desc.copy.regionMask;
2697 nt_desc.copy->Reserved = 0;
2698 break;
2700 case DvdDiskKeyDescriptor:
2701 memcpy(
2702 nt_desc.disk_key->DiskKeyData,
2703 desc.disk_key.discKeyStructures,
2704 2048);
2705 break;
2707 case DvdManufacturerDescriptor:
2708 memcpy(
2709 nt_desc.manf->ManufacturingInformation,
2710 desc.manf.discManufacturingInfo,
2711 2048);
2712 break;
2714 case DvdBCADescriptor:
2715 case DvdMaxDescriptor:
2716 default:
2717 /* Silence warning */
2718 break;
2721 return ret;
2722 #else
2723 FIXME("\n");
2724 #endif
2725 return STATUS_SUCCESS;
2729 /******************************************************************
2730 * GetInquiryData
2731 * Implements the IOCTL_GET_INQUIRY_DATA ioctl.
2732 * Returns Inquiry data for all devices on the specified scsi bus
2733 * Returns STATUS_BUFFER_TOO_SMALL if the output buffer is too small,
2734 * STATUS_INVALID_DEVICE_REQUEST if the given handle isn't to a SCSI device,
2735 * or STATUS_NOT_SUPPORTED if the OS driver is too old
2737 static NTSTATUS GetInquiryData(int fd, PSCSI_ADAPTER_BUS_INFO BufferOut, DWORD OutBufferSize)
2739 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
2740 PSCSI_INQUIRY_DATA pInquiryData = NULL;
2741 UCHAR sense_buffer[32];
2742 int iochk, version;
2743 sg_io_hdr_t iocmd;
2744 UCHAR inquiry[INQ_CMD_LEN] = {INQUIRY, 0, 0, 0, INQ_REPLY_LEN, 0};
2746 /* Check we have a SCSI device and a supported driver */
2747 if(ioctl(fd, SG_GET_VERSION_NUM, &version) != 0)
2749 WARN("IOCTL_SCSI_GET_INQUIRY_DATA sg driver is not loaded\n");
2750 return STATUS_INVALID_DEVICE_REQUEST;
2752 if(version < 30000 )
2753 return STATUS_NOT_SUPPORTED;
2755 /* FIXME: Enumerate devices on the bus */
2756 BufferOut->NumberOfBuses = 1;
2757 BufferOut->BusData[0].NumberOfLogicalUnits = 1;
2758 BufferOut->BusData[0].InquiryDataOffset = sizeof(SCSI_ADAPTER_BUS_INFO);
2760 pInquiryData = (PSCSI_INQUIRY_DATA)(BufferOut + 1);
2762 RtlZeroMemory(&iocmd, sizeof(iocmd));
2763 iocmd.interface_id = 'S';
2764 iocmd.cmd_len = sizeof(inquiry);
2765 iocmd.mx_sb_len = sizeof(sense_buffer);
2766 iocmd.dxfer_direction = SG_DXFER_FROM_DEV;
2767 iocmd.dxfer_len = INQ_REPLY_LEN;
2768 iocmd.dxferp = pInquiryData->InquiryData;
2769 iocmd.cmdp = inquiry;
2770 iocmd.sbp = sense_buffer;
2771 iocmd.timeout = 1000;
2773 iochk = ioctl(fd, SG_IO, &iocmd);
2774 if(iochk != 0)
2775 WARN("ioctl SG_IO returned %d, error (%s)\n", iochk, strerror(errno));
2777 CDROM_GetInterfaceInfo(fd, &BufferOut->BusData[0].InitiatorBusId, &pInquiryData->PathId, &pInquiryData->TargetId, &pInquiryData->Lun);
2778 pInquiryData->DeviceClaimed = TRUE;
2779 pInquiryData->InquiryDataLength = INQ_REPLY_LEN;
2780 pInquiryData->NextInquiryDataOffset = 0;
2781 return STATUS_SUCCESS;
2782 #else
2783 FIXME("not supported on this O/S\n");
2784 return STATUS_NOT_SUPPORTED;
2785 #endif
2788 /******************************************************************
2789 * CDROM_DeviceIoControl
2793 NTSTATUS CDROM_DeviceIoControl(HANDLE hDevice,
2794 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
2795 PVOID UserApcContext,
2796 PIO_STATUS_BLOCK piosb,
2797 ULONG dwIoControlCode,
2798 LPVOID lpInBuffer, DWORD nInBufferSize,
2799 LPVOID lpOutBuffer, DWORD nOutBufferSize)
2801 DWORD sz = 0;
2802 NTSTATUS status = STATUS_SUCCESS;
2803 int fd, needs_close, dev;
2805 TRACE("%p %s %p %d %p %d %p\n",
2806 hDevice, iocodex(dwIoControlCode), lpInBuffer, nInBufferSize,
2807 lpOutBuffer, nOutBufferSize, piosb);
2809 piosb->Information = 0;
2811 if ((status = server_get_unix_fd( hDevice, 0, &fd, &needs_close, NULL, NULL )))
2813 if (status == STATUS_BAD_DEVICE_TYPE) return status; /* no associated fd */
2814 goto error;
2817 if ((status = CDROM_Open(fd, &dev)))
2819 if (needs_close) close( fd );
2820 goto error;
2823 #ifdef __APPLE__
2825 char name[100];
2827 /* This is ugly as hell, but Mac OS is unable to do anything from the
2828 * partition fd, it wants an fd for the whole device, and it sometimes
2829 * also requires the device fd to be closed first, so we have to close
2830 * the handle that the caller gave us.
2831 * Also for some reason it wants the fd to be closed before we even
2832 * open the parent if we're trying to eject the disk.
2834 if ((status = get_parent_device( fd, name, sizeof(name) ))) goto error;
2835 if (dwIoControlCode == IOCTL_STORAGE_EJECT_MEDIA)
2836 NtClose( hDevice );
2837 if (needs_close) close( fd );
2838 TRACE("opening parent %s\n", name );
2839 if ((fd = open( name, O_RDONLY )) == -1)
2841 status = FILE_GetNtStatus();
2842 goto error;
2844 needs_close = 1;
2846 #endif
2848 switch (dwIoControlCode)
2850 case IOCTL_CDROM_CHECK_VERIFY:
2851 case IOCTL_DISK_CHECK_VERIFY:
2852 case IOCTL_STORAGE_CHECK_VERIFY:
2853 case IOCTL_STORAGE_CHECK_VERIFY2:
2854 sz = 0;
2855 CDROM_ClearCacheEntry(dev);
2856 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2857 status = STATUS_INVALID_PARAMETER;
2858 else status = CDROM_Verify(dev, fd);
2859 break;
2861 /* EPP case IOCTL_STORAGE_CHECK_VERIFY2: */
2863 /* EPP case IOCTL_STORAGE_FIND_NEW_DEVICES: */
2864 /* EPP case IOCTL_CDROM_FIND_NEW_DEVICES: */
2866 case IOCTL_STORAGE_LOAD_MEDIA:
2867 case IOCTL_CDROM_LOAD_MEDIA:
2868 sz = 0;
2869 CDROM_ClearCacheEntry(dev);
2870 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2871 status = STATUS_INVALID_PARAMETER;
2872 else status = CDROM_SetTray(fd, FALSE);
2873 break;
2874 case IOCTL_STORAGE_EJECT_MEDIA:
2875 sz = 0;
2876 CDROM_ClearCacheEntry(dev);
2877 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2878 status = STATUS_INVALID_PARAMETER;
2879 else
2880 status = CDROM_SetTray(fd, TRUE);
2881 break;
2883 case IOCTL_CDROM_MEDIA_REMOVAL:
2884 case IOCTL_DISK_MEDIA_REMOVAL:
2885 case IOCTL_STORAGE_MEDIA_REMOVAL:
2886 case IOCTL_STORAGE_EJECTION_CONTROL:
2887 /* FIXME the last ioctl:s is not the same as the two others...
2888 * lockcount/owner should be handled */
2889 sz = 0;
2890 CDROM_ClearCacheEntry(dev);
2891 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2892 else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) status = STATUS_BUFFER_TOO_SMALL;
2893 else status = CDROM_ControlEjection(fd, lpInBuffer);
2894 break;
2896 case IOCTL_STORAGE_GET_MEDIA_TYPES:
2897 case IOCTL_STORAGE_GET_MEDIA_TYPES_EX:
2898 sz = sizeof(GET_MEDIA_TYPES);
2899 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2900 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2901 else status = CDROM_GetMediaType(dev, lpOutBuffer);
2902 break;
2904 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
2905 sz = sizeof(STORAGE_DEVICE_NUMBER);
2906 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2907 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2908 else status = CDROM_GetDeviceNumber(dev, lpOutBuffer);
2909 break;
2911 case IOCTL_STORAGE_RESET_DEVICE:
2912 sz = 0;
2913 CDROM_ClearCacheEntry(dev);
2914 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2915 status = STATUS_INVALID_PARAMETER;
2916 else status = CDROM_ResetAudio(fd);
2917 break;
2919 case IOCTL_CDROM_GET_CONTROL:
2920 sz = sizeof(CDROM_AUDIO_CONTROL);
2921 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2922 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2923 else status = CDROM_GetControl(dev, fd, lpOutBuffer);
2924 break;
2926 case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
2927 sz = sizeof(DISK_GEOMETRY);
2928 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2929 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2930 else status = CDROM_GetDriveGeometry(dev, fd, lpOutBuffer);
2931 break;
2933 case IOCTL_CDROM_DISK_TYPE:
2934 sz = sizeof(CDROM_DISK_DATA);
2935 /* CDROM_ClearCacheEntry(dev); */
2936 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2937 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2938 else status = CDROM_GetDiskData(dev, fd, lpOutBuffer);
2939 break;
2941 /* EPP case IOCTL_CDROM_GET_LAST_SESSION: */
2943 case IOCTL_CDROM_READ_Q_CHANNEL:
2944 sz = sizeof(SUB_Q_CHANNEL_DATA);
2945 if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT))
2946 status = STATUS_INVALID_PARAMETER;
2947 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2948 else status = CDROM_ReadQChannel(dev, fd, lpInBuffer, lpOutBuffer);
2949 break;
2951 case IOCTL_CDROM_READ_TOC:
2952 sz = sizeof(CDROM_TOC);
2953 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2954 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2955 else status = CDROM_ReadTOC(dev, fd, lpOutBuffer);
2956 break;
2958 /* EPP case IOCTL_CDROM_READ_TOC_EX: */
2960 case IOCTL_CDROM_PAUSE_AUDIO:
2961 sz = 0;
2962 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2963 status = STATUS_INVALID_PARAMETER;
2964 else status = CDROM_PauseAudio(fd);
2965 break;
2966 case IOCTL_CDROM_PLAY_AUDIO_MSF:
2967 sz = 0;
2968 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2969 else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
2970 else status = CDROM_PlayAudioMSF(fd, lpInBuffer);
2971 break;
2972 case IOCTL_CDROM_RESUME_AUDIO:
2973 sz = 0;
2974 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2975 status = STATUS_INVALID_PARAMETER;
2976 else status = CDROM_ResumeAudio(fd);
2977 break;
2978 case IOCTL_CDROM_SEEK_AUDIO_MSF:
2979 sz = 0;
2980 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2981 else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
2982 else status = CDROM_SeekAudioMSF(dev, fd, lpInBuffer);
2983 break;
2984 case IOCTL_CDROM_STOP_AUDIO:
2985 sz = 0;
2986 CDROM_ClearCacheEntry(dev); /* Maybe intention is to change media */
2987 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2988 status = STATUS_INVALID_PARAMETER;
2989 else status = CDROM_StopAudio(fd);
2990 break;
2991 case IOCTL_CDROM_GET_VOLUME:
2992 sz = sizeof(VOLUME_CONTROL);
2993 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2994 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2995 else status = CDROM_GetVolume(fd, lpOutBuffer);
2996 break;
2997 case IOCTL_CDROM_SET_VOLUME:
2998 sz = 0;
2999 CDROM_ClearCacheEntry(dev);
3000 if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
3001 status = STATUS_INVALID_PARAMETER;
3002 else status = CDROM_SetVolume(fd, lpInBuffer);
3003 break;
3004 case IOCTL_CDROM_RAW_READ:
3005 sz = 0;
3006 if (nInBufferSize < sizeof(RAW_READ_INFO)) status = STATUS_INVALID_PARAMETER;
3007 else if (lpOutBuffer == NULL) status = STATUS_BUFFER_TOO_SMALL;
3008 else status = CDROM_RawRead(fd, lpInBuffer, lpOutBuffer,
3009 nOutBufferSize, &sz);
3010 break;
3011 case IOCTL_SCSI_GET_ADDRESS:
3012 sz = sizeof(SCSI_ADDRESS);
3013 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3014 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3015 else status = CDROM_GetAddress(fd, lpOutBuffer);
3016 break;
3017 case IOCTL_SCSI_PASS_THROUGH_DIRECT:
3018 sz = sizeof(SCSI_PASS_THROUGH_DIRECT);
3019 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3020 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH_DIRECT)) status = STATUS_BUFFER_TOO_SMALL;
3021 else status = CDROM_ScsiPassThroughDirect(fd, lpOutBuffer);
3022 break;
3023 case IOCTL_SCSI_PASS_THROUGH:
3024 sz = sizeof(SCSI_PASS_THROUGH);
3025 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3026 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH)) status = STATUS_BUFFER_TOO_SMALL;
3027 else status = CDROM_ScsiPassThrough(fd, lpOutBuffer);
3028 break;
3029 case IOCTL_SCSI_GET_CAPABILITIES:
3030 sz = sizeof(IO_SCSI_CAPABILITIES);
3031 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3032 else if (nOutBufferSize < sizeof(IO_SCSI_CAPABILITIES)) status = STATUS_BUFFER_TOO_SMALL;
3033 else status = CDROM_ScsiGetCaps(fd, lpOutBuffer);
3034 break;
3035 case IOCTL_DVD_START_SESSION:
3036 sz = sizeof(DVD_SESSION_ID);
3037 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3038 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3039 else
3041 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3042 *(PDVD_SESSION_ID)lpOutBuffer);
3043 status = DVD_StartSession(fd, lpInBuffer, lpOutBuffer);
3044 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3045 *(PDVD_SESSION_ID)lpOutBuffer);
3047 break;
3048 case IOCTL_DVD_END_SESSION:
3049 sz = sizeof(DVD_SESSION_ID);
3050 if ((lpInBuffer == NULL) || (nInBufferSize < sz))status = STATUS_INVALID_PARAMETER;
3051 else status = DVD_EndSession(fd, lpInBuffer);
3052 break;
3053 case IOCTL_DVD_SEND_KEY:
3054 sz = 0;
3055 if (!lpInBuffer ||
3056 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3057 status = STATUS_INVALID_PARAMETER;
3058 else
3060 TRACE("doing DVD_SendKey\n");
3061 status = DVD_SendKey(fd, lpInBuffer);
3063 break;
3064 case IOCTL_DVD_READ_KEY:
3065 if (!lpInBuffer ||
3066 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3067 status = STATUS_INVALID_PARAMETER;
3068 else if (lpInBuffer !=lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
3069 else
3071 TRACE("doing DVD_READ_KEY\n");
3072 sz = ((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength;
3073 status = DVD_ReadKey(fd, lpInBuffer);
3075 break;
3076 case IOCTL_DVD_GET_REGION:
3077 sz = sizeof(DVD_REGION);
3078 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3079 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3080 TRACE("doing DVD_Get_REGION\n");
3081 status = DVD_GetRegion(fd, lpOutBuffer);
3082 break;
3083 case IOCTL_DVD_READ_STRUCTURE:
3084 sz = sizeof(DVD_LAYER_DESCRIPTOR);
3085 if (lpInBuffer == NULL || nInBufferSize != sizeof(DVD_READ_STRUCTURE)) status = STATUS_INVALID_PARAMETER;
3086 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3087 else
3089 TRACE("doing DVD_READ_STRUCTURE\n");
3090 status = DVD_ReadStructure(fd, lpInBuffer, lpOutBuffer);
3092 break;
3094 case IOCTL_SCSI_GET_INQUIRY_DATA:
3095 sz = INQ_REPLY_LEN;
3096 status = GetInquiryData(fd, lpOutBuffer, nOutBufferSize);
3097 break;
3099 default:
3100 if (needs_close) close( fd );
3101 return STATUS_NOT_SUPPORTED;
3103 if (needs_close) close( fd );
3104 error:
3105 piosb->u.Status = status;
3106 piosb->Information = sz;
3107 if (hEvent) NtSetEvent(hEvent, NULL);
3108 return status;