cdrom.c 39.1 KB
Newer Older
1 2 3
/****************************************************************************
 * cdrom.c: cdrom tools
 *****************************************************************************
4
 * Copyright (C) 1998-2001 the VideoLAN team
5
 * $Id$
6
 *
Gildas Bazin's avatar
 
Gildas Bazin committed
7 8 9
 * Authors: Johan Bilien <jobi@via.ecp.fr>
 *          Gildas Bazin <gbazin@netcourrier.com>
 *          Jon Lech Johansen <jon-vl@nanocrew.net>
10 11 12 13 14
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
15
 *
16 17 18 19 20 21 22
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc_common.h>
Clément Stenac's avatar
Clément Stenac committed
34
#include <vlc_access.h>
35 36 37 38 39 40

#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif

#include <errno.h>
41 42 43 44 45 46
#ifdef HAVE_SYS_TYPES_H
#   include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
47
#ifdef HAVE_FCNTL_H
48
#   include <fcntl.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
49
#endif
50 51 52

#if defined( SYS_BSDI )
#   include <dvd.h>
53
#elif defined ( __APPLE__ )
54 55 56 57 58
#   include <CoreFoundation/CFBase.h>
#   include <IOKit/IOKitLib.h>
#   include <IOKit/storage/IOCDTypes.h>
#   include <IOKit/storage/IOCDMedia.h>
#   include <IOKit/storage/IOCDMediaBSDClient.h>
59 60 61 62
#elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
#   include <sys/inttypes.h>
#   include <sys/cdio.h>
#   include <sys/scsiio.h>
63 64 65
#elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
#   include <sys/cdio.h>
#   include <sys/cdrio.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
66 67 68
#elif defined( WIN32 )
#   include <windows.h>
#   include <winioctl.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
69 70
#elif defined (__linux__)
#   include <sys/ioctl.h>
71
#   include <linux/cdrom.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
72 73
#else
#   error FIXME
74 75
#endif

Gildas Bazin's avatar
 
Gildas Bazin committed
76
#include "cdrom_internals.h"
77
#include "cdrom.h"
Clément Stenac's avatar
Clément Stenac committed
78
#include <vlc_charset.h>
79 80

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
81
 * ioctl_Open: Opens a VCD device or file and returns an opaque handle
82
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
83 84 85 86 87 88 89
vcddev_t *ioctl_Open( vlc_object_t *p_this, const char *psz_dev )
{
    int i_ret;
    int b_is_file;
    vcddev_t *p_vcddev;
#ifndef WIN32
    struct stat fileinfo;
90 91
#endif

Gildas Bazin's avatar
 
Gildas Bazin committed
92
    if( !psz_dev ) return NULL;
93

Gildas Bazin's avatar
 
Gildas Bazin committed
94 95 96 97 98
    /*
     *  Initialize structure with default values
     */
    p_vcddev = (vcddev_t *)malloc( sizeof(vcddev_t) );
    if( p_vcddev == NULL )
99
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
100 101 102 103 104 105 106 107 108 109 110
        msg_Err( p_this, "out of memory" );
        return NULL;
    }
    p_vcddev->i_vcdimage_handle = -1;
    p_vcddev->psz_dev = NULL;
    b_is_file = 1;

    /*
     *  Check if we are dealing with a device or a file (vcd image)
     */
#ifdef WIN32
Gildas Bazin's avatar
 
Gildas Bazin committed
111
    if( (strlen( psz_dev ) == 2 && psz_dev[1] == ':') )
Gildas Bazin's avatar
 
Gildas Bazin committed
112 113
    {
        b_is_file = 0;
114 115
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
116 117 118 119 120 121
#else
    if( stat( psz_dev, &fileinfo ) < 0 )
    {
        free( p_vcddev );
        return NULL;
    }
122

Gildas Bazin's avatar
 
Gildas Bazin committed
123 124 125 126
    /* Check if this is a block/char device */
    if( S_ISBLK( fileinfo.st_mode ) || S_ISCHR( fileinfo.st_mode ) )
        b_is_file = 0;
#endif
127

Gildas Bazin's avatar
 
Gildas Bazin committed
128
    if( b_is_file )
129
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
130
        i_ret = OpenVCDImage( p_this, psz_dev, p_vcddev );
131
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
132 133 134 135 136
    else
    {
        /*
         *  open the vcd device
         */
137

Gildas Bazin's avatar
 
Gildas Bazin committed
138 139
#ifdef WIN32
        i_ret = win32_vcd_open( p_this, psz_dev, p_vcddev );
140
#else
Gildas Bazin's avatar
 
Gildas Bazin committed
141 142 143 144 145
        p_vcddev->i_device_handle = -1;
        p_vcddev->i_device_handle = open( psz_dev, O_RDONLY | O_NONBLOCK );
        i_ret = (p_vcddev->i_device_handle == -1) ? -1 : 0;
#endif
    }
146

Gildas Bazin's avatar
 
Gildas Bazin committed
147
    if( i_ret == 0 )
148
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
149 150 151 152 153 154
        p_vcddev->psz_dev = (char *)strdup( psz_dev );
    }
    else
    {
        free( p_vcddev );
        p_vcddev = NULL;
155 156
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
157
    return p_vcddev;
158 159 160
}

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
161
 * ioctl_Close: Closes an already opened VCD device or file.
162
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
163
void ioctl_Close( vlc_object_t * p_this, vcddev_t *p_vcddev )
164
{
165
    free( p_vcddev->psz_dev );
166

Gildas Bazin's avatar
 
Gildas Bazin committed
167
    if( p_vcddev->i_vcdimage_handle != -1 )
168
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
169 170 171 172 173 174
        /*
         *  vcd image mode
         */

        CloseVCDImage( p_this, p_vcddev );
        return;
175 176
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
177 178 179
    /*
     *  vcd device mode
     */
180

Gildas Bazin's avatar
 
Gildas Bazin committed
181 182 183 184 185 186 187 188 189 190
#ifdef WIN32
    if( p_vcddev->h_device_handle )
        CloseHandle( p_vcddev->h_device_handle );
    if( p_vcddev->hASPI )
        FreeLibrary( (HMODULE)p_vcddev->hASPI );
#else
    if( p_vcddev->i_device_handle != -1 )
        close( p_vcddev->i_device_handle );
#endif
}
191

