msvfw32: Simplify error handling in ICSeqCompressFrameStart.
[wine/multimedia.git] / dlls / ntdll / cdrom.c
blob44072770ad21339ce43a30a6f2c859ac2817915f
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /* Main file for CD-ROM support
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1999, 2001, 2003 Eric Pouech
6 * Copyright 2000 Andreas Mohr
7 * Copyright 2005 Ivan Leo Puoti
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <errno.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_IO_H
32 # include <io.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <fcntl.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
40 #endif
41 #include <sys/types.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 #include <sys/ioctl.h>
45 #endif
46 #ifdef HAVE_SCSI_SG_H
47 # include <scsi/sg.h>
48 #endif
49 #ifdef HAVE_SCSI_SCSI_H
50 # include <scsi/scsi.h>
51 # undef REASSIGN_BLOCKS /* avoid conflict with winioctl.h */
52 # undef FAILED /* avoid conflict with winerror.h */
53 #endif
54 #ifdef HAVE_SCSI_SCSI_IOCTL_H
55 # include <scsi/scsi_ioctl.h>
56 #endif
57 #ifdef HAVE_LINUX_MAJOR_H
58 # include <linux/major.h>
59 #endif
60 #ifdef HAVE_LINUX_HDREG_H
61 # include <linux/hdreg.h>
62 #endif
63 #ifdef HAVE_LINUX_PARAM_H
64 # include <linux/param.h>
65 #endif
66 #ifdef HAVE_LINUX_CDROM_H
67 # include <linux/cdrom.h>
68 #endif
69 #ifdef HAVE_LINUX_UCDROM_H
70 # include <linux/ucdrom.h>
71 #endif
72 #ifdef HAVE_SYS_CDIO_H
73 # include <sys/cdio.h>
74 #endif
75 #ifdef HAVE_SYS_SCSIIO_H
76 # include <sys/scsiio.h>
77 #endif
79 #ifdef HAVE_IOKIT_IOKITLIB_H
80 # include <libkern/OSByteOrder.h>
81 # include <sys/disk.h>
82 # include <IOKit/IOKitLib.h>
83 # include <IOKit/storage/IOMedia.h>
84 # include <IOKit/storage/IOCDMediaBSDClient.h>
85 # include <IOKit/storage/IODVDMediaBSDClient.h>
86 # include <IOKit/scsi/SCSITask.h>
87 # include <IOKit/scsi/SCSICmds_REQUEST_SENSE_Defs.h>
88 # define SENSEBUFLEN kSenseDefaultSize
90 typedef struct
92 uint32_t attribute;
93 uint32_t timeout;
94 uint32_t response;
95 uint32_t status;
96 uint8_t direction;
97 uint8_t cdbSize;
98 uint8_t reserved0144[2];
99 uint8_t cdb[16];
100 void* buffer;
101 uint64_t bufferSize;
102 void* sense;
103 uint64_t senseLen;
104 } dk_scsi_command_t;
106 typedef struct
108 uint64_t bus;
109 uint64_t port;
110 uint64_t target;
111 uint64_t lun;
112 } dk_scsi_identify_t;
114 #define DKIOCSCSICOMMAND _IOWR('d', 253, dk_scsi_command_t)
115 #define DKIOCSCSIIDENTIFY _IOR('d', 254, dk_scsi_identify_t)
117 #endif
119 #include "ntstatus.h"
120 #define WIN32_NO_STATUS
121 #define NONAMELESSUNION
122 #include "windef.h"
123 #include "winternl.h"
124 #include "winioctl.h"
125 #include "ntddstor.h"
126 #include "ntddcdrm.h"
127 #include "ddk/ntddcdvd.h"
128 #include "ntddscsi.h"
129 #include "ntdll_misc.h"
130 #include "wine/server.h"
131 #include "wine/library.h"
132 #include "wine/debug.h"
134 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
136 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
137 lack the following constants. */
139 #ifndef CD_SECS
140 # define CD_SECS 60 /* seconds per minute */
141 #endif
142 #ifndef CD_FRAMES
143 # define CD_FRAMES 75 /* frames per second */
144 #endif
146 #ifdef WORDS_BIGENDIAN
147 #define GET_BE_DWORD(x) (x)
148 #else
149 #define GET_BE_DWORD(x) RtlUlongByteSwap(x)
150 #endif
152 static const struct iocodexs
154 DWORD code;
155 const char *codex;
156 } iocodextable[] = {
157 #define X(x) { x, #x },
158 X(IOCTL_CDROM_CHECK_VERIFY)
159 X(IOCTL_CDROM_CURRENT_POSITION)
160 X(IOCTL_CDROM_DISK_TYPE)
161 X(IOCTL_CDROM_GET_CONTROL)
162 X(IOCTL_CDROM_GET_DRIVE_GEOMETRY)
163 X(IOCTL_CDROM_GET_VOLUME)
164 X(IOCTL_CDROM_LOAD_MEDIA)
165 X(IOCTL_CDROM_MEDIA_CATALOG)
166 X(IOCTL_CDROM_MEDIA_REMOVAL)
167 X(IOCTL_CDROM_PAUSE_AUDIO)
168 X(IOCTL_CDROM_PLAY_AUDIO_MSF)
169 X(IOCTL_CDROM_RAW_READ)
170 X(IOCTL_CDROM_READ_Q_CHANNEL)
171 X(IOCTL_CDROM_READ_TOC)
172 X(IOCTL_CDROM_RESUME_AUDIO)
173 X(IOCTL_CDROM_SEEK_AUDIO_MSF)
174 X(IOCTL_CDROM_SET_VOLUME)
175 X(IOCTL_CDROM_STOP_AUDIO)
176 X(IOCTL_CDROM_TRACK_ISRC)
177 X(IOCTL_DISK_GET_MEDIA_TYPES)
178 X(IOCTL_DISK_MEDIA_REMOVAL)
179 X(IOCTL_DVD_END_SESSION)
180 X(IOCTL_DVD_GET_REGION)
181 X(IOCTL_DVD_READ_KEY)
182 X(IOCTL_DVD_READ_STRUCTURE)
183 X(IOCTL_DVD_SEND_KEY)
184 X(IOCTL_DVD_START_SESSION)
185 X(IOCTL_SCSI_GET_ADDRESS)
186 X(IOCTL_SCSI_GET_CAPABILITIES)
187 X(IOCTL_SCSI_GET_INQUIRY_DATA)
188 X(IOCTL_SCSI_PASS_THROUGH)
189 X(IOCTL_SCSI_PASS_THROUGH_DIRECT)
190 X(IOCTL_STORAGE_CHECK_VERIFY)
191 X(IOCTL_STORAGE_CHECK_VERIFY2)
192 X(IOCTL_STORAGE_EJECTION_CONTROL)
193 X(IOCTL_STORAGE_EJECT_MEDIA)
194 X(IOCTL_STORAGE_GET_DEVICE_NUMBER)
195 X(IOCTL_STORAGE_GET_MEDIA_TYPES)
196 X(IOCTL_STORAGE_GET_MEDIA_TYPES_EX)
197 X(IOCTL_STORAGE_LOAD_MEDIA)
198 X(IOCTL_STORAGE_MEDIA_REMOVAL)
199 X(IOCTL_STORAGE_RESET_DEVICE)
200 #undef X
202 static const char *iocodex(DWORD code)
204 unsigned int i;
205 static char buffer[25];
206 for(i=0; i<sizeof(iocodextable)/sizeof(struct iocodexs); i++)
207 if (code==iocodextable[i].code)
208 return iocodextable[i].codex;
209 sprintf(buffer, "IOCTL_CODE_%x", (int)code);
210 return buffer;
213 #define INQ_REPLY_LEN 36
214 #define INQ_CMD_LEN 6
216 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
217 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
218 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
219 #define MSF_OF_FRAME(m,fr) {int f=(fr); ((UCHAR *)&(m))[2]=f%CD_FRAMES;f/=CD_FRAMES;((UCHAR *)&(m))[1]=f%CD_SECS;((UCHAR *)&(m))[0]=f/CD_SECS;}
221 /* The documented format of DVD_LAYER_DESCRIPTOR is wrong. Even the format in the
222 * DDK's header is wrong. There are four bytes at the start defined by
223 * MMC-5. The first two are the size of the structure in big-endian order as
224 * defined by MMC-5. The other two are reserved.
226 typedef struct
228 DVD_DESCRIPTOR_HEADER Header;
229 DVD_LAYER_DESCRIPTOR Descriptor;
230 UCHAR Padding;
231 } internal_dvd_layer_descriptor;
232 C_ASSERT(sizeof(internal_dvd_layer_descriptor) == 22);
234 typedef struct
236 DVD_DESCRIPTOR_HEADER Header;
237 DVD_MANUFACTURER_DESCRIPTOR Descriptor;
238 UCHAR Padding;
239 } internal_dvd_manufacturer_descriptor;
240 C_ASSERT(sizeof(internal_dvd_manufacturer_descriptor) == 2053);
242 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
243 static NTSTATUS CDROM_GetStatusCode(int);
246 #ifdef linux
248 # ifndef IDE6_MAJOR
249 # define IDE6_MAJOR 88
250 # endif
251 # ifndef IDE7_MAJOR
252 # define IDE7_MAJOR 89
253 # endif
255 # ifdef CDROM_SEND_PACKET
256 /* structure for CDROM_PACKET_COMMAND ioctl */
257 /* not all Linux versions have all the fields, so we define the
258 * structure ourselves to make sure */
259 struct linux_cdrom_generic_command
261 unsigned char cmd[CDROM_PACKET_SIZE];
262 unsigned char *buffer;
263 unsigned int buflen;
264 int stat;
265 struct request_sense *sense;
266 unsigned char data_direction;
267 int quiet;
268 int timeout;
269 void *reserved[1];
271 # endif /* CDROM_SEND_PACKET */
273 #endif /* linux */
275 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
276 * this should be removed when a proper device interface is implemented
278 * (WS) We need this to keep track of current position and to safely
279 * detect media changes. Besides this should provide a great speed up
280 * for toc inquiries.
282 struct cdrom_cache {
283 dev_t device;
284 ino_t inode;
285 char toc_good; /* if false, will reread TOC from disk */
286 CDROM_TOC toc;
287 SUB_Q_CURRENT_POSITION CurrentPosition;
289 /* who has more than 5 cdroms on his/her machine ?? */
290 /* FIXME: this should grow depending on the number of cdroms we install/configure
291 * at startup
293 #define MAX_CACHE_ENTRIES 5
294 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
296 static RTL_CRITICAL_SECTION cache_section;
297 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
299 0, 0, &cache_section,
300 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
301 0, 0, { (DWORD_PTR)(__FILE__ ": cache_section") }
303 static RTL_CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
305 /* Proposed media change function: not really needed at this time */
306 /* This is a 1 or 0 type of function */
307 #if 0
308 static int CDROM_MediaChanged(int dev)
310 int i;
312 struct cdrom_tochdr hdr;
313 struct cdrom_tocentry entry;
315 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
316 return 0;
317 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
318 return 0;
320 if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
321 return 1;
323 for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
325 if (i == hdr.cdth_trk1 + 1)
327 entry.cdte_track = CDROM_LEADOUT;
328 } else {
329 entry.cdte_track = i;
331 entry.cdte_format = CDROM_MSF;
332 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
333 return 0;
334 if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
335 sizeof(struct cdrom_tocentry)) )
336 return 1;
338 return 0;
340 #endif
343 /******************************************************************
344 * get_parent_device
346 * On Mac OS, get the device for the whole disk from a fd that points to a partition.
347 * This is ugly and inefficient, but we have no choice since the partition fd doesn't
348 * support the eject ioctl.
350 #ifdef __APPLE__
351 static NTSTATUS get_parent_device( int fd, char *name, size_t len )
353 NTSTATUS status = STATUS_NO_SUCH_FILE;
354 struct stat st;
355 int i;
356 io_service_t service;
357 CFMutableDictionaryRef dict;
358 CFTypeRef val;
360 if (fstat( fd, &st ) == -1) return FILE_GetNtStatus();
361 if (!S_ISCHR( st.st_mode )) return STATUS_OBJECT_TYPE_MISMATCH;
363 /* create a dictionary with the right major/minor numbers */
365 if (!(dict = IOServiceMatching( kIOMediaClass ))) return STATUS_NO_MEMORY;
367 i = major( st.st_rdev );
368 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
369 CFDictionaryAddValue( dict, CFSTR( "BSD Major" ), val );
370 CFRelease( val );
372 i = minor( st.st_rdev );
373 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
374 CFDictionaryAddValue( dict, CFSTR( "BSD Minor" ), val );
375 CFRelease( val );
377 CFDictionaryAddValue( dict, CFSTR("Removable"), kCFBooleanTrue );
379 service = IOServiceGetMatchingService( kIOMasterPortDefault, dict );
381 /* now look for the parent that has the "Whole" attribute set to TRUE */
383 while (service)
385 io_service_t parent = 0;
386 CFBooleanRef whole;
387 CFStringRef str;
388 int ok;
390 if (!IOObjectConformsTo( service, kIOMediaClass ))
391 goto next;
392 if (!(whole = IORegistryEntryCreateCFProperty( service, CFSTR("Whole"), NULL, 0 )))
393 goto next;
394 ok = (whole == kCFBooleanTrue);
395 CFRelease( whole );
396 if (!ok) goto next;
398 if ((str = IORegistryEntryCreateCFProperty( service, CFSTR("BSD Name"), NULL, 0 )))
400 strcpy( name, "/dev/r" );
401 CFStringGetCString( str, name + 6, len - 6, kCFStringEncodingUTF8 );
402 CFRelease( str );
403 status = STATUS_SUCCESS;
405 IOObjectRelease( service );
406 break;
408 next:
409 IORegistryEntryGetParentEntry( service, kIOServicePlane, &parent );
410 IOObjectRelease( service );
411 service = parent;
413 return status;
415 #endif
418 /******************************************************************
419 * CDROM_SyncCache [internal]
421 * Read the TOC in and store it in the cdrom_cache structure.
422 * Further requests for the TOC will be copied from the cache
423 * unless certain events like disk ejection is detected, in which
424 * case the cache will be cleared, causing it to be resynced.
425 * The cache section must be held by caller.
427 static NTSTATUS CDROM_SyncCache(int dev, int fd)
429 #ifdef linux
430 int i, tsz;
431 struct cdrom_tochdr hdr;
432 struct cdrom_tocentry entry;
434 CDROM_TOC *toc = &cdrom_cache[dev].toc;
435 cdrom_cache[dev].toc_good = 0;
437 if (ioctl(fd, CDROMREADTOCHDR, &hdr) == -1)
439 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
440 return FILE_GetNtStatus();
443 toc->FirstTrack = hdr.cdth_trk0;
444 toc->LastTrack = hdr.cdth_trk1;
445 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
446 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
447 toc->Length[0] = tsz >> 8;
448 toc->Length[1] = tsz;
450 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
452 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
454 if (i == toc->LastTrack + 1)
455 entry.cdte_track = CDROM_LEADOUT;
456 else
457 entry.cdte_track = i;
458 entry.cdte_format = CDROM_MSF;
459 if (ioctl(fd, CDROMREADTOCENTRY, &entry) == -1)
461 WARN("error read entry (%s)\n", strerror(errno));
462 return FILE_GetNtStatus();
464 toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
465 toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
466 /* marking last track with leadout value as index */
467 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
468 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
469 toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
470 toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
471 toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
473 cdrom_cache[dev].toc_good = 1;
474 return STATUS_SUCCESS;
476 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
478 int i, tsz;
479 struct ioc_toc_header hdr;
480 struct ioc_read_toc_entry entry;
481 struct cd_toc_entry toc_buffer;
483 CDROM_TOC *toc = &cdrom_cache[dev].toc;
484 cdrom_cache[dev].toc_good = 0;
486 if (ioctl(fd, CDIOREADTOCHEADER, &hdr) == -1)
488 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
489 return FILE_GetNtStatus();
491 toc->FirstTrack = hdr.starting_track;
492 toc->LastTrack = hdr.ending_track;
493 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
494 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
495 toc->Length[0] = tsz >> 8;
496 toc->Length[1] = tsz;
498 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
500 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
502 if (i == toc->LastTrack + 1)
504 #define LEADOUT 0xaa
505 entry.starting_track = LEADOUT;
506 } else {
507 entry.starting_track = i;
509 memset(&toc_buffer, 0, sizeof(toc_buffer));
510 entry.address_format = CD_MSF_FORMAT;
511 entry.data_len = sizeof(toc_buffer);
512 entry.data = &toc_buffer;
513 if (ioctl(fd, CDIOREADTOCENTRYS, &entry) == -1)
515 WARN("error read entry (%s)\n", strerror(errno));
516 return FILE_GetNtStatus();
518 toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
519 toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
520 /* marking last track with leadout value as index */
521 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
522 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
523 toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
524 toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
525 toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
527 cdrom_cache[dev].toc_good = 1;
528 return STATUS_SUCCESS;
530 #elif defined(__APPLE__)
531 int i;
532 dk_cd_read_toc_t hdr;
533 CDROM_TOC *toc = &cdrom_cache[dev].toc;
534 cdrom_cache[dev].toc_good = 0;
536 memset( &hdr, 0, sizeof(hdr) );
537 hdr.buffer = toc;
538 hdr.bufferLength = sizeof(*toc);
539 if (ioctl(fd, DKIOCCDREADTOC, &hdr) == -1)
541 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
542 return FILE_GetNtStatus();
544 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
546 /* convert address format */
547 TRACK_DATA *data = &toc->TrackData[i - toc->FirstTrack];
548 DWORD frame = (((DWORD)data->Address[0] << 24) | ((DWORD)data->Address[1] << 16) |
549 ((DWORD)data->Address[2] << 8) | data->Address[3]);
550 MSF_OF_FRAME( data->Address[1], frame );
551 data->Address[0] = 0;
554 cdrom_cache[dev].toc_good = 1;
555 return STATUS_SUCCESS;
556 #else
557 FIXME("not supported on this O/S\n");
558 return STATUS_NOT_SUPPORTED;
559 #endif
562 static void CDROM_ClearCacheEntry(int dev)
564 RtlEnterCriticalSection( &cache_section );
565 cdrom_cache[dev].toc_good = 0;
566 RtlLeaveCriticalSection( &cache_section );
571 /******************************************************************
572 * CDROM_GetInterfaceInfo
574 * Determines the ide interface (the number after the ide), and the
575 * number of the device on that interface for ide cdroms (*iface <= 1).
576 * Determines the scsi information for scsi cdroms (*iface >= 2).
577 * Returns FALSE if the info cannot not be obtained.
579 static BOOL CDROM_GetInterfaceInfo(int fd, UCHAR* iface, UCHAR* port, UCHAR* device, UCHAR* lun)
581 #if defined(linux)
582 struct stat st;
583 if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode)) return FALSE;
584 *port = 0;
585 *iface = 0;
586 *device = 0;
587 *lun = 0;
588 switch (major(st.st_rdev)) {
589 case IDE0_MAJOR: *iface = 0; break;
590 case IDE1_MAJOR: *iface = 1; break;
591 case IDE2_MAJOR: *iface = 2; break;
592 case IDE3_MAJOR: *iface = 3; break;
593 case IDE4_MAJOR: *iface = 4; break;
594 case IDE5_MAJOR: *iface = 5; break;
595 case IDE6_MAJOR: *iface = 6; break;
596 case IDE7_MAJOR: *iface = 7; break;
597 default: *port = 1; break;
600 if (*port == 0)
601 *device = (minor(st.st_rdev) >> 6);
602 else
604 #ifdef SCSI_IOCTL_GET_IDLUN
605 UINT32 idlun[2];
606 if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, idlun) != -1)
608 *port = (idlun[0] >> 24) & 0xff;
609 *iface = ((idlun[0] >> 16) & 0xff) + 2;
610 *device = idlun[0] & 0xff;
611 *lun = (idlun[0] >> 8) & 0xff;
613 else
614 #endif
616 WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
617 return FALSE;
620 return TRUE;
621 #elif defined(__NetBSD__)
622 struct scsi_addr addr;
623 if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
625 switch (addr.type)
627 case TYPE_SCSI:
628 *port = 1;
629 *iface = addr.addr.scsi.scbus;
630 *device = addr.addr.scsi.target;
631 *lun = addr.addr.scsi.lun;
632 return TRUE;
633 case TYPE_ATAPI:
634 *port = 0;
635 *iface = addr.addr.atapi.atbus;
636 *device = addr.addr.atapi.drive;
637 *lun = 0;
638 return TRUE;
641 return FALSE;
642 #elif defined(__APPLE__)
643 dk_scsi_identify_t addr;
644 if (ioctl(fd, DKIOCSCSIIDENTIFY, &addr) != -1)
646 *port = addr.bus;
647 *iface = addr.port;
648 *device = addr.target;
649 *lun = addr.lun;
650 return TRUE;
652 return FALSE;
653 #else
654 FIXME("not implemented on this O/S\n");
655 return FALSE;
656 #endif
660 /******************************************************************
661 * CDROM_Open
664 static NTSTATUS CDROM_Open(int fd, int* dev)
666 struct stat st;
667 NTSTATUS ret = STATUS_SUCCESS;
668 int empty = -1;
670 fstat(fd, &st);
672 RtlEnterCriticalSection( &cache_section );
673 for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
675 if (empty == -1 &&
676 cdrom_cache[*dev].device == 0 &&
677 cdrom_cache[*dev].inode == 0)
678 empty = *dev;
679 else if (cdrom_cache[*dev].device == st.st_dev &&
680 cdrom_cache[*dev].inode == st.st_ino)
681 break;
683 if (*dev == MAX_CACHE_ENTRIES)
685 if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
686 else
688 *dev = empty;
689 cdrom_cache[*dev].device = st.st_dev;
690 cdrom_cache[*dev].inode = st.st_ino;
693 RtlLeaveCriticalSection( &cache_section );
695 TRACE("%d, %d\n", *dev, fd);
696 return ret;
699 /******************************************************************
700 * CDROM_GetStatusCode
704 static NTSTATUS CDROM_GetStatusCode(int io)
706 if (io == 0) return STATUS_SUCCESS;
707 return FILE_GetNtStatus();
710 /******************************************************************
711 * CDROM_GetControl
714 static NTSTATUS CDROM_GetControl(int dev, int fd, CDROM_AUDIO_CONTROL* cac)
716 #ifdef __APPLE__
717 uint16_t speed;
718 int io = ioctl( fd, DKIOCCDGETSPEED, &speed );
719 if (io != 0) return CDROM_GetStatusCode( io );
720 /* DKIOCCDGETSPEED returns the speed in kilobytes per second,
721 * so convert to logical blocks (assumed to be ~2 KB).
723 cac->LogicalBlocksPerSecond = speed/2;
724 #else
725 cac->LogicalBlocksPerSecond = 1; /* FIXME */
726 #endif
727 cac->LbaFormat = 0; /* FIXME */
728 return STATUS_NOT_SUPPORTED;
731 /******************************************************************
732 * CDROM_GetDeviceNumber
735 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
737 FIXME( "stub\n" );
738 devnum->DeviceType = FILE_DEVICE_DISK;
739 devnum->DeviceNumber = 1;
740 devnum->PartitionNumber = 1;
741 return STATUS_SUCCESS;
744 /******************************************************************
745 * CDROM_GetDriveGeometry
748 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
750 CDROM_TOC toc;
751 NTSTATUS ret = 0;
752 int fsize = 0;
754 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
756 fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
757 - FRAME_OF_TOC(toc, 1); /* Total size in frames */
759 dg->Cylinders.QuadPart = fsize / (64 * 32);
760 dg->MediaType = RemovableMedia;
761 dg->TracksPerCylinder = 64;
762 dg->SectorsPerTrack = 32;
763 dg->BytesPerSector= 2048;
764 return ret;
767 /******************************************************************
768 * CDROM_GetMediaType
771 static NTSTATUS CDROM_GetMediaType(int dev, GET_MEDIA_TYPES* medtype)
773 FIXME(": faking success\n");
774 medtype->DeviceType = FILE_DEVICE_CD_ROM;
775 medtype->MediaInfoCount = 0;
776 return STATUS_SUCCESS;
779 /**************************************************************************
780 * CDROM_Reset [internal]
782 static NTSTATUS CDROM_ResetAudio(int fd)
784 #if defined(linux)
785 return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
786 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
787 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
788 #else
789 FIXME("not supported on this O/S\n");
790 return STATUS_NOT_SUPPORTED;
791 #endif
794 /******************************************************************
795 * CDROM_SetTray
799 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
801 #if defined(linux)
802 return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
803 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
804 return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
805 (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
806 (ioctl(fd, CDIOCPREVENT, NULL)));
807 #elif defined(__APPLE__)
808 if (doEject) return CDROM_GetStatusCode( ioctl( fd, DKIOCEJECT, NULL ) );
809 else return STATUS_NOT_SUPPORTED;
810 #else
811 FIXME("not supported on this O/S\n");
812 return STATUS_NOT_SUPPORTED;
813 #endif
816 /******************************************************************
817 * CDROM_ControlEjection
821 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
823 #if defined(linux)
824 return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
825 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
826 return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
827 #else
828 FIXME("not supported on this O/S\n");
829 return STATUS_NOT_SUPPORTED;
830 #endif
833 /******************************************************************
834 * CDROM_ReadTOC
838 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
840 NTSTATUS ret = STATUS_NOT_SUPPORTED;
842 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
843 return STATUS_INVALID_PARAMETER;
845 RtlEnterCriticalSection( &cache_section );
846 if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
848 *toc = cdrom_cache[dev].toc;
849 ret = STATUS_SUCCESS;
851 RtlLeaveCriticalSection( &cache_section );
852 return ret;
855 /******************************************************************
856 * CDROM_GetDiskData
860 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
862 CDROM_TOC toc;
863 NTSTATUS ret;
864 int i;
866 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
867 data->DiskData = 0;
868 for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
869 if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
870 data->DiskData |= CDROM_DISK_DATA_TRACK;
871 else
872 data->DiskData |= CDROM_DISK_AUDIO_TRACK;
874 return STATUS_SUCCESS;
877 /******************************************************************
878 * CDROM_ReadQChannel
882 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
883 SUB_Q_CHANNEL_DATA* data)
885 NTSTATUS ret = STATUS_NOT_SUPPORTED;
886 #ifdef linux
887 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
888 int io;
889 struct cdrom_subchnl sc;
890 sc.cdsc_format = CDROM_MSF;
892 io = ioctl(fd, CDROMSUBCHNL, &sc);
893 if (io == -1)
895 TRACE("opened or no_media (%s)!\n", strerror(errno));
896 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
897 CDROM_ClearCacheEntry(dev);
898 goto end;
901 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
903 switch (sc.cdsc_audiostatus) {
904 case CDROM_AUDIO_INVALID:
905 CDROM_ClearCacheEntry(dev);
906 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
907 break;
908 case CDROM_AUDIO_NO_STATUS:
909 CDROM_ClearCacheEntry(dev);
910 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
911 break;
912 case CDROM_AUDIO_PLAY:
913 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
914 break;
915 case CDROM_AUDIO_PAUSED:
916 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
917 break;
918 case CDROM_AUDIO_COMPLETED:
919 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
920 break;
921 case CDROM_AUDIO_ERROR:
922 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
923 break;
924 default:
925 TRACE("status=%02X !\n", sc.cdsc_audiostatus);
926 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
928 switch (fmt->Format)
930 case IOCTL_CDROM_CURRENT_POSITION:
931 RtlEnterCriticalSection( &cache_section );
932 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
933 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
934 data->CurrentPosition.Control = sc.cdsc_ctrl;
935 data->CurrentPosition.ADR = sc.cdsc_adr;
936 data->CurrentPosition.TrackNumber = sc.cdsc_trk;
937 data->CurrentPosition.IndexNumber = sc.cdsc_ind;
939 data->CurrentPosition.AbsoluteAddress[0] = 0;
940 data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute;
941 data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
942 data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
944 data->CurrentPosition.TrackRelativeAddress[0] = 0;
945 data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute;
946 data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
947 data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
949 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
951 else /* not playing */
953 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
954 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
956 RtlLeaveCriticalSection( &cache_section );
957 break;
958 case IOCTL_CDROM_MEDIA_CATALOG:
959 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
961 struct cdrom_mcn mcn;
962 if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
964 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
965 data->MediaCatalog.Mcval = 0; /* FIXME */
966 memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
967 data->MediaCatalog.MediaCatalog[14] = 0;
969 break;
970 case IOCTL_CDROM_TRACK_ISRC:
971 FIXME("TrackIsrc: NIY on linux\n");
972 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
973 data->TrackIsrc.Tcval = 0;
974 io = 0;
975 break;
978 end:
979 ret = CDROM_GetStatusCode(io);
980 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
981 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
982 int io;
983 struct ioc_read_subchannel read_sc;
984 struct cd_sub_channel_info sc;
986 read_sc.address_format = CD_MSF_FORMAT;
987 read_sc.track = 0;
988 read_sc.data_len = sizeof(sc);
989 read_sc.data = &sc;
990 switch (fmt->Format)
992 case IOCTL_CDROM_CURRENT_POSITION:
993 read_sc.data_format = CD_CURRENT_POSITION;
994 break;
995 case IOCTL_CDROM_MEDIA_CATALOG:
996 read_sc.data_format = CD_MEDIA_CATALOG;
997 break;
998 case IOCTL_CDROM_TRACK_ISRC:
999 read_sc.data_format = CD_TRACK_INFO;
1000 sc.what.track_info.track_number = data->TrackIsrc.Track;
1001 break;
1003 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1004 if (io == -1)
1006 TRACE("opened or no_media (%s)!\n", strerror(errno));
1007 CDROM_ClearCacheEntry(dev);
1008 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1009 goto end;
1012 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
1014 switch (sc.header.audio_status) {
1015 case CD_AS_AUDIO_INVALID:
1016 CDROM_ClearCacheEntry(dev);
1017 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
1018 break;
1019 case CD_AS_NO_STATUS:
1020 CDROM_ClearCacheEntry(dev);
1021 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1022 break;
1023 case CD_AS_PLAY_IN_PROGRESS:
1024 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
1025 break;
1026 case CD_AS_PLAY_PAUSED:
1027 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
1028 break;
1029 case CD_AS_PLAY_COMPLETED:
1030 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
1031 break;
1032 case CD_AS_PLAY_ERROR:
1033 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
1034 break;
1035 default:
1036 TRACE("status=%02X !\n", sc.header.audio_status);
1037 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1039 switch (fmt->Format)
1041 case IOCTL_CDROM_CURRENT_POSITION:
1042 RtlEnterCriticalSection( &cache_section );
1043 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
1044 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1045 data->CurrentPosition.Control = sc.what.position.control;
1046 data->CurrentPosition.ADR = sc.what.position.addr_type;
1047 data->CurrentPosition.TrackNumber = sc.what.position.track_number;
1048 data->CurrentPosition.IndexNumber = sc.what.position.index_number;
1050 data->CurrentPosition.AbsoluteAddress[0] = 0;
1051 data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
1052 data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
1053 data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
1054 data->CurrentPosition.TrackRelativeAddress[0] = 0;
1055 data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
1056 data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
1057 data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
1058 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
1060 else { /* not playing */
1061 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
1062 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
1064 RtlLeaveCriticalSection( &cache_section );
1065 break;
1066 case IOCTL_CDROM_MEDIA_CATALOG:
1067 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
1068 data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
1069 memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
1070 break;
1071 case IOCTL_CDROM_TRACK_ISRC:
1072 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
1073 data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
1074 memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
1075 break;
1078 end:
1079 ret = CDROM_GetStatusCode(io);
1080 #elif defined(__APPLE__)
1081 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
1082 int io = 0;
1083 union
1085 dk_cd_read_mcn_t mcn;
1086 dk_cd_read_isrc_t isrc;
1087 } ioc;
1088 /* We need IOCDAudioControl for IOCTL_CDROM_CURRENT_POSITION */
1089 if (fmt->Format == IOCTL_CDROM_CURRENT_POSITION)
1091 FIXME("NIY\n");
1092 return STATUS_NOT_SUPPORTED;
1094 /* No IOCDAudioControl support; just set the audio status to none */
1095 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1096 switch(fmt->Format)
1098 case IOCTL_CDROM_MEDIA_CATALOG:
1099 if ((io = ioctl(fd, DKIOCCDREADMCN, &ioc.mcn)) == -1) break;
1100 memcpy(data->MediaCatalog.MediaCatalog, ioc.mcn.mcn, kCDMCNMaxLength);
1101 data->MediaCatalog.Mcval = 1;
1102 break;
1103 case IOCTL_CDROM_TRACK_ISRC:
1104 ioc.isrc.track = fmt->Track;
1105 if ((io = ioctl(fd, DKIOCCDREADISRC, &ioc.isrc)) == -1) break;
1106 memcpy(data->TrackIsrc.TrackIsrc, ioc.isrc.isrc, kCDISRCMaxLength);
1107 data->TrackIsrc.Tcval = 1;
1108 data->TrackIsrc.Track = ioc.isrc.track;
1110 ret = CDROM_GetStatusCode(io);
1111 #endif
1112 return ret;
1115 /******************************************************************
1116 * CDROM_Verify
1117 * Implements: IOCTL_STORAGE_CHECK_VERIFY
1118 * IOCTL_CDROM_CHECK_VERIFY
1119 * IOCTL_DISK_CHECK_VERIFY
1122 static NTSTATUS CDROM_Verify(int dev, int fd)
1124 #if defined(linux)
1125 int ret;
1127 ret = ioctl(fd, CDROM_DRIVE_STATUS, NULL);
1128 if(ret == -1) {
1129 TRACE("ioctl CDROM_DRIVE_STATUS failed(%s)!\n", strerror(errno));
1130 return CDROM_GetStatusCode(ret);
1133 if(ret == CDS_DISC_OK)
1134 return STATUS_SUCCESS;
1135 else
1136 return STATUS_NO_MEDIA_IN_DEVICE;
1137 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1138 int ret;
1139 ret = ioctl(fd, CDIOCSTART, NULL);
1140 if(ret == 0)
1141 return STATUS_SUCCESS;
1142 else
1143 return STATUS_NO_MEDIA_IN_DEVICE;
1144 #elif defined(__APPLE__)
1145 /* At this point, we know that we have media, because in Mac OS X, the
1146 * device file is only created when media is present. */
1147 return STATUS_SUCCESS;
1148 #else
1149 FIXME("not supported on this O/S\n");
1150 return STATUS_NOT_SUPPORTED;
1151 #endif
1154 /******************************************************************
1155 * CDROM_PlayAudioMSF
1159 static NTSTATUS CDROM_PlayAudioMSF(int fd, const CDROM_PLAY_AUDIO_MSF* audio_msf)
1161 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1162 #ifdef linux
1163 struct cdrom_msf msf;
1164 int io;
1166 msf.cdmsf_min0 = audio_msf->StartingM;
1167 msf.cdmsf_sec0 = audio_msf->StartingS;
1168 msf.cdmsf_frame0 = audio_msf->StartingF;
1169 msf.cdmsf_min1 = audio_msf->EndingM;
1170 msf.cdmsf_sec1 = audio_msf->EndingS;
1171 msf.cdmsf_frame1 = audio_msf->EndingF;
1173 io = ioctl(fd, CDROMSTART);
1174 if (io == -1)
1176 WARN("motor doesn't start !\n");
1177 goto end;
1179 io = ioctl(fd, CDROMPLAYMSF, &msf);
1180 if (io == -1)
1182 WARN("device doesn't play !\n");
1183 goto end;
1185 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1186 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
1187 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
1188 end:
1189 ret = CDROM_GetStatusCode(io);
1190 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1191 struct ioc_play_msf msf;
1192 int io;
1194 msf.start_m = audio_msf->StartingM;
1195 msf.start_s = audio_msf->StartingS;
1196 msf.start_f = audio_msf->StartingF;
1197 msf.end_m = audio_msf->EndingM;
1198 msf.end_s = audio_msf->EndingS;
1199 msf.end_f = audio_msf->EndingF;
1201 io = ioctl(fd, CDIOCSTART, NULL);
1202 if (io == -1)
1204 WARN("motor doesn't start !\n");
1205 goto end;
1207 io = ioctl(fd, CDIOCPLAYMSF, &msf);
1208 if (io == -1)
1210 WARN("device doesn't play !\n");
1211 goto end;
1213 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1214 msf.start_m, msf.start_s, msf.start_f,
1215 msf.end_m, msf.end_s, msf.end_f);
1216 end:
1217 ret = CDROM_GetStatusCode(io);
1218 #endif
1219 return ret;
1222 /******************************************************************
1223 * CDROM_SeekAudioMSF
1227 static NTSTATUS CDROM_SeekAudioMSF(int dev, int fd, const CDROM_SEEK_AUDIO_MSF* audio_msf)
1229 CDROM_TOC toc;
1230 int i, io, frame;
1231 SUB_Q_CURRENT_POSITION *cp;
1232 #if defined(linux)
1233 struct cdrom_msf0 msf;
1234 struct cdrom_subchnl sc;
1235 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1236 struct ioc_play_msf msf;
1237 struct ioc_read_subchannel read_sc;
1238 struct cd_sub_channel_info sc;
1239 int final_frame;
1240 #endif
1242 /* Use the information on the TOC to compute the new current
1243 * position, which is shadowed on the cache. [Portable]. */
1244 frame = FRAME_OF_MSF(*audio_msf);
1246 if ((io = CDROM_ReadTOC(dev, fd, &toc)) != 0) return io;
1248 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
1249 if (FRAME_OF_TOC(toc,i)>frame) break;
1250 if (i <= toc.FirstTrack || i > toc.LastTrack+1)
1251 return STATUS_INVALID_PARAMETER;
1252 i--;
1253 RtlEnterCriticalSection( &cache_section );
1254 cp = &cdrom_cache[dev].CurrentPosition;
1255 cp->FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1256 cp->Control = toc.TrackData[i-toc.FirstTrack].Control;
1257 cp->ADR = toc.TrackData[i-toc.FirstTrack].Adr;
1258 cp->TrackNumber = toc.TrackData[i-toc.FirstTrack].TrackNumber;
1259 cp->IndexNumber = 0; /* FIXME: where do they keep these? */
1260 cp->AbsoluteAddress[0] = 0;
1261 cp->AbsoluteAddress[1] = toc.TrackData[i-toc.FirstTrack].Address[1];
1262 cp->AbsoluteAddress[2] = toc.TrackData[i-toc.FirstTrack].Address[2];
1263 cp->AbsoluteAddress[3] = toc.TrackData[i-toc.FirstTrack].Address[3];
1264 frame -= FRAME_OF_TOC(toc,i);
1265 cp->TrackRelativeAddress[0] = 0;
1266 MSF_OF_FRAME(cp->TrackRelativeAddress[1], frame);
1267 RtlLeaveCriticalSection( &cache_section );
1269 /* If playing, then issue a seek command, otherwise do nothing */
1270 #ifdef linux
1271 sc.cdsc_format = CDROM_MSF;
1273 io = ioctl(fd, CDROMSUBCHNL, &sc);
1274 if (io == -1)
1276 TRACE("opened or no_media (%s)!\n", strerror(errno));
1277 CDROM_ClearCacheEntry(dev);
1278 return CDROM_GetStatusCode(io);
1280 if (sc.cdsc_audiostatus==CDROM_AUDIO_PLAY)
1282 msf.minute = audio_msf->M;
1283 msf.second = audio_msf->S;
1284 msf.frame = audio_msf->F;
1285 return CDROM_GetStatusCode(ioctl(fd, CDROMSEEK, &msf));
1287 return STATUS_SUCCESS;
1288 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1289 read_sc.address_format = CD_MSF_FORMAT;
1290 read_sc.track = 0;
1291 read_sc.data_len = sizeof(sc);
1292 read_sc.data = &sc;
1293 read_sc.data_format = CD_CURRENT_POSITION;
1295 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1296 if (io == -1)
1298 TRACE("opened or no_media (%s)!\n", strerror(errno));
1299 CDROM_ClearCacheEntry(dev);
1300 return CDROM_GetStatusCode(io);
1302 if (sc.header.audio_status==CD_AS_PLAY_IN_PROGRESS)
1305 msf.start_m = audio_msf->M;
1306 msf.start_s = audio_msf->S;
1307 msf.start_f = audio_msf->F;
1308 final_frame = FRAME_OF_TOC(toc,toc.LastTrack+1)-1;
1309 MSF_OF_FRAME(msf.end_m, final_frame);
1311 return CDROM_GetStatusCode(ioctl(fd, CDIOCPLAYMSF, &msf));
1313 return STATUS_SUCCESS;
1314 #else
1315 FIXME("not supported on this O/S\n");
1316 return STATUS_NOT_SUPPORTED;
1317 #endif
1320 /******************************************************************
1321 * CDROM_PauseAudio
1325 static NTSTATUS CDROM_PauseAudio(int fd)
1327 #if defined(linux)
1328 return CDROM_GetStatusCode(ioctl(fd, CDROMPAUSE));
1329 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1330 return CDROM_GetStatusCode(ioctl(fd, CDIOCPAUSE, NULL));
1331 #else
1332 FIXME(": not supported on this O/S\n");
1333 return STATUS_NOT_SUPPORTED;
1334 #endif
1337 /******************************************************************
1338 * CDROM_ResumeAudio
1342 static NTSTATUS CDROM_ResumeAudio(int fd)
1344 #if defined(linux)
1345 return CDROM_GetStatusCode(ioctl(fd, CDROMRESUME));
1346 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1347 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESUME, NULL));
1348 #else
1349 FIXME("not supported on this O/S\n");
1350 return STATUS_NOT_SUPPORTED;
1351 #endif
1354 /******************************************************************
1355 * CDROM_StopAudio
1359 static NTSTATUS CDROM_StopAudio(int fd)
1361 #if defined(linux)
1362 return CDROM_GetStatusCode(ioctl(fd, CDROMSTOP));
1363 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1364 return CDROM_GetStatusCode(ioctl(fd, CDIOCSTOP, NULL));
1365 #else
1366 FIXME("not supported on this O/S\n");
1367 return STATUS_NOT_SUPPORTED;
1368 #endif
1371 /******************************************************************
1372 * CDROM_GetVolume
1376 static NTSTATUS CDROM_GetVolume(int fd, VOLUME_CONTROL* vc)
1378 #if defined(linux)
1379 struct cdrom_volctrl volc;
1380 int io;
1382 io = ioctl(fd, CDROMVOLREAD, &volc);
1383 if (io != -1)
1385 vc->PortVolume[0] = volc.channel0;
1386 vc->PortVolume[1] = volc.channel1;
1387 vc->PortVolume[2] = volc.channel2;
1388 vc->PortVolume[3] = volc.channel3;
1390 return CDROM_GetStatusCode(io);
1391 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1392 struct ioc_vol volc;
1393 int io;
1395 io = ioctl(fd, CDIOCGETVOL, &volc);
1396 if (io != -1)
1398 vc->PortVolume[0] = volc.vol[0];
1399 vc->PortVolume[1] = volc.vol[1];
1400 vc->PortVolume[2] = volc.vol[2];
1401 vc->PortVolume[3] = volc.vol[3];
1403 return CDROM_GetStatusCode(io);
1404 #else
1405 FIXME("not supported on this O/S\n");
1406 return STATUS_NOT_SUPPORTED;
1407 #endif
1410 /******************************************************************
1411 * CDROM_SetVolume
1415 static NTSTATUS CDROM_SetVolume(int fd, const VOLUME_CONTROL* vc)
1417 #if defined(linux)
1418 struct cdrom_volctrl volc;
1420 volc.channel0 = vc->PortVolume[0];
1421 volc.channel1 = vc->PortVolume[1];
1422 volc.channel2 = vc->PortVolume[2];
1423 volc.channel3 = vc->PortVolume[3];
1425 return CDROM_GetStatusCode(ioctl(fd, CDROMVOLCTRL, &volc));
1426 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__DragonFly__)
1427 struct ioc_vol volc;
1429 volc.vol[0] = vc->PortVolume[0];
1430 volc.vol[1] = vc->PortVolume[1];
1431 volc.vol[2] = vc->PortVolume[2];
1432 volc.vol[3] = vc->PortVolume[3];
1434 return CDROM_GetStatusCode(ioctl(fd, CDIOCSETVOL, &volc));
1435 #else
1436 FIXME(": not supported on this O/S\n");
1437 return STATUS_NOT_SUPPORTED;
1438 #endif
1441 /******************************************************************
1442 * CDROM_RawRead
1444 * Some features of this IOCTL are rather poorly documented and
1445 * not really intuitive either:
1447 * 1. Although the DiskOffset parameter is meant to be a
1448 * byte offset into the disk, it is in fact the sector
1449 * number multiplied by 2048 regardless of the actual
1450 * sector size.
1452 * 2. The least significant 11 bits of DiskOffset are ignored.
1454 * 3. The TrackMode parameter has no effect on the sector
1455 * size. The entire raw sector (i.e. 2352 bytes of data)
1456 * is always returned. IMO the TrackMode is only used
1457 * to check the correct sector type.
1460 static NTSTATUS CDROM_RawRead(int fd, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
1462 int ret = STATUS_NOT_SUPPORTED;
1463 int io = -1;
1464 #ifdef __APPLE__
1465 dk_cd_read_t cdrd;
1466 #endif
1468 TRACE("RAW_READ_INFO: DiskOffset=%i,%i SectorCount=%i TrackMode=%i\n buffer=%p len=%i sz=%p\n",
1469 raw->DiskOffset.u.HighPart, raw->DiskOffset.u.LowPart, raw->SectorCount, raw->TrackMode, buffer, len, sz);
1471 if (len < raw->SectorCount * 2352) return STATUS_BUFFER_TOO_SMALL;
1473 #if defined(linux)
1474 if (raw->DiskOffset.u.HighPart & ~2047) {
1475 WARN("DiskOffset points to a sector >= 2**32\n");
1476 return ret;
1479 switch (raw->TrackMode)
1481 case YellowMode2:
1482 case XAForm2:
1484 DWORD lba = raw->DiskOffset.QuadPart >> 11;
1485 struct cdrom_msf* msf;
1486 PBYTE *bp; /* current buffer pointer */
1487 DWORD i;
1489 if ((lba + raw->SectorCount) >
1490 ((1 << 8*sizeof(msf->cdmsf_min0)) * CD_SECS * CD_FRAMES
1491 - CD_MSF_OFFSET)) {
1492 WARN("DiskOffset not accessible with MSF\n");
1493 return ret;
1496 /* Linux reads only one sector at a time.
1497 * ioctl CDROMREADRAW takes struct cdrom_msf as an argument
1498 * on the contrary to what header comments state.
1500 lba += CD_MSF_OFFSET;
1501 for (i = 0, bp = buffer; i < raw->SectorCount;
1502 i++, lba++, bp += 2352)
1504 msf = (struct cdrom_msf*)bp;
1505 msf->cdmsf_min0 = lba / CD_FRAMES / CD_SECS;
1506 msf->cdmsf_sec0 = lba / CD_FRAMES % CD_SECS;
1507 msf->cdmsf_frame0 = lba % CD_FRAMES;
1508 io = ioctl(fd, CDROMREADRAW, msf);
1509 if (io != 0)
1511 *sz = 2352 * i;
1512 return CDROM_GetStatusCode(io);
1515 break;
1518 case CDDA:
1520 struct cdrom_read_audio cdra;
1522 cdra.addr.lba = raw->DiskOffset.QuadPart >> 11;
1523 TRACE("reading at %u\n", cdra.addr.lba);
1524 cdra.addr_format = CDROM_LBA;
1525 cdra.nframes = raw->SectorCount;
1526 cdra.buf = buffer;
1527 io = ioctl(fd, CDROMREADAUDIO, &cdra);
1528 break;
1531 default:
1532 FIXME("NIY: %d\n", raw->TrackMode);
1533 return STATUS_INVALID_PARAMETER;
1535 #elif defined(__APPLE__)
1536 /* Mac OS lets us read multiple parts of the sector at a time.
1537 * We can read all the sectors in at once, unlike Linux.
1539 memset(&cdrd, 0, sizeof(cdrd));
1540 cdrd.offset = (raw->DiskOffset.QuadPart >> 11) * kCDSectorSizeWhole;
1541 cdrd.buffer = buffer;
1542 cdrd.bufferLength = raw->SectorCount * kCDSectorSizeWhole;
1543 switch (raw->TrackMode)
1545 case YellowMode2:
1546 cdrd.sectorType = kCDSectorTypeMode2;
1547 cdrd.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader | kCDSectorAreaUser;
1548 break;
1550 case XAForm2:
1551 cdrd.sectorType = kCDSectorTypeMode2Form2;
1552 cdrd.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader | kCDSectorAreaSubHeader | kCDSectorAreaUser;
1553 break;
1555 case CDDA:
1556 cdrd.sectorType = kCDSectorTypeCDDA;
1557 cdrd.sectorArea = kCDSectorAreaUser;
1558 break;
1560 default:
1561 FIXME("NIY: %d\n", raw->TrackMode);
1562 return STATUS_INVALID_PARAMETER;
1564 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1565 if (io != 0)
1567 *sz = cdrd.bufferLength;
1568 return CDROM_GetStatusCode(io);
1570 #else
1571 switch (raw->TrackMode)
1573 case YellowMode2:
1574 FIXME("YellowMode2: NIY\n");
1575 return ret;
1576 case XAForm2:
1577 FIXME("XAForm2: NIY\n");
1578 return ret;
1579 case CDDA:
1580 FIXME("CDDA: NIY\n");
1581 return ret;
1582 default:
1583 FIXME("NIY: %d\n", raw->TrackMode);
1584 return STATUS_INVALID_PARAMETER;
1586 #endif
1588 *sz = 2352 * raw->SectorCount;
1589 ret = CDROM_GetStatusCode(io);
1590 return ret;
1593 /******************************************************************
1594 * CDROM_ScsiPassThroughDirect
1595 * Implements IOCTL_SCSI_PASS_THROUGH_DIRECT
1598 static NTSTATUS CDROM_ScsiPassThroughDirect(int fd, PSCSI_PASS_THROUGH_DIRECT pPacket)
1600 int ret = STATUS_NOT_SUPPORTED;
1601 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1602 sg_io_hdr_t cmd;
1603 int io;
1604 #elif defined HAVE_SCSIREQ_T_CMD
1605 scsireq_t cmd;
1606 int io;
1607 #elif defined __APPLE__
1608 dk_scsi_command_t cmd;
1609 int io;
1610 #endif
1612 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1613 return STATUS_BUFFER_TOO_SMALL;
1615 if (pPacket->CdbLength > 16)
1616 return STATUS_INVALID_PARAMETER;
1618 #ifdef SENSEBUFLEN
1619 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1620 return STATUS_INVALID_PARAMETER;
1621 #elif defined HAVE_REQUEST_SENSE
1622 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1623 return STATUS_INVALID_PARAMETER;
1624 #endif
1626 if (pPacket->DataTransferLength > 0 && !pPacket->DataBuffer)
1627 return STATUS_INVALID_PARAMETER;
1629 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1630 RtlZeroMemory(&cmd, sizeof(cmd));
1632 cmd.interface_id = 'S';
1633 cmd.cmd_len = pPacket->CdbLength;
1634 cmd.mx_sb_len = pPacket->SenseInfoLength;
1635 cmd.dxfer_len = pPacket->DataTransferLength;
1636 cmd.dxferp = pPacket->DataBuffer;
1637 cmd.cmdp = pPacket->Cdb;
1638 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1639 cmd.timeout = pPacket->TimeOutValue*1000;
1641 switch (pPacket->DataIn)
1643 case SCSI_IOCTL_DATA_IN:
1644 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1645 break;
1646 case SCSI_IOCTL_DATA_OUT:
1647 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1648 break;
1649 case SCSI_IOCTL_DATA_UNSPECIFIED:
1650 cmd.dxfer_direction = SG_DXFER_NONE;
1651 break;
1652 default:
1653 return STATUS_INVALID_PARAMETER;
1656 io = ioctl(fd, SG_IO, &cmd);
1658 pPacket->ScsiStatus = cmd.status;
1659 pPacket->DataTransferLength = cmd.resid;
1660 pPacket->SenseInfoLength = cmd.sb_len_wr;
1662 ret = CDROM_GetStatusCode(io);
1664 #elif defined HAVE_SCSIREQ_T_CMD
1666 memset(&cmd, 0, sizeof(cmd));
1667 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1669 cmd.cmdlen = pPacket->CdbLength;
1670 cmd.databuf = pPacket->DataBuffer;
1671 cmd.datalen = pPacket->DataTransferLength;
1672 cmd.senselen = pPacket->SenseInfoLength;
1673 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1675 switch (pPacket->DataIn)
1677 case SCSI_IOCTL_DATA_OUT:
1678 cmd.flags |= SCCMD_WRITE;
1679 break;
1680 case SCSI_IOCTL_DATA_IN:
1681 cmd.flags |= SCCMD_READ;
1682 break;
1683 case SCSI_IOCTL_DATA_UNSPECIFIED:
1684 cmd.flags = 0;
1685 break;
1686 default:
1687 return STATUS_INVALID_PARAMETER;
1690 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1692 switch (cmd.retsts)
1694 case SCCMD_OK: break;
1695 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1696 break;
1697 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1698 break;
1699 case SCCMD_SENSE: break;
1700 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1701 break;
1704 if (pPacket->SenseInfoLength != 0)
1706 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1707 cmd.sense, pPacket->SenseInfoLength);
1710 pPacket->ScsiStatus = cmd.status;
1712 ret = CDROM_GetStatusCode(io);
1714 #elif defined(__APPLE__)
1716 memset(&cmd, 0, sizeof(cmd));
1717 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1719 cmd.cdbSize = pPacket->CdbLength;
1720 cmd.buffer = pPacket->DataBuffer;
1721 cmd.bufferSize = pPacket->DataTransferLength;
1722 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1723 cmd.senseLen = pPacket->SenseInfoLength;
1724 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1726 switch (pPacket->DataIn)
1728 case SCSI_IOCTL_DATA_OUT:
1729 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
1730 break;
1731 case SCSI_IOCTL_DATA_IN:
1732 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
1733 break;
1734 case SCSI_IOCTL_DATA_UNSPECIFIED:
1735 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
1736 break;
1737 default:
1738 return STATUS_INVALID_PARAMETER;
1741 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
1743 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
1745 /* Command failed */
1746 switch (cmd.status)
1748 case kSCSITaskStatus_TaskTimeoutOccurred: return STATUS_TIMEOUT;
1749 break;
1750 case kSCSITaskStatus_ProtocolTimeoutOccurred: return STATUS_IO_TIMEOUT;
1751 break;
1752 case kSCSITaskStatus_DeviceNotResponding: return STATUS_DEVICE_BUSY;
1753 break;
1754 case kSCSITaskStatus_DeviceNotPresent:
1755 return STATUS_NO_SUCH_DEVICE;
1756 break;
1757 case kSCSITaskStatus_DeliveryFailure:
1758 return STATUS_DEVICE_PROTOCOL_ERROR;
1759 break;
1760 case kSCSITaskStatus_No_Status:
1761 default:
1762 return STATUS_UNSUCCESSFUL;
1763 break;
1767 if (cmd.status != kSCSITaskStatus_No_Status)
1768 pPacket->ScsiStatus = cmd.status;
1770 ret = CDROM_GetStatusCode(io);
1771 #endif
1772 return ret;
1775 /******************************************************************
1776 * CDROM_ScsiPassThrough
1777 * Implements IOCTL_SCSI_PASS_THROUGH
1780 static NTSTATUS CDROM_ScsiPassThrough(int fd, PSCSI_PASS_THROUGH pPacket)
1782 int ret = STATUS_NOT_SUPPORTED;
1783 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1784 sg_io_hdr_t cmd;
1785 int io;
1786 #elif defined HAVE_SCSIREQ_T_CMD
1787 scsireq_t cmd;
1788 int io;
1789 #elif defined __APPLE__
1790 dk_scsi_command_t cmd;
1791 int io;
1792 #endif
1794 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1795 return STATUS_BUFFER_TOO_SMALL;
1797 if (pPacket->CdbLength > 16)
1798 return STATUS_INVALID_PARAMETER;
1800 #ifdef SENSEBUFLEN
1801 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1802 return STATUS_INVALID_PARAMETER;
1803 #elif defined HAVE_REQUEST_SENSE
1804 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1805 return STATUS_INVALID_PARAMETER;
1806 #endif
1808 if (pPacket->DataTransferLength > 0 && pPacket->DataBufferOffset < sizeof(SCSI_PASS_THROUGH))
1809 return STATUS_INVALID_PARAMETER;
1811 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1812 RtlZeroMemory(&cmd, sizeof(cmd));
1814 cmd.interface_id = 'S';
1815 cmd.dxfer_len = pPacket->DataTransferLength;
1816 cmd.dxferp = (char*)pPacket + pPacket->DataBufferOffset;
1817 cmd.cmd_len = pPacket->CdbLength;
1818 cmd.cmdp = pPacket->Cdb;
1819 cmd.mx_sb_len = pPacket->SenseInfoLength;
1820 cmd.timeout = pPacket->TimeOutValue*1000;
1822 if(cmd.mx_sb_len > 0)
1823 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1825 switch (pPacket->DataIn)
1827 case SCSI_IOCTL_DATA_IN:
1828 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1829 break;
1830 case SCSI_IOCTL_DATA_OUT:
1831 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1832 break;
1833 case SCSI_IOCTL_DATA_UNSPECIFIED:
1834 cmd.dxfer_direction = SG_DXFER_NONE;
1835 break;
1836 default:
1837 return STATUS_INVALID_PARAMETER;
1840 io = ioctl(fd, SG_IO, &cmd);
1842 pPacket->ScsiStatus = cmd.status;
1843 pPacket->DataTransferLength = cmd.resid;
1844 pPacket->SenseInfoLength = cmd.sb_len_wr;
1846 ret = CDROM_GetStatusCode(io);
1848 #elif defined HAVE_SCSIREQ_T_CMD
1850 memset(&cmd, 0, sizeof(cmd));
1851 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1853 if ( pPacket->DataBufferOffset > 0x1000 )
1855 cmd.databuf = (void*)pPacket->DataBufferOffset;
1857 else
1859 cmd.databuf = (char*)pPacket + pPacket->DataBufferOffset;
1862 cmd.cmdlen = pPacket->CdbLength;
1863 cmd.datalen = pPacket->DataTransferLength;
1864 cmd.senselen = pPacket->SenseInfoLength;
1865 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1867 switch (pPacket->DataIn)
1869 case SCSI_IOCTL_DATA_OUT:
1870 cmd.flags |= SCCMD_WRITE;
1871 break;
1872 case SCSI_IOCTL_DATA_IN:
1873 cmd.flags |= SCCMD_READ;
1874 break;
1875 case SCSI_IOCTL_DATA_UNSPECIFIED:
1876 cmd.flags = 0;
1877 break;
1878 default:
1879 return STATUS_INVALID_PARAMETER;
1882 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1884 switch (cmd.retsts)
1886 case SCCMD_OK: break;
1887 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1888 break;
1889 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1890 break;
1891 case SCCMD_SENSE: break;
1892 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1893 break;
1896 if (pPacket->SenseInfoLength != 0)
1898 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1899 cmd.sense, pPacket->SenseInfoLength);
1902 pPacket->ScsiStatus = cmd.status;
1904 ret = CDROM_GetStatusCode(io);
1906 #elif defined(__APPLE__)
1908 memset(&cmd, 0, sizeof(cmd));
1909 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1911 cmd.cdbSize = pPacket->CdbLength;
1912 cmd.buffer = (char*)pPacket + pPacket->DataBufferOffset;
1913 cmd.bufferSize = pPacket->DataTransferLength;
1914 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1915 cmd.senseLen = pPacket->SenseInfoLength;
1916 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1918 switch (pPacket->DataIn)
1920 case SCSI_IOCTL_DATA_OUT:
1921 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
1922 break;
1923 case SCSI_IOCTL_DATA_IN:
1924 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
1925 break;
1926 case SCSI_IOCTL_DATA_UNSPECIFIED:
1927 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
1928 break;
1929 default:
1930 return STATUS_INVALID_PARAMETER;
1933 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
1935 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
1937 /* Command failed */
1938 switch (cmd.status)
1940 case kSCSITaskStatus_TaskTimeoutOccurred:
1941 return STATUS_TIMEOUT;
1942 break;
1943 case kSCSITaskStatus_ProtocolTimeoutOccurred:
1944 return STATUS_IO_TIMEOUT;
1945 break;
1946 case kSCSITaskStatus_DeviceNotResponding:
1947 return STATUS_DEVICE_BUSY;
1948 break;
1949 case kSCSITaskStatus_DeviceNotPresent:
1950 return STATUS_NO_SUCH_DEVICE;
1951 break;
1952 case kSCSITaskStatus_DeliveryFailure:
1953 return STATUS_DEVICE_PROTOCOL_ERROR;
1954 break;
1955 case kSCSITaskStatus_No_Status:
1956 default:
1957 return STATUS_UNSUCCESSFUL;
1958 break;
1962 if (cmd.status != kSCSITaskStatus_No_Status)
1963 pPacket->ScsiStatus = cmd.status;
1965 ret = CDROM_GetStatusCode(io);
1966 #endif
1967 return ret;
1970 /******************************************************************
1971 * CDROM_ScsiGetCaps
1975 static NTSTATUS CDROM_ScsiGetCaps(int fd, PIO_SCSI_CAPABILITIES caps)
1977 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
1979 #ifdef SG_SCATTER_SZ
1980 caps->Length = sizeof(*caps);
1981 caps->MaximumTransferLength = SG_SCATTER_SZ; /* FIXME */
1982 caps->MaximumPhysicalPages = SG_SCATTER_SZ / page_size;
1983 caps->SupportedAsynchronousEvents = TRUE;
1984 caps->AlignmentMask = page_size;
1985 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
1986 caps->AdapterScansDown = FALSE; /* FIXME ? */
1987 caps->AdapterUsesPio = FALSE; /* FIXME ? */
1988 ret = STATUS_SUCCESS;
1989 #elif defined __APPLE__
1990 uint64_t bytesr, bytesw, align;
1991 int io = ioctl(fd, DKIOCGETMAXBYTECOUNTREAD, &bytesr);
1992 if (io != 0) return CDROM_GetStatusCode(io);
1993 io = ioctl(fd, DKIOCGETMAXBYTECOUNTWRITE, &bytesw);
1994 if (io != 0) return CDROM_GetStatusCode(io);
1995 io = ioctl(fd, DKIOCGETMINSEGMENTALIGNMENTBYTECOUNT, &align);
1996 if (io != 0) return CDROM_GetStatusCode(io);
1997 caps->Length = sizeof(*caps);
1998 caps->MaximumTransferLength = bytesr < bytesw ? bytesr : bytesw;
1999 caps->MaximumPhysicalPages = caps->MaximumTransferLength / page_size;
2000 caps->SupportedAsynchronousEvents = TRUE;
2001 caps->AlignmentMask = align-1;
2002 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
2003 caps->AdapterScansDown = FALSE; /* FIXME ? */
2004 caps->AdapterUsesPio = FALSE; /* FIXME ? */
2005 ret = STATUS_SUCCESS;
2006 #else
2007 FIXME("Unimplemented\n");
2008 #endif
2009 return ret;
2012 /******************************************************************
2013 * CDROM_GetAddress
2015 * implements IOCTL_SCSI_GET_ADDRESS
2017 static NTSTATUS CDROM_GetAddress(int fd, SCSI_ADDRESS* address)
2019 UCHAR portnum, busid, targetid, lun;
2021 address->Length = sizeof(SCSI_ADDRESS);
2022 if ( ! CDROM_GetInterfaceInfo(fd, &portnum, &busid, &targetid, &lun))
2023 return STATUS_NOT_SUPPORTED;
2025 address->PortNumber = portnum; /* primary=0 secondary=1 for ide */
2026 address->PathId = busid; /* always 0 for ide */
2027 address->TargetId = targetid; /* master=0 slave=1 for ide */
2028 address->Lun = lun;
2029 return STATUS_SUCCESS;
2032 /******************************************************************
2033 * DVD_StartSession
2037 static NTSTATUS DVD_StartSession(int fd, const DVD_SESSION_ID *sid_in, PDVD_SESSION_ID sid_out)
2039 #if defined(linux)
2040 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2041 dvd_authinfo auth_info;
2043 memset( &auth_info, 0, sizeof( auth_info ) );
2044 auth_info.type = DVD_LU_SEND_AGID;
2045 if (sid_in) auth_info.lsa.agid = *(const int*)sid_in; /* ?*/
2047 TRACE("fd 0x%08x\n",fd);
2048 ret =CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2049 *sid_out = auth_info.lsa.agid;
2050 return ret;
2051 #elif defined(__APPLE__)
2052 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2053 dk_dvd_report_key_t dvdrk;
2054 DVDAuthenticationGrantIDInfo agid_info;
2056 dvdrk.format = kDVDKeyFormatAGID_CSS;
2057 dvdrk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2058 if(sid_in) dvdrk.grantID = *(uint8_t*)sid_in; /* ? */
2059 dvdrk.bufferLength = sizeof(DVDAuthenticationGrantIDInfo);
2060 dvdrk.buffer = &agid_info;
2062 ret = CDROM_GetStatusCode(ioctl(fd, DKIOCDVDREPORTKEY, &dvdrk));
2063 *sid_out = agid_info.grantID;
2064 return ret;
2065 #else
2066 FIXME("not supported on this O/S\n");
2067 return STATUS_NOT_SUPPORTED;
2068 #endif
2071 /******************************************************************
2072 * DVD_EndSession
2076 static NTSTATUS DVD_EndSession(int fd, const DVD_SESSION_ID *sid)
2078 #if defined(linux)
2079 dvd_authinfo auth_info;
2081 memset( &auth_info, 0, sizeof( auth_info ) );
2082 auth_info.type = DVD_INVALIDATE_AGID;
2083 auth_info.lsa.agid = *(const int*)sid;
2085 TRACE("\n");
2086 return CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2087 #elif defined(__APPLE__)
2088 dk_dvd_send_key_t dvdsk;
2090 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2091 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2092 dvdsk.grantID = (uint8_t)*sid;
2094 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2095 #else
2096 FIXME("not supported on this O/S\n");
2097 return STATUS_NOT_SUPPORTED;
2098 #endif
2101 /******************************************************************
2102 * DVD_SendKey
2106 static NTSTATUS DVD_SendKey(int fd, const DVD_COPY_PROTECT_KEY *key)
2108 #if defined(linux)
2109 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2110 dvd_authinfo auth_info;
2112 memset( &auth_info, 0, sizeof( auth_info ) );
2113 switch (key->KeyType)
2115 case DvdChallengeKey:
2116 auth_info.type = DVD_HOST_SEND_CHALLENGE;
2117 auth_info.hsc.agid = (int)key->SessionId;
2118 TRACE("DvdChallengeKey ioc 0x%x\n", DVD_AUTH );
2119 memcpy( auth_info.hsc.chal, key->KeyData, DVD_CHALLENGE_SIZE );
2120 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2121 break;
2122 case DvdBusKey2:
2123 auth_info.type = DVD_HOST_SEND_KEY2;
2124 auth_info.hsk.agid = (int)key->SessionId;
2126 memcpy( auth_info.hsk.key, key->KeyData, DVD_KEY_SIZE );
2128 TRACE("DvdBusKey2\n");
2129 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2130 break;
2132 default:
2133 FIXME("Unknown Keytype 0x%x\n",key->KeyType);
2135 return ret;
2136 #elif defined(__APPLE__)
2137 dk_dvd_send_key_t dvdsk;
2138 union
2140 DVDChallengeKeyInfo chal;
2141 DVDKey2Info key2;
2142 } key_desc;
2144 switch(key->KeyType)
2146 case DvdChallengeKey:
2147 dvdsk.format = kDVDKeyFormatChallengeKey;
2148 dvdsk.bufferLength = sizeof(key_desc.chal);
2149 dvdsk.buffer = &key_desc.chal;
2150 OSWriteBigInt16(key_desc.chal.dataLength, 0, key->KeyLength);
2151 memcpy(key_desc.chal.challengeKeyValue, key->KeyData, key->KeyLength);
2152 break;
2153 case DvdBusKey2:
2154 dvdsk.format = kDVDKeyFormatKey2;
2155 dvdsk.bufferLength = sizeof(key_desc.key2);
2156 dvdsk.buffer = &key_desc.key2;
2157 OSWriteBigInt16(key_desc.key2.dataLength, 0, key->KeyLength);
2158 memcpy(key_desc.key2.key2Value, key->KeyData, key->KeyLength);
2159 break;
2160 case DvdInvalidateAGID:
2161 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2162 break;
2163 case DvdBusKey1:
2164 case DvdTitleKey:
2165 case DvdAsf:
2166 case DvdGetRpcKey:
2167 case DvdDiskKey:
2168 ERR("attempted to write read-only key type 0x%x\n", key->KeyType);
2169 return STATUS_NOT_SUPPORTED;
2170 case DvdSetRpcKey:
2171 FIXME("DvdSetRpcKey NIY\n");
2172 return STATUS_NOT_SUPPORTED;
2173 default:
2174 FIXME("got unknown key type 0x%x\n", key->KeyType);
2175 return STATUS_NOT_SUPPORTED;
2177 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2178 dvdsk.grantID = (uint8_t)key->SessionId;
2180 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2181 #else
2182 FIXME("not supported on this O/S\n");
2183 return STATUS_NOT_SUPPORTED;
2184 #endif
2187 /******************************************************************
2188 * DVD_ReadKey
2192 static NTSTATUS DVD_ReadKey(int fd, PDVD_COPY_PROTECT_KEY key)
2194 #if defined(linux)
2195 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2196 dvd_struct dvd;
2197 dvd_authinfo auth_info;
2199 memset( &dvd, 0, sizeof( dvd_struct ) );
2200 memset( &auth_info, 0, sizeof( auth_info ) );
2201 switch (key->KeyType)
2203 case DvdDiskKey:
2205 dvd.type = DVD_STRUCT_DISCKEY;
2206 dvd.disckey.agid = (int)key->SessionId;
2207 memset( dvd.disckey.value, 0, DVD_DISCKEY_SIZE );
2209 TRACE("DvdDiskKey\n");
2210 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2211 if (ret == STATUS_SUCCESS)
2212 memcpy(key->KeyData,dvd.disckey.value,DVD_DISCKEY_SIZE);
2213 break;
2214 case DvdTitleKey:
2215 auth_info.type = DVD_LU_SEND_TITLE_KEY;
2216 auth_info.lstk.agid = (int)key->SessionId;
2217 auth_info.lstk.lba = (int)(key->Parameters.TitleOffset.QuadPart>>11);
2218 TRACE("DvdTitleKey session %d Quadpart 0x%08lx offset 0x%08x\n",
2219 (int)key->SessionId, (long)key->Parameters.TitleOffset.QuadPart,
2220 auth_info.lstk.lba);
2221 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2222 if (ret == STATUS_SUCCESS)
2223 memcpy(key->KeyData, auth_info.lstk.title_key, DVD_KEY_SIZE );
2224 break;
2225 case DvdChallengeKey:
2227 auth_info.type = DVD_LU_SEND_CHALLENGE;
2228 auth_info.lsc.agid = (int)key->SessionId;
2230 TRACE("DvdChallengeKey\n");
2231 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2232 if (ret == STATUS_SUCCESS)
2233 memcpy( key->KeyData, auth_info.lsc.chal, DVD_CHALLENGE_SIZE );
2234 break;
2235 case DvdAsf:
2236 auth_info.type = DVD_LU_SEND_ASF;
2237 TRACE("DvdAsf\n");
2238 auth_info.lsasf.asf=((PDVD_ASF)key->KeyData)->SuccessFlag;
2239 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2240 ((PDVD_ASF)key->KeyData)->SuccessFlag = auth_info.lsasf.asf;
2241 break;
2242 case DvdBusKey1:
2243 auth_info.type = DVD_LU_SEND_KEY1;
2244 auth_info.lsk.agid = (int)key->SessionId;
2246 TRACE("DvdBusKey1\n");
2247 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2249 if (ret == STATUS_SUCCESS)
2250 memcpy( key->KeyData, auth_info.lsk.key, DVD_KEY_SIZE );
2251 break;
2252 case DvdGetRpcKey:
2253 auth_info.type = DVD_LU_SEND_RPC_STATE;
2255 TRACE("DvdGetRpcKey\n");
2256 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2258 if (ret == STATUS_SUCCESS)
2260 ((PDVD_RPC_KEY)key->KeyData)->TypeCode = auth_info.lrpcs.type;
2261 ((PDVD_RPC_KEY)key->KeyData)->RegionMask = auth_info.lrpcs.region_mask;
2262 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme = auth_info.lrpcs.rpc_scheme;
2263 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable = auth_info.lrpcs.ucca;
2264 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable = auth_info.lrpcs.vra;
2266 break;
2267 default:
2268 FIXME("Unknown keytype 0x%x\n",key->KeyType);
2270 return ret;
2271 #elif defined(__APPLE__)
2272 union
2274 dk_dvd_report_key_t key;
2275 dk_dvd_read_structure_t disk_key;
2276 } ioc;
2277 union
2279 DVDDiscKeyInfo disk_key;
2280 DVDChallengeKeyInfo chal;
2281 DVDKey1Info key1;
2282 DVDTitleKeyInfo title;
2283 DVDAuthenticationSuccessFlagInfo asf;
2284 DVDRegionPlaybackControlInfo rpc;
2285 } desc;
2286 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2288 switch(key->KeyType)
2290 case DvdChallengeKey:
2291 ioc.key.format = kDVDKeyFormatChallengeKey;
2292 ioc.key.grantID = (uint8_t)key->SessionId;
2293 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2294 ioc.key.bufferLength = sizeof(desc.chal);
2295 ioc.key.buffer = &desc.chal;
2296 OSWriteBigInt16(desc.chal.dataLength, 0, key->KeyLength);
2297 break;
2298 case DvdBusKey1:
2299 ioc.key.format = kDVDKeyFormatKey1;
2300 ioc.key.grantID = (uint8_t)key->SessionId;
2301 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2302 ioc.key.bufferLength = sizeof(desc.key1);
2303 ioc.key.buffer = &desc.key1;
2304 OSWriteBigInt16(desc.key1.dataLength, 0, key->KeyLength);
2305 break;
2306 case DvdTitleKey:
2307 ioc.key.format = kDVDKeyFormatTitleKey;
2308 ioc.key.grantID = (uint8_t)key->SessionId;
2309 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2310 ioc.key.bufferLength = sizeof(desc.title);
2311 ioc.key.buffer = &desc.title;
2312 ioc.key.address = (uint32_t)(key->Parameters.TitleOffset.QuadPart>>11);
2313 OSWriteBigInt16(desc.title.dataLength, 0, key->KeyLength);
2314 break;
2315 case DvdAsf:
2316 ioc.key.format = kDVDKeyFormatASF;
2317 ioc.key.grantID = (uint8_t)key->SessionId;
2318 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2319 ioc.key.bufferLength = sizeof(desc.asf);
2320 ioc.key.buffer = &desc.asf;
2321 OSWriteBigInt16(desc.asf.dataLength, 0, key->KeyLength);
2322 break;
2323 case DvdGetRpcKey:
2324 ioc.key.format = kDVDKeyFormatRegionState;
2325 ioc.key.grantID = (uint8_t)key->SessionId;
2326 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2327 ioc.key.bufferLength = sizeof(desc.rpc);
2328 ioc.key.buffer = &desc.rpc;
2329 OSWriteBigInt16(desc.rpc.dataLength, 0, key->KeyLength);
2330 break;
2331 case DvdDiskKey:
2332 ioc.disk_key.format = kDVDStructureFormatDiscKeyInfo;
2333 ioc.disk_key.grantID = (uint8_t)key->SessionId;
2334 ioc.disk_key.bufferLength = sizeof(desc.disk_key);
2335 ioc.disk_key.buffer = &desc.disk_key;
2336 break;
2337 case DvdInvalidateAGID:
2338 ioc.key.format = kDVDKeyFormatAGID_Invalidate;
2339 ioc.key.grantID = (uint8_t)key->SessionId;
2340 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2341 break;
2342 case DvdBusKey2:
2343 case DvdSetRpcKey:
2344 ERR("attempted to read write-only key type 0x%x\n", key->KeyType);
2345 return STATUS_NOT_SUPPORTED;
2346 default:
2347 FIXME("got unknown key type 0x%x\n", key->KeyType);
2348 return STATUS_NOT_SUPPORTED;
2351 ret = CDROM_GetStatusCode(ioctl(fd, (key->KeyType == DvdDiskKey ? DKIOCDVDREADSTRUCTURE : DKIOCDVDREPORTKEY), &ioc));
2353 if (ret == STATUS_SUCCESS)
2355 switch(key->KeyType)
2357 case DvdChallengeKey:
2358 key->KeyLength = OSReadBigInt16(desc.chal.dataLength, 0);
2359 memcpy(key->KeyData, desc.chal.challengeKeyValue, key->KeyLength);
2360 break;
2361 case DvdBusKey1:
2362 key->KeyLength = OSReadBigInt16(desc.key1.dataLength, 0);
2363 memcpy(key->KeyData, desc.key1.key1Value, key->KeyLength);
2364 break;
2365 case DvdTitleKey:
2366 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2367 memcpy(key->KeyData, desc.title.titleKeyValue, key->KeyLength);
2368 key->KeyFlags = 0;
2369 if (desc.title.CPM)
2371 /*key->KeyFlags |= DVD_COPYRIGHTED;*/
2372 if (desc.title.CP_SEC) key->KeyFlags |= DVD_SECTOR_PROTECTED;
2373 /*else key->KeyFlags |= DVD_SECTOR_NOT_PROTECTED;*/
2374 #if 0
2375 switch (desc.title.CGMS)
2377 case 0:
2378 key->KeyFlags |= DVD_CGMS_COPY_PERMITTED;
2379 break;
2380 case 2:
2381 key->KeyFlags |= DVD_CGMS_COPY_ONCE;
2382 break;
2383 case 3:
2384 key->KeyFlags |= DVD_CGMS_NO_COPY;
2385 break;
2387 #endif
2388 } /*else key->KeyFlags |= DVD_NOT_COPYRIGHTED;*/
2389 break;
2390 case DvdAsf:
2391 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2392 ((PDVD_ASF)key->KeyData)->SuccessFlag = desc.asf.successFlag;
2393 break;
2394 case DvdGetRpcKey:
2395 key->KeyLength = OSReadBigInt16(desc.rpc.dataLength, 0);
2396 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable =
2397 desc.rpc.numberUserResets;
2398 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable =
2399 desc.rpc.numberVendorResets;
2400 ((PDVD_RPC_KEY)key->KeyData)->TypeCode =
2401 desc.rpc.typeCode;
2402 ((PDVD_RPC_KEY)key->KeyData)->RegionMask =
2403 desc.rpc.driveRegion;
2404 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme =
2405 desc.rpc.rpcScheme;
2406 case DvdDiskKey:
2407 key->KeyLength = OSReadBigInt16(desc.disk_key.dataLength, 0);
2408 memcpy(key->KeyData, desc.disk_key.discKeyStructures, key->KeyLength);
2409 break;
2410 case DvdBusKey2:
2411 case DvdSetRpcKey:
2412 case DvdInvalidateAGID:
2413 default:
2414 /* Silence warning */
2418 return ret;
2419 #else
2420 FIXME("not supported on this O/S\n");
2421 return STATUS_NOT_SUPPORTED;
2422 #endif
2425 /******************************************************************
2426 * DVD_GetRegion
2428 * This IOCTL combines information from both IOCTL_DVD_READ_KEY
2429 * with key type DvdGetRpcKey and IOCTL_DVD_READ_STRUCTURE with
2430 * structure type DvdCopyrightInformation into one structure.
2432 static NTSTATUS DVD_GetRegion(int fd, PDVD_REGION region)
2434 #if defined(linux)
2435 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2436 dvd_struct dvd;
2437 dvd_authinfo auth_info;
2439 dvd.type = DVD_STRUCT_COPYRIGHT;
2440 dvd.copyright.layer_num = 0;
2441 auth_info.type = DVD_LU_SEND_RPC_STATE;
2443 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2445 if (ret == STATUS_SUCCESS)
2447 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2449 if (ret == STATUS_SUCCESS)
2451 region->CopySystem = dvd.copyright.cpst;
2452 region->RegionData = dvd.copyright.rmi;
2453 region->SystemRegion = auth_info.lrpcs.region_mask;
2454 region->ResetCount = auth_info.lrpcs.ucca;
2457 return ret;
2458 #elif defined(__APPLE__)
2459 dk_dvd_report_key_t key;
2460 dk_dvd_read_structure_t dvd;
2461 DVDRegionPlaybackControlInfo rpc;
2462 DVDCopyrightInfo copy;
2463 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2465 key.format = kDVDKeyFormatRegionState;
2466 key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2467 key.bufferLength = sizeof(rpc);
2468 key.buffer = &rpc;
2469 dvd.format = kDVDStructureFormatCopyrightInfo;
2470 dvd.bufferLength = sizeof(copy);
2471 dvd.buffer = &copy;
2473 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREPORTKEY, &key ));
2475 if (ret == STATUS_SUCCESS)
2477 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREADSTRUCTURE, &dvd ));
2479 if (ret == STATUS_SUCCESS)
2481 region->CopySystem = copy.copyrightProtectionSystemType;
2482 region->RegionData = copy.regionMask;
2483 region->SystemRegion = rpc.driveRegion;
2484 region->ResetCount = rpc.numberUserResets;
2487 return ret;
2488 #else
2489 FIXME("not supported on this O/S\n");
2490 return STATUS_NOT_SUPPORTED;
2491 #endif
2494 static DWORD DVD_ReadStructureSize(const DVD_READ_STRUCTURE *structure, DWORD size)
2496 if (!structure || size != sizeof(DVD_READ_STRUCTURE))
2497 return 0;
2499 switch (structure->Format)
2501 case DvdPhysicalDescriptor:
2502 return sizeof(DVD_LAYER_DESCRIPTOR);
2503 case DvdCopyrightDescriptor:
2504 return sizeof(DVD_COPYRIGHT_DESCRIPTOR);
2505 case DvdDiskKeyDescriptor:
2506 return sizeof(DVD_DISK_KEY_DESCRIPTOR);
2507 case DvdBCADescriptor:
2508 return sizeof(DVD_BCA_DESCRIPTOR);
2509 case DvdManufacturerDescriptor:
2510 return sizeof(DVD_MANUFACTURER_DESCRIPTOR);
2511 default:
2512 return 0;
2516 /******************************************************************
2517 * DVD_ReadStructure
2521 static NTSTATUS DVD_ReadStructure(int dev, const DVD_READ_STRUCTURE *structure, PDVD_LAYER_DESCRIPTOR layer)
2523 #ifdef DVD_READ_STRUCT
2524 /* dvd_struct is not defined consistently across platforms */
2525 union
2527 struct dvd_physical physical;
2528 struct dvd_copyright copyright;
2529 struct dvd_disckey disckey;
2530 struct dvd_bca bca;
2531 struct dvd_manufact manufact;
2532 } s;
2534 if (structure->BlockByteOffset.QuadPart) FIXME(": BlockByteOffset is not handled\n");
2536 switch (structure->Format)
2538 case DvdPhysicalDescriptor:
2539 s.physical.type = DVD_STRUCT_PHYSICAL;
2540 s.physical.layer_num = structure->LayerNumber;
2541 break;
2543 case DvdCopyrightDescriptor:
2544 s.copyright.type = DVD_STRUCT_COPYRIGHT;
2545 s.copyright.layer_num = structure->LayerNumber;
2546 break;
2548 case DvdDiskKeyDescriptor:
2549 s.disckey.type = DVD_STRUCT_DISCKEY;
2550 s.disckey.agid = structure->SessionId;
2551 break;
2553 case DvdBCADescriptor:
2554 s.bca.type = DVD_STRUCT_BCA;
2555 break;
2557 case DvdManufacturerDescriptor:
2558 s.manufact.type = DVD_STRUCT_MANUFACT;
2559 s.manufact.layer_num = structure->LayerNumber;
2560 break;
2562 case DvdMaxDescriptor: /* This is not a real request, no matter what MSDN says! */
2563 default:
2564 return STATUS_INVALID_PARAMETER;
2567 if (ioctl(dev, DVD_READ_STRUCT, &s) < 0)
2568 return STATUS_INVALID_PARAMETER;
2570 switch (structure->Format)
2572 case DvdPhysicalDescriptor:
2574 internal_dvd_layer_descriptor *p = (internal_dvd_layer_descriptor *) layer;
2575 struct dvd_layer *l = &s.physical.layer[s.physical.layer_num];
2577 p->Header.Length = 0x0802;
2578 p->Header.Reserved[0] = 0;
2579 p->Header.Reserved[1] = 0;
2580 p->Descriptor.BookVersion = l->book_version;
2581 p->Descriptor.BookType = l->book_type;
2582 p->Descriptor.MinimumRate = l->min_rate;
2583 p->Descriptor.DiskSize = l->disc_size;
2584 p->Descriptor.LayerType = l->layer_type;
2585 p->Descriptor.TrackPath = l->track_path;
2586 p->Descriptor.NumberOfLayers = l->nlayers;
2587 p->Descriptor.Reserved1 = 0;
2588 p->Descriptor.TrackDensity = l->track_density;
2589 p->Descriptor.LinearDensity = l->linear_density;
2590 p->Descriptor.StartingDataSector = GET_BE_DWORD(l->start_sector);
2591 p->Descriptor.EndDataSector = GET_BE_DWORD(l->end_sector);
2592 p->Descriptor.EndLayerZeroSector = GET_BE_DWORD(l->end_sector_l0);
2593 p->Descriptor.Reserved5 = 0;
2594 p->Descriptor.BCAFlag = l->bca;
2596 break;
2598 case DvdCopyrightDescriptor:
2600 PDVD_COPYRIGHT_DESCRIPTOR p = (PDVD_COPYRIGHT_DESCRIPTOR) layer;
2602 p->CopyrightProtectionType = s.copyright.cpst;
2603 p->RegionManagementInformation = s.copyright.rmi;
2604 p->Reserved = 0;
2606 break;
2608 case DvdDiskKeyDescriptor:
2610 PDVD_DISK_KEY_DESCRIPTOR p = (PDVD_DISK_KEY_DESCRIPTOR) layer;
2612 memcpy(p->DiskKeyData, s.disckey.value, 2048);
2614 break;
2616 case DvdBCADescriptor:
2618 PDVD_BCA_DESCRIPTOR p = (PDVD_BCA_DESCRIPTOR) layer;
2620 memcpy(p->BCAInformation, s.bca.value, s.bca.len);
2622 break;
2624 case DvdManufacturerDescriptor:
2626 internal_dvd_manufacturer_descriptor *p = (internal_dvd_manufacturer_descriptor*) layer;
2628 p->Header.Length = 0x0802;
2629 p->Header.Reserved[0] = 0;
2630 p->Header.Reserved[1] = 0;
2631 memcpy(p->Descriptor.ManufacturingInformation, s.manufact.value, 2048);
2633 break;
2635 case DvdMaxDescriptor: /* Suppress warning */
2636 break;
2638 #elif defined(__APPLE__)
2639 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2640 dk_dvd_read_structure_t dvdrs;
2641 union
2643 DVDPhysicalFormatInfo phys;
2644 DVDCopyrightInfo copy;
2645 DVDDiscKeyInfo disk_key;
2646 DVDManufacturingInfo manf;
2647 } desc;
2648 union
2650 PDVD_LAYER_DESCRIPTOR layer;
2651 internal_dvd_layer_descriptor *xlayer;
2652 PDVD_COPYRIGHT_DESCRIPTOR copy;
2653 PDVD_DISK_KEY_DESCRIPTOR disk_key;
2654 internal_dvd_manufacturer_descriptor *manf;
2655 } nt_desc;
2657 nt_desc.layer = layer;
2658 RtlZeroMemory(&dvdrs, sizeof(dvdrs));
2659 dvdrs.address = (uint32_t)(structure->BlockByteOffset.QuadPart>>11);
2660 dvdrs.grantID = (uint8_t)structure->SessionId;
2661 dvdrs.layer = structure->LayerNumber;
2662 switch(structure->Format)
2664 case DvdPhysicalDescriptor:
2665 dvdrs.format = kDVDStructureFormatPhysicalFormatInfo;
2666 dvdrs.bufferLength = sizeof(desc.phys);
2667 dvdrs.buffer = &desc.phys;
2668 break;
2670 case DvdCopyrightDescriptor:
2671 dvdrs.format = kDVDStructureFormatCopyrightInfo;
2672 dvdrs.bufferLength = sizeof(desc.copy);
2673 dvdrs.buffer = &desc.copy;
2674 break;
2676 case DvdDiskKeyDescriptor:
2677 dvdrs.format = kDVDStructureFormatDiscKeyInfo;
2678 dvdrs.bufferLength = sizeof(desc.disk_key);
2679 dvdrs.buffer = &desc.disk_key;
2680 break;
2682 case DvdBCADescriptor:
2683 FIXME("DvdBCADescriptor NIY\n");
2684 return STATUS_NOT_SUPPORTED;
2686 case DvdManufacturerDescriptor:
2687 dvdrs.format = kDVDStructureFormatManufacturingInfo;
2688 dvdrs.bufferLength = sizeof(desc.manf);
2689 dvdrs.buffer = &desc.manf;
2690 break;
2692 case DvdMaxDescriptor:
2693 default:
2694 FIXME("got unknown structure type 0x%x\n", structure->Format);
2695 return STATUS_NOT_SUPPORTED;
2697 ret = CDROM_GetStatusCode(ioctl(dev, DKIOCDVDREADSTRUCTURE, &dvdrs));
2698 if(ret == STATUS_SUCCESS)
2700 switch(structure->Format)
2702 case DvdPhysicalDescriptor:
2703 nt_desc.xlayer->Header.Length = 0x0802;
2704 nt_desc.xlayer->Header.Reserved[0] = 0;
2705 nt_desc.xlayer->Header.Reserved[1] = 0;
2706 nt_desc.xlayer->Descriptor.BookVersion = desc.phys.partVersion;
2707 nt_desc.xlayer->Descriptor.BookType = desc.phys.bookType;
2708 nt_desc.xlayer->Descriptor.MinimumRate = desc.phys.minimumRate;
2709 nt_desc.xlayer->Descriptor.DiskSize = desc.phys.discSize;
2710 nt_desc.xlayer->Descriptor.LayerType = desc.phys.layerType;
2711 nt_desc.xlayer->Descriptor.TrackPath = desc.phys.trackPath;
2712 nt_desc.xlayer->Descriptor.NumberOfLayers = desc.phys.numberOfLayers;
2713 nt_desc.xlayer->Descriptor.Reserved1 = 0;
2714 nt_desc.xlayer->Descriptor.TrackDensity = desc.phys.trackDensity;
2715 nt_desc.xlayer->Descriptor.LinearDensity = desc.phys.linearDensity;
2716 nt_desc.xlayer->Descriptor.StartingDataSector = GET_BE_DWORD(*(DWORD *)&desc.phys.zero1);
2717 nt_desc.xlayer->Descriptor.EndDataSector = GET_BE_DWORD(*(DWORD *)&desc.phys.zero2);
2718 nt_desc.xlayer->Descriptor.EndLayerZeroSector = GET_BE_DWORD(*(DWORD *)&desc.phys.zero3);
2719 nt_desc.xlayer->Descriptor.Reserved5 = 0;
2720 nt_desc.xlayer->Descriptor.BCAFlag = desc.phys.bcaFlag;
2721 break;
2723 case DvdCopyrightDescriptor:
2724 nt_desc.copy->CopyrightProtectionType =
2725 desc.copy.copyrightProtectionSystemType;
2726 nt_desc.copy->RegionManagementInformation =
2727 desc.copy.regionMask;
2728 nt_desc.copy->Reserved = 0;
2729 break;
2731 case DvdDiskKeyDescriptor:
2732 memcpy(
2733 nt_desc.disk_key->DiskKeyData,
2734 desc.disk_key.discKeyStructures,
2735 2048);
2736 break;
2738 case DvdManufacturerDescriptor:
2739 nt_desc.manf->Header.Length = 0x0802;
2740 nt_desc.manf->Header.Reserved[0] = 0;
2741 nt_desc.manf->Header.Reserved[1] = 0;
2742 memcpy(
2743 nt_desc.manf->Descriptor.ManufacturingInformation,
2744 desc.manf.discManufacturingInfo,
2745 2048);
2746 break;
2748 case DvdBCADescriptor:
2749 case DvdMaxDescriptor:
2750 default:
2751 /* Silence warning */
2752 break;
2755 return ret;
2756 #else
2757 FIXME("\n");
2758 #endif
2759 return STATUS_SUCCESS;
2763 /******************************************************************
2764 * GetInquiryData
2765 * Implements the IOCTL_GET_INQUIRY_DATA ioctl.
2766 * Returns Inquiry data for all devices on the specified scsi bus
2767 * Returns STATUS_BUFFER_TOO_SMALL if the output buffer is too small,
2768 * STATUS_INVALID_DEVICE_REQUEST if the given handle isn't to a SCSI device,
2769 * or STATUS_NOT_SUPPORTED if the OS driver is too old
2771 static NTSTATUS GetInquiryData(int fd, PSCSI_ADAPTER_BUS_INFO BufferOut, DWORD OutBufferSize)
2773 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
2774 PSCSI_INQUIRY_DATA pInquiryData = NULL;
2775 UCHAR sense_buffer[32];
2776 int iochk, version;
2777 sg_io_hdr_t iocmd;
2778 UCHAR inquiry[INQ_CMD_LEN] = {INQUIRY, 0, 0, 0, INQ_REPLY_LEN, 0};
2780 /* Check we have a SCSI device and a supported driver */
2781 if(ioctl(fd, SG_GET_VERSION_NUM, &version) != 0)
2783 WARN("IOCTL_SCSI_GET_INQUIRY_DATA sg driver is not loaded\n");
2784 return STATUS_INVALID_DEVICE_REQUEST;
2786 if(version < 30000 )
2787 return STATUS_NOT_SUPPORTED;
2789 /* FIXME: Enumerate devices on the bus */
2790 BufferOut->NumberOfBuses = 1;
2791 BufferOut->BusData[0].NumberOfLogicalUnits = 1;
2792 BufferOut->BusData[0].InquiryDataOffset = sizeof(SCSI_ADAPTER_BUS_INFO);
2794 pInquiryData = (PSCSI_INQUIRY_DATA)(BufferOut + 1);
2796 RtlZeroMemory(&iocmd, sizeof(iocmd));
2797 iocmd.interface_id = 'S';
2798 iocmd.cmd_len = sizeof(inquiry);
2799 iocmd.mx_sb_len = sizeof(sense_buffer);
2800 iocmd.dxfer_direction = SG_DXFER_FROM_DEV;
2801 iocmd.dxfer_len = INQ_REPLY_LEN;
2802 iocmd.dxferp = pInquiryData->InquiryData;
2803 iocmd.cmdp = inquiry;
2804 iocmd.sbp = sense_buffer;
2805 iocmd.timeout = 1000;
2807 iochk = ioctl(fd, SG_IO, &iocmd);
2808 if(iochk != 0)
2809 WARN("ioctl SG_IO returned %d, error (%s)\n", iochk, strerror(errno));
2811 CDROM_GetInterfaceInfo(fd, &BufferOut->BusData[0].InitiatorBusId, &pInquiryData->PathId, &pInquiryData->TargetId, &pInquiryData->Lun);
2812 pInquiryData->DeviceClaimed = TRUE;
2813 pInquiryData->InquiryDataLength = INQ_REPLY_LEN;
2814 pInquiryData->NextInquiryDataOffset = 0;
2815 return STATUS_SUCCESS;
2816 #else
2817 FIXME("not supported on this O/S\n");
2818 return STATUS_NOT_SUPPORTED;
2819 #endif
2822 /******************************************************************
2823 * CDROM_DeviceIoControl
2827 NTSTATUS CDROM_DeviceIoControl(HANDLE hDevice,
2828 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
2829 PVOID UserApcContext,
2830 PIO_STATUS_BLOCK piosb,
2831 ULONG dwIoControlCode,
2832 LPVOID lpInBuffer, DWORD nInBufferSize,
2833 LPVOID lpOutBuffer, DWORD nOutBufferSize)
2835 DWORD sz = 0;
2836 NTSTATUS status = STATUS_SUCCESS;
2837 int fd, needs_close, dev;
2839 TRACE("%p %s %p %d %p %d %p\n",
2840 hDevice, iocodex(dwIoControlCode), lpInBuffer, nInBufferSize,
2841 lpOutBuffer, nOutBufferSize, piosb);
2843 piosb->Information = 0;
2845 if ((status = server_get_unix_fd( hDevice, 0, &fd, &needs_close, NULL, NULL )))
2847 if (status == STATUS_BAD_DEVICE_TYPE) return status; /* no associated fd */
2848 goto error;
2851 if ((status = CDROM_Open(fd, &dev)))
2853 if (needs_close) close( fd );
2854 goto error;
2857 #ifdef __APPLE__
2859 char name[100];
2861 /* This is ugly as hell, but Mac OS is unable to do anything from the
2862 * partition fd, it wants an fd for the whole device, and it sometimes
2863 * also requires the device fd to be closed first, so we have to close
2864 * the handle that the caller gave us.
2865 * Also for some reason it wants the fd to be closed before we even
2866 * open the parent if we're trying to eject the disk.
2868 if ((status = get_parent_device( fd, name, sizeof(name) ))) goto error;
2869 if (dwIoControlCode == IOCTL_STORAGE_EJECT_MEDIA)
2870 NtClose( hDevice );
2871 if (needs_close) close( fd );
2872 TRACE("opening parent %s\n", name );
2873 if ((fd = open( name, O_RDONLY )) == -1)
2875 status = FILE_GetNtStatus();
2876 goto error;
2878 needs_close = 1;
2880 #endif
2882 switch (dwIoControlCode)
2884 case IOCTL_CDROM_CHECK_VERIFY:
2885 case IOCTL_DISK_CHECK_VERIFY:
2886 case IOCTL_STORAGE_CHECK_VERIFY:
2887 case IOCTL_STORAGE_CHECK_VERIFY2:
2888 sz = 0;
2889 CDROM_ClearCacheEntry(dev);
2890 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2891 status = STATUS_INVALID_PARAMETER;
2892 else status = CDROM_Verify(dev, fd);
2893 break;
2895 /* EPP case IOCTL_STORAGE_CHECK_VERIFY2: */
2897 /* EPP case IOCTL_STORAGE_FIND_NEW_DEVICES: */
2898 /* EPP case IOCTL_CDROM_FIND_NEW_DEVICES: */
2900 case IOCTL_STORAGE_LOAD_MEDIA:
2901 case IOCTL_CDROM_LOAD_MEDIA:
2902 sz = 0;
2903 CDROM_ClearCacheEntry(dev);
2904 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2905 status = STATUS_INVALID_PARAMETER;
2906 else status = CDROM_SetTray(fd, FALSE);
2907 break;
2908 case IOCTL_STORAGE_EJECT_MEDIA:
2909 sz = 0;
2910 CDROM_ClearCacheEntry(dev);
2911 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2912 status = STATUS_INVALID_PARAMETER;
2913 else
2914 status = CDROM_SetTray(fd, TRUE);
2915 break;
2917 case IOCTL_CDROM_MEDIA_REMOVAL:
2918 case IOCTL_DISK_MEDIA_REMOVAL:
2919 case IOCTL_STORAGE_MEDIA_REMOVAL:
2920 case IOCTL_STORAGE_EJECTION_CONTROL:
2921 /* FIXME the last ioctl:s is not the same as the two others...
2922 * lockcount/owner should be handled */
2923 sz = 0;
2924 CDROM_ClearCacheEntry(dev);
2925 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2926 else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) status = STATUS_BUFFER_TOO_SMALL;
2927 else status = CDROM_ControlEjection(fd, lpInBuffer);
2928 break;
2930 case IOCTL_DISK_GET_MEDIA_TYPES:
2931 case IOCTL_STORAGE_GET_MEDIA_TYPES:
2932 case IOCTL_STORAGE_GET_MEDIA_TYPES_EX:
2933 sz = sizeof(GET_MEDIA_TYPES);
2934 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2935 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2936 else status = CDROM_GetMediaType(dev, lpOutBuffer);
2937 break;
2939 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
2940 sz = sizeof(STORAGE_DEVICE_NUMBER);
2941 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2942 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2943 else status = CDROM_GetDeviceNumber(dev, lpOutBuffer);
2944 break;
2946 case IOCTL_STORAGE_RESET_DEVICE:
2947 sz = 0;
2948 CDROM_ClearCacheEntry(dev);
2949 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2950 status = STATUS_INVALID_PARAMETER;
2951 else status = CDROM_ResetAudio(fd);
2952 break;
2954 case IOCTL_CDROM_GET_CONTROL:
2955 sz = sizeof(CDROM_AUDIO_CONTROL);
2956 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2957 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2958 else status = CDROM_GetControl(dev, fd, lpOutBuffer);
2959 break;
2961 case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
2962 sz = sizeof(DISK_GEOMETRY);
2963 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2964 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2965 else status = CDROM_GetDriveGeometry(dev, fd, lpOutBuffer);
2966 break;
2968 case IOCTL_CDROM_DISK_TYPE:
2969 sz = sizeof(CDROM_DISK_DATA);
2970 /* CDROM_ClearCacheEntry(dev); */
2971 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2972 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2973 else status = CDROM_GetDiskData(dev, fd, lpOutBuffer);
2974 break;
2976 /* EPP case IOCTL_CDROM_GET_LAST_SESSION: */
2978 case IOCTL_CDROM_READ_Q_CHANNEL:
2979 sz = sizeof(SUB_Q_CHANNEL_DATA);
2980 if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT))
2981 status = STATUS_INVALID_PARAMETER;
2982 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2983 else status = CDROM_ReadQChannel(dev, fd, lpInBuffer, lpOutBuffer);
2984 break;
2986 case IOCTL_CDROM_READ_TOC:
2987 sz = sizeof(CDROM_TOC);
2988 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2989 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2990 else status = CDROM_ReadTOC(dev, fd, lpOutBuffer);
2991 break;
2993 /* EPP case IOCTL_CDROM_READ_TOC_EX: */
2995 case IOCTL_CDROM_PAUSE_AUDIO:
2996 sz = 0;
2997 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2998 status = STATUS_INVALID_PARAMETER;
2999 else status = CDROM_PauseAudio(fd);
3000 break;
3001 case IOCTL_CDROM_PLAY_AUDIO_MSF:
3002 sz = 0;
3003 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3004 else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
3005 else status = CDROM_PlayAudioMSF(fd, lpInBuffer);
3006 break;
3007 case IOCTL_CDROM_RESUME_AUDIO:
3008 sz = 0;
3009 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3010 status = STATUS_INVALID_PARAMETER;
3011 else status = CDROM_ResumeAudio(fd);
3012 break;
3013 case IOCTL_CDROM_SEEK_AUDIO_MSF:
3014 sz = 0;
3015 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3016 else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
3017 else status = CDROM_SeekAudioMSF(dev, fd, lpInBuffer);
3018 break;
3019 case IOCTL_CDROM_STOP_AUDIO:
3020 sz = 0;
3021 CDROM_ClearCacheEntry(dev); /* Maybe intention is to change media */
3022 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3023 status = STATUS_INVALID_PARAMETER;
3024 else status = CDROM_StopAudio(fd);
3025 break;
3026 case IOCTL_CDROM_GET_VOLUME:
3027 sz = sizeof(VOLUME_CONTROL);
3028 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3029 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3030 else status = CDROM_GetVolume(fd, lpOutBuffer);
3031 break;
3032 case IOCTL_CDROM_SET_VOLUME:
3033 sz = 0;
3034 CDROM_ClearCacheEntry(dev);
3035 if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
3036 status = STATUS_INVALID_PARAMETER;
3037 else status = CDROM_SetVolume(fd, lpInBuffer);
3038 break;
3039 case IOCTL_CDROM_RAW_READ:
3040 sz = 0;
3041 if (nInBufferSize < sizeof(RAW_READ_INFO)) status = STATUS_INVALID_PARAMETER;
3042 else if (lpOutBuffer == NULL) status = STATUS_BUFFER_TOO_SMALL;
3043 else status = CDROM_RawRead(fd, lpInBuffer, lpOutBuffer,
3044 nOutBufferSize, &sz);
3045 break;
3046 case IOCTL_SCSI_GET_ADDRESS:
3047 sz = sizeof(SCSI_ADDRESS);
3048 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3049 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3050 else status = CDROM_GetAddress(fd, lpOutBuffer);
3051 break;
3052 case IOCTL_SCSI_PASS_THROUGH_DIRECT:
3053 sz = sizeof(SCSI_PASS_THROUGH_DIRECT);
3054 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3055 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH_DIRECT)) status = STATUS_BUFFER_TOO_SMALL;
3056 else status = CDROM_ScsiPassThroughDirect(fd, lpOutBuffer);
3057 break;
3058 case IOCTL_SCSI_PASS_THROUGH:
3059 sz = sizeof(SCSI_PASS_THROUGH);
3060 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3061 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH)) status = STATUS_BUFFER_TOO_SMALL;
3062 else status = CDROM_ScsiPassThrough(fd, lpOutBuffer);
3063 break;
3064 case IOCTL_SCSI_GET_CAPABILITIES:
3065 sz = sizeof(IO_SCSI_CAPABILITIES);
3066 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3067 else if (nOutBufferSize < sizeof(IO_SCSI_CAPABILITIES)) status = STATUS_BUFFER_TOO_SMALL;
3068 else status = CDROM_ScsiGetCaps(fd, lpOutBuffer);
3069 break;
3070 case IOCTL_DVD_START_SESSION:
3071 sz = sizeof(DVD_SESSION_ID);
3072 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3073 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3074 else
3076 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3077 *(PDVD_SESSION_ID)lpOutBuffer);
3078 status = DVD_StartSession(fd, lpInBuffer, lpOutBuffer);
3079 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3080 *(PDVD_SESSION_ID)lpOutBuffer);
3082 break;
3083 case IOCTL_DVD_END_SESSION:
3084 sz = sizeof(DVD_SESSION_ID);
3085 if ((lpInBuffer == NULL) || (nInBufferSize < sz))status = STATUS_INVALID_PARAMETER;
3086 else status = DVD_EndSession(fd, lpInBuffer);
3087 break;
3088 case IOCTL_DVD_SEND_KEY:
3089 sz = 0;
3090 if (!lpInBuffer ||
3091 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3092 status = STATUS_INVALID_PARAMETER;
3093 else
3095 TRACE("doing DVD_SendKey\n");
3096 status = DVD_SendKey(fd, lpInBuffer);
3098 break;
3099 case IOCTL_DVD_READ_KEY:
3100 if (!lpInBuffer ||
3101 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3102 status = STATUS_INVALID_PARAMETER;
3103 else if (lpInBuffer !=lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
3104 else
3106 TRACE("doing DVD_READ_KEY\n");
3107 sz = ((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength;
3108 status = DVD_ReadKey(fd, lpInBuffer);
3110 break;
3111 case IOCTL_DVD_GET_REGION:
3112 sz = sizeof(DVD_REGION);
3113 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3114 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3115 else
3117 TRACE("doing DVD_Get_REGION\n");
3118 status = DVD_GetRegion(fd, lpOutBuffer);
3120 break;
3121 case IOCTL_DVD_READ_STRUCTURE:
3122 sz = DVD_ReadStructureSize(lpInBuffer, nInBufferSize);
3123 if (lpInBuffer == NULL || nInBufferSize != sizeof(DVD_READ_STRUCTURE)) status = STATUS_INVALID_PARAMETER;
3124 else if (nOutBufferSize < sz || !lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
3125 else
3127 TRACE("doing DVD_READ_STRUCTURE\n");
3128 status = DVD_ReadStructure(fd, lpInBuffer, lpOutBuffer);
3130 break;
3132 case IOCTL_SCSI_GET_INQUIRY_DATA:
3133 sz = INQ_REPLY_LEN;
3134 status = GetInquiryData(fd, lpOutBuffer, nOutBufferSize);
3135 break;
3137 default:
3138 if (needs_close) close( fd );
3139 return STATUS_NOT_SUPPORTED;
3141 if (needs_close) close( fd );
3142 error:
3143 piosb->u.Status = status;
3144 piosb->Information = sz;
3145 if (hEvent) NtSetEvent(hEvent, NULL);
3146 return status;