/* @(#)scsi_mmc.c	1.18 06/06/05 Copyright 2002-2006 J. Schilling */
#ifndef lint
static	char sccsid[] =
	"@(#)scsi_mmc.c	1.18 06/06/05 Copyright 2002-2006 J. Schilling";
#endif
/*
 *	SCSI command functions for cdrecord
 *	covering MMC-3 level and above
 *
 *	Copyright (c) 2002-2006 J. Schilling
 */
/*
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * See the file CDDL.Schily.txt in this distribution for details.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file CDDL.Schily.txt from this distribution.
 */

/*#define	DEBUG*/

#include <mconfig.h>

#include <stdio.h>
#include <standard.h>
#include <stdxlib.h>
#include <unixstd.h>
#include <fctldefs.h>
#include <errno.h>
#include <strdefs.h>
#include <timedefs.h>

#include <utypes.h>
#include <btorder.h>
#include <intcvt.h>
#include <schily.h>

#include <scg/scgcmd.h>
#include <scg/scsidefs.h>
#include <scg/scsireg.h>
#include <scg/scsitransp.h>

#include "scsimmc.h"
#include "cdrecord.h"

extern	int	xdebug;

LOCAL struct features {
	UInt16_t	code;
	char		*name;
} fl[] = {
	{ 0x0000,	"Profile List", },
	{ 0x0001,	"Core", },
	{ 0x0002,	"Morphing", },
	{ 0x0003,	"Removable Medium", },
	{ 0x0004,	"Write Protect", },

	{ 0x0010,	"Random Readable", },

	{ 0x001D,	"Multi Read", },
	{ 0x001E,	"CD Read", },
	{ 0x001F,	"DVD Read", },
	{ 0x0020,	"Random Writable", },
	{ 0x0021,	"Incremental Streaming Writable", },
	{ 0x0022,	"Sector Erasable", },
	{ 0x0023,	"Formattable", },
	{ 0x0024,	"Defect Management", },
	{ 0x0025,	"Write Once", },
	{ 0x0026,	"Restricted Overwrite", },
	{ 0x0027,	"CD-RW CAV Write", },
	{ 0x0028,	"MRW", },
	{ 0x0029,	"Reserved", },
	{ 0x002A,	"DVD+RW", },
	{ 0x002B,	"DVD+R", },
	{ 0x002C,	"Rigid Restricted Overwrite", },
	{ 0x002D,	"CD Track at Once", },
	{ 0x002E,	"CD Mastering", },
	{ 0x002F,	"DVD-R/-RW Write", },
	{ 0x0030,	"DDCD Read", },
	{ 0x0031,	"DDCD-R Write", },
	{ 0x0032,	"DDCD-RW Write", },

	{ 0x0037,	"CD-RW Write", },

	{ 0x003B,	"DVD+R/DL Read", },

	{ 0x0100,	"Power Management", },
	{ 0x0101,	"S.M.A.R.T.", },
	{ 0x0102,	"Embedded Changer", },
	{ 0x0103,	"CD Audio analog play", },
	{ 0x0104,	"Microcode Upgrade", },
	{ 0x0105,	"Time-out", },
	{ 0x0106,	"DVD-CSS", },
	{ 0x0107,	"Real Time Streaming", },
	{ 0x0108,	"Logical Unit Serial Number", },
	{ 0x0109,	"Reserved (109)", },
	{ 0x010A,	"Disk Control Blocks", },
	{ 0x010B,	"DVD CPRM", },
};

