Attempt to add FreeBSD CAM access method. Hope I havent' broken

FreeBSD otherwise.
This commit is contained in:
rocky
2004-04-30 09:59:54 +00:00
parent 06a44699c8
commit a4cc1496e5
5 changed files with 1288 additions and 3 deletions

536
lib/FreeBSD/freebsd.c Normal file
View File

@@ -0,0 +1,536 @@
/*
$Id: freebsd.c,v 1.1 2004/04/30 09:59:54 rocky Exp $
Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
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.
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
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* This file contains FreeBSD-specific code and implements low-level
control of the CD drive. Culled initially I think from xine's or
mplayer's FreeBSD code with lots of modifications.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
static const char _rcsid[] = "$Id: freebsd.c,v 1.1 2004/04/30 09:59:54 rocky Exp $";
#include "freebsd.h"
#ifdef HAVE_FREEBSD_CDROM
static access_mode_t
str_to_access_mode_freebsd(const char *psz_access_mode)
{
const access_mode_t default_access_mode = _AM_IOCTL;
if (NULL==psz_access_mode) return default_access_mode;
if (!strcmp(psz_access_mode, "IOCTL"))
return _AM_IOCTL;
else if (!strcmp(psz_access_mode, "CAM"))
return _AM_CAM;
else {
cdio_warn ("unknown access type: %s. Default IOCTL used.",
psz_access_mode);
return default_access_mode;
}
}
static void
_free_freebsd (void *obj)
{
_img_private_t *env = obj;
if (NULL == env) return;
if (_AM_CAM == env->access_mode)
return free_freebsd_cam(env);
else
return cdio_generic_free(obj);
}
/* Check a drive to see if it is a CD-ROM
Return 1 if a CD-ROM. 0 if it exists but isn't a CD-ROM drive
and -1 if no device exists .
*/
static bool
cdio_is_cdrom(char *drive, char *mnttype)
{
return cdio_is_cdrom_freebsd_ioctl(drive, mnttype);
}
/*!
Reads a single mode2 sector from cd device into data starting from lsn.
Returns 0 if no error.
*/
static int
_read_audio_sectors_freebsd (void *env, void *data, lsn_t lsn,
unsigned int nblocks)
{
return read_audio_sectors_freebsd_ioctl(env, data, lsn, nblocks);
}
/*!
Reads a single mode2 sector from cd device into data starting
from lsn. Returns 0 if no error.
*/
static int
_read_mode2_sector_freebsd (void *env, void *data, lsn_t lsn,
bool b_form2)
{
return read_mode2_sector_freebsd_ioctl(env, data, lsn, b_form2);
}
/*!
Reads nblocks of mode2 sectors from cd device into data starting
from lsn.
Returns 0 if no error.
*/
static int
_read_mode2_sectors_freebsd (void *env, void *data, lsn_t lsn,
bool b_form2, unsigned int nblocks)
{
_img_private_t *_obj = env;
int i;
int retval;
for (i = 0; i < nblocks; i++) {
if (b_form2) {
if ( (retval = _read_mode2_sector_freebsd (_obj,
((char *)data) + (M2RAW_SECTOR_SIZE * i),
lsn + i, true)) )
return retval;
} else {
char buf[M2RAW_SECTOR_SIZE] = { 0, };
if ( (retval = _read_mode2_sector_freebsd (_obj, buf, lsn + i, true)) )
return retval;
memcpy (((char *)data) + (CDIO_CD_FRAMESIZE * i),
buf + CDIO_CD_SUBHEADER_SIZE, CDIO_CD_FRAMESIZE);
}
}
return 0;
}
/*!
Return the size of the CD in logical block address (LBA) units.
*/
static uint32_t
_stat_size_freebsd (void *env)
{
return stat_size_freebsd_ioctl(env);
}
/*!
Set the key "arg" to "value" in source device.
*/
static int
_set_arg_freebsd (void *env, const char key[], const char value[])
{
_img_private_t *_obj = env;
if (!strcmp (key, "source"))
{
if (!value)
return -2;
free (_obj->gen.source_name);
_obj->gen.source_name = strdup (value);
}
else if (!strcmp (key, "access-mode"))
{
return str_to_access_mode_freebsd(value);
}
else
return -1;
return 0;
}
/*!
Read and cache the CD's Track Table of Contents and track info.
Return false if successful or true if an error.
*/
static bool
_cdio_read_toc (_img_private_t *_obj)
{
int i;
struct ioc_read_toc_entry te;
/* read TOC header */
if ( ioctl(_obj->gen.fd, CDIOREADTOCHEADER, &_obj->tochdr) == -1 ) {
cdio_error("error in ioctl(CDIOREADTOCHEADER): %s\n", strerror(errno));
return false;
}
te.address_format = CD_LBA_FORMAT;
te.starting_track = 0;
te.data_len = (TOTAL_TRACKS+1) * sizeof(struct cd_toc_entry);
te.data = _obj->tocent;
if ( ioctl(_obj->gen.fd, CDIOREADTOCENTRYS, &te) == -1 ) {
cdio_error("%s %d: %s\n",
"error in ioctl CDROMREADTOCENTRYS for track",
i, strerror(errno));
return false;
}
return true;
}
/*!
Eject media. Return 1 if successful, 0 otherwise.
*/
static int
_eject_media_freebsd (void *env)
{
_img_private_t *_obj = env;
return (_obj->access_mode == _AM_IOCTL)
? eject_media_freebsd_ioctl(_obj)
: eject_media_freebsd_cam(_obj);
}
/*!
Return the value associated with the key "arg".
*/
static const char *
_get_arg_freebsd (void *env, const char key[])
{
_img_private_t *_obj = env;
if (!strcmp (key, "source")) {
return _obj->gen.source_name;
} else if (!strcmp (key, "access-mode")) {
switch (_obj->access_mode) {
case _AM_IOCTL:
return "ioctl";
case _AM_CAM:
return "CAM";
case _AM_NONE:
return "no access method";
}
}
return NULL;
}
/*!
Return the number of of the first track.
CDIO_INVALID_TRACK is returned on error.
*/
static track_t
_get_first_track_num_freebsd(void *env)
{
_img_private_t *_obj = env;
if (!_obj->toc_init) _cdio_read_toc (_obj) ;
return FIRST_TRACK_NUM;
}
/*!
Return the media catalog number MCN.
Note: string is malloc'd so caller should free() then returned
string when done with it.
FIXME: This is just a guess.
*/
static char *
_get_mcn_freebsd (const void *env) {
const _img_private_t *_obj = env;
struct ioc_read_subchannel subchannel;
struct cd_sub_channel_info subchannel_info;
subchannel.address_format = CD_LBA_FORMAT;
subchannel.data_format = CD_MEDIA_CATALOG;
subchannel.track = 0;
subchannel.data_len = 1;
subchannel.data = &subchannel_info;
if(ioctl(_obj->gen.fd, CDIOCREADSUBCHANNEL, &subchannel) < 0) {
perror("CDIOCREADSUBCHANNEL");
return NULL;
}
/* Probably need a loop over tracks rather than give up if we
can't find in track 0.
*/
if (subchannel_info.what.media_catalog.mc_valid)
return strdup(subchannel_info.what.media_catalog.mc_number);
else
return NULL;
}
/*!
Return the number of tracks in the current medium.
CDIO_INVALID_TRACK is returned on error.
*/
static track_t
_get_num_tracks_freebsd(void *env)
{
_img_private_t *_obj = env;
if (!_obj->toc_init) _cdio_read_toc (_obj) ;
return TOTAL_TRACKS;
}
/*!
Get format of track.
FIXME: We're just guessing this from the GNU/Linux code.
*/
static track_format_t
_get_track_format_freebsd(void *env, track_t track_num)
{
_img_private_t *_obj = env;
struct ioc_read_subchannel subchannel;
struct cd_sub_channel_info subchannel_info;
subchannel.address_format = CD_LBA_FORMAT;
subchannel.data_format = CD_CURRENT_POSITION;
subchannel.track = track_num;
subchannel.data_len = 1;
subchannel.data = &subchannel_info;
if(ioctl(_obj->gen.fd, CDIOCREADSUBCHANNEL, &subchannel) < 0) {
perror("CDIOCREADSUBCHANNEL");
return 1;
}
if (subchannel_info.what.position.control == 0x04) {
if (subchannel_info.what.position.data_format == 0x10)
return TRACK_FORMAT_CDI;
else if (subchannel_info.what.position.data_format == 0x20)
return TRACK_FORMAT_XA;
else
return TRACK_FORMAT_DATA;
} else
return TRACK_FORMAT_AUDIO;
}
/*!
Return true if we have XA data (green, mode2 form1) or
XA data (green, mode2 form2). That is track begins:
sync - header - subheader
12 4 - 8
FIXME: there's gotta be a better design for this and get_track_format?
*/
static bool
_get_track_green_freebsd(void *env, track_t track_num)
{
_img_private_t *_obj = env;
struct ioc_read_subchannel subchannel;
struct cd_sub_channel_info subchannel_info;
subchannel.address_format = CD_LBA_FORMAT;
subchannel.data_format = CD_CURRENT_POSITION;
subchannel.track = track_num;
subchannel.data_len = 1;
subchannel.data = &subchannel_info;
if(ioctl(_obj->gen.fd, CDIOCREADSUBCHANNEL, &subchannel) < 0) {
perror("CDIOCREADSUBCHANNEL");
return 1;
}
/* FIXME: Dunno if this is the right way, but it's what
I was using in cdinfo for a while.
*/
return (subchannel_info.what.position.control & 2) != 0;
}
/*!
Return the starting LSN track number
track_num in obj. Track numbers start at 1.
The "leadout" track is specified either by
using track_num LEADOUT_TRACK or the total tracks+1.
False is returned if there is no track entry.
*/
static lba_t
_get_track_lba_freebsd(void *env, track_t track_num)
{
_img_private_t *_obj = env;
if (!_obj->toc_init) _cdio_read_toc (_obj) ;
if (track_num == CDIO_CDROM_LEADOUT_TRACK) track_num = TOTAL_TRACKS+1;
if (track_num > TOTAL_TRACKS+1 || track_num == 0) {
return CDIO_INVALID_LBA;
} else {
return _obj->tocent[track_num-1].addr.lba;
}
}
#endif /* HAVE_FREEBSD_CDROM */
/*!
Return an array of strings giving possible CD devices.
*/
char **
cdio_get_devices_freebsd (void)
{
#ifndef HAVE_FREEBSD_CDROM
return NULL;
#else
char drive[40];
char **drives = NULL;
unsigned int num_drives=0;
bool exists=true;
char c;
/* Scan the system for CD-ROM drives.
*/
#ifdef USE_ETC_FSTAB
struct fstab *fs;
setfsent();
/* Check what's in /etc/fstab... */
while ( (fs = getfsent()) )
{
if (strncmp(fs->fs_spec, "/dev/sr", 7))
cdio_add_device_list(&drives, fs->fs_spec, &num_drives);
}
#endif
/* Scan the system for CD-ROM drives.
Not always 100% reliable, so use the USE_MNTENT code above first.
*/
for ( c='0'; exists && c <='9'; c++ ) {
sprintf(drive, "/dev/acd%cc", c);
exists = cdio_is_cdrom(drive, NULL);
if ( exists ) {
cdio_add_device_list(&drives, drive, &num_drives);
}
}
cdio_add_device_list(&drives, NULL, &num_drives);
return drives;
#endif /*HAVE_FREEBSD_CDROM*/
}
/*!
Return a string containing the default CD device if none is specified.
*/
char *
cdio_get_default_device_freebsd()
{
return strdup(DEFAULT_CDIO_DEVICE);
}
/*!
Initialization routine. This is the only thing that doesn't
get called via a function pointer. In fact *we* are the
ones to set that up.
*/
CdIo *
cdio_open_freebsd (const char *psz_source_name)
{
return cdio_open_am_freebsd(psz_source_name, NULL);
}
/*!
Initialization routine. This is the only thing that doesn't
get called via a function pointer. In fact *we* are the
ones to set that up.
*/
CdIo *
cdio_open_am_freebsd (const char *psz_source_name, const char *psz_access_mode)
{
#ifdef HAVE_FREEBSD_CDROM
CdIo *ret;
_img_private_t *_data;
cdio_funcs _funcs = {
.eject_media = _eject_media_freebsd,
.free = _free_freebsd,
.get_arg = _get_arg_freebsd,
.get_default_device = cdio_get_default_device_freebsd,
.get_devices = cdio_get_devices_freebsd,
.get_drive_cap = NULL,
.get_first_track_num= _get_first_track_num_freebsd,
.get_mcn = _get_mcn_freebsd,
.get_num_tracks = _get_num_tracks_freebsd,
.get_track_format = _get_track_format_freebsd,
.get_track_green = _get_track_green_freebsd,
.get_track_lba = _get_track_lba_freebsd,
.get_track_msf = NULL,
.lseek = cdio_generic_lseek,
.read = cdio_generic_read,
.read_audio_sectors = _read_audio_sectors_freebsd,
.read_mode2_sector = _read_mode2_sector_freebsd,
.read_mode2_sectors = _read_mode2_sectors_freebsd,
.set_arg = _set_arg_freebsd,
.stat_size = _stat_size_freebsd
};
_data = _cdio_malloc (sizeof (_img_private_t));
_data->access_mode = str_to_access_mode_freebsd(psz_access_mode);
_data->gen.init = false;
_data->gen.fd = -1;
_set_arg_freebsd(_data, "source", (NULL == psz_source_name)
? DEFAULT_CDIO_DEVICE: psz_source_name);
ret = cdio_new (_data, &_funcs);
if (ret == NULL) return NULL;
if ( _data->access_mode == _AM_IOCTL ) {
if (cdio_generic_init(_data))
return ret;
else {
cdio_generic_free (_data);
return NULL;
}
} else {
if (init_freebsd_cam(_data))
return ret;
else {
cdio_generic_free (_data);
return NULL;
}
}
#else
return NULL;
#endif /* HAVE_FREEBSD_CDROM */
}
bool
cdio_have_freebsd (void)
{
#ifdef HAVE_FREEBSD_CDROM
return true;
#else
return false;
#endif /* HAVE_FREEBSD_CDROM */
}

