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