winedbg: Add the Polish translation.
[wine/multimedia.git] / dlls / ntdll / cdrom.c
blob1ec03d8c4d1aa8f2c25f16092545d125898a5360
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /* Main file for CD-ROM support
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1999, 2001, 2003 Eric Pouech
6 * Copyright 2000 Andreas Mohr
7 * Copyright 2005 Ivan Leo Puoti
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <errno.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_IO_H
32 # include <io.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <fcntl.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
40 #endif
41 #include <sys/types.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 #include <sys/ioctl.h>
45 #endif
46 #ifdef HAVE_SCSI_SG_H
47 # include <scsi/sg.h>
48 #endif
49 #ifdef HAVE_SCSI_SCSI_H
50 # include <scsi/scsi.h>
51 # undef REASSIGN_BLOCKS /* avoid conflict with winioctl.h */
52 # undef FAILED /* avoid conflict with winerror.h */
53 #endif
54 #ifdef HAVE_SCSI_SCSI_IOCTL_H
55 # include <scsi/scsi_ioctl.h>
56 #endif
57 #ifdef HAVE_LINUX_MAJOR_H
58 # include <linux/major.h>
59 #endif
60 #ifdef HAVE_LINUX_HDREG_H
61 # include <linux/hdreg.h>
62 #endif
63 #ifdef HAVE_LINUX_PARAM_H
64 # include <linux/param.h>
65 #endif
66 #ifdef HAVE_LINUX_CDROM_H
67 # include <linux/cdrom.h>
68 #endif
69 #ifdef HAVE_LINUX_UCDROM_H
70 # include <linux/ucdrom.h>
71 #endif
72 #ifdef HAVE_SYS_CDIO_H
73 # include <sys/cdio.h>
74 #endif
75 #ifdef HAVE_SYS_SCSIIO_H
76 # include <sys/scsiio.h>
77 #endif
79 #ifdef HAVE_IOKIT_IOKITLIB_H
80 # include <libkern/OSByteOrder.h>
81 # include <sys/disk.h>
82 # include <IOKit/IOKitLib.h>
83 # include <IOKit/storage/IOMedia.h>
84 # include <IOKit/storage/IOCDMediaBSDClient.h>
85 # include <IOKit/storage/IODVDMediaBSDClient.h>
86 # include <IOKit/scsi/SCSITask.h>
87 # include <IOKit/scsi/SCSICmds_REQUEST_SENSE_Defs.h>
88 # define SENSEBUFLEN kSenseDefaultSize
90 typedef struct
92 uint32_t attribute;
93 uint32_t timeout;
94 uint32_t response;
95 uint32_t status;
96 uint8_t direction;
97 uint8_t cdbSize;
98 uint8_t reserved0144[2];
99 uint8_t cdb[16];
100 void* buffer;
101 uint64_t bufferSize;
102 void* sense;
103 uint64_t senseLen;
104 } dk_scsi_command_t;
106 #define DKIOCSCSICOMMAND _IOWR('d', 253, dk_scsi_command_t)
108 #endif
110 #define NONAMELESSUNION
111 #define NONAMELESSSTRUCT
112 #include "ntstatus.h"
113 #define WIN32_NO_STATUS
114 #include "windef.h"
115 #include "winternl.h"
116 #include "winioctl.h"
117 #include "ntddstor.h"
118 #include "ntddcdrm.h"
119 #include "ddk/ntddcdvd.h"
120 #include "ntddscsi.h"
121 #include "ntdll_misc.h"
122 #include "wine/server.h"
123 #include "wine/library.h"
124 #include "wine/debug.h"
126 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
128 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
129 lack the following constants. */
131 #ifndef CD_SECS
132 # define CD_SECS 60 /* seconds per minute */
133 #endif
134 #ifndef CD_FRAMES
135 # define CD_FRAMES 75 /* frames per second */
136 #endif
138 static const struct iocodexs
140 DWORD code;
141 const char *codex;
142 } iocodextable[] = {
143 #define X(x) { x, #x },
144 X(IOCTL_CDROM_CHECK_VERIFY)
145 X(IOCTL_CDROM_CURRENT_POSITION)
146 X(IOCTL_CDROM_DISK_TYPE)
147 X(IOCTL_CDROM_GET_CONTROL)
148 X(IOCTL_CDROM_GET_DRIVE_GEOMETRY)
149 X(IOCTL_CDROM_GET_VOLUME)
150 X(IOCTL_CDROM_LOAD_MEDIA)
151 X(IOCTL_CDROM_MEDIA_CATALOG)
152 X(IOCTL_CDROM_MEDIA_REMOVAL)
153 X(IOCTL_CDROM_PAUSE_AUDIO)
154 X(IOCTL_CDROM_PLAY_AUDIO_MSF)
155 X(IOCTL_CDROM_RAW_READ)
156 X(IOCTL_CDROM_READ_Q_CHANNEL)
157 X(IOCTL_CDROM_READ_TOC)
158 X(IOCTL_CDROM_RESUME_AUDIO)
159 X(IOCTL_CDROM_SEEK_AUDIO_MSF)
160 X(IOCTL_CDROM_SET_VOLUME)
161 X(IOCTL_CDROM_STOP_AUDIO)
162 X(IOCTL_CDROM_TRACK_ISRC)
163 X(IOCTL_DISK_MEDIA_REMOVAL)
164 X(IOCTL_DVD_END_SESSION)
165 X(IOCTL_DVD_GET_REGION)
166 X(IOCTL_DVD_READ_KEY)
167 X(IOCTL_DVD_READ_STRUCTURE)
168 X(IOCTL_DVD_SEND_KEY)
169 X(IOCTL_DVD_START_SESSION)
170 X(IOCTL_SCSI_GET_ADDRESS)
171 X(IOCTL_SCSI_GET_CAPABILITIES)
172 X(IOCTL_SCSI_GET_INQUIRY_DATA)
173 X(IOCTL_SCSI_PASS_THROUGH)
174 X(IOCTL_SCSI_PASS_THROUGH_DIRECT)
175 X(IOCTL_STORAGE_CHECK_VERIFY)
176 X(IOCTL_STORAGE_EJECTION_CONTROL)
177 X(IOCTL_STORAGE_EJECT_MEDIA)
178 X(IOCTL_STORAGE_GET_DEVICE_NUMBER)
179 X(IOCTL_STORAGE_GET_MEDIA_TYPES)
180 X(IOCTL_STORAGE_GET_MEDIA_TYPES_EX)
181 X(IOCTL_STORAGE_LOAD_MEDIA)
182 X(IOCTL_STORAGE_MEDIA_REMOVAL)
183 X(IOCTL_STORAGE_RESET_DEVICE)
184 #undef X
186 static const char *iocodex(DWORD code)
188 unsigned int i;
189 static char buffer[25];
190 for(i=0; i<sizeof(iocodextable)/sizeof(struct iocodexs); i++)
191 if (code==iocodextable[i].code)
192 return iocodextable[i].codex;
193 sprintf(buffer, "IOCTL_CODE_%x", (int)code);
194 return buffer;
197 #define INQ_REPLY_LEN 36
198 #define INQ_CMD_LEN 6
200 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
201 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
202 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
203 #define MSF_OF_FRAME(m,fr) {int f=(fr); ((UCHAR *)&(m))[2]=f%CD_FRAMES;f/=CD_FRAMES;((UCHAR *)&(m))[1]=f%CD_SECS;((UCHAR *)&(m))[0]=f/CD_SECS;}
205 /* The documented format of DVD_LAYER_DESCRIPTOR is wrong. Even the format in the
206 * DDK's header is wrong. There are four bytes at the start defined by
207 * MMC-5. The first two are the size of the structure in big-endian order as
208 * defined by MMC-5. The other two are reserved.
210 typedef struct
212 UCHAR DataLength[2];
213 UCHAR Reserved0[2];
214 UCHAR BookVersion : 4;
215 UCHAR BookType : 4;
216 UCHAR MinimumRate : 4;
217 UCHAR DiskSize : 4;
218 UCHAR LayerType : 4;
219 UCHAR TrackPath : 1;
220 UCHAR NumberOfLayers : 2;
221 UCHAR Reserved1 : 1;
222 UCHAR TrackDensity : 4;
223 UCHAR LinearDensity : 4;
224 ULONG StartingDataSector;
225 ULONG EndDataSector;
226 ULONG EndLayerZeroSector;
227 UCHAR Reserved5 : 7;
228 UCHAR BCAFlag : 1;
229 UCHAR Reserved6;
230 } internal_dvd_layer_descriptor;
233 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
234 static NTSTATUS CDROM_GetStatusCode(int);
237 #ifdef linux
239 # ifndef IDE6_MAJOR
240 # define IDE6_MAJOR 88
241 # endif
242 # ifndef IDE7_MAJOR
243 # define IDE7_MAJOR 89
244 # endif
246 # ifdef CDROM_SEND_PACKET
247 /* structure for CDROM_PACKET_COMMAND ioctl */
248 /* not all Linux versions have all the fields, so we define the
249 * structure ourselves to make sure */
250 struct linux_cdrom_generic_command
252 unsigned char cmd[CDROM_PACKET_SIZE];
253 unsigned char *buffer;
254 unsigned int buflen;
255 int stat;
256 struct request_sense *sense;
257 unsigned char data_direction;
258 int quiet;
259 int timeout;
260 void *reserved[1];
262 # endif /* CDROM_SEND_PACKET */
264 #endif /* linux */
266 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
267 * this should be removed when a proper device interface is implemented
269 * (WS) We need this to keep track of current position and to safely
270 * detect media changes. Besides this should provide a great speed up
271 * for toc inquiries.
273 struct cdrom_cache {
274 dev_t device;
275 ino_t inode;
276 char toc_good; /* if false, will reread TOC from disk */
277 CDROM_TOC toc;
278 SUB_Q_CURRENT_POSITION CurrentPosition;
280 /* who has more than 5 cdroms on his/her machine ?? */
281 /* FIXME: this should grow depending on the number of cdroms we install/configure
282 * at startup
284 #define MAX_CACHE_ENTRIES 5
285 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
287 static RTL_CRITICAL_SECTION cache_section;
288 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
290 0, 0, &cache_section,
291 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
292 0, 0, { (DWORD_PTR)(__FILE__ ": cache_section") }
294 static RTL_CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
296 /* Proposed media change function: not really needed at this time */
297 /* This is a 1 or 0 type of function */
298 #if 0
299 static int CDROM_MediaChanged(int dev)
301 int i;
303 struct cdrom_tochdr hdr;
304 struct cdrom_tocentry entry;
306 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
307 return 0;
308 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
309 return 0;
311 if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
312 return 1;
314 for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
316 if (i == hdr.cdth_trk1 + 1)
318 entry.cdte_track = CDROM_LEADOUT;
319 } else {
320 entry.cdte_track = i;
322 entry.cdte_format = CDROM_MSF;
323 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
324 return 0;
325 if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
326 sizeof(struct cdrom_tocentry)) )
327 return 1;
329 return 0;
331 #endif
334 /******************************************************************
335 * get_parent_device
337 * On Mac OS, get the device for the whole disk from a fd that points to a partition.
338 * This is ugly and inefficient, but we have no choice since the partition fd doesn't
339 * support the eject ioctl.
341 #ifdef __APPLE__
342 static NTSTATUS get_parent_device( int fd, char *name, size_t len )
344 NTSTATUS status = STATUS_NO_SUCH_FILE;
345 struct stat st;
346 int i;
347 io_service_t service;
348 CFMutableDictionaryRef dict;
349 CFTypeRef val;
351 if (fstat( fd, &st ) == -1) return FILE_GetNtStatus();
352 if (!S_ISCHR( st.st_mode )) return STATUS_OBJECT_TYPE_MISMATCH;
354 /* create a dictionary with the right major/minor numbers */
356 if (!(dict = IOServiceMatching( kIOMediaClass ))) return STATUS_NO_MEMORY;
358 i = major( st.st_rdev );
359 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
360 CFDictionaryAddValue( dict, CFSTR( "BSD Major" ), val );
361 CFRelease( val );
363 i = minor( st.st_rdev );
364 val = CFNumberCreate( NULL, kCFNumberIntType, &i );
365 CFDictionaryAddValue( dict, CFSTR( "BSD Minor" ), val );
366 CFRelease( val );
368 CFDictionaryAddValue( dict, CFSTR("Removable"), kCFBooleanTrue );
370 service = IOServiceGetMatchingService( kIOMasterPortDefault, dict );
372 /* now look for the parent that has the "Whole" attribute set to TRUE */
374 while (service)
376 io_service_t parent = 0;
377 CFBooleanRef whole;
378 CFStringRef str;
379 int ok;
381 if (!IOObjectConformsTo( service, kIOMediaClass ))
382 goto next;
383 if (!(whole = IORegistryEntryCreateCFProperty( service, CFSTR("Whole"), NULL, 0 )))
384 goto next;
385 ok = (whole == kCFBooleanTrue);
386 CFRelease( whole );
387 if (!ok) goto next;
389 if ((str = IORegistryEntryCreateCFProperty( service, CFSTR("BSD Name"), NULL, 0 )))
391 strcpy( name, "/dev/r" );
392 CFStringGetCString( str, name + 6, len - 6, kCFStringEncodingUTF8 );
393 CFRelease( str );
394 status = STATUS_SUCCESS;
396 IOObjectRelease( service );
397 break;
399 next:
400 IORegistryEntryGetParentEntry( service, kIOServicePlane, &parent );
401 IOObjectRelease( service );
402 service = parent;
404 return status;
406 #endif
409 /******************************************************************
410 * CDROM_SyncCache [internal]
412 * Read the TOC in and store it in the cdrom_cache structure.
413 * Further requests for the TOC will be copied from the cache
414 * unless certain events like disk ejection is detected, in which
415 * case the cache will be cleared, causing it to be resynced.
416 * The cache section must be held by caller.
418 static NTSTATUS CDROM_SyncCache(int dev, int fd)
420 #ifdef linux
421 int i, tsz;
422 struct cdrom_tochdr hdr;
423 struct cdrom_tocentry entry;
425 CDROM_TOC *toc = &cdrom_cache[dev].toc;
426 cdrom_cache[dev].toc_good = 0;
428 if (ioctl(fd, CDROMREADTOCHDR, &hdr) == -1)
430 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
431 return FILE_GetNtStatus();
434 toc->FirstTrack = hdr.cdth_trk0;
435 toc->LastTrack = hdr.cdth_trk1;
436 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
437 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
438 toc->Length[0] = tsz >> 8;
439 toc->Length[1] = tsz;
441 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
443 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
445 if (i == toc->LastTrack + 1)
446 entry.cdte_track = CDROM_LEADOUT;
447 else
448 entry.cdte_track = i;
449 entry.cdte_format = CDROM_MSF;
450 if (ioctl(fd, CDROMREADTOCENTRY, &entry) == -1)
452 WARN("error read entry (%s)\n", strerror(errno));
453 return FILE_GetNtStatus();
455 toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
456 toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
457 /* marking last track with leadout value as index */
458 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
459 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
460 toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
461 toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
462 toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
464 cdrom_cache[dev].toc_good = 1;
465 return STATUS_SUCCESS;
467 #elif defined(__FreeBSD__) || defined(__NetBSD__)
469 int i, tsz;
470 struct ioc_toc_header hdr;
471 struct ioc_read_toc_entry entry;
472 struct cd_toc_entry toc_buffer;
474 CDROM_TOC *toc = &cdrom_cache[dev].toc;
475 cdrom_cache[dev].toc_good = 0;
477 if (ioctl(fd, CDIOREADTOCHEADER, &hdr) == -1)
479 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
480 return FILE_GetNtStatus();
482 toc->FirstTrack = hdr.starting_track;
483 toc->LastTrack = hdr.ending_track;
484 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
485 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
486 toc->Length[0] = tsz >> 8;
487 toc->Length[1] = tsz;
489 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
491 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
493 if (i == toc->LastTrack + 1)
495 #define LEADOUT 0xaa
496 entry.starting_track = LEADOUT;
497 } else {
498 entry.starting_track = i;
500 memset(&toc_buffer, 0, sizeof(toc_buffer));
501 entry.address_format = CD_MSF_FORMAT;
502 entry.data_len = sizeof(toc_buffer);
503 entry.data = &toc_buffer;
504 if (ioctl(fd, CDIOREADTOCENTRYS, &entry) == -1)
506 WARN("error read entry (%s)\n", strerror(errno));
507 return FILE_GetNtStatus();
509 toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
510 toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
511 /* marking last track with leadout value as index */
512 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
513 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
514 toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
515 toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
516 toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
518 cdrom_cache[dev].toc_good = 1;
519 return STATUS_SUCCESS;
521 #elif defined(__APPLE__)
522 int i;
523 dk_cd_read_toc_t hdr;
524 CDROM_TOC *toc = &cdrom_cache[dev].toc;
525 cdrom_cache[dev].toc_good = 0;
527 memset( &hdr, 0, sizeof(hdr) );
528 hdr.buffer = toc;
529 hdr.bufferLength = sizeof(*toc);
530 if (ioctl(fd, DKIOCCDREADTOC, &hdr) == -1)
532 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
533 return FILE_GetNtStatus();
535 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
537 /* convert address format */
538 TRACK_DATA *data = &toc->TrackData[i - toc->FirstTrack];
539 DWORD frame = (((DWORD)data->Address[0] << 24) | ((DWORD)data->Address[1] << 16) |
540 ((DWORD)data->Address[2] << 8) | data->Address[3]);
541 MSF_OF_FRAME( data->Address[1], frame );
542 data->Address[0] = 0;
545 cdrom_cache[dev].toc_good = 1;
546 return STATUS_SUCCESS;
547 #else
548 return STATUS_NOT_SUPPORTED;
549 #endif
552 static void CDROM_ClearCacheEntry(int dev)
554 RtlEnterCriticalSection( &cache_section );
555 cdrom_cache[dev].toc_good = 0;
556 RtlLeaveCriticalSection( &cache_section );
561 /******************************************************************
562 * CDROM_GetInterfaceInfo
564 * Determines the ide interface (the number after the ide), and the
565 * number of the device on that interface for ide cdroms (*iface <= 1).
566 * Determines the scsi information for scsi cdroms (*iface >= 2).
567 * Returns false if the info cannot not be obtained.
569 static int CDROM_GetInterfaceInfo(int fd, UCHAR* iface, UCHAR* port, UCHAR* device, UCHAR* lun)
571 #if defined(linux)
572 struct stat st;
573 if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode)) return 0;
574 *port = 0;
575 *iface = 0;
576 *device = 0;
577 *lun = 0;
578 switch (major(st.st_rdev)) {
579 case IDE0_MAJOR: *iface = 0; break;
580 case IDE1_MAJOR: *iface = 1; break;
581 case IDE2_MAJOR: *iface = 2; break;
582 case IDE3_MAJOR: *iface = 3; break;
583 case IDE4_MAJOR: *iface = 4; break;
584 case IDE5_MAJOR: *iface = 5; break;
585 case IDE6_MAJOR: *iface = 6; break;
586 case IDE7_MAJOR: *iface = 7; break;
587 default: *port = 1; break;
590 if (*port == 0)
591 *device = (minor(st.st_rdev) >> 6);
592 else
594 #ifdef SCSI_IOCTL_GET_IDLUN
595 UINT32 idlun[2];
596 if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, idlun) != -1)
598 *port = (idlun[0] >> 24) & 0xff;
599 *iface = ((idlun[0] >> 16) & 0xff) + 2;
600 *device = idlun[0] & 0xff;
601 *lun = (idlun[0] >> 8) & 0xff;
603 else
604 #endif
606 WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
607 return 0;
610 return 1;
611 #elif defined(__NetBSD__)
612 struct scsi_addr addr;
613 if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
615 switch (addr.type)
617 case TYPE_SCSI:
618 *port = 1;
619 *iface = addr.addr.scsi.scbus;
620 *device = addr.addr.scsi.target;
621 *lun = addr.addr.scsi.lun;
622 return 1;
623 case TYPE_ATAPI:
624 *port = 0;
625 *iface = addr.addr.atapi.atbus;
626 *device = addr.addr.atapi.drive;
627 *lun = 0;
628 return 1;
631 return 0;
632 #elif defined(__FreeBSD__)
633 FIXME("not implemented for BSD\n");
634 return 0;
635 #else
636 FIXME("not implemented for nonlinux\n");
637 return 0;
638 #endif
642 /******************************************************************
643 * CDROM_Open
646 static NTSTATUS CDROM_Open(int fd, int* dev)
648 struct stat st;
649 NTSTATUS ret = STATUS_SUCCESS;
650 int empty = -1;
652 fstat(fd, &st);
654 RtlEnterCriticalSection( &cache_section );
655 for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
657 if (empty == -1 &&
658 cdrom_cache[*dev].device == 0 &&
659 cdrom_cache[*dev].inode == 0)
660 empty = *dev;
661 else if (cdrom_cache[*dev].device == st.st_dev &&
662 cdrom_cache[*dev].inode == st.st_ino)
663 break;
665 if (*dev == MAX_CACHE_ENTRIES)
667 if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
668 else
670 *dev = empty;
671 cdrom_cache[*dev].device = st.st_dev;
672 cdrom_cache[*dev].inode = st.st_ino;
675 RtlLeaveCriticalSection( &cache_section );
677 TRACE("%d, %d\n", *dev, fd);
678 return ret;
681 /******************************************************************
682 * CDROM_GetStatusCode
686 static NTSTATUS CDROM_GetStatusCode(int io)
688 if (io == 0) return STATUS_SUCCESS;
689 return FILE_GetNtStatus();
692 /******************************************************************
693 * CDROM_GetControl
696 static NTSTATUS CDROM_GetControl(int dev, int fd, CDROM_AUDIO_CONTROL* cac)
698 #ifdef __APPLE__
699 uint16_t speed;
700 int io = ioctl( fd, DKIOCCDGETSPEED, &speed );
701 if (io != 0) return CDROM_GetStatusCode( io );
702 /* DKIOCCDGETSPEED returns the speed in kilobytes per second,
703 * so convert to logical blocks (assumed to be ~2 KB).
705 cac->LogicalBlocksPerSecond = speed/2;
706 #else
707 cac->LogicalBlocksPerSecond = 1; /* FIXME */
708 #endif
709 cac->LbaFormat = 0; /* FIXME */
710 return STATUS_NOT_SUPPORTED;
713 /******************************************************************
714 * CDROM_GetDeviceNumber
717 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
719 FIXME( "stub\n" );
720 devnum->DeviceType = FILE_DEVICE_DISK;
721 devnum->DeviceNumber = 1;
722 devnum->PartitionNumber = 1;
723 return STATUS_SUCCESS;
726 /******************************************************************
727 * CDROM_GetDriveGeometry
730 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
732 CDROM_TOC toc;
733 NTSTATUS ret = 0;
734 int fsize = 0;
736 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
738 fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
739 - FRAME_OF_TOC(toc, 1); /* Total size in frames */
741 dg->Cylinders.u.LowPart = fsize / (64 * 32);
742 dg->Cylinders.u.HighPart = 0;
743 dg->MediaType = RemovableMedia;
744 dg->TracksPerCylinder = 64;
745 dg->SectorsPerTrack = 32;
746 dg->BytesPerSector= 2048;
747 return ret;
750 /******************************************************************
751 * CDROM_GetMediaType
754 static NTSTATUS CDROM_GetMediaType(int dev, GET_MEDIA_TYPES* medtype)
756 FIXME(": faking success\n");
757 medtype->DeviceType = FILE_DEVICE_CD_ROM;
758 medtype->MediaInfoCount = 0;
759 return STATUS_SUCCESS;
762 /**************************************************************************
763 * CDROM_Reset [internal]
765 static NTSTATUS CDROM_ResetAudio(int fd)
767 #if defined(linux)
768 return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
769 #elif defined(__FreeBSD__) || defined(__NetBSD__)
770 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
771 #else
772 return STATUS_NOT_SUPPORTED;
773 #endif
776 /******************************************************************
777 * CDROM_SetTray
781 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
783 #if defined(linux)
784 return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
785 #elif defined(__FreeBSD__) || defined(__NetBSD__)
786 return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
787 (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
788 (ioctl(fd, CDIOCPREVENT, NULL)));
789 #elif defined(__APPLE__)
790 if (doEject) return CDROM_GetStatusCode( ioctl( fd, DKIOCEJECT, NULL ) );
791 else return STATUS_NOT_SUPPORTED;
792 #else
793 return STATUS_NOT_SUPPORTED;
794 #endif
797 /******************************************************************
798 * CDROM_ControlEjection
802 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
804 #if defined(linux)
805 return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
806 #elif defined(__FreeBSD__) || defined(__NetBSD__)
807 return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
808 #else
809 return STATUS_NOT_SUPPORTED;
810 #endif
813 /******************************************************************
814 * CDROM_ReadTOC
818 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
820 NTSTATUS ret = STATUS_NOT_SUPPORTED;
822 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
823 return STATUS_INVALID_PARAMETER;
825 RtlEnterCriticalSection( &cache_section );
826 if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
828 *toc = cdrom_cache[dev].toc;
829 ret = STATUS_SUCCESS;
831 RtlLeaveCriticalSection( &cache_section );
832 return ret;
835 /******************************************************************
836 * CDROM_GetDiskData
840 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
842 CDROM_TOC toc;
843 NTSTATUS ret;
844 int i;
846 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
847 data->DiskData = 0;
848 for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
849 if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
850 data->DiskData |= CDROM_DISK_DATA_TRACK;
851 else
852 data->DiskData |= CDROM_DISK_AUDIO_TRACK;
854 return STATUS_SUCCESS;
857 /******************************************************************
858 * CDROM_ReadQChannel
862 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
863 SUB_Q_CHANNEL_DATA* data)
865 NTSTATUS ret = STATUS_NOT_SUPPORTED;
866 #ifdef linux
867 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
868 int io;
869 struct cdrom_subchnl sc;
870 sc.cdsc_format = CDROM_MSF;
872 io = ioctl(fd, CDROMSUBCHNL, &sc);
873 if (io == -1)
875 TRACE("opened or no_media (%s)!\n", strerror(errno));
876 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
877 CDROM_ClearCacheEntry(dev);
878 goto end;
881 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
883 switch (sc.cdsc_audiostatus) {
884 case CDROM_AUDIO_INVALID:
885 CDROM_ClearCacheEntry(dev);
886 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
887 break;
888 case CDROM_AUDIO_NO_STATUS:
889 CDROM_ClearCacheEntry(dev);
890 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
891 break;
892 case CDROM_AUDIO_PLAY:
893 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
894 break;
895 case CDROM_AUDIO_PAUSED:
896 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
897 break;
898 case CDROM_AUDIO_COMPLETED:
899 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
900 break;
901 case CDROM_AUDIO_ERROR:
902 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
903 break;
904 default:
905 TRACE("status=%02X !\n", sc.cdsc_audiostatus);
906 break;
908 switch (fmt->Format)
910 case IOCTL_CDROM_CURRENT_POSITION:
911 RtlEnterCriticalSection( &cache_section );
912 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
913 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
914 data->CurrentPosition.Control = sc.cdsc_ctrl;
915 data->CurrentPosition.ADR = sc.cdsc_adr;
916 data->CurrentPosition.TrackNumber = sc.cdsc_trk;
917 data->CurrentPosition.IndexNumber = sc.cdsc_ind;
919 data->CurrentPosition.AbsoluteAddress[0] = 0;
920 data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute;
921 data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
922 data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
924 data->CurrentPosition.TrackRelativeAddress[0] = 0;
925 data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute;
926 data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
927 data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
929 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
931 else /* not playing */
933 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
934 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
936 RtlLeaveCriticalSection( &cache_section );
937 break;
938 case IOCTL_CDROM_MEDIA_CATALOG:
939 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
941 struct cdrom_mcn mcn;
942 if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
944 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
945 data->MediaCatalog.Mcval = 0; /* FIXME */
946 memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
947 data->MediaCatalog.MediaCatalog[14] = 0;
949 break;
950 case IOCTL_CDROM_TRACK_ISRC:
951 FIXME("TrackIsrc: NIY on linux\n");
952 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
953 data->TrackIsrc.Tcval = 0;
954 io = 0;
955 break;
958 end:
959 ret = CDROM_GetStatusCode(io);
960 #elif defined(__FreeBSD__) || defined(__NetBSD__)
961 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
962 int io;
963 struct ioc_read_subchannel read_sc;
964 struct cd_sub_channel_info sc;
966 read_sc.address_format = CD_MSF_FORMAT;
967 read_sc.track = 0;
968 read_sc.data_len = sizeof(sc);
969 read_sc.data = &sc;
970 switch (fmt->Format)
972 case IOCTL_CDROM_CURRENT_POSITION:
973 read_sc.data_format = CD_CURRENT_POSITION;
974 break;
975 case IOCTL_CDROM_MEDIA_CATALOG:
976 read_sc.data_format = CD_MEDIA_CATALOG;
977 break;
978 case IOCTL_CDROM_TRACK_ISRC:
979 read_sc.data_format = CD_TRACK_INFO;
980 sc.what.track_info.track_number = data->TrackIsrc.Track;
981 break;
983 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
984 if (io == -1)
986 TRACE("opened or no_media (%s)!\n", strerror(errno));
987 CDROM_ClearCacheEntry(dev);
988 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
989 goto end;
992 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
994 switch (sc.header.audio_status) {
995 case CD_AS_AUDIO_INVALID:
996 CDROM_ClearCacheEntry(dev);
997 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
998 break;
999 case CD_AS_NO_STATUS:
1000 CDROM_ClearCacheEntry(dev);
1001 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1002 break;
1003 case CD_AS_PLAY_IN_PROGRESS:
1004 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
1005 break;
1006 case CD_AS_PLAY_PAUSED:
1007 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
1008 break;
1009 case CD_AS_PLAY_COMPLETED:
1010 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
1011 break;
1012 case CD_AS_PLAY_ERROR:
1013 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
1014 break;
1015 default:
1016 TRACE("status=%02X !\n", sc.header.audio_status);
1018 switch (fmt->Format)
1020 case IOCTL_CDROM_CURRENT_POSITION:
1021 RtlEnterCriticalSection( &cache_section );
1022 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
1023 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1024 data->CurrentPosition.Control = sc.what.position.control;
1025 data->CurrentPosition.ADR = sc.what.position.addr_type;
1026 data->CurrentPosition.TrackNumber = sc.what.position.track_number;
1027 data->CurrentPosition.IndexNumber = sc.what.position.index_number;
1029 data->CurrentPosition.AbsoluteAddress[0] = 0;
1030 data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
1031 data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
1032 data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
1033 data->CurrentPosition.TrackRelativeAddress[0] = 0;
1034 data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
1035 data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
1036 data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
1037 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
1039 else { /* not playing */
1040 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
1041 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
1043 RtlLeaveCriticalSection( &cache_section );
1044 break;
1045 case IOCTL_CDROM_MEDIA_CATALOG:
1046 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
1047 data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
1048 memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
1049 break;
1050 case IOCTL_CDROM_TRACK_ISRC:
1051 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
1052 data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
1053 memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
1054 break;
1057 end:
1058 ret = CDROM_GetStatusCode(io);
1059 #elif defined(__APPLE__)
1060 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
1061 int io = 0;
1062 union
1064 dk_cd_read_mcn_t mcn;
1065 dk_cd_read_isrc_t isrc;
1066 } ioc;
1067 /* We need IOCDAudioControl for IOCTL_CDROM_CURRENT_POSITION */
1068 if (fmt->Format == IOCTL_CDROM_CURRENT_POSITION)
1070 FIXME("NIY\n");
1071 return STATUS_NOT_SUPPORTED;
1073 /* No IOCDAudioControl support; just set the audio status to none */
1074 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
1075 switch(fmt->Format)
1077 case IOCTL_CDROM_MEDIA_CATALOG:
1078 if ((io = ioctl(fd, DKIOCCDREADMCN, &ioc.mcn)) == -1) break;
1079 memcpy(data->MediaCatalog.MediaCatalog, ioc.mcn.mcn, kCDMCNMaxLength);
1080 data->MediaCatalog.Mcval = 1;
1081 break;
1082 case IOCTL_CDROM_TRACK_ISRC:
1083 ioc.isrc.track = fmt->Track;
1084 if ((io = ioctl(fd, DKIOCCDREADISRC, &ioc.isrc)) == -1) break;
1085 memcpy(data->TrackIsrc.TrackIsrc, ioc.isrc.isrc, kCDISRCMaxLength);
1086 data->TrackIsrc.Tcval = 1;
1087 data->TrackIsrc.Track = ioc.isrc.track;
1089 ret = CDROM_GetStatusCode(io);
1090 #endif
1091 return ret;
1094 /******************************************************************
1095 * CDROM_Verify
1096 * Implements: IOCTL_STORAGE_CHECK_VERIFY
1097 * IOCTL_CDROM_CHECK_VERIFY
1098 * IOCTL_DISK_CHECK_VERIFY
1101 static NTSTATUS CDROM_Verify(int dev, int fd)
1103 #if defined(linux)
1104 int ret;
1106 ret = ioctl(fd, CDROM_DRIVE_STATUS, NULL);
1107 if(ret == -1) {
1108 TRACE("ioctl CDROM_DRIVE_STATUS failed(%s)!\n", strerror(errno));
1109 return CDROM_GetStatusCode(ret);
1112 if(ret == CDS_DISC_OK)
1113 return STATUS_SUCCESS;
1114 else
1115 return STATUS_NO_MEDIA_IN_DEVICE;
1116 #elif defined(__FreeBSD__)
1117 int ret;
1118 ret = ioctl(fd, CDIOCSTART, NULL);
1119 if(ret == 0)
1120 return STATUS_SUCCESS;
1121 else
1122 return STATUS_NO_MEDIA_IN_DEVICE;
1123 #else
1124 FIXME("not implemented for non-linux\n");
1125 return STATUS_NOT_SUPPORTED;
1126 #endif
1129 /******************************************************************
1130 * CDROM_PlayAudioMSF
1134 static NTSTATUS CDROM_PlayAudioMSF(int fd, const CDROM_PLAY_AUDIO_MSF* audio_msf)
1136 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1137 #ifdef linux
1138 struct cdrom_msf msf;
1139 int io;
1141 msf.cdmsf_min0 = audio_msf->StartingM;
1142 msf.cdmsf_sec0 = audio_msf->StartingS;
1143 msf.cdmsf_frame0 = audio_msf->StartingF;
1144 msf.cdmsf_min1 = audio_msf->EndingM;
1145 msf.cdmsf_sec1 = audio_msf->EndingS;
1146 msf.cdmsf_frame1 = audio_msf->EndingF;
1148 io = ioctl(fd, CDROMSTART);
1149 if (io == -1)
1151 WARN("motor doesn't start !\n");
1152 goto end;
1154 io = ioctl(fd, CDROMPLAYMSF, &msf);
1155 if (io == -1)
1157 WARN("device doesn't play !\n");
1158 goto end;
1160 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1161 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
1162 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
1163 end:
1164 ret = CDROM_GetStatusCode(io);
1165 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1166 struct ioc_play_msf msf;
1167 int io;
1169 msf.start_m = audio_msf->StartingM;
1170 msf.start_s = audio_msf->StartingS;
1171 msf.start_f = audio_msf->StartingF;
1172 msf.end_m = audio_msf->EndingM;
1173 msf.end_s = audio_msf->EndingS;
1174 msf.end_f = audio_msf->EndingF;
1176 io = ioctl(fd, CDIOCSTART, NULL);
1177 if (io == -1)
1179 WARN("motor doesn't start !\n");
1180 goto end;
1182 io = ioctl(fd, CDIOCPLAYMSF, &msf);
1183 if (io == -1)
1185 WARN("device doesn't play !\n");
1186 goto end;
1188 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1189 msf.start_m, msf.start_s, msf.start_f,
1190 msf.end_m, msf.end_s, msf.end_f);
1191 end:
1192 ret = CDROM_GetStatusCode(io);
1193 #endif
1194 return ret;
1197 /******************************************************************
1198 * CDROM_SeekAudioMSF
1202 static NTSTATUS CDROM_SeekAudioMSF(int dev, int fd, const CDROM_SEEK_AUDIO_MSF* audio_msf)
1204 CDROM_TOC toc;
1205 int i, io, frame;
1206 SUB_Q_CURRENT_POSITION *cp;
1207 #if defined(linux)
1208 struct cdrom_msf0 msf;
1209 struct cdrom_subchnl sc;
1210 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1211 struct ioc_play_msf msf;
1212 struct ioc_read_subchannel read_sc;
1213 struct cd_sub_channel_info sc;
1214 int final_frame;
1215 #endif
1217 /* Use the information on the TOC to compute the new current
1218 * position, which is shadowed on the cache. [Portable]. */
1219 frame = FRAME_OF_MSF(*audio_msf);
1221 if ((io = CDROM_ReadTOC(dev, fd, &toc)) != 0) return io;
1223 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
1224 if (FRAME_OF_TOC(toc,i)>frame) break;
1225 if (i <= toc.FirstTrack || i > toc.LastTrack+1)
1226 return STATUS_INVALID_PARAMETER;
1227 i--;
1228 RtlEnterCriticalSection( &cache_section );
1229 cp = &cdrom_cache[dev].CurrentPosition;
1230 cp->FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1231 cp->Control = toc.TrackData[i-toc.FirstTrack].Control;
1232 cp->ADR = toc.TrackData[i-toc.FirstTrack].Adr;
1233 cp->TrackNumber = toc.TrackData[i-toc.FirstTrack].TrackNumber;
1234 cp->IndexNumber = 0; /* FIXME: where do they keep these? */
1235 cp->AbsoluteAddress[0] = 0;
1236 cp->AbsoluteAddress[1] = toc.TrackData[i-toc.FirstTrack].Address[1];
1237 cp->AbsoluteAddress[2] = toc.TrackData[i-toc.FirstTrack].Address[2];
1238 cp->AbsoluteAddress[3] = toc.TrackData[i-toc.FirstTrack].Address[3];
1239 frame -= FRAME_OF_TOC(toc,i);
1240 cp->TrackRelativeAddress[0] = 0;
1241 MSF_OF_FRAME(cp->TrackRelativeAddress[1], frame);
1242 RtlLeaveCriticalSection( &cache_section );
1244 /* If playing, then issue a seek command, otherwise do nothing */
1245 #ifdef linux
1246 sc.cdsc_format = CDROM_MSF;
1248 io = ioctl(fd, CDROMSUBCHNL, &sc);
1249 if (io == -1)
1251 TRACE("opened or no_media (%s)!\n", strerror(errno));
1252 CDROM_ClearCacheEntry(dev);
1253 return CDROM_GetStatusCode(io);
1255 if (sc.cdsc_audiostatus==CDROM_AUDIO_PLAY)
1257 msf.minute = audio_msf->M;
1258 msf.second = audio_msf->S;
1259 msf.frame = audio_msf->F;
1260 return CDROM_GetStatusCode(ioctl(fd, CDROMSEEK, &msf));
1262 return STATUS_SUCCESS;
1263 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1264 read_sc.address_format = CD_MSF_FORMAT;
1265 read_sc.track = 0;
1266 read_sc.data_len = sizeof(sc);
1267 read_sc.data = &sc;
1268 read_sc.data_format = CD_CURRENT_POSITION;
1270 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1271 if (io == -1)
1273 TRACE("opened or no_media (%s)!\n", strerror(errno));
1274 CDROM_ClearCacheEntry(dev);
1275 return CDROM_GetStatusCode(io);
1277 if (sc.header.audio_status==CD_AS_PLAY_IN_PROGRESS)
1280 msf.start_m = audio_msf->M;
1281 msf.start_s = audio_msf->S;
1282 msf.start_f = audio_msf->F;
1283 final_frame = FRAME_OF_TOC(toc,toc.LastTrack+1)-1;
1284 MSF_OF_FRAME(msf.end_m, final_frame);
1286 return CDROM_GetStatusCode(ioctl(fd, CDIOCPLAYMSF, &msf));
1288 return STATUS_SUCCESS;
1289 #else
1290 return STATUS_NOT_SUPPORTED;
1291 #endif
1294 /******************************************************************
1295 * CDROM_PauseAudio
1299 static NTSTATUS CDROM_PauseAudio(int fd)
1301 #if defined(linux)
1302 return CDROM_GetStatusCode(ioctl(fd, CDROMPAUSE));
1303 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1304 return CDROM_GetStatusCode(ioctl(fd, CDIOCPAUSE, NULL));
1305 #else
1306 return STATUS_NOT_SUPPORTED;
1307 #endif
1310 /******************************************************************
1311 * CDROM_ResumeAudio
1315 static NTSTATUS CDROM_ResumeAudio(int fd)
1317 #if defined(linux)
1318 return CDROM_GetStatusCode(ioctl(fd, CDROMRESUME));
1319 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1320 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESUME, NULL));
1321 #else
1322 return STATUS_NOT_SUPPORTED;
1323 #endif
1326 /******************************************************************
1327 * CDROM_StopAudio
1331 static NTSTATUS CDROM_StopAudio(int fd)
1333 #if defined(linux)
1334 return CDROM_GetStatusCode(ioctl(fd, CDROMSTOP));
1335 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1336 return CDROM_GetStatusCode(ioctl(fd, CDIOCSTOP, NULL));
1337 #else
1338 return STATUS_NOT_SUPPORTED;
1339 #endif
1342 /******************************************************************
1343 * CDROM_GetVolume
1347 static NTSTATUS CDROM_GetVolume(int fd, VOLUME_CONTROL* vc)
1349 #if defined(linux)
1350 struct cdrom_volctrl volc;
1351 int io;
1353 io = ioctl(fd, CDROMVOLREAD, &volc);
1354 if (io != -1)
1356 vc->PortVolume[0] = volc.channel0;
1357 vc->PortVolume[1] = volc.channel1;
1358 vc->PortVolume[2] = volc.channel2;
1359 vc->PortVolume[3] = volc.channel3;
1361 return CDROM_GetStatusCode(io);
1362 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1363 struct ioc_vol volc;
1364 int io;
1366 io = ioctl(fd, CDIOCGETVOL, &volc);
1367 if (io != -1)
1369 vc->PortVolume[0] = volc.vol[0];
1370 vc->PortVolume[1] = volc.vol[1];
1371 vc->PortVolume[2] = volc.vol[2];
1372 vc->PortVolume[3] = volc.vol[3];
1374 return CDROM_GetStatusCode(io);
1375 #else
1376 return STATUS_NOT_SUPPORTED;
1377 #endif
1380 /******************************************************************
1381 * CDROM_SetVolume
1385 static NTSTATUS CDROM_SetVolume(int fd, const VOLUME_CONTROL* vc)
1387 #if defined(linux)
1388 struct cdrom_volctrl volc;
1390 volc.channel0 = vc->PortVolume[0];
1391 volc.channel1 = vc->PortVolume[1];
1392 volc.channel2 = vc->PortVolume[2];
1393 volc.channel3 = vc->PortVolume[3];
1395 return CDROM_GetStatusCode(ioctl(fd, CDROMVOLCTRL, &volc));
1396 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1397 struct ioc_vol volc;
1399 volc.vol[0] = vc->PortVolume[0];
1400 volc.vol[1] = vc->PortVolume[1];
1401 volc.vol[2] = vc->PortVolume[2];
1402 volc.vol[3] = vc->PortVolume[3];
1404 return CDROM_GetStatusCode(ioctl(fd, CDIOCSETVOL, &volc));
1405 #else
1406 return STATUS_NOT_SUPPORTED;
1407 #endif
1410 /******************************************************************
1411 * CDROM_RawRead
1413 * Some features of this IOCTL are rather poorly documented and
1414 * not really intuitive either:
1416 * 1. Although the DiskOffset parameter is meant to be a
1417 * byte offset into the disk, it is in fact the sector
1418 * number multiplied by 2048 regardless of the actual
1419 * sector size.
1421 * 2. The least significant 11 bits of DiskOffset are ignored.
1423 * 3. The TrackMode parameter has no effect on the sector
1424 * size. The entire raw sector (i.e. 2352 bytes of data)
1425 * is always returned. IMO the TrackMode is only used
1426 * to check the correct sector type.
1429 static NTSTATUS CDROM_RawRead(int fd, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
1431 int ret = STATUS_NOT_SUPPORTED;
1432 int io = -1;
1434 TRACE("RAW_READ_INFO: DiskOffset=%i,%i SectorCount=%i TrackMode=%i\n buffer=%p len=%i sz=%p\n",
1435 raw->DiskOffset.u.HighPart, raw->DiskOffset.u.LowPart, raw->SectorCount, raw->TrackMode, buffer, len, sz);
1437 if (len < raw->SectorCount * 2352) return STATUS_BUFFER_TOO_SMALL;
1439 #if defined(linux)
1440 if (raw->DiskOffset.u.HighPart & ~2047) {
1441 WARN("DiskOffset points to a sector >= 2**32\n");
1442 return ret;
1445 switch (raw->TrackMode)
1447 case YellowMode2:
1448 case XAForm2:
1450 DWORD lba = raw->DiskOffset.QuadPart >> 11;
1451 struct cdrom_msf* msf;
1452 PBYTE *bp; /* current buffer pointer */
1453 DWORD i;
1455 if ((lba + raw->SectorCount) >
1456 ((1 << 8*sizeof(msf->cdmsf_min0)) * CD_SECS * CD_FRAMES
1457 - CD_MSF_OFFSET)) {
1458 WARN("DiskOffset not accessible with MSF\n");
1459 return ret;
1462 /* Linux reads only one sector at a time.
1463 * ioctl CDROMREADRAW takes struct cdrom_msf as an argument
1464 * on the contrary to what header comments state.
1466 lba += CD_MSF_OFFSET;
1467 for (i = 0, bp = buffer; i < raw->SectorCount;
1468 i++, lba++, bp += 2352)
1470 msf = (struct cdrom_msf*)bp;
1471 msf->cdmsf_min0 = lba / CD_FRAMES / CD_SECS;
1472 msf->cdmsf_sec0 = lba / CD_FRAMES % CD_SECS;
1473 msf->cdmsf_frame0 = lba % CD_FRAMES;
1474 io = ioctl(fd, CDROMREADRAW, msf);
1475 if (io != 0)
1477 *sz = 2352 * i;
1478 return CDROM_GetStatusCode(io);
1481 break;
1484 case CDDA:
1486 struct cdrom_read_audio cdra;
1488 cdra.addr.lba = raw->DiskOffset.QuadPart >> 11;
1489 TRACE("reading at %u\n", cdra.addr.lba);
1490 cdra.addr_format = CDROM_LBA;
1491 cdra.nframes = raw->SectorCount;
1492 cdra.buf = buffer;
1493 io = ioctl(fd, CDROMREADAUDIO, &cdra);
1494 break;
1497 default:
1498 FIXME("NIY: %d\n", raw->TrackMode);
1499 return STATUS_INVALID_PARAMETER;
1501 #elif defined(__APPLE__)
1502 switch (raw->TrackMode)
1504 case YellowMode2:
1506 /* Mac OS, on the other hand, DOES read only one part of the sector
1507 * at a time. Therefore, we have to read each part of the sector, in
1508 * order, to get the whole raw sector in.
1509 * This means that we have to read each sector one at a time, as on
1510 * Linux.
1512 dk_cd_read_t cdrd;
1513 UInt64 lba = raw->DiskOffset.QuadPart >> 11;
1514 PBYTE bp;
1515 int i;
1517 for (i = 0, bp = buffer; i < raw->SectorCount;
1518 i++, lba++, bp += kCDSectorSizeWhole)
1520 cdrd.offset = lba * kCDSectorSizeWhole;
1521 cdrd.sectorType = kCDSectorTypeMode2;
1523 /* First, the sync area */
1524 cdrd.sectorArea = kCDSectorAreaSync;
1525 cdrd.buffer = bp;
1526 cdrd.bufferLength = 12;
1527 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1528 if (io != 0)
1530 *sz = kCDSectorSizeWhole * i;
1531 return CDROM_GetStatusCode(io);
1534 /* Then the header */
1535 cdrd.offset += 12;
1536 cdrd.sectorArea = kCDSectorAreaHeader;
1537 cdrd.buffer = (PBYTE)cdrd.buffer + 12;
1538 cdrd.bufferLength = 4;
1539 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1540 if (io != 0)
1542 *sz = kCDSectorSizeWhole * i + 12;
1543 return CDROM_GetStatusCode(io);
1546 /* And finally the sector proper */
1547 cdrd.offset += 4;
1548 cdrd.sectorArea = kCDSectorAreaUser;
1549 cdrd.buffer = (PBYTE)cdrd.buffer + 4;
1550 cdrd.bufferLength = kCDSectorSizeMode2;
1551 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1552 if (io != 0)
1554 *sz = kCDSectorSizeWhole * i + 16;
1555 return CDROM_GetStatusCode(io);
1559 break;
1562 case XAForm2:
1564 /* Same here */
1565 dk_cd_read_t cdrd;
1566 UInt64 lba = raw->DiskOffset.QuadPart >> 11;
1567 PBYTE bp;
1568 int i;
1570 for (i = 0, bp = buffer; i < raw->SectorCount;
1571 i++, lba++, bp += kCDSectorSizeWhole)
1573 cdrd.offset = lba * kCDSectorSizeWhole;
1574 cdrd.sectorType = kCDSectorTypeMode2Form2;
1576 /* First, the sync area */
1577 cdrd.sectorArea = kCDSectorAreaSync;
1578 cdrd.buffer = bp;
1579 cdrd.bufferLength = 12;
1580 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1581 if (io != 0)
1583 *sz = kCDSectorSizeWhole * i;
1584 return CDROM_GetStatusCode(io);
1587 /* Then the header */
1588 cdrd.offset += 12;
1589 cdrd.sectorArea = kCDSectorAreaHeader;
1590 cdrd.buffer = (PBYTE)cdrd.buffer + 12;
1591 cdrd.bufferLength = 4;
1592 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1593 if (io != 0)
1595 *sz = kCDSectorSizeWhole * i + 12;
1596 return CDROM_GetStatusCode(io);
1599 /* And the sub-header */
1600 cdrd.offset += 4;
1601 cdrd.sectorArea = kCDSectorAreaSubHeader;
1602 cdrd.buffer = (PBYTE)cdrd.buffer + 4;
1603 cdrd.bufferLength = 8;
1604 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1605 if (io != 0)
1607 *sz = kCDSectorSizeWhole * i + 16;
1608 return CDROM_GetStatusCode(io);
1611 /* And finally the sector proper */
1612 cdrd.offset += 8;
1613 cdrd.sectorArea = kCDSectorAreaUser;
1614 cdrd.buffer = (PBYTE)cdrd.buffer + 8;
1615 cdrd.bufferLength = kCDSectorSizeMode2;
1616 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1617 if (io != 0)
1619 *sz = kCDSectorSizeWhole * i + 24;
1620 return CDROM_GetStatusCode(io);
1624 break;
1627 case CDDA:
1629 /* With CDDA, the whole raw sector is considered user data, so there's
1630 * no need to read one at a time.
1632 dk_cd_read_t cdrd;
1634 cdrd.offset = (raw->DiskOffset.QuadPart >> 11) * kCDSectorSizeCDDA;
1635 cdrd.sectorArea = kCDSectorAreaUser;
1636 cdrd.sectorType = kCDSectorTypeCDDA;
1637 cdrd.buffer = buffer;
1638 cdrd.bufferLength = len < raw->SectorCount*kCDSectorSizeCDDA ? len :
1639 raw->SectorCount*kCDSectorSizeCDDA;
1641 io = ioctl(fd, DKIOCCDREAD, &cdrd);
1642 if (io != 0) return CDROM_GetStatusCode(io);
1643 break;
1646 default:
1647 FIXME("NIY: %d\n", raw->TrackMode);
1648 return STATUS_INVALID_PARAMETER;
1650 #else
1651 switch (raw->TrackMode)
1653 case YellowMode2:
1654 FIXME("YellowMode2: NIY\n");
1655 return ret;
1656 case XAForm2:
1657 FIXME("XAForm2: NIY\n");
1658 return ret;
1659 case CDDA:
1660 FIXME("CDDA: NIY\n");
1661 return ret;
1662 default:
1663 FIXME("NIY: %d\n", raw->TrackMode);
1664 return STATUS_INVALID_PARAMETER;
1666 #endif
1668 *sz = 2352 * raw->SectorCount;
1669 ret = CDROM_GetStatusCode(io);
1670 return ret;
1673 /******************************************************************
1674 * CDROM_ScsiPassThroughDirect
1675 * Implements IOCTL_SCSI_PASS_THROUGH_DIRECT
1678 static NTSTATUS CDROM_ScsiPassThroughDirect(int fd, PSCSI_PASS_THROUGH_DIRECT pPacket)
1680 int ret = STATUS_NOT_SUPPORTED;
1681 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1682 sg_io_hdr_t cmd;
1683 int io;
1684 #elif defined HAVE_SCSIREQ_T_CMD
1685 scsireq_t cmd;
1686 int io;
1687 #elif defined __APPLE__
1688 dk_scsi_command_t cmd;
1689 int io;
1690 #endif
1692 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1693 return STATUS_BUFFER_TOO_SMALL;
1695 if (pPacket->CdbLength > 16)
1696 return STATUS_INVALID_PARAMETER;
1698 #ifdef SENSEBUFLEN
1699 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1700 return STATUS_INVALID_PARAMETER;
1701 #elif defined HAVE_REQUEST_SENSE
1702 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1703 return STATUS_INVALID_PARAMETER;
1704 #endif
1706 if (pPacket->DataTransferLength > 0 && !pPacket->DataBuffer)
1707 return STATUS_INVALID_PARAMETER;
1709 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1710 RtlZeroMemory(&cmd, sizeof(cmd));
1712 cmd.interface_id = 'S';
1713 cmd.cmd_len = pPacket->CdbLength;
1714 cmd.mx_sb_len = pPacket->SenseInfoLength;
1715 cmd.dxfer_len = pPacket->DataTransferLength;
1716 cmd.dxferp = pPacket->DataBuffer;
1717 cmd.cmdp = pPacket->Cdb;
1718 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1719 cmd.timeout = pPacket->TimeOutValue*1000;
1721 switch (pPacket->DataIn)
1723 case SCSI_IOCTL_DATA_IN:
1724 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1725 break;
1726 case SCSI_IOCTL_DATA_OUT:
1727 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1728 break;
1729 case SCSI_IOCTL_DATA_UNSPECIFIED:
1730 cmd.dxfer_direction = SG_DXFER_NONE;
1731 break;
1732 default:
1733 return STATUS_INVALID_PARAMETER;
1736 io = ioctl(fd, SG_IO, &cmd);
1738 pPacket->ScsiStatus = cmd.status;
1739 pPacket->DataTransferLength = cmd.resid;
1740 pPacket->SenseInfoLength = cmd.sb_len_wr;
1742 ret = CDROM_GetStatusCode(io);
1744 #elif defined HAVE_SCSIREQ_T_CMD
1746 memset(&cmd, 0, sizeof(cmd));
1747 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1749 cmd.cmdlen = pPacket->CdbLength;
1750 cmd.databuf = pPacket->DataBuffer;
1751 cmd.datalen = pPacket->DataTransferLength;
1752 cmd.senselen = pPacket->SenseInfoLength;
1753 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1755 switch (pPacket->DataIn)
1757 case SCSI_IOCTL_DATA_OUT:
1758 cmd.flags |= SCCMD_WRITE;
1759 break;
1760 case SCSI_IOCTL_DATA_IN:
1761 cmd.flags |= SCCMD_READ;
1762 break;
1763 case SCSI_IOCTL_DATA_UNSPECIFIED:
1764 cmd.flags = 0;
1765 break;
1766 default:
1767 return STATUS_INVALID_PARAMETER;
1770 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1772 switch (cmd.retsts)
1774 case SCCMD_OK: break;
1775 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1776 break;
1777 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1778 break;
1779 case SCCMD_SENSE: break;
1780 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1781 break;
1784 if (pPacket->SenseInfoLength != 0)
1786 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1787 cmd.sense, pPacket->SenseInfoLength);
1790 pPacket->ScsiStatus = cmd.status;
1792 ret = CDROM_GetStatusCode(io);
1794 #elif defined(__APPLE__)
1796 memset(&cmd, 0, sizeof(cmd));
1797 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1799 cmd.cdbSize = pPacket->CdbLength;
1800 cmd.buffer = pPacket->DataBuffer;
1801 cmd.bufferSize = pPacket->DataTransferLength;
1802 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1803 cmd.senseLen = pPacket->SenseInfoLength;
1804 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1806 switch (pPacket->DataIn)
1808 case SCSI_IOCTL_DATA_OUT:
1809 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
1810 break;
1811 case SCSI_IOCTL_DATA_IN:
1812 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
1813 break;
1814 case SCSI_IOCTL_DATA_UNSPECIFIED:
1815 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
1816 break;
1817 default:
1818 return STATUS_INVALID_PARAMETER;
1821 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
1823 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
1825 /* Command failed */
1826 switch (cmd.status)
1828 case kSCSITaskStatus_TaskTimeoutOccurred: return STATUS_TIMEOUT;
1829 break;
1830 case kSCSITaskStatus_ProtocolTimeoutOccurred: return STATUS_IO_TIMEOUT;
1831 break;
1832 case kSCSITaskStatus_DeviceNotResponding: return STATUS_DEVICE_BUSY;
1833 break;
1834 case kSCSITaskStatus_DeviceNotPresent:
1835 return STATUS_NO_SUCH_DEVICE;
1836 break;
1837 case kSCSITaskStatus_DeliveryFailure:
1838 return STATUS_DEVICE_PROTOCOL_ERROR;
1839 break;
1840 case kSCSITaskStatus_No_Status:
1841 default:
1842 return STATUS_UNSUCCESSFUL;
1843 break;
1847 if (cmd.status != kSCSITaskStatus_No_Status)
1848 pPacket->ScsiStatus = cmd.status;
1850 ret = CDROM_GetStatusCode(io);
1851 #endif
1852 return ret;
1855 /******************************************************************
1856 * CDROM_ScsiPassThrough
1857 * Implements IOCTL_SCSI_PASS_THROUGH
1860 static NTSTATUS CDROM_ScsiPassThrough(int fd, PSCSI_PASS_THROUGH pPacket)
1862 int ret = STATUS_NOT_SUPPORTED;
1863 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1864 sg_io_hdr_t cmd;
1865 int io;
1866 #elif defined HAVE_SCSIREQ_T_CMD
1867 scsireq_t cmd;
1868 int io;
1869 #elif defined __APPLE__
1870 dk_scsi_command_t cmd;
1871 int io;
1872 #endif
1874 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1875 return STATUS_BUFFER_TOO_SMALL;
1877 if (pPacket->CdbLength > 16)
1878 return STATUS_INVALID_PARAMETER;
1880 #ifdef SENSEBUFLEN
1881 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1882 return STATUS_INVALID_PARAMETER;
1883 #elif defined HAVE_REQUEST_SENSE
1884 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1885 return STATUS_INVALID_PARAMETER;
1886 #endif
1888 if (pPacket->DataTransferLength > 0 && pPacket->DataBufferOffset < sizeof(SCSI_PASS_THROUGH))
1889 return STATUS_INVALID_PARAMETER;
1891 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1892 RtlZeroMemory(&cmd, sizeof(cmd));
1894 cmd.interface_id = 'S';
1895 cmd.dxfer_len = pPacket->DataTransferLength;
1896 cmd.dxferp = (char*)pPacket + pPacket->DataBufferOffset;
1897 cmd.cmd_len = pPacket->CdbLength;
1898 cmd.cmdp = pPacket->Cdb;
1899 cmd.mx_sb_len = pPacket->SenseInfoLength;
1900 cmd.timeout = pPacket->TimeOutValue*1000;
1902 if(cmd.mx_sb_len > 0)
1903 cmd.sbp = (unsigned char*)pPacket + pPacket->SenseInfoOffset;
1905 switch (pPacket->DataIn)
1907 case SCSI_IOCTL_DATA_IN:
1908 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1909 break;
1910 case SCSI_IOCTL_DATA_OUT:
1911 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1912 break;
1913 case SCSI_IOCTL_DATA_UNSPECIFIED:
1914 cmd.dxfer_direction = SG_DXFER_NONE;
1915 break;
1916 default:
1917 return STATUS_INVALID_PARAMETER;
1920 io = ioctl(fd, SG_IO, &cmd);
1922 pPacket->ScsiStatus = cmd.status;
1923 pPacket->DataTransferLength = cmd.resid;
1924 pPacket->SenseInfoLength = cmd.sb_len_wr;
1926 ret = CDROM_GetStatusCode(io);
1928 #elif defined HAVE_SCSIREQ_T_CMD
1930 memset(&cmd, 0, sizeof(cmd));
1931 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1933 if ( pPacket->DataBufferOffset > 0x1000 )
1935 cmd.databuf = (void*)pPacket->DataBufferOffset;
1937 else
1939 cmd.databuf = (char*)pPacket + pPacket->DataBufferOffset;
1942 cmd.cmdlen = pPacket->CdbLength;
1943 cmd.datalen = pPacket->DataTransferLength;
1944 cmd.senselen = pPacket->SenseInfoLength;
1945 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1947 switch (pPacket->DataIn)
1949 case SCSI_IOCTL_DATA_OUT:
1950 cmd.flags |= SCCMD_WRITE;
1951 break;
1952 case SCSI_IOCTL_DATA_IN:
1953 cmd.flags |= SCCMD_READ;
1954 break;
1955 case SCSI_IOCTL_DATA_UNSPECIFIED:
1956 cmd.flags = 0;
1957 break;
1958 default:
1959 return STATUS_INVALID_PARAMETER;
1962 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1964 switch (cmd.retsts)
1966 case SCCMD_OK: break;
1967 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1968 break;
1969 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1970 break;
1971 case SCCMD_SENSE: break;
1972 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1973 break;
1976 if (pPacket->SenseInfoLength != 0)
1978 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1979 cmd.sense, pPacket->SenseInfoLength);
1982 pPacket->ScsiStatus = cmd.status;
1984 ret = CDROM_GetStatusCode(io);
1986 #elif defined(__APPLE__)
1988 memset(&cmd, 0, sizeof(cmd));
1989 memcpy(cmd.cdb, pPacket->Cdb, pPacket->CdbLength);
1991 cmd.cdbSize = pPacket->CdbLength;
1992 cmd.buffer = (char*)pPacket + pPacket->DataBufferOffset;
1993 cmd.bufferSize = pPacket->DataTransferLength;
1994 cmd.sense = (char*)pPacket + pPacket->SenseInfoOffset;
1995 cmd.senseLen = pPacket->SenseInfoLength;
1996 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1998 switch (pPacket->DataIn)
2000 case SCSI_IOCTL_DATA_OUT:
2001 cmd.direction = kSCSIDataTransfer_FromInitiatorToTarget;
2002 break;
2003 case SCSI_IOCTL_DATA_IN:
2004 cmd.direction = kSCSIDataTransfer_FromTargetToInitiator;
2005 break;
2006 case SCSI_IOCTL_DATA_UNSPECIFIED:
2007 cmd.direction = kSCSIDataTransfer_NoDataTransfer;
2008 break;
2009 default:
2010 return STATUS_INVALID_PARAMETER;
2013 io = ioctl(fd, DKIOCSCSICOMMAND, &cmd);
2015 if (cmd.response == kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE)
2017 /* Command failed */
2018 switch (cmd.status)
2020 case kSCSITaskStatus_TaskTimeoutOccurred:
2021 return STATUS_TIMEOUT;
2022 break;
2023 case kSCSITaskStatus_ProtocolTimeoutOccurred:
2024 return STATUS_IO_TIMEOUT;
2025 break;
2026 case kSCSITaskStatus_DeviceNotResponding:
2027 return STATUS_DEVICE_BUSY;
2028 break;
2029 case kSCSITaskStatus_DeviceNotPresent:
2030 return STATUS_NO_SUCH_DEVICE;
2031 break;
2032 case kSCSITaskStatus_DeliveryFailure:
2033 return STATUS_DEVICE_PROTOCOL_ERROR;
2034 break;
2035 case kSCSITaskStatus_No_Status:
2036 default:
2037 return STATUS_UNSUCCESSFUL;
2038 break;
2042 if (cmd.status != kSCSITaskStatus_No_Status)
2043 pPacket->ScsiStatus = cmd.status;
2045 ret = CDROM_GetStatusCode(io);
2046 #endif
2047 return ret;
2050 /******************************************************************
2051 * CDROM_ScsiGetCaps
2055 static NTSTATUS CDROM_ScsiGetCaps(int fd, PIO_SCSI_CAPABILITIES caps)
2057 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
2059 #ifdef SG_SCATTER_SZ
2060 caps->Length = sizeof(*caps);
2061 caps->MaximumTransferLength = SG_SCATTER_SZ; /* FIXME */
2062 caps->MaximumPhysicalPages = SG_SCATTER_SZ / getpagesize();
2063 caps->SupportedAsynchronousEvents = TRUE;
2064 caps->AlignmentMask = getpagesize();
2065 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
2066 caps->AdapterScansDown = FALSE; /* FIXME ? */
2067 caps->AdapterUsesPio = FALSE; /* FIXME ? */
2068 ret = STATUS_SUCCESS;
2069 #elif defined __APPLE__
2070 uint64_t bytesr, bytesw, align;
2071 int io = ioctl(fd, DKIOCGETMAXBYTECOUNTREAD, &bytesr);
2072 if (io != 0) return CDROM_GetStatusCode(io);
2073 io = ioctl(fd, DKIOCGETMAXBYTECOUNTWRITE, &bytesw);
2074 if (io != 0) return CDROM_GetStatusCode(io);
2075 io = ioctl(fd, DKIOCGETMINSEGMENTALIGNMENTBYTECOUNT, &align);
2076 if (io != 0) return CDROM_GetStatusCode(io);
2077 caps->Length = sizeof(*caps);
2078 caps->MaximumTransferLength = bytesr < bytesw ? bytesr : bytesw;
2079 caps->MaximumPhysicalPages = caps->MaximumTransferLength / getpagesize();
2080 caps->SupportedAsynchronousEvents = TRUE;
2081 caps->AlignmentMask = align-1;
2082 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
2083 caps->AdapterScansDown = FALSE; /* FIXME ? */
2084 caps->AdapterUsesPio = FALSE; /* FIXME ? */
2085 ret = STATUS_SUCCESS;
2086 #else
2087 FIXME("Unimplemented\n");
2088 #endif
2089 return ret;
2092 /******************************************************************
2093 * CDROM_GetAddress
2095 * implements IOCTL_SCSI_GET_ADDRESS
2097 static NTSTATUS CDROM_GetAddress(int fd, SCSI_ADDRESS* address)
2099 UCHAR portnum, busid, targetid, lun;
2101 address->Length = sizeof(SCSI_ADDRESS);
2102 if ( ! CDROM_GetInterfaceInfo(fd, &portnum, &busid, &targetid, &lun))
2103 return STATUS_NOT_SUPPORTED;
2105 address->PortNumber = portnum; /* primary=0 secondary=1 for ide */
2106 address->PathId = busid; /* always 0 for ide */
2107 address->TargetId = targetid; /* master=0 slave=1 for ide */
2108 address->Lun = lun;
2109 return STATUS_SUCCESS;
2112 /******************************************************************
2113 * DVD_StartSession
2117 static NTSTATUS DVD_StartSession(int fd, const DVD_SESSION_ID *sid_in, PDVD_SESSION_ID sid_out)
2119 #if defined(linux)
2120 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2121 dvd_authinfo auth_info;
2123 memset( &auth_info, 0, sizeof( auth_info ) );
2124 auth_info.type = DVD_LU_SEND_AGID;
2125 if (sid_in) auth_info.lsa.agid = *(const int*)sid_in; /* ?*/
2127 TRACE("fd 0x%08x\n",fd);
2128 ret =CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2129 *sid_out = auth_info.lsa.agid;
2130 return ret;
2131 #elif defined(__FreeBSD__) || defined(__NetBSD__)
2132 return STATUS_NOT_SUPPORTED;
2133 #elif defined(__APPLE__)
2134 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2135 dk_dvd_report_key_t dvdrk;
2136 DVDAuthenticationGrantIDInfo agid_info;
2138 dvdrk.format = kDVDKeyFormatAGID_CSS;
2139 dvdrk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2140 if(sid_in) dvdrk.grantID = *(uint8_t*)sid_in; /* ? */
2141 dvdrk.bufferLength = sizeof(DVDAuthenticationGrantIDInfo);
2142 dvdrk.buffer = &agid_info;
2144 ret = CDROM_GetStatusCode(ioctl(fd, DKIOCDVDREPORTKEY, &dvdrk));
2145 *sid_out = agid_info.grantID;
2146 return ret;
2147 #else
2148 return STATUS_NOT_SUPPORTED;
2149 #endif
2152 /******************************************************************
2153 * DVD_EndSession
2157 static NTSTATUS DVD_EndSession(int fd, const DVD_SESSION_ID *sid)
2159 #if defined(linux)
2160 dvd_authinfo auth_info;
2162 memset( &auth_info, 0, sizeof( auth_info ) );
2163 auth_info.type = DVD_INVALIDATE_AGID;
2164 auth_info.lsa.agid = *(const int*)sid;
2166 TRACE("\n");
2167 return CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
2168 #elif defined(__FreeBSD__) || defined(__NetBSD__)
2169 return STATUS_NOT_SUPPORTED;
2170 #elif defined(__APPLE__)
2171 dk_dvd_send_key_t dvdsk;
2173 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2174 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2175 dvdsk.grantID = (uint8_t)*sid;
2177 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2178 #else
2179 return STATUS_NOT_SUPPORTED;
2180 #endif
2183 /******************************************************************
2184 * DVD_SendKey
2188 static NTSTATUS DVD_SendKey(int fd, const DVD_COPY_PROTECT_KEY *key)
2190 #if defined(linux)
2191 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2192 dvd_authinfo auth_info;
2194 memset( &auth_info, 0, sizeof( auth_info ) );
2195 switch (key->KeyType)
2197 case DvdChallengeKey:
2198 auth_info.type = DVD_HOST_SEND_CHALLENGE;
2199 auth_info.hsc.agid = (int)key->SessionId;
2200 TRACE("DvdChallengeKey ioc 0x%x\n", DVD_AUTH );
2201 memcpy( auth_info.hsc.chal, key->KeyData, DVD_CHALLENGE_SIZE );
2202 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2203 break;
2204 case DvdBusKey2:
2205 auth_info.type = DVD_HOST_SEND_KEY2;
2206 auth_info.hsk.agid = (int)key->SessionId;
2208 memcpy( auth_info.hsk.key, key->KeyData, DVD_KEY_SIZE );
2210 TRACE("DvdBusKey2\n");
2211 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2212 break;
2214 default:
2215 FIXME("Unknown Keytype 0x%x\n",key->KeyType);
2217 return ret;
2218 #elif defined(__APPLE__)
2219 dk_dvd_send_key_t dvdsk;
2220 union
2222 DVDChallengeKeyInfo chal;
2223 DVDKey2Info key2;
2224 } key_desc;
2226 switch(key->KeyType)
2228 case DvdChallengeKey:
2229 dvdsk.format = kDVDKeyFormatChallengeKey;
2230 dvdsk.bufferLength = sizeof(key_desc.chal);
2231 dvdsk.buffer = &key_desc.chal;
2232 OSWriteBigInt16(key_desc.chal.dataLength, 0, key->KeyLength);
2233 memcpy(key_desc.chal.challengeKeyValue, key->KeyData, key->KeyLength);
2234 break;
2235 case DvdBusKey2:
2236 dvdsk.format = kDVDKeyFormatKey2;
2237 dvdsk.bufferLength = sizeof(key_desc.key2);
2238 dvdsk.buffer = &key_desc.key2;
2239 OSWriteBigInt16(key_desc.key2.dataLength, 0, key->KeyLength);
2240 memcpy(key_desc.key2.key2Value, key->KeyData, key->KeyLength);
2241 break;
2242 case DvdInvalidateAGID:
2243 dvdsk.format = kDVDKeyFormatAGID_Invalidate;
2244 break;
2245 case DvdBusKey1:
2246 case DvdTitleKey:
2247 case DvdAsf:
2248 case DvdGetRpcKey:
2249 case DvdDiskKey:
2250 ERR("attempted to write read-only key type 0x%x\n", key->KeyType);
2251 return STATUS_NOT_SUPPORTED;
2252 case DvdSetRpcKey:
2253 FIXME("DvdSetRpcKey NIY\n");
2254 return STATUS_NOT_SUPPORTED;
2255 default:
2256 FIXME("got unknown key type 0x%x\n", key->KeyType);
2257 return STATUS_NOT_SUPPORTED;
2259 dvdsk.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2260 dvdsk.grantID = (uint8_t)key->SessionId;
2262 return CDROM_GetStatusCode(ioctl(fd, DKIOCDVDSENDKEY, &dvdsk));
2263 #else
2264 FIXME("unsupported on this platform\n");
2265 return STATUS_NOT_SUPPORTED;
2266 #endif
2269 /******************************************************************
2270 * DVD_ReadKey
2274 static NTSTATUS DVD_ReadKey(int fd, PDVD_COPY_PROTECT_KEY key)
2276 #if defined(linux)
2277 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2278 dvd_struct dvd;
2279 dvd_authinfo auth_info;
2281 memset( &dvd, 0, sizeof( dvd_struct ) );
2282 memset( &auth_info, 0, sizeof( auth_info ) );
2283 switch (key->KeyType)
2285 case DvdDiskKey:
2287 dvd.type = DVD_STRUCT_DISCKEY;
2288 dvd.disckey.agid = (int)key->SessionId;
2289 memset( dvd.disckey.value, 0, DVD_DISCKEY_SIZE );
2291 TRACE("DvdDiskKey\n");
2292 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2293 if (ret == STATUS_SUCCESS)
2294 memcpy(key->KeyData,dvd.disckey.value,DVD_DISCKEY_SIZE);
2295 break;
2296 case DvdTitleKey:
2297 auth_info.type = DVD_LU_SEND_TITLE_KEY;
2298 auth_info.lstk.agid = (int)key->SessionId;
2299 auth_info.lstk.lba = (int)(key->Parameters.TitleOffset.QuadPart>>11);
2300 TRACE("DvdTitleKey session %d Quadpart 0x%08lx offset 0x%08x\n",
2301 (int)key->SessionId, (long)key->Parameters.TitleOffset.QuadPart,
2302 auth_info.lstk.lba);
2303 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2304 if (ret == STATUS_SUCCESS)
2305 memcpy(key->KeyData, auth_info.lstk.title_key, DVD_KEY_SIZE );
2306 break;
2307 case DvdChallengeKey:
2309 auth_info.type = DVD_LU_SEND_CHALLENGE;
2310 auth_info.lsc.agid = (int)key->SessionId;
2312 TRACE("DvdChallengeKey\n");
2313 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2314 if (ret == STATUS_SUCCESS)
2315 memcpy( key->KeyData, auth_info.lsc.chal, DVD_CHALLENGE_SIZE );
2316 break;
2317 case DvdAsf:
2318 auth_info.type = DVD_LU_SEND_ASF;
2319 TRACE("DvdAsf\n");
2320 auth_info.lsasf.asf=((PDVD_ASF)key->KeyData)->SuccessFlag;
2321 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2322 ((PDVD_ASF)key->KeyData)->SuccessFlag = auth_info.lsasf.asf;
2323 break;
2324 case DvdBusKey1:
2325 auth_info.type = DVD_LU_SEND_KEY1;
2326 auth_info.lsk.agid = (int)key->SessionId;
2328 TRACE("DvdBusKey1\n");
2329 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2331 if (ret == STATUS_SUCCESS)
2332 memcpy( key->KeyData, auth_info.lsk.key, DVD_KEY_SIZE );
2333 break;
2334 case DvdGetRpcKey:
2335 auth_info.type = DVD_LU_SEND_RPC_STATE;
2337 TRACE("DvdGetRpcKey\n");
2338 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2340 if (ret == STATUS_SUCCESS)
2342 ((PDVD_RPC_KEY)key->KeyData)->TypeCode = auth_info.lrpcs.type;
2343 ((PDVD_RPC_KEY)key->KeyData)->RegionMask = auth_info.lrpcs.region_mask;
2344 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme = auth_info.lrpcs.rpc_scheme;
2345 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable = auth_info.lrpcs.ucca;
2346 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable = auth_info.lrpcs.vra;
2348 break;
2349 default:
2350 FIXME("Unknown keytype 0x%x\n",key->KeyType);
2352 return ret;
2353 #elif defined(__FreeBSD__) || defined(__NetBSD__)
2354 TRACE("bsd\n");
2355 return STATUS_NOT_SUPPORTED;
2356 #elif defined(__APPLE__)
2357 union
2359 dk_dvd_report_key_t key;
2360 dk_dvd_read_structure_t disk_key;
2361 } ioc;
2362 union
2364 DVDDiscKeyInfo disk_key;
2365 DVDChallengeKeyInfo chal;
2366 DVDKey1Info key1;
2367 DVDTitleKeyInfo title;
2368 DVDAuthenticationSuccessFlagInfo asf;
2369 DVDRegionPlaybackControlInfo rpc;
2370 } desc;
2371 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2373 switch(key->KeyType)
2375 case DvdChallengeKey:
2376 ioc.key.format = kDVDKeyFormatChallengeKey;
2377 ioc.key.grantID = (uint8_t)key->SessionId;
2378 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2379 ioc.key.bufferLength = sizeof(desc.chal);
2380 ioc.key.buffer = &desc.chal;
2381 OSWriteBigInt16(desc.chal.dataLength, 0, key->KeyLength);
2382 break;
2383 case DvdBusKey1:
2384 ioc.key.format = kDVDKeyFormatKey1;
2385 ioc.key.grantID = (uint8_t)key->SessionId;
2386 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2387 ioc.key.bufferLength = sizeof(desc.key1);
2388 ioc.key.buffer = &desc.key1;
2389 OSWriteBigInt16(desc.key1.dataLength, 0, key->KeyLength);
2390 break;
2391 case DvdTitleKey:
2392 ioc.key.format = kDVDKeyFormatTitleKey;
2393 ioc.key.grantID = (uint8_t)key->SessionId;
2394 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2395 ioc.key.bufferLength = sizeof(desc.title);
2396 ioc.key.buffer = &desc.title;
2397 ioc.key.address = (uint32_t)(key->Parameters.TitleOffset.QuadPart>>11);
2398 OSWriteBigInt16(desc.title.dataLength, 0, key->KeyLength);
2399 break;
2400 case DvdAsf:
2401 ioc.key.format = kDVDKeyFormatASF;
2402 ioc.key.grantID = (uint8_t)key->SessionId;
2403 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2404 ioc.key.bufferLength = sizeof(desc.asf);
2405 ioc.key.buffer = &desc.asf;
2406 OSWriteBigInt16(desc.asf.dataLength, 0, key->KeyLength);
2407 break;
2408 case DvdGetRpcKey:
2409 ioc.key.format = kDVDKeyFormatRegionState;
2410 ioc.key.grantID = (uint8_t)key->SessionId;
2411 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2412 ioc.key.bufferLength = sizeof(desc.rpc);
2413 ioc.key.buffer = &desc.rpc;
2414 OSWriteBigInt16(desc.rpc.dataLength, 0, key->KeyLength);
2415 break;
2416 case DvdDiskKey:
2417 ioc.disk_key.format = kDVDStructureFormatDiscKeyInfo;
2418 ioc.disk_key.grantID = (uint8_t)key->SessionId;
2419 ioc.disk_key.bufferLength = sizeof(desc.disk_key);
2420 ioc.disk_key.buffer = &desc.disk_key;
2421 break;
2422 case DvdInvalidateAGID:
2423 ioc.key.format = kDVDKeyFormatAGID_Invalidate;
2424 ioc.key.grantID = (uint8_t)key->SessionId;
2425 ioc.key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2426 break;
2427 case DvdBusKey2:
2428 case DvdSetRpcKey:
2429 ERR("attempted to read write-only key type 0x%x\n", key->KeyType);
2430 return STATUS_NOT_SUPPORTED;
2431 default:
2432 FIXME("got unknown key type 0x%x\n", key->KeyType);
2433 return STATUS_NOT_SUPPORTED;
2436 ret = CDROM_GetStatusCode(ioctl(fd, (key->KeyType == DvdDiskKey ? DKIOCDVDREADSTRUCTURE : DKIOCDVDREPORTKEY), &ioc));
2438 if (ret == STATUS_SUCCESS)
2440 switch(key->KeyType)
2442 case DvdChallengeKey:
2443 key->KeyLength = OSReadBigInt16(desc.chal.dataLength, 0);
2444 memcpy(key->KeyData, desc.chal.challengeKeyValue, key->KeyLength);
2445 break;
2446 case DvdBusKey1:
2447 key->KeyLength = OSReadBigInt16(desc.key1.dataLength, 0);
2448 memcpy(key->KeyData, desc.key1.key1Value, key->KeyLength);
2449 break;
2450 case DvdTitleKey:
2451 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2452 memcpy(key->KeyData, desc.title.titleKeyValue, key->KeyLength);
2453 key->KeyFlags = 0;
2454 if (desc.title.CPM)
2456 /*key->KeyFlags |= DVD_COPYRIGHTED;*/
2457 if (desc.title.CP_SEC) key->KeyFlags |= DVD_SECTOR_PROTECTED;
2458 /*else key->KeyFlags |= DVD_SECTOR_NOT_PROTECTED;*/
2459 #if 0
2460 switch (desc.title.CGMS)
2462 case 0:
2463 key->KeyFlags |= DVD_CGMS_COPY_PERMITTED;
2464 break;
2465 case 2:
2466 key->KeyFlags |= DVD_CGMS_COPY_ONCE;
2467 break;
2468 case 3:
2469 key->KeyFlags |= DVD_CGMS_NO_COPY;
2470 break;
2472 #endif
2473 } /*else key->KeyFlags |= DVD_NOT_COPYRIGHTED;*/
2474 break;
2475 case DvdAsf:
2476 key->KeyLength = OSReadBigInt16(desc.title.dataLength, 0);
2477 ((PDVD_ASF)key->KeyData)->SuccessFlag = desc.asf.successFlag;
2478 break;
2479 case DvdGetRpcKey:
2480 key->KeyLength = OSReadBigInt16(desc.rpc.dataLength, 0);
2481 ((PDVD_RPC_KEY)key->KeyData)->UserResetsAvailable =
2482 desc.rpc.numberUserResets;
2483 ((PDVD_RPC_KEY)key->KeyData)->ManufacturerResetsAvailable =
2484 desc.rpc.numberVendorResets;
2485 ((PDVD_RPC_KEY)key->KeyData)->TypeCode =
2486 desc.rpc.typeCode;
2487 ((PDVD_RPC_KEY)key->KeyData)->RegionMask =
2488 desc.rpc.driveRegion;
2489 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme =
2490 desc.rpc.rpcScheme;
2491 case DvdDiskKey:
2492 key->KeyLength = OSReadBigInt16(desc.disk_key.dataLength, 0);
2493 memcpy(key->KeyData, desc.disk_key.discKeyStructures, key->KeyLength);
2494 break;
2495 case DvdBusKey2:
2496 case DvdSetRpcKey:
2497 case DvdInvalidateAGID:
2498 default:
2499 /* Silence warning */
2503 return ret;
2504 #else
2505 TRACE("outside\n");
2506 return STATUS_NOT_SUPPORTED;
2507 #endif
2510 /******************************************************************
2511 * DVD_GetRegion
2513 * This IOCTL combines information from both IOCTL_DVD_READ_KEY
2514 * with key type DvdGetRpcKey and IOCTL_DVD_READ_STRUCTURE with
2515 * structure type DvdCopyrightInformation into one structure.
2517 static NTSTATUS DVD_GetRegion(int fd, PDVD_REGION region)
2519 #if defined(linux)
2520 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2521 dvd_struct dvd;
2522 dvd_authinfo auth_info;
2524 dvd.type = DVD_STRUCT_COPYRIGHT;
2525 dvd.copyright.layer_num = 0;
2526 auth_info.type = DVD_LU_SEND_RPC_STATE;
2528 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
2530 if (ret == STATUS_SUCCESS)
2532 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
2534 if (ret == STATUS_SUCCESS)
2536 region->CopySystem = dvd.copyright.cpst;
2537 region->RegionData = dvd.copyright.rmi;
2538 region->SystemRegion = auth_info.lrpcs.region_mask;
2539 region->ResetCount = auth_info.lrpcs.ucca;
2542 return ret;
2543 #elif defined(__FreeBSD__) || defined(__NetBSD__)
2544 TRACE("bsd\n");
2545 return STATUS_NOT_SUPPORTED;
2546 #elif defined(__APPLE__)
2547 dk_dvd_report_key_t key;
2548 dk_dvd_read_structure_t dvd;
2549 DVDRegionPlaybackControlInfo rpc;
2550 DVDCopyrightInfo copy;
2551 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2553 key.format = kDVDKeyFormatRegionState;
2554 key.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
2555 key.bufferLength = sizeof(rpc);
2556 key.buffer = &rpc;
2557 dvd.format = kDVDStructureFormatCopyrightInfo;
2558 dvd.bufferLength = sizeof(copy);
2559 dvd.buffer = &copy;
2561 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREPORTKEY, &key ));
2563 if (ret == STATUS_SUCCESS)
2565 ret = CDROM_GetStatusCode(ioctl( fd, DKIOCDVDREADSTRUCTURE, &dvd ));
2567 if (ret == STATUS_SUCCESS)
2569 region->CopySystem = copy.copyrightProtectionSystemType;
2570 region->RegionData = copy.regionMask;
2571 region->SystemRegion = rpc.driveRegion;
2572 region->ResetCount = rpc.numberUserResets;
2575 return ret;
2576 #else
2577 FIXME("\n");
2578 return STATUS_NOT_SUPPORTED;
2579 #endif
2582 /******************************************************************
2583 * DVD_ReadStructure
2587 static NTSTATUS DVD_ReadStructure(int dev, const DVD_READ_STRUCTURE *structure, PDVD_LAYER_DESCRIPTOR layer)
2589 #ifdef DVD_READ_STRUCT
2590 /* dvd_struct is not defined consistently across platforms */
2591 union
2593 struct dvd_physical physical;
2594 struct dvd_copyright copyright;
2595 struct dvd_disckey disckey;
2596 struct dvd_bca bca;
2597 struct dvd_manufact manufact;
2598 } s;
2600 if (structure->BlockByteOffset.u.HighPart || structure->BlockByteOffset.u.LowPart)
2601 FIXME(": BlockByteOffset is not handled\n");
2603 switch (structure->Format)
2605 case DvdPhysicalDescriptor:
2606 s.physical.type = DVD_STRUCT_PHYSICAL;
2607 s.physical.layer_num = structure->LayerNumber;
2608 break;
2610 case DvdCopyrightDescriptor:
2611 s.copyright.type = DVD_STRUCT_COPYRIGHT;
2612 s.copyright.layer_num = structure->LayerNumber;
2613 break;
2615 case DvdDiskKeyDescriptor:
2616 s.disckey.type = DVD_STRUCT_DISCKEY;
2617 s.disckey.agid = structure->SessionId;
2618 break;
2620 case DvdBCADescriptor:
2621 s.bca.type = DVD_STRUCT_BCA;
2622 break;
2624 case DvdManufacturerDescriptor:
2625 s.manufact.type = DVD_STRUCT_MANUFACT;
2626 s.manufact.layer_num = structure->LayerNumber;
2627 break;
2629 case DvdMaxDescriptor: /* This is not a real request, no matter what MSDN says! */
2630 default:
2631 return STATUS_INVALID_PARAMETER;
2634 if (ioctl(dev, DVD_READ_STRUCT, &s) < 0)
2635 return STATUS_INVALID_PARAMETER;
2637 switch (structure->Format)
2639 case DvdPhysicalDescriptor:
2641 internal_dvd_layer_descriptor *p = (internal_dvd_layer_descriptor *) layer;
2642 struct dvd_layer *l = &s.physical.layer[s.physical.layer_num];
2644 p->DataLength[0] = 2;
2645 p->DataLength[1] = 8;
2646 p->Reserved0[0] = 0;
2647 p->Reserved0[1] = 0;
2648 p->BookVersion = l->book_version;
2649 p->BookType = l->book_type;
2650 p->MinimumRate = l->min_rate;
2651 p->DiskSize = l->disc_size;
2652 p->LayerType = l->layer_type;
2653 p->TrackPath = l->track_path;
2654 p->NumberOfLayers = l->nlayers;
2655 p->Reserved1 = 0;
2656 p->TrackDensity = l->track_density;
2657 p->LinearDensity = l->linear_density;
2658 p->StartingDataSector = l->start_sector;
2659 p->EndDataSector = l->end_sector;
2660 p->EndLayerZeroSector = l->end_sector_l0;
2661 p->Reserved5 = 0;
2662 p->BCAFlag = l->bca;
2663 p->Reserved6 = 0;
2665 break;
2667 case DvdCopyrightDescriptor:
2669 PDVD_COPYRIGHT_DESCRIPTOR p = (PDVD_COPYRIGHT_DESCRIPTOR) layer;
2671 p->CopyrightProtectionType = s.copyright.cpst;
2672 p->RegionManagementInformation = s.copyright.rmi;
2673 p->Reserved = 0;
2675 break;
2677 case DvdDiskKeyDescriptor:
2679 PDVD_DISK_KEY_DESCRIPTOR p = (PDVD_DISK_KEY_DESCRIPTOR) layer;
2681 memcpy(p->DiskKeyData, s.disckey.value, 2048);
2683 break;
2685 case DvdBCADescriptor:
2687 PDVD_BCA_DESCRIPTOR p = (PDVD_BCA_DESCRIPTOR) layer;
2689 memcpy(p->BCAInformation, s.bca.value, s.bca.len);
2691 break;
2693 case DvdManufacturerDescriptor:
2695 PDVD_MANUFACTURER_DESCRIPTOR p = (PDVD_MANUFACTURER_DESCRIPTOR) layer;
2697 memcpy(p->ManufacturingInformation, s.manufact.value, 2048);
2699 break;
2701 case DvdMaxDescriptor: /* Suppress warning */
2702 break;
2704 #elif defined(__APPLE__)
2705 NTSTATUS ret = STATUS_NOT_SUPPORTED;
2706 dk_dvd_read_structure_t dvdrs;
2707 union
2709 DVDPhysicalFormatInfo phys;
2710 DVDCopyrightInfo copy;
2711 DVDDiscKeyInfo disk_key;
2712 DVDManufacturingInfo manf;
2713 } desc;
2714 union
2716 PDVD_LAYER_DESCRIPTOR layer;
2717 internal_dvd_layer_descriptor *xlayer;
2718 PDVD_COPYRIGHT_DESCRIPTOR copy;
2719 PDVD_DISK_KEY_DESCRIPTOR disk_key;
2720 PDVD_MANUFACTURER_DESCRIPTOR manf;
2721 } nt_desc;
2723 nt_desc.layer = layer;
2724 dvdrs.address = (uint32_t)(structure->BlockByteOffset.QuadPart>>11);
2725 dvdrs.grantID = (uint8_t)structure->SessionId;
2726 dvdrs.layer = structure->LayerNumber;
2727 switch(structure->Format)
2729 case DvdPhysicalDescriptor:
2730 dvdrs.format = kDVDStructureFormatPhysicalFormatInfo;
2731 dvdrs.bufferLength = sizeof(desc.phys);
2732 dvdrs.buffer = &desc.phys;
2733 break;
2735 case DvdCopyrightDescriptor:
2736 dvdrs.format = kDVDStructureFormatCopyrightInfo;
2737 dvdrs.bufferLength = sizeof(desc.copy);
2738 dvdrs.buffer = &desc.copy;
2739 break;
2741 case DvdDiskKeyDescriptor:
2742 dvdrs.format = kDVDStructureFormatDiscKeyInfo;
2743 dvdrs.bufferLength = sizeof(desc.disk_key);
2744 dvdrs.buffer = &desc.disk_key;
2745 break;
2747 case DvdBCADescriptor:
2748 FIXME("DvdBCADescriptor NIY\n");
2749 return STATUS_NOT_SUPPORTED;
2751 case DvdManufacturerDescriptor:
2752 dvdrs.format = kDVDStructureFormatManufacturingInfo;
2753 dvdrs.bufferLength = sizeof(desc.manf);
2754 dvdrs.buffer = &desc.manf;
2755 break;
2757 case DvdMaxDescriptor:
2758 default:
2759 FIXME("got unknown structure type 0x%x\n", structure->Format);
2760 return STATUS_NOT_SUPPORTED;
2762 ret = CDROM_GetStatusCode(ioctl(dev, DKIOCDVDREADSTRUCTURE, &dvdrs));
2763 if(ret == STATUS_SUCCESS)
2765 switch(structure->Format)
2767 case DvdPhysicalDescriptor:
2768 nt_desc.xlayer->DataLength[0] = 2;
2769 nt_desc.xlayer->DataLength[1] = 8;
2770 nt_desc.xlayer->Reserved0[0] = 0;
2771 nt_desc.xlayer->Reserved0[1] = 0;
2772 nt_desc.xlayer->BookVersion = desc.phys.partVersion;
2773 nt_desc.xlayer->BookType = desc.phys.bookType;
2774 nt_desc.xlayer->MinimumRate = desc.phys.minimumRate;
2775 nt_desc.xlayer->DiskSize = desc.phys.discSize;
2776 nt_desc.xlayer->LayerType = desc.phys.layerType;
2777 nt_desc.xlayer->TrackPath = desc.phys.trackPath;
2778 nt_desc.xlayer->NumberOfLayers = desc.phys.numberOfLayers;
2779 nt_desc.xlayer->Reserved1 = 0;
2780 nt_desc.xlayer->TrackDensity = desc.phys.trackDensity;
2781 nt_desc.xlayer->LinearDensity = desc.phys.linearDensity;
2782 nt_desc.xlayer->BCAFlag = desc.phys.bcaFlag;
2783 nt_desc.xlayer->StartingDataSector = OSReadBigInt32(&desc.phys.zero1, 0);
2784 nt_desc.xlayer->EndDataSector = OSReadBigInt32(&desc.phys.zero2, 0);
2785 nt_desc.xlayer->EndLayerZeroSector = OSReadBigInt32(&desc.phys.zero3, 0);
2786 nt_desc.xlayer->Reserved5 = 0;
2787 nt_desc.xlayer->Reserved6 = 0;
2788 break;
2790 case DvdCopyrightDescriptor:
2791 nt_desc.copy->CopyrightProtectionType =
2792 desc.copy.copyrightProtectionSystemType;
2793 nt_desc.copy->RegionManagementInformation =
2794 desc.copy.regionMask;
2795 nt_desc.copy->Reserved = 0;
2796 break;
2798 case DvdDiskKeyDescriptor:
2799 memcpy(
2800 nt_desc.disk_key->DiskKeyData,
2801 desc.disk_key.discKeyStructures,
2802 2048);
2803 break;
2805 case DvdManufacturerDescriptor:
2806 memcpy(
2807 nt_desc.manf->ManufacturingInformation,
2808 desc.manf.discManufacturingInfo,
2809 2048);
2810 break;
2812 case DvdBCADescriptor:
2813 case DvdMaxDescriptor:
2814 default:
2815 /* Silence warning */
2816 break;
2819 return ret;
2820 #else
2821 FIXME("\n");
2822 #endif
2823 return STATUS_SUCCESS;
2827 /******************************************************************
2828 * GetInquiryData
2829 * Implements the IOCTL_GET_INQUIRY_DATA ioctl.
2830 * Returns Inquiry data for all devices on the specified scsi bus
2831 * Returns STATUS_BUFFER_TOO_SMALL if the output buffer is too small,
2832 * STATUS_INVALID_DEVICE_REQUEST if the given handle isn't to a SCSI device,
2833 * or STATUS_NOT_SUPPORTED if the OS driver is too old
2835 static NTSTATUS GetInquiryData(int fd, PSCSI_ADAPTER_BUS_INFO BufferOut, DWORD OutBufferSize)
2837 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
2838 PSCSI_INQUIRY_DATA pInquiryData = NULL;
2839 UCHAR sense_buffer[32];
2840 int iochk, version;
2841 sg_io_hdr_t iocmd;
2842 UCHAR inquiry[INQ_CMD_LEN] = {INQUIRY, 0, 0, 0, INQ_REPLY_LEN, 0};
2844 /* Check we have a SCSI device and a supported driver */
2845 if(ioctl(fd, SG_GET_VERSION_NUM, &version) != 0)
2847 WARN("IOCTL_SCSI_GET_INQUIRY_DATA sg driver is not loaded\n");
2848 return STATUS_INVALID_DEVICE_REQUEST;
2850 if(version < 30000 )
2851 return STATUS_NOT_SUPPORTED;
2853 /* FIXME: Enumerate devices on the bus */
2854 BufferOut->NumberOfBuses = 1;
2855 BufferOut->BusData[0].NumberOfLogicalUnits = 1;
2856 BufferOut->BusData[0].InquiryDataOffset = sizeof(SCSI_ADAPTER_BUS_INFO);
2858 pInquiryData = (PSCSI_INQUIRY_DATA)(BufferOut + 1);
2860 RtlZeroMemory(&iocmd, sizeof(iocmd));
2861 iocmd.interface_id = 'S';
2862 iocmd.cmd_len = sizeof(inquiry);
2863 iocmd.mx_sb_len = sizeof(sense_buffer);
2864 iocmd.dxfer_direction = SG_DXFER_FROM_DEV;
2865 iocmd.dxfer_len = INQ_REPLY_LEN;
2866 iocmd.dxferp = pInquiryData->InquiryData;
2867 iocmd.cmdp = inquiry;
2868 iocmd.sbp = sense_buffer;
2869 iocmd.timeout = 1000;
2871 iochk = ioctl(fd, SG_IO, &iocmd);
2872 if(iochk != 0)
2873 WARN("ioctl SG_IO returned %d, error (%s)\n", iochk, strerror(errno));
2875 CDROM_GetInterfaceInfo(fd, &BufferOut->BusData[0].InitiatorBusId, &pInquiryData->PathId, &pInquiryData->TargetId, &pInquiryData->Lun);
2876 pInquiryData->DeviceClaimed = TRUE;
2877 pInquiryData->InquiryDataLength = INQ_REPLY_LEN;
2878 pInquiryData->NextInquiryDataOffset = 0;
2879 return STATUS_SUCCESS;
2880 #else
2881 FIXME("not implemented for nonlinux\n");
2882 return STATUS_NOT_SUPPORTED;
2883 #endif
2886 /******************************************************************
2887 * CDROM_DeviceIoControl
2891 NTSTATUS CDROM_DeviceIoControl(HANDLE hDevice,
2892 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
2893 PVOID UserApcContext,
2894 PIO_STATUS_BLOCK piosb,
2895 ULONG dwIoControlCode,
2896 LPVOID lpInBuffer, DWORD nInBufferSize,
2897 LPVOID lpOutBuffer, DWORD nOutBufferSize)
2899 DWORD sz = 0;
2900 NTSTATUS status = STATUS_SUCCESS;
2901 int fd, needs_close, dev;
2903 TRACE("%p %s %p %d %p %d %p\n",
2904 hDevice, iocodex(dwIoControlCode), lpInBuffer, nInBufferSize,
2905 lpOutBuffer, nOutBufferSize, piosb);
2907 piosb->Information = 0;
2909 if ((status = server_get_unix_fd( hDevice, 0, &fd, &needs_close, NULL, NULL )))
2911 if (status == STATUS_BAD_DEVICE_TYPE) return status; /* no associated fd */
2912 goto error;
2915 if ((status = CDROM_Open(fd, &dev)))
2917 if (needs_close) close( fd );
2918 goto error;
2921 #ifdef __APPLE__
2923 char name[100];
2925 /* This is ugly as hell, but Mac OS is unable to do anything from the
2926 * partition fd, it wants an fd for the whole device, and it sometimes
2927 * also requires the device fd to be closed first, so we have to close
2928 * the handle that the caller gave us.
2929 * Also for some reason it wants the fd to be closed before we even
2930 * open the parent if we're trying to eject the disk.
2932 if ((status = get_parent_device( fd, name, sizeof(name) ))) goto error;
2933 if (dwIoControlCode == IOCTL_STORAGE_EJECT_MEDIA)
2934 NtClose( hDevice );
2935 if (needs_close) close( fd );
2936 TRACE("opening parent %s\n", name );
2937 if ((fd = open( name, O_RDONLY )) == -1)
2939 status = FILE_GetNtStatus();
2940 goto error;
2942 needs_close = 1;
2944 #endif
2946 switch (dwIoControlCode)
2948 case IOCTL_STORAGE_CHECK_VERIFY:
2949 case IOCTL_CDROM_CHECK_VERIFY:
2950 case IOCTL_DISK_CHECK_VERIFY:
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_Verify(dev, fd);
2956 break;
2958 /* EPP case IOCTL_STORAGE_CHECK_VERIFY2: */
2960 /* EPP case IOCTL_STORAGE_FIND_NEW_DEVICES: */
2961 /* EPP case IOCTL_CDROM_FIND_NEW_DEVICES: */
2963 case IOCTL_STORAGE_LOAD_MEDIA:
2964 case IOCTL_CDROM_LOAD_MEDIA:
2965 sz = 0;
2966 CDROM_ClearCacheEntry(dev);
2967 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2968 status = STATUS_INVALID_PARAMETER;
2969 else status = CDROM_SetTray(fd, FALSE);
2970 break;
2971 case IOCTL_STORAGE_EJECT_MEDIA:
2972 sz = 0;
2973 CDROM_ClearCacheEntry(dev);
2974 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2975 status = STATUS_INVALID_PARAMETER;
2976 else
2977 status = CDROM_SetTray(fd, TRUE);
2978 break;
2980 case IOCTL_CDROM_MEDIA_REMOVAL:
2981 case IOCTL_DISK_MEDIA_REMOVAL:
2982 case IOCTL_STORAGE_MEDIA_REMOVAL:
2983 case IOCTL_STORAGE_EJECTION_CONTROL:
2984 /* FIXME the last ioctl:s is not the same as the two others...
2985 * lockcount/owner should be handled */
2986 sz = 0;
2987 CDROM_ClearCacheEntry(dev);
2988 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2989 else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) status = STATUS_BUFFER_TOO_SMALL;
2990 else status = CDROM_ControlEjection(fd, lpInBuffer);
2991 break;
2993 case IOCTL_STORAGE_GET_MEDIA_TYPES:
2994 case IOCTL_STORAGE_GET_MEDIA_TYPES_EX:
2995 sz = sizeof(GET_MEDIA_TYPES);
2996 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2997 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2998 else status = CDROM_GetMediaType(dev, lpOutBuffer);
2999 break;
3001 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
3002 sz = sizeof(STORAGE_DEVICE_NUMBER);
3003 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3004 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3005 else status = CDROM_GetDeviceNumber(dev, lpOutBuffer);
3006 break;
3008 case IOCTL_STORAGE_RESET_DEVICE:
3009 sz = 0;
3010 CDROM_ClearCacheEntry(dev);
3011 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3012 status = STATUS_INVALID_PARAMETER;
3013 else status = CDROM_ResetAudio(fd);
3014 break;
3016 case IOCTL_CDROM_GET_CONTROL:
3017 sz = sizeof(CDROM_AUDIO_CONTROL);
3018 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3019 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3020 else status = CDROM_GetControl(dev, fd, lpOutBuffer);
3021 break;
3023 case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
3024 sz = sizeof(DISK_GEOMETRY);
3025 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3026 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3027 else status = CDROM_GetDriveGeometry(dev, fd, lpOutBuffer);
3028 break;
3030 case IOCTL_CDROM_DISK_TYPE:
3031 sz = sizeof(CDROM_DISK_DATA);
3032 /* CDROM_ClearCacheEntry(dev); */
3033 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3034 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3035 else status = CDROM_GetDiskData(dev, fd, lpOutBuffer);
3036 break;
3038 /* EPP case IOCTL_CDROM_GET_LAST_SESSION: */
3040 case IOCTL_CDROM_READ_Q_CHANNEL:
3041 sz = sizeof(SUB_Q_CHANNEL_DATA);
3042 if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT))
3043 status = STATUS_INVALID_PARAMETER;
3044 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3045 else status = CDROM_ReadQChannel(dev, fd, lpInBuffer, lpOutBuffer);
3046 break;
3048 case IOCTL_CDROM_READ_TOC:
3049 sz = sizeof(CDROM_TOC);
3050 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3051 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3052 else status = CDROM_ReadTOC(dev, fd, lpOutBuffer);
3053 break;
3055 /* EPP case IOCTL_CDROM_READ_TOC_EX: */
3057 case IOCTL_CDROM_PAUSE_AUDIO:
3058 sz = 0;
3059 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3060 status = STATUS_INVALID_PARAMETER;
3061 else status = CDROM_PauseAudio(fd);
3062 break;
3063 case IOCTL_CDROM_PLAY_AUDIO_MSF:
3064 sz = 0;
3065 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3066 else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
3067 else status = CDROM_PlayAudioMSF(fd, lpInBuffer);
3068 break;
3069 case IOCTL_CDROM_RESUME_AUDIO:
3070 sz = 0;
3071 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3072 status = STATUS_INVALID_PARAMETER;
3073 else status = CDROM_ResumeAudio(fd);
3074 break;
3075 case IOCTL_CDROM_SEEK_AUDIO_MSF:
3076 sz = 0;
3077 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3078 else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
3079 else status = CDROM_SeekAudioMSF(dev, fd, lpInBuffer);
3080 break;
3081 case IOCTL_CDROM_STOP_AUDIO:
3082 sz = 0;
3083 CDROM_ClearCacheEntry(dev); /* Maybe intention is to change media */
3084 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
3085 status = STATUS_INVALID_PARAMETER;
3086 else status = CDROM_StopAudio(fd);
3087 break;
3088 case IOCTL_CDROM_GET_VOLUME:
3089 sz = sizeof(VOLUME_CONTROL);
3090 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3091 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3092 else status = CDROM_GetVolume(fd, lpOutBuffer);
3093 break;
3094 case IOCTL_CDROM_SET_VOLUME:
3095 sz = 0;
3096 CDROM_ClearCacheEntry(dev);
3097 if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
3098 status = STATUS_INVALID_PARAMETER;
3099 else status = CDROM_SetVolume(fd, lpInBuffer);
3100 break;
3101 case IOCTL_CDROM_RAW_READ:
3102 sz = 0;
3103 if (nInBufferSize < sizeof(RAW_READ_INFO)) status = STATUS_INVALID_PARAMETER;
3104 else if (lpOutBuffer == NULL) status = STATUS_BUFFER_TOO_SMALL;
3105 else status = CDROM_RawRead(fd, lpInBuffer, lpOutBuffer,
3106 nOutBufferSize, &sz);
3107 break;
3108 case IOCTL_SCSI_GET_ADDRESS:
3109 sz = sizeof(SCSI_ADDRESS);
3110 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3111 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3112 else status = CDROM_GetAddress(fd, lpOutBuffer);
3113 break;
3114 case IOCTL_SCSI_PASS_THROUGH_DIRECT:
3115 sz = sizeof(SCSI_PASS_THROUGH_DIRECT);
3116 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3117 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH_DIRECT)) status = STATUS_BUFFER_TOO_SMALL;
3118 else status = CDROM_ScsiPassThroughDirect(fd, lpOutBuffer);
3119 break;
3120 case IOCTL_SCSI_PASS_THROUGH:
3121 sz = sizeof(SCSI_PASS_THROUGH);
3122 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3123 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH)) status = STATUS_BUFFER_TOO_SMALL;
3124 else status = CDROM_ScsiPassThrough(fd, lpOutBuffer);
3125 break;
3126 case IOCTL_SCSI_GET_CAPABILITIES:
3127 sz = sizeof(IO_SCSI_CAPABILITIES);
3128 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3129 else if (nOutBufferSize < sizeof(IO_SCSI_CAPABILITIES)) status = STATUS_BUFFER_TOO_SMALL;
3130 else status = CDROM_ScsiGetCaps(fd, lpOutBuffer);
3131 break;
3132 case IOCTL_DVD_START_SESSION:
3133 sz = sizeof(DVD_SESSION_ID);
3134 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
3135 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3136 else
3138 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3139 *(PDVD_SESSION_ID)lpOutBuffer);
3140 status = DVD_StartSession(fd, lpInBuffer, lpOutBuffer);
3141 TRACE("before in 0x%08x out 0x%08x\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
3142 *(PDVD_SESSION_ID)lpOutBuffer);
3144 break;
3145 case IOCTL_DVD_END_SESSION:
3146 sz = sizeof(DVD_SESSION_ID);
3147 if ((lpInBuffer == NULL) || (nInBufferSize < sz))status = STATUS_INVALID_PARAMETER;
3148 else status = DVD_EndSession(fd, lpInBuffer);
3149 break;
3150 case IOCTL_DVD_SEND_KEY:
3151 sz = 0;
3152 if (!lpInBuffer ||
3153 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3154 status = STATUS_INVALID_PARAMETER;
3155 else
3157 TRACE("doing DVD_SendKey\n");
3158 status = DVD_SendKey(fd, lpInBuffer);
3160 break;
3161 case IOCTL_DVD_READ_KEY:
3162 if (!lpInBuffer ||
3163 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
3164 status = STATUS_INVALID_PARAMETER;
3165 else if (lpInBuffer !=lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
3166 else
3168 TRACE("doing DVD_READ_KEY\n");
3169 sz = ((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength;
3170 status = DVD_ReadKey(fd, lpInBuffer);
3172 break;
3173 case IOCTL_DVD_GET_REGION:
3174 sz = sizeof(DVD_REGION);
3175 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
3176 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3177 TRACE("doing DVD_Get_REGION\n");
3178 status = DVD_GetRegion(fd, lpOutBuffer);
3179 break;
3180 case IOCTL_DVD_READ_STRUCTURE:
3181 sz = sizeof(DVD_LAYER_DESCRIPTOR);
3182 if (lpInBuffer == NULL || nInBufferSize != sizeof(DVD_READ_STRUCTURE)) status = STATUS_INVALID_PARAMETER;
3183 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
3184 else
3186 TRACE("doing DVD_READ_STRUCTURE\n");
3187 status = DVD_ReadStructure(fd, lpInBuffer, lpOutBuffer);
3189 break;
3191 case IOCTL_SCSI_GET_INQUIRY_DATA:
3192 sz = INQ_REPLY_LEN;
3193 status = GetInquiryData(fd, lpOutBuffer, nOutBufferSize);
3194 break;
3196 default:
3197 if (needs_close) close( fd );
3198 return STATUS_NOT_SUPPORTED;
3200 if (needs_close) close( fd );
3201 error:
3202 piosb->u.Status = status;
3203 piosb->Information = sz;
3204 if (hEvent) NtSetEvent(hEvent, NULL);
3205 return status;