LOCAL struct profiles {
	UInt16_t	code;
	char		*name;
} pl[] = {
	{ 0x0000,	"Reserved", },
	{ 0x0001,	"Non -removable Disk", },
	{ 0x0002,	"Removable Disk", },
	{ 0x0003,	"MO Erasable", },
	{ 0x0004,	"MO Write Once", },
	{ 0x0005,	"AS-MO", },

	/* 0x06..0x07 is reserved */

	{ 0x0008,	"CD-ROM", },
	{ 0x0009,	"CD-R", },
	{ 0x000A,	"CD-RW", },

	/* 0x0B..0x0F is reserved */

	{ 0x0010,	"DVD-ROM", },
	{ 0x0011,	"DVD-R sequential recording", },
	{ 0x0012,	"DVD-RAM", },
	{ 0x0013,	"DVD-RW restricted overwrite", },
	{ 0x0014,	"DVD-RW sequential overwrite", },
	{ 0x0015,	"DVD-R/DL sequential recording", },
	{ 0x0016,	"DVD-R/DL layer jump recording", },

	/* 0x17..0x19 is reserved */

	{ 0x001A,	"DVD+RW", },
	{ 0x001B,	"DVD+R", },

	{ 0x0020,	"DDCD-ROM", },
	{ 0x0021,	"DDCD-R", },
	{ 0x0022,	"DDCD-RW", },

	{ 0x002B,	"DVD+R/DL", },

	{ 0x0040,	"BD-ROM", },
	{ 0x0041,	"BD-R sequential recording", },
	{ 0x0042,	"BD-R random recording", },
	{ 0x0043,	"BD-RE", },

	/* 0x44..0x4F is reserved */

	{ 0x0050,	"HD DVD-ROM", },
	{ 0x0051,	"HD DVD-R", },
	{ 0x0052,	"HD DVD-RAM", },

	{ 0xFFFF,	"No standard Profile", },
};


EXPORT	int	get_configuration	__PR((SCSI *scgp, caddr_t bp, int cnt, int st_feature, int rt));
LOCAL	int	get_conflen		__PR((SCSI *scgp, int st_feature, int rt));
EXPORT	int	get_curprofile		__PR((SCSI *scgp));
LOCAL	int	get_profiles		__PR((SCSI *scgp, caddr_t bp, int cnt));
EXPORT	int	print_profiles		__PR((SCSI *scgp));
EXPORT	int	get_proflist		__PR((SCSI *scgp, BOOL *wp, BOOL *cdp, BOOL *dvdp, BOOL *dvdplusp, BOOL *ddcdp));
EXPORT	int	get_wproflist		__PR((SCSI *scgp, BOOL *cdp, BOOL *dvdp,
							BOOL *dvdplusp, BOOL *ddcdp));
LOCAL	int	scsi_get_performance	__PR((SCSI *scgp, caddr_t bp, int cnd, int ndesc, int type, int datatype));
EXPORT	int	scsi_get_perf_maxspeed	__PR((SCSI *scgp, Ulong *readp, Ulong *writep, Ulong *endp));
EXPORT	int	scsi_get_perf_curspeed	__PR((SCSI *scgp, Ulong *readp, Ulong *writep, Ulong *endp));
LOCAL	int	scsi_set_streaming	__PR((SCSI *scgp, Ulong *readp, Ulong *writep, Ulong *endp));
EXPORT	int	speed_select_mdvd	__PR((SCSI *scgp, int readspeed, int writespeed));
LOCAL	char	*fname			__PR((Uint code));
LOCAL	char	*pname			__PR((Uint code));
LOCAL	BOOL	pname_known		__PR((Uint code));
EXPORT	void	gconf			__PR((SCSI *scgp));
EXPORT	void	print_format_capacities	__PR((SCSI *scgp));
EXPORT	int	get_format_capacities	__PR((SCSI *scgp, caddr_t bp, int cnt));
EXPORT	int	read_format_capacities	__PR((SCSI *scgp, caddr_t bp, int cnt));

/*
 * Get feature codes
 */
EXPORT int
get_configuration(scgp, bp, cnt, st_feature, rt)
	SCSI	*scgp;
	caddr_t	bp;
	int	cnt;
	int	st_feature;
	int	rt;
{
	register struct	scg_cmd	*scmd = scgp->scmd;

	fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
	scmd->addr = bp;
	scmd->size = cnt;
	scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
	scmd->cdb_len = SC_G1_CDBLEN;
	scmd->sense_len = CCS_SENSE_LEN;
	scmd->cdb.g1_cdb.cmd = 0x46;
	scmd->cdb.g1_cdb.lun = scg_lun(scgp);
	if (rt & 1)
		scmd->cdb.g1_cdb.reladr  = 1;
	if (rt & 2)
		scmd->cdb.g1_cdb.res  = 1;

	i_to_2_byte(scmd->cdb.g1_cdb.addr, st_feature);
	g1_cdblen(&scmd->cdb.g1_cdb, cnt);

	scgp->cmdname = "get_configuration";

	return (scg_cmd(scgp));
}