147
lib/FreeBSD/freebsd.h Normal file
View File

@@ -0,0 +1,147 @@
/*
$Id: freebsd.h,v 1.1 2004/04/30 09:59:54 rocky Exp $
Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
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.
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
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* This file contains FreeBSD-specific code and implements low-level
control of the CD drive. Culled initially I think from xine's or
mplayer's FreeBSD code with lots of modifications.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <cdio/sector.h>
#include "cdio_assert.h"
#include "cdio_private.h"
/* Is this the right default? */
#define DEFAULT_CDIO_DEVICE "/dev/acd0c"
#include <string.h>
#ifdef HAVE_FREEBSD_CDROM
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef HAVE_SYS_CDIO_H
# include <sys/cdio.h>
#endif
#include <sys/cdrio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#define HAVE_FREEBSD_CAM
#ifdef HAVE_FREEBSD_CAM
#include <camlib.h>
#include <cam/scsi/scsi_message.h>
#include <cam/scsi/scsi_pass.h>
#include <errno.h>
#define ERRCODE(s) ((((s)[2]&0x0F)<<16)|((s)[12]<<8)|((s)[13]))
#define EMEDIUMTYPE EINVAL
#define ENOMEDIUM ENODEV
#define CREAM_ON_ERRNO(s) do { \
switch ((s)[12]) \
{ case 0x04: errno=EAGAIN; break; \
case 0x20: errno=ENODEV; break; \
case 0x21: if ((s)[13]==0) errno=ENOSPC; \
else errno=EINVAL; \
break; \
case 0x30: errno=EMEDIUMTYPE; break; \
case 0x3A: errno=ENOMEDIUM; break; \
} \
} while(0)
#endif /*HAVE_FREEBSD_CAM*/
#include <cdio/util.h>
#define TOTAL_TRACKS ( _obj->tochdr.ending_track \
- _obj->tochdr.starting_track + 1)
#define FIRST_TRACK_NUM (_obj->tochdr.starting_track)
typedef enum {
_AM_NONE,
_AM_IOCTL,
_AM_CAM
} access_mode_t;
typedef struct {
/* Things common to all drivers like this.
This must be first. */
generic_img_private_t gen;
char *source_name;
#ifdef HAVE_FREEBSD_CAM
char *device;
struct cam_device *cam;
union ccb ccb;
#endif
access_mode_t access_mode;
bool b_ioctl_init;
bool b_cam_init;
/* Track information */
bool toc_init; /* if true, info below is valid. */
struct ioc_toc_header tochdr;
struct cd_toc_entry tocent[100]; /* entry info for each track */
} _img_private_t;
bool cdio_is_cdrom_freebsd_ioctl(char *drive, char *mnttype);
char *get_mcn_freebsd_ioctl (const void *env);
track_format_t get_track_format_freebsd_ioctl(void *env, track_t track_num);
bool get_track_green_freebsd_ioctl(void *env, track_t track_num);
int eject_media_freebsd_ioctl (_img_private_t *env);
int eject_media_freebsd_cam (_img_private_t *env);
void free_freebsd_cam (void *obj);
int read_audio_sectors_freebsd_ioctl (void *env, void *data, lsn_t lsn,
unsigned int nblocks);
int read_mode2_sector_freebsd_ioctl (void *env, void *data, lsn_t lsn,
bool b_form2);
int read_mode2_sectors_freebsd_cam (void *env, void *buf, uint32_t lba,
unsigned int nblocks, bool b_form2);
bool read_toc_freebsd_ioctl (_img_private_t *_obj);
/*!
Return the size of the CD in logical block address (LBA) units.
*/
uint32_t stat_size_freebsd_cam (void *env);
uint32_t stat_size_freebsd_ioctl (void *env);
bool init_freebsd_cam (_img_private_t *_obj);
void free_freebsd_cam (void *obj);
#endif /*HAVE_FREEBSD_CDROM*/

