DVDioctl.cpp 20.3 KB
Newer Older
Sam Hocevar's avatar
 
Sam Hocevar committed
1 2 3 4 5
/*****************************************************************************
 * DVDioctl.cpp: Linux-like DVD driver for Darwin and MacOS X
 *****************************************************************************
 * Copyright (C) 1998-2000 Apple Computer, Inc. All rights reserved.
 * Copyright (C) 2001 VideoLAN
Sam Hocevar's avatar
 
Sam Hocevar committed
6
 * $Id: DVDioctl.cpp,v 1.3 2001/04/04 16:33:07 sam Exp $
Sam Hocevar's avatar
 
Sam Hocevar committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *
 * The contents of this file constitute Original Code as defined in and
 * are subject to the Apple Public Source License Version 1.1 (the
 * "License").  You may not use this file except in compliance with the
 * License.  Please obtain a copy of the License at
 * http://www.apple.com/publicsource and read it before using this file.
 * 
 * This Original Code and all software distributed under the License are
 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
 * License for the specific language governing rights and limitations
 * under the License.
 *****************************************************************************/

/*****************************************************************************
 * TODO:
 * - add a timeout to waitForService() so that we don't wait forever
 * - find a way to prevent user from ejecting DVD using the GUI while
 *   it is still in use
 *****************************************************************************/

Sam Hocevar's avatar
 
Sam Hocevar committed
32 33 34
//XXX: uncomment to activate the key exchange ioctls - may hang the machine
//#define ACTIVATE_DANGEROUS_IOCTL 1

Sam Hocevar's avatar
 
Sam Hocevar committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/*****************************************************************************
 * Preamble
 *****************************************************************************/
extern "C"
{
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/syslog.h>
#include <sys/systm.h>
#include <sys/ioccom.h>
#include <sys/fcntl.h>
#include <sys/buf.h>
#include <sys/uio.h>
#include <miscfs/devfs/devfs.h>

#include <mach/mach_types.h>
}

#include <IOKit/IOLib.h>
#include <IOKit/IOService.h>
#include <IOKit/storage/IOMedia.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
56 57
#include <IOKit/storage/IODVDMedia.h>
#include <IOKit/storage/IODVDBlockStorageDriver.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

#include "DVDioctl.h"

/*****************************************************************************
 * Driver class
 *****************************************************************************/
class DVDioctl : public IOService
{
    OSDeclareDefaultStructors( DVDioctl )

public:

    virtual bool       init   ( OSDictionary *dictionary = 0 );
    virtual IOService *probe  ( IOService *provider, SInt32 *score );
    virtual bool       start  ( IOService *provider );
    virtual void       stop   ( IOService *provider );
    virtual void       free   ( void );
};

#define super IOService
OSDefineMetaClassAndStructors( DVDioctl, IOService )

/*****************************************************************************
 * Variable typedefs
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
83 84 85 86
typedef enum       { DKRTYPE_BUF, DKRTYPE_DIO }      dkrtype_t;
typedef struct dio { dev_t dev; struct uio * uio; }  dio_t;
typedef struct buf                                   buf_t;
typedef void *                                       dkr_t;
Sam Hocevar's avatar
 
Sam Hocevar committed
87 88 89 90

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
91 92 93 94 95 96
static int  DVDClose        ( dev_t, int, int, struct proc * );
static int  DVDBlockIoctl   ( dev_t, u_long, caddr_t, int, struct proc * );
static int  DVDOpen         ( dev_t, int, int, struct proc * );
static int  DVDSize         ( dev_t );
static void DVDStrategy     ( buf_t * );
static int  DVDReadWrite    ( dkr_t, dkrtype_t );
Sam Hocevar's avatar
 
Sam Hocevar committed
97 98 99 100
static void DVDReadWriteCompletion( void *, void *, IOReturn, UInt64 );

static struct bdevsw device_functions =
{
Sam Hocevar's avatar
 
Sam Hocevar committed
101
    DVDOpen, DVDClose, DVDStrategy, DVDBlockIoctl, eno_dump, DVDSize, D_DISK
Sam Hocevar's avatar
 
Sam Hocevar committed
102 103 104 105 106 107 108 109 110 111 112
};

/*****************************************************************************
 * Local variables
 *****************************************************************************/
static DVDioctl * p_this = NULL;

static bool b_inuse;
static int i_major;
static void *p_node;
static IODVDMedia *p_dvd;
Sam Hocevar's avatar
 
Sam Hocevar committed
113
static IODVDBlockStorageDriver *p_drive;
Sam Hocevar's avatar
 
Sam Hocevar committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

