Remove local declarations already in included public headers.
[wine/multimedia.git] / dlls / ntdll / cdrom.c
blob5552fcb042181476717450ad4b397e460d35d144
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
7 * Copyright 2005 Ivan Leo Puoti
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <errno.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #ifdef HAVE_IO_H
32 # include <io.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <fcntl.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
40 #endif
41 #include <sys/types.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 #include <sys/ioctl.h>
45 #endif
46 #ifdef HAVE_SCSI_SG_H
47 # include <scsi/sg.h>
48 #endif
49 #ifdef HAVE_SCSI_SCSI_H
50 # include <scsi/scsi.h>
51 # undef REASSIGN_BLOCKS /* avoid conflict with winioctl.h */
52 #endif
53 #ifdef HAVE_SCSI_SCSI_IOCTL_H
54 # include <scsi/scsi_ioctl.h>
55 #endif
56 #ifdef HAVE_LINUX_MAJOR_H
57 # include <linux/major.h>
58 #endif
59 #ifdef HAVE_LINUX_HDREG_H
60 # include <linux/hdreg.h>
61 #endif
62 #ifdef HAVE_LINUX_PARAM_H
63 # include <linux/param.h>
64 #endif
65 #ifdef HAVE_LINUX_CDROM_H
66 # include <linux/cdrom.h>
67 #endif
68 #ifdef HAVE_LINUX_UCDROM_H
69 # include <linux/ucdrom.h>
70 #endif
71 #ifdef HAVE_SYS_CDIO_H
72 # include <sys/cdio.h>
73 #endif
74 #ifdef HAVE_SYS_SCSIIO_H
75 # include <sys/scsiio.h>
76 #endif
78 #ifdef HAVE_IOKIT_IOKITLIB_H
79 # ifndef SENSEBUFLEN
80 # include <IOKit/IOKitLib.h>
81 # include <IOKit/scsi/SCSICmds_REQUEST_SENSE_Defs.h>
82 # define SENSEBUFLEN kSenseDefaultSize
83 # endif
84 #endif
86 #define NONAMELESSUNION
87 #define NONAMELESSSTRUCT
88 #include "ntstatus.h"
89 #include "windef.h"
90 #include "winternl.h"
91 #include "winioctl.h"
92 #include "ntddstor.h"
93 #include "ntddcdrm.h"
94 #include "ntddscsi.h"
95 #include "ntdll_misc.h"
96 #include "wine/server.h"
97 #include "wine/library.h"
98 #include "wine/debug.h"
100 WINE_DEFAULT_DEBUG_CHANNEL(cdrom);
102 /* Non-Linux systems do not have linux/cdrom.h and the like, and thus
103 lack the following constants. */
105 #ifndef CD_SECS
106 # define CD_SECS 60 /* seconds per minute */
107 #endif
108 #ifndef CD_FRAMES
109 # define CD_FRAMES 75 /* frames per second */
110 #endif
112 /* definitions taken from libdvdcss */
114 #define IOCTL_DVD_BASE FILE_DEVICE_DVD
116 #define IOCTL_DVD_START_SESSION CTL_CODE(IOCTL_DVD_BASE, 0x0400, METHOD_BUFFERED, FILE_READ_ACCESS)
117 #define IOCTL_DVD_READ_KEY CTL_CODE(IOCTL_DVD_BASE, 0x0401, METHOD_BUFFERED, FILE_READ_ACCESS)
118 #define IOCTL_DVD_SEND_KEY CTL_CODE(IOCTL_DVD_BASE, 0x0402, METHOD_BUFFERED, FILE_READ_ACCESS)
119 #define IOCTL_DVD_END_SESSION CTL_CODE(IOCTL_DVD_BASE, 0x0403, METHOD_BUFFERED, FILE_READ_ACCESS)
120 #define IOCTL_DVD_SET_READ_AHEAD CTL_CODE(IOCTL_DVD_BASE, 0x0404, METHOD_BUFFERED, FILE_READ_ACCESS)
121 #define IOCTL_DVD_GET_REGION CTL_CODE(IOCTL_DVD_BASE, 0x0405, METHOD_BUFFERED, FILE_READ_ACCESS)
122 #define IOCTL_DVD_SEND_KEY2 CTL_CODE(IOCTL_DVD_BASE, 0x0406, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
124 #define IOCTL_DVD_READ_STRUCTURE CTL_CODE(IOCTL_DVD_BASE, 0x0450, METHOD_BUFFERED, FILE_READ_ACCESS)
126 typedef enum {
127 DvdChallengeKey = 0x01,
128 DvdBusKey1,
129 DvdBusKey2,
130 DvdTitleKey,
131 DvdAsf,
132 DvdSetRpcKey = 0x6,
133 DvdGetRpcKey = 0x8,
134 DvdDiskKey = 0x80,
135 DvdInvalidateAGID = 0x3f
136 } DVD_KEY_TYPE;
138 typedef ULONG DVD_SESSION_ID, *PDVD_SESSION_ID;
140 typedef struct _DVD_COPY_PROTECT_KEY {
141 ULONG KeyLength;
142 DVD_SESSION_ID SessionId;
143 DVD_KEY_TYPE KeyType;
144 ULONG KeyFlags;
145 union {
146 struct {
147 ULONG FileHandle;
148 ULONG Reserved; /* used for NT alignment */
149 } s;
150 LARGE_INTEGER TitleOffset;
151 } Parameters;
152 UCHAR KeyData[1];
153 } DVD_COPY_PROTECT_KEY, *PDVD_COPY_PROTECT_KEY;
155 typedef struct _DVD_RPC_KEY {
156 UCHAR UserResetsAvailable:3;
157 UCHAR ManufacturerResetsAvailable:3;
158 UCHAR TypeCode:2;
159 UCHAR RegionMask;
160 UCHAR RpcScheme;
161 UCHAR Reserved2[1];
162 } DVD_RPC_KEY, * PDVD_RPC_KEY;
164 typedef struct _DVD_ASF {
165 UCHAR Reserved0[3];
166 UCHAR SuccessFlag:1;
167 UCHAR Reserved1:7;
168 } DVD_ASF, * PDVD_ASF;
170 typedef struct _DVD_REGION
172 unsigned char copy_system;
173 unsigned char region_data; /* current media region (not playable when set) */
174 unsigned char system_region; /* current drive region (playable when set) */
175 unsigned char reset_count; /* number of resets available */
176 } DVD_REGION, * PDVD_REGION;
178 typedef struct _DVD_READ_STRUCTURE {
179 /* Contains an offset to the logical block address of the descriptor to be retrieved. */
180 LARGE_INTEGER block_byte_offset;
182 /* 0:Physical descriptor, 1:Copyright descriptor, 2:Disk key descriptor
183 3:BCA descriptor, 4:Manufacturer descriptor, 5:Max descriptor
185 long format;
187 /* Session ID, that is obtained by IOCTL_DVD_START_SESSION */
188 long session;
190 /* From 0 to 4 */
191 unsigned char layer_no;
192 }DVD_READ_STRUCTURE, * PDVD_READ_STRUCTURE;
194 typedef struct _DVD_LAYER_DESCRIPTOR
196 unsigned short length;
198 unsigned char book_version : 4;
200 /* 0:DVD-ROM, 1:DVD-RAM, 2:DVD-R, 3:DVD-RW, 9:DVD-RW */
201 unsigned char book_type : 4;
203 unsigned char minimum_rate : 4;
205 /* The physical size of the media. 0:120 mm, 1:80 mm. */
206 unsigned char disk_size : 4;
208 /* 1:Read-only layer, 2:Recordable layer, 4:Rewritable layer */
209 unsigned char layer_type : 4;
211 /* 0:parallel track path, 1:opposite track path */
212 unsigned char track_path : 1;
214 /* 0:one layers, 1:two layers, and so on */
215 unsigned char num_of_layers : 2;
217 unsigned char reserved1 : 1;
219 /* 0:0.74 µm/track, 1:0.80 µm/track, 2:0.615 µm/track */
220 unsigned char track_density : 4;
222 /* 0:0.267 µm/bit, 1:0.293 µm/bit, 2:0.409 to 0.435 µm/bit, 4:0.280 to 0.291 µm/bit, 8:0.353 µm/bit */
223 unsigned char linear_density : 4;
225 /* Must be either 0x30000:DVD-ROM or DVD-R/-RW or 0x31000:DVD-RAM or DVD+RW */
226 unsigned long starting_data_sector;
228 unsigned long end_data_sector;
229 unsigned long end_layer_zero_sector;
230 unsigned char reserved5 : 7;
232 /* 0 indicates no BCA data */
233 unsigned char BCA_flag : 1;
235 unsigned char reserved6;
236 }DVD_LAYER_DESCRIPTOR, * PDVD_LAYER_DESCRIPTOR;
238 typedef struct _DVD_COPYRIGHT_DESCRIPTOR
240 unsigned char protection;
241 unsigned char region;
242 unsigned short reserved;
243 }DVD_COPYRIGHT_DESCRIPTOR, * PDVD_COPYRIGHT_DESCRIPTOR;
245 typedef struct _DVD_MANUFACTURER_DESCRIPTOR
247 unsigned char manufacturing[2048];
248 }DVD_MANUFACTURER_DESCRIPTOR, * PDVD_MANUFACTURER_DESCRIPTOR;
250 #define DVD_CHALLENGE_KEY_LENGTH (12 + sizeof(DVD_COPY_PROTECT_KEY) - sizeof(UCHAR))
252 #define DVD_DISK_KEY_LENGTH (2048 + sizeof(DVD_COPY_PROTECT_KEY) - sizeof(UCHAR))
254 #define DVD_KEY_SIZE 5
255 #define DVD_CHALLENGE_SIZE 10
256 #define DVD_DISCKEY_SIZE 2048
257 #define DVD_SECTOR_PROTECTED 0x00000020
259 static const struct iocodexs
261 DWORD code;
262 const char *codex;
263 } iocodextable[] = {
264 #define X(x) { x, #x },
265 X(IOCTL_CDROM_CHECK_VERIFY)
266 X(IOCTL_CDROM_CURRENT_POSITION)
267 X(IOCTL_CDROM_DISK_TYPE)
268 X(IOCTL_CDROM_GET_CONTROL)
269 X(IOCTL_CDROM_GET_DRIVE_GEOMETRY)
270 X(IOCTL_CDROM_GET_VOLUME)
271 X(IOCTL_CDROM_LOAD_MEDIA)
272 X(IOCTL_CDROM_MEDIA_CATALOG)
273 X(IOCTL_CDROM_MEDIA_REMOVAL)
274 X(IOCTL_CDROM_PAUSE_AUDIO)
275 X(IOCTL_CDROM_PLAY_AUDIO_MSF)
276 X(IOCTL_CDROM_RAW_READ)
277 X(IOCTL_CDROM_READ_Q_CHANNEL)
278 X(IOCTL_CDROM_READ_TOC)
279 X(IOCTL_CDROM_RESUME_AUDIO)
280 X(IOCTL_CDROM_SEEK_AUDIO_MSF)
281 X(IOCTL_CDROM_SET_VOLUME)
282 X(IOCTL_CDROM_STOP_AUDIO)
283 X(IOCTL_CDROM_TRACK_ISRC)
284 X(IOCTL_DISK_MEDIA_REMOVAL)
285 X(IOCTL_DVD_END_SESSION)
286 X(IOCTL_DVD_GET_REGION)
287 X(IOCTL_DVD_READ_KEY)
288 X(IOCTL_DVD_READ_STRUCTURE)
289 X(IOCTL_DVD_SEND_KEY)
290 X(IOCTL_DVD_START_SESSION)
291 X(IOCTL_SCSI_GET_ADDRESS)
292 X(IOCTL_SCSI_GET_CAPABILITIES)
293 X(IOCTL_SCSI_GET_INQUIRY_DATA)
294 X(IOCTL_SCSI_PASS_THROUGH)
295 X(IOCTL_SCSI_PASS_THROUGH_DIRECT)
296 X(IOCTL_STORAGE_CHECK_VERIFY)
297 X(IOCTL_STORAGE_EJECTION_CONTROL)
298 X(IOCTL_STORAGE_EJECT_MEDIA)
299 X(IOCTL_STORAGE_GET_DEVICE_NUMBER)
300 X(IOCTL_STORAGE_LOAD_MEDIA)
301 X(IOCTL_STORAGE_MEDIA_REMOVAL)
302 X(IOCTL_STORAGE_RESET_DEVICE)
303 #undef X
305 static const char *iocodex(DWORD code)
307 unsigned int i;
308 static char buffer[25];
309 for(i=0; i<sizeof(iocodextable)/sizeof(struct iocodexs); i++)
310 if (code==iocodextable[i].code)
311 return iocodextable[i].codex;
312 sprintf(buffer, "IOCTL_CODE_%x", (int)code);
313 return buffer;
316 #define INQ_REPLY_LEN 36
317 #define INQ_CMD_LEN 6
319 #define FRAME_OF_ADDR(a) (((int)(a)[1] * CD_SECS + (a)[2]) * CD_FRAMES + (a)[3])
320 #define FRAME_OF_MSF(a) (((int)(a).M * CD_SECS + (a).S) * CD_FRAMES + (a).F)
321 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
322 #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;}
324 static NTSTATUS CDROM_ReadTOC(int, int, CDROM_TOC*);
325 static NTSTATUS CDROM_GetStatusCode(int);
328 #ifdef linux
330 # ifndef IDE6_MAJOR
331 # define IDE6_MAJOR 88
332 # endif
333 # ifndef IDE7_MAJOR
334 # define IDE7_MAJOR 89
335 # endif
337 # ifdef CDROM_SEND_PACKET
338 /* structure for CDROM_PACKET_COMMAND ioctl */
339 /* not all Linux versions have all the fields, so we define the
340 * structure ourselves to make sure */
341 struct linux_cdrom_generic_command
343 unsigned char cmd[CDROM_PACKET_SIZE];
344 unsigned char *buffer;
345 unsigned int buflen;
346 int stat;
347 struct request_sense *sense;
348 unsigned char data_direction;
349 int quiet;
350 int timeout;
351 void *reserved[1];
353 # endif /* CDROM_SEND_PACKET */
355 #endif /* linux */
357 /* FIXME: this is needed because we can't open simultaneously several times /dev/cdrom
358 * this should be removed when a proper device interface is implemented
360 * (WS) We need this to keep track of current position and to safely
361 * detect media changes. Besides this should provide a great speed up
362 * for toc inquiries.
364 struct cdrom_cache {
365 dev_t device;
366 ino_t inode;
367 char toc_good; /* if false, will reread TOC from disk */
368 CDROM_TOC toc;
369 SUB_Q_CURRENT_POSITION CurrentPosition;
371 /* who has more than 5 cdroms on his/her machine ?? */
372 /* FIXME: this should grow depending on the number of cdroms we install/configure
373 * at startup
375 #define MAX_CACHE_ENTRIES 5
376 static struct cdrom_cache cdrom_cache[MAX_CACHE_ENTRIES];
378 static RTL_CRITICAL_SECTION cache_section;
379 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
381 0, 0, &cache_section,
382 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
383 0, 0, { 0, (DWORD)(__FILE__ ": cache_section") }
385 static RTL_CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
387 /* Proposed media change function: not really needed at this time */
388 /* This is a 1 or 0 type of function */
389 #if 0
390 static int CDROM_MediaChanged(int dev)
392 int i;
394 struct cdrom_tochdr hdr;
395 struct cdrom_tocentry entry;
397 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
398 return 0;
399 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCHDR, &hdr) == -1 )
400 return 0;
402 if ( memcmp(&hdr, &cdrom_cache[dev].hdr, sizeof(struct cdrom_tochdr)) )
403 return 1;
405 for (i=hdr.cdth_trk0; i<=hdr.cdth_trk1+1; i++)
407 if (i == hdr.cdth_trk1 + 1)
409 entry.cdte_track = CDROM_LEADOUT;
410 } else {
411 entry.cdte_track = i;
413 entry.cdte_format = CDROM_MSF;
414 if ( ioctl(cdrom_cache[dev].fd, CDROMREADTOCENTRY, &entry) == -1)
415 return 0;
416 if ( memcmp(&entry, cdrom_cache[dev].entry+i-hdr.cdth_trk0,
417 sizeof(struct cdrom_tocentry)) )
418 return 1;
420 return 0;
422 #endif
424 /******************************************************************
425 * CDROM_SyncCache [internal]
427 * Read the TOC in and store it in the cdrom_cache structure.
428 * Further requests for the TOC will be copied from the cache
429 * unless certain events like disk ejection is detected, in which
430 * case the cache will be cleared, causing it to be resynced.
431 * The cache section must be held by caller.
433 static int CDROM_SyncCache(int dev, int fd)
435 int i, io = 0, tsz;
436 #ifdef linux
437 struct cdrom_tochdr hdr;
438 struct cdrom_tocentry entry;
439 #elif defined(__FreeBSD__) || defined(__NetBSD__)
440 struct ioc_toc_header hdr;
441 struct ioc_read_toc_entry entry;
442 struct cd_toc_entry toc_buffer;
443 #endif
444 CDROM_TOC *toc = &cdrom_cache[dev].toc;
445 cdrom_cache[dev].toc_good = 0;
447 #ifdef linux
449 io = ioctl(fd, CDROMREADTOCHDR, &hdr);
450 if (io == -1)
452 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
453 goto end;
456 toc->FirstTrack = hdr.cdth_trk0;
457 toc->LastTrack = hdr.cdth_trk1;
458 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
459 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
460 toc->Length[0] = tsz >> 8;
461 toc->Length[1] = tsz;
463 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
465 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
467 if (i == toc->LastTrack + 1)
468 entry.cdte_track = CDROM_LEADOUT;
469 else
470 entry.cdte_track = i;
471 entry.cdte_format = CDROM_MSF;
472 io = ioctl(fd, CDROMREADTOCENTRY, &entry);
473 if (io == -1) {
474 WARN("error read entry (%s)\n", strerror(errno));
475 goto end;
477 toc->TrackData[i - toc->FirstTrack].Control = entry.cdte_ctrl;
478 toc->TrackData[i - toc->FirstTrack].Adr = entry.cdte_adr;
479 /* marking last track with leadout value as index */
480 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.cdte_track;
481 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
482 toc->TrackData[i - toc->FirstTrack].Address[1] = entry.cdte_addr.msf.minute;
483 toc->TrackData[i - toc->FirstTrack].Address[2] = entry.cdte_addr.msf.second;
484 toc->TrackData[i - toc->FirstTrack].Address[3] = entry.cdte_addr.msf.frame;
486 cdrom_cache[dev].toc_good = 1;
487 io = 0;
488 #elif defined(__FreeBSD__) || defined(__NetBSD__)
490 io = ioctl(fd, CDIOREADTOCHEADER, &hdr);
491 if (io == -1)
493 WARN("(%d) -- Error occurred (%s)!\n", dev, strerror(errno));
494 goto end;
496 toc->FirstTrack = hdr.starting_track;
497 toc->LastTrack = hdr.ending_track;
498 tsz = sizeof(toc->FirstTrack) + sizeof(toc->LastTrack)
499 + sizeof(TRACK_DATA) * (toc->LastTrack-toc->FirstTrack+2);
500 toc->Length[0] = tsz >> 8;
501 toc->Length[1] = tsz;
503 TRACE("caching toc from=%d to=%d\n", toc->FirstTrack, toc->LastTrack );
505 for (i = toc->FirstTrack; i <= toc->LastTrack + 1; i++)
507 if (i == toc->LastTrack + 1)
509 #define LEADOUT 0xaa
510 entry.starting_track = LEADOUT;
511 } else {
512 entry.starting_track = i;
514 memset((char *)&toc_buffer, 0, sizeof(toc_buffer));
515 entry.address_format = CD_MSF_FORMAT;
516 entry.data_len = sizeof(toc_buffer);
517 entry.data = &toc_buffer;
518 io = ioctl(fd, CDIOREADTOCENTRYS, &entry);
519 if (io == -1) {
520 WARN("error read entry (%s)\n", strerror(errno));
521 goto end;
523 toc->TrackData[i - toc->FirstTrack].Control = toc_buffer.control;
524 toc->TrackData[i - toc->FirstTrack].Adr = toc_buffer.addr_type;
525 /* marking last track with leadout value as index */
526 toc->TrackData[i - toc->FirstTrack].TrackNumber = entry.starting_track;
527 toc->TrackData[i - toc->FirstTrack].Address[0] = 0;
528 toc->TrackData[i - toc->FirstTrack].Address[1] = toc_buffer.addr.msf.minute;
529 toc->TrackData[i - toc->FirstTrack].Address[2] = toc_buffer.addr.msf.second;
530 toc->TrackData[i - toc->FirstTrack].Address[3] = toc_buffer.addr.msf.frame;
532 cdrom_cache[dev].toc_good = 1;
533 io = 0;
534 #else
535 return STATUS_NOT_SUPPORTED;
536 #endif
537 end:
538 return CDROM_GetStatusCode(io);
541 static void CDROM_ClearCacheEntry(int dev)
543 RtlEnterCriticalSection( &cache_section );
544 cdrom_cache[dev].toc_good = 0;
545 RtlLeaveCriticalSection( &cache_section );
550 /******************************************************************
551 * CDROM_GetInterfaceInfo
553 * Determines the ide interface (the number after the ide), and the
554 * number of the device on that interface for ide cdroms (*iface <= 1).
555 * Determines the scsi information for scsi cdroms (*iface >= 2).
556 * Returns false if the info cannot not be obtained.
558 static int CDROM_GetInterfaceInfo(int fd, UCHAR* iface, UCHAR* port, UCHAR* device, UCHAR* lun)
560 #if defined(linux)
561 struct stat st;
562 if ( fstat(fd, &st) == -1 || ! S_ISBLK(st.st_mode)) return 0;
563 *port = 0;
564 *iface = 0;
565 *device = 0;
566 *lun = 0;
567 switch (major(st.st_rdev)) {
568 case IDE0_MAJOR: *iface = 0; break;
569 case IDE1_MAJOR: *iface = 1; break;
570 case IDE2_MAJOR: *iface = 2; break;
571 case IDE3_MAJOR: *iface = 3; break;
572 case IDE4_MAJOR: *iface = 4; break;
573 case IDE5_MAJOR: *iface = 5; break;
574 case IDE6_MAJOR: *iface = 6; break;
575 case IDE7_MAJOR: *iface = 7; break;
576 default: *port = 1; break;
579 if (*port == 0)
580 *device = (minor(st.st_rdev) >> 6);
581 else
583 #ifdef SCSI_IOCTL_GET_IDLUN
584 UINT32 idlun[2];
585 if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, &idlun) != -1)
587 *port = (idlun[0] >> 24) & 0xff;
588 *iface = ((idlun[0] >> 16) & 0xff) + 2;
589 *device = idlun[0] & 0xff;
590 *lun = (idlun[0] >> 8) & 0xff;
592 else
593 #endif
595 WARN("CD-ROM device (%d, %d) not supported\n", major(st.st_rdev), minor(st.st_rdev));
596 return 0;
599 return 1;
600 #elif defined(__NetBSD__)
601 struct scsi_addr addr;
602 if (ioctl(fd, SCIOCIDENTIFY, &addr) != -1)
604 switch (addr.type)
606 case TYPE_SCSI: *port = 1;
607 *iface = addr.addr.scsi.scbus;
608 *device = addr.addr.scsi.target;
609 *lun = addr.addr.scsi.lun;
610 break;
611 case TYPE_ATAPI: *port = 0;
612 *iface = addr.addr.atapi.atbus;
613 *device = addr.addr.atapi.drive;
614 *lun = 0;
615 break;
617 return 1;
619 return 0;
620 #elif defined(__FreeBSD__)
621 FIXME("not implemented for BSD\n");
622 return 0;
623 #else
624 FIXME("not implemented for nonlinux\n");
625 return 0;
626 #endif
630 /******************************************************************
631 * CDROM_Open
634 static NTSTATUS CDROM_Open(int fd, int* dev)
636 struct stat st;
637 NTSTATUS ret = STATUS_SUCCESS;
638 int empty = -1;
640 fstat(fd, &st);
642 RtlEnterCriticalSection( &cache_section );
643 for (*dev = 0; *dev < MAX_CACHE_ENTRIES; (*dev)++)
645 if (empty == -1 &&
646 cdrom_cache[*dev].device == 0 &&
647 cdrom_cache[*dev].inode == 0)
648 empty = *dev;
649 else if (cdrom_cache[*dev].device == st.st_dev &&
650 cdrom_cache[*dev].inode == st.st_ino)
651 break;
653 if (*dev == MAX_CACHE_ENTRIES)
655 if (empty == -1) ret = STATUS_NOT_IMPLEMENTED;
656 else
658 *dev = empty;
659 cdrom_cache[*dev].device = st.st_dev;
660 cdrom_cache[*dev].inode = st.st_ino;
663 RtlLeaveCriticalSection( &cache_section );
665 TRACE("%d, %d\n", *dev, fd);
666 return ret;
669 /******************************************************************
670 * CDROM_GetStatusCode
674 static NTSTATUS CDROM_GetStatusCode(int io)
676 if (io == 0) return STATUS_SUCCESS;
677 return FILE_GetNtStatus();
680 /******************************************************************
681 * CDROM_GetControl
684 static NTSTATUS CDROM_GetControl(int dev, CDROM_AUDIO_CONTROL* cac)
686 cac->LbaFormat = 0; /* FIXME */
687 cac->LogicalBlocksPerSecond = 1; /* FIXME */
688 return STATUS_NOT_SUPPORTED;
691 /******************************************************************
692 * CDROM_GetDeviceNumber
695 static NTSTATUS CDROM_GetDeviceNumber(int dev, STORAGE_DEVICE_NUMBER* devnum)
697 return STATUS_NOT_SUPPORTED;
700 /******************************************************************
701 * CDROM_GetDriveGeometry
704 static NTSTATUS CDROM_GetDriveGeometry(int dev, int fd, DISK_GEOMETRY* dg)
706 CDROM_TOC toc;
707 NTSTATUS ret = 0;
708 int fsize = 0;
710 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
712 fsize = FRAME_OF_TOC(toc, toc.LastTrack+1)
713 - FRAME_OF_TOC(toc, 1); /* Total size in frames */
715 dg->Cylinders.u.LowPart = fsize / (64 * 32);
716 dg->Cylinders.u.HighPart = 0;
717 dg->MediaType = RemovableMedia;
718 dg->TracksPerCylinder = 64;
719 dg->SectorsPerTrack = 32;
720 dg->BytesPerSector= 2048;
721 return ret;
724 /**************************************************************************
725 * CDROM_Reset [internal]
727 static NTSTATUS CDROM_ResetAudio(int fd)
729 #if defined(linux)
730 return CDROM_GetStatusCode(ioctl(fd, CDROMRESET));
731 #elif defined(__FreeBSD__) || defined(__NetBSD__)
732 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESET, NULL));
733 #else
734 return STATUS_NOT_SUPPORTED;
735 #endif
738 /******************************************************************
739 * CDROM_SetTray
743 static NTSTATUS CDROM_SetTray(int fd, BOOL doEject)
745 #if defined(linux)
746 return CDROM_GetStatusCode(ioctl(fd, doEject ? CDROMEJECT : CDROMCLOSETRAY));
747 #elif defined(__FreeBSD__) || defined(__NetBSD__)
748 return CDROM_GetStatusCode((ioctl(fd, CDIOCALLOW, NULL)) ||
749 (ioctl(fd, doEject ? CDIOCEJECT : CDIOCCLOSE, NULL)) ||
750 (ioctl(fd, CDIOCPREVENT, NULL)));
751 #else
752 return STATUS_NOT_SUPPORTED;
753 #endif
756 /******************************************************************
757 * CDROM_ControlEjection
761 static NTSTATUS CDROM_ControlEjection(int fd, const PREVENT_MEDIA_REMOVAL* rmv)
763 #if defined(linux)
764 return CDROM_GetStatusCode(ioctl(fd, CDROM_LOCKDOOR, rmv->PreventMediaRemoval));
765 #elif defined(__FreeBSD__) || defined(__NetBSD__)
766 return CDROM_GetStatusCode(ioctl(fd, (rmv->PreventMediaRemoval) ? CDIOCPREVENT : CDIOCALLOW, NULL));
767 #else
768 return STATUS_NOT_SUPPORTED;
769 #endif
772 /******************************************************************
773 * CDROM_ReadTOC
777 static NTSTATUS CDROM_ReadTOC(int dev, int fd, CDROM_TOC* toc)
779 NTSTATUS ret = STATUS_NOT_SUPPORTED;
781 if (dev < 0 || dev >= MAX_CACHE_ENTRIES)
782 return STATUS_INVALID_PARAMETER;
784 RtlEnterCriticalSection( &cache_section );
785 if (cdrom_cache[dev].toc_good || !(ret = CDROM_SyncCache(dev, fd)))
787 *toc = cdrom_cache[dev].toc;
788 ret = STATUS_SUCCESS;
790 RtlLeaveCriticalSection( &cache_section );
791 return ret;
794 /******************************************************************
795 * CDROM_GetDiskData
799 static NTSTATUS CDROM_GetDiskData(int dev, int fd, CDROM_DISK_DATA* data)
801 CDROM_TOC toc;
802 NTSTATUS ret;
803 int i;
805 if ((ret = CDROM_ReadTOC(dev, fd, &toc)) != 0) return ret;
806 data->DiskData = 0;
807 for (i = toc.FirstTrack; i <= toc.LastTrack; i++) {
808 if (toc.TrackData[i-toc.FirstTrack].Control & 0x04)
809 data->DiskData |= CDROM_DISK_DATA_TRACK;
810 else
811 data->DiskData |= CDROM_DISK_AUDIO_TRACK;
813 return STATUS_SUCCESS;
816 /******************************************************************
817 * CDROM_ReadQChannel
821 static NTSTATUS CDROM_ReadQChannel(int dev, int fd, const CDROM_SUB_Q_DATA_FORMAT* fmt,
822 SUB_Q_CHANNEL_DATA* data)
824 NTSTATUS ret = STATUS_NOT_SUPPORTED;
825 #ifdef linux
826 unsigned size;
827 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
828 int io;
829 struct cdrom_subchnl sc;
830 sc.cdsc_format = CDROM_MSF;
832 io = ioctl(fd, CDROMSUBCHNL, &sc);
833 if (io == -1)
835 TRACE("opened or no_media (%s)!\n", strerror(errno));
836 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
837 CDROM_ClearCacheEntry(dev);
838 goto end;
841 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
843 switch (sc.cdsc_audiostatus) {
844 case CDROM_AUDIO_INVALID:
845 CDROM_ClearCacheEntry(dev);
846 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
847 break;
848 case CDROM_AUDIO_NO_STATUS:
849 CDROM_ClearCacheEntry(dev);
850 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
851 break;
852 case CDROM_AUDIO_PLAY:
853 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
854 break;
855 case CDROM_AUDIO_PAUSED:
856 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
857 break;
858 case CDROM_AUDIO_COMPLETED:
859 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
860 break;
861 case CDROM_AUDIO_ERROR:
862 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
863 break;
864 default:
865 TRACE("status=%02X !\n", sc.cdsc_audiostatus);
866 break;
868 switch (fmt->Format)
870 case IOCTL_CDROM_CURRENT_POSITION:
871 size = sizeof(SUB_Q_CURRENT_POSITION);
872 RtlEnterCriticalSection( &cache_section );
873 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
874 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
875 data->CurrentPosition.Control = sc.cdsc_ctrl;
876 data->CurrentPosition.ADR = sc.cdsc_adr;
877 data->CurrentPosition.TrackNumber = sc.cdsc_trk;
878 data->CurrentPosition.IndexNumber = sc.cdsc_ind;
880 data->CurrentPosition.AbsoluteAddress[0] = 0;
881 data->CurrentPosition.AbsoluteAddress[1] = sc.cdsc_absaddr.msf.minute;
882 data->CurrentPosition.AbsoluteAddress[2] = sc.cdsc_absaddr.msf.second;
883 data->CurrentPosition.AbsoluteAddress[3] = sc.cdsc_absaddr.msf.frame;
885 data->CurrentPosition.TrackRelativeAddress[0] = 0;
886 data->CurrentPosition.TrackRelativeAddress[1] = sc.cdsc_reladdr.msf.minute;
887 data->CurrentPosition.TrackRelativeAddress[2] = sc.cdsc_reladdr.msf.second;
888 data->CurrentPosition.TrackRelativeAddress[3] = sc.cdsc_reladdr.msf.frame;
890 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
892 else /* not playing */
894 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
895 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
897 RtlLeaveCriticalSection( &cache_section );
898 break;
899 case IOCTL_CDROM_MEDIA_CATALOG:
900 size = sizeof(SUB_Q_MEDIA_CATALOG_NUMBER);
901 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
903 struct cdrom_mcn mcn;
904 if ((io = ioctl(fd, CDROM_GET_MCN, &mcn)) == -1) goto end;
906 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
907 data->MediaCatalog.Mcval = 0; /* FIXME */
908 memcpy(data->MediaCatalog.MediaCatalog, mcn.medium_catalog_number, 14);
909 data->MediaCatalog.MediaCatalog[14] = 0;
911 break;
912 case IOCTL_CDROM_TRACK_ISRC:
913 size = sizeof(SUB_Q_CURRENT_POSITION);
914 FIXME("TrackIsrc: NIY on linux\n");
915 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
916 data->TrackIsrc.Tcval = 0;
917 io = 0;
918 break;
921 end:
922 ret = CDROM_GetStatusCode(io);
923 #elif defined(__FreeBSD__) || defined(__NetBSD__)
924 unsigned size;
925 SUB_Q_HEADER* hdr = (SUB_Q_HEADER*)data;
926 int io;
927 struct ioc_read_subchannel read_sc;
928 struct cd_sub_channel_info sc;
930 read_sc.address_format = CD_MSF_FORMAT;
931 read_sc.track = 0;
932 read_sc.data_len = sizeof(sc);
933 read_sc.data = &sc;
934 switch (fmt->Format)
936 case IOCTL_CDROM_CURRENT_POSITION:
937 read_sc.data_format = CD_CURRENT_POSITION;
938 break;
939 case IOCTL_CDROM_MEDIA_CATALOG:
940 read_sc.data_format = CD_MEDIA_CATALOG;
941 break;
942 case IOCTL_CDROM_TRACK_ISRC:
943 read_sc.data_format = CD_TRACK_INFO;
944 sc.what.track_info.track_number = data->TrackIsrc.Track;
945 break;
947 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
948 if (io == -1)
950 TRACE("opened or no_media (%s)!\n", strerror(errno));
951 CDROM_ClearCacheEntry(dev);
952 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
953 goto end;
956 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
958 switch (sc.header.audio_status) {
959 case CD_AS_AUDIO_INVALID:
960 CDROM_ClearCacheEntry(dev);
961 hdr->AudioStatus = AUDIO_STATUS_NOT_SUPPORTED;
962 break;
963 case CD_AS_NO_STATUS:
964 CDROM_ClearCacheEntry(dev);
965 hdr->AudioStatus = AUDIO_STATUS_NO_STATUS;
966 break;
967 case CD_AS_PLAY_IN_PROGRESS:
968 hdr->AudioStatus = AUDIO_STATUS_IN_PROGRESS;
969 break;
970 case CD_AS_PLAY_PAUSED:
971 hdr->AudioStatus = AUDIO_STATUS_PAUSED;
972 break;
973 case CD_AS_PLAY_COMPLETED:
974 hdr->AudioStatus = AUDIO_STATUS_PLAY_COMPLETE;
975 break;
976 case CD_AS_PLAY_ERROR:
977 hdr->AudioStatus = AUDIO_STATUS_PLAY_ERROR;
978 break;
979 default:
980 TRACE("status=%02X !\n", sc.header.audio_status);
982 switch (fmt->Format)
984 case IOCTL_CDROM_CURRENT_POSITION:
985 size = sizeof(SUB_Q_CURRENT_POSITION);
986 RtlEnterCriticalSection( &cache_section );
987 if (hdr->AudioStatus==AUDIO_STATUS_IN_PROGRESS) {
988 data->CurrentPosition.FormatCode = IOCTL_CDROM_CURRENT_POSITION;
989 data->CurrentPosition.Control = sc.what.position.control;
990 data->CurrentPosition.ADR = sc.what.position.addr_type;
991 data->CurrentPosition.TrackNumber = sc.what.position.track_number;
992 data->CurrentPosition.IndexNumber = sc.what.position.index_number;
994 data->CurrentPosition.AbsoluteAddress[0] = 0;
995 data->CurrentPosition.AbsoluteAddress[1] = sc.what.position.absaddr.msf.minute;
996 data->CurrentPosition.AbsoluteAddress[2] = sc.what.position.absaddr.msf.second;
997 data->CurrentPosition.AbsoluteAddress[3] = sc.what.position.absaddr.msf.frame;
998 data->CurrentPosition.TrackRelativeAddress[0] = 0;
999 data->CurrentPosition.TrackRelativeAddress[1] = sc.what.position.reladdr.msf.minute;
1000 data->CurrentPosition.TrackRelativeAddress[2] = sc.what.position.reladdr.msf.second;
1001 data->CurrentPosition.TrackRelativeAddress[3] = sc.what.position.reladdr.msf.frame;
1002 cdrom_cache[dev].CurrentPosition = data->CurrentPosition;
1004 else { /* not playing */
1005 cdrom_cache[dev].CurrentPosition.Header = *hdr; /* Preserve header info */
1006 data->CurrentPosition = cdrom_cache[dev].CurrentPosition;
1008 RtlLeaveCriticalSection( &cache_section );
1009 break;
1010 case IOCTL_CDROM_MEDIA_CATALOG:
1011 size = sizeof(SUB_Q_MEDIA_CATALOG_NUMBER);
1012 data->MediaCatalog.FormatCode = IOCTL_CDROM_MEDIA_CATALOG;
1013 data->MediaCatalog.Mcval = sc.what.media_catalog.mc_valid;
1014 memcpy(data->MediaCatalog.MediaCatalog, sc.what.media_catalog.mc_number, 15);
1015 break;
1016 case IOCTL_CDROM_TRACK_ISRC:
1017 size = sizeof(SUB_Q_CURRENT_POSITION);
1018 data->TrackIsrc.FormatCode = IOCTL_CDROM_TRACK_ISRC;
1019 data->TrackIsrc.Tcval = sc.what.track_info.ti_valid;
1020 memcpy(data->TrackIsrc.TrackIsrc, sc.what.track_info.ti_number, 15);
1021 break;
1024 end:
1025 ret = CDROM_GetStatusCode(io);
1026 #endif
1027 return ret;
1030 /******************************************************************
1031 * CDROM_Verify
1035 static NTSTATUS CDROM_Verify(int dev, int fd)
1037 /* quick implementation */
1038 CDROM_SUB_Q_DATA_FORMAT fmt;
1039 SUB_Q_CHANNEL_DATA data;
1041 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
1042 return CDROM_ReadQChannel(dev, fd, &fmt, &data) ? 1 : 0;
1045 /******************************************************************
1046 * CDROM_PlayAudioMSF
1050 static NTSTATUS CDROM_PlayAudioMSF(int fd, const CDROM_PLAY_AUDIO_MSF* audio_msf)
1052 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1053 #ifdef linux
1054 struct cdrom_msf msf;
1055 int io;
1057 msf.cdmsf_min0 = audio_msf->StartingM;
1058 msf.cdmsf_sec0 = audio_msf->StartingS;
1059 msf.cdmsf_frame0 = audio_msf->StartingF;
1060 msf.cdmsf_min1 = audio_msf->EndingM;
1061 msf.cdmsf_sec1 = audio_msf->EndingS;
1062 msf.cdmsf_frame1 = audio_msf->EndingF;
1064 io = ioctl(fd, CDROMSTART);
1065 if (io == -1)
1067 WARN("motor doesn't start !\n");
1068 goto end;
1070 io = ioctl(fd, CDROMPLAYMSF, &msf);
1071 if (io == -1)
1073 WARN("device doesn't play !\n");
1074 goto end;
1076 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1077 msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0,
1078 msf.cdmsf_min1, msf.cdmsf_sec1, msf.cdmsf_frame1);
1079 end:
1080 ret = CDROM_GetStatusCode(io);
1081 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1082 struct ioc_play_msf msf;
1083 int io;
1085 msf.start_m = audio_msf->StartingM;
1086 msf.start_s = audio_msf->StartingS;
1087 msf.start_f = audio_msf->StartingF;
1088 msf.end_m = audio_msf->EndingM;
1089 msf.end_s = audio_msf->EndingS;
1090 msf.end_f = audio_msf->EndingF;
1092 io = ioctl(fd, CDIOCSTART, NULL);
1093 if (io == -1)
1095 WARN("motor doesn't start !\n");
1096 goto end;
1098 io = ioctl(fd, CDIOCPLAYMSF, &msf);
1099 if (io == -1)
1101 WARN("device doesn't play !\n");
1102 goto end;
1104 TRACE("msf = %d:%d:%d %d:%d:%d\n",
1105 msf.start_m, msf.start_s, msf.start_f,
1106 msf.end_m, msf.end_s, msf.end_f);
1107 end:
1108 ret = CDROM_GetStatusCode(io);
1109 #endif
1110 return ret;
1113 /******************************************************************
1114 * CDROM_SeekAudioMSF
1118 static NTSTATUS CDROM_SeekAudioMSF(int dev, int fd, const CDROM_SEEK_AUDIO_MSF* audio_msf)
1120 CDROM_TOC toc;
1121 int i, io, frame;
1122 SUB_Q_CURRENT_POSITION *cp;
1123 #if defined(linux)
1124 struct cdrom_msf0 msf;
1125 struct cdrom_subchnl sc;
1126 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1127 struct ioc_play_msf msf;
1128 struct ioc_read_subchannel read_sc;
1129 struct cd_sub_channel_info sc;
1130 int final_frame;
1131 #endif
1133 /* Use the information on the TOC to compute the new current
1134 * position, which is shadowed on the cache. [Portable]. */
1135 frame = FRAME_OF_MSF(*audio_msf);
1137 if ((io = CDROM_ReadTOC(dev, fd, &toc)) != 0) return io;
1139 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
1140 if (FRAME_OF_TOC(toc,i)>frame) break;
1141 if (i <= toc.FirstTrack || i > toc.LastTrack+1)
1142 return STATUS_INVALID_PARAMETER;
1143 i--;
1144 RtlEnterCriticalSection( &cache_section );
1145 cp = &cdrom_cache[dev].CurrentPosition;
1146 cp->FormatCode = IOCTL_CDROM_CURRENT_POSITION;
1147 cp->Control = toc.TrackData[i-toc.FirstTrack].Control;
1148 cp->ADR = toc.TrackData[i-toc.FirstTrack].Adr;
1149 cp->TrackNumber = toc.TrackData[i-toc.FirstTrack].TrackNumber;
1150 cp->IndexNumber = 0; /* FIXME: where do they keep these? */
1151 cp->AbsoluteAddress[0] = 0;
1152 cp->AbsoluteAddress[1] = toc.TrackData[i-toc.FirstTrack].Address[1];
1153 cp->AbsoluteAddress[2] = toc.TrackData[i-toc.FirstTrack].Address[2];
1154 cp->AbsoluteAddress[3] = toc.TrackData[i-toc.FirstTrack].Address[3];
1155 frame -= FRAME_OF_TOC(toc,i);
1156 cp->TrackRelativeAddress[0] = 0;
1157 MSF_OF_FRAME(cp->TrackRelativeAddress[1], frame);
1158 RtlLeaveCriticalSection( &cache_section );
1160 /* If playing, then issue a seek command, otherwise do nothing */
1161 #ifdef linux
1162 sc.cdsc_format = CDROM_MSF;
1164 io = ioctl(fd, CDROMSUBCHNL, &sc);
1165 if (io == -1)
1167 TRACE("opened or no_media (%s)!\n", strerror(errno));
1168 CDROM_ClearCacheEntry(dev);
1169 return CDROM_GetStatusCode(io);
1171 if (sc.cdsc_audiostatus==CDROM_AUDIO_PLAY)
1173 msf.minute = audio_msf->M;
1174 msf.second = audio_msf->S;
1175 msf.frame = audio_msf->F;
1176 return CDROM_GetStatusCode(ioctl(fd, CDROMSEEK, &msf));
1178 return STATUS_SUCCESS;
1179 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1180 read_sc.address_format = CD_MSF_FORMAT;
1181 read_sc.track = 0;
1182 read_sc.data_len = sizeof(sc);
1183 read_sc.data = &sc;
1184 read_sc.data_format = CD_CURRENT_POSITION;
1186 io = ioctl(fd, CDIOCREADSUBCHANNEL, &read_sc);
1187 if (io == -1)
1189 TRACE("opened or no_media (%s)!\n", strerror(errno));
1190 CDROM_ClearCacheEntry(dev);
1191 return CDROM_GetStatusCode(io);
1193 if (sc.header.audio_status==CD_AS_PLAY_IN_PROGRESS)
1196 msf.start_m = audio_msf->M;
1197 msf.start_s = audio_msf->S;
1198 msf.start_f = audio_msf->F;
1199 final_frame = FRAME_OF_TOC(toc,toc.LastTrack+1)-1;
1200 MSF_OF_FRAME(msf.end_m, final_frame);
1202 return CDROM_GetStatusCode(ioctl(fd, CDIOCPLAYMSF, &msf));
1204 return STATUS_SUCCESS;
1205 #else
1206 return STATUS_NOT_SUPPORTED;
1207 #endif
1210 /******************************************************************
1211 * CDROM_PauseAudio
1215 static NTSTATUS CDROM_PauseAudio(int fd)
1217 #if defined(linux)
1218 return CDROM_GetStatusCode(ioctl(fd, CDROMPAUSE));
1219 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1220 return CDROM_GetStatusCode(ioctl(fd, CDIOCPAUSE, NULL));
1221 #else
1222 return STATUS_NOT_SUPPORTED;
1223 #endif
1226 /******************************************************************
1227 * CDROM_ResumeAudio
1231 static NTSTATUS CDROM_ResumeAudio(int fd)
1233 #if defined(linux)
1234 return CDROM_GetStatusCode(ioctl(fd, CDROMRESUME));
1235 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1236 return CDROM_GetStatusCode(ioctl(fd, CDIOCRESUME, NULL));
1237 #else
1238 return STATUS_NOT_SUPPORTED;
1239 #endif
1242 /******************************************************************
1243 * CDROM_StopAudio
1247 static NTSTATUS CDROM_StopAudio(int fd)
1249 #if defined(linux)
1250 return CDROM_GetStatusCode(ioctl(fd, CDROMSTOP));
1251 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1252 return CDROM_GetStatusCode(ioctl(fd, CDIOCSTOP, NULL));
1253 #else
1254 return STATUS_NOT_SUPPORTED;
1255 #endif
1258 /******************************************************************
1259 * CDROM_GetVolume
1263 static NTSTATUS CDROM_GetVolume(int fd, VOLUME_CONTROL* vc)
1265 #if defined(linux)
1266 struct cdrom_volctrl volc;
1267 int io;
1269 io = ioctl(fd, CDROMVOLREAD, &volc);
1270 if (io != -1)
1272 vc->PortVolume[0] = volc.channel0;
1273 vc->PortVolume[1] = volc.channel1;
1274 vc->PortVolume[2] = volc.channel2;
1275 vc->PortVolume[3] = volc.channel3;
1277 return CDROM_GetStatusCode(io);
1278 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1279 struct ioc_vol volc;
1280 int io;
1282 io = ioctl(fd, CDIOCGETVOL, &volc);
1283 if (io != -1)
1285 vc->PortVolume[0] = volc.vol[0];
1286 vc->PortVolume[1] = volc.vol[1];
1287 vc->PortVolume[2] = volc.vol[2];
1288 vc->PortVolume[3] = volc.vol[3];
1290 return CDROM_GetStatusCode(io);
1291 #else
1292 return STATUS_NOT_SUPPORTED;
1293 #endif
1296 /******************************************************************
1297 * CDROM_SetVolume
1301 static NTSTATUS CDROM_SetVolume(int fd, const VOLUME_CONTROL* vc)
1303 #if defined(linux)
1304 struct cdrom_volctrl volc;
1306 volc.channel0 = vc->PortVolume[0];
1307 volc.channel1 = vc->PortVolume[1];
1308 volc.channel2 = vc->PortVolume[2];
1309 volc.channel3 = vc->PortVolume[3];
1311 return CDROM_GetStatusCode(ioctl(fd, CDROMVOLCTRL, &volc));
1312 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1313 struct ioc_vol volc;
1315 volc.vol[0] = vc->PortVolume[0];
1316 volc.vol[1] = vc->PortVolume[1];
1317 volc.vol[2] = vc->PortVolume[2];
1318 volc.vol[3] = vc->PortVolume[3];
1320 return CDROM_GetStatusCode(ioctl(fd, CDIOCSETVOL, &volc));
1321 #else
1322 return STATUS_NOT_SUPPORTED;
1323 #endif
1326 /******************************************************************
1327 * CDROM_RawRead
1331 static NTSTATUS CDROM_RawRead(int fd, const RAW_READ_INFO* raw, void* buffer, DWORD len, DWORD* sz)
1333 int ret = STATUS_NOT_SUPPORTED;
1334 int io = -1;
1335 DWORD sectSize;
1337 switch (raw->TrackMode)
1339 case YellowMode2: sectSize = 2336; break;
1340 case XAForm2: sectSize = 2328; break;
1341 case CDDA: sectSize = 2352; break;
1342 default: return STATUS_INVALID_PARAMETER;
1344 if (len < raw->SectorCount * sectSize) return STATUS_BUFFER_TOO_SMALL;
1345 /* strangely enough, it seems that sector offsets are always indicated with a size of 2048,
1346 * even if a larger size if read...
1348 #if defined(linux)
1350 struct cdrom_read_audio cdra;
1351 struct cdrom_msf* msf;
1352 int i;
1353 LONGLONG t = ((LONGLONG)raw->DiskOffset.u.HighPart << 32) +
1354 raw->DiskOffset.u.LowPart + CD_MSF_OFFSET;
1356 switch (raw->TrackMode)
1358 case YellowMode2:
1359 /* Linux reads only one sector at a time.
1360 * ioctl CDROMREADMODE2 takes struct cdrom_msf as an argument
1361 * on the contrary to what header comments state.
1363 for (i = 0; i < raw->SectorCount; i++, t += sectSize)
1365 msf = (struct cdrom_msf*)buffer + i * sectSize;
1366 msf->cdmsf_min0 = t / CD_FRAMES / CD_SECS;
1367 msf->cdmsf_sec0 = t / CD_FRAMES % CD_SECS;
1368 msf->cdmsf_frame0 = t % CD_FRAMES;
1369 io = ioctl(fd, CDROMREADMODE2, msf);
1370 if (io != 0)
1372 *sz = sectSize * i;
1373 return CDROM_GetStatusCode(io);
1376 break;
1377 case XAForm2:
1378 FIXME("XAForm2: NIY\n");
1379 return ret;
1380 case CDDA:
1381 /* FIXME: the output doesn't seem 100% correct... in fact output is shifted
1382 * between by NT2K box and this... should check on the same drive...
1383 * otherwise, I fear a 2352/2368 mismatch somewhere in one of the drivers
1384 * (linux/NT).
1385 * Anyway, that's not critical at all. We're talking of 16/32 bytes, we're
1386 * talking of 0.2 ms of sound
1388 /* 2048 = 2 ** 11 */
1389 if (raw->DiskOffset.u.HighPart & ~2047) FIXME("Unsupported value\n");
1390 cdra.addr.lba = ((raw->DiskOffset.u.LowPart >> 11) |
1391 (raw->DiskOffset.u.HighPart << (32 - 11))) - 1;
1392 FIXME("reading at %u\n", cdra.addr.lba);
1393 cdra.addr_format = CDROM_LBA;
1394 cdra.nframes = raw->SectorCount;
1395 cdra.buf = buffer;
1396 io = ioctl(fd, CDROMREADAUDIO, &cdra);
1397 break;
1398 default:
1399 FIXME("NIY: %d\n", raw->TrackMode);
1400 return ret;
1403 #else
1405 switch (raw->TrackMode)
1407 case YellowMode2:
1408 FIXME("YellowMode2: NIY\n");
1409 return ret;
1410 case XAForm2:
1411 FIXME("XAForm2: NIY\n");
1412 return ret;
1413 case CDDA:
1414 FIXME("CDDA: NIY\n");
1415 return ret;
1418 #endif
1420 *sz = sectSize * raw->SectorCount;
1421 ret = CDROM_GetStatusCode(io);
1422 return ret;
1425 /******************************************************************
1426 * CDROM_ScsiPassThroughDirect
1427 * Implements IOCTL_SCSI_PASS_THROUGH_DIRECT
1430 static NTSTATUS CDROM_ScsiPassThroughDirect(int fd, PSCSI_PASS_THROUGH_DIRECT pPacket)
1432 int ret = STATUS_NOT_SUPPORTED;
1433 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1434 sg_io_hdr_t cmd;
1435 int io;
1436 #elif defined HAVE_SCSIREQ_T_CMD
1437 scsireq_t cmd;
1438 int io;
1439 #endif
1441 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH_DIRECT))
1442 return STATUS_BUFFER_TOO_SMALL;
1444 if (pPacket->CdbLength > 16)
1445 return STATUS_INVALID_PARAMETER;
1447 #ifdef SENSEBUFLEN
1448 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1449 return STATUS_INVALID_PARAMETER;
1450 #elif defined HAVE_REQUEST_SENSE
1451 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1452 return STATUS_INVALID_PARAMETER;
1453 #endif
1455 if (pPacket->DataTransferLength > 0 && !pPacket->DataBuffer)
1456 return STATUS_INVALID_PARAMETER;
1458 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1459 RtlZeroMemory(&cmd, sizeof(cmd));
1461 cmd.interface_id = 'S';
1462 cmd.cmd_len = pPacket->CdbLength;
1463 cmd.mx_sb_len = pPacket->SenseInfoLength;
1464 cmd.dxfer_len = pPacket->DataTransferLength;
1465 cmd.dxferp = pPacket->DataBuffer;
1466 cmd.cmdp = pPacket->Cdb;
1467 cmd.sbp = (char*)pPacket + pPacket->SenseInfoOffset;
1468 cmd.timeout = pPacket->TimeOutValue*1000;
1470 switch (pPacket->DataIn)
1472 case SCSI_IOCTL_DATA_IN:
1473 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1474 break;
1475 case SCSI_IOCTL_DATA_OUT:
1476 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1477 break;
1478 case SCSI_IOCTL_DATA_UNSPECIFIED:
1479 cmd.dxfer_direction = SG_DXFER_NONE;
1480 break;
1481 default:
1482 return STATUS_INVALID_PARAMETER;
1485 io = ioctl(fd, SG_IO, &cmd);
1487 pPacket->ScsiStatus = cmd.status;
1488 pPacket->DataTransferLength = cmd.resid;
1489 pPacket->SenseInfoLength = cmd.sb_len_wr;
1491 ret = CDROM_GetStatusCode(io);
1493 #elif defined HAVE_SCSIREQ_T_CMD
1495 memset(&cmd, 0, sizeof(cmd));
1496 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1498 cmd.cmdlen = pPacket->CdbLength;
1499 cmd.databuf = pPacket->DataBuffer;
1500 cmd.datalen = pPacket->DataTransferLength;
1501 cmd.senselen = pPacket->SenseInfoLength;
1502 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1504 switch (pPacket->DataIn)
1506 case SCSI_IOCTL_DATA_OUT:
1507 cmd.flags |= SCCMD_WRITE;
1508 break;
1509 case SCSI_IOCTL_DATA_IN:
1510 cmd.flags |= SCCMD_READ;
1511 break;
1512 case SCSI_IOCTL_DATA_UNSPECIFIED:
1513 cmd.flags = 0;
1514 break;
1515 default:
1516 return STATUS_INVALID_PARAMETER;
1519 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1521 switch (cmd.retsts)
1523 case SCCMD_OK: break;
1524 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1525 break;
1526 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1527 break;
1528 case SCCMD_SENSE: break;
1529 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1530 break;
1533 if (pPacket->SenseInfoLength != 0)
1535 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1536 cmd.sense, pPacket->SenseInfoLength);
1539 pPacket->ScsiStatus = cmd.status;
1541 ret = CDROM_GetStatusCode(io);
1542 #endif
1543 return ret;
1546 /******************************************************************
1547 * CDROM_ScsiPassThrough
1548 * Implements IOCTL_SCSI_PASS_THROUGH
1551 static NTSTATUS CDROM_ScsiPassThrough(int fd, PSCSI_PASS_THROUGH pPacket)
1553 int ret = STATUS_NOT_SUPPORTED;
1554 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1555 sg_io_hdr_t cmd;
1556 int io;
1557 #elif defined HAVE_SCSIREQ_T_CMD
1558 scsireq_t cmd;
1559 int io;
1560 #endif
1562 if (pPacket->Length < sizeof(SCSI_PASS_THROUGH))
1563 return STATUS_BUFFER_TOO_SMALL;
1565 if (pPacket->CdbLength > 16)
1566 return STATUS_INVALID_PARAMETER;
1568 #ifdef SENSEBUFLEN
1569 if (pPacket->SenseInfoLength > SENSEBUFLEN)
1570 return STATUS_INVALID_PARAMETER;
1571 #elif defined HAVE_REQUEST_SENSE
1572 if (pPacket->SenseInfoLength > sizeof(struct request_sense))
1573 return STATUS_INVALID_PARAMETER;
1574 #endif
1576 if (pPacket->DataTransferLength > 0 && pPacket->DataBufferOffset < sizeof(SCSI_PASS_THROUGH))
1577 return STATUS_INVALID_PARAMETER;
1579 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1580 RtlZeroMemory(&cmd, sizeof(cmd));
1582 cmd.interface_id = 'S';
1583 cmd.dxfer_len = pPacket->DataTransferLength;
1584 cmd.dxferp = (char*)pPacket + pPacket->DataBufferOffset;
1585 cmd.cmd_len = pPacket->CdbLength;
1586 cmd.cmdp = pPacket->Cdb;
1587 cmd.mx_sb_len = pPacket->SenseInfoLength;
1588 cmd.timeout = pPacket->TimeOutValue*1000;
1590 if(cmd.mx_sb_len > 0)
1591 cmd.sbp = (char*)pPacket + pPacket->SenseInfoOffset;
1593 switch (pPacket->DataIn)
1595 case SCSI_IOCTL_DATA_IN:
1596 cmd.dxfer_direction = SG_DXFER_FROM_DEV;
1597 break;
1598 case SCSI_IOCTL_DATA_OUT:
1599 cmd.dxfer_direction = SG_DXFER_TO_DEV;
1600 break;
1601 case SCSI_IOCTL_DATA_UNSPECIFIED:
1602 cmd.dxfer_direction = SG_DXFER_NONE;
1603 break;
1604 default:
1605 return STATUS_INVALID_PARAMETER;
1608 io = ioctl(fd, SG_IO, &cmd);
1610 pPacket->ScsiStatus = cmd.status;
1611 pPacket->DataTransferLength = cmd.resid;
1612 pPacket->SenseInfoLength = cmd.sb_len_wr;
1614 ret = CDROM_GetStatusCode(io);
1616 #elif defined HAVE_SCSIREQ_T_CMD
1618 memset(&cmd, 0, sizeof(cmd));
1619 memcpy(&(cmd.cmd), &(pPacket->Cdb), pPacket->CdbLength);
1621 if ( pPacket->DataBufferOffset > 0x1000 )
1623 cmd.databuf = (void*)pPacket->DataBufferOffset;
1625 else
1627 cmd.databuf = (char*)pPacket + pPacket->DataBufferOffset;
1630 cmd.cmdlen = pPacket->CdbLength;
1631 cmd.datalen = pPacket->DataTransferLength;
1632 cmd.senselen = pPacket->SenseInfoLength;
1633 cmd.timeout = pPacket->TimeOutValue*1000; /* in milliseconds */
1635 switch (pPacket->DataIn)
1637 case SCSI_IOCTL_DATA_OUT:
1638 cmd.flags |= SCCMD_WRITE;
1639 break;
1640 case SCSI_IOCTL_DATA_IN:
1641 cmd.flags |= SCCMD_READ;
1642 break;
1643 case SCSI_IOCTL_DATA_UNSPECIFIED:
1644 cmd.flags = 0;
1645 break;
1646 default:
1647 return STATUS_INVALID_PARAMETER;
1650 io = ioctl(fd, SCIOCCOMMAND, &cmd);
1652 switch (cmd.retsts)
1654 case SCCMD_OK: break;
1655 case SCCMD_TIMEOUT: return STATUS_TIMEOUT;
1656 break;
1657 case SCCMD_BUSY: return STATUS_DEVICE_BUSY;
1658 break;
1659 case SCCMD_SENSE: break;
1660 case SCCMD_UNKNOWN: return STATUS_UNSUCCESSFUL;
1661 break;
1664 if (pPacket->SenseInfoLength != 0)
1666 memcpy((char*)pPacket + pPacket->SenseInfoOffset,
1667 cmd.sense, pPacket->SenseInfoLength);
1670 pPacket->ScsiStatus = cmd.status;
1672 ret = CDROM_GetStatusCode(io);
1673 #endif
1674 return ret;
1677 /******************************************************************
1678 * CDROM_ScsiGetCaps
1682 static NTSTATUS CDROM_ScsiGetCaps(PIO_SCSI_CAPABILITIES caps)
1684 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
1686 caps->Length = sizeof(*caps);
1687 #ifdef SG_SCATTER_SZ
1688 caps->MaximumTransferLength = SG_SCATTER_SZ; /* FIXME */
1689 caps->MaximumPhysicalPages = SG_SCATTER_SZ / getpagesize();
1690 caps->SupportedAsynchronousEvents = TRUE;
1691 caps->AlignmentMask = getpagesize();
1692 caps->TaggedQueuing = FALSE; /* we could check that it works and answer TRUE */
1693 caps->AdapterScansDown = FALSE; /* FIXME ? */
1694 caps->AdapterUsesPio = FALSE; /* FIXME ? */
1695 ret = STATUS_SUCCESS;
1696 #else
1697 FIXME("Unimplemented\n");
1698 #endif
1699 return ret;
1702 /******************************************************************
1703 * CDROM_GetAddress
1705 * implements IOCTL_SCSI_GET_ADDRESS
1707 static NTSTATUS CDROM_GetAddress(int fd, SCSI_ADDRESS* address)
1709 UCHAR portnum, busid, targetid, lun;
1711 address->Length = sizeof(SCSI_ADDRESS);
1712 if ( ! CDROM_GetInterfaceInfo(fd, &portnum, &busid, &targetid, &lun))
1713 return STATUS_NOT_SUPPORTED;
1715 address->PortNumber = portnum; /* primary=0 secondary=1 for ide */
1716 address->PathId = busid; /* always 0 for ide */
1717 address->TargetId = targetid; /* master=0 slave=1 for ide */
1718 address->Lun = lun;
1719 return STATUS_SUCCESS;
1722 /******************************************************************
1723 * DVD_StartSession
1727 static NTSTATUS DVD_StartSession(int fd, PDVD_SESSION_ID sid_in, PDVD_SESSION_ID sid_out)
1729 #if defined(linux)
1730 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1731 dvd_authinfo auth_info;
1733 memset( &auth_info, 0, sizeof( auth_info ) );
1734 auth_info.type = DVD_LU_SEND_AGID;
1735 if (sid_in) auth_info.lsa.agid = *(int*)sid_in; /* ?*/
1737 TRACE("fd 0x%08x\n",fd);
1738 ret =CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
1739 *sid_out = auth_info.lsa.agid;
1740 return ret;
1741 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1742 return STATUS_NOT_SUPPORTED;
1743 #else
1744 return STATUS_NOT_SUPPORTED;
1745 #endif
1748 /******************************************************************
1749 * DVD_EndSession
1753 static NTSTATUS DVD_EndSession(int fd, PDVD_SESSION_ID sid)
1755 #if defined(linux)
1756 dvd_authinfo auth_info;
1758 memset( &auth_info, 0, sizeof( auth_info ) );
1759 auth_info.type = DVD_INVALIDATE_AGID;
1760 auth_info.lsa.agid = *(int*)sid;
1762 TRACE("\n");
1763 return CDROM_GetStatusCode(ioctl(fd, DVD_AUTH, &auth_info));
1764 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1765 return STATUS_NOT_SUPPORTED;
1766 #else
1767 return STATUS_NOT_SUPPORTED;
1768 #endif
1771 /******************************************************************
1772 * DVD_SendKey
1776 static NTSTATUS DVD_SendKey(int fd, PDVD_COPY_PROTECT_KEY key)
1778 #if defined(linux)
1779 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1780 dvd_authinfo auth_info;
1782 memset( &auth_info, 0, sizeof( auth_info ) );
1783 switch (key->KeyType)
1785 case DvdChallengeKey:
1786 auth_info.type = DVD_HOST_SEND_CHALLENGE;
1787 auth_info.hsc.agid = (int)key->SessionId;
1788 TRACE("DvdChallengeKey ioc 0x%x\n", DVD_AUTH );
1789 memcpy( auth_info.hsc.chal, key->KeyData, DVD_CHALLENGE_SIZE );
1790 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1791 break;
1792 case DvdBusKey2:
1793 auth_info.type = DVD_HOST_SEND_KEY2;
1794 auth_info.hsk.agid = (int)key->SessionId;
1796 memcpy( auth_info.hsk.key, key->KeyData, DVD_KEY_SIZE );
1798 TRACE("DvdBusKey2\n");
1799 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1800 break;
1802 default:
1803 FIXME("Unknown Keytype 0x%x\n",key->KeyType);
1805 return ret;
1806 #else
1807 FIXME("unsupported on this platform\n");
1808 return STATUS_NOT_SUPPORTED;
1809 #endif
1812 /******************************************************************
1813 * DVD_ReadKey
1817 static NTSTATUS DVD_ReadKey(int fd, PDVD_COPY_PROTECT_KEY key)
1819 #if defined(linux)
1820 NTSTATUS ret = STATUS_NOT_SUPPORTED;
1821 dvd_struct dvd;
1822 dvd_authinfo auth_info;
1824 memset( &dvd, 0, sizeof( dvd_struct ) );
1825 memset( &auth_info, 0, sizeof( auth_info ) );
1826 switch (key->KeyType)
1828 case DvdDiskKey:
1830 dvd.type = DVD_STRUCT_DISCKEY;
1831 dvd.disckey.agid = (int)key->SessionId;
1832 memset( dvd.disckey.value, 0, DVD_DISCKEY_SIZE );
1834 TRACE("DvdDiskKey\n");
1835 ret = CDROM_GetStatusCode(ioctl( fd, DVD_READ_STRUCT, &dvd ));
1836 if (ret == STATUS_SUCCESS)
1837 memcpy(key->KeyData,dvd.disckey.value,DVD_DISCKEY_SIZE);
1838 break;
1839 case DvdTitleKey:
1840 auth_info.type = DVD_LU_SEND_TITLE_KEY;
1841 auth_info.lstk.agid = (int)key->SessionId;
1842 auth_info.lstk.lba = (int)(key->Parameters.TitleOffset.QuadPart>>11);
1843 TRACE("DvdTitleKey session %d Quadpart 0x%08lx offset 0x%08x\n",
1844 (int)key->SessionId, (long)key->Parameters.TitleOffset.QuadPart,
1845 auth_info.lstk.lba);
1846 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1847 if (ret == STATUS_SUCCESS)
1848 memcpy(key->KeyData, auth_info.lstk.title_key, DVD_KEY_SIZE );
1849 break;
1850 case DvdChallengeKey:
1852 auth_info.type = DVD_LU_SEND_CHALLENGE;
1853 auth_info.lsc.agid = (int)key->SessionId;
1855 TRACE("DvdChallengeKey\n");
1856 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1857 if (ret == STATUS_SUCCESS)
1858 memcpy( key->KeyData, auth_info.lsc.chal, DVD_CHALLENGE_SIZE );
1859 break;
1860 case DvdAsf:
1861 auth_info.type = DVD_LU_SEND_ASF;
1862 TRACE("DvdAsf\n");
1863 auth_info.lsasf.asf=((PDVD_ASF)key->KeyData)->SuccessFlag;
1864 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1865 ((PDVD_ASF)key->KeyData)->SuccessFlag = auth_info.lsasf.asf;
1866 break;
1867 case DvdBusKey1:
1868 auth_info.type = DVD_LU_SEND_KEY1;
1869 auth_info.lsk.agid = (int)key->SessionId;
1871 TRACE("DvdBusKey1\n");
1872 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1874 if (ret == STATUS_SUCCESS)
1875 memcpy( key->KeyData, auth_info.lsk.key, DVD_KEY_SIZE );
1876 break;
1877 case DvdGetRpcKey:
1878 auth_info.type = DVD_LU_SEND_RPC_STATE;
1880 TRACE("DvdGetRpcKey\n");
1881 ret = CDROM_GetStatusCode(ioctl( fd, DVD_AUTH, &auth_info ));
1883 if (ret == STATUS_SUCCESS)
1885 ((PDVD_RPC_KEY)key->KeyData)->TypeCode = auth_info.lrpcs.type;
1886 ((PDVD_RPC_KEY)key->KeyData)->RegionMask = auth_info.lrpcs.region_mask;
1887 ((PDVD_RPC_KEY)key->KeyData)->RpcScheme = auth_info.lrpcs.rpc_scheme;
1889 break;
1890 default:
1891 FIXME("Unknown keytype 0x%x\n",key->KeyType);
1893 return ret;
1894 #elif defined(__FreeBSD__) || defined(__NetBSD__)
1895 TRACE("bsd\n");
1896 return STATUS_NOT_SUPPORTED;
1897 #else
1898 TRACE("outside\n");
1899 return STATUS_NOT_SUPPORTED;
1900 #endif
1901 TRACE("not reached\n");
1904 /******************************************************************
1905 * DVD_GetRegion
1909 static NTSTATUS DVD_GetRegion(int dev, PDVD_REGION region)
1911 FIXME("\n");
1912 return STATUS_SUCCESS;
1916 /******************************************************************
1917 * DVD_GetRegion
1921 static NTSTATUS DVD_ReadStructure(int dev, PDVD_READ_STRUCTURE structure, PDVD_LAYER_DESCRIPTOR layer)
1923 FIXME("\n");
1924 return STATUS_SUCCESS;
1928 /******************************************************************
1929 * GetInquiryData
1930 * Implements the IOCTL_GET_INQUIRY_DATA ioctl.
1931 * Returns Inquiry data for all devices on the specified scsi bus
1932 * Returns STATUS_BUFFER_TOO_SMALL if the output buffer is to small,
1933 * STATUS_INVALID_DEVICE_REQUEST if the given handle isn't to a SCSI device,
1934 * or STATUS_NOT_SUPPORTED if the OS driver is too old
1936 static NTSTATUS GetInquiryData(int fd, PSCSI_ADAPTER_BUS_INFO BufferOut, DWORD OutBufferSize)
1938 #ifdef HAVE_SG_IO_HDR_T_INTERFACE_ID
1939 PSCSI_INQUIRY_DATA pInquiryData = NULL;
1940 UCHAR sense_buffer[32];
1941 int iochk, version;
1942 sg_io_hdr_t iocmd;
1943 UCHAR inquiry[INQ_CMD_LEN] = {INQUIRY, 0, 0, 0, INQ_REPLY_LEN, 0};
1945 /* Check we have a SCSI device and a supported driver */
1946 if(ioctl(fd, SG_GET_VERSION_NUM, &version) != 0)
1948 WARN("IOCTL_SCSI_GET_INQUIRY_DATA sg driver is not loaded\n");
1949 return STATUS_INVALID_DEVICE_REQUEST;
1951 if(version < 30000 )
1952 return STATUS_NOT_SUPPORTED;
1954 /* FIXME: Enumerate devices on the bus */
1955 BufferOut->NumberOfBuses = 1;
1956 BufferOut->BusData[0].NumberOfLogicalUnits = 1;
1957 BufferOut->BusData[0].InquiryDataOffset = sizeof(SCSI_ADAPTER_BUS_INFO);
1959 pInquiryData = (PSCSI_INQUIRY_DATA)(BufferOut + 1);
1961 RtlZeroMemory(&iocmd, sizeof(iocmd));
1962 iocmd.interface_id = 'S';
1963 iocmd.cmd_len = sizeof(inquiry);
1964 iocmd.mx_sb_len = sizeof(sense_buffer);
1965 iocmd.dxfer_direction = SG_DXFER_FROM_DEV;
1966 iocmd.dxfer_len = INQ_REPLY_LEN;
1967 iocmd.dxferp = pInquiryData->InquiryData;
1968 iocmd.cmdp = inquiry;
1969 iocmd.sbp = sense_buffer;
1970 iocmd.timeout = 1000;
1972 iochk = ioctl(fd, SG_IO, &iocmd);
1973 if(iochk != 0)
1974 WARN("ioctl SG_IO returned %d, error (%s)\n", iochk, strerror(errno));
1976 CDROM_GetInterfaceInfo(fd, &BufferOut->BusData[0].InitiatorBusId, &pInquiryData->PathId, &pInquiryData->TargetId, &pInquiryData->Lun);
1977 pInquiryData->DeviceClaimed = TRUE;
1978 pInquiryData->InquiryDataLength = INQ_REPLY_LEN;
1979 pInquiryData->NextInquiryDataOffset = 0;
1980 return STATUS_SUCCESS;
1981 #else
1982 FIXME("not implemented for nonlinux\n");
1983 return STATUS_NOT_SUPPORTED;
1984 #endif
1987 /******************************************************************
1988 * CDROM_DeviceIoControl
1992 NTSTATUS CDROM_DeviceIoControl(HANDLE hDevice,
1993 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1994 PVOID UserApcContext,
1995 PIO_STATUS_BLOCK piosb,
1996 ULONG dwIoControlCode,
1997 LPVOID lpInBuffer, DWORD nInBufferSize,
1998 LPVOID lpOutBuffer, DWORD nOutBufferSize)
2000 DWORD sz = 0;
2001 NTSTATUS status = STATUS_SUCCESS;
2002 int fd, dev;
2004 TRACE("%lx %s %lx %ld %lx %ld %p\n",
2005 (DWORD)hDevice, iocodex(dwIoControlCode), (DWORD)lpInBuffer, nInBufferSize,
2006 (DWORD)lpOutBuffer, nOutBufferSize, piosb);
2008 piosb->Information = 0;
2010 if ((status = wine_server_handle_to_fd( hDevice, 0, &fd, NULL ))) goto error;
2011 if ((status = CDROM_Open(fd, &dev)))
2013 wine_server_release_fd( hDevice, fd );
2014 goto error;
2017 switch (dwIoControlCode)
2019 case IOCTL_STORAGE_CHECK_VERIFY:
2020 case IOCTL_CDROM_CHECK_VERIFY:
2021 sz = 0;
2022 CDROM_ClearCacheEntry(dev);
2023 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2024 status = STATUS_INVALID_PARAMETER;
2025 else status = CDROM_Verify(dev, fd);
2026 break;
2028 /* EPP case IOCTL_STORAGE_CHECK_VERIFY2: */
2030 /* EPP case IOCTL_STORAGE_FIND_NEW_DEVICES: */
2031 /* EPP case IOCTL_CDROM_FIND_NEW_DEVICES: */
2033 case IOCTL_STORAGE_LOAD_MEDIA:
2034 case IOCTL_CDROM_LOAD_MEDIA:
2035 sz = 0;
2036 CDROM_ClearCacheEntry(dev);
2037 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2038 status = STATUS_INVALID_PARAMETER;
2039 else status = CDROM_SetTray(fd, FALSE);
2040 break;
2041 case IOCTL_STORAGE_EJECT_MEDIA:
2042 sz = 0;
2043 CDROM_ClearCacheEntry(dev);
2044 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2045 status = STATUS_INVALID_PARAMETER;
2046 else status = CDROM_SetTray(fd, TRUE);
2047 break;
2049 case IOCTL_CDROM_MEDIA_REMOVAL:
2050 case IOCTL_DISK_MEDIA_REMOVAL:
2051 case IOCTL_STORAGE_MEDIA_REMOVAL:
2052 case IOCTL_STORAGE_EJECTION_CONTROL:
2053 /* FIXME the last ioctl:s is not the same as the two others...
2054 * lockcount/owner should be handled */
2055 sz = 0;
2056 CDROM_ClearCacheEntry(dev);
2057 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2058 else if (nInBufferSize < sizeof(PREVENT_MEDIA_REMOVAL)) status = STATUS_BUFFER_TOO_SMALL;
2059 else status = CDROM_ControlEjection(fd, (const PREVENT_MEDIA_REMOVAL*)lpInBuffer);
2060 break;
2062 /* EPP case IOCTL_STORAGE_GET_MEDIA_TYPES: */
2064 case IOCTL_STORAGE_GET_DEVICE_NUMBER:
2065 sz = sizeof(STORAGE_DEVICE_NUMBER);
2066 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2067 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2068 else status = CDROM_GetDeviceNumber(dev, (STORAGE_DEVICE_NUMBER*)lpOutBuffer);
2069 break;
2071 case IOCTL_STORAGE_RESET_DEVICE:
2072 sz = 0;
2073 CDROM_ClearCacheEntry(dev);
2074 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2075 status = STATUS_INVALID_PARAMETER;
2076 else status = CDROM_ResetAudio(fd);
2077 break;
2079 case IOCTL_CDROM_GET_CONTROL:
2080 sz = sizeof(CDROM_AUDIO_CONTROL);
2081 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2082 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2083 else status = CDROM_GetControl(dev, (CDROM_AUDIO_CONTROL*)lpOutBuffer);
2084 break;
2086 case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
2087 sz = sizeof(DISK_GEOMETRY);
2088 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2089 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2090 else status = CDROM_GetDriveGeometry(dev, fd, (DISK_GEOMETRY*)lpOutBuffer);
2091 break;
2093 case IOCTL_CDROM_DISK_TYPE:
2094 sz = sizeof(CDROM_DISK_DATA);
2095 /* CDROM_ClearCacheEntry(dev); */
2096 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2097 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2098 else status = CDROM_GetDiskData(dev, fd, (CDROM_DISK_DATA*)lpOutBuffer);
2099 break;
2101 /* EPP case IOCTL_CDROM_GET_LAST_SESSION: */
2103 case IOCTL_CDROM_READ_Q_CHANNEL:
2104 sz = sizeof(SUB_Q_CHANNEL_DATA);
2105 if (lpInBuffer == NULL || nInBufferSize < sizeof(CDROM_SUB_Q_DATA_FORMAT))
2106 status = STATUS_INVALID_PARAMETER;
2107 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2108 else status = CDROM_ReadQChannel(dev, fd, (const CDROM_SUB_Q_DATA_FORMAT*)lpInBuffer,
2109 (SUB_Q_CHANNEL_DATA*)lpOutBuffer);
2110 break;
2112 case IOCTL_CDROM_READ_TOC:
2113 sz = sizeof(CDROM_TOC);
2114 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2115 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2116 else status = CDROM_ReadTOC(dev, fd, (CDROM_TOC*)lpOutBuffer);
2117 break;
2119 /* EPP case IOCTL_CDROM_READ_TOC_EX: */
2121 case IOCTL_CDROM_PAUSE_AUDIO:
2122 sz = 0;
2123 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2124 status = STATUS_INVALID_PARAMETER;
2125 else status = CDROM_PauseAudio(fd);
2126 break;
2127 case IOCTL_CDROM_PLAY_AUDIO_MSF:
2128 sz = 0;
2129 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2130 else if (nInBufferSize < sizeof(CDROM_PLAY_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
2131 else status = CDROM_PlayAudioMSF(fd, (const CDROM_PLAY_AUDIO_MSF*)lpInBuffer);
2132 break;
2133 case IOCTL_CDROM_RESUME_AUDIO:
2134 sz = 0;
2135 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2136 status = STATUS_INVALID_PARAMETER;
2137 else status = CDROM_ResumeAudio(fd);
2138 break;
2139 case IOCTL_CDROM_SEEK_AUDIO_MSF:
2140 sz = 0;
2141 if (lpOutBuffer != NULL || nOutBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2142 else if (nInBufferSize < sizeof(CDROM_SEEK_AUDIO_MSF)) status = STATUS_BUFFER_TOO_SMALL;
2143 else status = CDROM_SeekAudioMSF(dev, fd, (const CDROM_SEEK_AUDIO_MSF*)lpInBuffer);
2144 break;
2145 case IOCTL_CDROM_STOP_AUDIO:
2146 sz = 0;
2147 CDROM_ClearCacheEntry(dev); /* Maybe intention is to change media */
2148 if (lpInBuffer != NULL || nInBufferSize != 0 || lpOutBuffer != NULL || nOutBufferSize != 0)
2149 status = STATUS_INVALID_PARAMETER;
2150 else status = CDROM_StopAudio(fd);
2151 break;
2152 case IOCTL_CDROM_GET_VOLUME:
2153 sz = sizeof(VOLUME_CONTROL);
2154 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2155 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2156 else status = CDROM_GetVolume(fd, (VOLUME_CONTROL*)lpOutBuffer);
2157 break;
2158 case IOCTL_CDROM_SET_VOLUME:
2159 sz = 0;
2160 CDROM_ClearCacheEntry(dev);
2161 if (lpInBuffer == NULL || nInBufferSize < sizeof(VOLUME_CONTROL) || lpOutBuffer != NULL)
2162 status = STATUS_INVALID_PARAMETER;
2163 else status = CDROM_SetVolume(fd, (const VOLUME_CONTROL*)lpInBuffer);
2164 break;
2165 case IOCTL_CDROM_RAW_READ:
2166 sz = 0;
2167 if (nInBufferSize < sizeof(RAW_READ_INFO)) status = STATUS_INVALID_PARAMETER;
2168 else if (lpOutBuffer == NULL) status = STATUS_BUFFER_TOO_SMALL;
2169 else status = CDROM_RawRead(fd, (const RAW_READ_INFO*)lpInBuffer,
2170 lpOutBuffer, nOutBufferSize, &sz);
2171 break;
2172 case IOCTL_SCSI_GET_ADDRESS:
2173 sz = sizeof(SCSI_ADDRESS);
2174 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2175 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2176 else status = CDROM_GetAddress(fd, (SCSI_ADDRESS*)lpOutBuffer);
2177 break;
2178 case IOCTL_SCSI_PASS_THROUGH_DIRECT:
2179 sz = sizeof(SCSI_PASS_THROUGH_DIRECT);
2180 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2181 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH_DIRECT)) status = STATUS_BUFFER_TOO_SMALL;
2182 else status = CDROM_ScsiPassThroughDirect(fd, (PSCSI_PASS_THROUGH_DIRECT)lpOutBuffer);
2183 break;
2184 case IOCTL_SCSI_PASS_THROUGH:
2185 sz = sizeof(SCSI_PASS_THROUGH);
2186 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2187 else if (nOutBufferSize < sizeof(SCSI_PASS_THROUGH)) status = STATUS_BUFFER_TOO_SMALL;
2188 else status = CDROM_ScsiPassThrough(fd, (PSCSI_PASS_THROUGH)lpOutBuffer);
2189 break;
2190 case IOCTL_SCSI_GET_CAPABILITIES:
2191 sz = sizeof(IO_SCSI_CAPABILITIES);
2192 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2193 else if (nOutBufferSize < sizeof(IO_SCSI_CAPABILITIES)) status = STATUS_BUFFER_TOO_SMALL;
2194 else status = CDROM_ScsiGetCaps((PIO_SCSI_CAPABILITIES)lpOutBuffer);
2195 break;
2196 case IOCTL_DVD_START_SESSION:
2197 sz = sizeof(DVD_SESSION_ID);
2198 if (lpOutBuffer == NULL) status = STATUS_INVALID_PARAMETER;
2199 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2200 else
2202 TRACE("before in 0x%08lx out 0x%08lx\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
2203 *(PDVD_SESSION_ID)lpOutBuffer);
2204 status = DVD_StartSession(fd, (PDVD_SESSION_ID)lpInBuffer, (PDVD_SESSION_ID)lpOutBuffer);
2205 TRACE("before in 0x%08lx out 0x%08lx\n",(lpInBuffer)?*(PDVD_SESSION_ID)lpInBuffer:0,
2206 *(PDVD_SESSION_ID)lpOutBuffer);
2208 break;
2209 case IOCTL_DVD_END_SESSION:
2210 sz = sizeof(DVD_SESSION_ID);
2211 if ((lpInBuffer == NULL) || (nInBufferSize < sz))status = STATUS_INVALID_PARAMETER;
2212 else status = DVD_EndSession(fd, (PDVD_SESSION_ID)lpInBuffer);
2213 break;
2214 case IOCTL_DVD_SEND_KEY:
2215 sz = 0;
2216 if (!lpInBuffer ||
2217 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
2218 status = STATUS_INVALID_PARAMETER;
2219 else
2221 TRACE("doing DVD_SendKey\n");
2222 status = DVD_SendKey(fd, (PDVD_COPY_PROTECT_KEY)lpInBuffer);
2224 break;
2225 case IOCTL_DVD_READ_KEY:
2226 if (!lpInBuffer ||
2227 (((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength != nInBufferSize))
2228 status = STATUS_INVALID_PARAMETER;
2229 else if (lpInBuffer !=lpOutBuffer) status = STATUS_BUFFER_TOO_SMALL;
2230 else
2232 TRACE("doing DVD_READ_KEY\n");
2233 sz = ((PDVD_COPY_PROTECT_KEY)lpInBuffer)->KeyLength;
2234 status = DVD_ReadKey(fd, (PDVD_COPY_PROTECT_KEY)lpInBuffer);
2236 break;
2237 case IOCTL_DVD_GET_REGION:
2238 sz = sizeof(DVD_REGION);
2239 if (lpInBuffer != NULL || nInBufferSize != 0) status = STATUS_INVALID_PARAMETER;
2240 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2241 TRACE("doing DVD_Get_REGION\n");
2242 status = DVD_GetRegion(fd, (PDVD_REGION)lpOutBuffer);
2243 break;
2244 case IOCTL_DVD_READ_STRUCTURE:
2245 sz = sizeof(DVD_LAYER_DESCRIPTOR);
2246 if (lpInBuffer == NULL || nInBufferSize != sizeof(DVD_READ_STRUCTURE)) status = STATUS_INVALID_PARAMETER;
2247 else if (nOutBufferSize < sz) status = STATUS_BUFFER_TOO_SMALL;
2248 TRACE("doing DVD_READ_STRUCTURE\n");
2249 status = DVD_ReadStructure(fd, (PDVD_READ_STRUCTURE)lpInBuffer, (PDVD_LAYER_DESCRIPTOR)lpOutBuffer);
2250 break;
2252 case IOCTL_SCSI_GET_INQUIRY_DATA:
2253 sz = INQ_REPLY_LEN;
2254 status = GetInquiryData(fd, (PSCSI_ADAPTER_BUS_INFO)lpOutBuffer, nOutBufferSize);
2255 break;
2257 default:
2258 FIXME("Unsupported IOCTL %lx (type=%lx access=%lx func=%lx meth=%lx)\n",
2259 dwIoControlCode, dwIoControlCode >> 16, (dwIoControlCode >> 14) & 3,
2260 (dwIoControlCode >> 2) & 0xFFF, dwIoControlCode & 3);
2261 sz = 0;
2262 status = STATUS_INVALID_PARAMETER;
2263 break;
2265 wine_server_release_fd( hDevice, fd );
2266 error:
2267 piosb->u.Status = status;
2268 piosb->Information = sz;
2269 if (hEvent) NtSetEvent(hEvent, NULL);
2270 return status;