1 /*****************************************************************************
2 * ioctl.c: DVD ioctl replacement function
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: ioctl.c,v 1.3 2001/06/25 11:34:08 sam Exp $
7 * Authors: Markus Kuespert <ltlBeBoy@beosmail.com>
8 * Samuel Hocevar <sam@zoy.org>
9 * Jon Lech Johansen <jon-vl@nanocrew.net>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <string.h> /* memcpy(), memset() */
32 #include <sys/types.h>
36 # include <winioctl.h>
38 # include <netinet/in.h>
39 # include <sys/ioctl.h>
42 #ifdef DVD_STRUCT_IN_SYS_CDIO_H
43 # include <sys/cdio.h>
45 #ifdef DVD_STRUCT_IN_SYS_DVDIO_H
46 # include <sys/dvdio.h>
48 #ifdef DVD_STRUCT_IN_LINUX_CDROM_H
49 # include <linux/cdrom.h>
60 # include "DVDioctl/DVDioctl.h"
65 /*****************************************************************************
66 * Local prototypes, BeOS specific
67 *****************************************************************************/
68 #if defined( SYS_BEOS )
69 static void BeInitRDC ( raw_device_command *, int );
72 /*****************************************************************************
73 * Local prototypes, win32 (aspi) specific
74 *****************************************************************************/
76 static void WinInitSSC ( struct SRB_ExecSCSICmd *, int );
77 static int WinSendSSC ( int, struct SRB_ExecSCSICmd * );
80 /*****************************************************************************
81 * ioctl_ReadCopyright: check whether the disc is encrypted or not
82 *****************************************************************************/
83 int ioctl_ReadCopyright( int i_fd, int i_layer, int *pi_copyright )
87 #if defined( DVD_STRUCT_IN_LINUX_CDROM_H )
90 dvd.type = DVD_STRUCT_COPYRIGHT;
91 dvd.copyright.layer_num = i_layer;
93 i_ret = ioctl( i_fd, DVD_READ_STRUCT, &dvd );
95 *pi_copyright = dvd.copyright.cpst;
97 #elif defined( HAVE_BSD_DVD_STRUCT )
98 struct dvd_struct dvd;
100 dvd.format = DVD_STRUCT_COPYRIGHT;
101 dvd.layer_num = i_layer;
103 i_ret = ioctl( i_fd, DVDIOCREADSTRUCTURE, &dvd );
105 *pi_copyright = dvd.cpst;
107 #elif defined( SYS_BEOS )
108 INIT_RDC( GPCMD_READ_DVD_STRUCTURE, 8 );
110 rdc.command[ 6 ] = i_layer;
111 rdc.command[ 7 ] = DVD_STRUCT_COPYRIGHT;
113 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
115 *pi_copyright = p_buffer[ 4 ];
117 #elif defined( SYS_DARWIN )
118 _dvd_error( dvdcss, "DVD ioctls not fully functional yet, "
119 "assuming disc is encrypted" );
125 #elif defined( WIN32 )
126 if( WIN2K ) /* NT/Win2000/Whistler */
130 SCSI_PASS_THROUGH_DIRECT sptd;
132 memset( &sptd, 0, sizeof( sptd ) );
133 memset( &p_buffer, 0, sizeof( p_buffer ) );
135 /* When using IOCTL_DVD_READ_STRUCTURE and
136 DVD_COPYRIGHT_DESCRIPTOR, CopyrightProtectionType
137 is always 6. So we send a raw scsi command instead. */
139 sptd.Length = sizeof( SCSI_PASS_THROUGH_DIRECT );
141 sptd.DataIn = SCSI_IOCTL_DATA_IN;
142 sptd.DataTransferLength = 8;
143 sptd.TimeOutValue = 2;
144 sptd.DataBuffer = p_buffer;
145 sptd.Cdb[ 0 ] = GPCMD_READ_DVD_STRUCTURE;
146 sptd.Cdb[ 6 ] = i_layer;
147 sptd.Cdb[ 7 ] = DVD_STRUCT_COPYRIGHT;
148 sptd.Cdb[ 8 ] = (8 >> 8) & 0xff;
149 sptd.Cdb[ 9 ] = 8 & 0xff;
151 i_ret = DeviceIoControl( (HANDLE) i_fd,
152 IOCTL_SCSI_PASS_THROUGH_DIRECT,
153 &sptd, sizeof( SCSI_PASS_THROUGH_DIRECT ),
154 &sptd, sizeof( SCSI_PASS_THROUGH_DIRECT ),
155 &tmp, NULL ) ? 0 : -1;
157 *pi_copyright = p_buffer[4];
161 INIT_SSC( GPCMD_READ_DVD_STRUCTURE, 8 );
163 ssc.CDBByte[ 6 ] = i_layer;
164 ssc.CDBByte[ 7 ] = DVD_STRUCT_COPYRIGHT;
166 i_ret = WinSendSSC( i_fd, &ssc );
168 *pi_copyright = p_buffer[ 4 ];
172 /* DVD ioctls unavailable - do as if the ioctl failed */
179 /*****************************************************************************
180 * ioctl_ReadKey: get the disc key
181 *****************************************************************************/
182 int ioctl_ReadKey( int i_fd, int *pi_agid, u8 *p_key )
186 #if defined( DVD_STRUCT_IN_LINUX_CDROM_H )
189 dvd.type = DVD_STRUCT_DISCKEY;
190 dvd.disckey.agid = *pi_agid;
191 memset( dvd.disckey.value, 0, 2048 );
193 i_ret = ioctl( i_fd, DVD_READ_STRUCT, &dvd );
200 memcpy( p_key, dvd.disckey.value, 2048 );
202 #elif defined( HAVE_BSD_DVD_STRUCT )
203 struct dvd_struct dvd;
205 dvd.format = DVD_STRUCT_DISCKEY;
207 memset( dvd.data, 0, 2048 );
209 i_ret = ioctl( i_fd, DVDIOCREADSTRUCTURE, &dvd );
216 memcpy( p_key, dvd.data, 2048 );
218 #elif defined( SYS_BEOS )
219 INIT_RDC( GPCMD_READ_DVD_STRUCTURE, 2048 + 4 );
221 rdc.command[ 7 ] = DVD_STRUCT_DISCKEY;
222 rdc.command[ 10 ] = *pi_agid << 6;
224 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
231 memcpy( p_key, p_buffer + 4, 2048 );
233 #elif defined( SYS_DARWIN )
234 _dvd_error( dvdcss, "DVD ioctls not fully functional yet, "
235 "sending an empty key" );
239 memset( p_key, 0x00, 2048 );
241 #elif defined( WIN32 )
242 if( WIN2K ) /* NT/Win2000/Whistler */
245 u8 buffer[DVD_DISK_KEY_LENGTH];
246 PDVD_COPY_PROTECT_KEY key = (PDVD_COPY_PROTECT_KEY) &buffer;
248 memset( &buffer, 0, sizeof( buffer ) );
250 key->KeyLength = DVD_DISK_KEY_LENGTH;
251 key->SessionId = *pi_agid;
252 key->KeyType = DvdDiskKey;
255 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_READ_KEY, key,
256 key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
263 memcpy( p_key, key->KeyData, 2048 );
267 INIT_SSC( GPCMD_READ_DVD_STRUCTURE, 2048 + 4 );
269 ssc.CDBByte[ 7 ] = DVD_STRUCT_DISCKEY;
270 ssc.CDBByte[ 10 ] = *pi_agid << 6;
272 i_ret = WinSendSSC( i_fd, &ssc );
279 memcpy( p_key, p_buffer + 4, 2048 );
283 /* DVD ioctls unavailable - do as if the ioctl failed */
290 /*****************************************************************************
291 * ioctl_ReportAgid: get AGID from the drive
292 *****************************************************************************/
293 int ioctl_ReportAgid( int i_fd, int *pi_agid )
297 #if defined( DVD_STRUCT_IN_LINUX_CDROM_H )
298 dvd_authinfo auth_info;
300 auth_info.type = DVD_LU_SEND_AGID;
301 auth_info.lsa.agid = *pi_agid;
303 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
305 *pi_agid = auth_info.lsa.agid;
307 #elif defined( HAVE_BSD_DVD_STRUCT )
308 struct dvd_authinfo auth_info;
310 auth_info.format = DVD_REPORT_AGID;
311 auth_info.agid = *pi_agid;
313 i_ret = ioctl( i_fd, DVDIOCREPORTKEY, &auth_info );
315 *pi_agid = auth_info.agid;
317 #elif defined( SYS_BEOS )
318 INIT_RDC( GPCMD_REPORT_KEY, 8 );
320 rdc.command[ 10 ] = DVD_REPORT_AGID | (*pi_agid << 6);
322 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
324 *pi_agid = p_buffer[ 7 ] >> 6;
326 #elif defined( SYS_DARWIN )
329 dvdioctl.i_keyformat = kCSSAGID;
330 dvdioctl.i_agid = *pi_agid;
333 i_ret = ioctl( i_fd, IODVD_REPORT_KEY, &dvdioctl );
335 *pi_agid = p_buffer[ 7 ] >> 6;
337 #elif defined( WIN32 )
338 if( WIN2K ) /* NT/Win2000/Whistler */
343 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_START_SESSION,
344 &tmp, 4, &id, sizeof( id ), &tmp, NULL ) ? 0 : -1;
350 INIT_SSC( GPCMD_REPORT_KEY, 8 );
352 ssc.CDBByte[ 10 ] = DVD_REPORT_AGID | (*pi_agid << 6);
354 i_ret = WinSendSSC( i_fd, &ssc );
356 *pi_agid = p_buffer[ 7 ] >> 6;
360 /* DVD ioctls unavailable - do as if the ioctl failed */
367 /*****************************************************************************
368 * ioctl_ReportChallenge: get challenge from the drive
369 *****************************************************************************/
370 int ioctl_ReportChallenge( int i_fd, int *pi_agid, u8 *p_challenge )
374 #if defined( DVD_STRUCT_IN_LINUX_CDROM_H )
375 dvd_authinfo auth_info;
377 auth_info.type = DVD_LU_SEND_CHALLENGE;
378 auth_info.lsc.agid = *pi_agid;
380 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
382 memcpy( p_challenge, auth_info.lsc.chal, sizeof(dvd_challenge) );
384 #elif defined( HAVE_BSD_DVD_STRUCT )
385 struct dvd_authinfo auth_info;
387 auth_info.format = DVD_REPORT_CHALLENGE;
388 auth_info.agid = *pi_agid;
390 i_ret = ioctl( i_fd, DVDIOCREPORTKEY, &auth_info );
392 memcpy( p_challenge, auth_info.keychal, 10 );
394 #elif defined( SYS_BEOS )
395 INIT_RDC( GPCMD_REPORT_KEY, 16 );
397 rdc.command[ 10 ] = DVD_REPORT_CHALLENGE | (*pi_agid << 6);
399 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
401 memcpy( p_challenge, p_buffer + 4, 12 );
403 #elif defined( SYS_DARWIN )
406 dvdioctl.i_keyformat = kChallengeKey;
407 dvdioctl.i_agid = *pi_agid;
410 i_ret = ioctl( i_fd, IODVD_REPORT_KEY, &dvdioctl );
412 memcpy( p_challenge, p_buffer + 4, 12 );
414 #elif defined( WIN32 )
415 if( WIN2K ) /* NT/Win2000/Whistler */
418 u8 buffer[DVD_CHALLENGE_KEY_LENGTH];
419 PDVD_COPY_PROTECT_KEY key = (PDVD_COPY_PROTECT_KEY) &buffer;
421 memset( &buffer, 0, sizeof( buffer ) );
423 key->KeyLength = DVD_CHALLENGE_KEY_LENGTH;
424 key->SessionId = *pi_agid;
425 key->KeyType = DvdChallengeKey;
428 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_READ_KEY, key,
429 key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
436 memcpy( p_challenge, key->KeyData, 10 );
440 INIT_SSC( GPCMD_REPORT_KEY, 16 );
442 ssc.CDBByte[ 10 ] = DVD_REPORT_CHALLENGE | (*pi_agid << 6);
444 i_ret = WinSendSSC( i_fd, &ssc );
446 memcpy( p_challenge, p_buffer + 4, 12 );
450 /* DVD ioctls unavailable - do as if the ioctl failed */
457 /*****************************************************************************
458 * ioctl_ReportASF: get ASF from the drive
459 *****************************************************************************/
460 int ioctl_ReportASF( int i_fd, int *pi_agid, int *pi_asf )
464 #if defined( DVD_STRUCT_IN_LINUX_CDROM_H )
465 dvd_authinfo auth_info;
467 auth_info.type = DVD_LU_SEND_ASF;
468 auth_info.lsasf.agid = *pi_agid;
469 auth_info.lsasf.asf = *pi_asf;
471 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
473 *pi_asf = auth_info.lsasf.asf;
475 #elif defined( HAVE_BSD_DVD_STRUCT )
476 struct dvd_authinfo auth_info;
478 auth_info.format = DVD_REPORT_ASF;
479 auth_info.agid = *pi_agid;
480 auth_info.asf = *pi_asf;
482 i_ret = ioctl( i_fd, DVDIOCREPORTKEY, &auth_info );
484 *pi_asf = auth_info.asf;
486 #elif defined( SYS_BEOS )
487 INIT_RDC( GPCMD_REPORT_KEY, 8 );
489 rdc.command[ 10 ] = DVD_REPORT_ASF | (*pi_agid << 6);
491 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
493 *pi_asf = p_buffer[ 7 ] & 1;
495 #elif defined( SYS_DARWIN )
498 dvdioctl.i_keyformat = kASF;
499 dvdioctl.i_agid = *pi_agid;
502 i_ret = ioctl( i_fd, IODVD_REPORT_KEY, &dvdioctl );
504 *pi_asf = p_buffer[ 7 ] & 1;
506 #elif defined( WIN32 )
507 if( WIN2K ) /* NT/Win2000/Whistler */
510 u8 buffer[DVD_ASF_LENGTH];
511 PDVD_COPY_PROTECT_KEY key = (PDVD_COPY_PROTECT_KEY) &buffer;
513 memset( &buffer, 0, sizeof( buffer ) );
515 key->KeyLength = DVD_ASF_LENGTH;
516 key->SessionId = *pi_agid;
517 key->KeyType = DvdAsf;
520 ((PDVD_ASF)key->KeyData)->SuccessFlag = *pi_asf;
522 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_READ_KEY, key,
523 key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
530 *pi_asf = ((PDVD_ASF)key->KeyData)->SuccessFlag;
534 INIT_SSC( GPCMD_REPORT_KEY, 8 );
536 ssc.CDBByte[ 10 ] = DVD_REPORT_ASF | (*pi_agid << 6);
538 i_ret = WinSendSSC( i_fd, &ssc );
540 *pi_asf = p_buffer[ 7 ] & 1;
544 /* DVD ioctls unavailable - do as if the ioctl failed */
551 /*****************************************************************************
552 * ioctl_ReportKey1: get the first key from the drive
553 *****************************************************************************/
554 int ioctl_ReportKey1( int i_fd, int *pi_agid, u8 *p_key )
558 #if defined( DVD_STRUCT_IN_LINUX_CDROM_H )
559 dvd_authinfo auth_info;
561 auth_info.type = DVD_LU_SEND_KEY1;
562 auth_info.lsk.agid = *pi_agid;
564 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
566 memcpy( p_key, auth_info.lsk.key, sizeof(dvd_key) );
568 #elif defined( HAVE_BSD_DVD_STRUCT )
569 struct dvd_authinfo auth_info;
571 auth_info.format = DVD_REPORT_KEY1;
572 auth_info.agid = *pi_agid;
574 i_ret = ioctl( i_fd, DVDIOCREPORTKEY, &auth_info );
576 memcpy( p_key, auth_info.keychal, 8 );
578 #elif defined( SYS_BEOS )
579 INIT_RDC( GPCMD_REPORT_KEY, 12 );
581 rdc.command[ 10 ] = DVD_REPORT_KEY1 | (*pi_agid << 6);
583 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
585 memcpy( p_key, p_buffer + 4, 8 );
587 #elif defined( SYS_DARWIN )
590 dvdioctl.i_keyformat = kKey1;
591 dvdioctl.i_agid = *pi_agid;
593 i_ret = ioctl( i_fd, IODVD_SEND_KEY, &dvdioctl );
595 memcpy( p_key, p_buffer + 4, 8 );
597 #elif defined( WIN32 )
598 if( WIN2K ) /* NT/Win2000/Whistler */
601 u8 buffer[DVD_BUS_KEY_LENGTH];
602 PDVD_COPY_PROTECT_KEY key = (PDVD_COPY_PROTECT_KEY) &buffer;
604 memset( &buffer, 0, sizeof( buffer ) );
606 key->KeyLength = DVD_BUS_KEY_LENGTH;
607 key->SessionId = *pi_agid;
608 key->KeyType = DvdBusKey1;
611 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_READ_KEY, key,
612 key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
614 memcpy( p_key, key->KeyData, 8 );
618 INIT_SSC( GPCMD_REPORT_KEY, 12 );
620 ssc.CDBByte[ 10 ] = DVD_REPORT_KEY1 | (*pi_agid << 6);
622 i_ret = WinSendSSC( i_fd, &ssc );
624 memcpy( p_key, p_buffer + 4, 8 );
628 /* DVD ioctls unavailable - do as if the ioctl failed */
635 /*****************************************************************************
636 * ioctl_InvalidateAgid: invalidate the current AGID
637 *****************************************************************************/
638 int ioctl_InvalidateAgid( int i_fd, int *pi_agid )
642 #if defined( DVD_STRUCT_IN_LINUX_CDROM_H )
643 dvd_authinfo auth_info;
645 auth_info.type = DVD_INVALIDATE_AGID;
646 auth_info.lsa.agid = *pi_agid;
648 i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );
650 *pi_agid = auth_info.lsa.agid;
652 #elif defined( HAVE_BSD_DVD_STRUCT )
653 struct dvd_authinfo auth_info;
655 auth_info.format = DVD_INVALIDATE_AGID;
656 auth_info.agid = *pi_agid;
658 i_ret = ioctl( i_fd, DVDIOCREPORTKEY, &auth_info );
660 *pi_agid = auth_info.agid;
662 #elif defined( SYS_BEOS )
663 INIT_RDC( GPCMD_REPORT_KEY, 0 );
665 rdc.command[ 10 ] = DVD_INVALIDATE_AGID | (*pi_agid << 6);
667 i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
669 #elif defined( SYS_DARWIN )
672 dvdioctl.i_keyformat = kInvalidateAGID;
673 dvdioctl.i_agid = *pi_agid;
675 i_ret = ioctl( i_fd, IODVD_SEND_KEY, &dvdioctl );
677 #elif defined( WIN32 )
678 if( WIN2K ) /* NT/Win2000/Whistler */
682 i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_END_SESSION,
683 pi_agid, sizeof( *pi_agid ), NULL, 0, &tmp, NULL ) ? 0 : -1;
687 #if defined( __MINGW32__ )
688 INIT_SSC( GPCMD_REPORT_KEY, 0 );
690 INIT_SSC( GPCMD_REPORT_KEY, 1 );
693 ssc.CDBByte[ 8 ] = 0;
694 ssc.CDBByte[ 9 ] = 0;
697 ssc.CDBByte[ 10 ] = DVD_INVALIDATE_AGID | (*pi_agid << 6);
699 i_ret = WinSendSSC( i_fd, &ssc );
703 /* DVD ioctls unavailable - do as if the ioctl failed */
710 /*****************************************************************************
711 * ioctl_SendChallenge: send challenge to the drive
712 *****************************************************************************/
713 int ioctl_SendChallenge( int i_fd, int *pi_agid, u8 *p_challenge )
715 #if defined( DVD_STRUCT_IN_LINUX_CDROM_H )
716 dvd_authinfo auth_info;
718 auth_info.type = DVD_HOST_SEND_CHALLENGE;
719 auth_info.hsc.agid = *pi_agid;
721 memcpy( auth_info.hsc.chal, p_challenge, sizeof(dvd_challenge) );
723 return ioctl( i_fd, DVD_AUTH, &auth_info );
725 #elif defined( HAVE_BSD_DVD_STRUCT )
726 struct dvd_authinfo auth_info;
728 auth_info.format = DVD_SEND_CHALLENGE;
729 auth_info.agid = *pi_agid;
731 memcpy( auth_info.keychal, p_challenge, 12 );
733 return ioctl( i_fd, DVDIOCSENDKEY, &auth_info );
735 #elif defined( SYS_BEOS )
736 INIT_RDC( GPCMD_SEND_KEY, 16 );
738 rdc.command[ 10 ] = DVD_SEND_CHALLENGE | (*pi_agid << 6);
741 memcpy( p_buffer + 4, p_challenge, 12 );
743 return ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
745 #elif defined( SYS_DARWIN )
748 dvdioctl.i_keyformat = kChallengeKey;
749 dvdioctl.i_agid = *pi_agid;
752 memcpy( p_buffer + 4, p_challenge, 12 );
754 return ioctl( i_fd, IODVD_SEND_KEY, &dvdioctl );
756 #elif defined( WIN32 )
757 if( WIN2K ) /* NT/Win2000/Whistler */
760 u8 buffer[DVD_CHALLENGE_KEY_LENGTH];
761 PDVD_COPY_PROTECT_KEY key = (PDVD_COPY_PROTECT_KEY) &buffer;
763 memset( &buffer, 0, sizeof( buffer ) );
765 key->KeyLength = DVD_CHALLENGE_KEY_LENGTH;
766 key->SessionId = *pi_agid;
767 key->KeyType = DvdChallengeKey;
770 memcpy( key->KeyData, p_challenge, 10 );
772 return DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_SEND_KEY, key,
773 key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
777 INIT_SSC( GPCMD_SEND_KEY, 16 );
779 ssc.CDBByte[ 10 ] = DVD_SEND_CHALLENGE | (*pi_agid << 6);
782 memcpy( p_buffer + 4, p_challenge, 12 );
784 return WinSendSSC( i_fd, &ssc );
788 /* DVD ioctls unavailable - do as if the ioctl failed */
794 /*****************************************************************************
795 * ioctl_SendKey2: send the second key to the drive
796 *****************************************************************************/
797 int ioctl_SendKey2( int i_fd, int *pi_agid, u8 *p_key )
799 #if defined( DVD_STRUCT_IN_LINUX_CDROM_H )
800 dvd_authinfo auth_info;
802 auth_info.type = DVD_HOST_SEND_KEY2;
803 auth_info.hsk.agid = *pi_agid;
805 memcpy( auth_info.hsk.key, p_key, sizeof(dvd_key) );
807 return ioctl( i_fd, DVD_AUTH, &auth_info );
809 #elif defined( HAVE_BSD_DVD_STRUCT )
810 struct dvd_authinfo auth_info;
812 auth_info.format = DVD_SEND_KEY2;
813 auth_info.agid = *pi_agid;
815 memcpy( auth_info.keychal, p_key, 8 );
817 return ioctl( i_fd, DVDIOCSENDKEY, &auth_info );
819 #elif defined( SYS_BEOS )
820 INIT_RDC( GPCMD_SEND_KEY, 12 );
822 rdc.command[ 10 ] = DVD_SEND_KEY2 | (*pi_agid << 6);
825 memcpy( p_buffer + 4, p_key, 8 );
827 return ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
829 #elif defined( WIN32 )
830 if( WIN2K ) /* NT/Win2000/Whistler */
833 u8 buffer[DVD_BUS_KEY_LENGTH];
834 PDVD_COPY_PROTECT_KEY key = (PDVD_COPY_PROTECT_KEY) &buffer;
836 memset( &buffer, 0, sizeof( buffer ) );
838 key->KeyLength = DVD_BUS_KEY_LENGTH;
839 key->SessionId = *pi_agid;
840 key->KeyType = DvdBusKey2;
843 memcpy( key->KeyData, p_key, 8 );
845 return DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_SEND_KEY, key,
846 key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
850 INIT_SSC( GPCMD_SEND_KEY, 12 );
852 ssc.CDBByte[ 10 ] = DVD_SEND_KEY2 | (*pi_agid << 6);
855 memcpy( p_buffer + 4, p_key, 8 );
857 return WinSendSSC( i_fd, &ssc );
860 #elif defined( SYS_DARWIN )
863 dvdioctl.i_keyformat = kKey2;
864 dvdioctl.i_agid = *pi_agid;
867 memcpy( p_buffer + 4, p_key, 8 );
869 return ioctl( i_fd, IODVD_SEND_KEY, &dvdioctl );
872 /* DVD ioctls unavailable - do as if the ioctl failed */
878 /* Local prototypes */
880 #if defined( SYS_BEOS )
881 /*****************************************************************************
882 * BeInitRDC: initialize a RDC structure for the BeOS kernel
883 *****************************************************************************
884 * This function initializes a BeOS raw device command structure for future
885 * use, either a read command or a write command.
886 *****************************************************************************/
887 static void BeInitRDC( raw_device_command *p_rdc, int i_type )
889 memset( p_rdc->data, 0, p_rdc->data_length );
894 /* leave the flags to 0 */
897 case GPCMD_READ_DVD_STRUCTURE:
898 case GPCMD_REPORT_KEY:
899 p_rdc->flags = B_RAW_DEVICE_DATA_IN;
903 p_rdc->command[ 0 ] = i_type;
905 p_rdc->command[ 8 ] = (p_rdc->data_length >> 8) & 0xff;
906 p_rdc->command[ 9 ] = p_rdc->data_length & 0xff;
907 p_rdc->command_length = 12;
909 p_rdc->sense_data = NULL;
910 p_rdc->sense_data_length = 0;
912 p_rdc->timeout = 1000000;
917 /*****************************************************************************
918 * WinInitSSC: initialize a ssc structure for the win32 aspi layer
919 *****************************************************************************
920 * This function initializes a ssc raw device command structure for future
921 * use, either a read command or a write command.
922 *****************************************************************************/
923 static void WinInitSSC( struct SRB_ExecSCSICmd *p_ssc, int i_type )
925 memset( p_ssc->SRB_BufPointer, 0, p_ssc->SRB_BufLen );
930 p_ssc->SRB_Flags = SRB_DIR_OUT;
933 case GPCMD_READ_DVD_STRUCTURE:
934 case GPCMD_REPORT_KEY:
935 p_ssc->SRB_Flags = SRB_DIR_IN;
939 p_ssc->SRB_Cmd = SC_EXEC_SCSI_CMD;
940 p_ssc->SRB_Flags |= SRB_EVENT_NOTIFY;
942 p_ssc->CDBByte[ 0 ] = i_type;
944 p_ssc->CDBByte[ 8 ] = (u8)(p_ssc->SRB_BufLen >> 8) & 0xff;
945 p_ssc->CDBByte[ 9 ] = (u8) p_ssc->SRB_BufLen & 0xff;
946 p_ssc->SRB_CDBLen = 12;
948 p_ssc->SRB_SenseLen = SENSE_LEN;
951 /*****************************************************************************
952 * WinSendSSC: send a ssc structure to the aspi layer
953 *****************************************************************************/
954 static int WinSendSSC( int i_fd, struct SRB_ExecSCSICmd *p_ssc )
956 HANDLE hEvent = NULL;
957 struct w32_aspidev *fd = (struct w32_aspidev *) i_fd;
959 hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
965 p_ssc->SRB_PostProc = hEvent;
966 p_ssc->SRB_HaId = LOBYTE( fd->i_sid );
967 p_ssc->SRB_Target = HIBYTE( fd->i_sid );
969 ResetEvent( hEvent );
970 if( fd->lpSendCommand( (void*) p_ssc ) == SS_PENDING )
971 WaitForSingleObject( hEvent, INFINITE );
973 CloseHandle( hEvent );
975 return p_ssc->SRB_Status == SS_COMP ? 0 : -1;