Implement NtAccessCheck.
[wine/gsoc-2012-control.git] / dlls / ntdll / cdrom.c
blob42699829f186cd2f857880a6da15cf7ead1529c1
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
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <errno.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #ifdef HAVE_IO_H
31 # include <io.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <fcntl.h>
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
39 #endif
40 #include <sys/types.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
44 #endif
45 #ifdef HAVE_SCSI_SG_H
46 # include <scsi/sg.h>
47 #endif
48 #ifdef HAVE_SCSI_SCSI_H
49 # include <scsi/scsi.h>
50 # undef REASSIGN_BLOCKS /* avoid conflict with winioctl.h */
51 #endif
52 #ifdef HAVE_SCSI_SCSI_IOCTL_H
53 # include <scsi/scsi_ioctl.h>
54 #endif
55 #ifdef HAVE_LINUX_MAJOR_H
56 # include <linux/major.h>
57 #endif
58 #ifdef HAVE_LINUX_HDREG_H
59 # include <linux/hdreg.h>
60 #endif
61 #ifdef HAVE_LINUX_PARAM_H
62 # include <linux/param.h>
63 #endif
64 #ifdef HAVE_LINUX_CDROM_H
65 # include <linux/cdrom.h>
66 #endif
67 #ifdef HAVE_LINUX_UCDROM_H
68 # include <linux/ucdrom.h>
69 #endif
70 #ifdef HAVE_SYS_CDIO_H
71 # include <sys/cdio.h>
72 #endif
73 #ifdef HAVE_SYS_SCSIIO_H
74 # include <sys/scsiio.h>
75 #endif
77 #define NONAMELESSUNION
78 #define NONAMELESSSTRUCT
79 #include "ntstatus.h"
80 #include "windef.h"
81 #include "winbase.h"
82 #include "winreg.h"
83 #include "winternl.h"
84 #include "winioctl.h"
85 #include "ntddstor.h"
86 #include "ntddcdrm.h"
87 #include "ntddscsi.h"
88 #include "ntdll_misc.h"
89 #include "wine/server.h"
90 #include "wine/library.h"
91 #include "wine/debug.h"
93 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
94 lack the following constants. */
96 #ifndef CD_SECS
97 # define CD_SECS 60 /* seconds per minute */
98 #endif
99 #ifndef CD_FRAMES
100 # define CD_FRAMES 75 /* frames per second */
101 #endif
103 /* definitions taken from libdvdcss */
105 #define IOCTL_DVD_BASE FILE_DEVICE_DVD
107 #define IOCTL_DVD_START_SESSION CTL_CODE(IOCTL_DVD_BASE, 0x0400, METHOD_BUFFERED, FILE_READ_ACCESS)
108 #define IOCTL_DVD_READ_KEY CTL_CODE(IOCTL_DVD_BASE, 0x0401, METHOD_BUFFERED, FILE_READ_ACCESS)
109 #define IOCTL_DVD_SEND_KEY CTL_CODE(IOCTL_DVD_BASE, 0x0402, METHOD_BUFFERED, FILE_READ_ACCESS)
110 #define IOCTL_DVD_END_SESSION CTL_CODE(IOCTL_DVD_BASE, 0x0403, METHOD_BUFFERED, FILE_READ_ACCESS)
111 #define IOCTL_DVD_SET_READ_AHEAD CTL_CODE(IOCTL_DVD_BASE, 0x0404, METHOD_BUFFERED, FILE_READ_ACCESS)
112 #define IOCTL_DVD_GET_REGION CTL_CODE(IOCTL_DVD_BASE, 0x0405, METHOD_BUFFERED, FILE_READ_ACCESS)
113 #define IOCTL_DVD_SEND_KEY2 CTL_CODE(IOCTL_DVD_BASE, 0x0406, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
115 #define IOCTL_DVD_READ_STRUCTURE CTL_CODE(IOCTL_DVD_BASE, 0x0450, METHOD_BUFFERED, FILE_READ_ACCESS)
117 typedef enum {
118 DvdChallengeKey = 0x01,
119 DvdBusKey1,
120 DvdBusKey2,
121 DvdTitleKey,
122 DvdAsf,
123 DvdSetRpcKey = 0x6,
124 DvdGetRpcKey = 0x8,
125 DvdDiskKey = 0x80,
126 DvdInvalidateAGID = 0x3f
127 } DVD_KEY_TYPE;
129 typedef ULONG DVD_SESSION_ID, *PDVD_SESSION_ID;
131 typedef struct _DVD_COPY_PROTECT_KEY {
132 ULONG KeyLength;
133 DVD_SESSION_ID SessionId;
134 DVD_KEY_TYPE KeyType;
135 ULONG KeyFlags;
136 union {
137 struct {
138 ULONG FileHandle;
139 ULONG Reserved; /* used for NT alignment */
140 } s;
141 LARGE_INTEGER TitleOffset;
142 } Parameters;
143 UCHAR KeyData[1];
144 } DVD_COPY_PROTECT_KEY, *PDVD_COPY_PROTECT_KEY;
146 typedef struct _DVD_RPC_KEY {
147 UCHAR UserResetsAvailable:3;
148 UCHAR ManufacturerResetsAvailable:3;
149 UCHAR TypeCode:2;
150 UCHAR RegionMask;
151 UCHAR RpcScheme;
152 UCHAR Reserved2[1];
153 } DVD_RPC_KEY, * PDVD_RPC_KEY;
155 typedef struct _DVD_ASF {
156 UCHAR Reserved0[3];
157 UCHAR SuccessFlag:1;
158 UCHAR Reserved1:7;
159 } DVD_ASF, * PDVD_ASF;
161 typedef struct _DVD_REGION
163 unsigned char copy_system;
164 unsigned char region_data; /* current media region (not playable when set) */
165 unsigned char system_region; /* current drive region (playable when set) */
166 unsigned char reset_count; /* number of resets available */
167 } DVD_REGION, * PDVD_REGION;
169 typedef struct _DVD_READ_STRUCTURE {
170 /* Contains an offset to the logical block address of the descriptor to be retrieved. */
171 LARGE_INTEGER block_byte_offset;
173 /* 0:Physical descriptor, 1:Copyright descriptor, 2:Disk key descriptor
174 3:BCA descriptor, 4:Manufacturer descriptor, 5:Max descriptor
176 long format;
178 /* Session ID, that is obtained by IOCTL_DVD_START_SESSION */
179 long session;
181 /* From 0 to 4 */
182 unsigned char layer_no;
183 }DVD_READ_STRUCTURE, * PDVD_READ_STRUCTURE;
185 typedef struct _DVD_LAYER_DESCRIPTOR
187 unsigned short length;
189 unsigned char book_version : 4;
191 /* 0:DVD-ROM, 1:DVD-RAM, 2:DVD-R, 3:DVD-RW, 9:DVD-RW */
192 unsigned char book_type : 4;
194 unsigned char minimum_rate : 4;
196 /* The physical size of the media. 0:120 mm, 1:80 mm. */
197 unsigned char disk_size : 4;
199 /* 1:Read-only layer, 2:Recordable layer, 4:Rewritable layer */
200 unsigned char layer_type : 4;
202 /* 0:parallel track path, 1:opposite track path */
203 unsigned char track_path : 1;
205 /* 0:one layers, 1:two layers, and so on */
206 unsigned char num_of_layers : 2;
208 unsigned char reserved1 : 1;
210 /* 0:0.74 µm/track, 1:0.80 µm/track, 2:0.615 µm/track */
211 unsigned char track_density : 4;
213 /* 0:0.267 µm/bit, 1:0.293 µm/bit, 2:0.409 to 0.435 µm/bit, 4:0.280 to 0.291 µm/bit, 8:0.353 µm/bit */
214 unsigned char linear_density : 4;
216 /* Must be either 0x30000:DVD-ROM or DVD-R/-RW or 0x31000:DVD-RAM or DVD+RW */
217 unsigned long starting_data_sector;
219 unsigned long end_data_sector;
220 unsigned long end_layer_zero_sector;
221 unsigned char reserved5 : 7;
223 /* 0 indicates no BCA data */
224 unsigned char BCA_flag : 1;
226 unsigned char reserved6;
227 }DVD_LAYER_DESCRIPTOR, * PDVD_LAYER_DESCRIPTOR;
229 typedef struct _DVD_COPYRIGHT_DESCRIPTOR
231 unsigned char protection;
232 unsigned char region;
233 unsigned short reserved;
234 }DVD_COPYRIGHT_DESCRIPTOR, * PDVD_COPYRIGHT_DESCRIPTOR;
236 typedef struct _DVD_MANUFACTURER_DESCRIPTOR
238 unsigned char manufacturing[2048];
239 }DVD_MANUFACTURER_DESCRIPTOR, * PDVD_MANUFACTURER_DESCRIPTOR;
241 #define DVD_CHALLENGE_KEY_LENGTH (12 + sizeof(DVD_COPY_PROTECT_KEY) - sizeof(UCHAR))
243 #define DVD_DISK_KEY_LENGTH (2048 + sizeof(DVD_COPY_PROTECT_KEY) - sizeof(UCHAR))
245 #define DVD_KEY_SIZE 5
246 #define DVD_CHALLENGE_SIZE 10
247 #define DVD_DISCKEY_SIZE 2048
248 #define DVD_SECTOR_PROTECTED 0x00000020
250 static const struct iocodexs
252 DWORD code;
253 const char *codex;
254 } iocodextable[] = {
255 #define X(x) { x, #x },
256 X(IOCTL_CDROM_CHECK_VERIFY)
257 X(IOCTL_CDROM_CURRENT_POSITION)
258 X(IOCTL_CDROM_DISK_TYPE)
259 X(IOCTL_CDROM_GET_CONTROL)
260 X(IOCTL_CDROM_GET_DRIVE_GEOMETRY)
261 X(IOCTL_CDROM_GET_VOLUME)
262 X(IOCTL_CDROM_LOAD_MEDIA)
263 X(IOCTL_CDROM_MEDIA_CATALOG)
264 X(IOCTL_CDROM_MEDIA_REMOVAL)
265 X(IOCTL_CDROM_PAUSE_AUDIO)
266 X(IOCTL_CDROM_PLAY_AUDIO_MSF)
267 X(IOCTL_CDROM_RAW_READ)
268 X(IOCTL_CDROM_READ_Q_CHANNEL)
269 X(IOCTL_CDROM_READ_TOC)
270 X(IOCTL_CDROM_RESUME_AUDIO)
271 X(IOCTL_CDROM_SEEK_AUDIO_MSF)
272 X(IOCTL_CDROM_SET_VOLUME)
273 X(IOCTL_CDROM_STOP_AUDIO)
274 X(IOCTL_CDROM_TRACK_ISRC)
275 X(IOCTL_DISK_MEDIA_REMOVAL)
276 X(IOCTL_DVD_END_SESSION)
277 X(IOCTL_DVD_GET_REGION)
278 X(IOCTL_DVD_READ_KEY)
279 X(IOCTL_DVD_READ_STRUCTURE)
280 X(IOCTL_DVD_SEND_KEY)
281 X(IOCTL_DVD_START_SESSION)
282 X(IOCTL_SCSI_GET_ADDRESS)
283 X(IOCTL_SCSI_GET_CAPABILITIES)
284 X(IOCTL_SCSI_PASS_THROUGH)
285 X(IOCTL_SCSI_PASS_THROUGH_DIRECT)
286 X(IOCTL_STORAGE_CHECK_VERIFY)
287 X(IOCTL_STORAGE_EJECTION_CONTROL)
288 X(IOCTL_STORAGE_EJECT_MEDIA)
289 X(IOCTL_STORAGE_GET_DEVICE_NUMBER)
290 X(IOCTL_STORAGE_LOAD_MEDIA)
291 X(IOCTL_STORAGE_MEDIA_REMOVAL)
292 X(IOCTL_STORAGE_RESET_DEVICE)
293 #undef X
295 static const char *iocodex(DWORD code)
297 unsigned int i;
298 static char buffer[25];
299 for(i=0; i<sizeof(iocodextable)/sizeof(struct iocodexs); i++)
300 if (code==iocodextable[i].code)
301 return iocodextable[i].codex;
302 sprintf(buffer, "IOCTL_CODE_%x", (int)code);
303 return buffer;
306 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
308 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
309 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
310 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
311 #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;}
313 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
314 static NTSTATUS CDROM_GetStatusCode(int);
317 #ifdef linux
319 # ifndef IDE6_MAJOR
320 # define IDE6_MAJOR 88
321 # endif
322 # ifndef IDE7_MAJOR
323 # define IDE7_MAJOR 89
324 # endif
326 # ifdef CDROM_SEND_PACKET
327 /* structure for CDROM_PACKET_COMMAND ioctl */
328 /* not all Linux versions have all the fields, so we define the
329 * structure ourselves to make sure */
330 struct linux_cdrom_generic_command
332 unsigned char cmd[CDROM_PACKET_SIZE];
333 unsigned char *buffer;
334 unsigned int buflen;
335 int stat;
336 struct request_sense *sense;
337 unsigned char data_direction;
338 int quiet;
339 int timeout;
340 void *reserved[1];
342 # endif /* CDROM_SEND_PACKET */
344 #endif /* linux */
346 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
347 * this should be removed when a proper device interface is implemented
349 * (WS) We need this to keep track of current position and to safely
350 * detect media changes. Besides this should provide a great speed up
351 * for toc inquiries.
353 struct cdrom_cache {
354 dev_t device;
355 ino_t inode;
356 char toc_good; /* if false, will reread TOC from disk */
357 CDROM_TOC toc;
358 SUB_Q_CURRENT_POSITION CurrentPosition;
360 /* who has more than 5 cdroms on his/her machine ?? */
361 /* FIXME: this should grow depending on the number of cdroms we install/configure
362 * at startup
364 #define MAX_CACHE_ENTRIES 5
365 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
367 static CRITICAL_SECTION cache_section;
368 static CRITICAL_SECTION_DEBUG critsect_debug =
370 0, 0, &cache_section,
371 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
372 0, 0, { 0, (DWORD)(__FILE__ ": cache_section") }
374 static CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
376 /* Proposed media change function: not really needed at this time */
377 /* This is a 1 or 0 type of function */
378 #if 0
379 static int CDROM_MediaChanged(int dev)
381 int i;
383 struct cdrom_tochdr hdr;
384 struct cdrom_tocentry entry;
386 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
387 return 0;
388 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
389 return 0;
391 if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
392 return 1;
394 for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
396 if (i == hdr.cdth_trk1 + 1)
398 entry.cdte_track = CDROM_LEADOUT;
399 } else {
400 entry.cdte_track = i;
402 entry.cdte_format = CDROM_MSF;
403 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
404 return 0;
405 if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
406 sizeof(struct cdrom_tocentry)) )
407 return 1;
409 return 0;
411 #endif
413 /******************************************************************
414 * CDROM_SyncCache [internal]
416 * Read the TOC in and store it in the cdrom_cache structure.
417 * Further requests for the TOC will be copied from the cache
418 * unless certain events like disk ejection is detected, in which
419 * case the cache will be cleared, causing it to be resynced.
420 * The cache section must be held by caller.
422 static int CDROM_SyncCache(int dev, int fd)
424 int i, io = 0, tsz;
425 #ifdef linux
426 struct cdrom_tochdr hdr;
427 struct cdrom_tocentry entry;
428 #elif defined(__FreeBSD__) || defined(__NetBSD__)
429 struct ioc_toc_header hdr;
430 struct ioc_read_toc_entry entry;
431 struct cd_toc_entry toc_buffer;
432 #endif
433 CDROM_TOC *toc = &cdrom_cache[dev].toc;
434 cdrom_cache[dev].toc_good = 0;
436 #ifdef linux
438 io = ioctl(fd, CDROMREADTOCHDR, &hdr);
439 if (io == -1)
441 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
442 goto end;
445 toc->FirstTrack = hdr.cdth_trk0;
446 toc->LastTrack = hdr.cdth_trk1;
447 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
448 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
449 toc->Length[0] = tsz >> 8;
450 toc->Length[1] = tsz;
452 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
454 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
456 if (i == toc->LastTrack + 1)
457 entry.cdte_track = CDROM_LEADOUT;
458 else
459 entry.cdte_track = i;
460 entry.cdte_format = CDROM_MSF;
461 io = ioctl(fd, CDROMREADTOCENTRY, &entry);
462 if (io == -1) {
463 WARN("error read entry (%s)\n", strerror(errno));
464 goto end;
466 toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
467 toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
468 /* marking last track with leadout value as index */
469 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
470 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
471 toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
472 toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
473 toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
475 cdrom_cache[dev].toc_good = 1;
476 io = 0;
477 #elif defined(__FreeBSD__) || defined(__NetBSD__)
479 io = ioctl(fd, CDIOREADTOCHEADER, &hdr);
480 if (io == -1)
482 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
483 goto end;
485 toc->FirstTrack = hdr.starting_track;
486 toc->LastTrack = hdr.ending_track;
487 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
488 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
489 toc->Length[0] = tsz >> 8;
490 toc->Length[1] = tsz;
492 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
494 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
496 if (i == toc->LastTrack + 1)
498 #define LEADOUT 0xaa
499 entry.starting_track = LEADOUT;
500 } else {
501 entry.starting_track = i;
503 memset((char *)&toc_buffer, 0, sizeof(toc_buffer));
504 entry.address_format = CD_MSF_FORMAT;
505 entry.data_len = sizeof(toc_buffer);
506 entry.data = &toc_buffer;
507 io = ioctl(fd, CDIOREADTOCENTRYS, &entry);
508 if (io == -1) {
509 WARN("error read entry (%s)\n", strerror(errno));
510 goto end;
512 toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
513 toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
514 /* marking last track with leadout value as index */
515 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
516 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
517 toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
518 toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
519 toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
521 cdrom_cache[dev].toc_good = 1;
522 io = 0;
523 #else
524 return STATUS_NOT_SUPPORTED;
525 #endif
526 end:
527 return CDROM_GetStatusCode(io);
530 static void CDROM_ClearCacheEntry(int dev)
532 RtlEnterCriticalSection( &cache_section );
533 cdrom_cache[dev].toc_good = 0;
534 RtlLeaveCriticalSection( &cache_section );
539 /******************************************************************
540 * CDROM_GetInterfaceInfo
542 * Determines the ide interface (the number after the ide), and the
543 * number of the device on that interface for ide cdroms (*port == 0).
544 * Determines the scsi information for scsi cdroms (*port >= 1).
545 * Returns false if the info cannot not be obtained.
547 static int CDROM_GetInterfaceInfo(int fd, int* port, int* iface, int* device,int* lun)
549 #if defined(linux)
550 struct stat st;
551 if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode)) return 0;
552 *port = 0;
553 *iface = 0;
554 *device = 0;
555 *lun = 0;
556 switch (major(st.st_rdev)) {
557 case IDE0_MAJOR: *iface = 0; break;
558 case IDE1_MAJOR: *iface = 1; break;
559 case IDE2_MAJOR: *iface = 2; break;
560 case IDE3_MAJOR: *iface = 3; break;
561 case IDE4_MAJOR: *iface = 4; break;
562 case IDE5_MAJOR: *iface = 5; break;
563 case IDE6_MAJOR: *iface = 6; break;
564 case IDE7_MAJOR: *iface = 7; break;
565 default: *port = 1; break;
568 if (*port == 0)
569 *device = (minor(st.st_rdev) >> 6);
570 else
572 #ifdef SCSI_IOCTL_GET_IDLUN
573 UINT32 idlun[2];
574 if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, &idlun) != -1)
576 *port = ((idlun[0] >> 24) & 0xff) + 1;
577 *iface = (idlun[0] >> 16) & 0xff;
578 *device = idlun[0] & 0xff;
579 *lun = (idlun[0] >> 8) & 0xff;
581 else
582 #endif
584 WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
585 return 0;
588 return 1;
589 #elif defined(__NetBSD__)
590 struct scsi_addr addr;
591 if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
593 switch (addr.type)
595 case TYPE_SCSI: *port = 1;
596 *iface = addr.addr.scsi.scbus;
597 *device = addr.addr.scsi.target;
598 *lun = addr.addr.scsi.lun;
599 break;
600 case TYPE_ATAPI: *port = 0;
601 *iface = addr.addr.atapi.atbus;
602 *device = addr.addr.atapi.drive;
603 *lun = 0;
604 break;
606 return 1;
608 return 0;
609 #elif defined(__FreeBSD__)
610 FIXME("not implemented for BSD\n");
611 return 0;
612 #else
613 FIXME("not implemented for nonlinux\n");
614 return 0;
615 #endif
619 /******************************************************************
620 * CDROM_Open
623 static NTSTATUS CDROM_Open(int fd, int* dev)
625 struct stat st;
626 NTSTATUS ret = STATUS_SUCCESS;
627 int empty = -1;
629 fstat(fd, &st);
631 RtlEnterCriticalSection( &cache_section );
632 for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
634 if (empty == -1 &&
635 cdrom_cache[*dev].device == 0 &&
636 cdrom_cache[*dev].inode == 0)
637 empty = *dev;
638 else if (cdrom_cache[*dev].device == st.st_dev &&
639 cdrom_cache[*dev].inode == st.st_ino)
640 break;
642 if (*dev == MAX_CACHE_ENTRIES)
644 if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
645 else
647 *dev = empty;
648 cdrom_cache[*dev].device = st.st_dev;
649 cdrom_cache[*dev].inode = st.st_ino;
652 RtlLeaveCriticalSection( &cache_section );
654 TRACE("%d, %d\n", *dev, fd);
655 return ret;
658 /******************************************************************
659 * CDROM_GetStatusCode
663 static NTSTATUS CDROM_GetStatusCode(int io)
665 if (io == 0) return STATUS_SUCCESS;
666 return FILE_GetNtStatus();
669 /******************************************************************
670 * CDROM_GetControl
673 static NTSTATUS CDROM_GetControl(int dev, CDROM_AUDIO_CONTROL* cac)
675 cac->LbaFormat = 0; /* FIXME */
676 cac->LogicalBlocksPerSecond = 1; /* FIXME */
677 return STATUS_NOT_SUPPORTED;
680 /******************************************************************
681 * CDROM_GetDeviceNumber
684 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
686 return STATUS_NOT_SUPPORTED;
689 /******************************************************************
690 * CDROM_GetDriveGeometry
693 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
695 CDROM_TOC toc;
696 NTSTATUS ret = 0;
697 int fsize = 0;
699 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
701 fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
702 - FRAME_OF_TOC(toc, 1); /* Total size in frames */
704 dg->Cylinders.u.LowPart = fsize / (64 * 32);
705 dg->Cylinders.u.HighPart = 0;
706 dg->MediaType = RemovableMedia;
707 dg->TracksPerCylinder = 64;
708 dg->SectorsPerTrack = 32;
709 dg->BytesPerSector= 2048;
710 return ret;
713 /**************************************************************************
714 * CDROM_Reset [internal]
716 static NTSTATUS CDROM_ResetAudio(int fd)
718 #if defined(linux)
719 return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
720 #elif defined(__FreeBSD__) || defined(__NetBSD__)
721 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
722 #else
723 return STATUS_NOT_SUPPORTED;
724 #endif
727 /******************************************************************
728 * CDROM_SetTray
732 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
734 #if defined(linux)
735 return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
736 #elif defined(__FreeBSD__) || defined(__NetBSD__)
737 return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
738 (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
739 (ioctl(fd, CDIOCPREVENT, NULL)));
740 #else
741 return STATUS_NOT_SUPPORTED;
742 #endif
745 /******************************************************************
746 * CDROM_ControlEjection
750 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
752 #if defined(linux)
753 return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
754 #elif defined(__FreeBSD__) || defined(__NetBSD__)
755 return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
756 #else
757 return STATUS_NOT_SUPPORTED;
758 #endif
761 /******************************************************************
762 * CDROM_ReadTOC
766 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
768 NTSTATUS ret = STATUS_NOT_SUPPORTED;
770 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
771 return STATUS_INVALID_PARAMETER;
773 RtlEnterCriticalSection( &cache_section );
774 if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
776 *toc = cdrom_cache[dev].toc;
777 ret = STATUS_SUCCESS;
779 RtlLeaveCriticalSection( &cache_section );
780 return ret;
783 /******************************************************************
784 * CDROM_GetDiskData
788 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
790 CDROM_TOC toc;
791 NTSTATUS ret;
792 int i;
794 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
795 data->DiskData = 0;
796 for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
797 if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
798 data->DiskData |= CDROM_DISK_DATA_TRACK;
799 else
800 data->DiskData |= CDROM_DISK_AUDIO_TRACK;
802 return STATUS_SUCCESS;
805 /******************************************************************
806 * CDROM_ReadQChannel
810 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
811 SUB_Q_CHANNEL_DATA* data)
813 NTSTATUS ret = STATUS_NOT_SUPPORTED;
814 #ifdef linux
815 unsigned size;
816 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
817 int io;
818 struct cdrom_subchnl sc;
819 sc.cdsc_format = CDROM_MSF;
821 io = ioctl(fd, CDROMSUBCHNL, &sc);
822 if (io == -1)
824 TRACE("opened or no_media (%s)!\n", strerror(errno));
825 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
826 CDROM_ClearCacheEntry(dev);
827 goto end;
830 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
832 switch (sc.cdsc_audiostatus) {
833 case CDROM_AUDIO_INVALID:
834 CDROM_ClearCacheEntry(dev);
835 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
836 break;
837 case CDROM_AUDIO_NO_STATUS:
838 CDROM_ClearCacheEntry(dev);
839 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
840 break;
841 case CDROM_AUDIO_PLAY:
842 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
843 break;
844 case CDROM_AUDIO_PAUSED:
845 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
846 break;
847 case CDROM_AUDIO_COMPLETED:
848 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
849 break;
850 case CDROM_AUDIO_ERROR:
851 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
852 break;
853 default:
854 TRACE("status=%02X !\n", sc.cdsc_audiostatus);
855 break;
857 switch (fmt->Format)
859 case IOCTL_CDROM_CURRENT_POSITION:
860 size = sizeof(SUB_Q_CURRENT_POSITION);
861 RtlEnterCriticalSection( &cache_section );
862 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
863 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
864 data->CurrentPosition.Control = sc.cdsc_ctrl;
865 data->CurrentPosition.ADR = sc.cdsc_adr;
866 data->CurrentPosition.TrackNumber = sc.cdsc_trk;
867 data->CurrentPosition.IndexNumber = sc.cdsc_ind;
869 data->CurrentPosition.AbsoluteAddress[0] = 0;
870 data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute;
871 data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
872 data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
874 data->CurrentPosition.TrackRelativeAddress[0] = 0;
875 data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute;
876 data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
877 data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
879 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
881 else /* not playing */
883 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
884 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
886 RtlLeaveCriticalSection( &cache_section );
887 break;
888 case IOCTL_CDROM_MEDIA_CATALOG:
889 size = sizeof(SUB_Q_MEDIA_CATALOG_NUMBER);
890 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
892 struct cdrom_mcn mcn;
893 if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
895 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
896 data->MediaCatalog.Mcval = 0; /* FIXME */
897 memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
898 data->MediaCatalog.MediaCatalog[14] = 0;
900 break;
901 case IOCTL_CDROM_TRACK_ISRC:
902 size = sizeof(SUB_Q_CURRENT_POSITION);
903 FIXME("TrackIsrc: NIY on linux\n");
904 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
905 data->TrackIsrc.Tcval = 0;
906 io = 0;
907 break;
910 end:
911 ret = CDROM_GetStatusCode(io);
912 #elif defined(__FreeBSD__) || defined(__NetBSD__)
913 unsigned size;
914 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
915 int io;
916 struct ioc_read_subchannel read_sc;
917 struct cd_sub_channel_info sc;
919 read_sc.address_format = CD_MSF_FORMAT;
920 read_sc.track = 0;
921 read_sc.data_len = sizeof(sc);
922 read_sc.data = &sc;
923 switch (fmt->Format)
925 case IOCTL_CDROM_CURRENT_POSITION:
926 read_sc.data_format = CD_CURRENT_POSITION;
927 break;
928 case IOCTL_CDROM_MEDIA_CATALOG:
929 read_sc.data_format = CD_MEDIA_CATALOG;
930 break;
931 case IOCTL_CDROM_TRACK_ISRC:
932 read_sc.data_format = CD_TRACK_INFO;
933 sc.what.track_info.track_number = data->TrackIsrc.Track;
934 break;
936 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
937 if (io == -1)
939 TRACE("opened or no_media (%s)!\n", strerror(errno));
940 CDROM_ClearCacheEntry(dev);
941 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
942 goto end;
945 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
947 switch (sc.header.audio_status) {
948 case CD_AS_AUDIO_INVALID:
949 CDROM_ClearCacheEntry(dev);
950 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
951 break;
952 case CD_AS_NO_STATUS:
953 CDROM_ClearCacheEntry(dev);
954 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
955 break;
956 case CD_AS_PLAY_IN_PROGRESS:
957 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
958 break;
959 case CD_AS_PLAY_PAUSED:
960 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
961 break;
962 case CD_AS_PLAY_COMPLETED:
963 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
964 break;
965 case CD_AS_PLAY_ERROR:
966 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
967 break;
968 default:
969 TRACE("status=%02X !\n", sc.header.audio_status);
971 switch (fmt->Format)
973 case IOCTL_CDROM_CURRENT_POSITION:
974 size = sizeof(SUB_Q_CURRENT_POSITION);
975 RtlEnterCriticalSection( &cache_section );
976 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
977 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
978 data->CurrentPosition.Control = sc.what.position.control;
979 data->CurrentPosition.ADR = sc.what.position.addr_type;
980 data->CurrentPosition.TrackNumber = sc.what.position.track_number;
981 data->CurrentPosition.IndexNumber = sc.what.position.index_number;
983 data->CurrentPosition.AbsoluteAddress[0] = 0;
984 data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
985 data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
986 data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
987 data->CurrentPosition.TrackRelativeAddress[0] = 0;
988 data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
989 data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
990 data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
991 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
993 else { /* not playing */
994 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
995 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
997 RtlLeaveCriticalSection( &cache_section );
998 break;
999 case IOCTL_CDROM_MEDIA_CATALOG:
1000 size = sizeof(SUB_Q_MEDIA_CATALOG_NUMBER);
1001 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
1002 data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
1003 memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
1004 break;
1005 case IOCTL_CDROM_TRACK_ISRC:
1006 size = sizeof(SUB_Q_CURRENT_POSITION);
1007 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
1008 data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
1009 memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
1010 break;
1013 end:
1014 ret = CDROM_GetStatusCode(io);
1015 #endif
1016 return ret;
1019 /******************************************************************
1020 * CDROM_Verify
1024 static NTSTATUS CDROM_Verify(int dev, int fd)
1026 /* quick implementation */
1027 CDROM_SUB_Q_DATA_FORMAT fmt;
1028 SUB_Q_CHANNEL_DATA data;
1030 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
1031 return CDROM_ReadQChannel(dev, fd, &fmt, &data) ? 1 : 0;
1034 /******************************************************************
1035 * CDROM_PlayAudioMSF
1039 static NTSTATUS CDROM_PlayAudioMSF(int fd, const CDROM_PLAY_AUDIO_MSF* audio_msf)
1041 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1042 #ifdef linux
1043 struct cdrom_msf msf;
1044 int io;
1046 msf.cdmsf_min0 = audio_msf->StartingM;
1047 msf.cdmsf_sec0 = audio_msf->StartingS;
1048 msf.cdmsf_frame0 = audio_msf->StartingF;
1049 msf.cdmsf_min1 = audio_msf->EndingM;
1050 msf.cdmsf_sec1 = audio_msf->EndingS;
1051 msf.cdmsf_frame1 = audio_msf->EndingF;
1053 io = ioctl(fd, CDROMSTART);
1054 if (io == -1)
1056 WARN("motor doesn't start !\n");
1057 goto end;
1059 io = ioctl(fd, CDROMPLAYMSF, &msf);
1060 if (io == -1)
1062 WARN("device doesn't play !\n");
1063 goto end;
1065 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1066 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
1067 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
1068 end:
1069 ret = CDROM_GetStatusCode(io);
1070 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1071 struct ioc_play_msf msf;
1072 int io;
1074 msf.start_m = audio_msf->StartingM;
1075 msf.start_s = audio_msf->StartingS;
1076 msf.start_f = audio_msf->StartingF;
1077 msf.end_m = audio_msf->EndingM;
1078 msf.end_s = audio_msf->EndingS;
1079 msf.end_f = audio_msf->EndingF;
1081 io = ioctl(fd, CDIOCSTART, NULL);
1082 if (io == -1)
1084 WARN("motor doesn't start !\n");
1085 goto end;
1087 io = ioctl(fd, CDIOCPLAYMSF, &msf);
1088 if (io == -1)
1090 WARN("device doesn't play !\n");
1091 goto end;
1093 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1094 msf.start_m, msf.start_s, msf.start_f,
1095 msf.end_m, msf.end_s, msf.end_f);
1096 end:
1097 ret = CDROM_GetStatusCode(io);
1098 #endif
1099 return ret;
1102 /******************************************************************
1103 * CDROM_SeekAudioMSF
1107 static NTSTATUS CDROM_SeekAudioMSF(int dev, int fd, const CDROM_SEEK_AUDIO_MSF* audio_msf)
1109 CDROM_TOC toc;
1110 int i, io, frame;
1111 SUB_Q_CURRENT_POSITION *cp;
1112 #if defined(linux)
1113 struct cdrom_msf0 msf;
1114 struct cdrom_subchnl sc;
1115 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1116 struct ioc_play_msf msf;
1117 struct ioc_read_subchannel read_sc;
1118 struct cd_sub_channel_info sc;
1119 int final_frame;
1120 #endif
1122 /* Use the information on the TOC to compute the new current
1123 * position, which is shadowed on the cache. [Portable]. */
1124 frame = FRAME_OF_MSF(*audio_msf);
1126 if ((io = CDROM_ReadTOC(dev, fd, &toc)) != 0) return io;
1128 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
1129 if (FRAME_OF_TOC(toc,i)>frame) break;
1130 if (i <= toc.FirstTrack || i > toc.LastTrack+1)
1131 return STATUS_INVALID_PARAMETER;
1132 i--;
1133 RtlEnterCriticalSection( &cache_section );
1134 cp = &cdrom_cache[dev].CurrentPosition;
1135 cp->FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1136 cp->Control = toc.TrackData[i-toc.FirstTrack].Control;
1137 cp->ADR = toc.TrackData[i-toc.FirstTrack].Adr;
1138 cp->TrackNumber = toc.TrackData[i-toc.FirstTrack].TrackNumber;
1139 cp->IndexNumber = 0; /* FIXME: where do they keep these? */
1140 cp->AbsoluteAddress[0] = 0;
1141 cp->AbsoluteAddress[1] = toc.TrackData[i-toc.FirstTrack].Address[1];
1142 cp->AbsoluteAddress[2] = toc.TrackData[i-toc.FirstTrack].Address[2];
1143 cp->AbsoluteAddress[3] = toc.TrackData[i-toc.FirstTrack].Address[3];
1144 frame -= FRAME_OF_TOC(toc,i);
1145 cp->TrackRelativeAddress[0] = 0;
1146 MSF_OF_FRAME(cp->TrackRelativeAddress[1], frame);
1147 RtlLeaveCriticalSection( &cache_section );
1149 /* If playing, then issue a seek command, otherwise do nothing */
1150 #ifdef linux
1151 sc.cdsc_format = CDROM_MSF;
1153 io = ioctl(fd, CDROMSUBCHNL, &sc);
1154 if (io == -1)
1156 TRACE("opened or no_media (%s)!\n", strerror(errno));
1157 CDROM_ClearCacheEntry(dev);
1158 return CDROM_GetStatusCode(io);
1160 if (sc.cdsc_audiostatus==CDROM_AUDIO_PLAY)
1162 msf.minute = audio_msf->M;
1163 msf.second = audio_msf->S;
1164 msf.frame = audio_msf->F;
1165 return CDROM_GetStatusCode(ioctl(fd, CDROMSEEK, &msf));
1167 return STATUS_SUCCESS;
1168 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1169 read_sc.address_format = CD_MSF_FORMAT;
1170 read_sc.track = 0;
1171 read_sc.data_len = sizeof(sc);
1172 read_sc.data = &sc;
1173 read_sc.data_format = CD_CURRENT_POSITION;
1175 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1176 if (io == -1)
1178 TRACE("opened or no_media (%s)!\n", strerror(errno));
1179 CDROM_ClearCacheEntry(dev);
1180 return CDROM_GetStatusCode(io);
1182 if (sc.header.audio_status==CD_AS_PLAY_IN_PROGRESS)
1185 msf.start_m = audio_msf->M;
1186 msf.start_s = audio_msf->S;
1187 msf.start_f = audio_msf->F;
1188 final_frame = FRAME_OF_TOC(toc,toc.LastTrack+1)-1;
1189 MSF_OF_FRAME(msf.end_m, final_frame);
1191 return CDROM_GetStatusCode(ioctl(fd, CDIOCPLAYMSF, &msf));
1193 return STATUS_SUCCESS;
1194 #else
1195 return STATUS_NOT_SUPPORTED;
1196 #endif
1199 /******************************************************************
1200 * CDROM_PauseAudio
1204 static NTSTATUS CDROM_PauseAudio(int fd)
1206 #if defined(linux)
1207 return CDROM_GetStatusCode(ioctl(fd, CDROMPAUSE));
1208 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1209 return CDROM_GetStatusCode(ioctl(fd, CDIOCPAUSE, NULL));
1210 #else
1211 return STATUS_NOT_SUPPORTED;
1212 #endif
1215 /******************************************************************
1216 * CDROM_ResumeAudio
1220 static NTSTATUS CDROM_ResumeAudio(int fd)
1222 #if defined(linux)
1223 return CDROM_GetStatusCode(ioctl(fd, CDROMRESUME));
1224 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1225 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESUME, NULL));
1226 #else
1227 return STATUS_NOT_SUPPORTED;
1228 #endif
1231 /******************************************************************
1232 * CDROM_StopAudio
1236 static NTSTATUS CDROM_StopAudio(int fd)
1238 #if defined(linux)
1239 return CDROM_GetStatusCode(ioctl(fd, CDROMSTOP));
1240 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1241 return CDROM_GetStatusCode(ioctl(fd, CDIOCSTOP, NULL));
1242 #else
1243 return STATUS_NOT_SUPPORTED;
1244 #endif
1247 /******************************************************************
1248 * CDROM_GetVolume
1252 static NTSTATUS CDROM_GetVolume(int fd, VOLUME_CONTROL* vc)
1254 #if defined(linux)
1255 struct cdrom_volctrl volc;
1256 int io;
1258 io = ioctl(fd, CDROMVOLREAD, &volc);
1259 if (io != -1)
1261 vc->PortVolume[0] = volc.channel0;
1262 vc->PortVolume[1] = volc.channel1;
1263 vc->PortVolume[2] = volc.channel2;
1264 vc->PortVolume[3] = volc.channel3;
1266 return CDROM_GetStatusCode(io);
1267 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1268 struct ioc_vol volc;
1269 int io;
1271 io = ioctl(fd, CDIOCGETVOL, &volc);
1272 if (io != -1)
1274 vc->PortVolume[0] = volc.vol[0];
1275 vc->PortVolume[1] = volc.vol[1];
1276 vc->PortVolume[2] = volc.vol[2];
1277 vc->PortVolume[3] = volc.vol[3];
1279 return CDROM_GetStatusCode(io);
1280 #else
1281 return STATUS_NOT_SUPPORTED;
1282 #endif
1285 /******************************************************************
1286 * CDROM_SetVolume
1290 static NTSTATUS CDROM_SetVolume(int fd, const VOLUME_CONTROL* vc)
1292 #if defined(linux)
1293 struct cdrom_volctrl volc;
1295 volc.channel0 = vc->PortVolume[0];
1296 volc.channel1 = vc->PortVolume[1];
1297 volc.channel2 = vc->PortVolume[2];
1298 volc.channel3 = vc->PortVolume[3];
1300 return CDROM_GetStatusCode(ioctl(fd, CDROMVOLCTRL, &volc));
1301 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1302 struct ioc_vol volc;
1304 volc.vol[0] = vc->PortVolume[0];
1305 volc.vol[1] = vc->PortVolume[1];
1306 volc.vol[2] = vc->PortVolume[2];
1307 volc.vol[3] = vc->PortVolume[3];
1309 return CDROM_GetStatusCode(ioctl(fd, CDIOCSETVOL, &volc));
1310 #else
1311 return STATUS_NOT_SUPPORTED;
1312 #endif
1315 /******************************************************************
1316 * CDROM_RawRead
1320 static NTSTATUS CDROM_RawRead(int fd, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
1322 int ret = STATUS_NOT_SUPPORTED;
1323 int io = -1;
1324 DWORD sectSize;
1326 switch (raw->TrackMode)
1328 case YellowMode2: sectSize = 2336; break;
1329 case XAForm2: sectSize = 2328; break;
1330 case CDDA: sectSize = 2352; break;
1331 default: return STATUS_INVALID_PARAMETER;
1333 if (len < raw->SectorCount * sectSize) return STATUS_BUFFER_TOO_SMALL;
1334 /* strangely enough, it seems that sector offsets are always indicated with a size of 2048,
1335 * even if a larger size if read...
1337 #if defined(linux)
1339 struct cdrom_read cdr;
1340 struct cdrom_read_audio cdra;
1342 switch (raw->TrackMode)
1344 case YellowMode2:
1345 if (raw->DiskOffset.u.HighPart) FIXME("Unsupported value\n");
1346 cdr.cdread_lba = raw->DiskOffset.u.LowPart; /* FIXME ? */
1347 cdr.cdread_bufaddr = buffer;
1348 cdr.cdread_buflen = raw->SectorCount * sectSize;
1349 io = ioctl(fd, CDROMREADMODE2, &cdr);
1350 break;
1351 case XAForm2:
1352 FIXME("XAForm2: NIY\n");
1353 return ret;
1354 case CDDA:
1355 /* FIXME: the output doesn't seem 100% correct... in fact output is shifted
1356 * between by NT2K box and this... should check on the same drive...
1357 * otherwise, I fear a 2352/2368 mismatch somewhere in one of the drivers
1358 * (linux/NT).
1359 * Anyway, that's not critical at all. We're talking of 16/32 bytes, we're
1360 * talking of 0.2 ms of sound
1362 /* 2048 = 2 ** 11 */
1363 if (raw->DiskOffset.u.HighPart & ~2047) FIXME("Unsupported value\n");
1364 cdra.addr.lba = ((raw->DiskOffset.u.LowPart >> 11) |
1365 (raw->DiskOffset.u.HighPart << (32 - 11))) - 1;
1366 FIXME("reading at %u\n", cdra.addr.lba);
1367 cdra.addr_format = CDROM_LBA;
1368 cdra.nframes = raw->SectorCount;
1369 cdra.buf = buffer;
1370 io = ioctl(fd, CDROMREADAUDIO, &cdra);
1371 break;
1372 default:
1373 FIXME("NIY: %d\n", raw->TrackMode);
1374 return ret;
1377 #else
1379 switch (raw->TrackMode)
1381 case YellowMode2:
1382 FIXME("YellowMode2: NIY\n");
1383 return ret;
1384 case XAForm2:
1385 FIXME("XAForm2: NIY\n");
1386 return ret;
1387 case CDDA:
1388 FIXME("CDDA: NIY\n");
1389 return ret;
1392 #endif
1394 *sz = sectSize * raw->SectorCount;
1395 ret = CDROM_GetStatusCode(io);
1396 return ret;
1399 /******************************************************************
1400 * CDROM_ScsiPassThroughDirect
1404 static NTSTATUS CDROM_ScsiPassThroughDirect(int fd, PSCSI_PASS_THROUGH_DIRECT pPacket)
1406 int ret = STATUS_NOT_SUPPORTED;
1407 #if defined(linux) && defined(CDROM_SEND_PACKET)
1408 struct linux_cdrom_generic_command cmd;
1409 struct request_sense sense;
1410 int io;
1412 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1413 return STATUS_BUFFER_TOO_SMALL;
1415 if (pPacket->CdbLength > 12)
1416 return STATUS_INVALID_PARAMETER;
1418 if (pPacket->SenseInfoLength > sizeof(sense))
1419 return STATUS_INVALID_PARAMETER;
1421 memset(&cmd, 0, sizeof(cmd));
1422 memset(&sense, 0, sizeof(sense));
1424 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1426 cmd.buffer = pPacket->DataBuffer;
1427 cmd.buflen = pPacket->DataTransferLength;
1428 cmd.sense = &sense;
1429 cmd.quiet = 0;
1430 cmd.timeout = pPacket->TimeOutValue*HZ;
1432 switch (pPacket->DataIn)
1434 case SCSI_IOCTL_DATA_OUT:
1435 cmd.data_direction = CGC_DATA_WRITE;
1436 break;
1437 case SCSI_IOCTL_DATA_IN:
1438 cmd.data_direction = CGC_DATA_READ;
1439 break;
1440 case SCSI_IOCTL_DATA_UNSPECIFIED:
1441 cmd.data_direction = CGC_DATA_NONE;
1442 break;
1443 default:
1444 return STATUS_INVALID_PARAMETER;
1447 io = ioctl(fd, CDROM_SEND_PACKET, &cmd);
1449 if (pPacket->SenseInfoLength != 0)
1451 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1452 &sense, pPacket->SenseInfoLength);
1455 pPacket->ScsiStatus = cmd.stat;
1457 ret = CDROM_GetStatusCode(io);
1459 #elif defined(__NetBSD__)
1460 scsireq_t cmd;
1461 int io;
1463 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1464 return STATUS_BUFFER_TOO_SMALL;
1466 if (pPacket->CdbLength > 12)
1467 return STATUS_INVALID_PARAMETER;
1469 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1470 return STATUS_INVALID_PARAMETER;
1472 memset(&cmd, 0, sizeof(cmd));
1473 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1475 cmd.cmdlen = pPacket->CdbLength;
1476 cmd.databuf = pPacket->DataBuffer;
1477 cmd.datalen = pPacket->DataTransferLength;
1478 cmd.senselen = pPacket->SenseInfoLength;
1479 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1481 switch (pPacket->DataIn)
1483 case SCSI_IOCTL_DATA_OUT:
1484 cmd.flags |= SCCMD_WRITE;
1485 break;
1486 case SCSI_IOCTL_DATA_IN:
1487 cmd.flags |= SCCMD_READ;
1488 break;
1489 case SCSI_IOCTL_DATA_UNSPECIFIED:
1490 cmd.flags = 0;
1491 break;
1492 default:
1493 return STATUS_INVALID_PARAMETER;
1496 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1498 switch (cmd.retsts)
1500 case SCCMD_OK: break;
1501 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1502 break;
1503 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1504 break;
1505 case SCCMD_SENSE: break;
1506 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1507 break;
1510 if (pPacket->SenseInfoLength != 0)
1512 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1513 cmd.sense, pPacket->SenseInfoLength);
1516 pPacket->ScsiStatus = cmd.status;
1518 ret = CDROM_GetStatusCode(io);
1519 #endif
1520 return ret;
1523 /******************************************************************
1524 * CDROM_ScsiPassThrough
1528 static NTSTATUS CDROM_ScsiPassThrough(int fd, PSCSI_PASS_THROUGH pPacket)
1530 int ret = STATUS_NOT_SUPPORTED;
1531 #if defined(linux) && defined(CDROM_SEND_PACKET)
1532 struct linux_cdrom_generic_command cmd;
1533 struct request_sense sense;
1534 int io;
1536 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1537 return STATUS_BUFFER_TOO_SMALL;
1539 if (pPacket->CdbLength > 12)
1540 return STATUS_INVALID_PARAMETER;
1542 if (pPacket->SenseInfoLength > sizeof(sense))
1543 return STATUS_INVALID_PARAMETER;
1545 memset(&cmd, 0, sizeof(cmd));
1546 memset(&sense, 0, sizeof(sense));
1548 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1550 if ( pPacket->DataBufferOffset > 0x1000 )
1552 cmd.buffer = (void*)pPacket->DataBufferOffset;
1554 else
1556 cmd.buffer = (char*)pPacket + pPacket->DataBufferOffset;
1558 cmd.buflen = pPacket->DataTransferLength;
1559 cmd.sense = &sense;
1560 cmd.quiet = 0;
1561 cmd.timeout = pPacket->TimeOutValue*HZ;
1563 switch (pPacket->DataIn)
1565 case SCSI_IOCTL_DATA_OUT:
1566 cmd.data_direction = CGC_DATA_WRITE;
1567 break;
1568 case SCSI_IOCTL_DATA_IN:
1569 cmd.data_direction = CGC_DATA_READ;
1570 break;
1571 case SCSI_IOCTL_DATA_UNSPECIFIED:
1572 cmd.data_direction = CGC_DATA_NONE;
1573 break;
1574 default:
1575 return STATUS_INVALID_PARAMETER;
1578 io = ioctl(fd, CDROM_SEND_PACKET, &cmd);
1580 if (pPacket->SenseInfoLength != 0)
1582 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1583 &sense, pPacket->SenseInfoLength);
1586 pPacket->ScsiStatus = cmd.stat;
1588 ret = CDROM_GetStatusCode(io);
1590 #elif defined(__NetBSD__)
1591 scsireq_t cmd;
1592 int io;
1594 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1595 return STATUS_BUFFER_TOO_SMALL;
1597 if (pPacket->CdbLength > 12)
1598 return STATUS_INVALID_PARAMETER;
1600 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1601 return STATUS_INVALID_PARAMETER;
1603 memset(&cmd, 0, sizeof(cmd));
1604 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1606 if ( pPacket->DataBufferOffset > 0x1000 )
1608 cmd.databuf = (void*)pPacket->DataBufferOffset;
1610 else
1612 cmd.databuf = (char*)pPacket + pPacket->DataBufferOffset;
1615 cmd.cmdlen = pPacket->CdbLength;
1616 cmd.datalen = pPacket->DataTransferLength;
1617 cmd.senselen = pPacket->SenseInfoLength;
1618 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1620 switch (pPacket->DataIn)
1622 case SCSI_IOCTL_DATA_OUT:
1623 cmd.flags |= SCCMD_WRITE;
1624 break;
1625 case SCSI_IOCTL_DATA_IN:
1626 cmd.flags |= SCCMD_READ;
1627 break;
1628 case SCSI_IOCTL_DATA_UNSPECIFIED:
1629 cmd.flags = 0;
1630 break;
1631 default:
1632 return STATUS_INVALID_PARAMETER;
1635 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1637 switch (cmd.retsts)
1639 case SCCMD_OK: break;
1640 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1641 break;
1642 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1643 break;
1644 case SCCMD_SENSE: break;
1645 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1646 break;
1649 if (pPacket->SenseInfoLength != 0)
1651 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1652 cmd.sense, pPacket->SenseInfoLength);
1655 pPacket->ScsiStatus = cmd.status;
1657 ret = CDROM_GetStatusCode(io);
1658 #endif
1659 return ret;
1662 /******************************************************************
1663 * CDROM_ScsiGetCaps
1667 static NTSTATUS CDROM_ScsiGetCaps(PIO_SCSI_CAPABILITIES caps)
1669 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
1671 caps->Length = sizeof(*caps);
1672 #if defined(linux)
1673 caps->MaximumTransferLength = SG_SCATTER_SZ; /* FIXME */
1674 caps->MaximumPhysicalPages = SG_SCATTER_SZ / getpagesize();
1675 caps->SupportedAsynchronousEvents = TRUE;
1676 caps->AlignmentMask = getpagesize();
1677 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
1678 caps->AdapterScansDown = FALSE; /* FIXME ? */
1679 caps->AdapterUsesPio = FALSE; /* FIXME ? */
1680 ret = STATUS_SUCCESS;
1681 #else
1682 FIXME("Unimplemented\n");
1683 #endif
1684 return ret;
1687 /******************************************************************
1688 * CDROM_GetAddress
1690 * implements IOCTL_SCSI_GET_ADDRESS
1692 static NTSTATUS CDROM_GetAddress(int fd, SCSI_ADDRESS* address)
1694 int portnum, busid, targetid, lun;
1696 address->Length = sizeof(SCSI_ADDRESS);
1697 if ( ! CDROM_GetInterfaceInfo(fd, &portnum, &busid, &targetid, &lun))
1698 return STATUS_NOT_SUPPORTED;
1700 address->PortNumber = portnum;
1701 address->PathId = busid; /* bus number */
1702 address->TargetId = targetid;
1703 address->Lun = lun;
1704 return STATUS_SUCCESS;
1707 /******************************************************************
1708 * DVD_StartSession
1712 static NTSTATUS DVD_StartSession(int fd, PDVD_SESSION_ID sid_in, PDVD_SESSION_ID sid_out)
1714 #if defined(linux)
1715 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1716 dvd_authinfo auth_info;
1718 memset( &auth_info, 0, sizeof( auth_info ) );
1719 auth_info.type = DVD_LU_SEND_AGID;
1720 if (sid_in) auth_info.lsa.agid = *(int*)sid_in; /* ?*/
1722 TRACE("fd 0x%08x\n",fd);
1723 ret =CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
1724 *sid_out = auth_info.lsa.agid;
1725 return ret;
1726 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1727 return STATUS_NOT_SUPPORTED;
1728 #else
1729 return STATUS_NOT_SUPPORTED;
1730 #endif
1733 /******************************************************************
1734 * DVD_EndSession
1738 static NTSTATUS DVD_EndSession(int fd, PDVD_SESSION_ID sid)
1740 #if defined(linux)
1741 dvd_authinfo auth_info;
1743 memset( &auth_info, 0, sizeof( auth_info ) );
1744 auth_info.type = DVD_INVALIDATE_AGID;
1745 auth_info.lsa.agid = *(int*)sid;
1747 TRACE("\n");
1748 return CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
1749 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1750 return STATUS_NOT_SUPPORTED;
1751 #else
1752 return STATUS_NOT_SUPPORTED;
1753 #endif
1756 /******************************************************************
1757 * DVD_SendKey
1761 static NTSTATUS DVD_SendKey(int fd, PDVD_COPY_PROTECT_KEY key)
1763 #if defined(linux)
1764 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1765 dvd_authinfo auth_info;
1767 memset( &auth_info, 0, sizeof( auth_info ) );
1768 switch (key->KeyType)
1770 case DvdChallengeKey:
1771 auth_info.type = DVD_HOST_SEND_CHALLENGE;
1772 auth_info.hsc.agid = (int)key->SessionId;
1773 TRACE("DvdChallengeKey ioc 0x%x\n", DVD_AUTH );
1774 memcpy( auth_info.hsc.chal, key->KeyData, DVD_CHALLENGE_SIZE );
1775 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1776 break;
1777 case DvdBusKey2:
1778 auth_info.type = DVD_HOST_SEND_KEY2;
1779 auth_info.hsk.agid = (int)key->SessionId;
1781 memcpy( auth_info.hsk.key, key->KeyData, DVD_KEY_SIZE );
1783 TRACE("DvdBusKey2\n");
1784 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1785 break;
1787 default:
1788 FIXME("Unknown Keytype 0x%x\n",key->KeyType);
1790 return ret;
1791 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1792 TRACE("bsd\n");
1793 return STATUS_NOT_SUPPORTED;
1794 #else
1795 TRACE("outside\n");
1796 return STATUS_NOT_SUPPORTED;
1797 #endif
1798 TRACE("not reached\n");
1801 /******************************************************************
1802 * DVD_ReadKey
1806 static NTSTATUS DVD_ReadKey(int fd, PDVD_COPY_PROTECT_KEY key)
1808 #if defined(linux)
1809 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1810 dvd_struct dvd;
1811 dvd_authinfo auth_info;
1813 memset( &dvd, 0, sizeof( dvd_struct ) );
1814 memset( &auth_info, 0, sizeof( auth_info ) );
1815 switch (key->KeyType)
1817 case DvdDiskKey:
1819 dvd.type = DVD_STRUCT_DISCKEY;
1820 dvd.disckey.agid = (int)key->SessionId;
1821 memset( dvd.disckey.value, 0, DVD_DISCKEY_SIZE );
1823 TRACE("DvdDiskKey\n");
1824 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
1825 if (ret == STATUS_SUCCESS)
1826 memcpy(key->KeyData,dvd.disckey.value,DVD_DISCKEY_SIZE);
1827 break;
1828 case DvdTitleKey:
1829 auth_info.type = DVD_LU_SEND_TITLE_KEY;
1830 auth_info.lstk.agid = (int)key->SessionId;
1831 auth_info.lstk.lba = (int)(key->Parameters.TitleOffset.QuadPart>>11);
1832 TRACE("DvdTitleKey session %d Quadpart 0x%08lx offset 0x%08x\n",
1833 (int)key->SessionId, (long)key->Parameters.TitleOffset.QuadPart,
1834 auth_info.lstk.lba);
1835 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1836 if (ret == STATUS_SUCCESS)
1837 memcpy(key->KeyData, auth_info.lstk.title_key, DVD_KEY_SIZE );
1838 break;
1839 case DvdChallengeKey:
1841 auth_info.type = DVD_LU_SEND_CHALLENGE;
1842 auth_info.lsc.agid = (int)key->SessionId;
1844 TRACE("DvdChallengeKey\n");
1845 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1846 if (ret == STATUS_SUCCESS)
1847 memcpy( key->KeyData, auth_info.lsc.chal, DVD_CHALLENGE_SIZE );
1848 break;
1849 case DvdAsf:
1850 auth_info.type = DVD_LU_SEND_ASF;
1851 TRACE("DvdAsf\n");
1852 auth_info.lsasf.asf=((PDVD_ASF)key->KeyData)->SuccessFlag;
1853 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1854 ((PDVD_ASF)key->KeyData)->SuccessFlag = auth_info.lsasf.asf;
1855 break;
1856 case DvdBusKey1:
1857 auth_info.type = DVD_LU_SEND_KEY1;
1858 auth_info.lsk.agid = (int)key->SessionId;
1860 TRACE("DvdBusKey1\n");
1861 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1863 if (ret == STATUS_SUCCESS)
1864 memcpy( key->KeyData, auth_info.lsk.key, DVD_KEY_SIZE );
1865 break;
1866 case DvdGetRpcKey:
1867 auth_info.type = DVD_LU_SEND_RPC_STATE;
1869 TRACE("DvdGetRpcKey\n");
1870 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1872 if (ret == STATUS_SUCCESS)
1874 ((PDVD_RPC_KEY)key->KeyData)->TypeCode = auth_info.lrpcs.type;
1875 ((PDVD_RPC_KEY)key->KeyData)->RegionMask = auth_info.lrpcs.region_mask;
1876 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme = auth_info.lrpcs.rpc_scheme;
1878 break;
1879 default:
1880 FIXME("Unknown keytype 0x%x\n",key->KeyType);
1882 return ret;
1883 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1884 TRACE("bsd\n");
1885 return STATUS_NOT_SUPPORTED;
1886 #else
1887 TRACE("outside\n");
1888 return STATUS_NOT_SUPPORTED;
1889 #endif
1890 TRACE("not reached\n");
1893 /******************************************************************
1894 * DVD_GetRegion
1898 static NTSTATUS DVD_GetRegion(int dev, PDVD_REGION region)
1900 FIXME("\n");
1901 return STATUS_SUCCESS;
1905 /******************************************************************
1906 * DVD_GetRegion
1910 static NTSTATUS DVD_ReadStructure(int dev, PDVD_READ_STRUCTURE structure, PDVD_LAYER_DESCRIPTOR layer)
1912 FIXME("\n");
1913 return STATUS_SUCCESS;
1916 /******************************************************************
1917 * CDROM_DeviceIoControl
1921 NTSTATUS CDROM_DeviceIoControl(HANDLE hDevice,
1922 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1923 PVOID UserApcContext,
1924 PIO_STATUS_BLOCK piosb,
1925 ULONG dwIoControlCode,
1926 LPVOID lpInBuffer, DWORD nInBufferSize,
1927 LPVOID lpOutBuffer, DWORD nOutBufferSize)
1929 DWORD sz = 0;
1930 NTSTATUS status = STATUS_SUCCESS;
1931 int fd, dev;
1933 TRACE("%lx %s %lx %ld %lx %ld %p\n",
1934 (DWORD)hDevice, iocodex(dwIoControlCode), (DWORD)lpInBuffer, nInBufferSize,
1935 (DWORD)lpOutBuffer, nOutBufferSize, piosb);
1937 piosb->Information = 0;
1939 if ((status = wine_server_handle_to_fd( hDevice, 0, &fd, NULL ))) goto error;
1940 if ((status = CDROM_Open(fd, &dev)))
1942 wine_server_release_fd( hDevice, fd );
1943 goto error;
1946 switch (dwIoControlCode)
1948 case IOCTL_STORAGE_CHECK_VERIFY:
1949 case IOCTL_CDROM_CHECK_VERIFY:
1950 sz = 0;
1951 CDROM_ClearCacheEntry(dev);
1952 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
1953 status = STATUS_INVALID_PARAMETER;
1954 else status = CDROM_Verify(dev, fd);
1955 break;
1957 /* EPP case IOCTL_STORAGE_CHECK_VERIFY2: */
1959 /* EPP case IOCTL_STORAGE_FIND_NEW_DEVICES: */
1960 /* EPP case IOCTL_CDROM_FIND_NEW_DEVICES: */
1962 case IOCTL_STORAGE_LOAD_MEDIA:
1963 case IOCTL_CDROM_LOAD_MEDIA:
1964 sz = 0;
1965 CDROM_ClearCacheEntry(dev);
1966 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
1967 status = STATUS_INVALID_PARAMETER;
1968 else status = CDROM_SetTray(fd, FALSE);
1969 break;
1970 case IOCTL_STORAGE_EJECT_MEDIA:
1971 sz = 0;
1972 CDROM_ClearCacheEntry(dev);
1973 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
1974 status = STATUS_INVALID_PARAMETER;
1975 else status = CDROM_SetTray(fd, TRUE);
1976 break;
1978 case IOCTL_CDROM_MEDIA_REMOVAL:
1979 case IOCTL_DISK_MEDIA_REMOVAL:
1980 case IOCTL_STORAGE_MEDIA_REMOVAL:
1981 case IOCTL_STORAGE_EJECTION_CONTROL:
1982 /* FIXME the last ioctl:s is not the same as the two others...
1983 * lockcount/owner should be handled */
1984 sz = 0;
1985 CDROM_ClearCacheEntry(dev);
1986 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
1987 else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) status = STATUS_BUFFER_TOO_SMALL;
1988 else status = CDROM_ControlEjection(fd, (const PREVENT_MEDIA_REMOVAL*)lpInBuffer);
1989 break;
1991 /* EPP case IOCTL_STORAGE_GET_MEDIA_TYPES: */
1993 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
1994 sz = sizeof(STORAGE_DEVICE_NUMBER);
1995 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
1996 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
1997 else status = CDROM_GetDeviceNumber(dev, (STORAGE_DEVICE_NUMBER*)lpOutBuffer);
1998 break;
2000 case IOCTL_STORAGE_RESET_DEVICE:
2001 sz = 0;
2002 CDROM_ClearCacheEntry(dev);
2003 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2004 status = STATUS_INVALID_PARAMETER;
2005 else status = CDROM_ResetAudio(fd);
2006 break;
2008 case IOCTL_CDROM_GET_CONTROL:
2009 sz = sizeof(CDROM_AUDIO_CONTROL);
2010 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2011 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2012 else status = CDROM_GetControl(dev, (CDROM_AUDIO_CONTROL*)lpOutBuffer);
2013 break;
2015 case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
2016 sz = sizeof(DISK_GEOMETRY);
2017 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2018 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2019 else status = CDROM_GetDriveGeometry(dev, fd, (DISK_GEOMETRY*)lpOutBuffer);
2020 break;
2022 case IOCTL_CDROM_DISK_TYPE:
2023 sz = sizeof(CDROM_DISK_DATA);
2024 /* CDROM_ClearCacheEntry(dev); */
2025 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2026 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2027 else status = CDROM_GetDiskData(dev, fd, (CDROM_DISK_DATA*)lpOutBuffer);
2028 break;
2030 /* EPP case IOCTL_CDROM_GET_LAST_SESSION: */
2032 case IOCTL_CDROM_READ_Q_CHANNEL:
2033 sz = sizeof(SUB_Q_CHANNEL_DATA);
2034 if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT))
2035 status = STATUS_INVALID_PARAMETER;
2036 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2037 else status = CDROM_ReadQChannel(dev, fd, (const CDROM_SUB_Q_DATA_FORMAT*)lpInBuffer,
2038 (SUB_Q_CHANNEL_DATA*)lpOutBuffer);
2039 break;
2041 case IOCTL_CDROM_READ_TOC:
2042 sz = sizeof(CDROM_TOC);
2043 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2044 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2045 else status = CDROM_ReadTOC(dev, fd, (CDROM_TOC*)lpOutBuffer);
2046 break;
2048 /* EPP case IOCTL_CDROM_READ_TOC_EX: */
2050 case IOCTL_CDROM_PAUSE_AUDIO:
2051 sz = 0;
2052 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2053 status = STATUS_INVALID_PARAMETER;
2054 else status = CDROM_PauseAudio(fd);
2055 break;
2056 case IOCTL_CDROM_PLAY_AUDIO_MSF:
2057 sz = 0;
2058 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2059 else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
2060 else status = CDROM_PlayAudioMSF(fd, (const CDROM_PLAY_AUDIO_MSF*)lpInBuffer);
2061 break;
2062 case IOCTL_CDROM_RESUME_AUDIO:
2063 sz = 0;
2064 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2065 status = STATUS_INVALID_PARAMETER;
2066 else status = CDROM_ResumeAudio(fd);
2067 break;
2068 case IOCTL_CDROM_SEEK_AUDIO_MSF:
2069 sz = 0;
2070 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2071 else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
2072 else status = CDROM_SeekAudioMSF(dev, fd, (const CDROM_SEEK_AUDIO_MSF*)lpInBuffer);
2073 break;
2074 case IOCTL_CDROM_STOP_AUDIO:
2075 sz = 0;
2076 CDROM_ClearCacheEntry(dev); /* Maybe intention is to change media */
2077 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2078 status = STATUS_INVALID_PARAMETER;
2079 else status = CDROM_StopAudio(fd);
2080 break;
2081 case IOCTL_CDROM_GET_VOLUME:
2082 sz = sizeof(VOLUME_CONTROL);
2083 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2084 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2085 else status = CDROM_GetVolume(fd, (VOLUME_CONTROL*)lpOutBuffer);
2086 break;
2087 case IOCTL_CDROM_SET_VOLUME:
2088 sz = 0;
2089 CDROM_ClearCacheEntry(dev);
2090 if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
2091 status = STATUS_INVALID_PARAMETER;
2092 else status = CDROM_SetVolume(fd, (const VOLUME_CONTROL*)lpInBuffer);
2093 break;
2094 case IOCTL_CDROM_RAW_READ:
2095 sz = 0;
2096 if (nInBufferSize < sizeof(RAW_READ_INFO)) status = STATUS_INVALID_PARAMETER;
2097 else if (lpOutBuffer == NULL) status = STATUS_BUFFER_TOO_SMALL;
2098 else status = CDROM_RawRead(fd, (const RAW_READ_INFO*)lpInBuffer,
2099 lpOutBuffer, nOutBufferSize, &sz);
2100 break;
2101 case IOCTL_SCSI_GET_ADDRESS:
2102 sz = sizeof(SCSI_ADDRESS);
2103 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2104 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2105 else status = CDROM_GetAddress(fd, (SCSI_ADDRESS*)lpOutBuffer);
2106 break;
2107 case IOCTL_SCSI_PASS_THROUGH_DIRECT:
2108 sz = sizeof(SCSI_PASS_THROUGH_DIRECT);
2109 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2110 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH_DIRECT)) status = STATUS_BUFFER_TOO_SMALL;
2111 else status = CDROM_ScsiPassThroughDirect(fd, (PSCSI_PASS_THROUGH_DIRECT)lpOutBuffer);
2112 break;
2113 case IOCTL_SCSI_PASS_THROUGH:
2114 sz = sizeof(SCSI_PASS_THROUGH);
2115 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2116 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH)) status = STATUS_BUFFER_TOO_SMALL;
2117 else status = CDROM_ScsiPassThrough(fd, (PSCSI_PASS_THROUGH)lpOutBuffer);
2118 break;
2119 case IOCTL_SCSI_GET_CAPABILITIES:
2120 sz = sizeof(IO_SCSI_CAPABILITIES);
2121 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2122 else if (nOutBufferSize < sizeof(IO_SCSI_CAPABILITIES)) status = STATUS_BUFFER_TOO_SMALL;
2123 else status = CDROM_ScsiGetCaps((PIO_SCSI_CAPABILITIES)lpOutBuffer);
2124 break;
2125 case IOCTL_DVD_START_SESSION:
2126 sz = sizeof(DVD_SESSION_ID);
2127 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2128 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2129 else
2131 TRACE("before in 0x%08lx out 0x%08lx\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
2132 *(PDVD_SESSION_ID)lpOutBuffer);
2133 status = DVD_StartSession(fd, (PDVD_SESSION_ID)lpInBuffer, (PDVD_SESSION_ID)lpOutBuffer);
2134 TRACE("before in 0x%08lx out 0x%08lx\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
2135 *(PDVD_SESSION_ID)lpOutBuffer);
2137 break;
2138 case IOCTL_DVD_END_SESSION:
2139 sz = sizeof(DVD_SESSION_ID);
2140 if ((lpInBuffer == NULL) || (nInBufferSize < sz))status = STATUS_INVALID_PARAMETER;
2141 else status = DVD_EndSession(fd, (PDVD_SESSION_ID)lpInBuffer);
2142 break;
2143 case IOCTL_DVD_SEND_KEY:
2144 sz = 0;
2145 if (!lpInBuffer ||
2146 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
2147 status = STATUS_INVALID_PARAMETER;
2148 else
2150 TRACE("doing DVD_SendKey\n");
2151 status = DVD_SendKey(fd, (PDVD_COPY_PROTECT_KEY)lpInBuffer);
2153 break;
2154 case IOCTL_DVD_READ_KEY:
2155 if (!lpInBuffer ||
2156 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
2157 status = STATUS_INVALID_PARAMETER;
2158 else if (lpInBuffer !=lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
2159 else
2161 TRACE("doing DVD_READ_KEY\n");
2162 sz = ((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength;
2163 status = DVD_ReadKey(fd, (PDVD_COPY_PROTECT_KEY)lpInBuffer);
2165 break;
2166 case IOCTL_DVD_GET_REGION:
2167 sz = sizeof(DVD_REGION);
2168 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2169 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2170 TRACE("doing DVD_Get_REGION\n");
2171 status = DVD_GetRegion(fd, (PDVD_REGION)lpOutBuffer);
2172 break;
2173 case IOCTL_DVD_READ_STRUCTURE:
2174 sz = sizeof(DVD_LAYER_DESCRIPTOR);
2175 if (lpInBuffer == NULL || nInBufferSize != sizeof(DVD_READ_STRUCTURE)) status = STATUS_INVALID_PARAMETER;
2176 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2177 TRACE("doing DVD_READ_STRUCTURE\n");
2178 status = DVD_ReadStructure(fd, (PDVD_READ_STRUCTURE)lpInBuffer, (PDVD_LAYER_DESCRIPTOR)lpOutBuffer);
2179 break;
2181 default:
2182 FIXME("Unsupported IOCTL %lx (type=%lx access=%lx func=%lx meth=%lx)\n",
2183 dwIoControlCode, dwIoControlCode >> 16, (dwIoControlCode >> 14) & 3,
2184 (dwIoControlCode >> 2) & 0xFFF, dwIoControlCode & 3);
2185 sz = 0;
2186 status = STATUS_INVALID_PARAMETER;
2187 break;
2189 wine_server_release_fd( hDevice, fd );
2190 error:
2191 piosb->u.Status = status;
2192 piosb->Information = sz;
2193 if (hEvent) NtSetEvent(hEvent, NULL);
2194 return status;