1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /* Main file for CD-ROM support
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1999, 2001, 2003 Eric Pouech
6 * Copyright 2000 Andreas Mohr
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.
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.
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
24 #include "wine/port.h"
38 #include <sys/types.h>
40 #ifdef HAVE_SYS_IOCTL_H
41 #include <sys/ioctl.h>
46 #ifdef HAVE_SCSI_SCSI_H
47 # include <scsi/scsi.h>
48 # undef REASSIGN_BLOCKS /* avoid conflict with winioctl.h */
50 #ifdef HAVE_SCSI_SCSI_IOCTL_H
51 # include <scsi/scsi_ioctl.h>
53 #ifdef HAVE_LINUX_MAJOR_H
54 # include <linux/major.h>
56 #ifdef HAVE_LINUX_HDREG_H
57 # include <linux/hdreg.h>
59 #ifdef HAVE_LINUX_PARAM_H
60 # include <linux/param.h>
62 #ifdef HAVE_LINUX_CDROM_H
63 # include <linux/cdrom.h>
65 #ifdef HAVE_LINUX_UCDROM_H
66 # include <linux/ucdrom.h>
68 #ifdef HAVE_SYS_CDIO_H
69 # include <sys/cdio.h>
71 #ifdef HAVE_SYS_SCSIIO_H
72 # include <sys/scsiio.h>
75 #define NONAMELESSUNION
76 #define NONAMELESSSTRUCT
86 #include "ntdll_misc.h"
87 #include "wine/server.h"
88 #include "wine/library.h"
89 #include "wine/debug.h"
91 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
92 lack the following constants. */
95 #define CD_SECS 60 /* seconds per minute */
98 #define CD_FRAMES 75 /* frames per second */
101 static const struct iocodexs
106 {IOCTL_CDROM_UNLOAD_DRIVER, "IOCTL_CDROM_UNLOAD_DRIVER"},
107 {IOCTL_CDROM_READ_TOC, "IOCTL_CDROM_READ_TOC"},
108 {IOCTL_CDROM_GET_CONTROL, "IOCTL_CDROM_GET_CONTROL"},
109 {IOCTL_CDROM_PLAY_AUDIO_MSF, "IOCTL_CDROM_PLAY_AUDIO_MSF"},
110 {IOCTL_CDROM_SEEK_AUDIO_MSF, "IOCTL_CDROM_SEEK_AUDIO_MSF"},
111 {IOCTL_CDROM_STOP_AUDIO, "IOCTL_CDROM_STOP_AUDIO"},
112 {IOCTL_CDROM_PAUSE_AUDIO, "IOCTL_CDROM_PAUSE_AUDIO"},
113 {IOCTL_CDROM_RESUME_AUDIO, "IOCTL_CDROM_RESUME_AUDIO"},
114 {IOCTL_CDROM_GET_VOLUME, "IOCTL_CDROM_GET_VOLUME"},
115 {IOCTL_CDROM_SET_VOLUME, "IOCTL_CDROM_SET_VOLUME"},
116 {IOCTL_CDROM_READ_Q_CHANNEL, "IOCTL_CDROM_READ_Q_CHANNEL"},
117 {IOCTL_CDROM_GET_LAST_SESSION, "IOCTL_CDROM_GET_LAST_SESSION"},
118 {IOCTL_CDROM_RAW_READ, "IOCTL_CDROM_RAW_READ"},
119 {IOCTL_CDROM_DISK_TYPE, "IOCTL_CDROM_DISK_TYPE"},
120 {IOCTL_CDROM_GET_DRIVE_GEOMETRY, "IOCTL_CDROM_GET_DRIVE_GEOMETRY"},
121 {IOCTL_CDROM_CHECK_VERIFY, "IOCTL_CDROM_CHECK_VERIFY"},
122 {IOCTL_CDROM_MEDIA_REMOVAL, "IOCTL_CDROM_MEDIA_REMOVAL"},
123 {IOCTL_CDROM_EJECT_MEDIA, "IOCTL_CDROM_EJECT_MEDIA"},
124 {IOCTL_CDROM_LOAD_MEDIA, "IOCTL_CDROM_LOAD_MEDIA"},
125 {IOCTL_CDROM_RESERVE, "IOCTL_CDROM_RESERVE"},
126 {IOCTL_CDROM_RELEASE, "IOCTL_CDROM_RELEASE"},
127 {IOCTL_CDROM_FIND_NEW_DEVICES, "IOCTL_CDROM_FIND_NEW_DEVICES"}
129 static const char *iocodex(DWORD code)
132 static char buffer[25];
133 for(i=0; i<sizeof(iocodextable)/sizeof(struct iocodexs); i++)
134 if (code==iocodextable[i].code)
135 return iocodextable[i].codex;
136 sprintf(buffer, "IOCTL_CODE_%x", (int)code);
140 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
142 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
143 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
144 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
145 #define MSF_OF_FRAME(m,fr) {int f=(fr); ((UCHAR *)&(m))[2]=f%CD_FRAMES;f/=CD_FRAMES;((UCHAR *)&(m))[1]=f%CD_SECS;((UCHAR *)&(m))[0]=f/CD_SECS;}
147 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
148 static NTSTATUS CDROM_GetStatusCode(int);
154 # define IDE6_MAJOR 88
157 # define IDE7_MAJOR 89
160 # ifdef CDROM_SEND_PACKET
161 /* structure for CDROM_PACKET_COMMAND ioctl */
162 /* not all Linux versions have all the fields, so we define the
163 * structure ourselves to make sure */
164 struct linux_cdrom_generic_command
166 unsigned char cmd[CDROM_PACKET_SIZE];
167 unsigned char *buffer;
170 struct request_sense *sense;
171 unsigned char data_direction;
176 # endif /* CDROM_SEND_PACKET */
180 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
181 * this should be removed when a proper device interface is implemented
183 * (WS) We need this to keep track of current position and to safely
184 * detect media changes. Besides this should provide a great speed up
190 char toc_good; /* if false, will reread TOC from disk */
192 SUB_Q_CURRENT_POSITION CurrentPosition;
194 /* who has more than 5 cdroms on his/her machine ?? */
195 /* FIXME: this should grow depending on the number of cdroms we install/configure
198 #define MAX_CACHE_ENTRIES 5
199 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
201 static CRITICAL_SECTION cache_section;
202 static CRITICAL_SECTION_DEBUG critsect_debug =
204 0, 0, &cache_section,
205 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
206 0, 0, { 0, (DWORD)(__FILE__ ": cache_section") }
208 static CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
210 /* Proposed media change function: not really needed at this time */
211 /* This is a 1 or 0 type of function */
213 static int CDROM_MediaChanged(int dev)
217 struct cdrom_tochdr hdr;
218 struct cdrom_tocentry entry;
220 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
222 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
225 if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
228 for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
230 if (i == hdr.cdth_trk1 + 1)
232 entry.cdte_track = CDROM_LEADOUT;
234 entry.cdte_track = i;
236 entry.cdte_format = CDROM_MSF;
237 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
239 if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
240 sizeof(struct cdrom_tocentry)) )
247 /******************************************************************
248 * CDROM_SyncCache [internal]
250 * Read the TOC in and store it in the cdrom_cache structure.
251 * Further requests for the TOC will be copied from the cache
252 * unless certain events like disk ejection is detected, in which
253 * case the cache will be cleared, causing it to be resynced.
254 * The cache section must be held by caller.
256 static int CDROM_SyncCache(int dev, int fd)
260 struct cdrom_tochdr hdr;
261 struct cdrom_tocentry entry;
262 #elif defined(__FreeBSD__) || defined(__NetBSD__)
263 struct ioc_toc_header hdr;
264 struct ioc_read_toc_entry entry;
265 struct cd_toc_entry toc_buffer;
267 CDROM_TOC *toc = &cdrom_cache[dev].toc;
268 cdrom_cache[dev].toc_good = 0;
272 io = ioctl(fd, CDROMREADTOCHDR, &hdr);
275 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
279 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
281 toc->FirstTrack = hdr.cdth_trk0;
282 toc->LastTrack = hdr.cdth_trk1;
283 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
284 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
285 toc->Length[0] = tsz >> 8;
286 toc->Length[1] = tsz;
288 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
290 if (i == toc->LastTrack + 1)
291 entry.cdte_track = CDROM_LEADOUT;
293 entry.cdte_track = i;
294 entry.cdte_format = CDROM_MSF;
295 io = ioctl(fd, CDROMREADTOCENTRY, &entry);
297 WARN("error read entry (%s)\n", strerror(errno));
300 toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
301 toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
302 /* marking last track with leadout value as index */
303 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
304 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
305 toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
306 toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
307 toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
309 cdrom_cache[dev].toc_good = 1;
311 #elif defined(__FreeBSD__) || defined(__NetBSD__)
313 io = ioctl(fd, CDIOREADTOCHEADER, &hdr);
316 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
319 toc->FirstTrack = hdr.starting_track;
320 toc->LastTrack = hdr.ending_track;
321 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
322 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
323 toc->Length[0] = tsz >> 8;
324 toc->Length[1] = tsz;
326 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
328 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
330 if (i == toc->LastTrack + 1)
333 entry.starting_track = LEADOUT;
335 entry.starting_track = i;
337 memset((char *)&toc_buffer, 0, sizeof(toc_buffer));
338 entry.address_format = CD_MSF_FORMAT;
339 entry.data_len = sizeof(toc_buffer);
340 entry.data = &toc_buffer;
341 io = ioctl(fd, CDIOREADTOCENTRYS, &entry);
343 WARN("error read entry (%s)\n", strerror(errno));
346 toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
347 toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
348 /* marking last track with leadout value as index */
349 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
350 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
351 toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
352 toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
353 toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
355 cdrom_cache[dev].toc_good = 1;
358 return STATUS_NOT_SUPPORTED;
361 return CDROM_GetStatusCode(io);
364 static void CDROM_ClearCacheEntry(int dev)
366 RtlEnterCriticalSection( &cache_section );
367 cdrom_cache[dev].toc_good = 0;
368 RtlLeaveCriticalSection( &cache_section );
373 /******************************************************************
374 * CDROM_GetInterfaceInfo
376 * Determines the ide interface (the number after the ide), and the
377 * number of the device on that interface for ide cdroms (*port == 0).
378 * Determines the scsi information for scsi cdroms (*port >= 1).
379 * Returns false if the info cannot not be obtained.
381 static int CDROM_GetInterfaceInfo(int fd, int* port, int* iface, int* device,int* lun)
385 if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode))
387 FIXME("cdrom not a block device!!!\n");
394 switch (major(st.st_rdev)) {
395 case IDE0_MAJOR: *iface = 0; break;
396 case IDE1_MAJOR: *iface = 1; break;
397 case IDE2_MAJOR: *iface = 2; break;
398 case IDE3_MAJOR: *iface = 3; break;
399 case IDE4_MAJOR: *iface = 4; break;
400 case IDE5_MAJOR: *iface = 5; break;
401 case IDE6_MAJOR: *iface = 6; break;
402 case IDE7_MAJOR: *iface = 7; break;
403 default: *port = 1; break;
407 *device = (minor(st.st_rdev) >> 6);
410 #ifdef SCSI_IOCTL_GET_IDLUN
412 if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, &idlun) != -1)
414 *port = ((idlun[0] >> 24) & 0xff) + 1;
415 *iface = (idlun[0] >> 16) & 0xff;
416 *device = idlun[0] & 0xff;
417 *lun = (idlun[0] >> 8) & 0xff;
422 WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
427 #elif defined(__NetBSD__)
428 struct scsi_addr addr;
429 if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
433 case TYPE_SCSI: *port = 1;
434 *iface = addr.addr.scsi.scbus;
435 *device = addr.addr.scsi.target;
436 *lun = addr.addr.scsi.lun;
438 case TYPE_ATAPI: *port = 0;
439 *iface = addr.addr.atapi.atbus;
440 *device = addr.addr.atapi.drive;
447 #elif defined(__FreeBSD__)
448 FIXME("not implemented for BSD\n");
451 FIXME("not implemented for nonlinux\n");
457 /******************************************************************
461 static NTSTATUS CDROM_Open(int fd, int* dev)
464 NTSTATUS ret = STATUS_SUCCESS;
469 RtlEnterCriticalSection( &cache_section );
470 for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
473 cdrom_cache[*dev].device == 0 &&
474 cdrom_cache[*dev].inode == 0)
476 else if (cdrom_cache[*dev].device == st.st_dev &&
477 cdrom_cache[*dev].inode == st.st_ino)
480 if (*dev == MAX_CACHE_ENTRIES)
482 if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
486 cdrom_cache[*dev].device = st.st_dev;
487 cdrom_cache[*dev].inode = st.st_ino;
490 RtlLeaveCriticalSection( &cache_section );
492 TRACE("%d, %d\n", *dev, fd);
496 /******************************************************************
497 * CDROM_GetStatusCode
501 static NTSTATUS CDROM_GetStatusCode(int io)
503 if (io == 0) return STATUS_SUCCESS;
510 return STATUS_NO_MEDIA_IN_DEVICE;
512 return STATUS_ACCESS_DENIED;
514 return STATUS_INVALID_PARAMETER;
515 /* case EBADF: Bad file descriptor */
517 return STATUS_NOT_SUPPORTED;
519 FIXME("Unmapped error code %d: %s\n", errno, strerror(errno));
520 return STATUS_IO_DEVICE_ERROR;
523 /******************************************************************
527 static NTSTATUS CDROM_GetControl(int dev, CDROM_AUDIO_CONTROL* cac)
529 cac->LbaFormat = 0; /* FIXME */
530 cac->LogicalBlocksPerSecond = 1; /* FIXME */
531 return STATUS_NOT_SUPPORTED;
534 /******************************************************************
535 * CDROM_GetDeviceNumber
538 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
540 return STATUS_NOT_SUPPORTED;
543 /******************************************************************
544 * CDROM_GetDriveGeometry
547 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
553 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
555 fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
556 - FRAME_OF_TOC(toc, 1); /* Total size in frames */
558 dg->Cylinders.u.LowPart = fsize / (64 * 32);
559 dg->Cylinders.u.HighPart = 0;
560 dg->MediaType = RemovableMedia;
561 dg->TracksPerCylinder = 64;
562 dg->SectorsPerTrack = 32;
563 dg->BytesPerSector= 2048;
567 /**************************************************************************
568 * CDROM_Reset [internal]
570 static NTSTATUS CDROM_ResetAudio(int fd)
573 return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
574 #elif defined(__FreeBSD__) || defined(__NetBSD__)
575 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
577 return STATUS_NOT_SUPPORTED;
581 /******************************************************************
586 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
589 return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
590 #elif defined(__FreeBSD__) || defined(__NetBSD__)
591 return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
592 (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
593 (ioctl(fd, CDIOCPREVENT, NULL)));
595 return STATUS_NOT_SUPPORTED;
599 /******************************************************************
600 * CDROM_ControlEjection
604 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
607 return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
608 #elif defined(__FreeBSD__) || defined(__NetBSD__)
609 return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
611 return STATUS_NOT_SUPPORTED;
615 /******************************************************************
620 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
622 NTSTATUS ret = STATUS_NOT_SUPPORTED;
624 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
625 return STATUS_INVALID_PARAMETER;
627 RtlEnterCriticalSection( &cache_section );
628 if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
630 *toc = cdrom_cache[dev].toc;
631 ret = STATUS_SUCCESS;
633 RtlLeaveCriticalSection( &cache_section );
637 /******************************************************************
642 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
648 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
650 for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
651 if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
652 data->DiskData |= CDROM_DISK_DATA_TRACK;
654 data->DiskData |= CDROM_DISK_AUDIO_TRACK;
656 return STATUS_SUCCESS;
659 /******************************************************************
664 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
665 SUB_Q_CHANNEL_DATA* data)
667 NTSTATUS ret = STATUS_NOT_SUPPORTED;
670 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
672 struct cdrom_subchnl sc;
673 sc.cdsc_format = CDROM_MSF;
675 io = ioctl(fd, CDROMSUBCHNL, &sc);
678 TRACE("opened or no_media (%s)!\n", strerror(errno));
679 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
680 CDROM_ClearCacheEntry(dev);
684 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
686 switch (sc.cdsc_audiostatus) {
687 case CDROM_AUDIO_INVALID:
688 CDROM_ClearCacheEntry(dev);
689 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
691 case CDROM_AUDIO_NO_STATUS:
692 CDROM_ClearCacheEntry(dev);
693 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
695 case CDROM_AUDIO_PLAY:
696 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
698 case CDROM_AUDIO_PAUSED:
699 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
701 case CDROM_AUDIO_COMPLETED:
702 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
704 case CDROM_AUDIO_ERROR:
705 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
708 TRACE("status=%02X !\n", sc.cdsc_audiostatus);
713 case IOCTL_CDROM_CURRENT_POSITION:
714 size = sizeof(SUB_Q_CURRENT_POSITION);
715 RtlEnterCriticalSection( &cache_section );
716 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
717 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
718 data->CurrentPosition.Control = sc.cdsc_ctrl;
719 data->CurrentPosition.ADR = sc.cdsc_adr;
720 data->CurrentPosition.TrackNumber = sc.cdsc_trk;
721 data->CurrentPosition.IndexNumber = sc.cdsc_ind;
723 data->CurrentPosition.AbsoluteAddress[0] = 0;
724 data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute;
725 data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
726 data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
728 data->CurrentPosition.TrackRelativeAddress[0] = 0;
729 data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute;
730 data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
731 data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
733 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
735 else /* not playing */
737 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
738 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
740 RtlLeaveCriticalSection( &cache_section );
742 case IOCTL_CDROM_MEDIA_CATALOG:
743 size = sizeof(SUB_Q_MEDIA_CATALOG_NUMBER);
744 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
746 struct cdrom_mcn mcn;
747 if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
749 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
750 data->MediaCatalog.Mcval = 0; /* FIXME */
751 memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
752 data->MediaCatalog.MediaCatalog[14] = 0;
755 case IOCTL_CDROM_TRACK_ISRC:
756 size = sizeof(SUB_Q_CURRENT_POSITION);
757 FIXME("TrackIsrc: NIY on linux\n");
758 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
759 data->TrackIsrc.Tcval = 0;
765 ret = CDROM_GetStatusCode(io);
766 #elif defined(__FreeBSD__) || defined(__NetBSD__)
768 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
770 struct ioc_read_subchannel read_sc;
771 struct cd_sub_channel_info sc;
773 read_sc.address_format = CD_MSF_FORMAT;
775 read_sc.data_len = sizeof(sc);
779 case IOCTL_CDROM_CURRENT_POSITION:
780 read_sc.data_format = CD_CURRENT_POSITION;
782 case IOCTL_CDROM_MEDIA_CATALOG:
783 read_sc.data_format = CD_MEDIA_CATALOG;
785 case IOCTL_CDROM_TRACK_ISRC:
786 read_sc.data_format = CD_TRACK_INFO;
787 sc.what.track_info.track_number = data->TrackIsrc.Track;
790 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
793 TRACE("opened or no_media (%s)!\n", strerror(errno));
794 CDROM_ClearCacheEntry(dev);
795 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
799 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
801 switch (sc.header.audio_status) {
802 case CD_AS_AUDIO_INVALID:
803 CDROM_ClearCacheEntry(dev);
804 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
806 case CD_AS_NO_STATUS:
807 CDROM_ClearCacheEntry(dev);
808 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
810 case CD_AS_PLAY_IN_PROGRESS:
811 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
813 case CD_AS_PLAY_PAUSED:
814 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
816 case CD_AS_PLAY_COMPLETED:
817 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
819 case CD_AS_PLAY_ERROR:
820 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
823 TRACE("status=%02X !\n", sc.header.audio_status);
827 case IOCTL_CDROM_CURRENT_POSITION:
828 size = sizeof(SUB_Q_CURRENT_POSITION);
829 RtlEnterCriticalSection( &cache_section );
830 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
831 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
832 data->CurrentPosition.Control = sc.what.position.control;
833 data->CurrentPosition.ADR = sc.what.position.addr_type;
834 data->CurrentPosition.TrackNumber = sc.what.position.track_number;
835 data->CurrentPosition.IndexNumber = sc.what.position.index_number;
837 data->CurrentPosition.AbsoluteAddress[0] = 0;
838 data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
839 data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
840 data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
841 data->CurrentPosition.TrackRelativeAddress[0] = 0;
842 data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
843 data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
844 data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
845 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
847 else { /* not playing */
848 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
849 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
851 RtlLeaveCriticalSection( &cache_section );
853 case IOCTL_CDROM_MEDIA_CATALOG:
854 size = sizeof(SUB_Q_MEDIA_CATALOG_NUMBER);
855 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
856 data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
857 memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
859 case IOCTL_CDROM_TRACK_ISRC:
860 size = sizeof(SUB_Q_CURRENT_POSITION);
861 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
862 data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
863 memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
868 ret = CDROM_GetStatusCode(io);
873 /******************************************************************
878 static NTSTATUS CDROM_Verify(int dev, int fd)
880 /* quick implementation */
881 CDROM_SUB_Q_DATA_FORMAT fmt;
882 SUB_Q_CHANNEL_DATA data;
884 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
885 return CDROM_ReadQChannel(dev, fd, &fmt, &data) ? 1 : 0;
888 /******************************************************************
893 static NTSTATUS CDROM_PlayAudioMSF(int fd, const CDROM_PLAY_AUDIO_MSF* audio_msf)
895 NTSTATUS ret = STATUS_NOT_SUPPORTED;
897 struct cdrom_msf msf;
900 msf.cdmsf_min0 = audio_msf->StartingM;
901 msf.cdmsf_sec0 = audio_msf->StartingS;
902 msf.cdmsf_frame0 = audio_msf->StartingF;
903 msf.cdmsf_min1 = audio_msf->EndingM;
904 msf.cdmsf_sec1 = audio_msf->EndingS;
905 msf.cdmsf_frame1 = audio_msf->EndingF;
907 io = ioctl(fd, CDROMSTART);
910 WARN("motor doesn't start !\n");
913 io = ioctl(fd, CDROMPLAYMSF, &msf);
916 WARN("device doesn't play !\n");
919 TRACE("msf = %d:%d:%d %d:%d:%d\n",
920 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
921 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
923 ret = CDROM_GetStatusCode(io);
924 #elif defined(__FreeBSD__) || defined(__NetBSD__)
925 struct ioc_play_msf msf;
928 msf.start_m = audio_msf->StartingM;
929 msf.start_s = audio_msf->StartingS;
930 msf.start_f = audio_msf->StartingF;
931 msf.end_m = audio_msf->EndingM;
932 msf.end_s = audio_msf->EndingS;
933 msf.end_f = audio_msf->EndingF;
935 io = ioctl(fd, CDIOCSTART, NULL);
938 WARN("motor doesn't start !\n");
941 io = ioctl(fd, CDIOCPLAYMSF, &msf);
944 WARN("device doesn't play !\n");
947 TRACE("msf = %d:%d:%d %d:%d:%d\n",
948 msf.start_m, msf.start_s, msf.start_f,
949 msf.end_m, msf.end_s, msf.end_f);
951 ret = CDROM_GetStatusCode(io);
956 /******************************************************************
961 static NTSTATUS CDROM_SeekAudioMSF(int dev, int fd, const CDROM_SEEK_AUDIO_MSF* audio_msf)
965 SUB_Q_CURRENT_POSITION *cp;
967 struct cdrom_msf0 msf;
968 struct cdrom_subchnl sc;
969 #elif defined(__FreeBSD__) || defined(__NetBSD__)
970 struct ioc_play_msf msf;
971 struct ioc_read_subchannel read_sc;
972 struct cd_sub_channel_info sc;
976 /* Use the information on the TOC to compute the new current
977 * position, which is shadowed on the cache. [Portable]. */
978 frame = FRAME_OF_MSF(*audio_msf);
980 if ((io = CDROM_ReadTOC(dev, fd, &toc)) != 0) return io;
982 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
983 if (FRAME_OF_TOC(toc,i)>frame) break;
984 if (i <= toc.FirstTrack || i > toc.LastTrack+1)
985 return STATUS_INVALID_PARAMETER;
987 RtlEnterCriticalSection( &cache_section );
988 cp = &cdrom_cache[dev].CurrentPosition;
989 cp->FormatCode = IOCTL_CDROM_CURRENT_POSITION;
990 cp->Control = toc.TrackData[i-toc.FirstTrack].Control;
991 cp->ADR = toc.TrackData[i-toc.FirstTrack].Adr;
992 cp->TrackNumber = toc.TrackData[i-toc.FirstTrack].TrackNumber;
993 cp->IndexNumber = 0; /* FIXME: where do they keep these? */
994 cp->AbsoluteAddress[0] = 0;
995 cp->AbsoluteAddress[1] = toc.TrackData[i-toc.FirstTrack].Address[1];
996 cp->AbsoluteAddress[2] = toc.TrackData[i-toc.FirstTrack].Address[2];
997 cp->AbsoluteAddress[3] = toc.TrackData[i-toc.FirstTrack].Address[3];
998 frame -= FRAME_OF_TOC(toc,i);
999 cp->TrackRelativeAddress[0] = 0;
1000 MSF_OF_FRAME(cp->TrackRelativeAddress[1], frame);
1001 RtlLeaveCriticalSection( &cache_section );
1003 /* If playing, then issue a seek command, otherwise do nothing */
1005 sc.cdsc_format = CDROM_MSF;
1007 io = ioctl(fd, CDROMSUBCHNL, &sc);
1010 TRACE("opened or no_media (%s)!\n", strerror(errno));
1011 CDROM_ClearCacheEntry(dev);
1012 return CDROM_GetStatusCode(io);
1014 if (sc.cdsc_audiostatus==CDROM_AUDIO_PLAY)
1016 msf.minute = audio_msf->M;
1017 msf.second = audio_msf->S;
1018 msf.frame = audio_msf->F;
1019 return CDROM_GetStatusCode(ioctl(fd, CDROMSEEK, &msf));
1021 return STATUS_SUCCESS;
1022 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1023 read_sc.address_format = CD_MSF_FORMAT;
1025 read_sc.data_len = sizeof(sc);
1027 read_sc.data_format = CD_CURRENT_POSITION;
1029 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1032 TRACE("opened or no_media (%s)!\n", strerror(errno));
1033 CDROM_ClearCacheEntry(dev);
1034 return CDROM_GetStatusCode(io);
1036 if (sc.header.audio_status==CD_AS_PLAY_IN_PROGRESS)
1039 msf.start_m = audio_msf->M;
1040 msf.start_s = audio_msf->S;
1041 msf.start_f = audio_msf->F;
1042 final_frame = FRAME_OF_TOC(toc,toc.LastTrack+1)-1;
1043 MSF_OF_FRAME(msf.end_m, final_frame);
1045 return CDROM_GetStatusCode(ioctl(fd, CDIOCPLAYMSF, &msf));
1047 return STATUS_SUCCESS;
1049 return STATUS_NOT_SUPPORTED;
1053 /******************************************************************
1058 static NTSTATUS CDROM_PauseAudio(int fd)
1061 return CDROM_GetStatusCode(ioctl(fd, CDROMPAUSE));
1062 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1063 return CDROM_GetStatusCode(ioctl(fd, CDIOCPAUSE, NULL));
1065 return STATUS_NOT_SUPPORTED;
1069 /******************************************************************
1074 static NTSTATUS CDROM_ResumeAudio(int fd)
1077 return CDROM_GetStatusCode(ioctl(fd, CDROMRESUME));
1078 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1079 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESUME, NULL));
1081 return STATUS_NOT_SUPPORTED;
1085 /******************************************************************
1090 static NTSTATUS CDROM_StopAudio(int fd)
1093 return CDROM_GetStatusCode(ioctl(fd, CDROMSTOP));
1094 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1095 return CDROM_GetStatusCode(ioctl(fd, CDIOCSTOP, NULL));
1097 return STATUS_NOT_SUPPORTED;
1101 /******************************************************************
1106 static NTSTATUS CDROM_GetVolume(int fd, VOLUME_CONTROL* vc)
1109 struct cdrom_volctrl volc;
1112 io = ioctl(fd, CDROMVOLREAD, &volc);
1115 vc->PortVolume[0] = volc.channel0;
1116 vc->PortVolume[1] = volc.channel1;
1117 vc->PortVolume[2] = volc.channel2;
1118 vc->PortVolume[3] = volc.channel3;
1120 return CDROM_GetStatusCode(io);
1121 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1122 struct ioc_vol volc;
1125 io = ioctl(fd, CDIOCGETVOL, &volc);
1128 vc->PortVolume[0] = volc.vol[0];
1129 vc->PortVolume[1] = volc.vol[1];
1130 vc->PortVolume[2] = volc.vol[2];
1131 vc->PortVolume[3] = volc.vol[3];
1133 return CDROM_GetStatusCode(io);
1135 return STATUS_NOT_SUPPORTED;
1139 /******************************************************************
1144 static NTSTATUS CDROM_SetVolume(int fd, const VOLUME_CONTROL* vc)
1147 struct cdrom_volctrl volc;
1149 volc.channel0 = vc->PortVolume[0];
1150 volc.channel1 = vc->PortVolume[1];
1151 volc.channel2 = vc->PortVolume[2];
1152 volc.channel3 = vc->PortVolume[3];
1154 return CDROM_GetStatusCode(ioctl(fd, CDROMVOLCTRL, &volc));
1155 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1156 struct ioc_vol volc;
1158 volc.vol[0] = vc->PortVolume[0];
1159 volc.vol[1] = vc->PortVolume[1];
1160 volc.vol[2] = vc->PortVolume[2];
1161 volc.vol[3] = vc->PortVolume[3];
1163 return CDROM_GetStatusCode(ioctl(fd, CDIOCSETVOL, &volc));
1165 return STATUS_NOT_SUPPORTED;
1169 /******************************************************************
1174 static NTSTATUS CDROM_RawRead(int fd, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
1176 int ret = STATUS_NOT_SUPPORTED;
1180 switch (raw->TrackMode)
1182 case YellowMode2: sectSize = 2336; break;
1183 case XAForm2: sectSize = 2328; break;
1184 case CDDA: sectSize = 2352; break;
1185 default: return STATUS_INVALID_PARAMETER;
1187 if (len < raw->SectorCount * sectSize) return STATUS_BUFFER_TOO_SMALL;
1188 /* strangely enough, it seems that sector offsets are always indicated with a size of 2048,
1189 * even if a larger size if read...
1193 struct cdrom_read cdr;
1194 struct cdrom_read_audio cdra;
1196 switch (raw->TrackMode)
1199 if (raw->DiskOffset.u.HighPart) FIXME("Unsupported value\n");
1200 cdr.cdread_lba = raw->DiskOffset.u.LowPart; /* FIXME ? */
1201 cdr.cdread_bufaddr = buffer;
1202 cdr.cdread_buflen = raw->SectorCount * sectSize;
1203 io = ioctl(fd, CDROMREADMODE2, &cdr);
1206 FIXME("XAForm2: NIY\n");
1209 /* FIXME: the output doesn't seem 100% correct... in fact output is shifted
1210 * between by NT2K box and this... should check on the same drive...
1211 * otherwise, I fear a 2352/2368 mismatch somewhere in one of the drivers
1213 * Anyway, that's not critical at all. We're talking of 16/32 bytes, we're
1214 * talking of 0.2 ms of sound
1216 /* 2048 = 2 ** 11 */
1217 if (raw->DiskOffset.u.HighPart & ~2047) FIXME("Unsupported value\n");
1218 cdra.addr.lba = ((raw->DiskOffset.u.LowPart >> 11) |
1219 (raw->DiskOffset.u.HighPart << (32 - 11))) - 1;
1220 FIXME("reading at %u\n", cdra.addr.lba);
1221 cdra.addr_format = CDROM_LBA;
1222 cdra.nframes = raw->SectorCount;
1224 io = ioctl(fd, CDROMREADAUDIO, &cdra);
1227 FIXME("NIY: %d\n", raw->TrackMode);
1233 switch (raw->TrackMode)
1236 FIXME("YellowMode2: NIY\n");
1239 FIXME("XAForm2: NIY\n");
1242 FIXME("CDDA: NIY\n");
1248 *sz = sectSize * raw->SectorCount;
1249 ret = CDROM_GetStatusCode(io);
1253 /******************************************************************
1254 * CDROM_ScsiPassThroughDirect
1258 static NTSTATUS CDROM_ScsiPassThroughDirect(int fd, PSCSI_PASS_THROUGH_DIRECT pPacket)
1260 int ret = STATUS_NOT_SUPPORTED;
1261 #if defined(linux) && defined(CDROM_SEND_PACKET)
1262 struct linux_cdrom_generic_command cmd;
1263 struct request_sense sense;
1266 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1267 return STATUS_BUFFER_TOO_SMALL;
1269 if (pPacket->CdbLength > 12)
1270 return STATUS_INVALID_PARAMETER;
1272 if (pPacket->SenseInfoLength > sizeof(sense))
1273 return STATUS_INVALID_PARAMETER;
1275 memset(&cmd, 0, sizeof(cmd));
1276 memset(&sense, 0, sizeof(sense));
1278 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1280 cmd.buffer = pPacket->DataBuffer;
1281 cmd.buflen = pPacket->DataTransferLength;
1284 cmd.timeout = pPacket->TimeOutValue*HZ;
1286 switch (pPacket->DataIn)
1288 case SCSI_IOCTL_DATA_OUT:
1289 cmd.data_direction = CGC_DATA_WRITE;
1291 case SCSI_IOCTL_DATA_IN:
1292 cmd.data_direction = CGC_DATA_READ;
1294 case SCSI_IOCTL_DATA_UNSPECIFIED:
1295 cmd.data_direction = CGC_DATA_NONE;
1298 return STATUS_INVALID_PARAMETER;
1301 io = ioctl(fd, CDROM_SEND_PACKET, &cmd);
1303 if (pPacket->SenseInfoLength != 0)
1305 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1306 &sense, pPacket->SenseInfoLength);
1309 pPacket->ScsiStatus = cmd.stat;
1311 ret = CDROM_GetStatusCode(io);
1313 #elif defined(__NetBSD__)
1317 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1318 return STATUS_BUFFER_TOO_SMALL;
1320 if (pPacket->CdbLength > 12)
1321 return STATUS_INVALID_PARAMETER;
1323 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1324 return STATUS_INVALID_PARAMETER;
1326 memset(&cmd, 0, sizeof(cmd));
1327 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1329 cmd.cmdlen = pPacket->CdbLength;
1330 cmd.databuf = pPacket->DataBuffer;
1331 cmd.datalen = pPacket->DataTransferLength;
1332 cmd.senselen = pPacket->SenseInfoLength;
1333 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1335 switch (pPacket->DataIn)
1337 case SCSI_IOCTL_DATA_OUT:
1338 cmd.flags |= SCCMD_WRITE;
1340 case SCSI_IOCTL_DATA_IN:
1341 cmd.flags |= SCCMD_READ;
1343 case SCSI_IOCTL_DATA_UNSPECIFIED:
1347 return STATUS_INVALID_PARAMETER;
1350 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1354 case SCCMD_OK: break;
1355 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1357 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1359 case SCCMD_SENSE: break;
1360 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1364 if (pPacket->SenseInfoLength != 0)
1366 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1367 cmd.sense, pPacket->SenseInfoLength);
1370 pPacket->ScsiStatus = cmd.status;
1372 ret = CDROM_GetStatusCode(io);
1377 /******************************************************************
1378 * CDROM_ScsiPassThrough
1382 static NTSTATUS CDROM_ScsiPassThrough(int fd, PSCSI_PASS_THROUGH pPacket)
1384 int ret = STATUS_NOT_SUPPORTED;
1385 #if defined(linux) && defined(CDROM_SEND_PACKET)
1386 struct linux_cdrom_generic_command cmd;
1387 struct request_sense sense;
1390 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1391 return STATUS_BUFFER_TOO_SMALL;
1393 if (pPacket->CdbLength > 12)
1394 return STATUS_INVALID_PARAMETER;
1396 if (pPacket->SenseInfoLength > sizeof(sense))
1397 return STATUS_INVALID_PARAMETER;
1399 memset(&cmd, 0, sizeof(cmd));
1400 memset(&sense, 0, sizeof(sense));
1402 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1404 if ( pPacket->DataBufferOffset > 0x1000 )
1406 cmd.buffer = (void*)pPacket->DataBufferOffset;
1410 cmd.buffer = (char*)pPacket + pPacket->DataBufferOffset;
1412 cmd.buflen = pPacket->DataTransferLength;
1415 cmd.timeout = pPacket->TimeOutValue*HZ;
1417 switch (pPacket->DataIn)
1419 case SCSI_IOCTL_DATA_OUT:
1420 cmd.data_direction = CGC_DATA_WRITE;
1422 case SCSI_IOCTL_DATA_IN:
1423 cmd.data_direction = CGC_DATA_READ;
1425 case SCSI_IOCTL_DATA_UNSPECIFIED:
1426 cmd.data_direction = CGC_DATA_NONE;
1429 return STATUS_INVALID_PARAMETER;
1432 io = ioctl(fd, CDROM_SEND_PACKET, &cmd);
1434 if (pPacket->SenseInfoLength != 0)
1436 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1437 &sense, pPacket->SenseInfoLength);
1440 pPacket->ScsiStatus = cmd.stat;
1442 ret = CDROM_GetStatusCode(io);
1444 #elif defined(__NetBSD__)
1448 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1449 return STATUS_BUFFER_TOO_SMALL;
1451 if (pPacket->CdbLength > 12)
1452 return STATUS_INVALID_PARAMETER;
1454 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1455 return STATUS_INVALID_PARAMETER;
1457 memset(&cmd, 0, sizeof(cmd));
1458 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1460 if ( pPacket->DataBufferOffset > 0x1000 )
1462 cmd.databuf = (void*)pPacket->DataBufferOffset;
1466 cmd.databuf = (char*)pPacket + pPacket->DataBufferOffset;
1469 cmd.cmdlen = pPacket->CdbLength;
1470 cmd.datalen = pPacket->DataTransferLength;
1471 cmd.senselen = pPacket->SenseInfoLength;
1472 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1474 switch (pPacket->DataIn)
1476 case SCSI_IOCTL_DATA_OUT:
1477 cmd.flags |= SCCMD_WRITE;
1479 case SCSI_IOCTL_DATA_IN:
1480 cmd.flags |= SCCMD_READ;
1482 case SCSI_IOCTL_DATA_UNSPECIFIED:
1486 return STATUS_INVALID_PARAMETER;
1489 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1493 case SCCMD_OK: break;
1494 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1496 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1498 case SCCMD_SENSE: break;
1499 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1503 if (pPacket->SenseInfoLength != 0)
1505 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1506 cmd.sense, pPacket->SenseInfoLength);
1509 pPacket->ScsiStatus = cmd.status;
1511 ret = CDROM_GetStatusCode(io);
1516 /******************************************************************
1521 static NTSTATUS CDROM_ScsiGetCaps(PIO_SCSI_CAPABILITIES caps)
1523 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
1525 caps->Length = sizeof(*caps);
1527 caps->MaximumTransferLength = SG_SCATTER_SZ; /* FIXME */
1528 caps->MaximumPhysicalPages = SG_SCATTER_SZ / getpagesize();
1529 caps->SupportedAsynchronousEvents = TRUE;
1530 caps->AlignmentMask = getpagesize();
1531 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
1532 caps->AdapterScansDown = FALSE; /* FIXME ? */
1533 caps->AdapterUsesPio = FALSE; /* FIXME ? */
1534 ret = STATUS_SUCCESS;
1536 FIXME("Unimplemented\n");
1541 /******************************************************************
1544 * implements IOCTL_SCSI_GET_ADDRESS
1546 static NTSTATUS CDROM_GetAddress(int fd, SCSI_ADDRESS* address)
1548 int portnum, busid, targetid, lun;
1550 address->Length = sizeof(SCSI_ADDRESS);
1551 if ( ! CDROM_GetInterfaceInfo(fd, &portnum, &busid, &targetid, &lun))
1552 return STATUS_NOT_SUPPORTED;
1554 address->PortNumber = portnum;
1555 address->PathId = busid; /* bus number */
1556 address->TargetId = targetid;
1558 return STATUS_SUCCESS;
1561 /******************************************************************
1562 * CDROM_DeviceIoControl
1566 NTSTATUS CDROM_DeviceIoControl(HANDLE hDevice,
1567 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1568 PVOID UserApcContext,
1569 PIO_STATUS_BLOCK piosb,
1570 ULONG dwIoControlCode,
1571 LPVOID lpInBuffer, DWORD nInBufferSize,
1572 LPVOID lpOutBuffer, DWORD nOutBufferSize)
1575 NTSTATUS status = STATUS_SUCCESS;
1578 TRACE("%lx %s %lx %ld %lx %ld %p\n",
1579 (DWORD)hDevice, iocodex(dwIoControlCode), (DWORD)lpInBuffer, nInBufferSize,
1580 (DWORD)lpOutBuffer, nOutBufferSize, piosb);
1582 piosb->Information = 0;
1584 if ((status = wine_server_handle_to_fd( hDevice, 0, &fd, NULL, NULL ))) goto error;
1585 if ((status = CDROM_Open(fd, &dev)))
1587 wine_server_release_fd( hDevice, fd );
1591 switch (dwIoControlCode)
1593 case IOCTL_STORAGE_CHECK_VERIFY:
1594 case IOCTL_CDROM_CHECK_VERIFY:
1596 CDROM_ClearCacheEntry(dev);
1597 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
1598 status = STATUS_INVALID_PARAMETER;
1599 else status = CDROM_Verify(dev, fd);
1602 /* EPP case IOCTL_STORAGE_CHECK_VERIFY2: */
1604 /* EPP case IOCTL_STORAGE_FIND_NEW_DEVICES: */
1605 /* EPP case IOCTL_CDROM_FIND_NEW_DEVICES: */
1607 case IOCTL_STORAGE_LOAD_MEDIA:
1608 case IOCTL_CDROM_LOAD_MEDIA:
1610 CDROM_ClearCacheEntry(dev);
1611 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
1612 status = STATUS_INVALID_PARAMETER;
1613 else status = CDROM_SetTray(fd, FALSE);
1615 case IOCTL_STORAGE_EJECT_MEDIA:
1617 CDROM_ClearCacheEntry(dev);
1618 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
1619 status = STATUS_INVALID_PARAMETER;
1620 else status = CDROM_SetTray(fd, TRUE);
1623 case IOCTL_CDROM_MEDIA_REMOVAL:
1624 case IOCTL_DISK_MEDIA_REMOVAL:
1625 case IOCTL_STORAGE_MEDIA_REMOVAL:
1626 case IOCTL_STORAGE_EJECTION_CONTROL:
1627 /* FIXME the last ioctl:s is not the same as the two others...
1628 * lockcount/owner should be handled */
1630 CDROM_ClearCacheEntry(dev);
1631 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
1632 else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) status = STATUS_BUFFER_TOO_SMALL;
1633 else status = CDROM_ControlEjection(fd, (const PREVENT_MEDIA_REMOVAL*)lpInBuffer);
1636 /* EPP case IOCTL_STORAGE_GET_MEDIA_TYPES: */
1638 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
1639 sz = sizeof(STORAGE_DEVICE_NUMBER);
1640 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
1641 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
1642 else status = CDROM_GetDeviceNumber(dev, (STORAGE_DEVICE_NUMBER*)lpOutBuffer);
1645 case IOCTL_STORAGE_RESET_DEVICE:
1647 CDROM_ClearCacheEntry(dev);
1648 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
1649 status = STATUS_INVALID_PARAMETER;
1650 else status = CDROM_ResetAudio(fd);
1653 case IOCTL_CDROM_GET_CONTROL:
1654 sz = sizeof(CDROM_AUDIO_CONTROL);
1655 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
1656 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
1657 else status = CDROM_GetControl(dev, (CDROM_AUDIO_CONTROL*)lpOutBuffer);
1660 case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
1661 sz = sizeof(DISK_GEOMETRY);
1662 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
1663 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
1664 else status = CDROM_GetDriveGeometry(dev, fd, (DISK_GEOMETRY*)lpOutBuffer);
1667 case IOCTL_CDROM_DISK_TYPE:
1668 sz = sizeof(CDROM_DISK_DATA);
1669 /* CDROM_ClearCacheEntry(dev); */
1670 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
1671 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
1672 else status = CDROM_GetDiskData(dev, fd, (CDROM_DISK_DATA*)lpOutBuffer);
1675 /* EPP case IOCTL_CDROM_GET_LAST_SESSION: */
1677 case IOCTL_CDROM_READ_Q_CHANNEL:
1678 sz = sizeof(SUB_Q_CHANNEL_DATA);
1679 if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT))
1680 status = STATUS_INVALID_PARAMETER;
1681 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
1682 else status = CDROM_ReadQChannel(dev, fd, (const CDROM_SUB_Q_DATA_FORMAT*)lpInBuffer,
1683 (SUB_Q_CHANNEL_DATA*)lpOutBuffer);
1686 case IOCTL_CDROM_READ_TOC:
1687 sz = sizeof(CDROM_TOC);
1688 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
1689 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
1690 else status = CDROM_ReadTOC(dev, fd, (CDROM_TOC*)lpOutBuffer);
1693 /* EPP case IOCTL_CDROM_READ_TOC_EX: */
1695 case IOCTL_CDROM_PAUSE_AUDIO:
1697 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
1698 status = STATUS_INVALID_PARAMETER;
1699 else status = CDROM_PauseAudio(fd);
1701 case IOCTL_CDROM_PLAY_AUDIO_MSF:
1703 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
1704 else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
1705 else status = CDROM_PlayAudioMSF(fd, (const CDROM_PLAY_AUDIO_MSF*)lpInBuffer);
1707 case IOCTL_CDROM_RESUME_AUDIO:
1709 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
1710 status = STATUS_INVALID_PARAMETER;
1711 else status = CDROM_ResumeAudio(fd);
1713 case IOCTL_CDROM_SEEK_AUDIO_MSF:
1715 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
1716 else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
1717 else status = CDROM_SeekAudioMSF(dev, fd, (const CDROM_SEEK_AUDIO_MSF*)lpInBuffer);
1719 case IOCTL_CDROM_STOP_AUDIO:
1721 CDROM_ClearCacheEntry(dev); /* Maybe intention is to change media */
1722 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
1723 status = STATUS_INVALID_PARAMETER;
1724 else status = CDROM_StopAudio(fd);
1726 case IOCTL_CDROM_GET_VOLUME:
1727 sz = sizeof(VOLUME_CONTROL);
1728 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
1729 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
1730 else status = CDROM_GetVolume(fd, (VOLUME_CONTROL*)lpOutBuffer);
1732 case IOCTL_CDROM_SET_VOLUME:
1734 CDROM_ClearCacheEntry(dev);
1735 if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
1736 status = STATUS_INVALID_PARAMETER;
1737 else status = CDROM_SetVolume(fd, (const VOLUME_CONTROL*)lpInBuffer);
1739 case IOCTL_CDROM_RAW_READ:
1741 if (nInBufferSize < sizeof(RAW_READ_INFO)) status = STATUS_INVALID_PARAMETER;
1742 else if (lpOutBuffer == NULL) status = STATUS_BUFFER_TOO_SMALL;
1743 else status = CDROM_RawRead(fd, (const RAW_READ_INFO*)lpInBuffer,
1744 lpOutBuffer, nOutBufferSize, &sz);
1746 case IOCTL_SCSI_GET_ADDRESS:
1747 sz = sizeof(SCSI_ADDRESS);
1748 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
1749 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
1750 else status = CDROM_GetAddress(fd, (SCSI_ADDRESS*)lpOutBuffer);
1752 case IOCTL_SCSI_PASS_THROUGH_DIRECT:
1753 sz = sizeof(SCSI_PASS_THROUGH_DIRECT);
1754 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
1755 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH_DIRECT)) status = STATUS_BUFFER_TOO_SMALL;
1756 else status = CDROM_ScsiPassThroughDirect(fd, (PSCSI_PASS_THROUGH_DIRECT)lpOutBuffer);
1758 case IOCTL_SCSI_PASS_THROUGH:
1759 sz = sizeof(SCSI_PASS_THROUGH);
1760 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
1761 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH)) status = STATUS_BUFFER_TOO_SMALL;
1762 else status = CDROM_ScsiPassThrough(fd, (PSCSI_PASS_THROUGH)lpOutBuffer);
1764 case IOCTL_SCSI_GET_CAPABILITIES:
1765 sz = sizeof(IO_SCSI_CAPABILITIES);
1766 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
1767 else if (nOutBufferSize < sizeof(IO_SCSI_CAPABILITIES)) status = STATUS_BUFFER_TOO_SMALL;
1768 else status = CDROM_ScsiGetCaps((PIO_SCSI_CAPABILITIES)lpOutBuffer);
1771 FIXME("Unsupported IOCTL %lx (type=%lx access=%lx func=%lx meth=%lx)\n",
1772 dwIoControlCode, dwIoControlCode >> 16, (dwIoControlCode >> 14) & 3,
1773 (dwIoControlCode >> 2) & 0xFFF, dwIoControlCode & 3);
1775 status = STATUS_INVALID_PARAMETER;
1778 wine_server_release_fd( hDevice, fd );
1780 piosb->u.Status = status;
1781 piosb->Information = sz;
1782 if (hEvent) NtSetEvent(hEvent, NULL);