/*
 * Retrieve feature code list length
 */
LOCAL int
get_conflen(scgp, st_feature, rt)
	SCSI	*scgp;
	int	st_feature;
	int	rt;
{
	Uchar	cbuf[8];
	int	flen;
	int	i;

	fillbytes(cbuf, sizeof (cbuf), '\0');
	scgp->silent++;
	i = get_configuration(scgp, (char *)cbuf, sizeof (cbuf), st_feature, rt);
	scgp->silent--;
	if (i < 0)
		return (-1);
	i = sizeof (cbuf) - scg_getresid(scgp);
	if (i < 4)
		return (-1);

	flen = a_to_u_4_byte(cbuf);
	if (flen < 4)
		return (-1);
	return (flen);
}

EXPORT int
get_curprofile(scgp)
	SCSI	*scgp;
{
	Uchar	cbuf[8];
	int	amt;
	int	flen;
	int	profile;
	int	i;

	fillbytes(cbuf, sizeof (cbuf), '\0');
	scgp->silent++;
	i = get_configuration(scgp, (char *)cbuf, sizeof (cbuf), 0, 0);
	scgp->silent--;
	if (i < 0)
		return (-1);

	amt = sizeof (cbuf) - scg_getresid(scgp);
	if (amt < 8)
		return (-1);
	flen = a_to_u_4_byte(cbuf);
	if (flen < 4)
		return (-1);

	profile = a_to_u_2_byte(&cbuf[6]);

	if (xdebug > 1)
		scg_prbytes("Features: ", cbuf, amt);

	if (xdebug > 0)
		printf("feature len: %d current profile 0x%04X len %d\n",
				flen, profile, amt);

	return (profile);
}

LOCAL int
get_profiles(scgp, bp, cnt)
	SCSI	*scgp;
	caddr_t	bp;
	int	cnt;
{
	int	amt;
	int	flen;
	int	i;

	flen = get_conflen(scgp, 0, 0);
	if (flen < 0)
		return (-1);
	if (cnt < flen)
		flen = cnt;

	fillbytes(bp, cnt, '\0');
	scgp->silent++;
	i = get_configuration(scgp, (char *)bp, flen, 0, 0);
	scgp->silent--;
	if (i < 0)
		return (-1);
	amt = flen - scg_getresid(scgp);

	flen = a_to_u_4_byte(bp);
	if ((flen+4) < amt)
		amt = flen+4;

	return (amt);
}

EXPORT int
print_profiles(scgp)
	SCSI	*scgp;
{
	Uchar	cbuf[1024];
	Uchar	*p;
	int	flen;
	int	curprofile;
	int	profile;
	int	i;
	int	n;

	flen = get_profiles(scgp, (caddr_t)cbuf, sizeof (cbuf));
	if (flen < 0)
		return (-1);

	p = cbuf;
	if (xdebug > 1)
		scg_prbytes("Features: ", cbuf, flen);

	curprofile = a_to_u_2_byte(&p[6]);
	if (xdebug > 0)
		printf("feature len: %d current profile 0x%04X\n",
				flen, curprofile);

	if (pname_known(curprofile))
		printf("Current: %s\n", curprofile == 0 ? "none" : pname(curprofile));
	else
		printf("Current: 0x%04X unknown\n", curprofile);

	p += 8;		/* Skip feature header	*/
	n = p[3];	/* Additional length	*/
	n /= 4;
	p += 4;

	for (i = 0; i < n; i++) {
		profile = a_to_u_2_byte(p);
		if (xdebug > 0)
			printf("Profile: 0x%04X ", profile);
		else
			printf("Profile: ");
		if (pname_known(profile))
			printf("%s %s\n", pname(profile), p[2] & 1 ? "(current)":"");
		else
			printf("0x%04X %s\n", profile, p[2] & 1 ? "(current)":"");
		p += 4;
	}
	return (curprofile);
}