/*****************************************************************************
 * DKR_GET_DEV: borrowed from IOMediaBSDClient.cpp
 *****************************************************************************/
static inline dev_t DKR_GET_DEV(dkr_t dkr, dkrtype_t dkrtype)
{
    return (dkrtype == DKRTYPE_BUF)
           ? ((buf_t *)dkr)->b_dev : ((dio_t *)dkr)->dev;
}

/*****************************************************************************
 * DKR_GET_BYTE_COUNT: borrowed from IOMediaBSDClient.cpp
 *****************************************************************************/
static inline UInt64 DKR_GET_BYTE_COUNT(dkr_t dkr, dkrtype_t dkrtype)
{
    return (dkrtype == DKRTYPE_BUF)
           ? ((buf_t *)dkr)->b_bcount : ((dio_t *)dkr)->uio->uio_resid;
}

/*****************************************************************************
 * DKR_GET_BYTE_START: borrowed from IOMediaBSDClient.cpp
 *****************************************************************************/
static inline UInt64 DKR_GET_BYTE_START(dkr_t dkr, dkrtype_t dkrtype)
{
    if (dkrtype == DKRTYPE_BUF)
    {
        buf_t * bp    = (buf_t *)dkr;
        return bp->b_blkno * p_dvd->getPreferredBlockSize();
    }
    return ((dio_t *)dkr)->uio->uio_offset;
}

/*****************************************************************************
 * DKR_IS_READ: borrowed from IOMediaBSDClient.cpp
 *****************************************************************************/
static inline bool DKR_IS_READ(dkr_t dkr, dkrtype_t dkrtype)
{
    return (dkrtype == DKRTYPE_BUF)
           ? ((((buf_t *)dkr)->b_flags & B_READ) == B_READ)
           : ((((dio_t *)dkr)->uio->uio_rw) == UIO_READ);
}

/*****************************************************************************
 * DKR_IS_ASYNCHRONOUS: borrowed from IOMediaBSDClient.cpp
 *****************************************************************************/
static inline bool DKR_IS_ASYNCHRONOUS(dkr_t dkr, dkrtype_t dkrtype)
{
    return (dkrtype == DKRTYPE_BUF) ? true : false;
}

/*****************************************************************************
 * DKR_IS_RAW: borrowed from IOMediaBSDClient.cpp
 *****************************************************************************/
static inline bool DKR_IS_RAW(dkr_t dkr, dkrtype_t dkrtype)
{
    return (dkrtype == DKRTYPE_BUF) ? false : true;
}

/*****************************************************************************
 * DKR_SET_BYTE_COUNT: borrowed from IOMediaBSDClient.cpp
 *****************************************************************************/
static inline void DKR_SET_BYTE_COUNT(dkr_t dkr, dkrtype_t dkrtype, UInt64 bcount)
{
    if (dkrtype == DKRTYPE_BUF)
    {
        ((buf_t *)dkr)->b_resid = ((buf_t *)dkr)->b_bcount - bcount;
    }
    else
    {
        ((dio_t *)dkr)->uio->uio_resid -= bcount;
    }
}

/*****************************************************************************
 * DKR_RUN_COMPLETION: borrowed from IOMediaBSDClient.cpp
 *****************************************************************************/
static inline void DKR_RUN_COMPLETION(dkr_t dkr, dkrtype_t dkrtype, IOReturn status)
{
    if (dkrtype == DKRTYPE_BUF)
    {
        buf_t * bp = (buf_t *)dkr;

        bp->b_error  = p_this->errnoFromReturn(status);
        bp->b_flags |= (status != kIOReturnSuccess) ? B_ERROR : 0;
        biodone(bp);
    }
}

/*****************************************************************************
 * DKR_GET_BUFFER: borrowed from IOMediaBSDClient.cpp
 *****************************************************************************/