Gildas Bazin's avatar
 
Gildas Bazin committed
192 193 194 195 196 197 198 199 200 201 202
/*****************************************************************************
 * ioctl_GetTracksMap: Read the Table of Content, fill in the pp_sectors map
 *                     if pp_sectors is not null and return the number of
 *                     tracks available.
 *****************************************************************************/
int ioctl_GetTracksMap( vlc_object_t *p_this, const vcddev_t *p_vcddev,
                        int **pp_sectors )
{
    int i_tracks = 0;

    if( p_vcddev->i_vcdimage_handle != -1 )
203
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
204 205 206
        /*
         *  vcd image mode
         */
207

Gildas Bazin's avatar
 
Gildas Bazin committed
208
        i_tracks = p_vcddev->i_tracks;
209

Gildas Bazin's avatar
 
Gildas Bazin committed
210 211 212 213 214 215 216 217 218 219 220
        if( pp_sectors )
        {
            *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
            if( *pp_sectors == NULL )
            {
                msg_Err( p_this, "out of memory" );
                return 0;
            }
            memcpy( *pp_sectors, p_vcddev->p_sectors,
                    (i_tracks + 1) * sizeof(int) );
        }
221

Gildas Bazin's avatar
 
Gildas Bazin committed
222
        return i_tracks;
223
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
224
    else
225 226
    {

Gildas Bazin's avatar
 
Gildas Bazin committed
227 228 229
        /*
         *  vcd device mode
         */
230

231
#if defined( __APPLE__ )
Gildas Bazin's avatar
 
Gildas Bazin committed
232 233 234 235

        CDTOC *pTOC;
        int i_descriptors;

236
        if( ( pTOC = darwin_getTOC( p_this, p_vcddev ) ) == NULL )
Gildas Bazin's avatar
 
Gildas Bazin committed
237 238 239 240 241
        {
            msg_Err( p_this, "failed to get the TOC" );
            return 0;
        }

242
        i_descriptors = CDTOCGetDescriptorCount( pTOC );
Gildas Bazin's avatar
 
Gildas Bazin committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
        i_tracks = darwin_getNumberOfTracks( pTOC, i_descriptors );

        if( pp_sectors )
        {
            int i, i_leadout = -1;
            CDTOCDescriptor *pTrackDescriptors;
            u_char track;

            *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
            if( *pp_sectors == NULL )
            {
                msg_Err( p_this, "out of memory" );
                darwin_freeTOC( pTOC );
                return 0;
            }

            pTrackDescriptors = pTOC->descriptors;

261
            for( i_tracks = 0, i = 0; i < i_descriptors; i++ )
Gildas Bazin's avatar
 
Gildas Bazin committed
262 263 264 265 266 267 268 269 270
            {
                track = pTrackDescriptors[i].point;

                if( track == 0xA2 )
                    i_leadout = i;

                if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
                    continue;

271
                (*pp_sectors)[i_tracks++] =
Gildas Bazin's avatar
 
Gildas Bazin committed
272 273 274 275 276 277 278 279 280 281 282 283
                    CDConvertMSFToLBA( pTrackDescriptors[i].p );
            }

            if( i_leadout == -1 )
            {
                msg_Err( p_this, "leadout not found" );
                free( *pp_sectors );
                darwin_freeTOC( pTOC );
                return 0;
            }

            /* set leadout sector */
284
            (*pp_sectors)[i_tracks] =
Gildas Bazin's avatar
 
Gildas Bazin committed
285 286 287 288 289 290 291 292 293 294
                CDConvertMSFToLBA( pTrackDescriptors[i_leadout].p );
        }

        darwin_freeTOC( pTOC );

#elif defined( WIN32 )
        if( p_vcddev->hASPI )
        {
            HANDLE hEvent;
            struct SRB_ExecSCSICmd ssc;
295
            uint8_t p_tocheader[ 4 ];
Gildas Bazin's avatar
 
Gildas Bazin committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

            /* Create the transfer completion event */
            hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
            if( hEvent == NULL )
            {
                return -1;
            }

            memset( &ssc, 0, sizeof( ssc ) );

            ssc.SRB_Cmd         = SC_EXEC_SCSI_CMD;
            ssc.SRB_Flags       = SRB_DIR_IN | SRB_EVENT_NOTIFY;
            ssc.SRB_HaId        = LOBYTE( p_vcddev->i_sid );
            ssc.SRB_Target      = HIBYTE( p_vcddev->i_sid );
            ssc.SRB_SenseLen    = SENSE_LEN;

            ssc.SRB_PostProc = (LPVOID) hEvent;
            ssc.SRB_CDBLen      = 10;

            /* Operation code */
            ssc.CDBByte[ 0 ] = READ_TOC;

            /* Format */
            ssc.CDBByte[ 2 ] = READ_TOC_FORMAT_TOC;

            /* Starting track */
            ssc.CDBByte[ 6 ] = 0;

            /* Allocation length and buffer */
            ssc.SRB_BufLen = sizeof( p_tocheader );
            ssc.SRB_BufPointer  = p_tocheader;
            ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >>  8 ) & 0xff;
            ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen       ) & 0xff;

            /* Initiate transfer */
            ResetEvent( hEvent );
            p_vcddev->lpSendCommand( (void*) &ssc );

            /* If the command has still not been processed, wait until it's
             * finished */
            if( ssc.SRB_Status == SS_PENDING )
                WaitForSingleObject( hEvent, INFINITE );

            /* check that the transfer went as planned */
            if( ssc.SRB_Status != SS_COMP )
            {
                CloseHandle( hEvent );
                return 0;
            }

            i_tracks = p_tocheader[3] - p_tocheader[2] + 1;

            if( pp_sectors )
            {
                int i, i_toclength;
351
                uint8_t *p_fulltoc;
Gildas Bazin's avatar
 
Gildas Bazin committed
352 353 354 355 356 357 358 359 360

                i_toclength = 4 /* header */ + p_tocheader[0] +
                              ((unsigned int)p_tocheader[1] << 8);

                p_fulltoc = malloc( i_toclength );
                *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );

                if( *pp_sectors == NULL || p_fulltoc == NULL )
                {
361 362
                    free( *pp_sectors );
                    free( p_fulltoc );
Gildas Bazin's avatar
 
Gildas Bazin committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
                    msg_Err( p_this, "out of memory" );
                    CloseHandle( hEvent );
                    return 0;
                }

                /* Allocation length and buffer */
                ssc.SRB_BufLen = i_toclength;
                ssc.SRB_BufPointer  = p_fulltoc;
                ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >>  8 ) & 0xff;
                ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen       ) & 0xff;

                /* Initiate transfer */
                ResetEvent( hEvent );
                p_vcddev->lpSendCommand( (void*) &ssc );

                /* If the command has still not been processed, wait until it's
                 * finished */
                if( ssc.SRB_Status == SS_PENDING )
                    WaitForSingleObject( hEvent, INFINITE );

                /* check that the transfer went as planned */
                if( ssc.SRB_Status != SS_COMP )
                    i_tracks = 0;

                for( i = 0 ; i <= i_tracks ; i++ )
                {
                    int i_index = 8 + 8 * i;
                    (*pp_sectors)[ i ] = ((int)p_fulltoc[ i_index ] << 24) +
                                         ((int)p_fulltoc[ i_index+1 ] << 16) +
                                         ((int)p_fulltoc[ i_index+2 ] << 8) +
                                         (int)p_fulltoc[ i_index+3 ];

                    msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
                }

                free( p_fulltoc );
            }

            CloseHandle( hEvent );

        }
        else
        {
            DWORD dwBytesReturned;
            CDROM_TOC cdrom_toc;

            if( DeviceIoControl( p_vcddev->h_device_handle,
                                 IOCTL_CDROM_READ_TOC,
                                 NULL, 0, &cdrom_toc, sizeof(CDROM_TOC),
                                 &dwBytesReturned, NULL ) == 0 )
            {
                msg_Err( p_this, "could not read TOCHDR" );
                return 0;
            }

            i_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1;

            if( pp_sectors )
            {
                int i;

                *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
                if( *pp_sectors == NULL )
                {
                    msg_Err( p_this, "out of memory" );
                    return 0;
                }

                for( i = 0 ; i <= i_tracks ; i++ )
                {
                    (*pp_sectors)[ i ] = MSF_TO_LBA2(
                                           cdrom_toc.TrackData[i].Address[1],
                                           cdrom_toc.TrackData[i].Address[2],
                                           cdrom_toc.TrackData[i].Address[3] );
                    msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
                }
            }
        }