EXPORT int
get_proflist(scgp, wp, cdp, dvdp, dvdplusp, ddcdp)
	SCSI	*scgp;
	BOOL	*wp;
	BOOL	*cdp;
	BOOL	*dvdp;
	BOOL	*dvdplusp;
	BOOL	*ddcdp;
{
	Uchar	cbuf[1024];
	Uchar	*p;
	int	flen;
	int	curprofile;
	int	profile;
	int	i;
	int	n;
	BOOL	wr	= FALSE;
	BOOL	cd	= FALSE;
	BOOL	dvd	= FALSE;
	BOOL	dvdplus	= FALSE;
	BOOL	ddcd	= FALSE;

	flen = get_profiles(scgp, (caddr_t)cbuf, sizeof (cbuf));
	if (flen < 0)
		return (-1);

	p = cbuf;
	if (xdebug > 1)
		scg_prbytes("Features: ", cbuf, flen);

	curprofile = a_to_u_2_byte(&p[6]);
	if (xdebug > 0)
		printf("feature len: %d current profile 0x%04X\n",
				flen, curprofile);

	p += 8;		/* Skip feature header	*/
	n = p[3];	/* Additional length	*/
	n /= 4;
	p += 4;

	for (i = 0; i < n; i++) {
		profile = a_to_u_2_byte(p);
		p += 4;
		if (profile >= 0x0008 && profile < 0x0010)
			cd = TRUE;
		if (profile > 0x0008 && profile < 0x0010)
			wr = TRUE;

		if (profile >= 0x0010 && profile < 0x0018)
			dvd = TRUE;
		if (profile > 0x0010 && profile < 0x0018)
			wr = TRUE;

		if (profile >= 0x0018 && profile < 0x0020)
			dvdplus = TRUE;
		if (profile > 0x0018 && profile < 0x0020)
			wr = TRUE;

		if (profile >= 0x0020 && profile < 0x0028)
			ddcd = TRUE;
		if (profile > 0x0020 && profile < 0x0028)
			wr = TRUE;
	}
	if (wp)
		*wp	= wr;
	if (cdp)
		*cdp	= cd;
	if (dvdp)
		*dvdp	= dvd;
	if (dvdplusp)
		*dvdplusp = dvdplus;
	if (ddcdp)
		*ddcdp	= ddcd;

	return (curprofile);
}

EXPORT int
get_wproflist(scgp, cdp, dvdp, dvdplusp, ddcdp)
	SCSI	*scgp;
	BOOL	*cdp;
	BOOL	*dvdp;
	BOOL	*dvdplusp;
	BOOL	*ddcdp;
{
	Uchar	cbuf[1024];
	Uchar	*p;
	int	flen;
	int	curprofile;
	int	profile;
	int	i;
	int	n;
	BOOL	cd	= FALSE;
	BOOL	dvd	= FALSE;
	BOOL	dvdplus	= FALSE;
	BOOL	ddcd	= FALSE;

	flen = get_profiles(scgp, (caddr_t)cbuf, sizeof (cbuf));
	if (flen < 0)
		return (-1);
	p = cbuf;
	curprofile = a_to_u_2_byte(&p[6]);

	p += 8;		/* Skip feature header	*/
	n = p[3];	/* Additional length	*/
	n /= 4;
	p += 4;

	for (i = 0; i < n; i++) {
		profile = a_to_u_2_byte(p);
		p += 4;
		if (profile > 0x0008 && profile < 0x0010)
			cd = TRUE;
		if (profile > 0x0010 && profile < 0x0018)
			dvd = TRUE;
		if (profile > 0x0018 && profile < 0x0020)
			dvdplus = TRUE;
		if (profile > 0x0020 && profile < 0x0028)
			ddcd = TRUE;
	}
	if (cdp)
		*cdp	= cd;
	if (dvdp)
		*dvdp	= dvd;
	if (dvdplusp)
		*dvdplusp = dvdplus;
	if (ddcdp)
		*ddcdp	= ddcd;

	return (curprofile);
}