static inline IOMemoryDescriptor * DKR_GET_BUFFER(dkr_t dkr, dkrtype_t dkrtype)
{
    if (dkrtype == DKRTYPE_BUF)
    {
        buf_t * bp = (buf_t *)dkr;

        if ( (bp->b_flags & B_VECTORLIST) )
        {
            assert(sizeof(IOPhysicalRange         ) == sizeof(iovec          ));
            assert(sizeof(IOPhysicalRange::address) == sizeof(iovec::iov_base));
            assert(sizeof(IOPhysicalRange::length ) == sizeof(iovec::iov_len ));
            return IOMemoryDescriptor::withPhysicalRanges(
              (IOPhysicalRange *) bp->b_vectorlist,
              (UInt32)            bp->b_vectorcount,
              (bp->b_flags & B_READ) ? kIODirectionIn : kIODirectionOut,
              true );
        }

        return IOMemoryDescriptor::withAddress(
          (vm_address_t) bp->b_data,
          (vm_size_t)    bp->b_bcount,
          (bp->b_flags & B_READ) ? kIODirectionIn : kIODirectionOut,
          (bp->b_flags & B_PHYS) ? current_task() : kernel_task );
    }
    else
    {
        struct uio * uio = ((dio_t *)dkr)->uio;

        assert(sizeof(IOVirtualRange         ) == sizeof(iovec          ));
        assert(sizeof(IOVirtualRange::address) == sizeof(iovec::iov_base));
        assert(sizeof(IOVirtualRange::length ) == sizeof(iovec::iov_len ));

        return IOMemoryDescriptor::withRanges(
        (IOVirtualRange *) uio->uio_iov,
        (UInt32)           uio->uio_iovcnt,
        (uio->uio_rw     == UIO_READ    ) ? kIODirectionIn : kIODirectionOut,
        (uio->uio_segflg != UIO_SYSSPACE) ? current_task() : kernel_task,
        true );
    }
}

/*****************************************************************************
 * DVDioctl::init: initialize the driver structure
 *****************************************************************************/
bool DVDioctl::init( OSDictionary *p_dict = 0 )
{
    //IOLog( "DVD ioctl: initializing\n" );

    p_this = this;

    p_node  = NULL;
    p_dvd   = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
257
    p_drive = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 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 351 352 353 354 355 356 357 358 359 360 361 362 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
    i_major = -1;
    b_inuse = false;

    bool res = super::init( p_dict );

    return res;
}
    
/*****************************************************************************
 * DVDioctl::probe: check whether the driver can be safely activated
 *****************************************************************************/
IOService * DVDioctl::probe( IOService *provider, SInt32 *score )
{
    //IOLog( "DVD ioctl: probing\n" );
    IOService * res = super::probe( provider, score );

    return res;
}

/*****************************************************************************
 * DVDioctl::start: start the driver
 *****************************************************************************/
bool DVDioctl::start( IOService *provider )
{
    //IOLog( "DVD ioctl: starting\n" );

    if( !super::start( provider ) )
    {
        return false;
    }

    //IOLog( "DVD ioctl: creating device\n" );

    i_major = bdevsw_add( -1, &device_functions );

    if( i_major == -1 )
    {
        //log(LOG_INFO, "DVD ioctl: failed to allocate a major number\n");
        return false;
    }

    p_node = devfs_make_node ( makedev( i_major, 0 ), DEVFS_BLOCK,
                               UID_ROOT, GID_WHEEL, 0666, "dvd" );

    if( p_node == NULL )
    {
        //log( LOG_INFO, "DVD ioctl: failed creating node\n" );

        if( bdevsw_remove(i_major, &device_functions) == -1 )
        {
            //log( LOG_INFO, "DVD ioctl: bdevsw_remove failed\n" );
        }

        return false;
    }

    return true;
}

/*****************************************************************************
 * DVDioctl::stop: stop the driver
 *****************************************************************************/
void DVDioctl::stop( IOService *provider )
{
    //IOLog( "DVD ioctl: removing device\n" );

    if( p_node != NULL )
    {
        devfs_remove( p_node );
    }

    if( i_major != -1 )
    {
        if( bdevsw_remove(i_major, &device_functions) == -1 )
        {
            //log( LOG_INFO, "DVD ioctl: bdevsw_remove failed\n" );
        }
    }

    //IOLog( "DVD ioctl: stopping\n" );
    super::stop( provider );
}
  
/*****************************************************************************
 * DVDioctl::free: free all resources allocated by the driver
 *****************************************************************************/
void DVDioctl::free( void )
{
    //IOLog( "DVD ioctl: freeing\n" );
    super::free( );
}

/* following functions are local */

/*****************************************************************************
 * DVDOpen: look for an IODVDMedia object and open it
 *****************************************************************************/
