windowscodecs: Fix ICO palette size calculation.
[wine/multimedia.git] / dlls / ntdll / cdrom.c
blob0737e6ec5a69e8c246d8ba8dda69e3207ac29a81
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /* Main file for CD-ROM support
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1999, 2001, 2003 Eric Pouech
6 * Copyright 2000 Andreas Mohr
7 * Copyright 2005 Ivan Leo Puoti
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <errno.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_IO_H
32 # include <io.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <fcntl.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
40 #endif
41 #include <sys/types.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 #include <sys/ioctl.h>
45 #endif
46 #ifdef HAVE_SCSI_SG_H
47 # include <scsi/sg.h>
48 #endif
49 #ifdef HAVE_SCSI_SCSI_H
50 # include <scsi/scsi.h>
51 # undef REASSIGN_BLOCKS /* avoid conflict with winioctl.h */
52 # undef FAILED /* avoid conflict with winerror.h */
53 #endif
54 #ifdef HAVE_SCSI_SCSI_IOCTL_H
55 # include <scsi/scsi_ioctl.h>
56 #endif
57 #ifdef HAVE_LINUX_MAJOR_H
58 # include <linux/major.h>
59 #endif
60 #ifdef HAVE_LINUX_HDREG_H
61 # include <linux/hdreg.h>
62 #endif
63 #ifdef HAVE_LINUX_PARAM_H
64 # include <linux/param.h>
65 #endif
66 #ifdef HAVE_LINUX_CDROM_H
67 # include <linux/cdrom.h>
68 #endif
69 #ifdef HAVE_LINUX_UCDROM_H
70 # include <linux/ucdrom.h>
71 #endif
72 #ifdef HAVE_SYS_CDIO_H
73 # include <sys/cdio.h>
74 #endif
75 #ifdef HAVE_SYS_SCSIIO_H
76 # include <sys/scsiio.h>
77 #endif
79 #ifdef HAVE_IOKIT_IOKITLIB_H
80 # include <libkern/OSByteOrder.h>
81 # include <sys/disk.h>
82 # include <IOKit/IOKitLib.h>
83 # include <IOKit/storage/IOMedia.h>
84 # include <IOKit/storage/IOCDMediaBSDClient.h>
85 # include <IOKit/storage/IODVDMediaBSDClient.h>
86 # include <IOKit/scsi/SCSITask.h>
87 # include <IOKit/scsi/SCSICmds_REQUEST_SENSE_Defs.h>
88 # define SENSEBUFLEN kSenseDefaultSize
90 typedef struct
92 uint32_t attribute;
93 uint32_t timeout;
94 uint32_t response;
95 uint32_t status;
96 uint8_t direction;
97 uint8_t cdbSize;
98 uint8_t reserved0144[2];
99 uint8_t cdb[16];
100 void* buffer;
101 uint64_t bufferSize;
102 void* sense;
103 uint64_t senseLen;
104 } dk_scsi_command_t;
106 #define DKIOCSCSICOMMAND _IOWR('d', 253, dk_scsi_command_t)
108 #endif
110 #define NONAMELESSUNION
111 #define NONAMELESSSTRUCT
112 #include "ntstatus.h"
113 #define WIN32_NO_STATUS
114 #include "windef.h"
115 #include "winternl.h"
116 #include "winioctl.h"
117 #include "ntddstor.h"
118 #include "ntddcdrm.h"
119 #include "ddk/ntddcdvd.h"
120 #include "ntddscsi.h"
121 #include "ntdll_misc.h"
122 #include "wine/server.h"
123 #include "wine/library.h"
124 #include "wine/debug.h"
126 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
128 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
129 lack the following constants. */
131 #ifndef CD_SECS
132 # define CD_SECS 60 /* seconds per minute */
133 #endif
134 #ifndef CD_FRAMES
135 # define CD_FRAMES 75 /* frames per second */
136 #endif
138 static const struct iocodexs
140 DWORD code;
141 const char *codex;
142 } iocodextable[] = {
143 #define X(x) { x, #x },
144 X(IOCTL_CDROM_CHECK_VERIFY)
145 X(IOCTL_CDROM_CURRENT_POSITION)
146 X(IOCTL_CDROM_DISK_TYPE)
147 X(IOCTL_CDROM_GET_CONTROL)
148 X(IOCTL_CDROM_GET_DRIVE_GEOMETRY)
149 X(IOCTL_CDROM_GET_VOLUME)
150 X(IOCTL_CDROM_LOAD_MEDIA)
151 X(IOCTL_CDROM_MEDIA_CATALOG)
152 X(IOCTL_CDROM_MEDIA_REMOVAL)
153 X(IOCTL_CDROM_PAUSE_AUDIO)
154 X(IOCTL_CDROM_PLAY_AUDIO_MSF)
155 X(IOCTL_CDROM_RAW_READ)
156 X(IOCTL_CDROM_READ_Q_CHANNEL)
157 X(IOCTL_CDROM_READ_TOC)
158 X(IOCTL_CDROM_RESUME_AUDIO)
159 X(IOCTL_CDROM_SEEK_AUDIO_MSF)
160 X(IOCTL_CDROM_SET_VOLUME)
161 X(IOCTL_CDROM_STOP_AUDIO)
162 X(IOCTL_CDROM_TRACK_ISRC)
163 X(IOCTL_DISK_MEDIA_REMOVAL)
164 X(IOCTL_DVD_END_SESSION)
165 X(IOCTL_DVD_GET_REGION)
166 X(IOCTL_DVD_READ_KEY)
167 X(IOCTL_DVD_READ_STRUCTURE)
168 X(IOCTL_DVD_SEND_KEY)
169 X(IOCTL_DVD_START_SESSION)
170 X(IOCTL_SCSI_GET_ADDRESS)
171 X(IOCTL_SCSI_GET_CAPABILITIES)
172 X(IOCTL_SCSI_GET_INQUIRY_DATA)
173 X(IOCTL_SCSI_PASS_THROUGH)
174 X(IOCTL_SCSI_PASS_THROUGH_DIRECT)
175 X(IOCTL_STORAGE_CHECK_VERIFY)
176 X(IOCTL_STORAGE_EJECTION_CONTROL)
177 X(IOCTL_STORAGE_EJECT_MEDIA)
178 X(IOCTL_STORAGE_GET_DEVICE_NUMBER)
179 X(IOCTL_STORAGE_LOAD_MEDIA)
180 X(IOCTL_STORAGE_MEDIA_REMOVAL)
181 X(IOCTL_STORAGE_RESET_DEVICE)
182 #undef X
184 static const char *iocodex(DWORD code)
186 unsigned int i;
187 static char buffer[25];
188 for(i=0; i<sizeof(iocodextable)/sizeof(struct iocodexs); i++)
189 if (code==iocodextable[i].code)
190 return iocodextable[i].codex;
191 sprintf(buffer, "IOCTL_CODE_%x", (int)code);
192 return buffer;
195 #define INQ_REPLY_LEN 36
196 #define INQ_CMD_LEN 6
198 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
199 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
200 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
201 #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;}
203 /* The documented format of DVD_LAYER_DESCRIPTOR is wrong. Even the format in the
204 * DDK's header is wrong. There are four bytes at the start defined by
205 * MMC-5. The first two are the size of the structure in big-endian order as
206 * defined by MMC-5. The other two are reserved.
208 typedef struct
210 UCHAR DataLength[2];
211 UCHAR Reserved0[2];
212 UCHAR BookVersion : 4;
213 UCHAR BookType : 4;
214 UCHAR MinimumRate : 4;
215 UCHAR DiskSize : 4;
216 UCHAR LayerType : 4;
217 UCHAR TrackPath : 1;
218 UCHAR NumberOfLayers : 2;
219 UCHAR Reserved1 : 1;
220 UCHAR TrackDensity : 4;
221 UCHAR LinearDensity : 4;
222 ULONG StartingDataSector;
223 ULONG EndDataSector;
224 ULONG EndLayerZeroSector;
225 UCHAR Reserved5 : 7;
226 UCHAR BCAFlag : 1;
227 UCHAR Reserved6;
228 } internal_dvd_layer_descriptor;
231 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
232 static NTSTATUS CDROM_GetStatusCode(int);
235 #ifdef linux
237 # ifndef IDE6_MAJOR
238 # define IDE6_MAJOR 88
239 # endif
240 # ifndef IDE7_MAJOR
241 # define IDE7_MAJOR 89
242 # endif
244 # ifdef CDROM_SEND_PACKET
245 /* structure for CDROM_PACKET_COMMAND ioctl */
246 /* not all Linux versions have all the fields, so we define the
247 * structure ourselves to make sure */
248 struct linux_cdrom_generic_command
250 unsigned char cmd[CDROM_PACKET_SIZE];
251 unsigned char *buffer;
252 unsigned int buflen;
253 int stat;
254 struct request_sense *sense;
255 unsigned char data_direction;
256 int quiet;
257 int timeout;
258 void *reserved[1];
260 # endif /* CDROM_SEND_PACKET */
262 #endif /* linux */
264 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
265 * this should be removed when a proper device interface is implemented
267 * (WS) We need this to keep track of current position and to safely
268 * detect media changes. Besides this should provide a great speed up
269 * for toc inquiries.
271 struct cdrom_cache {
272 dev_t device;
273 ino_t inode;
274 char toc_good; /* if false, will reread TOC from disk */
275 CDROM_TOC toc;
276 SUB_Q_CURRENT_POSITION CurrentPosition;
278 /* who has more than 5 cdroms on his/her machine ?? */
279 /* FIXME: this should grow depending on the number of cdroms we install/configure
280 * at startup
282 #define MAX_CACHE_ENTRIES 5
283 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
285 static RTL_CRITICAL_SECTION cache_section;
286 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
288 0, 0, &cache_section,
289 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
290 0, 0, { (DWORD_PTR)(__FILE__ ": cache_section") }
292 static RTL_CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
294 /* Proposed media change function: not really needed at this time */
295 /* This is a 1 or 0 type of function */
296 #if 0
297 static int CDROM_MediaChanged(int dev)
299 int i;
301 struct cdrom_tochdr hdr;
302 struct cdrom_tocentry entry;
304 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
305 return 0;
306 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
307 return 0;
309 if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
310 return 1;
312 for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
314 if (i == hdr.cdth_trk1 + 1)
316 entry.cdte_track = CDROM_LEADOUT;
317 } else {
318 entry.cdte_track = i;
320 entry.cdte_format = CDROM_MSF;
321 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
322 return 0;
323 if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
324 sizeof(struct cdrom_tocentry)) )
325 return 1;
327 return 0;
329 #endif
332 /******************************************************************
333 * get_parent_device
335 * On Mac OS, get the device for the whole disk from a fd that points to a partition.
336 * This is ugly and inefficient, but we have no choice since the partition fd doesn't
337 * support the eject ioctl.
339 #ifdef __APPLE__
340 static NTSTATUS get_parent_device( int fd, char *name, size_t len )
342 NTSTATUS status = STATUS_NO_SUCH_FILE;
343 struct stat st;
344 int i;
345 io_service_t service;
346 CFMutableDictionaryRef dict;
347 CFTypeRef val;
349 if (fstat( fd, &st ) == -1) return FILE_GetNtStatus();
350 if (!S_ISCHR( st.st_mode )) return STATUS_OBJECT_TYPE_MISMATCH;
352 /* create a dictionary with the right major/minor numbers */
354 if (!(dict = IOServiceMatching( kIOMediaClass ))) return STATUS_NO_MEMORY;
356 i = major( st.st_rdev );
357 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
358 CFDictionaryAddValue( dict, CFSTR( "BSD Major" ), val );
359 CFRelease( val );
361 i = minor( st.st_rdev );
362 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
363 CFDictionaryAddValue( dict, CFSTR( "BSD Minor" ), val );
364 CFRelease( val );
366 CFDictionaryAddValue( dict, CFSTR("Removable"), kCFBooleanTrue );
368 service = IOServiceGetMatchingService( kIOMasterPortDefault, dict );
370 /* now look for the parent that has the "Whole" attribute set to TRUE */
372 while (service)
374 io_service_t parent = 0;
375 CFBooleanRef whole;
376 CFStringRef str;
377 int ok;
379 if (!IOObjectConformsTo( service, kIOMediaClass ))
380 goto next;
381 if (!(whole = IORegistryEntryCreateCFProperty( service, CFSTR("Whole"), NULL, 0 )))
382 goto next;
383 ok = (whole == kCFBooleanTrue);
384 CFRelease( whole );
385 if (!ok) goto next;
387 if ((str = IORegistryEntryCreateCFProperty( service, CFSTR("BSD Name"), NULL, 0 )))
389 strcpy( name, "/dev/r" );
390 CFStringGetCString( str, name + 6, len - 6, kCFStringEncodingUTF8 );
391 CFRelease( str );
392 status = STATUS_SUCCESS;
394 IOObjectRelease( service );
395 break;
397 next:
398 IORegistryEntryGetParentEntry( service, kIOServicePlane, &parent );
399 IOObjectRelease( service );
400 service = parent;
402 return status;
404 #endif
407 /******************************************************************
408 * CDROM_SyncCache [internal]
410 * Read the TOC in and store it in the cdrom_cache structure.
411 * Further requests for the TOC will be copied from the cache
412 * unless certain events like disk ejection is detected, in which
413 * case the cache will be cleared, causing it to be resynced.
414 * The cache section must be held by caller.
416 static NTSTATUS CDROM_SyncCache(int dev, int fd)
418 #ifdef linux
419 int i, tsz;
420 struct cdrom_tochdr hdr;
421 struct cdrom_tocentry entry;
423 CDROM_TOC *toc = &cdrom_cache[dev].toc;
424 cdrom_cache[dev].toc_good = 0;
426 if (ioctl(fd, CDROMREADTOCHDR, &hdr) == -1)
428 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
429 return FILE_GetNtStatus();
432 toc->FirstTrack = hdr.cdth_trk0;
433 toc->LastTrack = hdr.cdth_trk1;
434 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
435 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
436 toc->Length[0] = tsz >> 8;
437 toc->Length[1] = tsz;
439 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
441 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
443 if (i == toc->LastTrack + 1)
444 entry.cdte_track = CDROM_LEADOUT;
445 else
446 entry.cdte_track = i;
447 entry.cdte_format = CDROM_MSF;
448 if (ioctl(fd, CDROMREADTOCENTRY, &entry) == -1)
450 WARN("error read entry (%s)\n", strerror(errno));
451 return FILE_GetNtStatus();
453 toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
454 toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
455 /* marking last track with leadout value as index */
456 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
457 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
458 toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
459 toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
460 toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
462 cdrom_cache[dev].toc_good = 1;
463 return STATUS_SUCCESS;
465 #elif defined(__FreeBSD__) || defined(__NetBSD__)
467 int i, tsz;
468 struct ioc_toc_header hdr;
469 struct ioc_read_toc_entry entry;
470 struct cd_toc_entry toc_buffer;
472 CDROM_TOC *toc = &cdrom_cache[dev].toc;
473 cdrom_cache[dev].toc_good = 0;
475 if (ioctl(fd, CDIOREADTOCHEADER, &hdr) == -1)
477 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
478 return FILE_GetNtStatus();
480 toc->FirstTrack = hdr.starting_track;
481 toc->LastTrack = hdr.ending_track;
482 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
483 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
484 toc->Length[0] = tsz >> 8;
485 toc->Length[1] = tsz;
487 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
489 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
491 if (i == toc->LastTrack + 1)
493 #define LEADOUT 0xaa
494 entry.starting_track = LEADOUT;
495 } else {
496 entry.starting_track = i;
498 memset(&toc_buffer, 0, sizeof(toc_buffer));
499 entry.address_format = CD_MSF_FORMAT;
500 entry.data_len = sizeof(toc_buffer);
501 entry.data = &toc_buffer;
502 if (ioctl(fd, CDIOREADTOCENTRYS, &entry) == -1)
504 WARN("error read entry (%s)\n", strerror(errno));
505 return FILE_GetNtStatus();
507 toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
508 toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
509 /* marking last track with leadout value as index */
510 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
511 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
512 toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
513 toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
514 toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
516 cdrom_cache[dev].toc_good = 1;
517 return STATUS_SUCCESS;
519 #elif defined(__APPLE__)
520 int i;
521 dk_cd_read_toc_t hdr;
522 CDROM_TOC *toc = &cdrom_cache[dev].toc;
523 cdrom_cache[dev].toc_good = 0;
525 memset( &hdr, 0, sizeof(hdr) );
526 hdr.buffer = toc;
527 hdr.bufferLength = sizeof(*toc);
528 if (ioctl(fd, DKIOCCDREADTOC, &hdr) == -1)
530 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
531 return FILE_GetNtStatus();
533 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
535 /* convert address format */
536 TRACK_DATA *data = &toc->TrackData[i - toc->FirstTrack];
537 DWORD frame = (((DWORD)data->Address[0] << 24) | ((DWORD)data->Address[1] << 16) |
538 ((DWORD)data->Address[2] << 8) | data->Address[3]);
539 MSF_OF_FRAME( data->Address[1], frame );
540 data->Address[0] = 0;
543 cdrom_cache[dev].toc_good = 1;
544 return STATUS_SUCCESS;
545 #else
546 return STATUS_NOT_SUPPORTED;
547 #endif
550 static void CDROM_ClearCacheEntry(int dev)
552 RtlEnterCriticalSection( &cache_section );
553 cdrom_cache[dev].toc_good = 0;
554 RtlLeaveCriticalSection( &cache_section );
559 /******************************************************************
560 * CDROM_GetInterfaceInfo
562 * Determines the ide interface (the number after the ide), and the
563 * number of the device on that interface for ide cdroms (*iface <= 1).
564 * Determines the scsi information for scsi cdroms (*iface >= 2).
565 * Returns false if the info cannot not be obtained.
567 static int CDROM_GetInterfaceInfo(int fd, UCHAR* iface, UCHAR* port, UCHAR* device, UCHAR* lun)
569 #if defined(linux)
570 struct stat st;
571 if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode)) return 0;
572 *port = 0;
573 *iface = 0;
574 *device = 0;
575 *lun = 0;
576 switch (major(st.st_rdev)) {
577 case IDE0_MAJOR: *iface = 0; break;
578 case IDE1_MAJOR: *iface = 1; break;
579 case IDE2_MAJOR: *iface = 2; break;
580 case IDE3_MAJOR: *iface = 3; break;
581 case IDE4_MAJOR: *iface = 4; break;
582 case IDE5_MAJOR: *iface = 5; break;
583 case IDE6_MAJOR: *iface = 6; break;
584 case IDE7_MAJOR: *iface = 7; break;
585 default: *port = 1; break;
588 if (*port == 0)
589 *device = (minor(st.st_rdev) >> 6);
590 else
592 #ifdef SCSI_IOCTL_GET_IDLUN
593 UINT32 idlun[2];
594 if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, idlun) != -1)
596 *port = (idlun[0] >> 24) & 0xff;
597 *iface = ((idlun[0] >> 16) & 0xff) + 2;
598 *device = idlun[0] & 0xff;
599 *lun = (idlun[0] >> 8) & 0xff;
601 else
602 #endif
604 WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
605 return 0;
608 return 1;
609 #elif defined(__NetBSD__)
610 struct scsi_addr addr;
611 if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
613 switch (addr.type)
615 case TYPE_SCSI:
616 *port = 1;
617 *iface = addr.addr.scsi.scbus;
618 *device = addr.addr.scsi.target;
619 *lun = addr.addr.scsi.lun;
620 return 1;
621 case TYPE_ATAPI:
622 *port = 0;
623 *iface = addr.addr.atapi.atbus;
624 *device = addr.addr.atapi.drive;
625 *lun = 0;
626 return 1;
629 return 0;
630 #elif defined(__FreeBSD__)
631 FIXME("not implemented for BSD\n");
632 return 0;
633 #else
634 FIXME("not implemented for nonlinux\n");
635 return 0;
636 #endif
640 /******************************************************************
641 * CDROM_Open
644 static NTSTATUS CDROM_Open(int fd, int* dev)
646 struct stat st;
647 NTSTATUS ret = STATUS_SUCCESS;
648 int empty = -1;
650 fstat(fd, &st);
652 RtlEnterCriticalSection( &cache_section );
653 for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
655 if (empty == -1 &&
656 cdrom_cache[*dev].device == 0 &&
657 cdrom_cache[*dev].inode == 0)
658 empty = *dev;
659 else if (cdrom_cache[*dev].device == st.st_dev &&
660 cdrom_cache[*dev].inode == st.st_ino)
661 break;
663 if (*dev == MAX_CACHE_ENTRIES)
665 if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
666 else
668 *dev = empty;
669 cdrom_cache[*dev].device = st.st_dev;
670 cdrom_cache[*dev].inode = st.st_ino;
673 RtlLeaveCriticalSection( &cache_section );
675 TRACE("%d, %d\n", *dev, fd);
676 return ret;
679 /******************************************************************
680 * CDROM_GetStatusCode
684 static NTSTATUS CDROM_GetStatusCode(int io)
686 if (io == 0) return STATUS_SUCCESS;
687 return FILE_GetNtStatus();
690 /******************************************************************
691 * CDROM_GetControl
694 static NTSTATUS CDROM_GetControl(int dev, int fd, CDROM_AUDIO_CONTROL* cac)
696 #ifdef __APPLE__
697 uint16_t speed;
698 int io = ioctl( fd, DKIOCCDGETSPEED, &speed );
699 if (io != 0) return CDROM_GetStatusCode( io );
700 /* DKIOCCDGETSPEED returns the speed in kilobytes per second,
701 * so convert to logical blocks (assumed to be ~2 KB).
703 cac->LogicalBlocksPerSecond = speed/2;
704 #else
705 cac->LogicalBlocksPerSecond = 1; /* FIXME */
706 #endif
707 cac->LbaFormat = 0; /* FIXME */
708 return STATUS_NOT_SUPPORTED;
711 /******************************************************************
712 * CDROM_GetDeviceNumber
715 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
717 FIXME( "stub\n" );
718 devnum->DeviceType = FILE_DEVICE_DISK;
719 devnum->DeviceNumber = 1;
720 devnum->PartitionNumber = 1;
721 return STATUS_SUCCESS;
724 /******************************************************************
725 * CDROM_GetDriveGeometry
728 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
730 CDROM_TOC toc;
731 NTSTATUS ret = 0;
732 int fsize = 0;
734 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
736 fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
737 - FRAME_OF_TOC(toc, 1); /* Total size in frames */
739 dg->Cylinders.u.LowPart = fsize / (64 * 32);
740 dg->Cylinders.u.HighPart = 0;
741 dg->MediaType = RemovableMedia;
742 dg->TracksPerCylinder = 64;
743 dg->SectorsPerTrack = 32;
744 dg->BytesPerSector= 2048;
745 return ret;
748 /**************************************************************************
749 * CDROM_Reset [internal]
751 static NTSTATUS CDROM_ResetAudio(int fd)
753 #if defined(linux)
754 return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
755 #elif defined(__FreeBSD__) || defined(__NetBSD__)
756 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
757 #else
758 return STATUS_NOT_SUPPORTED;
759 #endif
762 /******************************************************************
763 * CDROM_SetTray
767 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
769 #if defined(linux)
770 return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
771 #elif defined(__FreeBSD__) || defined(__NetBSD__)
772 return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
773 (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
774 (ioctl(fd, CDIOCPREVENT, NULL)));
775 #elif defined(__APPLE__)
776 if (doEject) return CDROM_GetStatusCode( ioctl( fd, DKIOCEJECT, NULL ) );
777 else return STATUS_NOT_SUPPORTED;
778 #else
779 return STATUS_NOT_SUPPORTED;
780 #endif
783 /******************************************************************
784 * CDROM_ControlEjection
788 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
790 #if defined(linux)
791 return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
792 #elif defined(__FreeBSD__) || defined(__NetBSD__)
793 return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
794 #else
795 return STATUS_NOT_SUPPORTED;
796 #endif
799 /******************************************************************
800 * CDROM_ReadTOC
804 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
806 NTSTATUS ret = STATUS_NOT_SUPPORTED;
808 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
809 return STATUS_INVALID_PARAMETER;
811 RtlEnterCriticalSection( &cache_section );
812 if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
814 *toc = cdrom_cache[dev].toc;
815 ret = STATUS_SUCCESS;
817 RtlLeaveCriticalSection( &cache_section );
818 return ret;
821 /******************************************************************
822 * CDROM_GetDiskData
826 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
828 CDROM_TOC toc;
829 NTSTATUS ret;
830 int i;
832 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
833 data->DiskData = 0;
834 for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
835 if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
836 data->DiskData |= CDROM_DISK_DATA_TRACK;
837 else
838 data->DiskData |= CDROM_DISK_AUDIO_TRACK;
840 return STATUS_SUCCESS;
843 /******************************************************************
844 * CDROM_ReadQChannel
848 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
849 SUB_Q_CHANNEL_DATA* data)
851 NTSTATUS ret = STATUS_NOT_SUPPORTED;
852 #ifdef linux
853 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
854 int io;
855 struct cdrom_subchnl sc;
856 sc.cdsc_format = CDROM_MSF;
858 io = ioctl(fd, CDROMSUBCHNL, &sc);
859 if (io == -1)
861 TRACE("opened or no_media (%s)!\n", strerror(errno));
862 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
863 CDROM_ClearCacheEntry(dev);
864 goto end;
867 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
869 switch (sc.cdsc_audiostatus) {
870 case CDROM_AUDIO_INVALID:
871 CDROM_ClearCacheEntry(dev);
872 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
873 break;
874 case CDROM_AUDIO_NO_STATUS:
875 CDROM_ClearCacheEntry(dev);
876 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
877 break;
878 case CDROM_AUDIO_PLAY:
879 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
880 break;
881 case CDROM_AUDIO_PAUSED:
882 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
883 break;
884 case CDROM_AUDIO_COMPLETED:
885 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
886 break;
887 case CDROM_AUDIO_ERROR:
888 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
889 break;
890 default:
891 TRACE("status=%02X !\n", sc.cdsc_audiostatus);
892 break;
894 switch (fmt->Format)
896 case IOCTL_CDROM_CURRENT_POSITION:
897 RtlEnterCriticalSection( &cache_section );
898 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
899 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
900 data->CurrentPosition.Control = sc.cdsc_ctrl;
901 data->CurrentPosition.ADR = sc.cdsc_adr;
902 data->CurrentPosition.TrackNumber = sc.cdsc_trk;
903 data->CurrentPosition.IndexNumber = sc.cdsc_ind;
905 data->CurrentPosition.AbsoluteAddress[0] = 0;
906 data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute;
907 data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
908 data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
910 data->CurrentPosition.TrackRelativeAddress[0] = 0;
911 data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute;
912 data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
913 data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
915 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
917 else /* not playing */
919 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
920 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
922 RtlLeaveCriticalSection( &cache_section );
923 break;
924 case IOCTL_CDROM_MEDIA_CATALOG:
925 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
927 struct cdrom_mcn mcn;
928 if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
930 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
931 data->MediaCatalog.Mcval = 0; /* FIXME */
932 memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
933 data->MediaCatalog.MediaCatalog[14] = 0;
935 break;
936 case IOCTL_CDROM_TRACK_ISRC:
937 FIXME("TrackIsrc: NIY on linux\n");
938 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
939 data->TrackIsrc.Tcval = 0;
940 io = 0;
941 break;
944 end:
945 ret = CDROM_GetStatusCode(io);
946 #elif defined(__FreeBSD__) || defined(__NetBSD__)
947 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
948 int io;
949 struct ioc_read_subchannel read_sc;
950 struct cd_sub_channel_info sc;
952 read_sc.address_format = CD_MSF_FORMAT;
953 read_sc.track = 0;
954 read_sc.data_len = sizeof(sc);
955 read_sc.data = &sc;
956 switch (fmt->Format)
958 case IOCTL_CDROM_CURRENT_POSITION:
959 read_sc.data_format = CD_CURRENT_POSITION;
960 break;
961 case IOCTL_CDROM_MEDIA_CATALOG:
962 read_sc.data_format = CD_MEDIA_CATALOG;
963 break;
964 case IOCTL_CDROM_TRACK_ISRC:
965 read_sc.data_format = CD_TRACK_INFO;
966 sc.what.track_info.track_number = data->TrackIsrc.Track;
967 break;
969 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
970 if (io == -1)
972 TRACE("opened or no_media (%s)!\n", strerror(errno));
973 CDROM_ClearCacheEntry(dev);
974 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
975 goto end;
978 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
980 switch (sc.header.audio_status) {
981 case CD_AS_AUDIO_INVALID:
982 CDROM_ClearCacheEntry(dev);
983 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
984 break;
985 case CD_AS_NO_STATUS:
986 CDROM_ClearCacheEntry(dev);
987 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
988 break;
989 case CD_AS_PLAY_IN_PROGRESS:
990 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
991 break;
992 case CD_AS_PLAY_PAUSED:
993 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
994 break;
995 case CD_AS_PLAY_COMPLETED:
996 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
997 break;
998 case CD_AS_PLAY_ERROR:
999 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
1000 break;
1001 default:
1002 TRACE("status=%02X !\n", sc.header.audio_status);
1004 switch (fmt->Format)
1006 case IOCTL_CDROM_CURRENT_POSITION:
1007 RtlEnterCriticalSection( &cache_section );
1008 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
1009 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1010 data->CurrentPosition.Control = sc.what.position.control;
1011 data->CurrentPosition.ADR = sc.what.position.addr_type;
1012 data->CurrentPosition.TrackNumber = sc.what.position.track_number;
1013 data->CurrentPosition.IndexNumber = sc.what.position.index_number;
1015 data->CurrentPosition.AbsoluteAddress[0] = 0;
1016 data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
1017 data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
1018 data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
1019 data->CurrentPosition.TrackRelativeAddress[0] = 0;
1020 data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
1021 data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
1022 data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
1023 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
1025 else { /* not playing */
1026 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
1027 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
1029 RtlLeaveCriticalSection( &cache_section );
1030 break;
1031 case IOCTL_CDROM_MEDIA_CATALOG:
1032 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
1033 data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
1034 memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
1035 break;
1036 case IOCTL_CDROM_TRACK_ISRC:
1037 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
1038 data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
1039 memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
1040 break;
1043 end:
1044 ret = CDROM_GetStatusCode(io);
1045 #elif defined(__APPLE__)
1046 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
1047 int io = 0;
1048 union
1050 dk_cd_read_mcn_t mcn;
1051 dk_cd_read_isrc_t isrc;
1052 } ioc;
1053 /* We need IOCDAudioControl for IOCTL_CDROM_CURRENT_POSITION */
1054 if (fmt->Format == IOCTL_CDROM_CURRENT_POSITION)
1056 FIXME("NIY\n");
1057 return STATUS_NOT_SUPPORTED;
1059 /* No IOCDAudioControl support; just set the audio status to none */
1060 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1061 switch(fmt->Format)
1063 case IOCTL_CDROM_MEDIA_CATALOG:
1064 if ((io = ioctl(fd, DKIOCCDREADMCN, &ioc.mcn)) == -1) break;
1065 memcpy(data->MediaCatalog.MediaCatalog, ioc.mcn.mcn, kCDMCNMaxLength);
1066 data->MediaCatalog.Mcval = 1;
1067 break;
1068 case IOCTL_CDROM_TRACK_ISRC:
1069 ioc.isrc.track = fmt->Track;
1070 if ((io = ioctl(fd, DKIOCCDREADISRC, &ioc.isrc)) == -1) break;
1071 memcpy(data->TrackIsrc.TrackIsrc, ioc.isrc.isrc, kCDISRCMaxLength);
1072 data->TrackIsrc.Tcval = 1;
1073 data->TrackIsrc.Track = ioc.isrc.track;
1075 ret = CDROM_GetStatusCode(io);
1076 #endif
1077 return ret;
1080 /******************************************************************
1081 * CDROM_Verify
1082 * Implements: IOCTL_STORAGE_CHECK_VERIFY
1083 * IOCTL_CDROM_CHECK_VERIFY
1084 * IOCTL_DISK_CHECK_VERIFY
1087 static NTSTATUS CDROM_Verify(int dev, int fd)
1089 #if defined(linux)
1090 int ret;
1092 ret = ioctl(fd, CDROM_DRIVE_STATUS, NULL);
1093 if(ret == -1) {
1094 TRACE("ioctl CDROM_DRIVE_STATUS failed(%s)!\n", strerror(errno));
1095 return CDROM_GetStatusCode(ret);
1098 if(ret == CDS_DISC_OK)
1099 return STATUS_SUCCESS;
1100 else
1101 return STATUS_NO_MEDIA_IN_DEVICE;
1102 #elif defined(__FreeBSD__)
1103 int ret;
1104 ret = ioctl(fd, CDIOCSTART, NULL);
1105 if(ret == 0)
1106 return STATUS_SUCCESS;
1107 else
1108 return STATUS_NO_MEDIA_IN_DEVICE;
1109 #else
1110 FIXME("not implemented for non-linux\n");
1111 return STATUS_NOT_SUPPORTED;
1112 #endif
1115 /******************************************************************
1116 * CDROM_PlayAudioMSF
1120 static NTSTATUS CDROM_PlayAudioMSF(int fd, const CDROM_PLAY_AUDIO_MSF* audio_msf)
1122 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1123 #ifdef linux
1124 struct cdrom_msf msf;
1125 int io;
1127 msf.cdmsf_min0 = audio_msf->StartingM;
1128 msf.cdmsf_sec0 = audio_msf->StartingS;
1129 msf.cdmsf_frame0 = audio_msf->StartingF;
1130 msf.cdmsf_min1 = audio_msf->EndingM;
1131 msf.cdmsf_sec1 = audio_msf->EndingS;
1132 msf.cdmsf_frame1 = audio_msf->EndingF;
1134 io = ioctl(fd, CDROMSTART);
1135 if (io == -1)
1137 WARN("motor doesn't start !\n");
1138 goto end;
1140 io = ioctl(fd, CDROMPLAYMSF, &msf);
1141 if (io == -1)
1143 WARN("device doesn't play !\n");
1144 goto end;
1146 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1147 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
1148 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
1149 end:
1150 ret = CDROM_GetStatusCode(io);
1151 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1152 struct ioc_play_msf msf;
1153 int io;
1155 msf.start_m = audio_msf->StartingM;
1156 msf.start_s = audio_msf->StartingS;
1157 msf.start_f = audio_msf->StartingF;
1158 msf.end_m = audio_msf->EndingM;
1159 msf.end_s = audio_msf->EndingS;
1160 msf.end_f = audio_msf->EndingF;
1162 io = ioctl(fd, CDIOCSTART, NULL);
1163 if (io == -1)
1165 WARN("motor doesn't start !\n");
1166 goto end;
1168 io = ioctl(fd, CDIOCPLAYMSF, &msf);
1169 if (io == -1)
1171 WARN("device doesn't play !\n");
1172 goto end;
1174 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1175 msf.start_m, msf.start_s, msf.start_f,
1176 msf.end_m, msf.end_s, msf.end_f);
1177 end:
1178 ret = CDROM_GetStatusCode(io);
1179 #endif
1180 return ret;
1183 /******************************************************************
1184 * CDROM_SeekAudioMSF
1188 static NTSTATUS CDROM_SeekAudioMSF(int dev, int fd, const CDROM_SEEK_AUDIO_MSF* audio_msf)
1190 CDROM_TOC toc;
1191 int i, io, frame;
1192 SUB_Q_CURRENT_POSITION *cp;
1193 #if defined(linux)
1194 struct cdrom_msf0 msf;
1195 struct cdrom_subchnl sc;
1196 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1197 struct ioc_play_msf msf;
1198 struct ioc_read_subchannel read_sc;
1199 struct cd_sub_channel_info sc;
1200 int final_frame;
1201 #endif
1203 /* Use the information on the TOC to compute the new current
1204 * position, which is shadowed on the cache. [Portable]. */
1205 frame = FRAME_OF_MSF(*audio_msf);
1207 if ((io = CDROM_ReadTOC(dev, fd, &toc)) != 0) return io;
1209 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
1210 if (FRAME_OF_TOC(toc,i)>frame) break;
1211 if (i <= toc.FirstTrack || i > toc.LastTrack+1)
1212 return STATUS_INVALID_PARAMETER;
1213 i--;
1214 RtlEnterCriticalSection( &cache_section );
1215 cp = &cdrom_cache[dev].CurrentPosition;
1216 cp->FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1217 cp->Control = toc.TrackData[i-toc.FirstTrack].Control;
1218 cp->ADR = toc.TrackData[i-toc.FirstTrack].Adr;
1219 cp->TrackNumber = toc.TrackData[i-toc.FirstTrack].TrackNumber;
1220 cp->IndexNumber = 0; /* FIXME: where do they keep these? */
1221 cp->AbsoluteAddress[0] = 0;
1222 cp->AbsoluteAddress[1] = toc.TrackData[i-toc.FirstTrack].Address[1];
1223 cp->AbsoluteAddress[2] = toc.TrackData[i-toc.FirstTrack].Address[2];
1224 cp->AbsoluteAddress[3] = toc.TrackData[i-toc.FirstTrack].Address[3];
1225 frame -= FRAME_OF_TOC(toc,i);
1226 cp->TrackRelativeAddress[0] = 0;
1227 MSF_OF_FRAME(cp->TrackRelativeAddress[1], frame);
1228 RtlLeaveCriticalSection( &cache_section );
1230 /* If playing, then issue a seek command, otherwise do nothing */
1231 #ifdef linux
1232 sc.cdsc_format = CDROM_MSF;
1234 io = ioctl(fd, CDROMSUBCHNL, &sc);
1235 if (io == -1)
1237 TRACE("opened or no_media (%s)!\n", strerror(errno));
1238 CDROM_ClearCacheEntry(dev);
1239 return CDROM_GetStatusCode(io);
1241 if (sc.cdsc_audiostatus==CDROM_AUDIO_PLAY)
1243 msf.minute = audio_msf->M;
1244 msf.second = audio_msf->S;
1245 msf.frame = audio_msf->F;
1246 return CDROM_GetStatusCode(ioctl(fd, CDROMSEEK, &msf));
1248 return STATUS_SUCCESS;
1249 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1250 read_sc.address_format = CD_MSF_FORMAT;
1251 read_sc.track = 0;
1252 read_sc.data_len = sizeof(sc);
1253 read_sc.data = &sc;
1254 read_sc.data_format = CD_CURRENT_POSITION;
1256 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1257 if (io == -1)
1259 TRACE("opened or no_media (%s)!\n", strerror(errno));
1260 CDROM_ClearCacheEntry(dev);
1261 return CDROM_GetStatusCode(io);
1263 if (sc.header.audio_status==CD_AS_PLAY_IN_PROGRESS)
1266 msf.start_m = audio_msf->M;
1267 msf.start_s = audio_msf->S;
1268 msf.start_f = audio_msf->F;
1269 final_frame = FRAME_OF_TOC(toc,toc.LastTrack+1)-1;
1270 MSF_OF_FRAME(msf.end_m, final_frame);
1272 return CDROM_GetStatusCode(ioctl(fd, CDIOCPLAYMSF, &msf));
1274 return STATUS_SUCCESS;
1275 #else
1276 return STATUS_NOT_SUPPORTED;
1277 #endif
1280 /******************************************************************
1281 * CDROM_PauseAudio
1285 static NTSTATUS CDROM_PauseAudio(int fd)
1287 #if defined(linux)
1288 return CDROM_GetStatusCode(ioctl(fd, CDROMPAUSE));
1289 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1290 return CDROM_GetStatusCode(ioctl(fd, CDIOCPAUSE, NULL));
1291 #else
1292 return STATUS_NOT_SUPPORTED;
1293 #endif
1296 /******************************************************************
1297 * CDROM_ResumeAudio
1301 static NTSTATUS CDROM_ResumeAudio(int fd)
1303 #if defined(linux)
1304 return CDROM_GetStatusCode(ioctl(fd, CDROMRESUME));
1305 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1306 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESUME, NULL));
1307 #else
1308 return STATUS_NOT_SUPPORTED;
1309 #endif
1312 /******************************************************************
1313 * CDROM_StopAudio
1317 static NTSTATUS CDROM_StopAudio(int fd)
1319 #if defined(linux)
1320 return CDROM_GetStatusCode(ioctl(fd, CDROMSTOP));
1321 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1322 return CDROM_GetStatusCode(ioctl(fd, CDIOCSTOP, NULL));
1323 #else
1324 return STATUS_NOT_SUPPORTED;
1325 #endif
1328 /******************************************************************
1329 * CDROM_GetVolume
1333 static NTSTATUS CDROM_GetVolume(int fd, VOLUME_CONTROL* vc)
1335 #if defined(linux)
1336 struct cdrom_volctrl volc;
1337 int io;
1339 io = ioctl(fd, CDROMVOLREAD, &volc);
1340 if (io != -1)
1342 vc->PortVolume[0] = volc.channel0;
1343 vc->PortVolume[1] = volc.channel1;
1344 vc->PortVolume[2] = volc.channel2;
1345 vc->PortVolume[3] = volc.channel3;
1347 return CDROM_GetStatusCode(io);
1348 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1349 struct ioc_vol volc;
1350 int io;
1352 io = ioctl(fd, CDIOCGETVOL, &volc);
1353 if (io != -1)
1355 vc->PortVolume[0] = volc.vol[0];
1356 vc->PortVolume[1] = volc.vol[1];
1357 vc->PortVolume[2] = volc.vol[2];
1358 vc->PortVolume[3] = volc.vol[3];
1360 return CDROM_GetStatusCode(io);
1361 #else
1362 return STATUS_NOT_SUPPORTED;
1363 #endif
1366 /******************************************************************
1367 * CDROM_SetVolume
1371 static NTSTATUS CDROM_SetVolume(int fd, const VOLUME_CONTROL* vc)
1373 #if defined(linux)
1374 struct cdrom_volctrl volc;
1376 volc.channel0 = vc->PortVolume[0];
1377 volc.channel1 = vc->PortVolume[1];
1378 volc.channel2 = vc->PortVolume[2];
1379 volc.channel3 = vc->PortVolume[3];
1381 return CDROM_GetStatusCode(ioctl(fd, CDROMVOLCTRL, &volc));
1382 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1383 struct ioc_vol volc;
1385 volc.vol[0] = vc->PortVolume[0];
1386 volc.vol[1] = vc->PortVolume[1];
1387 volc.vol[2] = vc->PortVolume[2];
1388 volc.vol[3] = vc->PortVolume[3];
1390 return CDROM_GetStatusCode(ioctl(fd, CDIOCSETVOL, &volc));
1391 #else
1392 return STATUS_NOT_SUPPORTED;
1393 #endif
1396 /******************************************************************
1397 * CDROM_RawRead
1399 * Some features of this IOCTL are rather poorly documented and
1400 * not really intuitive either:
1402 * 1. Although the DiskOffset parameter is meant to be a
1403 * byte offset into the disk, it is in fact the sector
1404 * number multiplied by 2048 regardless of the actual
1405 * sector size.
1407 * 2. The least significant 11 bits of DiskOffset are ignored.
1409 * 3. The TrackMode parameter has no effect on the sector
1410 * size. The entire raw sector (i.e. 2352 bytes of data)
1411 * is always returned. IMO the TrackMode is only used
1412 * to check the correct sector type.
1415 static NTSTATUS CDROM_RawRead(int fd, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
1417 int ret = STATUS_NOT_SUPPORTED;
1418 int io = -1;
1420 TRACE("RAW_READ_INFO: DiskOffset=%i,%i SectorCount=%i TrackMode=%i\n buffer=%p len=%i sz=%p\n",
1421 raw->DiskOffset.u.HighPart, raw->DiskOffset.u.LowPart, raw->SectorCount, raw->TrackMode, buffer, len, sz);
1423 if (len < raw->SectorCount * 2352) return STATUS_BUFFER_TOO_SMALL;
1425 #if defined(linux)
1426 if (raw->DiskOffset.u.HighPart & ~2047) {
1427 WARN("DiskOffset points to a sector >= 2**32\n");
1428 return ret;
1431 switch (raw->TrackMode)
1433 case YellowMode2:
1434 case XAForm2:
1436 DWORD lba = raw->DiskOffset.QuadPart >> 11;
1437 struct cdrom_msf* msf;
1438 PBYTE *bp; /* current buffer pointer */
1439 DWORD i;
1441 if ((lba + raw->SectorCount) >
1442 ((1 << 8*sizeof(msf->cdmsf_min0)) * CD_SECS * CD_FRAMES
1443 - CD_MSF_OFFSET)) {
1444 WARN("DiskOffset not accessible with MSF\n");
1445 return ret;
1448 /* Linux reads only one sector at a time.
1449 * ioctl CDROMREADRAW takes struct cdrom_msf as an argument
1450 * on the contrary to what header comments state.
1452 lba += CD_MSF_OFFSET;
1453 for (i = 0, bp = buffer; i < raw->SectorCount;
1454 i++, lba++, bp += 2352)
1456 msf = (struct cdrom_msf*)bp;
1457 msf->cdmsf_min0 = lba / CD_FRAMES / CD_SECS;
1458 msf->cdmsf_sec0 = lba / CD_FRAMES % CD_SECS;
1459 msf->cdmsf_frame0 = lba % CD_FRAMES;
1460 io = ioctl(fd, CDROMREADRAW, msf);
1461 if (io != 0)
1463 *sz = 2352 * i;
1464 return CDROM_GetStatusCode(io);
1467 break;
1470 case CDDA:
1472 struct cdrom_read_audio cdra;
1474 cdra.addr.lba = raw->DiskOffset.QuadPart >> 11;
1475 TRACE("reading at %u\n", cdra.addr.lba);
1476 cdra.addr_format = CDROM_LBA;
1477 cdra.nframes = raw->SectorCount;
1478 cdra.buf = buffer;
1479 io = ioctl(fd, CDROMREADAUDIO, &cdra);
1480 break;
1483 default:
1484 FIXME("NIY: %d\n", raw->TrackMode);
1485 return STATUS_INVALID_PARAMETER;
1487 #elif defined(__APPLE__)
1488 switch (raw->TrackMode)
1490 case YellowMode2:
1492 /* Mac OS, on the other hand, DOES read only one part of the sector
1493 * at a time. Therefore, we have to read each part of the sector, in
1494 * order, to get the whole raw sector in.
1495 * This means that we have to read each sector one at a time, as on
1496 * Linux.
1498 dk_cd_read_t cdrd;
1499 UInt64 lba = raw->DiskOffset.QuadPart >> 11;
1500 PBYTE bp;
1501 int i;
1503 for (i = 0, bp = buffer; i < raw->SectorCount;
1504 i++, lba++, bp += kCDSectorSizeWhole)
1506 cdrd.offset = lba * kCDSectorSizeWhole;
1507 cdrd.sectorType = kCDSectorTypeMode2;
1509 /* First, the sync area */
1510 cdrd.sectorArea = kCDSectorAreaSync;
1511 cdrd.buffer = bp;
1512 cdrd.bufferLength = 12;
1513 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1514 if (io != 0)
1516 *sz = kCDSectorSizeWhole * i;
1517 return CDROM_GetStatusCode(io);
1520 /* Then the header */
1521 cdrd.offset += 12;
1522 cdrd.sectorArea = kCDSectorAreaHeader;
1523 cdrd.buffer = (PBYTE)cdrd.buffer + 12;
1524 cdrd.bufferLength = 4;
1525 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1526 if (io != 0)
1528 *sz = kCDSectorSizeWhole * i + 12;
1529 return CDROM_GetStatusCode(io);
1532 /* And finally the sector proper */
1533 cdrd.offset += 4;
1534 cdrd.sectorArea = kCDSectorAreaUser;
1535 cdrd.buffer = (PBYTE)cdrd.buffer + 4;
1536 cdrd.bufferLength = kCDSectorSizeMode2;
1537 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1538 if (io != 0)
1540 *sz = kCDSectorSizeWhole * i + 16;
1541 return CDROM_GetStatusCode(io);
1545 break;
1548 case XAForm2:
1550 /* Same here */
1551 dk_cd_read_t cdrd;
1552 UInt64 lba = raw->DiskOffset.QuadPart >> 11;
1553 PBYTE bp;
1554 int i;
1556 for (i = 0, bp = buffer; i < raw->SectorCount;
1557 i++, lba++, bp += kCDSectorSizeWhole)
1559 cdrd.offset = lba * kCDSectorSizeWhole;
1560 cdrd.sectorType = kCDSectorTypeMode2Form2;
1562 /* First, the sync area */
1563 cdrd.sectorArea = kCDSectorAreaSync;
1564 cdrd.buffer = bp;
1565 cdrd.bufferLength = 12;
1566 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1567 if (io != 0)
1569 *sz = kCDSectorSizeWhole * i;
1570 return CDROM_GetStatusCode(io);
1573 /* Then the header */
1574 cdrd.offset += 12;
1575 cdrd.sectorArea = kCDSectorAreaHeader;
1576 cdrd.buffer = (PBYTE)cdrd.buffer + 12;
1577 cdrd.bufferLength = 4;
1578 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1579 if (io != 0)
1581 *sz = kCDSectorSizeWhole * i + 12;
1582 return CDROM_GetStatusCode(io);
1585 /* And the sub-header */
1586 cdrd.offset += 4;
1587 cdrd.sectorArea = kCDSectorAreaSubHeader;
1588 cdrd.buffer = (PBYTE)cdrd.buffer + 4;
1589 cdrd.bufferLength = 8;
1590 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1591 if (io != 0)
1593 *sz = kCDSectorSizeWhole * i + 16;
1594 return CDROM_GetStatusCode(io);
1597 /* And finally the sector proper */
1598 cdrd.offset += 8;
1599 cdrd.sectorArea = kCDSectorAreaUser;
1600 cdrd.buffer = (PBYTE)cdrd.buffer + 8;
1601 cdrd.bufferLength = kCDSectorSizeMode2;
1602 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1603 if (io != 0)
1605 *sz = kCDSectorSizeWhole * i + 24;
1606 return CDROM_GetStatusCode(io);
1610 break;
1613 case CDDA:
1615 /* With CDDA, the whole raw sector is considered user data, so there's
1616 * no need to read one at a time.
1618 dk_cd_read_t cdrd;
1620 cdrd.offset = (raw->DiskOffset.QuadPart >> 11) * kCDSectorSizeCDDA;
1621 cdrd.sectorArea = kCDSectorAreaUser;
1622 cdrd.sectorType = kCDSectorTypeCDDA;
1623 cdrd.buffer = buffer;
1624 cdrd.bufferLength = len < raw->SectorCount*kCDSectorSizeCDDA ? len :
1625 raw->SectorCount*kCDSectorSizeCDDA;
1627 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1628 if (io != 0) return CDROM_GetStatusCode(io);
1629 break;
1632 default:
1633 FIXME("NIY: %d\n", raw->TrackMode);
1634 return STATUS_INVALID_PARAMETER;
1636 #else
1637 switch (raw->TrackMode)
1639 case YellowMode2:
1640 FIXME("YellowMode2: NIY\n");
1641 return ret;
1642 case XAForm2:
1643 FIXME("XAForm2: NIY\n");
1644 return ret;
1645 case CDDA:
1646 FIXME("CDDA: NIY\n");
1647 return ret;
1648 default:
1649 FIXME("NIY: %d\n", raw->TrackMode);
1650 return STATUS_INVALID_PARAMETER;
1652 #endif
1654 *sz = 2352 * raw->SectorCount;
1655 ret = CDROM_GetStatusCode(io);
1656 return ret;
1659 /******************************************************************
1660 * CDROM_ScsiPassThroughDirect
1661 * Implements IOCTL_SCSI_PASS_THROUGH_DIRECT
1664 static NTSTATUS CDROM_ScsiPassThroughDirect(int fd, PSCSI_PASS_THROUGH_DIRECT pPacket)
1666 int ret = STATUS_NOT_SUPPORTED;
1667 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1668 sg_io_hdr_t cmd;
1669 int io;
1670 #elif defined HAVE_SCSIREQ_T_CMD
1671 scsireq_t cmd;
1672 int io;
1673 #elif defined __APPLE__
1674 dk_scsi_command_t cmd;
1675 int io;
1676 #endif
1678 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1679 return STATUS_BUFFER_TOO_SMALL;
1681 if (pPacket->CdbLength > 16)
1682 return STATUS_INVALID_PARAMETER;
1684 #ifdef SENSEBUFLEN
1685 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1686 return STATUS_INVALID_PARAMETER;
1687 #elif defined HAVE_REQUEST_SENSE
1688 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1689 return STATUS_INVALID_PARAMETER;
1690 #endif
1692 if (pPacket->DataTransferLength > 0 && !pPacket->DataBuffer)
1693 return STATUS_INVALID_PARAMETER;
1695 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1696 RtlZeroMemory(&cmd, sizeof(cmd));
1698 cmd.interface_id = 'S';
1699 cmd.cmd_len = pPacket->CdbLength;
1700 cmd.mx_sb_len = pPacket->SenseInfoLength;
1701 cmd.dxfer_len = pPacket->DataTransferLength;
1702 cmd.dxferp = pPacket->DataBuffer;
1703 cmd.cmdp = pPacket->Cdb;
1704 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1705 cmd.timeout = pPacket->TimeOutValue*1000;
1707 switch (pPacket->DataIn)
1709 case SCSI_IOCTL_DATA_IN:
1710 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1711 break;
1712 case SCSI_IOCTL_DATA_OUT:
1713 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1714 break;
1715 case SCSI_IOCTL_DATA_UNSPECIFIED:
1716 cmd.dxfer_direction = SG_DXFER_NONE;
1717 break;
1718 default:
1719 return STATUS_INVALID_PARAMETER;
1722 io = ioctl(fd, SG_IO, &cmd);
1724 pPacket->ScsiStatus = cmd.status;
1725 pPacket->DataTransferLength = cmd.resid;
1726 pPacket->SenseInfoLength = cmd.sb_len_wr;
1728 ret = CDROM_GetStatusCode(io);
1730 #elif defined HAVE_SCSIREQ_T_CMD
1732 memset(&cmd, 0, sizeof(cmd));
1733 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1735 cmd.cmdlen = pPacket->CdbLength;
1736 cmd.databuf = pPacket->DataBuffer;
1737 cmd.datalen = pPacket->DataTransferLength;
1738 cmd.senselen = pPacket->SenseInfoLength;
1739 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1741 switch (pPacket->DataIn)
1743 case SCSI_IOCTL_DATA_OUT:
1744 cmd.flags |= SCCMD_WRITE;
1745 break;
1746 case SCSI_IOCTL_DATA_IN:
1747 cmd.flags |= SCCMD_READ;
1748 break;
1749 case SCSI_IOCTL_DATA_UNSPECIFIED:
1750 cmd.flags = 0;
1751 break;
1752 default:
1753 return STATUS_INVALID_PARAMETER;
1756 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1758 switch (cmd.retsts)
1760 case SCCMD_OK: break;
1761 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1762 break;
1763 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1764 break;
1765 case SCCMD_SENSE: break;
1766 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1767 break;
1770 if (pPacket->SenseInfoLength != 0)
1772 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1773 cmd.sense, pPacket->SenseInfoLength);
1776 pPacket->ScsiStatus = cmd.status;
1778 ret = CDROM_GetStatusCode(io);
1780 #elif defined(__APPLE__)
1782 memset(&cmd, 0, sizeof(cmd));
1783 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1785 cmd.cdbSize = pPacket->CdbLength;
1786 cmd.buffer = pPacket->DataBuffer;
1787 cmd.bufferSize = pPacket->DataTransferLength;
1788 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1789 cmd.senseLen = pPacket->SenseInfoLength;
1790 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1792 switch (pPacket->DataIn)
1794 case SCSI_IOCTL_DATA_OUT:
1795 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
1796 break;
1797 case SCSI_IOCTL_DATA_IN:
1798 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
1799 break;
1800 case SCSI_IOCTL_DATA_UNSPECIFIED:
1801 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
1802 break;
1803 default:
1804 return STATUS_INVALID_PARAMETER;
1807 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
1809 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
1811 /* Command failed */
1812 switch (cmd.status)
1814 case kSCSITaskStatus_TaskTimeoutOccurred: return STATUS_TIMEOUT;
1815 break;
1816 case kSCSITaskStatus_ProtocolTimeoutOccurred: return STATUS_IO_TIMEOUT;
1817 break;
1818 case kSCSITaskStatus_DeviceNotResponding: return STATUS_DEVICE_BUSY;
1819 break;
1820 case kSCSITaskStatus_DeviceNotPresent:
1821 return STATUS_NO_SUCH_DEVICE;
1822 break;
1823 case kSCSITaskStatus_DeliveryFailure:
1824 return STATUS_DEVICE_PROTOCOL_ERROR;
1825 break;
1826 case kSCSITaskStatus_No_Status:
1827 default:
1828 return STATUS_UNSUCCESSFUL;
1829 break;
1833 if (cmd.status != kSCSITaskStatus_No_Status)
1834 pPacket->ScsiStatus = cmd.status;
1836 ret = CDROM_GetStatusCode(io);
1837 #endif
1838 return ret;
1841 /******************************************************************
1842 * CDROM_ScsiPassThrough
1843 * Implements IOCTL_SCSI_PASS_THROUGH
1846 static NTSTATUS CDROM_ScsiPassThrough(int fd, PSCSI_PASS_THROUGH pPacket)
1848 int ret = STATUS_NOT_SUPPORTED;
1849 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1850 sg_io_hdr_t cmd;
1851 int io;
1852 #elif defined HAVE_SCSIREQ_T_CMD
1853 scsireq_t cmd;
1854 int io;
1855 #elif defined __APPLE__
1856 dk_scsi_command_t cmd;
1857 int io;
1858 #endif
1860 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1861 return STATUS_BUFFER_TOO_SMALL;
1863 if (pPacket->CdbLength > 16)
1864 return STATUS_INVALID_PARAMETER;
1866 #ifdef SENSEBUFLEN
1867 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1868 return STATUS_INVALID_PARAMETER;
1869 #elif defined HAVE_REQUEST_SENSE
1870 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1871 return STATUS_INVALID_PARAMETER;
1872 #endif
1874 if (pPacket->DataTransferLength > 0 && pPacket->DataBufferOffset < sizeof(SCSI_PASS_THROUGH))
1875 return STATUS_INVALID_PARAMETER;
1877 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1878 RtlZeroMemory(&cmd, sizeof(cmd));
1880 cmd.interface_id = 'S';
1881 cmd.dxfer_len = pPacket->DataTransferLength;
1882 cmd.dxferp = (char*)pPacket + pPacket->DataBufferOffset;
1883 cmd.cmd_len = pPacket->CdbLength;
1884 cmd.cmdp = pPacket->Cdb;
1885 cmd.mx_sb_len = pPacket->SenseInfoLength;
1886 cmd.timeout = pPacket->TimeOutValue*1000;
1888 if(cmd.mx_sb_len > 0)
1889 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1891 switch (pPacket->DataIn)
1893 case SCSI_IOCTL_DATA_IN:
1894 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1895 break;
1896 case SCSI_IOCTL_DATA_OUT:
1897 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1898 break;
1899 case SCSI_IOCTL_DATA_UNSPECIFIED:
1900 cmd.dxfer_direction = SG_DXFER_NONE;
1901 break;
1902 default:
1903 return STATUS_INVALID_PARAMETER;
1906 io = ioctl(fd, SG_IO, &cmd);
1908 pPacket->ScsiStatus = cmd.status;
1909 pPacket->DataTransferLength = cmd.resid;
1910 pPacket->SenseInfoLength = cmd.sb_len_wr;
1912 ret = CDROM_GetStatusCode(io);
1914 #elif defined HAVE_SCSIREQ_T_CMD
1916 memset(&cmd, 0, sizeof(cmd));
1917 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1919 if ( pPacket->DataBufferOffset > 0x1000 )
1921 cmd.databuf = (void*)pPacket->DataBufferOffset;
1923 else
1925 cmd.databuf = (char*)pPacket + pPacket->DataBufferOffset;
1928 cmd.cmdlen = pPacket->CdbLength;
1929 cmd.datalen = pPacket->DataTransferLength;
1930 cmd.senselen = pPacket->SenseInfoLength;
1931 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1933 switch (pPacket->DataIn)
1935 case SCSI_IOCTL_DATA_OUT:
1936 cmd.flags |= SCCMD_WRITE;
1937 break;
1938 case SCSI_IOCTL_DATA_IN:
1939 cmd.flags |= SCCMD_READ;
1940 break;
1941 case SCSI_IOCTL_DATA_UNSPECIFIED:
1942 cmd.flags = 0;
1943 break;
1944 default:
1945 return STATUS_INVALID_PARAMETER;
1948 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1950 switch (cmd.retsts)
1952 case SCCMD_OK: break;
1953 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1954 break;
1955 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1956 break;
1957 case SCCMD_SENSE: break;
1958 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1959 break;
1962 if (pPacket->SenseInfoLength != 0)
1964 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1965 cmd.sense, pPacket->SenseInfoLength);
1968 pPacket->ScsiStatus = cmd.status;
1970 ret = CDROM_GetStatusCode(io);
1972 #elif defined(__APPLE__)
1974 memset(&cmd, 0, sizeof(cmd));
1975 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1977 cmd.cdbSize = pPacket->CdbLength;
1978 cmd.buffer = (char*)pPacket + pPacket->DataBufferOffset;
1979 cmd.bufferSize = pPacket->DataTransferLength;
1980 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1981 cmd.senseLen = pPacket->SenseInfoLength;
1982 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1984 switch (pPacket->DataIn)
1986 case SCSI_IOCTL_DATA_OUT:
1987 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
1988 break;
1989 case SCSI_IOCTL_DATA_IN:
1990 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
1991 break;
1992 case SCSI_IOCTL_DATA_UNSPECIFIED:
1993 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
1994 break;
1995 default:
1996 return STATUS_INVALID_PARAMETER;
1999 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
2001 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
2003 /* Command failed */
2004 switch (cmd.status)
2006 case kSCSITaskStatus_TaskTimeoutOccurred:
2007 return STATUS_TIMEOUT;
2008 break;
2009 case kSCSITaskStatus_ProtocolTimeoutOccurred:
2010 return STATUS_IO_TIMEOUT;
2011 break;
2012 case kSCSITaskStatus_DeviceNotResponding:
2013 return STATUS_DEVICE_BUSY;
2014 break;
2015 case kSCSITaskStatus_DeviceNotPresent:
2016 return STATUS_NO_SUCH_DEVICE;
2017 break;
2018 case kSCSITaskStatus_DeliveryFailure:
2019 return STATUS_DEVICE_PROTOCOL_ERROR;
2020 break;
2021 case kSCSITaskStatus_No_Status:
2022 default:
2023 return STATUS_UNSUCCESSFUL;
2024 break;
2028 if (cmd.status != kSCSITaskStatus_No_Status)
2029 pPacket->ScsiStatus = cmd.status;
2031 ret = CDROM_GetStatusCode(io);
2032 #endif
2033 return ret;
2036 /******************************************************************
2037 * CDROM_ScsiGetCaps
2041 static NTSTATUS CDROM_ScsiGetCaps(int fd, PIO_SCSI_CAPABILITIES caps)
2043 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
2045 #ifdef SG_SCATTER_SZ
2046 caps->Length = sizeof(*caps);
2047 caps->MaximumTransferLength = SG_SCATTER_SZ; /* FIXME */
2048 caps->MaximumPhysicalPages = SG_SCATTER_SZ / getpagesize();
2049 caps->SupportedAsynchronousEvents = TRUE;
2050 caps->AlignmentMask = getpagesize();
2051 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
2052 caps->AdapterScansDown = FALSE; /* FIXME ? */
2053 caps->AdapterUsesPio = FALSE; /* FIXME ? */
2054 ret = STATUS_SUCCESS;
2055 #elif defined __APPLE__
2056 uint64_t bytesr, bytesw, align;
2057 int io = ioctl(fd, DKIOCGETMAXBYTECOUNTREAD, &bytesr);
2058 if (io != 0) return CDROM_GetStatusCode(io);
2059 io = ioctl(fd, DKIOCGETMAXBYTECOUNTWRITE, &bytesw);
2060 if (io != 0) return CDROM_GetStatusCode(io);
2061 io = ioctl(fd, DKIOCGETMINSEGMENTALIGNMENTBYTECOUNT, &align);
2062 if (io != 0) return CDROM_GetStatusCode(io);
2063 caps->Length = sizeof(*caps);
2064 caps->MaximumTransferLength = bytesr < bytesw ? bytesr : bytesw;
2065 caps->MaximumPhysicalPages = caps->MaximumTransferLength / getpagesize();
2066 caps->SupportedAsynchronousEvents = TRUE;
2067 caps->AlignmentMask = align-1;
2068 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
2069 caps->AdapterScansDown = FALSE; /* FIXME ? */
2070 caps->AdapterUsesPio = FALSE; /* FIXME ? */
2071 ret = STATUS_SUCCESS;
2072 #else
2073 FIXME("Unimplemented\n");
2074 #endif
2075 return ret;
2078 /******************************************************************
2079 * CDROM_GetAddress
2081 * implements IOCTL_SCSI_GET_ADDRESS
2083 static NTSTATUS CDROM_GetAddress(int fd, SCSI_ADDRESS* address)
2085 UCHAR portnum, busid, targetid, lun;
2087 address->Length = sizeof(SCSI_ADDRESS);
2088 if ( ! CDROM_GetInterfaceInfo(fd, &portnum, &busid, &targetid, &lun))
2089 return STATUS_NOT_SUPPORTED;
2091 address->PortNumber = portnum; /* primary=0 secondary=1 for ide */
2092 address->PathId = busid; /* always 0 for ide */
2093 address->TargetId = targetid; /* master=0 slave=1 for ide */
2094 address->Lun = lun;
2095 return STATUS_SUCCESS;
2098 /******************************************************************
2099 * DVD_StartSession
2103 static NTSTATUS DVD_StartSession(int fd, const DVD_SESSION_ID *sid_in, PDVD_SESSION_ID sid_out)
2105 #if defined(linux)
2106 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2107 dvd_authinfo auth_info;
2109 memset( &auth_info, 0, sizeof( auth_info ) );
2110 auth_info.type = DVD_LU_SEND_AGID;
2111 if (sid_in) auth_info.lsa.agid = *(const int*)sid_in; /* ?*/
2113 TRACE("fd 0x%08x\n",fd);
2114 ret =CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2115 *sid_out = auth_info.lsa.agid;
2116 return ret;
2117 #elif defined(__FreeBSD__) || defined(__NetBSD__)
2118 return STATUS_NOT_SUPPORTED;
2119 #elif defined(__APPLE__)
2120 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2121 dk_dvd_report_key_t dvdrk;
2122 DVDAuthenticationGrantIDInfo agid_info;
2124 dvdrk.format = kDVDKeyFormatAGID_CSS;
2125 dvdrk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2126 if(sid_in) dvdrk.grantID = *(uint8_t*)sid_in; /* ? */
2127 dvdrk.bufferLength = sizeof(DVDAuthenticationGrantIDInfo);
2128 dvdrk.buffer = &agid_info;
2130 ret = CDROM_GetStatusCode(ioctl(fd, DKIOCDVDREPORTKEY, &dvdrk));
2131 *sid_out = agid_info.grantID;
2132 return ret;
2133 #else
2134 return STATUS_NOT_SUPPORTED;
2135 #endif
2138 /******************************************************************
2139 * DVD_EndSession
2143 static NTSTATUS DVD_EndSession(int fd, const DVD_SESSION_ID *sid)
2145 #if defined(linux)
2146 dvd_authinfo auth_info;
2148 memset( &auth_info, 0, sizeof( auth_info ) );
2149 auth_info.type = DVD_INVALIDATE_AGID;
2150 auth_info.lsa.agid = *(const int*)sid;
2152 TRACE("\n");
2153 return CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2154 #elif defined(__FreeBSD__) || defined(__NetBSD__)
2155 return STATUS_NOT_SUPPORTED;
2156 #elif defined(__APPLE__)
2157 dk_dvd_send_key_t dvdsk;
2159 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2160 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2161 dvdsk.grantID = (uint8_t)*sid;
2163 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2164 #else
2165 return STATUS_NOT_SUPPORTED;
2166 #endif
2169 /******************************************************************
2170 * DVD_SendKey
2174 static NTSTATUS DVD_SendKey(int fd, const DVD_COPY_PROTECT_KEY *key)
2176 #if defined(linux)
2177 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2178 dvd_authinfo auth_info;
2180 memset( &auth_info, 0, sizeof( auth_info ) );
2181 switch (key->KeyType)
2183 case DvdChallengeKey:
2184 auth_info.type = DVD_HOST_SEND_CHALLENGE;
2185 auth_info.hsc.agid = (int)key->SessionId;
2186 TRACE("DvdChallengeKey ioc 0x%x\n", DVD_AUTH );
2187 memcpy( auth_info.hsc.chal, key->KeyData, DVD_CHALLENGE_SIZE );
2188 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2189 break;
2190 case DvdBusKey2:
2191 auth_info.type = DVD_HOST_SEND_KEY2;
2192 auth_info.hsk.agid = (int)key->SessionId;
2194 memcpy( auth_info.hsk.key, key->KeyData, DVD_KEY_SIZE );
2196 TRACE("DvdBusKey2\n");
2197 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2198 break;
2200 default:
2201 FIXME("Unknown Keytype 0x%x\n",key->KeyType);
2203 return ret;
2204 #elif defined(__APPLE__)
2205 dk_dvd_send_key_t dvdsk;
2206 union
2208 DVDChallengeKeyInfo chal;
2209 DVDKey2Info key2;
2210 } key_desc;
2212 switch(key->KeyType)
2214 case DvdChallengeKey:
2215 dvdsk.format = kDVDKeyFormatChallengeKey;
2216 dvdsk.bufferLength = sizeof(key_desc.chal);
2217 dvdsk.buffer = &key_desc.chal;
2218 OSWriteBigInt16(key_desc.chal.dataLength, 0, key->KeyLength);
2219 memcpy(key_desc.chal.challengeKeyValue, key->KeyData, key->KeyLength);
2220 break;
2221 case DvdBusKey2:
2222 dvdsk.format = kDVDKeyFormatKey2;
2223 dvdsk.bufferLength = sizeof(key_desc.key2);
2224 dvdsk.buffer = &key_desc.key2;
2225 OSWriteBigInt16(key_desc.key2.dataLength, 0, key->KeyLength);
2226 memcpy(key_desc.key2.key2Value, key->KeyData, key->KeyLength);
2227 break;
2228 case DvdInvalidateAGID:
2229 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2230 break;
2231 case DvdBusKey1:
2232 case DvdTitleKey:
2233 case DvdAsf:
2234 case DvdGetRpcKey:
2235 case DvdDiskKey:
2236 ERR("attempted to write read-only key type 0x%x\n", key->KeyType);
2237 return STATUS_NOT_SUPPORTED;
2238 case DvdSetRpcKey:
2239 FIXME("DvdSetRpcKey NIY\n");
2240 return STATUS_NOT_SUPPORTED;
2241 default:
2242 FIXME("got unknown key type 0x%x\n", key->KeyType);
2243 return STATUS_NOT_SUPPORTED;
2245 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2246 dvdsk.grantID = (uint8_t)key->SessionId;
2248 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2249 #else
2250 FIXME("unsupported on this platform\n");
2251 return STATUS_NOT_SUPPORTED;
2252 #endif
2255 /******************************************************************
2256 * DVD_ReadKey
2260 static NTSTATUS DVD_ReadKey(int fd, PDVD_COPY_PROTECT_KEY key)
2262 #if defined(linux)
2263 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2264 dvd_struct dvd;
2265 dvd_authinfo auth_info;
2267 memset( &dvd, 0, sizeof( dvd_struct ) );
2268 memset( &auth_info, 0, sizeof( auth_info ) );
2269 switch (key->KeyType)
2271 case DvdDiskKey:
2273 dvd.type = DVD_STRUCT_DISCKEY;
2274 dvd.disckey.agid = (int)key->SessionId;
2275 memset( dvd.disckey.value, 0, DVD_DISCKEY_SIZE );
2277 TRACE("DvdDiskKey\n");
2278 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2279 if (ret == STATUS_SUCCESS)
2280 memcpy(key->KeyData,dvd.disckey.value,DVD_DISCKEY_SIZE);
2281 break;
2282 case DvdTitleKey:
2283 auth_info.type = DVD_LU_SEND_TITLE_KEY;
2284 auth_info.lstk.agid = (int)key->SessionId;
2285 auth_info.lstk.lba = (int)(key->Parameters.TitleOffset.QuadPart>>11);
2286 TRACE("DvdTitleKey session %d Quadpart 0x%08lx offset 0x%08x\n",
2287 (int)key->SessionId, (long)key->Parameters.TitleOffset.QuadPart,
2288 auth_info.lstk.lba);
2289 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2290 if (ret == STATUS_SUCCESS)
2291 memcpy(key->KeyData, auth_info.lstk.title_key, DVD_KEY_SIZE );
2292 break;
2293 case DvdChallengeKey:
2295 auth_info.type = DVD_LU_SEND_CHALLENGE;
2296 auth_info.lsc.agid = (int)key->SessionId;
2298 TRACE("DvdChallengeKey\n");
2299 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2300 if (ret == STATUS_SUCCESS)
2301 memcpy( key->KeyData, auth_info.lsc.chal, DVD_CHALLENGE_SIZE );
2302 break;
2303 case DvdAsf:
2304 auth_info.type = DVD_LU_SEND_ASF;
2305 TRACE("DvdAsf\n");
2306 auth_info.lsasf.asf=((PDVD_ASF)key->KeyData)->SuccessFlag;
2307 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2308 ((PDVD_ASF)key->KeyData)->SuccessFlag = auth_info.lsasf.asf;
2309 break;
2310 case DvdBusKey1:
2311 auth_info.type = DVD_LU_SEND_KEY1;
2312 auth_info.lsk.agid = (int)key->SessionId;
2314 TRACE("DvdBusKey1\n");
2315 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2317 if (ret == STATUS_SUCCESS)
2318 memcpy( key->KeyData, auth_info.lsk.key, DVD_KEY_SIZE );
2319 break;
2320 case DvdGetRpcKey:
2321 auth_info.type = DVD_LU_SEND_RPC_STATE;
2323 TRACE("DvdGetRpcKey\n");
2324 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2326 if (ret == STATUS_SUCCESS)
2328 ((PDVD_RPC_KEY)key->KeyData)->TypeCode = auth_info.lrpcs.type;
2329 ((PDVD_RPC_KEY)key->KeyData)->RegionMask = auth_info.lrpcs.region_mask;
2330 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme = auth_info.lrpcs.rpc_scheme;
2331 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable = auth_info.lrpcs.ucca;
2332 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable = auth_info.lrpcs.vra;
2334 break;
2335 default:
2336 FIXME("Unknown keytype 0x%x\n",key->KeyType);
2338 return ret;
2339 #elif defined(__FreeBSD__) || defined(__NetBSD__)
2340 TRACE("bsd\n");
2341 return STATUS_NOT_SUPPORTED;
2342 #elif defined(__APPLE__)
2343 union
2345 dk_dvd_report_key_t key;
2346 dk_dvd_read_structure_t disk_key;
2347 } ioc;
2348 union
2350 DVDDiscKeyInfo disk_key;
2351 DVDChallengeKeyInfo chal;
2352 DVDKey1Info key1;
2353 DVDTitleKeyInfo title;
2354 DVDAuthenticationSuccessFlagInfo asf;
2355 DVDRegionPlaybackControlInfo rpc;
2356 } desc;
2357 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2359 switch(key->KeyType)
2361 case DvdChallengeKey:
2362 ioc.key.format = kDVDKeyFormatChallengeKey;
2363 ioc.key.grantID = (uint8_t)key->SessionId;
2364 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2365 ioc.key.bufferLength = sizeof(desc.chal);
2366 ioc.key.buffer = &desc.chal;
2367 OSWriteBigInt16(desc.chal.dataLength, 0, key->KeyLength);
2368 break;
2369 case DvdBusKey1:
2370 ioc.key.format = kDVDKeyFormatKey1;
2371 ioc.key.grantID = (uint8_t)key->SessionId;
2372 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2373 ioc.key.bufferLength = sizeof(desc.key1);
2374 ioc.key.buffer = &desc.key1;
2375 OSWriteBigInt16(desc.key1.dataLength, 0, key->KeyLength);
2376 break;
2377 case DvdTitleKey:
2378 ioc.key.format = kDVDKeyFormatTitleKey;
2379 ioc.key.grantID = (uint8_t)key->SessionId;
2380 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2381 ioc.key.bufferLength = sizeof(desc.title);
2382 ioc.key.buffer = &desc.title;
2383 ioc.key.address = (uint32_t)(key->Parameters.TitleOffset.QuadPart>>11);
2384 OSWriteBigInt16(desc.title.dataLength, 0, key->KeyLength);
2385 break;
2386 case DvdAsf:
2387 ioc.key.format = kDVDKeyFormatASF;
2388 ioc.key.grantID = (uint8_t)key->SessionId;
2389 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2390 ioc.key.bufferLength = sizeof(desc.asf);
2391 ioc.key.buffer = &desc.asf;
2392 OSWriteBigInt16(desc.asf.dataLength, 0, key->KeyLength);
2393 break;
2394 case DvdGetRpcKey:
2395 ioc.key.format = kDVDKeyFormatRegionState;
2396 ioc.key.grantID = (uint8_t)key->SessionId;
2397 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2398 ioc.key.bufferLength = sizeof(desc.rpc);
2399 ioc.key.buffer = &desc.rpc;
2400 OSWriteBigInt16(desc.rpc.dataLength, 0, key->KeyLength);
2401 break;
2402 case DvdDiskKey:
2403 ioc.disk_key.format = kDVDStructureFormatDiscKeyInfo;
2404 ioc.disk_key.grantID = (uint8_t)key->SessionId;
2405 ioc.disk_key.bufferLength = sizeof(desc.disk_key);
2406 ioc.disk_key.buffer = &desc.disk_key;
2407 break;
2408 case DvdInvalidateAGID:
2409 ioc.key.format = kDVDKeyFormatAGID_Invalidate;
2410 ioc.key.grantID = (uint8_t)key->SessionId;
2411 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2412 break;
2413 case DvdBusKey2:
2414 case DvdSetRpcKey:
2415 ERR("attempted to read write-only key type 0x%x\n", key->KeyType);
2416 return STATUS_NOT_SUPPORTED;
2417 default:
2418 FIXME("got unknown key type 0x%x\n", key->KeyType);
2419 return STATUS_NOT_SUPPORTED;
2422 ret = CDROM_GetStatusCode(ioctl(fd, (key->KeyType == DvdDiskKey ? DKIOCDVDREADSTRUCTURE : DKIOCDVDREPORTKEY), &ioc));
2424 if (ret == STATUS_SUCCESS)
2426 switch(key->KeyType)
2428 case DvdChallengeKey:
2429 key->KeyLength = OSReadBigInt16(desc.chal.dataLength, 0);
2430 memcpy(key->KeyData, desc.chal.challengeKeyValue, key->KeyLength);
2431 break;
2432 case DvdBusKey1:
2433 key->KeyLength = OSReadBigInt16(desc.key1.dataLength, 0);
2434 memcpy(key->KeyData, desc.key1.key1Value, key->KeyLength);
2435 break;
2436 case DvdTitleKey:
2437 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2438 memcpy(key->KeyData, desc.title.titleKeyValue, key->KeyLength);
2439 key->KeyFlags = 0;
2440 if (desc.title.CPM)
2442 /*key->KeyFlags |= DVD_COPYRIGHTED;*/
2443 if (desc.title.CP_SEC) key->KeyFlags |= DVD_SECTOR_PROTECTED;
2444 /*else key->KeyFlags |= DVD_SECTOR_NOT_PROTECTED;*/
2445 #if 0
2446 switch (desc.title.CGMS)
2448 case 0:
2449 key->KeyFlags |= DVD_CGMS_COPY_PERMITTED;
2450 break;
2451 case 2:
2452 key->KeyFlags |= DVD_CGMS_COPY_ONCE;
2453 break;
2454 case 3:
2455 key->KeyFlags |= DVD_CGMS_NO_COPY;
2456 break;
2458 #endif
2459 } /*else key->KeyFlags |= DVD_NOT_COPYRIGHTED;*/
2460 break;
2461 case DvdAsf:
2462 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2463 ((PDVD_ASF)key->KeyData)->SuccessFlag = desc.asf.successFlag;
2464 break;
2465 case DvdGetRpcKey:
2466 key->KeyLength = OSReadBigInt16(desc.rpc.dataLength, 0);
2467 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable =
2468 desc.rpc.numberUserResets;
2469 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable =
2470 desc.rpc.numberVendorResets;
2471 ((PDVD_RPC_KEY)key->KeyData)->TypeCode =
2472 desc.rpc.typeCode;
2473 ((PDVD_RPC_KEY)key->KeyData)->RegionMask =
2474 desc.rpc.driveRegion;
2475 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme =
2476 desc.rpc.rpcScheme;
2477 case DvdDiskKey:
2478 key->KeyLength = OSReadBigInt16(desc.disk_key.dataLength, 0);
2479 memcpy(key->KeyData, desc.disk_key.discKeyStructures, key->KeyLength);
2480 break;
2481 case DvdBusKey2:
2482 case DvdSetRpcKey:
2483 case DvdInvalidateAGID:
2484 default:
2485 /* Silence warning */
2489 return ret;
2490 #else
2491 TRACE("outside\n");
2492 return STATUS_NOT_SUPPORTED;
2493 #endif
2496 /******************************************************************
2497 * DVD_GetRegion
2499 * This IOCTL combines information from both IOCTL_DVD_READ_KEY
2500 * with key type DvdGetRpcKey and IOCTL_DVD_READ_STRUCTURE with
2501 * structure type DvdCopyrightInformation into one structure.
2503 static NTSTATUS DVD_GetRegion(int fd, PDVD_REGION region)
2505 #if defined(linux)
2506 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2507 dvd_struct dvd;
2508 dvd_authinfo auth_info;
2510 dvd.type = DVD_STRUCT_COPYRIGHT;
2511 dvd.copyright.layer_num = 0;
2512 auth_info.type = DVD_LU_SEND_RPC_STATE;
2514 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2516 if (ret == STATUS_SUCCESS)
2518 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2520 if (ret == STATUS_SUCCESS)
2522 region->CopySystem = dvd.copyright.cpst;
2523 region->RegionData = dvd.copyright.rmi;
2524 region->SystemRegion = auth_info.lrpcs.region_mask;
2525 region->ResetCount = auth_info.lrpcs.ucca;
2528 return ret;
2529 #elif defined(__FreeBSD__) || defined(__NetBSD__)
2530 TRACE("bsd\n");
2531 return STATUS_NOT_SUPPORTED;
2532 #elif defined(__APPLE__)
2533 dk_dvd_report_key_t key;
2534 dk_dvd_read_structure_t dvd;
2535 DVDRegionPlaybackControlInfo rpc;
2536 DVDCopyrightInfo copy;
2537 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2539 key.format = kDVDKeyFormatRegionState;
2540 key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2541 key.bufferLength = sizeof(rpc);
2542 key.buffer = &rpc;
2543 dvd.format = kDVDStructureFormatCopyrightInfo;
2544 dvd.bufferLength = sizeof(copy);
2545 dvd.buffer = &copy;
2547 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREPORTKEY, &key ));
2549 if (ret == STATUS_SUCCESS)
2551 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREADSTRUCTURE, &dvd ));
2553 if (ret == STATUS_SUCCESS)
2555 region->CopySystem = copy.copyrightProtectionSystemType;
2556 region->RegionData = copy.regionMask;
2557 region->SystemRegion = rpc.driveRegion;
2558 region->ResetCount = rpc.numberUserResets;
2561 return ret;
2562 #else
2563 FIXME("\n");
2564 return STATUS_NOT_SUPPORTED;
2565 #endif
2568 /******************************************************************
2569 * DVD_ReadStructure
2573 static NTSTATUS DVD_ReadStructure(int dev, const DVD_READ_STRUCTURE *structure, PDVD_LAYER_DESCRIPTOR layer)
2575 #ifdef DVD_READ_STRUCT
2576 /* dvd_struct is not defined consistently across platforms */
2577 union
2579 struct dvd_physical physical;
2580 struct dvd_copyright copyright;
2581 struct dvd_disckey disckey;
2582 struct dvd_bca bca;
2583 struct dvd_manufact manufact;
2584 } s;
2586 if (structure->BlockByteOffset.u.HighPart || structure->BlockByteOffset.u.LowPart)
2587 FIXME(": BlockByteOffset is not handled\n");
2589 switch (structure->Format)
2591 case DvdPhysicalDescriptor:
2592 s.physical.type = DVD_STRUCT_PHYSICAL;
2593 s.physical.layer_num = structure->LayerNumber;
2594 break;
2596 case DvdCopyrightDescriptor:
2597 s.copyright.type = DVD_STRUCT_COPYRIGHT;
2598 s.copyright.layer_num = structure->LayerNumber;
2599 break;
2601 case DvdDiskKeyDescriptor:
2602 s.disckey.type = DVD_STRUCT_DISCKEY;
2603 s.disckey.agid = structure->SessionId;
2604 break;
2606 case DvdBCADescriptor:
2607 s.bca.type = DVD_STRUCT_BCA;
2608 break;
2610 case DvdManufacturerDescriptor:
2611 s.manufact.type = DVD_STRUCT_MANUFACT;
2612 s.manufact.layer_num = structure->LayerNumber;
2613 break;
2615 case DvdMaxDescriptor: /* This is not a real request, no matter what MSDN says! */
2616 default:
2617 return STATUS_INVALID_PARAMETER;
2620 if (ioctl(dev, DVD_READ_STRUCT, &s) < 0)
2621 return STATUS_INVALID_PARAMETER;
2623 switch (structure->Format)
2625 case DvdPhysicalDescriptor:
2627 internal_dvd_layer_descriptor *p = (internal_dvd_layer_descriptor *) layer;
2628 struct dvd_layer *l = &s.physical.layer[s.physical.layer_num];
2630 p->DataLength[0] = 2;
2631 p->DataLength[1] = 8;
2632 p->Reserved0[0] = 0;
2633 p->Reserved0[1] = 0;
2634 p->BookVersion = l->book_version;
2635 p->BookType = l->book_type;
2636 p->MinimumRate = l->min_rate;
2637 p->DiskSize = l->disc_size;
2638 p->LayerType = l->layer_type;
2639 p->TrackPath = l->track_path;
2640 p->NumberOfLayers = l->nlayers;
2641 p->Reserved1 = 0;
2642 p->TrackDensity = l->track_density;
2643 p->LinearDensity = l->linear_density;
2644 p->StartingDataSector = l->start_sector;
2645 p->EndDataSector = l->end_sector;
2646 p->EndLayerZeroSector = l->end_sector_l0;
2647 p->Reserved5 = 0;
2648 p->BCAFlag = l->bca;
2649 p->Reserved6 = 0;
2651 break;
2653 case DvdCopyrightDescriptor:
2655 PDVD_COPYRIGHT_DESCRIPTOR p = (PDVD_COPYRIGHT_DESCRIPTOR) layer;
2657 p->CopyrightProtectionType = s.copyright.cpst;
2658 p->RegionManagementInformation = s.copyright.rmi;
2659 p->Reserved = 0;
2661 break;
2663 case DvdDiskKeyDescriptor:
2665 PDVD_DISK_KEY_DESCRIPTOR p = (PDVD_DISK_KEY_DESCRIPTOR) layer;
2667 memcpy(p->DiskKeyData, s.disckey.value, 2048);
2669 break;
2671 case DvdBCADescriptor:
2673 PDVD_BCA_DESCRIPTOR p = (PDVD_BCA_DESCRIPTOR) layer;
2675 memcpy(p->BCAInformation, s.bca.value, s.bca.len);
2677 break;
2679 case DvdManufacturerDescriptor:
2681 PDVD_MANUFACTURER_DESCRIPTOR p = (PDVD_MANUFACTURER_DESCRIPTOR) layer;
2683 memcpy(p->ManufacturingInformation, s.manufact.value, 2048);
2685 break;
2687 case DvdMaxDescriptor: /* Suppress warning */
2688 break;
2690 #elif defined(__APPLE__)
2691 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2692 dk_dvd_read_structure_t dvdrs;
2693 union
2695 DVDPhysicalFormatInfo phys;
2696 DVDCopyrightInfo copy;
2697 DVDDiscKeyInfo disk_key;
2698 DVDManufacturingInfo manf;
2699 } desc;
2700 union
2702 PDVD_LAYER_DESCRIPTOR layer;
2703 internal_dvd_layer_descriptor *xlayer;
2704 PDVD_COPYRIGHT_DESCRIPTOR copy;
2705 PDVD_DISK_KEY_DESCRIPTOR disk_key;
2706 PDVD_MANUFACTURER_DESCRIPTOR manf;
2707 } nt_desc;
2709 nt_desc.layer = layer;
2710 dvdrs.address = (uint32_t)(structure->BlockByteOffset.QuadPart>>11);
2711 dvdrs.grantID = (uint8_t)structure->SessionId;
2712 dvdrs.layer = structure->LayerNumber;
2713 switch(structure->Format)
2715 case DvdPhysicalDescriptor:
2716 dvdrs.format = kDVDStructureFormatPhysicalFormatInfo;
2717 dvdrs.bufferLength = sizeof(desc.phys);
2718 dvdrs.buffer = &desc.phys;
2719 break;
2721 case DvdCopyrightDescriptor:
2722 dvdrs.format = kDVDStructureFormatCopyrightInfo;
2723 dvdrs.bufferLength = sizeof(desc.copy);
2724 dvdrs.buffer = &desc.copy;
2725 break;
2727 case DvdDiskKeyDescriptor:
2728 dvdrs.format = kDVDStructureFormatDiscKeyInfo;
2729 dvdrs.bufferLength = sizeof(desc.disk_key);
2730 dvdrs.buffer = &desc.disk_key;
2731 break;
2733 case DvdBCADescriptor:
2734 FIXME("DvdBCADescriptor NIY\n");
2735 return STATUS_NOT_SUPPORTED;
2737 case DvdManufacturerDescriptor:
2738 dvdrs.format = kDVDStructureFormatManufacturingInfo;
2739 dvdrs.bufferLength = sizeof(desc.manf);
2740 dvdrs.buffer = &desc.manf;
2741 break;
2743 case DvdMaxDescriptor:
2744 default:
2745 FIXME("got unknown structure type 0x%x\n", structure->Format);
2746 return STATUS_NOT_SUPPORTED;
2748 ret = CDROM_GetStatusCode(ioctl(dev, DKIOCDVDREADSTRUCTURE, &dvdrs));
2749 if(ret == STATUS_SUCCESS)
2751 switch(structure->Format)
2753 case DvdPhysicalDescriptor:
2754 nt_desc.xlayer->DataLength[0] = 2;
2755 nt_desc.xlayer->DataLength[1] = 8;
2756 nt_desc.xlayer->Reserved0[0] = 0;
2757 nt_desc.xlayer->Reserved0[1] = 0;
2758 nt_desc.xlayer->BookVersion = desc.phys.partVersion;
2759 nt_desc.xlayer->BookType = desc.phys.bookType;
2760 nt_desc.xlayer->MinimumRate = desc.phys.minimumRate;
2761 nt_desc.xlayer->DiskSize = desc.phys.discSize;
2762 nt_desc.xlayer->LayerType = desc.phys.layerType;
2763 nt_desc.xlayer->TrackPath = desc.phys.trackPath;
2764 nt_desc.xlayer->NumberOfLayers = desc.phys.numberOfLayers;
2765 nt_desc.xlayer->Reserved1 = 0;
2766 nt_desc.xlayer->TrackDensity = desc.phys.trackDensity;
2767 nt_desc.xlayer->LinearDensity = desc.phys.linearDensity;
2768 nt_desc.xlayer->BCAFlag = desc.phys.bcaFlag;
2769 nt_desc.xlayer->StartingDataSector = OSReadBigInt32(&desc.phys.zero1, 0);
2770 nt_desc.xlayer->EndDataSector = OSReadBigInt32(&desc.phys.zero2, 0);
2771 nt_desc.xlayer->EndLayerZeroSector = OSReadBigInt32(&desc.phys.zero3, 0);
2772 nt_desc.xlayer->Reserved5 = 0;
2773 nt_desc.xlayer->Reserved6 = 0;
2774 break;
2776 case DvdCopyrightDescriptor:
2777 nt_desc.copy->CopyrightProtectionType =
2778 desc.copy.copyrightProtectionSystemType;
2779 nt_desc.copy->RegionManagementInformation =
2780 desc.copy.regionMask;
2781 nt_desc.copy->Reserved = 0;
2782 break;
2784 case DvdDiskKeyDescriptor:
2785 memcpy(
2786 nt_desc.disk_key->DiskKeyData,
2787 desc.disk_key.discKeyStructures,
2788 2048);
2789 break;
2791 case DvdManufacturerDescriptor:
2792 memcpy(
2793 nt_desc.manf->ManufacturingInformation,
2794 desc.manf.discManufacturingInfo,
2795 2048);
2796 break;
2798 case DvdBCADescriptor:
2799 case DvdMaxDescriptor:
2800 default:
2801 /* Silence warning */
2802 break;
2805 return ret;
2806 #else
2807 FIXME("\n");
2808 #endif
2809 return STATUS_SUCCESS;
2813 /******************************************************************
2814 * GetInquiryData
2815 * Implements the IOCTL_GET_INQUIRY_DATA ioctl.
2816 * Returns Inquiry data for all devices on the specified scsi bus
2817 * Returns STATUS_BUFFER_TOO_SMALL if the output buffer is too small,
2818 * STATUS_INVALID_DEVICE_REQUEST if the given handle isn't to a SCSI device,
2819 * or STATUS_NOT_SUPPORTED if the OS driver is too old
2821 static NTSTATUS GetInquiryData(int fd, PSCSI_ADAPTER_BUS_INFO BufferOut, DWORD OutBufferSize)
2823 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
2824 PSCSI_INQUIRY_DATA pInquiryData = NULL;
2825 UCHAR sense_buffer[32];
2826 int iochk, version;
2827 sg_io_hdr_t iocmd;
2828 UCHAR inquiry[INQ_CMD_LEN] = {INQUIRY, 0, 0, 0, INQ_REPLY_LEN, 0};
2830 /* Check we have a SCSI device and a supported driver */
2831 if(ioctl(fd, SG_GET_VERSION_NUM, &version) != 0)
2833 WARN("IOCTL_SCSI_GET_INQUIRY_DATA sg driver is not loaded\n");
2834 return STATUS_INVALID_DEVICE_REQUEST;
2836 if(version < 30000 )
2837 return STATUS_NOT_SUPPORTED;
2839 /* FIXME: Enumerate devices on the bus */
2840 BufferOut->NumberOfBuses = 1;
2841 BufferOut->BusData[0].NumberOfLogicalUnits = 1;
2842 BufferOut->BusData[0].InquiryDataOffset = sizeof(SCSI_ADAPTER_BUS_INFO);
2844 pInquiryData = (PSCSI_INQUIRY_DATA)(BufferOut + 1);
2846 RtlZeroMemory(&iocmd, sizeof(iocmd));
2847 iocmd.interface_id = 'S';
2848 iocmd.cmd_len = sizeof(inquiry);
2849 iocmd.mx_sb_len = sizeof(sense_buffer);
2850 iocmd.dxfer_direction = SG_DXFER_FROM_DEV;
2851 iocmd.dxfer_len = INQ_REPLY_LEN;
2852 iocmd.dxferp = pInquiryData->InquiryData;
2853 iocmd.cmdp = inquiry;
2854 iocmd.sbp = sense_buffer;
2855 iocmd.timeout = 1000;
2857 iochk = ioctl(fd, SG_IO, &iocmd);
2858 if(iochk != 0)
2859 WARN("ioctl SG_IO returned %d, error (%s)\n", iochk, strerror(errno));
2861 CDROM_GetInterfaceInfo(fd, &BufferOut->BusData[0].InitiatorBusId, &pInquiryData->PathId, &pInquiryData->TargetId, &pInquiryData->Lun);
2862 pInquiryData->DeviceClaimed = TRUE;
2863 pInquiryData->InquiryDataLength = INQ_REPLY_LEN;
2864 pInquiryData->NextInquiryDataOffset = 0;
2865 return STATUS_SUCCESS;
2866 #else
2867 FIXME("not implemented for nonlinux\n");
2868 return STATUS_NOT_SUPPORTED;
2869 #endif
2872 /******************************************************************
2873 * CDROM_DeviceIoControl
2877 NTSTATUS CDROM_DeviceIoControl(HANDLE hDevice,
2878 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
2879 PVOID UserApcContext,
2880 PIO_STATUS_BLOCK piosb,
2881 ULONG dwIoControlCode,
2882 LPVOID lpInBuffer, DWORD nInBufferSize,
2883 LPVOID lpOutBuffer, DWORD nOutBufferSize)
2885 DWORD sz = 0;
2886 NTSTATUS status = STATUS_SUCCESS;
2887 int fd, needs_close, dev;
2889 TRACE("%p %s %p %d %p %d %p\n",
2890 hDevice, iocodex(dwIoControlCode), lpInBuffer, nInBufferSize,
2891 lpOutBuffer, nOutBufferSize, piosb);
2893 piosb->Information = 0;
2895 if ((status = server_get_unix_fd( hDevice, 0, &fd, &needs_close, NULL, NULL )))
2897 if (status == STATUS_BAD_DEVICE_TYPE) return status; /* no associated fd */
2898 goto error;
2901 if ((status = CDROM_Open(fd, &dev)))
2903 if (needs_close) close( fd );
2904 goto error;
2907 #ifdef __APPLE__
2909 char name[100];
2911 /* This is ugly as hell, but Mac OS is unable to do anything from the
2912 * partition fd, it wants an fd for the whole device, and it sometimes
2913 * also requires the device fd to be closed first, so we have to close
2914 * the handle that the caller gave us.
2915 * Also for some reason it wants the fd to be closed before we even
2916 * open the parent if we're trying to eject the disk.
2918 if ((status = get_parent_device( fd, name, sizeof(name) ))) goto error;
2919 if (dwIoControlCode == IOCTL_STORAGE_EJECT_MEDIA)
2920 NtClose( hDevice );
2921 if (needs_close) close( fd );
2922 TRACE("opening parent %s\n", name );
2923 if ((fd = open( name, O_RDONLY )) == -1)
2925 status = FILE_GetNtStatus();
2926 goto error;
2928 needs_close = 1;
2930 #endif
2932 switch (dwIoControlCode)
2934 case IOCTL_STORAGE_CHECK_VERIFY:
2935 case IOCTL_CDROM_CHECK_VERIFY:
2936 case IOCTL_DISK_CHECK_VERIFY:
2937 sz = 0;
2938 CDROM_ClearCacheEntry(dev);
2939 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2940 status = STATUS_INVALID_PARAMETER;
2941 else status = CDROM_Verify(dev, fd);
2942 break;
2944 /* EPP case IOCTL_STORAGE_CHECK_VERIFY2: */
2946 /* EPP case IOCTL_STORAGE_FIND_NEW_DEVICES: */
2947 /* EPP case IOCTL_CDROM_FIND_NEW_DEVICES: */
2949 case IOCTL_STORAGE_LOAD_MEDIA:
2950 case IOCTL_CDROM_LOAD_MEDIA:
2951 sz = 0;
2952 CDROM_ClearCacheEntry(dev);
2953 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2954 status = STATUS_INVALID_PARAMETER;
2955 else status = CDROM_SetTray(fd, FALSE);
2956 break;
2957 case IOCTL_STORAGE_EJECT_MEDIA:
2958 sz = 0;
2959 CDROM_ClearCacheEntry(dev);
2960 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2961 status = STATUS_INVALID_PARAMETER;
2962 else
2963 status = CDROM_SetTray(fd, TRUE);
2964 break;
2966 case IOCTL_CDROM_MEDIA_REMOVAL:
2967 case IOCTL_DISK_MEDIA_REMOVAL:
2968 case IOCTL_STORAGE_MEDIA_REMOVAL:
2969 case IOCTL_STORAGE_EJECTION_CONTROL:
2970 /* FIXME the last ioctl:s is not the same as the two others...
2971 * lockcount/owner should be handled */
2972 sz = 0;
2973 CDROM_ClearCacheEntry(dev);
2974 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2975 else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) status = STATUS_BUFFER_TOO_SMALL;
2976 else status = CDROM_ControlEjection(fd, lpInBuffer);
2977 break;
2979 /* EPP case IOCTL_STORAGE_GET_MEDIA_TYPES: */
2981 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
2982 sz = sizeof(STORAGE_DEVICE_NUMBER);
2983 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2984 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2985 else status = CDROM_GetDeviceNumber(dev, lpOutBuffer);
2986 break;
2988 case IOCTL_STORAGE_RESET_DEVICE:
2989 sz = 0;
2990 CDROM_ClearCacheEntry(dev);
2991 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2992 status = STATUS_INVALID_PARAMETER;
2993 else status = CDROM_ResetAudio(fd);
2994 break;
2996 case IOCTL_CDROM_GET_CONTROL:
2997 sz = sizeof(CDROM_AUDIO_CONTROL);
2998 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2999 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3000 else status = CDROM_GetControl(dev, fd, lpOutBuffer);
3001 break;
3003 case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
3004 sz = sizeof(DISK_GEOMETRY);
3005 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3006 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3007 else status = CDROM_GetDriveGeometry(dev, fd, lpOutBuffer);
3008 break;
3010 case IOCTL_CDROM_DISK_TYPE:
3011 sz = sizeof(CDROM_DISK_DATA);
3012 /* CDROM_ClearCacheEntry(dev); */
3013 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3014 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3015 else status = CDROM_GetDiskData(dev, fd, lpOutBuffer);
3016 break;
3018 /* EPP case IOCTL_CDROM_GET_LAST_SESSION: */
3020 case IOCTL_CDROM_READ_Q_CHANNEL:
3021 sz = sizeof(SUB_Q_CHANNEL_DATA);
3022 if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT))
3023 status = STATUS_INVALID_PARAMETER;
3024 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3025 else status = CDROM_ReadQChannel(dev, fd, lpInBuffer, lpOutBuffer);
3026 break;
3028 case IOCTL_CDROM_READ_TOC:
3029 sz = sizeof(CDROM_TOC);
3030 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3031 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3032 else status = CDROM_ReadTOC(dev, fd, lpOutBuffer);
3033 break;
3035 /* EPP case IOCTL_CDROM_READ_TOC_EX: */
3037 case IOCTL_CDROM_PAUSE_AUDIO:
3038 sz = 0;
3039 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3040 status = STATUS_INVALID_PARAMETER;
3041 else status = CDROM_PauseAudio(fd);
3042 break;
3043 case IOCTL_CDROM_PLAY_AUDIO_MSF:
3044 sz = 0;
3045 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3046 else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
3047 else status = CDROM_PlayAudioMSF(fd, lpInBuffer);
3048 break;
3049 case IOCTL_CDROM_RESUME_AUDIO:
3050 sz = 0;
3051 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3052 status = STATUS_INVALID_PARAMETER;
3053 else status = CDROM_ResumeAudio(fd);
3054 break;
3055 case IOCTL_CDROM_SEEK_AUDIO_MSF:
3056 sz = 0;
3057 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3058 else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
3059 else status = CDROM_SeekAudioMSF(dev, fd, lpInBuffer);
3060 break;
3061 case IOCTL_CDROM_STOP_AUDIO:
3062 sz = 0;
3063 CDROM_ClearCacheEntry(dev); /* Maybe intention is to change media */
3064 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3065 status = STATUS_INVALID_PARAMETER;
3066 else status = CDROM_StopAudio(fd);
3067 break;
3068 case IOCTL_CDROM_GET_VOLUME:
3069 sz = sizeof(VOLUME_CONTROL);
3070 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3071 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3072 else status = CDROM_GetVolume(fd, lpOutBuffer);
3073 break;
3074 case IOCTL_CDROM_SET_VOLUME:
3075 sz = 0;
3076 CDROM_ClearCacheEntry(dev);
3077 if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
3078 status = STATUS_INVALID_PARAMETER;
3079 else status = CDROM_SetVolume(fd, lpInBuffer);
3080 break;
3081 case IOCTL_CDROM_RAW_READ:
3082 sz = 0;
3083 if (nInBufferSize < sizeof(RAW_READ_INFO)) status = STATUS_INVALID_PARAMETER;
3084 else if (lpOutBuffer == NULL) status = STATUS_BUFFER_TOO_SMALL;
3085 else status = CDROM_RawRead(fd, lpInBuffer, lpOutBuffer,
3086 nOutBufferSize, &sz);
3087 break;
3088 case IOCTL_SCSI_GET_ADDRESS:
3089 sz = sizeof(SCSI_ADDRESS);
3090 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3091 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3092 else status = CDROM_GetAddress(fd, lpOutBuffer);
3093 break;
3094 case IOCTL_SCSI_PASS_THROUGH_DIRECT:
3095 sz = sizeof(SCSI_PASS_THROUGH_DIRECT);
3096 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3097 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH_DIRECT)) status = STATUS_BUFFER_TOO_SMALL;
3098 else status = CDROM_ScsiPassThroughDirect(fd, lpOutBuffer);
3099 break;
3100 case IOCTL_SCSI_PASS_THROUGH:
3101 sz = sizeof(SCSI_PASS_THROUGH);
3102 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3103 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH)) status = STATUS_BUFFER_TOO_SMALL;
3104 else status = CDROM_ScsiPassThrough(fd, lpOutBuffer);
3105 break;
3106 case IOCTL_SCSI_GET_CAPABILITIES:
3107 sz = sizeof(IO_SCSI_CAPABILITIES);
3108 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3109 else if (nOutBufferSize < sizeof(IO_SCSI_CAPABILITIES)) status = STATUS_BUFFER_TOO_SMALL;
3110 else status = CDROM_ScsiGetCaps(fd, lpOutBuffer);
3111 break;
3112 case IOCTL_DVD_START_SESSION:
3113 sz = sizeof(DVD_SESSION_ID);
3114 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3115 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3116 else
3118 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3119 *(PDVD_SESSION_ID)lpOutBuffer);
3120 status = DVD_StartSession(fd, lpInBuffer, lpOutBuffer);
3121 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3122 *(PDVD_SESSION_ID)lpOutBuffer);
3124 break;
3125 case IOCTL_DVD_END_SESSION:
3126 sz = sizeof(DVD_SESSION_ID);
3127 if ((lpInBuffer == NULL) || (nInBufferSize < sz))status = STATUS_INVALID_PARAMETER;
3128 else status = DVD_EndSession(fd, lpInBuffer);
3129 break;
3130 case IOCTL_DVD_SEND_KEY:
3131 sz = 0;
3132 if (!lpInBuffer ||
3133 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3134 status = STATUS_INVALID_PARAMETER;
3135 else
3137 TRACE("doing DVD_SendKey\n");
3138 status = DVD_SendKey(fd, lpInBuffer);
3140 break;
3141 case IOCTL_DVD_READ_KEY:
3142 if (!lpInBuffer ||
3143 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3144 status = STATUS_INVALID_PARAMETER;
3145 else if (lpInBuffer !=lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
3146 else
3148 TRACE("doing DVD_READ_KEY\n");
3149 sz = ((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength;
3150 status = DVD_ReadKey(fd, lpInBuffer);
3152 break;
3153 case IOCTL_DVD_GET_REGION:
3154 sz = sizeof(DVD_REGION);
3155 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3156 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3157 TRACE("doing DVD_Get_REGION\n");
3158 status = DVD_GetRegion(fd, lpOutBuffer);
3159 break;
3160 case IOCTL_DVD_READ_STRUCTURE:
3161 sz = sizeof(DVD_LAYER_DESCRIPTOR);
3162 if (lpInBuffer == NULL || nInBufferSize != sizeof(DVD_READ_STRUCTURE)) status = STATUS_INVALID_PARAMETER;
3163 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3164 else
3166 TRACE("doing DVD_READ_STRUCTURE\n");
3167 status = DVD_ReadStructure(fd, lpInBuffer, lpOutBuffer);
3169 break;
3171 case IOCTL_SCSI_GET_INQUIRY_DATA:
3172 sz = INQ_REPLY_LEN;
3173 status = GetInquiryData(fd, lpOutBuffer, nOutBufferSize);
3174 break;
3176 default:
3177 if (needs_close) close( fd );
3178 return STATUS_NOT_SUPPORTED;
3180 if (needs_close) close( fd );
3181 error:
3182 piosb->u.Status = status;
3183 piosb->Information = sz;
3184 if (hEvent) NtSetEvent(hEvent, NULL);
3185 return status;