LOCAL int
scsi_get_performance(scgp, bp, cnt, ndesc, type, datatype)
	SCSI	*scgp;
	caddr_t	bp;
	int	cnt;
	int	ndesc;
	int	type;
	int	datatype;
{
	register struct	scg_cmd	*scmd = scgp->scmd;

	fillbytes((caddr_t) scmd, sizeof (*scmd), '\0');
	scmd->addr = bp;
	scmd->size = cnt;
	scmd->flags = SCG_RECV_DATA | SCG_DISRE_ENA;
	scmd->cdb_len = SC_G5_CDBLEN;
	scmd->sense_len = CCS_SENSE_LEN;
	scmd->cdb.g1_cdb.cmd = 0xAC;
	scmd->cdb.g1_cdb.lun = scg_lun(scgp);
	scmd->cdb.cmd_cdb[1] |= datatype & 0x1F;
	scmd->cdb.cmd_cdb[9] = ndesc;
	scmd->cdb.cmd_cdb[10] = type;

	scgp->cmdname = "get performance";

	return (scg_cmd(scgp));
}


EXPORT int
scsi_get_perf_maxspeed(scgp, readp, writep, endp)
	SCSI	*scgp;
	Ulong	*readp;
	Ulong	*writep;
	Ulong	*endp;
{
	register struct	scg_cmd	*scmd = scgp->scmd;
	struct mmc_performance_header	*ph;
	struct mmc_write_speed		*wsp;
/*	char buffer[128];*/
	char buffer[8 + 100*16];
	Ulong	ul;
#ifdef	DEBUG
	int	i;
#endif

	fillbytes((caddr_t) buffer, sizeof (buffer), '\0');
	if (scsi_get_performance(scgp, buffer, 104, 100, 0x03, 0) < 0)
		return (-1);

#ifdef	DEBUG
error("Bytes: %d\n", scmd->size - scg_getresid(scgp));
error("header: %ld\n", a_to_4_byte(buffer) + 4);
#endif

	ph = (struct mmc_performance_header *)buffer;
	wsp = (struct mmc_write_speed *)(((char *)ph) +
				sizeof (struct mmc_performance_header));

	ul = a_to_u_4_byte(wsp->end_lba);
	if (endp)
		*endp = ul;

	ul = a_to_u_4_byte(wsp->read_speed);
	if (readp)
		*readp = ul;

	ul = a_to_u_4_byte(wsp->write_speed);
	if (writep)
		*writep = ul;

	wsp = (struct mmc_write_speed *)(((char *)ph) +
				sizeof (struct mmc_performance_header));

#ifdef	DEBUG
	i = (a_to_4_byte(buffer) -4)/sizeof (struct mmc_write_speed);
	if (i > scmd->cdb.cmd_cdb[9])
		i = scmd->cdb.cmd_cdb[9];
	error("Nperf:       %d\n", i);
	for (; --i >= 0; wsp++) {
		ul = a_to_u_4_byte(wsp->end_lba);
		error("End LBA:     %7lu\n", ul);
		ul = a_to_u_4_byte(wsp->read_speed);
		error("Read Speed:  %7lu == %lux\n", ul, ul/1385);
		ul = a_to_u_4_byte(wsp->write_speed);
		error("Write Speed: %7lu == %lux\n", ul, ul/1385);
		error("\n");
	}
	scg_prbytes("Performance data:", (Uchar *)buffer, scmd->size - scg_getresid(scgp));
#endif

	return (0);
}

