DirectSoundEnumerateA() now passes a GUID to the callback if the first
[wine] / dlls / ntdll / cdrom.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /* Main file for CD-ROM support
3  *
4  * Copyright 1994 Martin Ayotte
5  * Copyright 1999, 2001 Eric Pouech
6  * Copyright 2000 Andreas Mohr
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24
25 #include <errno.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/ioctl.h>
30 #include "ntddk.h"
31 #include "winioctl.h"
32 #include "ntddstor.h"
33 #include "ntddcdrm.h"
34 #include "drive.h"
35 #include "file.h"
36 #include "wine/debug.h"
37
38 #ifdef HAVE_LINUX_CDROM_H
39 # include <linux/cdrom.h>
40 #endif
41 #ifdef HAVE_LINUX_UCDROM_H
42 # include <linux/ucdrom.h>
43 #endif
44 #ifdef HAVE_SYS_CDIO_H
45 # include <sys/cdio.h>
46 #endif
47
48 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
49
50 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom 
51  * this should be removed when a proper device interface is implemented
52  */
53 struct cdrom_cache {
54     int fd;
55     int count;
56 };
57 static struct cdrom_cache cdrom_cache[26];
58
59 /******************************************************************
60  *              CDROM_Open
61  *
62  *
63  */
64 static int CDROM_Open(HANDLE hDevice, DWORD clientID)
65 {
66     int dev = LOWORD(clientID);
67
68     if (dev >= 26) return -1;
69
70     if (!cdrom_cache[dev].count)
71     {
72         char root[4];
73
74         strcpy(root, "A:\\");
75         root[0] += dev;
76         if (GetDriveTypeA(root) != DRIVE_CDROM) return -1;
77         cdrom_cache[dev].fd = open(DRIVE_GetDevice(dev), O_RDONLY|O_NONBLOCK);
78         if (cdrom_cache[dev].fd == -1)
79         {
80             FIXME("Can't open %s: %s\n", root, strerror(errno));
81             return -1;
82         }
83     }
84     cdrom_cache[dev].count++;
85     return cdrom_cache[dev].fd;
86 }
87
88 /******************************************************************
89  *              CDROM_Close
90  *
91  *
92  */
93 static void CDROM_Close(DWORD clientID, int fd)
94 {
95     int dev = LOWORD(clientID);
96     
97     if (dev >= 26 || fd != cdrom_cache[dev].fd) FIXME("how come\n");
98     if (--cdrom_cache[dev].count == 0)
99         close(cdrom_cache[dev].fd);
100 }
101
102 /******************************************************************
103  *              CDROM_GetStatusCode
104  *
105  *
106  */
107 static DWORD CDROM_GetStatusCode(int io)
108 {
109     if (io == 0) return 0;
110     switch (errno)
111     {
112     case EIO:
113 #ifdef ENOMEDIUM
114     case ENOMEDIUM:
115 #endif
116             return STATUS_NO_MEDIA_IN_DEVICE;
117     }
118     FIXME("Unmapped error code %d: %s\n", errno, strerror(errno));
119     return STATUS_IO_DEVICE_ERROR;
120 }
121
122 static DWORD CDROM_GetControl(int dev, CDROM_AUDIO_CONTROL* cac)
123 {
124     cac->LbaFormat = 0; /* FIXME */
125     cac->LogicalBlocksPerSecond = 1; /* FIXME */
126     return  STATUS_NOT_SUPPORTED;
127 }
128
129 static DWORD CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
130 {
131     return STATUS_NOT_SUPPORTED;
132 }
133
134 static DWORD CDROM_GetDriveGeometry(int dev, DISK_GEOMETRY* dg)
135 {
136 #if 0
137     dg->Cylinders.s.LowPart = 1; /* FIXME */
138     dg->Cylinders.s.HighPart = 0; /* FIXME */
139     dg->MediaType = 1; /* FIXME */
140     dg->TracksPerCylinder = 1; /* FIXME */
141     dg->SectorsPerTrack = 1; /* FIXME */
142     dg->BytesPerSector= 1; /* FIXME */
143 #endif
144     return STATUS_NOT_SUPPORTED;
145 }
146
147 /**************************************************************************
148  *                              CDROM_Reset                     [internal]
149  */
150 static DWORD CDROM_ResetAudio(int dev)
151 {
152 #if defined(linux)
153     return CDROM_GetStatusCode(ioctl(dev, CDROMRESET));
154 #elif defined(__FreeBSD__) || defined(__NetBSD__)
155     return CDROM_GetStatusCode(ioctl(dev, CDIOCRESET, NULL));
156 #else
157     return STATUS_NOT_SUPPORTED;
158 #endif
159 }  
160
161 /******************************************************************
162  *              CDROM_SetTray
163  *
164  *
165  */
166 static DWORD CDROM_SetTray(int dev, BOOL doEject)
167 {
168 #if defined(linux)
169     return CDROM_GetStatusCode(ioctl(dev, doEject ? CDROMEJECT : CDROMCLOSETRAY));
170 #elif defined(__FreeBSD__) || defined(__NetBSD__)
171     return CDROM_GetStatusCode((ioctl(dev, CDIOCALLOW, NULL)) || 
172                                (ioctl(dev, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
173                                (ioctl(dev, CDIOCPREVENT, NULL)));
174 #else
175     return STATUS_NOT_SUPPORTED;
176 #endif
177 }
178
179 /******************************************************************
180  *              CDROM_ControlEjection
181  *
182  *
183  */
184 static DWORD CDROM_ControlEjection(int dev, const PREVENT_MEDIA_REMOVAL* rmv)
185 {
186 #if defined(linux)
187     return CDROM_GetStatusCode(ioctl(dev, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
188 #elif defined(__FreeBSD__) || defined(__NetBSD__)
189     return CDROM_GetStatusCode(ioctl(dev, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
190 #else
191     return STATUS_NOT_SUPPORTED;
192 #endif
193 }
194
195 /******************************************************************
196  *              CDROM_ReadTOC
197  *
198  *
199  */
200 static DWORD CDROM_ReadTOC(int dev, CDROM_TOC* toc)
201 {
202     DWORD       ret = STATUS_NOT_SUPPORTED;
203
204 #if defined(linux)
205     int                         i, io = -1;
206     struct cdrom_tochdr         hdr;
207     struct cdrom_tocentry       entry;
208
209     io = ioctl(dev, CDROMREADTOCHDR, &hdr);
210     if (io == -1) 
211     {
212         WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
213         goto end;
214     }
215     toc->FirstTrack = hdr.cdth_trk0;
216     toc->LastTrack  = hdr.cdth_trk1;
217
218     TRACE("from=%d to=%d\n", toc->FirstTrack, toc->LastTrack);
219
220     for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
221     {
222         if (i == toc->LastTrack + 1)
223         {
224             entry.cdte_track = CDROM_LEADOUT;
225         } else {
226             entry.cdte_track = i;
227         }
228         entry.cdte_format = CDROM_MSF;
229         io = ioctl(dev, CDROMREADTOCENTRY, &entry);
230         if (io == -1) {
231             WARN("error read entry (%s)\n", strerror(errno));
232             goto end;
233         }
234         toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
235         toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
236         /* marking last track with leadout value as index */
237         toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
238         toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
239         toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
240         toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
241         toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
242     }
243 end:
244     ret = CDROM_GetStatusCode(io);
245 #elif defined(__FreeBSD__) || defined(__NetBSD__)
246     int                         i, io = -1;
247     struct ioc_toc_header       hdr;
248     struct ioc_read_toc_entry   entry;
249     struct cd_toc_entry         toc_buffer;
250
251     io = ioctl(dev, CDIOREADTOCHEADER, &hdr);
252     if (io == -1) 
253     {
254         WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
255         goto end;
256     }
257     toc->FirstTrack = hdr.starting_track;
258     toc->LastTrack  = hdr.ending_track;
259
260     TRACE("from=%d to=%d\n", toc->FirstTrack, toc->LastTrack);
261
262     for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
263     {
264         if (i == toc->LastTrack + 1)
265         {
266 #define LEADOUT 0xaa
267             entry.starting_track = LEADOUT;
268         } else {
269             entry.starting_track = i;
270         }
271         memset((char *)&toc_buffer, 0, sizeof(toc_buffer));
272         entry.address_format = CD_MSF_FORMAT;
273         entry.data_len = sizeof(toc_buffer);
274         entry.data = &toc_buffer;
275         io = ioctl(dev, CDIOREADTOCENTRYS, &entry);
276         if (io == -1) {
277             WARN("error read entry (%s)\n", strerror(errno));
278             goto end;
279         }
280         toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
281         toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
282         /* marking last track with leadout value as index */
283         toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
284         toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
285         toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
286         toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
287         toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
288     }
289 end:
290     ret = CDROM_GetStatusCode(io);
291 #else
292     ret = STATUS_NOT_SUPPORTED;
293 #endif
294     return ret;
295 }
296
297 /******************************************************************
298  *              CDROM_GetDiskData
299  *
300  *
301  */
302 static DWORD CDROM_GetDiskData(int dev, CDROM_DISK_DATA* data)
303 {
304     CDROM_TOC   toc;
305     DWORD       ret;
306     int         i;
307
308     if ((ret = CDROM_ReadTOC(dev, &toc)) != 0) return ret;
309     data->DiskData = 0;
310     for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
311         if (toc.TrackData[i].Control & 0x04)
312             data->DiskData |= CDROM_DISK_DATA_TRACK;
313         else
314             data->DiskData |= CDROM_DISK_AUDIO_TRACK;
315     }
316     return 0;
317 }
318
319 /******************************************************************
320  *              CDROM_ReadQChannel
321  *
322  *
323  */
324 static DWORD CDROM_ReadQChannel(int dev, const CDROM_SUB_Q_DATA_FORMAT* fmt, 
325                                 SUB_Q_CHANNEL_DATA* data)
326 {
327     DWORD               ret = STATUS_NOT_SUPPORTED;
328 #ifdef linux
329     unsigned            size;
330     SUB_Q_HEADER*       hdr = (SUB_Q_HEADER*)data;
331     int                 io;
332     struct cdrom_subchnl        sc;
333     sc.cdsc_format = CDROM_MSF;
334
335     io = ioctl(dev, CDROMSUBCHNL, &sc);
336     if (io == -1)
337     {
338         TRACE("opened or no_media (%s)!\n", strerror(errno));
339         hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
340         goto end;
341     }
342
343     hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
344
345     switch (sc.cdsc_audiostatus) {
346     case CDROM_AUDIO_INVALID:
347         hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
348         break;
349     case CDROM_AUDIO_NO_STATUS: 
350         hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
351         break;
352     case CDROM_AUDIO_PLAY:
353         hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
354         break;
355     case CDROM_AUDIO_PAUSED:
356         hdr->AudioStatus = AUDIO_STATUS_PAUSED;
357         break;
358     case CDROM_AUDIO_COMPLETED:
359         hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
360         break;
361     case CDROM_AUDIO_ERROR:
362         hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
363         break;
364     default:
365         TRACE("status=%02X !\n", sc.cdsc_audiostatus);
366         break;
367     }
368     switch (fmt->Format)
369     {
370     case IOCTL_CDROM_CURRENT_POSITION:
371         size = sizeof(SUB_Q_CURRENT_POSITION);
372         data->CurrentPosition.FormatCode = sc.cdsc_format;
373         data->CurrentPosition.Control = sc.cdsc_ctrl;
374         data->CurrentPosition.ADR = sc.cdsc_adr;
375         data->CurrentPosition.TrackNumber = sc.cdsc_trk;
376         data->CurrentPosition.IndexNumber = sc.cdsc_ind;
377
378         data->CurrentPosition.AbsoluteAddress[0] = 0;
379         data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute;
380         data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
381         data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
382
383         data->CurrentPosition.TrackRelativeAddress[0] = 0;
384         data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute;
385         data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
386         data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
387         break;
388     case IOCTL_CDROM_MEDIA_CATALOG:
389         size = sizeof(SUB_Q_MEDIA_CATALOG_NUMBER);
390         data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
391         {
392             struct cdrom_mcn mcn;
393             if ((io = ioctl(dev, CDROM_GET_MCN, &mcn)) == -1) goto end;
394
395             data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
396             data->MediaCatalog.Mcval = 0; /* FIXME */
397             memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
398             data->MediaCatalog.MediaCatalog[14] = 0;
399         }
400         break;
401     case IOCTL_CDROM_TRACK_ISRC:
402         size = sizeof(SUB_Q_CURRENT_POSITION);
403         FIXME("TrackIsrc: NIY on linux");
404         data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
405         data->TrackIsrc.Tcval = 0;
406         io = 0;
407         break;
408     }
409
410  end:
411     ret = CDROM_GetStatusCode(io);
412 #elif defined(__FreeBSD__) || defined(__NetBSD__)
413     unsigned            size;
414     SUB_Q_HEADER*       hdr = (SUB_Q_HEADER*)data;
415     int                 io;
416     struct ioc_read_subchannel  read_sc;
417     struct cd_sub_channel_info  sc;
418
419     read_sc.address_format = CD_MSF_FORMAT;
420     read_sc.track          = 0;
421     read_sc.data_len       = sizeof(sc);
422     read_sc.data           = &sc;
423     switch (fmt->Format)
424     {
425     case IOCTL_CDROM_CURRENT_POSITION:
426         read_sc.data_format    = CD_CURRENT_POSITION;
427         break;
428     case IOCTL_CDROM_MEDIA_CATALOG:
429         read_sc.data_format    = CD_MEDIA_CATALOG;
430         break;
431     case IOCTL_CDROM_TRACK_ISRC:
432         read_sc.data_format    = CD_TRACK_INFO;
433         sc.what.track_info.track_number = data->TrackIsrc.Track;
434         break;
435     }
436     io = ioctl(dev, CDIOCREADSUBCHANNEL, &read_sc);
437     if (io == -1)
438     {
439         TRACE("opened or no_media (%s)!\n", strerror(errno));
440         hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
441         goto end;
442     }
443
444     hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
445
446     switch (sc.header.audio_status) {
447     case CD_AS_AUDIO_INVALID:
448         hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
449         break;
450     case CD_AS_NO_STATUS:
451         hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
452         break;
453     case CD_AS_PLAY_IN_PROGRESS:
454         hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
455         break;
456     case CD_AS_PLAY_PAUSED:
457         hdr->AudioStatus = AUDIO_STATUS_PAUSED;
458         break;
459     case CD_AS_PLAY_COMPLETED:
460         hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
461         break;
462     case CD_AS_PLAY_ERROR:
463         hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
464         break;
465     default:
466         TRACE("status=%02X !\n", sc.header.audio_status);
467     }
468     switch (fmt->Format)
469     {
470     case IOCTL_CDROM_CURRENT_POSITION:
471         size = sizeof(SUB_Q_CURRENT_POSITION);
472         data->CurrentPosition.FormatCode = sc.what.position.data_format;
473         data->CurrentPosition.Control = sc.what.position.control;
474         data->CurrentPosition.ADR = sc.what.position.addr_type;
475         data->CurrentPosition.TrackNumber = sc.what.position.track_number;
476         data->CurrentPosition.IndexNumber = sc.what.position.index_number;
477
478         data->CurrentPosition.AbsoluteAddress[0] = 0;
479         data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
480         data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
481         data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
482         data->CurrentPosition.TrackRelativeAddress[0] = 0;
483         data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
484         data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
485         data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
486         break;
487     case IOCTL_CDROM_MEDIA_CATALOG:
488         size = sizeof(SUB_Q_MEDIA_CATALOG_NUMBER);
489         data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
490         data->MediaCatalog.FormatCode = sc.what.media_catalog.data_format;
491         data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
492         memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
493         break;
494     case IOCTL_CDROM_TRACK_ISRC:
495         size = sizeof(SUB_Q_CURRENT_POSITION);
496         data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
497         memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
498         break;
499     }
500
501  end:
502     ret = CDROM_GetStatusCode(io);
503 #endif
504     return ret;
505 }
506
507 /******************************************************************
508  *              CDROM_Verify
509  *
510  *
511  */
512 static DWORD CDROM_Verify(int dev)
513 {
514     /* quick implementation */
515     CDROM_SUB_Q_DATA_FORMAT     fmt;
516     SUB_Q_CHANNEL_DATA          data;
517
518     fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
519     return CDROM_ReadQChannel(dev, &fmt, &data) ? 0 : 1;
520 }
521
522 /******************************************************************
523  *              CDROM_PlayAudioMSF
524  *
525  *
526  */
527 static DWORD CDROM_PlayAudioMSF(int dev, const CDROM_PLAY_AUDIO_MSF* audio_msf)
528 {
529     DWORD       ret = STATUS_NOT_SUPPORTED;
530 #ifdef linux
531     struct      cdrom_msf       msf;
532     int         io;
533
534     msf.cdmsf_min0   = audio_msf->StartingM;
535     msf.cdmsf_sec0   = audio_msf->StartingS;
536     msf.cdmsf_frame0 = audio_msf->StartingF;
537     msf.cdmsf_min1   = audio_msf->EndingM;
538     msf.cdmsf_sec1   = audio_msf->EndingS;
539     msf.cdmsf_frame1 = audio_msf->EndingF;
540
541     io = ioctl(dev, CDROMSTART);
542     if (io == -1)
543     {
544         WARN("motor doesn't start !\n");
545         goto end;
546     }
547     io = ioctl(dev, CDROMPLAYMSF, &msf);
548     if (io == -1)
549     {
550         WARN("device doesn't play !\n");
551         goto end;
552     }
553     TRACE("msf = %d:%d:%d %d:%d:%d\n",
554           msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
555           msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
556  end:
557     ret = CDROM_GetStatusCode(io);
558 #elif defined(__FreeBSD__) || defined(__NetBSD__)
559     struct      ioc_play_msf    msf;
560     int         io;
561
562     msf.start_m      = audio_msf->StartingM;
563     msf.start_s      = audio_msf->StartingS;
564     msf.start_f      = audio_msf->StartingF;
565     msf.end_m        = audio_msf->EndingM;
566     msf.end_s        = audio_msf->EndingS;
567     msf.end_f        = audio_msf->EndingF;
568
569     io = ioctl(dev, CDIOCSTART, NULL);
570     if (io == -1)
571     {
572         WARN("motor doesn't start !\n");
573         goto end;
574     }
575     io = ioctl(dev, CDIOCPLAYMSF, &msf);
576     if (io == -1)
577     {
578         WARN("device doesn't play !\n");
579         goto end;
580     }
581     TRACE("msf = %d:%d:%d %d:%d:%d\n",
582           msf.start_m, msf.start_s, msf.start_f,
583           msf.end_m,   msf.end_s,   msf.end_f);
584 end:
585     ret = CDROM_GetStatusCode(io);
586 #endif
587     return ret;
588 }
589
590 /******************************************************************
591  *              CDROM_SeekAudioMSF
592  *
593  *
594  */
595 static DWORD CDROM_SeekAudioMSF(int dev, const CDROM_SEEK_AUDIO_MSF* audio_msf)
596 {
597 #if defined(linux)
598     struct cdrom_msf0   msf;
599     msf.minute = audio_msf->M;
600     msf.second = audio_msf->S;
601     msf.frame  = audio_msf->F;
602
603     return CDROM_GetStatusCode(ioctl(dev, CDROMSEEK, &msf));
604 #elif defined(__FreeBSD__) || defined(__NetBSD__)
605     FIXME("Could a BSD expert implement the seek function ?\n");
606     return STATUS_NOT_SUPPORTED;
607 #else
608     return STATUS_NOT_SUPPORTED;
609 #endif
610 }
611
612 /******************************************************************
613  *              CDROM_PauseAudio
614  *
615  *
616  */
617 static DWORD CDROM_PauseAudio(int dev)
618 {
619 #if defined(linux)
620     return CDROM_GetStatusCode(ioctl(dev, CDROMPAUSE));
621 #elif defined(__FreeBSD__) || defined(__NetBSD__)
622     return CDROM_GetStatusCode(ioctl(dev, CDIOCPAUSE, NULL));
623 #else
624     return STATUS_NOT_SUPPORTED;
625 #endif
626 }
627
628 /******************************************************************
629  *              CDROM_ResumeAudio
630  *
631  *
632  */
633 static DWORD CDROM_ResumeAudio(int dev)
634 {
635 #if defined(linux)
636     return CDROM_GetStatusCode(ioctl(dev, CDROMRESUME));
637 #elif defined(__FreeBSD__) || defined(__NetBSD__)
638     return CDROM_GetStatusCode(ioctl(dev, CDIOCRESUME, NULL));
639 #else
640     return STATUS_NOT_SUPPORTED;
641 #endif
642 }
643
644 /******************************************************************
645  *              CDROM_StopAudio
646  *
647  *
648  */
649 static DWORD CDROM_StopAudio(int dev)
650 {
651 #if defined(linux)
652     return CDROM_GetStatusCode(ioctl(dev, CDROMSTOP));
653 #elif defined(__FreeBSD__) || defined(__NetBSD__)
654     return CDROM_GetStatusCode(ioctl(dev, CDIOCSTOP, NULL));
655 #else
656     return STATUS_NOT_SUPPORTED;
657 #endif
658 }
659
660 /******************************************************************
661  *              CDROM_GetVolume
662  *
663  *
664  */
665 static DWORD CDROM_GetVolume(int dev, VOLUME_CONTROL* vc)
666 {
667 #if defined(linux)
668     struct cdrom_volctrl volc;
669     int io;
670     
671     io = ioctl(dev, CDROMVOLREAD, &volc);
672     if (io != -1)
673     {
674         vc->PortVolume[0] = volc.channel0;
675         vc->PortVolume[1] = volc.channel1;
676         vc->PortVolume[2] = volc.channel2;
677         vc->PortVolume[3] = volc.channel3;
678     }
679     return CDROM_GetStatusCode(io);
680 #elif defined(__FreeBSD__) || defined(__NetBSD__)
681     struct  ioc_vol     volc;
682     int io;
683     
684     io = ioctl(dev, CDIOCGETVOL, &volc);
685     if (io != -1)
686     {
687         vc->PortVolume[0] = volc.vol[0];
688         vc->PortVolume[1] = volc.vol[1];
689         vc->PortVolume[2] = volc.vol[2];
690         vc->PortVolume[3] = volc.vol[3];
691     } 
692     return CDROM_GetStatusCode(io);
693 #else
694     return STATUS_NOT_SUPPORTED;
695 #endif
696 }
697
698 /******************************************************************
699  *              CDROM_SetVolume
700  *
701  *
702  */
703 static DWORD CDROM_SetVolume(int dev, const VOLUME_CONTROL* vc)
704 {
705 #if defined(linux)
706     struct cdrom_volctrl volc;
707
708     volc.channel0 = vc->PortVolume[0];
709     volc.channel1 = vc->PortVolume[1];
710     volc.channel2 = vc->PortVolume[2];
711     volc.channel3 = vc->PortVolume[3];
712    
713     return CDROM_GetStatusCode(ioctl(dev, CDROMVOLCTRL, &volc));
714 #elif defined(__FreeBSD__) || defined(__NetBSD__)
715     struct  ioc_vol     volc;
716
717     volc.vol[0] = vc->PortVolume[0];
718     volc.vol[1] = vc->PortVolume[1];
719     volc.vol[2] = vc->PortVolume[2];
720     volc.vol[3] = vc->PortVolume[3];
721
722     return CDROM_GetStatusCode(ioctl(dev, CDIOCSETVOL, &volc));
723 #else
724     return STATUS_NOT_SUPPORTED;
725 #endif
726 }
727
728 /******************************************************************
729  *              CDROM_RawRead
730  *
731  *
732  */
733 static DWORD CDROM_RawRead(int dev, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
734 {
735     int         ret = STATUS_NOT_SUPPORTED;
736     int         io = -1;
737     DWORD       sectSize;
738
739     switch (raw->TrackMode)
740     {
741     case YellowMode2:   sectSize = 2336;        break;
742     case XAForm2:       sectSize = 2328;        break;
743     case CDDA:          sectSize = 2352;        break;
744     default:    return STATUS_INVALID_PARAMETER;
745     }
746     if (len < raw->SectorCount * sectSize) return STATUS_BUFFER_TOO_SMALL;
747     /* strangely enough, it seems that sector offsets are always indicated with a size of 2048, 
748      * even if a larger size if read...
749      */
750 #if defined(linux)
751     {
752         struct cdrom_read       cdr;
753         struct cdrom_read_audio cdra;
754
755         switch (raw->TrackMode)
756         {
757         case YellowMode2:
758             if (raw->DiskOffset.s.HighPart) FIXME("Unsupported value\n");
759             cdr.cdread_lba = raw->DiskOffset.s.LowPart; /* FIXME ? */
760             cdr.cdread_bufaddr = buffer;
761             cdr.cdread_buflen = raw->SectorCount * sectSize;
762             io = ioctl(dev, CDROMREADMODE2, &cdr);
763             break;
764         case XAForm2:
765             FIXME("XAForm2: NIY\n");
766             return ret;
767         case CDDA:
768             /* FIXME: the output doesn't seem 100% correct... in fact output is shifted
769              * between by NT2K box and this... should check on the same drive... 
770              * otherwise, I fear a 2352/2368 mismatch somewhere in one of the drivers
771              * (linux/NT).
772              * Anyway, that's not critical at all. We're talking of 16/32 bytes, we're
773              * talking of 0.2 ms of sound
774              */
775             /* 2048 = 2 ** 11 */
776             if (raw->DiskOffset.s.HighPart & ~2047) FIXME("Unsupported value\n");
777             cdra.addr.lba = ((raw->DiskOffset.s.LowPart >> 11) |
778                 (raw->DiskOffset.s.HighPart << (32 - 11))) - 1;
779             FIXME("reading at %u\n", cdra.addr.lba);
780             cdra.addr_format = CDROM_LBA;
781             cdra.nframes = raw->SectorCount;
782             cdra.buf = buffer;
783             io = ioctl(dev, CDROMREADAUDIO, &cdra);
784             break;
785         }
786     }
787 #elif defined(__FreeBSD__)
788     {
789         struct ioc_read_audio   ira;
790
791         switch (raw->TrackMode)
792         {
793         case YellowMode2:
794             FIXME("YellowMode2: NIY\n");
795             return ret;
796         case XAForm2:
797             FIXME("XAForm2: NIY\n");
798             return ret;
799         case CDDA:
800             /* 2048 = 2 ** 11 */
801             if (raw->DiskOffset.s.HighPart & ~2047) FIXME("Unsupported value\n");
802             ira.address.lba = ((raw->DiskOffset.s.LowPart >> 11) |
803                 raw->DiskOffset.s.HighPart << (32 - 11)) - 1;
804             ira.address_format = CD_LBA_FORMAT;
805             ira.nframes = raw->SectorCount;
806             ira.buffer = buffer;
807             io = ioctl(dev, CDIOCREADAUDIO, &ira);
808             break;
809         }
810     }
811 #elif defined(__NetBSD__)
812     {
813         switch (raw->TrackMode)
814         {
815         case YellowMode2:
816             FIXME("YellowMode2: NIY\n");
817             return ret;
818         case XAForm2:
819             FIXME("XAForm2: NIY\n");
820             return ret;
821         case CDDA:
822             FIXME("CDDA: NIY\n");
823             return ret;
824         }
825     }
826 #endif
827     *sz = sectSize * raw->SectorCount;
828     ret = CDROM_GetStatusCode(io);
829     return ret;
830 }
831
832 /******************************************************************
833  *              CDROM_DeviceIoControl
834  *
835  *
836  */
837 BOOL CDROM_DeviceIoControl(DWORD clientID, HANDLE hDevice, DWORD dwIoControlCode, 
838                            LPVOID lpInBuffer, DWORD nInBufferSize, 
839                            LPVOID lpOutBuffer, DWORD nOutBufferSize,
840                            LPDWORD lpBytesReturned, LPOVERLAPPED lpOverlapped)
841 {
842     DWORD       sz;
843     DWORD       error = 0;
844     int         dev;
845
846     TRACE("%lx[%c] %lx %lx %ld %lx %ld %lx %lx\n", 
847           (DWORD)hDevice, 'A' + LOWORD(clientID), dwIoControlCode, (DWORD)lpInBuffer, nInBufferSize, 
848           (DWORD)lpOutBuffer, nOutBufferSize, (DWORD)lpBytesReturned, (DWORD)lpOverlapped);
849
850     if (lpBytesReturned) *lpBytesReturned = 0;
851     if (lpOverlapped)
852     {
853         FIXME("Overlapped isn't implemented yet\n");
854         SetLastError(STATUS_NOT_SUPPORTED);
855         return FALSE;
856     }
857
858     SetLastError(0);
859     dev = CDROM_Open(hDevice, clientID);
860     if (dev == -1)
861     {
862         CDROM_GetStatusCode(-1);
863         return FALSE;
864     }
865
866     switch (dwIoControlCode)
867     {
868     case IOCTL_STORAGE_CHECK_VERIFY:
869     case IOCTL_CDROM_CHECK_VERIFY:
870         sz = 0;
871         if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
872             error = STATUS_INVALID_PARAMETER;
873         else error = CDROM_Verify(dev);
874         break;
875
876 /* EPP     case IOCTL_STORAGE_CHECK_VERIFY2: */
877
878 /* EPP     case IOCTL_STORAGE_FIND_NEW_DEVICES: */
879 /* EPP     case IOCTL_CDROM_FIND_NEW_DEVICES: */
880
881     case IOCTL_STORAGE_LOAD_MEDIA:
882     case IOCTL_CDROM_LOAD_MEDIA:
883         sz = 0;
884         if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
885             error = STATUS_INVALID_PARAMETER;
886         else error = CDROM_SetTray(dev, FALSE);
887         break;
888      case IOCTL_STORAGE_EJECT_MEDIA:
889         sz = 0;
890         if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
891             error = STATUS_INVALID_PARAMETER;
892         else error = CDROM_SetTray(dev, TRUE);
893         break;
894
895     case IOCTL_DISK_MEDIA_REMOVAL:
896     case IOCTL_STORAGE_MEDIA_REMOVAL:
897     case IOCTL_STORAGE_EJECTION_CONTROL:
898         /* FIXME the last ioctl:s is not the same as the two others... 
899          * lockcount/owner should be handled */
900         sz = 0;
901         if (lpOutBuffer != NULL || nOutBufferSize != 0) error = STATUS_INVALID_PARAMETER;
902         else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) error = STATUS_BUFFER_TOO_SMALL;
903         else error = CDROM_ControlEjection(dev, (const PREVENT_MEDIA_REMOVAL*)lpInBuffer);
904         break;
905
906 /* EPP     case IOCTL_STORAGE_GET_MEDIA_TYPES: */
907
908     case IOCTL_STORAGE_GET_DEVICE_NUMBER:
909         sz = sizeof(STORAGE_DEVICE_NUMBER);
910         if (lpInBuffer != NULL || nInBufferSize != 0) error = STATUS_INVALID_PARAMETER;
911         else if (nOutBufferSize < sz) error = STATUS_BUFFER_TOO_SMALL;
912         else error = CDROM_GetDeviceNumber(dev, (STORAGE_DEVICE_NUMBER*)lpOutBuffer);
913         break;
914
915     case IOCTL_STORAGE_RESET_DEVICE:
916         sz = 0;
917         if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
918             error = STATUS_INVALID_PARAMETER;
919         else error = CDROM_ResetAudio(dev);
920         break;
921
922     case IOCTL_CDROM_GET_CONTROL:
923         sz = sizeof(CDROM_AUDIO_CONTROL);
924         if (lpInBuffer != NULL || nInBufferSize != 0) error = STATUS_INVALID_PARAMETER;
925         else if (nOutBufferSize < sz) error = STATUS_BUFFER_TOO_SMALL;
926         else error = CDROM_GetControl(dev, (CDROM_AUDIO_CONTROL*)lpOutBuffer);
927         break;
928
929     case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
930         sz = sizeof(DISK_GEOMETRY);
931         if (lpInBuffer != NULL || nInBufferSize != 0) error = STATUS_INVALID_PARAMETER;
932         else if (nOutBufferSize < sz) error = STATUS_BUFFER_TOO_SMALL;
933         else error = CDROM_GetDriveGeometry(dev, (DISK_GEOMETRY*)lpOutBuffer);
934         break;
935
936     case IOCTL_CDROM_DISK_TYPE:
937         sz = sizeof(CDROM_DISK_DATA);
938         if (lpInBuffer != NULL || nInBufferSize != 0) error = STATUS_INVALID_PARAMETER;
939         else if (nOutBufferSize < sz) error = STATUS_BUFFER_TOO_SMALL;
940         else error = CDROM_GetDiskData(dev, (CDROM_DISK_DATA*)lpOutBuffer);
941         break;
942
943 /* EPP     case IOCTL_CDROM_GET_LAST_SESSION: */
944
945     case IOCTL_CDROM_READ_Q_CHANNEL:
946         sz = sizeof(SUB_Q_CHANNEL_DATA);
947         if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT)) 
948             error = STATUS_INVALID_PARAMETER;
949         else if (nOutBufferSize < sz) error = STATUS_BUFFER_TOO_SMALL;
950         else error = CDROM_ReadQChannel(dev, (const CDROM_SUB_Q_DATA_FORMAT*)lpInBuffer,
951                                         (SUB_Q_CHANNEL_DATA*)lpOutBuffer);
952         break;
953
954     case IOCTL_CDROM_READ_TOC:
955         sz = sizeof(CDROM_TOC);
956         if (lpInBuffer != NULL || nInBufferSize != 0) error = STATUS_INVALID_PARAMETER;
957         else if (nOutBufferSize < sz) error = STATUS_BUFFER_TOO_SMALL;
958         else error = CDROM_ReadTOC(dev, (CDROM_TOC*)lpOutBuffer);
959         break;
960
961 /* EPP     case IOCTL_CDROM_READ_TOC_EX: */
962
963     case IOCTL_CDROM_PAUSE_AUDIO:
964         sz = 0;
965         if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
966             error = STATUS_INVALID_PARAMETER;
967         else error = CDROM_PauseAudio(dev);
968         break;
969     case IOCTL_CDROM_PLAY_AUDIO_MSF:
970         sz = 0;
971         if (lpOutBuffer != NULL || nOutBufferSize != 0) error = STATUS_INVALID_PARAMETER;
972         else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) error = STATUS_BUFFER_TOO_SMALL;
973         else error = CDROM_PlayAudioMSF(dev, (const CDROM_PLAY_AUDIO_MSF*)lpInBuffer);
974         break;
975     case IOCTL_CDROM_RESUME_AUDIO:
976         sz = 0;
977         if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
978             error = STATUS_INVALID_PARAMETER;
979         else error = CDROM_ResumeAudio(dev);
980         break;
981     case IOCTL_CDROM_SEEK_AUDIO_MSF:
982         sz = 0;
983         if (lpOutBuffer != NULL || nOutBufferSize != 0) error = STATUS_INVALID_PARAMETER;
984         else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) error = STATUS_BUFFER_TOO_SMALL;
985         else error = CDROM_SeekAudioMSF(dev, (const CDROM_SEEK_AUDIO_MSF*)lpInBuffer);
986         break;
987     case IOCTL_CDROM_STOP_AUDIO:
988         sz = 0;
989         if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
990             error = STATUS_INVALID_PARAMETER;
991         else error = CDROM_StopAudio(dev);
992         break;
993     case IOCTL_CDROM_GET_VOLUME:
994         sz = sizeof(VOLUME_CONTROL);
995         if (lpInBuffer != NULL || nInBufferSize != 0) error = STATUS_INVALID_PARAMETER;
996         else if (nOutBufferSize < sz) error = STATUS_BUFFER_TOO_SMALL;
997         else error = CDROM_GetVolume(dev, (VOLUME_CONTROL*)lpOutBuffer);
998         break;
999     case IOCTL_CDROM_SET_VOLUME:
1000         sz = 0;
1001         if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
1002             error = STATUS_INVALID_PARAMETER;
1003         else error = CDROM_SetVolume(dev, (const VOLUME_CONTROL*)lpInBuffer);
1004         break;
1005     case IOCTL_CDROM_RAW_READ:
1006         sz = 0;
1007         if (nInBufferSize < sizeof(RAW_READ_INFO)) error = STATUS_INVALID_PARAMETER;
1008         else if (lpOutBuffer == NULL) error = STATUS_BUFFER_TOO_SMALL;
1009         else error = CDROM_RawRead(dev, (const RAW_READ_INFO*)lpInBuffer, 
1010                                    lpOutBuffer, nOutBufferSize, &sz);
1011         break;
1012     default:
1013         FIXME("Unsupported IOCTL %lx\n", dwIoControlCode);
1014         sz = 0;
1015         error = STATUS_INVALID_PARAMETER;
1016         break;
1017     }
1018
1019     if (lpBytesReturned) *lpBytesReturned = sz;
1020     if (error) 
1021     {
1022         SetLastError(error);
1023         return FALSE;
1024     }
1025     CDROM_Close(clientID, dev);
1026     return TRUE;
1027 }
1028