293
lib/FreeBSD/freebsd_cam.c Normal file
View File

@@ -0,0 +1,293 @@
/*
$Id: freebsd_cam.c,v 1.1 2004/04/30 09:59:54 rocky Exp $
Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
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.
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
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* This file contains FreeBSD-specific code and implements low-level
control of the CD drive via SCSI emulation.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
static const char _rcsid[] = "$Id: freebsd_cam.c,v 1.1 2004/04/30 09:59:54 rocky Exp $";
#ifdef HAVE_FREEBSD_CDROM
#include "freebsd.h"
#include "scsi_mmc.h"
static const u_char scsi_cdblen[8] = {6, 10, 10, 12, 12, 12, 10, 10};
static int
_scsi_cmd (_img_private_t * _obj)
{
int retval;
_obj->ccb.csio.cdb_len = scsi_cdblen[(_obj->ccb.csio.cdb_io.cdb_bytes[0] >> 5) & 7];
if ((retval = cam_send_ccb(_obj->cam, &_obj->ccb)) < 0)
{
cdio_error ("transport failed: ", retval);
return -1;
}
if ((_obj->ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
{
return 0;
}
errno = EIO;
retval = ERRCODE(((unsigned char *)&_obj->ccb.csio.sense_data));
if (retval == 0)
retval = -1;
else
CREAM_ON_ERRNO(((unsigned char *)&_obj->ccb.csio.sense_data));
cdio_error ("transport failed: ", retval);
return retval;
}
bool
init_freebsd_cam (_img_private_t *_obj)
{
char pass[100];
if (_obj->gen.init)
return true;
_obj->cam=NULL;
memset (&_obj->ccb, 0, sizeof(_obj->ccb));
_obj->ccb.ccb_h.func_code = XPT_GDEVLIST;
_obj->gen.fd = open (_obj->device, O_RDONLY, 0);
if (_obj->gen.fd < 0)
{
cdio_error ("open (%s): %s", _obj->device, strerror (errno));
return false;
}
if (ioctl (_obj->gen.fd, CAMGETPASSTHRU, &_obj->ccb) < 0)
{
cdio_error ("open: %s", strerror (errno));
return false;
}
sprintf (pass,"/dev/%.15s%u",
_obj->ccb.cgdl.periph_name,
_obj->ccb.cgdl.unit_number);
_obj->cam = cam_open_pass (pass,O_RDWR,NULL);
_obj->gen.init = true;
_obj->b_cam_init = true;
return true;
}
void
free_freebsd_cam (void *obj)
{
_img_private_t *env = obj;
if (NULL == env) return;
free (env->device);
if (env->gen.fd > 0)
close (env->gen.fd);
env->gen.fd = -1;
if(env->cam)
cam_close_device(env->cam);
free (env);
}
static int
_set_bsize (_img_private_t *_obj, unsigned int bsize)
{
struct
{
uint8_t reserved1;
uint8_t medium;
uint8_t reserved2;
uint8_t block_desc_length;
uint8_t density;
uint8_t number_of_blocks_hi;
uint8_t number_of_blocks_med;
uint8_t number_of_blocks_lo;
uint8_t reserved3;
uint8_t block_length_hi;
uint8_t block_length_med;
uint8_t block_length_lo;
} mh;
memset(&_obj->ccb, 0, sizeof(_obj->ccb));
memset (&mh, 0, sizeof (mh));
_obj->ccb.ccb_h.path_id = _obj->cam->path_id;
_obj->ccb.ccb_h.target_id = _obj->cam->target_id;
_obj->ccb.ccb_h.target_lun = _obj->cam->target_lun;
cam_fill_csio (&(_obj->ccb.csio), 1, NULL,
CAM_DEV_QFRZDIS, MSG_SIMPLE_Q_TAG, NULL, 0,
sizeof(_obj->ccb.csio.sense_data), 0, 30*1000);
_obj->ccb.csio.cdb_len = 4+1;
_obj->ccb.csio.cdb_io.cdb_bytes[0] = 0x15;
_obj->ccb.csio.cdb_io.cdb_bytes[1] = 1 << 4;
_obj->ccb.csio.cdb_io.cdb_bytes[4] = 12;
_obj->ccb.csio.data_ptr = (u_char *)&mh;
_obj->ccb.csio.dxfer_len = sizeof (mh);;
/* suc.suc_timeout = 500; */
_obj->ccb.csio.ccb_h.flags = CAM_DIR_OUT;
mh.block_desc_length = 0x08;
mh.block_length_hi = (bsize >> 16) & 0xff;
mh.block_length_med = (bsize >> 8) & 0xff;
mh.block_length_lo = (bsize >> 0) & 0xff;
return _scsi_cmd (_obj);
}
int
read_mode2_sectors_freebsd_cam (void *env, void *buf, uint32_t lba,
unsigned int nblocks, bool b_form2)
{
_img_private_t *_obj = env;
int retval = 0;
memset(&_obj->ccb,0,sizeof(_obj->ccb));
_obj->ccb.ccb_h.path_id = _obj->cam->path_id;
_obj->ccb.ccb_h.target_id = _obj->cam->target_id;
_obj->ccb.ccb_h.target_lun = _obj->cam->target_lun;
cam_fill_csio (&(_obj->ccb.csio), 1, NULL,
CAM_DEV_QFRZDIS, MSG_SIMPLE_Q_TAG, NULL, 0,
sizeof(_obj->ccb.csio.sense_data), 0, 30*1000);
_obj->ccb.csio.cdb_len = (b_form2?8:9)+1;
_obj->ccb.csio.cdb_io.cdb_bytes[0] = (b_form2
? CDIO_MMC_GPCMD_READ_10
: CDIO_MMC_GPCMD_READ_CD );
if (!b_form2)
_obj->ccb.csio.cdb_io.cdb_bytes[1] = 0; /* sector size mode2 */
CDIO_MMC_SET_READ_LBA(_obj->ccb.csio.cdb_io.cdb_bytes, lba);
CDIO_MMC_SET_READ_LENGTH(_obj->ccb.csio.cdb_io.cdb_bytes, nblocks);
if (!b_form2)
_obj->ccb.csio.cdb_io.cdb_bytes[9] = 0x58; /* 2336 mode2 mixed form */
_obj->ccb.csio.data_ptr = buf;
_obj->ccb.csio.dxfer_len = M2RAW_SECTOR_SIZE * nblocks;
/* suc.suc_timeout = 500; */
_obj->ccb.csio.ccb_h.flags = CAM_DIR_IN;
if (b_form2)
{
if ((retval = _set_bsize (_obj, M2RAW_SECTOR_SIZE)))
goto out;
if ((retval = _scsi_cmd(_obj)))
{
_set_bsize (_obj, CDIO_CD_FRAMESIZE);
goto out;
}
retval = _set_bsize (_obj, CDIO_CD_FRAMESIZE);
}
else
retval = _scsi_cmd(_obj);
out:
return retval;
}
/*!
Return the size of the CD in logical block address (LBA) units.
*/
uint32_t
stat_size_freebsd_cam (void *env)
{
_img_private_t *_obj = env;
uint8_t buf[12] = { 0, };
uint32_t retval;
memset(&_obj->ccb,0,sizeof(_obj->ccb));
_obj->ccb.ccb_h.path_id = _obj->cam->path_id;
_obj->ccb.ccb_h.target_id = _obj->cam->target_id;
_obj->ccb.ccb_h.target_lun = _obj->cam->target_lun;
cam_fill_csio (&(_obj->ccb.csio), 1, NULL, CAM_DEV_QFRZDIS,
MSG_SIMPLE_Q_TAG, NULL, 0,
sizeof(_obj->ccb.csio.sense_data), 0, 30*1000);
_obj->ccb.csio.cdb_len = 8+1;
_obj->ccb.csio.cdb_io.cdb_bytes[0] = CDIO_MMC_READ_TOC;
_obj->ccb.csio.cdb_io.cdb_bytes[1] = 0; /* lba; msf: 0x2 */
_obj->ccb.csio.cdb_io.cdb_bytes[6] = CDIO_CDROM_LEADOUT_TRACK;
_obj->ccb.csio.cdb_io.cdb_bytes[8] = 12; /* ? */
_obj->ccb.csio.data_ptr = buf;
_obj->ccb.csio.dxfer_len = sizeof (buf);
/*suc.suc_timeout = 500; */
_obj->ccb.csio.ccb_h.flags = CAM_DIR_IN;
if (_scsi_cmd(_obj))
return 0;
{
int i;
retval = 0;
for (i = 8; i < 12; i++)
{
retval <<= 8;
retval += buf[i];
}
}
return retval;
}
int
eject_media_freebsd_cam (_img_private_t *_obj)
{
if (_obj->gen.fd == -1)
return 2;
memset(&_obj->ccb,0,sizeof(_obj->ccb));
_obj->ccb.ccb_h.path_id = _obj->cam->path_id;
_obj->ccb.ccb_h.target_id = _obj->cam->target_id;
_obj->ccb.ccb_h.target_lun = _obj->cam->target_lun;
cam_fill_csio (&(_obj->ccb.csio), 1, NULL, CAM_DEV_QFRZDIS,
MSG_SIMPLE_Q_TAG, NULL, 0, sizeof(_obj->ccb.csio.sense_data),
0, 30*1000);
_obj->ccb.csio.cdb_len = 5+1;
_obj->ccb.csio.cdb_io.cdb_bytes[0] = CDIO_MMC_START_STOP;
_obj->ccb.csio.cdb_io.cdb_bytes[1] = 0x1; /* immediate */
_obj->ccb.csio.cdb_io.cdb_bytes[4] = 0x2; /* eject */
_obj->ccb.csio.data_ptr = 0;
_obj->ccb.csio.dxfer_len = 0;
/* suc.suc_timeout = 500; */
_obj->ccb.csio.ccb_h.flags = CAM_DIR_IN;
return(_scsi_cmd(_obj));
}
#endif /* HAVE_FREEBSD_CDROM */