EXPORT int
scsi_get_perf_curspeed(scgp, readp, writep, endp)
	SCSI	*scgp;
	Ulong	*readp;
	Ulong	*writep;
	Ulong	*endp;
{
	register struct	scg_cmd	*scmd = scgp->scmd;
	struct mmc_performance_header	*ph;
	struct mmc_performance		*perfp;
	char buffer[8 + 100*16];
	Ulong	ul;
	Ulong	end;
	Ulong	speed;
	int	i;

	if (endp || writep) {
		fillbytes((caddr_t) buffer, sizeof (buffer), '\0');
		if (scsi_get_performance(scgp, buffer, 8+16, 1, 0x00, 0x04) < 0)
			return (-1);

		ph = (struct mmc_performance_header *)buffer;
		i = (a_to_4_byte(ph->p_datalen) -4)/sizeof (struct mmc_performance);
		
		if (scsi_get_performance(scgp, buffer, 8+16*i, i, 0x00, 0x04) < 0)
			return (-1);

#ifdef	DEBUG
error("Bytes: %d\n", scmd->size - scg_getresid(scgp));
error("header: %ld\n", a_to_4_byte(buffer) + 4);
#endif

		ph = (struct mmc_performance_header *)buffer;
		perfp = (struct mmc_performance *)(((char *)ph) +
				sizeof (struct mmc_performance_header));

		ul = i;
		i = (a_to_4_byte(buffer) -4)/sizeof (struct mmc_performance);
		if (i > ul)
			i = ul;
		end = 0;
		speed = 0;
		for (; --i >= 0; perfp++) {
			ul = a_to_u_4_byte(perfp->end_lba);
			if (ul > end) {
				end = ul;
				ul = a_to_u_4_byte(perfp->end_perf);
				speed = ul;
			}
		}

		if (endp)
			*endp = end;

		if (writep)
			*writep = speed;

#ifdef	DEBUG
		perfp = (struct mmc_performance *)(((char *)ph) +
				sizeof (struct mmc_performance_header));
		i = (a_to_4_byte(buffer) -4)/sizeof (struct mmc_performance);
		if (i > scmd->cdb.cmd_cdb[9])
			i = scmd->cdb.cmd_cdb[9];
		error("Writeperf:   %d\n", i);
		for (; --i >= 0; perfp++) {
			ul = a_to_u_4_byte(perfp->start_lba);
			error("START LBA:   %7lu\n", ul);
			ul = a_to_u_4_byte(perfp->end_lba);
			error("End LBA:     %7lu\n", ul);
			ul = a_to_u_4_byte(perfp->start_perf);
			error("Start Perf:  %7lu == %lux\n", ul, ul/1385);
			ul = a_to_u_4_byte(perfp->end_perf);
			error("END Perf:    %7lu == %lux\n", ul, ul/1385);
			error("\n");
		}
		scg_prbytes("Performance data:", (Uchar *)buffer, scmd->size - scg_getresid(scgp));
#endif
	}
	if (readp) {
		fillbytes((caddr_t) buffer, sizeof (buffer), '\0');
		if (scsi_get_performance(scgp, buffer, 8+16, 1, 0x00, 0x00) < 0)
			return (-1);

		ph = (struct mmc_performance_header *)buffer;
		i = (a_to_4_byte(ph->p_datalen) -4)/sizeof (struct mmc_performance);
		
		if (scsi_get_performance(scgp, buffer, 8+16*i, i, 0x00, 0x00) < 0)
			return (-1);

#ifdef	DEBUG
error("Bytes: %d\n", scmd->size - scg_getresid(scgp));
error("header: %ld\n", a_to_4_byte(buffer) + 4);
#endif

		ph = (struct mmc_performance_header *)buffer;
		perfp = (struct mmc_performance *)(((char *)ph) +
				sizeof (struct mmc_performance_header));

		ul = i;
		i = (a_to_4_byte(buffer) -4)/sizeof (struct mmc_performance);
		if (i > ul)
			i = ul;
		end = 0;
		speed = 0;
		for (; --i >= 0; perfp++) {
			ul = a_to_u_4_byte(perfp->end_lba);
			if (ul > end) {
				end = ul;
				ul = a_to_u_4_byte(perfp->end_perf);
				speed = ul;
			}
		}

		if (readp)
			*readp = speed;

#ifdef	DEBUG
		i = (a_to_4_byte(buffer) -4)/sizeof (struct mmc_performance);
		if (i > scmd->cdb.cmd_cdb[9])
			i = scmd->cdb.cmd_cdb[9];
		error("Readperf:    %d\n", i);
		for (; --i >= 0; perfp++) {
			ul = a_to_u_4_byte(perfp->start_lba);
			error("START LBA:   %7lu\n", ul);
			ul = a_to_u_4_byte(perfp->end_lba);
			error("End LBA:     %7lu\n", ul);
			ul = a_to_u_4_byte(perfp->start_perf);
			error("Start Perf:  %7lu == %lux\n", ul, ul/1385);
			ul = a_to_u_4_byte(perfp->end_perf);
			error("END Perf:    %7lu == %lux\n", ul, ul/1385);
			error("\n");
		}
		scg_prbytes("Performance data:", (Uchar *)buffer, scmd->size - scg_getresid(scgp));
#endif
	}

	return (0);
}