441

442 443
#elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H ) \
       || defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
Gildas Bazin's avatar
 
Gildas Bazin committed
444 445
        struct ioc_toc_header tochdr;
        struct ioc_read_toc_entry toc_entries;
446

447
        if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCHEADER, &tochdr )
Gildas Bazin's avatar
 
Gildas Bazin committed
448 449 450 451 452
            == -1 )
        {
            msg_Err( p_this, "could not read TOCHDR" );
            return 0;
        }
453

Gildas Bazin's avatar
 
Gildas Bazin committed
454 455 456 457 458 459 460 461 462 463
        i_tracks = tochdr.ending_track - tochdr.starting_track + 1;

        if( pp_sectors )
        {
             int i;

             *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
             if( *pp_sectors == NULL )
             {
                 msg_Err( p_this, "out of memory" );
Rafaël Carré's avatar
Rafaël Carré committed
464
                 return 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
465 466 467 468 469
             }

             toc_entries.address_format = CD_LBA_FORMAT;
             toc_entries.starting_track = 0;
             toc_entries.data_len = ( i_tracks + 1 ) *
470
                                        sizeof( struct cd_toc_entry );
Gildas Bazin's avatar
 
Gildas Bazin committed
471 472 473 474 475 476 477 478
             toc_entries.data = (struct cd_toc_entry *)
                                    malloc( toc_entries.data_len );
             if( toc_entries.data == NULL )
             {
                 msg_Err( p_this, "out of memory" );
                 free( *pp_sectors );
                 return 0;
             }
479

Gildas Bazin's avatar
 
Gildas Bazin committed
480 481 482 483 484 485 486 487 488
             /* Read the TOC */
             if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCENTRYS,
                        &toc_entries ) == -1 )
             {
                 msg_Err( p_this, "could not read the TOC" );
                 free( *pp_sectors );
                 free( toc_entries.data );
                 return 0;
             }
489

Gildas Bazin's avatar
 
Gildas Bazin committed
490 491 492
             /* Fill the p_sectors structure with the track/sector matches */
             for( i = 0 ; i <= i_tracks ; i++ )
             {
493 494 495 496
#if defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
                 /* FIXME: is this ok? */
                 (*pp_sectors)[ i ] = toc_entries.data[i].addr.lba;
#else
Gildas Bazin's avatar
 
Gildas Bazin committed
497
                 (*pp_sectors)[ i ] = ntohl( toc_entries.data[i].addr.lba );
498
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
499 500
             }
        }
501
#else
Gildas Bazin's avatar
 
Gildas Bazin committed
502 503
        struct cdrom_tochdr   tochdr;
        struct cdrom_tocentry tocent;
504

Gildas Bazin's avatar
 
Gildas Bazin committed
505 506 507 508 509 510 511
        /* First we read the TOC header */
        if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCHDR, &tochdr )
            == -1 )
        {
            msg_Err( p_this, "could not read TOCHDR" );
            return 0;
        }
512

Gildas Bazin's avatar
 
Gildas Bazin committed
513
        i_tracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
514

Gildas Bazin's avatar
 
Gildas Bazin committed
515
        if( pp_sectors )
516
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
            int i;

            *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
            if( *pp_sectors == NULL )
            {
                msg_Err( p_this, "out of memory" );
                return 0;
            }

            /* Fill the p_sectors structure with the track/sector matches */
            for( i = 0 ; i <= i_tracks ; i++ )
            {
                tocent.cdte_format = CDROM_LBA;
                tocent.cdte_track =
                    ( i == i_tracks ) ? CDROM_LEADOUT : tochdr.cdth_trk0 + i;

                if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCENTRY,
                           &tocent ) == -1 )
                {
                    msg_Err( p_this, "could not read TOCENTRY" );
                    free( *pp_sectors );
                    return 0;
                }

                (*pp_sectors)[ i ] = tocent.cdte_addr.lba;
            }
