fdopen: don't rewind the file after creating the FILE* handle. Added
[wine] / dlls / winedos / int2f.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3  * DOS interrupt 2fh handler
4  *
5  *  Cdrom - device driver emulation - Audio features.
6  *     Copyright (c) 1998 Petr Tomasek <tomasek@etf.cuni.cz>
7  *     Copyright (c) 1999,2002 Eric Pouech
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
26 #include <string.h>
27 #include "wine/winbase16.h"
28 #include "miscemu.h"
29 #include "module.h"
30 /* #define DEBUG_INT */
31 #include "wine/debug.h"
32 #include "winioctl.h"
33 #include "ntddstor.h"
34 #include "ntddcdrm.h"
35 #include "dosexe.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(int);
38
39 /* base WPROCS.DLL ordinal number for VxDs */
40 #define VXD_BASE 400
41
42 static void do_int2f_16( CONTEXT86 *context );
43 static void MSCDEX_Handler( CONTEXT86 *context );
44
45 /**********************************************************************
46  *          DOSVM_Int2fHandler (WINEDOS16.147)
47  *
48  * Handler for int 2fh (multiplex).
49  */
50 void WINAPI DOSVM_Int2fHandler( CONTEXT86 *context )
51 {
52     TRACE("Subfunction 0x%X\n", AX_reg(context));
53
54     switch(AH_reg(context))
55     {
56     case 0x10:
57         SET_AL( context, 0xff ); /* share is installed */
58         break;
59
60     case 0x11:  /* Network Redirector / IFSFUNC */
61         switch (LOBYTE(context->Eax))
62         {
63         case 0x00:  /* Install check */
64             /* not installed */
65             break;
66         case 0x80:  /* Enhanced services - Install check */
67             /* not installed */
68             break;
69         default:
70            INT_BARF( context, 0x2f );
71             break;
72         }
73         break;
74
75     case 0x12:
76         switch (LOBYTE(context->Eax))
77         {
78         case 0x2e: /* get or set DOS error table address */
79             switch (DL_reg(context))
80             {
81             /* Four tables: even commands are 'get', odd are 'set' */
82             /* DOS 5.0+ ignores "set" commands */
83             case 0x01:
84             case 0x03:
85             case 0x05:
86             case 0x07:
87             case 0x09:
88                 break;
89             /* Instead of having a message table in DOS-space, */
90             /* we can use a special case for MS-DOS to force   */
91             /* the secondary interface.                               */
92             case 0x00:
93             case 0x02:
94             case 0x04:
95             case 0x06:
96                 context->SegEs = 0x0001;
97                 SET_DI( context, 0x0000 );
98                 break;
99             case 0x08:
100                 FIXME("No real-mode handler for errors yet! (bye!)\n");
101                 break;
102             default:
103                 INT_BARF(context, 0x2f);
104             }
105             break;
106         default:
107            INT_BARF(context, 0x2f);
108         }
109         break;
110
111     case 0x15: /* mscdex */
112         MSCDEX_Handler(context);
113         break;
114
115     case 0x16:
116         do_int2f_16( context );
117         break;
118
119     case 0x1a: /* ANSI.SYS / AVATAR.SYS Install Check */
120         /* Not supported yet, do nothing */
121         break;
122
123     case 0x43:
124 #if 1
125        switch (LOBYTE(context->Eax))
126        {
127        case 0x00:   /* XMS v2+ installation check */
128            WARN("XMS is not fully implemented\n");
129            SET_AL( context, 0x80 );
130            break;
131        case 0x10:   /* XMS v2+ get driver address */
132        {
133             context->SegEs = DOSVM_dpmi_segments->xms_seg;
134             SET_BX( context, 0 );
135             break;
136        }
137        default:
138            INT_BARF( context, 0x2f );
139        }
140 #else
141        FIXME("check for XMS (not supported)\n");
142        SET_AL( context, 0x42 ); /* != 0x80 */
143 #endif
144        break;
145
146     case 0x45:
147        switch (LOBYTE(context->Eax))
148        {
149        case 0x00:
150        case 0x01:
151        case 0x02:
152        case 0x03:
153        case 0x04:
154        case 0x05:
155        case 0x06:
156        case 0x07:
157        case 0x08:
158            /* Microsoft Profiler - not installed */
159            break;
160        default:
161             INT_BARF( context, 0x2f );
162        }
163        break;
164
165     case 0x4a:
166         switch(LOBYTE(context->Eax))
167         {
168        case 0x10:  /* smartdrv */
169            break;  /* not installed */
170         case 0x11:  /* dblspace */
171             break;  /* not installed */
172         case 0x12:  /* realtime compression interface */
173             break;  /* not installed */
174         case 0x32:  /* patch IO.SYS (???) */
175             break;  /* we have no IO.SYS, so we can't patch it :-/ */
176         default:
177             INT_BARF( context, 0x2f );
178         }
179         break;
180     case 0x4b:
181        switch(LOBYTE(context->Eax))
182        {
183        case 0x01:
184        case 0x02:
185        case 0x03:
186        case 0x04:
187        case 0x05:
188            FIXME("Task Switcher - not implemented\n");
189            break;
190        default:
191            INT_BARF( context, 0x2f );
192        }
193        break;
194     case 0x56:  /* INTERLNK */
195        switch(LOBYTE(context->Eax))
196        {
197        case 0x01:  /* check if redirected drive */
198            SET_AL( context, 0 ); /* not redirected */
199            break;
200        default:
201            INT_BARF( context, 0x2f );
202        }
203        break;
204     case 0x7a:  /* NOVELL NetWare */
205         switch (LOBYTE(context->Eax))
206         {
207        case 0x0:  /* Low-level Netware installation check AL=0 not installed.*/
208             SET_AL( context, 0 );
209             break;
210         case 0x20:  /* Get VLM Call Address */
211             /* return nothing -> NetWare not installed */
212             break;
213         default:
214            INT_BARF( context, 0x2f );
215             break;
216         }
217         break;
218     case 0xb7:  /* append */
219         SET_AL( context, 0 ); /* not installed */
220         break;
221     case 0xb8:  /* network */
222         switch (LOBYTE(context->Eax))
223         {
224         case 0x00:  /* Install check */
225             /* not installed */
226             break;
227         default:
228            INT_BARF( context, 0x2f );
229             break;
230         }
231         break;
232     case 0xbd:  /* some Novell network install check ??? */
233         SET_AX( context, 0xa5a5 ); /* pretend to have Novell IPX installed */
234        break;
235     case 0xbf:  /* REDIRIFS.EXE */
236         switch (LOBYTE(context->Eax))
237         {
238         case 0x00:  /* Install check */
239             /* not installed */
240             break;
241         default:
242            INT_BARF( context, 0x2f );
243             break;
244         }
245         break;
246     case 0xd2:
247        switch(LOBYTE(context->Eax))
248        {
249        case 0x01: /* Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE functions */
250            if(BX_reg(context) == 0x5145 && CX_reg(context) == 0x4D4D
251              && DX_reg(context) == 0x3432)
252                TRACE("Check for QEMM v5.0+ (not installed)\n");
253                break;
254        default:
255            INT_BARF( context, 0x2f );
256            break;
257        }
258        break;
259     case 0xd7:  /* Banyan Vines */
260         switch (LOBYTE(context->Eax))
261         {
262         case 0x01:  /* Install check - Get Int Number */
263             /* not installed */
264             break;
265         default:
266            INT_BARF( context, 0x2f );
267             break;
268         }
269         break;
270     case 0xde:
271        switch(LOBYTE(context->Eax))
272        {
273        case 0x01:   /* Quarterdeck QDPMI.SYS - DESQview */
274            if(BX_reg(context) == 0x4450 && CX_reg(context) == 0x4d49
275              && DX_reg(context) == 0x8f4f)
276                TRACE("Check for QDPMI.SYS (not installed)\n");
277                break;
278        default:
279            INT_BARF( context, 0x2f );
280            break;
281        }
282        break;
283     case 0xfa:  /* Watcom debugger check, returns 0x666 if installed */
284         break;
285     default:
286         INT_BARF( context, 0x2f );
287         break;
288     }
289 }
290
291
292 /**********************************************************************
293  *         do_int2f_16
294  */
295 static void do_int2f_16( CONTEXT86 *context )
296 {
297     DWORD addr;
298
299     switch(LOBYTE(context->Eax))
300     {
301     case 0x00:  /* Windows enhanced mode installation check */
302         SET_AX( context, (GetWinFlags16() & WF_ENHANCED) ? LOWORD(GetVersion16()) : 0 );
303         break;
304
305     case 0x0a:  /* Get Windows version and type */
306         SET_AX( context, 0 );
307         SET_BX( context, (LOWORD(GetVersion16()) << 8) | (LOWORD(GetVersion16()) >> 8) );
308         SET_CX( context, (GetWinFlags16() & WF_ENHANCED) ? 3 : 2 );
309         break;
310
311     case 0x0b:  /* Identify Windows-aware TSRs */
312         /* we don't have any pre-Windows TSRs */
313         break;
314
315     case 0x11:  /* Get Shell Parameters - (SHELL= in CONFIG.SYS) */
316         /* We can mock this up. But not today... */
317         FIXME("Get Shell Parameters\n");
318         break;
319
320     case 0x80:  /* Release time-slice */
321        /* Linux sched_yield() still keeps burning CPU cycles
322         * if the current process is the only one in highest priority list
323         * (as Linux will immediately return to this process to waste
324         * more CPU cycles), so sched_yield() is essentially useless for us
325         * (poor API, if you ask me: its return code should indicate
326         * whether other processes did run in between, in order for us
327         * to be able to decide whether to do an additional Sleep() or not...)
328         * Thus we better unconditionally use a well-balanced Sleep()
329         * instead to really make sure the process calling int 0x2f/0x1680
330         * *doesn't* use 100% CPU...
331         */
332        Sleep(55); /* just wait 55ms (one "timer tick") for now. */
333        SET_AL( context, 0 );
334         break;
335
336     case 0x81: /* Begin critical section.  */
337         /* FIXME? */
338         break;
339
340     case 0x82: /* End critical section.  */
341         /* FIXME? */
342         break;
343
344     case 0x83:  /* Return Current Virtual Machine ID */
345         /* Virtual Machines are usually created/destroyed when Windows runs
346          * DOS programs. Since we never do, we are always in the System VM.
347          * According to Ralf Brown's Interrupt List, never return 0. But it
348          * seems to work okay (returning 0), just to be sure we return 1.
349          */
350        SET_BX( context, 1 ); /* VM 1 is probably the System VM */
351        break;
352
353     case 0x84:  /* Get device API entry point */
354         {
355             HMODULE16 mod = GetModuleHandle16("wprocs");
356             if (mod < 32) mod = LoadLibrary16( "wprocs" );
357             addr = (DWORD)GetProcAddress16( mod, (LPCSTR)(VXD_BASE + BX_reg(context)) );
358             if (!addr)  /* not supported */
359                 ERR("Accessing unknown VxD %04x - Expect a failure now.\n", BX_reg(context) );
360             context->SegEs = SELECTOROF(addr);
361             SET_DI( context, OFFSETOF(addr) );
362         }
363        break;
364
365     case 0x86:  /* DPMI detect mode */
366         SET_AX( context, 0 );  /* Running under DPMI */
367         break;
368
369     case 0x87: /* DPMI installation check */
370         {
371            SYSTEM_INFO si;
372            GetSystemInfo(&si);
373            SET_AX( context, 0x0000 ); /* DPMI Installed */
374             SET_BX( context, 0x0001 ); /* 32bits available */
375             SET_CL( context, si.wProcessorLevel );
376             SET_DX( context, 0x005a ); /* DPMI major/minor 0.90 */
377             SET_SI( context, 0 );      /* # of para. of DOS extended private data */
378             context->SegEs = DOSVM_dpmi_segments->dpmi_seg;
379             SET_DI( context, 0 );      /* ES:DI is DPMI switch entry point */
380             break;
381         }
382     case 0x8a:  /* DPMI get vendor-specific API entry point. */
383        /* The 1.0 specs say this should work with all 0.9 hosts.  */
384        break;
385
386     default:
387         INT_BARF( context, 0x2f );
388     }
389 }
390
391 /* FIXME: this macro may have to be changed on architectures where <size> reads/writes
392  * must be <size> aligned
393  * <size> could be WORD, DWORD...
394  * in this case, we would need two macros, one for read, the other one for write
395  * Note: PTR_AT can be used as an l-value
396  */
397 #define        PTR_AT(_ptr, _ofs, _typ)        (*((_typ*)(((char*)_ptr)+(_ofs))))
398
399 /* Use #if 1 if you want full int 2f debug... normal users can leave it at 0 */
400 #if 0
401 /**********************************************************************
402  *         MSCDEX_Dump                                 [internal]
403  *
404  * Dumps mscdex requests to int debug channel.
405  */
406 static void    MSCDEX_Dump(char* pfx, BYTE* req, int dorealmode)
407 {
408     int        i;
409     BYTE       buf[2048];
410     BYTE*      ptr;
411     BYTE*      ios;
412
413     ptr = buf;
414     ptr += sprintf(ptr, "%s\tCommand => ", pfx);
415     for (i = 0; i < req[0]; i++) {
416        ptr += sprintf(ptr, "%02x ", req[i]);
417     }
418
419     switch (req[2]) {
420     case 3:
421     case 12:
422        ptr += sprintf(ptr, "\n\t\t\t\tIO_struct => ");
423        ios = (dorealmode) ? PTR_REAL_TO_LIN( PTR_AT(req, 16, WORD), PTR_AT(req, 14, WORD)) :
424                              MapSL(MAKESEGPTR(PTR_AT(req, 16, WORD), PTR_AT(req, 14, WORD)));
425
426        for (i = 0; i < PTR_AT(req, 18, WORD); i++) {
427            ptr += sprintf(ptr, "%02x ", ios[i]);
428            if ((i & 0x1F) == 0x1F) {
429                *ptr++ = '\n';
430                *ptr = 0;
431            }
432        }
433        break;
434     }
435     TRACE("%s\n", buf);
436 }
437 #else
438 #define MSCDEX_Dump(pfx, req, drm)
439 #endif
440
441 #define CDFRAMES_PERSEC                 75
442 #define CDFRAMES_PERMIN                 (CDFRAMES_PERSEC * 60)
443 #define FRAME_OF_ADDR(a)        ((a)[1] * CDFRAMES_PERMIN + (a)[2] * CDFRAMES_PERSEC + (a)[3])
444 #define FRAME_OF_TOC(toc, idx)  FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
445 #define CTRL_OF_TOC(toc, idx)   (((toc).TrackData[idx - (toc).FirstTrack].Control << 4) | \
446                                   (toc).TrackData[idx - (toc).FirstTrack].Adr)
447
448 static void    MSCDEX_StoreMSF(DWORD frame, BYTE* val)
449 {
450     val[3] = 0;        /* zero */
451     val[2] = frame / CDFRAMES_PERMIN; /* minutes */
452     val[1] = (frame / CDFRAMES_PERSEC) % 60; /* seconds */
453     val[0] = frame % CDFRAMES_PERSEC; /* frames */
454 }
455
456 static int is_cdrom( int drive)
457 {
458     char root[] = "A:\\";
459     root[0] += drive;
460     return (GetDriveTypeA(root) == DRIVE_CDROM);
461 }
462
463 static void MSCDEX_Handler(CONTEXT86* context)
464 {
465     int        drive, count;
466     char*      p;
467
468     switch (LOBYTE(context->Eax)) {
469     case 0x00: /* Installation check */
470        /* Count the number of contiguous CDROM drives
471         */
472        for (drive = count = 0; drive < 26; drive++) {
473            if (is_cdrom(drive)) {
474                while (is_cdrom(drive + count)) count++;
475                break;
476            }
477        }
478        TRACE("Installation check: %d cdroms, starting at %d\n", count, drive);
479        SET_BX( context, count );
480        SET_CX( context, (drive < 26) ? drive : 0 );
481        break;
482
483     case 0x0B: /* drive check */
484        SET_AX( context, is_cdrom(CX_reg(context)) );
485        SET_BX( context, 0xADAD );
486        break;
487
488     case 0x0C: /* get version */
489        SET_BX( context, 0x020a );
490        TRACE("Version number => %04x\n", BX_reg(context));
491        break;
492
493     case 0x0D: /* get drive letters */
494        p = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx);
495        memset(p, 0, 26);
496        for (drive = 0; drive < 26; drive++) {
497            if (is_cdrom(drive)) *p++ = drive;
498        }
499        TRACE("Get drive letters\n");
500        break;
501
502     case 0x10: /* direct driver access */
503        {
504            BYTE*       driver_request;
505            BYTE*       io_stru;
506            BYTE        Error = 255; /* No Error */
507            int         dorealmode = ISV86(context);
508             char        devName[] = "\\\\.\\@:";
509            HANDLE      h;
510             CDROM_TOC                   toc;
511             CDROM_SUB_Q_DATA_FORMAT     fmt;
512             SUB_Q_CHANNEL_DATA          data;
513             DWORD                       br;
514             DWORD                       present = TRUE;
515
516            driver_request = CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx);
517
518            if (!driver_request) {
519                /* FIXME - to be deleted ?? */
520                ERR("ES:BX==0 ! SEGFAULT ?\n");
521                ERR("-->BX=0x%04x, ES=0x%04lx, DS=0x%04lx, CX=0x%04x\n",
522                    BX_reg(context), context->SegEs, context->SegDs, CX_reg(context));
523                driver_request[4] |= 0x80;
524                driver_request[3] = 5;  /* bad request length */
525                return;
526            }
527            /* FIXME
528              * the following tests are wrong because lots of functions don't require the
529              * tray to be closed with a CD inside
530             */
531            TRACE("CDROM device driver -> command <%d>\n", (unsigned char)driver_request[2]);
532
533            if (!is_cdrom(CX_reg(context))) {
534                WARN("Request made doesn't match a CD ROM drive (%d)\n", CX_reg(context));
535                driver_request[4] |= 0x80;
536                driver_request[3] = 1;  /* unknown unit */
537                return;
538            }
539
540            MSCDEX_Dump("Beg", driver_request, dorealmode);
541
542            /* set status to 0 */
543            PTR_AT(driver_request, 3, WORD) = 0;
544             devName[4] = 'A' + CX_reg(context);
545             h = CreateFileA(devName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
546             if (!h) {
547                WARN("Couldn't open cdrom handle\n");
548                driver_request[4] |= 0x80;
549                driver_request[3] = 1;  /* unknown unit */
550                return;
551            }
552
553             fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
554             if (!DeviceIoControl(h, IOCTL_CDROM_READ_TOC, NULL, 0, &toc, sizeof(toc), &br, NULL) ||
555                 !DeviceIoControl(h, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
556                                  &data, sizeof(data), &br, NULL)) {
557                 if (GetLastError() == STATUS_NO_MEDIA_IN_DEVICE)
558                 {
559                     if (driver_request[2] != 6 && driver_request[2] != 15)
560                     {
561                         driver_request[4] |= 0x80;
562                         driver_request[3] = 2; /* drive not ready */
563                         CloseHandle(h);
564                         return;
565                     }
566                     present = FALSE;
567                 }
568                 else
569                 {
570                     driver_request[4] |= 0x80;
571                     driver_request[3] = 1;     /* unknown unit */
572                     CloseHandle(h);
573                     return;
574                 }
575             }
576
577            switch (driver_request[2]) {
578            case 3:
579                io_stru = (dorealmode) ?
580                     PTR_REAL_TO_LIN( PTR_AT(driver_request, 16, WORD), PTR_AT(driver_request, 14, WORD) ) :
581                        MapSL( MAKESEGPTR(PTR_AT(driver_request, 16, WORD), PTR_AT(driver_request, 14, WORD)));
582
583                TRACE(" --> IOCTL INPUT <%d>\n", io_stru[0]);
584                switch (io_stru[0]) {
585 #if 0
586                case 0: /* Get device Header */
587                    {
588                        static  LPSTR ptr = 0;
589                        if (ptr == 0)   {
590                            ptr = SEGPTR_ALLOC(22);
591                            PTR_AT(ptr,  0, DWORD) = ~1;        /* Next Device Driver */
592                            PTR_AT(ptr,  4,  WORD) = 0xC800;    /* Device attributes  */
593                            PTR_AT(ptr,  6,  WORD) = 0x1234;    /* Pointer to device strategy routine: FIXME */
594                            PTR_AT(ptr,  8,  WORD) = 0x3142;    /* Pointer to device interrupt routine: FIXME */
595                            PTR_AT(ptr, 10,  char) = 'W';       /* 8-byte character device name field */
596                            PTR_AT(ptr, 11,  char) = 'I';
597                            PTR_AT(ptr, 12,  char) = 'N';
598                            PTR_AT(ptr, 13,  char) = 'E';
599                            PTR_AT(ptr, 14,  char) = '_';
600                            PTR_AT(ptr, 15,  char) = 'C';
601                            PTR_AT(ptr, 16,  char) = 'D';
602                            PTR_AT(ptr, 17,  char) = '_';
603                            PTR_AT(ptr, 18,  WORD) = 0;         /* Reserved (must be zero) */
604                            PTR_AT(ptr, 20,  BYTE) = 0;         /* Drive letter (must be zero) */
605                            PTR_AT(ptr, 21,  BYTE) = 1;         /* Number of units supported (one or more) FIXME*/
606                        }
607                        PTR_AT(io_stru, DWORD,  0) = SEGPTR_GET(ptr);
608                    }
609                    break;
610 #endif
611
612                case 1: /* location of head */
613                    switch (io_stru[1]) {
614                    case 0:
615                        PTR_AT(io_stru, 2, DWORD) =
616                             FRAME_OF_ADDR(data.CurrentPosition.AbsoluteAddress);
617                        break;
618                    case 1:
619                        MSCDEX_StoreMSF(FRAME_OF_ADDR(data.CurrentPosition.AbsoluteAddress),
620                                         io_stru + 2);
621                        break;
622                    default:
623                        ERR("CD-ROM driver: unsupported addressing mode !!\n");
624                        Error = 0x0c;
625                    }
626                    TRACE(" ----> HEAD LOCATION <%ld>\n", PTR_AT(io_stru, 2, DWORD));
627                    break;
628
629                case 4: /* Audio channel info */
630                    io_stru[1] = 0;
631                    io_stru[2] = 0xff;
632                    io_stru[3] = 1;
633                    io_stru[4] = 0xff;
634                    io_stru[5] = 2;
635                    io_stru[6] = 0;
636                    io_stru[7] = 3;
637                    io_stru[8] = 0;
638                    TRACE(" ----> AUDIO CHANNEL INFO\n");
639                    break;
640
641                case 6: /* device status */
642                    PTR_AT(io_stru, 1, DWORD) = 0x00000290;
643                    /* 290 =>
644                     * 1        Supports HSG and Red Book addressing modes
645                     * 0        Supports audio channel manipulation
646                     *
647                     * 1        Supports prefetching requests
648                     * 0        Reserved
649                     * 0        No interleaving
650                     * 1        Data read and plays audio/video tracks
651                     *
652                     * 0        Read only
653                     * 0        Supports only cooked reading
654                     * 0        Door locked
655                     * 0        see below (Door closed/opened)
656                     */
657                    if (!present) PTR_AT(io_stru, 1, DWORD) |= 1;
658                    TRACE(" ----> DEVICE STATUS <0x%08lx>\n", PTR_AT(io_stru, 1, DWORD));
659                    break;
660
661                case 8: /* Volume size */
662                    PTR_AT(io_stru, 1, DWORD) = FRAME_OF_TOC(toc, toc.LastTrack + 1) -
663                         FRAME_OF_TOC(toc, toc.FirstTrack) - 1;
664                    TRACE(" ----> VOLUME SIZE <%ld>\n", PTR_AT(io_stru, 1, DWORD));
665                    break;
666
667                case 9: /* media changed ? */
668                    /* answers don't know... -1/1 for yes/no would be better */
669                    io_stru[1] = 0; /* FIXME? 1? */
670                    TRACE(" ----> MEDIA CHANGED <%d>\n", io_stru[1]);
671                    break;
672
673                case 10: /* audio disk info */
674                    io_stru[1] = toc.FirstTrack; /* starting track of the disc */
675                    io_stru[2] = toc.LastTrack;  /* ending track */
676                    MSCDEX_StoreMSF(FRAME_OF_TOC(toc, toc.LastTrack + 1) -
677                                     FRAME_OF_TOC(toc, toc.FirstTrack) - 1, io_stru + 3);
678
679                    TRACE(" ----> AUDIO DISK INFO <%d-%d/%08lx>\n",
680                          io_stru[1], io_stru[2], PTR_AT(io_stru, 3, DWORD));
681                    break;
682
683                case 11: /* audio track info */
684                    if (io_stru[1] >= toc.FirstTrack && io_stru[1] <= toc.LastTrack) {
685                        MSCDEX_StoreMSF(FRAME_OF_TOC(toc, io_stru[1]), io_stru + 2);
686                        /* starting point if the track */
687                        io_stru[6] = CTRL_OF_TOC(toc, io_stru[1]);
688                    } else {
689                        PTR_AT(io_stru, 2, DWORD) = 0;
690                        io_stru[6] = 0;
691                    }
692                    TRACE(" ----> AUDIO TRACK INFO[%d] = [%08lx:%d]\n",
693                          io_stru[1], PTR_AT(io_stru, 2, DWORD), io_stru[6]);
694                    break;
695
696                case 12: /* get Q-Channel info */
697                    io_stru[1] = CTRL_OF_TOC(toc, data.CurrentPosition.TrackNumber);
698                    io_stru[2] = data.CurrentPosition.TrackNumber;
699                    io_stru[3] = 0; /* FIXME ?? */
700
701                    /* why the heck did MS use another format for 0MSF information... sigh */
702                    {
703                        BYTE    bTmp[4];
704
705                        MSCDEX_StoreMSF(FRAME_OF_ADDR(data.CurrentPosition.TrackRelativeAddress), bTmp);
706                        io_stru[ 4] = bTmp[2];
707                        io_stru[ 5] = bTmp[1];
708                        io_stru[ 6] = bTmp[0];
709                        io_stru[ 7] = 0;
710
711                        MSCDEX_StoreMSF(FRAME_OF_ADDR(data.CurrentPosition.AbsoluteAddress), bTmp);
712                        io_stru[ 8] = bTmp[2];
713                        io_stru[ 9] = bTmp[1];
714                        io_stru[10] = bTmp[0];
715                        io_stru[11] = 0;
716                    }
717                    TRACE("Q-Channel info: Ctrl/adr=%02x TNO=%02x X=%02x rtt=%02x:%02x:%02x rtd=%02x:%02x:%02x (cf=%08x, tp=%08x)\n",
718                          io_stru[ 1], io_stru[ 2], io_stru[ 3],
719                          io_stru[ 4], io_stru[ 5], io_stru[ 6],
720                          io_stru[ 8], io_stru[ 9], io_stru[10],
721                          FRAME_OF_ADDR(data.CurrentPosition.AbsoluteAddress),
722                           FRAME_OF_TOC(toc, data.CurrentPosition.TrackNumber));
723                    break;
724
725                case 15: /* Audio status info */
726                    /* !!!! FIXME FIXME FIXME !! */
727                    PTR_AT(io_stru, 1,  WORD) = 2 | ((data.CurrentPosition.Header.AudioStatus == AUDIO_STATUS_PAUSED) ? 1 : 0);
728                    if (!present) {
729                        PTR_AT(io_stru, 3, DWORD) = 0;
730                        PTR_AT(io_stru, 7, DWORD) = 0;
731                    } else {
732                        PTR_AT(io_stru, 3, DWORD) = FRAME_OF_TOC(toc, toc.FirstTrack);
733                        PTR_AT(io_stru, 7, DWORD) = FRAME_OF_TOC(toc, toc.LastTrack + 1);
734                    }
735                    TRACE("Audio status info: status=%04x startLoc=%ld endLoc=%ld\n",
736                          PTR_AT(io_stru, 1, WORD), PTR_AT(io_stru, 3, DWORD), PTR_AT(io_stru, 7, DWORD));
737                    break;
738
739                default:
740                    FIXME("IOCTL INPUT: Unimplemented <%d>!!\n", io_stru[0]);
741                    Error = 0x0c;
742                    break;
743                }
744                break;
745
746            case 12:
747                io_stru = (dorealmode) ?
748                    PTR_REAL_TO_LIN( PTR_AT(driver_request, 16, WORD), PTR_AT(driver_request, 14, WORD)) :
749                        MapSL( MAKESEGPTR(PTR_AT(driver_request, 16, WORD), PTR_AT(driver_request, 14, WORD)));
750
751                TRACE(" --> IOCTL OUTPUT <%d>\n", io_stru[0]);
752                switch (io_stru[0]) {
753                case 0: /* eject */
754                     DeviceIoControl(h, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &br, NULL);
755                    TRACE(" ----> EJECT\n");
756                    break;
757                case 2: /* reset drive */
758                    DeviceIoControl(h, IOCTL_STORAGE_RESET_DEVICE, NULL, 0, NULL, 0, &br, NULL);
759                    TRACE(" ----> RESET\n");
760                    break;
761                case 3: /* Audio Channel Control */
762                    FIXME(" ----> AUDIO CHANNEL CONTROL (NIY)\n");
763                    break;
764                case 5: /* close tray */
765                     DeviceIoControl(h, IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0, &br, NULL);
766                    TRACE(" ----> CLOSE TRAY\n");
767                    break;
768                default:
769                    FIXME(" IOCTL OUTPUT: Unimplemented <%d>!!\n", io_stru[0]);
770                    Error = 0x0c;
771                    break;
772                }
773                break;
774
775             case 128: /* read long */
776                 {
777                     LPVOID              dst = MapSL(MAKESEGPTR(PTR_AT(driver_request, 16, WORD),
778                                                                PTR_AT(driver_request, 14, WORD)));
779                     DWORD               at = PTR_AT(driver_request, 20, DWORD);
780                     WORD                num = PTR_AT(driver_request, 18, WORD);
781                     RAW_READ_INFO       rri;
782
783                    switch (driver_request[13]) {
784                    case 1: /* Red book addressing mode = 0:m:s:f */
785                        /* FIXME : frame <=> msf conversion routines could be shared
786                         * between mscdex and mcicda
787                         */
788                        at = LOBYTE(HIWORD(at)) * CDFRAMES_PERMIN +
789                            HIBYTE(LOWORD(at)) * CDFRAMES_PERSEC +
790                            LOBYTE(LOWORD(at));
791                        /* fall through */
792                    case 0: /* HSG addressing mode */
793                         switch (PTR_AT(driver_request, 24, BYTE))
794                         {
795                         case 0: /* cooked */
796                             ReadFile(h, dst, num * 2048, &br, NULL);
797                             break;
798                         case 1:
799                             /* FIXME: computation is wrong */
800                             rri.DiskOffset.s.HighPart = 0;
801                             rri.DiskOffset.s.LowPart = at << 11;
802                             rri.TrackMode = YellowMode2;
803                             rri.SectorCount = num;
804                             DeviceIoControl(h, IOCTL_CDROM_RAW_READ, &rri, sizeof(rri),
805                                             dst, num * 2352, &br, NULL);
806                        break;
807                         default:
808                             ERR("Unsupported read mode !!\n");
809                             Error = 0x0c;
810                             break;
811                         }
812                         break;
813                    default:
814                        ERR("Unsupported address mode !!\n");
815                        Error = 0x0c;
816                        break;
817                    }
818                 }
819                 break;
820
821            case 131: /* seek */
822                {
823                    DWORD                       at;
824                     CDROM_SEEK_AUDIO_MSF        seek;
825
826                    at = PTR_AT(driver_request, 20, DWORD);
827
828                    TRACE(" --> SEEK AUDIO mode :<0x%02X>, [%ld]\n",
829                          (BYTE)driver_request[13], at);
830
831                    switch (driver_request[13]) {
832                    case 1: /* Red book addressing mode = 0:m:s:f */
833                        /* FIXME : frame <=> msf conversion routines could be shared
834                         * between mscdex and mcicda
835                         */
836                        at = LOBYTE(HIWORD(at)) * CDFRAMES_PERMIN +
837                            HIBYTE(LOWORD(at)) * CDFRAMES_PERSEC +
838                            LOBYTE(LOWORD(at));
839                        /* fall through */
840                    case 0: /* HSG addressing mode */
841                         seek.M = at / CDFRAMES_PERMIN;
842                         seek.S = (at / CDFRAMES_PERSEC) % 60;
843                         seek.F = at % CDFRAMES_PERSEC;
844                         DeviceIoControl(h, IOCTL_CDROM_SEEK_AUDIO_MSF, &seek, sizeof(seek),
845                                         NULL, 0, &br, NULL);
846                        break;
847                    default:
848                        ERR("Unsupported address mode !!\n");
849                        Error = 0x0c;
850                        break;
851                    }
852                }
853                break;
854
855            case 132: /* play */
856                {
857                    DWORD                       beg, end;
858                     CDROM_PLAY_AUDIO_MSF        play;
859
860                    beg = end = PTR_AT(driver_request, 14, DWORD);
861                    end += PTR_AT(driver_request, 18, DWORD);
862
863                    TRACE(" --> PLAY AUDIO mode :<0x%02X>, [%ld-%ld]\n",
864                          (BYTE)driver_request[13], beg, end);
865
866                    switch (driver_request[13]) {
867                    case 1:
868                        /* Red book addressing mode = 0:m:s:f */
869                        /* FIXME : frame <=> msf conversion routines could be shared
870                         * between mscdex and mcicda
871                         */
872                        beg = LOBYTE(LOWORD(beg)) * CDFRAMES_PERMIN +
873                            HIBYTE(LOWORD(beg)) * CDFRAMES_PERSEC +
874                            LOBYTE(HIWORD(beg));
875                        end = LOBYTE(LOWORD(end)) * CDFRAMES_PERMIN +
876                            HIBYTE(LOWORD(end)) * CDFRAMES_PERSEC +
877                            LOBYTE(HIWORD(end));
878                        /* fall through */
879                    case 0: /* HSG addressing mode */
880                         play.StartingM = beg / CDFRAMES_PERMIN;
881                         play.StartingS = (beg / CDFRAMES_PERSEC) % 60;
882                         play.StartingF = beg % CDFRAMES_PERSEC;
883                         play.EndingM   = end / CDFRAMES_PERMIN;
884                         play.EndingS   = (end / CDFRAMES_PERSEC) % 60;
885                         play.EndingF   = end % CDFRAMES_PERSEC;
886                         DeviceIoControl(h, IOCTL_CDROM_PLAY_AUDIO_MSF, &play, sizeof(play),
887                                         NULL, 0, &br, NULL);
888                        break;
889                    default:
890                        ERR("Unsupported address mode !!\n");
891                        Error = 0x0c;
892                        break;
893                    }
894                }
895                break;
896
897            case 133:
898                if (data.CurrentPosition.Header.AudioStatus == AUDIO_STATUS_IN_PROGRESS) {
899                     DeviceIoControl(h, IOCTL_CDROM_PAUSE_AUDIO, NULL, 0, NULL, 0, &br, NULL);
900                    TRACE(" --> STOP AUDIO (Paused)\n");
901                } else {
902                     DeviceIoControl(h, IOCTL_CDROM_STOP_AUDIO, NULL, 0, NULL, 0, &br, NULL);
903                    TRACE(" --> STOP AUDIO (Stopped)\n");
904                }
905                break;
906
907            case 136:
908                TRACE(" --> RESUME AUDIO\n");
909                 DeviceIoControl(h, IOCTL_CDROM_PAUSE_AUDIO, NULL, 0, NULL, 0, &br, NULL);
910                break;
911
912            default:
913                FIXME(" ioctl unimplemented <%d>\n", driver_request[2]);
914                Error = 0x0c;
915            }
916
917            /* setting error codes if any */
918            if (Error < 255) {
919                driver_request[4] |= 0x80;
920                driver_request[3] = Error;
921            }
922
923            CloseHandle(h);
924            /* setting status bits
925             * 3 == playing && done
926             * 1 == done
927             */
928            driver_request[4] |=
929                 (data.CurrentPosition.Header.AudioStatus == AUDIO_STATUS_IN_PROGRESS) ? 3 : 1;
930
931            MSCDEX_Dump("End", driver_request, dorealmode);
932        }
933        break;
934     default:
935        FIXME("Unimplemented MSCDEX function 0x%02X.\n", LOBYTE(context->Eax));
936        break;
937     }
938 }