LOCAL int
scsi_set_streaming(scgp, readp, writep, endp)
	SCSI	*scgp;
	Ulong	*readp;
	Ulong	*writep;
	Ulong	*endp;
{
	register struct	scg_cmd	*scmd = scgp->scmd;
	struct mmc_streaming	str;
	struct mmc_streaming	*sp = &str;

	fillbytes((caddr_t) scmd, sizeof (*scmd), '\0');
	scmd->addr = (caddr_t) sp;
	scmd->size = sizeof (*sp);
	scmd->flags = SCG_DISRE_ENA;
	scmd->cdb_len = SC_G5_CDBLEN;
	scmd->sense_len = CCS_SENSE_LEN;
	scmd->cdb.g5_cdb.cmd = 0xB6;
	scmd->cdb.g5_cdb.lun = scg_lun(scgp);
	i_to_2_byte(&scmd->cdb.cmd_cdb[9], sizeof (*sp)); /* Sz not G5 alike */

	scgp->cmdname = "set streaming";

	fillbytes(sp, sizeof (*sp), '\0');
	if (endp)
		i_to_4_byte(sp->end_lba, *endp);
	else
		i_to_4_byte(sp->end_lba, 0x7FFFFFFF);

	if (readp)
		i_to_4_byte(sp->read_size, *readp);
	else
		i_to_4_byte(sp->read_size, 0x7FFFFFFF);

	if (writep)
		i_to_4_byte(sp->write_size, *writep);
	else
		i_to_4_byte(sp->write_size, 0x7FFFFFFF);

	i_to_4_byte(sp->read_time, 1000);
	i_to_4_byte(sp->write_time, 1000);

#ifdef	DEBUG
	scg_prbytes("Streaming data:", (Uchar *)sp, sizeof (*sp));
#endif

	return (scg_cmd(scgp));
}

/*
 * set speed using the streaming descriptors
 */
EXPORT int
speed_select_mdvd(scgp, readspeed, writespeed)
	SCSI	*scgp;
	int	readspeed;
	int	writespeed;
{
	Ulong	end_lba = 0x7FFFFFFF;
	Ulong	wspeed = writespeed;

	if (scsi_get_perf_maxspeed(scgp, (Ulong *)NULL, (Ulong *)NULL, &end_lba) < 0)
		return (-1);

	if (scsi_set_streaming(scgp, (Ulong *)NULL, &wspeed, &end_lba) < 0)
		return (-1);

	return (0);
}


LOCAL char *
fname(code)
	Uint	code;
{
	UInt16_t	i;

	for (i = 0; i < sizeof (fl) / sizeof (fl[0]); i++) {
		if (code == fl[i].code)
			return (fl[i].name);
	}
	return ("XXX");
}

LOCAL char *
pname(code)
	Uint	code;
{
	UInt16_t	i;

	for (i = 0; i < sizeof (pl) / sizeof (pl[0]); i++) {
		if (code == pl[i].code)
			return (pl[i].name);
	}
	return ("Unknown");
}

LOCAL BOOL
pname_known(code)
	Uint	code;
{
	UInt16_t	i;

	for (i = 0; i < sizeof (pl) / sizeof (pl[0]); i++) {
		if (code == pl[i].code)
			return (TRUE);
	}
	return (FALSE);
}

