Search
lxdream.org :: lxdream/src/drivers/cdrom/sector.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/cdrom/sector.h
changeset 1097:d4807997e450
next1107:7b279d10f46f
author nkeynes
date Sun Jan 31 18:35:06 2010 +1000 (13 years ago)
permissions -rw-r--r--
last change Refactor CDROM host support
- Completely separate GDROM hardware (in gdrom/gdrom.c) from generic CDROM
support (now in drivers/cdrom)
- Add concept of 'sector sources' that can be mixed and matched to create
cdrom discs (makes support of arbitrary disc types much simpler)
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * low-level 'block device' for input to cdrom discs.
     5  *
     6  * Copyright (c) 2009 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #ifndef cdrom_sector_H
    20 #define cdrom_sector_H 1
    22 #include <stdio.h>
    23 #include <glib/gtypes.h>
    24 #include "drivers/cdrom/defs.h"
    26 #ifdef __cplusplus
    27 extern "C" {
    28 #endif
    30 /******************************** Sector Source ******************************/
    31 #define SECTOR_SOURCE_TAG 0x42444556
    32 typedef struct sector_source *sector_source_t;
    33 typedef enum {
    34     NULL_SECTOR_SOURCE,
    35     FILE_SECTOR_SOURCE,
    36     DISC_SECTOR_SOURCE,
    37     TRACK_SECTOR_SOURCE
    38 } sector_source_type_t;
    40 typedef cdrom_error_t (*sector_source_read_fn_t)(sector_source_t, cdrom_lba_t, cdrom_count_t, unsigned char *outbuf);
    41 typedef cdrom_error_t (*sector_source_read_sectors_fn_t)(sector_source_t, cdrom_lba_t, cdrom_count_t, cdrom_read_mode_t mode,
    42         unsigned char *outbuf, size_t *length);
    43 typedef void (*sector_source_destroy_fn_t)(sector_source_t);
    45 /**
    46  * A 'sector source' is a read-only random-access data source that supports
    47  * reads of arbitrary blocks within their capacity. A block device has a
    48  * defined mode, block size, and block count.
    49  *
    50  * Source are ref-counted, and automatically destroyed when the reference
    51  * count reaches 0.
    52  */
    53 struct sector_source {
    54     uint32_t tag;       /* sector source tag */
    55     uint32_t ref_count; /* Reference count. Initialized to 0 */
    56     sector_source_type_t type;
    58     sector_mode_t mode; /* Implies sector size. */
    59     uint32_t size; /* Block count */
    61     /**
    62      * Read blocks from the device using the native block size.
    63      * @param buf Buffer to receive the blocks
    64      * @param block First block to transfer (numbered from 0)
    65      * @param block_count number of blocks to transfer.
    66      * @return 0 on success, otherwise an error code.
    67      */
    68     sector_source_read_fn_t read_blocks;
    70     /**
    71      * Read sectors from the device using the specified read mode, performing any
    72      * necessary conversions.
    73      */
    74     sector_source_read_sectors_fn_t read_sectors;
    76     /**
    77      * Release all resources and memory used by the device (note should never
    78      * be called directly
    79      */
    80     sector_source_destroy_fn_t destroy;
    82 };
    84 /**
    85  * Block device that always returns zeros.
    86  */
    87 sector_source_t null_sector_source_new( sector_mode_t mode, cdrom_count_t size );
    89 #define FILE_SECTOR_FULL_FILE ((cdrom_count_t)-1)
    91 /**
    92  * File reader. Last block is 0-padded.
    93  */
    94 sector_source_t file_sector_source_new_filename( const gchar *filename, sector_mode_t mode,
    95                                                  uint32_t offset, cdrom_count_t sector_count );
    96 sector_source_t file_sector_source_new( FILE *f, sector_mode_t mode, uint32_t offset, cdrom_count_t sector_count,
    97                                                 gboolean closeOnDestroy );
    98 sector_source_t file_sector_source_new_full( FILE *f, sector_mode_t mode, gboolean closeOnDestroy );
   100 /**
   101  * Construct a file source that shares its file descriptor with another
   102  * file source.
   103  */
   104 sector_source_t file_sector_source_new_source( sector_source_t ref, sector_mode_t mode, uint32_t offset,
   105                                                cdrom_count_t sector_count );
   107 /**
   108  * Change the value of the source's closeOnDestroy flag
   109  */
   110 void file_sector_source_set_close_on_destroy( sector_source_t ref, gboolean closeOnDestroy );
   112 /**
   113  * Retrieve the source's underlying FILE
   114  */
   115 FILE *file_sector_source_get_file( sector_source_t ref );
   117 /**
   118  * Retrieve the source's underlying file descriptor
   119  */
   120 int file_sector_source_get_fd( sector_source_t ref );
   122 /**
   123  * Increment the reference count for a block device.
   124  */
   125 void sector_source_ref( sector_source_t device );
   127 /**
   128  * Unreference a block device. If decremented to 0, the device will be
   129  * destroyed.
   130  */
   131 void sector_source_unref( sector_source_t device );
   133 /**
   134  * Release an unbound block device. If the ref count is 0, the device is
   135  * destroyed. Otherwise the function has no effect.
   136  */
   137 void sector_source_release( sector_source_t device );
   139 cdrom_error_t sector_source_read( sector_source_t device, cdrom_lba_t lba, cdrom_count_t block_count, unsigned char *buf );
   141 cdrom_error_t sector_source_read_sectors( sector_source_t device, cdrom_lba_t lba, cdrom_count_t block_count,
   142                                           cdrom_read_mode_t mode, unsigned char *buf, size_t *length );
   144 /***** Internals for sector source implementations *****/
   146 /**
   147  * Initialize a new (pre-allocated) sector source
   148  */
   149 sector_source_t sector_source_init( sector_source_t device, sector_source_type_t type, sector_mode_t mode, cdrom_count_t size,
   150                         sector_source_read_fn_t readfn, sector_source_destroy_fn_t destroyfn );
   152 /**
   153  * Default sector source destructor method
   154  */
   155 void default_sector_source_destroy( sector_source_t device );
   158 /**
   159  * Extract the necessary fields from a single raw sector for the given read mode.
   160  * @param raw_sector input raw 2352 byte sector
   161  * @param mode sector mode and field specification flags
   162  * @param buf output buffer for sector data
   163  * @param length output length of sector written to buf
   164  * @return CDROM_ERROR_OK on success, otherwise an appropriate error code.
   165  */
   166 cdrom_error_t sector_extract_from_raw( unsigned char *raw_sector, cdrom_read_mode_t mode, unsigned char *buf, size_t *length );
   168 /**
   169  * Test if the given pointer is a valid sector source
   170  */
   171 #define IS_SECTOR_SOURCE(dev) ((dev) != NULL && ((sector_source_t)(dev))->tag == SECTOR_SOURCE_TAG)
   173  /**
   174   * Test if the given pointer is a valid sector source of the given type
   175   */
   176 #define IS_SECTOR_SOURCE_TYPE(dev,id) (IS_SECTOR_SOURCE(dev) && ((sector_source_t)(dev))->type == id)
   180 #ifdef __cplusplus
   181 }
   182 #endif
   184 #endif /* !cdrom_sector_H */
.