306
lib/FreeBSD/freebsd_ioctl.c Normal file
View File

@@ -0,0 +1,306 @@
/*
$Id: freebsd_ioctl.c,v 1.1 2004/04/30 09:59:54 rocky Exp $
Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
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.
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
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* This file contains FreeBSD-specific code and implements low-level
control of the CD drive. Culled initially I think from xine's or
mplayer's FreeBSD code with lots of modifications.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
static const char _rcsid[] = "$Id: freebsd_ioctl.c,v 1.1 2004/04/30 09:59:54 rocky Exp $";
#ifdef HAVE_FREEBSD_CDROM
#include "freebsd.h"
/* Check a drive to see if it is a CD-ROM
Return 1 if a CD-ROM. 0 if it exists but isn't a CD-ROM drive
and -1 if no device exists .
*/
bool
cdio_is_cdrom_freebsd_ioctl(char *drive, char *mnttype)
{
bool is_cd=false;
int cdfd;
struct ioc_toc_header tochdr;
/* If it doesn't exist, return -1 */
if ( !cdio_is_device_quiet_generic(drive) ) {
return(false);
}
/* If it does exist, verify that it's an available CD-ROM */
cdfd = open(drive, (O_RDONLY|O_EXCL|O_NONBLOCK), 0);
/* Should we want to test the condition in more detail:
ENOENT is the error for /dev/xxxxx does not exist;
ENODEV means there's no drive present. */
if ( cdfd >= 0 ) {
if ( ioctl(cdfd, CDIOREADTOCHEADER, &tochdr) != -1 ) {
is_cd = true;
}
close(cdfd);
}
/* Even if we can't read it, it might be mounted */
else if ( mnttype && (strcmp(mnttype, "iso9660") == 0) ) {
is_cd = true;
}
return(is_cd);
}
/*!
Reads a single mode2 sector from cd device into data starting from lsn.
Returns 0 if no error.
*/
int
read_audio_sectors_freebsd_ioctl (void *env, void *data, lsn_t lsn,
unsigned int nblocks)
{
_img_private_t *_obj = env;
unsigned char buf[CDIO_CD_FRAMESIZE_RAW] = { 0, };
struct ioc_read_audio cdda;
cdda.address.lba = lsn;
cdda.buffer = buf;
cdda.nframes = nblocks;
cdda.address_format = CD_LBA_FORMAT;
/* read a frame */
if(ioctl(_obj->gen.fd, CDIOCREADAUDIO, &cdda) < 0) {
perror("CDIOCREADAUDIO");
return 1;
}
memcpy (data, buf, CDIO_CD_FRAMESIZE_RAW);
return 0;
}
/*!
Reads a single mode2 sector from cd device into data starting
from lsn. Returns 0 if no error.
*/
int
read_mode2_sector_freebsd_ioctl (void *env, void *data, lsn_t lsn,
bool b_form2)
{
char buf[M2RAW_SECTOR_SIZE] = { 0, };
int retval;
if ( (retval = read_audio_sectors_freebsd_ioctl (env, buf, lsn, 1)) )
return retval;
if (b_form2)
memcpy (data, buf + CDIO_CD_XA_SYNC_HEADER, M2RAW_SECTOR_SIZE);
else
memcpy (data, buf + CDIO_CD_XA_SYNC_HEADER, CDIO_CD_FRAMESIZE);
return 0;
}
/*!
Return the size of the CD in logical block address (LBA) units.
*/
uint32_t
stat_size_freebsd_ioctl (void *env)
{
_img_private_t *_obj = env;
struct ioc_read_toc_single_entry tocent;
uint32_t size;
tocent.track = CDIO_CDROM_LEADOUT_TRACK;
tocent.address_format = CD_LBA_FORMAT;
if (ioctl (_obj->gen.fd, CDIOREADTOCENTRY, &tocent) == -1)
{
perror ("ioctl(CDROMREADTOCENTRY)");
exit (EXIT_FAILURE);
}
size = tocent.entry.addr.lba;
return size;
}
/*!
Read and cache the CD's Track Table of Contents and track info.
Return false if successful or true if an error.
*/
bool
read_toc_freebsd_ioctl (_img_private_t *_obj)
{
int i;
struct ioc_read_toc_entry te;
/* read TOC header */
if ( ioctl(_obj->gen.fd, CDIOREADTOCHEADER, &_obj->tochdr) == -1 ) {
cdio_error("error in ioctl(CDIOREADTOCHEADER): %s\n", strerror(errno));
return false;
}
te.address_format = CD_LBA_FORMAT;
te.starting_track = 0;
te.data_len = (TOTAL_TRACKS+1) * sizeof(struct cd_toc_entry);
te.data = _obj->tocent;
if ( ioctl(_obj->gen.fd, CDIOREADTOCENTRYS, &te) == -1 ) {
cdio_error("%s %d: %s\n",
"error in ioctl CDROMREADTOCENTRYS for track",
i, strerror(errno));
return false;
}
return true;
}
/*!
Eject media. Return 1 if successful, 0 otherwise.
*/
int
eject_media_freebsd_ioctl (_img_private_t *env)
{
_img_private_t *_obj = env;
int ret=2;
int fd;
if ((fd = open(_obj->gen.source_name, O_RDONLY|O_NONBLOCK)) > -1) {
ret = 1;
if (ioctl(fd, CDIOCALLOW) == -1) {
cdio_error("ioctl(fd, CDIOCALLOW) failed: %s\n", strerror(errno));
} else if (ioctl(fd, CDIOCEJECT) == -1) {
cdio_error("ioctl(CDIOCEJECT) failed: %s\n", strerror(errno));
} else {
ret = 0;
}
close(fd);
}
return ret;
}
/*!
Return the media catalog number MCN.
Note: string is malloc'd so caller should free() then returned
string when done with it.
FIXME: This is just a guess.
*/
char *
get_mcn_freebsd_ioctl (const void *env) {
const _img_private_t *_obj = env;
struct ioc_read_subchannel subchannel;
struct cd_sub_channel_info subchannel_info;
subchannel.address_format = CD_LBA_FORMAT;
subchannel.data_format = CD_MEDIA_CATALOG;
subchannel.track = 0;
subchannel.data_len = 1;
subchannel.data = &subchannel_info;
if(ioctl(_obj->gen.fd, CDIOCREADSUBCHANNEL, &subchannel) < 0) {
perror("CDIOCREADSUBCHANNEL");
return NULL;
}
/* Probably need a loop over tracks rather than give up if we
can't find in track 0.
*/
if (subchannel_info.what.media_catalog.mc_valid)
return strdup(subchannel_info.what.media_catalog.mc_number);
else
return NULL;
}
/*!
Get format of track.
FIXME: We're just guessing this from the GNU/Linux code.
*/
track_format_t
get_track_format_freebsd_ioctl(void *env, track_t track_num)
{
_img_private_t *_obj = env;
struct ioc_read_subchannel subchannel;
struct cd_sub_channel_info subchannel_info;
subchannel.address_format = CD_LBA_FORMAT;
subchannel.data_format = CD_CURRENT_POSITION;
subchannel.track = track_num;
subchannel.data_len = 1;
subchannel.data = &subchannel_info;
if(ioctl(_obj->gen.fd, CDIOCREADSUBCHANNEL, &subchannel) < 0) {
perror("CDIOCREADSUBCHANNEL");
return 1;
}
if (subchannel_info.what.position.control == 0x04) {
if (subchannel_info.what.position.data_format == 0x10)
return TRACK_FORMAT_CDI;
else if (subchannel_info.what.position.data_format == 0x20)
return TRACK_FORMAT_XA;
else
return TRACK_FORMAT_DATA;
} else
return TRACK_FORMAT_AUDIO;
}
/*!
Return true if we have XA data (green, mode2 form1) or
XA data (green, mode2 form2). That is track begins:
sync - header - subheader
12 4 - 8
FIXME: there's gotta be a better design for this and get_track_format?
*/
bool
get_track_green_freebsd_ioctl(void *env, track_t track_num)
{
_img_private_t *_obj = env;
struct ioc_read_subchannel subchannel;
struct cd_sub_channel_info subchannel_info;
subchannel.address_format = CD_LBA_FORMAT;
subchannel.data_format = CD_CURRENT_POSITION;
subchannel.track = track_num;
subchannel.data_len = 1;
subchannel.data = &subchannel_info;
if(ioctl(_obj->gen.fd, CDIOCREADSUBCHANNEL, &subchannel) < 0) {
perror("CDIOCREADSUBCHANNEL");
return 1;
}
/* FIXME: Dunno if this is the right way, but it's what
I was using in cdinfo for a while.
*/
return (subchannel_info.what.position.control & 2) != 0;
}
#endif /* HAVE_FREEBSD_CDROM */

View File

@@ -1,4 +1,4 @@
# $Id: Makefile.am,v 1.32 2004/04/23 02:18:07 rocky Exp $
# $Id: Makefile.am,v 1.33 2004/04/30 09:59:54 rocky Exp $
#
# Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
#
@@ -53,7 +53,6 @@ noinst_HEADERS = cdio_assert.h bytesex.h bytesex_asm.h \
libcdio_sources = \
_cdio_bsdi.c \
_cdio_freebsd.c \
_cdio_generic.c \
_cdio_linux.c \
_cdio_osx.c \
@@ -68,13 +67,17 @@ libcdio_sources = \
cd_types.c \
ds.c \
ds.h \
FreeBSD/freebsd.c \
FreeBSD/freebsd.h \
FreeBSD/freebsd_cam.c \
FreeBSD/freebsd_ioctl.c \
image/bincue.c \
image_common.h \
image/nrg.c \
logging.c \
MSWindows/aspi32.c \
MSWindows/aspi32.h \
MSWindows/ioctl.c \
MSWindows/win32_ioctl.c \
MSWindows/win32.c \
MSWindows/win32.h \
scsi_mmc.h \