void
gconf(scgp)
	SCSI	*scgp;
{
	Uchar	b[32 * 1024];
	Uchar	*p;
	Uchar	*pend;
	int	amt;
	int	flen;
	int	profile;
	int	i;
	int	n;

/*scsi_get_perf_maxspeed(scgp, (Ulong *)0, (Ulong *)0, (Ulong *)0);*/
/*return;*/

	fillbytes(b, sizeof (b), '\0');
	scgp->silent++;
	i = get_configuration(scgp, (char *)b, sizeof (b), 0, 0);
	scgp->silent--;
	amt = sizeof (b) - scg_getresid(scgp);
	if (i < 0) {
		printf("The drive is not mmc3 compliant.\n");
		return;
	}

	p = b;
	pend = &p[sizeof (b) - scg_getresid(scgp)];
	flen = a_to_u_4_byte(p);
	if ((flen+4) < amt)
		amt = flen+4;
	pend = &p[amt];
	scg_prbytes("Features: ", b, amt);

	profile = a_to_u_2_byte(&p[6]);
	printf("feature len: %d current profile 0x%04X len %lld\n",
				flen, profile, (Llong)(pend - p));


	p += 8;		/* Skip feature header	*/
	n = p[3];	/* Additional length	*/
	n /= 4;
	p += 4;

	for (i = 0; i < n; i++) {
		profile = a_to_u_2_byte(p);
		printf("Profile: 0x%04X %s %s\n", profile, pname(profile), p[2] & 1 ? "(current)":"");
		p += 4;
	}
	scg_prbytes("Rest: ", p, pend - p);

	p = b + 8;
	while (p < pend) {
		profile = a_to_u_2_byte(p);
		printf("Feature: 0x%04X '%s' %s %s\n",
			profile,
			fname(profile),
			p[2] & 1 ? "(current)":"",
			p[2] & 2 ? "(persistent)":"");
		if (p[3])
			scg_prbytes("Data: ", &p[4], p[3]);
		if (profile == 0x108)
			printf("Serial: '%.*s'\n", p[3], &p[4]);
		p += p[3];
		p += 4;
	}

	print_format_capacities(scgp);
}

EXPORT void
print_format_capacities(scgp)
	SCSI	*scgp;
{
	Uchar	b[1024];
	int	i;

	fillbytes(b, sizeof (b), '\0');
	read_format_capacities(scgp, (char *)b, sizeof (b));
	if (scg_getresid(scgp) != 0) {
		i = sizeof (b) - scg_getresid(scgp);
	} else {
		for (i = sizeof (b)-1; b[i] == '\0'; --i)
			;
		i++;
	}
	scg_prbytes("Format cap: ", b, i);

	fillbytes(b, sizeof (b), '\0');
	read_format_capacities(scgp, (char *)b, 4+16);
	scg_prbytes("Format cap: ", b, 4+16);
}

EXPORT int
get_format_capacities(scgp, bp, cnt)
	SCSI	*scgp;
	caddr_t	bp;
	int	cnt;
{
	int			len = sizeof (struct scsi_format_cap_header);
	struct scsi_format_cap_header	*hp;

	fillbytes(bp, cnt, '\0');
	if (cnt < len)
		return (-1);
	if (read_format_capacities(scgp, bp, len) < 0)
		return (-1);

	if (scg_getresid(scgp) > 0)
		return (-1);

	hp = (struct scsi_format_cap_header *)bp;
	len = hp->len;
	len += sizeof (struct scsi_format_cap_header);
	while (len > cnt)
		len -= sizeof (struct scsi_format_cap_desc);

	if (read_format_capacities(scgp, bp, len) < 0)
		return (-1);

	len -= scg_getresid(scgp);
	return (len);
}

EXPORT int
read_format_capacities(scgp, bp, cnt)
	SCSI	*scgp;
	caddr_t	bp;
	int	cnt;
{
	register struct	scg_cmd	*scmd = scgp->scmd;

	fillbytes((caddr_t)scmd, sizeof (*scmd), '\0');
	scmd->addr = bp;
	scmd->size = cnt;
	scmd->flags = SCG_RECV_DATA|SCG_DISRE_ENA;
	scmd->cdb_len = SC_G1_CDBLEN;
	scmd->sense_len = CCS_SENSE_LEN;
	scmd->cdb.g1_cdb.cmd = 0x23;
	scmd->cdb.g1_cdb.lun = scg_lun(scgp);
	g1_cdblen(&scmd->cdb.g1_cdb, cnt);

	scgp->cmdname = "read_format_capacities";

	return (scg_cmd(scgp));
}

