vo_corevideo: Avoid some code duplication.
[mplayer/glamo.git] / libdvdcss / ioctl.c
blob5c7e6153279291e542630a82ef70313bdbf0bc56
1 /*****************************************************************************
2 * ioctl.c: DVD ioctl replacement function
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id$
7 * Authors: Markus Kuespert <ltlBeBoy@beosmail.com>
8 * Sam Hocevar <sam@zoy.org>
9 * Jon Lech Johansen <jon-vl@nanocrew.net>
10 * Håkan Hjort <d95hjort@dtek.chalmers.se>
11 * Eugenio Jarosiewicz <ej0@cise.ufl.edu>
12 * David Siebörger <drs-videolan@rucus.ru.ac.za>
13 * Alex Strelnikov <lelik@os2.ru>
14 * Gildas Bazin <gbazin@netcourrier.com>
16 * This library is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with this library; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 *****************************************************************************/
31 /*****************************************************************************
32 * Preamble
33 *****************************************************************************/
34 #include "config.h"
36 #include <stdio.h>
38 #include <string.h> /* memcpy(), memset() */
39 #include <sys/types.h>
41 #if defined( WIN32 )
42 # include <windows.h>
43 # include <winioctl.h>
44 #elif defined ( SYS_OS2 )
45 # define INCL_DOSFILEMGR
46 # define INCL_DOSDEVICES
47 # define INCL_DOSDEVIOCTL
48 # define INCL_DOSERRORS
49 # include <os2.h>
50 # include <sys/ioctl.h>
51 #else
52 # include <netinet/in.h>
53 # include <sys/ioctl.h>
54 #endif
56 #ifdef DVD_STRUCT_IN_SYS_CDIO_H
57 # include <sys/cdio.h>
58 #endif
59 #ifdef DVD_STRUCT_IN_SYS_DVDIO_H
60 # include <sys/dvdio.h>
61 #endif
62 #ifdef DVD_STRUCT_IN_LINUX_CDROM_H
63 # include <linux/cdrom.h>
64 #endif
65 #ifdef DVD_STRUCT_IN_DVD_H
66 # include <dvd.h>
67 #endif
68 #ifdef DVD_STRUCT_IN_BSDI_DVDIOCTL_DVD_H
69 # include "bsdi_dvd.h"
70 #endif
71 #ifdef SYS_BEOS
72 # include <malloc.h>
73 # include <scsi.h>
74 #endif
75 #ifdef HPUX_SCTL_IO
76 # include <sys/scsi.h>
77 #endif
78 #ifdef SOLARIS_USCSI
79 # include <dlfcn.h>
80 # include <unistd.h>
81 # include <stropts.h>
82 # include <sys/scsi/scsi_types.h>
83 # include <sys/scsi/impl/uscsi.h>
84 #endif
85 #ifdef DARWIN_DVD_IOCTL
86 # include <IOKit/storage/IODVDMediaBSDClient.h>
87 #endif
88 #ifdef __QNXNTO__
89 # include <sys/mman.h>
90 # include <sys/dcmd_cam.h>
91 #endif
93 #include "common.h"
95 #include "ioctl.h"
97 /*****************************************************************************
98 * Local prototypes, BeOS specific
99 *****************************************************************************/
100 #if defined( SYS_BEOS )
101 static void BeInitRDC ( raw_device_command *, int );
102 #endif
104 /*****************************************************************************
105 * Local prototypes, HP-UX specific
106 *****************************************************************************/
107 #if defined( HPUX_SCTL_IO )
108 static void HPUXInitSCTL ( struct sctl_io *sctl_io, int i_type );
109 #endif
111 /*****************************************************************************
112 * Local prototypes, Solaris specific
113 *****************************************************************************/
114 #if defined( SOLARIS_USCSI )
115 static void SolarisInitUSCSI( struct uscsi_cmd *p_sc, int i_type );
116 static int SolarisSendUSCSI( int fd, struct uscsi_cmd *p_sc );
117 #endif
119 /*****************************************************************************
120 * Local prototypes, win32 (aspi) specific
121 *****************************************************************************/
122 #if defined( WIN32 )
123 static void WinInitSPTD ( SCSI_PASS_THROUGH_DIRECT *, int );
124 static void WinInitSSC ( struct SRB_ExecSCSICmd *, int );
125 static int WinSendSSC ( int, struct SRB_ExecSCSICmd * );
126 #endif
128 /*****************************************************************************
129 * Local prototypes, QNX specific
130 *****************************************************************************/
131 #if defined( __QNXNTO__ )
132 static void QNXInitCPT ( CAM_PASS_THRU *, int );
133 #endif
135 /*****************************************************************************
136 * Local prototypes, OS2 specific
137 *****************************************************************************/
138 #if defined( SYS_OS2 )
139 static void OS2InitSDC( struct OS2_ExecSCSICmd *, int );
140 #endif
142 /*****************************************************************************
143 * ioctl_ReadCopyright: check whether the disc is encrypted or not
144 *****************************************************************************/
145 int ioctl_ReadCopyright( int i_fd, int i_layer, int *pi_copyright )
147 int i_ret;
149 #if defined( HAVE_LINUX_DVD_STRUCT )
150 dvd_struct dvd;
152 memset( &dvd, 0, sizeof( dvd ) );
153 dvd.type = DVD_STRUCT_COPYRIGHT;
154 dvd.copyright.layer_num = i_layer;
156 i_ret = ioctl( i_fd, DVD_READ_STRUCT, &dvd );
158 *pi_copyright = dvd.copyright.cpst;
160 #elif defined( HAVE_BSD_DVD_STRUCT )
161 struct dvd_struct dvd;
163 memset( &dvd, 0, sizeof( dvd ) );
164 dvd.format = DVD_STRUCT_COPYRIGHT;
165 dvd.layer_num = i_layer;
167 i_ret = ioctl( i_fd, DVDIOCREADSTRUCTURE, &dvd );
169 *pi_copyright = dvd.cpst;
171 #elif defined( SYS_BEOS )
172 INIT_RDC( GPCMD_READ_DVD_STRUCTURE, 8 );
174 rdc.command[ 6 ] = i_layer;
175 rdc.command[ 7 ] = DVD_STRUCT_COPYRIGHT;
177 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
179 *pi_copyright = p_buffer[ 4 ];
181 #elif defined( HPUX_SCTL_IO )
182 INIT_SCTL_IO( GPCMD_READ_DVD_STRUCTURE, 8 );
184 sctl_io.cdb[ 6 ] = i_layer;
185 sctl_io.cdb[ 7 ] = DVD_STRUCT_COPYRIGHT;
187 i_ret = ioctl( i_fd, SIOC_IO, &sctl_io );
189 *pi_copyright = p_buffer[ 4 ];
191 #elif defined( SOLARIS_USCSI )
192 INIT_USCSI( GPCMD_READ_DVD_STRUCTURE, 8 );
194 rs_cdb.cdb_opaque[ 6 ] = i_layer;
195 rs_cdb.cdb_opaque[ 7 ] = DVD_STRUCT_COPYRIGHT;
197 i_ret = SolarisSendUSCSI(i_fd, &sc);
199 if( i_ret < 0 || sc.uscsi_status ) {
200 i_ret = -1;
203 *pi_copyright = p_buffer[ 4 ];
204 /* s->copyright.rmi = p_buffer[ 5 ]; */
206 #elif defined( DARWIN_DVD_IOCTL )
207 INIT_DVDIOCTL( dk_dvd_read_structure_t, DVDCopyrightInfo,
208 kDVDStructureFormatCopyrightInfo );
210 dvd.layer = i_layer;
212 i_ret = ioctl( i_fd, DKIOCDVDREADSTRUCTURE, &dvd );
214 *pi_copyright = dvdbs.copyrightProtectionSystemType;
216 #elif defined( WIN32 )
217 if( WIN2K ) /* NT/2k/XP */
219 INIT_SPTD( GPCMD_READ_DVD_STRUCTURE, 8 );
221 /* When using IOCTL_DVD_READ_STRUCTURE and
222 DVD_COPYRIGHT_DESCRIPTOR, CopyrightProtectionType
223 seems to be always 6 ???
224 To work around this MS bug we try to send a raw scsi command
225 instead (if we've got enough privileges to do so). */
227 sptd.Cdb[ 6 ] = i_layer;
228 sptd.Cdb[ 7 ] = DVD_STRUCT_COPYRIGHT;
230 i_ret = SEND_SPTD( i_fd, &sptd, &tmp );
232 if( i_ret == 0 )
234 *pi_copyright = p_buffer[ 4 ];
237 else
239 INIT_SSC( GPCMD_READ_DVD_STRUCTURE, 8 );
241 ssc.CDBByte[ 6 ] = i_layer;
242 ssc.CDBByte[ 7 ] = DVD_STRUCT_COPYRIGHT;
244 i_ret = WinSendSSC( i_fd, &ssc );
246 *pi_copyright = p_buffer[ 4 ];
249 #elif defined( __QNXNTO__ )
251 INIT_CPT( GPCMD_READ_DVD_STRUCTURE, 8 );
253 p_cpt->cam_cdb[ 6 ] = i_layer;
254 p_cpt->cam_cdb[ 7 ] = DVD_STRUCT_COPYRIGHT;
256 i_ret = devctl(i_fd, DCMD_CAM_PASS_THRU, p_cpt, structSize, NULL);
258 *pi_copyright = p_buffer[4];
260 #elif defined( SYS_OS2 )
261 INIT_SSC( GPCMD_READ_DVD_STRUCTURE, 8 );
263 sdc.command[ 6 ] = i_layer;
264 sdc.command[ 7 ] = DVD_STRUCT_COPYRIGHT;
266 i_ret = DosDevIOCtl(i_fd, IOCTL_CDROMDISK, CDROMDISK_EXECMD,
267 &sdc, sizeof(sdc), &ulParamLen,
268 p_buffer, sizeof(p_buffer), &ulDataLen);
270 *pi_copyright = p_buffer[ 4 ];
272 #else
273 # error "DVD ioctls are unavailable on this system"
275 #endif
276 return i_ret;
279 /*****************************************************************************
280 * ioctl_ReadDiscKey: get the disc key
281 *****************************************************************************/
282 int ioctl_ReadDiscKey( int i_fd, int *pi_agid, uint8_t *p_key )
284 int i_ret;
286 #if defined( HAVE_LINUX_DVD_STRUCT )
287 dvd_struct dvd;
289 memset( &dvd, 0, sizeof( dvd ) );
290 dvd.type = DVD_STRUCT_DISCKEY;
291 dvd.disckey.agid = *pi_agid;
292 memset( dvd.disckey.value, 0, DVD_DISCKEY_SIZE );
294 i_ret = ioctl( i_fd, DVD_READ_STRUCT, &dvd );
296 if( i_ret < 0 )
298 return i_ret;
301 memcpy( p_key, dvd.disckey.value, DVD_DISCKEY_SIZE );
303 #elif defined( HAVE_BSD_DVD_STRUCT )
304 struct dvd_struct dvd;
306 memset( &dvd, 0, sizeof( dvd ) );
307 dvd.format = DVD_STRUCT_DISCKEY;
308 dvd.agid = *pi_agid;
309 memset( dvd.data, 0, DVD_DISCKEY_SIZE );
311 i_ret = ioctl( i_fd, DVDIOCREADSTRUCTURE, &dvd );
313 if( i_ret < 0 )
315 return i_ret;
318 memcpy( p_key, dvd.data, DVD_DISCKEY_SIZE );
320 #elif defined( SYS_BEOS )
321 INIT_RDC( GPCMD_READ_DVD_STRUCTURE, DVD_DISCKEY_SIZE + 4 );
323 rdc.command[ 7 ] = DVD_STRUCT_DISCKEY;
324 rdc.command[ 10 ] = *pi_agid << 6;
326 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
328 if( i_ret < 0 )
330 return i_ret;
333 memcpy( p_key, p_buffer + 4, DVD_DISCKEY_SIZE );
335 #elif defined( HPUX_SCTL_IO )
336 INIT_SCTL_IO( GPCMD_READ_DVD_STRUCTURE, DVD_DISCKEY_SIZE + 4 );
338 sctl_io.cdb[ 7 ] = DVD_STRUCT_DISCKEY;
339 sctl_io.cdb[ 10 ] = *pi_agid << 6;
341 i_ret = ioctl( i_fd, SIOC_IO, &sctl_io );
343 if( i_ret < 0 )
345 return i_ret;
348 memcpy( p_key, p_buffer + 4, DVD_DISCKEY_SIZE );
350 #elif defined( SOLARIS_USCSI )
351 INIT_USCSI( GPCMD_READ_DVD_STRUCTURE, DVD_DISCKEY_SIZE + 4 );
353 rs_cdb.cdb_opaque[ 7 ] = DVD_STRUCT_DISCKEY;
354 rs_cdb.cdb_opaque[ 10 ] = *pi_agid << 6;
356 i_ret = SolarisSendUSCSI( i_fd, &sc );
358 if( i_ret < 0 || sc.uscsi_status )
360 i_ret = -1;
361 return i_ret;
364 memcpy( p_key, p_buffer + 4, DVD_DISCKEY_SIZE );
366 #elif defined( DARWIN_DVD_IOCTL )
367 INIT_DVDIOCTL( dk_dvd_read_structure_t, DVDDiscKeyInfo,
368 kDVDStructureFormatDiscKeyInfo );
370 dvd.grantID = *pi_agid;
372 i_ret = ioctl( i_fd, DKIOCDVDREADSTRUCTURE, &dvd );
374 memcpy( p_key, dvdbs.discKeyStructures, DVD_DISCKEY_SIZE );
376 #elif defined( WIN32 )
377 if( WIN2K ) /* NT/2k/XP */
379 DWORD tmp;
380 uint8_t buffer[DVD_DISK_KEY_LENGTH];
381 PDVD_COPY_PROTECT_KEY key = (PDVD_COPY_PROTECT_KEY) &buffer;
383 memset( &buffer, 0, sizeof( buffer ) );
385 key->KeyLength = DVD_DISK_KEY_LENGTH;
386 key->SessionId = *pi_agid;
387 key->KeyType = DvdDiskKey;
388 key->KeyFlags = 0;
390 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_READ_KEY, key,
391 key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
393 if( i_ret < 0 )
395 return i_ret;
398 memcpy( p_key, key->KeyData, DVD_DISCKEY_SIZE );
400 else
402 INIT_SSC( GPCMD_READ_DVD_STRUCTURE, DVD_DISCKEY_SIZE + 4 );
404 ssc.CDBByte[ 7 ] = DVD_STRUCT_DISCKEY;
405 ssc.CDBByte[ 10 ] = *pi_agid << 6;
407 i_ret = WinSendSSC( i_fd, &ssc );
409 if( i_ret < 0 )
411 return i_ret;
414 memcpy( p_key, p_buffer + 4, DVD_DISCKEY_SIZE );
417 #elif defined( __QNXNTO__ )
419 INIT_CPT( GPCMD_READ_DVD_STRUCTURE, DVD_DISCKEY_SIZE + 4 );
421 p_cpt->cam_cdb[ 7 ] = DVD_STRUCT_DISCKEY;
422 p_cpt->cam_cdb[ 10 ] = *pi_agid << 6;
424 i_ret = devctl(i_fd, DCMD_CAM_PASS_THRU, p_cpt, structSize, NULL);
426 memcpy( p_key, p_buffer + 4, DVD_DISCKEY_SIZE );
428 #elif defined ( SYS_OS2 )
429 INIT_SSC( GPCMD_READ_DVD_STRUCTURE, DVD_DISCKEY_SIZE + 4 );
431 sdc.command[ 7 ] = DVD_STRUCT_DISCKEY;
432 sdc.command[ 10 ] = *pi_agid << 6;
434 i_ret = DosDevIOCtl(i_fd, IOCTL_CDROMDISK, CDROMDISK_EXECMD,
435 &sdc, sizeof(sdc), &ulParamLen,
436 p_buffer, sizeof(p_buffer), &ulDataLen);
438 if( i_ret < 0 )
440 return i_ret;
443 memcpy( p_key, p_buffer + 4, DVD_DISCKEY_SIZE );
445 #else
446 # error "DVD ioctls are unavailable on this system"
448 #endif
449 return i_ret;
452 /*****************************************************************************
453 * ioctl_ReadTitleKey: get the title key
454 *****************************************************************************/
455 int ioctl_ReadTitleKey( int i_fd, int *pi_agid, int i_pos, uint8_t *p_key )
457 int i_ret;
459 #if defined( HAVE_LINUX_DVD_STRUCT )
460 dvd_authinfo auth_info;
462 memset( &auth_info, 0, sizeof( auth_info ) );
463 auth_info.type = DVD_LU_SEND_TITLE_KEY;
464 auth_info.lstk.agid = *pi_agid;
465 auth_info.lstk.lba = i_pos;
467 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
469 memcpy( p_key, auth_info.lstk.title_key, DVD_KEY_SIZE );
471 #elif defined( HAVE_BSD_DVD_STRUCT )
472 struct dvd_authinfo auth_info;
474 memset( &auth_info, 0, sizeof( auth_info ) );
475 auth_info.format = DVD_REPORT_TITLE_KEY;
476 auth_info.agid = *pi_agid;
477 auth_info.lba = i_pos;
479 i_ret = ioctl( i_fd, DVDIOCREPORTKEY, &auth_info );
481 memcpy( p_key, auth_info.keychal, DVD_KEY_SIZE );
483 #elif defined( SYS_BEOS )
484 INIT_RDC( GPCMD_REPORT_KEY, 12 );
486 rdc.command[ 2 ] = ( i_pos >> 24 ) & 0xff;
487 rdc.command[ 3 ] = ( i_pos >> 16 ) & 0xff;
488 rdc.command[ 4 ] = ( i_pos >> 8 ) & 0xff;
489 rdc.command[ 5 ] = ( i_pos ) & 0xff;
490 rdc.command[ 10 ] = DVD_REPORT_TITLE_KEY | (*pi_agid << 6);
492 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
494 memcpy( p_key, p_buffer + 5, DVD_KEY_SIZE );
496 #elif defined( HPUX_SCTL_IO )
497 INIT_SCTL_IO( GPCMD_REPORT_KEY, 12 );
499 sctl_io.cdb[ 2 ] = ( i_pos >> 24 ) & 0xff;
500 sctl_io.cdb[ 3 ] = ( i_pos >> 16 ) & 0xff;
501 sctl_io.cdb[ 4 ] = ( i_pos >> 8 ) & 0xff;
502 sctl_io.cdb[ 5 ] = ( i_pos ) & 0xff;
503 sctl_io.cdb[ 10 ] = DVD_REPORT_TITLE_KEY | (*pi_agid << 6);
505 i_ret = ioctl( i_fd, SIOC_IO, &sctl_io );
507 memcpy( p_key, p_buffer + 5, DVD_KEY_SIZE );
509 #elif defined( SOLARIS_USCSI )
510 INIT_USCSI( GPCMD_REPORT_KEY, 12 );
512 rs_cdb.cdb_opaque[ 2 ] = ( i_pos >> 24 ) & 0xff;
513 rs_cdb.cdb_opaque[ 3 ] = ( i_pos >> 16 ) & 0xff;
514 rs_cdb.cdb_opaque[ 4 ] = ( i_pos >> 8 ) & 0xff;
515 rs_cdb.cdb_opaque[ 5 ] = ( i_pos ) & 0xff;
516 rs_cdb.cdb_opaque[ 10 ] = DVD_REPORT_TITLE_KEY | (*pi_agid << 6);
518 i_ret = SolarisSendUSCSI( i_fd, &sc );
520 if( i_ret < 0 || sc.uscsi_status )
522 i_ret = -1;
525 /* Do we want to return the cp_sec flag perhaps? */
526 /* a->lstk.cpm = (buf[ 4 ] >> 7) & 1; */
527 /* a->lstk.cp_sec = (buf[ 4 ] >> 6) & 1; */
528 /* a->lstk.cgms = (buf[ 4 ] >> 4) & 3; */
530 memcpy( p_key, p_buffer + 5, DVD_KEY_SIZE );
532 #elif defined( DARWIN_DVD_IOCTL )
533 INIT_DVDIOCTL( dk_dvd_report_key_t, DVDTitleKeyInfo,
534 kDVDKeyFormatTitleKey );
536 dvd.address = i_pos;
537 dvd.grantID = *pi_agid;
538 dvd.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
540 i_ret = ioctl( i_fd, DKIOCDVDREPORTKEY, &dvd );
542 memcpy( p_key, dvdbs.titleKeyValue, DVD_KEY_SIZE );
544 #elif defined( WIN32 )
545 if( WIN2K ) /* NT/2k/XP */
547 DWORD tmp;
548 uint8_t buffer[DVD_TITLE_KEY_LENGTH];
549 PDVD_COPY_PROTECT_KEY key = (PDVD_COPY_PROTECT_KEY) &buffer;
551 memset( &buffer, 0, sizeof( buffer ) );
553 key->KeyLength = DVD_TITLE_KEY_LENGTH;
554 key->SessionId = *pi_agid;
555 key->KeyType = DvdTitleKey;
556 key->KeyFlags = 0;
557 key->Parameters.TitleOffset.QuadPart = (LONGLONG) i_pos *
558 2048 /*DVDCSS_BLOCK_SIZE*/;
560 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_READ_KEY, key,
561 key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
563 memcpy( p_key, key->KeyData, DVD_KEY_SIZE );
565 else
567 INIT_SSC( GPCMD_REPORT_KEY, 12 );
569 ssc.CDBByte[ 2 ] = ( i_pos >> 24 ) & 0xff;
570 ssc.CDBByte[ 3 ] = ( i_pos >> 16 ) & 0xff;
571 ssc.CDBByte[ 4 ] = ( i_pos >> 8 ) & 0xff;
572 ssc.CDBByte[ 5 ] = ( i_pos ) & 0xff;
573 ssc.CDBByte[ 10 ] = DVD_REPORT_TITLE_KEY | (*pi_agid << 6);
575 i_ret = WinSendSSC( i_fd, &ssc );
577 memcpy( p_key, p_buffer + 5, DVD_KEY_SIZE );
580 #elif defined( __QNXNTO__ )
582 INIT_CPT( GPCMD_REPORT_KEY, 12 );
584 p_cpt->cam_cdb[ 2 ] = ( i_pos >> 24 ) & 0xff;
585 p_cpt->cam_cdb[ 3 ] = ( i_pos >> 16 ) & 0xff;
586 p_cpt->cam_cdb[ 4 ] = ( i_pos >> 8 ) & 0xff;
587 p_cpt->cam_cdb[ 5 ] = ( i_pos ) & 0xff;
588 p_cpt->cam_cdb[ 10 ] = DVD_REPORT_TITLE_KEY | (*pi_agid << 6);
590 i_ret = devctl(i_fd, DCMD_CAM_PASS_THRU, p_cpt, structSize, NULL);
592 memcpy( p_key, p_buffer + 5, DVD_KEY_SIZE );
594 #elif defined( SYS_OS2 )
595 INIT_SSC( GPCMD_REPORT_KEY, 12 );
597 sdc.command[ 2 ] = ( i_pos >> 24 ) & 0xff;
598 sdc.command[ 3 ] = ( i_pos >> 16 ) & 0xff;
599 sdc.command[ 4 ] = ( i_pos >> 8 ) & 0xff;
600 sdc.command[ 5 ] = ( i_pos ) & 0xff;
601 sdc.command[ 10 ] = DVD_REPORT_TITLE_KEY | (*pi_agid << 6);
603 i_ret = DosDevIOCtl(i_fd, IOCTL_CDROMDISK, CDROMDISK_EXECMD,
604 &sdc, sizeof(sdc), &ulParamLen,
605 p_buffer, sizeof(p_buffer), &ulDataLen);
607 memcpy( p_key, p_buffer + 5, DVD_KEY_SIZE );
609 #else
610 # error "DVD ioctls are unavailable on this system"
612 #endif
614 return i_ret;
618 /*****************************************************************************
619 * ioctl_ReportAgid: get AGID from the drive
620 *****************************************************************************/
621 int ioctl_ReportAgid( int i_fd, int *pi_agid )
623 int i_ret;
625 #if defined( HAVE_LINUX_DVD_STRUCT )
626 dvd_authinfo auth_info;
628 memset( &auth_info, 0, sizeof( auth_info ) );
629 auth_info.type = DVD_LU_SEND_AGID;
630 auth_info.lsa.agid = *pi_agid;
632 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
634 *pi_agid = auth_info.lsa.agid;
636 #elif defined( HAVE_BSD_DVD_STRUCT )
637 struct dvd_authinfo auth_info;
639 memset( &auth_info, 0, sizeof( auth_info ) );
640 auth_info.format = DVD_REPORT_AGID;
641 auth_info.agid = *pi_agid;
643 i_ret = ioctl( i_fd, DVDIOCREPORTKEY, &auth_info );
645 *pi_agid = auth_info.agid;
647 #elif defined( SYS_BEOS )
648 INIT_RDC( GPCMD_REPORT_KEY, 8 );
650 rdc.command[ 10 ] = DVD_REPORT_AGID | (*pi_agid << 6);
652 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
654 *pi_agid = p_buffer[ 7 ] >> 6;
656 #elif defined( HPUX_SCTL_IO )
657 INIT_SCTL_IO( GPCMD_REPORT_KEY, 8 );
659 sctl_io.cdb[ 10 ] = DVD_REPORT_AGID | (*pi_agid << 6);
661 i_ret = ioctl( i_fd, SIOC_IO, &sctl_io );
663 *pi_agid = p_buffer[ 7 ] >> 6;
665 #elif defined( SOLARIS_USCSI )
666 INIT_USCSI( GPCMD_REPORT_KEY, 8 );
668 rs_cdb.cdb_opaque[ 10 ] = DVD_REPORT_AGID | (*pi_agid << 6);
670 i_ret = SolarisSendUSCSI( i_fd, &sc );
672 if( i_ret < 0 || sc.uscsi_status )
674 i_ret = -1;
677 *pi_agid = p_buffer[ 7 ] >> 6;
679 #elif defined( DARWIN_DVD_IOCTL )
680 INIT_DVDIOCTL( dk_dvd_report_key_t, DVDAuthenticationGrantIDInfo,
681 kDVDKeyFormatAGID_CSS );
683 dvd.grantID = *pi_agid;
684 dvd.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
686 i_ret = ioctl( i_fd, DKIOCDVDREPORTKEY, &dvd );
688 *pi_agid = dvdbs.grantID;
690 #elif defined( WIN32 )
691 if( WIN2K ) /* NT/2k/XP */
693 ULONG id;
694 DWORD tmp;
696 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_START_SESSION,
697 &tmp, 4, &id, sizeof( id ), &tmp, NULL ) ? 0 : -1;
699 *pi_agid = id;
701 else
703 INIT_SSC( GPCMD_REPORT_KEY, 8 );
705 ssc.CDBByte[ 10 ] = DVD_REPORT_AGID | (*pi_agid << 6);
707 i_ret = WinSendSSC( i_fd, &ssc );
709 *pi_agid = p_buffer[ 7 ] >> 6;
712 #elif defined( __QNXNTO__ )
714 INIT_CPT( GPCMD_REPORT_KEY, 8 );
716 p_cpt->cam_cdb[ 10 ] = DVD_REPORT_AGID | (*pi_agid << 6);
718 i_ret = devctl(i_fd, DCMD_CAM_PASS_THRU, p_cpt, structSize, NULL);
720 *pi_agid = p_buffer[ 7 ] >> 6;
722 #elif defined( SYS_OS2 )
723 INIT_SSC( GPCMD_REPORT_KEY, 8 );
725 sdc.command[ 10 ] = DVD_REPORT_AGID | (*pi_agid << 6);
727 i_ret = DosDevIOCtl(i_fd, IOCTL_CDROMDISK, CDROMDISK_EXECMD,
728 &sdc, sizeof(sdc), &ulParamLen,
729 p_buffer, sizeof(p_buffer), &ulDataLen);
731 *pi_agid = p_buffer[ 7 ] >> 6;
733 #else
734 # error "DVD ioctls are unavailable on this system"
736 #endif
737 return i_ret;
740 /*****************************************************************************
741 * ioctl_ReportChallenge: get challenge from the drive
742 *****************************************************************************/
743 int ioctl_ReportChallenge( int i_fd, int *pi_agid, uint8_t *p_challenge )
745 int i_ret;
747 #if defined( HAVE_LINUX_DVD_STRUCT )
748 dvd_authinfo auth_info;
750 memset( &auth_info, 0, sizeof( auth_info ) );
751 auth_info.type = DVD_LU_SEND_CHALLENGE;
752 auth_info.lsc.agid = *pi_agid;
754 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
756 memcpy( p_challenge, auth_info.lsc.chal, DVD_CHALLENGE_SIZE );
758 #elif defined( HAVE_BSD_DVD_STRUCT )
759 struct dvd_authinfo auth_info;
761 memset( &auth_info, 0, sizeof( auth_info ) );
762 auth_info.format = DVD_REPORT_CHALLENGE;
763 auth_info.agid = *pi_agid;
765 i_ret = ioctl( i_fd, DVDIOCREPORTKEY, &auth_info );
767 memcpy( p_challenge, auth_info.keychal, DVD_CHALLENGE_SIZE );
769 #elif defined( SYS_BEOS )
770 INIT_RDC( GPCMD_REPORT_KEY, 16 );
772 rdc.command[ 10 ] = DVD_REPORT_CHALLENGE | (*pi_agid << 6);
774 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
776 memcpy( p_challenge, p_buffer + 4, DVD_CHALLENGE_SIZE );
778 #elif defined( HPUX_SCTL_IO )
779 INIT_SCTL_IO( GPCMD_REPORT_KEY, 16 );
781 sctl_io.cdb[ 10 ] = DVD_REPORT_CHALLENGE | (*pi_agid << 6);
783 i_ret = ioctl( i_fd, SIOC_IO, &sctl_io );
785 memcpy( p_challenge, p_buffer + 4, DVD_CHALLENGE_SIZE );
787 #elif defined( SOLARIS_USCSI )
788 INIT_USCSI( GPCMD_REPORT_KEY, 16 );
790 rs_cdb.cdb_opaque[ 10 ] = DVD_REPORT_CHALLENGE | (*pi_agid << 6);
792 i_ret = SolarisSendUSCSI( i_fd, &sc );
794 if( i_ret < 0 || sc.uscsi_status )
796 i_ret = -1;
799 memcpy( p_challenge, p_buffer + 4, DVD_CHALLENGE_SIZE );
801 #elif defined( DARWIN_DVD_IOCTL )
802 INIT_DVDIOCTL( dk_dvd_report_key_t, DVDChallengeKeyInfo,
803 kDVDKeyFormatChallengeKey );
805 dvd.grantID = *pi_agid;
807 i_ret = ioctl( i_fd, DKIOCDVDREPORTKEY, &dvd );
809 memcpy( p_challenge, dvdbs.challengeKeyValue, DVD_CHALLENGE_SIZE );
811 #elif defined( WIN32 )
812 if( WIN2K ) /* NT/2k/XP */
814 DWORD tmp;
815 uint8_t buffer[DVD_CHALLENGE_KEY_LENGTH];
816 PDVD_COPY_PROTECT_KEY key = (PDVD_COPY_PROTECT_KEY) &buffer;
818 memset( &buffer, 0, sizeof( buffer ) );
820 key->KeyLength = DVD_CHALLENGE_KEY_LENGTH;
821 key->SessionId = *pi_agid;
822 key->KeyType = DvdChallengeKey;
823 key->KeyFlags = 0;
825 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_READ_KEY, key,
826 key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
828 if( i_ret < 0 )
830 return i_ret;
833 memcpy( p_challenge, key->KeyData, DVD_CHALLENGE_SIZE );
835 else
837 INIT_SSC( GPCMD_REPORT_KEY, 16 );
839 ssc.CDBByte[ 10 ] = DVD_REPORT_CHALLENGE | (*pi_agid << 6);
841 i_ret = WinSendSSC( i_fd, &ssc );
843 memcpy( p_challenge, p_buffer + 4, DVD_CHALLENGE_SIZE );
846 #elif defined( __QNXNTO__ )
848 INIT_CPT( GPCMD_REPORT_KEY, 16 );
850 p_cpt->cam_cdb[ 10 ] = DVD_REPORT_CHALLENGE | (*pi_agid << 6);
852 i_ret = devctl(i_fd, DCMD_CAM_PASS_THRU, p_cpt, structSize, NULL);
854 memcpy( p_challenge, p_buffer + 4, DVD_CHALLENGE_SIZE );
856 #elif defined( SYS_OS2 )
857 INIT_SSC( GPCMD_REPORT_KEY, 16 );
859 sdc.command[ 10 ] = DVD_REPORT_CHALLENGE | (*pi_agid << 6);
861 i_ret = DosDevIOCtl(i_fd, IOCTL_CDROMDISK, CDROMDISK_EXECMD,
862 &sdc, sizeof(sdc), &ulParamLen,
863 p_buffer, sizeof(p_buffer), &ulDataLen);
865 memcpy( p_challenge, p_buffer + 4, DVD_CHALLENGE_SIZE );
867 #else
868 # error "DVD ioctls are unavailable on this system"
870 #endif
871 return i_ret;
874 /*****************************************************************************
875 * ioctl_ReportASF: get ASF from the drive
876 *****************************************************************************/
877 int ioctl_ReportASF( int i_fd, int *pi_remove_me, int *pi_asf )
879 int i_ret;
881 #if defined( HAVE_LINUX_DVD_STRUCT )
882 dvd_authinfo auth_info;
884 memset( &auth_info, 0, sizeof( auth_info ) );
885 auth_info.type = DVD_LU_SEND_ASF;
886 auth_info.lsasf.asf = *pi_asf;
888 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
890 *pi_asf = auth_info.lsasf.asf;
892 #elif defined( HAVE_BSD_DVD_STRUCT )
893 struct dvd_authinfo auth_info;
895 memset( &auth_info, 0, sizeof( auth_info ) );
896 auth_info.format = DVD_REPORT_ASF;
897 auth_info.asf = *pi_asf;
899 i_ret = ioctl( i_fd, DVDIOCREPORTKEY, &auth_info );
901 *pi_asf = auth_info.asf;
903 #elif defined( SYS_BEOS )
904 INIT_RDC( GPCMD_REPORT_KEY, 8 );
906 rdc.command[ 10 ] = DVD_REPORT_ASF;
908 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
910 *pi_asf = p_buffer[ 7 ] & 1;
912 #elif defined( HPUX_SCTL_IO )
913 INIT_SCTL_IO( GPCMD_REPORT_KEY, 8 );
915 sctl_io.cdb[ 10 ] = DVD_REPORT_ASF;
917 i_ret = ioctl( i_fd, SIOC_IO, &sctl_io );
919 *pi_asf = p_buffer[ 7 ] & 1;
921 #elif defined( SOLARIS_USCSI )
922 INIT_USCSI( GPCMD_REPORT_KEY, 8 );
924 rs_cdb.cdb_opaque[ 10 ] = DVD_REPORT_ASF;
926 i_ret = SolarisSendUSCSI( i_fd, &sc );
928 if( i_ret < 0 || sc.uscsi_status )
930 i_ret = -1;
933 *pi_asf = p_buffer[ 7 ] & 1;
935 #elif defined( DARWIN_DVD_IOCTL )
936 INIT_DVDIOCTL( dk_dvd_report_key_t, DVDAuthenticationSuccessFlagInfo,
937 kDVDKeyFormatASF );
939 i_ret = ioctl( i_fd, DKIOCDVDREPORTKEY, &dvd );
941 *pi_asf = dvdbs.successFlag;
943 #elif defined( WIN32 )
944 if( WIN2K ) /* NT/2k/XP */
946 DWORD tmp;
947 uint8_t buffer[DVD_ASF_LENGTH];
948 PDVD_COPY_PROTECT_KEY key = (PDVD_COPY_PROTECT_KEY) &buffer;
950 memset( &buffer, 0, sizeof( buffer ) );
952 key->KeyLength = DVD_ASF_LENGTH;
953 key->KeyType = DvdAsf;
954 key->KeyFlags = 0;
956 ((PDVD_ASF)key->KeyData)->SuccessFlag = *pi_asf;
958 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_READ_KEY, key,
959 key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
961 if( i_ret < 0 )
963 return i_ret;
966 *pi_asf = ((PDVD_ASF)key->KeyData)->SuccessFlag;
968 else
970 INIT_SSC( GPCMD_REPORT_KEY, 8 );
972 ssc.CDBByte[ 10 ] = DVD_REPORT_ASF;
974 i_ret = WinSendSSC( i_fd, &ssc );
976 *pi_asf = p_buffer[ 7 ] & 1;
979 #elif defined( __QNXNTO__ )
981 INIT_CPT( GPCMD_REPORT_KEY, 8 );
983 p_cpt->cam_cdb[ 10 ] = DVD_REPORT_ASF;
985 i_ret = devctl(i_fd, DCMD_CAM_PASS_THRU, p_cpt, structSize, NULL);
987 *pi_asf = p_buffer[ 7 ] & 1;
989 #elif defined( SYS_OS2 )
990 INIT_SSC( GPCMD_REPORT_KEY, 8 );
992 sdc.command[ 10 ] = DVD_REPORT_ASF;
994 i_ret = DosDevIOCtl(i_fd, IOCTL_CDROMDISK, CDROMDISK_EXECMD,
995 &sdc, sizeof(sdc), &ulParamLen,
996 p_buffer, sizeof(p_buffer), &ulDataLen);
998 *pi_asf = p_buffer[ 7 ] & 1;
1000 #else
1001 # error "DVD ioctls are unavailable on this system"
1003 #endif
1004 return i_ret;
1007 /*****************************************************************************
1008 * ioctl_ReportKey1: get the first key from the drive
1009 *****************************************************************************/
1010 int ioctl_ReportKey1( int i_fd, int *pi_agid, uint8_t *p_key )
1012 int i_ret;
1014 #if defined( HAVE_LINUX_DVD_STRUCT )
1015 dvd_authinfo auth_info;
1017 memset( &auth_info, 0, sizeof( auth_info ) );
1018 auth_info.type = DVD_LU_SEND_KEY1;
1019 auth_info.lsk.agid = *pi_agid;
1021 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
1023 memcpy( p_key, auth_info.lsk.key, DVD_KEY_SIZE );
1025 #elif defined( HAVE_BSD_DVD_STRUCT )
1026 struct dvd_authinfo auth_info;
1028 memset( &auth_info, 0, sizeof( auth_info ) );
1029 auth_info.format = DVD_REPORT_KEY1;
1030 auth_info.agid = *pi_agid;
1032 i_ret = ioctl( i_fd, DVDIOCREPORTKEY, &auth_info );
1034 memcpy( p_key, auth_info.keychal, DVD_KEY_SIZE );
1036 #elif defined( SYS_BEOS )
1037 INIT_RDC( GPCMD_REPORT_KEY, 12 );
1039 rdc.command[ 10 ] = DVD_REPORT_KEY1 | (*pi_agid << 6);
1041 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
1043 memcpy( p_key, p_buffer + 4, DVD_KEY_SIZE );
1045 #elif defined( HPUX_SCTL_IO )
1046 INIT_SCTL_IO( GPCMD_REPORT_KEY, 12 );
1048 sctl_io.cdb[ 10 ] = DVD_REPORT_KEY1 | (*pi_agid << 6);
1050 i_ret = ioctl( i_fd, SIOC_IO, &sctl_io );
1052 memcpy( p_key, p_buffer + 4, DVD_KEY_SIZE );
1054 #elif defined( SOLARIS_USCSI )
1055 INIT_USCSI( GPCMD_REPORT_KEY, 12 );
1057 rs_cdb.cdb_opaque[ 10 ] = DVD_REPORT_KEY1 | (*pi_agid << 6);
1059 i_ret = SolarisSendUSCSI( i_fd, &sc );
1061 if( i_ret < 0 || sc.uscsi_status )
1063 i_ret = -1;
1066 memcpy( p_key, p_buffer + 4, DVD_KEY_SIZE );
1068 #elif defined( DARWIN_DVD_IOCTL )
1069 INIT_DVDIOCTL( dk_dvd_report_key_t, DVDKey1Info,
1070 kDVDKeyFormatKey1 );
1072 dvd.grantID = *pi_agid;
1074 i_ret = ioctl( i_fd, DKIOCDVDREPORTKEY, &dvd );
1076 memcpy( p_key, dvdbs.key1Value, DVD_KEY_SIZE );
1078 #elif defined( WIN32 )
1079 if( WIN2K ) /* NT/2k/XP */
1081 DWORD tmp;
1082 uint8_t buffer[DVD_BUS_KEY_LENGTH];
1083 PDVD_COPY_PROTECT_KEY key = (PDVD_COPY_PROTECT_KEY) &buffer;
1085 memset( &buffer, 0, sizeof( buffer ) );
1087 key->KeyLength = DVD_BUS_KEY_LENGTH;
1088 key->SessionId = *pi_agid;
1089 key->KeyType = DvdBusKey1;
1090 key->KeyFlags = 0;
1092 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_READ_KEY, key,
1093 key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
1095 memcpy( p_key, key->KeyData, DVD_KEY_SIZE );
1097 else
1099 INIT_SSC( GPCMD_REPORT_KEY, 12 );
1101 ssc.CDBByte[ 10 ] = DVD_REPORT_KEY1 | (*pi_agid << 6);
1103 i_ret = WinSendSSC( i_fd, &ssc );
1105 memcpy( p_key, p_buffer + 4, DVD_KEY_SIZE );
1108 #elif defined( __QNXNTO__ )
1110 INIT_CPT( GPCMD_REPORT_KEY, 12 );
1112 p_cpt->cam_cdb[ 10 ] = DVD_REPORT_KEY1 | (*pi_agid << 6);
1114 i_ret = devctl(i_fd, DCMD_CAM_PASS_THRU, p_cpt, structSize, NULL);
1116 memcpy( p_key, p_buffer + 4, DVD_KEY_SIZE );
1118 #elif defined( SYS_OS2 )
1119 INIT_SSC( GPCMD_REPORT_KEY, 12 );
1121 sdc.command[ 10 ] = DVD_REPORT_KEY1 | (*pi_agid << 6);
1123 i_ret = DosDevIOCtl(i_fd, IOCTL_CDROMDISK, CDROMDISK_EXECMD,
1124 &sdc, sizeof(sdc), &ulParamLen,
1125 p_buffer, sizeof(p_buffer), &ulDataLen);
1127 memcpy( p_key, p_buffer + 4, DVD_KEY_SIZE );
1129 #else
1130 # error "DVD ioctls are unavailable on this system"
1132 #endif
1133 return i_ret;
1136 /*****************************************************************************
1137 * ioctl_InvalidateAgid: invalidate the current AGID
1138 *****************************************************************************/
1139 int ioctl_InvalidateAgid( int i_fd, int *pi_agid )
1141 int i_ret;
1143 #if defined( HAVE_LINUX_DVD_STRUCT )
1144 dvd_authinfo auth_info;
1146 memset( &auth_info, 0, sizeof( auth_info ) );
1147 auth_info.type = DVD_INVALIDATE_AGID;
1148 auth_info.lsa.agid = *pi_agid;
1150 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
1152 #elif defined( HAVE_BSD_DVD_STRUCT )
1153 struct dvd_authinfo auth_info;
1155 memset( &auth_info, 0, sizeof( auth_info ) );
1156 auth_info.format = DVD_INVALIDATE_AGID;
1157 auth_info.agid = *pi_agid;
1159 i_ret = ioctl( i_fd, DVDIOCREPORTKEY, &auth_info );
1161 #elif defined( SYS_BEOS )
1162 INIT_RDC( GPCMD_REPORT_KEY, 0 );
1164 rdc.command[ 10 ] = DVD_INVALIDATE_AGID | (*pi_agid << 6);
1166 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
1168 #elif defined( HPUX_SCTL_IO )
1169 INIT_SCTL_IO( GPCMD_REPORT_KEY, 0 );
1171 sctl_io.cdb[ 10 ] = DVD_INVALIDATE_AGID | (*pi_agid << 6);
1173 i_ret = ioctl( i_fd, SIOC_IO, &sctl_io );
1175 #elif defined( SOLARIS_USCSI )
1176 INIT_USCSI( GPCMD_REPORT_KEY, 0 );
1178 rs_cdb.cdb_opaque[ 10 ] = DVD_INVALIDATE_AGID | (*pi_agid << 6);
1180 i_ret = SolarisSendUSCSI( i_fd, &sc );
1182 if( i_ret < 0 || sc.uscsi_status )
1184 i_ret = -1;
1187 #elif defined( DARWIN_DVD_IOCTL )
1188 INIT_DVDIOCTL( dk_dvd_send_key_t, DVDAuthenticationGrantIDInfo,
1189 kDVDKeyFormatAGID_Invalidate );
1191 dvd.grantID = *pi_agid;
1193 i_ret = ioctl( i_fd, DKIOCDVDSENDKEY, &dvd );
1195 #elif defined( WIN32 )
1196 if( WIN2K ) /* NT/2k/XP */
1198 DWORD tmp;
1200 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_END_SESSION,
1201 pi_agid, sizeof( *pi_agid ), NULL, 0, &tmp, NULL ) ? 0 : -1;
1203 else
1205 #if defined( __MINGW32__ )
1206 INIT_SSC( GPCMD_REPORT_KEY, 0 );
1207 #else
1208 INIT_SSC( GPCMD_REPORT_KEY, 1 );
1210 ssc.SRB_BufLen = 0;
1211 ssc.CDBByte[ 8 ] = 0;
1212 ssc.CDBByte[ 9 ] = 0;
1213 #endif
1215 ssc.CDBByte[ 10 ] = DVD_INVALIDATE_AGID | (*pi_agid << 6);
1217 i_ret = WinSendSSC( i_fd, &ssc );
1220 #elif defined( __QNXNTO__ )
1222 INIT_CPT( GPCMD_REPORT_KEY, 0 );
1224 p_cpt->cam_cdb[ 10 ] = DVD_INVALIDATE_AGID | (*pi_agid << 6);
1226 i_ret = devctl(i_fd, DCMD_CAM_PASS_THRU, p_cpt, structSize, NULL);
1228 #elif defined( SYS_OS2 )
1229 INIT_SSC( GPCMD_REPORT_KEY, 1 );
1231 sdc.data_length = 0;
1232 sdc.command[ 8 ] = 0;
1233 sdc.command[ 9 ] = 0;
1235 sdc.command[ 10 ] = DVD_INVALIDATE_AGID | (*pi_agid << 6);
1237 i_ret = DosDevIOCtl(i_fd, IOCTL_CDROMDISK, CDROMDISK_EXECMD,
1238 &sdc, sizeof(sdc), &ulParamLen,
1239 NULL, 0, &ulDataLen);
1240 #else
1241 # error "DVD ioctls are unavailable on this system"
1243 #endif
1244 return i_ret;
1247 /*****************************************************************************
1248 * ioctl_SendChallenge: send challenge to the drive
1249 *****************************************************************************/
1250 int ioctl_SendChallenge( int i_fd, int *pi_agid, uint8_t *p_challenge )
1252 int i_ret;
1254 #if defined( HAVE_LINUX_DVD_STRUCT )
1255 dvd_authinfo auth_info;
1257 memset( &auth_info, 0, sizeof( auth_info ) );
1258 auth_info.type = DVD_HOST_SEND_CHALLENGE;
1259 auth_info.hsc.agid = *pi_agid;
1261 memcpy( auth_info.hsc.chal, p_challenge, DVD_CHALLENGE_SIZE );
1263 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
1265 #elif defined( HAVE_BSD_DVD_STRUCT )
1266 struct dvd_authinfo auth_info;
1268 memset( &auth_info, 0, sizeof( auth_info ) );
1269 auth_info.format = DVD_SEND_CHALLENGE;
1270 auth_info.agid = *pi_agid;
1272 memcpy( auth_info.keychal, p_challenge, DVD_CHALLENGE_SIZE );
1274 i_ret = ioctl( i_fd, DVDIOCSENDKEY, &auth_info );
1276 #elif defined( SYS_BEOS )
1277 INIT_RDC( GPCMD_SEND_KEY, 16 );
1279 rdc.command[ 10 ] = DVD_SEND_CHALLENGE | (*pi_agid << 6);
1281 p_buffer[ 1 ] = 0xe;
1282 memcpy( p_buffer + 4, p_challenge, DVD_CHALLENGE_SIZE );
1284 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
1286 #elif defined( HPUX_SCTL_IO )
1287 INIT_SCTL_IO( GPCMD_SEND_KEY, 16 );
1289 sctl_io.cdb[ 10 ] = DVD_SEND_CHALLENGE | (*pi_agid << 6);
1291 p_buffer[ 1 ] = 0xe;
1292 memcpy( p_buffer + 4, p_challenge, DVD_CHALLENGE_SIZE );
1294 i_ret = ioctl( i_fd, SIOC_IO, &sctl_io );
1296 #elif defined( SOLARIS_USCSI )
1297 INIT_USCSI( GPCMD_SEND_KEY, 16 );
1299 rs_cdb.cdb_opaque[ 10 ] = DVD_SEND_CHALLENGE | (*pi_agid << 6);
1301 p_buffer[ 1 ] = 0xe;
1302 memcpy( p_buffer + 4, p_challenge, DVD_CHALLENGE_SIZE );
1304 if( SolarisSendUSCSI( i_fd, &sc ) < 0 || sc.uscsi_status )
1306 return -1;
1309 i_ret = 0;
1311 #elif defined( DARWIN_DVD_IOCTL )
1312 INIT_DVDIOCTL( dk_dvd_send_key_t, DVDChallengeKeyInfo,
1313 kDVDKeyFormatChallengeKey );
1315 dvd.grantID = *pi_agid;
1316 dvd.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
1318 dvdbs.dataLength[ 1 ] = 0xe;
1319 memcpy( dvdbs.challengeKeyValue, p_challenge, DVD_CHALLENGE_SIZE );
1321 i_ret = ioctl( i_fd, DKIOCDVDSENDKEY, &dvd );
1323 #elif defined( WIN32 )
1324 if( WIN2K ) /* NT/2k/XP */
1326 DWORD tmp;
1327 uint8_t buffer[DVD_CHALLENGE_KEY_LENGTH];
1328 PDVD_COPY_PROTECT_KEY key = (PDVD_COPY_PROTECT_KEY) &buffer;
1330 memset( &buffer, 0, sizeof( buffer ) );
1332 key->KeyLength = DVD_CHALLENGE_KEY_LENGTH;
1333 key->SessionId = *pi_agid;
1334 key->KeyType = DvdChallengeKey;
1335 key->KeyFlags = 0;
1337 memcpy( key->KeyData, p_challenge, DVD_CHALLENGE_SIZE );
1339 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_SEND_KEY, key,
1340 key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
1342 else
1344 INIT_SSC( GPCMD_SEND_KEY, 16 );
1346 ssc.CDBByte[ 10 ] = DVD_SEND_CHALLENGE | (*pi_agid << 6);
1348 p_buffer[ 1 ] = 0xe;
1349 memcpy( p_buffer + 4, p_challenge, DVD_CHALLENGE_SIZE );
1351 i_ret = WinSendSSC( i_fd, &ssc );
1354 #elif defined( __QNXNTO__ )
1356 INIT_CPT( GPCMD_SEND_KEY, 16 );
1358 p_cpt->cam_cdb[ 10 ] = DVD_SEND_CHALLENGE | (*pi_agid << 6);
1360 p_buffer[ 1 ] = 0xe;
1361 memcpy( p_buffer + 4, p_challenge, DVD_CHALLENGE_SIZE );
1363 i_ret = devctl(i_fd, DCMD_CAM_PASS_THRU, p_cpt, structSize, NULL);
1365 #elif defined( SYS_OS2 )
1366 INIT_SSC( GPCMD_SEND_KEY, 16 );
1368 sdc.command[ 10 ] = DVD_SEND_CHALLENGE | (*pi_agid << 6);
1370 p_buffer[ 1 ] = 0xe;
1371 memcpy( p_buffer + 4, p_challenge, DVD_CHALLENGE_SIZE );
1373 i_ret = DosDevIOCtl( i_fd, IOCTL_CDROMDISK, CDROMDISK_EXECMD,
1374 &sdc, sizeof(sdc), &ulParamLen,
1375 p_buffer, sizeof(p_buffer), &ulDataLen );
1377 #else
1378 # error "DVD ioctls are unavailable on this system"
1380 #endif
1381 return i_ret;
1384 /*****************************************************************************
1385 * ioctl_SendKey2: send the second key to the drive
1386 *****************************************************************************/
1387 int ioctl_SendKey2( int i_fd, int *pi_agid, uint8_t *p_key )
1389 int i_ret;
1391 #if defined( HAVE_LINUX_DVD_STRUCT )
1392 dvd_authinfo auth_info;
1394 memset( &auth_info, 0, sizeof( auth_info ) );
1395 auth_info.type = DVD_HOST_SEND_KEY2;
1396 auth_info.hsk.agid = *pi_agid;
1398 memcpy( auth_info.hsk.key, p_key, DVD_KEY_SIZE );
1400 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
1402 #elif defined( HAVE_BSD_DVD_STRUCT )
1403 struct dvd_authinfo auth_info;
1405 memset( &auth_info, 0, sizeof( auth_info ) );
1406 auth_info.format = DVD_SEND_KEY2;
1407 auth_info.agid = *pi_agid;
1409 memcpy( auth_info.keychal, p_key, DVD_KEY_SIZE );
1411 i_ret = ioctl( i_fd, DVDIOCSENDKEY, &auth_info );
1413 #elif defined( SYS_BEOS )
1414 INIT_RDC( GPCMD_SEND_KEY, 12 );
1416 rdc.command[ 10 ] = DVD_SEND_KEY2 | (*pi_agid << 6);
1418 p_buffer[ 1 ] = 0xa;
1419 memcpy( p_buffer + 4, p_key, DVD_KEY_SIZE );
1421 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
1423 #elif defined( HPUX_SCTL_IO )
1424 INIT_SCTL_IO( GPCMD_SEND_KEY, 12 );
1426 sctl_io.cdb[ 10 ] = DVD_SEND_KEY2 | (*pi_agid << 6);
1428 p_buffer[ 1 ] = 0xa;
1429 memcpy( p_buffer + 4, p_key, DVD_KEY_SIZE );
1431 i_ret = ioctl( i_fd, SIOC_IO, &sctl_io );
1433 #elif defined( SOLARIS_USCSI )
1434 INIT_USCSI( GPCMD_SEND_KEY, 12 );
1436 rs_cdb.cdb_opaque[ 10 ] = DVD_SEND_KEY2 | (*pi_agid << 6);
1438 p_buffer[ 1 ] = 0xa;
1439 memcpy( p_buffer + 4, p_key, DVD_KEY_SIZE );
1441 if( SolarisSendUSCSI( i_fd, &sc ) < 0 || sc.uscsi_status )
1443 return -1;
1446 i_ret = 0;
1448 #elif defined( DARWIN_DVD_IOCTL )
1449 INIT_DVDIOCTL( dk_dvd_send_key_t, DVDKey2Info,
1450 kDVDKeyFormatKey2 );
1452 dvd.grantID = *pi_agid;
1453 dvd.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
1455 dvdbs.dataLength[ 1 ] = 0xa;
1456 memcpy( dvdbs.key2Value, p_key, DVD_KEY_SIZE );
1458 i_ret = ioctl( i_fd, DKIOCDVDSENDKEY, &dvd );
1460 #elif defined( WIN32 )
1461 if( WIN2K ) /* NT/2k/XP */
1463 DWORD tmp;
1464 uint8_t buffer[DVD_BUS_KEY_LENGTH];
1465 PDVD_COPY_PROTECT_KEY key = (PDVD_COPY_PROTECT_KEY) &buffer;
1467 memset( &buffer, 0, sizeof( buffer ) );
1469 key->KeyLength = DVD_BUS_KEY_LENGTH;
1470 key->SessionId = *pi_agid;
1471 key->KeyType = DvdBusKey2;
1472 key->KeyFlags = 0;
1474 memcpy( key->KeyData, p_key, DVD_KEY_SIZE );
1476 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_SEND_KEY, key,
1477 key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
1479 else
1481 INIT_SSC( GPCMD_SEND_KEY, 12 );
1483 ssc.CDBByte[ 10 ] = DVD_SEND_KEY2 | (*pi_agid << 6);
1485 p_buffer[ 1 ] = 0xa;
1486 memcpy( p_buffer + 4, p_key, DVD_KEY_SIZE );
1488 i_ret = WinSendSSC( i_fd, &ssc );
1491 #elif defined( __QNXNTO__ )
1493 INIT_CPT( GPCMD_SEND_KEY, 12 );
1495 p_cpt->cam_cdb[ 10 ] = DVD_SEND_KEY2 | (*pi_agid << 6);
1497 p_buffer[ 1 ] = 0xa;
1498 memcpy( p_buffer + 4, p_key, DVD_KEY_SIZE );
1500 i_ret = devctl(i_fd, DCMD_CAM_PASS_THRU, p_cpt, structSize, NULL);
1502 #elif defined( SYS_OS2 )
1503 INIT_SSC( GPCMD_SEND_KEY, 12 );
1505 sdc.command[ 10 ] = DVD_SEND_KEY2 | (*pi_agid << 6);
1507 p_buffer[ 1 ] = 0xa;
1508 memcpy( p_buffer + 4, p_key, DVD_KEY_SIZE );
1510 i_ret = DosDevIOCtl( i_fd, IOCTL_CDROMDISK, CDROMDISK_EXECMD,
1511 &sdc, sizeof(sdc), &ulParamLen,
1512 p_buffer, sizeof(p_buffer), &ulDataLen );
1514 #else
1515 # error "DVD ioctls are unavailable on this system"
1517 #endif
1518 return i_ret;
1521 /*****************************************************************************
1522 * ioctl_ReportRPC: get RPC status for the drive
1523 *****************************************************************************/
1524 int ioctl_ReportRPC( int i_fd, int *p_type, int *p_mask, int *p_scheme )
1526 int i_ret;
1528 #if defined( HAVE_LINUX_DVD_STRUCT ) && defined( DVD_LU_SEND_RPC_STATE )
1529 dvd_authinfo auth_info;
1531 memset( &auth_info, 0, sizeof( auth_info ) );
1532 auth_info.type = DVD_LU_SEND_RPC_STATE;
1534 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
1536 *p_type = auth_info.lrpcs.type;
1537 *p_mask = auth_info.lrpcs.region_mask;
1538 *p_scheme = auth_info.lrpcs.rpc_scheme;
1540 #elif defined( HAVE_LINUX_DVD_STRUCT )
1541 /* FIXME: OpenBSD doesn't know this */
1542 i_ret = -1;
1544 #elif defined( HAVE_BSD_DVD_STRUCT )
1545 struct dvd_authinfo auth_info;
1547 memset( &auth_info, 0, sizeof( auth_info ) );
1548 auth_info.format = DVD_REPORT_RPC;
1550 i_ret = ioctl( i_fd, DVDIOCREPORTKEY, &auth_info );
1552 *p_type = auth_info.reg_type;
1553 *p_mask = auth_info.region; // ??
1554 *p_scheme = auth_info.rpc_scheme;
1556 #elif defined( SYS_BEOS )
1557 INIT_RDC( GPCMD_REPORT_KEY, 8 );
1559 rdc.command[ 10 ] = DVD_REPORT_RPC;
1561 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
1563 *p_type = p_buffer[ 4 ] >> 6;
1564 *p_mask = p_buffer[ 5 ];
1565 *p_scheme = p_buffer[ 6 ];
1567 #elif defined( HPUX_SCTL_IO )
1568 INIT_SCTL_IO( GPCMD_REPORT_KEY, 8 );
1570 sctl_io.cdb[ 10 ] = DVD_REPORT_RPC;
1572 i_ret = ioctl( i_fd, SIOC_IO, &sctl_io );
1574 *p_type = p_buffer[ 4 ] >> 6;
1575 *p_mask = p_buffer[ 5 ];
1576 *p_scheme = p_buffer[ 6 ];
1578 #elif defined( SOLARIS_USCSI )
1579 INIT_USCSI( GPCMD_REPORT_KEY, 8 );
1581 rs_cdb.cdb_opaque[ 10 ] = DVD_REPORT_RPC;
1583 i_ret = SolarisSendUSCSI( i_fd, &sc );
1585 if( i_ret < 0 || sc.uscsi_status )
1587 i_ret = -1;
1590 *p_type = p_buffer[ 4 ] >> 6;
1591 *p_mask = p_buffer[ 5 ];
1592 *p_scheme = p_buffer[ 6 ];
1594 #elif defined( DARWIN_DVD_IOCTL )
1595 INIT_DVDIOCTL( dk_dvd_report_key_t, DVDRegionPlaybackControlInfo,
1596 kDVDKeyFormatRegionState );
1598 dvd.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
1600 i_ret = ioctl( i_fd, DKIOCDVDREPORTKEY, &dvd );
1602 *p_type = dvdbs.typeCode;
1603 *p_mask = dvdbs.driveRegion;
1604 *p_scheme = dvdbs.rpcScheme;
1606 #elif defined( WIN32 )
1607 if( WIN2K ) /* NT/2k/XP */
1609 DWORD tmp;
1610 uint8_t buffer[DVD_RPC_KEY_LENGTH];
1611 PDVD_COPY_PROTECT_KEY key = (PDVD_COPY_PROTECT_KEY) &buffer;
1613 memset( &buffer, 0, sizeof( buffer ) );
1615 key->KeyLength = DVD_RPC_KEY_LENGTH;
1616 key->KeyType = DvdGetRpcKey;
1617 key->KeyFlags = 0;
1619 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_READ_KEY, key,
1620 key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
1622 if( i_ret < 0 )
1624 return i_ret;
1627 *p_type = ((PDVD_RPC_KEY)key->KeyData)->TypeCode;
1628 *p_mask = ((PDVD_RPC_KEY)key->KeyData)->RegionMask;
1629 *p_scheme = ((PDVD_RPC_KEY)key->KeyData)->RpcScheme;
1631 else
1633 INIT_SSC( GPCMD_REPORT_KEY, 8 );
1635 ssc.CDBByte[ 10 ] = DVD_REPORT_RPC;
1637 i_ret = WinSendSSC( i_fd, &ssc );
1639 *p_type = p_buffer[ 4 ] >> 6;
1640 *p_mask = p_buffer[ 5 ];
1641 *p_scheme = p_buffer[ 6 ];
1644 #elif defined( __QNXNTO__ )
1646 INIT_CPT( GPCMD_REPORT_KEY, 8 );
1648 p_cpt->cam_cdb[ 10 ] = DVD_REPORT_RPC;
1650 i_ret = devctl(i_fd, DCMD_CAM_PASS_THRU, p_cpt, structSize, NULL);
1652 *p_type = p_buffer[ 4 ] >> 6;
1653 *p_mask = p_buffer[ 5 ];
1654 *p_scheme = p_buffer[ 6 ];
1656 #elif defined( SYS_OS2 )
1657 INIT_SSC( GPCMD_REPORT_KEY, 8 );
1659 sdc.command[ 10 ] = DVD_REPORT_RPC;
1661 i_ret = DosDevIOCtl(i_fd, IOCTL_CDROMDISK, CDROMDISK_EXECMD,
1662 &sdc, sizeof(sdc), &ulParamLen,
1663 p_buffer, sizeof(p_buffer), &ulDataLen);
1665 *p_type = p_buffer[ 4 ] >> 6;
1666 *p_mask = p_buffer[ 5 ];
1667 *p_scheme = p_buffer[ 6 ];
1669 #else
1670 # error "DVD ioctls are unavailable on this system"
1672 #endif
1673 return i_ret;
1676 /*****************************************************************************
1677 * ioctl_SendRPC: set RPC status for the drive
1678 *****************************************************************************/
1679 int ioctl_SendRPC( int i_fd, int i_pdrc )
1681 int i_ret;
1683 #if defined( HAVE_LINUX_DVD_STRUCT ) && defined( DVD_HOST_SEND_RPC_STATE )
1684 dvd_authinfo auth_info;
1686 memset( &auth_info, 0, sizeof( auth_info ) );
1687 auth_info.type = DVD_HOST_SEND_RPC_STATE;
1688 auth_info.hrpcs.pdrc = i_pdrc;
1690 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
1692 #elif defined( HAVE_LINUX_DVD_STRUCT )
1693 /* FIXME: OpenBSD doesn't know this */
1694 i_ret = -1;
1696 #elif defined( HAVE_BSD_DVD_STRUCT )
1697 struct dvd_authinfo auth_info;
1699 memset( &auth_info, 0, sizeof( auth_info ) );
1700 auth_info.format = DVD_SEND_RPC;
1701 auth_info.region = i_pdrc;
1703 i_ret = ioctl( i_fd, DVDIOCSENDKEY, &auth_info );
1705 #elif defined( SYS_BEOS )
1706 INIT_RDC( GPCMD_SEND_KEY, 8 );
1708 rdc.command[ 10 ] = DVD_SEND_RPC;
1710 p_buffer[ 1 ] = 6;
1711 p_buffer[ 4 ] = i_pdrc;
1713 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
1715 #elif defined( HPUX_SCTL_IO )
1716 INIT_SCTL_IO( GPCMD_SEND_KEY, 8 );
1718 sctl_io.cdb[ 10 ] = DVD_SEND_RPC;
1720 p_buffer[ 1 ] = 6;
1721 p_buffer[ 4 ] = i_pdrc;
1723 i_ret = ioctl( i_fd, SIOC_IO, &sctl_io );
1725 #elif defined( SOLARIS_USCSI )
1726 INIT_USCSI( GPCMD_SEND_KEY, 8 );
1728 rs_cdb.cdb_opaque[ 10 ] = DVD_SEND_RPC;
1730 p_buffer[ 1 ] = 6;
1731 p_buffer[ 4 ] = i_pdrc;
1733 i_ret = SolarisSendUSCSI( i_fd, &sc );
1735 if( i_ret < 0 || sc.uscsi_status )
1737 i_ret = -1;
1740 #elif defined( DARWIN_DVD_IOCTL )
1741 INIT_DVDIOCTL( dk_dvd_send_key_t, DVDRegionPlaybackControlInfo,
1742 kDVDKeyFormatSetRegion );
1744 dvd.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
1745 dvdbs.driveRegion = i_pdrc;
1747 i_ret = ioctl( i_fd, DKIOCDVDSENDKEY, &dvd );
1749 #elif defined( WIN32 )
1750 if( WIN2K ) /* NT/2k/XP */
1752 INIT_SPTD( GPCMD_SEND_KEY, 8 );
1754 sptd.Cdb[ 10 ] = DVD_SEND_RPC;
1756 p_buffer[ 1 ] = 6;
1757 p_buffer[ 4 ] = i_pdrc;
1759 i_ret = SEND_SPTD( i_fd, &sptd, &tmp );
1761 else
1763 INIT_SSC( GPCMD_SEND_KEY, 8 );
1765 ssc.CDBByte[ 10 ] = DVD_SEND_RPC;
1767 p_buffer[ 1 ] = 6;
1768 p_buffer[ 4 ] = i_pdrc;
1770 i_ret = WinSendSSC( i_fd, &ssc );
1773 #elif defined( __QNXNTO__ )
1775 INIT_CPT( GPCMD_SEND_KEY, 8 );
1777 p_cpt->cam_cdb[ 10 ] = DVD_SEND_RPC;
1779 p_buffer[ 1 ] = 6;
1780 p_buffer[ 4 ] = i_pdrc;
1782 i_ret = devctl(i_fd, DCMD_CAM_PASS_THRU, p_cpt, structSize, NULL);
1784 #elif defined( SYS_OS2 )
1785 INIT_SSC( GPCMD_SEND_KEY, 8 );
1787 sdc.command[ 10 ] = DVD_SEND_RPC;
1789 p_buffer[ 1 ] = 6;
1790 p_buffer[ 4 ] = i_pdrc;
1792 i_ret = DosDevIOCtl( i_fd, IOCTL_CDROMDISK, CDROMDISK_EXECMD,
1793 &sdc, sizeof(sdc), &ulParamLen,
1794 p_buffer, sizeof(p_buffer), &ulDataLen );
1796 #else
1797 # error "DVD ioctls are unavailable on this system"
1799 #endif
1800 return i_ret;
1803 /* Local prototypes */
1805 #if defined( SYS_BEOS )
1806 /*****************************************************************************
1807 * BeInitRDC: initialize a RDC structure for the BeOS kernel
1808 *****************************************************************************
1809 * This function initializes a BeOS raw device command structure for future
1810 * use, either a read command or a write command.
1811 *****************************************************************************/
1812 static void BeInitRDC( raw_device_command *p_rdc, int i_type )
1814 memset( p_rdc->data, 0, p_rdc->data_length );
1816 switch( i_type )
1818 case GPCMD_SEND_KEY:
1819 /* leave the flags to 0 */
1820 break;
1822 case GPCMD_READ_DVD_STRUCTURE: case GPCMD_REPORT_KEY:
1823 p_rdc->flags = B_RAW_DEVICE_DATA_IN; break; }
1825 p_rdc->command[ 0 ] = i_type;
1827 p_rdc->command[ 8 ] = (p_rdc->data_length >> 8) & 0xff;
1828 p_rdc->command[ 9 ] = p_rdc->data_length & 0xff;
1829 p_rdc->command_length = 12;
1831 p_rdc->sense_data = NULL;
1832 p_rdc->sense_data_length = 0;
1834 p_rdc->timeout = 1000000;
1836 #endif
1838 #if defined( HPUX_SCTL_IO )
1839 /*****************************************************************************
1840 * HPUXInitSCTL: initialize a sctl_io structure for the HP-UX kernel
1841 *****************************************************************************
1842 * This function initializes a HP-UX command structure for future
1843 * use, either a read command or a write command.
1844 *****************************************************************************/
1845 static void HPUXInitSCTL( struct sctl_io *sctl_io, int i_type )
1847 memset( sctl_io->data, 0, sctl_io->data_length );
1849 switch( i_type )
1851 case GPCMD_SEND_KEY:
1852 /* leave the flags to 0 */
1853 break;
1855 case GPCMD_READ_DVD_STRUCTURE:
1856 case GPCMD_REPORT_KEY:
1857 sctl_io->flags = SCTL_READ;
1858 break;
1861 sctl_io->cdb[ 0 ] = i_type;
1863 sctl_io->cdb[ 8 ] = (sctl_io->data_length >> 8) & 0xff;
1864 sctl_io->cdb[ 9 ] = sctl_io->data_length & 0xff;
1865 sctl_io->cdb_length = 12;
1867 sctl_io->max_msecs = 1000000;
1869 #endif
1871 #if defined( SOLARIS_USCSI )
1872 /*****************************************************************************
1873 * SolarisInitUSCSI: initialize a USCSICMD structure for the Solaris kernel
1874 *****************************************************************************
1875 * This function initializes a Solaris userspace scsi command structure for
1876 * future use, either a read command or a write command.
1877 *****************************************************************************/
1878 static void SolarisInitUSCSI( struct uscsi_cmd *p_sc, int i_type )
1880 union scsi_cdb *rs_cdb;
1881 memset( p_sc->uscsi_cdb, 0, sizeof( union scsi_cdb ) );
1882 memset( p_sc->uscsi_bufaddr, 0, p_sc->uscsi_buflen );
1884 switch( i_type )
1886 case GPCMD_SEND_KEY:
1887 p_sc->uscsi_flags = USCSI_ISOLATE | USCSI_WRITE;
1888 break;
1890 case GPCMD_READ_DVD_STRUCTURE:
1891 case GPCMD_REPORT_KEY:
1892 p_sc->uscsi_flags = USCSI_ISOLATE | USCSI_READ;
1893 break;
1896 rs_cdb = (union scsi_cdb *)p_sc->uscsi_cdb;
1898 rs_cdb->scc_cmd = i_type;
1900 rs_cdb->cdb_opaque[ 8 ] = (p_sc->uscsi_buflen >> 8) & 0xff;
1901 rs_cdb->cdb_opaque[ 9 ] = p_sc->uscsi_buflen & 0xff;
1902 p_sc->uscsi_cdblen = 12;
1904 USCSI_TIMEOUT( p_sc, 15 );
1907 /*****************************************************************************
1908 * SolarisSendUSCSI: send a USCSICMD structure to the Solaris kernel
1909 * for execution
1910 *****************************************************************************
1911 * When available, this function uses the function smedia_uscsi_cmd()
1912 * from Solaris' libsmedia library (Solaris 9 or newer) to execute the
1913 * USCSI command. smedia_uscsi_cmd() allows USCSI commands for
1914 * non-root users on removable media devices on Solaris 9; sending the
1915 * USCSI command directly to the device using the USCSICMD ioctl fails
1916 * with an EPERM error on Solaris 9.
1918 * The code will fall back to the USCSICMD ioctl method, when
1919 * libsmedia.so is not available or does not export the
1920 * smedia_uscsi_cmd() function (on Solaris releases upto and including
1921 * Solaris 8). Fortunatelly, on these old releases non-root users are
1922 * allowed to perform USCSICMD ioctls on removable media devices.
1923 *****************************************************************************/
1924 static int SolarisSendUSCSI( int i_fd, struct uscsi_cmd *p_sc )
1926 void *p_handle;
1928 /* We use static variables to keep track of the libsmedia symbols, which
1929 * is harmless even in a multithreaded program because the library and
1930 * its symbols will always be mapped at the same address. */
1931 static int b_tried = 0;
1932 static int b_have_sm = 0;
1933 static void * (*p_get_handle) ( int32_t );
1934 static int (*p_uscsi_cmd) ( void *, struct uscsi_cmd * );
1935 static int (*p_release_handle) ( void * );
1937 if( !b_tried )
1939 void *p_lib;
1941 p_lib = dlopen( "libsmedia.so", RTLD_NOW );
1942 if( p_lib )
1944 p_get_handle = dlsym( p_lib, "smedia_get_handle" );
1945 p_uscsi_cmd = dlsym( p_lib, "smedia_uscsi_cmd" );
1946 p_release_handle = dlsym( p_lib, "smedia_release_handle" );
1948 if( p_get_handle && p_uscsi_cmd && p_release_handle )
1950 b_have_sm = 1;
1952 else
1954 dlclose( p_lib );
1958 b_tried = 1;
1961 if( b_have_sm && (p_handle = p_get_handle(i_fd)) )
1963 int i_ret = p_uscsi_cmd( p_handle, p_sc );
1964 p_release_handle( p_handle );
1965 return i_ret;
1968 return ioctl( i_fd, USCSICMD, p_sc );
1970 #endif
1972 #if defined( WIN32 )
1973 /*****************************************************************************
1974 * WinInitSPTD: initialize a sptd structure
1975 *****************************************************************************
1976 * This function initializes a SCSI pass through command structure for future
1977 * use, either a read command or a write command.
1978 *****************************************************************************/
1979 static void WinInitSPTD( SCSI_PASS_THROUGH_DIRECT *p_sptd, int i_type )
1981 memset( p_sptd->DataBuffer, 0, p_sptd->DataTransferLength );
1983 switch( i_type )
1985 case GPCMD_SEND_KEY:
1986 p_sptd->DataIn = SCSI_IOCTL_DATA_OUT;
1987 break;
1989 case GPCMD_READ_DVD_STRUCTURE:
1990 case GPCMD_REPORT_KEY:
1991 p_sptd->DataIn = SCSI_IOCTL_DATA_IN;
1992 break;
1995 p_sptd->Cdb[ 0 ] = i_type;
1996 p_sptd->Cdb[ 8 ] = (uint8_t)(p_sptd->DataTransferLength >> 8) & 0xff;
1997 p_sptd->Cdb[ 9 ] = (uint8_t) p_sptd->DataTransferLength & 0xff;
1998 p_sptd->CdbLength = 12;
2000 p_sptd->TimeOutValue = 2;
2003 /*****************************************************************************
2004 * WinInitSSC: initialize a ssc structure for the win32 aspi layer
2005 *****************************************************************************
2006 * This function initializes a ssc raw device command structure for future
2007 * use, either a read command or a write command.
2008 *****************************************************************************/
2009 static void WinInitSSC( struct SRB_ExecSCSICmd *p_ssc, int i_type )
2011 memset( p_ssc->SRB_BufPointer, 0, p_ssc->SRB_BufLen );
2013 switch( i_type )
2015 case GPCMD_SEND_KEY:
2016 p_ssc->SRB_Flags = SRB_DIR_OUT;
2017 break;
2019 case GPCMD_READ_DVD_STRUCTURE:
2020 case GPCMD_REPORT_KEY:
2021 p_ssc->SRB_Flags = SRB_DIR_IN;
2022 break;
2025 p_ssc->SRB_Cmd = SC_EXEC_SCSI_CMD;
2026 p_ssc->SRB_Flags |= SRB_EVENT_NOTIFY;
2028 p_ssc->CDBByte[ 0 ] = i_type;
2030 p_ssc->CDBByte[ 8 ] = (uint8_t)(p_ssc->SRB_BufLen >> 8) & 0xff;
2031 p_ssc->CDBByte[ 9 ] = (uint8_t) p_ssc->SRB_BufLen & 0xff;
2032 p_ssc->SRB_CDBLen = 12;
2034 p_ssc->SRB_SenseLen = SENSE_LEN;
2037 /*****************************************************************************
2038 * WinSendSSC: send a ssc structure to the aspi layer
2039 *****************************************************************************/
2040 static int WinSendSSC( int i_fd, struct SRB_ExecSCSICmd *p_ssc )
2042 HANDLE hEvent = NULL;
2043 struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
2045 hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
2046 if( hEvent == NULL )
2048 return -1;
2051 p_ssc->SRB_PostProc = hEvent;
2052 p_ssc->SRB_HaId = LOBYTE( fd->i_sid );
2053 p_ssc->SRB_Target = HIBYTE( fd->i_sid );
2055 ResetEvent( hEvent );
2056 if( fd->lpSendCommand( (void*) p_ssc ) == SS_PENDING )
2057 WaitForSingleObject( hEvent, INFINITE );
2059 CloseHandle( hEvent );
2061 return p_ssc->SRB_Status == SS_COMP ? 0 : -1;
2063 #endif
2065 #if defined( __QNXNTO__ )
2066 /*****************************************************************************
2067 * QNXInitCPT: initialize a CPT structure for QNX Neutrino
2068 *****************************************************************************
2069 * This function initializes a cpt command structure for future use,
2070 * either a read command or a write command.
2071 *****************************************************************************/
2072 static void QNXInitCPT( CAM_PASS_THRU * p_cpt, int i_type )
2074 switch( i_type )
2076 case GPCMD_SEND_KEY:
2077 p_cpt->cam_flags = CAM_DIR_OUT;
2078 break;
2080 case GPCMD_READ_DVD_STRUCTURE:
2081 case GPCMD_REPORT_KEY:
2082 p_cpt->cam_flags = CAM_DIR_IN;
2083 break;
2086 p_cpt->cam_cdb[0] = i_type;
2088 p_cpt->cam_cdb[ 8 ] = (p_cpt->cam_dxfer_len >> 8) & 0xff;
2089 p_cpt->cam_cdb[ 9 ] = p_cpt->cam_dxfer_len & 0xff;
2090 p_cpt->cam_cdb_len = 12;
2092 p_cpt->cam_timeout = CAM_TIME_DEFAULT;
2094 #endif
2096 #if defined( SYS_OS2 )
2097 /*****************************************************************************
2098 * OS2InitSDC: initialize a SDC structure for the Execute SCSI-command
2099 *****************************************************************************
2100 * This function initializes a OS2 'execute SCSI command' structure for
2101 * future use, either a read command or a write command.
2102 *****************************************************************************/
2103 static void OS2InitSDC( struct OS2_ExecSCSICmd *p_sdc, int i_type )
2105 switch( i_type )
2107 case GPCMD_SEND_KEY:
2108 p_sdc->flags = 0;
2109 break;
2111 case GPCMD_READ_DVD_STRUCTURE:
2112 case GPCMD_REPORT_KEY:
2113 p_sdc->flags = EX_DIRECTION_IN;
2114 break;
2117 p_sdc->command[ 0 ] = i_type;
2118 p_sdc->command[ 8 ] = (p_sdc->data_length >> 8) & 0xff;
2119 p_sdc->command[ 9 ] = p_sdc->data_length & 0xff;
2120 p_sdc->id_code = 0x31304443; // 'CD01'
2121 p_sdc->cmd_length = 12;
2123 #endif