Fixes for -Wmissing-declaration and -Wwrite-string warnings.
[wine] / dlls / ntdll / cdrom.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /* Main file for CD-ROM support
3  *
4  * Copyright 1994 Martin Ayotte
5  * Copyright 1999, 2001, 2003 Eric Pouech
6  * Copyright 2000 Andreas Mohr
7  * Copyright 2005 Ivan Leo Puoti
8  *
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.
13  *
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.
18  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
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>
42
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 #endif
53 #ifdef HAVE_SCSI_SCSI_IOCTL_H
54 # include <scsi/scsi_ioctl.h>
55 #endif
56 #ifdef HAVE_LINUX_MAJOR_H
57 # include <linux/major.h>
58 #endif
59 #ifdef HAVE_LINUX_HDREG_H
60 # include <linux/hdreg.h>
61 #endif
62 #ifdef HAVE_LINUX_PARAM_H
63 # include <linux/param.h>
64 #endif
65 #ifdef HAVE_LINUX_CDROM_H
66 # include <linux/cdrom.h>
67 #endif
68 #ifdef HAVE_LINUX_UCDROM_H
69 # include <linux/ucdrom.h>
70 #endif
71 #ifdef HAVE_SYS_CDIO_H
72 # include <sys/cdio.h>
73 #endif
74 #ifdef HAVE_SYS_SCSIIO_H
75 # include <sys/scsiio.h>
76 #endif
77
78 #define NONAMELESSUNION
79 #define NONAMELESSSTRUCT
80 #include "ntstatus.h"
81 #include "windef.h"
82 #include "winbase.h"
83 #include "winreg.h"
84 #include "winternl.h"
85 #include "winioctl.h"
86 #include "ntddstor.h"
87 #include "ntddcdrm.h"
88 #include "ntddscsi.h"
89 #include "ntdll_misc.h"
90 #include "wine/server.h"
91 #include "wine/library.h"
92 #include "wine/debug.h"
93
94 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
95    lack the following constants. */
96
97 #ifndef CD_SECS
98 # define CD_SECS              60 /* seconds per minute */
99 #endif
100 #ifndef CD_FRAMES
101 # define CD_FRAMES            75 /* frames per second */
102 #endif
103
104 /* definitions taken from libdvdcss */
105
106 #define IOCTL_DVD_BASE                 FILE_DEVICE_DVD
107
108 #define IOCTL_DVD_START_SESSION     CTL_CODE(IOCTL_DVD_BASE, 0x0400, METHOD_BUFFERED, FILE_READ_ACCESS)
109 #define IOCTL_DVD_READ_KEY          CTL_CODE(IOCTL_DVD_BASE, 0x0401, METHOD_BUFFERED, FILE_READ_ACCESS)
110 #define IOCTL_DVD_SEND_KEY          CTL_CODE(IOCTL_DVD_BASE, 0x0402, METHOD_BUFFERED, FILE_READ_ACCESS)
111 #define IOCTL_DVD_END_SESSION       CTL_CODE(IOCTL_DVD_BASE, 0x0403, METHOD_BUFFERED, FILE_READ_ACCESS)
112 #define IOCTL_DVD_SET_READ_AHEAD    CTL_CODE(IOCTL_DVD_BASE, 0x0404, METHOD_BUFFERED, FILE_READ_ACCESS)
113 #define IOCTL_DVD_GET_REGION        CTL_CODE(IOCTL_DVD_BASE, 0x0405, METHOD_BUFFERED, FILE_READ_ACCESS)
114 #define IOCTL_DVD_SEND_KEY2         CTL_CODE(IOCTL_DVD_BASE, 0x0406, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
115
116 #define IOCTL_DVD_READ_STRUCTURE    CTL_CODE(IOCTL_DVD_BASE, 0x0450, METHOD_BUFFERED, FILE_READ_ACCESS)
117
118 typedef enum {
119     DvdChallengeKey = 0x01,
120     DvdBusKey1,
121     DvdBusKey2,
122     DvdTitleKey,
123     DvdAsf,
124     DvdSetRpcKey = 0x6,
125     DvdGetRpcKey = 0x8,
126     DvdDiskKey = 0x80,
127     DvdInvalidateAGID = 0x3f
128 } DVD_KEY_TYPE;
129
130 typedef ULONG DVD_SESSION_ID, *PDVD_SESSION_ID;
131
132 typedef struct _DVD_COPY_PROTECT_KEY {
133     ULONG KeyLength;
134     DVD_SESSION_ID SessionId;
135     DVD_KEY_TYPE KeyType;
136     ULONG KeyFlags;
137     union {
138         struct {
139             ULONG FileHandle;
140             ULONG Reserved;   /* used for NT alignment */
141         } s;
142         LARGE_INTEGER TitleOffset;
143     } Parameters;
144     UCHAR KeyData[1];
145 } DVD_COPY_PROTECT_KEY, *PDVD_COPY_PROTECT_KEY;
146
147 typedef struct _DVD_RPC_KEY {
148     UCHAR UserResetsAvailable:3;
149     UCHAR ManufacturerResetsAvailable:3;
150     UCHAR TypeCode:2;
151     UCHAR RegionMask;
152     UCHAR RpcScheme;
153     UCHAR Reserved2[1];
154 } DVD_RPC_KEY, * PDVD_RPC_KEY;
155
156 typedef struct _DVD_ASF {
157     UCHAR Reserved0[3];
158     UCHAR SuccessFlag:1;
159     UCHAR Reserved1:7;
160 } DVD_ASF, * PDVD_ASF;
161
162 typedef struct _DVD_REGION
163 {
164         unsigned char copy_system;
165         unsigned char region_data;              /* current media region (not playable when set) */
166         unsigned char system_region;    /* current drive region (playable when set) */
167         unsigned char reset_count;              /* number of resets available */
168 } DVD_REGION, * PDVD_REGION;
169
170 typedef struct _DVD_READ_STRUCTURE {
171         /* Contains an offset to the logical block address of the descriptor to be retrieved. */
172         LARGE_INTEGER block_byte_offset;
173
174         /* 0:Physical descriptor, 1:Copyright descriptor, 2:Disk key descriptor
175            3:BCA descriptor, 4:Manufacturer descriptor, 5:Max descriptor
176          */
177         long format;
178
179         /* Session ID, that is obtained by IOCTL_DVD_START_SESSION */
180         long session;
181
182         /* From 0 to 4 */
183         unsigned char layer_no;
184 }DVD_READ_STRUCTURE, * PDVD_READ_STRUCTURE;
185
186 typedef struct _DVD_LAYER_DESCRIPTOR
187 {
188     unsigned short length;
189
190         unsigned char book_version : 4;
191
192         /* 0:DVD-ROM, 1:DVD-RAM, 2:DVD-R, 3:DVD-RW, 9:DVD-RW */
193         unsigned char book_type : 4;
194
195         unsigned char minimum_rate : 4;
196
197         /* The physical size of the media. 0:120 mm, 1:80 mm. */
198     unsigned char disk_size : 4;
199
200     /* 1:Read-only layer, 2:Recordable layer, 4:Rewritable layer */
201         unsigned char layer_type : 4;
202
203         /* 0:parallel track path, 1:opposite track path */
204     unsigned char track_path : 1;
205
206         /* 0:one layers, 1:two layers, and so on */
207     unsigned char num_of_layers : 2;
208
209     unsigned char reserved1 : 1;
210
211         /* 0:0.74 µm/track, 1:0.80 µm/track, 2:0.615 µm/track */
212     unsigned char track_density : 4;
213
214         /* 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 */
215     unsigned char linear_density : 4;
216
217         /* Must be either 0x30000:DVD-ROM or DVD-R/-RW or 0x31000:DVD-RAM or DVD+RW */
218     unsigned long starting_data_sector;
219
220     unsigned long end_data_sector;
221     unsigned long end_layer_zero_sector;
222     unsigned char reserved5 : 7;
223
224         /* 0 indicates no BCA data */
225         unsigned char BCA_flag : 1;
226
227         unsigned char reserved6;
228 }DVD_LAYER_DESCRIPTOR, * PDVD_LAYER_DESCRIPTOR;
229
230 typedef struct _DVD_COPYRIGHT_DESCRIPTOR
231 {
232         unsigned char protection;
233     unsigned char region;
234     unsigned short reserved;
235 }DVD_COPYRIGHT_DESCRIPTOR, * PDVD_COPYRIGHT_DESCRIPTOR;
236
237 typedef struct _DVD_MANUFACTURER_DESCRIPTOR
238 {
239         unsigned char manufacturing[2048];
240 }DVD_MANUFACTURER_DESCRIPTOR, * PDVD_MANUFACTURER_DESCRIPTOR;
241
242 #define DVD_CHALLENGE_KEY_LENGTH    (12 + sizeof(DVD_COPY_PROTECT_KEY) - sizeof(UCHAR))
243
244 #define DVD_DISK_KEY_LENGTH         (2048 + sizeof(DVD_COPY_PROTECT_KEY) - sizeof(UCHAR))
245
246 #define DVD_KEY_SIZE 5
247 #define DVD_CHALLENGE_SIZE 10
248 #define DVD_DISCKEY_SIZE 2048
249 #define DVD_SECTOR_PROTECTED            0x00000020
250
251 static const struct iocodexs
252 {
253   DWORD code;
254   const char *codex;
255 } iocodextable[] = {
256 #define X(x) { x, #x },
257 X(IOCTL_CDROM_CHECK_VERIFY)
258 X(IOCTL_CDROM_CURRENT_POSITION)
259 X(IOCTL_CDROM_DISK_TYPE)
260 X(IOCTL_CDROM_GET_CONTROL)
261 X(IOCTL_CDROM_GET_DRIVE_GEOMETRY)
262 X(IOCTL_CDROM_GET_VOLUME)
263 X(IOCTL_CDROM_LOAD_MEDIA)
264 X(IOCTL_CDROM_MEDIA_CATALOG)
265 X(IOCTL_CDROM_MEDIA_REMOVAL)
266 X(IOCTL_CDROM_PAUSE_AUDIO)
267 X(IOCTL_CDROM_PLAY_AUDIO_MSF)
268 X(IOCTL_CDROM_RAW_READ)
269 X(IOCTL_CDROM_READ_Q_CHANNEL)
270 X(IOCTL_CDROM_READ_TOC)
271 X(IOCTL_CDROM_RESUME_AUDIO)
272 X(IOCTL_CDROM_SEEK_AUDIO_MSF)
273 X(IOCTL_CDROM_SET_VOLUME)
274 X(IOCTL_CDROM_STOP_AUDIO)
275 X(IOCTL_CDROM_TRACK_ISRC)
276 X(IOCTL_DISK_MEDIA_REMOVAL)
277 X(IOCTL_DVD_END_SESSION)
278 X(IOCTL_DVD_GET_REGION)
279 X(IOCTL_DVD_READ_KEY)
280 X(IOCTL_DVD_READ_STRUCTURE)
281 X(IOCTL_DVD_SEND_KEY)
282 X(IOCTL_DVD_START_SESSION)
283 X(IOCTL_SCSI_GET_ADDRESS)
284 X(IOCTL_SCSI_GET_CAPABILITIES)
285 X(IOCTL_SCSI_PASS_THROUGH)
286 X(IOCTL_SCSI_PASS_THROUGH_DIRECT)
287 X(IOCTL_STORAGE_CHECK_VERIFY)
288 X(IOCTL_STORAGE_EJECTION_CONTROL)
289 X(IOCTL_STORAGE_EJECT_MEDIA)
290 X(IOCTL_STORAGE_GET_DEVICE_NUMBER)
291 X(IOCTL_STORAGE_LOAD_MEDIA)
292 X(IOCTL_STORAGE_MEDIA_REMOVAL)
293 X(IOCTL_STORAGE_RESET_DEVICE)
294 #undef X
295 };
296 static const char *iocodex(DWORD code)
297 {
298    unsigned int i;
299    static char buffer[25];
300    for(i=0; i<sizeof(iocodextable)/sizeof(struct iocodexs); i++)
301       if (code==iocodextable[i].code)
302          return iocodextable[i].codex;
303    sprintf(buffer, "IOCTL_CODE_%x", (int)code);
304    return buffer;
305 }
306
307 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
308
309 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
310 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
311 #define FRAME_OF_TOC(toc, idx)  FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
312 #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
314 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
315 static NTSTATUS CDROM_GetStatusCode(int);
316
317
318 #ifdef linux
319
320 # ifndef IDE6_MAJOR
321 #  define IDE6_MAJOR 88
322 # endif
323 # ifndef IDE7_MAJOR
324 #  define IDE7_MAJOR 89
325 # endif
326
327 # ifdef CDROM_SEND_PACKET
328 /* structure for CDROM_PACKET_COMMAND ioctl */
329 /* not all Linux versions have all the fields, so we define the
330  * structure ourselves to make sure */
331 struct linux_cdrom_generic_command
332 {
333     unsigned char          cmd[CDROM_PACKET_SIZE];
334     unsigned char         *buffer;
335     unsigned int           buflen;
336     int                    stat;
337     struct request_sense  *sense;
338     unsigned char          data_direction;
339     int                    quiet;
340     int                    timeout;
341     void                  *reserved[1];
342 };
343 # endif  /* CDROM_SEND_PACKET */
344
345 #endif  /* linux */
346
347 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
348  * this should be removed when a proper device interface is implemented
349  * 
350  * (WS) We need this to keep track of current position and to safely
351  * detect media changes. Besides this should provide a great speed up
352  * for toc inquiries.
353  */
354 struct cdrom_cache {
355     dev_t       device;
356     ino_t       inode;
357     char        toc_good; /* if false, will reread TOC from disk */
358     CDROM_TOC   toc;
359     SUB_Q_CURRENT_POSITION CurrentPosition;
360 };
361 /* who has more than 5 cdroms on his/her machine ?? */
362 /* FIXME: this should grow depending on the number of cdroms we install/configure 
363  * at startup
364  */
365 #define MAX_CACHE_ENTRIES       5
366 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
367
368 static CRITICAL_SECTION cache_section;
369 static CRITICAL_SECTION_DEBUG critsect_debug =
370 {
371     0, 0, &cache_section,
372     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
373       0, 0, { 0, (DWORD)(__FILE__ ": cache_section") }
374 };
375 static CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
376
377 /* Proposed media change function: not really needed at this time */
378 /* This is a 1 or 0 type of function */
379 #if 0
380 static int CDROM_MediaChanged(int dev)
381 {
382    int i;
383
384    struct cdrom_tochdr  hdr;
385    struct cdrom_tocentry entry;
386
387    if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
388       return 0;
389    if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
390       return 0;
391
392    if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
393       return 1;
394
395    for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
396    {
397       if (i == hdr.cdth_trk1 + 1)
398       {
399          entry.cdte_track = CDROM_LEADOUT;
400       } else {
401          entry.cdte_track = i;
402       }
403       entry.cdte_format = CDROM_MSF;
404       if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
405          return 0;
406       if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
407                               sizeof(struct cdrom_tocentry)) )
408          return 1;
409    }
410    return 0;
411 }
412 #endif
413
414 /******************************************************************
415  *              CDROM_SyncCache                          [internal]
416  *
417  * Read the TOC in and store it in the cdrom_cache structure.
418  * Further requests for the TOC will be copied from the cache
419  * unless certain events like disk ejection is detected, in which
420  * case the cache will be cleared, causing it to be resynced.
421  * The cache section must be held by caller.
422  */
423 static int CDROM_SyncCache(int dev, int fd)
424 {
425    int i, io = 0, tsz;
426 #ifdef linux
427    struct cdrom_tochdr          hdr;
428    struct cdrom_tocentry        entry;
429 #elif defined(__FreeBSD__) || defined(__NetBSD__)
430    struct ioc_toc_header        hdr;
431    struct ioc_read_toc_entry    entry;
432    struct cd_toc_entry         toc_buffer;
433 #endif
434    CDROM_TOC *toc = &cdrom_cache[dev].toc;
435    cdrom_cache[dev].toc_good = 0;
436
437 #ifdef linux
438
439    io = ioctl(fd, CDROMREADTOCHDR, &hdr);
440    if (io == -1)
441    {
442       WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
443       goto end;
444    }
445
446    toc->FirstTrack = hdr.cdth_trk0;
447    toc->LastTrack  = hdr.cdth_trk1;
448    tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
449        + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
450    toc->Length[0] = tsz >> 8;
451    toc->Length[1] = tsz;
452
453    TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
454
455    for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
456    {
457      if (i == toc->LastTrack + 1)
458        entry.cdte_track = CDROM_LEADOUT;
459      else 
460        entry.cdte_track = i;
461      entry.cdte_format = CDROM_MSF;
462      io = ioctl(fd, CDROMREADTOCENTRY, &entry);
463      if (io == -1) {
464        WARN("error read entry (%s)\n", strerror(errno));
465        goto end;
466      }
467      toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
468      toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
469      /* marking last track with leadout value as index */
470      toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
471      toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
472      toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
473      toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
474      toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
475     }
476     cdrom_cache[dev].toc_good = 1;
477     io = 0;
478 #elif defined(__FreeBSD__) || defined(__NetBSD__)
479
480     io = ioctl(fd, CDIOREADTOCHEADER, &hdr);
481     if (io == -1)
482     {
483         WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
484         goto end;
485     }
486     toc->FirstTrack = hdr.starting_track;
487     toc->LastTrack  = hdr.ending_track;
488     tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
489         + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
490     toc->Length[0] = tsz >> 8;
491     toc->Length[1] = tsz;
492
493     TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
494
495     for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
496     {
497         if (i == toc->LastTrack + 1)
498         {
499 #define LEADOUT 0xaa
500             entry.starting_track = LEADOUT;
501         } else {
502             entry.starting_track = i;
503         }
504         memset((char *)&toc_buffer, 0, sizeof(toc_buffer));
505         entry.address_format = CD_MSF_FORMAT;
506         entry.data_len = sizeof(toc_buffer);
507         entry.data = &toc_buffer;
508         io = ioctl(fd, CDIOREADTOCENTRYS, &entry);
509         if (io == -1) {
510             WARN("error read entry (%s)\n", strerror(errno));
511             goto end;
512         }
513         toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
514         toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
515         /* marking last track with leadout value as index */
516         toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
517         toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
518         toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
519         toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
520         toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
521     }
522     cdrom_cache[dev].toc_good = 1;
523     io = 0;
524 #else
525     return STATUS_NOT_SUPPORTED;
526 #endif
527 end:
528     return CDROM_GetStatusCode(io);
529 }
530
531 static void CDROM_ClearCacheEntry(int dev)
532 {
533     RtlEnterCriticalSection( &cache_section );
534     cdrom_cache[dev].toc_good = 0;
535     RtlLeaveCriticalSection( &cache_section );
536 }
537
538
539
540 /******************************************************************
541  *              CDROM_GetInterfaceInfo
542  *
543  * Determines the ide interface (the number after the ide), and the
544  * number of the device on that interface for ide cdroms (*port == 0).
545  * Determines the scsi information for scsi cdroms (*port >= 1).
546  * Returns false if the info cannot not be obtained.
547  */
548 static int CDROM_GetInterfaceInfo(int fd, int* port, int* iface, int* device,int* lun)
549 {
550 #if defined(linux)
551     struct stat st;
552     if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode)) return 0;
553     *port = 0;
554     *iface = 0;
555     *device = 0;
556     *lun = 0;
557     switch (major(st.st_rdev)) {
558     case IDE0_MAJOR: *iface = 0; break;
559     case IDE1_MAJOR: *iface = 1; break;
560     case IDE2_MAJOR: *iface = 2; break;
561     case IDE3_MAJOR: *iface = 3; break;
562     case IDE4_MAJOR: *iface = 4; break;
563     case IDE5_MAJOR: *iface = 5; break;
564     case IDE6_MAJOR: *iface = 6; break;
565     case IDE7_MAJOR: *iface = 7; break;
566     default: *port = 1; break;
567     }
568
569     if (*port == 0)
570         *device = (minor(st.st_rdev) >> 6);
571     else
572     {
573 #ifdef SCSI_IOCTL_GET_IDLUN
574         UINT32 idlun[2];
575         if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, &idlun) != -1)
576         {
577             *port = ((idlun[0] >> 24) & 0xff) + 1;
578             *iface = (idlun[0] >> 16) & 0xff;
579             *device = idlun[0] & 0xff;
580             *lun = (idlun[0] >> 8) & 0xff;
581         }
582         else
583 #endif
584         {
585             WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
586             return 0;
587         }
588     }
589     return 1;
590 #elif defined(__NetBSD__)
591     struct scsi_addr addr;
592     if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
593     {
594         switch (addr.type) 
595         {
596         case TYPE_SCSI:  *port = 1;
597             *iface = addr.addr.scsi.scbus;
598             *device = addr.addr.scsi.target;
599             *lun = addr.addr.scsi.lun;
600             break;
601         case TYPE_ATAPI: *port = 0;
602             *iface = addr.addr.atapi.atbus;
603             *device = addr.addr.atapi.drive;
604             *lun = 0;
605             break;
606         }
607         return 1;
608     }
609     return 0;
610 #elif defined(__FreeBSD__)
611     FIXME("not implemented for BSD\n");
612     return 0;
613 #else
614     FIXME("not implemented for nonlinux\n");
615     return 0;
616 #endif
617 }
618
619
620 /******************************************************************
621  *              CDROM_Open
622  *
623  */
624 static NTSTATUS CDROM_Open(int fd, int* dev)
625 {
626     struct stat st;
627     NTSTATUS ret = STATUS_SUCCESS;
628     int         empty = -1;
629
630     fstat(fd, &st);
631
632     RtlEnterCriticalSection( &cache_section );
633     for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
634     {
635         if (empty == -1 &&
636             cdrom_cache[*dev].device == 0 &&
637             cdrom_cache[*dev].inode == 0)
638             empty = *dev;
639         else if (cdrom_cache[*dev].device == st.st_dev &&
640                  cdrom_cache[*dev].inode == st.st_ino)
641             break;
642     }
643     if (*dev == MAX_CACHE_ENTRIES)
644     {
645         if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
646         else
647         {
648             *dev = empty;
649             cdrom_cache[*dev].device  = st.st_dev;
650             cdrom_cache[*dev].inode   = st.st_ino;
651         }
652     }
653     RtlLeaveCriticalSection( &cache_section );
654
655     TRACE("%d, %d\n", *dev, fd);
656     return ret;
657 }
658
659 /******************************************************************
660  *              CDROM_GetStatusCode
661  *
662  *
663  */
664 static NTSTATUS CDROM_GetStatusCode(int io)
665 {
666     if (io == 0) return STATUS_SUCCESS;
667     return FILE_GetNtStatus();
668 }
669
670 /******************************************************************
671  *              CDROM_GetControl
672  *
673  */
674 static NTSTATUS CDROM_GetControl(int dev, CDROM_AUDIO_CONTROL* cac)
675 {
676     cac->LbaFormat = 0; /* FIXME */
677     cac->LogicalBlocksPerSecond = 1; /* FIXME */
678     return  STATUS_NOT_SUPPORTED;
679 }
680
681 /******************************************************************
682  *              CDROM_GetDeviceNumber
683  *
684  */
685 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
686 {
687     return STATUS_NOT_SUPPORTED;
688 }
689
690 /******************************************************************
691  *              CDROM_GetDriveGeometry
692  *
693  */
694 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
695 {
696   CDROM_TOC     toc;
697   NTSTATUS      ret = 0;
698   int           fsize = 0;
699
700   if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
701
702   fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
703         - FRAME_OF_TOC(toc, 1); /* Total size in frames */
704   
705   dg->Cylinders.u.LowPart = fsize / (64 * 32); 
706   dg->Cylinders.u.HighPart = 0; 
707   dg->MediaType = RemovableMedia;  
708   dg->TracksPerCylinder = 64; 
709   dg->SectorsPerTrack = 32;  
710   dg->BytesPerSector= 2048; 
711   return ret;
712 }
713
714 /**************************************************************************
715  *                              CDROM_Reset                     [internal]
716  */
717 static NTSTATUS CDROM_ResetAudio(int fd)
718 {
719 #if defined(linux)
720     return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
721 #elif defined(__FreeBSD__) || defined(__NetBSD__)
722     return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
723 #else
724     return STATUS_NOT_SUPPORTED;
725 #endif
726 }
727
728 /******************************************************************
729  *              CDROM_SetTray
730  *
731  *
732  */
733 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
734 {
735 #if defined(linux)
736     return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
737 #elif defined(__FreeBSD__) || defined(__NetBSD__)
738     return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
739                                (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
740                                (ioctl(fd, CDIOCPREVENT, NULL)));
741 #else
742     return STATUS_NOT_SUPPORTED;
743 #endif
744 }
745
746 /******************************************************************
747  *              CDROM_ControlEjection
748  *
749  *
750  */
751 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
752 {
753 #if defined(linux)
754     return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
755 #elif defined(__FreeBSD__) || defined(__NetBSD__)
756     return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
757 #else
758     return STATUS_NOT_SUPPORTED;
759 #endif
760 }
761
762 /******************************************************************
763  *              CDROM_ReadTOC
764  *
765  *
766  */
767 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
768 {
769     NTSTATUS       ret = STATUS_NOT_SUPPORTED;
770
771     if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
772         return STATUS_INVALID_PARAMETER;
773
774     RtlEnterCriticalSection( &cache_section );
775     if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
776     {
777         *toc = cdrom_cache[dev].toc;
778         ret = STATUS_SUCCESS;
779     }
780     RtlLeaveCriticalSection( &cache_section );
781     return ret;
782 }
783
784 /******************************************************************
785  *              CDROM_GetDiskData
786  *
787  *
788  */
789 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
790 {
791     CDROM_TOC   toc;
792     NTSTATUS    ret;
793     int         i;
794
795     if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
796     data->DiskData = 0;
797     for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
798         if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
799             data->DiskData |= CDROM_DISK_DATA_TRACK;
800         else
801             data->DiskData |= CDROM_DISK_AUDIO_TRACK;
802     }
803     return STATUS_SUCCESS;
804 }
805
806 /******************************************************************
807  *              CDROM_ReadQChannel
808  *
809  *
810  */
811 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
812                                    SUB_Q_CHANNEL_DATA* data)
813 {
814     NTSTATUS            ret = STATUS_NOT_SUPPORTED;
815 #ifdef linux
816     unsigned            size;
817     SUB_Q_HEADER*       hdr = (SUB_Q_HEADER*)data;
818     int                 io;
819     struct cdrom_subchnl        sc;
820     sc.cdsc_format = CDROM_MSF;
821
822     io = ioctl(fd, CDROMSUBCHNL, &sc);
823     if (io == -1)
824     {
825         TRACE("opened or no_media (%s)!\n", strerror(errno));
826         hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
827         CDROM_ClearCacheEntry(dev);
828         goto end;
829     }
830
831     hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
832
833     switch (sc.cdsc_audiostatus) {
834     case CDROM_AUDIO_INVALID:
835         CDROM_ClearCacheEntry(dev);
836         hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
837         break;
838     case CDROM_AUDIO_NO_STATUS:
839         CDROM_ClearCacheEntry(dev);
840         hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
841         break;
842     case CDROM_AUDIO_PLAY:
843         hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
844         break;
845     case CDROM_AUDIO_PAUSED:
846         hdr->AudioStatus = AUDIO_STATUS_PAUSED;
847         break;
848     case CDROM_AUDIO_COMPLETED:
849         hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
850         break;
851     case CDROM_AUDIO_ERROR:
852         hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
853         break;
854     default:
855         TRACE("status=%02X !\n", sc.cdsc_audiostatus);
856         break;
857     }
858     switch (fmt->Format)
859     {
860     case IOCTL_CDROM_CURRENT_POSITION:
861         size = sizeof(SUB_Q_CURRENT_POSITION);
862         RtlEnterCriticalSection( &cache_section );
863         if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
864           data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
865           data->CurrentPosition.Control = sc.cdsc_ctrl; 
866           data->CurrentPosition.ADR = sc.cdsc_adr; 
867           data->CurrentPosition.TrackNumber = sc.cdsc_trk; 
868           data->CurrentPosition.IndexNumber = sc.cdsc_ind; 
869
870           data->CurrentPosition.AbsoluteAddress[0] = 0; 
871           data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute; 
872           data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
873           data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
874  
875           data->CurrentPosition.TrackRelativeAddress[0] = 0; 
876           data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute; 
877           data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
878           data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
879
880           cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
881         }
882         else /* not playing */
883         {
884           cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
885           data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
886         }
887         RtlLeaveCriticalSection( &cache_section );
888         break;
889     case IOCTL_CDROM_MEDIA_CATALOG:
890         size = sizeof(SUB_Q_MEDIA_CATALOG_NUMBER);
891         data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
892         {
893             struct cdrom_mcn mcn;
894             if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
895
896             data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
897             data->MediaCatalog.Mcval = 0; /* FIXME */
898             memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
899             data->MediaCatalog.MediaCatalog[14] = 0;
900         }
901         break;
902     case IOCTL_CDROM_TRACK_ISRC:
903         size = sizeof(SUB_Q_CURRENT_POSITION);
904         FIXME("TrackIsrc: NIY on linux\n");
905         data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
906         data->TrackIsrc.Tcval = 0;
907         io = 0;
908         break;
909     }
910
911  end:
912     ret = CDROM_GetStatusCode(io);
913 #elif defined(__FreeBSD__) || defined(__NetBSD__)
914     unsigned            size;
915     SUB_Q_HEADER*       hdr = (SUB_Q_HEADER*)data;
916     int                 io;
917     struct ioc_read_subchannel  read_sc;
918     struct cd_sub_channel_info  sc;
919
920     read_sc.address_format = CD_MSF_FORMAT;
921     read_sc.track          = 0;
922     read_sc.data_len       = sizeof(sc);
923     read_sc.data           = &sc;
924     switch (fmt->Format)
925     {
926     case IOCTL_CDROM_CURRENT_POSITION:
927         read_sc.data_format    = CD_CURRENT_POSITION;
928         break;
929     case IOCTL_CDROM_MEDIA_CATALOG:
930         read_sc.data_format    = CD_MEDIA_CATALOG;
931         break;
932     case IOCTL_CDROM_TRACK_ISRC:
933         read_sc.data_format    = CD_TRACK_INFO;
934         sc.what.track_info.track_number = data->TrackIsrc.Track;
935         break;
936     }
937     io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
938     if (io == -1)
939     {
940         TRACE("opened or no_media (%s)!\n", strerror(errno));
941         CDROM_ClearCacheEntry(dev);
942         hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
943         goto end;
944     }
945
946     hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
947
948     switch (sc.header.audio_status) {
949     case CD_AS_AUDIO_INVALID:
950         CDROM_ClearCacheEntry(dev);
951         hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
952         break;
953     case CD_AS_NO_STATUS:
954         CDROM_ClearCacheEntry(dev);
955         hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
956         break;
957     case CD_AS_PLAY_IN_PROGRESS:
958         hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
959         break;
960     case CD_AS_PLAY_PAUSED:
961         hdr->AudioStatus = AUDIO_STATUS_PAUSED;
962         break;
963     case CD_AS_PLAY_COMPLETED:
964         hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
965         break;
966     case CD_AS_PLAY_ERROR:
967         hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
968         break;
969     default:
970         TRACE("status=%02X !\n", sc.header.audio_status);
971     }
972     switch (fmt->Format)
973     {
974     case IOCTL_CDROM_CURRENT_POSITION:
975         size = sizeof(SUB_Q_CURRENT_POSITION);
976         RtlEnterCriticalSection( &cache_section );
977         if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
978           data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
979           data->CurrentPosition.Control = sc.what.position.control;
980           data->CurrentPosition.ADR = sc.what.position.addr_type;
981           data->CurrentPosition.TrackNumber = sc.what.position.track_number;
982           data->CurrentPosition.IndexNumber = sc.what.position.index_number;
983
984           data->CurrentPosition.AbsoluteAddress[0] = 0;
985           data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
986           data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
987           data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
988           data->CurrentPosition.TrackRelativeAddress[0] = 0;
989           data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
990           data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
991           data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
992           cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
993         }
994         else { /* not playing */
995           cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
996           data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
997         }
998         RtlLeaveCriticalSection( &cache_section );
999         break;
1000     case IOCTL_CDROM_MEDIA_CATALOG:
1001         size = sizeof(SUB_Q_MEDIA_CATALOG_NUMBER);
1002         data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
1003         data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
1004         memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
1005         break;
1006     case IOCTL_CDROM_TRACK_ISRC:
1007         size = sizeof(SUB_Q_CURRENT_POSITION);
1008         data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
1009         data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
1010         memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
1011         break;
1012     }
1013
1014  end:
1015     ret = CDROM_GetStatusCode(io);
1016 #endif
1017     return ret;
1018 }
1019
1020 /******************************************************************
1021  *              CDROM_Verify
1022  *
1023  *
1024  */
1025 static NTSTATUS CDROM_Verify(int dev, int fd)
1026 {
1027     /* quick implementation */
1028     CDROM_SUB_Q_DATA_FORMAT     fmt;
1029     SUB_Q_CHANNEL_DATA          data;
1030
1031     fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
1032     return CDROM_ReadQChannel(dev, fd, &fmt, &data) ? 1 : 0;
1033 }
1034
1035 /******************************************************************
1036  *              CDROM_PlayAudioMSF
1037  *
1038  *
1039  */
1040 static NTSTATUS CDROM_PlayAudioMSF(int fd, const CDROM_PLAY_AUDIO_MSF* audio_msf)
1041 {
1042     NTSTATUS       ret = STATUS_NOT_SUPPORTED;
1043 #ifdef linux
1044     struct      cdrom_msf       msf;
1045     int         io;
1046
1047     msf.cdmsf_min0   = audio_msf->StartingM;
1048     msf.cdmsf_sec0   = audio_msf->StartingS;
1049     msf.cdmsf_frame0 = audio_msf->StartingF;
1050     msf.cdmsf_min1   = audio_msf->EndingM;
1051     msf.cdmsf_sec1   = audio_msf->EndingS;
1052     msf.cdmsf_frame1 = audio_msf->EndingF;
1053
1054     io = ioctl(fd, CDROMSTART);
1055     if (io == -1)
1056     {
1057         WARN("motor doesn't start !\n");
1058         goto end;
1059     }
1060     io = ioctl(fd, CDROMPLAYMSF, &msf);
1061     if (io == -1)
1062     {
1063         WARN("device doesn't play !\n");
1064         goto end;
1065     }
1066     TRACE("msf = %d:%d:%d %d:%d:%d\n",
1067           msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
1068           msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
1069  end:
1070     ret = CDROM_GetStatusCode(io);
1071 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1072     struct      ioc_play_msf    msf;
1073     int         io;
1074
1075     msf.start_m      = audio_msf->StartingM;
1076     msf.start_s      = audio_msf->StartingS;
1077     msf.start_f      = audio_msf->StartingF;
1078     msf.end_m        = audio_msf->EndingM;
1079     msf.end_s        = audio_msf->EndingS;
1080     msf.end_f        = audio_msf->EndingF;
1081
1082     io = ioctl(fd, CDIOCSTART, NULL);
1083     if (io == -1)
1084     {
1085         WARN("motor doesn't start !\n");
1086         goto end;
1087     }
1088     io = ioctl(fd, CDIOCPLAYMSF, &msf);
1089     if (io == -1)
1090     {
1091         WARN("device doesn't play !\n");
1092         goto end;
1093     }
1094     TRACE("msf = %d:%d:%d %d:%d:%d\n",
1095           msf.start_m, msf.start_s, msf.start_f,
1096           msf.end_m,   msf.end_s,   msf.end_f);
1097 end:
1098     ret = CDROM_GetStatusCode(io);
1099 #endif
1100     return ret;
1101 }
1102
1103 /******************************************************************
1104  *              CDROM_SeekAudioMSF
1105  *
1106  *
1107  */
1108 static NTSTATUS CDROM_SeekAudioMSF(int dev, int fd, const CDROM_SEEK_AUDIO_MSF* audio_msf)
1109 {
1110     CDROM_TOC toc;
1111     int i, io, frame;
1112     SUB_Q_CURRENT_POSITION *cp;
1113 #if defined(linux)
1114     struct cdrom_msf0   msf;
1115     struct cdrom_subchnl sc;
1116 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1117     struct ioc_play_msf msf;
1118     struct ioc_read_subchannel  read_sc;
1119     struct cd_sub_channel_info  sc;
1120     int final_frame;
1121 #endif
1122
1123     /* Use the information on the TOC to compute the new current
1124      * position, which is shadowed on the cache. [Portable]. */
1125     frame = FRAME_OF_MSF(*audio_msf);
1126
1127     if ((io = CDROM_ReadTOC(dev, fd, &toc)) != 0) return io;
1128
1129     for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
1130       if (FRAME_OF_TOC(toc,i)>frame) break;
1131     if (i <= toc.FirstTrack || i > toc.LastTrack+1)
1132       return STATUS_INVALID_PARAMETER;
1133     i--;
1134     RtlEnterCriticalSection( &cache_section );
1135     cp = &cdrom_cache[dev].CurrentPosition;
1136     cp->FormatCode = IOCTL_CDROM_CURRENT_POSITION; 
1137     cp->Control = toc.TrackData[i-toc.FirstTrack].Control; 
1138     cp->ADR = toc.TrackData[i-toc.FirstTrack].Adr; 
1139     cp->TrackNumber = toc.TrackData[i-toc.FirstTrack].TrackNumber;
1140     cp->IndexNumber = 0; /* FIXME: where do they keep these? */
1141     cp->AbsoluteAddress[0] = 0; 
1142     cp->AbsoluteAddress[1] = toc.TrackData[i-toc.FirstTrack].Address[1];
1143     cp->AbsoluteAddress[2] = toc.TrackData[i-toc.FirstTrack].Address[2];
1144     cp->AbsoluteAddress[3] = toc.TrackData[i-toc.FirstTrack].Address[3];
1145     frame -= FRAME_OF_TOC(toc,i);
1146     cp->TrackRelativeAddress[0] = 0;
1147     MSF_OF_FRAME(cp->TrackRelativeAddress[1], frame); 
1148     RtlLeaveCriticalSection( &cache_section );
1149
1150     /* If playing, then issue a seek command, otherwise do nothing */
1151 #ifdef linux
1152     sc.cdsc_format = CDROM_MSF;
1153
1154     io = ioctl(fd, CDROMSUBCHNL, &sc);
1155     if (io == -1)
1156     {
1157         TRACE("opened or no_media (%s)!\n", strerror(errno));
1158         CDROM_ClearCacheEntry(dev);
1159         return CDROM_GetStatusCode(io);
1160     }
1161     if (sc.cdsc_audiostatus==CDROM_AUDIO_PLAY)
1162     {
1163       msf.minute = audio_msf->M;
1164       msf.second = audio_msf->S;
1165       msf.frame  = audio_msf->F;
1166       return CDROM_GetStatusCode(ioctl(fd, CDROMSEEK, &msf));
1167     }
1168     return STATUS_SUCCESS;
1169 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1170     read_sc.address_format = CD_MSF_FORMAT;
1171     read_sc.track          = 0;
1172     read_sc.data_len       = sizeof(sc);
1173     read_sc.data           = &sc;
1174     read_sc.data_format    = CD_CURRENT_POSITION;
1175
1176     io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1177     if (io == -1)
1178     {
1179         TRACE("opened or no_media (%s)!\n", strerror(errno));
1180         CDROM_ClearCacheEntry(dev);
1181         return CDROM_GetStatusCode(io);
1182     }
1183     if (sc.header.audio_status==CD_AS_PLAY_IN_PROGRESS) 
1184     {
1185
1186       msf.start_m      = audio_msf->M;
1187       msf.start_s      = audio_msf->S;
1188       msf.start_f      = audio_msf->F;
1189       final_frame = FRAME_OF_TOC(toc,toc.LastTrack+1)-1;
1190       MSF_OF_FRAME(msf.end_m, final_frame);
1191
1192       return CDROM_GetStatusCode(ioctl(fd, CDIOCPLAYMSF, &msf));
1193     }
1194     return STATUS_SUCCESS;
1195 #else
1196     return STATUS_NOT_SUPPORTED;
1197 #endif
1198 }
1199
1200 /******************************************************************
1201  *              CDROM_PauseAudio
1202  *
1203  *
1204  */
1205 static NTSTATUS CDROM_PauseAudio(int fd)
1206 {
1207 #if defined(linux)
1208     return CDROM_GetStatusCode(ioctl(fd, CDROMPAUSE));
1209 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1210     return CDROM_GetStatusCode(ioctl(fd, CDIOCPAUSE, NULL));
1211 #else
1212     return STATUS_NOT_SUPPORTED;
1213 #endif
1214 }
1215
1216 /******************************************************************
1217  *              CDROM_ResumeAudio
1218  *
1219  *
1220  */
1221 static NTSTATUS CDROM_ResumeAudio(int fd)
1222 {
1223 #if defined(linux)
1224     return CDROM_GetStatusCode(ioctl(fd, CDROMRESUME));
1225 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1226     return CDROM_GetStatusCode(ioctl(fd, CDIOCRESUME, NULL));
1227 #else
1228     return STATUS_NOT_SUPPORTED;
1229 #endif
1230 }
1231
1232 /******************************************************************
1233  *              CDROM_StopAudio
1234  *
1235  *
1236  */
1237 static NTSTATUS CDROM_StopAudio(int fd)
1238 {
1239 #if defined(linux)
1240     return CDROM_GetStatusCode(ioctl(fd, CDROMSTOP));
1241 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1242     return CDROM_GetStatusCode(ioctl(fd, CDIOCSTOP, NULL));
1243 #else
1244     return STATUS_NOT_SUPPORTED;
1245 #endif
1246 }
1247
1248 /******************************************************************
1249  *              CDROM_GetVolume
1250  *
1251  *
1252  */
1253 static NTSTATUS CDROM_GetVolume(int fd, VOLUME_CONTROL* vc)
1254 {
1255 #if defined(linux)
1256     struct cdrom_volctrl volc;
1257     int io;
1258
1259     io = ioctl(fd, CDROMVOLREAD, &volc);
1260     if (io != -1)
1261     {
1262         vc->PortVolume[0] = volc.channel0;
1263         vc->PortVolume[1] = volc.channel1;
1264         vc->PortVolume[2] = volc.channel2;
1265         vc->PortVolume[3] = volc.channel3;
1266     }
1267     return CDROM_GetStatusCode(io);
1268 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1269     struct  ioc_vol     volc;
1270     int io;
1271
1272     io = ioctl(fd, CDIOCGETVOL, &volc);
1273     if (io != -1)
1274     {
1275         vc->PortVolume[0] = volc.vol[0];
1276         vc->PortVolume[1] = volc.vol[1];
1277         vc->PortVolume[2] = volc.vol[2];
1278         vc->PortVolume[3] = volc.vol[3];
1279     }
1280     return CDROM_GetStatusCode(io);
1281 #else
1282     return STATUS_NOT_SUPPORTED;
1283 #endif
1284 }
1285
1286 /******************************************************************
1287  *              CDROM_SetVolume
1288  *
1289  *
1290  */
1291 static NTSTATUS CDROM_SetVolume(int fd, const VOLUME_CONTROL* vc)
1292 {
1293 #if defined(linux)
1294     struct cdrom_volctrl volc;
1295
1296     volc.channel0 = vc->PortVolume[0];
1297     volc.channel1 = vc->PortVolume[1];
1298     volc.channel2 = vc->PortVolume[2];
1299     volc.channel3 = vc->PortVolume[3];
1300
1301     return CDROM_GetStatusCode(ioctl(fd, CDROMVOLCTRL, &volc));
1302 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1303     struct  ioc_vol     volc;
1304
1305     volc.vol[0] = vc->PortVolume[0];
1306     volc.vol[1] = vc->PortVolume[1];
1307     volc.vol[2] = vc->PortVolume[2];
1308     volc.vol[3] = vc->PortVolume[3];
1309
1310     return CDROM_GetStatusCode(ioctl(fd, CDIOCSETVOL, &volc));
1311 #else
1312     return STATUS_NOT_SUPPORTED;
1313 #endif
1314 }
1315
1316 /******************************************************************
1317  *              CDROM_RawRead
1318  *
1319  *
1320  */
1321 static NTSTATUS CDROM_RawRead(int fd, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
1322 {
1323     int         ret = STATUS_NOT_SUPPORTED;
1324     int         io = -1;
1325     DWORD       sectSize;
1326
1327     switch (raw->TrackMode)
1328     {
1329     case YellowMode2:   sectSize = 2336;        break;
1330     case XAForm2:       sectSize = 2328;        break;
1331     case CDDA:          sectSize = 2352;        break;
1332     default:    return STATUS_INVALID_PARAMETER;
1333     }
1334     if (len < raw->SectorCount * sectSize) return STATUS_BUFFER_TOO_SMALL;
1335     /* strangely enough, it seems that sector offsets are always indicated with a size of 2048,
1336      * even if a larger size if read...
1337      */
1338 #if defined(linux)
1339     {
1340         struct cdrom_read       cdr;
1341         struct cdrom_read_audio cdra;
1342
1343         switch (raw->TrackMode)
1344         {
1345         case YellowMode2:
1346             if (raw->DiskOffset.u.HighPart) FIXME("Unsupported value\n");
1347             cdr.cdread_lba = raw->DiskOffset.u.LowPart; /* FIXME ? */
1348             cdr.cdread_bufaddr = buffer;
1349             cdr.cdread_buflen = raw->SectorCount * sectSize;
1350             io = ioctl(fd, CDROMREADMODE2, &cdr);
1351             break;
1352         case XAForm2:
1353             FIXME("XAForm2: NIY\n");
1354             return ret;
1355         case CDDA:
1356             /* FIXME: the output doesn't seem 100% correct... in fact output is shifted
1357              * between by NT2K box and this... should check on the same drive...
1358              * otherwise, I fear a 2352/2368 mismatch somewhere in one of the drivers
1359              * (linux/NT).
1360              * Anyway, that's not critical at all. We're talking of 16/32 bytes, we're
1361              * talking of 0.2 ms of sound
1362              */
1363             /* 2048 = 2 ** 11 */
1364             if (raw->DiskOffset.u.HighPart & ~2047) FIXME("Unsupported value\n");
1365             cdra.addr.lba = ((raw->DiskOffset.u.LowPart >> 11) |
1366                 (raw->DiskOffset.u.HighPart << (32 - 11))) - 1;
1367             FIXME("reading at %u\n", cdra.addr.lba);
1368             cdra.addr_format = CDROM_LBA;
1369             cdra.nframes = raw->SectorCount;
1370             cdra.buf = buffer;
1371             io = ioctl(fd, CDROMREADAUDIO, &cdra);
1372             break;
1373         default:
1374             FIXME("NIY: %d\n", raw->TrackMode);
1375             return ret;
1376         }
1377     }
1378 #else
1379     {
1380         switch (raw->TrackMode)
1381         {
1382         case YellowMode2:
1383             FIXME("YellowMode2: NIY\n");
1384             return ret;
1385         case XAForm2:
1386             FIXME("XAForm2: NIY\n");
1387             return ret;
1388         case CDDA:
1389             FIXME("CDDA: NIY\n");
1390             return ret;
1391         }
1392     }
1393 #endif
1394
1395     *sz = sectSize * raw->SectorCount;
1396     ret = CDROM_GetStatusCode(io);
1397     return ret;
1398 }
1399
1400 /******************************************************************
1401  *        CDROM_ScsiPassThroughDirect
1402  *        Implements IOCTL_SCSI_PASS_THROUGH_DIRECT
1403  *
1404  */
1405 static NTSTATUS CDROM_ScsiPassThroughDirect(int fd, PSCSI_PASS_THROUGH_DIRECT pPacket)
1406 {
1407     int ret = STATUS_NOT_SUPPORTED;
1408
1409 #if defined(linux)
1410     sg_io_hdr_t cmd;
1411     int io;
1412
1413     if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1414         return STATUS_BUFFER_TOO_SMALL;
1415
1416     if (pPacket->CdbLength > 16)
1417         return STATUS_INVALID_PARAMETER;
1418
1419     if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1420         return STATUS_INVALID_PARAMETER;
1421
1422     if (pPacket->DataTransferLength > 0 && !pPacket->DataBuffer)
1423         return STATUS_INVALID_PARAMETER;
1424
1425     RtlZeroMemory(&cmd, sizeof(cmd));
1426
1427     cmd.interface_id   = 'S';
1428     cmd.cmd_len        = pPacket->CdbLength;
1429     cmd.mx_sb_len      = pPacket->SenseInfoLength;
1430     cmd.dxfer_len      = pPacket->DataTransferLength;
1431     cmd.dxferp         = pPacket->DataBuffer;
1432     cmd.cmdp           = pPacket->Cdb;
1433     cmd.sbp            = (char*)pPacket + pPacket->SenseInfoOffset;
1434     cmd.timeout        = pPacket->TimeOutValue*1000;
1435
1436     switch (pPacket->DataIn)
1437     {
1438     case SCSI_IOCTL_DATA_IN:
1439         cmd.dxfer_direction = SG_DXFER_TO_DEV;
1440         break;
1441     case SCSI_IOCTL_DATA_OUT:
1442         cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1443         break;
1444     case SCSI_IOCTL_DATA_UNSPECIFIED:
1445         cmd.dxfer_direction = SG_DXFER_NONE;
1446         break;
1447     default:
1448        return STATUS_INVALID_PARAMETER;
1449     }
1450
1451     io = ioctl(fd, SG_IO, &cmd);
1452
1453     pPacket->ScsiStatus         = cmd.status;
1454     pPacket->DataTransferLength = cmd.resid;
1455     pPacket->SenseInfoLength    = cmd.sb_len_wr;
1456
1457     ret = CDROM_GetStatusCode(io);
1458
1459 #elif defined(__NetBSD__)
1460     scsireq_t cmd;
1461     int io;
1462
1463     if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1464         return STATUS_BUFFER_TOO_SMALL;
1465
1466     if (pPacket->CdbLength > 12)
1467         return STATUS_INVALID_PARAMETER;
1468
1469     if (pPacket->SenseInfoLength > SENSEBUFLEN)
1470         return STATUS_INVALID_PARAMETER;
1471
1472     memset(&cmd, 0, sizeof(cmd));
1473     memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1474
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 */
1480
1481     switch (pPacket->DataIn)
1482     {
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;
1494     }
1495
1496     io = ioctl(fd, SCIOCCOMMAND, &cmd);
1497
1498     switch (cmd.retsts)
1499     {
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;
1508     }
1509
1510     if (pPacket->SenseInfoLength != 0)
1511     {
1512         memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1513                cmd.sense, pPacket->SenseInfoLength);
1514     }
1515
1516     pPacket->ScsiStatus = cmd.status;
1517
1518     ret = CDROM_GetStatusCode(io);
1519 #endif
1520     return ret;
1521 }
1522
1523 /******************************************************************
1524  *              CDROM_ScsiPassThrough
1525  *              Implements IOCTL_SCSI_PASS_THROUGH
1526  *
1527  */
1528 static NTSTATUS CDROM_ScsiPassThrough(int fd, PSCSI_PASS_THROUGH pPacket)
1529 {
1530     int ret = STATUS_NOT_SUPPORTED;
1531 #if defined(linux)
1532     sg_io_hdr_t cmd;
1533     int io;
1534
1535     if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1536         return STATUS_BUFFER_TOO_SMALL;
1537
1538     if (pPacket->CdbLength > 16)
1539         return STATUS_INVALID_PARAMETER;
1540
1541     if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1542         return STATUS_INVALID_PARAMETER;
1543
1544     if (pPacket->DataTransferLength > 0 && pPacket->DataBufferOffset < sizeof(SCSI_PASS_THROUGH))
1545         return STATUS_INVALID_PARAMETER;
1546
1547     RtlZeroMemory(&cmd, sizeof(cmd));
1548
1549     cmd.interface_id   = 'S';
1550     cmd.dxfer_len      = pPacket->DataTransferLength;
1551     cmd.dxferp         = (char*)pPacket + pPacket->DataBufferOffset;
1552     cmd.cmd_len        = pPacket->CdbLength;
1553     cmd.cmdp           = pPacket->Cdb;
1554     cmd.mx_sb_len      = pPacket->SenseInfoLength;
1555     cmd.timeout        = pPacket->TimeOutValue*1000;
1556
1557     if(cmd.mx_sb_len > 0)
1558         cmd.sbp = (char*)pPacket + pPacket->SenseInfoOffset;
1559
1560     switch (pPacket->DataIn)
1561     {
1562     case SCSI_IOCTL_DATA_IN:
1563         cmd.dxfer_direction = SG_DXFER_TO_DEV;
1564                              break;
1565     case SCSI_IOCTL_DATA_OUT:
1566         cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1567                              break;
1568     case SCSI_IOCTL_DATA_UNSPECIFIED:
1569         cmd.dxfer_direction = SG_DXFER_NONE;
1570                              break;
1571     default:
1572        return STATUS_INVALID_PARAMETER;
1573     }
1574
1575     io = ioctl(fd, SG_IO, &cmd);
1576
1577     pPacket->ScsiStatus         = cmd.status;
1578     pPacket->DataTransferLength = cmd.resid;
1579     pPacket->SenseInfoLength    = cmd.sb_len_wr;
1580
1581     ret = CDROM_GetStatusCode(io);
1582
1583 #elif defined(__NetBSD__)
1584     scsireq_t cmd;
1585     int io;
1586
1587     if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1588         return STATUS_BUFFER_TOO_SMALL;
1589
1590     if (pPacket->CdbLength > 12)
1591         return STATUS_INVALID_PARAMETER;
1592
1593     if (pPacket->SenseInfoLength > SENSEBUFLEN)
1594         return STATUS_INVALID_PARAMETER;
1595
1596     memset(&cmd, 0, sizeof(cmd));
1597     memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1598
1599     if ( pPacket->DataBufferOffset > 0x1000 )
1600     {
1601         cmd.databuf     = (void*)pPacket->DataBufferOffset;
1602     }
1603     else
1604     {
1605         cmd.databuf     = (char*)pPacket + pPacket->DataBufferOffset;
1606     }
1607
1608     cmd.cmdlen         = pPacket->CdbLength;
1609     cmd.datalen        = pPacket->DataTransferLength;
1610     cmd.senselen       = pPacket->SenseInfoLength;
1611     cmd.timeout        = pPacket->TimeOutValue*1000; /* in milliseconds */
1612
1613     switch (pPacket->DataIn)
1614     {
1615     case SCSI_IOCTL_DATA_OUT:
1616         cmd.flags |= SCCMD_WRITE;
1617         break;
1618     case SCSI_IOCTL_DATA_IN:
1619         cmd.flags |= SCCMD_READ;
1620         break;
1621     case SCSI_IOCTL_DATA_UNSPECIFIED:
1622         cmd.flags = 0;
1623         break;
1624     default:
1625        return STATUS_INVALID_PARAMETER;
1626     }
1627
1628     io = ioctl(fd, SCIOCCOMMAND, &cmd);
1629
1630     switch (cmd.retsts)
1631     {
1632     case SCCMD_OK:         break;
1633     case SCCMD_TIMEOUT:    return STATUS_TIMEOUT;
1634                            break;
1635     case SCCMD_BUSY:       return STATUS_DEVICE_BUSY;
1636                            break;
1637     case SCCMD_SENSE:      break;
1638     case SCCMD_UNKNOWN:    return STATUS_UNSUCCESSFUL;
1639                            break;
1640     }
1641
1642     if (pPacket->SenseInfoLength != 0)
1643     {
1644         memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1645                cmd.sense, pPacket->SenseInfoLength);
1646     }
1647
1648     pPacket->ScsiStatus = cmd.status;
1649
1650     ret = CDROM_GetStatusCode(io);
1651 #endif
1652     return ret;
1653 }
1654
1655 /******************************************************************
1656  *              CDROM_ScsiGetCaps
1657  *
1658  *
1659  */
1660 static NTSTATUS CDROM_ScsiGetCaps(PIO_SCSI_CAPABILITIES caps)
1661 {
1662     NTSTATUS    ret = STATUS_NOT_IMPLEMENTED;
1663
1664     caps->Length = sizeof(*caps);
1665 #if defined(linux)
1666     caps->MaximumTransferLength = SG_SCATTER_SZ; /* FIXME */
1667     caps->MaximumPhysicalPages = SG_SCATTER_SZ / getpagesize();
1668     caps->SupportedAsynchronousEvents = TRUE;
1669     caps->AlignmentMask = getpagesize();
1670     caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
1671     caps->AdapterScansDown = FALSE; /* FIXME ? */
1672     caps->AdapterUsesPio = FALSE; /* FIXME ? */
1673     ret = STATUS_SUCCESS;
1674 #else
1675     FIXME("Unimplemented\n");
1676 #endif
1677     return ret;
1678 }
1679
1680 /******************************************************************
1681  *              CDROM_GetAddress
1682  *
1683  * implements IOCTL_SCSI_GET_ADDRESS
1684  */
1685 static NTSTATUS CDROM_GetAddress(int fd, SCSI_ADDRESS* address)
1686 {
1687     int portnum, busid, targetid, lun;
1688
1689     address->Length = sizeof(SCSI_ADDRESS);
1690     if ( ! CDROM_GetInterfaceInfo(fd, &portnum, &busid, &targetid, &lun))
1691         return STATUS_NOT_SUPPORTED;
1692
1693     address->PortNumber = portnum;
1694     address->PathId = busid; /* bus number */
1695     address->TargetId = targetid;
1696     address->Lun = lun;
1697     return STATUS_SUCCESS;
1698 }
1699
1700 /******************************************************************
1701  *              DVD_StartSession
1702  *
1703  *
1704  */
1705 static NTSTATUS DVD_StartSession(int fd, PDVD_SESSION_ID sid_in, PDVD_SESSION_ID sid_out)
1706 {
1707 #if defined(linux)
1708     NTSTATUS ret = STATUS_NOT_SUPPORTED;
1709     dvd_authinfo auth_info;
1710
1711     memset( &auth_info, 0, sizeof( auth_info ) );
1712     auth_info.type = DVD_LU_SEND_AGID;
1713     if (sid_in) auth_info.lsa.agid = *(int*)sid_in; /* ?*/
1714
1715     TRACE("fd 0x%08x\n",fd);
1716     ret =CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
1717     *sid_out = auth_info.lsa.agid;
1718     return ret;
1719 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1720     return STATUS_NOT_SUPPORTED;
1721 #else
1722     return STATUS_NOT_SUPPORTED;
1723 #endif
1724 }
1725
1726 /******************************************************************
1727  *              DVD_EndSession
1728  *
1729  *
1730  */
1731 static NTSTATUS DVD_EndSession(int fd, PDVD_SESSION_ID sid)
1732 {
1733 #if defined(linux)
1734     dvd_authinfo auth_info;
1735
1736     memset( &auth_info, 0, sizeof( auth_info ) );
1737     auth_info.type = DVD_INVALIDATE_AGID;
1738     auth_info.lsa.agid = *(int*)sid;
1739
1740     TRACE("\n");
1741     return CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
1742 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1743     return STATUS_NOT_SUPPORTED;
1744 #else
1745     return STATUS_NOT_SUPPORTED;
1746 #endif
1747 }
1748
1749 /******************************************************************
1750  *              DVD_SendKey
1751  *
1752  *
1753  */
1754 static NTSTATUS DVD_SendKey(int fd, PDVD_COPY_PROTECT_KEY key)
1755 {
1756 #if defined(linux)
1757     NTSTATUS ret = STATUS_NOT_SUPPORTED;
1758     dvd_authinfo auth_info;
1759
1760     memset( &auth_info, 0, sizeof( auth_info ) );
1761     switch (key->KeyType)
1762     {
1763     case DvdChallengeKey:
1764         auth_info.type = DVD_HOST_SEND_CHALLENGE;
1765         auth_info.hsc.agid = (int)key->SessionId;
1766         TRACE("DvdChallengeKey ioc 0x%x\n", DVD_AUTH );
1767         memcpy( auth_info.hsc.chal, key->KeyData, DVD_CHALLENGE_SIZE );
1768         ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1769         break;
1770     case DvdBusKey2:
1771         auth_info.type = DVD_HOST_SEND_KEY2;
1772         auth_info.hsk.agid = (int)key->SessionId;
1773
1774         memcpy( auth_info.hsk.key, key->KeyData, DVD_KEY_SIZE );
1775         
1776         TRACE("DvdBusKey2\n");
1777         ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1778         break;
1779
1780     default:
1781         FIXME("Unknown Keytype 0x%x\n",key->KeyType);
1782     }
1783     return ret;
1784 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1785     TRACE("bsd\n");
1786     return STATUS_NOT_SUPPORTED;
1787 #else
1788     TRACE("outside\n");
1789     return STATUS_NOT_SUPPORTED;
1790 #endif
1791     TRACE("not reached\n");
1792 }
1793
1794 /******************************************************************
1795  *              DVD_ReadKey
1796  *
1797  *
1798  */
1799 static NTSTATUS DVD_ReadKey(int fd, PDVD_COPY_PROTECT_KEY key)
1800 {
1801 #if defined(linux)
1802     NTSTATUS ret = STATUS_NOT_SUPPORTED;
1803     dvd_struct dvd;
1804     dvd_authinfo auth_info;
1805
1806     memset( &dvd, 0, sizeof( dvd_struct ) );
1807     memset( &auth_info, 0, sizeof( auth_info ) );
1808     switch (key->KeyType)
1809     {
1810     case DvdDiskKey:
1811         
1812         dvd.type = DVD_STRUCT_DISCKEY;
1813         dvd.disckey.agid = (int)key->SessionId;
1814         memset( dvd.disckey.value, 0, DVD_DISCKEY_SIZE );
1815         
1816         TRACE("DvdDiskKey\n");
1817         ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
1818         if (ret == STATUS_SUCCESS)
1819             memcpy(key->KeyData,dvd.disckey.value,DVD_DISCKEY_SIZE);
1820         break;
1821     case DvdTitleKey:
1822         auth_info.type = DVD_LU_SEND_TITLE_KEY;
1823         auth_info.lstk.agid = (int)key->SessionId;
1824         auth_info.lstk.lba = (int)(key->Parameters.TitleOffset.QuadPart>>11);
1825         TRACE("DvdTitleKey session %d Quadpart 0x%08lx offset 0x%08x\n",
1826               (int)key->SessionId, (long)key->Parameters.TitleOffset.QuadPart, 
1827               auth_info.lstk.lba);
1828         ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1829         if (ret == STATUS_SUCCESS)
1830             memcpy(key->KeyData, auth_info.lstk.title_key, DVD_KEY_SIZE );
1831         break;
1832     case DvdChallengeKey:
1833
1834         auth_info.type = DVD_LU_SEND_CHALLENGE;
1835         auth_info.lsc.agid = (int)key->SessionId;
1836
1837         TRACE("DvdChallengeKey\n");
1838         ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1839         if (ret == STATUS_SUCCESS)
1840             memcpy( key->KeyData, auth_info.lsc.chal, DVD_CHALLENGE_SIZE );
1841         break;
1842     case DvdAsf:
1843         auth_info.type = DVD_LU_SEND_ASF;
1844         TRACE("DvdAsf\n");
1845         auth_info.lsasf.asf=((PDVD_ASF)key->KeyData)->SuccessFlag;
1846         ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1847         ((PDVD_ASF)key->KeyData)->SuccessFlag = auth_info.lsasf.asf;
1848         break;
1849     case DvdBusKey1:
1850         auth_info.type = DVD_LU_SEND_KEY1;
1851         auth_info.lsk.agid = (int)key->SessionId;
1852
1853         TRACE("DvdBusKey1\n");
1854         ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1855
1856         if (ret == STATUS_SUCCESS)
1857             memcpy( key->KeyData, auth_info.lsk.key, DVD_KEY_SIZE );
1858         break;
1859     case DvdGetRpcKey:
1860         auth_info.type = DVD_LU_SEND_RPC_STATE;
1861
1862         TRACE("DvdGetRpcKey\n");
1863         ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1864
1865         if (ret == STATUS_SUCCESS)
1866         {
1867             ((PDVD_RPC_KEY)key->KeyData)->TypeCode = auth_info.lrpcs.type;
1868             ((PDVD_RPC_KEY)key->KeyData)->RegionMask = auth_info.lrpcs.region_mask;
1869             ((PDVD_RPC_KEY)key->KeyData)->RpcScheme = auth_info.lrpcs.rpc_scheme;
1870         }
1871         break;
1872     default:
1873         FIXME("Unknown keytype 0x%x\n",key->KeyType);
1874     }
1875     return ret;
1876 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1877     TRACE("bsd\n");
1878     return STATUS_NOT_SUPPORTED;
1879 #else
1880     TRACE("outside\n");
1881     return STATUS_NOT_SUPPORTED;
1882 #endif
1883     TRACE("not reached\n");
1884 }
1885
1886 /******************************************************************
1887  *              DVD_GetRegion
1888  *
1889  *
1890  */
1891 static NTSTATUS DVD_GetRegion(int dev, PDVD_REGION region)
1892 {
1893     FIXME("\n");
1894     return STATUS_SUCCESS;
1895
1896 }
1897
1898 /******************************************************************
1899  *              DVD_GetRegion
1900  *
1901  *
1902  */
1903 static NTSTATUS DVD_ReadStructure(int dev, PDVD_READ_STRUCTURE structure, PDVD_LAYER_DESCRIPTOR layer)
1904 {
1905     FIXME("\n");
1906     return STATUS_SUCCESS;
1907
1908 }
1909 /******************************************************************
1910  *              CDROM_DeviceIoControl
1911  *
1912  *
1913  */
1914 NTSTATUS CDROM_DeviceIoControl(HANDLE hDevice, 
1915                                HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1916                                PVOID UserApcContext, 
1917                                PIO_STATUS_BLOCK piosb, 
1918                                ULONG dwIoControlCode,
1919                                LPVOID lpInBuffer, DWORD nInBufferSize,
1920                                LPVOID lpOutBuffer, DWORD nOutBufferSize)
1921 {
1922     DWORD       sz = 0;
1923     NTSTATUS    status = STATUS_SUCCESS;
1924     int fd, dev;
1925
1926     TRACE("%lx %s %lx %ld %lx %ld %p\n",
1927           (DWORD)hDevice, iocodex(dwIoControlCode), (DWORD)lpInBuffer, nInBufferSize,
1928           (DWORD)lpOutBuffer, nOutBufferSize, piosb);
1929
1930     piosb->Information = 0;
1931
1932     if ((status = wine_server_handle_to_fd( hDevice, 0, &fd, NULL ))) goto error;
1933     if ((status = CDROM_Open(fd, &dev)))
1934     {
1935         wine_server_release_fd( hDevice, fd );
1936         goto error;
1937     }
1938
1939     switch (dwIoControlCode)
1940     {
1941     case IOCTL_STORAGE_CHECK_VERIFY:
1942     case IOCTL_CDROM_CHECK_VERIFY:
1943         sz = 0;
1944         CDROM_ClearCacheEntry(dev);
1945         if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
1946             status = STATUS_INVALID_PARAMETER;
1947         else status = CDROM_Verify(dev, fd);
1948         break;
1949
1950 /* EPP     case IOCTL_STORAGE_CHECK_VERIFY2: */
1951
1952 /* EPP     case IOCTL_STORAGE_FIND_NEW_DEVICES: */
1953 /* EPP     case IOCTL_CDROM_FIND_NEW_DEVICES: */
1954
1955     case IOCTL_STORAGE_LOAD_MEDIA:
1956     case IOCTL_CDROM_LOAD_MEDIA:
1957         sz = 0;
1958         CDROM_ClearCacheEntry(dev);
1959         if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
1960             status = STATUS_INVALID_PARAMETER;
1961         else status = CDROM_SetTray(fd, FALSE);
1962         break;
1963      case IOCTL_STORAGE_EJECT_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, TRUE);
1969         break;
1970
1971     case IOCTL_CDROM_MEDIA_REMOVAL:
1972     case IOCTL_DISK_MEDIA_REMOVAL:
1973     case IOCTL_STORAGE_MEDIA_REMOVAL:
1974     case IOCTL_STORAGE_EJECTION_CONTROL:
1975         /* FIXME the last ioctl:s is not the same as the two others...
1976          * lockcount/owner should be handled */
1977         sz = 0;
1978         CDROM_ClearCacheEntry(dev);
1979         if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
1980         else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) status = STATUS_BUFFER_TOO_SMALL;
1981         else status = CDROM_ControlEjection(fd, (const PREVENT_MEDIA_REMOVAL*)lpInBuffer);
1982         break;
1983
1984 /* EPP     case IOCTL_STORAGE_GET_MEDIA_TYPES: */
1985
1986     case IOCTL_STORAGE_GET_DEVICE_NUMBER:
1987         sz = sizeof(STORAGE_DEVICE_NUMBER);
1988         if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
1989         else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
1990         else status = CDROM_GetDeviceNumber(dev, (STORAGE_DEVICE_NUMBER*)lpOutBuffer);
1991         break;
1992
1993     case IOCTL_STORAGE_RESET_DEVICE:
1994         sz = 0;
1995         CDROM_ClearCacheEntry(dev);
1996         if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
1997             status = STATUS_INVALID_PARAMETER;
1998         else status = CDROM_ResetAudio(fd);
1999         break;
2000
2001     case IOCTL_CDROM_GET_CONTROL:
2002         sz = sizeof(CDROM_AUDIO_CONTROL);
2003         if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2004         else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2005         else status = CDROM_GetControl(dev, (CDROM_AUDIO_CONTROL*)lpOutBuffer);
2006         break;
2007
2008     case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
2009         sz = sizeof(DISK_GEOMETRY);
2010         if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2011         else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2012         else status = CDROM_GetDriveGeometry(dev, fd, (DISK_GEOMETRY*)lpOutBuffer);
2013         break;
2014
2015     case IOCTL_CDROM_DISK_TYPE:
2016         sz = sizeof(CDROM_DISK_DATA);
2017         /* CDROM_ClearCacheEntry(dev); */
2018         if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2019         else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2020         else status = CDROM_GetDiskData(dev, fd, (CDROM_DISK_DATA*)lpOutBuffer);
2021         break;
2022
2023 /* EPP     case IOCTL_CDROM_GET_LAST_SESSION: */
2024
2025     case IOCTL_CDROM_READ_Q_CHANNEL:
2026         sz = sizeof(SUB_Q_CHANNEL_DATA);
2027         if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT))
2028             status = STATUS_INVALID_PARAMETER;
2029         else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2030         else status = CDROM_ReadQChannel(dev, fd, (const CDROM_SUB_Q_DATA_FORMAT*)lpInBuffer,
2031                                         (SUB_Q_CHANNEL_DATA*)lpOutBuffer);
2032         break;
2033
2034     case IOCTL_CDROM_READ_TOC:
2035         sz = sizeof(CDROM_TOC);
2036         if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2037         else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2038         else status = CDROM_ReadTOC(dev, fd, (CDROM_TOC*)lpOutBuffer);
2039         break;
2040
2041 /* EPP     case IOCTL_CDROM_READ_TOC_EX: */
2042
2043     case IOCTL_CDROM_PAUSE_AUDIO:
2044         sz = 0;
2045         if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2046             status = STATUS_INVALID_PARAMETER;
2047         else status = CDROM_PauseAudio(fd);
2048         break;
2049     case IOCTL_CDROM_PLAY_AUDIO_MSF:
2050         sz = 0;
2051         if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2052         else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
2053         else status = CDROM_PlayAudioMSF(fd, (const CDROM_PLAY_AUDIO_MSF*)lpInBuffer);
2054         break;
2055     case IOCTL_CDROM_RESUME_AUDIO:
2056         sz = 0;
2057         if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2058             status = STATUS_INVALID_PARAMETER;
2059         else status = CDROM_ResumeAudio(fd);
2060         break;
2061     case IOCTL_CDROM_SEEK_AUDIO_MSF:
2062         sz = 0;
2063         if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2064         else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
2065         else status = CDROM_SeekAudioMSF(dev, fd, (const CDROM_SEEK_AUDIO_MSF*)lpInBuffer);
2066         break;
2067     case IOCTL_CDROM_STOP_AUDIO:
2068         sz = 0;
2069         CDROM_ClearCacheEntry(dev); /* Maybe intention is to change media */
2070         if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2071             status = STATUS_INVALID_PARAMETER;
2072         else status = CDROM_StopAudio(fd);
2073         break;
2074     case IOCTL_CDROM_GET_VOLUME:
2075         sz = sizeof(VOLUME_CONTROL);
2076         if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2077         else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2078         else status = CDROM_GetVolume(fd, (VOLUME_CONTROL*)lpOutBuffer);
2079         break;
2080     case IOCTL_CDROM_SET_VOLUME:
2081         sz = 0;
2082         CDROM_ClearCacheEntry(dev);
2083         if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
2084             status = STATUS_INVALID_PARAMETER;
2085         else status = CDROM_SetVolume(fd, (const VOLUME_CONTROL*)lpInBuffer);
2086         break;
2087     case IOCTL_CDROM_RAW_READ:
2088         sz = 0;
2089         if (nInBufferSize < sizeof(RAW_READ_INFO)) status = STATUS_INVALID_PARAMETER;
2090         else if (lpOutBuffer == NULL) status = STATUS_BUFFER_TOO_SMALL;
2091         else status = CDROM_RawRead(fd, (const RAW_READ_INFO*)lpInBuffer,
2092                                    lpOutBuffer, nOutBufferSize, &sz);
2093         break;
2094     case IOCTL_SCSI_GET_ADDRESS:
2095         sz = sizeof(SCSI_ADDRESS);
2096         if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2097         else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2098         else status = CDROM_GetAddress(fd, (SCSI_ADDRESS*)lpOutBuffer);
2099         break;
2100     case IOCTL_SCSI_PASS_THROUGH_DIRECT:
2101         sz = sizeof(SCSI_PASS_THROUGH_DIRECT);
2102         if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2103         else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH_DIRECT)) status = STATUS_BUFFER_TOO_SMALL;
2104         else status = CDROM_ScsiPassThroughDirect(fd, (PSCSI_PASS_THROUGH_DIRECT)lpOutBuffer);
2105         break;
2106     case IOCTL_SCSI_PASS_THROUGH:
2107         sz = sizeof(SCSI_PASS_THROUGH);
2108         if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2109         else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH)) status = STATUS_BUFFER_TOO_SMALL;
2110         else status = CDROM_ScsiPassThrough(fd, (PSCSI_PASS_THROUGH)lpOutBuffer);
2111         break;
2112     case IOCTL_SCSI_GET_CAPABILITIES:
2113         sz = sizeof(IO_SCSI_CAPABILITIES);
2114         if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2115         else if (nOutBufferSize < sizeof(IO_SCSI_CAPABILITIES)) status = STATUS_BUFFER_TOO_SMALL;
2116         else status = CDROM_ScsiGetCaps((PIO_SCSI_CAPABILITIES)lpOutBuffer);
2117         break;
2118     case IOCTL_DVD_START_SESSION:
2119         sz = sizeof(DVD_SESSION_ID);
2120         if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2121         else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2122         else
2123         {
2124             TRACE("before in 0x%08lx out 0x%08lx\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
2125                   *(PDVD_SESSION_ID)lpOutBuffer);
2126             status = DVD_StartSession(fd, (PDVD_SESSION_ID)lpInBuffer, (PDVD_SESSION_ID)lpOutBuffer);
2127             TRACE("before in 0x%08lx out 0x%08lx\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
2128                   *(PDVD_SESSION_ID)lpOutBuffer);
2129         }
2130         break;
2131     case IOCTL_DVD_END_SESSION:
2132         sz = sizeof(DVD_SESSION_ID);
2133         if ((lpInBuffer == NULL) ||  (nInBufferSize < sz))status = STATUS_INVALID_PARAMETER;
2134         else status = DVD_EndSession(fd, (PDVD_SESSION_ID)lpInBuffer);
2135         break;
2136     case IOCTL_DVD_SEND_KEY:
2137         sz = 0;
2138         if (!lpInBuffer ||
2139             (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
2140             status = STATUS_INVALID_PARAMETER;
2141         else
2142         {
2143             TRACE("doing DVD_SendKey\n");
2144             status = DVD_SendKey(fd, (PDVD_COPY_PROTECT_KEY)lpInBuffer);
2145         }
2146         break;
2147     case IOCTL_DVD_READ_KEY:
2148         if (!lpInBuffer ||
2149             (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
2150             status = STATUS_INVALID_PARAMETER;
2151         else if (lpInBuffer !=lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
2152         else
2153         {
2154             TRACE("doing DVD_READ_KEY\n");
2155             sz = ((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength;
2156             status = DVD_ReadKey(fd, (PDVD_COPY_PROTECT_KEY)lpInBuffer);
2157         }
2158         break;
2159     case IOCTL_DVD_GET_REGION:
2160         sz = sizeof(DVD_REGION);
2161         if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2162         else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2163         TRACE("doing DVD_Get_REGION\n");
2164         status = DVD_GetRegion(fd, (PDVD_REGION)lpOutBuffer);
2165         break;
2166     case IOCTL_DVD_READ_STRUCTURE:
2167         sz = sizeof(DVD_LAYER_DESCRIPTOR);
2168         if (lpInBuffer == NULL || nInBufferSize != sizeof(DVD_READ_STRUCTURE)) status = STATUS_INVALID_PARAMETER;
2169         else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2170         TRACE("doing DVD_READ_STRUCTURE\n");
2171         status = DVD_ReadStructure(fd, (PDVD_READ_STRUCTURE)lpInBuffer, (PDVD_LAYER_DESCRIPTOR)lpOutBuffer);
2172         break;
2173
2174     default:
2175         FIXME("Unsupported IOCTL %lx (type=%lx access=%lx func=%lx meth=%lx)\n", 
2176               dwIoControlCode, dwIoControlCode >> 16, (dwIoControlCode >> 14) & 3,
2177               (dwIoControlCode >> 2) & 0xFFF, dwIoControlCode & 3);
2178         sz = 0;
2179         status = STATUS_INVALID_PARAMETER;
2180         break;
2181     }
2182     wine_server_release_fd( hDevice, fd );
2183  error:
2184     piosb->u.Status = status;
2185     piosb->Information = sz;
2186     if (hEvent) NtSetEvent(hEvent, NULL);
2187     return status;
2188 }