#include "opt_cd.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/buf.h>
#include <sys/conf.h>
#include <sys/disk.h>
#include <sys/caps.h>
#include <sys/dtype.h>
#include <sys/malloc.h>
#include <sys/cdio.h>
#include <sys/cdrio.h>
#include <sys/dvdio.h>
#include <sys/devicestat.h>
#include <sys/sysctl.h>
#include <sys/taskqueue.h>
#include <sys/proc.h>
#include <sys/camlib.h>
#include <sys/udev.h>
#include <sys/buf2.h>
#include "../cam.h"
#include "../cam_ccb.h"
#include "../cam_extend.h"
#include "../cam_periph.h"
#include "../cam_xpt_periph.h"
#include "../cam_queue.h"
#include "../cam_sim.h"
#include "scsi_message.h"
#include "scsi_da.h"
#include "scsi_cd.h"
#define LEADOUT 0xaa
struct cd_params {
u_int32_t blksize;
u_long disksize;
};
typedef enum {
CD_Q_NONE = 0x00,
CD_Q_NO_TOUCH = 0x01,
CD_Q_BCD_TRACKS = 0x02,
CD_Q_NO_CHANGER = 0x04,
CD_Q_CHANGER = 0x08,
CD_Q_10_BYTE_ONLY = 0x10
} cd_quirks;
typedef enum {
CD_FLAG_INVALID = 0x0001,
CD_FLAG_NEW_DISC = 0x0002,
CD_FLAG_DISC_LOCKED = 0x0004,
CD_FLAG_DISC_REMOVABLE = 0x0008,
CD_FLAG_TAGGED_QUEUING = 0x0010,
CD_FLAG_CHANGER = 0x0040,
CD_FLAG_ACTIVE = 0x0080,
CD_FLAG_SCHED_ON_COMP = 0x0100,
CD_FLAG_RETRY_UA = 0x0200,
CD_FLAG_VALID_MEDIA = 0x0400,
CD_FLAG_VALID_TOC = 0x0800,
CD_FLAG_SCTX_INIT = 0x1000,
CD_FLAG_OPEN = 0x2000,
CD_FLAG_CAP_MUTE = 0x4000
} cd_flags;
typedef enum {
CD_CCB_POLLED = 0x00,
CD_CCB_PROBE = 0x01,
CD_CCB_BUFFER_IO = 0x02,
CD_CCB_WAITING = 0x03,
CD_CCB_TYPE_MASK = 0x0F,
CD_CCB_RETRY_UA = 0x10
} cd_ccb_state;
typedef enum {
CHANGER_TIMEOUT_SCHED = 0x01,
CHANGER_SHORT_TMOUT_SCHED = 0x02,
CHANGER_MANUAL_CALL = 0x04,
CHANGER_NEED_TIMEOUT = 0x08
} cd_changer_flags;
#define ccb_state ppriv_field0
#define ccb_bio ppriv_ptr1
struct cd_tocdata {
struct ioc_toc_header header;
struct cd_toc_entry entries[100];
};
struct cd_toc_single {
struct ioc_toc_header header;
struct cd_toc_entry entry;
};
typedef enum {
CD_STATE_PROBE,
CD_STATE_NORMAL
} cd_state;
struct cd_softc {
cam_pinfo pinfo;
cd_state state;
volatile cd_flags flags;
struct bio_queue_head bio_queue;
LIST_HEAD(, ccb_hdr) pending_ccbs;
struct cd_params params;
struct disk disk;
union ccb saved_ccb;
cd_quirks quirks;
struct devstat device_stats;
STAILQ_ENTRY(cd_softc) changer_links;
struct cdchanger *changer;
int bufs_left;
struct cam_periph *periph;
int minimum_command_size;
int outstanding_cmds;
struct task sysctl_task;
struct sysctl_ctx_list sysctl_ctx;
struct sysctl_oid *sysctl_tree;
STAILQ_HEAD(, cd_mode_params) mode_queue;
struct cd_tocdata toc;
};
struct cd_page_sizes {
int page;
int page_size;
};
static struct cd_page_sizes cd_page_size_table[] =
{
{ AUDIO_PAGE, sizeof(struct cd_audio_page)}
};
struct cd_quirk_entry {
struct scsi_inquiry_pattern inq_pat;
cd_quirks quirks;
};
static struct cd_quirk_entry cd_quirk_table[] =
{
{
{ T_CDROM, SIP_MEDIA_REMOVABLE, "NRC", "MBR-7", "*"},
CD_Q_CHANGER
},
{
{ T_CDROM, SIP_MEDIA_REMOVABLE, "PIONEER", "CD-ROM DRM*",
"*"}, CD_Q_CHANGER
},
{
{ T_CDROM, SIP_MEDIA_REMOVABLE, "NAKAMICH", "MJ-*", "*"},
CD_Q_CHANGER
},
{
{ T_CDROM, SIP_MEDIA_REMOVABLE, "CHINON", "CD-ROM CDS-535","*"},
CD_Q_BCD_TRACKS
}
};
static d_open_t cdopen;
static d_close_t cdclose;
static d_ioctl_t cdioctl;
static d_strategy_t cdstrategy;
static periph_init_t cdinit;
static periph_ctor_t cdregister;
static periph_dtor_t cdcleanup;
static periph_start_t cdstart;
static periph_oninv_t cdoninvalidate;
static void cdasync(void *callback_arg, u_int32_t code,
struct cam_path *path, void *arg);
static int cdcmdsizesysctl(SYSCTL_HANDLER_ARGS);
static void cdshorttimeout(void *arg);
static void cdschedule(struct cam_periph *periph, int priority);
static void cdrunchangerqueue(void *arg);
static void cdchangerschedule(struct cd_softc *softc);
static int cdrunccb(union ccb *ccb,
int (*error_routine)(union ccb *ccb,
u_int32_t cam_flags,
u_int32_t sense_flags),
u_int32_t cam_flags, u_int32_t sense_flags);
static union ccb *cdgetccb(struct cam_periph *periph,
u_int32_t priority);
static void cddone(struct cam_periph *periph,
union ccb *start_ccb);
static int cderror(union ccb *ccb, u_int32_t cam_flags,
u_int32_t sense_flags);
static union cd_pages *cdgetpage(struct cd_mode_params *mode_params);
static int cdgetpagesize(int page_num);
static void cdprevent(struct cam_periph *periph, int action);
static int cdcheckmedia(struct cam_periph *periph);
static int cdsize(struct cam_periph *periph, u_int32_t *size);
static int cd6byteworkaround(union ccb *ccb);
static int cdreadtoc(struct cam_periph *periph, u_int32_t mode,
u_int32_t start, u_int8_t *data,
u_int32_t len, u_int32_t sense_flags);
static int cdgetmode(struct cam_periph *periph,
struct cd_mode_params *data, u_int32_t page);
static int cdsetmode(struct cam_periph *periph,
struct cd_mode_params *data);
static int cdplay(struct cam_periph *periph, u_int32_t blk,
u_int32_t len);
static int cdreadsubchannel(struct cam_periph *periph,
u_int32_t mode, u_int32_t format,
int track,
struct cd_sub_channel_info *data,
u_int32_t len);
static int cdplaymsf(struct cam_periph *periph, u_int32_t startm,
u_int32_t starts, u_int32_t startf,
u_int32_t endm, u_int32_t ends,
u_int32_t endf);
static int cdplaytracks(struct cam_periph *periph,
u_int32_t strack, u_int32_t sindex,
u_int32_t etrack, u_int32_t eindex);
static int cdpause(struct cam_periph *periph, u_int32_t go);
static int cdstopunit(struct cam_periph *periph, u_int32_t eject);
static int cdstartunit(struct cam_periph *periph, int load);
static int cdsetspeed(struct cam_periph *periph,
u_int32_t rdspeed, u_int32_t wrspeed);
static int cdreportkey(struct cam_periph *periph,
struct dvd_authinfo *authinfo);
static int cdsendkey(struct cam_periph *periph,
struct dvd_authinfo *authinfo);
static int cdreaddvdstructure(struct cam_periph *periph,
struct dvd_struct *dvdstruct);
static struct periph_driver cddriver =
{
cdinit, "cd",
TAILQ_HEAD_INITIALIZER(cddriver.units), 0
};
PERIPHDRIVER_DECLARE(cd, cddriver);
static struct dev_ops cd_ops = {
{ "cd", 0, D_DISK | D_MPSAFE },
.d_open = cdopen,
.d_close = cdclose,
.d_read = physread,
.d_write = physwrite,
.d_ioctl = cdioctl,
.d_strategy = cdstrategy
};
static struct extend_array *cdperiphs;
#ifndef CHANGER_MIN_BUSY_SECONDS
#define CHANGER_MIN_BUSY_SECONDS 5
#endif
#ifndef CHANGER_MAX_BUSY_SECONDS
#define CHANGER_MAX_BUSY_SECONDS 15
#endif
static int changer_min_busy_seconds = CHANGER_MIN_BUSY_SECONDS;
static int changer_max_busy_seconds = CHANGER_MAX_BUSY_SECONDS;
SYSCTL_NODE(_kern_cam, OID_AUTO, cd, CTLFLAG_RD, 0, "CAM CDROM driver");
SYSCTL_NODE(_kern_cam_cd, OID_AUTO, changer, CTLFLAG_RD, 0, "CD Changer");
SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, min_busy_seconds, CTLFLAG_RW,
&changer_min_busy_seconds, 0, "Minimum changer scheduling quantum");
TUNABLE_INT("kern.cam.cd.changer.min_busy_seconds", &changer_min_busy_seconds);
SYSCTL_INT(_kern_cam_cd_changer, OID_AUTO, max_busy_seconds, CTLFLAG_RW,
&changer_max_busy_seconds, 0, "Maximum changer scheduling quantum");
TUNABLE_INT("kern.cam.cd.changer.max_busy_seconds", &changer_max_busy_seconds);
struct cdchanger {
path_id_t path_id;
target_id_t target_id;
int num_devices;
struct camq devq;
struct timeval start_time;
struct cd_softc *cur_device;
struct callout short_handle;
struct callout long_handle;
volatile cd_changer_flags flags;
STAILQ_ENTRY(cdchanger) changer_links;
STAILQ_HEAD(chdevlist, cd_softc) chluns;
};
static struct lock changerq_lock;
static STAILQ_HEAD(changerlist, cdchanger) changerq;
static int num_changers;
MALLOC_DEFINE(M_SCSICD, "scsi_cd", "scsi_cd buffers");
static void
cdinit(void)
{
cam_status status;
lockinit(&changerq_lock, "cdchangerq", 0, LK_CANRECURSE);
STAILQ_INIT(&changerq);
cdperiphs = cam_extend_new();
if (cdperiphs == NULL) {
kprintf("cd: Failed to alloc extend array!\n");
return;
}
status = xpt_register_async(AC_FOUND_DEVICE, cdasync, NULL, NULL);
if (status != CAM_REQ_CMP) {
kprintf("cd: Failed to attach master async callback "
"due to status 0x%x!\n", status);
}
}
static void
cdoninvalidate(struct cam_periph *periph)
{
struct cd_softc *softc;
struct buf *q_bp;
struct bio *q_bio;
softc = (struct cd_softc *)periph->softc;
xpt_register_async(0, cdasync, periph, periph->path);
softc->flags |= CD_FLAG_INVALID;
while ((q_bio = bioq_takefirst(&softc->bio_queue)) != NULL) {
q_bp = q_bio->bio_buf;
q_bp->b_resid = q_bp->b_bcount;
q_bp->b_error = ENXIO;
q_bp->b_flags |= B_ERROR;
biodone(q_bio);
}
if ((softc->flags & CD_FLAG_CHANGER)
&& (softc->pinfo.index != CAM_UNQUEUED_INDEX))
camq_remove(&softc->changer->devq, softc->pinfo.index);
xpt_print(periph->path, "lost device\n");
}
static void
cdcleanup(struct cam_periph *periph)
{
struct cd_softc *softc;
softc = (struct cd_softc *)periph->softc;
xpt_print(periph->path, "removing device entry\n");
if ((softc->flags & CD_FLAG_SCTX_INIT) != 0
&& sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
xpt_print(periph->path, "can't remove sysctl context\n");
}
if ((softc->flags & CD_FLAG_CHANGER)
&& (softc->flags & CD_FLAG_ACTIVE)) {
if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
callout_stop(&softc->changer->short_handle);
softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
}
softc->changer->devq.qfrozen_cnt--;
softc->changer->flags |= CHANGER_MANUAL_CALL;
cdrunchangerqueue(softc->changer);
}
if ((softc->flags & CD_FLAG_CHANGER)
&& (--softc->changer->num_devices == 0)) {
if (softc->changer->flags & CHANGER_TIMEOUT_SCHED) {
callout_stop(&softc->changer->long_handle);
softc->changer->flags &= ~CHANGER_TIMEOUT_SCHED;
}
if (softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
callout_stop(&softc->changer->short_handle);
softc->changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
}
lockmgr(&changerq_lock, LK_EXCLUSIVE);
STAILQ_REMOVE(&changerq, softc->changer, cdchanger,
changer_links);
num_changers--;
lockmgr(&changerq_lock, LK_RELEASE);
xpt_print(periph->path, "removing changer entry\n");
kfree(softc->changer, M_DEVBUF);
}
devstat_remove_entry(&softc->device_stats);
cam_extend_release(cdperiphs, periph->unit_number);
if (softc->disk.d_rawdev) {
cam_periph_unlock(periph);
disk_destroy(&softc->disk);
cam_periph_lock(periph);
}
kfree(softc, M_DEVBUF);
}
static void
cdasync(void *callback_arg, u_int32_t code,
struct cam_path *path, void *arg)
{
struct cam_periph *periph;
periph = (struct cam_periph *)callback_arg;
switch (code) {
case AC_FOUND_DEVICE:
{
struct ccb_getdev *cgd;
cam_status status;
cgd = (struct ccb_getdev *)arg;
if (cgd == NULL)
break;
if (SID_TYPE(&cgd->inq_data) != T_CDROM
&& SID_TYPE(&cgd->inq_data) != T_WORM)
break;
periph = cam_periph_find(cgd->ccb_h.path, "cd");
if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0)
break;
status = cam_periph_alloc(cdregister, cdoninvalidate,
cdcleanup, cdstart,
"cd", CAM_PERIPH_BIO,
cgd->ccb_h.path, cdasync,
AC_FOUND_DEVICE, cgd);
if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
kprintf("cdasync: Unable to attach new device "
"due to status 0x%x\n", status);
}
break;
}
case AC_SENT_BDR:
case AC_BUS_RESET:
{
struct cd_softc *softc;
struct ccb_hdr *ccbh;
softc = (struct cd_softc *)periph->softc;
softc->flags |= CD_FLAG_RETRY_UA;
LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
ccbh->ccb_state |= CD_CCB_RETRY_UA;
}
default:
cam_periph_async(periph, code, path, arg);
break;
}
}
static void
cdsysctlinit(void *context, int pending)
{
struct cam_periph *periph;
struct cd_softc *softc;
char tmpstr[80], tmpstr2[80];
periph = (struct cam_periph *)context;
if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
return;
}
softc = (struct cd_softc *)periph->softc;
ksnprintf(tmpstr, sizeof(tmpstr),
"CAM CD unit %d", periph->unit_number);
ksnprintf(tmpstr2, sizeof(tmpstr2),
"%d", periph->unit_number);
sysctl_ctx_free(&softc->sysctl_ctx);
sysctl_ctx_init(&softc->sysctl_ctx);
softc->flags |= CD_FLAG_SCTX_INIT;
softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
SYSCTL_STATIC_CHILDREN(_kern_cam_cd), OID_AUTO,
tmpstr2, CTLFLAG_RD, 0, tmpstr);
if (softc->sysctl_tree == NULL) {
kprintf("cdsysctlinit: unable to allocate sysctl tree\n");
cam_periph_release(periph);
return;
}
SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
&softc->minimum_command_size, 0, cdcmdsizesysctl, "I",
"Minimum CDB size");
cam_periph_release(periph);
}
static int
cdcmdsizesysctl(SYSCTL_HANDLER_ARGS)
{
int error, value;
value = *(int *)arg1;
error = sysctl_handle_int(oidp, &value, 0, req);
if ((error != 0) || (req->newptr == NULL))
return (error);
if (value < 6)
value = 6;
else if (value > 6)
value = 10;
*(int *)arg1 = value;
return (0);
}
static cam_status
cdregister(struct cam_periph *periph, void *arg)
{
struct cd_softc *softc;
struct ccb_pathinq *cpi;
struct ccb_getdev *cgd;
char tmpstr[80];
caddr_t match;
cgd = (struct ccb_getdev *)arg;
if (periph == NULL) {
kprintf("cdregister: periph was NULL!!\n");
return(CAM_REQ_CMP_ERR);
}
if (cgd == NULL) {
kprintf("cdregister: no getdev CCB, can't register device\n");
return(CAM_REQ_CMP_ERR);
}
softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
sysctl_ctx_init(&softc->sysctl_ctx);
LIST_INIT(&softc->pending_ccbs);
STAILQ_INIT(&softc->mode_queue);
softc->state = CD_STATE_PROBE;
bioq_init(&softc->bio_queue);
if (SID_IS_REMOVABLE(&cgd->inq_data))
softc->flags |= CD_FLAG_DISC_REMOVABLE;
if ((cgd->inq_data.flags & SID_CmdQue) != 0)
softc->flags |= CD_FLAG_TAGGED_QUEUING;
periph->softc = softc;
softc->periph = periph;
cam_extend_set(cdperiphs, periph->unit_number, periph);
match = cam_quirkmatch((caddr_t)&cgd->inq_data,
(caddr_t)cd_quirk_table,
NELEM(cd_quirk_table),
sizeof(*cd_quirk_table), scsi_inquiry_match);
if (match != NULL)
softc->quirks = ((struct cd_quirk_entry *)match)->quirks;
else
softc->quirks = CD_Q_NONE;
cpi = &xpt_alloc_ccb()->cpi;
xpt_setup_ccb(&cpi->ccb_h, periph->path, 1);
cpi->ccb_h.func_code = XPT_PATH_INQ;
xpt_action((union ccb *)cpi);
if (cpi->ccb_h.status == CAM_REQ_CMP && (cpi->hba_misc & PIM_NO_6_BYTE))
softc->quirks |= CD_Q_10_BYTE_ONLY;
xpt_free_ccb(&cpi->ccb_h);
TASK_INIT(&softc->sysctl_task, 0, cdsysctlinit, periph);
if (softc->quirks & CD_Q_10_BYTE_ONLY)
softc->minimum_command_size = 10;
else
softc->minimum_command_size = 6;
ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.cd.%d.minimum_cmd_size",
periph->unit_number);
TUNABLE_INT_FETCH(tmpstr, &softc->minimum_command_size);
if (softc->minimum_command_size < 6)
softc->minimum_command_size = 6;
else if (softc->minimum_command_size > 6)
softc->minimum_command_size = 10;
cam_periph_unlock(periph);
devstat_add_entry(&softc->device_stats, "cd",
periph->unit_number, 0,
DEVSTAT_BS_UNAVAILABLE,
DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_SCSI,
DEVSTAT_PRIORITY_CD);
disk_create(periph->unit_number, &softc->disk, &cd_ops);
disk_setdisktype(&softc->disk, "optical");
softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
softc->disk.d_info.d_dsflags = DSO_ONESLICE | DSO_COMPATLABEL |
DSO_COMPATPARTA;
cam_periph_lock(periph);
xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
cdasync, periph, periph->path);
if (((cgd->ccb_h.target_lun > 0)
&& ((softc->quirks & CD_Q_NO_CHANGER) == 0))
|| ((softc->quirks & CD_Q_CHANGER) != 0)) {
struct cdchanger *nchanger;
struct cam_periph *nperiph;
struct cam_path *path;
cam_status status;
int found;
softc->flags |= CD_FLAG_CHANGER;
lockmgr(&changerq_lock, LK_EXCLUSIVE);
for (found = 0,
nchanger = (struct cdchanger *)STAILQ_FIRST(&changerq);
nchanger != NULL;
nchanger = STAILQ_NEXT(nchanger, changer_links)){
if ((nchanger->path_id == cgd->ccb_h.path_id)
&& (nchanger->target_id == cgd->ccb_h.target_id)) {
found = 1;
break;
}
}
lockmgr(&changerq_lock, LK_RELEASE);
if (found == 1) {
struct chdevlist *chlunhead;
chlunhead = &nchanger->chluns;
status = xpt_create_path(&path, periph,
cgd->ccb_h.path_id,
cgd->ccb_h.target_id, 0);
if ((status == CAM_REQ_CMP)
&& ((nperiph = cam_periph_find(path, "cd")) != NULL)){
struct cd_softc *nsoftc;
nsoftc = (struct cd_softc *)nperiph->softc;
if ((nsoftc->flags & CD_FLAG_CHANGER) == 0){
nsoftc->flags |= CD_FLAG_CHANGER;
nchanger->num_devices++;
if (camq_resize(&nchanger->devq,
nchanger->num_devices)!=CAM_REQ_CMP){
kprintf("cdregister: "
"camq_resize "
"failed, changer "
"support may "
"be messed up\n");
}
nsoftc->changer = nchanger;
nsoftc->pinfo.index =CAM_UNQUEUED_INDEX;
STAILQ_INSERT_TAIL(&nchanger->chluns,
nsoftc,changer_links);
}
xpt_free_path(path);
} else if (status == CAM_REQ_CMP)
xpt_free_path(path);
else {
kprintf("cdregister: unable to allocate path\n"
"cdregister: changer support may be "
"broken\n");
}
nchanger->num_devices++;
softc->changer = nchanger;
softc->pinfo.index = CAM_UNQUEUED_INDEX;
if (camq_resize(&nchanger->devq,
nchanger->num_devices) != CAM_REQ_CMP) {
kprintf("cdregister: camq_resize "
"failed, changer support may "
"be messed up\n");
}
STAILQ_INSERT_TAIL(chlunhead, softc, changer_links);
}
else {
nchanger = kmalloc(sizeof(struct cdchanger),
M_DEVBUF, M_INTWAIT | M_ZERO);
callout_init_lk(&nchanger->short_handle,
periph->sim->lock);
callout_init_lk(&nchanger->long_handle,
periph->sim->lock);
if (camq_init(&nchanger->devq, 1) != 0) {
softc->flags &= ~CD_FLAG_CHANGER;
kprintf("cdregister: changer support "
"disabled\n");
goto cdregisterexit;
}
nchanger->path_id = cgd->ccb_h.path_id;
nchanger->target_id = cgd->ccb_h.target_id;
nchanger->num_devices = 0;
STAILQ_INIT(&nchanger->chluns);
callout_init_lk(&nchanger->long_handle,
periph->sim->lock);
callout_init_lk(&nchanger->short_handle,
periph->sim->lock);
lockmgr(&changerq_lock, LK_EXCLUSIVE);
num_changers++;
STAILQ_INSERT_TAIL(&changerq, nchanger,
changer_links);
lockmgr(&changerq_lock, LK_RELEASE);
status = xpt_create_path(&path, periph,
cgd->ccb_h.path_id,
cgd->ccb_h.target_id, 0);
if ((status == CAM_REQ_CMP)
&& ((nperiph = cam_periph_find(path, "cd")) != NULL)
&& ((((struct cd_softc *)periph->softc)->flags &
CD_FLAG_CHANGER) == 0)) {
struct cd_softc *nsoftc;
nsoftc = (struct cd_softc *)nperiph->softc;
nsoftc->flags |= CD_FLAG_CHANGER;
nchanger->num_devices++;
if (camq_resize(&nchanger->devq,
nchanger->num_devices) != CAM_REQ_CMP) {
kprintf("cdregister: camq_resize "
"failed, changer support may "
"be messed up\n");
}
nsoftc->changer = nchanger;
nsoftc->pinfo.index = CAM_UNQUEUED_INDEX;
STAILQ_INSERT_TAIL(&nchanger->chluns,
nsoftc, changer_links);
xpt_free_path(path);
} else if (status == CAM_REQ_CMP)
xpt_free_path(path);
else {
kprintf("cdregister: unable to allocate path\n"
"cdregister: changer support may be "
"broken\n");
}
softc->changer = nchanger;
softc->pinfo.index = CAM_UNQUEUED_INDEX;
nchanger->num_devices++;
if (camq_resize(&nchanger->devq,
nchanger->num_devices) != CAM_REQ_CMP) {
kprintf("cdregister: camq_resize "
"failed, changer support may "
"be messed up\n");
}
STAILQ_INSERT_TAIL(&nchanger->chluns, softc,
changer_links);
}
}
cdregisterexit:
cam_periph_hold(periph, 0);
if ((softc->flags & CD_FLAG_CHANGER) == 0)
xpt_schedule(periph, 5);
else
cdschedule(periph, 5);
return(CAM_REQ_CMP);
}
static int
cdopen(struct dev_open_args *ap)
{
cdev_t dev = ap->a_head.a_dev;
struct cam_periph *periph;
struct cd_softc *softc;
int unit, error;
if (caps_priv_check_self(SYSCAP_RESTRICTEDROOT))
return (EPERM);
unit = dkunit(dev);
periph = cam_extend_get(cdperiphs, unit);
if (periph == NULL)
return (ENXIO);
softc = (struct cd_softc *)periph->softc;
if (cam_periph_acquire(periph) != CAM_REQ_CMP)
return(ENXIO);
cam_periph_lock(periph);
if (softc->flags & CD_FLAG_INVALID) {
cam_periph_unlock(periph);
cam_periph_release(periph);
return(ENXIO);
}
if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
cam_periph_unlock(periph);
cam_periph_release(periph);
return (error);
}
if (softc->flags & CD_FLAG_OPEN)
cam_periph_release(periph);
else
softc->flags |= CD_FLAG_OPEN;
error = cdcheckmedia(periph);
if (error) {
softc->flags |= CD_FLAG_CAP_MUTE;
} else {
softc->flags &= ~CD_FLAG_CAP_MUTE;
}
cdprevent(periph, PR_PREVENT);
CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));
cam_periph_unhold(periph, 1);
return (0);
}
static int
cdclose(struct dev_close_args *ap)
{
cdev_t dev = ap->a_head.a_dev;
struct cam_periph *periph;
struct cd_softc *softc;
struct disk_info *info;
int unit;
unit = dkunit(dev);
periph = cam_extend_get(cdperiphs, unit);
if (periph == NULL)
return (ENXIO);
softc = (struct cd_softc *)periph->softc;
cam_periph_lock(periph);
cam_periph_hold(periph, 0);
if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)
cdprevent(periph, PR_ALLOW);
info = &softc->disk.d_info;
info->d_dsflags &= ~DSO_NOLABELS;
info->d_dsflags |= DSO_COMPATLABEL;
softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC|CD_FLAG_OPEN);
cam_periph_unhold(periph, 1);
cam_periph_release(periph);
return (0);
}
static void
cdshorttimeout(void *arg)
{
struct cdchanger *changer;
changer = (struct cdchanger *)arg;
changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
if ((bioq_first(&changer->cur_device->bio_queue) == NULL)
&& (changer->cur_device->outstanding_cmds == 0)) {
changer->flags |= CHANGER_MANUAL_CALL;
cdrunchangerqueue(changer);
}
}
static void
cdschedule(struct cam_periph *periph, int priority)
{
struct cd_softc *softc;
softc = (struct cd_softc *)periph->softc;
if ((softc->pinfo.index == CAM_UNQUEUED_INDEX)
&& ((softc->flags & CD_FLAG_ACTIVE) == 0)) {
softc->pinfo.priority = 1;
softc->pinfo.generation = ++softc->changer->devq.generation;
camq_insert(&softc->changer->devq, (cam_pinfo *)softc);
if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
&& ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
&& ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){
softc->changer->flags |= CHANGER_MANUAL_CALL;
cdrunchangerqueue(softc->changer);
}
} else if ((softc->flags & CD_FLAG_ACTIVE)
&& ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0)) {
xpt_schedule(periph, priority);
}
}
static void
cdrunchangerqueue(void *arg)
{
struct cd_softc *softc;
struct cdchanger *changer;
int called_from_timeout;
changer = (struct cdchanger *)arg;
if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {
changer->flags &= ~CHANGER_TIMEOUT_SCHED;
called_from_timeout = 1;
} else
called_from_timeout = 0;
changer->flags &= ~CHANGER_MANUAL_CALL;
if (changer->devq.entries <= 0) {
return;
}
if (changer->devq.qfrozen_cnt > 0) {
changer->devq.qfrozen_cnt--;
changer->cur_device->flags &= ~CD_FLAG_ACTIVE;
changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;
if (changer->cur_device->outstanding_cmds > 0) {
changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;
changer->cur_device->bufs_left =
changer->cur_device->outstanding_cmds;
if (called_from_timeout) {
callout_reset(&changer->long_handle,
changer_max_busy_seconds * hz,
cdrunchangerqueue, changer);
changer->flags |= CHANGER_TIMEOUT_SCHED;
}
return;
}
if (bioq_first(&changer->cur_device->bio_queue) != NULL) {
changer->cur_device->pinfo.generation =
++changer->devq.generation;
camq_insert(&changer->devq,
(cam_pinfo *)changer->cur_device);
}
}
softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);
changer->cur_device = softc;
changer->devq.qfrozen_cnt++;
softc->flags |= CD_FLAG_ACTIVE;
wakeup(&softc->changer);
xpt_schedule(softc->periph, 1);
if (changer->flags & CHANGER_TIMEOUT_SCHED) {
callout_stop(&changer->long_handle);
changer->flags &= ~CHANGER_TIMEOUT_SCHED;
}
if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {
callout_stop(&changer->short_handle);
changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;
}
changer->flags |= CHANGER_NEED_TIMEOUT;
}
static void
cdchangerschedule(struct cd_softc *softc)
{
struct cdchanger *changer;
changer = softc->changer;
if ((softc->flags & CD_FLAG_ACTIVE)
&& ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)
&& ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {
if (softc->flags & CD_FLAG_SCHED_ON_COMP) {
if (--softc->bufs_left == 0) {
softc->changer->flags |=
CHANGER_MANUAL_CALL;
softc->flags &= ~CD_FLAG_SCHED_ON_COMP;
cdrunchangerqueue(softc->changer);
}
} else if ((bioq_first(&softc->bio_queue) == NULL)
&& (softc->outstanding_cmds == 0)) {
softc->changer->flags |= CHANGER_MANUAL_CALL;
cdrunchangerqueue(softc->changer);
}
} else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT)
&& (softc->flags & CD_FLAG_ACTIVE)) {
if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {
callout_reset(&changer->long_handle,
changer_max_busy_seconds * hz,
cdrunchangerqueue, changer);
changer->flags |= CHANGER_TIMEOUT_SCHED;
} else
kprintf("cdchangerschedule: already have a long"
" timeout!\n");
if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {
callout_reset(&changer->short_handle,
changer_min_busy_seconds * hz,
cdshorttimeout, changer);
changer->flags |= CHANGER_SHORT_TMOUT_SCHED;
} else
kprintf("cdchangerschedule: already have a short "
"timeout!\n");
changer->flags &= ~CHANGER_NEED_TIMEOUT;
}
}
static int
cdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,
u_int32_t cam_flags,
u_int32_t sense_flags),
u_int32_t cam_flags, u_int32_t sense_flags)
{
struct cd_softc *softc;
struct cam_periph *periph;
int error;
periph = xpt_path_periph(ccb->ccb_h.path);
softc = (struct cd_softc *)periph->softc;
error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,
&softc->device_stats);
if (softc->flags & CD_FLAG_CHANGER)
cdchangerschedule(softc);
return(error);
}
static union ccb *
cdgetccb(struct cam_periph *periph, u_int32_t priority)
{
struct cd_softc *softc;
static union ccb *ccb;
softc = (struct cd_softc *)periph->softc;
if (softc->flags & CD_FLAG_CHANGER) {
while ((softc->flags & CD_FLAG_ACTIVE) == 0) {
if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {
softc->pinfo.priority = 1;
softc->pinfo.generation =
++softc->changer->devq.generation;
camq_insert(&softc->changer->devq,
(cam_pinfo *)softc);
}
if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)
&& ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)
&& ((softc->changer->flags
& CHANGER_SHORT_TMOUT_SCHED)==0)) {
softc->changer->flags |= CHANGER_MANUAL_CALL;
cdrunchangerqueue(softc->changer);
} else
sim_lock_sleep(&softc->changer, 0, "cgticb", 0,
periph->sim->lock);
}
}
ccb = cam_periph_getccb(periph, priority);
ccb->ccb_h.ccb_state = CD_CCB_POLLED;
return ccb;
}
static int
cdstrategy(struct dev_strategy_args *ap)
{
cdev_t dev = ap->a_head.a_dev;
struct bio *bio = ap->a_bio;
struct buf *bp = bio->bio_buf;
struct cam_periph *periph;
struct cd_softc *softc;
u_int unit;
unit = dkunit(dev);
periph = cam_extend_get(cdperiphs, unit);
if (periph == NULL) {
bp->b_error = ENXIO;
goto bad;
}
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));
softc = (struct cd_softc *)periph->softc;
if ((softc->flags & CD_FLAG_INVALID)) {
cam_periph_unlock(periph);
bp->b_error = ENXIO;
goto bad;
}
if ((softc->flags & CD_FLAG_VALID_MEDIA) == 0) {
int error;
error = cdcheckmedia(periph);
if (error != 0) {
cam_periph_unlock(periph);
bp->b_error = error;
goto bad;
}
}
bioqdisksort(&softc->bio_queue, bio);
if ((softc->flags & CD_FLAG_CHANGER) == 0)
xpt_schedule(periph, 1);
else
cdschedule(periph, 1);
cam_periph_unlock(periph);
return(0);
bad:
bp->b_flags |= B_ERROR;
bp->b_resid = bp->b_bcount;
biodone(bio);
return(0);
}
static void
cdstart(struct cam_periph *periph, union ccb *start_ccb)
{
struct cd_softc *softc;
struct bio *bio;
struct buf *bp;
struct ccb_scsiio *csio;
struct scsi_read_capacity_data *rcap;
softc = (struct cd_softc *)periph->softc;
CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));
switch (softc->state) {
case CD_STATE_NORMAL:
{
bio = bioq_first(&softc->bio_queue);
if (periph->immediate_priority <= periph->pinfo.priority) {
start_ccb->ccb_h.ccb_state = CD_CCB_WAITING;
SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
periph_links.sle);
periph->immediate_priority = CAM_PRIORITY_NONE;
wakeup(&periph->ccb_list);
} else if (bio == NULL) {
xpt_release_ccb(start_ccb);
} else {
bp = bio->bio_buf;
bioq_remove(&softc->bio_queue, bio);
devstat_start_transaction(&softc->device_stats);
KKASSERT(bio->bio_offset % softc->params.blksize == 0);
scsi_read_write(&start_ccb->csio,
4,
cddone,
MSG_SIMPLE_Q_TAG,
(bp->b_cmd == BUF_CMD_READ),
0,
10,
bio->bio_offset / softc->params.blksize,
bp->b_bcount / softc->params.blksize,
bp->b_data,
bp->b_bcount,
SSD_FULL_SIZE,
30000);
start_ccb->ccb_h.ccb_state = CD_CCB_BUFFER_IO;
LIST_INSERT_HEAD(&softc->pending_ccbs,
&start_ccb->ccb_h, periph_links.le);
softc->outstanding_cmds++;
if ((softc->flags & CD_FLAG_RETRY_UA) != 0) {
start_ccb->ccb_h.ccb_state |= CD_CCB_RETRY_UA;
softc->flags &= ~CD_FLAG_RETRY_UA;
}
start_ccb->ccb_h.ccb_bio = bio;
bio = bioq_first(&softc->bio_queue);
xpt_action(start_ccb);
}
if (bio != NULL) {
xpt_schedule(periph, 1);
}
break;
}
case CD_STATE_PROBE:
{
rcap = kmalloc(sizeof(*rcap), M_SCSICD, M_INTWAIT);
csio = &start_ccb->csio;
scsi_read_capacity(csio,
1,
cddone,
MSG_SIMPLE_Q_TAG,
rcap,
SSD_FULL_SIZE,
20000);
start_ccb->ccb_h.ccb_bio = NULL;
start_ccb->ccb_h.ccb_state = CD_CCB_PROBE;
xpt_action(start_ccb);
break;
}
}
}
static void
cddone(struct cam_periph *periph, union ccb *done_ccb)
{
struct cd_softc *softc;
struct ccb_scsiio *csio;
CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cddone\n"));
softc = (struct cd_softc *)periph->softc;
csio = &done_ccb->csio;
switch (csio->ccb_h.ccb_state & CD_CCB_TYPE_MASK) {
case CD_CCB_BUFFER_IO:
{
struct buf *bp;
struct bio *bio;
int error;
bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
bp = bio->bio_buf;
error = 0;
if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
int sf;
if ((done_ccb->ccb_h.ccb_state & CD_CCB_RETRY_UA) != 0)
sf = SF_RETRY_UA;
else
sf = 0;
error = cderror(done_ccb, CAM_RETRY_SELTO, sf);
if (error == ERESTART) {
return;
}
}
if (error != 0) {
struct bio *q_bio;
struct buf *q_bp;
xpt_print(periph->path,
"cddone: got error %#x back\n", error);
while ((q_bio = bioq_takefirst(&softc->bio_queue)) != NULL) {
q_bp = q_bio->bio_buf;
q_bp->b_resid = q_bp->b_bcount;
q_bp->b_error = EIO;
q_bp->b_flags |= B_ERROR;
biodone(q_bio);
}
bp->b_resid = bp->b_bcount;
bp->b_error = error;
bp->b_flags |= B_ERROR;
cam_release_devq(done_ccb->ccb_h.path,
0,
0,
0,
0);
} else {
bp->b_resid = csio->resid;
bp->b_error = 0;
if (bp->b_resid != 0) {
bp->b_flags |= B_ERROR;
}
}
LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
softc->outstanding_cmds--;
if (softc->flags & CD_FLAG_CHANGER)
cdchangerschedule(softc);
devstat_end_transaction_buf(&softc->device_stats, bp);
biodone(bio);
break;
}
case CD_CCB_PROBE:
{
struct scsi_read_capacity_data *rdcap;
struct disk_info info;
char announce_buf[120];
struct cd_params *cdp;
int doinfo = 0;
cdp = &softc->params;
bzero(&info, sizeof(info));
info.d_type = DTYPE_SCSI;
info.d_dsflags &= ~DSO_COMPATLABEL;
info.d_dsflags |= DSO_NOLABELS | DSO_COMPATPARTA;
info.d_serialno = xpt_path_serialno(periph->path);
rdcap = (struct scsi_read_capacity_data *)csio->data_ptr;
cdp->disksize = scsi_4btoul (rdcap->addr) + 1;
cdp->blksize = scsi_4btoul (rdcap->length);
if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
ksnprintf(announce_buf, sizeof(announce_buf),
"cd present [%lu x %lu byte records]",
cdp->disksize, (u_long)cdp->blksize);
info.d_media_blksize = cdp->blksize;
info.d_media_blocks = cdp->disksize;
doinfo = 1;
} else {
int error;
error = cderror(done_ccb, CAM_RETRY_SELTO,
SF_RETRY_UA | SF_NO_PRINT);
if (error == ERESTART) {
return;
} else if (error != 0) {
struct scsi_sense_data *sense;
int asc, ascq;
int sense_key, error_code;
int have_sense;
cam_status status;
struct ccb_getdev *cgd;
cam_release_devq(done_ccb->ccb_h.path,
0,
0,
0,
0);
status = done_ccb->ccb_h.status;
cgd = &xpt_alloc_ccb()->cgd;
xpt_setup_ccb(&cgd->ccb_h,
done_ccb->ccb_h.path,
1);
cgd->ccb_h.func_code = XPT_GDEV_TYPE;
xpt_action((union ccb *)cgd);
if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
|| ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
|| ((status & CAM_AUTOSNS_VALID) == 0))
have_sense = FALSE;
else
have_sense = TRUE;
if (have_sense) {
sense = &csio->sense_data;
scsi_extract_sense(sense, &error_code,
&sense_key,
&asc, &ascq);
}
if ((have_sense) && (asc != 0x25)
&& (error_code == SSD_CURRENT_ERROR)) {
const char *sense_key_desc;
const char *asc_desc;
scsi_sense_desc(sense_key, asc, ascq,
&cgd->inq_data,
&sense_key_desc,
&asc_desc);
ksnprintf(announce_buf,
sizeof(announce_buf),
"Attempt to query device "
"size failed: %s, %s",
sense_key_desc,
asc_desc);
info.d_media_blksize = 2048;
doinfo = 1;
} else if (SID_TYPE(&cgd->inq_data) == T_CDROM) {
if (have_sense)
scsi_sense_print(
&done_ccb->csio);
else {
xpt_print(periph->path,
"got CAM status %#x\n",
done_ccb->ccb_h.status);
}
xpt_print(periph->path, "fatal error, "
"failed to attach to device\n");
cam_periph_invalidate(periph);
announce_buf[0] = '\0';
} else {
cam_periph_invalidate(periph);
announce_buf[0] = '\0';
}
xpt_free_ccb(&cgd->ccb_h);
}
}
kfree(rdcap, M_SCSICD);
if (announce_buf[0] != '\0') {
xpt_announce_periph(periph, announce_buf);
if (softc->flags & CD_FLAG_CHANGER)
cdchangerschedule(softc);
taskqueue_enqueue(taskqueue_thread[mycpuid],
&softc->sysctl_task);
}
softc->state = CD_STATE_NORMAL;
xpt_release_ccb(done_ccb);
cam_periph_unhold(periph, 0);
if (doinfo) {
disk_setdiskinfo(&softc->disk, &info);
}
return;
}
case CD_CCB_WAITING:
{
CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
("trying to wakeup ccbwait\n"));
wakeup(&done_ccb->ccb_h.cbfcnp);
return;
}
case CD_CCB_POLLED:
wakeup(&done_ccb->ccb_h.cbfcnp);
return;
default:
break;
}
xpt_release_ccb(done_ccb);
}
static union cd_pages *
cdgetpage(struct cd_mode_params *mode_params)
{
union cd_pages *page;
if (mode_params->cdb_size == 10)
page = (union cd_pages *)find_mode_page_10(
(struct scsi_mode_header_10 *)mode_params->mode_buf);
else
page = (union cd_pages *)find_mode_page_6(
(struct scsi_mode_header_6 *)mode_params->mode_buf);
return (page);
}
static int
cdgetpagesize(int page_num)
{
int i;
for (i = 0; i < NELEM(cd_page_size_table); i++) {
if (cd_page_size_table[i].page == page_num)
return (cd_page_size_table[i].page_size);
}
return (-1);
}
static int
cdioctl(struct dev_ioctl_args *ap)
{
cdev_t dev = ap->a_head.a_dev;
caddr_t addr = ap->a_data;
struct cam_periph *periph;
struct cd_softc *softc;
int unit, error = 0;
unit = dkunit(dev);
periph = cam_extend_get(cdperiphs, unit);
if (periph == NULL)
return(ENXIO);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdioctl\n"));
softc = (struct cd_softc *)periph->softc;
CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
("trying to do ioctl %#lx\n", ap->a_cmd));
if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
cam_periph_unlock(periph);
cam_periph_release(periph);
return (error);
}
if (((softc->flags & CD_FLAG_VALID_MEDIA) == 0)
&& ((ap->a_cmd != CDIOCCLOSE)
&& (ap->a_cmd != CDIOCEJECT))
&& (IOCGROUP(ap->a_cmd) == 'c')) {
error = cdcheckmedia(periph);
if (error != 0) {
cam_periph_unhold(periph, 1);
return (error);
}
}
cam_periph_unlock(periph);
switch (ap->a_cmd) {
case CDIOCPLAYTRACKS:
{
struct ioc_play_track *args
= (struct ioc_play_track *)addr;
struct cd_mode_params params;
union cd_pages *page;
params.alloc_len = sizeof(union cd_mode_data_6_10);
params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
M_WAITOK | M_ZERO);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOCPLAYTRACKS\n"));
error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
if (error) {
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
break;
}
page = cdgetpage(¶ms);
page->audio.flags &= ~CD_PA_SOTC;
page->audio.flags |= CD_PA_IMMED;
error = cdsetmode(periph, ¶ms);
kfree(params.mode_buf, M_SCSICD);
if (error) {
cam_periph_unlock(periph);
break;
}
if (softc->flags & CD_FLAG_VALID_TOC) {
union msf_lba *sentry, *eentry;
int st, et;
if (args->end_track <
softc->toc.header.ending_track + 1)
args->end_track++;
if (args->end_track >
softc->toc.header.ending_track + 1)
args->end_track =
softc->toc.header.ending_track + 1;
st = args->start_track -
softc->toc.header.starting_track;
et = args->end_track -
softc->toc.header.starting_track;
if ((st < 0)
|| (et < 0)
|| (st > (softc->toc.header.ending_track -
softc->toc.header.starting_track))) {
error = EINVAL;
break;
}
sentry = &softc->toc.entries[st].addr;
eentry = &softc->toc.entries[et].addr;
error = cdplaymsf(periph,
sentry->msf.minute,
sentry->msf.second,
sentry->msf.frame,
eentry->msf.minute,
eentry->msf.second,
eentry->msf.frame);
} else {
if (softc->quirks & CD_Q_BCD_TRACKS) {
args->start_track =
bin2bcd(args->start_track);
args->end_track =
bin2bcd(args->end_track);
}
error = cdplaytracks(periph,
args->start_track,
args->start_index,
args->end_track,
args->end_index);
}
cam_periph_unlock(periph);
}
break;
case CDIOCPLAYMSF:
{
struct ioc_play_msf *args
= (struct ioc_play_msf *) addr;
struct cd_mode_params params;
union cd_pages *page;
params.alloc_len = sizeof(union cd_mode_data_6_10);
params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
M_WAITOK | M_ZERO);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOCPLAYMSF\n"));
error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
if (error) {
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
break;
}
page = cdgetpage(¶ms);
page->audio.flags &= ~CD_PA_SOTC;
page->audio.flags |= CD_PA_IMMED;
error = cdsetmode(periph, ¶ms);
kfree(params.mode_buf, M_SCSICD);
if (error) {
cam_periph_unlock(periph);
break;
}
error = cdplaymsf(periph,
args->start_m,
args->start_s,
args->start_f,
args->end_m,
args->end_s,
args->end_f);
cam_periph_unlock(periph);
}
break;
case CDIOCPLAYBLOCKS:
{
struct ioc_play_blocks *args
= (struct ioc_play_blocks *) addr;
struct cd_mode_params params;
union cd_pages *page;
params.alloc_len = sizeof(union cd_mode_data_6_10);
params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
M_WAITOK | M_ZERO);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOCPLAYBLOCKS\n"));
error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
if (error) {
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
break;
}
page = cdgetpage(¶ms);
page->audio.flags &= ~CD_PA_SOTC;
page->audio.flags |= CD_PA_IMMED;
error = cdsetmode(periph, ¶ms);
kfree(params.mode_buf, M_SCSICD);
if (error) {
cam_periph_unlock(periph);
break;
}
error = cdplay(periph, args->blk, args->len);
cam_periph_unlock(periph);
}
break;
case CDIOCREADSUBCHANNEL:
{
struct ioc_read_subchannel *args
= (struct ioc_read_subchannel *) addr;
struct cd_sub_channel_info *data;
u_int32_t len = args->data_len;
data = kmalloc(sizeof(struct cd_sub_channel_info),
M_SCSICD, M_WAITOK);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOCREADSUBCHANNEL\n"));
if ((len > sizeof(struct cd_sub_channel_info)) ||
(len < sizeof(struct cd_sub_channel_header))) {
kprintf(
"scsi_cd: cdioctl: "
"cdioreadsubchannel: error, len=%d\n",
len);
error = EINVAL;
kfree(data, M_SCSICD);
cam_periph_unlock(periph);
break;
}
if (softc->quirks & CD_Q_BCD_TRACKS)
args->track = bin2bcd(args->track);
error = cdreadsubchannel(periph, args->address_format,
args->data_format, args->track, data, len);
if (error) {
kfree(data, M_SCSICD);
cam_periph_unlock(periph);
break;
}
if (softc->quirks & CD_Q_BCD_TRACKS)
data->what.track_info.track_number =
bcd2bin(data->what.track_info.track_number);
len = min(len, ((data->header.data_len[0] << 8) +
data->header.data_len[1] +
sizeof(struct cd_sub_channel_header)));
cam_periph_unlock(periph);
if (copyout(data, args->data, len) != 0) {
error = EFAULT;
}
kfree(data, M_SCSICD);
}
break;
case CDIOREADTOCHEADER:
{
struct ioc_toc_header *th;
th = kmalloc(sizeof(struct ioc_toc_header), M_SCSICD,
M_WAITOK);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOREADTOCHEADER\n"));
error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
sizeof (*th), 0);
if (error) {
kfree(th, M_SCSICD);
cam_periph_unlock(periph);
break;
}
if (softc->quirks & CD_Q_BCD_TRACKS) {
th->starting_track =
bcd2bin(th->starting_track);
th->ending_track = bcd2bin(th->ending_track);
}
th->len = ntohs(th->len);
bcopy(th, addr, sizeof(*th));
kfree(th, M_SCSICD);
cam_periph_unlock(periph);
}
break;
case CDIOREADTOCENTRYS:
{
struct cd_tocdata *data;
struct cd_toc_single *lead;
struct ioc_read_toc_entry *te =
(struct ioc_read_toc_entry *) addr;
struct ioc_toc_header *th;
u_int32_t len, readlen, idx, num;
u_int32_t starting_track = te->starting_track;
data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK);
lead = kmalloc(sizeof(*lead), M_SCSICD, M_WAITOK);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOREADTOCENTRYS\n"));
if (te->data_len < sizeof(struct cd_toc_entry)
|| (te->data_len % sizeof(struct cd_toc_entry)) != 0
|| (te->address_format != CD_MSF_FORMAT
&& te->address_format != CD_LBA_FORMAT)) {
error = EINVAL;
kprintf("scsi_cd: error in readtocentries, "
"returning EINVAL\n");
kfree(data, M_SCSICD);
kfree(lead, M_SCSICD);
cam_periph_unlock(periph);
break;
}
th = &data->header;
error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
sizeof (*th), 0);
if (error) {
kfree(data, M_SCSICD);
kfree(lead, M_SCSICD);
cam_periph_unlock(periph);
break;
}
if (softc->quirks & CD_Q_BCD_TRACKS) {
th->starting_track =
bcd2bin(th->starting_track);
th->ending_track = bcd2bin(th->ending_track);
}
if (starting_track == 0)
starting_track = th->starting_track;
else if (starting_track == LEADOUT)
starting_track = th->ending_track + 1;
else if (starting_track < th->starting_track ||
starting_track > th->ending_track + 1) {
kprintf("scsi_cd: error in readtocentries, "
"returning EINVAL\n");
kfree(data, M_SCSICD);
kfree(lead, M_SCSICD);
cam_periph_unlock(periph);
error = EINVAL;
break;
}
readlen = (th->ending_track - starting_track + 1) *
sizeof(struct cd_toc_entry);
len = readlen + sizeof(struct cd_toc_entry);
if (te->data_len < len) {
len = te->data_len;
if (readlen > len)
readlen = len;
}
if (len > sizeof(data->entries)) {
kprintf("scsi_cd: error in readtocentries, "
"returning EINVAL\n");
error = EINVAL;
kfree(data, M_SCSICD);
kfree(lead, M_SCSICD);
cam_periph_unlock(periph);
break;
}
num = len / sizeof(struct cd_toc_entry);
if (readlen > 0) {
error = cdreadtoc(periph, te->address_format,
starting_track,
(u_int8_t *)data,
readlen + sizeof (*th),
0);
if (error) {
kfree(data, M_SCSICD);
kfree(lead, M_SCSICD);
cam_periph_unlock(periph);
break;
}
}
idx = starting_track + num - 1;
if (softc->quirks & CD_Q_BCD_TRACKS)
th->ending_track = bcd2bin(th->ending_track);
if (idx == th->ending_track + 1) {
error = cdreadtoc(periph, te->address_format,
LEADOUT, (u_int8_t *)lead,
sizeof(*lead),
0);
if (error) {
kfree(data, M_SCSICD);
kfree(lead, M_SCSICD);
cam_periph_unlock(periph);
break;
}
data->entries[idx - starting_track] =
lead->entry;
}
if (softc->quirks & CD_Q_BCD_TRACKS) {
for (idx = 0; idx < num - 1; idx++) {
data->entries[idx].track =
bcd2bin(data->entries[idx].track);
}
}
cam_periph_unlock(periph);
error = copyout(data->entries, te->data, len);
kfree(data, M_SCSICD);
kfree(lead, M_SCSICD);
}
break;
case CDIOREADTOCENTRY:
{
struct cd_toc_single *data;
struct ioc_read_toc_single_entry *te =
(struct ioc_read_toc_single_entry *) addr;
struct ioc_toc_header *th;
u_int32_t track;
data = kmalloc(sizeof(*data), M_SCSICD, M_WAITOK);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOREADTOCENTRY\n"));
if (te->address_format != CD_MSF_FORMAT
&& te->address_format != CD_LBA_FORMAT) {
kprintf("error in readtocentry, "
" returning EINVAL\n");
kfree(data, M_SCSICD);
error = EINVAL;
cam_periph_unlock(periph);
break;
}
th = &data->header;
error = cdreadtoc(periph, 0, 0, (u_int8_t *)th,
sizeof (*th), 0);
if (error) {
kfree(data, M_SCSICD);
cam_periph_unlock(periph);
break;
}
if (softc->quirks & CD_Q_BCD_TRACKS) {
th->starting_track =
bcd2bin(th->starting_track);
th->ending_track = bcd2bin(th->ending_track);
}
track = te->track;
if (track == 0)
track = th->starting_track;
else if (track == LEADOUT)
;
else if (track < th->starting_track ||
track > th->ending_track + 1) {
kprintf("error in readtocentry, "
" returning EINVAL\n");
kfree(data, M_SCSICD);
error = EINVAL;
cam_periph_unlock(periph);
break;
}
error = cdreadtoc(periph, te->address_format, track,
(u_int8_t *)data, sizeof(*data),
0);
if (error) {
kfree(data, M_SCSICD);
cam_periph_unlock(periph);
break;
}
if (softc->quirks & CD_Q_BCD_TRACKS)
data->entry.track = bcd2bin(data->entry.track);
bcopy(&data->entry, &te->entry,
sizeof(struct cd_toc_entry));
kfree(data, M_SCSICD);
cam_periph_unlock(periph);
}
break;
case CDIOCSETPATCH:
{
struct ioc_patch *arg = (struct ioc_patch *)addr;
struct cd_mode_params params;
union cd_pages *page;
params.alloc_len = sizeof(union cd_mode_data_6_10);
params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
M_WAITOK | M_ZERO);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOCSETPATCH\n"));
error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
if (error) {
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
break;
}
page = cdgetpage(¶ms);
page->audio.port[LEFT_PORT].channels =
arg->patch[0];
page->audio.port[RIGHT_PORT].channels =
arg->patch[1];
page->audio.port[2].channels = arg->patch[2];
page->audio.port[3].channels = arg->patch[3];
error = cdsetmode(periph, ¶ms);
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
}
break;
case CDIOCGETVOL:
{
struct ioc_vol *arg = (struct ioc_vol *) addr;
struct cd_mode_params params;
union cd_pages *page;
params.alloc_len = sizeof(union cd_mode_data_6_10);
params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
M_WAITOK | M_ZERO);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOCGETVOL\n"));
error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
if (error) {
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
break;
}
page = cdgetpage(¶ms);
arg->vol[LEFT_PORT] =
page->audio.port[LEFT_PORT].volume;
arg->vol[RIGHT_PORT] =
page->audio.port[RIGHT_PORT].volume;
arg->vol[2] = page->audio.port[2].volume;
arg->vol[3] = page->audio.port[3].volume;
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
}
break;
case CDIOCSETVOL:
{
struct ioc_vol *arg = (struct ioc_vol *) addr;
struct cd_mode_params params;
union cd_pages *page;
params.alloc_len = sizeof(union cd_mode_data_6_10);
params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
M_WAITOK | M_ZERO);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOCSETVOL\n"));
error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
if (error) {
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
break;
}
page = cdgetpage(¶ms);
page->audio.port[LEFT_PORT].channels = CHANNEL_0;
page->audio.port[LEFT_PORT].volume =
arg->vol[LEFT_PORT];
page->audio.port[RIGHT_PORT].channels = CHANNEL_1;
page->audio.port[RIGHT_PORT].volume =
arg->vol[RIGHT_PORT];
page->audio.port[2].volume = arg->vol[2];
page->audio.port[3].volume = arg->vol[3];
error = cdsetmode(periph, ¶ms);
cam_periph_unlock(periph);
kfree(params.mode_buf, M_SCSICD);
}
break;
case CDIOCSETMONO:
{
struct cd_mode_params params;
union cd_pages *page;
params.alloc_len = sizeof(union cd_mode_data_6_10);
params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
M_WAITOK | M_ZERO);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOCSETMONO\n"));
error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
if (error) {
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
break;
}
page = cdgetpage(¶ms);
page->audio.port[LEFT_PORT].channels =
LEFT_CHANNEL | RIGHT_CHANNEL;
page->audio.port[RIGHT_PORT].channels =
LEFT_CHANNEL | RIGHT_CHANNEL;
page->audio.port[2].channels = 0;
page->audio.port[3].channels = 0;
error = cdsetmode(periph, ¶ms);
cam_periph_unlock(periph);
kfree(params.mode_buf, M_SCSICD);
}
break;
case CDIOCSETSTEREO:
{
struct cd_mode_params params;
union cd_pages *page;
params.alloc_len = sizeof(union cd_mode_data_6_10);
params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
M_WAITOK | M_ZERO);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOCSETSTEREO\n"));
error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
if (error) {
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
break;
}
page = cdgetpage(¶ms);
page->audio.port[LEFT_PORT].channels =
LEFT_CHANNEL;
page->audio.port[RIGHT_PORT].channels =
RIGHT_CHANNEL;
page->audio.port[2].channels = 0;
page->audio.port[3].channels = 0;
error = cdsetmode(periph, ¶ms);
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
}
break;
case CDIOCSETMUTE:
{
struct cd_mode_params params;
union cd_pages *page;
params.alloc_len = sizeof(union cd_mode_data_6_10);
params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
M_WAITOK | M_ZERO);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOCSETMUTE\n"));
error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
if (error) {
kfree(¶ms, M_SCSICD);
cam_periph_unlock(periph);
break;
}
page = cdgetpage(¶ms);
page->audio.port[LEFT_PORT].channels = 0;
page->audio.port[RIGHT_PORT].channels = 0;
page->audio.port[2].channels = 0;
page->audio.port[3].channels = 0;
error = cdsetmode(periph, ¶ms);
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
}
break;
case CDIOCSETLEFT:
{
struct cd_mode_params params;
union cd_pages *page;
params.alloc_len = sizeof(union cd_mode_data_6_10);
params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
M_WAITOK | M_ZERO);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOCSETLEFT\n"));
error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
if (error) {
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
break;
}
page = cdgetpage(¶ms);
page->audio.port[LEFT_PORT].channels = LEFT_CHANNEL;
page->audio.port[RIGHT_PORT].channels = LEFT_CHANNEL;
page->audio.port[2].channels = 0;
page->audio.port[3].channels = 0;
error = cdsetmode(periph, ¶ms);
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
}
break;
case CDIOCSETRIGHT:
{
struct cd_mode_params params;
union cd_pages *page;
params.alloc_len = sizeof(union cd_mode_data_6_10);
params.mode_buf = kmalloc(params.alloc_len, M_SCSICD,
M_WAITOK | M_ZERO);
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
("trying to do CDIOCSETRIGHT\n"));
error = cdgetmode(periph, ¶ms, AUDIO_PAGE);
if (error) {
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
break;
}
page = cdgetpage(¶ms);
page->audio.port[LEFT_PORT].channels = RIGHT_CHANNEL;
page->audio.port[RIGHT_PORT].channels = RIGHT_CHANNEL;
page->audio.port[2].channels = 0;
page->audio.port[3].channels = 0;
error = cdsetmode(periph, ¶ms);
kfree(params.mode_buf, M_SCSICD);
cam_periph_unlock(periph);
}
break;
case CDIOCRESUME:
cam_periph_lock(periph);
error = cdpause(periph, 1);
cam_periph_unlock(periph);
break;
case CDIOCPAUSE:
cam_periph_lock(periph);
error = cdpause(periph, 0);
cam_periph_unlock(periph);
break;
case CDIOCSTART:
cam_periph_lock(periph);
error = cdstartunit(periph, 0);
cam_periph_unlock(periph);
break;
case CDIOCCLOSE:
cam_periph_lock(periph);
error = cdstartunit(periph, 1);
cam_periph_unlock(periph);
break;
case CDIOCSTOP:
cam_periph_lock(periph);
error = cdstopunit(periph, 0);
cam_periph_unlock(periph);
break;
case CDIOCEJECT:
cam_periph_lock(periph);
error = cdstopunit(periph, 1);
cam_periph_unlock(periph);
break;
case CDIOCALLOW:
cam_periph_lock(periph);
cdprevent(periph, PR_ALLOW);
cam_periph_unlock(periph);
break;
case CDIOCPREVENT:
cam_periph_lock(periph);
cdprevent(periph, PR_PREVENT);
cam_periph_unlock(periph);
break;
case CDIOCSETDEBUG:
error = ENOTTY;
break;
case CDIOCCLRDEBUG:
error = ENOTTY;
break;
case CDIOCRESET:
error = ENOTTY;
break;
case CDRIOCREADSPEED:
cam_periph_lock(periph);
error = cdsetspeed(periph, *(u_int32_t *)addr, CDR_MAX_SPEED);
cam_periph_unlock(periph);
break;
case CDRIOCWRITESPEED:
cam_periph_lock(periph);
error = cdsetspeed(periph, CDR_MAX_SPEED, *(u_int32_t *)addr);
cam_periph_unlock(periph);
break;
case DVDIOCSENDKEY:
case DVDIOCREPORTKEY: {
struct dvd_authinfo *authinfo;
authinfo = (struct dvd_authinfo *)addr;
cam_periph_lock(periph);
if (ap->a_cmd == DVDIOCREPORTKEY)
error = cdreportkey(periph, authinfo);
else
error = cdsendkey(periph, authinfo);
cam_periph_unlock(periph);
break;
}
case DVDIOCREADSTRUCTURE: {
struct dvd_struct *dvdstruct;
dvdstruct = (struct dvd_struct *)addr;
cam_periph_lock(periph);
error = cdreaddvdstructure(periph, dvdstruct);
cam_periph_unlock(periph);
break;
}
default:
cam_periph_lock(periph);
error = cam_periph_ioctl(periph, ap->a_cmd, addr, cderror);
cam_periph_unlock(periph);
break;
}
cam_periph_lock(periph);
CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdioctl\n"));
cam_periph_unhold(periph, 1);
return (error);
}
static void
cdprevent(struct cam_periph *periph, int action)
{
union ccb *ccb;
struct cd_softc *softc;
int error;
CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdprevent\n"));
softc = (struct cd_softc *)periph->softc;
if (((action == PR_ALLOW)
&& (softc->flags & CD_FLAG_DISC_LOCKED) == 0)
|| ((action == PR_PREVENT)
&& (softc->flags & CD_FLAG_DISC_LOCKED) != 0)) {
return;
}
ccb = cdgetccb(periph, 1);
scsi_prevent(&ccb->csio,
1,
cddone,
MSG_SIMPLE_Q_TAG,
action,
SSD_FULL_SIZE,
60000);
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA|SF_NO_PRINT);
xpt_release_ccb(ccb);
if (error == 0) {
if (action == PR_ALLOW)
softc->flags &= ~CD_FLAG_DISC_LOCKED;
else
softc->flags |= CD_FLAG_DISC_LOCKED;
}
}
static int
cdcheckmedia(struct cam_periph *periph)
{
struct cd_softc *softc;
struct ioc_toc_header *toch;
struct cd_toc_single leadout;
struct ccb_getdev *cgd;
u_int32_t size, toclen;
int error, num_entries, cdindex;
int first_track_audio;
struct disk_info info;
softc = (struct cd_softc *)periph->softc;
first_track_audio = -1;
error = 0;
bzero(&info, sizeof(info));
info.d_type = DTYPE_SCSI;
info.d_dsflags &= ~DSO_COMPATLABEL;
info.d_dsflags |= DSO_NOLABELS | DSO_COMPATPARTA;
info.d_serialno = xpt_path_serialno(periph->path);
cgd = &xpt_alloc_ccb()->cgd;
xpt_setup_ccb(&cgd->ccb_h, periph->path, 1);
cgd->ccb_h.func_code = XPT_GDEV_TYPE;
xpt_action((union ccb *)cgd);
#if 0
strncpy(label->d_typename, cgd->inq_data.vendor,
min(SID_VENDOR_SIZE, sizeof(label->d_typename)));
strncpy(label->d_packname, cgd->inq_data.product,
min(SID_PRODUCT_SIZE, sizeof(label->d_packname)));
#endif
xpt_free_ccb(&cgd->ccb_h);
softc->flags &= ~(CD_FLAG_VALID_MEDIA|CD_FLAG_VALID_TOC);
if ((error = cdsize(periph, &size)) != 0) {
info.d_media_blksize = 2048;
disk_setdiskinfo(&softc->disk, &info);
softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
return (error);
} else {
info.d_media_blksize = softc->params.blksize;
info.d_media_blocks = softc->params.disksize;
disk_setdiskinfo(&softc->disk, &info);
if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0)
softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
softc->device_stats.block_size = softc->params.blksize;
softc->flags |= CD_FLAG_VALID_MEDIA;
}
bzero(&softc->toc, sizeof(softc->toc));
toch = &softc->toc.header;
error = cdreadtoc(periph, 0, 0, (u_int8_t *)toch, sizeof(*toch),
SF_NO_PRINT);
if (error != 0) {
error = 0;
bzero(&softc->toc, sizeof(softc->toc));
goto bailout;
}
if (softc->quirks & CD_Q_BCD_TRACKS) {
toch->starting_track = bcd2bin(toch->starting_track);
toch->ending_track = bcd2bin(toch->ending_track);
}
num_entries = (toch->ending_track - toch->starting_track) + 2;
if (num_entries <= 0)
goto bailout;
toclen = num_entries * sizeof(struct cd_toc_entry);
error = cdreadtoc(periph, CD_MSF_FORMAT, toch->starting_track,
(u_int8_t *)&softc->toc, toclen + sizeof(*toch),
SF_NO_PRINT);
if (error != 0) {
error = 0;
bzero(&softc->toc, sizeof(softc->toc));
goto bailout;
}
if (softc->quirks & CD_Q_BCD_TRACKS) {
toch->starting_track = bcd2bin(toch->starting_track);
toch->ending_track = bcd2bin(toch->ending_track);
}
toch->len = scsi_2btoul((uint8_t *)&toch->len);
cdindex = toch->starting_track + num_entries -1;
if (cdindex == toch->ending_track + 1) {
error = cdreadtoc(periph, CD_MSF_FORMAT, LEADOUT,
(u_int8_t *)&leadout, sizeof(leadout),
SF_NO_PRINT);
if (error != 0) {
error = 0;
goto bailout;
}
softc->toc.entries[cdindex - toch->starting_track] =
leadout.entry;
}
if (softc->quirks & CD_Q_BCD_TRACKS) {
for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
softc->toc.entries[cdindex].track =
bcd2bin(softc->toc.entries[cdindex].track);
}
}
for (cdindex = 0; cdindex < (num_entries - 1); cdindex++) {
if (softc->toc.entries[cdindex].track == toch->starting_track) {
if (softc->toc.entries[cdindex].control & 0x04)
first_track_audio = 0;
else
first_track_audio = 1;
break;
}
}
if (first_track_audio == 0) {
info.d_dsflags |= DSO_COMPATLABEL;
info.d_dsflags &= ~DSO_NOLABELS;
disk_setdiskinfo(&softc->disk, &info);
}
softc->flags |= CD_FLAG_VALID_TOC;
bailout:
return (error);
}
static int
cdsize(struct cam_periph *periph, u_int32_t *size)
{
struct cd_softc *softc;
union ccb *ccb;
struct scsi_read_capacity_data *rcap_buf;
int error;
CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdsize\n"));
softc = (struct cd_softc *)periph->softc;
ccb = cdgetccb(periph, 1);
rcap_buf = kmalloc(sizeof(struct scsi_read_capacity_data),
M_SCSICD, M_INTWAIT | M_ZERO);
scsi_read_capacity(&ccb->csio,
1,
cddone,
MSG_SIMPLE_Q_TAG,
rcap_buf,
SSD_FULL_SIZE,
20000);
if (softc->flags & CD_FLAG_CAP_MUTE)
ccb->ccb_h.flags |= CAM_QUIET;
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA|SF_NO_PRINT);
xpt_release_ccb(ccb);
softc->params.disksize = scsi_4btoul(rcap_buf->addr) + 1;
softc->params.blksize = scsi_4btoul(rcap_buf->length);
if (softc->params.blksize > 2048 && softc->params.blksize <= 2352)
softc->params.blksize = 2048;
kfree(rcap_buf, M_SCSICD);
*size = softc->params.disksize;
return (error);
}
static int
cd6byteworkaround(union ccb *ccb)
{
u_int8_t *cdb;
struct cam_periph *periph;
struct cd_softc *softc;
struct cd_mode_params *params;
int frozen, found;
periph = xpt_path_periph(ccb->ccb_h.path);
softc = (struct cd_softc *)periph->softc;
cdb = ccb->csio.cdb_io.cdb_bytes;
if ((ccb->ccb_h.flags & CAM_CDB_POINTER)
|| ((cdb[0] != MODE_SENSE_6)
&& (cdb[0] != MODE_SELECT_6)))
return (0);
found = 0;
STAILQ_FOREACH(params, &softc->mode_queue, links) {
if (params->mode_buf == ccb->csio.data_ptr) {
found = 1;
break;
}
}
if (found == 0) {
xpt_print(periph->path,
"mode buffer not found in mode queue!\n");
return (0);
}
params->cdb_size = 10;
softc->minimum_command_size = 10;
xpt_print(ccb->ccb_h.path,
"%s(6) failed, increasing minimum CDB size to 10 bytes\n",
(cdb[0] == MODE_SENSE_6) ? "MODE_SENSE" : "MODE_SELECT");
if (cdb[0] == MODE_SENSE_6) {
struct scsi_mode_sense_10 ms10;
struct scsi_mode_sense_6 *ms6;
int len;
ms6 = (struct scsi_mode_sense_6 *)cdb;
bzero(&ms10, sizeof(ms10));
ms10.opcode = MODE_SENSE_10;
ms10.byte2 = ms6->byte2;
ms10.page = ms6->page;
len = sizeof(struct cd_mode_data_10);
ccb->csio.dxfer_len = len;
scsi_ulto2b(len, ms10.length);
ms10.control = ms6->control;
bcopy(&ms10, cdb, 10);
ccb->csio.cdb_len = 10;
} else {
struct scsi_mode_select_10 ms10;
struct scsi_mode_select_6 *ms6;
struct scsi_mode_header_6 *header6;
struct scsi_mode_header_10 *header10;
struct scsi_mode_page_header *page_header;
int blk_desc_len, page_num, page_size, len;
ms6 = (struct scsi_mode_select_6 *)cdb;
bzero(&ms10, sizeof(ms10));
ms10.opcode = MODE_SELECT_10;
ms10.byte2 = ms6->byte2;
header6 = (struct scsi_mode_header_6 *)params->mode_buf;
header10 = (struct scsi_mode_header_10 *)params->mode_buf;
page_header = find_mode_page_6(header6);
page_num = page_header->page_code;
blk_desc_len = header6->blk_desc_len;
page_size = cdgetpagesize(page_num);
if (page_size != (page_header->page_length +
sizeof(*page_header)))
page_size = page_header->page_length +
sizeof(*page_header);
len = sizeof(*header10) + blk_desc_len + page_size;
len = min(params->alloc_len, len);
bcopy(params->mode_buf + sizeof(*header6),
params->mode_buf + sizeof(*header10),
len - sizeof(*header10));
scsi_ulto2b(0, header10->data_length);
header10->medium_type = 0;
scsi_ulto2b(blk_desc_len, header10->blk_desc_len);
ccb->csio.dxfer_len = len;
scsi_ulto2b(len, ms10.length);
ms10.control = ms6->control;
bcopy(&ms10, cdb, 10);
ccb->csio.cdb_len = 10;
}
frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
ccb->ccb_h.status = CAM_REQUEUE_REQ;
xpt_action(ccb);
if (frozen) {
cam_release_devq(ccb->ccb_h.path,
0,
0,
0,
0);
}
return (ERESTART);
}
static int
cderror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
{
struct cd_softc *softc;
struct cam_periph *periph;
int error;
periph = xpt_path_periph(ccb->ccb_h.path);
softc = (struct cd_softc *)periph->softc;
error = 0;
if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
error = cd6byteworkaround(ccb);
} else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
CAM_SCSI_STATUS_ERROR)
&& (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
&& (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
&& ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
&& ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
int sense_key, error_code, asc, ascq;
scsi_extract_sense(&ccb->csio.sense_data,
&error_code, &sense_key, &asc, &ascq);
if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
error = cd6byteworkaround(ccb);
}
if (error == ERESTART)
return (error);
sense_flags |= SF_RETRY_UA;
return (cam_periph_error(ccb, cam_flags, sense_flags,
&softc->saved_ccb));
}
static int
cdreadtoc(struct cam_periph *periph, u_int32_t mode, u_int32_t start,
u_int8_t *data, u_int32_t len, u_int32_t sense_flags)
{
struct scsi_read_toc *scsi_cmd;
u_int32_t ntoc;
struct ccb_scsiio *csio;
union ccb *ccb;
int error;
ntoc = len;
error = 0;
ccb = cdgetccb(periph, 1);
csio = &ccb->csio;
cam_fill_csio(csio,
1,
cddone,
CAM_DIR_IN,
MSG_SIMPLE_Q_TAG,
data,
len,
SSD_FULL_SIZE,
sizeof(struct scsi_read_toc),
50000);
scsi_cmd = (struct scsi_read_toc *)&csio->cdb_io.cdb_bytes;
bzero (scsi_cmd, sizeof(*scsi_cmd));
if (mode == CD_MSF_FORMAT)
scsi_cmd->byte2 |= CD_MSF;
scsi_cmd->from_track = start;
scsi_cmd->data_len[0] = (ntoc) >> 8;
scsi_cmd->data_len[1] = (ntoc) & 0xff;
scsi_cmd->op_code = READ_TOC;
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA | sense_flags);
xpt_release_ccb(ccb);
return(error);
}
static int
cdreadsubchannel(struct cam_periph *periph, u_int32_t mode,
u_int32_t format, int track,
struct cd_sub_channel_info *data, u_int32_t len)
{
struct scsi_read_subchannel *scsi_cmd;
struct ccb_scsiio *csio;
union ccb *ccb;
int error;
error = 0;
ccb = cdgetccb(periph, 1);
csio = &ccb->csio;
cam_fill_csio(csio,
1,
cddone,
CAM_DIR_IN,
MSG_SIMPLE_Q_TAG,
(u_int8_t *)data,
len,
SSD_FULL_SIZE,
sizeof(struct scsi_read_subchannel),
50000);
scsi_cmd = (struct scsi_read_subchannel *)&csio->cdb_io.cdb_bytes;
bzero (scsi_cmd, sizeof(*scsi_cmd));
scsi_cmd->op_code = READ_SUBCHANNEL;
if (mode == CD_MSF_FORMAT)
scsi_cmd->byte1 |= CD_MSF;
scsi_cmd->byte2 = SRS_SUBQ;
scsi_cmd->subchan_format = format;
scsi_cmd->track = track;
scsi_ulto2b(len, (u_int8_t *)scsi_cmd->data_len);
scsi_cmd->control = 0;
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA);
xpt_release_ccb(ccb);
return(error);
}
static int
cdgetmode(struct cam_periph *periph, struct cd_mode_params *data,
u_int32_t page)
{
struct ccb_scsiio *csio;
struct cd_softc *softc;
union ccb *ccb;
int param_len;
int error;
softc = (struct cd_softc *)periph->softc;
ccb = cdgetccb(periph, 1);
csio = &ccb->csio;
data->cdb_size = softc->minimum_command_size;
if (data->cdb_size < 10)
param_len = sizeof(struct cd_mode_data);
else
param_len = sizeof(struct cd_mode_data_10);
param_len = min(param_len, data->alloc_len);
scsi_mode_sense_len(csio,
1,
cddone,
MSG_SIMPLE_Q_TAG,
0,
SMS_PAGE_CTRL_CURRENT,
page,
data->mode_buf,
param_len,
softc->minimum_command_size,
SSD_FULL_SIZE,
50000);
STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA);
xpt_release_ccb(ccb);
STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
if (error == 0) {
u_int32_t data_len;
if (data->cdb_size == 10) {
struct scsi_mode_header_10 *hdr10;
hdr10 = (struct scsi_mode_header_10 *)data->mode_buf;
data_len = scsi_2btoul(hdr10->data_length);
data_len += sizeof(hdr10->data_length);
} else {
struct scsi_mode_header_6 *hdr6;
hdr6 = (struct scsi_mode_header_6 *)data->mode_buf;
data_len = hdr6->data_length;
data_len += sizeof(hdr6->data_length);
}
if (data_len > data->alloc_len) {
xpt_print(periph->path, "allocated modepage %d length "
"%d < returned length %d\n", page, data->alloc_len,
data_len);
error = ENOSPC;
}
}
return (error);
}
static int
cdsetmode(struct cam_periph *periph, struct cd_mode_params *data)
{
struct ccb_scsiio *csio;
struct cd_softc *softc;
union ccb *ccb;
int cdb_size, param_len;
int error;
softc = (struct cd_softc *)periph->softc;
ccb = cdgetccb(periph, 1);
csio = &ccb->csio;
error = 0;
if (data->cdb_size == 10)
cdb_size = data->cdb_size;
else
cdb_size = softc->minimum_command_size;
if (cdb_size >= 10) {
struct scsi_mode_header_10 *mode_header;
u_int32_t data_len;
mode_header = (struct scsi_mode_header_10 *)data->mode_buf;
data_len = scsi_2btoul(mode_header->data_length);
scsi_ulto2b(0, mode_header->data_length);
mode_header->medium_type = 0;
param_len = data_len + sizeof(mode_header->data_length);
} else {
struct scsi_mode_header_6 *mode_header;
mode_header = (struct scsi_mode_header_6 *)data->mode_buf;
param_len = mode_header->data_length + 1;
mode_header->data_length = 0;
mode_header->medium_type = 0;
}
param_len = min(param_len, data->alloc_len);
scsi_mode_select_len(csio,
1,
cddone,
MSG_SIMPLE_Q_TAG,
1,
0,
data->mode_buf,
param_len,
cdb_size,
SSD_FULL_SIZE,
50000);
STAILQ_INSERT_TAIL(&softc->mode_queue, data, links);
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA);
xpt_release_ccb(ccb);
STAILQ_REMOVE(&softc->mode_queue, data, cd_mode_params, links);
return (error);
}
static int
cdplay(struct cam_periph *periph, u_int32_t blk, u_int32_t len)
{
struct ccb_scsiio *csio;
union ccb *ccb;
int error;
u_int8_t cdb_len;
error = 0;
ccb = cdgetccb(periph, 1);
csio = &ccb->csio;
if ((len & 0xffff0000) == 0) {
struct scsi_play_10 *scsi_cmd;
scsi_cmd = (struct scsi_play_10 *)&csio->cdb_io.cdb_bytes;
bzero (scsi_cmd, sizeof(*scsi_cmd));
scsi_cmd->op_code = PLAY_10;
scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
scsi_ulto2b(len, (u_int8_t *)scsi_cmd->xfer_len);
cdb_len = sizeof(*scsi_cmd);
} else {
struct scsi_play_12 *scsi_cmd;
scsi_cmd = (struct scsi_play_12 *)&csio->cdb_io.cdb_bytes;
bzero (scsi_cmd, sizeof(*scsi_cmd));
scsi_cmd->op_code = PLAY_12;
scsi_ulto4b(blk, (u_int8_t *)scsi_cmd->blk_addr);
scsi_ulto4b(len, (u_int8_t *)scsi_cmd->xfer_len);
cdb_len = sizeof(*scsi_cmd);
}
cam_fill_csio(csio,
2,
cddone,
CAM_DIR_NONE,
MSG_SIMPLE_Q_TAG,
NULL,
0,
SSD_FULL_SIZE,
cdb_len,
50 * 1000);
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA);
xpt_release_ccb(ccb);
return(error);
}
static int
cdplaymsf(struct cam_periph *periph, u_int32_t startm, u_int32_t starts,
u_int32_t startf, u_int32_t endm, u_int32_t ends, u_int32_t endf)
{
struct scsi_play_msf *scsi_cmd;
struct ccb_scsiio *csio;
union ccb *ccb;
int error;
error = 0;
ccb = cdgetccb(periph, 1);
csio = &ccb->csio;
cam_fill_csio(csio,
1,
cddone,
CAM_DIR_NONE,
MSG_SIMPLE_Q_TAG,
NULL,
0,
SSD_FULL_SIZE,
sizeof(struct scsi_play_msf),
50000);
scsi_cmd = (struct scsi_play_msf *)&csio->cdb_io.cdb_bytes;
bzero (scsi_cmd, sizeof(*scsi_cmd));
scsi_cmd->op_code = PLAY_MSF;
scsi_cmd->start_m = startm;
scsi_cmd->start_s = starts;
scsi_cmd->start_f = startf;
scsi_cmd->end_m = endm;
scsi_cmd->end_s = ends;
scsi_cmd->end_f = endf;
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA);
xpt_release_ccb(ccb);
return(error);
}
static int
cdplaytracks(struct cam_periph *periph, u_int32_t strack, u_int32_t sindex,
u_int32_t etrack, u_int32_t eindex)
{
struct scsi_play_track *scsi_cmd;
struct ccb_scsiio *csio;
union ccb *ccb;
int error;
error = 0;
ccb = cdgetccb(periph, 1);
csio = &ccb->csio;
cam_fill_csio(csio,
1,
cddone,
CAM_DIR_NONE,
MSG_SIMPLE_Q_TAG,
NULL,
0,
SSD_FULL_SIZE,
sizeof(struct scsi_play_track),
50000);
scsi_cmd = (struct scsi_play_track *)&csio->cdb_io.cdb_bytes;
bzero (scsi_cmd, sizeof(*scsi_cmd));
scsi_cmd->op_code = PLAY_TRACK;
scsi_cmd->start_track = strack;
scsi_cmd->start_index = sindex;
scsi_cmd->end_track = etrack;
scsi_cmd->end_index = eindex;
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA);
xpt_release_ccb(ccb);
return(error);
}
static int
cdpause(struct cam_periph *periph, u_int32_t go)
{
struct scsi_pause *scsi_cmd;
struct ccb_scsiio *csio;
union ccb *ccb;
int error;
error = 0;
ccb = cdgetccb(periph, 1);
csio = &ccb->csio;
cam_fill_csio(csio,
1,
cddone,
CAM_DIR_NONE,
MSG_SIMPLE_Q_TAG,
NULL,
0,
SSD_FULL_SIZE,
sizeof(struct scsi_pause),
50000);
scsi_cmd = (struct scsi_pause *)&csio->cdb_io.cdb_bytes;
bzero (scsi_cmd, sizeof(*scsi_cmd));
scsi_cmd->op_code = PAUSE;
scsi_cmd->resume = go;
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA);
xpt_release_ccb(ccb);
return(error);
}
static int
cdstartunit(struct cam_periph *periph, int load)
{
union ccb *ccb;
int error;
error = 0;
ccb = cdgetccb(periph, 1);
scsi_start_stop(&ccb->csio,
1,
cddone,
MSG_SIMPLE_Q_TAG,
TRUE,
load,
FALSE,
SSD_FULL_SIZE,
50000);
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA);
xpt_release_ccb(ccb);
return(error);
}
static int
cdstopunit(struct cam_periph *periph, u_int32_t eject)
{
union ccb *ccb;
int error;
error = 0;
ccb = cdgetccb(periph, 1);
scsi_start_stop(&ccb->csio,
1,
cddone,
MSG_SIMPLE_Q_TAG,
FALSE,
eject,
FALSE,
SSD_FULL_SIZE,
50000);
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA);
xpt_release_ccb(ccb);
return(error);
}
static int
cdsetspeed(struct cam_periph *periph, u_int32_t rdspeed, u_int32_t wrspeed)
{
struct scsi_set_speed *scsi_cmd;
struct ccb_scsiio *csio;
union ccb *ccb;
int error;
error = 0;
ccb = cdgetccb(periph, 1);
csio = &ccb->csio;
if (rdspeed < 177)
rdspeed *= 177;
if (wrspeed < 177)
wrspeed *= 177;
cam_fill_csio(csio,
1,
cddone,
CAM_DIR_NONE,
MSG_SIMPLE_Q_TAG,
NULL,
0,
SSD_FULL_SIZE,
sizeof(struct scsi_set_speed),
50000);
scsi_cmd = (struct scsi_set_speed *)&csio->cdb_io.cdb_bytes;
bzero(scsi_cmd, sizeof(*scsi_cmd));
scsi_cmd->opcode = SET_CD_SPEED;
scsi_ulto2b(rdspeed, scsi_cmd->readspeed);
scsi_ulto2b(wrspeed, scsi_cmd->writespeed);
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA);
xpt_release_ccb(ccb);
return(error);
}
static int
cdreportkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
{
union ccb *ccb;
u_int8_t *databuf;
u_int32_t lba;
int error;
int length;
error = 0;
databuf = NULL;
lba = 0;
ccb = cdgetccb(periph, 1);
switch (authinfo->format) {
case DVD_REPORT_AGID:
length = sizeof(struct scsi_report_key_data_agid);
break;
case DVD_REPORT_CHALLENGE:
length = sizeof(struct scsi_report_key_data_challenge);
break;
case DVD_REPORT_KEY1:
length = sizeof(struct scsi_report_key_data_key1_key2);
break;
case DVD_REPORT_TITLE_KEY:
length = sizeof(struct scsi_report_key_data_title);
lba = authinfo->lba;
break;
case DVD_REPORT_ASF:
length = sizeof(struct scsi_report_key_data_asf);
break;
case DVD_REPORT_RPC:
length = sizeof(struct scsi_report_key_data_rpc);
break;
case DVD_INVALIDATE_AGID:
length = 0;
break;
default:
error = EINVAL;
goto bailout;
break;
}
if (length != 0) {
databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
} else {
databuf = NULL;
}
scsi_report_key(&ccb->csio,
1,
cddone,
MSG_SIMPLE_Q_TAG,
lba,
authinfo->agid,
authinfo->format,
databuf,
length,
SSD_FULL_SIZE,
50000);
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA);
if (error != 0)
goto bailout;
if (ccb->csio.resid != 0) {
xpt_print(periph->path, "warning, residual for report key "
"command is %d\n", ccb->csio.resid);
}
switch(authinfo->format) {
case DVD_REPORT_AGID: {
struct scsi_report_key_data_agid *agid_data;
agid_data = (struct scsi_report_key_data_agid *)databuf;
authinfo->agid = (agid_data->agid & RKD_AGID_MASK) >>
RKD_AGID_SHIFT;
break;
}
case DVD_REPORT_CHALLENGE: {
struct scsi_report_key_data_challenge *chal_data;
chal_data = (struct scsi_report_key_data_challenge *)databuf;
bcopy(chal_data->challenge_key, authinfo->keychal,
min(sizeof(chal_data->challenge_key),
sizeof(authinfo->keychal)));
break;
}
case DVD_REPORT_KEY1: {
struct scsi_report_key_data_key1_key2 *key1_data;
key1_data = (struct scsi_report_key_data_key1_key2 *)databuf;
bcopy(key1_data->key1, authinfo->keychal,
min(sizeof(key1_data->key1), sizeof(authinfo->keychal)));
break;
}
case DVD_REPORT_TITLE_KEY: {
struct scsi_report_key_data_title *title_data;
title_data = (struct scsi_report_key_data_title *)databuf;
authinfo->cpm = (title_data->byte0 & RKD_TITLE_CPM) >>
RKD_TITLE_CPM_SHIFT;
authinfo->cp_sec = (title_data->byte0 & RKD_TITLE_CP_SEC) >>
RKD_TITLE_CP_SEC_SHIFT;
authinfo->cgms = (title_data->byte0 & RKD_TITLE_CMGS_MASK) >>
RKD_TITLE_CMGS_SHIFT;
bcopy(title_data->title_key, authinfo->keychal,
min(sizeof(title_data->title_key),
sizeof(authinfo->keychal)));
break;
}
case DVD_REPORT_ASF: {
struct scsi_report_key_data_asf *asf_data;
asf_data = (struct scsi_report_key_data_asf *)databuf;
authinfo->asf = asf_data->success & RKD_ASF_SUCCESS;
break;
}
case DVD_REPORT_RPC: {
struct scsi_report_key_data_rpc *rpc_data;
rpc_data = (struct scsi_report_key_data_rpc *)databuf;
authinfo->reg_type = (rpc_data->byte4 & RKD_RPC_TYPE_MASK) >>
RKD_RPC_TYPE_SHIFT;
authinfo->vend_rsts =
(rpc_data->byte4 & RKD_RPC_VENDOR_RESET_MASK) >>
RKD_RPC_VENDOR_RESET_SHIFT;
authinfo->user_rsts = rpc_data->byte4 & RKD_RPC_USER_RESET_MASK;
authinfo->region = rpc_data->region_mask;
authinfo->rpc_scheme = rpc_data->rpc_scheme1;
break;
}
case DVD_INVALIDATE_AGID:
break;
default:
error = EINVAL;
goto bailout;
break;
}
bailout:
if (databuf != NULL)
kfree(databuf, M_DEVBUF);
xpt_release_ccb(ccb);
return(error);
}
static int
cdsendkey(struct cam_periph *periph, struct dvd_authinfo *authinfo)
{
union ccb *ccb;
u_int8_t *databuf;
int length;
int error;
error = 0;
databuf = NULL;
ccb = cdgetccb(periph, 1);
switch(authinfo->format) {
case DVD_SEND_CHALLENGE: {
struct scsi_report_key_data_challenge *challenge_data;
length = sizeof(*challenge_data);
challenge_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
databuf = (u_int8_t *)challenge_data;
scsi_ulto2b(length - sizeof(challenge_data->data_len),
challenge_data->data_len);
bcopy(authinfo->keychal, challenge_data->challenge_key,
min(sizeof(authinfo->keychal),
sizeof(challenge_data->challenge_key)));
break;
}
case DVD_SEND_KEY2: {
struct scsi_report_key_data_key1_key2 *key2_data;
length = sizeof(*key2_data);
key2_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
databuf = (u_int8_t *)key2_data;
scsi_ulto2b(length - sizeof(key2_data->data_len),
key2_data->data_len);
bcopy(authinfo->keychal, key2_data->key1,
min(sizeof(authinfo->keychal), sizeof(key2_data->key1)));
break;
}
case DVD_SEND_RPC: {
struct scsi_send_key_data_rpc *rpc_data;
length = sizeof(*rpc_data);
rpc_data = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
databuf = (u_int8_t *)rpc_data;
scsi_ulto2b(length - sizeof(rpc_data->data_len),
rpc_data->data_len);
rpc_data->region_code = authinfo->region;
break;
}
default:
error = EINVAL;
goto bailout;
break;
}
scsi_send_key(&ccb->csio,
1,
cddone,
MSG_SIMPLE_Q_TAG,
authinfo->agid,
authinfo->format,
databuf,
length,
SSD_FULL_SIZE,
50000);
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA);
bailout:
if (databuf != NULL)
kfree(databuf, M_DEVBUF);
xpt_release_ccb(ccb);
return(error);
}
static int
cdreaddvdstructure(struct cam_periph *periph, struct dvd_struct *dvdstruct)
{
union ccb *ccb;
u_int8_t *databuf;
u_int32_t address;
int error;
int length;
error = 0;
databuf = NULL;
address = 0;
ccb = cdgetccb(periph, 1);
switch(dvdstruct->format) {
case DVD_STRUCT_PHYSICAL:
length = sizeof(struct scsi_read_dvd_struct_data_physical);
break;
case DVD_STRUCT_COPYRIGHT:
length = sizeof(struct scsi_read_dvd_struct_data_copyright);
break;
case DVD_STRUCT_DISCKEY:
length = sizeof(struct scsi_read_dvd_struct_data_disc_key);
break;
case DVD_STRUCT_BCA:
length = sizeof(struct scsi_read_dvd_struct_data_bca);
break;
case DVD_STRUCT_MANUFACT:
length = sizeof(struct scsi_read_dvd_struct_data_manufacturer);
break;
case DVD_STRUCT_CMI:
error = ENODEV;
goto bailout;
#ifdef notyet
length = sizeof(struct scsi_read_dvd_struct_data_copy_manage);
address = dvdstruct->address;
#endif
break;
case DVD_STRUCT_PROTDISCID:
length = sizeof(struct scsi_read_dvd_struct_data_prot_discid);
break;
case DVD_STRUCT_DISCKEYBLOCK:
length = sizeof(struct scsi_read_dvd_struct_data_disc_key_blk);
break;
case DVD_STRUCT_DDS:
length = sizeof(struct scsi_read_dvd_struct_data_dds);
break;
case DVD_STRUCT_MEDIUM_STAT:
length = sizeof(struct scsi_read_dvd_struct_data_medium_status);
break;
case DVD_STRUCT_SPARE_AREA:
length = sizeof(struct scsi_read_dvd_struct_data_spare_area);
break;
case DVD_STRUCT_RMD_LAST:
error = ENODEV;
goto bailout;
#ifdef notyet
length = sizeof(struct scsi_read_dvd_struct_data_rmd_borderout);
address = dvdstruct->address;
#endif
break;
case DVD_STRUCT_RMD_RMA:
error = ENODEV;
goto bailout;
#ifdef notyet
length = sizeof(struct scsi_read_dvd_struct_data_rmd);
address = dvdstruct->address;
#endif
break;
case DVD_STRUCT_PRERECORDED:
length = sizeof(struct scsi_read_dvd_struct_data_leadin);
break;
case DVD_STRUCT_UNIQUEID:
length = sizeof(struct scsi_read_dvd_struct_data_disc_id);
break;
case DVD_STRUCT_DCB:
error = ENODEV;
goto bailout;
#ifdef notyet
length = sizeof(struct scsi_read_dvd_struct_data_dcb);
address = dvdstruct->address;
#endif
break;
case DVD_STRUCT_LIST:
length = 65535;
break;
default:
error = EINVAL;
goto bailout;
break;
}
if (length != 0) {
databuf = kmalloc(length, M_DEVBUF, M_INTWAIT | M_ZERO);
} else {
databuf = NULL;
}
scsi_read_dvd_structure(&ccb->csio,
1,
cddone,
MSG_SIMPLE_Q_TAG,
address,
dvdstruct->layer_num,
dvdstruct->format,
dvdstruct->agid,
databuf,
length,
SSD_FULL_SIZE,
50000);
error = cdrunccb(ccb, cderror, CAM_RETRY_SELTO,
SF_RETRY_UA);
if (error != 0)
goto bailout;
switch(dvdstruct->format) {
case DVD_STRUCT_PHYSICAL: {
struct scsi_read_dvd_struct_data_layer_desc *inlayer;
struct dvd_layer *outlayer;
struct scsi_read_dvd_struct_data_physical *phys_data;
phys_data =
(struct scsi_read_dvd_struct_data_physical *)databuf;
inlayer = &phys_data->layer_desc;
outlayer = (struct dvd_layer *)&dvdstruct->data;
dvdstruct->length = sizeof(*inlayer);
outlayer->book_type = (inlayer->book_type_version &
RDSD_BOOK_TYPE_MASK) >> RDSD_BOOK_TYPE_SHIFT;
outlayer->book_version = (inlayer->book_type_version &
RDSD_BOOK_VERSION_MASK);
outlayer->disc_size = (inlayer->disc_size_max_rate &
RDSD_DISC_SIZE_MASK) >> RDSD_DISC_SIZE_SHIFT;
outlayer->max_rate = (inlayer->disc_size_max_rate &
RDSD_MAX_RATE_MASK);
outlayer->nlayers = (inlayer->layer_info &
RDSD_NUM_LAYERS_MASK) >> RDSD_NUM_LAYERS_SHIFT;
outlayer->track_path = (inlayer->layer_info &
RDSD_TRACK_PATH_MASK) >> RDSD_TRACK_PATH_SHIFT;
outlayer->layer_type = (inlayer->layer_info &
RDSD_LAYER_TYPE_MASK);
outlayer->linear_density = (inlayer->density &
RDSD_LIN_DENSITY_MASK) >> RDSD_LIN_DENSITY_SHIFT;
outlayer->track_density = (inlayer->density &
RDSD_TRACK_DENSITY_MASK);
outlayer->bca = (inlayer->bca & RDSD_BCA_MASK) >>
RDSD_BCA_SHIFT;
outlayer->start_sector = scsi_3btoul(inlayer->main_data_start);
outlayer->end_sector = scsi_3btoul(inlayer->main_data_end);
outlayer->end_sector_l0 =
scsi_3btoul(inlayer->end_sector_layer0);
break;
}
case DVD_STRUCT_COPYRIGHT: {
struct scsi_read_dvd_struct_data_copyright *copy_data;
copy_data = (struct scsi_read_dvd_struct_data_copyright *)
databuf;
dvdstruct->cpst = copy_data->cps_type;
dvdstruct->rmi = copy_data->region_info;
dvdstruct->length = 0;
break;
}
default:
dvdstruct->length = length - ccb->csio.resid -
sizeof(struct scsi_read_dvd_struct_data_header);
bcopy(databuf + sizeof(struct scsi_read_dvd_struct_data_header),
dvdstruct->data,
min(sizeof(dvdstruct->data), dvdstruct->length));
break;
}
bailout:
if (databuf != NULL)
kfree(databuf, M_DEVBUF);
xpt_release_ccb(ccb);
return(error);
}
void
scsi_report_key(struct ccb_scsiio *csio, u_int32_t retries,
void (*cbfcnp)(struct cam_periph *, union ccb *),
u_int8_t tag_action, u_int32_t lba, u_int8_t agid,
u_int8_t key_format, u_int8_t *data_ptr, u_int32_t dxfer_len,
u_int8_t sense_len, u_int32_t timeout)
{
struct scsi_report_key *scsi_cmd;
scsi_cmd = (struct scsi_report_key *)&csio->cdb_io.cdb_bytes;
bzero(scsi_cmd, sizeof(*scsi_cmd));
scsi_cmd->opcode = REPORT_KEY;
scsi_ulto4b(lba, scsi_cmd->lba);
scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
(key_format & RK_KF_KEYFORMAT_MASK);
cam_fill_csio(csio,
retries,
cbfcnp,
(dxfer_len == 0) ? CAM_DIR_NONE : CAM_DIR_IN,
tag_action,
data_ptr,
dxfer_len,
sense_len,
sizeof(*scsi_cmd),
timeout);
}
void
scsi_send_key(struct ccb_scsiio *csio, u_int32_t retries,
void (*cbfcnp)(struct cam_periph *, union ccb *),
u_int8_t tag_action, u_int8_t agid, u_int8_t key_format,
u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
u_int32_t timeout)
{
struct scsi_send_key *scsi_cmd;
scsi_cmd = (struct scsi_send_key *)&csio->cdb_io.cdb_bytes;
bzero(scsi_cmd, sizeof(*scsi_cmd));
scsi_cmd->opcode = SEND_KEY;
scsi_ulto2b(dxfer_len, scsi_cmd->param_len);
scsi_cmd->agid_keyformat = (agid << RK_KF_AGID_SHIFT) |
(key_format & RK_KF_KEYFORMAT_MASK);
cam_fill_csio(csio,
retries,
cbfcnp,
CAM_DIR_OUT,
tag_action,
data_ptr,
dxfer_len,
sense_len,
sizeof(*scsi_cmd),
timeout);
}
void
scsi_read_dvd_structure(struct ccb_scsiio *csio, u_int32_t retries,
void (*cbfcnp)(struct cam_periph *, union ccb *),
u_int8_t tag_action, u_int32_t address,
u_int8_t layer_number, u_int8_t format, u_int8_t agid,
u_int8_t *data_ptr, u_int32_t dxfer_len,
u_int8_t sense_len, u_int32_t timeout)
{
struct scsi_read_dvd_structure *scsi_cmd;
scsi_cmd = (struct scsi_read_dvd_structure *)&csio->cdb_io.cdb_bytes;
bzero(scsi_cmd, sizeof(*scsi_cmd));
scsi_cmd->opcode = READ_DVD_STRUCTURE;
scsi_ulto4b(address, scsi_cmd->address);
scsi_cmd->layer_number = layer_number;
scsi_cmd->format = format;
scsi_ulto2b(dxfer_len, scsi_cmd->alloc_len);
scsi_cmd->agid = agid << 6;
cam_fill_csio(csio,
retries,
cbfcnp,
CAM_DIR_IN,
tag_action,
data_ptr,
dxfer_len,
sense_len,
sizeof(*scsi_cmd),
timeout);
}