mfplat: Make functionality of GetMediaTypeHandler more clear.
[wine.git] / dlls / ntdll / cdrom.c
blobf6e3a86e34faf35584b68aad48dbd4f74f160d6c
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_UNISTD_H
32 # include <unistd.h>
33 #endif
34 #include <fcntl.h>
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
37 #endif
38 #ifdef MAJOR_IN_MKDEV
39 # include <sys/mkdev.h>
40 #elif defined(MAJOR_IN_SYSMACROS)
41 # include <sys/sysmacros.h>
42 #endif
43 #include <sys/types.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48 #ifdef HAVE_SCSI_SG_H
49 # include <scsi/sg.h>
50 #endif
51 #ifdef HAVE_SCSI_SCSI_H
52 # include <scsi/scsi.h>
53 # undef REASSIGN_BLOCKS /* avoid conflict with winioctl.h */
54 # undef FAILED /* avoid conflict with winerror.h */
55 #endif
56 #ifdef HAVE_SCSI_SCSI_IOCTL_H
57 # include <scsi/scsi_ioctl.h>
58 #endif
59 #ifdef HAVE_LINUX_MAJOR_H
60 # include <linux/major.h>
61 #endif
62 #ifdef HAVE_LINUX_HDREG_H
63 # include <linux/hdreg.h>
64 #endif
65 #ifdef HAVE_LINUX_PARAM_H
66 # include <linux/param.h>
67 #endif
68 #ifdef HAVE_LINUX_CDROM_H
69 # include <linux/cdrom.h>
70 #endif
71 #ifdef HAVE_LINUX_UCDROM_H
72 # include <linux/ucdrom.h>
73 #endif
74 #ifdef HAVE_SYS_CDIO_H
75 # include <sys/cdio.h>
76 #endif
77 #ifdef HAVE_SYS_SCSIIO_H
78 # include <sys/scsiio.h>
79 #endif
81 #ifdef HAVE_IOKIT_IOKITLIB_H
82 # include <libkern/OSByteOrder.h>
83 # include <sys/disk.h>
84 # include <IOKit/IOKitLib.h>
85 # include <IOKit/storage/IOMedia.h>
86 # include <IOKit/storage/IOCDMediaBSDClient.h>
87 # include <IOKit/storage/IODVDMediaBSDClient.h>
88 # include <IOKit/scsi/SCSITask.h>
89 # include <IOKit/scsi/SCSICmds_REQUEST_SENSE_Defs.h>
90 # define SENSEBUFLEN kSenseDefaultSize
92 typedef struct
94 uint32_t attribute;
95 uint32_t timeout;
96 uint32_t response;
97 uint32_t status;
98 uint8_t direction;
99 uint8_t cdbSize;
100 uint8_t reserved0144[2];
101 uint8_t cdb[16];
102 void* buffer;
103 uint64_t bufferSize;
104 void* sense;
105 uint64_t senseLen;
106 } dk_scsi_command_t;
108 typedef struct
110 uint64_t bus;
111 uint64_t port;
112 uint64_t target;
113 uint64_t lun;
114 } dk_scsi_identify_t;
116 #define DKIOCSCSICOMMAND _IOWR('d', 253, dk_scsi_command_t)
117 #define DKIOCSCSIIDENTIFY _IOR('d', 254, dk_scsi_identify_t)
119 #endif
121 #include "ntstatus.h"
122 #define WIN32_NO_STATUS
123 #define NONAMELESSUNION
124 #include "windef.h"
125 #include "winternl.h"
126 #include "winioctl.h"
127 #include "ntddstor.h"
128 #include "ntddcdrm.h"
129 #include "ddk/ntddcdvd.h"
130 #include "ntddscsi.h"
131 #include "ntdll_misc.h"
132 #include "wine/server.h"
133 #include "wine/library.h"
134 #include "wine/debug.h"
136 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
138 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
139 lack the following constants. */
141 #ifndef CD_SECS
142 # define CD_SECS 60 /* seconds per minute */
143 #endif
144 #ifndef CD_FRAMES
145 # define CD_FRAMES 75 /* frames per second */
146 #endif
148 #ifdef WORDS_BIGENDIAN
149 #define GET_BE_DWORD(x) (x)
150 #else
151 #define GET_BE_DWORD(x) RtlUlongByteSwap(x)
152 #endif
154 static const struct iocodexs
156 DWORD code;
157 const char *codex;
158 } iocodextable[] = {
159 #define X(x) { x, #x },
160 X(IOCTL_CDROM_CHECK_VERIFY)
161 X(IOCTL_CDROM_CURRENT_POSITION)
162 X(IOCTL_CDROM_DISK_TYPE)
163 X(IOCTL_CDROM_GET_CONTROL)
164 X(IOCTL_CDROM_GET_DRIVE_GEOMETRY)
165 X(IOCTL_CDROM_GET_VOLUME)
166 X(IOCTL_CDROM_LOAD_MEDIA)
167 X(IOCTL_CDROM_MEDIA_CATALOG)
168 X(IOCTL_CDROM_MEDIA_REMOVAL)
169 X(IOCTL_CDROM_PAUSE_AUDIO)
170 X(IOCTL_CDROM_PLAY_AUDIO_MSF)
171 X(IOCTL_CDROM_RAW_READ)
172 X(IOCTL_CDROM_READ_Q_CHANNEL)
173 X(IOCTL_CDROM_READ_TOC)
174 X(IOCTL_CDROM_RESUME_AUDIO)
175 X(IOCTL_CDROM_SEEK_AUDIO_MSF)
176 X(IOCTL_CDROM_SET_VOLUME)
177 X(IOCTL_CDROM_STOP_AUDIO)
178 X(IOCTL_CDROM_TRACK_ISRC)
179 X(IOCTL_DISK_GET_MEDIA_TYPES)
180 X(IOCTL_DISK_MEDIA_REMOVAL)
181 X(IOCTL_DVD_END_SESSION)
182 X(IOCTL_DVD_GET_REGION)
183 X(IOCTL_DVD_READ_KEY)
184 X(IOCTL_DVD_READ_STRUCTURE)
185 X(IOCTL_DVD_SEND_KEY)
186 X(IOCTL_DVD_START_SESSION)
187 X(IOCTL_SCSI_GET_ADDRESS)
188 X(IOCTL_SCSI_GET_CAPABILITIES)
189 X(IOCTL_SCSI_GET_INQUIRY_DATA)
190 X(IOCTL_SCSI_PASS_THROUGH)
191 X(IOCTL_SCSI_PASS_THROUGH_DIRECT)
192 X(IOCTL_STORAGE_CHECK_VERIFY)
193 X(IOCTL_STORAGE_CHECK_VERIFY2)
194 X(IOCTL_STORAGE_EJECTION_CONTROL)
195 X(IOCTL_STORAGE_EJECT_MEDIA)
196 X(IOCTL_STORAGE_GET_DEVICE_NUMBER)
197 X(IOCTL_STORAGE_GET_MEDIA_TYPES)
198 X(IOCTL_STORAGE_GET_MEDIA_TYPES_EX)
199 X(IOCTL_STORAGE_LOAD_MEDIA)
200 X(IOCTL_STORAGE_MEDIA_REMOVAL)
201 X(IOCTL_STORAGE_RESET_DEVICE)
202 #undef X
204 static const char *iocodex(DWORD code)
206 unsigned int i;
207 static char buffer[25];
208 for(i=0; i<ARRAY_SIZE(iocodextable); i++)
209 if (code==iocodextable[i].code)
210 return iocodextable[i].codex;
211 sprintf(buffer, "IOCTL_CODE_%x", (int)code);
212 return buffer;
215 #define INQ_REPLY_LEN 36
216 #define INQ_CMD_LEN 6
218 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
219 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
220 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
221 #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;}
223 /* The documented format of DVD_LAYER_DESCRIPTOR is wrong. Even the format in the
224 * DDK's header is wrong. There are four bytes at the start defined by
225 * MMC-5. The first two are the size of the structure in big-endian order as
226 * defined by MMC-5. The other two are reserved.
228 typedef struct
230 DVD_DESCRIPTOR_HEADER Header;
231 DVD_LAYER_DESCRIPTOR Descriptor;
232 UCHAR Padding;
233 } internal_dvd_layer_descriptor;
234 C_ASSERT(sizeof(internal_dvd_layer_descriptor) == 22);
236 typedef struct
238 DVD_DESCRIPTOR_HEADER Header;
239 DVD_MANUFACTURER_DESCRIPTOR Descriptor;
240 UCHAR Padding;
241 } internal_dvd_manufacturer_descriptor;
242 C_ASSERT(sizeof(internal_dvd_manufacturer_descriptor) == 2053);
244 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
245 static NTSTATUS CDROM_GetStatusCode(int);
248 #ifdef linux
250 # ifndef IDE6_MAJOR
251 # define IDE6_MAJOR 88
252 # endif
253 # ifndef IDE7_MAJOR
254 # define IDE7_MAJOR 89
255 # endif
257 # ifdef CDROM_SEND_PACKET
258 /* structure for CDROM_PACKET_COMMAND ioctl */
259 /* not all Linux versions have all the fields, so we define the
260 * structure ourselves to make sure */
261 struct linux_cdrom_generic_command
263 unsigned char cmd[CDROM_PACKET_SIZE];
264 unsigned char *buffer;
265 unsigned int buflen;
266 int stat;
267 struct request_sense *sense;
268 unsigned char data_direction;
269 int quiet;
270 int timeout;
271 void *reserved[1];
273 # endif /* CDROM_SEND_PACKET */
275 #endif /* linux */
277 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
278 * this should be removed when a proper device interface is implemented
280 * (WS) We need this to keep track of current position and to safely
281 * detect media changes. Besides this should provide a great speed up
282 * for toc inquiries.
284 struct cdrom_cache {
285 dev_t device;
286 ino_t inode;
287 char toc_good; /* if false, will reread TOC from disk */
288 CDROM_TOC toc;
289 SUB_Q_CURRENT_POSITION CurrentPosition;
291 /* who has more than 5 cdroms on his/her machine ?? */
292 /* FIXME: this should grow depending on the number of cdroms we install/configure
293 * at startup
295 #define MAX_CACHE_ENTRIES 5
296 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
298 static RTL_CRITICAL_SECTION cache_section;
299 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
301 0, 0, &cache_section,
302 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
303 0, 0, { (DWORD_PTR)(__FILE__ ": cache_section") }
305 static RTL_CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
307 /* Proposed media change function: not really needed at this time */
308 /* This is a 1 or 0 type of function */
309 #if 0
310 static int CDROM_MediaChanged(int dev)
312 int i;
314 struct cdrom_tochdr hdr;
315 struct cdrom_tocentry entry;
317 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
318 return 0;
319 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
320 return 0;
322 if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
323 return 1;
325 for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
327 if (i == hdr.cdth_trk1 + 1)
329 entry.cdte_track = CDROM_LEADOUT;
330 } else {
331 entry.cdte_track = i;
333 entry.cdte_format = CDROM_MSF;
334 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
335 return 0;
336 if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
337 sizeof(struct cdrom_tocentry)) )
338 return 1;
340 return 0;
342 #endif
345 /******************************************************************
346 * get_parent_device
348 * On Mac OS, get the device for the whole disk from a fd that points to a partition.
349 * This is ugly and inefficient, but we have no choice since the partition fd doesn't
350 * support the eject ioctl.
352 #ifdef __APPLE__
353 static NTSTATUS get_parent_device( int fd, char *name, size_t len )
355 NTSTATUS status = STATUS_NO_SUCH_FILE;
356 struct stat st;
357 int i;
358 io_service_t service;
359 CFMutableDictionaryRef dict;
360 CFTypeRef val;
362 if (fstat( fd, &st ) == -1) return FILE_GetNtStatus();
363 if (!S_ISCHR( st.st_mode )) return STATUS_OBJECT_TYPE_MISMATCH;
365 /* create a dictionary with the right major/minor numbers */
367 if (!(dict = IOServiceMatching( kIOMediaClass ))) return STATUS_NO_MEMORY;
369 i = major( st.st_rdev );
370 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
371 CFDictionaryAddValue( dict, CFSTR( "BSD Major" ), val );
372 CFRelease( val );
374 i = minor( st.st_rdev );
375 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
376 CFDictionaryAddValue( dict, CFSTR( "BSD Minor" ), val );
377 CFRelease( val );
379 CFDictionaryAddValue( dict, CFSTR("Removable"), kCFBooleanTrue );
381 service = IOServiceGetMatchingService( kIOMasterPortDefault, dict );
383 /* now look for the parent that has the "Whole" attribute set to TRUE */
385 while (service)
387 io_service_t parent = 0;
388 CFBooleanRef whole;
389 CFStringRef str;
390 int ok;
392 if (!IOObjectConformsTo( service, kIOMediaClass ))
393 goto next;
394 if (!(whole = IORegistryEntryCreateCFProperty( service, CFSTR("Whole"), NULL, 0 )))
395 goto next;
396 ok = (whole == kCFBooleanTrue);
397 CFRelease( whole );
398 if (!ok) goto next;
400 if ((str = IORegistryEntryCreateCFProperty( service, CFSTR("BSD Name"), NULL, 0 )))
402 strcpy( name, "/dev/r" );
403 CFStringGetCString( str, name + 6, len - 6, kCFStringEncodingUTF8 );
404 CFRelease( str );
405 status = STATUS_SUCCESS;
407 IOObjectRelease( service );
408 break;
410 next:
411 IORegistryEntryGetParentEntry( service, kIOServicePlane, &parent );
412 IOObjectRelease( service );
413 service = parent;
415 return status;
417 #endif
420 /******************************************************************
421 * CDROM_SyncCache [internal]
423 * Read the TOC in and store it in the cdrom_cache structure.
424 * Further requests for the TOC will be copied from the cache
425 * unless certain events like disk ejection is detected, in which
426 * case the cache will be cleared, causing it to be resynced.
427 * The cache section must be held by caller.
429 static NTSTATUS CDROM_SyncCache(int dev, int fd)
431 #ifdef linux
432 int i, tsz;
433 struct cdrom_tochdr hdr;
434 struct cdrom_tocentry entry;
436 CDROM_TOC *toc = &cdrom_cache[dev].toc;
437 cdrom_cache[dev].toc_good = 0;
439 if (ioctl(fd, CDROMREADTOCHDR, &hdr) == -1)
441 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
442 return FILE_GetNtStatus();
445 toc->FirstTrack = hdr.cdth_trk0;
446 toc->LastTrack = hdr.cdth_trk1;
447 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
448 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
449 toc->Length[0] = tsz >> 8;
450 toc->Length[1] = tsz;
452 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
454 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
456 if (i == toc->LastTrack + 1)
457 entry.cdte_track = CDROM_LEADOUT;
458 else
459 entry.cdte_track = i;
460 entry.cdte_format = CDROM_MSF;
461 if (ioctl(fd, CDROMREADTOCENTRY, &entry) == -1)
463 WARN("error read entry (%s)\n", strerror(errno));
464 return FILE_GetNtStatus();
466 toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
467 toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
468 /* marking last track with leadout value as index */
469 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
470 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
471 toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
472 toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
473 toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
475 cdrom_cache[dev].toc_good = 1;
476 return STATUS_SUCCESS;
478 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
480 int i, tsz;
481 struct ioc_toc_header hdr;
482 struct ioc_read_toc_entry entry;
483 struct cd_toc_entry toc_buffer;
485 CDROM_TOC *toc = &cdrom_cache[dev].toc;
486 cdrom_cache[dev].toc_good = 0;
488 if (ioctl(fd, CDIOREADTOCHEADER, &hdr) == -1)
490 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
491 return FILE_GetNtStatus();
493 toc->FirstTrack = hdr.starting_track;
494 toc->LastTrack = hdr.ending_track;
495 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
496 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
497 toc->Length[0] = tsz >> 8;
498 toc->Length[1] = tsz;
500 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
502 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
504 if (i == toc->LastTrack + 1)
506 #define LEADOUT 0xaa
507 entry.starting_track = LEADOUT;
508 } else {
509 entry.starting_track = i;
511 memset(&toc_buffer, 0, sizeof(toc_buffer));
512 entry.address_format = CD_MSF_FORMAT;
513 entry.data_len = sizeof(toc_buffer);
514 entry.data = &toc_buffer;
515 if (ioctl(fd, CDIOREADTOCENTRYS, &entry) == -1)
517 WARN("error read entry (%s)\n", strerror(errno));
518 return FILE_GetNtStatus();
520 toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
521 toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
522 /* marking last track with leadout value as index */
523 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
524 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
525 toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
526 toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
527 toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
529 cdrom_cache[dev].toc_good = 1;
530 return STATUS_SUCCESS;
532 #elif defined(__APPLE__)
533 int i;
534 dk_cd_read_toc_t hdr;
535 CDROM_TOC *toc = &cdrom_cache[dev].toc;
536 cdrom_cache[dev].toc_good = 0;
538 memset( &hdr, 0, sizeof(hdr) );
539 hdr.buffer = toc;
540 hdr.bufferLength = sizeof(*toc);
541 if (ioctl(fd, DKIOCCDREADTOC, &hdr) == -1)
543 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
544 return FILE_GetNtStatus();
546 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
548 /* convert address format */
549 TRACK_DATA *data = &toc->TrackData[i - toc->FirstTrack];
550 DWORD frame = (((DWORD)data->Address[0] << 24) | ((DWORD)data->Address[1] << 16) |
551 ((DWORD)data->Address[2] << 8) | data->Address[3]);
552 MSF_OF_FRAME( data->Address[1], frame );
553 data->Address[0] = 0;
556 cdrom_cache[dev].toc_good = 1;
557 return STATUS_SUCCESS;
558 #else
559 FIXME("not supported on this O/S\n");
560 return STATUS_NOT_SUPPORTED;
561 #endif
564 static void CDROM_ClearCacheEntry(int dev)
566 RtlEnterCriticalSection( &cache_section );
567 cdrom_cache[dev].toc_good = 0;
568 RtlLeaveCriticalSection( &cache_section );
573 /******************************************************************
574 * CDROM_GetInterfaceInfo
576 * Determines the ide interface (the number after the ide), and the
577 * number of the device on that interface for ide cdroms (*iface <= 1).
578 * Determines the scsi information for scsi cdroms (*iface >= 2).
579 * Returns FALSE if the info cannot not be obtained.
581 static BOOL CDROM_GetInterfaceInfo(int fd, UCHAR* iface, UCHAR* port, UCHAR* device, UCHAR* lun)
583 #if defined(linux)
584 struct stat st;
585 if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode)) return FALSE;
586 *port = 0;
587 *iface = 0;
588 *device = 0;
589 *lun = 0;
590 switch (major(st.st_rdev)) {
591 case IDE0_MAJOR: *iface = 0; break;
592 case IDE1_MAJOR: *iface = 1; break;
593 case IDE2_MAJOR: *iface = 2; break;
594 case IDE3_MAJOR: *iface = 3; break;
595 case IDE4_MAJOR: *iface = 4; break;
596 case IDE5_MAJOR: *iface = 5; break;
597 case IDE6_MAJOR: *iface = 6; break;
598 case IDE7_MAJOR: *iface = 7; break;
599 default: *port = 1; break;
602 if (*port == 0)
603 *device = (minor(st.st_rdev) >> 6);
604 else
606 #ifdef SCSI_IOCTL_GET_IDLUN
607 UINT32 idlun[2];
608 if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, idlun) != -1)
610 *port = (idlun[0] >> 24) & 0xff;
611 *iface = ((idlun[0] >> 16) & 0xff) + 2;
612 *device = idlun[0] & 0xff;
613 *lun = (idlun[0] >> 8) & 0xff;
615 else
616 #endif
618 WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
619 return FALSE;
622 return TRUE;
623 #elif defined(__NetBSD__)
624 struct scsi_addr addr;
625 if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
627 switch (addr.type)
629 case TYPE_SCSI:
630 *port = 1;
631 *iface = addr.addr.scsi.scbus;
632 *device = addr.addr.scsi.target;
633 *lun = addr.addr.scsi.lun;
634 return TRUE;
635 case TYPE_ATAPI:
636 *port = 0;
637 *iface = addr.addr.atapi.atbus;
638 *device = addr.addr.atapi.drive;
639 *lun = 0;
640 return TRUE;
643 return FALSE;
644 #elif defined(__APPLE__)
645 dk_scsi_identify_t addr;
646 if (ioctl(fd, DKIOCSCSIIDENTIFY, &addr) != -1)
648 *port = addr.bus;
649 *iface = addr.port;
650 *device = addr.target;
651 *lun = addr.lun;
652 return TRUE;
654 return FALSE;
655 #else
656 FIXME("not implemented on this O/S\n");
657 return FALSE;
658 #endif
662 /******************************************************************
663 * CDROM_Open
666 static NTSTATUS CDROM_Open(int fd, int* dev)
668 struct stat st;
669 NTSTATUS ret = STATUS_SUCCESS;
670 int empty = -1;
672 if (fstat(fd, &st) == -1) return FILE_GetNtStatus();
674 RtlEnterCriticalSection( &cache_section );
675 for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
677 if (empty == -1 &&
678 cdrom_cache[*dev].device == 0 &&
679 cdrom_cache[*dev].inode == 0)
680 empty = *dev;
681 else if (cdrom_cache[*dev].device == st.st_dev &&
682 cdrom_cache[*dev].inode == st.st_ino)
683 break;
685 if (*dev == MAX_CACHE_ENTRIES)
687 if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
688 else
690 *dev = empty;
691 cdrom_cache[*dev].device = st.st_dev;
692 cdrom_cache[*dev].inode = st.st_ino;
695 RtlLeaveCriticalSection( &cache_section );
697 TRACE("%d, %d\n", *dev, fd);
698 return ret;
701 /******************************************************************
702 * CDROM_GetStatusCode
706 static NTSTATUS CDROM_GetStatusCode(int io)
708 if (io == 0) return STATUS_SUCCESS;
709 return FILE_GetNtStatus();
712 /******************************************************************
713 * CDROM_GetControl
716 static NTSTATUS CDROM_GetControl(int dev, int fd, CDROM_AUDIO_CONTROL* cac)
718 #ifdef __APPLE__
719 uint16_t speed;
720 int io = ioctl( fd, DKIOCCDGETSPEED, &speed );
721 if (io != 0) return CDROM_GetStatusCode( io );
722 /* DKIOCCDGETSPEED returns the speed in kilobytes per second,
723 * so convert to logical blocks (assumed to be ~2 KB).
725 cac->LogicalBlocksPerSecond = speed/2;
726 #else
727 cac->LogicalBlocksPerSecond = 1; /* FIXME */
728 #endif
729 cac->LbaFormat = 0; /* FIXME */
730 return STATUS_NOT_SUPPORTED;
733 /******************************************************************
734 * CDROM_GetDeviceNumber
737 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
739 FIXME( "stub\n" );
740 devnum->DeviceType = FILE_DEVICE_DISK;
741 devnum->DeviceNumber = 1;
742 devnum->PartitionNumber = 1;
743 return STATUS_SUCCESS;
746 /******************************************************************
747 * CDROM_GetDriveGeometry
750 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
752 CDROM_TOC toc;
753 NTSTATUS ret;
754 int fsize;
756 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
758 fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
759 - FRAME_OF_TOC(toc, 1); /* Total size in frames */
761 dg->Cylinders.QuadPart = fsize / (64 * 32);
762 dg->MediaType = RemovableMedia;
763 dg->TracksPerCylinder = 64;
764 dg->SectorsPerTrack = 32;
765 dg->BytesPerSector= 2048;
766 return ret;
769 /******************************************************************
770 * CDROM_GetMediaType
773 static NTSTATUS CDROM_GetMediaType(int dev, GET_MEDIA_TYPES* medtype)
775 FIXME(": faking success\n");
776 medtype->DeviceType = FILE_DEVICE_CD_ROM;
777 medtype->MediaInfoCount = 0;
778 return STATUS_SUCCESS;
781 /**************************************************************************
782 * CDROM_Reset [internal]
784 static NTSTATUS CDROM_ResetAudio(int fd)
786 #if defined(linux)
787 return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
788 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
789 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
790 #else
791 FIXME("not supported on this O/S\n");
792 return STATUS_NOT_SUPPORTED;
793 #endif
796 /******************************************************************
797 * CDROM_SetTray
801 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
803 #if defined(linux)
804 return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
805 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
806 return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
807 (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
808 (ioctl(fd, CDIOCPREVENT, NULL)));
809 #elif defined(__APPLE__)
810 if (doEject) return CDROM_GetStatusCode( ioctl( fd, DKIOCEJECT, NULL ) );
811 else return STATUS_NOT_SUPPORTED;
812 #else
813 FIXME("not supported on this O/S\n");
814 return STATUS_NOT_SUPPORTED;
815 #endif
818 /******************************************************************
819 * CDROM_ControlEjection
823 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
825 #if defined(linux)
826 return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
827 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
828 return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
829 #else
830 FIXME("not supported on this O/S\n");
831 return STATUS_NOT_SUPPORTED;
832 #endif
835 /******************************************************************
836 * CDROM_ReadTOC
840 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
842 NTSTATUS ret = STATUS_NOT_SUPPORTED;
844 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
845 return STATUS_INVALID_PARAMETER;
847 RtlEnterCriticalSection( &cache_section );
848 if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
850 *toc = cdrom_cache[dev].toc;
851 ret = STATUS_SUCCESS;
853 RtlLeaveCriticalSection( &cache_section );
854 return ret;
857 /******************************************************************
858 * CDROM_GetDiskData
862 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
864 CDROM_TOC toc;
865 NTSTATUS ret;
866 int i;
868 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
869 data->DiskData = 0;
870 for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
871 if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
872 data->DiskData |= CDROM_DISK_DATA_TRACK;
873 else
874 data->DiskData |= CDROM_DISK_AUDIO_TRACK;
876 return STATUS_SUCCESS;
879 /******************************************************************
880 * CDROM_ReadQChannel
884 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
885 SUB_Q_CHANNEL_DATA* data)
887 NTSTATUS ret = STATUS_NOT_SUPPORTED;
888 #ifdef linux
889 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
890 int io;
891 struct cdrom_subchnl sc;
892 sc.cdsc_format = CDROM_MSF;
894 io = ioctl(fd, CDROMSUBCHNL, &sc);
895 if (io == -1)
897 TRACE("opened or no_media (%s)!\n", strerror(errno));
898 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
899 CDROM_ClearCacheEntry(dev);
900 goto end;
903 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
905 switch (sc.cdsc_audiostatus) {
906 case CDROM_AUDIO_INVALID:
907 CDROM_ClearCacheEntry(dev);
908 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
909 break;
910 case CDROM_AUDIO_NO_STATUS:
911 CDROM_ClearCacheEntry(dev);
912 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
913 break;
914 case CDROM_AUDIO_PLAY:
915 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
916 break;
917 case CDROM_AUDIO_PAUSED:
918 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
919 break;
920 case CDROM_AUDIO_COMPLETED:
921 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
922 break;
923 case CDROM_AUDIO_ERROR:
924 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
925 break;
926 default:
927 TRACE("status=%02X !\n", sc.cdsc_audiostatus);
928 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
930 switch (fmt->Format)
932 case IOCTL_CDROM_CURRENT_POSITION:
933 RtlEnterCriticalSection( &cache_section );
934 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
935 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
936 data->CurrentPosition.Control = sc.cdsc_ctrl;
937 data->CurrentPosition.ADR = sc.cdsc_adr;
938 data->CurrentPosition.TrackNumber = sc.cdsc_trk;
939 data->CurrentPosition.IndexNumber = sc.cdsc_ind;
941 data->CurrentPosition.AbsoluteAddress[0] = 0;
942 data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute;
943 data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
944 data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
946 data->CurrentPosition.TrackRelativeAddress[0] = 0;
947 data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute;
948 data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
949 data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
951 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
953 else /* not playing */
955 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
956 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
958 RtlLeaveCriticalSection( &cache_section );
959 break;
960 case IOCTL_CDROM_MEDIA_CATALOG:
961 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
963 struct cdrom_mcn mcn;
964 if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
966 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
967 data->MediaCatalog.Mcval = 0; /* FIXME */
968 memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
969 data->MediaCatalog.MediaCatalog[14] = 0;
971 break;
972 case IOCTL_CDROM_TRACK_ISRC:
973 FIXME("TrackIsrc: NIY on linux\n");
974 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
975 data->TrackIsrc.Tcval = 0;
976 io = 0;
977 break;
980 end:
981 ret = CDROM_GetStatusCode(io);
982 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
983 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
984 int io;
985 struct ioc_read_subchannel read_sc;
986 struct cd_sub_channel_info sc;
988 read_sc.address_format = CD_MSF_FORMAT;
989 read_sc.track = 0;
990 read_sc.data_len = sizeof(sc);
991 read_sc.data = &sc;
992 switch (fmt->Format)
994 case IOCTL_CDROM_CURRENT_POSITION:
995 read_sc.data_format = CD_CURRENT_POSITION;
996 break;
997 case IOCTL_CDROM_MEDIA_CATALOG:
998 read_sc.data_format = CD_MEDIA_CATALOG;
999 break;
1000 case IOCTL_CDROM_TRACK_ISRC:
1001 read_sc.data_format = CD_TRACK_INFO;
1002 sc.what.track_info.track_number = data->TrackIsrc.Track;
1003 break;
1005 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1006 if (io == -1)
1008 TRACE("opened or no_media (%s)!\n", strerror(errno));
1009 CDROM_ClearCacheEntry(dev);
1010 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1011 goto end;
1014 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
1016 switch (sc.header.audio_status) {
1017 case CD_AS_AUDIO_INVALID:
1018 CDROM_ClearCacheEntry(dev);
1019 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
1020 break;
1021 case CD_AS_NO_STATUS:
1022 CDROM_ClearCacheEntry(dev);
1023 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1024 break;
1025 case CD_AS_PLAY_IN_PROGRESS:
1026 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
1027 break;
1028 case CD_AS_PLAY_PAUSED:
1029 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
1030 break;
1031 case CD_AS_PLAY_COMPLETED:
1032 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
1033 break;
1034 case CD_AS_PLAY_ERROR:
1035 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
1036 break;
1037 default:
1038 TRACE("status=%02X !\n", sc.header.audio_status);
1039 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1041 switch (fmt->Format)
1043 case IOCTL_CDROM_CURRENT_POSITION:
1044 RtlEnterCriticalSection( &cache_section );
1045 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
1046 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1047 data->CurrentPosition.Control = sc.what.position.control;
1048 data->CurrentPosition.ADR = sc.what.position.addr_type;
1049 data->CurrentPosition.TrackNumber = sc.what.position.track_number;
1050 data->CurrentPosition.IndexNumber = sc.what.position.index_number;
1052 data->CurrentPosition.AbsoluteAddress[0] = 0;
1053 data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
1054 data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
1055 data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
1056 data->CurrentPosition.TrackRelativeAddress[0] = 0;
1057 data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
1058 data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
1059 data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
1060 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
1062 else { /* not playing */
1063 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
1064 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
1066 RtlLeaveCriticalSection( &cache_section );
1067 break;
1068 case IOCTL_CDROM_MEDIA_CATALOG:
1069 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
1070 data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
1071 memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
1072 break;
1073 case IOCTL_CDROM_TRACK_ISRC:
1074 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
1075 data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
1076 memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
1077 break;
1080 end:
1081 ret = CDROM_GetStatusCode(io);
1082 #elif defined(__APPLE__)
1083 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
1084 int io = 0;
1085 union
1087 dk_cd_read_mcn_t mcn;
1088 dk_cd_read_isrc_t isrc;
1089 } ioc;
1090 /* We need IOCDAudioControl for IOCTL_CDROM_CURRENT_POSITION */
1091 if (fmt->Format == IOCTL_CDROM_CURRENT_POSITION)
1093 FIXME("NIY\n");
1094 return STATUS_NOT_SUPPORTED;
1096 /* No IOCDAudioControl support; just set the audio status to none */
1097 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1098 switch(fmt->Format)
1100 case IOCTL_CDROM_MEDIA_CATALOG:
1101 if ((io = ioctl(fd, DKIOCCDREADMCN, &ioc.mcn)) == -1) break;
1102 memcpy(data->MediaCatalog.MediaCatalog, ioc.mcn.mcn, kCDMCNMaxLength);
1103 data->MediaCatalog.Mcval = 1;
1104 break;
1105 case IOCTL_CDROM_TRACK_ISRC:
1106 ioc.isrc.track = fmt->Track;
1107 if ((io = ioctl(fd, DKIOCCDREADISRC, &ioc.isrc)) == -1) break;
1108 memcpy(data->TrackIsrc.TrackIsrc, ioc.isrc.isrc, kCDISRCMaxLength);
1109 data->TrackIsrc.Tcval = 1;
1110 data->TrackIsrc.Track = ioc.isrc.track;
1112 ret = CDROM_GetStatusCode(io);
1113 #endif
1114 return ret;
1117 /******************************************************************
1118 * CDROM_Verify
1119 * Implements: IOCTL_STORAGE_CHECK_VERIFY
1120 * IOCTL_CDROM_CHECK_VERIFY
1121 * IOCTL_DISK_CHECK_VERIFY
1124 static NTSTATUS CDROM_Verify(int dev, int fd)
1126 #if defined(linux)
1127 int ret;
1129 ret = ioctl(fd, CDROM_DRIVE_STATUS, NULL);
1130 if(ret == -1) {
1131 TRACE("ioctl CDROM_DRIVE_STATUS failed(%s)!\n", strerror(errno));
1132 return CDROM_GetStatusCode(ret);
1135 if(ret == CDS_DISC_OK)
1136 return STATUS_SUCCESS;
1137 else
1138 return STATUS_NO_MEDIA_IN_DEVICE;
1139 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1140 int ret;
1141 ret = ioctl(fd, CDIOCSTART, NULL);
1142 if(ret == 0)
1143 return STATUS_SUCCESS;
1144 else
1145 return STATUS_NO_MEDIA_IN_DEVICE;
1146 #elif defined(__APPLE__)
1147 /* At this point, we know that we have media, because in Mac OS X, the
1148 * device file is only created when media is present. */
1149 return STATUS_SUCCESS;
1150 #else
1151 FIXME("not supported on this O/S\n");
1152 return STATUS_NOT_SUPPORTED;
1153 #endif
1156 /******************************************************************
1157 * CDROM_PlayAudioMSF
1161 static NTSTATUS CDROM_PlayAudioMSF(int fd, const CDROM_PLAY_AUDIO_MSF* audio_msf)
1163 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1164 #ifdef linux
1165 struct cdrom_msf msf;
1166 int io;
1168 msf.cdmsf_min0 = audio_msf->StartingM;
1169 msf.cdmsf_sec0 = audio_msf->StartingS;
1170 msf.cdmsf_frame0 = audio_msf->StartingF;
1171 msf.cdmsf_min1 = audio_msf->EndingM;
1172 msf.cdmsf_sec1 = audio_msf->EndingS;
1173 msf.cdmsf_frame1 = audio_msf->EndingF;
1175 io = ioctl(fd, CDROMSTART);
1176 if (io == -1)
1178 WARN("motor doesn't start !\n");
1179 goto end;
1181 io = ioctl(fd, CDROMPLAYMSF, &msf);
1182 if (io == -1)
1184 WARN("device doesn't play !\n");
1185 goto end;
1187 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1188 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
1189 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
1190 end:
1191 ret = CDROM_GetStatusCode(io);
1192 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1193 struct ioc_play_msf msf;
1194 int io;
1196 msf.start_m = audio_msf->StartingM;
1197 msf.start_s = audio_msf->StartingS;
1198 msf.start_f = audio_msf->StartingF;
1199 msf.end_m = audio_msf->EndingM;
1200 msf.end_s = audio_msf->EndingS;
1201 msf.end_f = audio_msf->EndingF;
1203 io = ioctl(fd, CDIOCSTART, NULL);
1204 if (io == -1)
1206 WARN("motor doesn't start !\n");
1207 goto end;
1209 io = ioctl(fd, CDIOCPLAYMSF, &msf);
1210 if (io == -1)
1212 WARN("device doesn't play !\n");
1213 goto end;
1215 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1216 msf.start_m, msf.start_s, msf.start_f,
1217 msf.end_m, msf.end_s, msf.end_f);
1218 end:
1219 ret = CDROM_GetStatusCode(io);
1220 #endif
1221 return ret;
1224 /******************************************************************
1225 * CDROM_SeekAudioMSF
1229 static NTSTATUS CDROM_SeekAudioMSF(int dev, int fd, const CDROM_SEEK_AUDIO_MSF* audio_msf)
1231 CDROM_TOC toc;
1232 int i, io, frame;
1233 SUB_Q_CURRENT_POSITION *cp;
1234 #if defined(linux)
1235 struct cdrom_msf0 msf;
1236 struct cdrom_subchnl sc;
1237 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1238 struct ioc_play_msf msf;
1239 struct ioc_read_subchannel read_sc;
1240 struct cd_sub_channel_info sc;
1241 int final_frame;
1242 #endif
1244 /* Use the information on the TOC to compute the new current
1245 * position, which is shadowed on the cache. [Portable]. */
1246 frame = FRAME_OF_MSF(*audio_msf);
1248 if ((io = CDROM_ReadTOC(dev, fd, &toc)) != 0) return io;
1250 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
1251 if (FRAME_OF_TOC(toc,i)>frame) break;
1252 if (i <= toc.FirstTrack || i > toc.LastTrack+1)
1253 return STATUS_INVALID_PARAMETER;
1254 i--;
1255 RtlEnterCriticalSection( &cache_section );
1256 cp = &cdrom_cache[dev].CurrentPosition;
1257 cp->FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1258 cp->Control = toc.TrackData[i-toc.FirstTrack].Control;
1259 cp->ADR = toc.TrackData[i-toc.FirstTrack].Adr;
1260 cp->TrackNumber = toc.TrackData[i-toc.FirstTrack].TrackNumber;
1261 cp->IndexNumber = 0; /* FIXME: where do they keep these? */
1262 cp->AbsoluteAddress[0] = 0;
1263 cp->AbsoluteAddress[1] = toc.TrackData[i-toc.FirstTrack].Address[1];
1264 cp->AbsoluteAddress[2] = toc.TrackData[i-toc.FirstTrack].Address[2];
1265 cp->AbsoluteAddress[3] = toc.TrackData[i-toc.FirstTrack].Address[3];
1266 frame -= FRAME_OF_TOC(toc,i);
1267 cp->TrackRelativeAddress[0] = 0;
1268 MSF_OF_FRAME(cp->TrackRelativeAddress[1], frame);
1269 RtlLeaveCriticalSection( &cache_section );
1271 /* If playing, then issue a seek command, otherwise do nothing */
1272 #ifdef linux
1273 sc.cdsc_format = CDROM_MSF;
1275 io = ioctl(fd, CDROMSUBCHNL, &sc);
1276 if (io == -1)
1278 TRACE("opened or no_media (%s)!\n", strerror(errno));
1279 CDROM_ClearCacheEntry(dev);
1280 return CDROM_GetStatusCode(io);
1282 if (sc.cdsc_audiostatus==CDROM_AUDIO_PLAY)
1284 msf.minute = audio_msf->M;
1285 msf.second = audio_msf->S;
1286 msf.frame = audio_msf->F;
1287 return CDROM_GetStatusCode(ioctl(fd, CDROMSEEK, &msf));
1289 return STATUS_SUCCESS;
1290 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1291 read_sc.address_format = CD_MSF_FORMAT;
1292 read_sc.track = 0;
1293 read_sc.data_len = sizeof(sc);
1294 read_sc.data = &sc;
1295 read_sc.data_format = CD_CURRENT_POSITION;
1297 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1298 if (io == -1)
1300 TRACE("opened or no_media (%s)!\n", strerror(errno));
1301 CDROM_ClearCacheEntry(dev);
1302 return CDROM_GetStatusCode(io);
1304 if (sc.header.audio_status==CD_AS_PLAY_IN_PROGRESS)
1307 msf.start_m = audio_msf->M;
1308 msf.start_s = audio_msf->S;
1309 msf.start_f = audio_msf->F;
1310 final_frame = FRAME_OF_TOC(toc,toc.LastTrack+1)-1;
1311 MSF_OF_FRAME(msf.end_m, final_frame);
1313 return CDROM_GetStatusCode(ioctl(fd, CDIOCPLAYMSF, &msf));
1315 return STATUS_SUCCESS;
1316 #else
1317 FIXME("not supported on this O/S\n");
1318 return STATUS_NOT_SUPPORTED;
1319 #endif
1322 /******************************************************************
1323 * CDROM_PauseAudio
1327 static NTSTATUS CDROM_PauseAudio(int fd)
1329 #if defined(linux)
1330 return CDROM_GetStatusCode(ioctl(fd, CDROMPAUSE));
1331 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1332 return CDROM_GetStatusCode(ioctl(fd, CDIOCPAUSE, NULL));
1333 #else
1334 FIXME(": not supported on this O/S\n");
1335 return STATUS_NOT_SUPPORTED;
1336 #endif
1339 /******************************************************************
1340 * CDROM_ResumeAudio
1344 static NTSTATUS CDROM_ResumeAudio(int fd)
1346 #if defined(linux)
1347 return CDROM_GetStatusCode(ioctl(fd, CDROMRESUME));
1348 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1349 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESUME, NULL));
1350 #else
1351 FIXME("not supported on this O/S\n");
1352 return STATUS_NOT_SUPPORTED;
1353 #endif
1356 /******************************************************************
1357 * CDROM_StopAudio
1361 static NTSTATUS CDROM_StopAudio(int fd)
1363 #if defined(linux)
1364 return CDROM_GetStatusCode(ioctl(fd, CDROMSTOP));
1365 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1366 return CDROM_GetStatusCode(ioctl(fd, CDIOCSTOP, NULL));
1367 #else
1368 FIXME("not supported on this O/S\n");
1369 return STATUS_NOT_SUPPORTED;
1370 #endif
1373 /******************************************************************
1374 * CDROM_GetVolume
1378 static NTSTATUS CDROM_GetVolume(int fd, VOLUME_CONTROL* vc)
1380 #if defined(linux)
1381 struct cdrom_volctrl volc;
1382 int io;
1384 io = ioctl(fd, CDROMVOLREAD, &volc);
1385 if (io != -1)
1387 vc->PortVolume[0] = volc.channel0;
1388 vc->PortVolume[1] = volc.channel1;
1389 vc->PortVolume[2] = volc.channel2;
1390 vc->PortVolume[3] = volc.channel3;
1392 return CDROM_GetStatusCode(io);
1393 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1394 struct ioc_vol volc;
1395 int io;
1397 io = ioctl(fd, CDIOCGETVOL, &volc);
1398 if (io != -1)
1400 vc->PortVolume[0] = volc.vol[0];
1401 vc->PortVolume[1] = volc.vol[1];
1402 vc->PortVolume[2] = volc.vol[2];
1403 vc->PortVolume[3] = volc.vol[3];
1405 return CDROM_GetStatusCode(io);
1406 #else
1407 FIXME("not supported on this O/S\n");
1408 return STATUS_NOT_SUPPORTED;
1409 #endif
1412 /******************************************************************
1413 * CDROM_SetVolume
1417 static NTSTATUS CDROM_SetVolume(int fd, const VOLUME_CONTROL* vc)
1419 #if defined(linux)
1420 struct cdrom_volctrl volc;
1422 volc.channel0 = vc->PortVolume[0];
1423 volc.channel1 = vc->PortVolume[1];
1424 volc.channel2 = vc->PortVolume[2];
1425 volc.channel3 = vc->PortVolume[3];
1427 return CDROM_GetStatusCode(ioctl(fd, CDROMVOLCTRL, &volc));
1428 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1429 struct ioc_vol volc;
1431 volc.vol[0] = vc->PortVolume[0];
1432 volc.vol[1] = vc->PortVolume[1];
1433 volc.vol[2] = vc->PortVolume[2];
1434 volc.vol[3] = vc->PortVolume[3];
1436 return CDROM_GetStatusCode(ioctl(fd, CDIOCSETVOL, &volc));
1437 #else
1438 FIXME(": not supported on this O/S\n");
1439 return STATUS_NOT_SUPPORTED;
1440 #endif
1443 /******************************************************************
1444 * CDROM_RawRead
1446 * Some features of this IOCTL are rather poorly documented and
1447 * not really intuitive either:
1449 * 1. Although the DiskOffset parameter is meant to be a
1450 * byte offset into the disk, it is in fact the sector
1451 * number multiplied by 2048 regardless of the actual
1452 * sector size.
1454 * 2. The least significant 11 bits of DiskOffset are ignored.
1456 * 3. The TrackMode parameter has no effect on the sector
1457 * size. The entire raw sector (i.e. 2352 bytes of data)
1458 * is always returned. IMO the TrackMode is only used
1459 * to check the correct sector type.
1462 static NTSTATUS CDROM_RawRead(int fd, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
1464 int ret = STATUS_NOT_SUPPORTED;
1465 int io = -1;
1466 #ifdef __APPLE__
1467 dk_cd_read_t cdrd;
1468 #endif
1470 TRACE("RAW_READ_INFO: DiskOffset=%i,%i SectorCount=%i TrackMode=%i\n buffer=%p len=%i sz=%p\n",
1471 raw->DiskOffset.u.HighPart, raw->DiskOffset.u.LowPart, raw->SectorCount, raw->TrackMode, buffer, len, sz);
1473 if (len < raw->SectorCount * 2352) return STATUS_BUFFER_TOO_SMALL;
1475 #if defined(linux)
1476 if (raw->DiskOffset.u.HighPart & ~2047) {
1477 WARN("DiskOffset points to a sector >= 2**32\n");
1478 return ret;
1481 switch (raw->TrackMode)
1483 case YellowMode2:
1484 case XAForm2:
1486 DWORD lba = raw->DiskOffset.QuadPart >> 11;
1487 struct cdrom_msf* msf;
1488 PBYTE *bp; /* current buffer pointer */
1489 DWORD i;
1491 if ((lba + raw->SectorCount) >
1492 ((1 << 8*sizeof(msf->cdmsf_min0)) * CD_SECS * CD_FRAMES
1493 - CD_MSF_OFFSET)) {
1494 WARN("DiskOffset not accessible with MSF\n");
1495 return ret;
1498 /* Linux reads only one sector at a time.
1499 * ioctl CDROMREADRAW takes struct cdrom_msf as an argument
1500 * on the contrary to what header comments state.
1502 lba += CD_MSF_OFFSET;
1503 for (i = 0, bp = buffer; i < raw->SectorCount;
1504 i++, lba++, bp += 2352)
1506 msf = (struct cdrom_msf*)bp;
1507 msf->cdmsf_min0 = lba / CD_FRAMES / CD_SECS;
1508 msf->cdmsf_sec0 = lba / CD_FRAMES % CD_SECS;
1509 msf->cdmsf_frame0 = lba % CD_FRAMES;
1510 io = ioctl(fd, CDROMREADRAW, msf);
1511 if (io != 0)
1513 *sz = 2352 * i;
1514 return CDROM_GetStatusCode(io);
1517 break;
1520 case CDDA:
1522 struct cdrom_read_audio cdra;
1524 cdra.addr.lba = raw->DiskOffset.QuadPart >> 11;
1525 TRACE("reading at %u\n", cdra.addr.lba);
1526 cdra.addr_format = CDROM_LBA;
1527 cdra.nframes = raw->SectorCount;
1528 cdra.buf = buffer;
1529 io = ioctl(fd, CDROMREADAUDIO, &cdra);
1530 break;
1533 default:
1534 FIXME("NIY: %d\n", raw->TrackMode);
1535 return STATUS_INVALID_PARAMETER;
1537 #elif defined(__APPLE__)
1538 /* Mac OS lets us read multiple parts of the sector at a time.
1539 * We can read all the sectors in at once, unlike Linux.
1541 memset(&cdrd, 0, sizeof(cdrd));
1542 cdrd.offset = (raw->DiskOffset.QuadPart >> 11) * kCDSectorSizeWhole;
1543 cdrd.buffer = buffer;
1544 cdrd.bufferLength = raw->SectorCount * kCDSectorSizeWhole;
1545 switch (raw->TrackMode)
1547 case YellowMode2:
1548 cdrd.sectorType = kCDSectorTypeMode2;
1549 cdrd.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader | kCDSectorAreaUser;
1550 break;
1552 case XAForm2:
1553 cdrd.sectorType = kCDSectorTypeMode2Form2;
1554 cdrd.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader | kCDSectorAreaSubHeader | kCDSectorAreaUser;
1555 break;
1557 case CDDA:
1558 cdrd.sectorType = kCDSectorTypeCDDA;
1559 cdrd.sectorArea = kCDSectorAreaUser;
1560 break;
1562 default:
1563 FIXME("NIY: %d\n", raw->TrackMode);
1564 return STATUS_INVALID_PARAMETER;
1566 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1567 if (io != 0)
1569 *sz = cdrd.bufferLength;
1570 return CDROM_GetStatusCode(io);
1572 #else
1573 switch (raw->TrackMode)
1575 case YellowMode2:
1576 FIXME("YellowMode2: NIY\n");
1577 return ret;
1578 case XAForm2:
1579 FIXME("XAForm2: NIY\n");
1580 return ret;
1581 case CDDA:
1582 FIXME("CDDA: NIY\n");
1583 return ret;
1584 default:
1585 FIXME("NIY: %d\n", raw->TrackMode);
1586 return STATUS_INVALID_PARAMETER;
1588 #endif
1590 *sz = 2352 * raw->SectorCount;
1591 ret = CDROM_GetStatusCode(io);
1592 return ret;
1595 /******************************************************************
1596 * CDROM_ScsiPassThroughDirect
1597 * Implements IOCTL_SCSI_PASS_THROUGH_DIRECT
1600 static NTSTATUS CDROM_ScsiPassThroughDirect(int fd, PSCSI_PASS_THROUGH_DIRECT pPacket)
1602 int ret = STATUS_NOT_SUPPORTED;
1603 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1604 sg_io_hdr_t cmd;
1605 int io;
1606 #elif defined HAVE_SCSIREQ_T_CMD
1607 scsireq_t cmd;
1608 int io;
1609 #elif defined __APPLE__
1610 dk_scsi_command_t cmd;
1611 int io;
1612 #endif
1614 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1615 return STATUS_BUFFER_TOO_SMALL;
1617 if (pPacket->CdbLength > 16)
1618 return STATUS_INVALID_PARAMETER;
1620 #ifdef SENSEBUFLEN
1621 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1622 return STATUS_INVALID_PARAMETER;
1623 #elif defined HAVE_REQUEST_SENSE
1624 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1625 return STATUS_INVALID_PARAMETER;
1626 #endif
1628 if (pPacket->DataTransferLength > 0 && !pPacket->DataBuffer)
1629 return STATUS_INVALID_PARAMETER;
1631 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1632 RtlZeroMemory(&cmd, sizeof(cmd));
1634 cmd.interface_id = 'S';
1635 cmd.cmd_len = pPacket->CdbLength;
1636 cmd.mx_sb_len = pPacket->SenseInfoLength;
1637 cmd.dxfer_len = pPacket->DataTransferLength;
1638 cmd.dxferp = pPacket->DataBuffer;
1639 cmd.cmdp = pPacket->Cdb;
1640 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1641 cmd.timeout = pPacket->TimeOutValue*1000;
1643 switch (pPacket->DataIn)
1645 case SCSI_IOCTL_DATA_IN:
1646 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1647 break;
1648 case SCSI_IOCTL_DATA_OUT:
1649 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1650 break;
1651 case SCSI_IOCTL_DATA_UNSPECIFIED:
1652 cmd.dxfer_direction = SG_DXFER_NONE;
1653 break;
1654 default:
1655 return STATUS_INVALID_PARAMETER;
1658 io = ioctl(fd, SG_IO, &cmd);
1660 pPacket->ScsiStatus = cmd.status;
1661 pPacket->DataTransferLength -= cmd.resid;
1662 pPacket->SenseInfoLength = cmd.sb_len_wr;
1664 ret = CDROM_GetStatusCode(io);
1666 #elif defined HAVE_SCSIREQ_T_CMD
1668 memset(&cmd, 0, sizeof(cmd));
1669 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1671 cmd.cmdlen = pPacket->CdbLength;
1672 cmd.databuf = pPacket->DataBuffer;
1673 cmd.datalen = pPacket->DataTransferLength;
1674 cmd.senselen = pPacket->SenseInfoLength;
1675 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1677 switch (pPacket->DataIn)
1679 case SCSI_IOCTL_DATA_OUT:
1680 cmd.flags |= SCCMD_WRITE;
1681 break;
1682 case SCSI_IOCTL_DATA_IN:
1683 cmd.flags |= SCCMD_READ;
1684 break;
1685 case SCSI_IOCTL_DATA_UNSPECIFIED:
1686 cmd.flags = 0;
1687 break;
1688 default:
1689 return STATUS_INVALID_PARAMETER;
1692 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1694 switch (cmd.retsts)
1696 case SCCMD_OK: break;
1697 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1698 break;
1699 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1700 break;
1701 case SCCMD_SENSE: break;
1702 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1703 break;
1706 if (pPacket->SenseInfoLength != 0)
1708 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1709 cmd.sense, pPacket->SenseInfoLength);
1712 pPacket->ScsiStatus = cmd.status;
1714 ret = CDROM_GetStatusCode(io);
1716 #elif defined(__APPLE__)
1718 memset(&cmd, 0, sizeof(cmd));
1719 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1721 cmd.cdbSize = pPacket->CdbLength;
1722 cmd.buffer = pPacket->DataBuffer;
1723 cmd.bufferSize = pPacket->DataTransferLength;
1724 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1725 cmd.senseLen = pPacket->SenseInfoLength;
1726 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1728 switch (pPacket->DataIn)
1730 case SCSI_IOCTL_DATA_OUT:
1731 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
1732 break;
1733 case SCSI_IOCTL_DATA_IN:
1734 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
1735 break;
1736 case SCSI_IOCTL_DATA_UNSPECIFIED:
1737 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
1738 break;
1739 default:
1740 return STATUS_INVALID_PARAMETER;
1743 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
1745 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
1747 /* Command failed */
1748 switch (cmd.status)
1750 case kSCSITaskStatus_TaskTimeoutOccurred: return STATUS_TIMEOUT;
1751 break;
1752 case kSCSITaskStatus_ProtocolTimeoutOccurred: return STATUS_IO_TIMEOUT;
1753 break;
1754 case kSCSITaskStatus_DeviceNotResponding: return STATUS_DEVICE_BUSY;
1755 break;
1756 case kSCSITaskStatus_DeviceNotPresent:
1757 return STATUS_NO_SUCH_DEVICE;
1758 break;
1759 case kSCSITaskStatus_DeliveryFailure:
1760 return STATUS_DEVICE_PROTOCOL_ERROR;
1761 break;
1762 case kSCSITaskStatus_No_Status:
1763 default:
1764 return STATUS_UNSUCCESSFUL;
1765 break;
1769 if (cmd.status != kSCSITaskStatus_No_Status)
1770 pPacket->ScsiStatus = cmd.status;
1772 ret = CDROM_GetStatusCode(io);
1773 #endif
1774 return ret;
1777 /******************************************************************
1778 * CDROM_ScsiPassThrough
1779 * Implements IOCTL_SCSI_PASS_THROUGH
1782 static NTSTATUS CDROM_ScsiPassThrough(int fd, PSCSI_PASS_THROUGH pPacket)
1784 int ret = STATUS_NOT_SUPPORTED;
1785 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1786 sg_io_hdr_t cmd;
1787 int io;
1788 #elif defined HAVE_SCSIREQ_T_CMD
1789 scsireq_t cmd;
1790 int io;
1791 #elif defined __APPLE__
1792 dk_scsi_command_t cmd;
1793 int io;
1794 #endif
1796 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1797 return STATUS_BUFFER_TOO_SMALL;
1799 if (pPacket->CdbLength > 16)
1800 return STATUS_INVALID_PARAMETER;
1802 #ifdef SENSEBUFLEN
1803 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1804 return STATUS_INVALID_PARAMETER;
1805 #elif defined HAVE_REQUEST_SENSE
1806 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1807 return STATUS_INVALID_PARAMETER;
1808 #endif
1810 if (pPacket->DataTransferLength > 0 && pPacket->DataBufferOffset < sizeof(SCSI_PASS_THROUGH))
1811 return STATUS_INVALID_PARAMETER;
1813 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1814 RtlZeroMemory(&cmd, sizeof(cmd));
1816 cmd.interface_id = 'S';
1817 cmd.dxfer_len = pPacket->DataTransferLength;
1818 cmd.dxferp = (char*)pPacket + pPacket->DataBufferOffset;
1819 cmd.cmd_len = pPacket->CdbLength;
1820 cmd.cmdp = pPacket->Cdb;
1821 cmd.mx_sb_len = pPacket->SenseInfoLength;
1822 cmd.timeout = pPacket->TimeOutValue*1000;
1824 if(cmd.mx_sb_len > 0)
1825 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1827 switch (pPacket->DataIn)
1829 case SCSI_IOCTL_DATA_IN:
1830 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1831 break;
1832 case SCSI_IOCTL_DATA_OUT:
1833 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1834 break;
1835 case SCSI_IOCTL_DATA_UNSPECIFIED:
1836 cmd.dxfer_direction = SG_DXFER_NONE;
1837 break;
1838 default:
1839 return STATUS_INVALID_PARAMETER;
1842 io = ioctl(fd, SG_IO, &cmd);
1844 pPacket->ScsiStatus = cmd.status;
1845 pPacket->DataTransferLength -= cmd.resid;
1846 pPacket->SenseInfoLength = cmd.sb_len_wr;
1848 ret = CDROM_GetStatusCode(io);
1850 #elif defined HAVE_SCSIREQ_T_CMD
1852 memset(&cmd, 0, sizeof(cmd));
1853 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1855 if ( pPacket->DataBufferOffset > 0x1000 )
1857 cmd.databuf = (void*)pPacket->DataBufferOffset;
1859 else
1861 cmd.databuf = (char*)pPacket + pPacket->DataBufferOffset;
1864 cmd.cmdlen = pPacket->CdbLength;
1865 cmd.datalen = pPacket->DataTransferLength;
1866 cmd.senselen = pPacket->SenseInfoLength;
1867 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1869 switch (pPacket->DataIn)
1871 case SCSI_IOCTL_DATA_OUT:
1872 cmd.flags |= SCCMD_WRITE;
1873 break;
1874 case SCSI_IOCTL_DATA_IN:
1875 cmd.flags |= SCCMD_READ;
1876 break;
1877 case SCSI_IOCTL_DATA_UNSPECIFIED:
1878 cmd.flags = 0;
1879 break;
1880 default:
1881 return STATUS_INVALID_PARAMETER;
1884 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1886 switch (cmd.retsts)
1888 case SCCMD_OK: break;
1889 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1890 break;
1891 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1892 break;
1893 case SCCMD_SENSE: break;
1894 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1895 break;
1898 if (pPacket->SenseInfoLength != 0)
1900 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1901 cmd.sense, pPacket->SenseInfoLength);
1904 pPacket->ScsiStatus = cmd.status;
1906 ret = CDROM_GetStatusCode(io);
1908 #elif defined(__APPLE__)
1910 memset(&cmd, 0, sizeof(cmd));
1911 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1913 cmd.cdbSize = pPacket->CdbLength;
1914 cmd.buffer = (char*)pPacket + pPacket->DataBufferOffset;
1915 cmd.bufferSize = pPacket->DataTransferLength;
1916 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1917 cmd.senseLen = pPacket->SenseInfoLength;
1918 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1920 switch (pPacket->DataIn)
1922 case SCSI_IOCTL_DATA_OUT:
1923 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
1924 break;
1925 case SCSI_IOCTL_DATA_IN:
1926 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
1927 break;
1928 case SCSI_IOCTL_DATA_UNSPECIFIED:
1929 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
1930 break;
1931 default:
1932 return STATUS_INVALID_PARAMETER;
1935 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
1937 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
1939 /* Command failed */
1940 switch (cmd.status)
1942 case kSCSITaskStatus_TaskTimeoutOccurred:
1943 return STATUS_TIMEOUT;
1944 break;
1945 case kSCSITaskStatus_ProtocolTimeoutOccurred:
1946 return STATUS_IO_TIMEOUT;
1947 break;
1948 case kSCSITaskStatus_DeviceNotResponding:
1949 return STATUS_DEVICE_BUSY;
1950 break;
1951 case kSCSITaskStatus_DeviceNotPresent:
1952 return STATUS_NO_SUCH_DEVICE;
1953 break;
1954 case kSCSITaskStatus_DeliveryFailure:
1955 return STATUS_DEVICE_PROTOCOL_ERROR;
1956 break;
1957 case kSCSITaskStatus_No_Status:
1958 default:
1959 return STATUS_UNSUCCESSFUL;
1960 break;
1964 if (cmd.status != kSCSITaskStatus_No_Status)
1965 pPacket->ScsiStatus = cmd.status;
1967 ret = CDROM_GetStatusCode(io);
1968 #endif
1969 return ret;
1972 /******************************************************************
1973 * CDROM_ScsiGetCaps
1977 static NTSTATUS CDROM_ScsiGetCaps(int fd, PIO_SCSI_CAPABILITIES caps)
1979 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
1981 #ifdef SG_SCATTER_SZ
1982 caps->Length = sizeof(*caps);
1983 caps->MaximumTransferLength = SG_SCATTER_SZ; /* FIXME */
1984 caps->MaximumPhysicalPages = SG_SCATTER_SZ / page_size;
1985 caps->SupportedAsynchronousEvents = TRUE;
1986 caps->AlignmentMask = page_size;
1987 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
1988 caps->AdapterScansDown = FALSE; /* FIXME ? */
1989 caps->AdapterUsesPio = FALSE; /* FIXME ? */
1990 ret = STATUS_SUCCESS;
1991 #elif defined __APPLE__
1992 uint64_t bytesr, bytesw, align;
1993 int io = ioctl(fd, DKIOCGETMAXBYTECOUNTREAD, &bytesr);
1994 if (io != 0) return CDROM_GetStatusCode(io);
1995 io = ioctl(fd, DKIOCGETMAXBYTECOUNTWRITE, &bytesw);
1996 if (io != 0) return CDROM_GetStatusCode(io);
1997 io = ioctl(fd, DKIOCGETMINSEGMENTALIGNMENTBYTECOUNT, &align);
1998 if (io != 0) return CDROM_GetStatusCode(io);
1999 caps->Length = sizeof(*caps);
2000 caps->MaximumTransferLength = bytesr < bytesw ? bytesr : bytesw;
2001 caps->MaximumPhysicalPages = caps->MaximumTransferLength / page_size;
2002 caps->SupportedAsynchronousEvents = TRUE;
2003 caps->AlignmentMask = align-1;
2004 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
2005 caps->AdapterScansDown = FALSE; /* FIXME ? */
2006 caps->AdapterUsesPio = FALSE; /* FIXME ? */
2007 ret = STATUS_SUCCESS;
2008 #else
2009 FIXME("Unimplemented\n");
2010 #endif
2011 return ret;
2014 /******************************************************************
2015 * CDROM_GetAddress
2017 * implements IOCTL_SCSI_GET_ADDRESS
2019 static NTSTATUS CDROM_GetAddress(int fd, SCSI_ADDRESS* address)
2021 UCHAR portnum, busid, targetid, lun;
2023 address->Length = sizeof(SCSI_ADDRESS);
2024 if ( ! CDROM_GetInterfaceInfo(fd, &portnum, &busid, &targetid, &lun))
2025 return STATUS_NOT_SUPPORTED;
2027 address->PortNumber = portnum; /* primary=0 secondary=1 for ide */
2028 address->PathId = busid; /* always 0 for ide */
2029 address->TargetId = targetid; /* master=0 slave=1 for ide */
2030 address->Lun = lun;
2031 return STATUS_SUCCESS;
2034 /******************************************************************
2035 * DVD_StartSession
2039 static NTSTATUS DVD_StartSession(int fd, const DVD_SESSION_ID *sid_in, PDVD_SESSION_ID sid_out)
2041 #if defined(linux)
2042 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2043 dvd_authinfo auth_info;
2045 memset( &auth_info, 0, sizeof( auth_info ) );
2046 auth_info.type = DVD_LU_SEND_AGID;
2047 if (sid_in) auth_info.lsa.agid = *(const int*)sid_in; /* ?*/
2049 TRACE("fd 0x%08x\n",fd);
2050 ret =CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2051 *sid_out = auth_info.lsa.agid;
2052 return ret;
2053 #elif defined(__APPLE__)
2054 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2055 dk_dvd_report_key_t dvdrk;
2056 DVDAuthenticationGrantIDInfo agid_info;
2058 dvdrk.format = kDVDKeyFormatAGID_CSS;
2059 dvdrk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2060 if(sid_in) dvdrk.grantID = *(uint8_t*)sid_in; /* ? */
2061 dvdrk.bufferLength = sizeof(DVDAuthenticationGrantIDInfo);
2062 dvdrk.buffer = &agid_info;
2064 ret = CDROM_GetStatusCode(ioctl(fd, DKIOCDVDREPORTKEY, &dvdrk));
2065 *sid_out = agid_info.grantID;
2066 return ret;
2067 #else
2068 FIXME("not supported on this O/S\n");
2069 return STATUS_NOT_SUPPORTED;
2070 #endif
2073 /******************************************************************
2074 * DVD_EndSession
2078 static NTSTATUS DVD_EndSession(int fd, const DVD_SESSION_ID *sid)
2080 #if defined(linux)
2081 dvd_authinfo auth_info;
2083 memset( &auth_info, 0, sizeof( auth_info ) );
2084 auth_info.type = DVD_INVALIDATE_AGID;
2085 auth_info.lsa.agid = *(const int*)sid;
2087 TRACE("\n");
2088 return CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2089 #elif defined(__APPLE__)
2090 dk_dvd_send_key_t dvdsk;
2092 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2093 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2094 dvdsk.grantID = (uint8_t)*sid;
2096 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2097 #else
2098 FIXME("not supported on this O/S\n");
2099 return STATUS_NOT_SUPPORTED;
2100 #endif
2103 /******************************************************************
2104 * DVD_SendKey
2108 static NTSTATUS DVD_SendKey(int fd, const DVD_COPY_PROTECT_KEY *key)
2110 #if defined(linux)
2111 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2112 dvd_authinfo auth_info;
2114 memset( &auth_info, 0, sizeof( auth_info ) );
2115 switch (key->KeyType)
2117 case DvdChallengeKey:
2118 auth_info.type = DVD_HOST_SEND_CHALLENGE;
2119 auth_info.hsc.agid = (int)key->SessionId;
2120 TRACE("DvdChallengeKey ioc 0x%x\n", DVD_AUTH );
2121 memcpy( auth_info.hsc.chal, key->KeyData, DVD_CHALLENGE_SIZE );
2122 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2123 break;
2124 case DvdBusKey2:
2125 auth_info.type = DVD_HOST_SEND_KEY2;
2126 auth_info.hsk.agid = (int)key->SessionId;
2128 memcpy( auth_info.hsk.key, key->KeyData, DVD_KEY_SIZE );
2130 TRACE("DvdBusKey2\n");
2131 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2132 break;
2134 default:
2135 FIXME("Unknown Keytype 0x%x\n",key->KeyType);
2137 return ret;
2138 #elif defined(__APPLE__)
2139 dk_dvd_send_key_t dvdsk;
2140 union
2142 DVDChallengeKeyInfo chal;
2143 DVDKey2Info key2;
2144 } key_desc;
2146 switch(key->KeyType)
2148 case DvdChallengeKey:
2149 dvdsk.format = kDVDKeyFormatChallengeKey;
2150 dvdsk.bufferLength = sizeof(key_desc.chal);
2151 dvdsk.buffer = &key_desc.chal;
2152 OSWriteBigInt16(key_desc.chal.dataLength, 0, key->KeyLength);
2153 memcpy(key_desc.chal.challengeKeyValue, key->KeyData, key->KeyLength);
2154 break;
2155 case DvdBusKey2:
2156 dvdsk.format = kDVDKeyFormatKey2;
2157 dvdsk.bufferLength = sizeof(key_desc.key2);
2158 dvdsk.buffer = &key_desc.key2;
2159 OSWriteBigInt16(key_desc.key2.dataLength, 0, key->KeyLength);
2160 memcpy(key_desc.key2.key2Value, key->KeyData, key->KeyLength);
2161 break;
2162 case DvdInvalidateAGID:
2163 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2164 break;
2165 case DvdBusKey1:
2166 case DvdTitleKey:
2167 case DvdAsf:
2168 case DvdGetRpcKey:
2169 case DvdDiskKey:
2170 ERR("attempted to write read-only key type 0x%x\n", key->KeyType);
2171 return STATUS_NOT_SUPPORTED;
2172 case DvdSetRpcKey:
2173 FIXME("DvdSetRpcKey NIY\n");
2174 return STATUS_NOT_SUPPORTED;
2175 default:
2176 FIXME("got unknown key type 0x%x\n", key->KeyType);
2177 return STATUS_NOT_SUPPORTED;
2179 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2180 dvdsk.grantID = (uint8_t)key->SessionId;
2182 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2183 #else
2184 FIXME("not supported on this O/S\n");
2185 return STATUS_NOT_SUPPORTED;
2186 #endif
2189 /******************************************************************
2190 * DVD_ReadKey
2194 static NTSTATUS DVD_ReadKey(int fd, PDVD_COPY_PROTECT_KEY key)
2196 #if defined(linux)
2197 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2198 dvd_struct dvd;
2199 dvd_authinfo auth_info;
2201 memset( &dvd, 0, sizeof( dvd_struct ) );
2202 memset( &auth_info, 0, sizeof( auth_info ) );
2203 switch (key->KeyType)
2205 case DvdDiskKey:
2207 dvd.type = DVD_STRUCT_DISCKEY;
2208 dvd.disckey.agid = (int)key->SessionId;
2209 memset( dvd.disckey.value, 0, DVD_DISCKEY_SIZE );
2211 TRACE("DvdDiskKey\n");
2212 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2213 if (ret == STATUS_SUCCESS)
2214 memcpy(key->KeyData,dvd.disckey.value,DVD_DISCKEY_SIZE);
2215 break;
2216 case DvdTitleKey:
2217 auth_info.type = DVD_LU_SEND_TITLE_KEY;
2218 auth_info.lstk.agid = (int)key->SessionId;
2219 auth_info.lstk.lba = (int)(key->Parameters.TitleOffset.QuadPart>>11);
2220 TRACE("DvdTitleKey session %d Quadpart 0x%08lx offset 0x%08x\n",
2221 (int)key->SessionId, (long)key->Parameters.TitleOffset.QuadPart,
2222 auth_info.lstk.lba);
2223 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2224 if (ret == STATUS_SUCCESS)
2225 memcpy(key->KeyData, auth_info.lstk.title_key, DVD_KEY_SIZE );
2226 break;
2227 case DvdChallengeKey:
2229 auth_info.type = DVD_LU_SEND_CHALLENGE;
2230 auth_info.lsc.agid = (int)key->SessionId;
2232 TRACE("DvdChallengeKey\n");
2233 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2234 if (ret == STATUS_SUCCESS)
2235 memcpy( key->KeyData, auth_info.lsc.chal, DVD_CHALLENGE_SIZE );
2236 break;
2237 case DvdAsf:
2238 auth_info.type = DVD_LU_SEND_ASF;
2239 TRACE("DvdAsf\n");
2240 auth_info.lsasf.asf=((PDVD_ASF)key->KeyData)->SuccessFlag;
2241 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2242 ((PDVD_ASF)key->KeyData)->SuccessFlag = auth_info.lsasf.asf;
2243 break;
2244 case DvdBusKey1:
2245 auth_info.type = DVD_LU_SEND_KEY1;
2246 auth_info.lsk.agid = (int)key->SessionId;
2248 TRACE("DvdBusKey1\n");
2249 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2251 if (ret == STATUS_SUCCESS)
2252 memcpy( key->KeyData, auth_info.lsk.key, DVD_KEY_SIZE );
2253 break;
2254 case DvdGetRpcKey:
2255 auth_info.type = DVD_LU_SEND_RPC_STATE;
2257 TRACE("DvdGetRpcKey\n");
2258 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2260 if (ret == STATUS_SUCCESS)
2262 ((PDVD_RPC_KEY)key->KeyData)->TypeCode = auth_info.lrpcs.type;
2263 ((PDVD_RPC_KEY)key->KeyData)->RegionMask = auth_info.lrpcs.region_mask;
2264 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme = auth_info.lrpcs.rpc_scheme;
2265 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable = auth_info.lrpcs.ucca;
2266 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable = auth_info.lrpcs.vra;
2268 break;
2269 default:
2270 FIXME("Unknown keytype 0x%x\n",key->KeyType);
2272 return ret;
2273 #elif defined(__APPLE__)
2274 union
2276 dk_dvd_report_key_t key;
2277 dk_dvd_read_structure_t disk_key;
2278 } ioc;
2279 union
2281 DVDDiscKeyInfo disk_key;
2282 DVDChallengeKeyInfo chal;
2283 DVDKey1Info key1;
2284 DVDTitleKeyInfo title;
2285 DVDAuthenticationSuccessFlagInfo asf;
2286 DVDRegionPlaybackControlInfo rpc;
2287 } desc;
2288 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2290 switch(key->KeyType)
2292 case DvdChallengeKey:
2293 ioc.key.format = kDVDKeyFormatChallengeKey;
2294 ioc.key.grantID = (uint8_t)key->SessionId;
2295 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2296 ioc.key.bufferLength = sizeof(desc.chal);
2297 ioc.key.buffer = &desc.chal;
2298 OSWriteBigInt16(desc.chal.dataLength, 0, key->KeyLength);
2299 break;
2300 case DvdBusKey1:
2301 ioc.key.format = kDVDKeyFormatKey1;
2302 ioc.key.grantID = (uint8_t)key->SessionId;
2303 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2304 ioc.key.bufferLength = sizeof(desc.key1);
2305 ioc.key.buffer = &desc.key1;
2306 OSWriteBigInt16(desc.key1.dataLength, 0, key->KeyLength);
2307 break;
2308 case DvdTitleKey:
2309 ioc.key.format = kDVDKeyFormatTitleKey;
2310 ioc.key.grantID = (uint8_t)key->SessionId;
2311 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2312 ioc.key.bufferLength = sizeof(desc.title);
2313 ioc.key.buffer = &desc.title;
2314 ioc.key.address = (uint32_t)(key->Parameters.TitleOffset.QuadPart>>11);
2315 OSWriteBigInt16(desc.title.dataLength, 0, key->KeyLength);
2316 break;
2317 case DvdAsf:
2318 ioc.key.format = kDVDKeyFormatASF;
2319 ioc.key.grantID = (uint8_t)key->SessionId;
2320 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2321 ioc.key.bufferLength = sizeof(desc.asf);
2322 ioc.key.buffer = &desc.asf;
2323 OSWriteBigInt16(desc.asf.dataLength, 0, key->KeyLength);
2324 break;
2325 case DvdGetRpcKey:
2326 ioc.key.format = kDVDKeyFormatRegionState;
2327 ioc.key.grantID = (uint8_t)key->SessionId;
2328 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2329 ioc.key.bufferLength = sizeof(desc.rpc);
2330 ioc.key.buffer = &desc.rpc;
2331 OSWriteBigInt16(desc.rpc.dataLength, 0, key->KeyLength);
2332 break;
2333 case DvdDiskKey:
2334 ioc.disk_key.format = kDVDStructureFormatDiscKeyInfo;
2335 ioc.disk_key.grantID = (uint8_t)key->SessionId;
2336 ioc.disk_key.bufferLength = sizeof(desc.disk_key);
2337 ioc.disk_key.buffer = &desc.disk_key;
2338 break;
2339 case DvdInvalidateAGID:
2340 ioc.key.format = kDVDKeyFormatAGID_Invalidate;
2341 ioc.key.grantID = (uint8_t)key->SessionId;
2342 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2343 break;
2344 case DvdBusKey2:
2345 case DvdSetRpcKey:
2346 ERR("attempted to read write-only key type 0x%x\n", key->KeyType);
2347 return STATUS_NOT_SUPPORTED;
2348 default:
2349 FIXME("got unknown key type 0x%x\n", key->KeyType);
2350 return STATUS_NOT_SUPPORTED;
2353 ret = CDROM_GetStatusCode(ioctl(fd, (key->KeyType == DvdDiskKey ? DKIOCDVDREADSTRUCTURE : DKIOCDVDREPORTKEY), &ioc));
2355 if (ret == STATUS_SUCCESS)
2357 switch(key->KeyType)
2359 case DvdChallengeKey:
2360 key->KeyLength = OSReadBigInt16(desc.chal.dataLength, 0);
2361 memcpy(key->KeyData, desc.chal.challengeKeyValue, key->KeyLength);
2362 break;
2363 case DvdBusKey1:
2364 key->KeyLength = OSReadBigInt16(desc.key1.dataLength, 0);
2365 memcpy(key->KeyData, desc.key1.key1Value, key->KeyLength);
2366 break;
2367 case DvdTitleKey:
2368 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2369 memcpy(key->KeyData, desc.title.titleKeyValue, key->KeyLength);
2370 key->KeyFlags = 0;
2371 if (desc.title.CPM)
2373 /*key->KeyFlags |= DVD_COPYRIGHTED;*/
2374 if (desc.title.CP_SEC) key->KeyFlags |= DVD_SECTOR_PROTECTED;
2375 /*else key->KeyFlags |= DVD_SECTOR_NOT_PROTECTED;*/
2376 #if 0
2377 switch (desc.title.CGMS)
2379 case 0:
2380 key->KeyFlags |= DVD_CGMS_COPY_PERMITTED;
2381 break;
2382 case 2:
2383 key->KeyFlags |= DVD_CGMS_COPY_ONCE;
2384 break;
2385 case 3:
2386 key->KeyFlags |= DVD_CGMS_NO_COPY;
2387 break;
2389 #endif
2390 } /*else key->KeyFlags |= DVD_NOT_COPYRIGHTED;*/
2391 break;
2392 case DvdAsf:
2393 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2394 ((PDVD_ASF)key->KeyData)->SuccessFlag = desc.asf.successFlag;
2395 break;
2396 case DvdGetRpcKey:
2397 key->KeyLength = OSReadBigInt16(desc.rpc.dataLength, 0);
2398 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable =
2399 desc.rpc.numberUserResets;
2400 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable =
2401 desc.rpc.numberVendorResets;
2402 ((PDVD_RPC_KEY)key->KeyData)->TypeCode =
2403 desc.rpc.typeCode;
2404 ((PDVD_RPC_KEY)key->KeyData)->RegionMask =
2405 desc.rpc.driveRegion;
2406 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme =
2407 desc.rpc.rpcScheme;
2408 case DvdDiskKey:
2409 key->KeyLength = OSReadBigInt16(desc.disk_key.dataLength, 0);
2410 memcpy(key->KeyData, desc.disk_key.discKeyStructures, key->KeyLength);
2411 break;
2412 case DvdBusKey2:
2413 case DvdSetRpcKey:
2414 case DvdInvalidateAGID:
2415 default:
2416 /* Silence warning */
2420 return ret;
2421 #else
2422 FIXME("not supported on this O/S\n");
2423 return STATUS_NOT_SUPPORTED;
2424 #endif
2427 /******************************************************************
2428 * DVD_GetRegion
2430 * This IOCTL combines information from both IOCTL_DVD_READ_KEY
2431 * with key type DvdGetRpcKey and IOCTL_DVD_READ_STRUCTURE with
2432 * structure type DvdCopyrightInformation into one structure.
2434 static NTSTATUS DVD_GetRegion(int fd, PDVD_REGION region)
2436 #if defined(linux)
2437 NTSTATUS ret;
2438 dvd_struct dvd;
2439 dvd_authinfo auth_info;
2441 dvd.type = DVD_STRUCT_COPYRIGHT;
2442 dvd.copyright.layer_num = 0;
2443 auth_info.type = DVD_LU_SEND_RPC_STATE;
2445 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2447 if (ret == STATUS_SUCCESS)
2449 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2451 if (ret == STATUS_SUCCESS)
2453 region->CopySystem = dvd.copyright.cpst;
2454 region->RegionData = dvd.copyright.rmi;
2455 region->SystemRegion = auth_info.lrpcs.region_mask;
2456 region->ResetCount = auth_info.lrpcs.ucca;
2459 return ret;
2460 #elif defined(__APPLE__)
2461 dk_dvd_report_key_t key;
2462 dk_dvd_read_structure_t dvd;
2463 DVDRegionPlaybackControlInfo rpc;
2464 DVDCopyrightInfo copy;
2465 NTSTATUS ret;
2467 key.format = kDVDKeyFormatRegionState;
2468 key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2469 key.bufferLength = sizeof(rpc);
2470 key.buffer = &rpc;
2471 dvd.format = kDVDStructureFormatCopyrightInfo;
2472 dvd.bufferLength = sizeof(copy);
2473 dvd.buffer = &copy;
2475 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREPORTKEY, &key ));
2477 if (ret == STATUS_SUCCESS)
2479 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREADSTRUCTURE, &dvd ));
2481 if (ret == STATUS_SUCCESS)
2483 region->CopySystem = copy.copyrightProtectionSystemType;
2484 region->RegionData = copy.regionMask;
2485 region->SystemRegion = rpc.driveRegion;
2486 region->ResetCount = rpc.numberUserResets;
2489 return ret;
2490 #else
2491 FIXME("not supported on this O/S\n");
2492 return STATUS_NOT_SUPPORTED;
2493 #endif
2496 static DWORD DVD_ReadStructureSize(const DVD_READ_STRUCTURE *structure, DWORD size)
2498 if (!structure || size != sizeof(DVD_READ_STRUCTURE))
2499 return 0;
2501 switch (structure->Format)
2503 case DvdPhysicalDescriptor:
2504 return sizeof(DVD_LAYER_DESCRIPTOR);
2505 case DvdCopyrightDescriptor:
2506 return sizeof(DVD_COPYRIGHT_DESCRIPTOR);
2507 case DvdDiskKeyDescriptor:
2508 return sizeof(DVD_DISK_KEY_DESCRIPTOR);
2509 case DvdBCADescriptor:
2510 return sizeof(DVD_BCA_DESCRIPTOR);
2511 case DvdManufacturerDescriptor:
2512 return sizeof(DVD_MANUFACTURER_DESCRIPTOR);
2513 default:
2514 return 0;
2518 /******************************************************************
2519 * DVD_ReadStructure
2523 static NTSTATUS DVD_ReadStructure(int dev, const DVD_READ_STRUCTURE *structure, PDVD_LAYER_DESCRIPTOR layer)
2525 #ifdef DVD_READ_STRUCT
2526 /* dvd_struct is not defined consistently across platforms */
2527 union
2529 struct dvd_physical physical;
2530 struct dvd_copyright copyright;
2531 struct dvd_disckey disckey;
2532 struct dvd_bca bca;
2533 struct dvd_manufact manufact;
2534 } s;
2536 if (structure->BlockByteOffset.QuadPart) FIXME(": BlockByteOffset is not handled\n");
2538 switch (structure->Format)
2540 case DvdPhysicalDescriptor:
2541 s.physical.type = DVD_STRUCT_PHYSICAL;
2542 s.physical.layer_num = structure->LayerNumber;
2543 break;
2545 case DvdCopyrightDescriptor:
2546 s.copyright.type = DVD_STRUCT_COPYRIGHT;
2547 s.copyright.layer_num = structure->LayerNumber;
2548 break;
2550 case DvdDiskKeyDescriptor:
2551 s.disckey.type = DVD_STRUCT_DISCKEY;
2552 s.disckey.agid = structure->SessionId;
2553 break;
2555 case DvdBCADescriptor:
2556 s.bca.type = DVD_STRUCT_BCA;
2557 break;
2559 case DvdManufacturerDescriptor:
2560 s.manufact.type = DVD_STRUCT_MANUFACT;
2561 s.manufact.layer_num = structure->LayerNumber;
2562 break;
2564 case DvdMaxDescriptor: /* This is not a real request, no matter what MSDN says! */
2565 default:
2566 return STATUS_INVALID_PARAMETER;
2569 if (ioctl(dev, DVD_READ_STRUCT, &s) < 0)
2570 return STATUS_INVALID_PARAMETER;
2572 switch (structure->Format)
2574 case DvdPhysicalDescriptor:
2576 internal_dvd_layer_descriptor *p = (internal_dvd_layer_descriptor *) layer;
2577 struct dvd_layer *l = &s.physical.layer[s.physical.layer_num];
2579 p->Header.Length = 0x0802;
2580 p->Header.Reserved[0] = 0;
2581 p->Header.Reserved[1] = 0;
2582 p->Descriptor.BookVersion = l->book_version;
2583 p->Descriptor.BookType = l->book_type;
2584 p->Descriptor.MinimumRate = l->min_rate;
2585 p->Descriptor.DiskSize = l->disc_size;
2586 p->Descriptor.LayerType = l->layer_type;
2587 p->Descriptor.TrackPath = l->track_path;
2588 p->Descriptor.NumberOfLayers = l->nlayers;
2589 p->Descriptor.Reserved1 = 0;
2590 p->Descriptor.TrackDensity = l->track_density;
2591 p->Descriptor.LinearDensity = l->linear_density;
2592 p->Descriptor.StartingDataSector = GET_BE_DWORD(l->start_sector);
2593 p->Descriptor.EndDataSector = GET_BE_DWORD(l->end_sector);
2594 p->Descriptor.EndLayerZeroSector = GET_BE_DWORD(l->end_sector_l0);
2595 p->Descriptor.Reserved5 = 0;
2596 p->Descriptor.BCAFlag = l->bca;
2598 break;
2600 case DvdCopyrightDescriptor:
2602 PDVD_COPYRIGHT_DESCRIPTOR p = (PDVD_COPYRIGHT_DESCRIPTOR) layer;
2604 p->CopyrightProtectionType = s.copyright.cpst;
2605 p->RegionManagementInformation = s.copyright.rmi;
2606 p->Reserved = 0;
2608 break;
2610 case DvdDiskKeyDescriptor:
2612 PDVD_DISK_KEY_DESCRIPTOR p = (PDVD_DISK_KEY_DESCRIPTOR) layer;
2614 memcpy(p->DiskKeyData, s.disckey.value, 2048);
2616 break;
2618 case DvdBCADescriptor:
2620 PDVD_BCA_DESCRIPTOR p = (PDVD_BCA_DESCRIPTOR) layer;
2622 memcpy(p->BCAInformation, s.bca.value, s.bca.len);
2624 break;
2626 case DvdManufacturerDescriptor:
2628 internal_dvd_manufacturer_descriptor *p = (internal_dvd_manufacturer_descriptor*) layer;
2630 p->Header.Length = 0x0802;
2631 p->Header.Reserved[0] = 0;
2632 p->Header.Reserved[1] = 0;
2633 memcpy(p->Descriptor.ManufacturingInformation, s.manufact.value, 2048);
2635 break;
2637 case DvdMaxDescriptor: /* Suppress warning */
2638 break;
2640 #elif defined(__APPLE__)
2641 NTSTATUS ret;
2642 dk_dvd_read_structure_t dvdrs;
2643 union
2645 DVDPhysicalFormatInfo phys;
2646 DVDCopyrightInfo copy;
2647 DVDDiscKeyInfo disk_key;
2648 DVDManufacturingInfo manf;
2649 } desc;
2650 union
2652 PDVD_LAYER_DESCRIPTOR layer;
2653 internal_dvd_layer_descriptor *xlayer;
2654 PDVD_COPYRIGHT_DESCRIPTOR copy;
2655 PDVD_DISK_KEY_DESCRIPTOR disk_key;
2656 internal_dvd_manufacturer_descriptor *manf;
2657 } nt_desc;
2659 nt_desc.layer = layer;
2660 RtlZeroMemory(&dvdrs, sizeof(dvdrs));
2661 dvdrs.address = (uint32_t)(structure->BlockByteOffset.QuadPart>>11);
2662 dvdrs.grantID = (uint8_t)structure->SessionId;
2663 dvdrs.layer = structure->LayerNumber;
2664 switch(structure->Format)
2666 case DvdPhysicalDescriptor:
2667 dvdrs.format = kDVDStructureFormatPhysicalFormatInfo;
2668 dvdrs.bufferLength = sizeof(desc.phys);
2669 dvdrs.buffer = &desc.phys;
2670 break;
2672 case DvdCopyrightDescriptor:
2673 dvdrs.format = kDVDStructureFormatCopyrightInfo;
2674 dvdrs.bufferLength = sizeof(desc.copy);
2675 dvdrs.buffer = &desc.copy;
2676 break;
2678 case DvdDiskKeyDescriptor:
2679 dvdrs.format = kDVDStructureFormatDiscKeyInfo;
2680 dvdrs.bufferLength = sizeof(desc.disk_key);
2681 dvdrs.buffer = &desc.disk_key;
2682 break;
2684 case DvdBCADescriptor:
2685 FIXME("DvdBCADescriptor NIY\n");
2686 return STATUS_NOT_SUPPORTED;
2688 case DvdManufacturerDescriptor:
2689 dvdrs.format = kDVDStructureFormatManufacturingInfo;
2690 dvdrs.bufferLength = sizeof(desc.manf);
2691 dvdrs.buffer = &desc.manf;
2692 break;
2694 case DvdMaxDescriptor:
2695 default:
2696 FIXME("got unknown structure type 0x%x\n", structure->Format);
2697 return STATUS_NOT_SUPPORTED;
2699 ret = CDROM_GetStatusCode(ioctl(dev, DKIOCDVDREADSTRUCTURE, &dvdrs));
2700 if(ret == STATUS_SUCCESS)
2702 switch(structure->Format)
2704 case DvdPhysicalDescriptor:
2705 nt_desc.xlayer->Header.Length = 0x0802;
2706 nt_desc.xlayer->Header.Reserved[0] = 0;
2707 nt_desc.xlayer->Header.Reserved[1] = 0;
2708 nt_desc.xlayer->Descriptor.BookVersion = desc.phys.partVersion;
2709 nt_desc.xlayer->Descriptor.BookType = desc.phys.bookType;
2710 nt_desc.xlayer->Descriptor.MinimumRate = desc.phys.minimumRate;
2711 nt_desc.xlayer->Descriptor.DiskSize = desc.phys.discSize;
2712 nt_desc.xlayer->Descriptor.LayerType = desc.phys.layerType;
2713 nt_desc.xlayer->Descriptor.TrackPath = desc.phys.trackPath;
2714 nt_desc.xlayer->Descriptor.NumberOfLayers = desc.phys.numberOfLayers;
2715 nt_desc.xlayer->Descriptor.Reserved1 = 0;
2716 nt_desc.xlayer->Descriptor.TrackDensity = desc.phys.trackDensity;
2717 nt_desc.xlayer->Descriptor.LinearDensity = desc.phys.linearDensity;
2718 nt_desc.xlayer->Descriptor.StartingDataSector = GET_BE_DWORD(*(DWORD *)&desc.phys.zero1);
2719 nt_desc.xlayer->Descriptor.EndDataSector = GET_BE_DWORD(*(DWORD *)&desc.phys.zero2);
2720 nt_desc.xlayer->Descriptor.EndLayerZeroSector = GET_BE_DWORD(*(DWORD *)&desc.phys.zero3);
2721 nt_desc.xlayer->Descriptor.Reserved5 = 0;
2722 nt_desc.xlayer->Descriptor.BCAFlag = desc.phys.bcaFlag;
2723 break;
2725 case DvdCopyrightDescriptor:
2726 nt_desc.copy->CopyrightProtectionType =
2727 desc.copy.copyrightProtectionSystemType;
2728 nt_desc.copy->RegionManagementInformation =
2729 desc.copy.regionMask;
2730 nt_desc.copy->Reserved = 0;
2731 break;
2733 case DvdDiskKeyDescriptor:
2734 memcpy(
2735 nt_desc.disk_key->DiskKeyData,
2736 desc.disk_key.discKeyStructures,
2737 2048);
2738 break;
2740 case DvdManufacturerDescriptor:
2741 nt_desc.manf->Header.Length = 0x0802;
2742 nt_desc.manf->Header.Reserved[0] = 0;
2743 nt_desc.manf->Header.Reserved[1] = 0;
2744 memcpy(
2745 nt_desc.manf->Descriptor.ManufacturingInformation,
2746 desc.manf.discManufacturingInfo,
2747 2048);
2748 break;
2750 case DvdBCADescriptor:
2751 case DvdMaxDescriptor:
2752 default:
2753 /* Silence warning */
2754 break;
2757 return ret;
2758 #else
2759 FIXME("\n");
2760 #endif
2761 return STATUS_SUCCESS;
2765 /******************************************************************
2766 * GetInquiryData
2767 * Implements the IOCTL_GET_INQUIRY_DATA ioctl.
2768 * Returns Inquiry data for all devices on the specified scsi bus
2769 * Returns STATUS_BUFFER_TOO_SMALL if the output buffer is too small,
2770 * STATUS_INVALID_DEVICE_REQUEST if the given handle isn't to a SCSI device,
2771 * or STATUS_NOT_SUPPORTED if the OS driver is too old
2773 static NTSTATUS GetInquiryData(int fd, PSCSI_ADAPTER_BUS_INFO BufferOut, DWORD OutBufferSize)
2775 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
2776 PSCSI_INQUIRY_DATA pInquiryData;
2777 UCHAR sense_buffer[32];
2778 int iochk, version;
2779 sg_io_hdr_t iocmd;
2780 UCHAR inquiry[INQ_CMD_LEN] = {INQUIRY, 0, 0, 0, INQ_REPLY_LEN, 0};
2782 /* Check we have a SCSI device and a supported driver */
2783 if(ioctl(fd, SG_GET_VERSION_NUM, &version) != 0)
2785 WARN("IOCTL_SCSI_GET_INQUIRY_DATA sg driver is not loaded\n");
2786 return STATUS_INVALID_DEVICE_REQUEST;
2788 if(version < 30000 )
2789 return STATUS_NOT_SUPPORTED;
2791 /* FIXME: Enumerate devices on the bus */
2792 BufferOut->NumberOfBuses = 1;
2793 BufferOut->BusData[0].NumberOfLogicalUnits = 1;
2794 BufferOut->BusData[0].InquiryDataOffset = sizeof(SCSI_ADAPTER_BUS_INFO);
2796 pInquiryData = (PSCSI_INQUIRY_DATA)(BufferOut + 1);
2798 RtlZeroMemory(&iocmd, sizeof(iocmd));
2799 iocmd.interface_id = 'S';
2800 iocmd.cmd_len = sizeof(inquiry);
2801 iocmd.mx_sb_len = sizeof(sense_buffer);
2802 iocmd.dxfer_direction = SG_DXFER_FROM_DEV;
2803 iocmd.dxfer_len = INQ_REPLY_LEN;
2804 iocmd.dxferp = pInquiryData->InquiryData;
2805 iocmd.cmdp = inquiry;
2806 iocmd.sbp = sense_buffer;
2807 iocmd.timeout = 1000;
2809 iochk = ioctl(fd, SG_IO, &iocmd);
2810 if(iochk != 0)
2811 WARN("ioctl SG_IO returned %d, error (%s)\n", iochk, strerror(errno));
2813 CDROM_GetInterfaceInfo(fd, &BufferOut->BusData[0].InitiatorBusId, &pInquiryData->PathId, &pInquiryData->TargetId, &pInquiryData->Lun);
2814 pInquiryData->DeviceClaimed = TRUE;
2815 pInquiryData->InquiryDataLength = INQ_REPLY_LEN;
2816 pInquiryData->NextInquiryDataOffset = 0;
2817 return STATUS_SUCCESS;
2818 #else
2819 FIXME("not supported on this O/S\n");
2820 return STATUS_NOT_SUPPORTED;
2821 #endif
2824 /******************************************************************
2825 * CDROM_DeviceIoControl
2829 NTSTATUS CDROM_DeviceIoControl(HANDLE hDevice,
2830 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
2831 PVOID UserApcContext,
2832 PIO_STATUS_BLOCK piosb,
2833 ULONG dwIoControlCode,
2834 LPVOID lpInBuffer, DWORD nInBufferSize,
2835 LPVOID lpOutBuffer, DWORD nOutBufferSize)
2837 DWORD sz = 0;
2838 NTSTATUS status = STATUS_SUCCESS;
2839 int fd, needs_close, dev = 0;
2841 TRACE("%p %s %p %d %p %d %p\n",
2842 hDevice, iocodex(dwIoControlCode), lpInBuffer, nInBufferSize,
2843 lpOutBuffer, nOutBufferSize, piosb);
2845 piosb->Information = 0;
2847 if ((status = server_get_unix_fd( hDevice, 0, &fd, &needs_close, NULL, NULL )))
2849 if (status == STATUS_BAD_DEVICE_TYPE) return status; /* no associated fd */
2850 goto error;
2853 if ((status = CDROM_Open(fd, &dev)))
2855 if (needs_close) close( fd );
2856 goto error;
2859 #ifdef __APPLE__
2861 char name[100];
2863 /* This is ugly as hell, but Mac OS is unable to do anything from the
2864 * partition fd, it wants an fd for the whole device, and it sometimes
2865 * also requires the device fd to be closed first, so we have to close
2866 * the handle that the caller gave us.
2867 * Also for some reason it wants the fd to be closed before we even
2868 * open the parent if we're trying to eject the disk.
2870 if ((status = get_parent_device( fd, name, sizeof(name) ))) goto error;
2871 if (dwIoControlCode == IOCTL_STORAGE_EJECT_MEDIA)
2872 NtClose( hDevice );
2873 if (needs_close) close( fd );
2874 TRACE("opening parent %s\n", name );
2875 if ((fd = open( name, O_RDONLY )) == -1)
2877 status = FILE_GetNtStatus();
2878 goto error;
2880 needs_close = 1;
2882 #endif
2884 switch (dwIoControlCode)
2886 case IOCTL_CDROM_CHECK_VERIFY:
2887 case IOCTL_DISK_CHECK_VERIFY:
2888 case IOCTL_STORAGE_CHECK_VERIFY:
2889 case IOCTL_STORAGE_CHECK_VERIFY2:
2890 sz = 0;
2891 CDROM_ClearCacheEntry(dev);
2892 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2893 status = STATUS_INVALID_PARAMETER;
2894 else status = CDROM_Verify(dev, fd);
2895 break;
2897 /* EPP case IOCTL_STORAGE_CHECK_VERIFY2: */
2899 /* EPP case IOCTL_STORAGE_FIND_NEW_DEVICES: */
2900 /* EPP case IOCTL_CDROM_FIND_NEW_DEVICES: */
2902 case IOCTL_STORAGE_LOAD_MEDIA:
2903 case IOCTL_CDROM_LOAD_MEDIA:
2904 sz = 0;
2905 CDROM_ClearCacheEntry(dev);
2906 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2907 status = STATUS_INVALID_PARAMETER;
2908 else status = CDROM_SetTray(fd, FALSE);
2909 break;
2910 case IOCTL_STORAGE_EJECT_MEDIA:
2911 sz = 0;
2912 CDROM_ClearCacheEntry(dev);
2913 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2914 status = STATUS_INVALID_PARAMETER;
2915 else
2916 status = CDROM_SetTray(fd, TRUE);
2917 break;
2919 case IOCTL_CDROM_MEDIA_REMOVAL:
2920 case IOCTL_DISK_MEDIA_REMOVAL:
2921 case IOCTL_STORAGE_MEDIA_REMOVAL:
2922 case IOCTL_STORAGE_EJECTION_CONTROL:
2923 /* FIXME the last ioctl:s is not the same as the two others...
2924 * lockcount/owner should be handled */
2925 sz = 0;
2926 CDROM_ClearCacheEntry(dev);
2927 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2928 else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) status = STATUS_BUFFER_TOO_SMALL;
2929 else status = CDROM_ControlEjection(fd, lpInBuffer);
2930 break;
2932 case IOCTL_DISK_GET_MEDIA_TYPES:
2933 case IOCTL_STORAGE_GET_MEDIA_TYPES:
2934 case IOCTL_STORAGE_GET_MEDIA_TYPES_EX:
2935 sz = sizeof(GET_MEDIA_TYPES);
2936 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2937 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2938 else status = CDROM_GetMediaType(dev, lpOutBuffer);
2939 break;
2941 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
2942 sz = sizeof(STORAGE_DEVICE_NUMBER);
2943 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2944 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2945 else status = CDROM_GetDeviceNumber(dev, lpOutBuffer);
2946 break;
2948 case IOCTL_STORAGE_RESET_DEVICE:
2949 sz = 0;
2950 CDROM_ClearCacheEntry(dev);
2951 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2952 status = STATUS_INVALID_PARAMETER;
2953 else status = CDROM_ResetAudio(fd);
2954 break;
2956 case IOCTL_CDROM_GET_CONTROL:
2957 sz = sizeof(CDROM_AUDIO_CONTROL);
2958 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2959 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2960 else status = CDROM_GetControl(dev, fd, lpOutBuffer);
2961 break;
2963 case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
2964 sz = sizeof(DISK_GEOMETRY);
2965 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2966 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2967 else status = CDROM_GetDriveGeometry(dev, fd, lpOutBuffer);
2968 break;
2970 case IOCTL_CDROM_DISK_TYPE:
2971 sz = sizeof(CDROM_DISK_DATA);
2972 /* CDROM_ClearCacheEntry(dev); */
2973 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2974 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2975 else status = CDROM_GetDiskData(dev, fd, lpOutBuffer);
2976 break;
2978 /* EPP case IOCTL_CDROM_GET_LAST_SESSION: */
2980 case IOCTL_CDROM_READ_Q_CHANNEL:
2981 sz = sizeof(SUB_Q_CHANNEL_DATA);
2982 if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT))
2983 status = STATUS_INVALID_PARAMETER;
2984 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2985 else status = CDROM_ReadQChannel(dev, fd, lpInBuffer, lpOutBuffer);
2986 break;
2988 case IOCTL_CDROM_READ_TOC:
2989 sz = sizeof(CDROM_TOC);
2990 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2991 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2992 else status = CDROM_ReadTOC(dev, fd, lpOutBuffer);
2993 break;
2995 /* EPP case IOCTL_CDROM_READ_TOC_EX: */
2997 case IOCTL_CDROM_PAUSE_AUDIO:
2998 sz = 0;
2999 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3000 status = STATUS_INVALID_PARAMETER;
3001 else status = CDROM_PauseAudio(fd);
3002 break;
3003 case IOCTL_CDROM_PLAY_AUDIO_MSF:
3004 sz = 0;
3005 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3006 else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
3007 else status = CDROM_PlayAudioMSF(fd, lpInBuffer);
3008 break;
3009 case IOCTL_CDROM_RESUME_AUDIO:
3010 sz = 0;
3011 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3012 status = STATUS_INVALID_PARAMETER;
3013 else status = CDROM_ResumeAudio(fd);
3014 break;
3015 case IOCTL_CDROM_SEEK_AUDIO_MSF:
3016 sz = 0;
3017 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3018 else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
3019 else status = CDROM_SeekAudioMSF(dev, fd, lpInBuffer);
3020 break;
3021 case IOCTL_CDROM_STOP_AUDIO:
3022 sz = 0;
3023 CDROM_ClearCacheEntry(dev); /* Maybe intention is to change media */
3024 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3025 status = STATUS_INVALID_PARAMETER;
3026 else status = CDROM_StopAudio(fd);
3027 break;
3028 case IOCTL_CDROM_GET_VOLUME:
3029 sz = sizeof(VOLUME_CONTROL);
3030 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3031 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3032 else status = CDROM_GetVolume(fd, lpOutBuffer);
3033 break;
3034 case IOCTL_CDROM_SET_VOLUME:
3035 sz = 0;
3036 CDROM_ClearCacheEntry(dev);
3037 if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
3038 status = STATUS_INVALID_PARAMETER;
3039 else status = CDROM_SetVolume(fd, lpInBuffer);
3040 break;
3041 case IOCTL_CDROM_RAW_READ:
3042 sz = 0;
3043 if (nInBufferSize < sizeof(RAW_READ_INFO)) status = STATUS_INVALID_PARAMETER;
3044 else if (lpOutBuffer == NULL) status = STATUS_BUFFER_TOO_SMALL;
3045 else status = CDROM_RawRead(fd, lpInBuffer, lpOutBuffer,
3046 nOutBufferSize, &sz);
3047 break;
3048 case IOCTL_SCSI_GET_ADDRESS:
3049 sz = sizeof(SCSI_ADDRESS);
3050 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3051 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3052 else status = CDROM_GetAddress(fd, lpOutBuffer);
3053 break;
3054 case IOCTL_SCSI_PASS_THROUGH_DIRECT:
3055 sz = sizeof(SCSI_PASS_THROUGH_DIRECT);
3056 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3057 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH_DIRECT)) status = STATUS_BUFFER_TOO_SMALL;
3058 else status = CDROM_ScsiPassThroughDirect(fd, lpOutBuffer);
3059 break;
3060 case IOCTL_SCSI_PASS_THROUGH:
3061 sz = sizeof(SCSI_PASS_THROUGH);
3062 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3063 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH)) status = STATUS_BUFFER_TOO_SMALL;
3064 else status = CDROM_ScsiPassThrough(fd, lpOutBuffer);
3065 break;
3066 case IOCTL_SCSI_GET_CAPABILITIES:
3067 sz = sizeof(IO_SCSI_CAPABILITIES);
3068 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3069 else if (nOutBufferSize < sizeof(IO_SCSI_CAPABILITIES)) status = STATUS_BUFFER_TOO_SMALL;
3070 else status = CDROM_ScsiGetCaps(fd, lpOutBuffer);
3071 break;
3072 case IOCTL_DVD_START_SESSION:
3073 sz = sizeof(DVD_SESSION_ID);
3074 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3075 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3076 else
3078 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3079 *(PDVD_SESSION_ID)lpOutBuffer);
3080 status = DVD_StartSession(fd, lpInBuffer, lpOutBuffer);
3081 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3082 *(PDVD_SESSION_ID)lpOutBuffer);
3084 break;
3085 case IOCTL_DVD_END_SESSION:
3086 sz = sizeof(DVD_SESSION_ID);
3087 if ((lpInBuffer == NULL) || (nInBufferSize < sz))status = STATUS_INVALID_PARAMETER;
3088 else status = DVD_EndSession(fd, lpInBuffer);
3089 break;
3090 case IOCTL_DVD_SEND_KEY:
3091 sz = 0;
3092 if (!lpInBuffer ||
3093 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3094 status = STATUS_INVALID_PARAMETER;
3095 else
3097 TRACE("doing DVD_SendKey\n");
3098 status = DVD_SendKey(fd, lpInBuffer);
3100 break;
3101 case IOCTL_DVD_READ_KEY:
3102 if (!lpInBuffer ||
3103 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3104 status = STATUS_INVALID_PARAMETER;
3105 else if (lpInBuffer !=lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
3106 else
3108 TRACE("doing DVD_READ_KEY\n");
3109 sz = ((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength;
3110 status = DVD_ReadKey(fd, lpInBuffer);
3112 break;
3113 case IOCTL_DVD_GET_REGION:
3114 sz = sizeof(DVD_REGION);
3115 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3116 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3117 else
3119 TRACE("doing DVD_Get_REGION\n");
3120 status = DVD_GetRegion(fd, lpOutBuffer);
3122 break;
3123 case IOCTL_DVD_READ_STRUCTURE:
3124 sz = DVD_ReadStructureSize(lpInBuffer, nInBufferSize);
3125 if (lpInBuffer == NULL || nInBufferSize != sizeof(DVD_READ_STRUCTURE)) status = STATUS_INVALID_PARAMETER;
3126 else if (nOutBufferSize < sz || !lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
3127 else
3129 TRACE("doing DVD_READ_STRUCTURE\n");
3130 status = DVD_ReadStructure(fd, lpInBuffer, lpOutBuffer);
3132 break;
3134 case IOCTL_SCSI_GET_INQUIRY_DATA:
3135 sz = INQ_REPLY_LEN;
3136 status = GetInquiryData(fd, lpOutBuffer, nOutBufferSize);
3137 break;
3139 default:
3140 if (needs_close) close( fd );
3141 return STATUS_NOT_SUPPORTED;
3143 if (needs_close) close( fd );
3144 error:
3145 piosb->u.Status = status;
3146 piosb->Information = sz;
3147 if (hEvent) NtSetEvent(hEvent, NULL);
3148 return status;