static int DVDOpen( dev_t dev, int flags, int devtype, struct proc * )
{
    IOStorageAccess level;

    /* Check that the device hasn't already been opened */
    if( b_inuse )
    {
        //log( LOG_INFO, "DVD ioctl: already opened\n" );
        return EBUSY;
    }
    else
    {
        b_inuse = true;
    }

    IOService * p_root = IOService::getServiceRoot();
   
    if( p_root == NULL )
    {
        //log( LOG_INFO, "DVD ioctl: couldn't find root\n" );
        b_inuse = false;
        return ENXIO;
    }

    OSDictionary * p_dict = p_root->serviceMatching( kIODVDMediaClass );

    if( p_dict == NULL )
    {
        //log( LOG_INFO, "DVD ioctl: couldn't find dictionary\n" );
        b_inuse = false;
        return ENXIO;
    }

    p_dvd = OSDynamicCast( IODVDMedia, p_root->waitForService( p_dict ) );

    if( p_dvd == NULL )
    {
        //log( LOG_INFO, "DVD ioctl: couldn't find service\n" );
        b_inuse = false;
        return ENXIO;
    }

    //log( LOG_INFO, "DVD ioctl: found DVD\n" );

    level = (flags & FWRITE) ? kIOStorageAccessReaderWriter
                             : kIOStorageAccessReader;

Sam Hocevar's avatar
 
Sam Hocevar committed
402
    if( ! p_dvd->open( p_this, 0, level) )
Sam Hocevar's avatar
 
Sam Hocevar committed
403 404 405
    {
        log( LOG_INFO, "DVD ioctl: IODVDMedia object busy\n" );
        b_inuse = false;
Sam Hocevar's avatar
 
Sam Hocevar committed
406
        return EBUSY;
Sam Hocevar's avatar
 
Sam Hocevar committed
407 408
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
409 410 411 412 413
    p_drive = p_dvd->getProvider();

    log( LOG_INFO, "DVD ioctl: IODVDMedia->open()\n" );

    return 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
414 415 416 417 418 419 420 421 422
}

/*****************************************************************************
 * DVDClose: close the IODVDMedia object
 *****************************************************************************/
static int DVDClose( dev_t dev, int flags, int devtype, struct proc * )
{
    /* Release the device */
    p_dvd->close( p_this );
Sam Hocevar's avatar
 
Sam Hocevar committed
423 424 425

    p_dvd   = NULL;
    p_drive = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
    b_inuse = false;

    log( LOG_INFO, "DVD ioctl: IODVDMedia->close()\n" );

    return 0;
}

/*****************************************************************************
 * DVDSize: return the device size
 *****************************************************************************/
static int DVDSize( dev_t dev )
{
    return p_dvd->getPreferredBlockSize();
}

/*****************************************************************************
 * DVDStrategy: perform read or write operations
 *****************************************************************************/
static void DVDStrategy( buf_t * bp )
{
    DVDReadWrite(bp, DKRTYPE_BUF);
    return;
}

/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
451
 * DVDBlockIoctl: issue an ioctl on the block device
Sam Hocevar's avatar
 
Sam Hocevar committed
452
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
453 454
static int DVDBlockIoctl( dev_t dev, u_long cmd, caddr_t addr, int flags,
                          struct proc *p )
Sam Hocevar's avatar
 
Sam Hocevar committed
455
{
Sam Hocevar's avatar
 
Sam Hocevar committed
456 457
    dvdioctl_data_t * p_data = (dvdioctl_data_t *)addr;

Sam Hocevar's avatar
 
Sam Hocevar committed
458 459 460
    switch( cmd )
    {
        case IODVD_READ_STRUCTURE:
Sam Hocevar's avatar
 
Sam Hocevar committed
461 462 463

            log( LOG_INFO, "DVD ioctl: IODVD_READ_STRUCTURE\n" );

Sam Hocevar's avatar
 
Sam Hocevar committed
464 465 466
            return 0;

        case IODVD_SEND_KEY:
Sam Hocevar's avatar
 
Sam Hocevar committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

            log( LOG_INFO, "DVD ioctl: send key to `%s', "
                 "buf %d, format %d, class %d, agid %d\n",
                 p_drive->getDeviceTypeName(),
                 (int)p_data->p_buffer, p_data->i_keyformat,
                 p_data->i_keyclass, p_data->i_agid );

#ifdef ACTIVATE_DANGEROUS_IOCTL
            return p_drive->sendKey( (IOMemoryDescriptor *)p_data->p_buffer,
                                     (DVDKeyClass)p_data->i_keyclass,
                                     p_data->i_agid,
                                     (DVDKeyFormat)p_data->i_keyformat );
#else
            return -1;
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
482 483

        case IODVD_REPORT_KEY:
Sam Hocevar's avatar
 
Sam Hocevar committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

            log( LOG_INFO, "DVD ioctl: report key from `%s', "
                 p_drive->getDeviceTypeName(),
                 "buf %d, class %d, lba %d, agid %d, format %d\n",
                 (int)p_data->p_buffer, p_data->i_keyclass, p_data->i_lba,
                 p_data->i_agid, p_data->i_keyformat );

#ifdef ACTIVATE_DANGEROUS_IOCTL
            return p_drive->reportKey( (IOMemoryDescriptor *)p_data->p_buffer,
                                       (DVDKeyClass)p_data->i_keyclass,
                                       p_data->i_lba, p_data->i_agid,
                                       (DVDKeyFormat)p_data->i_keyformat );
#else
            return -1;
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
499 500

        default:
Sam Hocevar's avatar
 
Sam Hocevar committed
501 502 503

            log( LOG_INFO, "DVD ioctl: unknown ioctl\n" );

Sam Hocevar's avatar
 
Sam Hocevar committed
504 505 506 507 508 509 510 511 512 513 514 515 516 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 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
            return EINVAL;
    }
}

/*****************************************************************************
 * DVDReadWrite: borrowed from IOMediaBSDClient.cpp
 *****************************************************************************/
static int DVDReadWrite(dkr_t dkr, dkrtype_t dkrtype)
{
    IOMemoryDescriptor * buffer;
    register UInt64      byteCount;
    register UInt64      byteStart;
    UInt64               mediaSize;
    IOReturn             status;

    byteCount = DKR_GET_BYTE_COUNT(dkr, dkrtype);
    byteStart = DKR_GET_BYTE_START(dkr, dkrtype);
    mediaSize = p_dvd->getSize();

    if ( byteStart >= mediaSize )
    {
        status = DKR_IS_READ(dkr,dkrtype) ? kIOReturnSuccess : kIOReturnIOError;        goto dkreadwriteErr;
    }

    if ( DKR_IS_RAW(dkr, dkrtype) )
    {
        UInt64 mediaBlockSize = p_dvd->getPreferredBlockSize();

        if ( (byteStart % mediaBlockSize) || (byteCount % mediaBlockSize) )
        {
            status = kIOReturnNotAligned;
            goto dkreadwriteErr;
        }
    }

    buffer = DKR_GET_BUFFER(dkr, dkrtype);

    if ( buffer == 0 )
    {
        status = kIOReturnNoMemory;
        goto dkreadwriteErr;
    }

    if ( byteCount > mediaSize - byteStart )
    {
        IOMemoryDescriptor * originalBuffer = buffer;

        buffer = IOMemoryDescriptor::withSubRange( originalBuffer, 0,
                     mediaSize - byteStart, originalBuffer->getDirection() );
        originalBuffer->release();
        if ( buffer == 0 )
        {
            status = kIOReturnNoMemory;
            goto dkreadwriteErr;
        }
    }

    if ( DKR_IS_ASYNCHRONOUS(dkr, dkrtype) )
    {
        IOStorageCompletion completion;

        completion.target    = dkr;
        completion.action    = DVDReadWriteCompletion;
        completion.parameter = (void *) dkrtype;

        if ( DKR_IS_READ(dkr, dkrtype) )
        {
            p_dvd->read(  p_this, byteStart, buffer, completion );
        }
        else
        {
            p_dvd->write( p_this, byteStart, buffer, completion );
        }

        status = kIOReturnSuccess;
    }
    else
    {
        if ( DKR_IS_READ(dkr, dkrtype) )
        {
            status = p_dvd->IOStorage::read( p_this, byteStart,
                                             buffer, &byteCount );
        }
        else
        {
            status = p_dvd->IOStorage::write( p_this, byteStart,
                                              buffer, &byteCount );
        }

        DVDReadWriteCompletion(dkr, (void *)dkrtype, status, byteCount);
    }

    buffer->release();
    return p_this->errnoFromReturn(status);
dkreadwriteErr:

    DVDReadWriteCompletion(dkr, (void *)dkrtype, status, 0);

    return p_this->errnoFromReturn(status);
}

/*****************************************************************************
 * DVDReadWriteCompletion: borrowed from IOMediaBSDClient.cpp
 *****************************************************************************/
static void DVDReadWriteCompletion( void *   target,
                                    void *   parameter,
                                    IOReturn status,
                                    UInt64   actualByteCount )
{
    dkr_t     dkr      = (dkr_t) target;
    dkrtype_t dkrtype  = (dkrtype_t) (int) parameter;
    dev_t     dev      = DKR_GET_DEV(dkr, dkrtype);

    if ( status != kIOReturnSuccess )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
619
        IOLog( "DVD ioctl: %s (is the disc authenticated ?)\n",
Sam Hocevar's avatar
 
Sam Hocevar committed
620 621 622 623 624 625 626
               p_this->stringFromReturn(status) );
    }

    DKR_SET_BYTE_COUNT(dkr, dkrtype, actualByteCount);
    DKR_RUN_COMPLETION(dkr, dkrtype, status);
}