Release 971101
[wine] / multimedia / mcicda.c
1 /*
2  * Sample MCI CDAUDIO Wine Driver for Linux
3  *
4  * Copyright 1994 Martin Ayotte
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <sys/ioctl.h>
13 #include "windows.h"
14 #include "user.h"
15 #include "driver.h"
16 #include "mmsystem.h"
17 #include "stddebug.h"
18 #include "debug.h"
19
20 #ifdef linux
21 #include <linux/soundcard.h>
22 #include <linux/cdrom.h>
23 #elif __FreeBSD__
24 #include <machine/soundcard.h>
25 #include <sys/cdio.h>
26 #endif
27
28 #define SOUND_DEV "/dev/dsp"
29 #ifdef __FreeBSD__
30 #define CDAUDIO_DEV "/dev/rcd0c"
31 #else
32 #define CDAUDIO_DEV "/dev/cdrom"
33 #endif
34
35 #ifdef SOUND_VERSION
36 #define IOCTL(a,b,c)            ioctl(a,b,&c)
37 #else
38 #define IOCTL(a,b,c)            (c = ioctl(a,b,c) )
39 #endif
40
41 #define MAX_CDAUDIODRV          (1)
42 #define MAX_CDAUDIO_TRACKS      256
43
44 #define CDFRAMES_PERSEC         75
45 #define CDFRAMES_PERMIN         4500
46 #define SECONDS_PERMIN          60
47
48 #if defined(linux) || defined(__FreeBSD__)
49 typedef struct {
50     int     nUseCount;          /* Incremented for each shared open */
51     BOOL16  fShareable;         /* TRUE if first open was shareable */
52     WORD    wNotifyDeviceID;    /* MCI device ID with a pending notification */
53     HANDLE16 hCallback;          /* Callback handle for pending notification */
54         MCI_OPEN_PARMS16 openParms;
55         DWORD   dwTimeFormat;
56         int             unixdev;
57 #ifdef linux
58         struct cdrom_subchnl    sc;
59 #elif __FreeBSD__
60         struct cd_sub_channel_info      sc;
61 #endif
62         int             mode;
63         UINT16  nCurTrack;
64         DWORD   dwCurFrame;
65         UINT16  nTracks;
66         DWORD   dwTotalLen;
67         LPDWORD lpdwTrackLen;
68         LPDWORD lpdwTrackPos;
69         DWORD   dwFirstOffset;
70         } LINUX_CDAUDIO;
71
72 static LINUX_CDAUDIO    CDADev[MAX_CDAUDIODRV];
73 #endif
74
75
76 /*-----------------------------------------------------------------------*/
77
78
79 /**************************************************************************
80 *                               CDAUDIO_GetNumberOfTracks               [internal]
81 */
82 static UINT16 CDAUDIO_GetNumberOfTracks(UINT16 wDevID)
83 {
84 #if defined(linux) || defined(__FreeBSD__)
85 #ifdef linux
86         struct cdrom_tochdr     hdr;
87 #elif __FreeBSD__
88         struct ioc_toc_header   hdr;
89 #endif
90
91         if (CDADev[wDevID].nTracks == 0) {
92                 if (ioctl(CDADev[wDevID].unixdev,
93 #ifdef linux
94                           CDROMREADTOCHDR
95 #elif __FreeBSD__
96                           CDIOREADTOCHEADER
97 #endif
98                           , &hdr)) {
99                         dprintf_cdaudio(stddeb,
100                                 "GetNumberOfTracks(%04X) // Error occured !\n", 
101                                 wDevID);
102                         return (WORD)-1;
103                         }
104 #ifdef linux
105                 CDADev[wDevID].nTracks = hdr.cdth_trk1;
106 #elif __FreeBSD__
107                 CDADev[wDevID].nTracks = hdr.ending_track - hdr.starting_track + 1;
108 #endif
109                 }
110         return CDADev[wDevID].nTracks;
111 #else
112         return (WORD)-1;
113 #endif
114 }
115
116
117 /**************************************************************************
118 *                               CDAUDIO_GetTracksInfo                   [internal]
119 */
120 static BOOL32 CDAUDIO_GetTracksInfo(UINT16 wDevID)
121 {
122 #if defined(linux) || defined(__FreeBSD__)
123         int             i, length;
124         int             start, last_start = 0;
125         int             total_length = 0;
126 #ifdef linux
127         struct cdrom_tocentry   entry;
128 #elif __FreeBSD__
129         struct ioc_read_toc_entry       entry;
130         struct cd_toc_entry             toc_buffer;
131 #endif
132
133         if (CDADev[wDevID].nTracks == 0) {
134                 if (CDAUDIO_GetNumberOfTracks(wDevID) == (WORD)-1) return FALSE;
135                 }
136         dprintf_cdaudio(stddeb,"CDAUDIO_GetTracksInfo // nTracks=%u\n", 
137                 CDADev[wDevID].nTracks);
138         if (CDADev[wDevID].lpdwTrackLen != NULL) 
139                 free(CDADev[wDevID].lpdwTrackLen);
140         CDADev[wDevID].lpdwTrackLen = (LPDWORD)malloc(
141                 (CDADev[wDevID].nTracks + 1) * sizeof(DWORD));
142         if (CDADev[wDevID].lpdwTrackPos != NULL) 
143                 free(CDADev[wDevID].lpdwTrackPos);
144         CDADev[wDevID].lpdwTrackPos = (LPDWORD)malloc(
145                 (CDADev[wDevID].nTracks + 1) * sizeof(DWORD));
146         if (CDADev[wDevID].lpdwTrackLen == NULL ||
147                 CDADev[wDevID].lpdwTrackPos == NULL) {
148                         dprintf_cdaudio(stddeb,
149                                 "CDAUDIO_GetTracksInfo // error allocating track table !\n");
150                 return FALSE;
151                 }
152         memset(CDADev[wDevID].lpdwTrackLen, 0, 
153                 (CDADev[wDevID].nTracks + 1) * sizeof(DWORD));
154         memset(CDADev[wDevID].lpdwTrackPos, 0, 
155                 (CDADev[wDevID].nTracks + 1) * sizeof(DWORD));
156         for (i = 0; i <= CDADev[wDevID].nTracks; i++) {
157                 if (i == CDADev[wDevID].nTracks)
158 #ifdef linux
159                         entry.cdte_track = CDROM_LEADOUT;
160 #elif __FreeBSD__
161 #define LEADOUT 0xaa
162                         entry.starting_track = LEADOUT; /* XXX */
163 #endif
164                 else
165 #ifdef linux
166                         entry.cdte_track = i + 1;
167 #elif __FreeBSD__
168                         entry.starting_track = i + 1;
169 #endif
170 #ifdef linux
171                 entry.cdte_format = CDROM_MSF;
172 #elif __FreeBSD__
173                 bzero((char *)&toc_buffer, sizeof(toc_buffer));
174                 entry.address_format = CD_MSF_FORMAT;
175                 entry.data_len = sizeof(toc_buffer);
176                 entry.data = &toc_buffer;
177 #endif
178                 if (ioctl(CDADev[wDevID].unixdev, 
179 #ifdef linux
180                           CDROMREADTOCENTRY
181 #elif __FreeBSD__
182                           CDIOREADTOCENTRYS
183 #endif
184                           , &entry)) {
185                         dprintf_cdaudio(stddeb,
186                                 "CDAUDIO_GetTracksInfo // error read entry\n");
187                         return FALSE;
188                         }
189 #ifdef linux
190                 start = CDFRAMES_PERSEC * (SECONDS_PERMIN * 
191                         entry.cdte_addr.msf.minute + entry.cdte_addr.msf.second) + 
192                         entry.cdte_addr.msf.frame;
193 #elif __FreeBSD__
194                 start = CDFRAMES_PERSEC * (SECONDS_PERMIN *
195                         toc_buffer.addr.msf.minute + toc_buffer.addr.msf.second) +
196                         toc_buffer.addr.msf.frame;
197 #endif
198                 if (i == 0) {
199                         last_start = start;
200                         CDADev[wDevID].dwFirstOffset = start;
201                         dprintf_cdaudio(stddeb,
202                                 "CDAUDIO_GetTracksInfo // dwFirstOffset=%u\n", 
203                                 start);
204                         }
205                 else {
206                         length = start - last_start;
207                         last_start = start;
208                         start = last_start - length;
209                         total_length += length;
210                         CDADev[wDevID].lpdwTrackLen[i - 1] = length;
211                         CDADev[wDevID].lpdwTrackPos[i - 1] = start;
212                         dprintf_cdaudio(stddeb,
213                         "CDAUDIO_GetTracksInfo // track #%u start=%u len=%u\n",
214                                 i, start, length);
215                         }
216                 }
217         CDADev[wDevID].dwTotalLen = total_length;
218         dprintf_cdaudio(stddeb,"CDAUDIO_GetTracksInfo // total_len=%u\n", 
219                 total_length);
220         return TRUE;
221 #else
222         return FALSE;
223 #endif
224 }
225
226
227 /**************************************************************************
228 *                               CDAUDIO_mciOpen                 [internal]
229 */
230 static DWORD CDAUDIO_mciOpen(UINT16 wDevID, DWORD dwFlags, LPMCI_OPEN_PARMS16 lpParms)
231 {
232 #if defined(linux) || defined(__FreeBSD__)
233         dprintf_cdaudio(stddeb,"CDAUDIO_mciOpen(%04X, %08lX, %p);\n", 
234                                         wDevID, dwFlags, lpParms);
235         if (lpParms == NULL) return MCIERR_INTERNAL;
236
237         if (CDADev[wDevID].nUseCount > 0) {
238                 /* The driver already open on this channel */
239                 /* If the driver was opened shareable before and this open specifies */
240                 /* shareable then increment the use count */
241                 if (CDADev[wDevID].fShareable && (dwFlags & MCI_OPEN_SHAREABLE))
242                         ++CDADev[wDevID].nUseCount;
243                 else
244                         return MCIERR_MUST_USE_SHAREABLE;
245                 }
246         else {
247                 CDADev[wDevID].nUseCount = 1;
248                 CDADev[wDevID].fShareable = dwFlags & MCI_OPEN_SHAREABLE;
249                 }
250     if (dwFlags & MCI_OPEN_ELEMENT) {
251                 dprintf_cdaudio(stddeb,"CDAUDIO_mciOpen // MCI_OPEN_ELEMENT !\n");
252 /*              return MCIERR_NO_ELEMENT_ALLOWED; */
253                 }
254         memcpy(&CDADev[wDevID].openParms, lpParms, sizeof(MCI_OPEN_PARMS16));
255         CDADev[wDevID].wNotifyDeviceID = lpParms->wDeviceID;
256         CDADev[wDevID].unixdev = open (CDAUDIO_DEV, O_RDONLY, 0);
257         if (CDADev[wDevID].unixdev == -1) {
258                 dprintf_cdaudio(stddeb,"CDAUDIO_mciOpen // can't open '%s' !\n", CDAUDIO_DEV);
259                 return MCIERR_HARDWARE;
260                 }
261         CDADev[wDevID].mode = 0;
262         CDADev[wDevID].dwTimeFormat = MCI_FORMAT_TMSF;
263         CDADev[wDevID].nCurTrack = 0;
264         CDADev[wDevID].nTracks = 0;
265         CDADev[wDevID].dwTotalLen = 0;
266         CDADev[wDevID].dwFirstOffset = 0;
267         CDADev[wDevID].lpdwTrackLen = NULL;
268         CDADev[wDevID].lpdwTrackPos = NULL;
269         if (!CDAUDIO_GetTracksInfo(wDevID)) {
270                 dprintf_cdaudio(stddeb,"CDAUDIO_mciOpen // error reading TracksInfo !\n");
271 /*              return MCIERR_INTERNAL; */
272                 }
273
274 /*
275    Moved to mmsystem.c mciOpen routine
276
277         if (dwFlags & MCI_NOTIFY) {
278                 dprintf_cdaudio(stddeb,
279                         "CDAUDIO_mciOpen // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
280                         lpParms->dwCallback);
281                 mciDriverNotify((HWND16)LOWORD(lpParms->dwCallback), 
282                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
283                 }
284 */
285         return 0;
286 #else
287         return MCIERR_HARDWARE;
288 #endif
289 }
290
291 /**************************************************************************
292 *                               CDAUDIO_mciClose                [internal]
293 */
294 static DWORD CDAUDIO_mciClose(UINT16 wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
295 {
296 #if defined(linux) || defined(__FreeBSD__)
297         dprintf_cdaudio(stddeb,"CDAUDIO_mciClose(%04X, %08lX, %p);\n", 
298                 wDevID, dwParam, lpParms);
299         if (CDADev[wDevID].lpdwTrackLen != NULL) free(CDADev[wDevID].lpdwTrackLen);
300         if (CDADev[wDevID].lpdwTrackPos != NULL) free(CDADev[wDevID].lpdwTrackPos);
301         close(CDADev[wDevID].unixdev);
302 #endif
303         return 0;
304 }
305
306 /**************************************************************************
307 *                               CDAUDIO_mciGetDevCaps   [internal]
308 */
309 static DWORD CDAUDIO_mciGetDevCaps(UINT16 wDevID, DWORD dwFlags, 
310                                                 LPMCI_GETDEVCAPS_PARMS lpParms)
311 {
312 #if defined(linux) || defined(__FreeBSD__)
313         dprintf_cdaudio(stddeb,"CDAUDIO_mciGetDevCaps(%04X, %08lX, %p);\n", 
314                 wDevID, dwFlags, lpParms);
315         if (lpParms == NULL) return MCIERR_INTERNAL;
316         if (dwFlags & MCI_GETDEVCAPS_ITEM) {
317                 dprintf_cdaudio(stddeb,
318                 "CDAUDIO_mciGetDevCaps // MCI_GETDEVCAPS_ITEM dwItem=%08lX;\n",
319                                 lpParms->dwItem);
320                 switch(lpParms->dwItem) {
321                         case MCI_GETDEVCAPS_CAN_RECORD:
322                                 lpParms->dwReturn = FALSE;
323                                 break;
324                         case MCI_GETDEVCAPS_HAS_AUDIO:
325                                 lpParms->dwReturn = TRUE;
326                                 break;
327                         case MCI_GETDEVCAPS_HAS_VIDEO:
328                                 lpParms->dwReturn = FALSE;
329                                 break;
330                         case MCI_GETDEVCAPS_DEVICE_TYPE:
331                                 lpParms->dwReturn = MCI_DEVTYPE_CD_AUDIO;
332                                 break;
333                         case MCI_GETDEVCAPS_USES_FILES:
334                                 lpParms->dwReturn = FALSE;
335                                 break;
336                         case MCI_GETDEVCAPS_COMPOUND_DEVICE:
337                                 lpParms->dwReturn = FALSE;
338                                 break;
339                         case MCI_GETDEVCAPS_CAN_EJECT:
340                                 lpParms->dwReturn = TRUE;
341                                 break;
342                         case MCI_GETDEVCAPS_CAN_PLAY:
343                                 lpParms->dwReturn = TRUE;
344                                 break;
345                         case MCI_GETDEVCAPS_CAN_SAVE:
346                                 lpParms->dwReturn = FALSE;
347                                 break;
348                         default:
349                                 return MCIERR_UNRECOGNIZED_COMMAND;
350                         }
351                 }
352         dprintf_cdaudio(stddeb,
353                 "CDAUDIO_mciGetDevCaps // lpParms->dwReturn=%08lX;\n", 
354                 lpParms->dwReturn);
355         return 0;
356 #else
357         return MCIERR_INTERNAL;
358 #endif
359 }
360
361 /**************************************************************************
362 *                               CDAUDIO_mciInfo                 [internal]
363 */
364 static DWORD CDAUDIO_mciInfo(UINT16 wDevID, DWORD dwFlags, LPMCI_INFO_PARMS16 lpParms)
365 {
366 #if defined(linux) || defined(__FreeBSD__)
367         dprintf_cdaudio(stddeb,"CDAUDIO_mciInfo(%04X, %08lX, %p);\n", 
368                 wDevID, dwFlags, lpParms);
369         if (lpParms == NULL) return MCIERR_INTERNAL;
370         lpParms->lpstrReturn = NULL;
371         switch(dwFlags) {
372                 case MCI_INFO_PRODUCT:
373                         lpParms->lpstrReturn = "Linux CDROM 0.5";
374                         break;
375                 default:
376                         return MCIERR_UNRECOGNIZED_COMMAND;
377                 }
378         if (lpParms->lpstrReturn != NULL)
379                 lpParms->dwRetSize = strlen(lpParms->lpstrReturn);
380         else
381                 lpParms->dwRetSize = 0;
382         return 0;
383 #else
384         return MCIERR_INTERNAL;
385 #endif
386 }
387
388
389 /**************************************************************************
390 *                               CDAUDIO_CalcFrame                       [internal]
391 */
392 static DWORD CDAUDIO_CalcFrame(UINT16 wDevID, DWORD dwFormatType, DWORD dwTime)
393 {
394         DWORD   dwFrame = 0;
395 #if defined(linux) || defined(__FreeBSD__)
396         UINT16  wTrack;
397     
398         dprintf_cdaudio(stddeb,"CDAUDIO_CalcFrame(%04X, %08lX, %lu);\n", 
399                 wDevID, dwFormatType, dwTime);
400     
401         switch (dwFormatType) {
402                 case MCI_FORMAT_MILLISECONDS:
403                         dwFrame = dwTime * CDFRAMES_PERSEC / 1000;
404                         dprintf_cdaudio(stddeb,
405                                 "CDAUDIO_CalcFrame // MILLISECONDS %lu\n", 
406                                 dwFrame);
407                         break;
408                 case MCI_FORMAT_MSF:
409                         dprintf_cdaudio(stddeb,
410                                 "CDAUDIO_CalcFrame // MSF %02u:%02u:%02u\n",
411                                 MCI_MSF_MINUTE(dwTime), MCI_MSF_SECOND(dwTime), 
412                                 MCI_MSF_FRAME(dwTime));
413                         dwFrame += CDFRAMES_PERMIN * MCI_MSF_MINUTE(dwTime);
414                         dwFrame += CDFRAMES_PERSEC * MCI_MSF_SECOND(dwTime);
415                         dwFrame += MCI_MSF_FRAME(dwTime);
416                         break;
417                 default:
418                         /* unknown format ! force TMSF ! ... */
419                         dwFormatType = MCI_FORMAT_TMSF;
420                 case MCI_FORMAT_TMSF:
421                         wTrack = MCI_TMSF_TRACK(dwTime);
422                         dprintf_cdaudio(stddeb,
423                         "CDAUDIO_CalcFrame // TMSF %02u-%02u:%02u:%02u\n",
424                                         MCI_TMSF_TRACK(dwTime), MCI_TMSF_MINUTE(dwTime), 
425                                         MCI_TMSF_SECOND(dwTime), MCI_TMSF_FRAME(dwTime));
426                         dprintf_cdaudio(stddeb,
427                                 "CDAUDIO_CalcFrame // TMSF trackpos[%u]=%lu\n",
428                                 wTrack, CDADev[wDevID].lpdwTrackPos[wTrack - 1]);
429                         dwFrame = CDADev[wDevID].lpdwTrackPos[wTrack - 1];
430                         dwFrame += CDFRAMES_PERMIN * MCI_TMSF_MINUTE(dwTime);
431                         dwFrame += CDFRAMES_PERSEC * MCI_TMSF_SECOND(dwTime);
432                         dwFrame += MCI_TMSF_FRAME(dwTime);
433                         break;
434                 }
435 #endif
436         return dwFrame;
437 }
438
439
440 /**************************************************************************
441 *                               CDAUDIO_GetCDStatus                             [internal]
442 */
443 static BOOL32 CDAUDIO_GetCDStatus(UINT16 wDevID)
444 {
445 #if defined(linux) || defined(__FreeBSD__)
446         int             oldmode = CDADev[wDevID].mode;
447 #ifdef __FreeBSD__
448         struct ioc_read_subchannel      read_sc;
449
450         read_sc.address_format = CD_MSF_FORMAT;
451         read_sc.data_format    = CD_CURRENT_POSITION;
452         read_sc.track          = 0;
453         read_sc.data_len       = sizeof(CDADev[wDevID].sc);
454         read_sc.data           = (struct cd_sub_channel_info *)&CDADev[wDevID].sc;
455 #elif linux
456         CDADev[wDevID].sc.cdsc_format = CDROM_MSF;
457 #endif
458         if (ioctl(CDADev[wDevID].unixdev,
459 #ifdef linux
460                   CDROMSUBCHNL, &CDADev[wDevID].sc
461 #elif __FreeBSD__
462                   CDIOCREADSUBCHANNEL, &read_sc
463 #endif
464                   )) {
465                 dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // opened or no_media !\n");
466                 CDADev[wDevID].mode = MCI_MODE_NOT_READY;
467                 return TRUE;
468                 }
469         switch (
470 #ifdef linux
471                 CDADev[wDevID].sc.cdsc_audiostatus
472 #elif __FreeBSD__
473                 CDADev[wDevID].sc.header.audio_status
474 #endif
475                 ) {
476 #ifdef linux
477                 case CDROM_AUDIO_INVALID:
478 #elif __FreeBSD__
479                 case CD_AS_AUDIO_INVALID:
480 #endif
481                         dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // device doesn't support status, returning NOT_READY.\n");
482 #ifdef linux
483                         CDADev[wDevID].mode = MCI_MODE_NOT_READY;
484 #elif __FreeBSD__
485                         CDADev[wDevID].mode = MCI_MODE_STOP;
486 #endif
487                         break;
488 #ifdef linux
489                 case CDROM_AUDIO_NO_STATUS: 
490 #elif __FreeBSD__
491                 case CD_AS_NO_STATUS:
492 #endif
493                         CDADev[wDevID].mode = MCI_MODE_STOP;
494                         dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // MCI_MODE_STOP !\n");
495                         break;
496 #ifdef linux
497                 case CDROM_AUDIO_PLAY: 
498 #elif __FreeBSD__
499                 case CD_AS_PLAY_IN_PROGRESS:
500 #endif
501                         CDADev[wDevID].mode = MCI_MODE_PLAY;
502                         dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // MCI_MODE_PLAY !\n");
503                         break;
504 #ifdef linux
505                 case CDROM_AUDIO_PAUSED:
506 #elif __FreeBSD__
507                 case CD_AS_PLAY_PAUSED:
508 #endif
509                         CDADev[wDevID].mode = MCI_MODE_PAUSE;
510                         dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // MCI_MODE_PAUSE !\n");
511                         break;
512                 default:
513                         dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // status=%02X !\n",
514 #ifdef linux
515                                         CDADev[wDevID].sc.cdsc_audiostatus
516 #elif __FreeBSD__
517                                         CDADev[wDevID].sc.header.audio_status
518 #endif
519                                         );
520                 }
521 #ifdef linux
522         CDADev[wDevID].nCurTrack = CDADev[wDevID].sc.cdsc_trk;
523         CDADev[wDevID].dwCurFrame = 
524                 CDFRAMES_PERMIN * CDADev[wDevID].sc.cdsc_absaddr.msf.minute +
525                 CDFRAMES_PERSEC * CDADev[wDevID].sc.cdsc_absaddr.msf.second +
526                 CDADev[wDevID].sc.cdsc_absaddr.msf.frame;
527 #elif __FreeBSD__
528         CDADev[wDevID].nCurTrack = CDADev[wDevID].sc.what.position.track_number;
529         CDADev[wDevID].dwCurFrame = 
530                 CDFRAMES_PERMIN * CDADev[wDevID].sc.what.position.absaddr.msf.minute +
531                 CDFRAMES_PERSEC * CDADev[wDevID].sc.what.position.absaddr.msf.second +
532                 CDADev[wDevID].sc.what.position.absaddr.msf.frame;
533 #endif
534         dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // %02u-%02u:%02u:%02u \n",
535 #ifdef linux
536                 CDADev[wDevID].sc.cdsc_trk,
537                 CDADev[wDevID].sc.cdsc_absaddr.msf.minute,
538                 CDADev[wDevID].sc.cdsc_absaddr.msf.second,
539                 CDADev[wDevID].sc.cdsc_absaddr.msf.frame
540 #elif __FreeBSD__
541                 CDADev[wDevID].sc.what.position.track_number,
542                 CDADev[wDevID].sc.what.position.absaddr.msf.minute,
543                 CDADev[wDevID].sc.what.position.absaddr.msf.second,
544                 CDADev[wDevID].sc.what.position.absaddr.msf.frame
545 #endif
546                         );
547         if (oldmode != CDADev[wDevID].mode && oldmode == MCI_MODE_OPEN) {
548                 if (!CDAUDIO_GetTracksInfo(wDevID)) {
549             dprintf_cdaudio(stddeb,"CDAUDIO_GetCDStatus // error updating TracksInfo !\n");
550                         return MCIERR_INTERNAL;
551                         }
552                 }
553         return TRUE;
554 #else
555         return FALSE;
556 #endif
557 }
558
559
560 /**************************************************************************
561 *                               CDAUDIO_CalcTime                        [internal]
562 */
563 static DWORD CDAUDIO_CalcTime(UINT16 wDevID, DWORD dwFormatType, DWORD dwFrame)
564 {
565         DWORD   dwTime = 0;
566 #if defined(linux) || defined(__FreeBSD__)
567         UINT16  wTrack;
568         UINT16  wMinutes;
569         UINT16  wSeconds;
570         UINT16  wFrames;
571
572         dprintf_cdaudio(stddeb,"CDAUDIO_CalcTime(%04X, %08lX, %lu);\n", 
573                 wDevID, dwFormatType, dwFrame);
574
575         switch (dwFormatType) {
576                 case MCI_FORMAT_MILLISECONDS:
577                         dwTime = dwFrame / CDFRAMES_PERSEC * 1000;
578                         dprintf_cdaudio(stddeb,
579                                 "CDAUDIO_CalcTime // MILLISECONDS %lu\n", 
580                                 dwTime);
581                         break;
582                 case MCI_FORMAT_MSF:
583                         wMinutes = dwFrame / CDFRAMES_PERMIN;
584                         wSeconds = (dwFrame - CDFRAMES_PERMIN * wMinutes) / CDFRAMES_PERSEC;
585                         wFrames = dwFrame - CDFRAMES_PERMIN * wMinutes - 
586                                                                 CDFRAMES_PERSEC * wSeconds;
587                         dwTime = MCI_MAKE_MSF(wMinutes, wSeconds, wFrames);
588                         dprintf_cdaudio(stddeb,"CDAUDIO_CalcTime // MSF %02u:%02u:%02u -> dwTime=%lu\n",
589                                                                 wMinutes, wSeconds, wFrames, dwTime);
590                         break;
591                 default:
592                         /* unknown format ! force TMSF ! ... */
593                         dwFormatType = MCI_FORMAT_TMSF;
594                 case MCI_FORMAT_TMSF:
595                         for (wTrack = 0; wTrack < CDADev[wDevID].nTracks; wTrack++) {
596 /*                              dwTime += CDADev[wDevID].lpdwTrackLen[wTrack - 1];
597                                 printf("Adding trk#%u curpos=%u \n", dwTime);
598                                 if (dwTime >= dwFrame) break; */
599                                 if (CDADev[wDevID].lpdwTrackPos[wTrack - 1] >= dwFrame) break;
600                                 }
601                         wMinutes = dwFrame / CDFRAMES_PERMIN;
602                         wSeconds = (dwFrame - CDFRAMES_PERMIN * wMinutes) / CDFRAMES_PERSEC;
603                         wFrames = dwFrame - CDFRAMES_PERMIN * wMinutes - 
604                                                                 CDFRAMES_PERSEC * wSeconds;
605                         dwTime = MCI_MAKE_TMSF(wTrack, wMinutes, wSeconds, wFrames);
606                         dprintf_cdaudio(stddeb,
607                                 "CDAUDIO_CalcTime // %02u-%02u:%02u:%02u\n",
608                                         wTrack, wMinutes, wSeconds, wFrames);
609                         break;
610                 }
611 #endif
612         return dwTime;
613 }
614
615
616 /**************************************************************************
617 *                               CDAUDIO_mciStatus               [internal]
618 */
619 static DWORD CDAUDIO_mciStatus(UINT16 wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
620 {
621 #if defined(linux) || defined(__FreeBSD__)
622         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus(%04X, %08lX, %p);\n", 
623                 wDevID, dwFlags, lpParms);
624         if (lpParms == NULL) return MCIERR_INTERNAL;
625         if (CDADev[wDevID].unixdev == 0) return MMSYSERR_NOTENABLED;
626         if (dwFlags & MCI_NOTIFY) {
627                 dprintf_cdaudio(stddeb,
628                         "CDAUDIO_mciStatus // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
629                         lpParms->dwCallback);
630                 mciDriverNotify((HWND16)LOWORD(lpParms->dwCallback), 
631                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
632                 }
633         if (dwFlags & MCI_STATUS_ITEM) {
634                 switch(lpParms->dwItem) {
635                         case MCI_STATUS_CURRENT_TRACK:
636                                 if (!CDAUDIO_GetCDStatus(wDevID)) return MCIERR_INTERNAL;
637                                 lpParms->dwReturn = CDADev[wDevID].nCurTrack;
638                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // CURRENT_TRACK=%lu!\n", lpParms->dwReturn);
639                                 return 0;
640                         case MCI_STATUS_LENGTH:
641                                 if (CDADev[wDevID].nTracks == 0) {
642                                         if (!CDAUDIO_GetTracksInfo(wDevID)) {
643                                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // error reading TracksInfo !\n");
644                                                 return MCIERR_INTERNAL;
645                                                 }
646                                         }
647                                 if (dwFlags & MCI_TRACK) {
648                                         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MCI_TRACK #%lu LENGTH=??? !\n",
649                                                         lpParms->dwTrack);
650                                         if (lpParms->dwTrack > CDADev[wDevID].nTracks)
651                                                 return MCIERR_OUTOFRANGE;
652                                         lpParms->dwReturn = CDADev[wDevID].lpdwTrackLen[lpParms->dwTrack];
653                                         }
654                                 else
655                                         lpParms->dwReturn = CDADev[wDevID].dwTotalLen;
656                                 lpParms->dwReturn = CDAUDIO_CalcTime(wDevID, 
657                                         CDADev[wDevID].dwTimeFormat, lpParms->dwReturn);
658                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // LENGTH=%lu !\n", lpParms->dwReturn);
659                                 return 0;
660                         case MCI_STATUS_MODE:
661                                 if (!CDAUDIO_GetCDStatus(wDevID)) return MCIERR_INTERNAL;
662                                 lpParms->dwReturn = CDADev[wDevID].mode;
663                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MCI_STATUS_MODE=%08lX !\n",
664                                                                                                 lpParms->dwReturn);
665                                 return 0;
666                         case MCI_STATUS_MEDIA_PRESENT:
667                                 lpParms->dwReturn = (CDADev[wDevID].nTracks > 0) ? TRUE : FALSE;
668                                 if (lpParms->dwReturn == FALSE)
669                                         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MEDIA_NOT_PRESENT !\n");
670                                 else
671                                         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MCI_STATUS_MEDIA_PRESENT !\n");
672                                 return 0;
673                         case MCI_STATUS_NUMBER_OF_TRACKS:
674                                 lpParms->dwReturn = CDAUDIO_GetNumberOfTracks(wDevID);
675                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MCI_STATUS_NUMBER_OF_TRACKS = %lu !\n",
676                                                                                                         lpParms->dwReturn);
677                                 if (lpParms->dwReturn == (WORD)-1) return MCIERR_INTERNAL;
678                                 return 0;
679                         case MCI_STATUS_POSITION:
680                                 if (!CDAUDIO_GetCDStatus(wDevID)) return MCIERR_INTERNAL;
681                                 lpParms->dwReturn = CDADev[wDevID].dwCurFrame;
682                                 if (dwFlags & MCI_STATUS_START) {
683                                         lpParms->dwReturn = CDADev[wDevID].dwFirstOffset;
684                                         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // get MCI_STATUS_START !\n");
685                                         }
686                                 if (dwFlags & MCI_TRACK) {
687                                         if (lpParms->dwTrack > CDADev[wDevID].nTracks)
688                                                 return MCIERR_OUTOFRANGE;
689                                         lpParms->dwReturn = CDADev[wDevID].lpdwTrackPos[lpParms->dwTrack - 1];
690                                         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // get MCI_TRACK #%lu !\n", lpParms->dwTrack);
691                                         }
692                                 lpParms->dwReturn = CDAUDIO_CalcTime(wDevID,
693                                         CDADev[wDevID].dwTimeFormat, lpParms->dwReturn);
694                                         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MCI_STATUS_POSITION=%08lX !\n",
695                                                                                                                 lpParms->dwReturn);
696                                 return 0;
697                         case MCI_STATUS_READY:
698                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MCI_STATUS_READY !\n");
699                                 lpParms->dwReturn = TRUE;
700                                 return 0;
701                         case MCI_STATUS_TIME_FORMAT:
702                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // MCI_STATUS_TIME_FORMAT !\n");
703                                 lpParms->dwReturn = CDADev[wDevID].dwTimeFormat;
704                                 return 0;
705                         default:
706                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // unknown command %08lX !\n", lpParms->dwItem);
707                                 return MCIERR_UNRECOGNIZED_COMMAND;
708                         }
709                 }
710         dprintf_cdaudio(stddeb,"CDAUDIO_mciStatus // not MCI_STATUS_ITEM !\n");
711         return 0;
712 #else
713         return MMSYSERR_NOTENABLED;
714 #endif
715 }
716
717
718 /**************************************************************************
719 *                               CDAUDIO_mciPlay                 [internal]
720 */
721 static DWORD CDAUDIO_mciPlay(UINT16 wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
722 {
723 #if defined(linux) || defined(__FreeBSD__)
724         int     start, end;
725 #ifdef linux
726         struct  cdrom_msf       msf;
727 #elif __FreeBSD__
728         struct  ioc_play_msf    msf;
729 #endif
730
731         dprintf_cdaudio(stddeb,"CDAUDIO_mciPlay(%04X, %08lX, %p);\n", 
732                 wDevID, dwFlags, lpParms);
733         if (lpParms == NULL) return MCIERR_INTERNAL;
734         if (CDADev[wDevID].unixdev == 0) return MMSYSERR_NOTENABLED;
735         start = 0;              end = CDADev[wDevID].dwTotalLen;
736         CDADev[wDevID].nCurTrack = 1;
737         if (dwFlags & MCI_FROM) {
738                 start = CDAUDIO_CalcFrame(wDevID, 
739                         CDADev[wDevID].dwTimeFormat, lpParms->dwFrom); 
740         dprintf_cdaudio(stddeb,"CDAUDIO_mciPlay // MCI_FROM=%08lX -> %u \n",
741                                 lpParms->dwFrom, start);
742                 }
743         if (dwFlags & MCI_TO) {
744                 end = CDAUDIO_CalcFrame(wDevID, 
745                         CDADev[wDevID].dwTimeFormat, lpParms->dwTo);
746                 dprintf_cdaudio(stddeb,
747                         "CDAUDIO_mciPlay // MCI_TO=%08lX -> %u \n",
748                         lpParms->dwTo, end);
749                 }
750         start += CDADev[wDevID].dwFirstOffset;  
751         end += CDADev[wDevID].dwFirstOffset;
752 #ifdef linux
753         msf.cdmsf_min0 = start / CDFRAMES_PERMIN;
754         msf.cdmsf_sec0 = (start % CDFRAMES_PERMIN) / CDFRAMES_PERSEC;
755         msf.cdmsf_frame0 = start % CDFRAMES_PERSEC;
756         msf.cdmsf_min1 = end / CDFRAMES_PERMIN;
757         msf.cdmsf_sec1 = (end % CDFRAMES_PERMIN) / CDFRAMES_PERSEC;
758         msf.cdmsf_frame1 = end % CDFRAMES_PERSEC;
759 #elif __FreeBSD__
760         msf.start_m     = start / CDFRAMES_PERMIN;
761         msf.start_s     = (start % CDFRAMES_PERMIN) / CDFRAMES_PERSEC;
762         msf.start_f     = start % CDFRAMES_PERSEC;
763         msf.end_m       = end / CDFRAMES_PERMIN;
764         msf.end_s       = (end % CDFRAMES_PERMIN) / CDFRAMES_PERSEC;
765         msf.end_f       = end % CDFRAMES_PERSEC;
766 #endif
767         if (ioctl(CDADev[wDevID].unixdev,
768 #ifdef linux
769                   CDROMSTART
770 #elif __FreeBSD__
771                   CDIOCSTART
772 #endif
773                   )) {
774                 dprintf_cdaudio(stddeb,"CDAUDIO_mciPlay // motor doesn't start !\n");
775                 return MCIERR_HARDWARE;
776                 }
777         if (ioctl(CDADev[wDevID].unixdev, 
778 #ifdef linux
779                   CDROMPLAYMSF
780 #elif __FreeBSD__
781                   CDIOCPLAYMSF
782 #endif
783                   , &msf)) {
784                 dprintf_cdaudio(stddeb,"CDAUDIO_mciPlay // device doesn't play !\n");
785                 return MCIERR_HARDWARE;
786                 }
787         dprintf_cdaudio(stddeb,"CDAUDIO_mciPlay // msf = %d:%d:%d %d:%d:%d\n",
788 #ifdef linux
789                 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
790                 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1
791 #elif __FreeBSD__
792                 msf.start_m, msf.start_s, msf.start_f,
793                 msf.end_m,   msf.end_s,   msf.end_f
794 #endif
795                         );
796         CDADev[wDevID].mode = MCI_MODE_PLAY;
797         if (dwFlags & MCI_NOTIFY) {
798                 dprintf_cdaudio(stddeb,
799                         "CDAUDIO_mciPlay // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
800                         lpParms->dwCallback);
801 /*
802                 mciDriverNotify((HWND16)LOWORD(lpParms->dwCallback), 
803                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
804 */
805                 }
806         return 0;
807 #else
808         return MCIERR_HARDWARE;
809 #endif
810 }
811
812 /**************************************************************************
813 *                               CDAUDIO_mciStop                 [internal]
814 */
815 static DWORD CDAUDIO_mciStop(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
816 {
817 #if defined(linux) || defined(__FreeBSD__)
818         dprintf_cdaudio(stddeb,"CDAUDIO_mciStop(%04X, %08lX, %p);\n", 
819                 wDevID, dwFlags, lpParms);
820         if (lpParms == NULL) return MCIERR_INTERNAL;
821         if (ioctl(CDADev[wDevID].unixdev,
822 #ifdef linux
823                   CDROMSTOP
824 #elif __FreeBSD__
825                   CDIOCSTOP
826 #endif
827                   )) return MCIERR_HARDWARE;
828         CDADev[wDevID].mode = MCI_MODE_STOP;
829         if (dwFlags & MCI_NOTIFY) {
830                 dprintf_cdaudio(stddeb,
831                         "CDAUDIO_mciStop // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
832                         lpParms->dwCallback);
833                 mciDriverNotify((HWND16)LOWORD(lpParms->dwCallback), 
834                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
835                 }
836         return 0;
837 #else
838         return MCIERR_HARDWARE;
839 #endif
840 }
841
842 /**************************************************************************
843 *                               CDAUDIO_mciPause                [internal]
844 */
845 static DWORD CDAUDIO_mciPause(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
846 {
847 #if defined(linux) || defined(__FreeBSD__)
848         dprintf_cdaudio(stddeb,"CDAUDIO_mciPause(%04X, %08lX, %p);\n", 
849                 wDevID, dwFlags, lpParms);
850         if (lpParms == NULL) return MCIERR_INTERNAL;
851         if (ioctl(CDADev[wDevID].unixdev,
852 #ifdef linux
853                   CDROMPAUSE
854 #elif __FreeBSD__
855                   CDIOCPAUSE
856 #endif
857                   )) return MCIERR_HARDWARE;
858         CDADev[wDevID].mode = MCI_MODE_PAUSE;
859         if (dwFlags & MCI_NOTIFY) {
860         dprintf_cdaudio(stddeb,
861                 "CDAUDIO_mciPause // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
862                 lpParms->dwCallback);
863                 mciDriverNotify((HWND16)LOWORD(lpParms->dwCallback), 
864                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
865                 }
866         return 0;
867 #else
868         return MCIERR_HARDWARE;
869 #endif
870 }
871
872 /**************************************************************************
873 *                               CDAUDIO_mciResume               [internal]
874 */
875 static DWORD CDAUDIO_mciResume(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
876 {
877 #if defined(linux) || defined(__FreeBSD__)
878         dprintf_cdaudio(stddeb,"CDAUDIO_mciResume(%04X, %08lX, %p);\n", 
879                 wDevID, dwFlags, lpParms);
880         if (lpParms == NULL) return MCIERR_INTERNAL;
881         if (ioctl(CDADev[wDevID].unixdev, 
882 #ifdef linux
883                   CDROMRESUME
884 #elif __FreeBSD__
885                   CDIOCRESUME
886 #endif
887                   )) return MCIERR_HARDWARE;
888         CDADev[wDevID].mode = MCI_MODE_STOP;
889         if (dwFlags & MCI_NOTIFY) {
890                 dprintf_cdaudio(stddeb,
891                         "CDAUDIO_mciResume // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
892                         lpParms->dwCallback);
893                 mciDriverNotify((HWND16)LOWORD(lpParms->dwCallback), 
894                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
895                 }
896         return 0;
897 #else
898         return MCIERR_HARDWARE;
899 #endif
900 }
901
902 /**************************************************************************
903 *                               CDAUDIO_mciSeek                 [internal]
904 */
905 static DWORD CDAUDIO_mciSeek(UINT16 wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
906 {
907 #if defined(linux) || defined(__FreeBSD__)
908         DWORD   dwRet;
909         MCI_PLAY_PARMS PlayParms;
910         dprintf_cdaudio(stddeb,"CDAUDIO_mciSeek(%04X, %08lX, %p);\n", 
911                 wDevID, dwFlags, lpParms);
912         if (lpParms == NULL) return MCIERR_INTERNAL;
913         if (ioctl(CDADev[wDevID].unixdev,
914 #ifdef linux
915                   CDROMRESUME
916 #elif __FreeBSD__
917                   CDIOCRESUME
918 #endif
919                   )) {
920                 perror("ioctl CDROMRESUME");
921                 return MCIERR_HARDWARE;
922         }
923         CDADev[wDevID].mode = MCI_MODE_SEEK;
924         switch(dwFlags) {
925                 case MCI_SEEK_TO_START:
926                         PlayParms.dwFrom = 0;
927                         break;
928                 case MCI_SEEK_TO_END:
929                         PlayParms.dwFrom = CDADev[wDevID].dwTotalLen;
930                         break;
931                 case MCI_TO:
932                         PlayParms.dwFrom = lpParms->dwTo;
933                         break;
934                 }
935         dwRet = CDAUDIO_mciPlay(wDevID, MCI_WAIT | MCI_FROM, &PlayParms);
936         if (dwRet != 0) return dwRet;
937         dwRet = CDAUDIO_mciStop(wDevID, MCI_WAIT, (LPMCI_GENERIC_PARMS)&PlayParms);
938         if (dwFlags & MCI_NOTIFY) {
939                 dprintf_cdaudio(stddeb,
940                         "CDAUDIO_mciSeek // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
941                         lpParms->dwCallback);
942                 mciDriverNotify((HWND16)LOWORD(lpParms->dwCallback), 
943                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
944                 }
945         return dwRet;
946 #else
947         return MCIERR_HARDWARE;
948 #endif
949 }
950
951
952 /**************************************************************************
953 *                               CDAUDIO_mciSet                  [internal]
954 */
955 static DWORD CDAUDIO_mciSet(UINT16 wDevID, DWORD dwFlags, LPMCI_SET_PARMS lpParms)
956 {
957 #if defined(linux) || defined(__FreeBSD__)
958         dprintf_cdaudio(stddeb,"CDAUDIO_mciSet(%04X, %08lX, %p);\n", 
959                 wDevID, dwFlags, lpParms);
960         if (lpParms == NULL) return MCIERR_INTERNAL;
961 /*
962         printf("CDAUDIO_mciSet // dwTimeFormat=%08lX\n", lpParms->dwTimeFormat);
963         printf("CDAUDIO_mciSet // dwAudio=%08lX\n", lpParms->dwAudio);
964 */
965         if (dwFlags & MCI_SET_TIME_FORMAT) {
966                 switch (lpParms->dwTimeFormat) {
967                         case MCI_FORMAT_MILLISECONDS:
968                                 dprintf_cdaudio(stddeb,
969                                 "CDAUDIO_mciSet // MCI_FORMAT_MILLISECONDS !\n");
970                                 break;
971                         case MCI_FORMAT_MSF:
972                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciSet // MCI_FORMAT_MSF !\n");
973                                 break;
974                         case MCI_FORMAT_TMSF:
975                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciSet // MCI_FORMAT_TMSF !\n");
976                                 break;
977                         default:
978                                 dprintf_cdaudio(stddeb,"CDAUDIO_mciSet // bad time format !\n");
979                                 return MCIERR_BAD_TIME_FORMAT;
980                         }
981                 CDADev[wDevID].dwTimeFormat = lpParms->dwTimeFormat;
982                 }
983         if (dwFlags & MCI_SET_DOOR_OPEN) {
984                 dprintf_cdaudio(stddeb,
985                         "CDAUDIO_mciSet // MCI_SET_DOOR_OPEN !\n");
986 #ifdef __FreeBSD__
987                 if (ioctl(CDADev[wDevID].unixdev, CDIOCALLOW)) return MCIERR_HARDWARE;
988                 if (ioctl(CDADev[wDevID].unixdev, CDIOCEJECT)) return MCIERR_HARDWARE;
989                 if (ioctl(CDADev[wDevID].unixdev, CDIOCPREVENT)) return MCIERR_HARDWARE;
990 #elif linux
991                 if (ioctl(CDADev[wDevID].unixdev, CDROMEJECT)) return MCIERR_HARDWARE;
992 #endif
993                 CDADev[wDevID].nTracks = 0;
994                 }
995         if (dwFlags & MCI_SET_DOOR_CLOSED) {
996                 dprintf_cdaudio(stddeb,
997                         "CDAUDIO_mciSet // MCI_SET_DOOR_CLOSED !\n");
998 #ifdef __FreeBSD__
999                 if (ioctl(CDADev[wDevID].unixdev, CDIOCALLOW)) return MCIERR_HARDWARE;
1000                 if (ioctl(CDADev[wDevID].unixdev, CDIOCCLOSE)) return MCIERR_HARDWARE;
1001                 if (ioctl(CDADev[wDevID].unixdev, CDIOCPREVENT)) return MCIERR_HARDWARE;
1002 #elif linux
1003                 if (ioctl(CDADev[wDevID].unixdev, CDROMEJECT)) return MCIERR_HARDWARE;
1004                           /* XXX should it be ",1" ??? */
1005 #endif
1006                 CDADev[wDevID].nTracks = 0;
1007                 }
1008         if (dwFlags & MCI_SET_VIDEO) return MCIERR_UNSUPPORTED_FUNCTION;
1009         if (dwFlags & MCI_SET_ON) return MCIERR_UNSUPPORTED_FUNCTION;
1010         if (dwFlags & MCI_SET_OFF) return MCIERR_UNSUPPORTED_FUNCTION;
1011         if (dwFlags & MCI_NOTIFY) {
1012                 dprintf_cdaudio(stddeb,
1013                         "CDAUDIO_mciSet // MCI_NOTIFY_SUCCESSFUL %08lX !\n", 
1014                         lpParms->dwCallback);
1015                 mciDriverNotify((HWND16)LOWORD(lpParms->dwCallback), 
1016                         CDADev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1017                 }
1018         return 0;
1019 #else
1020         return MCIERR_HARDWARE;
1021 #endif
1022 }
1023
1024
1025 /**************************************************************************
1026 *                               CDAUDIO_DriverProc              [sample driver]
1027 */
1028 LONG CDAUDIO_DriverProc(DWORD dwDevID, HDRVR16 hDriv, WORD wMsg, 
1029                                                         DWORD dwParam1, DWORD dwParam2)
1030 {
1031 #if defined(linux) || defined(__FreeBSD__)
1032         switch(wMsg) {
1033                 case DRV_LOAD:
1034                         return 1;
1035                 case DRV_FREE:
1036                         return 1;
1037                 case DRV_OPEN:
1038                 case MCI_OPEN_DRIVER:
1039                 case MCI_OPEN:
1040                         return CDAUDIO_mciOpen(dwDevID, dwParam1, (LPMCI_OPEN_PARMS16)PTR_SEG_TO_LIN(dwParam2)); 
1041                 case DRV_CLOSE:
1042                 case MCI_CLOSE_DRIVER:
1043                 case MCI_CLOSE:
1044                         return CDAUDIO_mciClose(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)PTR_SEG_TO_LIN(dwParam2));
1045                 case DRV_ENABLE:
1046                         return 1;
1047                 case DRV_DISABLE:
1048                         return 1;
1049                 case DRV_QUERYCONFIGURE:
1050                         return 1;
1051                 case DRV_CONFIGURE:
1052                         MessageBox16(0, "Sample MultiMedia Linux Driver !", 
1053                                                                 "MMLinux Driver", MB_OK);
1054                         return 1;
1055                 case DRV_INSTALL:
1056                         return DRVCNF_RESTART;
1057                 case DRV_REMOVE:
1058                         return DRVCNF_RESTART;
1059                 case MCI_GETDEVCAPS:
1060                         return CDAUDIO_mciGetDevCaps(dwDevID, dwParam1, 
1061                                 (LPMCI_GETDEVCAPS_PARMS)PTR_SEG_TO_LIN(dwParam2));
1062                 case MCI_INFO:
1063                         return CDAUDIO_mciInfo(dwDevID, dwParam1, 
1064                                 (LPMCI_INFO_PARMS16)PTR_SEG_TO_LIN(dwParam2));
1065                 case MCI_STATUS:
1066                         return CDAUDIO_mciStatus(dwDevID, dwParam1, 
1067                                 (LPMCI_STATUS_PARMS)PTR_SEG_TO_LIN(dwParam2));
1068                 case MCI_SET:
1069                         return CDAUDIO_mciSet(dwDevID, dwParam1, 
1070                                 (LPMCI_SET_PARMS)PTR_SEG_TO_LIN(dwParam2));
1071                 case MCI_PLAY:
1072                         return CDAUDIO_mciPlay(dwDevID, dwParam1, 
1073                                 (LPMCI_PLAY_PARMS)PTR_SEG_TO_LIN(dwParam2));
1074                 case MCI_STOP:
1075                         return CDAUDIO_mciStop(dwDevID, dwParam1, 
1076                                 (LPMCI_GENERIC_PARMS)PTR_SEG_TO_LIN(dwParam2));
1077                 case MCI_PAUSE:
1078                         return CDAUDIO_mciPause(dwDevID, dwParam1, 
1079                                 (LPMCI_GENERIC_PARMS)PTR_SEG_TO_LIN(dwParam2));
1080                 case MCI_RESUME:
1081                         return CDAUDIO_mciResume(dwDevID, dwParam1, 
1082                                 (LPMCI_GENERIC_PARMS)PTR_SEG_TO_LIN(dwParam2));
1083                 case MCI_SEEK:
1084                         return CDAUDIO_mciSeek(dwDevID, dwParam1, 
1085                                 (LPMCI_SEEK_PARMS)PTR_SEG_TO_LIN(dwParam2));
1086                 case MCI_SET_DOOR_OPEN:
1087                         dprintf_cdaudio(stddeb,
1088                                 "CDAUDIO_DriverProc // MCI_SET_DOOR_OPEN !\n");
1089 #ifdef __FreeBSD__
1090                         if (ioctl(CDADev[dwDevID].unixdev, CDIOCALLOW)) return MCIERR_HARDWARE;
1091                         if (ioctl(CDADev[dwDevID].unixdev, CDIOCEJECT)) return MCIERR_HARDWARE;
1092                         if (ioctl(CDADev[dwDevID].unixdev, CDIOCPREVENT)) return MCIERR_HARDWARE;
1093 #elif linux
1094                         if (ioctl(CDADev[dwDevID].unixdev, CDROMEJECT)) return MCIERR_HARDWARE;
1095 #endif
1096                         CDADev[dwDevID].nTracks = 0;
1097                         return 0;
1098                 case MCI_SET_DOOR_CLOSED:
1099                         dprintf_cdaudio(stddeb,"CDAUDIO_DriverProc // MCI_SET_DOOR_CLOSED !\n");
1100 #ifdef __FreeBSD__
1101                         if (ioctl(CDADev[dwDevID].unixdev, CDIOCALLOW)) return MCIERR_HARDWARE;
1102                         if (ioctl(CDADev[dwDevID].unixdev, CDIOCCLOSE)) return MCIERR_HARDWARE;
1103                         if (ioctl(CDADev[dwDevID].unixdev, CDIOCPREVENT)) return MCIERR_HARDWARE;
1104 #elif linux
1105                         if (ioctl(CDADev[dwDevID].unixdev, CDROMEJECT, 1)) return MCIERR_HARDWARE;
1106 #endif
1107                         CDADev[dwDevID].nTracks = 0;
1108                         return 0;
1109                 default:
1110                         return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1111                 }
1112 #else
1113         return MCIERR_HARDWARE;
1114 #endif
1115 }
1116
1117
1118 /*-----------------------------------------------------------------------*/