xref: /titanic_50/usr/src/cmd/cdrw/misc_scsi.c (revision 4730c9c455fdcb7ffe43b6f08727468d499ca837)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*4730c9c4Smikeri  * Common Development and Distribution License (the "License").
6*4730c9c4Smikeri  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
225d465cc7Sminht  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate #include <sys/time.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "mmc.h"
397c478bd9Sstevel@tonic-gate #include "util.h"
407c478bd9Sstevel@tonic-gate #include "misc_scsi.h"
417c478bd9Sstevel@tonic-gate #include "transport.h"
427c478bd9Sstevel@tonic-gate #include "main.h"
437c478bd9Sstevel@tonic-gate #include "toshiba.h"
447c478bd9Sstevel@tonic-gate #include "msgs.h"
4598584592Sarutz #include "device.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate uint32_t
487c478bd9Sstevel@tonic-gate read_scsi32(void *addr)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	uchar_t *ad = (uchar_t *)addr;
517c478bd9Sstevel@tonic-gate 	uint32_t ret;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	ret = ((((uint32_t)ad[0]) << 24) | (((uint32_t)ad[1]) << 16) |
547c478bd9Sstevel@tonic-gate 	    (((uint32_t)ad[2]) << 8) | ad[3]);
557c478bd9Sstevel@tonic-gate 	return (ret);
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate uint16_t
597c478bd9Sstevel@tonic-gate read_scsi16(void *addr)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	uchar_t *ad = (uchar_t *)addr;
627c478bd9Sstevel@tonic-gate 	uint16_t ret;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	ret = ((((uint16_t)ad[0]) << 8) | ad[1]);
657c478bd9Sstevel@tonic-gate 	return (ret);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate void
697c478bd9Sstevel@tonic-gate load_scsi32(void *addr, uint32_t v)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	uchar_t *ad = (uchar_t *)addr;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	ad[0] = (uchar_t)(v >> 24);
747c478bd9Sstevel@tonic-gate 	ad[1] = (uchar_t)(v >> 16);
757c478bd9Sstevel@tonic-gate 	ad[2] = (uchar_t)(v >> 8);
767c478bd9Sstevel@tonic-gate 	ad[3] = (uchar_t)v;
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate void
807c478bd9Sstevel@tonic-gate load_scsi16(void *addr, uint16_t v)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	uchar_t *ad = (uchar_t *)addr;
837c478bd9Sstevel@tonic-gate 	ad[0] = (uchar_t)(v >> 8);
847c478bd9Sstevel@tonic-gate 	ad[1] = (uchar_t)v;
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * will get the mode page only i.e. will strip off the header.
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate int
907c478bd9Sstevel@tonic-gate get_mode_page(int fd, int page_no, int pc, int buf_len, uchar_t *buffer)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	int ret;
937c478bd9Sstevel@tonic-gate 	uchar_t byte2, *buf;
947c478bd9Sstevel@tonic-gate 	uint_t header_len, page_len, copy_cnt;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	byte2 = (uchar_t)(((pc << 6) & 0xC0) | (page_no & 0x3f));
977c478bd9Sstevel@tonic-gate 	buf = (uchar_t *)my_zalloc(256);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/* Ask 254 bytes only to make our IDE driver happy */
1007c478bd9Sstevel@tonic-gate 	ret = mode_sense(fd, byte2, 1, 254, buf);
1017c478bd9Sstevel@tonic-gate 	if (ret == 0) {
1027c478bd9Sstevel@tonic-gate 		free(buf);
1037c478bd9Sstevel@tonic-gate 		return (0);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	header_len = 8 + read_scsi16(&buf[6]);
1077c478bd9Sstevel@tonic-gate 	page_len = buf[header_len + 1] + 2;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	copy_cnt = (page_len > buf_len) ? buf_len : page_len;
1107c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer, &buf[header_len], copy_cnt);
1117c478bd9Sstevel@tonic-gate 	free(buf);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	return (1);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * will take care of adding mode header and any extra bytes at the end.
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate int
1207c478bd9Sstevel@tonic-gate set_mode_page(int fd, uchar_t *buffer)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	int ret;
1237c478bd9Sstevel@tonic-gate 	uchar_t *buf;
1247c478bd9Sstevel@tonic-gate 	uint_t total, p_len;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	p_len = buffer[1] + 2;
1277c478bd9Sstevel@tonic-gate 	total = p_len + 8;
1287c478bd9Sstevel@tonic-gate 	buf = (uchar_t *)my_zalloc(total);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	(void) memcpy(&buf[8], buffer, p_len);
1317c478bd9Sstevel@tonic-gate 	if (debug) {
1327c478bd9Sstevel@tonic-gate 		int i;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 		(void) printf("MODE: [");
1357c478bd9Sstevel@tonic-gate 		for (i = 0; i < p_len; i++) {
1367c478bd9Sstevel@tonic-gate 			(void) printf("0x%02x ", (uchar_t)buffer[i]);
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		(void) printf("]\n");
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	ret = mode_select(fd, total, buf);
1427c478bd9Sstevel@tonic-gate 	free(buf);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	return (ret);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * Builds track information database for track trackno. If trackno is
1497c478bd9Sstevel@tonic-gate  * -1, builds the database for next blank track.
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate int
1527c478bd9Sstevel@tonic-gate build_track_info(cd_device *dev, int trackno, struct track_info *t_info)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	uchar_t *ti;
1557c478bd9Sstevel@tonic-gate 	uchar_t toc[20];		/* 2 entries + 4 byte header */
1567c478bd9Sstevel@tonic-gate 	int ret;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	(void) memset(t_info, 0, sizeof (*t_info));
1597c478bd9Sstevel@tonic-gate 	/* 1st try READ TRACK INFORMATION */
1607c478bd9Sstevel@tonic-gate 	ti = (uchar_t *)my_zalloc(TRACK_INFO_SIZE);
1617c478bd9Sstevel@tonic-gate 	t_info->ti_track_no = trackno;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/* Gererate faked information for writing to DVD */
1647c478bd9Sstevel@tonic-gate 	if (device_type != CD_RW) {
1657c478bd9Sstevel@tonic-gate 		uint_t bsize;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		t_info->ti_flags = 0x3000;
1687c478bd9Sstevel@tonic-gate 		t_info->ti_track_no = 1;
1697c478bd9Sstevel@tonic-gate 		t_info->ti_session_no = 1;
1707c478bd9Sstevel@tonic-gate 		t_info->ti_track_mode = 0x4;
1717c478bd9Sstevel@tonic-gate 		t_info->ti_data_mode = 1;
1727c478bd9Sstevel@tonic-gate 		t_info->ti_start_address = 0;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		/* only 1 track on DVD make it max size */
1757c478bd9Sstevel@tonic-gate 		t_info->ti_track_size = read_format_capacity(target->d_fd,
1767c478bd9Sstevel@tonic-gate 		    &bsize);
1777c478bd9Sstevel@tonic-gate 		if (t_info->ti_track_size < MAX_CD_BLKS) {
1787c478bd9Sstevel@tonic-gate 			t_info->ti_track_size = MAX_DVD_BLKS;
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 		t_info->ti_nwa = 0;
1827c478bd9Sstevel@tonic-gate 		t_info->ti_lra = 0;
1837c478bd9Sstevel@tonic-gate 		t_info->ti_packet_size = 0x10;
1847c478bd9Sstevel@tonic-gate 		t_info->ti_free_blocks = 0;
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (read_track_info(dev->d_fd, trackno, ti)) {
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		if (debug)
1907c478bd9Sstevel@tonic-gate 			(void) printf("using read_track_info for TOC \n");
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		t_info->ti_track_no = ti[2];
1937c478bd9Sstevel@tonic-gate 		t_info->ti_session_no = ti[3];
1947c478bd9Sstevel@tonic-gate 		t_info->ti_flags = (ti[6] >> 4) & 0xf;
1957c478bd9Sstevel@tonic-gate 		t_info->ti_flags |= (uint32_t)(ti[5] & 0xf0);
1967c478bd9Sstevel@tonic-gate 		t_info->ti_flags |= (uint32_t)(ti[7]) << 8;
1977c478bd9Sstevel@tonic-gate 		t_info->ti_flags |= TI_SESSION_NO_VALID | TI_FREE_BLOCKS_VALID;
1987c478bd9Sstevel@tonic-gate 		t_info->ti_track_mode = ti[5] & 0xf;
1997c478bd9Sstevel@tonic-gate 		if ((ti[6] & 0xf) == 0xf)
2007c478bd9Sstevel@tonic-gate 			t_info->ti_data_mode = 0xff;
2017c478bd9Sstevel@tonic-gate 		else
2027c478bd9Sstevel@tonic-gate 			t_info->ti_data_mode = ti[6] & 0xf;
2037c478bd9Sstevel@tonic-gate 		t_info->ti_start_address = read_scsi32(&ti[8]);
2047c478bd9Sstevel@tonic-gate 		t_info->ti_nwa = read_scsi32(&ti[12]);
2057c478bd9Sstevel@tonic-gate 		t_info->ti_free_blocks = read_scsi32(&ti[16]);
2067c478bd9Sstevel@tonic-gate 		t_info->ti_packet_size = read_scsi32(&ti[20]);
2077c478bd9Sstevel@tonic-gate 		t_info->ti_track_size = read_scsi32(&ti[24]);
2087c478bd9Sstevel@tonic-gate 		t_info->ti_lra = read_scsi32(&ti[28]);
2097c478bd9Sstevel@tonic-gate 		free(ti);
2107c478bd9Sstevel@tonic-gate 		return (1);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	/* READ TRACK INFORMATION not supported, try other options */
2137c478bd9Sstevel@tonic-gate 	free(ti);
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * We can get info for next blank track if READ TRACK INFO is not
2167c478bd9Sstevel@tonic-gate 	 * supported.
2177c478bd9Sstevel@tonic-gate 	 */
2187c478bd9Sstevel@tonic-gate 	if (trackno == -1)
2197c478bd9Sstevel@tonic-gate 		return (0);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if (debug)
2227c478bd9Sstevel@tonic-gate 		(void) printf("using READ_TOC for TOC\n");
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/* Try Read TOC */
2257c478bd9Sstevel@tonic-gate 	if (!read_toc(dev->d_fd, 0, trackno, 20, toc)) {
2267c478bd9Sstevel@tonic-gate 		return (0);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	t_info->ti_start_address = read_scsi32(&toc[8]);
2297c478bd9Sstevel@tonic-gate 	t_info->ti_track_mode = toc[5] & 0xf;
2307c478bd9Sstevel@tonic-gate 	t_info->ti_track_size = read_scsi32(&toc[16]) - read_scsi32(&toc[8]);
2317c478bd9Sstevel@tonic-gate 	t_info->ti_data_mode = get_data_mode(dev->d_fd, read_scsi32(&toc[8]));
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/* Numbers for audio tracks are always in 2K chunks */
2347c478bd9Sstevel@tonic-gate 	if ((dev->d_blksize == 512) && ((t_info->ti_track_mode & 4) == 0)) {
2357c478bd9Sstevel@tonic-gate 		t_info->ti_start_address /= 4;
2367c478bd9Sstevel@tonic-gate 		t_info->ti_track_size /= 4;
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/* Now find out the session thing */
2407c478bd9Sstevel@tonic-gate 	ret = read_toc(dev->d_fd, 1, trackno, 12, toc);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * Make sure that the call succeeds and returns the requested
2447c478bd9Sstevel@tonic-gate 	 * TOC size correctly.
2457c478bd9Sstevel@tonic-gate 	 */
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if ((ret == 0) || (toc[1] != 0x0a)) {
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 		/* For ATAPI drives or old Toshiba drives */
2507c478bd9Sstevel@tonic-gate 		ret = read_toc_as_per_8020(dev->d_fd, 1, trackno, 12, toc);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	/* If this goes through well TOC length will always be 0x0a */
2537c478bd9Sstevel@tonic-gate 	if (ret && (toc[1] == 0x0a)) {
2547c478bd9Sstevel@tonic-gate 		if (trackno >= toc[6]) {
2557c478bd9Sstevel@tonic-gate 			t_info->ti_session_no = toc[3];
2567c478bd9Sstevel@tonic-gate 			t_info->ti_flags |= TI_SESSION_NO_VALID;
2577c478bd9Sstevel@tonic-gate 		}
2587c478bd9Sstevel@tonic-gate 		/*
2597c478bd9Sstevel@tonic-gate 		 * This might be the last track of this session. If so,
2607c478bd9Sstevel@tonic-gate 		 * exclude the leadout and next lead in.
2617c478bd9Sstevel@tonic-gate 		 */
2627c478bd9Sstevel@tonic-gate 		if (trackno == (toc[6] - 1)) {
2637c478bd9Sstevel@tonic-gate 			/*
2647c478bd9Sstevel@tonic-gate 			 * 1.5 Min leadout + 1 min. leadin + 2 sec. pre-gap.
2657c478bd9Sstevel@tonic-gate 			 * For 2nd+ leadout it will be 0.5 min. But currently
2667c478bd9Sstevel@tonic-gate 			 * there is no direct way. And it will not happen
2677c478bd9Sstevel@tonic-gate 			 * for any normal case.
2687c478bd9Sstevel@tonic-gate 			 *
2697c478bd9Sstevel@tonic-gate 			 * 75 frames/sec, 60 sec/min, so leadin gap is
2707c478bd9Sstevel@tonic-gate 			 * ((1.5 +1)*60 + 2)*75 = 11400 frames (blocks)
2717c478bd9Sstevel@tonic-gate 			 */
2727c478bd9Sstevel@tonic-gate 			t_info->ti_track_size -= 11400;
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 	return (1);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate uchar_t
2797c478bd9Sstevel@tonic-gate get_data_mode(int fd, uint32_t lba)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	int ret;
2827c478bd9Sstevel@tonic-gate 	uchar_t *buf;
2837c478bd9Sstevel@tonic-gate 	uchar_t mode;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	buf = (uchar_t *)my_zalloc(8);
2867c478bd9Sstevel@tonic-gate 	ret = read_header(fd, lba, buf);
2877c478bd9Sstevel@tonic-gate 	if (ret == 0)
2887c478bd9Sstevel@tonic-gate 		mode = 0xff;
2897c478bd9Sstevel@tonic-gate 	else
2907c478bd9Sstevel@tonic-gate 		mode = buf[0];
2917c478bd9Sstevel@tonic-gate 	free(buf);
2927c478bd9Sstevel@tonic-gate 	return (mode);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * Set page code 5 for TAO mode.
2977c478bd9Sstevel@tonic-gate  */
2987c478bd9Sstevel@tonic-gate int
2997c478bd9Sstevel@tonic-gate prepare_for_write(cd_device *dev, int track_mode, int test_write,
3007c478bd9Sstevel@tonic-gate     int keep_disc_open)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	uchar_t *buf;
3037c478bd9Sstevel@tonic-gate 	int no_err;
3047c478bd9Sstevel@tonic-gate 	int reset_device;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if ((write_mode == DAO_MODE) && keep_disc_open) {
3077c478bd9Sstevel@tonic-gate 		(void) printf(gettext(
3087c478bd9Sstevel@tonic-gate 		    "Multi-session is not supported on DVD media\n"));
3097c478bd9Sstevel@tonic-gate 		exit(1);
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	if ((write_mode == DAO_MODE) && debug) {
3137c478bd9Sstevel@tonic-gate 		(void) printf("Preparing to write in DAO\n");
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	(void) start_stop(dev->d_fd, 1);
3177c478bd9Sstevel@tonic-gate 	/* Some drives do not support this command but still do it */
3187c478bd9Sstevel@tonic-gate 	(void) rezero_unit(dev->d_fd);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	buf = (uchar_t *)my_zalloc(64);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	no_err = get_mode_page(dev->d_fd, 5, 0, 64, buf);
3237c478bd9Sstevel@tonic-gate 	if (no_err)
3247c478bd9Sstevel@tonic-gate 		no_err = ((buf[1] + 2) > 64) ? 0 : 1;
3257c478bd9Sstevel@tonic-gate 	/*
3267c478bd9Sstevel@tonic-gate 	 * If the device is already in simulation mode and again a
3277c478bd9Sstevel@tonic-gate 	 * simulation is requested, then set the device in non-simulation
3287c478bd9Sstevel@tonic-gate 	 * 1st and then take it to simulation mode. This will flush any
3297c478bd9Sstevel@tonic-gate 	 * previous fake state in the drive.
3307c478bd9Sstevel@tonic-gate 	 */
3317c478bd9Sstevel@tonic-gate 	if (no_err && test_write && (buf[2] & 0x10)) {
3327c478bd9Sstevel@tonic-gate 		reset_device = 1;
3337c478bd9Sstevel@tonic-gate 	} else {
3347c478bd9Sstevel@tonic-gate 		reset_device = 0;
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 	if (no_err != 0) {
3377c478bd9Sstevel@tonic-gate 		buf[0] &= 0x3f;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		/* set TAO or DAO writing mode */
3407c478bd9Sstevel@tonic-gate 		buf[2] = (write_mode == TAO_MODE)?1:2;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 		/* set simulation flag */
3437c478bd9Sstevel@tonic-gate 		if (test_write && (!reset_device)) {
3447c478bd9Sstevel@tonic-gate 			buf[2] |= 0x10;
3457c478bd9Sstevel@tonic-gate 		} else {
3467c478bd9Sstevel@tonic-gate 			buf[2] &= ~0x10;
3477c478bd9Sstevel@tonic-gate 		}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		/* Turn on HW buffer underrun protection (BUFE) */
3507c478bd9Sstevel@tonic-gate 		if (!test_write) {
3517c478bd9Sstevel@tonic-gate 			buf[2] |= 0x40;
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		/* set track mode type */
3557c478bd9Sstevel@tonic-gate 		if (device_type == CD_RW) {
3567c478bd9Sstevel@tonic-gate 			buf[3] = track_mode & 0x0f;	/* ctrl nibble */
3577c478bd9Sstevel@tonic-gate 		}
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 		if (keep_disc_open) {
3607c478bd9Sstevel@tonic-gate 			buf[3] |= 0xc0;		/* Allow more sessions */
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 		/* Select track type (audio or data) */
3647c478bd9Sstevel@tonic-gate 		if (track_mode == TRACK_MODE_DATA) {
3657c478bd9Sstevel@tonic-gate 			buf[4] = 8;		/* 2048 byte sector */
3667c478bd9Sstevel@tonic-gate 		} else {
3677c478bd9Sstevel@tonic-gate 			buf[4] = 0;		/* 2352 byte sector */
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate 		buf[7] = buf[8] = 0;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 		/* Need to clear these fields for setting into DAO */
3727c478bd9Sstevel@tonic-gate 		if (write_mode == DAO_MODE)
3737c478bd9Sstevel@tonic-gate 			buf[5] = buf[15] = 0;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		/* print out mode for detailed log */
3767c478bd9Sstevel@tonic-gate 		if (debug && verbose) {
3777c478bd9Sstevel@tonic-gate 			int i;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 			(void) printf("setting = [ ");
3807c478bd9Sstevel@tonic-gate 			for (i = 0; i < 15; i++)
3817c478bd9Sstevel@tonic-gate 				(void) printf("0x%x ", buf[i]);
3827c478bd9Sstevel@tonic-gate 			(void) printf("]\n");
3837c478bd9Sstevel@tonic-gate 		}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 		no_err = set_mode_page(dev->d_fd, buf);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 		if (no_err && reset_device) {
3887c478bd9Sstevel@tonic-gate 			/* Turn the test write bit back on */
3897c478bd9Sstevel@tonic-gate 			buf[2] |= 0x10;
3907c478bd9Sstevel@tonic-gate 			no_err = set_mode_page(dev->d_fd, buf);
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		/*
3947c478bd9Sstevel@tonic-gate 		 * Since BUFE is the only optional flag we are
3957c478bd9Sstevel@tonic-gate 		 * setting we will try to turn it off if the command
3967c478bd9Sstevel@tonic-gate 		 * fails.
3977c478bd9Sstevel@tonic-gate 		 */
3987c478bd9Sstevel@tonic-gate 		if (!no_err) {
3997c478bd9Sstevel@tonic-gate 			/*
4007c478bd9Sstevel@tonic-gate 			 * Some old drives may not support HW
4017c478bd9Sstevel@tonic-gate 			 * buffer underrun protection, try again
4027c478bd9Sstevel@tonic-gate 			 * after turning it off.
4037c478bd9Sstevel@tonic-gate 			 */
4047c478bd9Sstevel@tonic-gate 			if (debug)
4057c478bd9Sstevel@tonic-gate 				(void) printf("Turning off BUFE\n");
4067c478bd9Sstevel@tonic-gate 			buf[2] &= ~0x40;
4077c478bd9Sstevel@tonic-gate 			no_err = set_mode_page(dev->d_fd, buf);
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	free(buf);
4127c478bd9Sstevel@tonic-gate 	return (no_err);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate  * Close session. This will write TOC.
4177c478bd9Sstevel@tonic-gate  */
4187c478bd9Sstevel@tonic-gate int
4197c478bd9Sstevel@tonic-gate finalize(cd_device *dev)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	uchar_t *di;
4227c478bd9Sstevel@tonic-gate 	int count, ret, err;
4237c478bd9Sstevel@tonic-gate 	int immediate;
4247c478bd9Sstevel@tonic-gate 	int finalize_max;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * For ATAPI devices we will use the immediate mode and will
4287c478bd9Sstevel@tonic-gate 	 * poll the command for completion so that this command may
4297c478bd9Sstevel@tonic-gate 	 * not hog the channel. But for SCSI, we will use the treditional
4307c478bd9Sstevel@tonic-gate 	 * way of issuing the command with a large enough timeout. This
4317c478bd9Sstevel@tonic-gate 	 * is done because immediate mode was designed for ATAPI and some
4327c478bd9Sstevel@tonic-gate 	 * SCSI RW drives might not be even tested with it.
4337c478bd9Sstevel@tonic-gate 	 */
4347c478bd9Sstevel@tonic-gate 	if ((dev->d_inq[2] & 7) != 0) {
4357c478bd9Sstevel@tonic-gate 		/* SCSI device */
4367c478bd9Sstevel@tonic-gate 		immediate = 0;
4377c478bd9Sstevel@tonic-gate 	} else {
4387c478bd9Sstevel@tonic-gate 		/* non-SCSI (e.g ATAPI) device */
4397c478bd9Sstevel@tonic-gate 		immediate = 1;
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	/* We need to close track before close session */
4437c478bd9Sstevel@tonic-gate 	if (device_type == DVD_PLUS) {
4447c478bd9Sstevel@tonic-gate 		if (!close_track(dev->d_fd, 0, 0, immediate))
4457c478bd9Sstevel@tonic-gate 			return (0);
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	if (!close_track(dev->d_fd, 0, 1, immediate)) {
4497c478bd9Sstevel@tonic-gate 		/*
4505d465cc7Sminht 		 * For DAO mode which we use for DVD-RW, the latest MMC
4515d465cc7Sminht 		 * specification does not mention close_track. Some
4525d465cc7Sminht 		 * newer drives will return an ILLEGAL INSTRUCTION
4535d465cc7Sminht 		 * which we will ignore. We have also found a Panasonic
4545d465cc7Sminht 		 * drive which will return a MEDIA ERROR. It is safe
4555d465cc7Sminht 		 * to ignore both errors as this is not needed for
4565d465cc7Sminht 		 * these drives.
4575d465cc7Sminht 		 * This is kept for older drives which had needed
4585d465cc7Sminht 		 * us to issue close_track to flush the cache fully.
4595d465cc7Sminht 		 * once we are certain these drives have cleared the
4605d465cc7Sminht 		 * market, this can be removed.
4617c478bd9Sstevel@tonic-gate 		 */
4627c478bd9Sstevel@tonic-gate 		if (device_type == DVD_MINUS) {
4637c478bd9Sstevel@tonic-gate 			return (0);
4647c478bd9Sstevel@tonic-gate 		}
4657c478bd9Sstevel@tonic-gate 	} else {
4667c478bd9Sstevel@tonic-gate 		if (!immediate)
4677c478bd9Sstevel@tonic-gate 			return (1);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 	if (immediate) {
4707c478bd9Sstevel@tonic-gate 		(void) sleep(10);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
4737c478bd9Sstevel@tonic-gate 		err = 0;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 		if (device_type == CD_RW) {
4767c478bd9Sstevel@tonic-gate 			/* Finalization should not take more than 6 minutes */
4777c478bd9Sstevel@tonic-gate 			finalize_max = FINALIZE_TIMEOUT;
4787c478bd9Sstevel@tonic-gate 		} else {
4797c478bd9Sstevel@tonic-gate 			/* some DVD-RW drives take longer than 6 minutes */
4807c478bd9Sstevel@tonic-gate 			finalize_max = FINALIZE_TIMEOUT*2;
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		for (count = 0; count < finalize_max; count++) {
4847c478bd9Sstevel@tonic-gate 			ret = read_disc_info(dev->d_fd, di);
4857c478bd9Sstevel@tonic-gate 			if (ret != 0)
4867c478bd9Sstevel@tonic-gate 				break;
4877c478bd9Sstevel@tonic-gate 			if (uscsi_status != 2)
4887c478bd9Sstevel@tonic-gate 				err = 1;
4897c478bd9Sstevel@tonic-gate 			if (SENSE_KEY(rqbuf) == 2) {
4907c478bd9Sstevel@tonic-gate 				/* not ready but not becoming ready */
4917c478bd9Sstevel@tonic-gate 				if (ASC(rqbuf) != 4)
4927c478bd9Sstevel@tonic-gate 					err = 1;
4937c478bd9Sstevel@tonic-gate 			} else if (SENSE_KEY(rqbuf) == 5) {
4947c478bd9Sstevel@tonic-gate 				/* illegal mode for this track */
4957c478bd9Sstevel@tonic-gate 				if (ASC(rqbuf) != 0x64)
4967c478bd9Sstevel@tonic-gate 					err = 1;
4977c478bd9Sstevel@tonic-gate 			} else {
4987c478bd9Sstevel@tonic-gate 				err = 1;
4997c478bd9Sstevel@tonic-gate 			}
5007c478bd9Sstevel@tonic-gate 			if (err == 1) {
5017c478bd9Sstevel@tonic-gate 				if (debug) {
5027c478bd9Sstevel@tonic-gate 					(void) printf("Finalization failed\n");
5037c478bd9Sstevel@tonic-gate 					(void) printf("%x %x %x %x\n",
5047c478bd9Sstevel@tonic-gate 					    uscsi_status, SENSE_KEY(rqbuf),
5057c478bd9Sstevel@tonic-gate 					    ASC(rqbuf), ASCQ(rqbuf));
5067c478bd9Sstevel@tonic-gate 				}
5077c478bd9Sstevel@tonic-gate 				free(di);
5087c478bd9Sstevel@tonic-gate 				return (0);
5097c478bd9Sstevel@tonic-gate 			}
5107c478bd9Sstevel@tonic-gate 			if (uscsi_status == 2) {
5117c478bd9Sstevel@tonic-gate 				int i;
5127c478bd9Sstevel@tonic-gate 				/* illegal field in command packet */
5137c478bd9Sstevel@tonic-gate 				if (ASC(rqbuf) == 0x24) {
5147c478bd9Sstevel@tonic-gate 					/* print it out! */
5157c478bd9Sstevel@tonic-gate 					(void) printf("\n");
5167c478bd9Sstevel@tonic-gate 					for (i = 0; i < 18; i++)
5177c478bd9Sstevel@tonic-gate 						(void) printf("%x ",
5187c478bd9Sstevel@tonic-gate 						    (unsigned)(rqbuf[i]));
5197c478bd9Sstevel@tonic-gate 					(void) printf("\n");
5207c478bd9Sstevel@tonic-gate 				}
5217c478bd9Sstevel@tonic-gate 			}
5227c478bd9Sstevel@tonic-gate 			(void) sleep(5);
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 		free(di);
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 	return (ret);
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate /*
5307c478bd9Sstevel@tonic-gate  * Find out media capacity.
5317c478bd9Sstevel@tonic-gate  */
5327c478bd9Sstevel@tonic-gate int
5337c478bd9Sstevel@tonic-gate get_last_possible_lba(cd_device *dev)
5347c478bd9Sstevel@tonic-gate {
5357c478bd9Sstevel@tonic-gate 	uchar_t *di;
5367c478bd9Sstevel@tonic-gate 	int cap;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
5397c478bd9Sstevel@tonic-gate 	if (!read_disc_info(dev->d_fd, di)) {
5407c478bd9Sstevel@tonic-gate 		free(di);
5417c478bd9Sstevel@tonic-gate 		return (0);
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 	if ((di[21] != 0) && (di[21] != 0xff)) {
5447c478bd9Sstevel@tonic-gate 		cap = ((di[21] * 60) + di[22]) * 75;
5457c478bd9Sstevel@tonic-gate 	} else {
5467c478bd9Sstevel@tonic-gate 		cap = 0;
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	free(di);
5507c478bd9Sstevel@tonic-gate 	return (cap);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate int
5547c478bd9Sstevel@tonic-gate read_audio_through_read_cd(cd_device *dev, uint_t start_lba, uint_t nblks,
5557c478bd9Sstevel@tonic-gate     uchar_t *buf)
5567c478bd9Sstevel@tonic-gate {
5577c478bd9Sstevel@tonic-gate 	int retry;
5587c478bd9Sstevel@tonic-gate 	int ret;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	for (retry = 0; retry < 3; retry++) {
5617c478bd9Sstevel@tonic-gate 		ret = read_cd(dev->d_fd, (uint32_t)start_lba, (uint16_t)nblks,
5627c478bd9Sstevel@tonic-gate 		    1, buf, (uint32_t)(nblks * 2352));
5637c478bd9Sstevel@tonic-gate 		if (ret)
5647c478bd9Sstevel@tonic-gate 			break;
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 	return (ret);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate int
5707c478bd9Sstevel@tonic-gate eject_media(cd_device *dev)
5717c478bd9Sstevel@tonic-gate {
5727c478bd9Sstevel@tonic-gate 	if (vol_running) {
5737c478bd9Sstevel@tonic-gate 		/* If there is a media, try using DKIOCEJECT 1st */
5747c478bd9Sstevel@tonic-gate 		if (check_device(dev, CHECK_NO_MEDIA) == 0) {
5757c478bd9Sstevel@tonic-gate 			if (ioctl(dev->d_fd, DKIOCEJECT, 0) == 0) {
5767c478bd9Sstevel@tonic-gate 				return (1);
5777c478bd9Sstevel@tonic-gate 			}
5787c478bd9Sstevel@tonic-gate 		}
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 	if (load_unload(dev->d_fd, 0) == 0) {
5817c478bd9Sstevel@tonic-gate 		/* if eject fails */
5827c478bd9Sstevel@tonic-gate 		if ((uscsi_status == 2) && (ASC(rqbuf) == 0x53)) {
5837c478bd9Sstevel@tonic-gate 			/*
5847c478bd9Sstevel@tonic-gate 			 * check that eject is not blocked on the device
5857c478bd9Sstevel@tonic-gate 			 */
5867c478bd9Sstevel@tonic-gate 			if (!prevent_allow_mr(dev->d_fd, 1))
5877c478bd9Sstevel@tonic-gate 				return (0);
5887c478bd9Sstevel@tonic-gate 			return (load_unload(dev->d_fd, 0));
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 		return (0);
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 	return (1);
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate /*
59698584592Sarutz  * Get current Read or Write Speed from Mode Page 0x2a.
59798584592Sarutz  *
59898584592Sarutz  * Use the size of the Page to determine which Multimedia Command
59998584592Sarutz  * set (MMC) is present.  Based on the MMC version, get the
60098584592Sarutz  * specified Read/Write Speed.
60198584592Sarutz  *
60298584592Sarutz  * Note that some MMC versions do not necessarily support a
60398584592Sarutz  * (current) Read or Write Speed.  As a result, this function
60498584592Sarutz  * _can_ return a value of zero.
60598584592Sarutz  *
60698584592Sarutz  * The newer standards (reserve and) mark the field(s) as Obsolete,
60798584592Sarutz  * yet many vendors populate the Obsolete fields with valid values
60898584592Sarutz  * (assumedly for backward compatibility).  This is important, as
60998584592Sarutz  * a command like GET PERFORMANCE cannot return _the_ speed; it can
61098584592Sarutz  * only return a Logical-Block-Address-dependent (LBA) speed.  Such
61198584592Sarutz  * values can vary widely between the innermost and outermost Track.
61298584592Sarutz  * Mode Page 0x2a is the best solution identifying "the current
61398584592Sarutz  * (nominal) speed".
6147c478bd9Sstevel@tonic-gate  */
6157c478bd9Sstevel@tonic-gate static uint16_t
61698584592Sarutz cd_speed_get(cd_device *dev, int cmd)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	uchar_t		*mp2a;
61998584592Sarutz 	uint16_t	rate = 0;
62098584592Sarutz 	int		offset;
62198584592Sarutz 	uint_t		buflen = 254;
6227c478bd9Sstevel@tonic-gate 
62398584592Sarutz 	/*
62498584592Sarutz 	 * Allocate a buffer acceptably larger than any nominal
62598584592Sarutz 	 * Page for Page Code 0x2A.
62698584592Sarutz 	 */
62798584592Sarutz 	mp2a = (uchar_t *)my_zalloc(buflen);
62898584592Sarutz 	if (get_mode_page(dev->d_fd, 0x2A, 0, buflen, mp2a) == 0)
62998584592Sarutz 		goto end;
63098584592Sarutz 
63198584592Sarutz 	/* Determine MMC version based on 'Page Length' field */
63298584592Sarutz 	switch (mp2a[1]) {
63398584592Sarutz 	case 0x14:  /* MMC-1 */
63498584592Sarutz 		if (debug)
63598584592Sarutz 			(void) printf("Mode Page 2A: MMC-1\n");
63698584592Sarutz 
63798584592Sarutz 		offset = (cmd == GET_READ_SPEED) ? 14 : 20;
63898584592Sarutz 		rate = read_scsi16(&mp2a[offset]);
63998584592Sarutz 		break;
64098584592Sarutz 
64198584592Sarutz 
64298584592Sarutz 	case 0x18: /* MMC-2 */
64398584592Sarutz 		if (debug)
64498584592Sarutz 			(void) printf("Mode Page 2A: MMC-2;"
64598584592Sarutz 			    " Read and Write Speeds are "
64698584592Sarutz 			    "obsolete\n");
64798584592Sarutz 
64898584592Sarutz 		/* see if "Obsolete" values are valid: */
64998584592Sarutz 		offset = (cmd == GET_READ_SPEED) ? 14 : 20;
65098584592Sarutz 		rate = read_scsi16(&mp2a[offset]);
65198584592Sarutz 		break;
65298584592Sarutz 
65398584592Sarutz 	default: /* MMC-3 or newer */
65498584592Sarutz 		if (debug)
65598584592Sarutz 			(void) printf("Mode Page 2A: MMC-3 or"
65698584592Sarutz 			    " newer; Read Speed is obsolete.\n");
65798584592Sarutz 
6587c478bd9Sstevel@tonic-gate 		if (cmd == GET_READ_SPEED) {
65998584592Sarutz 			/* this is Obsolete, but try it */
66098584592Sarutz 			offset = 14;
66198584592Sarutz 			rate = read_scsi16(&mp2a[offset]);
6627c478bd9Sstevel@tonic-gate 		} else {
66398584592Sarutz 			/* Write Speed is not obsolete */
66498584592Sarutz 			offset = 28;
66598584592Sarutz 			rate = read_scsi16(&mp2a[offset]);
66698584592Sarutz 
66798584592Sarutz 			if (rate == 0) {
66898584592Sarutz 				/*
66998584592Sarutz 				 * then try an Obsolete field
67098584592Sarutz 				 * (but this shouldn't happen!)
67198584592Sarutz 				 */
67298584592Sarutz 				offset = 20;
67398584592Sarutz 				rate = read_scsi16(&mp2a[offset]);
6747c478bd9Sstevel@tonic-gate 			}
6757c478bd9Sstevel@tonic-gate 		}
67698584592Sarutz 		break;
67798584592Sarutz 	}
67898584592Sarutz end:
6797c478bd9Sstevel@tonic-gate 	free(mp2a);
68098584592Sarutz 
68198584592Sarutz 	if (debug)
68298584592Sarutz 		(void) printf("cd_speed_get: %s Speed is "
68398584592Sarutz 		    "%uX\n", (cmd == GET_READ_SPEED) ?
68498584592Sarutz 		    "Read" : "Write", cdrw_bandwidth_to_x(rate));
6857c478bd9Sstevel@tonic-gate 	return (rate);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate /*
6897c478bd9Sstevel@tonic-gate  * CD speed related functions (ioctl style) for drives which do not support
6907c478bd9Sstevel@tonic-gate  * real time streaming.
69198584592Sarutz  *
69298584592Sarutz  * For the SET operations, the SET CD SPEED command needs
69398584592Sarutz  * both the Read Speed and the Write Speed.  Eg, if
69498584592Sarutz  * we're trying to set the Write Speed (SET_WRITE_SPEED),
69598584592Sarutz  * then we first need to obtain the current Read Speed.
69698584592Sarutz  * That speed is specified along with the chosen_speed (the
69798584592Sarutz  * Write Speed in this case) in the SET CD SPEED command.
6987c478bd9Sstevel@tonic-gate  */
6997c478bd9Sstevel@tonic-gate int
7007c478bd9Sstevel@tonic-gate cd_speed_ctrl(cd_device *dev, int cmd, int speed)
7017c478bd9Sstevel@tonic-gate {
7027c478bd9Sstevel@tonic-gate 	uint16_t rate;
7037c478bd9Sstevel@tonic-gate 
70498584592Sarutz 	switch (cmd) {
70598584592Sarutz 	case GET_READ_SPEED:
70698584592Sarutz 		rate = cd_speed_get(dev, GET_READ_SPEED);
70798584592Sarutz 		return (cdrw_bandwidth_to_x(rate));
70898584592Sarutz 
70998584592Sarutz 	case GET_WRITE_SPEED:
71098584592Sarutz 		rate = cd_speed_get(dev, GET_WRITE_SPEED);
71198584592Sarutz 		return (cdrw_bandwidth_to_x(rate));
71298584592Sarutz 
71398584592Sarutz 	case SET_READ_SPEED:
71498584592Sarutz 		rate = cd_speed_get(dev, GET_WRITE_SPEED);
71598584592Sarutz 		return (set_cd_speed(dev->d_fd,
71698584592Sarutz 		    cdrw_x_to_bandwidth(speed), rate));
71798584592Sarutz 		break;
71898584592Sarutz 
71998584592Sarutz 	case SET_WRITE_SPEED:
72098584592Sarutz 		rate = cd_speed_get(dev, GET_READ_SPEED);
7217c478bd9Sstevel@tonic-gate 		return (set_cd_speed(dev->d_fd, rate,
72298584592Sarutz 		    cdrw_x_to_bandwidth(speed)));
72398584592Sarutz 		break;
72498584592Sarutz 
72598584592Sarutz 	default:
7267c478bd9Sstevel@tonic-gate 		return (0);
7277c478bd9Sstevel@tonic-gate 	}
72898584592Sarutz }
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate /*
73198584592Sarutz  * Manage sending of SET STREAMING command using the specified
73298584592Sarutz  * read_speed and write_speed.
73398584592Sarutz  *
73498584592Sarutz  * This function allocates and initializes a Performance
73598584592Sarutz  * Descriptor, which is sent as part of the SET STREAMING
73698584592Sarutz  * command.  The descriptor is deallocated before function
73798584592Sarutz  * exit.
73898584592Sarutz  */
73998584592Sarutz static int
74098584592Sarutz do_set_streaming(cd_device *dev, uint_t read_speed,
74198584592Sarutz 	uint_t write_speed)
74298584592Sarutz {
74398584592Sarutz 	int ret;
74498584592Sarutz 	uchar_t *str;
74598584592Sarutz 
74698584592Sarutz 	/* Allocate and initialize the Performance Descriptor */
74798584592Sarutz 	str = (uchar_t *)my_zalloc(SET_STREAM_DATA_LEN);
74898584592Sarutz 
74998584592Sarutz 	/* Read Time (in milliseconds) */
75098584592Sarutz 	load_scsi32(&str[16], 1000);
75198584592Sarutz 	/* Write Time (in milliseconds) */
75298584592Sarutz 	load_scsi32(&str[24], 1000);
75398584592Sarutz 
75498584592Sarutz 	/* Read Speed */
75598584592Sarutz 	load_scsi32(&str[12], (uint32_t)read_speed);
75698584592Sarutz 	/* Write Speed */
75798584592Sarutz 	load_scsi32(&str[20], (uint32_t)write_speed);
75898584592Sarutz 
75998584592Sarutz 	/* issue SET STREAMING command */
76098584592Sarutz 	ret = set_streaming(dev->d_fd, str);
76198584592Sarutz 	free(str);
76298584592Sarutz 
76398584592Sarutz 	return (ret);
76498584592Sarutz }
76598584592Sarutz 
76698584592Sarutz /*
76798584592Sarutz  * cd speed related functions for drives which support
76898584592Sarutz  * Real-Time Streaming Feature.
76998584592Sarutz  *
77098584592Sarutz  * For the SET operations, the SET STREAMING command needs
77198584592Sarutz  * both the Read Speed and the Write Speed.  Eg, if
77298584592Sarutz  * we're trying to set the Write Speed (SET_WRITE_SPEED),
77398584592Sarutz  * then we first need to obtain the current Read Speed.
77498584592Sarutz  * That speed is specified along with the chosen_speed (the
77598584592Sarutz  * Write Speed in this case) in the SET STREAMING command.
7767c478bd9Sstevel@tonic-gate  */
7777c478bd9Sstevel@tonic-gate int
7787c478bd9Sstevel@tonic-gate rt_streaming_ctrl(cd_device *dev, int cmd, int speed)
7797c478bd9Sstevel@tonic-gate {
78098584592Sarutz 	int ret = 0;
78198584592Sarutz 	uint_t rate;
7827c478bd9Sstevel@tonic-gate 
78398584592Sarutz 	switch (cmd) {
78498584592Sarutz 	case GET_WRITE_SPEED:
78598584592Sarutz 		rate = cd_speed_get(dev, GET_WRITE_SPEED);
78698584592Sarutz 		ret = (int)cdrw_bandwidth_to_x(rate);
78798584592Sarutz 		break;
78898584592Sarutz 
78998584592Sarutz 	case GET_READ_SPEED:
79098584592Sarutz 		rate = cd_speed_get(dev, GET_READ_SPEED);
79198584592Sarutz 		ret = (int)cdrw_bandwidth_to_x(rate);
79298584592Sarutz 		break;
79398584592Sarutz 
79498584592Sarutz 	case SET_READ_SPEED: {
79598584592Sarutz 		uint_t write_speed = cd_speed_get(dev, GET_WRITE_SPEED);
79698584592Sarutz 
79798584592Sarutz 		/* set Read Speed using SET STREAMING */
79898584592Sarutz 		ret = do_set_streaming(dev,
79998584592Sarutz 		    cdrw_x_to_bandwidth(speed), write_speed);
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 		/* If rt_speed_ctrl fails for any reason use cd_speed_ctrl */
8027c478bd9Sstevel@tonic-gate 		if (ret == 0) {
8037c478bd9Sstevel@tonic-gate 			if (debug)
8047c478bd9Sstevel@tonic-gate 				(void) printf(" real time speed control"
8057c478bd9Sstevel@tonic-gate 				    " failed, using CD speed control\n");
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 			dev->d_speed_ctrl = cd_speed_ctrl;
8087c478bd9Sstevel@tonic-gate 			ret = dev->d_speed_ctrl(dev, cmd, speed);
8097c478bd9Sstevel@tonic-gate 		}
81098584592Sarutz 		break;
81198584592Sarutz 	}
8127c478bd9Sstevel@tonic-gate 
81398584592Sarutz 	case SET_WRITE_SPEED: {
81498584592Sarutz 		uint_t read_speed = cd_speed_get(dev, GET_READ_SPEED);
81598584592Sarutz 
81698584592Sarutz 		/* set Write Speed using SET STREAMING */
81798584592Sarutz 		ret = do_set_streaming(dev, read_speed,
81898584592Sarutz 		    cdrw_x_to_bandwidth(speed));
81998584592Sarutz 
82098584592Sarutz 		/* If rt_speed_ctrl fails for any reason use cd_speed_ctrl */
82198584592Sarutz 		if (ret == 0) {
82298584592Sarutz 			if (debug)
82398584592Sarutz 				(void) printf(" real time speed control"
82498584592Sarutz 				    " failed, using CD speed control\n");
82598584592Sarutz 
82698584592Sarutz 			dev->d_speed_ctrl = cd_speed_ctrl;
82798584592Sarutz 			ret = dev->d_speed_ctrl(dev, cmd, speed);
82898584592Sarutz 		}
82998584592Sarutz 		break;
83098584592Sarutz 	}
83198584592Sarutz 
83298584592Sarutz 	default:
83398584592Sarutz 		break;
83498584592Sarutz 	}
83598584592Sarutz 
8367c478bd9Sstevel@tonic-gate 	return (ret);
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate /*
8407c478bd9Sstevel@tonic-gate  * Initialize device for track-at-once mode of writing. All of the data will
8417c478bd9Sstevel@tonic-gate  * need to be written to the track without interruption.
8427c478bd9Sstevel@tonic-gate  * This initialized TAO by setting page code 5 and speed.
8437c478bd9Sstevel@tonic-gate  */
8447c478bd9Sstevel@tonic-gate void
8457c478bd9Sstevel@tonic-gate write_init(int mode)
8467c478bd9Sstevel@tonic-gate {
8477c478bd9Sstevel@tonic-gate 	(void) printf(gettext("Initializing device"));
8487c478bd9Sstevel@tonic-gate 	if (simulation)
8497c478bd9Sstevel@tonic-gate 		(void) printf(gettext("(Simulation mode)"));
8507c478bd9Sstevel@tonic-gate 	print_n_flush("...");
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	get_media_type(target->d_fd);
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	/* DVD- requires DAO mode */
8557c478bd9Sstevel@tonic-gate 	if (device_type == DVD_MINUS) {
8567c478bd9Sstevel@tonic-gate 		write_mode = DAO_MODE;
8577c478bd9Sstevel@tonic-gate 	}
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	/* DVD+ and DVD- have no support for AUDIO, bail out */
8607c478bd9Sstevel@tonic-gate 	if ((mode == TRACK_MODE_AUDIO) && (device_type != CD_RW)) {
8617c478bd9Sstevel@tonic-gate 		err_msg(gettext("Audio mode is only supported for CD media\n"));
8627c478bd9Sstevel@tonic-gate 		exit(1);
8637c478bd9Sstevel@tonic-gate 	}
864a2b4fdf6Srameshc 	if (simulation &&
865a2b4fdf6Srameshc 	    check_device(target, CHECK_MEDIA_IS_NOT_BLANK) &&
866a2b4fdf6Srameshc 	    !check_device(target, CHECK_MEDIA_IS_NOT_ERASABLE) &&
867a2b4fdf6Srameshc 	    device_type != DVD_PLUS_W) {
868a2b4fdf6Srameshc 		/*
869a2b4fdf6Srameshc 		 * If we were in simulation mode, and media wasn't blank,
870a2b4fdf6Srameshc 		 * but medium was erasable, then cdrw goes to erase the
871a2b4fdf6Srameshc 		 * contents of the media after the simulation writing in order
872a2b4fdf6Srameshc 		 * to cleanup the ghost TOC (see write_fini() calls blank()).
873a2b4fdf6Srameshc 		 * This is bad because it removes existing data if media was
874a2b4fdf6Srameshc 		 * multi-session. Therefore, we no longer allow simulation
875a2b4fdf6Srameshc 		 * writing if such condition is met. we don't blank the DVD+RW
876a2b4fdf6Srameshc 		 * media, so DVD+RWs are fine.
877a2b4fdf6Srameshc 		 */
878a2b4fdf6Srameshc 		err_msg(gettext(
879a2b4fdf6Srameshc 		    "Cannot perform simulation for non-blank media\n"));
880a2b4fdf6Srameshc 		exit(1);
881a2b4fdf6Srameshc 	}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	if (!prepare_for_write(target, mode, simulation, keep_disc_open)) {
8847c478bd9Sstevel@tonic-gate 		/* l10n_NOTE : 'failed' as in Initializing device...failed  */
8857c478bd9Sstevel@tonic-gate 		(void) printf(gettext("failed.\n"));
8867c478bd9Sstevel@tonic-gate 		err_msg(gettext("Cannot initialize device for write\n"));
8877c478bd9Sstevel@tonic-gate 		exit(1);
8887c478bd9Sstevel@tonic-gate 	}
8897c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : 'done' as in "Initializing device...done"  */
8907c478bd9Sstevel@tonic-gate 	(void) printf(gettext("done.\n"));
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	/* if speed change option was used (-p) then try to set the speed */
8937c478bd9Sstevel@tonic-gate 	if (requested_speed != 0) {
8947c478bd9Sstevel@tonic-gate 		if (verbose)
8957c478bd9Sstevel@tonic-gate 			(void) printf(gettext("Trying to set speed to %dX.\n"),
8967c478bd9Sstevel@tonic-gate 			    requested_speed);
8977c478bd9Sstevel@tonic-gate 		if (target->d_speed_ctrl(target, SET_WRITE_SPEED,
8987c478bd9Sstevel@tonic-gate 		    requested_speed) == 0) {
8997c478bd9Sstevel@tonic-gate 			err_msg(gettext("Unable to set speed.\n"));
9007c478bd9Sstevel@tonic-gate 			exit(1);
9017c478bd9Sstevel@tonic-gate 		}
9027c478bd9Sstevel@tonic-gate 		if (verbose) {
9037c478bd9Sstevel@tonic-gate 			int speed;
9047c478bd9Sstevel@tonic-gate 			speed = target->d_speed_ctrl(target,
9057c478bd9Sstevel@tonic-gate 			    GET_WRITE_SPEED, 0);
9067c478bd9Sstevel@tonic-gate 			if (speed == requested_speed) {
9077c478bd9Sstevel@tonic-gate 				(void) printf(gettext("Speed set to %dX.\n"),
9087c478bd9Sstevel@tonic-gate 				    speed);
90998584592Sarutz 			} else if (speed == 0) {
91098584592Sarutz 				(void) printf(gettext("Could not obtain "
91198584592Sarutz 				    "current Write Speed.\n"));
9127c478bd9Sstevel@tonic-gate 			} else {
9137c478bd9Sstevel@tonic-gate 				(void) printf(
9147c478bd9Sstevel@tonic-gate 				gettext("Speed set to closest approximation "
9157c478bd9Sstevel@tonic-gate 				    "of %dX allowed by device (%dX).\n"),
9167c478bd9Sstevel@tonic-gate 				    requested_speed, speed);
9177c478bd9Sstevel@tonic-gate 			}
9187c478bd9Sstevel@tonic-gate 		}
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate void
9237c478bd9Sstevel@tonic-gate write_fini(void)
9247c478bd9Sstevel@tonic-gate {
9257c478bd9Sstevel@tonic-gate 	print_n_flush(gettext("Finalizing (Can take several minutes)..."));
9267c478bd9Sstevel@tonic-gate 	/* Some drives don't like this while in test write mode */
9277c478bd9Sstevel@tonic-gate 	if (!simulation) {
9287c478bd9Sstevel@tonic-gate 		if (!finalize(target)) {
9297c478bd9Sstevel@tonic-gate 			/*
9307c478bd9Sstevel@tonic-gate 			 * It is possible that the drive is busy writing the
9317c478bd9Sstevel@tonic-gate 			 * buffered portion. So do not get upset yet.
9327c478bd9Sstevel@tonic-gate 			 */
9337c478bd9Sstevel@tonic-gate 			(void) sleep(10);
9347c478bd9Sstevel@tonic-gate 			if (!finalize(target)) {
9357c478bd9Sstevel@tonic-gate 				if (debug) {
9367c478bd9Sstevel@tonic-gate 					(void) printf("status %x, %x/%x/%x\n",
9377c478bd9Sstevel@tonic-gate 					    uscsi_status, SENSE_KEY(rqbuf),
9387c478bd9Sstevel@tonic-gate 					    ASC(rqbuf), ASCQ(rqbuf));
9397c478bd9Sstevel@tonic-gate 				}
9407c478bd9Sstevel@tonic-gate 
9415d465cc7Sminht 				/*
9425d465cc7Sminht 				 * Different vendor drives return different
9435d465cc7Sminht 				 * sense error info for CLOSE SESSION command.
9445d465cc7Sminht 				 * The Panasonic drive that we are using is
9455d465cc7Sminht 				 * one such drive.
9465d465cc7Sminht 				 */
9475d465cc7Sminht 				if (device_type == DVD_MINUS) {
9487c478bd9Sstevel@tonic-gate 					if (verbose) {
9497c478bd9Sstevel@tonic-gate 						(void) printf(
9507c478bd9Sstevel@tonic-gate 						    "skipping finalizing\n");
9517c478bd9Sstevel@tonic-gate 					}
9527c478bd9Sstevel@tonic-gate 				} else {
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 			/* l10n_NOTE : 'failed' as in finishing up...failed  */
9557c478bd9Sstevel@tonic-gate 					(void) printf(gettext("failed.\n"));
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 					err_msg(gettext(
9587c478bd9Sstevel@tonic-gate 					    "Could not finalize the disc.\n"));
9597c478bd9Sstevel@tonic-gate 					exit(1);
9607c478bd9Sstevel@tonic-gate 				}
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 			}
9647c478bd9Sstevel@tonic-gate 		}
9657c478bd9Sstevel@tonic-gate 		if (vol_running) {
9667c478bd9Sstevel@tonic-gate 			(void) eject_media(target);
9677c478bd9Sstevel@tonic-gate 		}
9687c478bd9Sstevel@tonic-gate 	} else if (check_device(target, CHECK_MEDIA_IS_NOT_BLANK)) {
9697c478bd9Sstevel@tonic-gate 		/*
9707c478bd9Sstevel@tonic-gate 		 * Some drives such as the pioneer A04 will retain a
9717c478bd9Sstevel@tonic-gate 		 * ghost TOC after a simulation write is done. The
9727c478bd9Sstevel@tonic-gate 		 * media will actually be blank, but the drive will
9737c478bd9Sstevel@tonic-gate 		 * report a TOC. There is currently no other way to
9747c478bd9Sstevel@tonic-gate 		 * re-initialize the media other than ejecting or
9757c478bd9Sstevel@tonic-gate 		 * to ask the drive to clear the leadout. The laser
9767c478bd9Sstevel@tonic-gate 		 * is currently off so nothing is written to the
9777c478bd9Sstevel@tonic-gate 		 * media (on a good behaving drive).
9787c478bd9Sstevel@tonic-gate 		 * NOTE that a device reset does not work to make
9797c478bd9Sstevel@tonic-gate 		 * the drive re-initialize the media.
9807c478bd9Sstevel@tonic-gate 		 */
9817c478bd9Sstevel@tonic-gate 
982a2b4fdf6Srameshc 		blanking_type = "clear_ghost";
9837c478bd9Sstevel@tonic-gate 		blank();
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : 'done' as in "Finishing up...done"  */
9877c478bd9Sstevel@tonic-gate 	(void) printf(gettext("done.\n"));
9887c478bd9Sstevel@tonic-gate }
989