1 /****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: cdrom.c,v 1.12 2003/05/22 12:00:57 gbazin Exp $
7 * Authors: Johan Bilien <jobi@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
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 *****************************************************************************/
39 #include <sys/types.h>
44 #ifdef HAVE_SYS_IOCTL_H
45 # include <sys/ioctl.h>
48 #if defined( SYS_BSDI )
50 #elif defined ( SYS_DARWIN )
51 # include <CoreFoundation/CFBase.h>
52 # include <IOKit/IOKitLib.h>
53 # include <IOKit/storage/IOCDTypes.h>
54 # include <IOKit/storage/IOCDMedia.h>
55 # include <IOKit/storage/IOCDMediaBSDClient.h>
56 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
57 # include <sys/inttypes.h>
58 # include <sys/cdio.h>
59 # include <sys/scsiio.h>
60 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
61 # include <sys/cdio.h>
62 # include <sys/cdrio.h>
63 #elif defined( WIN32 )
65 # include <winioctl.h>
67 # include <linux/cdrom.h>
70 #include "cdrom_internals.h"
73 /*****************************************************************************
74 * ioctl_Open: Opens a VCD device or file and returns an opaque handle
75 *****************************************************************************/
76 vcddev_t *ioctl_Open( vlc_object_t *p_this, const char *psz_dev )
85 if( !psz_dev ) return NULL;
88 * Initialize structure with default values
90 p_vcddev = (vcddev_t *)malloc( sizeof(vcddev_t) );
91 if( p_vcddev == NULL )
93 msg_Err( p_this, "out of memory" );
96 p_vcddev->i_vcdimage_handle = -1;
97 p_vcddev->psz_dev = NULL;
101 * Check if we are dealing with a device or a file (vcd image)
104 if( (strlen( psz_dev ) == 2 && psz_dev[1] == ':') )
110 if( stat( psz_dev, &fileinfo ) < 0 )
116 /* Check if this is a block/char device */
117 if( S_ISBLK( fileinfo.st_mode ) || S_ISCHR( fileinfo.st_mode ) )
123 i_ret = OpenVCDImage( p_this, psz_dev, p_vcddev );
128 * open the vcd device
132 i_ret = win32_vcd_open( p_this, psz_dev, p_vcddev );
134 p_vcddev->i_device_handle = -1;
135 p_vcddev->i_device_handle = open( psz_dev, O_RDONLY | O_NONBLOCK );
136 i_ret = (p_vcddev->i_device_handle == -1) ? -1 : 0;
142 p_vcddev->psz_dev = (char *)strdup( psz_dev );
153 /*****************************************************************************
154 * ioctl_Close: Closes an already opened VCD device or file.
155 *****************************************************************************/
156 void ioctl_Close( vlc_object_t * p_this, vcddev_t *p_vcddev )
158 if( p_vcddev->psz_dev ) free( p_vcddev->psz_dev );
160 if( p_vcddev->i_vcdimage_handle != -1 )
166 CloseVCDImage( p_this, p_vcddev );
175 if( p_vcddev->h_device_handle )
176 CloseHandle( p_vcddev->h_device_handle );
177 if( p_vcddev->hASPI )
178 FreeLibrary( (HMODULE)p_vcddev->hASPI );
180 if( p_vcddev->i_device_handle != -1 )
181 close( p_vcddev->i_device_handle );
185 /*****************************************************************************
186 * ioctl_GetTracksMap: Read the Table of Content, fill in the pp_sectors map
187 * if pp_sectors is not null and return the number of
189 *****************************************************************************/
190 int ioctl_GetTracksMap( vlc_object_t *p_this, const vcddev_t *p_vcddev,
195 if( p_vcddev->i_vcdimage_handle != -1 )
201 i_tracks = p_vcddev->i_tracks;
205 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
206 if( *pp_sectors == NULL )
208 msg_Err( p_this, "out of memory" );
211 memcpy( *pp_sectors, p_vcddev->p_sectors,
212 (i_tracks + 1) * sizeof(int) );
224 #if defined( SYS_DARWIN )
229 if( ( pTOC = darwin_getTOC( p_this, p_vcddev ) ) == NULL )
231 msg_Err( p_this, "failed to get the TOC" );
235 i_descriptors = darwin_getNumberOfDescriptors( pTOC );
236 i_tracks = darwin_getNumberOfTracks( pTOC, i_descriptors );
240 int i, i_leadout = -1;
241 CDTOCDescriptor *pTrackDescriptors;
244 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
245 if( *pp_sectors == NULL )
247 msg_Err( p_this, "out of memory" );
248 darwin_freeTOC( pTOC );
252 pTrackDescriptors = pTOC->descriptors;
254 for( i_tracks = 0, i = 0; i <= i_descriptors; i++ )
256 track = pTrackDescriptors[i].point;
261 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
264 (*pp_sectors)[i_tracks++] =
265 CDConvertMSFToLBA( pTrackDescriptors[i].p );
268 if( i_leadout == -1 )
270 msg_Err( p_this, "leadout not found" );
272 darwin_freeTOC( pTOC );
276 /* set leadout sector */
277 (*pp_sectors)[i_tracks] =
278 CDConvertMSFToLBA( pTrackDescriptors[i_leadout].p );
281 darwin_freeTOC( pTOC );
283 #elif defined( WIN32 )
284 if( p_vcddev->hASPI )
287 struct SRB_ExecSCSICmd ssc;
288 byte_t p_tocheader[ 4 ];
290 /* Create the transfer completion event */
291 hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
297 memset( &ssc, 0, sizeof( ssc ) );
299 ssc.SRB_Cmd = SC_EXEC_SCSI_CMD;
300 ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
301 ssc.SRB_HaId = LOBYTE( p_vcddev->i_sid );
302 ssc.SRB_Target = HIBYTE( p_vcddev->i_sid );
303 ssc.SRB_SenseLen = SENSE_LEN;
305 ssc.SRB_PostProc = (LPVOID) hEvent;
309 ssc.CDBByte[ 0 ] = READ_TOC;
312 ssc.CDBByte[ 2 ] = READ_TOC_FORMAT_TOC;
315 ssc.CDBByte[ 6 ] = 0;
317 /* Allocation length and buffer */
318 ssc.SRB_BufLen = sizeof( p_tocheader );
319 ssc.SRB_BufPointer = p_tocheader;
320 ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >> 8 ) & 0xff;
321 ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen ) & 0xff;
323 /* Initiate transfer */
324 ResetEvent( hEvent );
325 p_vcddev->lpSendCommand( (void*) &ssc );
327 /* If the command has still not been processed, wait until it's
329 if( ssc.SRB_Status == SS_PENDING )
330 WaitForSingleObject( hEvent, INFINITE );
332 /* check that the transfer went as planned */
333 if( ssc.SRB_Status != SS_COMP )
335 CloseHandle( hEvent );
339 i_tracks = p_tocheader[3] - p_tocheader[2] + 1;
346 i_toclength = 4 /* header */ + p_tocheader[0] +
347 ((unsigned int)p_tocheader[1] << 8);
349 p_fulltoc = malloc( i_toclength );
350 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
352 if( *pp_sectors == NULL || p_fulltoc == NULL )
354 if( *pp_sectors ) free( *pp_sectors );
355 if( p_fulltoc ) free( p_fulltoc );
356 msg_Err( p_this, "out of memory" );
357 CloseHandle( hEvent );
361 /* Allocation length and buffer */
362 ssc.SRB_BufLen = i_toclength;
363 ssc.SRB_BufPointer = p_fulltoc;
364 ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >> 8 ) & 0xff;
365 ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen ) & 0xff;
367 /* Initiate transfer */
368 ResetEvent( hEvent );
369 p_vcddev->lpSendCommand( (void*) &ssc );
371 /* If the command has still not been processed, wait until it's
373 if( ssc.SRB_Status == SS_PENDING )
374 WaitForSingleObject( hEvent, INFINITE );
376 /* check that the transfer went as planned */
377 if( ssc.SRB_Status != SS_COMP )
380 for( i = 0 ; i <= i_tracks ; i++ )
382 int i_index = 8 + 8 * i;
383 (*pp_sectors)[ i ] = ((int)p_fulltoc[ i_index ] << 24) +
384 ((int)p_fulltoc[ i_index+1 ] << 16) +
385 ((int)p_fulltoc[ i_index+2 ] << 8) +
386 (int)p_fulltoc[ i_index+3 ];
388 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
394 CloseHandle( hEvent );
399 DWORD dwBytesReturned;
402 if( DeviceIoControl( p_vcddev->h_device_handle,
403 IOCTL_CDROM_READ_TOC,
404 NULL, 0, &cdrom_toc, sizeof(CDROM_TOC),
405 &dwBytesReturned, NULL ) == 0 )
407 msg_Err( p_this, "could not read TOCHDR" );
411 i_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1;
417 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
418 if( *pp_sectors == NULL )
420 msg_Err( p_this, "out of memory" );
424 for( i = 0 ; i <= i_tracks ; i++ )
426 (*pp_sectors)[ i ] = MSF_TO_LBA2(
427 cdrom_toc.TrackData[i].Address[1],
428 cdrom_toc.TrackData[i].Address[2],
429 cdrom_toc.TrackData[i].Address[3] );
430 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
435 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H ) \
436 || defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
437 struct ioc_toc_header tochdr;
438 struct ioc_read_toc_entry toc_entries;
440 if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCHEADER, &tochdr )
443 msg_Err( p_this, "could not read TOCHDR" );
447 i_tracks = tochdr.ending_track - tochdr.starting_track + 1;
453 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
454 if( *pp_sectors == NULL )
456 msg_Err( p_this, "out of memory" );
460 toc_entries.address_format = CD_LBA_FORMAT;
461 toc_entries.starting_track = 0;
462 toc_entries.data_len = ( i_tracks + 1 ) *
463 sizeof( struct cd_toc_entry );
464 toc_entries.data = (struct cd_toc_entry *)
465 malloc( toc_entries.data_len );
466 if( toc_entries.data == NULL )
468 msg_Err( p_this, "out of memory" );
474 if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCENTRYS,
475 &toc_entries ) == -1 )
477 msg_Err( p_this, "could not read the TOC" );
479 free( toc_entries.data );
483 /* Fill the p_sectors structure with the track/sector matches */
484 for( i = 0 ; i <= i_tracks ; i++ )
486 #if defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
487 /* FIXME: is this ok? */
488 (*pp_sectors)[ i ] = toc_entries.data[i].addr.lba;
490 (*pp_sectors)[ i ] = ntohl( toc_entries.data[i].addr.lba );
495 struct cdrom_tochdr tochdr;
496 struct cdrom_tocentry tocent;
498 /* First we read the TOC header */
499 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCHDR, &tochdr )
502 msg_Err( p_this, "could not read TOCHDR" );
506 i_tracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
512 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
513 if( *pp_sectors == NULL )
515 msg_Err( p_this, "out of memory" );
519 /* Fill the p_sectors structure with the track/sector matches */
520 for( i = 0 ; i <= i_tracks ; i++ )
522 tocent.cdte_format = CDROM_LBA;
524 ( i == i_tracks ) ? CDROM_LEADOUT : tochdr.cdth_trk0 + i;
526 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCENTRY,
529 msg_Err( p_this, "could not read TOCENTRY" );
534 (*pp_sectors)[ i ] = tocent.cdte_addr.lba;
543 /****************************************************************************
544 * ioctl_ReadSector: Read VCD or CDDA sectors
545 ****************************************************************************/
546 int ioctl_ReadSectors( vlc_object_t *p_this, const vcddev_t *p_vcddev,
547 int i_sector, byte_t * p_buffer, int i_nb, int i_type )
552 if( i_type == VCD_TYPE ) p_block = malloc( VCD_SECTOR_SIZE * i_nb );
553 else p_block = p_buffer;
555 if( p_vcddev->i_vcdimage_handle != -1 )
560 if( lseek( p_vcddev->i_vcdimage_handle, i_sector * VCD_SECTOR_SIZE,
563 msg_Err( p_this, "Could not lseek to sector %d", i_sector );
564 if( i_type == VCD_TYPE ) free( p_block );
568 if( read( p_vcddev->i_vcdimage_handle, p_block, VCD_SECTOR_SIZE * i_nb)
571 msg_Err( p_this, "Could not read sector %d", i_sector );
572 if( i_type == VCD_TYPE ) free( p_block );
584 #if defined( SYS_DARWIN )
585 dk_cd_read_t cd_read;
587 memset( &cd_read, 0, sizeof(cd_read) );
589 cd_read.offset = i_sector * VCD_SECTOR_SIZE;
590 cd_read.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader |
591 kCDSectorAreaSubHeader | kCDSectorAreaUser |
592 kCDSectorAreaAuxiliary;
593 cd_read.sectorType = kCDSectorTypeUnknown;
595 cd_read.buffer = p_block;
596 cd_read.bufferLength = VCD_SECTOR_SIZE * i_nb;
598 if( ioctl( p_vcddev->i_device_handle, DKIOCCDREAD, &cd_read ) == -1 )
600 msg_Err( p_this, "could not read block %d", i_sector );
601 if( i_type == VCD_TYPE ) free( p_block );
605 #elif defined( WIN32 )
606 if( p_vcddev->hASPI )
609 struct SRB_ExecSCSICmd ssc;
611 /* Create the transfer completion event */
612 hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
615 if( i_type == VCD_TYPE ) free( p_block );
619 memset( &ssc, 0, sizeof( ssc ) );
621 ssc.SRB_Cmd = SC_EXEC_SCSI_CMD;
622 ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
623 ssc.SRB_HaId = LOBYTE( p_vcddev->i_sid );
624 ssc.SRB_Target = HIBYTE( p_vcddev->i_sid );
625 ssc.SRB_SenseLen = SENSE_LEN;
627 ssc.SRB_PostProc = (LPVOID) hEvent;
631 ssc.CDBByte[ 0 ] = READ_CD;
634 ssc.CDBByte[ 1 ] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2 :
638 ssc.CDBByte[ 2 ] = ( i_sector >> 24 ) & 0xff;
639 ssc.CDBByte[ 3 ] = ( i_sector >> 16 ) & 0xff;
640 ssc.CDBByte[ 4 ] = ( i_sector >> 8 ) & 0xff;
641 ssc.CDBByte[ 5 ] = ( i_sector ) & 0xff;
643 /* Transfer length */
644 ssc.CDBByte[ 6 ] = ( i_nb >> 16 ) & 0xff;
645 ssc.CDBByte[ 7 ] = ( i_nb >> 8 ) & 0xff;
646 ssc.CDBByte[ 8 ] = ( i_nb ) & 0xff;
649 ssc.CDBByte[ 9 ] = i_type == VCD_TYPE ? READ_CD_RAW_MODE2 :
653 ssc.SRB_BufPointer = p_block;
654 ssc.SRB_BufLen = VCD_SECTOR_SIZE * i_nb;
656 /* Initiate transfer */
657 ResetEvent( hEvent );
658 p_vcddev->lpSendCommand( (void*) &ssc );
660 /* If the command has still not been processed, wait until it's
662 if( ssc.SRB_Status == SS_PENDING )
664 WaitForSingleObject( hEvent, INFINITE );
666 CloseHandle( hEvent );
668 /* check that the transfer went as planned */
669 if( ssc.SRB_Status != SS_COMP )
671 if( i_type == VCD_TYPE ) free( p_block );
677 DWORD dwBytesReturned;
678 RAW_READ_INFO cdrom_raw;
680 /* Initialize CDROM_RAW_READ structure */
681 cdrom_raw.DiskOffset.QuadPart = CD_SECTOR_SIZE * i_sector;
682 cdrom_raw.SectorCount = i_nb;
683 cdrom_raw.TrackMode = i_type == VCD_TYPE ? XAForm2 : CDDA;
685 if( DeviceIoControl( p_vcddev->h_device_handle,
686 IOCTL_CDROM_RAW_READ, &cdrom_raw,
687 sizeof(RAW_READ_INFO), p_block,
688 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
691 if( i_type == VCD_TYPE ) free( p_block );
696 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
700 memset( &sc, 0, sizeof(sc) );
702 sc.cmd[1] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2:
704 sc.cmd[2] = (i_sector >> 24) & 0xff;
705 sc.cmd[3] = (i_sector >> 16) & 0xff;
706 sc.cmd[4] = (i_sector >> 8) & 0xff;
707 sc.cmd[5] = (i_sector >> 0) & 0xff;
708 sc.cmd[6] = (i_nb >> 16) & 0xff;
709 sc.cmd[7] = (i_nb >> 8) & 0xff;
710 sc.cmd[8] = (i_nb ) & 0xff;
711 sc.cmd[9] = i_type == VCD_TYPE ? READ_CD_RAW_MODE2 : READ_CD_USERDATA;
712 sc.cmd[10] = 0; /* sub channel */
714 sc.databuf = (caddr_t)p_block;
715 sc.datalen = VCD_SECTOR_SIZE * i_nb;
716 sc.senselen = sizeof( sc.sense );
717 sc.flags = SCCMD_READ;
720 i_ret = ioctl( i_fd, SCIOCCOMMAND, &sc );
723 msg_Err( p_this, "SCIOCCOMMAND failed" );
724 if( i_type == VCD_TYPE ) free( p_block );
727 if( sc.retsts || sc.error )
729 msg_Err( p_this, "SCSI command failed: status %d error %d\n",
730 sc.retsts, sc.error );
731 if( i_type == VCD_TYPE ) free( p_block );
735 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
736 int i_size = VCD_SECTOR_SIZE;
738 if( ioctl( p_vcddev->i_device_handle, CDRIOCSETBLOCKSIZE, &i_size )
741 msg_Err( p_this, "Could not set block size" );
742 if( i_type == VCD_TYPE ) free( p_block );
746 if( lseek( p_vcddev->i_device_handle,
747 i_sector * VCD_SECTOR_SIZE, SEEK_SET ) == -1 )
749 msg_Err( p_this, "Could not lseek to sector %d", i_sector );
750 if( i_type == VCD_TYPE ) free( p_block );
754 if( read( p_vcddev->i_device_handle,
755 p_block, VCD_SECTOR_SIZE * i_nb ) == -1 )
757 msg_Err( p_this, "Could not read sector %d", i_sector );
758 if( i_type == VCD_TYPE ) free( p_block );
763 for( i = 0; i < i_nb; i++ )
765 int i_dummy = i_sector + i + 2 * CD_FRAMES;
767 #define p_msf ((struct cdrom_msf0 *)(p_block + i * VCD_SECTOR_SIZE))
768 p_msf->minute = i_dummy / (CD_FRAMES * CD_SECS);
769 p_msf->second = ( i_dummy % (CD_FRAMES * CD_SECS) ) / CD_FRAMES;
770 p_msf->frame = ( i_dummy % (CD_FRAMES * CD_SECS) ) % CD_FRAMES;
773 if( ioctl( p_vcddev->i_device_handle, CDROMREADRAW,
774 p_block + i * VCD_SECTOR_SIZE ) == -1 )
776 msg_Err( p_this, "could not read block %i from disc",
781 if( i_type == VCD_TYPE ) free( p_block );
790 /* For VCDs, we don't want to keep the header and footer of the
792 if( i_type == VCD_TYPE )
794 for( i = 0; i < i_nb; i++ )
796 memcpy( p_buffer + i * VCD_DATA_SIZE,
797 p_block + i * VCD_SECTOR_SIZE + VCD_DATA_START,
806 /****************************************************************************
808 ****************************************************************************/
810 /****************************************************************************
811 * OpenVCDImage: try to open a vcd image from a .cue file
812 ****************************************************************************/
813 static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
818 char *psz_vcdfile = NULL;
819 char *psz_cuefile = NULL;
823 /* Check if we are dealing with a .cue file */
824 p_pos = strrchr( psz_dev, '.' );
825 if( p_pos && !strcmp( p_pos, ".cue" ) )
827 psz_cuefile = strdup( psz_dev );
831 /* psz_dev must be the actual vcd file. Let's assume there's a .cue
832 * file with the same filename */
835 psz_cuefile = malloc( p_pos - psz_dev + 5 /* ".cue" */ );
836 strncpy( psz_cuefile, psz_dev, p_pos - psz_dev );
837 strcpy( psz_cuefile + (p_pos - psz_dev), ".cue");
841 psz_cuefile = malloc( strlen(psz_dev) + 5 /* ".cue" */ );
842 sprintf( psz_cuefile, "%s.cue", psz_dev );
846 /* Open the cue file and try to parse it */
847 msg_Dbg( p_this,"trying .cue file: %s", psz_cuefile );
848 cuefile = fopen( psz_cuefile, "rt" );
849 if( cuefile && fscanf( cuefile, "FILE %c", line ) &&
850 fgets( line, 1024, cuefile ) )
852 p_pos = strchr( line, '"' );
857 /* Take care of path standardization */
858 if( *line != '/' && ((p_pos = strrchr( psz_cuefile, '/' ))
859 || (p_pos = strrchr( psz_cuefile, '\\' ) )) )
861 psz_vcdfile = malloc( strlen(line) +
862 (p_pos - psz_cuefile + 1) + 1 );
863 strncpy( psz_vcdfile, psz_cuefile, (p_pos - psz_cuefile + 1) );
864 strcpy( psz_vcdfile + (p_pos - psz_cuefile + 1), line );
866 else psz_vcdfile = strdup( line );
872 msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
873 p_vcddev->i_vcdimage_handle = open( psz_vcdfile,
874 O_RDONLY | O_NONBLOCK | O_BINARY );
875 i_ret = (p_vcddev->i_vcdimage_handle == -1) ? -1 : 0;
878 /* Try to parse the i_tracks and p_sectors info so we can just forget
879 * about the cuefile */
887 while( fgets( line, 1024, cuefile ) )
889 /* look for a TRACK line */
890 if( !sscanf( line, "%9s", psz_dummy ) ||
891 strcmp(psz_dummy, "TRACK") )
894 /* look for an INDEX line */
895 while( fgets( line, 1024, cuefile ) )
897 int i_min, i_sec, i_frame;
899 if( (sscanf( line, "%9s %2u %2u:%2u:%2u", psz_dummy, &i_num,
900 &i_min, &i_sec, &i_frame ) != 5) || (i_num != 1) )
904 p_sectors[i_tracks - 1] = MSF_TO_LBA(i_min, i_sec, i_frame);
905 msg_Dbg( p_this, "vcd track %i begins at sector:%i",
906 i_tracks - 1, p_sectors[i_tracks - 1] );
911 /* fill in the last entry */
912 p_sectors[i_tracks] = lseek(p_vcddev->i_vcdimage_handle, 0, SEEK_END)
914 msg_Dbg( p_this, "vcd track %i, begins at sector:%i",
915 i_tracks, p_sectors[i_tracks] );
916 p_vcddev->i_tracks = i_tracks;
917 p_vcddev->p_sectors = malloc( (i_tracks + 1) * sizeof(int) );
918 memcpy( p_vcddev->p_sectors, p_sectors, (i_tracks + 1) * sizeof(int) );
922 if( cuefile ) fclose( cuefile );
923 if( psz_cuefile ) free( psz_cuefile );
924 if( psz_vcdfile ) free( psz_vcdfile );
929 /****************************************************************************
930 * CloseVCDImage: closes a vcd image opened by OpenVCDImage
931 ****************************************************************************/
932 static void CloseVCDImage( vlc_object_t * p_this, vcddev_t *p_vcddev )
934 if( p_vcddev->i_vcdimage_handle != -1 )
935 close( p_vcddev->i_vcdimage_handle );
939 if( p_vcddev->p_sectors )
940 free( p_vcddev->p_sectors );
943 #if defined( SYS_DARWIN )
944 /****************************************************************************
945 * darwin_getTOC: get the TOC
946 ****************************************************************************/
947 static CDTOC *darwin_getTOC( vlc_object_t * p_this, const vcddev_t *p_vcddev )
953 io_iterator_t iterator;
954 io_registry_entry_t service;
955 CFMutableDictionaryRef properties;
958 /* get the device name */
959 if( ( psz_devname = strrchr( p_vcddev->psz_dev, '/') ) != NULL )
962 psz_devname = p_vcddev->psz_dev;
964 /* unraw the device name */
965 if( *psz_devname == 'r' )
968 /* get port for IOKit communication */
969 if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS )
971 msg_Err( p_this, "IOMasterPort: 0x%08x", ret );
975 /* get service iterator for the device */
976 if( ( ret = IOServiceGetMatchingServices(
977 port, IOBSDNameMatching( port, 0, psz_devname ),
978 &iterator ) ) != KERN_SUCCESS )
980 msg_Err( p_this, "IOServiceGetMatchingServices: 0x%08x", ret );
985 service = IOIteratorNext( iterator );
986 IOObjectRelease( iterator );
988 /* search for kIOCDMediaClass */
989 while( service && !IOObjectConformsTo( service, kIOCDMediaClass ) )
991 if( ( ret = IORegistryEntryGetParentIterator( service,
992 kIOServicePlane, &iterator ) ) != KERN_SUCCESS )
994 msg_Err( p_this, "IORegistryEntryGetParentIterator: 0x%08x", ret );
995 IOObjectRelease( service );
999 IOObjectRelease( service );
1000 service = IOIteratorNext( iterator );
1001 IOObjectRelease( iterator );
1004 if( service == NULL )
1006 msg_Err( p_this, "search for kIOCDMediaClass came up empty" );
1010 /* create a CF dictionary containing the TOC */
1011 if( ( ret = IORegistryEntryCreateCFProperties( service, &properties,
1012 kCFAllocatorDefault, kNilOptions ) ) != KERN_SUCCESS )
1014 msg_Err( p_this, "IORegistryEntryCreateCFProperties: 0x%08x", ret );
1015 IOObjectRelease( service );
1019 /* get the TOC from the dictionary */
1020 if( ( data = (CFDataRef) CFDictionaryGetValue( properties,
1021 CFSTR(kIOCDMediaTOCKey) ) ) != NULL )
1026 buf_len = CFDataGetLength( data ) + 1;
1027 range = CFRangeMake( 0, buf_len );
1029 if( ( pTOC = (CDTOC *)malloc( buf_len ) ) != NULL )
1031 CFDataGetBytes( data, range, (u_char *)pTOC );
1036 msg_Err( p_this, "CFDictionaryGetValue failed" );
1039 CFRelease( properties );
1040 IOObjectRelease( service );
1045 /****************************************************************************
1046 * darwin_getNumberOfDescriptors: get number of descriptors in TOC
1047 ****************************************************************************/
1048 static int darwin_getNumberOfDescriptors( CDTOC *pTOC )
1052 /* get TOC length */
1053 i_descriptors = pTOC->length;
1055 /* remove the first and last session */
1056 i_descriptors -= ( sizeof(pTOC->sessionFirst) +
1057 sizeof(pTOC->sessionLast) );
1059 /* divide the length by the size of a single descriptor */
1060 i_descriptors /= sizeof(CDTOCDescriptor);
1062 return( i_descriptors );
1065 /****************************************************************************
1066 * darwin_getNumberOfTracks: get number of tracks in TOC
1067 ****************************************************************************/
1068 static int darwin_getNumberOfTracks( CDTOC *pTOC, int i_descriptors )
1071 int i, i_tracks = 0;
1072 CDTOCDescriptor *pTrackDescriptors;
1074 pTrackDescriptors = pTOC->descriptors;
1076 for( i = i_descriptors; i >= 0; i-- )
1078 track = pTrackDescriptors[i].point;
1080 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
1088 #endif /* SYS_DARWIN */
1090 #if defined( WIN32 )
1091 /*****************************************************************************
1092 * win32_vcd_open: open vcd drive
1093 *****************************************************************************
1094 * Load and use aspi if it is available, otherwise use IOCTLs on WinNT/2K/XP.
1095 *****************************************************************************/
1096 static int win32_vcd_open( vlc_object_t * p_this, const char *psz_dev,
1097 vcddev_t *p_vcddev )
1099 /* Initializations */
1100 p_vcddev->h_device_handle = NULL;
1101 p_vcddev->i_sid = 0;
1102 p_vcddev->hASPI = 0;
1103 p_vcddev->lpSendCommand = 0;
1107 char psz_win32_drive[7];
1109 msg_Dbg( p_this, "using winNT/2K/XP ioctl layer" );
1111 sprintf( psz_win32_drive, "\\\\.\\%c:", psz_dev[0] );
1113 p_vcddev->h_device_handle = CreateFile( psz_win32_drive, GENERIC_READ,
1114 FILE_SHARE_READ | FILE_SHARE_WRITE,
1115 NULL, OPEN_EXISTING,
1116 FILE_FLAG_NO_BUFFERING |
1117 FILE_FLAG_RANDOM_ACCESS, NULL );
1118 return (p_vcddev->h_device_handle == NULL) ? -1 : 0;
1122 HMODULE hASPI = NULL;
1123 long (*lpGetSupport)( void ) = NULL;
1124 long (*lpSendCommand)( void* ) = NULL;
1125 DWORD dwSupportInfo;
1126 int i, j, i_hostadapters;
1127 char c_drive = psz_dev[0];
1129 hASPI = LoadLibrary( "wnaspi32.dll" );
1132 (FARPROC) lpGetSupport = GetProcAddress( hASPI,
1133 "GetASPI32SupportInfo" );
1134 (FARPROC) lpSendCommand = GetProcAddress( hASPI,
1135 "SendASPI32Command" );
1138 if( hASPI == NULL || lpGetSupport == NULL || lpSendCommand == NULL )
1141 "unable to load aspi or get aspi function pointers" );
1142 if( hASPI ) FreeLibrary( hASPI );
1146 /* ASPI support seems to be there */
1148 dwSupportInfo = lpGetSupport();
1150 if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS )
1152 msg_Dbg( p_this, "no host adapters found (aspi)" );
1153 FreeLibrary( hASPI );
1157 if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP )
1159 msg_Dbg( p_this, "unable to initalize aspi layer" );
1160 FreeLibrary( hASPI );
1164 i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
1165 if( i_hostadapters == 0 )
1167 FreeLibrary( hASPI );
1171 c_drive = c_drive > 'Z' ? c_drive - 'a' : c_drive - 'A';
1173 for( i = 0; i < i_hostadapters; i++ )
1175 for( j = 0; j < 15; j++ )
1177 struct SRB_GetDiskInfo srbDiskInfo;
1179 srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO;
1180 srbDiskInfo.SRB_HaId = i;
1181 srbDiskInfo.SRB_Flags = 0;
1182 srbDiskInfo.SRB_Hdr_Rsvd = 0;
1183 srbDiskInfo.SRB_Target = j;
1184 srbDiskInfo.SRB_Lun = 0;
1186 lpSendCommand( (void*) &srbDiskInfo );
1188 if( (srbDiskInfo.SRB_Status == SS_COMP) &&
1189 (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) )
1191 /* Make sure this is a cdrom device */
1192 struct SRB_GDEVBlock srbGDEVBlock;
1194 memset( &srbGDEVBlock, 0, sizeof(struct SRB_GDEVBlock) );
1195 srbGDEVBlock.SRB_Cmd = SC_GET_DEV_TYPE;
1196 srbGDEVBlock.SRB_HaId = i;
1197 srbGDEVBlock.SRB_Target = j;
1199 lpSendCommand( (void*) &srbGDEVBlock );
1201 if( ( srbGDEVBlock.SRB_Status == SS_COMP ) &&
1202 ( srbGDEVBlock.SRB_DeviceType == DTYPE_CDROM ) )
1204 p_vcddev->i_sid = MAKEWORD( i, j );
1205 p_vcddev->hASPI = (long)hASPI;
1206 p_vcddev->lpSendCommand = lpSendCommand;
1207 msg_Dbg( p_this, "using aspi layer" );
1213 FreeLibrary( hASPI );
1214 msg_Dbg( p_this, "%c: is not a cdrom drive",
1222 FreeLibrary( hASPI );
1223 msg_Dbg( p_this, "unable to get haid and target (aspi)" );