543 544 545
        }
#endif

Gildas Bazin's avatar
 
Gildas Bazin committed
546 547
        return i_tracks;
    }
548 549 550
}

/****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
551
 * ioctl_ReadSector: Read VCD or CDDA sectors
552
 ****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
553
int ioctl_ReadSectors( vlc_object_t *p_this, const vcddev_t *p_vcddev,
554
                       int i_sector, uint8_t *p_buffer, int i_nb, int i_type )
555
{
556
    uint8_t *p_block;
Gildas Bazin's avatar
 
Gildas Bazin committed
557 558 559 560
    int i;

    if( i_type == VCD_TYPE ) p_block = malloc( VCD_SECTOR_SIZE * i_nb );
    else p_block = p_buffer;
561

Gildas Bazin's avatar
 
Gildas Bazin committed
562 563 564 565 566 567 568 569 570
    if( p_vcddev->i_vcdimage_handle != -1 )
    {
        /*
         *  vcd image mode
         */
        if( lseek( p_vcddev->i_vcdimage_handle, i_sector * VCD_SECTOR_SIZE,
                   SEEK_SET ) == -1 )
        {
            msg_Err( p_this, "Could not lseek to sector %d", i_sector );
Gildas Bazin's avatar
 
Gildas Bazin committed
571
            if( i_type == VCD_TYPE ) free( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
572 573 574
            return -1;
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
575
        if( read( p_vcddev->i_vcdimage_handle, p_block, VCD_SECTOR_SIZE * i_nb)
Gildas Bazin's avatar
 
Gildas Bazin committed
576 577
            == -1 )
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
578 579
            msg_Err( p_this, "Could not read sector %d", i_sector );
            if( i_type == VCD_TYPE ) free( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
580 581 582 583 584 585 586 587 588 589 590
            return -1;
        }

    }
    else
    {

        /*
         *  vcd device mode
         */

591
#if defined( __APPLE__ )
Gildas Bazin's avatar
 
Gildas Bazin committed
592
        dk_cd_read_t cd_read;
593

Gildas Bazin's avatar
 
Gildas Bazin committed
594
        memset( &cd_read, 0, sizeof(cd_read) );
595

Gildas Bazin's avatar
 
Gildas Bazin committed
596 597 598 599 600
        cd_read.offset = i_sector * VCD_SECTOR_SIZE;
        cd_read.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader |
                             kCDSectorAreaSubHeader | kCDSectorAreaUser |
                             kCDSectorAreaAuxiliary;
        cd_read.sectorType = kCDSectorTypeUnknown;
601

Gildas Bazin's avatar
 
Gildas Bazin committed
602
        cd_read.buffer = p_block;
Gildas Bazin's avatar
 
Gildas Bazin committed
603
        cd_read.bufferLength = VCD_SECTOR_SIZE * i_nb;
604

Gildas Bazin's avatar
 
Gildas Bazin committed
605 606 607
        if( ioctl( p_vcddev->i_device_handle, DKIOCCDREAD, &cd_read ) == -1 )
        {
            msg_Err( p_this, "could not read block %d", i_sector );
Gildas Bazin's avatar
 
Gildas Bazin committed
608
            if( i_type == VCD_TYPE ) free( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
609 610 611 612 613 614 615 616 617 618 619 620 621
            return -1;
        }

#elif defined( WIN32 )
        if( p_vcddev->hASPI )
        {
            HANDLE hEvent;
            struct SRB_ExecSCSICmd ssc;

            /* Create the transfer completion event */
            hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
            if( hEvent == NULL )
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
622
                if( i_type == VCD_TYPE ) free( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
                return -1;
            }

            memset( &ssc, 0, sizeof( ssc ) );

            ssc.SRB_Cmd         = SC_EXEC_SCSI_CMD;
            ssc.SRB_Flags       = SRB_DIR_IN | SRB_EVENT_NOTIFY;
            ssc.SRB_HaId        = LOBYTE( p_vcddev->i_sid );
            ssc.SRB_Target      = HIBYTE( p_vcddev->i_sid );
            ssc.SRB_SenseLen    = SENSE_LEN;

            ssc.SRB_PostProc = (LPVOID) hEvent;
            ssc.SRB_CDBLen      = 12;

            /* Operation code */
            ssc.CDBByte[ 0 ] = READ_CD;

Gildas Bazin's avatar
 
Gildas Bazin committed
640
            /* Sector type */
Gildas Bazin's avatar
 
Gildas Bazin committed
641 642
            ssc.CDBByte[ 1 ] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2 :
                                                    SECTOR_TYPE_CDDA;
Gildas Bazin's avatar
 
Gildas Bazin committed
643

Gildas Bazin's avatar
 
Gildas Bazin committed
644 645 646 647 648 649 650
            /* Start of LBA */
            ssc.CDBByte[ 2 ] = ( i_sector >> 24 ) & 0xff;
            ssc.CDBByte[ 3 ] = ( i_sector >> 16 ) & 0xff;
            ssc.CDBByte[ 4 ] = ( i_sector >>  8 ) & 0xff;
            ssc.CDBByte[ 5 ] = ( i_sector       ) & 0xff;

            /* Transfer length */
Gildas Bazin's avatar
 
Gildas Bazin committed
651 652 653
            ssc.CDBByte[ 6 ] = ( i_nb >> 16 ) & 0xff;
            ssc.CDBByte[ 7 ] = ( i_nb >> 8  ) & 0xff;
            ssc.CDBByte[ 8 ] = ( i_nb       ) & 0xff;
Gildas Bazin's avatar
 
Gildas Bazin committed
654 655

            /* Data selection */
Gildas Bazin's avatar
 
Gildas Bazin committed
656 657
            ssc.CDBByte[ 9 ] = i_type == VCD_TYPE ? READ_CD_RAW_MODE2 :
                                                    READ_CD_USERDATA;
Gildas Bazin's avatar
 
Gildas Bazin committed
658 659 660

            /* Result buffer */
            ssc.SRB_BufPointer  = p_block;
Gildas Bazin's avatar
 
Gildas Bazin committed
661
            ssc.SRB_BufLen = VCD_SECTOR_SIZE * i_nb;
Gildas Bazin's avatar
 
Gildas Bazin committed
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677

            /* Initiate transfer */
            ResetEvent( hEvent );
            p_vcddev->lpSendCommand( (void*) &ssc );

            /* If the command has still not been processed, wait until it's
             * finished */
            if( ssc.SRB_Status == SS_PENDING )
            {
                WaitForSingleObject( hEvent, INFINITE );
            }
            CloseHandle( hEvent );

            /* check that the transfer went as planned */
            if( ssc.SRB_Status != SS_COMP )
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
678
                if( i_type == VCD_TYPE ) free( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
679 680 681 682 683 684 685 686 687 688
                return -1;
            }
        }
        else
        {
            DWORD dwBytesReturned;
            RAW_READ_INFO cdrom_raw;

            /* Initialize CDROM_RAW_READ structure */
            cdrom_raw.DiskOffset.QuadPart = CD_SECTOR_SIZE * i_sector;
Gildas Bazin's avatar
 
Gildas Bazin committed
689
            cdrom_raw.SectorCount = i_nb;
Gildas Bazin's avatar
 
Gildas Bazin committed
690
            cdrom_raw.TrackMode =  i_type == VCD_TYPE ? XAForm2 : CDDA;
Gildas Bazin's avatar
 
Gildas Bazin committed
691 692 693 694

            if( DeviceIoControl( p_vcddev->h_device_handle,
                                 IOCTL_CDROM_RAW_READ, &cdrom_raw,
                                 sizeof(RAW_READ_INFO), p_block,
Gildas Bazin's avatar
 
Gildas Bazin committed
695 696
                                 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
                                 NULL ) == 0 )
Gildas Bazin's avatar
 
Gildas Bazin committed
697
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
                if( i_type == VCD_TYPE )
                {
                    /* Retry in YellowMode2 */
                    cdrom_raw.TrackMode = YellowMode2;
                    if( DeviceIoControl( p_vcddev->h_device_handle,
                                 IOCTL_CDROM_RAW_READ, &cdrom_raw,
                                 sizeof(RAW_READ_INFO), p_block,
                                 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
                                 NULL ) == 0 )
                    {
                        free( p_block );
                        return -1;
                    }
                }
                else return -1;
Gildas Bazin's avatar
 
Gildas Bazin committed
713 714
            }
        }
715

716 717 718 719 720 721
#elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
        struct scsireq  sc;
        int i_ret;

        memset( &sc, 0, sizeof(sc) );
        sc.cmd[0] = 0xBE;
Gildas Bazin's avatar
 
Gildas Bazin committed
722 723
        sc.cmd[1] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2:
                                         SECTOR_TYPE_CDDA;
724 725 726 727
        sc.cmd[2] = (i_sector >> 24) & 0xff;
        sc.cmd[3] = (i_sector >> 16) & 0xff;
        sc.cmd[4] = (i_sector >>  8) & 0xff;
        sc.cmd[5] = (i_sector >>  0) & 0xff;
Gildas Bazin's avatar
 
Gildas Bazin committed
728 729 730 731
        sc.cmd[6] = (i_nb >> 16) & 0xff;
        sc.cmd[7] = (i_nb >>  8) & 0xff;
        sc.cmd[8] = (i_nb      ) & 0xff;
        sc.cmd[9] = i_type == VCD_TYPE ? READ_CD_RAW_MODE2 : READ_CD_USERDATA;
Gildas Bazin's avatar
 
Gildas Bazin committed
732
        sc.cmd[10] = 0; /* sub channel */
733 734
        sc.cmdlen = 12;
        sc.databuf = (caddr_t)p_block;
Gildas Bazin's avatar
 
Gildas Bazin committed
735
        sc.datalen = VCD_SECTOR_SIZE * i_nb;
736 737 738 739 740 741 742 743
        sc.senselen = sizeof( sc.sense );
        sc.flags = SCCMD_READ;
        sc.timeout = 10000;

        i_ret = ioctl( i_fd, SCIOCCOMMAND, &sc );
        if( i_ret == -1 )
        {
            msg_Err( p_this, "SCIOCCOMMAND failed" );
Gildas Bazin's avatar
 
Gildas Bazin committed
744
            if( i_type == VCD_TYPE ) free( p_block );
745 746 747 748 749 750
            return -1;
        }
        if( sc.retsts || sc.error )
        {
            msg_Err( p_this, "SCSI command failed: status %d error %d\n",
                             sc.retsts, sc.error );
Gildas Bazin's avatar
 
Gildas Bazin committed
751
            if( i_type == VCD_TYPE ) free( p_block );
752 753 754
           return -1;
        }

755
#elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
Gildas Bazin's avatar
 
Gildas Bazin committed
756 757 758 759 760 761
        int i_size = VCD_SECTOR_SIZE;

        if( ioctl( p_vcddev->i_device_handle, CDRIOCSETBLOCKSIZE, &i_size )
            == -1 )
        {
            msg_Err( p_this, "Could not set block size" );
Gildas Bazin's avatar
 
Gildas Bazin committed
762
            if( i_type == VCD_TYPE ) free( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
763 764 765 766 767 768 769
            return( -1 );
        }

        if( lseek( p_vcddev->i_device_handle,
                   i_sector * VCD_SECTOR_SIZE, SEEK_SET ) == -1 )
        {
            msg_Err( p_this, "Could not lseek to sector %d", i_sector );
Gildas Bazin's avatar
 
Gildas Bazin committed
770
            if( i_type == VCD_TYPE ) free( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
771 772 773
            return( -1 );
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
774 775
        if( read( p_vcddev->i_device_handle,
                  p_block, VCD_SECTOR_SIZE * i_nb ) == -1 )
Gildas Bazin's avatar
 
Gildas Bazin committed
776 777
        {
            msg_Err( p_this, "Could not read sector %d", i_sector );
Gildas Bazin's avatar
 
Gildas Bazin committed
778
            if( i_type == VCD_TYPE ) free( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
779 780 781 782
            return( -1 );
        }

#else
Gildas Bazin's avatar
 
Gildas Bazin committed
783 784 785
        for( i = 0; i < i_nb; i++ )
        {
            int i_dummy = i_sector + i + 2 * CD_FRAMES;
Gildas Bazin's avatar
 
Gildas Bazin committed
786

Gildas Bazin's avatar
 
Gildas Bazin committed
787 788 789 790
#define p_msf ((struct cdrom_msf0 *)(p_block + i * VCD_SECTOR_SIZE))
            p_msf->minute =   i_dummy / (CD_FRAMES * CD_SECS);
            p_msf->second = ( i_dummy % (CD_FRAMES * CD_SECS) ) / CD_FRAMES;
            p_msf->frame =  ( i_dummy % (CD_FRAMES * CD_SECS) ) % CD_FRAMES;
Gildas Bazin's avatar
 
Gildas Bazin committed
791 792
#undef p_msf

Gildas Bazin's avatar
 
Gildas Bazin committed
793 794 795 796 797 798 799 800 801 802 803 804 805
            if( ioctl( p_vcddev->i_device_handle, CDROMREADRAW,
                       p_block + i * VCD_SECTOR_SIZE ) == -1 )
            {
                msg_Err( p_this, "could not read block %i from disc",
                         i_sector );

                if( i == 0 )
                {
                    if( i_type == VCD_TYPE ) free( p_block );
                    return( -1 );
                }
                else break;
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
806 807
        }
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
808
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
809

Gildas Bazin's avatar
 
Gildas Bazin committed
810 811 812 813 814 815 816 817 818 819 820
    /* For VCDs, we don't want to keep the header and footer of the
     * sectors read */
    if( i_type == VCD_TYPE )
    {
        for( i = 0; i < i_nb; i++ )
        {
            memcpy( p_buffer + i * VCD_DATA_SIZE,
                    p_block + i * VCD_SECTOR_SIZE + VCD_DATA_START,
                    VCD_DATA_SIZE );
        }
        free( p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
821
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
822 823

    return( 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
}

/****************************************************************************
 * Private functions
 ****************************************************************************/

/****************************************************************************
 * OpenVCDImage: try to open a vcd image from a .cue file
 ****************************************************************************/
static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
                         vcddev_t *p_vcddev )
{
    int i_ret = -1;
    char *p_pos;
    char *psz_vcdfile = NULL;
    char *psz_cuefile = NULL;
840
    FILE *cuefile     = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
841 842 843 844 845
    char line[1024];

    /* Check if we are dealing with a .cue file */
    p_pos = strrchr( psz_dev, '.' );
    if( p_pos && !strcmp( p_pos, ".cue" ) )
846
    {
847 848 849 850 851 852 853 854 855 856 857 858 859
        /* psz_dev must be the cue file. Let's assume there's a .bin
         * file with the same filename */
        if( p_pos )
        {
            psz_vcdfile = malloc( p_pos - psz_dev + 5 /* ".bin" */ );
            strncpy( psz_vcdfile, psz_dev, p_pos - psz_dev );
            strcpy( psz_vcdfile + (p_pos - psz_dev), ".bin");
        }
        else
        {
            psz_vcdfile = malloc( strlen(psz_dev) + 5 /* ".bin" */ );
            sprintf( psz_vcdfile, "%s.bin", psz_dev );
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
        psz_cuefile = strdup( psz_dev );
    }
    else
    {
        /* psz_dev must be the actual vcd file. Let's assume there's a .cue
         * file with the same filename */
        if( p_pos )
        {
            psz_cuefile = malloc( p_pos - psz_dev + 5 /* ".cue" */ );
            strncpy( psz_cuefile, psz_dev, p_pos - psz_dev );
            strcpy( psz_cuefile + (p_pos - psz_dev), ".cue");
        }
        else
        {
            psz_cuefile = malloc( strlen(psz_dev) + 5 /* ".cue" */ );
            sprintf( psz_cuefile, "%s.cue", psz_dev );
        }
877 878
        /* If we need to look up the .cue file, then we don't have to look for the vcd */
        psz_vcdfile = strdup( psz_dev );
879 880
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
881
    /* Open the cue file and try to parse it */
Gildas Bazin's avatar
 
Gildas Bazin committed
882
    msg_Dbg( p_this,"trying .cue file: %s", psz_cuefile );
883
    cuefile = utf8_fopen( psz_cuefile, "rt" );
884 885 886 887 888 889 890 891 892 893
    if( cuefile == NULL )
    {
        i_ret = -1;
        msg_Dbg( p_this, "could not find .cue file" );
        goto error;
    }

    msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
    p_vcddev->i_vcdimage_handle = utf8_open( psz_vcdfile,
                                    O_RDONLY | O_NONBLOCK | O_BINARY, 0666 );
894 895
 
    if( p_vcddev->i_vcdimage_handle == -1 &&
896
        fscanf( cuefile, "FILE %c", line ) &&
Gildas Bazin's avatar
 
Gildas Bazin committed
897
        fgets( line, 1024, cuefile ) )
898
    {
899 900
        /* We have a cue file, but no valid vcd file yet */
        free( psz_vcdfile );
Gildas Bazin's avatar
 
Gildas Bazin committed
901 902 903 904 905 906
        p_pos = strchr( line, '"' );
        if( p_pos )
        {
            *p_pos = 0;

            /* Take care of path standardization */
Gildas Bazin's avatar
 
Gildas Bazin committed
907 908
            if( *line != '/' && ((p_pos = strrchr( psz_cuefile, '/' ))
                || (p_pos = strrchr( psz_cuefile, '\\' ) )) )
Gildas Bazin's avatar
 
Gildas Bazin committed
909 910 911 912 913 914 915 916
            {
                psz_vcdfile = malloc( strlen(line) +
                                      (p_pos - psz_cuefile + 1) + 1 );
                strncpy( psz_vcdfile, psz_cuefile, (p_pos - psz_cuefile + 1) );
                strcpy( psz_vcdfile + (p_pos - psz_cuefile + 1), line );
            }
            else psz_vcdfile = strdup( line );
        }
917 918 919
        msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
        p_vcddev->i_vcdimage_handle = utf8_open( psz_vcdfile,
                                        O_RDONLY | O_NONBLOCK | O_BINARY, 0666 );
920 921
    }

922
    if( p_vcddev->i_vcdimage_handle == -1)
923
    {
924 925
        i_ret = -1;
        goto error;
926
    }
927
    else i_ret = 0;
928

Gildas Bazin's avatar
 
Gildas Bazin committed
929 930 931 932 933 934 935 936
    /* Try to parse the i_tracks and p_sectors info so we can just forget
     * about the cuefile */
    if( i_ret == 0 )
    {
        int p_sectors[100];
        int i_tracks = 0;
        int i_num;
        char psz_dummy[10];
937

Gildas Bazin's avatar
 
Gildas Bazin committed
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
        while( fgets( line, 1024, cuefile ) )
        {
            /* look for a TRACK line */
            if( !sscanf( line, "%9s", psz_dummy ) ||
                strcmp(psz_dummy, "TRACK") )
                continue;

            /* look for an INDEX line */
            while( fgets( line, 1024, cuefile ) )
            {
                int i_min, i_sec, i_frame;

                if( (sscanf( line, "%9s %2u %2u:%2u:%2u", psz_dummy, &i_num,
                            &i_min, &i_sec, &i_frame ) != 5) || (i_num != 1) )
                    continue;

                i_tracks++;
                p_sectors[i_tracks - 1] = MSF_TO_LBA(i_min, i_sec, i_frame);
                msg_Dbg( p_this, "vcd track %i begins at sector:%i",
                         i_tracks - 1, p_sectors[i_tracks - 1] );
                break;
            }
        }

        /* fill in the last entry */
        p_sectors[i_tracks] = lseek(p_vcddev->i_vcdimage_handle, 0, SEEK_END)
                                / VCD_SECTOR_SIZE;
        msg_Dbg( p_this, "vcd track %i, begins at sector:%i",
                 i_tracks, p_sectors[i_tracks] );
        p_vcddev->i_tracks = i_tracks;
        p_vcddev->p_sectors = malloc( (i_tracks + 1) * sizeof(int) );
        memcpy( p_vcddev->p_sectors, p_sectors, (i_tracks + 1) * sizeof(int) );
970 971 972

    }

973
error:
Gildas Bazin's avatar
 
Gildas Bazin committed
974
    if( cuefile ) fclose( cuefile );
975 976
    free( psz_cuefile );
    free( psz_vcdfile );
977

Gildas Bazin's avatar
 
Gildas Bazin committed
978 979 980 981 982 983 984 985 986 987 988 989 990
    return i_ret;
}

/****************************************************************************
 * CloseVCDImage: closes a vcd image opened by OpenVCDImage
 ****************************************************************************/
static void CloseVCDImage( vlc_object_t * p_this, vcddev_t *p_vcddev )
{
    if( p_vcddev->i_vcdimage_handle != -1 )
        close( p_vcddev->i_vcdimage_handle );
    else
        return;

991
    free( p_vcddev->p_sectors );
992 993
}

994
#if defined( __APPLE__ )
995
/****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
996
 * darwin_getTOC: get the TOC
997
 ****************************************************************************/
998
static CDTOC *darwin_getTOC( vlc_object_t * p_this, const vcddev_t *p_vcddev )
999 1000 1001 1002 1003 1004 1005
{
    mach_port_t port;
    char *psz_devname;
    kern_return_t ret;
    CDTOC *pTOC = NULL;
    io_iterator_t iterator;
    io_registry_entry_t service;
1006
    CFMutableDictionaryRef properties;
1007 1008 1009
    CFDataRef data;

    /* get the device name */
Gildas Bazin's avatar
 
Gildas Bazin committed
1010
    if( ( psz_devname = strrchr( p_vcddev->psz_dev, '/') ) != NULL )
1011 1012
        ++psz_devname;
    else
Gildas Bazin's avatar
 
Gildas Bazin committed
1013
        psz_devname = p_vcddev->psz_dev;
1014 1015 1016 1017 1018 1019 1020 1021

    /* unraw the device name */
    if( *psz_devname == 'r' )
        ++psz_devname;

    /* get port for IOKit communication */
    if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS )
    {
1022
        msg_Err( p_this, "IOMasterPort: 0x%08x", ret );
1023 1024 1025 1026
        return( NULL );
    }

    /* get service iterator for the device */
1027
    if( ( ret = IOServiceGetMatchingServices(
1028 1029 1030
                    port, IOBSDNameMatching( port, 0, psz_devname ),
                    &iterator ) ) != KERN_SUCCESS )
    {
1031
        msg_Err( p_this, "IOServiceGetMatchingServices: 0x%08x", ret );
1032 1033 1034 1035 1036 1037 1038
        return( NULL );
    }

    /* first service */
    service = IOIteratorNext( iterator );
    IOObjectRelease( iterator );

1039
    /* search for kIOCDMediaClass */
1040 1041
    while( service && !IOObjectConformsTo( service, kIOCDMediaClass ) )
    {
1042
        if( ( ret = IORegistryEntryGetParentIterator( service,
1043 1044
                        kIOServicePlane, &iterator ) ) != KERN_SUCCESS )
        {
1045
            msg_Err( p_this, "IORegistryEntryGetParentIterator: 0x%08x", ret );
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
            IOObjectRelease( service );
            return( NULL );
        }

        IOObjectRelease( service );
        service = IOIteratorNext( iterator );
        IOObjectRelease( iterator );
    }

    if( service == NULL )
    {
1057
        msg_Err( p_this, "search for kIOCDMediaClass came up empty" );
1058 1059 1060 1061 1062 1063 1064
        return( NULL );
    }

    /* create a CF dictionary containing the TOC */
    if( ( ret = IORegistryEntryCreateCFProperties( service, &properties,
                    kCFAllocatorDefault, kNilOptions ) ) != KERN_SUCCESS )
    {
1065
        msg_Err( p_this, "IORegistryEntryCreateCFProperties: 0x%08x", ret );
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
        IOObjectRelease( service );
        return( NULL );
    }

    /* get the TOC from the dictionary */
    if( ( data = (CFDataRef) CFDictionaryGetValue( properties,
                                    CFSTR(kIOCDMediaTOCKey) ) ) != NULL )
    {
        CFRange range;
        CFIndex buf_len;

        buf_len = CFDataGetLength( data ) + 1;
        range = CFRangeMake( 0, buf_len );

        if( ( pTOC = (CDTOC *)malloc( buf_len ) ) != NULL )
        {
            CFDataGetBytes( data, range, (u_char *)pTOC );
        }
    }
    else
    {
1087
        msg_Err( p_this, "CFDictionaryGetValue failed" );
1088 1089 1090
    }

    CFRelease( properties );
1091
    IOObjectRelease( service );
1092

1093
    return( pTOC );
1094 1095 1096
}

/****************************************************************************
1097
 * darwin_getNumberOfTracks: get number of tracks in TOC
1098
 ****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
1099
static int darwin_getNumberOfTracks( CDTOC *pTOC, int i_descriptors )
1100 1101
{
    u_char track;
1102
    int i, i_tracks = 0;
1103
    CDTOCDescriptor *pTrackDescriptors = NULL;
1104

1105
    pTrackDescriptors = (CDTOCDescriptor *)pTOC->descriptors;
1106

1107
    for( i = i_descriptors; i > 0; i-- )
1108 1109 1110 1111 1112 1113
    {
        track = pTrackDescriptors[i].point;

        if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
            continue;

1114
        i_tracks++;
1115 1116 1117 1118
    }

    return( i_tracks );
}
1119
#endif /* __APPLE__ */
Gildas Bazin's avatar
 
Gildas Bazin committed
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162

#if defined( WIN32 )
/*****************************************************************************
 * win32_vcd_open: open vcd drive
 *****************************************************************************
 * Load and use aspi if it is available, otherwise use IOCTLs on WinNT/2K/XP.
 *****************************************************************************/
static int win32_vcd_open( vlc_object_t * p_this, const char *psz_dev,
                           vcddev_t *p_vcddev )
{
    /* Initializations */
    p_vcddev->h_device_handle = NULL;
    p_vcddev->i_sid = 0;
    p_vcddev->hASPI = 0;
    p_vcddev->lpSendCommand = 0;

    if( WIN_NT )
    {
        char psz_win32_drive[7];

        msg_Dbg( p_this, "using winNT/2K/XP ioctl layer" );

        sprintf( psz_win32_drive, "\\\\.\\%c:", psz_dev[0] );

        p_vcddev->h_device_handle = CreateFile( psz_win32_drive, GENERIC_READ,
                                            FILE_SHARE_READ | FILE_SHARE_WRITE,
                                            NULL, OPEN_EXISTING,
                                            FILE_FLAG_NO_BUFFERING |
                                            FILE_FLAG_RANDOM_ACCESS, NULL );
        return (p_vcddev->h_device_handle == NULL) ? -1 : 0;
    }
    else
    {
        HMODULE hASPI = NULL;
        long (*lpGetSupport)( void ) = NULL;
        long (*lpSendCommand)( void* ) = NULL;
        DWORD dwSupportInfo;
        int i, j, i_hostadapters;
        char c_drive = psz_dev[0];

        hASPI = LoadLibrary( "wnaspi32.dll" );
        if( hASPI != NULL )
        {
1163 1164
            lpGetSupport = (void *)GetProcAddress( hASPI, "GetASPI32SupportInfo" );
            lpSendCommand = (void *)GetProcAddress( hASPI, "SendASPI32Command" );
Gildas Bazin's avatar
 
Gildas Bazin committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
        }

        if( hASPI == NULL || lpGetSupport == NULL || lpSendCommand == NULL )
        {
            msg_Dbg( p_this,
                     "unable to load aspi or get aspi function pointers" );
            if( hASPI ) FreeLibrary( hASPI );
            return -1;
        }

        /* ASPI support seems to be there */

        dwSupportInfo = lpGetSupport();

        if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS )
        {
            msg_Dbg( p_this, "no host adapters found (aspi)" );
            FreeLibrary( hASPI );
            return -1;
        }

        if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP )
        {
            msg_Dbg( p_this, "unable to initalize aspi layer" );
            FreeLibrary( hASPI );
            return -1;
        }

        i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
        if( i_hostadapters == 0 )
        {
            FreeLibrary( hASPI );
            return -1;
        }

        c_drive = c_drive > 'Z' ? c_drive - 'a' : c_drive - 'A';

        for( i = 0; i < i_hostadapters; i++ )
        {
          for( j = 0; j < 15; j++ )
          {
              struct SRB_GetDiskInfo srbDiskInfo;

              srbDiskInfo.SRB_Cmd         = SC_GET_DISK_INFO;
              srbDiskInfo.SRB_HaId        = i;
              srbDiskInfo.SRB_Flags       = 0;
              srbDiskInfo.SRB_Hdr_Rsvd    = 0;
              srbDiskInfo.SRB_Target      = j;
              srbDiskInfo.SRB_Lun         = 0;

              lpSendCommand( (void*) &srbDiskInfo );

              if( (srbDiskInfo.SRB_Status == SS_COMP) &&
                  (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) )
              {
                  /* Make sure this is a cdrom device */
                  struct SRB_GDEVBlock   srbGDEVBlock;

                  memset( &srbGDEVBlock, 0, sizeof(struct SRB_GDEVBlock) );
                  srbGDEVBlock.SRB_Cmd    = SC_GET_DEV_TYPE;
                  srbGDEVBlock.SRB_HaId   = i;
                  srbGDEVBlock.SRB_Target = j;

                  lpSendCommand( (void*) &srbGDEVBlock );

                  if( ( srbGDEVBlock.SRB_Status == SS_COMP ) &&
                      ( srbGDEVBlock.SRB_DeviceType == DTYPE_CDROM ) )
                  {
                      p_vcddev->i_sid = MAKEWORD( i, j );
                      p_vcddev->hASPI = (long)hASPI;
                      p_vcddev->lpSendCommand = lpSendCommand;
                      msg_Dbg( p_this, "using aspi layer" );

                      return 0;
                  }
                  else
                  {
                      FreeLibrary( hASPI );
Gildas Bazin's avatar
 
Gildas Bazin committed
1243
                      msg_Dbg( p_this, "%c: is not a cdrom drive",
Gildas Bazin's avatar
 
Gildas Bazin committed
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
                               psz_dev[0] );
                      return -1;
                  }
              }
          }
        }

        FreeLibrary( hASPI );
        msg_Dbg( p_this, "unable to get haid and target (aspi)" );

    }

    return -1;
}

#endif /* WIN32 */