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